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