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/slab.h>
12 #include <asm/unaligned.h>
14 #define ILI2XXX_POLL_PERIOD 20
16 #define ILI210X_DATA_SIZE 64
17 #define ILI211X_DATA_SIZE 43
18 #define ILI251X_DATA_SIZE1 31
19 #define ILI251X_DATA_SIZE2 20
21 /* Touchscreen commands */
22 #define REG_TOUCHDATA 0x10
23 #define REG_PANEL_INFO 0x20
24 #define REG_FIRMWARE_VERSION 0x40
25 #define REG_CALIBRATE 0xcc
27 struct firmware_version {
34 int (*read_reg)(struct i2c_client *client, u8 reg,
35 void *buf, size_t len);
36 int (*get_touch_data)(struct i2c_client *client, u8 *data);
37 bool (*parse_touch_data)(const u8 *data, unsigned int finger,
38 unsigned int *x, unsigned int *y);
39 bool (*continue_polling)(const u8 *data, bool touch);
40 unsigned int max_touches;
44 struct i2c_client *client;
45 struct input_dev *input;
46 struct gpio_desc *reset_gpio;
47 struct touchscreen_properties prop;
48 const struct ili2xxx_chip *chip;
52 static int ili210x_read_reg(struct i2c_client *client,
53 u8 reg, void *buf, size_t len)
55 struct i2c_msg msg[] = {
71 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
72 if (ret != ARRAY_SIZE(msg)) {
73 error = ret < 0 ? ret : -EIO;
74 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
81 static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
83 return ili210x_read_reg(client, REG_TOUCHDATA,
84 data, ILI210X_DATA_SIZE);
87 static bool ili210x_touchdata_to_coords(const u8 *touchdata,
89 unsigned int *x, unsigned int *y)
91 if (touchdata[0] & BIT(finger))
94 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
95 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
100 static bool ili210x_check_continue_polling(const u8 *data, bool touch)
102 return data[0] & 0xf3;
105 static const struct ili2xxx_chip ili210x_chip = {
106 .read_reg = ili210x_read_reg,
107 .get_touch_data = ili210x_read_touch_data,
108 .parse_touch_data = ili210x_touchdata_to_coords,
109 .continue_polling = ili210x_check_continue_polling,
113 static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
120 ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE);
121 if (ret != ILI211X_DATA_SIZE) {
122 error = ret < 0 ? ret : -EIO;
123 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
127 /* This chip uses custom checksum at the end of data */
128 for (i = 0; i < ILI211X_DATA_SIZE - 1; i++)
129 sum = (sum + data[i]) & 0xff;
131 if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) {
132 dev_err(&client->dev,
133 "CRC error (crc=0x%02x expected=0x%02x)\n",
134 sum, data[ILI211X_DATA_SIZE - 1]);
141 static bool ili211x_touchdata_to_coords(const u8 *touchdata,
143 unsigned int *x, unsigned int *y)
147 data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
148 if (data == 0xffffffff) /* Finger up */
151 *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) |
152 touchdata[1 + (finger * 4) + 1];
153 *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) |
154 touchdata[1 + (finger * 4) + 2];
159 static bool ili211x_decline_polling(const u8 *data, bool touch)
164 static const struct ili2xxx_chip ili211x_chip = {
165 .read_reg = ili210x_read_reg,
166 .get_touch_data = ili211x_read_touch_data,
167 .parse_touch_data = ili211x_touchdata_to_coords,
168 .continue_polling = ili211x_decline_polling,
172 static int ili251x_read_reg(struct i2c_client *client,
173 u8 reg, void *buf, size_t len)
178 ret = i2c_master_send(client, ®, 1);
180 usleep_range(5000, 5500);
182 ret = i2c_master_recv(client, buf, len);
187 error = ret < 0 ? ret : -EIO;
188 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
192 static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
196 error = ili251x_read_reg(client, REG_TOUCHDATA,
197 data, ILI251X_DATA_SIZE1);
198 if (!error && data[0] == 2) {
199 error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1,
201 if (error >= 0 && error != ILI251X_DATA_SIZE2)
208 static bool ili251x_touchdata_to_coords(const u8 *touchdata,
210 unsigned int *x, unsigned int *y)
214 val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
215 if (!(val & BIT(15))) /* Touch indication */
219 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
224 static bool ili251x_check_continue_polling(const u8 *data, bool touch)
229 static const struct ili2xxx_chip ili251x_chip = {
230 .read_reg = ili251x_read_reg,
231 .get_touch_data = ili251x_read_touch_data,
232 .parse_touch_data = ili251x_touchdata_to_coords,
233 .continue_polling = ili251x_check_continue_polling,
237 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
239 struct input_dev *input = priv->input;
241 bool contact = false, touch;
242 unsigned int x = 0, y = 0;
244 for (i = 0; i < priv->chip->max_touches; i++) {
245 touch = priv->chip->parse_touch_data(touchdata, i, &x, &y);
247 input_mt_slot(input, i);
248 if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) {
249 touchscreen_report_pos(input, &priv->prop, x, y, true);
254 input_mt_report_pointer_emulation(input, false);
260 static irqreturn_t ili210x_irq(int irq, void *irq_data)
262 struct ili210x *priv = irq_data;
263 struct i2c_client *client = priv->client;
264 const struct ili2xxx_chip *chip = priv->chip;
265 u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
271 error = chip->get_touch_data(client, touchdata);
273 dev_err(&client->dev,
274 "Unable to get touch data: %d\n", error);
278 touch = ili210x_report_events(priv, touchdata);
279 keep_polling = chip->continue_polling(touchdata, touch);
281 msleep(ILI2XXX_POLL_PERIOD);
282 } while (!priv->stop && keep_polling);
287 static ssize_t ili210x_calibrate(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
291 struct i2c_client *client = to_i2c_client(dev);
292 struct ili210x *priv = i2c_get_clientdata(client);
293 unsigned long calibrate;
295 u8 cmd = REG_CALIBRATE;
297 if (kstrtoul(buf, 10, &calibrate))
304 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
305 if (rc != sizeof(cmd))
311 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
313 static struct attribute *ili210x_attributes[] = {
314 &dev_attr_calibrate.attr,
318 static const struct attribute_group ili210x_attr_group = {
319 .attrs = ili210x_attributes,
322 static void ili210x_power_down(void *data)
324 struct gpio_desc *reset_gpio = data;
326 gpiod_set_value_cansleep(reset_gpio, 1);
329 static void ili210x_stop(void *data)
331 struct ili210x *priv = data;
333 /* Tell ISR to quit even if there is a contact. */
337 static int ili210x_i2c_probe(struct i2c_client *client,
338 const struct i2c_device_id *id)
340 struct device *dev = &client->dev;
341 const struct ili2xxx_chip *chip;
342 struct ili210x *priv;
343 struct gpio_desc *reset_gpio;
344 struct input_dev *input;
345 struct firmware_version firmware;
348 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
350 chip = device_get_match_data(dev);
352 chip = (const struct ili2xxx_chip *)id->driver_data;
354 dev_err(&client->dev, "unknown device model\n");
358 if (client->irq <= 0) {
359 dev_err(dev, "No IRQ!\n");
363 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
364 if (IS_ERR(reset_gpio))
365 return PTR_ERR(reset_gpio);
368 error = devm_add_action_or_reset(dev, ili210x_power_down,
373 usleep_range(50, 100);
374 gpiod_set_value_cansleep(reset_gpio, 0);
378 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
382 input = devm_input_allocate_device(dev);
386 priv->client = client;
388 priv->reset_gpio = reset_gpio;
390 i2c_set_clientdata(client, priv);
392 /* Get firmware version */
393 error = chip->read_reg(client, REG_FIRMWARE_VERSION,
394 &firmware, sizeof(firmware));
396 dev_err(dev, "Failed to get firmware version, err: %d\n",
401 /* Setup input device */
402 input->name = "ILI210x Touchscreen";
403 input->id.bustype = BUS_I2C;
406 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 0xffff, 0, 0);
407 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 0xffff, 0, 0);
408 touchscreen_parse_properties(input, true, &priv->prop);
410 error = input_mt_init_slots(input, priv->chip->max_touches,
413 dev_err(dev, "Unable to set up slots, err: %d\n", error);
417 error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
418 IRQF_ONESHOT, client->name, priv);
420 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
425 error = devm_add_action_or_reset(dev, ili210x_stop, priv);
429 error = devm_device_add_group(dev, &ili210x_attr_group);
431 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
436 error = input_register_device(priv->input);
438 dev_err(dev, "Cannot register input device, err: %d\n", error);
443 "ILI210x initialized (IRQ: %d), firmware version %d.%d.%d",
444 client->irq, firmware.id, firmware.major, firmware.minor);
449 static const struct i2c_device_id ili210x_i2c_id[] = {
450 { "ili210x", (long)&ili210x_chip },
451 { "ili2117", (long)&ili211x_chip },
452 { "ili251x", (long)&ili251x_chip },
455 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
457 static const struct of_device_id ili210x_dt_ids[] = {
458 { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
459 { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
460 { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
463 MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
465 static struct i2c_driver ili210x_ts_driver = {
467 .name = "ili210x_i2c",
468 .of_match_table = ili210x_dt_ids,
470 .id_table = ili210x_i2c_id,
471 .probe = ili210x_i2c_probe,
474 module_i2c_driver(ili210x_ts_driver);
476 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
477 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
478 MODULE_LICENSE("GPL");