1 // SPDX-License-Identifier: GPL-2.0
3 * ILITEK Touch IC driver for 23XX, 25XX and Lego series
5 * Copyright (C) 2011 ILI Technology Corporation.
6 * Copyright (C) 2020 Luca Hsu <luca_hsu@ilitek.com>
7 * Copyright (C) 2021 Joe Hung <joe_hung@ilitek.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/input.h>
13 #include <linux/input/mt.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/errno.h>
20 #include <linux/acpi.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/unaligned.h>
25 #define ILITEK_TS_NAME "ilitek_ts"
30 #define ILITEK_TP_CMD_GET_TP_RES 0x20
31 #define ILITEK_TP_CMD_GET_SCRN_RES 0x21
32 #define ILITEK_TP_CMD_SET_IC_SLEEP 0x30
33 #define ILITEK_TP_CMD_SET_IC_WAKE 0x31
34 #define ILITEK_TP_CMD_GET_FW_VER 0x40
35 #define ILITEK_TP_CMD_GET_PRL_VER 0x42
36 #define ILITEK_TP_CMD_GET_MCU_VER 0x61
37 #define ILITEK_TP_CMD_GET_IC_MODE 0xC0
39 #define ILITEK_TP_I2C_REPORT_ID 0x48
41 #define REPORT_COUNT_ADDRESS 61
42 #define ILITEK_SUPPORT_MAX_POINT 40
44 struct ilitek_protocol_info
{
49 struct ilitek_ts_data
{
50 struct i2c_client
*client
;
51 struct gpio_desc
*reset_gpio
;
52 struct input_dev
*input_dev
;
53 struct touchscreen_properties prop
;
55 const struct ilitek_protocol_map
*ptl_cb_func
;
56 struct ilitek_protocol_info ptl
;
71 struct ilitek_protocol_map
{
74 int (*func
)(struct ilitek_ts_data
*ts
, u16 cmd
, u8
*inbuf
, u8
*outbuf
);
88 /* ALWAYS keep at the end */
92 /* ILITEK I2C R/W APIs */
93 static int ilitek_i2c_write_and_read(struct ilitek_ts_data
*ts
,
94 u8
*cmd
, int write_len
, int delay
,
95 u8
*data
, int read_len
)
98 struct i2c_client
*client
= ts
->client
;
99 struct i2c_msg msgs
[] = {
101 .addr
= client
->addr
,
107 .addr
= client
->addr
,
114 if (delay
== 0 && write_len
> 0 && read_len
> 0) {
115 error
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
120 error
= i2c_transfer(client
->adapter
, msgs
, 1);
128 error
= i2c_transfer(client
->adapter
, msgs
+ 1, 1);
137 /* ILITEK ISR APIs */
138 static void ilitek_touch_down(struct ilitek_ts_data
*ts
, unsigned int id
,
139 unsigned int x
, unsigned int y
)
141 struct input_dev
*input
= ts
->input_dev
;
143 input_mt_slot(input
, id
);
144 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
146 touchscreen_report_pos(input
, &ts
->prop
, x
, y
, true);
149 static int ilitek_process_and_report_v6(struct ilitek_ts_data
*ts
)
154 int packet_max_point
= 10;
155 int report_max_point
;
157 struct input_dev
*input
= ts
->input_dev
;
158 struct device
*dev
= &ts
->client
->dev
;
159 unsigned int x
, y
, status
, id
;
161 error
= ilitek_i2c_write_and_read(ts
, NULL
, 0, 0, buf
, 64);
163 dev_err(dev
, "get touch info failed, err:%d\n", error
);
167 if (buf
[0] != ILITEK_TP_I2C_REPORT_ID
) {
168 dev_err(dev
, "get touch info failed. Wrong id: 0x%02X\n", buf
[0]);
172 report_max_point
= buf
[REPORT_COUNT_ADDRESS
];
173 if (report_max_point
> ts
->max_tp
) {
174 dev_err(dev
, "FW report max point:%d > panel info. max:%d\n",
175 report_max_point
, ts
->max_tp
);
179 count
= DIV_ROUND_UP(report_max_point
, packet_max_point
);
180 for (i
= 1; i
< count
; i
++) {
181 error
= ilitek_i2c_write_and_read(ts
, NULL
, 0, 0,
184 dev_err(dev
, "get touch info. failed, cnt:%d, err:%d\n",
190 for (i
= 0; i
< report_max_point
; i
++) {
191 status
= buf
[i
* packet_len
+ 1] & 0x40;
195 id
= buf
[i
* packet_len
+ 1] & 0x3F;
197 x
= get_unaligned_le16(buf
+ i
* packet_len
+ 2);
198 y
= get_unaligned_le16(buf
+ i
* packet_len
+ 4);
200 if (x
> ts
->screen_max_x
|| x
< ts
->screen_min_x
||
201 y
> ts
->screen_max_y
|| y
< ts
->screen_min_y
) {
202 dev_warn(dev
, "invalid position, X[%d,%u,%d], Y[%d,%u,%d]\n",
203 ts
->screen_min_x
, x
, ts
->screen_max_x
,
204 ts
->screen_min_y
, y
, ts
->screen_max_y
);
208 ilitek_touch_down(ts
, id
, x
, y
);
211 input_mt_sync_frame(input
);
217 /* APIs of cmds for ILITEK Touch IC */
218 static int api_protocol_set_cmd(struct ilitek_ts_data
*ts
,
219 u16 idx
, u8
*inbuf
, u8
*outbuf
)
224 if (idx
>= MAX_CMD_CNT
)
227 cmd
= ts
->ptl_cb_func
[idx
].cmd
;
228 error
= ts
->ptl_cb_func
[idx
].func(ts
, cmd
, inbuf
, outbuf
);
235 static int api_protocol_get_ptl_ver(struct ilitek_ts_data
*ts
,
236 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
242 error
= ilitek_i2c_write_and_read(ts
, buf
, 1, 5, outbuf
, 3);
246 ts
->ptl
.ver
= get_unaligned_be16(outbuf
);
247 ts
->ptl
.ver_major
= outbuf
[0];
252 static int api_protocol_get_mcu_ver(struct ilitek_ts_data
*ts
,
253 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
259 error
= ilitek_i2c_write_and_read(ts
, buf
, 1, 5, outbuf
, 32);
263 ts
->mcu_ver
= get_unaligned_le16(outbuf
);
264 memset(ts
->product_id
, 0, sizeof(ts
->product_id
));
265 memcpy(ts
->product_id
, outbuf
+ 6, 26);
270 static int api_protocol_get_fw_ver(struct ilitek_ts_data
*ts
,
271 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
277 error
= ilitek_i2c_write_and_read(ts
, buf
, 1, 5, outbuf
, 8);
281 memcpy(ts
->firmware_ver
, outbuf
, 8);
286 static int api_protocol_get_scrn_res(struct ilitek_ts_data
*ts
,
287 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
293 error
= ilitek_i2c_write_and_read(ts
, buf
, 1, 5, outbuf
, 8);
297 ts
->screen_min_x
= get_unaligned_le16(outbuf
);
298 ts
->screen_min_y
= get_unaligned_le16(outbuf
+ 2);
299 ts
->screen_max_x
= get_unaligned_le16(outbuf
+ 4);
300 ts
->screen_max_y
= get_unaligned_le16(outbuf
+ 6);
305 static int api_protocol_get_tp_res(struct ilitek_ts_data
*ts
,
306 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
312 error
= ilitek_i2c_write_and_read(ts
, buf
, 1, 5, outbuf
, 15);
316 ts
->max_tp
= outbuf
[8];
317 if (ts
->max_tp
> ILITEK_SUPPORT_MAX_POINT
) {
318 dev_err(&ts
->client
->dev
, "Invalid MAX_TP:%d from FW\n",
326 static int api_protocol_get_ic_mode(struct ilitek_ts_data
*ts
,
327 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
333 error
= ilitek_i2c_write_and_read(ts
, buf
, 1, 5, outbuf
, 2);
337 ts
->ic_mode
= outbuf
[0];
341 static int api_protocol_set_ic_sleep(struct ilitek_ts_data
*ts
,
342 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
347 return ilitek_i2c_write_and_read(ts
, buf
, 1, 0, NULL
, 0);
350 static int api_protocol_set_ic_wake(struct ilitek_ts_data
*ts
,
351 u16 cmd
, u8
*inbuf
, u8
*outbuf
)
356 return ilitek_i2c_write_and_read(ts
, buf
, 1, 0, NULL
, 0);
359 static const struct ilitek_protocol_map ptl_func_map
[] = {
362 ILITEK_TP_CMD_GET_PRL_VER
, "GET_PTL_VER",
363 api_protocol_get_ptl_ver
366 ILITEK_TP_CMD_GET_FW_VER
, "GET_FW_VER",
367 api_protocol_get_fw_ver
370 ILITEK_TP_CMD_GET_SCRN_RES
, "GET_SCRN_RES",
371 api_protocol_get_scrn_res
374 ILITEK_TP_CMD_GET_TP_RES
, "GET_TP_RES",
375 api_protocol_get_tp_res
378 ILITEK_TP_CMD_GET_IC_MODE
, "GET_IC_MODE",
379 api_protocol_get_ic_mode
382 ILITEK_TP_CMD_GET_MCU_VER
, "GET_MOD_VER",
383 api_protocol_get_mcu_ver
386 ILITEK_TP_CMD_SET_IC_SLEEP
, "SET_IC_SLEEP",
387 api_protocol_set_ic_sleep
390 ILITEK_TP_CMD_SET_IC_WAKE
, "SET_IC_WAKE",
391 api_protocol_set_ic_wake
396 static void ilitek_reset(struct ilitek_ts_data
*ts
, int delay
)
398 if (ts
->reset_gpio
) {
399 gpiod_set_value(ts
->reset_gpio
, 1);
401 gpiod_set_value(ts
->reset_gpio
, 0);
406 static int ilitek_protocol_init(struct ilitek_ts_data
*ts
)
411 ts
->ptl_cb_func
= ptl_func_map
;
412 ts
->reset_time
= 600;
414 error
= api_protocol_set_cmd(ts
, GET_PTL_VER
, NULL
, outbuf
);
418 /* Protocol v3 is not support currently */
419 if (ts
->ptl
.ver_major
== 0x3 ||
420 ts
->ptl
.ver
== BL_V1_6
||
421 ts
->ptl
.ver
== BL_V1_7
)
427 static int ilitek_read_tp_info(struct ilitek_ts_data
*ts
, bool boot
)
432 error
= api_protocol_set_cmd(ts
, GET_PTL_VER
, NULL
, outbuf
);
436 error
= api_protocol_set_cmd(ts
, GET_MCU_VER
, NULL
, outbuf
);
440 error
= api_protocol_set_cmd(ts
, GET_FW_VER
, NULL
, outbuf
);
445 error
= api_protocol_set_cmd(ts
, GET_SCRN_RES
, NULL
,
451 error
= api_protocol_set_cmd(ts
, GET_TP_RES
, NULL
, outbuf
);
455 error
= api_protocol_set_cmd(ts
, GET_IC_MODE
, NULL
, outbuf
);
462 static int ilitek_input_dev_init(struct device
*dev
, struct ilitek_ts_data
*ts
)
465 struct input_dev
*input
;
467 input
= devm_input_allocate_device(dev
);
471 ts
->input_dev
= input
;
472 input
->name
= ILITEK_TS_NAME
;
473 input
->id
.bustype
= BUS_I2C
;
475 __set_bit(INPUT_PROP_DIRECT
, input
->propbit
);
477 input_set_abs_params(input
, ABS_MT_POSITION_X
,
478 ts
->screen_min_x
, ts
->screen_max_x
, 0, 0);
479 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
480 ts
->screen_min_y
, ts
->screen_max_y
, 0, 0);
482 touchscreen_parse_properties(input
, true, &ts
->prop
);
484 error
= input_mt_init_slots(input
, ts
->max_tp
,
485 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
487 dev_err(dev
, "initialize MT slots failed, err:%d\n", error
);
491 error
= input_register_device(input
);
493 dev_err(dev
, "register input device failed, err:%d\n", error
);
500 static irqreturn_t
ilitek_i2c_isr(int irq
, void *dev_id
)
502 struct ilitek_ts_data
*ts
= dev_id
;
505 error
= ilitek_process_and_report_v6(ts
);
507 dev_err(&ts
->client
->dev
, "[%s] err:%d\n", __func__
, error
);
514 static ssize_t
firmware_version_show(struct device
*dev
,
515 struct device_attribute
*attr
, char *buf
)
517 struct i2c_client
*client
= to_i2c_client(dev
);
518 struct ilitek_ts_data
*ts
= i2c_get_clientdata(client
);
520 return sysfs_emit(buf
,
521 "fw version: [%02X%02X.%02X%02X.%02X%02X.%02X%02X]\n",
522 ts
->firmware_ver
[0], ts
->firmware_ver
[1],
523 ts
->firmware_ver
[2], ts
->firmware_ver
[3],
524 ts
->firmware_ver
[4], ts
->firmware_ver
[5],
525 ts
->firmware_ver
[6], ts
->firmware_ver
[7]);
527 static DEVICE_ATTR_RO(firmware_version
);
529 static ssize_t
product_id_show(struct device
*dev
,
530 struct device_attribute
*attr
, char *buf
)
532 struct i2c_client
*client
= to_i2c_client(dev
);
533 struct ilitek_ts_data
*ts
= i2c_get_clientdata(client
);
535 return sysfs_emit(buf
, "product id: [%04X], module: [%s]\n",
536 ts
->mcu_ver
, ts
->product_id
);
538 static DEVICE_ATTR_RO(product_id
);
540 static struct attribute
*ilitek_sysfs_attrs
[] = {
541 &dev_attr_firmware_version
.attr
,
542 &dev_attr_product_id
.attr
,
545 ATTRIBUTE_GROUPS(ilitek_sysfs
);
547 static int ilitek_ts_i2c_probe(struct i2c_client
*client
)
549 struct ilitek_ts_data
*ts
;
550 struct device
*dev
= &client
->dev
;
553 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
554 dev_err(dev
, "i2c check functionality failed\n");
558 ts
= devm_kzalloc(dev
, sizeof(*ts
), GFP_KERNEL
);
563 i2c_set_clientdata(client
, ts
);
565 ts
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
566 if (IS_ERR(ts
->reset_gpio
)) {
567 error
= PTR_ERR(ts
->reset_gpio
);
568 dev_err(dev
, "request gpiod failed: %d", error
);
572 ilitek_reset(ts
, 1000);
574 error
= ilitek_protocol_init(ts
);
576 dev_err(dev
, "protocol init failed: %d", error
);
580 error
= ilitek_read_tp_info(ts
, true);
582 dev_err(dev
, "read tp info failed: %d", error
);
586 error
= ilitek_input_dev_init(dev
, ts
);
588 dev_err(dev
, "input dev init failed: %d", error
);
592 error
= devm_request_threaded_irq(dev
, ts
->client
->irq
,
593 NULL
, ilitek_i2c_isr
, IRQF_ONESHOT
,
594 "ilitek_touch_irq", ts
);
596 dev_err(dev
, "request threaded irq failed: %d\n", error
);
603 static int ilitek_suspend(struct device
*dev
)
605 struct i2c_client
*client
= to_i2c_client(dev
);
606 struct ilitek_ts_data
*ts
= i2c_get_clientdata(client
);
609 disable_irq(client
->irq
);
611 if (!device_may_wakeup(dev
)) {
612 error
= api_protocol_set_cmd(ts
, SET_IC_SLEEP
, NULL
, NULL
);
620 static int ilitek_resume(struct device
*dev
)
622 struct i2c_client
*client
= to_i2c_client(dev
);
623 struct ilitek_ts_data
*ts
= i2c_get_clientdata(client
);
626 if (!device_may_wakeup(dev
)) {
627 error
= api_protocol_set_cmd(ts
, SET_IC_WAKE
, NULL
, NULL
);
631 ilitek_reset(ts
, ts
->reset_time
);
634 enable_irq(client
->irq
);
639 static DEFINE_SIMPLE_DEV_PM_OPS(ilitek_pm_ops
, ilitek_suspend
, ilitek_resume
);
641 static const struct i2c_device_id ilitek_ts_i2c_id
[] = {
645 MODULE_DEVICE_TABLE(i2c
, ilitek_ts_i2c_id
);
648 static const struct acpi_device_id ilitekts_acpi_id
[] = {
652 MODULE_DEVICE_TABLE(acpi
, ilitekts_acpi_id
);
656 static const struct of_device_id ilitek_ts_i2c_match
[] = {
657 {.compatible
= "ilitek,ili2130",},
658 {.compatible
= "ilitek,ili2131",},
659 {.compatible
= "ilitek,ili2132",},
660 {.compatible
= "ilitek,ili2316",},
661 {.compatible
= "ilitek,ili2322",},
662 {.compatible
= "ilitek,ili2323",},
663 {.compatible
= "ilitek,ili2326",},
664 {.compatible
= "ilitek,ili2520",},
665 {.compatible
= "ilitek,ili2521",},
668 MODULE_DEVICE_TABLE(of
, ilitek_ts_i2c_match
);
671 static struct i2c_driver ilitek_ts_i2c_driver
= {
673 .name
= ILITEK_TS_NAME
,
674 .dev_groups
= ilitek_sysfs_groups
,
675 .pm
= pm_sleep_ptr(&ilitek_pm_ops
),
676 .of_match_table
= of_match_ptr(ilitek_ts_i2c_match
),
677 .acpi_match_table
= ACPI_PTR(ilitekts_acpi_id
),
679 .probe
= ilitek_ts_i2c_probe
,
680 .id_table
= ilitek_ts_i2c_id
,
682 module_i2c_driver(ilitek_ts_i2c_driver
);
684 MODULE_AUTHOR("ILITEK");
685 MODULE_DESCRIPTION("ILITEK I2C Touchscreen Driver");
686 MODULE_LICENSE("GPL");