Merge tag 'soc-ep93xx-dt-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / drivers / input / touchscreen / ili210x.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
c6ac8f0b 2#include <linux/crc-ccitt.h>
71f8e38a
DT
3#include <linux/delay.h>
4#include <linux/gpio/consumer.h>
5c6a7a62 5#include <linux/i2c.h>
c6ac8f0b 6#include <linux/ihex.h>
5c6a7a62
OS
7#include <linux/input.h>
8#include <linux/input/mt.h>
f67cc3e9 9#include <linux/input/touchscreen.h>
71f8e38a 10#include <linux/interrupt.h>
dbce1a7d 11#include <linux/mod_devicetable.h>
71f8e38a 12#include <linux/module.h>
b32fbeae 13#include <linux/sizes.h>
71f8e38a 14#include <linux/slab.h>
e3559442 15#include <asm/unaligned.h>
5c6a7a62 16
27931d38 17#define ILI2XXX_POLL_PERIOD 15
5c6a7a62 18
ef536abd
DT
19#define ILI210X_DATA_SIZE 64
20#define ILI211X_DATA_SIZE 43
21#define ILI251X_DATA_SIZE1 31
22#define ILI251X_DATA_SIZE2 20
23
5c6a7a62
OS
24/* Touchscreen commands */
25#define REG_TOUCHDATA 0x10
26#define REG_PANEL_INFO 0x20
70a7681d
MV
27#define REG_FIRMWARE_VERSION 0x40
28#define REG_PROTOCOL_VERSION 0x42
29#define REG_KERNEL_VERSION 0x61
c6ac8f0b
MV
30#define REG_IC_BUSY 0x80
31#define REG_IC_BUSY_NOT_BUSY 0x50
70a7681d
MV
32#define REG_GET_MODE 0xc0
33#define REG_GET_MODE_AP 0x5a
34#define REG_GET_MODE_BL 0x55
c6ac8f0b
MV
35#define REG_SET_MODE_AP 0xc1
36#define REG_SET_MODE_BL 0xc2
37#define REG_WRITE_DATA 0xc3
38#define REG_WRITE_ENABLE 0xc4
39#define REG_READ_DATA_CRC 0xc7
5c6a7a62
OS
40#define REG_CALIBRATE 0xcc
41
c6ac8f0b
MV
42#define ILI251X_FW_FILENAME "ilitek/ili251x.bin"
43
ef536abd
DT
44struct ili2xxx_chip {
45 int (*read_reg)(struct i2c_client *client, u8 reg,
46 void *buf, size_t len);
47 int (*get_touch_data)(struct i2c_client *client, u8 *data);
48 bool (*parse_touch_data)(const u8 *data, unsigned int finger,
60159e9e
MV
49 unsigned int *x, unsigned int *y,
50 unsigned int *z);
ef536abd
DT
51 bool (*continue_polling)(const u8 *data, bool touch);
52 unsigned int max_touches;
b32fbeae 53 unsigned int resolution;
cc12ba18 54 bool has_calibrate_reg;
235300ed 55 bool has_firmware_proto;
60159e9e 56 bool has_pressure_reg;
49588917
MV
57};
58
5c6a7a62
OS
59struct ili210x {
60 struct i2c_client *client;
61 struct input_dev *input;
201f3c80 62 struct gpio_desc *reset_gpio;
f67cc3e9 63 struct touchscreen_properties prop;
ef536abd 64 const struct ili2xxx_chip *chip;
70a7681d
MV
65 u8 version_firmware[8];
66 u8 version_kernel[5];
67 u8 version_proto[2];
68 u8 ic_mode[2];
71f8e38a 69 bool stop;
5c6a7a62
OS
70};
71
ef536abd
DT
72static int ili210x_read_reg(struct i2c_client *client,
73 u8 reg, void *buf, size_t len)
5c6a7a62 74{
ef536abd 75 struct i2c_msg msg[] = {
5c6a7a62
OS
76 {
77 .addr = client->addr,
78 .flags = 0,
79 .len = 1,
80 .buf = &reg,
81 },
82 {
83 .addr = client->addr,
84 .flags = I2C_M_RD,
85 .len = len,
86 .buf = buf,
87 }
88 };
ef536abd 89 int error, ret;
5c6a7a62 90
ef536abd
DT
91 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
92 if (ret != ARRAY_SIZE(msg)) {
93 error = ret < 0 ? ret : -EIO;
94 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
95 return error;
49588917
MV
96 }
97
98 return 0;
99}
100
ef536abd 101static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
49588917 102{
ef536abd
DT
103 return ili210x_read_reg(client, REG_TOUCHDATA,
104 data, ILI210X_DATA_SIZE);
5c6a7a62
OS
105}
106
ef536abd 107static bool ili210x_touchdata_to_coords(const u8 *touchdata,
e3559442 108 unsigned int finger,
60159e9e
MV
109 unsigned int *x, unsigned int *y,
110 unsigned int *z)
e3559442 111{
ac05a8a9 112 if (!(touchdata[0] & BIT(finger)))
e3559442
MV
113 return false;
114
115 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
116 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
117
118 return true;
119}
120
ef536abd
DT
121static bool ili210x_check_continue_polling(const u8 *data, bool touch)
122{
123 return data[0] & 0xf3;
124}
125
126static const struct ili2xxx_chip ili210x_chip = {
127 .read_reg = ili210x_read_reg,
128 .get_touch_data = ili210x_read_touch_data,
129 .parse_touch_data = ili210x_touchdata_to_coords,
130 .continue_polling = ili210x_check_continue_polling,
131 .max_touches = 2,
cc12ba18 132 .has_calibrate_reg = true,
ef536abd
DT
133};
134
135static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
136{
137 s16 sum = 0;
138 int error;
139 int ret;
140 int i;
141
142 ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE);
143 if (ret != ILI211X_DATA_SIZE) {
144 error = ret < 0 ? ret : -EIO;
145 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
146 return error;
147 }
148
149 /* This chip uses custom checksum at the end of data */
150 for (i = 0; i < ILI211X_DATA_SIZE - 1; i++)
151 sum = (sum + data[i]) & 0xff;
152
153 if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) {
154 dev_err(&client->dev,
155 "CRC error (crc=0x%02x expected=0x%02x)\n",
156 sum, data[ILI211X_DATA_SIZE - 1]);
157 return -EIO;
158 }
159
160 return 0;
161}
162
163static bool ili211x_touchdata_to_coords(const u8 *touchdata,
eb91ecc9 164 unsigned int finger,
60159e9e
MV
165 unsigned int *x, unsigned int *y,
166 unsigned int *z)
eb91ecc9
MV
167{
168 u32 data;
169
eb91ecc9
MV
170 data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
171 if (data == 0xffffffff) /* Finger up */
172 return false;
173
174 *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) |
175 touchdata[1 + (finger * 4) + 1];
176 *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) |
177 touchdata[1 + (finger * 4) + 2];
178
179 return true;
180}
181
ef536abd
DT
182static bool ili211x_decline_polling(const u8 *data, bool touch)
183{
184 return false;
185}
186
187static const struct ili2xxx_chip ili211x_chip = {
188 .read_reg = ili210x_read_reg,
189 .get_touch_data = ili211x_read_touch_data,
190 .parse_touch_data = ili211x_touchdata_to_coords,
191 .continue_polling = ili211x_decline_polling,
192 .max_touches = 10,
b32fbeae 193 .resolution = 2048,
ef536abd
DT
194};
195
d0c5e7d4
LW
196static bool ili212x_touchdata_to_coords(const u8 *touchdata,
197 unsigned int finger,
60159e9e
MV
198 unsigned int *x, unsigned int *y,
199 unsigned int *z)
d0c5e7d4
LW
200{
201 u16 val;
202
203 val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0);
204 if (!(val & BIT(15))) /* Touch indication */
205 return false;
206
207 *x = val & 0x3fff;
208 *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2);
209
210 return true;
211}
212
213static bool ili212x_check_continue_polling(const u8 *data, bool touch)
214{
215 return touch;
216}
217
218static const struct ili2xxx_chip ili212x_chip = {
219 .read_reg = ili210x_read_reg,
220 .get_touch_data = ili210x_read_touch_data,
221 .parse_touch_data = ili212x_touchdata_to_coords,
222 .continue_polling = ili212x_check_continue_polling,
223 .max_touches = 10,
224 .has_calibrate_reg = true,
225};
226
de889108
MV
227static int ili251x_read_reg_common(struct i2c_client *client,
228 u8 reg, void *buf, size_t len,
229 unsigned int delay)
ef536abd
DT
230{
231 int error;
232 int ret;
233
234 ret = i2c_master_send(client, &reg, 1);
235 if (ret == 1) {
de889108
MV
236 if (delay)
237 usleep_range(delay, delay + 500);
ef536abd
DT
238
239 ret = i2c_master_recv(client, buf, len);
240 if (ret == len)
241 return 0;
242 }
243
244 error = ret < 0 ? ret : -EIO;
245 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
246 return ret;
247}
248
de889108
MV
249static int ili251x_read_reg(struct i2c_client *client,
250 u8 reg, void *buf, size_t len)
251{
252 return ili251x_read_reg_common(client, reg, buf, len, 5000);
253}
254
ef536abd
DT
255static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
256{
257 int error;
258
de889108
MV
259 error = ili251x_read_reg_common(client, REG_TOUCHDATA,
260 data, ILI251X_DATA_SIZE1, 0);
ef536abd
DT
261 if (!error && data[0] == 2) {
262 error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1,
263 ILI251X_DATA_SIZE2);
9f0fad03
JK
264 if (error >= 0)
265 error = error == ILI251X_DATA_SIZE2 ? 0 : -EIO;
ef536abd
DT
266 }
267
268 return error;
269}
270
271static bool ili251x_touchdata_to_coords(const u8 *touchdata,
49588917 272 unsigned int finger,
60159e9e
MV
273 unsigned int *x, unsigned int *y,
274 unsigned int *z)
49588917 275{
ef536abd 276 u16 val;
49588917 277
ef536abd
DT
278 val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
279 if (!(val & BIT(15))) /* Touch indication */
49588917
MV
280 return false;
281
ef536abd 282 *x = val & 0x3fff;
49588917 283 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
60159e9e 284 *z = touchdata[1 + (finger * 5) + 4];
49588917
MV
285
286 return true;
287}
288
ef536abd
DT
289static bool ili251x_check_continue_polling(const u8 *data, bool touch)
290{
291 return touch;
292}
293
294static const struct ili2xxx_chip ili251x_chip = {
295 .read_reg = ili251x_read_reg,
296 .get_touch_data = ili251x_read_touch_data,
297 .parse_touch_data = ili251x_touchdata_to_coords,
298 .continue_polling = ili251x_check_continue_polling,
299 .max_touches = 10,
cc12ba18 300 .has_calibrate_reg = true,
235300ed 301 .has_firmware_proto = true,
60159e9e 302 .has_pressure_reg = true,
ef536abd
DT
303};
304
e3559442 305static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
5c6a7a62 306{
e3559442 307 struct input_dev *input = priv->input;
5c6a7a62 308 int i;
ef536abd 309 bool contact = false, touch;
60159e9e 310 unsigned int x = 0, y = 0, z = 0;
5c6a7a62 311
ef536abd 312 for (i = 0; i < priv->chip->max_touches; i++) {
60159e9e 313 touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z);
49588917 314
f67cc3e9 315 input_mt_slot(input, i);
ef536abd
DT
316 if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) {
317 touchscreen_report_pos(input, &priv->prop, x, y, true);
60159e9e
MV
318 if (priv->chip->has_pressure_reg)
319 input_report_abs(input, ABS_MT_PRESSURE, z);
ef536abd
DT
320 contact = true;
321 }
5c6a7a62
OS
322 }
323
324 input_mt_report_pointer_emulation(input, false);
325 input_sync(input);
e3559442 326
49588917 327 return contact;
5c6a7a62
OS
328}
329
71f8e38a 330static irqreturn_t ili210x_irq(int irq, void *irq_data)
5c6a7a62 331{
71f8e38a 332 struct ili210x *priv = irq_data;
5c6a7a62 333 struct i2c_client *client = priv->client;
ef536abd
DT
334 const struct ili2xxx_chip *chip = priv->chip;
335 u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
336 bool keep_polling;
8639e042
MV
337 ktime_t time_next;
338 s64 time_delta;
e3559442 339 bool touch;
71f8e38a
DT
340 int error;
341
342 do {
8639e042 343 time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
ef536abd 344 error = chip->get_touch_data(client, touchdata);
71f8e38a
DT
345 if (error) {
346 dev_err(&client->dev,
ef536abd 347 "Unable to get touch data: %d\n", error);
71f8e38a
DT
348 break;
349 }
5c6a7a62 350
71f8e38a 351 touch = ili210x_report_events(priv, touchdata);
ef536abd 352 keep_polling = chip->continue_polling(touchdata, touch);
8639e042
MV
353 if (keep_polling) {
354 time_delta = ktime_us_delta(time_next, ktime_get());
355 if (time_delta > 0)
356 usleep_range(time_delta, time_delta + 1000);
357 }
ef536abd 358 } while (!priv->stop && keep_polling);
5c6a7a62
OS
359
360 return IRQ_HANDLED;
361}
362
235300ed
MV
363static int ili251x_firmware_update_resolution(struct device *dev)
364{
365 struct i2c_client *client = to_i2c_client(dev);
366 struct ili210x *priv = i2c_get_clientdata(client);
367 u16 resx, resy;
368 u8 rs[10];
369 int error;
370
371 /* The firmware update blob might have changed the resolution. */
372 error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs));
e1141b0c
MV
373 if (!error) {
374 resx = le16_to_cpup((__le16 *)rs);
375 resy = le16_to_cpup((__le16 *)(rs + 2));
235300ed 376
e1141b0c
MV
377 /* The value reported by the firmware is invalid. */
378 if (!resx || resx == 0xffff || !resy || resy == 0xffff)
379 error = -EINVAL;
380 }
235300ed 381
e1141b0c
MV
382 /*
383 * In case of error, the firmware might be stuck in bootloader mode,
384 * e.g. after a failed firmware update. Set maximum resolution, but
385 * do not fail to probe, so the user can re-trigger the firmware
386 * update and recover the touch controller.
387 */
388 if (error) {
389 dev_warn(dev, "Invalid resolution reported by controller.\n");
390 resx = 16384;
391 resy = 16384;
392 }
235300ed
MV
393
394 input_abs_set_max(priv->input, ABS_X, resx - 1);
395 input_abs_set_max(priv->input, ABS_Y, resy - 1);
396 input_abs_set_max(priv->input, ABS_MT_POSITION_X, resx - 1);
397 input_abs_set_max(priv->input, ABS_MT_POSITION_Y, resy - 1);
398
e1141b0c 399 return error;
235300ed
MV
400}
401
70a7681d
MV
402static ssize_t ili251x_firmware_update_firmware_version(struct device *dev)
403{
404 struct i2c_client *client = to_i2c_client(dev);
405 struct ili210x *priv = i2c_get_clientdata(client);
406 int error;
407 u8 fw[8];
408
409 /* Get firmware version */
410 error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION,
411 &fw, sizeof(fw));
412 if (!error)
413 memcpy(priv->version_firmware, fw, sizeof(fw));
414
415 return error;
416}
417
418static ssize_t ili251x_firmware_update_kernel_version(struct device *dev)
419{
420 struct i2c_client *client = to_i2c_client(dev);
421 struct ili210x *priv = i2c_get_clientdata(client);
422 int error;
423 u8 kv[5];
424
425 /* Get kernel version */
426 error = priv->chip->read_reg(client, REG_KERNEL_VERSION,
427 &kv, sizeof(kv));
428 if (!error)
429 memcpy(priv->version_kernel, kv, sizeof(kv));
430
431 return error;
432}
433
434static ssize_t ili251x_firmware_update_protocol_version(struct device *dev)
435{
436 struct i2c_client *client = to_i2c_client(dev);
437 struct ili210x *priv = i2c_get_clientdata(client);
438 int error;
439 u8 pv[2];
440
441 /* Get protocol version */
442 error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION,
443 &pv, sizeof(pv));
444 if (!error)
445 memcpy(priv->version_proto, pv, sizeof(pv));
446
447 return error;
448}
449
450static ssize_t ili251x_firmware_update_ic_mode(struct device *dev)
451{
452 struct i2c_client *client = to_i2c_client(dev);
453 struct ili210x *priv = i2c_get_clientdata(client);
454 int error;
455 u8 md[2];
456
457 /* Get chip boot mode */
458 error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md));
459 if (!error)
460 memcpy(priv->ic_mode, md, sizeof(md));
461
462 return error;
463}
464
235300ed
MV
465static int ili251x_firmware_update_cached_state(struct device *dev)
466{
467 struct i2c_client *client = to_i2c_client(dev);
468 struct ili210x *priv = i2c_get_clientdata(client);
469 int error;
470
471 if (!priv->chip->has_firmware_proto)
472 return 0;
473
474 /* Wait for firmware to boot and stabilize itself. */
475 msleep(200);
476
477 /* Firmware does report valid information. */
478 error = ili251x_firmware_update_resolution(dev);
479 if (error)
480 return error;
481
70a7681d
MV
482 error = ili251x_firmware_update_firmware_version(dev);
483 if (error)
484 return error;
485
486 error = ili251x_firmware_update_kernel_version(dev);
487 if (error)
488 return error;
489
490 error = ili251x_firmware_update_protocol_version(dev);
491 if (error)
492 return error;
493
494 error = ili251x_firmware_update_ic_mode(dev);
495 if (error)
496 return error;
497
235300ed
MV
498 return 0;
499}
500
70a7681d
MV
501static ssize_t ili251x_firmware_version_show(struct device *dev,
502 struct device_attribute *attr,
503 char *buf)
504{
505 struct i2c_client *client = to_i2c_client(dev);
506 struct ili210x *priv = i2c_get_clientdata(client);
507 u8 *fw = priv->version_firmware;
508
509 return sysfs_emit(buf, "%02x%02x.%02x%02x.%02x%02x.%02x%02x\n",
510 fw[0], fw[1], fw[2], fw[3],
511 fw[4], fw[5], fw[6], fw[7]);
512}
513static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL);
514
515static ssize_t ili251x_kernel_version_show(struct device *dev,
516 struct device_attribute *attr,
517 char *buf)
518{
519 struct i2c_client *client = to_i2c_client(dev);
520 struct ili210x *priv = i2c_get_clientdata(client);
521 u8 *kv = priv->version_kernel;
522
523 return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n",
524 kv[0], kv[1], kv[2], kv[3], kv[4]);
525}
526static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL);
527
528static ssize_t ili251x_protocol_version_show(struct device *dev,
529 struct device_attribute *attr,
530 char *buf)
531{
532 struct i2c_client *client = to_i2c_client(dev);
533 struct ili210x *priv = i2c_get_clientdata(client);
534 u8 *pv = priv->version_proto;
535
536 return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]);
537}
538static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL);
539
540static ssize_t ili251x_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
543 struct i2c_client *client = to_i2c_client(dev);
544 struct ili210x *priv = i2c_get_clientdata(client);
545 u8 *md = priv->ic_mode;
546 char *mode = "AP";
547
548 if (md[0] == REG_GET_MODE_AP) /* Application Mode */
549 mode = "AP";
550 else if (md[0] == REG_GET_MODE_BL) /* BootLoader Mode */
551 mode = "BL";
552 else /* Unknown Mode */
553 mode = "??";
554
555 return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode);
556}
557static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL);
558
5c6a7a62
OS
559static ssize_t ili210x_calibrate(struct device *dev,
560 struct device_attribute *attr,
561 const char *buf, size_t count)
562{
563 struct i2c_client *client = to_i2c_client(dev);
564 struct ili210x *priv = i2c_get_clientdata(client);
565 unsigned long calibrate;
566 int rc;
567 u8 cmd = REG_CALIBRATE;
568
569 if (kstrtoul(buf, 10, &calibrate))
570 return -EINVAL;
571
572 if (calibrate > 1)
573 return -EINVAL;
574
575 if (calibrate) {
576 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
577 if (rc != sizeof(cmd))
578 return -EIO;
579 }
580
581 return count;
582}
b27c0d0c 583static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
5c6a7a62 584
ac7e0839
DT
585static const u8 *ili251x_firmware_to_buffer(const struct firmware *fw,
586 u16 *ac_end, u16 *df_end)
c6ac8f0b
MV
587{
588 const struct ihex_binrec *rec;
589 u32 fw_addr, fw_last_addr = 0;
590 u16 fw_len;
c6ac8f0b
MV
591
592 /*
593 * The firmware ihex blob can never be bigger than 64 kiB, so make this
594 * simple -- allocate a 64 kiB buffer, iterate over the ihex blob records
595 * once, copy them all into this buffer at the right locations, and then
596 * do all operations on this linear buffer.
597 */
ac7e0839 598 u8* fw_buf __free(kvfree) = kvmalloc(SZ_64K, GFP_KERNEL);
c6ac8f0b 599 if (!fw_buf)
ac7e0839 600 return ERR_PTR(-ENOMEM);
c6ac8f0b
MV
601
602 rec = (const struct ihex_binrec *)fw->data;
603 while (rec) {
604 fw_addr = be32_to_cpu(rec->addr);
605 fw_len = be16_to_cpu(rec->len);
606
607 /* The last 32 Byte firmware block can be 0xffe0 */
ac7e0839
DT
608 if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32)
609 return ERR_PTR(-EFBIG);
c6ac8f0b
MV
610
611 /* Find the last address before DF start address, that is AC end */
612 if (fw_addr == 0xf000)
613 *ac_end = fw_last_addr;
614 fw_last_addr = fw_addr + fw_len;
615
616 memcpy(fw_buf + fw_addr, rec->data, fw_len);
617 rec = ihex_next_binrec(rec);
618 }
619
620 /* DF end address is the last address in the firmware blob */
621 *df_end = fw_addr + fw_len;
c6ac8f0b 622
ac7e0839 623 return_ptr(fw_buf);
c6ac8f0b
MV
624}
625
626/* Switch mode between Application and BootLoader */
627static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode)
628{
629 struct ili210x *priv = i2c_get_clientdata(client);
630 u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 };
631 u8 md[2];
632 int error;
633
634 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
635 if (error)
636 return error;
637 /* Mode already set */
638 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
639 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
640 return 0;
641
642 /* Unlock writes */
643 error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren));
644 if (error != sizeof(cmd_wren))
645 return -EINVAL;
646
647 mdelay(20);
648
649 /* Select mode (BootLoader or Application) */
650 error = i2c_master_send(client, &cmd_mode, 1);
651 if (error != 1)
652 return -EINVAL;
653
654 mdelay(200); /* Reboot into bootloader takes a lot of time ... */
655
656 /* Read back mode */
657 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
658 if (error)
659 return error;
660 /* Check if mode is correct now. */
661 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
662 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
663 return 0;
664
665 return -EINVAL;
666}
667
668static int ili251x_firmware_busy(struct i2c_client *client)
669{
670 struct ili210x *priv = i2c_get_clientdata(client);
671 int error, i = 0;
672 u8 data;
673
674 do {
675 /* The read_reg already contains suitable delay */
676 error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1);
677 if (error)
678 return error;
679 if (i++ == 100000)
680 return -ETIMEDOUT;
681 } while (data != REG_IC_BUSY_NOT_BUSY);
682
683 return 0;
684}
685
ac7e0839 686static int ili251x_firmware_write_to_ic(struct device *dev, const u8 *fwbuf,
c6ac8f0b
MV
687 u16 start, u16 end, u8 dataflash)
688{
689 struct i2c_client *client = to_i2c_client(dev);
690 struct ili210x *priv = i2c_get_clientdata(client);
691 u8 cmd_crc = REG_READ_DATA_CRC;
692 u8 crcrb[4] = { 0 };
693 u8 fw_data[33];
694 u16 fw_addr;
695 int error;
696
697 /*
698 * The DF (dataflash) needs 2 bytes offset for unknown reasons,
699 * the AC (application) has 2 bytes CRC16-CCITT at the end.
700 */
701 u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0),
702 end - start - 2);
703
704 /* Unlock write to either AC (application) or DF (dataflash) area */
705 u8 cmd_wr[10] = {
706 REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash,
707 (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff,
708 (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff
709 };
710
711 error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr));
712 if (error != sizeof(cmd_wr))
713 return -EINVAL;
714
715 error = ili251x_firmware_busy(client);
716 if (error)
717 return error;
718
719 for (fw_addr = start; fw_addr < end; fw_addr += 32) {
720 fw_data[0] = REG_WRITE_DATA;
721 memcpy(&(fw_data[1]), fwbuf + fw_addr, 32);
722 error = i2c_master_send(client, fw_data, 33);
723 if (error != sizeof(fw_data))
724 return error;
725 error = ili251x_firmware_busy(client);
726 if (error)
727 return error;
728 }
729
730 error = i2c_master_send(client, &cmd_crc, 1);
731 if (error != 1)
732 return -EINVAL;
733
734 error = ili251x_firmware_busy(client);
735 if (error)
736 return error;
737
738 error = priv->chip->read_reg(client, REG_READ_DATA_CRC,
739 &crcrb, sizeof(crcrb));
740 if (error)
741 return error;
742
743 /* Check CRC readback */
744 if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff))
745 return -EINVAL;
746
747 return 0;
748}
749
750static int ili251x_firmware_reset(struct i2c_client *client)
751{
752 u8 cmd_reset[2] = { 0xf2, 0x01 };
753 int error;
754
755 error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset));
756 if (error != sizeof(cmd_reset))
757 return -EINVAL;
758
759 return ili251x_firmware_busy(client);
760}
761
b26ff913 762static void ili210x_hardware_reset(struct gpio_desc *reset_gpio)
c6ac8f0b 763{
c6ac8f0b 764 /* Reset the controller */
b26ff913
MV
765 gpiod_set_value_cansleep(reset_gpio, 1);
766 usleep_range(12000, 15000);
767 gpiod_set_value_cansleep(reset_gpio, 0);
c6ac8f0b
MV
768 msleep(300);
769}
770
ac7e0839
DT
771static int ili210x_do_firmware_update(struct ili210x *priv,
772 const u8 *fwbuf, u16 ac_end, u16 df_end)
c6ac8f0b 773{
ac7e0839
DT
774 struct i2c_client *client = priv->client;
775 struct device *dev = &client->dev;
c6ac8f0b
MV
776 int error;
777 int i;
778
c6ac8f0b
MV
779 error = ili251x_firmware_reset(client);
780 if (error)
ac7e0839 781 return error;
c6ac8f0b
MV
782
783 /* This may not succeed on first try, so re-try a few times. */
784 for (i = 0; i < 5; i++) {
785 error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL);
786 if (!error)
787 break;
788 }
789
790 if (error)
ac7e0839 791 return error;
c6ac8f0b
MV
792
793 dev_dbg(dev, "IC is now in BootLoader mode\n");
794
795 msleep(200); /* The bootloader seems to need some time too. */
796
797 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1);
798 if (error) {
799 dev_err(dev, "DF firmware update failed, error=%d\n", error);
ac7e0839 800 return error;
c6ac8f0b
MV
801 }
802
803 dev_dbg(dev, "DataFlash firmware written\n");
804
805 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0);
806 if (error) {
807 dev_err(dev, "AC firmware update failed, error=%d\n", error);
ac7e0839 808 return error;
c6ac8f0b
MV
809 }
810
811 dev_dbg(dev, "Application firmware written\n");
812
813 /* This may not succeed on first try, so re-try a few times. */
814 for (i = 0; i < 5; i++) {
815 error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP);
816 if (!error)
817 break;
818 }
819
820 if (error)
ac7e0839 821 return error;
c6ac8f0b
MV
822
823 dev_dbg(dev, "IC is now in Application mode\n");
824
825 error = ili251x_firmware_update_cached_state(dev);
826 if (error)
ac7e0839 827 return error;
c6ac8f0b 828
ac7e0839
DT
829 return 0;
830}
c6ac8f0b 831
ac7e0839
DT
832static ssize_t ili210x_firmware_update_store(struct device *dev,
833 struct device_attribute *attr,
834 const char *buf, size_t count)
835{
836 struct i2c_client *client = to_i2c_client(dev);
837 struct ili210x *priv = i2c_get_clientdata(client);
838 const char *fwname = ILI251X_FW_FILENAME;
839 u16 ac_end, df_end;
840 int error;
841
842 const struct firmware *fw __free(firmware) = NULL;
843 error = request_ihex_firmware(&fw, fwname, dev);
844 if (error) {
845 dev_err(dev, "Failed to request firmware %s, error=%d\n",
846 fwname, error);
847 return error;
848 }
849
850 const u8* fwbuf __free(kvfree) =
851 ili251x_firmware_to_buffer(fw, &ac_end, &df_end);
852 error = PTR_ERR_OR_ZERO(fwbuf);
853 if (error)
854 return error;
855
856 /*
857 * Disable touchscreen IRQ, so that we would not get spurious touch
858 * interrupt during firmware update, and so that the IRQ handler won't
859 * trigger and interfere with the firmware update. There is no bit in
860 * the touch controller to disable the IRQs during update, so we have
861 * to do it this way here.
862 */
7c459517
DT
863 scoped_guard(disable_irq, &client->irq) {
864 dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname);
ac7e0839 865
7c459517 866 ili210x_hardware_reset(priv->reset_gpio);
ac7e0839 867
7c459517 868 error = ili210x_do_firmware_update(priv, fwbuf, ac_end, df_end);
ac7e0839 869
7c459517 870 ili210x_hardware_reset(priv->reset_gpio);
c6ac8f0b 871
7c459517
DT
872 dev_dbg(dev, "Firmware update ended, error=%i\n", error);
873 }
ac7e0839
DT
874
875 return error ?: count;
c6ac8f0b
MV
876}
877
878static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store);
879
6a539138 880static struct attribute *ili210x_attrs[] = {
5c6a7a62 881 &dev_attr_calibrate.attr,
c6ac8f0b 882 &dev_attr_firmware_update.attr,
70a7681d
MV
883 &dev_attr_firmware_version.attr,
884 &dev_attr_kernel_version.attr,
885 &dev_attr_protocol_version.attr,
886 &dev_attr_mode.attr,
5c6a7a62
OS
887 NULL,
888};
889
70a7681d 890static umode_t ili210x_attributes_visible(struct kobject *kobj,
cc12ba18
SVA
891 struct attribute *attr, int index)
892{
893 struct device *dev = kobj_to_dev(kobj);
894 struct i2c_client *client = to_i2c_client(dev);
895 struct ili210x *priv = i2c_get_clientdata(client);
896
70a7681d
MV
897 /* Calibrate is present on all ILI2xxx which have calibrate register */
898 if (attr == &dev_attr_calibrate.attr)
899 return priv->chip->has_calibrate_reg ? attr->mode : 0;
900
901 /* Firmware/Kernel/Protocol/BootMode is implememted only for ILI251x */
902 if (!priv->chip->has_firmware_proto)
903 return 0;
904
905 return attr->mode;
cc12ba18
SVA
906}
907
6a539138
DT
908static const struct attribute_group ili210x_group = {
909 .attrs = ili210x_attrs,
70a7681d 910 .is_visible = ili210x_attributes_visible,
5c6a7a62 911};
6a539138 912__ATTRIBUTE_GROUPS(ili210x);
5c6a7a62 913
201f3c80
MV
914static void ili210x_power_down(void *data)
915{
916 struct gpio_desc *reset_gpio = data;
917
918 gpiod_set_value_cansleep(reset_gpio, 1);
919}
920
71f8e38a 921static void ili210x_stop(void *data)
1bdec5d9
MV
922{
923 struct ili210x *priv = data;
924
71f8e38a
DT
925 /* Tell ISR to quit even if there is a contact. */
926 priv->stop = true;
1bdec5d9
MV
927}
928
497a2c8e 929static int ili210x_i2c_probe(struct i2c_client *client)
5c6a7a62 930{
497a2c8e 931 const struct i2c_device_id *id = i2c_client_get_device_id(client);
5c6a7a62 932 struct device *dev = &client->dev;
ef536abd 933 const struct ili2xxx_chip *chip;
5c6a7a62 934 struct ili210x *priv;
201f3c80 935 struct gpio_desc *reset_gpio;
5c6a7a62 936 struct input_dev *input;
5c6a7a62 937 int error;
b32fbeae 938 unsigned int max_xy;
5c6a7a62
OS
939
940 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
941
ef536abd
DT
942 chip = device_get_match_data(dev);
943 if (!chip && id)
944 chip = (const struct ili2xxx_chip *)id->driver_data;
945 if (!chip) {
946 dev_err(&client->dev, "unknown device model\n");
947 return -ENODEV;
948 }
949
5c6a7a62
OS
950 if (client->irq <= 0) {
951 dev_err(dev, "No IRQ!\n");
952 return -EINVAL;
953 }
954
201f3c80
MV
955 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
956 if (IS_ERR(reset_gpio))
957 return PTR_ERR(reset_gpio);
958
959 if (reset_gpio) {
960 error = devm_add_action_or_reset(dev, ili210x_power_down,
961 reset_gpio);
962 if (error)
963 return error;
964
b26ff913 965 ili210x_hardware_reset(reset_gpio);
201f3c80
MV
966 }
967
12294577
MV
968 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
969 if (!priv)
970 return -ENOMEM;
971
972 input = devm_input_allocate_device(dev);
973 if (!input)
974 return -ENOMEM;
975
976 priv->client = client;
977 priv->input = input;
12294577 978 priv->reset_gpio = reset_gpio;
ef536abd 979 priv->chip = chip;
12294577
MV
980 i2c_set_clientdata(client, priv);
981
5c6a7a62
OS
982 /* Setup input device */
983 input->name = "ILI210x Touchscreen";
984 input->id.bustype = BUS_I2C;
5c6a7a62 985
5c6a7a62 986 /* Multi touch */
b32fbeae
SVA
987 max_xy = (chip->resolution ?: SZ_64K) - 1;
988 input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
989 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
60159e9e
MV
990 if (priv->chip->has_pressure_reg)
991 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0);
235300ed 992 error = ili251x_firmware_update_cached_state(dev);
e1141b0c
MV
993 if (error)
994 dev_warn(dev, "Unable to cache firmware information, err: %d\n",
995 error);
996
f67cc3e9 997 touchscreen_parse_properties(input, true, &priv->prop);
43f06a4c 998
ef536abd
DT
999 error = input_mt_init_slots(input, priv->chip->max_touches,
1000 INPUT_MT_DIRECT);
43f06a4c
DT
1001 if (error) {
1002 dev_err(dev, "Unable to set up slots, err: %d\n", error);
1003 return error;
1004 }
5c6a7a62 1005
71f8e38a
DT
1006 error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
1007 IRQF_ONESHOT, client->name, priv);
5c6a7a62
OS
1008 if (error) {
1009 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
1010 error);
1bdec5d9 1011 return error;
5c6a7a62
OS
1012 }
1013
71f8e38a
DT
1014 error = devm_add_action_or_reset(dev, ili210x_stop, priv);
1015 if (error)
1016 return error;
1017
5c6a7a62
OS
1018 error = input_register_device(priv->input);
1019 if (error) {
971bd8fa 1020 dev_err(dev, "Cannot register input device, err: %d\n", error);
576057bf 1021 return error;
5c6a7a62
OS
1022 }
1023
5c6a7a62 1024 return 0;
5c6a7a62
OS
1025}
1026
5c6a7a62 1027static const struct i2c_device_id ili210x_i2c_id[] = {
ef536abd
DT
1028 { "ili210x", (long)&ili210x_chip },
1029 { "ili2117", (long)&ili211x_chip },
d0c5e7d4 1030 { "ili2120", (long)&ili212x_chip },
ef536abd 1031 { "ili251x", (long)&ili251x_chip },
5c6a7a62
OS
1032 { }
1033};
1034MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
1035
c5d0e4b5 1036static const struct of_device_id ili210x_dt_ids[] = {
ef536abd
DT
1037 { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
1038 { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
d0c5e7d4 1039 { .compatible = "ilitek,ili2120", .data = &ili212x_chip },
ef536abd
DT
1040 { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
1041 { }
c5d0e4b5
MV
1042};
1043MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
1044
5c6a7a62
OS
1045static struct i2c_driver ili210x_ts_driver = {
1046 .driver = {
1047 .name = "ili210x_i2c",
6a539138 1048 .dev_groups = ili210x_groups,
c5d0e4b5 1049 .of_match_table = ili210x_dt_ids,
5c6a7a62
OS
1050 },
1051 .id_table = ili210x_i2c_id,
d8bde56d 1052 .probe = ili210x_i2c_probe,
5c6a7a62
OS
1053};
1054
1055module_i2c_driver(ili210x_ts_driver);
1056
1057MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1058MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
1059MODULE_LICENSE("GPL");