brcm2708: add linux 4.19 support
[oweals/openwrt.git] / target / linux / brcm2708 / patches-4.19 / 950-0423-Input-ili210x-fetch-touchscreen-geometry-from-DT.patch
1 From 7424847b3053bb995d52b13b6b7a387d682cde31 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 423/703] Input: ili210x - fetch touchscreen geometry from DT
5
6 commit f67cc3e927d8414ad3872e046764534ea1f5db0d upstream
7
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.
11
12 Signed-off-by: Marek Vasut <marex@denx.de>
13 Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
14 ---
15  drivers/input/touchscreen/ili210x.c | 321 +++++++++++++++++-----------
16  1 file changed, 194 insertions(+), 127 deletions(-)
17
18 --- a/drivers/input/touchscreen/ili210x.c
19 +++ b/drivers/input/touchscreen/ili210x.c
20 @@ -4,11 +4,15 @@
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>
31  
32 -#define MAX_TOUCHES            2
33 +#define ILI210X_TOUCHES                2
34 +#define ILI251X_TOUCHES                10
35  #define DEFAULT_POLL_PERIOD    20
36  
37  /* Touchscreen commands */
38 @@ -17,41 +21,32 @@
39  #define REG_FIRMWARE_VERSION   0x40
40  #define REG_CALIBRATE          0xcc
41  
42 -struct finger {
43 -       u8 x_low;
44 -       u8 x_high;
45 -       u8 y_low;
46 -       u8 y_high;
47 -} __packed;
48 -
49 -struct touchdata {
50 -       u8 status;
51 -       struct finger finger[MAX_TOUCHES];
52 -} __packed;
53 -
54 -struct panel_info {
55 -       struct finger finger_max;
56 -       u8 xchannel_num;
57 -       u8 ychannel_num;
58 -} __packed;
59 -
60  struct firmware_version {
61         u8 id;
62         u8 major;
63         u8 minor;
64  } __packed;
65  
66 +enum ili2xxx_model {
67 +       MODEL_ILI210X,
68 +       MODEL_ILI251X,
69 +};
70 +
71  struct ili210x {
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;
81  };
82  
83  static int ili210x_read_reg(struct i2c_client *client, u8 reg, void *buf,
84                             size_t len)
85  {
86 +       struct ili210x *priv = i2c_get_clientdata(client);
87         struct i2c_msg msg[2] = {
88                 {
89                         .addr   = client->addr,
90 @@ -67,7 +62,38 @@ static int ili210x_read_reg(struct i2c_c
91                 }
92         };
93  
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");
98 +                       return -EIO;
99 +               }
100 +
101 +               usleep_range(5000, 5500);
102 +
103 +               if (i2c_transfer(client->adapter, msg + 1, 1) != 1) {
104 +                       dev_err(&client->dev, "i2c transfer failed\n");
105 +                       return -EIO;
106 +               }
107 +       } else {
108 +               if (i2c_transfer(client->adapter, msg, 2) != 2) {
109 +                       dev_err(&client->dev, "i2c transfer failed\n");
110 +                       return -EIO;
111 +               }
112 +       }
113 +
114 +       return 0;
115 +}
116 +
117 +static int ili210x_read(struct i2c_client *client, void *buf, size_t len)
118 +{
119 +       struct i2c_msg msg = {
120 +               .addr   = client->addr,
121 +               .flags  = I2C_M_RD,
122 +               .len    = len,
123 +               .buf    = buf,
124 +       };
125 +
126 +       if (i2c_transfer(client->adapter, &msg, 1) != 1) {
127                 dev_err(&client->dev, "i2c transfer failed\n");
128                 return -EIO;
129         }
130 @@ -75,42 +101,72 @@ static int ili210x_read_reg(struct i2c_c
131         return 0;
132  }
133  
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)
139  {
140 -       int i;
141 -       bool touch;
142 -       unsigned int x, y;
143 -       const struct finger *finger;
144 +       if (finger >= ILI210X_TOUCHES)
145 +               return false;
146  
147 -       for (i = 0; i < MAX_TOUCHES; i++) {
148 -               input_mt_slot(input, i);
149 +       if (touchdata[0] & BIT(finger))
150 +               return false;
151  
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);
155  
156 -               touch = touchdata->status & (1 << i);
157 -               input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
158 -               if (touch) {
159 -                       x = finger->x_low | (finger->x_high << 8);
160 -                       y = finger->y_low | (finger->y_high << 8);
161 +       return true;
162 +}
163 +
164 +static bool ili251x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
165 +                                       unsigned int finger,
166 +                                       unsigned int *x, unsigned int *y)
167 +{
168 +       if (finger >= ILI251X_TOUCHES)
169 +               return false;
170 +
171 +       *x = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
172 +       if (!(*x & BIT(15)))    /* Touch indication */
173 +               return false;
174 +
175 +       *x &= 0x3fff;
176 +       *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
177 +
178 +       return true;
179 +}
180 +
181 +static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
182 +{
183 +       struct input_dev *input = priv->input;
184 +       int i;
185 +       bool contact = false, touch = false;
186 +       unsigned int x = 0, y = 0;
187  
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,
193 +                                                           i, &x, &y);
194 +               } else if (priv->model == MODEL_ILI251X) {
195 +                       touch = ili251x_touchdata_to_coords(priv, touchdata,
196 +                                                           i, &x, &y);
197 +                       if (touch)
198 +                               contact = true;
199                 }
200 +
201 +               input_mt_slot(input, i);
202 +               input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
203 +               if (!touch)
204 +                       continue;
205 +               touchscreen_report_pos(input, &priv->prop, x, y,
206 +                                      true);
207         }
208  
209         input_mt_report_pointer_emulation(input, false);
210         input_sync(input);
211 -}
212  
213 -static bool get_pendown_state(const struct ili210x *priv)
214 -{
215 -       bool state = false;
216 -
217 -       if (priv->get_pendown_state)
218 -               state = priv->get_pendown_state();
219 +       if (priv->model == MODEL_ILI210X)
220 +               contact = touchdata[0] & 0xf3;
221  
222 -       return state;
223 +       return contact;
224  }
225  
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,
229                                             dwork.work);
230         struct i2c_client *client = priv->client;
231 -       struct touchdata touchdata;
232 -       int error;
233 +       u8 touchdata[64] = { 0 };
234 +       bool touch;
235 +       int error = -EINVAL;
236 +
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,
242 +                                        touchdata, 31);
243 +               if (!error && touchdata[0] == 2)
244 +                       error = ili210x_read(client, &touchdata[31], 20);
245 +       }
246  
247 -       error = ili210x_read_reg(client, REG_TOUCHDATA,
248 -                                &touchdata, sizeof(touchdata));
249         if (error) {
250                 dev_err(&client->dev,
251                         "Unable to get touchdata, err = %d\n", error);
252                 return;
253         }
254  
255 -       ili210x_report_events(priv->input, &touchdata);
256 +       touch = ili210x_report_events(priv, touchdata);
257  
258 -       if ((touchdata.status & 0xf3) || get_pendown_state(priv))
259 +       if (touch)
260                 schedule_delayed_work(&priv->dwork,
261                                       msecs_to_jiffies(priv->poll_period));
262  }
263 @@ -180,30 +245,76 @@ static const struct attribute_group ili2
264         .attrs = ili210x_attributes,
265  };
266  
267 +static void ili210x_power_down(void *data)
268 +{
269 +       struct gpio_desc *reset_gpio = data;
270 +
271 +       gpiod_set_value_cansleep(reset_gpio, 1);
272 +}
273 +
274 +static void ili210x_cancel_work(void *data)
275 +{
276 +       struct ili210x *priv = data;
277 +
278 +       cancel_delayed_work_sync(&priv->dwork);
279 +}
280 +
281  static int ili210x_i2c_probe(struct i2c_client *client,
282                                        const struct i2c_device_id *id)
283  {
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;
291 -       int xmax, ymax;
292 +       enum ili2xxx_model model;
293         int error;
294  
295 -       dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
296 +       model = (enum ili2xxx_model)id->driver_data;
297  
298 -       if (!pdata) {
299 -               dev_err(dev, "No platform data!\n");
300 -               return -EINVAL;
301 -       }
302 +       dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
303  
304         if (client->irq <= 0) {
305                 dev_err(dev, "No IRQ!\n");
306                 return -EINVAL;
307         }
308  
309 +       reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
310 +       if (IS_ERR(reset_gpio))
311 +               return PTR_ERR(reset_gpio);
312 +
313 +       if (reset_gpio) {
314 +               error = devm_add_action_or_reset(dev, ili210x_power_down,
315 +                                                reset_gpio);
316 +               if (error)
317 +                       return error;
318 +
319 +               usleep_range(50, 100);
320 +               gpiod_set_value_cansleep(reset_gpio, 0);
321 +               msleep(100);
322 +       }
323 +
324 +       priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
325 +       if (!priv)
326 +               return -ENOMEM;
327 +
328 +       input = devm_input_allocate_device(dev);
329 +       if (!input)
330 +               return -ENOMEM;
331 +
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;
342 +
343 +       i2c_set_clientdata(client, priv);
344 +
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_
349                 return error;
350         }
351  
352 -       /* get panel info */
353 -       error = ili210x_read_reg(client, REG_PANEL_INFO, &panel, sizeof(panel));
354 -       if (error) {
355 -               dev_err(dev, "Failed to get panel information, err: %d\n",
356 -                       error);
357 -               return error;
358 -       }
359 -
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);
362 -
363 -       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
364 -       input = input_allocate_device();
365 -       if (!priv || !input) {
366 -               error = -ENOMEM;
367 -               goto err_free_mem;
368 -       }
369 -
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);
375 -
376         /* Setup input device */
377         input->name = "ILI210x Touchscreen";
378         input->id.bustype = BUS_I2C;
379         input->dev.parent = dev;
380  
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);
385 -
386 -       /* Single touch */
387 -       input_set_abs_params(input, ABS_X, 0, xmax, 0, 0);
388 -       input_set_abs_params(input, ABS_Y, 0, ymax, 0, 0);
389 -
390         /* Multi touch */
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);
398  
399 -       i2c_set_clientdata(client, priv);
400 +       error = devm_add_action(dev, ili210x_cancel_work, priv);
401 +       if (error)
402 +               return error;
403  
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);
408         if (error) {
409                 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
410                         error);
411 -               goto err_free_mem;
412 +               return error;
413         }
414  
415 -       error = sysfs_create_group(&dev->kobj, &ili210x_attr_group);
416 +       error = devm_device_add_group(dev, &ili210x_attr_group);
417         if (error) {
418                 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
419                         error);
420 -               goto err_free_irq;
421 +               return error;
422         }
423  
424         error = input_register_device(priv->input);
425         if (error) {
426                 dev_err(dev, "Cannot register input device, err: %d\n", error);
427 -               goto err_remove_sysfs;
428 +               return error;
429         }
430  
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);
434  
435         return 0;
436 -
437 -err_remove_sysfs:
438 -       sysfs_remove_group(&dev->kobj, &ili210x_attr_group);
439 -err_free_irq:
440 -       free_irq(client->irq, priv);
441 -err_free_mem:
442 -       input_free_device(input);
443 -       kfree(priv);
444 -       return error;
445 -}
446 -
447 -static int ili210x_i2c_remove(struct i2c_client *client)
448 -{
449 -       struct ili210x *priv = i2c_get_clientdata(client);
450 -
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);
455 -       kfree(priv);
456 -
457 -       return 0;
458  }
459  
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);
463  
464  static const struct i2c_device_id ili210x_i2c_id[] = {
465 -       { "ili210x", 0 },
466 +       { "ili210x", MODEL_ILI210X },
467 +       { "ili251x", MODEL_ILI251X },
468         { }
469  };
470  MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
471  
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 },
475 +       { },
476 +};
477 +MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
478 +
479  static struct i2c_driver ili210x_ts_driver = {
480         .driver = {
481                 .name = "ili210x_i2c",
482                 .pm = &ili210x_i2c_pm,
483 +               .of_match_table = ili210x_dt_ids,
484         },
485         .id_table = ili210x_i2c_id,
486         .probe = ili210x_i2c_probe,
487 -       .remove = ili210x_i2c_remove,
488  };
489  
490  module_i2c_driver(ili210x_ts_driver);