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