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