1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/crc-ccitt.h>
3 #include <linux/delay.h>
4 #include <linux/gpio/consumer.h>
6 #include <linux/ihex.h>
7 #include <linux/input.h>
8 #include <linux/input/mt.h>
9 #include <linux/input/touchscreen.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/sizes.h>
14 #include <linux/slab.h>
15 #include <asm/unaligned.h>
17 #define ILI2XXX_POLL_PERIOD 15
19 #define ILI210X_DATA_SIZE 64
20 #define ILI211X_DATA_SIZE 43
21 #define ILI251X_DATA_SIZE1 31
22 #define ILI251X_DATA_SIZE2 20
24 /* Touchscreen commands */
25 #define REG_TOUCHDATA 0x10
26 #define REG_PANEL_INFO 0x20
27 #define REG_FIRMWARE_VERSION 0x40
28 #define REG_PROTOCOL_VERSION 0x42
29 #define REG_KERNEL_VERSION 0x61
30 #define REG_IC_BUSY 0x80
31 #define REG_IC_BUSY_NOT_BUSY 0x50
32 #define REG_GET_MODE 0xc0
33 #define REG_GET_MODE_AP 0x5a
34 #define REG_GET_MODE_BL 0x55
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
40 #define REG_CALIBRATE 0xcc
42 #define ILI251X_FW_FILENAME "ilitek/ili251x.bin"
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,
49 unsigned int *x, unsigned int *y,
51 bool (*continue_polling)(const u8 *data, bool touch);
52 unsigned int max_touches;
53 unsigned int resolution;
54 bool has_calibrate_reg;
55 bool has_firmware_proto;
56 bool has_pressure_reg;
60 struct i2c_client *client;
61 struct input_dev *input;
62 struct gpio_desc *reset_gpio;
63 struct touchscreen_properties prop;
64 const struct ili2xxx_chip *chip;
65 u8 version_firmware[8];
72 static int ili210x_read_reg(struct i2c_client *client,
73 u8 reg, void *buf, size_t len)
75 struct i2c_msg msg[] = {
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);
101 static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
103 return ili210x_read_reg(client, REG_TOUCHDATA,
104 data, ILI210X_DATA_SIZE);
107 static bool ili210x_touchdata_to_coords(const u8 *touchdata,
109 unsigned int *x, unsigned int *y,
112 if (!(touchdata[0] & BIT(finger)))
115 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
116 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
121 static bool ili210x_check_continue_polling(const u8 *data, bool touch)
123 return data[0] & 0xf3;
126 static 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,
132 .has_calibrate_reg = true,
135 static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
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);
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;
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]);
163 static bool ili211x_touchdata_to_coords(const u8 *touchdata,
165 unsigned int *x, unsigned int *y,
170 data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
171 if (data == 0xffffffff) /* Finger up */
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];
182 static bool ili211x_decline_polling(const u8 *data, bool touch)
187 static 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,
196 static bool ili212x_touchdata_to_coords(const u8 *touchdata,
198 unsigned int *x, unsigned int *y,
203 val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0);
204 if (!(val & BIT(15))) /* Touch indication */
208 *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2);
213 static bool ili212x_check_continue_polling(const u8 *data, bool touch)
218 static 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,
224 .has_calibrate_reg = true,
227 static int ili251x_read_reg_common(struct i2c_client *client,
228 u8 reg, void *buf, size_t len,
234 ret = i2c_master_send(client, ®, 1);
237 usleep_range(delay, delay + 500);
239 ret = i2c_master_recv(client, buf, len);
244 error = ret < 0 ? ret : -EIO;
245 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
249 static int ili251x_read_reg(struct i2c_client *client,
250 u8 reg, void *buf, size_t len)
252 return ili251x_read_reg_common(client, reg, buf, len, 5000);
255 static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
259 error = ili251x_read_reg_common(client, REG_TOUCHDATA,
260 data, ILI251X_DATA_SIZE1, 0);
261 if (!error && data[0] == 2) {
262 error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1,
264 if (error >= 0 && error != ILI251X_DATA_SIZE2)
271 static bool ili251x_touchdata_to_coords(const u8 *touchdata,
273 unsigned int *x, unsigned int *y,
278 val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
279 if (!(val & BIT(15))) /* Touch indication */
283 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
284 *z = touchdata[1 + (finger * 5) + 4];
289 static bool ili251x_check_continue_polling(const u8 *data, bool touch)
294 static 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,
300 .has_calibrate_reg = true,
301 .has_firmware_proto = true,
302 .has_pressure_reg = true,
305 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
307 struct input_dev *input = priv->input;
309 bool contact = false, touch;
310 unsigned int x = 0, y = 0, z = 0;
312 for (i = 0; i < priv->chip->max_touches; i++) {
313 touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z);
315 input_mt_slot(input, i);
316 if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) {
317 touchscreen_report_pos(input, &priv->prop, x, y, true);
318 if (priv->chip->has_pressure_reg)
319 input_report_abs(input, ABS_MT_PRESSURE, z);
324 input_mt_report_pointer_emulation(input, false);
330 static irqreturn_t ili210x_irq(int irq, void *irq_data)
332 struct ili210x *priv = irq_data;
333 struct i2c_client *client = priv->client;
334 const struct ili2xxx_chip *chip = priv->chip;
335 u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
343 time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
344 error = chip->get_touch_data(client, touchdata);
346 dev_err(&client->dev,
347 "Unable to get touch data: %d\n", error);
351 touch = ili210x_report_events(priv, touchdata);
352 keep_polling = chip->continue_polling(touchdata, touch);
354 time_delta = ktime_us_delta(time_next, ktime_get());
356 usleep_range(time_delta, time_delta + 1000);
358 } while (!priv->stop && keep_polling);
363 static int ili251x_firmware_update_resolution(struct device *dev)
365 struct i2c_client *client = to_i2c_client(dev);
366 struct ili210x *priv = i2c_get_clientdata(client);
371 /* The firmware update blob might have changed the resolution. */
372 error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs));
374 resx = le16_to_cpup((__le16 *)rs);
375 resy = le16_to_cpup((__le16 *)(rs + 2));
377 /* The value reported by the firmware is invalid. */
378 if (!resx || resx == 0xffff || !resy || resy == 0xffff)
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.
389 dev_warn(dev, "Invalid resolution reported by controller.\n");
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);
402 static ssize_t ili251x_firmware_update_firmware_version(struct device *dev)
404 struct i2c_client *client = to_i2c_client(dev);
405 struct ili210x *priv = i2c_get_clientdata(client);
409 /* Get firmware version */
410 error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION,
413 memcpy(priv->version_firmware, fw, sizeof(fw));
418 static ssize_t ili251x_firmware_update_kernel_version(struct device *dev)
420 struct i2c_client *client = to_i2c_client(dev);
421 struct ili210x *priv = i2c_get_clientdata(client);
425 /* Get kernel version */
426 error = priv->chip->read_reg(client, REG_KERNEL_VERSION,
429 memcpy(priv->version_kernel, kv, sizeof(kv));
434 static ssize_t ili251x_firmware_update_protocol_version(struct device *dev)
436 struct i2c_client *client = to_i2c_client(dev);
437 struct ili210x *priv = i2c_get_clientdata(client);
441 /* Get protocol version */
442 error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION,
445 memcpy(priv->version_proto, pv, sizeof(pv));
450 static ssize_t ili251x_firmware_update_ic_mode(struct device *dev)
452 struct i2c_client *client = to_i2c_client(dev);
453 struct ili210x *priv = i2c_get_clientdata(client);
457 /* Get chip boot mode */
458 error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md));
460 memcpy(priv->ic_mode, md, sizeof(md));
465 static int ili251x_firmware_update_cached_state(struct device *dev)
467 struct i2c_client *client = to_i2c_client(dev);
468 struct ili210x *priv = i2c_get_clientdata(client);
471 if (!priv->chip->has_firmware_proto)
474 /* Wait for firmware to boot and stabilize itself. */
477 /* Firmware does report valid information. */
478 error = ili251x_firmware_update_resolution(dev);
482 error = ili251x_firmware_update_firmware_version(dev);
486 error = ili251x_firmware_update_kernel_version(dev);
490 error = ili251x_firmware_update_protocol_version(dev);
494 error = ili251x_firmware_update_ic_mode(dev);
501 static ssize_t ili251x_firmware_version_show(struct device *dev,
502 struct device_attribute *attr,
505 struct i2c_client *client = to_i2c_client(dev);
506 struct ili210x *priv = i2c_get_clientdata(client);
507 u8 *fw = priv->version_firmware;
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]);
513 static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL);
515 static ssize_t ili251x_kernel_version_show(struct device *dev,
516 struct device_attribute *attr,
519 struct i2c_client *client = to_i2c_client(dev);
520 struct ili210x *priv = i2c_get_clientdata(client);
521 u8 *kv = priv->version_kernel;
523 return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n",
524 kv[0], kv[1], kv[2], kv[3], kv[4]);
526 static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL);
528 static ssize_t ili251x_protocol_version_show(struct device *dev,
529 struct device_attribute *attr,
532 struct i2c_client *client = to_i2c_client(dev);
533 struct ili210x *priv = i2c_get_clientdata(client);
534 u8 *pv = priv->version_proto;
536 return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]);
538 static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL);
540 static ssize_t ili251x_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 struct i2c_client *client = to_i2c_client(dev);
544 struct ili210x *priv = i2c_get_clientdata(client);
545 u8 *md = priv->ic_mode;
548 if (md[0] == REG_GET_MODE_AP) /* Application Mode */
550 else if (md[0] == REG_GET_MODE_BL) /* BootLoader Mode */
552 else /* Unknown Mode */
555 return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode);
557 static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL);
559 static ssize_t ili210x_calibrate(struct device *dev,
560 struct device_attribute *attr,
561 const char *buf, size_t count)
563 struct i2c_client *client = to_i2c_client(dev);
564 struct ili210x *priv = i2c_get_clientdata(client);
565 unsigned long calibrate;
567 u8 cmd = REG_CALIBRATE;
569 if (kstrtoul(buf, 10, &calibrate))
576 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
577 if (rc != sizeof(cmd))
583 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
585 static int ili251x_firmware_to_buffer(const struct firmware *fw,
586 u8 **buf, u16 *ac_end, u16 *df_end)
588 const struct ihex_binrec *rec;
589 u32 fw_addr, fw_last_addr = 0;
595 * The firmware ihex blob can never be bigger than 64 kiB, so make this
596 * simple -- allocate a 64 kiB buffer, iterate over the ihex blob records
597 * once, copy them all into this buffer at the right locations, and then
598 * do all operations on this linear buffer.
600 fw_buf = kzalloc(SZ_64K, GFP_KERNEL);
604 rec = (const struct ihex_binrec *)fw->data;
606 fw_addr = be32_to_cpu(rec->addr);
607 fw_len = be16_to_cpu(rec->len);
609 /* The last 32 Byte firmware block can be 0xffe0 */
610 if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32) {
615 /* Find the last address before DF start address, that is AC end */
616 if (fw_addr == 0xf000)
617 *ac_end = fw_last_addr;
618 fw_last_addr = fw_addr + fw_len;
620 memcpy(fw_buf + fw_addr, rec->data, fw_len);
621 rec = ihex_next_binrec(rec);
624 /* DF end address is the last address in the firmware blob */
625 *df_end = fw_addr + fw_len;
634 /* Switch mode between Application and BootLoader */
635 static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode)
637 struct ili210x *priv = i2c_get_clientdata(client);
638 u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 };
642 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
645 /* Mode already set */
646 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
647 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
651 error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren));
652 if (error != sizeof(cmd_wren))
657 /* Select mode (BootLoader or Application) */
658 error = i2c_master_send(client, &cmd_mode, 1);
662 mdelay(200); /* Reboot into bootloader takes a lot of time ... */
665 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
668 /* Check if mode is correct now. */
669 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
670 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
676 static int ili251x_firmware_busy(struct i2c_client *client)
678 struct ili210x *priv = i2c_get_clientdata(client);
683 /* The read_reg already contains suitable delay */
684 error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1);
689 } while (data != REG_IC_BUSY_NOT_BUSY);
694 static int ili251x_firmware_write_to_ic(struct device *dev, u8 *fwbuf,
695 u16 start, u16 end, u8 dataflash)
697 struct i2c_client *client = to_i2c_client(dev);
698 struct ili210x *priv = i2c_get_clientdata(client);
699 u8 cmd_crc = REG_READ_DATA_CRC;
706 * The DF (dataflash) needs 2 bytes offset for unknown reasons,
707 * the AC (application) has 2 bytes CRC16-CCITT at the end.
709 u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0),
712 /* Unlock write to either AC (application) or DF (dataflash) area */
714 REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash,
715 (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff,
716 (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff
719 error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr));
720 if (error != sizeof(cmd_wr))
723 error = ili251x_firmware_busy(client);
727 for (fw_addr = start; fw_addr < end; fw_addr += 32) {
728 fw_data[0] = REG_WRITE_DATA;
729 memcpy(&(fw_data[1]), fwbuf + fw_addr, 32);
730 error = i2c_master_send(client, fw_data, 33);
731 if (error != sizeof(fw_data))
733 error = ili251x_firmware_busy(client);
738 error = i2c_master_send(client, &cmd_crc, 1);
742 error = ili251x_firmware_busy(client);
746 error = priv->chip->read_reg(client, REG_READ_DATA_CRC,
747 &crcrb, sizeof(crcrb));
751 /* Check CRC readback */
752 if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff))
758 static int ili251x_firmware_reset(struct i2c_client *client)
760 u8 cmd_reset[2] = { 0xf2, 0x01 };
763 error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset));
764 if (error != sizeof(cmd_reset))
767 return ili251x_firmware_busy(client);
770 static void ili210x_hardware_reset(struct gpio_desc *reset_gpio)
772 /* Reset the controller */
773 gpiod_set_value_cansleep(reset_gpio, 1);
774 usleep_range(12000, 15000);
775 gpiod_set_value_cansleep(reset_gpio, 0);
779 static ssize_t ili210x_firmware_update_store(struct device *dev,
780 struct device_attribute *attr,
781 const char *buf, size_t count)
783 struct i2c_client *client = to_i2c_client(dev);
784 struct ili210x *priv = i2c_get_clientdata(client);
785 const char *fwname = ILI251X_FW_FILENAME;
786 const struct firmware *fw;
792 error = request_ihex_firmware(&fw, fwname, dev);
794 dev_err(dev, "Failed to request firmware %s, error=%d\n",
799 error = ili251x_firmware_to_buffer(fw, &fwbuf, &ac_end, &df_end);
800 release_firmware(fw);
805 * Disable touchscreen IRQ, so that we would not get spurious touch
806 * interrupt during firmware update, and so that the IRQ handler won't
807 * trigger and interfere with the firmware update. There is no bit in
808 * the touch controller to disable the IRQs during update, so we have
809 * to do it this way here.
811 disable_irq(client->irq);
813 dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname);
815 ili210x_hardware_reset(priv->reset_gpio);
817 error = ili251x_firmware_reset(client);
821 /* This may not succeed on first try, so re-try a few times. */
822 for (i = 0; i < 5; i++) {
823 error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL);
831 dev_dbg(dev, "IC is now in BootLoader mode\n");
833 msleep(200); /* The bootloader seems to need some time too. */
835 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1);
837 dev_err(dev, "DF firmware update failed, error=%d\n", error);
841 dev_dbg(dev, "DataFlash firmware written\n");
843 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0);
845 dev_err(dev, "AC firmware update failed, error=%d\n", error);
849 dev_dbg(dev, "Application firmware written\n");
851 /* This may not succeed on first try, so re-try a few times. */
852 for (i = 0; i < 5; i++) {
853 error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP);
861 dev_dbg(dev, "IC is now in Application mode\n");
863 error = ili251x_firmware_update_cached_state(dev);
870 ili210x_hardware_reset(priv->reset_gpio);
871 dev_dbg(dev, "Firmware update ended, error=%i\n", error);
872 enable_irq(client->irq);
877 static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store);
879 static struct attribute *ili210x_attributes[] = {
880 &dev_attr_calibrate.attr,
881 &dev_attr_firmware_update.attr,
882 &dev_attr_firmware_version.attr,
883 &dev_attr_kernel_version.attr,
884 &dev_attr_protocol_version.attr,
889 static umode_t ili210x_attributes_visible(struct kobject *kobj,
890 struct attribute *attr, int index)
892 struct device *dev = kobj_to_dev(kobj);
893 struct i2c_client *client = to_i2c_client(dev);
894 struct ili210x *priv = i2c_get_clientdata(client);
896 /* Calibrate is present on all ILI2xxx which have calibrate register */
897 if (attr == &dev_attr_calibrate.attr)
898 return priv->chip->has_calibrate_reg ? attr->mode : 0;
900 /* Firmware/Kernel/Protocol/BootMode is implememted only for ILI251x */
901 if (!priv->chip->has_firmware_proto)
907 static const struct attribute_group ili210x_attr_group = {
908 .attrs = ili210x_attributes,
909 .is_visible = ili210x_attributes_visible,
912 static void ili210x_power_down(void *data)
914 struct gpio_desc *reset_gpio = data;
916 gpiod_set_value_cansleep(reset_gpio, 1);
919 static void ili210x_stop(void *data)
921 struct ili210x *priv = data;
923 /* Tell ISR to quit even if there is a contact. */
927 static int ili210x_i2c_probe(struct i2c_client *client)
929 const struct i2c_device_id *id = i2c_client_get_device_id(client);
930 struct device *dev = &client->dev;
931 const struct ili2xxx_chip *chip;
932 struct ili210x *priv;
933 struct gpio_desc *reset_gpio;
934 struct input_dev *input;
938 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
940 chip = device_get_match_data(dev);
942 chip = (const struct ili2xxx_chip *)id->driver_data;
944 dev_err(&client->dev, "unknown device model\n");
948 if (client->irq <= 0) {
949 dev_err(dev, "No IRQ!\n");
953 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
954 if (IS_ERR(reset_gpio))
955 return PTR_ERR(reset_gpio);
958 error = devm_add_action_or_reset(dev, ili210x_power_down,
963 ili210x_hardware_reset(reset_gpio);
966 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
970 input = devm_input_allocate_device(dev);
974 priv->client = client;
976 priv->reset_gpio = reset_gpio;
978 i2c_set_clientdata(client, priv);
980 /* Setup input device */
981 input->name = "ILI210x Touchscreen";
982 input->id.bustype = BUS_I2C;
985 max_xy = (chip->resolution ?: SZ_64K) - 1;
986 input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
987 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
988 if (priv->chip->has_pressure_reg)
989 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0);
990 error = ili251x_firmware_update_cached_state(dev);
992 dev_warn(dev, "Unable to cache firmware information, err: %d\n",
995 touchscreen_parse_properties(input, true, &priv->prop);
997 error = input_mt_init_slots(input, priv->chip->max_touches,
1000 dev_err(dev, "Unable to set up slots, err: %d\n", error);
1004 error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
1005 IRQF_ONESHOT, client->name, priv);
1007 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
1012 error = devm_add_action_or_reset(dev, ili210x_stop, priv);
1016 error = devm_device_add_group(dev, &ili210x_attr_group);
1018 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
1023 error = input_register_device(priv->input);
1025 dev_err(dev, "Cannot register input device, err: %d\n", error);
1032 static const struct i2c_device_id ili210x_i2c_id[] = {
1033 { "ili210x", (long)&ili210x_chip },
1034 { "ili2117", (long)&ili211x_chip },
1035 { "ili2120", (long)&ili212x_chip },
1036 { "ili251x", (long)&ili251x_chip },
1039 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
1041 static const struct of_device_id ili210x_dt_ids[] = {
1042 { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
1043 { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
1044 { .compatible = "ilitek,ili2120", .data = &ili212x_chip },
1045 { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
1048 MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
1050 static struct i2c_driver ili210x_ts_driver = {
1052 .name = "ili210x_i2c",
1053 .of_match_table = ili210x_dt_ids,
1055 .id_table = ili210x_i2c_id,
1056 .probe = ili210x_i2c_probe,
1059 module_i2c_driver(ili210x_ts_driver);
1062 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
1063 MODULE_LICENSE("GPL");