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