1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/delay.h>
3 #include <linux/gpio/consumer.h>
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/input/touchscreen.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/sizes.h>
12 #include <linux/slab.h>
13 #include <asm/unaligned.h>
15 #define ILI2XXX_POLL_PERIOD 20
17 #define ILI210X_DATA_SIZE 64
18 #define ILI211X_DATA_SIZE 43
19 #define ILI251X_DATA_SIZE1 31
20 #define ILI251X_DATA_SIZE2 20
22 /* Touchscreen commands */
23 #define REG_TOUCHDATA 0x10
24 #define REG_PANEL_INFO 0x20
25 #define REG_CALIBRATE 0xcc
28 int (*read_reg
)(struct i2c_client
*client
, u8 reg
,
29 void *buf
, size_t len
);
30 int (*get_touch_data
)(struct i2c_client
*client
, u8
*data
);
31 bool (*parse_touch_data
)(const u8
*data
, unsigned int finger
,
32 unsigned int *x
, unsigned int *y
);
33 bool (*continue_polling
)(const u8
*data
, bool touch
);
34 unsigned int max_touches
;
35 unsigned int resolution
;
36 bool has_calibrate_reg
;
40 struct i2c_client
*client
;
41 struct input_dev
*input
;
42 struct gpio_desc
*reset_gpio
;
43 struct touchscreen_properties prop
;
44 const struct ili2xxx_chip
*chip
;
48 static int ili210x_read_reg(struct i2c_client
*client
,
49 u8 reg
, void *buf
, size_t len
)
51 struct i2c_msg msg
[] = {
67 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
68 if (ret
!= ARRAY_SIZE(msg
)) {
69 error
= ret
< 0 ? ret
: -EIO
;
70 dev_err(&client
->dev
, "%s failed: %d\n", __func__
, error
);
77 static int ili210x_read_touch_data(struct i2c_client
*client
, u8
*data
)
79 return ili210x_read_reg(client
, REG_TOUCHDATA
,
80 data
, ILI210X_DATA_SIZE
);
83 static bool ili210x_touchdata_to_coords(const u8
*touchdata
,
85 unsigned int *x
, unsigned int *y
)
87 if (touchdata
[0] & BIT(finger
))
90 *x
= get_unaligned_be16(touchdata
+ 1 + (finger
* 4) + 0);
91 *y
= get_unaligned_be16(touchdata
+ 1 + (finger
* 4) + 2);
96 static bool ili210x_check_continue_polling(const u8
*data
, bool touch
)
98 return data
[0] & 0xf3;
101 static const struct ili2xxx_chip ili210x_chip
= {
102 .read_reg
= ili210x_read_reg
,
103 .get_touch_data
= ili210x_read_touch_data
,
104 .parse_touch_data
= ili210x_touchdata_to_coords
,
105 .continue_polling
= ili210x_check_continue_polling
,
107 .has_calibrate_reg
= true,
110 static int ili211x_read_touch_data(struct i2c_client
*client
, u8
*data
)
117 ret
= i2c_master_recv(client
, data
, ILI211X_DATA_SIZE
);
118 if (ret
!= ILI211X_DATA_SIZE
) {
119 error
= ret
< 0 ? ret
: -EIO
;
120 dev_err(&client
->dev
, "%s failed: %d\n", __func__
, error
);
124 /* This chip uses custom checksum at the end of data */
125 for (i
= 0; i
< ILI211X_DATA_SIZE
- 1; i
++)
126 sum
= (sum
+ data
[i
]) & 0xff;
128 if ((-sum
& 0xff) != data
[ILI211X_DATA_SIZE
- 1]) {
129 dev_err(&client
->dev
,
130 "CRC error (crc=0x%02x expected=0x%02x)\n",
131 sum
, data
[ILI211X_DATA_SIZE
- 1]);
138 static bool ili211x_touchdata_to_coords(const u8
*touchdata
,
140 unsigned int *x
, unsigned int *y
)
144 data
= get_unaligned_be32(touchdata
+ 1 + (finger
* 4) + 0);
145 if (data
== 0xffffffff) /* Finger up */
148 *x
= ((touchdata
[1 + (finger
* 4) + 0] & 0xf0) << 4) |
149 touchdata
[1 + (finger
* 4) + 1];
150 *y
= ((touchdata
[1 + (finger
* 4) + 0] & 0x0f) << 8) |
151 touchdata
[1 + (finger
* 4) + 2];
156 static bool ili211x_decline_polling(const u8
*data
, bool touch
)
161 static const struct ili2xxx_chip ili211x_chip
= {
162 .read_reg
= ili210x_read_reg
,
163 .get_touch_data
= ili211x_read_touch_data
,
164 .parse_touch_data
= ili211x_touchdata_to_coords
,
165 .continue_polling
= ili211x_decline_polling
,
170 static bool ili212x_touchdata_to_coords(const u8
*touchdata
,
172 unsigned int *x
, unsigned int *y
)
176 val
= get_unaligned_be16(touchdata
+ 3 + (finger
* 5) + 0);
177 if (!(val
& BIT(15))) /* Touch indication */
181 *y
= get_unaligned_be16(touchdata
+ 3 + (finger
* 5) + 2);
186 static bool ili212x_check_continue_polling(const u8
*data
, bool touch
)
191 static const struct ili2xxx_chip ili212x_chip
= {
192 .read_reg
= ili210x_read_reg
,
193 .get_touch_data
= ili210x_read_touch_data
,
194 .parse_touch_data
= ili212x_touchdata_to_coords
,
195 .continue_polling
= ili212x_check_continue_polling
,
197 .has_calibrate_reg
= true,
200 static int ili251x_read_reg(struct i2c_client
*client
,
201 u8 reg
, void *buf
, size_t len
)
206 ret
= i2c_master_send(client
, ®
, 1);
208 usleep_range(5000, 5500);
210 ret
= i2c_master_recv(client
, buf
, len
);
215 error
= ret
< 0 ? ret
: -EIO
;
216 dev_err(&client
->dev
, "%s failed: %d\n", __func__
, error
);
220 static int ili251x_read_touch_data(struct i2c_client
*client
, u8
*data
)
224 error
= ili251x_read_reg(client
, REG_TOUCHDATA
,
225 data
, ILI251X_DATA_SIZE1
);
226 if (!error
&& data
[0] == 2) {
227 error
= i2c_master_recv(client
, data
+ ILI251X_DATA_SIZE1
,
229 if (error
>= 0 && error
!= ILI251X_DATA_SIZE2
)
236 static bool ili251x_touchdata_to_coords(const u8
*touchdata
,
238 unsigned int *x
, unsigned int *y
)
242 val
= get_unaligned_be16(touchdata
+ 1 + (finger
* 5) + 0);
243 if (!(val
& BIT(15))) /* Touch indication */
247 *y
= get_unaligned_be16(touchdata
+ 1 + (finger
* 5) + 2);
252 static bool ili251x_check_continue_polling(const u8
*data
, bool touch
)
257 static const struct ili2xxx_chip ili251x_chip
= {
258 .read_reg
= ili251x_read_reg
,
259 .get_touch_data
= ili251x_read_touch_data
,
260 .parse_touch_data
= ili251x_touchdata_to_coords
,
261 .continue_polling
= ili251x_check_continue_polling
,
263 .has_calibrate_reg
= true,
266 static bool ili210x_report_events(struct ili210x
*priv
, u8
*touchdata
)
268 struct input_dev
*input
= priv
->input
;
270 bool contact
= false, touch
;
271 unsigned int x
= 0, y
= 0;
273 for (i
= 0; i
< priv
->chip
->max_touches
; i
++) {
274 touch
= priv
->chip
->parse_touch_data(touchdata
, i
, &x
, &y
);
276 input_mt_slot(input
, i
);
277 if (input_mt_report_slot_state(input
, MT_TOOL_FINGER
, touch
)) {
278 touchscreen_report_pos(input
, &priv
->prop
, x
, y
, true);
283 input_mt_report_pointer_emulation(input
, false);
289 static irqreturn_t
ili210x_irq(int irq
, void *irq_data
)
291 struct ili210x
*priv
= irq_data
;
292 struct i2c_client
*client
= priv
->client
;
293 const struct ili2xxx_chip
*chip
= priv
->chip
;
294 u8 touchdata
[ILI210X_DATA_SIZE
] = { 0 };
300 error
= chip
->get_touch_data(client
, touchdata
);
302 dev_err(&client
->dev
,
303 "Unable to get touch data: %d\n", error
);
307 touch
= ili210x_report_events(priv
, touchdata
);
308 keep_polling
= chip
->continue_polling(touchdata
, touch
);
310 msleep(ILI2XXX_POLL_PERIOD
);
311 } while (!priv
->stop
&& keep_polling
);
316 static ssize_t
ili210x_calibrate(struct device
*dev
,
317 struct device_attribute
*attr
,
318 const char *buf
, size_t count
)
320 struct i2c_client
*client
= to_i2c_client(dev
);
321 struct ili210x
*priv
= i2c_get_clientdata(client
);
322 unsigned long calibrate
;
324 u8 cmd
= REG_CALIBRATE
;
326 if (kstrtoul(buf
, 10, &calibrate
))
333 rc
= i2c_master_send(priv
->client
, &cmd
, sizeof(cmd
));
334 if (rc
!= sizeof(cmd
))
340 static DEVICE_ATTR(calibrate
, S_IWUSR
, NULL
, ili210x_calibrate
);
342 static struct attribute
*ili210x_attributes
[] = {
343 &dev_attr_calibrate
.attr
,
347 static umode_t
ili210x_calibrate_visible(struct kobject
*kobj
,
348 struct attribute
*attr
, int index
)
350 struct device
*dev
= kobj_to_dev(kobj
);
351 struct i2c_client
*client
= to_i2c_client(dev
);
352 struct ili210x
*priv
= i2c_get_clientdata(client
);
354 return priv
->chip
->has_calibrate_reg
? attr
->mode
: 0;
357 static const struct attribute_group ili210x_attr_group
= {
358 .attrs
= ili210x_attributes
,
359 .is_visible
= ili210x_calibrate_visible
,
362 static void ili210x_power_down(void *data
)
364 struct gpio_desc
*reset_gpio
= data
;
366 gpiod_set_value_cansleep(reset_gpio
, 1);
369 static void ili210x_stop(void *data
)
371 struct ili210x
*priv
= data
;
373 /* Tell ISR to quit even if there is a contact. */
377 static int ili210x_i2c_probe(struct i2c_client
*client
,
378 const struct i2c_device_id
*id
)
380 struct device
*dev
= &client
->dev
;
381 const struct ili2xxx_chip
*chip
;
382 struct ili210x
*priv
;
383 struct gpio_desc
*reset_gpio
;
384 struct input_dev
*input
;
388 dev_dbg(dev
, "Probing for ILI210X I2C Touschreen driver");
390 chip
= device_get_match_data(dev
);
392 chip
= (const struct ili2xxx_chip
*)id
->driver_data
;
394 dev_err(&client
->dev
, "unknown device model\n");
398 if (client
->irq
<= 0) {
399 dev_err(dev
, "No IRQ!\n");
403 reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
404 if (IS_ERR(reset_gpio
))
405 return PTR_ERR(reset_gpio
);
408 error
= devm_add_action_or_reset(dev
, ili210x_power_down
,
413 usleep_range(50, 100);
414 gpiod_set_value_cansleep(reset_gpio
, 0);
418 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
422 input
= devm_input_allocate_device(dev
);
426 priv
->client
= client
;
428 priv
->reset_gpio
= reset_gpio
;
430 i2c_set_clientdata(client
, priv
);
432 /* Setup input device */
433 input
->name
= "ILI210x Touchscreen";
434 input
->id
.bustype
= BUS_I2C
;
437 max_xy
= (chip
->resolution
?: SZ_64K
) - 1;
438 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, max_xy
, 0, 0);
439 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, max_xy
, 0, 0);
440 touchscreen_parse_properties(input
, true, &priv
->prop
);
442 error
= input_mt_init_slots(input
, priv
->chip
->max_touches
,
445 dev_err(dev
, "Unable to set up slots, err: %d\n", error
);
449 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, ili210x_irq
,
450 IRQF_ONESHOT
, client
->name
, priv
);
452 dev_err(dev
, "Unable to request touchscreen IRQ, err: %d\n",
457 error
= devm_add_action_or_reset(dev
, ili210x_stop
, priv
);
461 error
= devm_device_add_group(dev
, &ili210x_attr_group
);
463 dev_err(dev
, "Unable to create sysfs attributes, err: %d\n",
468 error
= input_register_device(priv
->input
);
470 dev_err(dev
, "Cannot register input device, err: %d\n", error
);
477 static const struct i2c_device_id ili210x_i2c_id
[] = {
478 { "ili210x", (long)&ili210x_chip
},
479 { "ili2117", (long)&ili211x_chip
},
480 { "ili2120", (long)&ili212x_chip
},
481 { "ili251x", (long)&ili251x_chip
},
484 MODULE_DEVICE_TABLE(i2c
, ili210x_i2c_id
);
486 static const struct of_device_id ili210x_dt_ids
[] = {
487 { .compatible
= "ilitek,ili210x", .data
= &ili210x_chip
},
488 { .compatible
= "ilitek,ili2117", .data
= &ili211x_chip
},
489 { .compatible
= "ilitek,ili2120", .data
= &ili212x_chip
},
490 { .compatible
= "ilitek,ili251x", .data
= &ili251x_chip
},
493 MODULE_DEVICE_TABLE(of
, ili210x_dt_ids
);
495 static struct i2c_driver ili210x_ts_driver
= {
497 .name
= "ili210x_i2c",
498 .of_match_table
= ili210x_dt_ids
,
500 .id_table
= ili210x_i2c_id
,
501 .probe
= ili210x_i2c_probe
,
504 module_i2c_driver(ili210x_ts_driver
);
506 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
507 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
508 MODULE_LICENSE("GPL");