1 From 9c823e2ee1ec1b815b8ec29c231b112c5e397202 Mon Sep 17 00:00:00 2001
2 From: Samuel Hsu <hsu@distec.de>
3 Date: Mon, 8 Apr 2019 16:42:17 +0200
4 Subject: [PATCH] Input: ili210x - fetch touchscreen geometry from DT
6 commit f67cc3e927d8414ad3872e046764534ea1f5db0d upstream
8 Fetching the geometry from the ILI251x registers seems unreliable and
9 sometimes returns all zeroes. Add support for fetching the geometry and
10 axis inversion from DT instead.
12 Signed-off-by: Marek Vasut <marex@denx.de>
13 Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
15 drivers/input/touchscreen/ili210x.c | 321 +++++++++++++++++-----------
16 1 file changed, 194 insertions(+), 127 deletions(-)
18 --- a/drivers/input/touchscreen/ili210x.c
19 +++ b/drivers/input/touchscreen/ili210x.c
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/input/mt.h>
24 +#include <linux/input/touchscreen.h>
25 #include <linux/delay.h>
26 #include <linux/workqueue.h>
27 -#include <linux/input/ili210x.h>
28 +#include <linux/gpio/consumer.h>
29 +#include <linux/of_device.h>
30 +#include <asm/unaligned.h>
32 -#define MAX_TOUCHES 2
33 +#define ILI210X_TOUCHES 2
34 +#define ILI251X_TOUCHES 10
35 #define DEFAULT_POLL_PERIOD 20
37 /* Touchscreen commands */
39 #define REG_FIRMWARE_VERSION 0x40
40 #define REG_CALIBRATE 0xcc
51 - struct finger finger[MAX_TOUCHES];
55 - struct finger finger_max;
60 struct firmware_version {
72 struct i2c_client *client;
73 struct input_dev *input;
74 - bool (*get_pendown_state)(void);
75 unsigned int poll_period;
76 struct delayed_work dwork;
77 + struct gpio_desc *reset_gpio;
78 + struct touchscreen_properties prop;
79 + enum ili2xxx_model model;
80 + unsigned int max_touches;
83 static int ili210x_read_reg(struct i2c_client *client, u8 reg, void *buf,
86 + struct ili210x *priv = i2c_get_clientdata(client);
87 struct i2c_msg msg[2] = {
90 @@ -67,7 +62,38 @@ static int ili210x_read_reg(struct i2c_c
94 - if (i2c_transfer(client->adapter, msg, 2) != 2) {
95 + if (priv->model == MODEL_ILI251X) {
96 + if (i2c_transfer(client->adapter, msg, 1) != 1) {
97 + dev_err(&client->dev, "i2c transfer failed\n");
101 + usleep_range(5000, 5500);
103 + if (i2c_transfer(client->adapter, msg + 1, 1) != 1) {
104 + dev_err(&client->dev, "i2c transfer failed\n");
108 + if (i2c_transfer(client->adapter, msg, 2) != 2) {
109 + dev_err(&client->dev, "i2c transfer failed\n");
117 +static int ili210x_read(struct i2c_client *client, void *buf, size_t len)
119 + struct i2c_msg msg = {
120 + .addr = client->addr,
126 + if (i2c_transfer(client->adapter, &msg, 1) != 1) {
127 dev_err(&client->dev, "i2c transfer failed\n");
130 @@ -75,42 +101,72 @@ static int ili210x_read_reg(struct i2c_c
134 -static void ili210x_report_events(struct input_dev *input,
135 - const struct touchdata *touchdata)
136 +static bool ili210x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
137 + unsigned int finger,
138 + unsigned int *x, unsigned int *y)
143 - const struct finger *finger;
144 + if (finger >= ILI210X_TOUCHES)
147 - for (i = 0; i < MAX_TOUCHES; i++) {
148 - input_mt_slot(input, i);
149 + if (touchdata[0] & BIT(finger))
152 - finger = &touchdata->finger[i];
153 + *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
154 + *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
156 - touch = touchdata->status & (1 << i);
157 - input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
159 - x = finger->x_low | (finger->x_high << 8);
160 - y = finger->y_low | (finger->y_high << 8);
164 +static bool ili251x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
165 + unsigned int finger,
166 + unsigned int *x, unsigned int *y)
168 + if (finger >= ILI251X_TOUCHES)
171 + *x = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
172 + if (!(*x & BIT(15))) /* Touch indication */
176 + *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
181 +static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
183 + struct input_dev *input = priv->input;
185 + bool contact = false, touch = false;
186 + unsigned int x = 0, y = 0;
188 - input_report_abs(input, ABS_MT_POSITION_X, x);
189 - input_report_abs(input, ABS_MT_POSITION_Y, y);
190 + for (i = 0; i < priv->max_touches; i++) {
191 + if (priv->model == MODEL_ILI210X) {
192 + touch = ili210x_touchdata_to_coords(priv, touchdata,
194 + } else if (priv->model == MODEL_ILI251X) {
195 + touch = ili251x_touchdata_to_coords(priv, touchdata,
201 + input_mt_slot(input, i);
202 + input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
205 + touchscreen_report_pos(input, &priv->prop, x, y,
209 input_mt_report_pointer_emulation(input, false);
213 -static bool get_pendown_state(const struct ili210x *priv)
215 - bool state = false;
217 - if (priv->get_pendown_state)
218 - state = priv->get_pendown_state();
219 + if (priv->model == MODEL_ILI210X)
220 + contact = touchdata[0] & 0xf3;
226 static void ili210x_work(struct work_struct *work)
227 @@ -118,20 +174,29 @@ static void ili210x_work(struct work_str
228 struct ili210x *priv = container_of(work, struct ili210x,
230 struct i2c_client *client = priv->client;
231 - struct touchdata touchdata;
233 + u8 touchdata[64] = { 0 };
235 + int error = -EINVAL;
237 + if (priv->model == MODEL_ILI210X) {
238 + error = ili210x_read_reg(client, REG_TOUCHDATA,
239 + touchdata, sizeof(touchdata));
240 + } else if (priv->model == MODEL_ILI251X) {
241 + error = ili210x_read_reg(client, REG_TOUCHDATA,
243 + if (!error && touchdata[0] == 2)
244 + error = ili210x_read(client, &touchdata[31], 20);
247 - error = ili210x_read_reg(client, REG_TOUCHDATA,
248 - &touchdata, sizeof(touchdata));
250 dev_err(&client->dev,
251 "Unable to get touchdata, err = %d\n", error);
255 - ili210x_report_events(priv->input, &touchdata);
256 + touch = ili210x_report_events(priv, touchdata);
258 - if ((touchdata.status & 0xf3) || get_pendown_state(priv))
260 schedule_delayed_work(&priv->dwork,
261 msecs_to_jiffies(priv->poll_period));
263 @@ -180,30 +245,76 @@ static const struct attribute_group ili2
264 .attrs = ili210x_attributes,
267 +static void ili210x_power_down(void *data)
269 + struct gpio_desc *reset_gpio = data;
271 + gpiod_set_value_cansleep(reset_gpio, 1);
274 +static void ili210x_cancel_work(void *data)
276 + struct ili210x *priv = data;
278 + cancel_delayed_work_sync(&priv->dwork);
281 static int ili210x_i2c_probe(struct i2c_client *client,
282 const struct i2c_device_id *id)
284 struct device *dev = &client->dev;
285 - const struct ili210x_platform_data *pdata = dev_get_platdata(dev);
286 struct ili210x *priv;
287 + struct gpio_desc *reset_gpio;
288 struct input_dev *input;
289 - struct panel_info panel;
290 struct firmware_version firmware;
292 + enum ili2xxx_model model;
295 - dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
296 + model = (enum ili2xxx_model)id->driver_data;
299 - dev_err(dev, "No platform data!\n");
302 + dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
304 if (client->irq <= 0) {
305 dev_err(dev, "No IRQ!\n");
309 + reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
310 + if (IS_ERR(reset_gpio))
311 + return PTR_ERR(reset_gpio);
314 + error = devm_add_action_or_reset(dev, ili210x_power_down,
319 + usleep_range(50, 100);
320 + gpiod_set_value_cansleep(reset_gpio, 0);
324 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
328 + input = devm_input_allocate_device(dev);
332 + priv->client = client;
333 + priv->input = input;
334 + priv->poll_period = DEFAULT_POLL_PERIOD;
335 + INIT_DELAYED_WORK(&priv->dwork, ili210x_work);
336 + priv->reset_gpio = reset_gpio;
337 + priv->model = model;
338 + if (model == MODEL_ILI210X)
339 + priv->max_touches = ILI210X_TOUCHES;
340 + if (model == MODEL_ILI251X)
341 + priv->max_touches = ILI251X_TOUCHES;
343 + i2c_set_clientdata(client, priv);
345 /* Get firmware version */
346 error = ili210x_read_reg(client, REG_FIRMWARE_VERSION,
347 &firmware, sizeof(firmware));
348 @@ -213,70 +324,40 @@ static int ili210x_i2c_probe(struct i2c_
352 - /* get panel info */
353 - error = ili210x_read_reg(client, REG_PANEL_INFO, &panel, sizeof(panel));
355 - dev_err(dev, "Failed to get panel information, err: %d\n",
360 - xmax = panel.finger_max.x_low | (panel.finger_max.x_high << 8);
361 - ymax = panel.finger_max.y_low | (panel.finger_max.y_high << 8);
363 - priv = kzalloc(sizeof(*priv), GFP_KERNEL);
364 - input = input_allocate_device();
365 - if (!priv || !input) {
370 - priv->client = client;
371 - priv->input = input;
372 - priv->get_pendown_state = pdata->get_pendown_state;
373 - priv->poll_period = pdata->poll_period ? : DEFAULT_POLL_PERIOD;
374 - INIT_DELAYED_WORK(&priv->dwork, ili210x_work);
376 /* Setup input device */
377 input->name = "ILI210x Touchscreen";
378 input->id.bustype = BUS_I2C;
379 input->dev.parent = dev;
381 - __set_bit(EV_SYN, input->evbit);
382 - __set_bit(EV_KEY, input->evbit);
383 - __set_bit(EV_ABS, input->evbit);
384 - __set_bit(BTN_TOUCH, input->keybit);
387 - input_set_abs_params(input, ABS_X, 0, xmax, 0, 0);
388 - input_set_abs_params(input, ABS_Y, 0, ymax, 0, 0);
391 - input_mt_init_slots(input, MAX_TOUCHES, 0);
392 - input_set_abs_params(input, ABS_MT_POSITION_X, 0, xmax, 0, 0);
393 - input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ymax, 0, 0);
394 + input_set_abs_params(input, ABS_MT_POSITION_X, 0, 0xffff, 0, 0);
395 + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 0xffff, 0, 0);
396 + touchscreen_parse_properties(input, true, &priv->prop);
397 + input_mt_init_slots(input, priv->max_touches, INPUT_MT_DIRECT);
399 - i2c_set_clientdata(client, priv);
400 + error = devm_add_action(dev, ili210x_cancel_work, priv);
404 - error = request_irq(client->irq, ili210x_irq, pdata->irq_flags,
405 - client->name, priv);
406 + error = devm_request_irq(dev, client->irq, ili210x_irq, 0,
407 + client->name, priv);
409 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
415 - error = sysfs_create_group(&dev->kobj, &ili210x_attr_group);
416 + error = devm_device_add_group(dev, &ili210x_attr_group);
418 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
424 error = input_register_device(priv->input);
426 dev_err(dev, "Cannot register input device, err: %d\n", error);
427 - goto err_remove_sysfs;
431 device_init_wakeup(dev, 1);
432 @@ -286,28 +367,6 @@ static int ili210x_i2c_probe(struct i2c_
433 client->irq, firmware.id, firmware.major, firmware.minor);
438 - sysfs_remove_group(&dev->kobj, &ili210x_attr_group);
440 - free_irq(client->irq, priv);
442 - input_free_device(input);
447 -static int ili210x_i2c_remove(struct i2c_client *client)
449 - struct ili210x *priv = i2c_get_clientdata(client);
451 - sysfs_remove_group(&client->dev.kobj, &ili210x_attr_group);
452 - free_irq(priv->client->irq, priv);
453 - cancel_delayed_work_sync(&priv->dwork);
454 - input_unregister_device(priv->input);
460 static int __maybe_unused ili210x_i2c_suspend(struct device *dev)
461 @@ -334,19 +393,27 @@ static SIMPLE_DEV_PM_OPS(ili210x_i2c_pm,
462 ili210x_i2c_suspend, ili210x_i2c_resume);
464 static const struct i2c_device_id ili210x_i2c_id[] = {
466 + { "ili210x", MODEL_ILI210X },
467 + { "ili251x", MODEL_ILI251X },
470 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
472 +static const struct of_device_id ili210x_dt_ids[] = {
473 + { .compatible = "ilitek,ili210x", .data = (void *)MODEL_ILI210X },
474 + { .compatible = "ilitek,ili251x", .data = (void *)MODEL_ILI251X },
477 +MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
479 static struct i2c_driver ili210x_ts_driver = {
481 .name = "ili210x_i2c",
482 .pm = &ili210x_i2c_pm,
483 + .of_match_table = ili210x_dt_ids,
485 .id_table = ili210x_i2c_id,
486 .probe = ili210x_i2c_probe,
487 - .remove = ili210x_i2c_remove,
490 module_i2c_driver(ili210x_ts_driver);