Input: ili210x - do not unconditionally mark touchscreen as wakeup source
[platform/kernel/linux-starfive.git] / drivers / input / touchscreen / ili210x.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/delay.h>
3 #include <linux/gpio/consumer.h>
4 #include <linux/i2c.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>
13
14 #define ILI2XXX_POLL_PERIOD     20
15
16 #define ILI210X_DATA_SIZE       64
17 #define ILI211X_DATA_SIZE       43
18 #define ILI251X_DATA_SIZE1      31
19 #define ILI251X_DATA_SIZE2      20
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
26
27 struct firmware_version {
28         u8 id;
29         u8 major;
30         u8 minor;
31 } __packed;
32
33 struct ili2xxx_chip {
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;
41 };
42
43 struct ili210x {
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;
49         bool stop;
50 };
51
52 static int ili210x_read_reg(struct i2c_client *client,
53                             u8 reg, void *buf, size_t len)
54 {
55         struct i2c_msg msg[] = {
56                 {
57                         .addr   = client->addr,
58                         .flags  = 0,
59                         .len    = 1,
60                         .buf    = &reg,
61                 },
62                 {
63                         .addr   = client->addr,
64                         .flags  = I2C_M_RD,
65                         .len    = len,
66                         .buf    = buf,
67                 }
68         };
69         int error, ret;
70
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);
75                 return error;
76         }
77
78         return 0;
79 }
80
81 static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
82 {
83         return ili210x_read_reg(client, REG_TOUCHDATA,
84                                 data, ILI210X_DATA_SIZE);
85 }
86
87 static bool ili210x_touchdata_to_coords(const u8 *touchdata,
88                                         unsigned int finger,
89                                         unsigned int *x, unsigned int *y)
90 {
91         if (touchdata[0] & BIT(finger))
92                 return false;
93
94         *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
95         *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
96
97         return true;
98 }
99
100 static bool ili210x_check_continue_polling(const u8 *data, bool touch)
101 {
102         return data[0] & 0xf3;
103 }
104
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,
110         .max_touches            = 2,
111 };
112
113 static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
114 {
115         s16 sum = 0;
116         int error;
117         int ret;
118         int i;
119
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);
124                 return error;
125         }
126
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;
130
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]);
135                 return -EIO;
136         }
137
138         return 0;
139 }
140
141 static bool ili211x_touchdata_to_coords(const u8 *touchdata,
142                                         unsigned int finger,
143                                         unsigned int *x, unsigned int *y)
144 {
145         u32 data;
146
147         data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
148         if (data == 0xffffffff) /* Finger up */
149                 return false;
150
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];
155
156         return true;
157 }
158
159 static bool ili211x_decline_polling(const u8 *data, bool touch)
160 {
161         return false;
162 }
163
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,
169         .max_touches            = 10,
170 };
171
172 static int ili251x_read_reg(struct i2c_client *client,
173                             u8 reg, void *buf, size_t len)
174 {
175         int error;
176         int ret;
177
178         ret = i2c_master_send(client, &reg, 1);
179         if (ret == 1) {
180                 usleep_range(5000, 5500);
181
182                 ret = i2c_master_recv(client, buf, len);
183                 if (ret == len)
184                         return 0;
185         }
186
187         error = ret < 0 ? ret : -EIO;
188         dev_err(&client->dev, "%s failed: %d\n", __func__, error);
189         return ret;
190 }
191
192 static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
193 {
194         int error;
195
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,
200                                         ILI251X_DATA_SIZE2);
201                 if (error >= 0 && error != ILI251X_DATA_SIZE2)
202                         error = -EIO;
203         }
204
205         return error;
206 }
207
208 static bool ili251x_touchdata_to_coords(const u8 *touchdata,
209                                         unsigned int finger,
210                                         unsigned int *x, unsigned int *y)
211 {
212         u16 val;
213
214         val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
215         if (!(val & BIT(15)))   /* Touch indication */
216                 return false;
217
218         *x = val & 0x3fff;
219         *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
220
221         return true;
222 }
223
224 static bool ili251x_check_continue_polling(const u8 *data, bool touch)
225 {
226         return touch;
227 }
228
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,
234         .max_touches            = 10,
235 };
236
237 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
238 {
239         struct input_dev *input = priv->input;
240         int i;
241         bool contact = false, touch;
242         unsigned int x = 0, y = 0;
243
244         for (i = 0; i < priv->chip->max_touches; i++) {
245                 touch = priv->chip->parse_touch_data(touchdata, i, &x, &y);
246
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);
250                         contact = true;
251                 }
252         }
253
254         input_mt_report_pointer_emulation(input, false);
255         input_sync(input);
256
257         return contact;
258 }
259
260 static irqreturn_t ili210x_irq(int irq, void *irq_data)
261 {
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 };
266         bool keep_polling;
267         bool touch;
268         int error;
269
270         do {
271                 error = chip->get_touch_data(client, touchdata);
272                 if (error) {
273                         dev_err(&client->dev,
274                                 "Unable to get touch data: %d\n", error);
275                         break;
276                 }
277
278                 touch = ili210x_report_events(priv, touchdata);
279                 keep_polling = chip->continue_polling(touchdata, touch);
280                 if (keep_polling)
281                         msleep(ILI2XXX_POLL_PERIOD);
282         } while (!priv->stop && keep_polling);
283
284         return IRQ_HANDLED;
285 }
286
287 static ssize_t ili210x_calibrate(struct device *dev,
288                                  struct device_attribute *attr,
289                                  const char *buf, size_t count)
290 {
291         struct i2c_client *client = to_i2c_client(dev);
292         struct ili210x *priv = i2c_get_clientdata(client);
293         unsigned long calibrate;
294         int rc;
295         u8 cmd = REG_CALIBRATE;
296
297         if (kstrtoul(buf, 10, &calibrate))
298                 return -EINVAL;
299
300         if (calibrate > 1)
301                 return -EINVAL;
302
303         if (calibrate) {
304                 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
305                 if (rc != sizeof(cmd))
306                         return -EIO;
307         }
308
309         return count;
310 }
311 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
312
313 static struct attribute *ili210x_attributes[] = {
314         &dev_attr_calibrate.attr,
315         NULL,
316 };
317
318 static const struct attribute_group ili210x_attr_group = {
319         .attrs = ili210x_attributes,
320 };
321
322 static void ili210x_power_down(void *data)
323 {
324         struct gpio_desc *reset_gpio = data;
325
326         gpiod_set_value_cansleep(reset_gpio, 1);
327 }
328
329 static void ili210x_stop(void *data)
330 {
331         struct ili210x *priv = data;
332
333         /* Tell ISR to quit even if there is a contact. */
334         priv->stop = true;
335 }
336
337 static int ili210x_i2c_probe(struct i2c_client *client,
338                              const struct i2c_device_id *id)
339 {
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;
346         int error;
347
348         dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
349
350         chip = device_get_match_data(dev);
351         if (!chip && id)
352                 chip = (const struct ili2xxx_chip *)id->driver_data;
353         if (!chip) {
354                 dev_err(&client->dev, "unknown device model\n");
355                 return -ENODEV;
356         }
357
358         if (client->irq <= 0) {
359                 dev_err(dev, "No IRQ!\n");
360                 return -EINVAL;
361         }
362
363         reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
364         if (IS_ERR(reset_gpio))
365                 return PTR_ERR(reset_gpio);
366
367         if (reset_gpio) {
368                 error = devm_add_action_or_reset(dev, ili210x_power_down,
369                                                  reset_gpio);
370                 if (error)
371                         return error;
372
373                 usleep_range(50, 100);
374                 gpiod_set_value_cansleep(reset_gpio, 0);
375                 msleep(100);
376         }
377
378         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
379         if (!priv)
380                 return -ENOMEM;
381
382         input = devm_input_allocate_device(dev);
383         if (!input)
384                 return -ENOMEM;
385
386         priv->client = client;
387         priv->input = input;
388         priv->reset_gpio = reset_gpio;
389         priv->chip = chip;
390         i2c_set_clientdata(client, priv);
391
392         /* Get firmware version */
393         error = chip->read_reg(client, REG_FIRMWARE_VERSION,
394                                &firmware, sizeof(firmware));
395         if (error) {
396                 dev_err(dev, "Failed to get firmware version, err: %d\n",
397                         error);
398                 return error;
399         }
400
401         /* Setup input device */
402         input->name = "ILI210x Touchscreen";
403         input->id.bustype = BUS_I2C;
404
405         /* Multi touch */
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);
409
410         error = input_mt_init_slots(input, priv->chip->max_touches,
411                                     INPUT_MT_DIRECT);
412         if (error) {
413                 dev_err(dev, "Unable to set up slots, err: %d\n", error);
414                 return error;
415         }
416
417         error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
418                                           IRQF_ONESHOT, client->name, priv);
419         if (error) {
420                 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
421                         error);
422                 return error;
423         }
424
425         error = devm_add_action_or_reset(dev, ili210x_stop, priv);
426         if (error)
427                 return error;
428
429         error = devm_device_add_group(dev, &ili210x_attr_group);
430         if (error) {
431                 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
432                         error);
433                 return error;
434         }
435
436         error = input_register_device(priv->input);
437         if (error) {
438                 dev_err(dev, "Cannot register input device, err: %d\n", error);
439                 return error;
440         }
441
442         dev_dbg(dev,
443                 "ILI210x initialized (IRQ: %d), firmware version %d.%d.%d",
444                 client->irq, firmware.id, firmware.major, firmware.minor);
445
446         return 0;
447 }
448
449 static int __maybe_unused ili210x_i2c_suspend(struct device *dev)
450 {
451         struct i2c_client *client = to_i2c_client(dev);
452
453         if (device_may_wakeup(&client->dev))
454                 enable_irq_wake(client->irq);
455
456         return 0;
457 }
458
459 static int __maybe_unused ili210x_i2c_resume(struct device *dev)
460 {
461         struct i2c_client *client = to_i2c_client(dev);
462
463         if (device_may_wakeup(&client->dev))
464                 disable_irq_wake(client->irq);
465
466         return 0;
467 }
468
469 static SIMPLE_DEV_PM_OPS(ili210x_i2c_pm,
470                          ili210x_i2c_suspend, ili210x_i2c_resume);
471
472 static const struct i2c_device_id ili210x_i2c_id[] = {
473         { "ili210x", (long)&ili210x_chip },
474         { "ili2117", (long)&ili211x_chip },
475         { "ili251x", (long)&ili251x_chip },
476         { }
477 };
478 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
479
480 static const struct of_device_id ili210x_dt_ids[] = {
481         { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
482         { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
483         { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
484         { }
485 };
486 MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
487
488 static struct i2c_driver ili210x_ts_driver = {
489         .driver = {
490                 .name = "ili210x_i2c",
491                 .pm = &ili210x_i2c_pm,
492                 .of_match_table = ili210x_dt_ids,
493         },
494         .id_table = ili210x_i2c_id,
495         .probe = ili210x_i2c_probe,
496 };
497
498 module_i2c_driver(ili210x_ts_driver);
499
500 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
501 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
502 MODULE_LICENSE("GPL");