1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for I2C connected EETI EXC3000 multiple touch controller
7 * minimal implementation based on egalax_ts.c and egalax_i2c.c
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>
21 #include <linux/sizes.h>
22 #include <linux/timer.h>
23 #include <asm/unaligned.h>
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
31 #define EXC3000_LEN_MODEL_NAME 16
32 #define EXC3000_LEN_FW_VERSION 16
34 #define EXC3000_VENDOR_EVENT 0x03
35 #define EXC3000_MT1_EVENT 0x06
36 #define EXC3000_MT2_EVENT 0x18
38 #define EXC3000_TIMEOUT_MS 100
40 #define EXC3000_RESET_MS 10
41 #define EXC3000_READY_MS 100
43 static const struct i2c_device_id exc3000_id[];
45 struct eeti_dev_info {
56 static struct eeti_dev_info exc3000_info[] = {
58 .name = "EETI EXC3000 Touch Screen",
62 .name = "EETI EXC80H60 Touch Screen",
66 .name = "EETI EXC80H84 Touch Screen",
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;
83 static void exc3000_report_slots(struct input_dev *input,
84 struct touchscreen_properties *prop,
85 const u8 *buf, int num)
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),
99 static void exc3000_timer(struct timer_list *t)
101 struct exc3000_data *data = from_timer(data, t, timer);
103 input_mt_sync_frame(data->input);
104 input_sync(data->input);
107 static inline void exc3000_schedule_timer(struct exc3000_data *data)
109 mod_timer(&data->timer, jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS));
112 static int exc3000_read_frame(struct exc3000_data *data, u8 *buf)
114 struct i2c_client *client = data->client;
117 ret = i2c_master_send(client, "'", 2);
124 ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME);
128 if (ret != EXC3000_LEN_FRAME)
131 if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME)
137 static int exc3000_handle_mt_event(struct exc3000_data *data)
139 struct input_dev *input = data->input;
140 int ret, total_slots;
143 total_slots = buf[3];
144 if (!total_slots || total_slots > EXC3000_NUM_SLOTS) {
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);
155 /* 2nd chunk must have number of contacts set to 0. */
156 if (buf[EXC3000_LEN_FRAME + 3] != 0) {
163 * We read full state successfully, no contacts will be "stuck".
165 del_timer_sync(&data->timer);
167 while (total_slots > 0) {
168 int slots = min(total_slots, EXC3000_SLOTS_PER_FRAME);
170 exc3000_report_slots(input, &data->prop, buf + 4, slots);
171 total_slots -= slots;
172 buf += EXC3000_LEN_FRAME;
175 input_mt_sync_frame(input);
181 /* Schedule a timer to release "stuck" contacts */
182 exc3000_schedule_timer(data);
187 static irqreturn_t exc3000_interrupt(int irq, void *dev_id)
189 struct exc3000_data *data = dev_id;
193 ret = exc3000_read_frame(data, buf);
195 /* Schedule a timer to release "stuck" contacts */
196 exc3000_schedule_timer(data);
201 case EXC3000_VENDOR_EVENT:
202 complete(&data->wait_event);
205 case EXC3000_MT1_EVENT:
206 case EXC3000_MT2_EVENT:
207 exc3000_handle_mt_event(data);
218 static int exc3000_vendor_data_request(struct exc3000_data *data, u8 *request,
219 u8 request_len, u8 *response, int timeout)
221 u8 buf[EXC3000_LEN_VENDOR_REQUEST] = { 0x67, 0x00, 0x42, 0x00, 0x03 };
223 unsigned long time_left;
225 mutex_lock(&data->query_lock);
227 reinit_completion(&data->wait_event);
229 buf[5] = request_len;
230 memcpy(&buf[6], request, request_len);
232 ret = i2c_master_send(data->client, buf, EXC3000_LEN_VENDOR_REQUEST);
237 time_left = wait_for_completion_timeout(&data->wait_event,
239 if (time_left == 0) {
244 if (data->buf[3] >= EXC3000_LEN_FRAME) {
249 memcpy(response, &data->buf[4], data->buf[3]);
254 mutex_unlock(&data->query_lock);
259 static ssize_t fw_version_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
262 struct i2c_client *client = to_i2c_client(dev);
263 struct exc3000_data *data = i2c_get_clientdata(client);
264 u8 response[EXC3000_LEN_FRAME];
267 /* query bootloader info */
268 ret = exc3000_vendor_data_request(data,
269 (u8[]){0x39, 0x02}, 2, response, 1);
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.
278 if (response[2] || response[3])
279 return sprintf(buf, "%d.%d\n", response[2], response[3]);
281 ret = exc3000_vendor_data_request(data, (u8[]){'D'}, 1, response, 1);
285 return sprintf(buf, "%s\n", &response[1]);
287 static DEVICE_ATTR_RO(fw_version);
289 static ssize_t model_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct i2c_client *client = to_i2c_client(dev);
293 struct exc3000_data *data = i2c_get_clientdata(client);
294 u8 response[EXC3000_LEN_FRAME];
297 ret = exc3000_vendor_data_request(data, (u8[]){'E'}, 1, response, 1);
301 return sprintf(buf, "%s\n", &response[1]);
303 static DEVICE_ATTR_RO(model);
305 static ssize_t type_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct i2c_client *client = to_i2c_client(dev);
309 struct exc3000_data *data = i2c_get_clientdata(client);
310 u8 response[EXC3000_LEN_FRAME];
313 ret = exc3000_vendor_data_request(data, (u8[]){'F'}, 1, response, 1);
317 return sprintf(buf, "%s\n", &response[1]);
319 static DEVICE_ATTR_RO(type);
321 static struct attribute *sysfs_attrs[] = {
322 &dev_attr_fw_version.attr,
323 &dev_attr_model.attr,
328 static struct attribute_group exc3000_attribute_group = {
332 static int exc3000_probe(struct i2c_client *client)
334 struct exc3000_data *data;
335 struct input_dev *input;
336 int error, max_xy, retry;
338 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
342 data->client = client;
343 data->info = device_get_match_data(&client->dev);
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];
349 timer_setup(&data->timer, exc3000_timer, 0);
350 init_completion(&data->wait_event);
351 mutex_init(&data->query_lock);
353 data->reset = devm_gpiod_get_optional(&client->dev, "reset",
355 if (IS_ERR(data->reset))
356 return PTR_ERR(data->reset);
359 msleep(EXC3000_RESET_MS);
360 gpiod_set_value_cansleep(data->reset, 0);
361 msleep(EXC3000_READY_MS);
364 input = devm_input_allocate_device(&client->dev);
369 input_set_drvdata(input, data);
371 input->name = data->info->name;
372 input->id.bustype = BUS_I2C;
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);
378 touchscreen_parse_properties(input, true, &data->prop);
380 error = input_mt_init_slots(input, EXC3000_NUM_SLOTS,
381 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
385 error = input_register_device(input);
389 error = devm_request_threaded_irq(&client->dev, client->irq,
390 NULL, exc3000_interrupt, IRQF_ONESHOT,
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).
403 for (retry = 0; retry < 3; retry++) {
404 u8 response[EXC3000_LEN_FRAME];
406 error = exc3000_vendor_data_request(data, (u8[]){'E'}, 1,
409 dev_dbg(&client->dev, "TS Model: %s", &response[1]);
413 dev_warn(&client->dev, "Retry %d get EETI EXC3000 model: %d\n",
420 i2c_set_clientdata(client, data);
422 error = devm_device_add_group(&client->dev, &exc3000_attribute_group);
429 static const struct i2c_device_id exc3000_id[] = {
430 { "exc3000", EETI_EXC3000 },
431 { "exc80h60", EETI_EXC80H60 },
432 { "exc80h84", EETI_EXC80H84 },
435 MODULE_DEVICE_TABLE(i2c, exc3000_id);
438 static 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] },
444 MODULE_DEVICE_TABLE(of, exc3000_of_match);
447 static struct i2c_driver exc3000_driver = {
450 .of_match_table = of_match_ptr(exc3000_of_match),
452 .id_table = exc3000_id,
453 .probe_new = exc3000_probe,
456 module_i2c_driver(exc3000_driver);
459 MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver");
460 MODULE_LICENSE("GPL v2");