Merge drm/drm-next into drm-misc-next
[sfrench/cifs-2.6.git] / drivers / input / touchscreen / exc3000.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for I2C connected EETI EXC3000 multiple touch controller
4  *
5  * Copyright (C) 2017 Ahmet Inan <inan@distec.de>
6  *
7  * minimal implementation based on egalax_ts.c and egalax_i2c.c
8  */
9
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/input/mt.h>
17 #include <linux/input/touchscreen.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/sizes.h>
22 #include <linux/timer.h>
23 #include <asm/unaligned.h>
24
25 #define EXC3000_NUM_SLOTS               10
26 #define EXC3000_SLOTS_PER_FRAME         5
27 #define EXC3000_LEN_FRAME               66
28 #define EXC3000_LEN_VENDOR_REQUEST      68
29 #define EXC3000_LEN_POINT               10
30
31 #define EXC3000_LEN_MODEL_NAME          16
32 #define EXC3000_LEN_FW_VERSION          16
33
34 #define EXC3000_VENDOR_EVENT            0x03
35 #define EXC3000_MT1_EVENT               0x06
36 #define EXC3000_MT2_EVENT               0x18
37
38 #define EXC3000_TIMEOUT_MS              100
39
40 #define EXC3000_RESET_MS                10
41 #define EXC3000_READY_MS                100
42
43 static const struct i2c_device_id exc3000_id[];
44
45 struct eeti_dev_info {
46         const char *name;
47         int max_xy;
48 };
49
50 enum eeti_dev_id {
51         EETI_EXC3000,
52         EETI_EXC80H60,
53         EETI_EXC80H84,
54 };
55
56 static struct eeti_dev_info exc3000_info[] = {
57         [EETI_EXC3000] = {
58                 .name = "EETI EXC3000 Touch Screen",
59                 .max_xy = SZ_4K - 1,
60         },
61         [EETI_EXC80H60] = {
62                 .name = "EETI EXC80H60 Touch Screen",
63                 .max_xy = SZ_16K - 1,
64         },
65         [EETI_EXC80H84] = {
66                 .name = "EETI EXC80H84 Touch Screen",
67                 .max_xy = SZ_16K - 1,
68         },
69 };
70
71 struct exc3000_data {
72         struct i2c_client *client;
73         const struct eeti_dev_info *info;
74         struct input_dev *input;
75         struct touchscreen_properties prop;
76         struct gpio_desc *reset;
77         struct timer_list timer;
78         u8 buf[2 * EXC3000_LEN_FRAME];
79         struct completion wait_event;
80         struct mutex query_lock;
81 };
82
83 static void exc3000_report_slots(struct input_dev *input,
84                                  struct touchscreen_properties *prop,
85                                  const u8 *buf, int num)
86 {
87         for (; num--; buf += EXC3000_LEN_POINT) {
88                 if (buf[0] & BIT(0)) {
89                         input_mt_slot(input, buf[1]);
90                         input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
91                         touchscreen_report_pos(input, prop,
92                                                get_unaligned_le16(buf + 2),
93                                                get_unaligned_le16(buf + 4),
94                                                true);
95                 }
96         }
97 }
98
99 static void exc3000_timer(struct timer_list *t)
100 {
101         struct exc3000_data *data = from_timer(data, t, timer);
102
103         input_mt_sync_frame(data->input);
104         input_sync(data->input);
105 }
106
107 static inline void exc3000_schedule_timer(struct exc3000_data *data)
108 {
109         mod_timer(&data->timer, jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS));
110 }
111
112 static void exc3000_shutdown_timer(void *timer)
113 {
114         timer_shutdown_sync(timer);
115 }
116
117 static int exc3000_read_frame(struct exc3000_data *data, u8 *buf)
118 {
119         struct i2c_client *client = data->client;
120         int ret;
121
122         ret = i2c_master_send(client, "'", 2);
123         if (ret < 0)
124                 return ret;
125
126         if (ret != 2)
127                 return -EIO;
128
129         ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME);
130         if (ret < 0)
131                 return ret;
132
133         if (ret != EXC3000_LEN_FRAME)
134                 return -EIO;
135
136         if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME)
137                 return -EINVAL;
138
139         return 0;
140 }
141
142 static int exc3000_handle_mt_event(struct exc3000_data *data)
143 {
144         struct input_dev *input = data->input;
145         int ret, total_slots;
146         u8 *buf = data->buf;
147
148         total_slots = buf[3];
149         if (!total_slots || total_slots > EXC3000_NUM_SLOTS) {
150                 ret = -EINVAL;
151                 goto out_fail;
152         }
153
154         if (total_slots > EXC3000_SLOTS_PER_FRAME) {
155                 /* Read 2nd frame to get the rest of the contacts. */
156                 ret = exc3000_read_frame(data, buf + EXC3000_LEN_FRAME);
157                 if (ret)
158                         goto out_fail;
159
160                 /* 2nd chunk must have number of contacts set to 0. */
161                 if (buf[EXC3000_LEN_FRAME + 3] != 0) {
162                         ret = -EINVAL;
163                         goto out_fail;
164                 }
165         }
166
167         /*
168          * We read full state successfully, no contacts will be "stuck".
169          */
170         del_timer_sync(&data->timer);
171
172         while (total_slots > 0) {
173                 int slots = min(total_slots, EXC3000_SLOTS_PER_FRAME);
174
175                 exc3000_report_slots(input, &data->prop, buf + 4, slots);
176                 total_slots -= slots;
177                 buf += EXC3000_LEN_FRAME;
178         }
179
180         input_mt_sync_frame(input);
181         input_sync(input);
182
183         return 0;
184
185 out_fail:
186         /* Schedule a timer to release "stuck" contacts */
187         exc3000_schedule_timer(data);
188
189         return ret;
190 }
191
192 static irqreturn_t exc3000_interrupt(int irq, void *dev_id)
193 {
194         struct exc3000_data *data = dev_id;
195         u8 *buf = data->buf;
196         int ret;
197
198         ret = exc3000_read_frame(data, buf);
199         if (ret) {
200                 /* Schedule a timer to release "stuck" contacts */
201                 exc3000_schedule_timer(data);
202                 goto out;
203         }
204
205         switch (buf[2]) {
206         case EXC3000_VENDOR_EVENT:
207                 complete(&data->wait_event);
208                 break;
209
210         case EXC3000_MT1_EVENT:
211         case EXC3000_MT2_EVENT:
212                 exc3000_handle_mt_event(data);
213                 break;
214
215         default:
216                 break;
217         }
218
219 out:
220         return IRQ_HANDLED;
221 }
222
223 static int exc3000_vendor_data_request(struct exc3000_data *data, u8 *request,
224                                        u8 request_len, u8 *response, int timeout)
225 {
226         u8 buf[EXC3000_LEN_VENDOR_REQUEST] = { 0x67, 0x00, 0x42, 0x00, 0x03 };
227         int ret;
228         unsigned long time_left;
229
230         mutex_lock(&data->query_lock);
231
232         reinit_completion(&data->wait_event);
233
234         buf[5] = request_len;
235         memcpy(&buf[6], request, request_len);
236
237         ret = i2c_master_send(data->client, buf, EXC3000_LEN_VENDOR_REQUEST);
238         if (ret < 0)
239                 goto out_unlock;
240
241         if (response) {
242                 time_left = wait_for_completion_timeout(&data->wait_event,
243                                                         timeout * HZ);
244                 if (time_left == 0) {
245                         ret = -ETIMEDOUT;
246                         goto out_unlock;
247                 }
248
249                 if (data->buf[3] >= EXC3000_LEN_FRAME) {
250                         ret = -ENOSPC;
251                         goto out_unlock;
252                 }
253
254                 memcpy(response, &data->buf[4], data->buf[3]);
255                 ret = data->buf[3];
256         }
257
258 out_unlock:
259         mutex_unlock(&data->query_lock);
260
261         return ret;
262 }
263
264 static ssize_t fw_version_show(struct device *dev,
265                                struct device_attribute *attr, char *buf)
266 {
267         struct i2c_client *client = to_i2c_client(dev);
268         struct exc3000_data *data = i2c_get_clientdata(client);
269         u8 response[EXC3000_LEN_FRAME];
270         int ret;
271
272         /* query bootloader info */
273         ret = exc3000_vendor_data_request(data,
274                                           (u8[]){0x39, 0x02}, 2, response, 1);
275         if (ret < 0)
276                 return ret;
277
278         /*
279          * If the bootloader version is non-zero then the device is in
280          * bootloader mode and won't answer a query for the application FW
281          * version, so we just use the bootloader version info.
282          */
283         if (response[2] || response[3])
284                 return sprintf(buf, "%d.%d\n", response[2], response[3]);
285
286         ret = exc3000_vendor_data_request(data, (u8[]){'D'}, 1, response, 1);
287         if (ret < 0)
288                 return ret;
289
290         return sprintf(buf, "%s\n", &response[1]);
291 }
292 static DEVICE_ATTR_RO(fw_version);
293
294 static ssize_t model_show(struct device *dev,
295                           struct device_attribute *attr, char *buf)
296 {
297         struct i2c_client *client = to_i2c_client(dev);
298         struct exc3000_data *data = i2c_get_clientdata(client);
299         u8 response[EXC3000_LEN_FRAME];
300         int ret;
301
302         ret = exc3000_vendor_data_request(data, (u8[]){'E'}, 1, response, 1);
303         if (ret < 0)
304                 return ret;
305
306         return sprintf(buf, "%s\n", &response[1]);
307 }
308 static DEVICE_ATTR_RO(model);
309
310 static ssize_t type_show(struct device *dev,
311                           struct device_attribute *attr, char *buf)
312 {
313         struct i2c_client *client = to_i2c_client(dev);
314         struct exc3000_data *data = i2c_get_clientdata(client);
315         u8 response[EXC3000_LEN_FRAME];
316         int ret;
317
318         ret = exc3000_vendor_data_request(data, (u8[]){'F'}, 1, response, 1);
319         if (ret < 0)
320                 return ret;
321
322         return sprintf(buf, "%s\n", &response[1]);
323 }
324 static DEVICE_ATTR_RO(type);
325
326 static struct attribute *sysfs_attrs[] = {
327         &dev_attr_fw_version.attr,
328         &dev_attr_model.attr,
329         &dev_attr_type.attr,
330         NULL
331 };
332
333 static struct attribute_group exc3000_attribute_group = {
334         .attrs = sysfs_attrs
335 };
336
337 static int exc3000_probe(struct i2c_client *client)
338 {
339         struct exc3000_data *data;
340         struct input_dev *input;
341         int error, max_xy, retry;
342
343         data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
344         if (!data)
345                 return -ENOMEM;
346
347         data->client = client;
348         data->info = device_get_match_data(&client->dev);
349         if (!data->info) {
350                 enum eeti_dev_id eeti_dev_id =
351                         i2c_match_id(exc3000_id, client)->driver_data;
352                 data->info = &exc3000_info[eeti_dev_id];
353         }
354         timer_setup(&data->timer, exc3000_timer, 0);
355         init_completion(&data->wait_event);
356         mutex_init(&data->query_lock);
357
358         data->reset = devm_gpiod_get_optional(&client->dev, "reset",
359                                               GPIOD_OUT_HIGH);
360         if (IS_ERR(data->reset))
361                 return PTR_ERR(data->reset);
362
363         if (data->reset) {
364                 msleep(EXC3000_RESET_MS);
365                 gpiod_set_value_cansleep(data->reset, 0);
366                 msleep(EXC3000_READY_MS);
367         }
368
369         input = devm_input_allocate_device(&client->dev);
370         if (!input)
371                 return -ENOMEM;
372
373         data->input = input;
374         input_set_drvdata(input, data);
375
376         input->name = data->info->name;
377         input->id.bustype = BUS_I2C;
378
379         max_xy = data->info->max_xy;
380         input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
381         input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
382
383         touchscreen_parse_properties(input, true, &data->prop);
384
385         error = input_mt_init_slots(input, EXC3000_NUM_SLOTS,
386                                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
387         if (error)
388                 return error;
389
390         error = input_register_device(input);
391         if (error)
392                 return error;
393
394         error = devm_add_action_or_reset(&client->dev, exc3000_shutdown_timer,
395                                          &data->timer);
396         if (error)
397                 return error;
398
399         error = devm_request_threaded_irq(&client->dev, client->irq,
400                                           NULL, exc3000_interrupt, IRQF_ONESHOT,
401                                           client->name, data);
402         if (error)
403                 return error;
404
405         /*
406          * I²C does not have built-in recovery, so retry on failure. This
407          * ensures, that the device probe will not fail for temporary issues
408          * on the bus.  This is not needed for the sysfs calls (userspace
409          * will receive the error code and can start another query) and
410          * cannot be done for touch events (but that only means loosing one
411          * or two touch events anyways).
412          */
413         for (retry = 0; retry < 3; retry++) {
414                 u8 response[EXC3000_LEN_FRAME];
415
416                 error = exc3000_vendor_data_request(data, (u8[]){'E'}, 1,
417                                                     response, 1);
418                 if (error > 0) {
419                         dev_dbg(&client->dev, "TS Model: %s", &response[1]);
420                         error = 0;
421                         break;
422                 }
423                 dev_warn(&client->dev, "Retry %d get EETI EXC3000 model: %d\n",
424                          retry + 1, error);
425         }
426
427         if (error)
428                 return error;
429
430         i2c_set_clientdata(client, data);
431
432         error = devm_device_add_group(&client->dev, &exc3000_attribute_group);
433         if (error)
434                 return error;
435
436         return 0;
437 }
438
439 static const struct i2c_device_id exc3000_id[] = {
440         { "exc3000", EETI_EXC3000 },
441         { "exc80h60", EETI_EXC80H60 },
442         { "exc80h84", EETI_EXC80H84 },
443         { }
444 };
445 MODULE_DEVICE_TABLE(i2c, exc3000_id);
446
447 #ifdef CONFIG_OF
448 static const struct of_device_id exc3000_of_match[] = {
449         { .compatible = "eeti,exc3000", .data = &exc3000_info[EETI_EXC3000] },
450         { .compatible = "eeti,exc80h60", .data = &exc3000_info[EETI_EXC80H60] },
451         { .compatible = "eeti,exc80h84", .data = &exc3000_info[EETI_EXC80H84] },
452         { }
453 };
454 MODULE_DEVICE_TABLE(of, exc3000_of_match);
455 #endif
456
457 static struct i2c_driver exc3000_driver = {
458         .driver = {
459                 .name   = "exc3000",
460                 .of_match_table = of_match_ptr(exc3000_of_match),
461         },
462         .id_table       = exc3000_id,
463         .probe_new      = exc3000_probe,
464 };
465
466 module_i2c_driver(exc3000_driver);
467
468 MODULE_AUTHOR("Ahmet Inan <inan@distec.de>");
469 MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver");
470 MODULE_LICENSE("GPL v2");