1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
4  * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
5  * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
6  */
7 
8 /*
9  * This is a driver for the EDT "Polytouch" family of touch controllers
10  * based on the FocalTech FT5x06 line of chips.
11  *
12  * Development of this driver has been sponsored by Glyn:
13  *    http://www.glyn.com/Products/Displays
14  */
15 
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/input.h>
22 #include <linux/input/mt.h>
23 #include <linux/input/touchscreen.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/ratelimit.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 
32 #include <asm/unaligned.h>
33 
34 #define WORK_REGISTER_THRESHOLD		0x00
35 #define WORK_REGISTER_REPORT_RATE	0x08
36 #define WORK_REGISTER_GAIN		0x30
37 #define WORK_REGISTER_OFFSET		0x31
38 #define WORK_REGISTER_NUM_X		0x33
39 #define WORK_REGISTER_NUM_Y		0x34
40 
41 #define PMOD_REGISTER_ACTIVE		0x00
42 #define PMOD_REGISTER_HIBERNATE		0x03
43 
44 #define M09_REGISTER_THRESHOLD		0x80
45 #define M09_REGISTER_GAIN		0x92
46 #define M09_REGISTER_OFFSET		0x93
47 #define M09_REGISTER_NUM_X		0x94
48 #define M09_REGISTER_NUM_Y		0x95
49 
50 #define EV_REGISTER_THRESHOLD		0x40
51 #define EV_REGISTER_GAIN		0x41
52 #define EV_REGISTER_OFFSET_Y		0x45
53 #define EV_REGISTER_OFFSET_X		0x46
54 
55 #define NO_REGISTER			0xff
56 
57 #define WORK_REGISTER_OPMODE		0x3c
58 #define FACTORY_REGISTER_OPMODE		0x01
59 #define PMOD_REGISTER_OPMODE		0xa5
60 
61 #define TOUCH_EVENT_DOWN		0x00
62 #define TOUCH_EVENT_UP			0x01
63 #define TOUCH_EVENT_ON			0x02
64 #define TOUCH_EVENT_RESERVED		0x03
65 
66 #define EDT_NAME_LEN			23
67 #define EDT_SWITCH_MODE_RETRIES		10
68 #define EDT_SWITCH_MODE_DELAY		5 /* msec */
69 #define EDT_RAW_DATA_RETRIES		100
70 #define EDT_RAW_DATA_DELAY		1000 /* usec */
71 
72 enum edt_pmode {
73 	EDT_PMODE_NOT_SUPPORTED,
74 	EDT_PMODE_HIBERNATE,
75 	EDT_PMODE_POWEROFF,
76 };
77 
78 enum edt_ver {
79 	EDT_M06,
80 	EDT_M09,
81 	EDT_M12,
82 	EV_FT,
83 	GENERIC_FT,
84 };
85 
86 struct edt_reg_addr {
87 	int reg_threshold;
88 	int reg_report_rate;
89 	int reg_gain;
90 	int reg_offset;
91 	int reg_offset_x;
92 	int reg_offset_y;
93 	int reg_num_x;
94 	int reg_num_y;
95 };
96 
97 struct edt_ft5x06_ts_data {
98 	struct i2c_client *client;
99 	struct input_dev *input;
100 	struct touchscreen_properties prop;
101 	u16 num_x;
102 	u16 num_y;
103 	struct regulator *vcc;
104 
105 	struct gpio_desc *reset_gpio;
106 	struct gpio_desc *wake_gpio;
107 
108 #if defined(CONFIG_DEBUG_FS)
109 	struct dentry *debug_dir;
110 	u8 *raw_buffer;
111 	size_t raw_bufsize;
112 #endif
113 
114 	struct mutex mutex;
115 	bool factory_mode;
116 	enum edt_pmode suspend_mode;
117 	int threshold;
118 	int gain;
119 	int offset;
120 	int offset_x;
121 	int offset_y;
122 	int report_rate;
123 	int max_support_points;
124 
125 	char name[EDT_NAME_LEN];
126 
127 	struct edt_reg_addr reg_addr;
128 	enum edt_ver version;
129 };
130 
131 struct edt_i2c_chip_data {
132 	int  max_support_points;
133 };
134 
135 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
136 				   u16 wr_len, u8 *wr_buf,
137 				   u16 rd_len, u8 *rd_buf)
138 {
139 	struct i2c_msg wrmsg[2];
140 	int i = 0;
141 	int ret;
142 
143 	if (wr_len) {
144 		wrmsg[i].addr  = client->addr;
145 		wrmsg[i].flags = 0;
146 		wrmsg[i].len = wr_len;
147 		wrmsg[i].buf = wr_buf;
148 		i++;
149 	}
150 	if (rd_len) {
151 		wrmsg[i].addr  = client->addr;
152 		wrmsg[i].flags = I2C_M_RD;
153 		wrmsg[i].len = rd_len;
154 		wrmsg[i].buf = rd_buf;
155 		i++;
156 	}
157 
158 	ret = i2c_transfer(client->adapter, wrmsg, i);
159 	if (ret < 0)
160 		return ret;
161 	if (ret != i)
162 		return -EIO;
163 
164 	return 0;
165 }
166 
167 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
168 				    u8 *buf, int buflen)
169 {
170 	int i;
171 	u8 crc = 0;
172 
173 	for (i = 0; i < buflen - 1; i++)
174 		crc ^= buf[i];
175 
176 	if (crc != buf[buflen-1]) {
177 		dev_err_ratelimited(&tsdata->client->dev,
178 				    "crc error: 0x%02x expected, got 0x%02x\n",
179 				    crc, buf[buflen-1]);
180 		return false;
181 	}
182 
183 	return true;
184 }
185 
186 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
187 {
188 	struct edt_ft5x06_ts_data *tsdata = dev_id;
189 	struct device *dev = &tsdata->client->dev;
190 	u8 cmd;
191 	u8 rdbuf[63];
192 	int i, type, x, y, id;
193 	int offset, tplen, datalen, crclen;
194 	int error;
195 
196 	switch (tsdata->version) {
197 	case EDT_M06:
198 		cmd = 0xf9; /* tell the controller to send touch data */
199 		offset = 5; /* where the actual touch data starts */
200 		tplen = 4;  /* data comes in so called frames */
201 		crclen = 1; /* length of the crc data */
202 		break;
203 
204 	case EDT_M09:
205 	case EDT_M12:
206 	case EV_FT:
207 	case GENERIC_FT:
208 		cmd = 0x0;
209 		offset = 3;
210 		tplen = 6;
211 		crclen = 0;
212 		break;
213 
214 	default:
215 		goto out;
216 	}
217 
218 	memset(rdbuf, 0, sizeof(rdbuf));
219 	datalen = tplen * tsdata->max_support_points + offset + crclen;
220 
221 	error = edt_ft5x06_ts_readwrite(tsdata->client,
222 					sizeof(cmd), &cmd,
223 					datalen, rdbuf);
224 	if (error) {
225 		dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
226 				    error);
227 		goto out;
228 	}
229 
230 	/* M09/M12 does not send header or CRC */
231 	if (tsdata->version == EDT_M06) {
232 		if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
233 			rdbuf[2] != datalen) {
234 			dev_err_ratelimited(dev,
235 					"Unexpected header: %02x%02x%02x!\n",
236 					rdbuf[0], rdbuf[1], rdbuf[2]);
237 			goto out;
238 		}
239 
240 		if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
241 			goto out;
242 	}
243 
244 	for (i = 0; i < tsdata->max_support_points; i++) {
245 		u8 *buf = &rdbuf[i * tplen + offset];
246 
247 		type = buf[0] >> 6;
248 		/* ignore Reserved events */
249 		if (type == TOUCH_EVENT_RESERVED)
250 			continue;
251 
252 		/* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
253 		if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
254 			continue;
255 
256 		x = get_unaligned_be16(buf) & 0x0fff;
257 		y = get_unaligned_be16(buf + 2) & 0x0fff;
258 		/* The FT5x26 send the y coordinate first */
259 		if (tsdata->version == EV_FT)
260 			swap(x, y);
261 
262 		id = (buf[2] >> 4) & 0x0f;
263 
264 		input_mt_slot(tsdata->input, id);
265 		if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
266 					       type != TOUCH_EVENT_UP))
267 			touchscreen_report_pos(tsdata->input, &tsdata->prop,
268 					       x, y, true);
269 	}
270 
271 	input_mt_report_pointer_emulation(tsdata->input, true);
272 	input_sync(tsdata->input);
273 
274 out:
275 	return IRQ_HANDLED;
276 }
277 
278 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
279 				     u8 addr, u8 value)
280 {
281 	u8 wrbuf[4];
282 
283 	switch (tsdata->version) {
284 	case EDT_M06:
285 		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
286 		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
287 		wrbuf[2] = value;
288 		wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
289 		return edt_ft5x06_ts_readwrite(tsdata->client, 4,
290 					wrbuf, 0, NULL);
291 
292 	case EDT_M09:
293 	case EDT_M12:
294 	case EV_FT:
295 	case GENERIC_FT:
296 		wrbuf[0] = addr;
297 		wrbuf[1] = value;
298 
299 		return edt_ft5x06_ts_readwrite(tsdata->client, 2,
300 					wrbuf, 0, NULL);
301 
302 	default:
303 		return -EINVAL;
304 	}
305 }
306 
307 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
308 				    u8 addr)
309 {
310 	u8 wrbuf[2], rdbuf[2];
311 	int error;
312 
313 	switch (tsdata->version) {
314 	case EDT_M06:
315 		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
316 		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
317 		wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
318 
319 		error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
320 						rdbuf);
321 		if (error)
322 			return error;
323 
324 		if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
325 			dev_err(&tsdata->client->dev,
326 				"crc error: 0x%02x expected, got 0x%02x\n",
327 				wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
328 				rdbuf[1]);
329 			return -EIO;
330 		}
331 		break;
332 
333 	case EDT_M09:
334 	case EDT_M12:
335 	case EV_FT:
336 	case GENERIC_FT:
337 		wrbuf[0] = addr;
338 		error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
339 						wrbuf, 1, rdbuf);
340 		if (error)
341 			return error;
342 		break;
343 
344 	default:
345 		return -EINVAL;
346 	}
347 
348 	return rdbuf[0];
349 }
350 
351 struct edt_ft5x06_attribute {
352 	struct device_attribute dattr;
353 	size_t field_offset;
354 	u8 limit_low;
355 	u8 limit_high;
356 	u8 addr_m06;
357 	u8 addr_m09;
358 	u8 addr_ev;
359 };
360 
361 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev,		\
362 		_limit_low, _limit_high)				\
363 	struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = {	\
364 		.dattr = __ATTR(_field, _mode,				\
365 				edt_ft5x06_setting_show,		\
366 				edt_ft5x06_setting_store),		\
367 		.field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
368 		.addr_m06 = _addr_m06,					\
369 		.addr_m09 = _addr_m09,					\
370 		.addr_ev  = _addr_ev,					\
371 		.limit_low = _limit_low,				\
372 		.limit_high = _limit_high,				\
373 	}
374 
375 static ssize_t edt_ft5x06_setting_show(struct device *dev,
376 				       struct device_attribute *dattr,
377 				       char *buf)
378 {
379 	struct i2c_client *client = to_i2c_client(dev);
380 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
381 	struct edt_ft5x06_attribute *attr =
382 			container_of(dattr, struct edt_ft5x06_attribute, dattr);
383 	u8 *field = (u8 *)tsdata + attr->field_offset;
384 	int val;
385 	size_t count = 0;
386 	int error = 0;
387 	u8 addr;
388 
389 	mutex_lock(&tsdata->mutex);
390 
391 	if (tsdata->factory_mode) {
392 		error = -EIO;
393 		goto out;
394 	}
395 
396 	switch (tsdata->version) {
397 	case EDT_M06:
398 		addr = attr->addr_m06;
399 		break;
400 
401 	case EDT_M09:
402 	case EDT_M12:
403 	case GENERIC_FT:
404 		addr = attr->addr_m09;
405 		break;
406 
407 	case EV_FT:
408 		addr = attr->addr_ev;
409 		break;
410 
411 	default:
412 		error = -ENODEV;
413 		goto out;
414 	}
415 
416 	if (addr != NO_REGISTER) {
417 		val = edt_ft5x06_register_read(tsdata, addr);
418 		if (val < 0) {
419 			error = val;
420 			dev_err(&tsdata->client->dev,
421 				"Failed to fetch attribute %s, error %d\n",
422 				dattr->attr.name, error);
423 			goto out;
424 		}
425 	} else {
426 		val = *field;
427 	}
428 
429 	if (val != *field) {
430 		dev_warn(&tsdata->client->dev,
431 			 "%s: read (%d) and stored value (%d) differ\n",
432 			 dattr->attr.name, val, *field);
433 		*field = val;
434 	}
435 
436 	count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
437 out:
438 	mutex_unlock(&tsdata->mutex);
439 	return error ?: count;
440 }
441 
442 static ssize_t edt_ft5x06_setting_store(struct device *dev,
443 					struct device_attribute *dattr,
444 					const char *buf, size_t count)
445 {
446 	struct i2c_client *client = to_i2c_client(dev);
447 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
448 	struct edt_ft5x06_attribute *attr =
449 			container_of(dattr, struct edt_ft5x06_attribute, dattr);
450 	u8 *field = (u8 *)tsdata + attr->field_offset;
451 	unsigned int val;
452 	int error;
453 	u8 addr;
454 
455 	mutex_lock(&tsdata->mutex);
456 
457 	if (tsdata->factory_mode) {
458 		error = -EIO;
459 		goto out;
460 	}
461 
462 	error = kstrtouint(buf, 0, &val);
463 	if (error)
464 		goto out;
465 
466 	if (val < attr->limit_low || val > attr->limit_high) {
467 		error = -ERANGE;
468 		goto out;
469 	}
470 
471 	switch (tsdata->version) {
472 	case EDT_M06:
473 		addr = attr->addr_m06;
474 		break;
475 
476 	case EDT_M09:
477 	case EDT_M12:
478 	case GENERIC_FT:
479 		addr = attr->addr_m09;
480 		break;
481 
482 	case EV_FT:
483 		addr = attr->addr_ev;
484 		break;
485 
486 	default:
487 		error = -ENODEV;
488 		goto out;
489 	}
490 
491 	if (addr != NO_REGISTER) {
492 		error = edt_ft5x06_register_write(tsdata, addr, val);
493 		if (error) {
494 			dev_err(&tsdata->client->dev,
495 				"Failed to update attribute %s, error: %d\n",
496 				dattr->attr.name, error);
497 			goto out;
498 		}
499 	}
500 	*field = val;
501 
502 out:
503 	mutex_unlock(&tsdata->mutex);
504 	return error ?: count;
505 }
506 
507 /* m06, m09: range 0-31, m12: range 0-5 */
508 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
509 		M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
510 /* m06, m09: range 0-31, m12: range 0-16 */
511 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
512 		M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
513 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
514 static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
515 		EV_REGISTER_OFFSET_X, 0, 80);
516 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
517 static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
518 		EV_REGISTER_OFFSET_Y, 0, 80);
519 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
520 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
521 		M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
522 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
523 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
524 		NO_REGISTER, NO_REGISTER, 0, 255);
525 
526 static struct attribute *edt_ft5x06_attrs[] = {
527 	&edt_ft5x06_attr_gain.dattr.attr,
528 	&edt_ft5x06_attr_offset.dattr.attr,
529 	&edt_ft5x06_attr_offset_x.dattr.attr,
530 	&edt_ft5x06_attr_offset_y.dattr.attr,
531 	&edt_ft5x06_attr_threshold.dattr.attr,
532 	&edt_ft5x06_attr_report_rate.dattr.attr,
533 	NULL
534 };
535 
536 static const struct attribute_group edt_ft5x06_attr_group = {
537 	.attrs = edt_ft5x06_attrs,
538 };
539 
540 static void edt_ft5x06_restore_reg_parameters(struct edt_ft5x06_ts_data *tsdata)
541 {
542 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
543 
544 	edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
545 				  tsdata->threshold);
546 	edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
547 				  tsdata->gain);
548 	if (reg_addr->reg_offset != NO_REGISTER)
549 		edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
550 					  tsdata->offset);
551 	if (reg_addr->reg_offset_x != NO_REGISTER)
552 		edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x,
553 					  tsdata->offset_x);
554 	if (reg_addr->reg_offset_y != NO_REGISTER)
555 		edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y,
556 					  tsdata->offset_y);
557 	if (reg_addr->reg_report_rate != NO_REGISTER)
558 		edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
559 				  tsdata->report_rate);
560 
561 }
562 
563 #ifdef CONFIG_DEBUG_FS
564 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
565 {
566 	struct i2c_client *client = tsdata->client;
567 	int retries = EDT_SWITCH_MODE_RETRIES;
568 	int ret;
569 	int error;
570 
571 	if (tsdata->version != EDT_M06) {
572 		dev_err(&client->dev,
573 			"No factory mode support for non-M06 devices\n");
574 		return -EINVAL;
575 	}
576 
577 	disable_irq(client->irq);
578 
579 	if (!tsdata->raw_buffer) {
580 		tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
581 				      sizeof(u16);
582 		tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
583 		if (!tsdata->raw_buffer) {
584 			error = -ENOMEM;
585 			goto err_out;
586 		}
587 	}
588 
589 	/* mode register is 0x3c when in the work mode */
590 	error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
591 	if (error) {
592 		dev_err(&client->dev,
593 			"failed to switch to factory mode, error %d\n", error);
594 		goto err_out;
595 	}
596 
597 	tsdata->factory_mode = true;
598 	do {
599 		mdelay(EDT_SWITCH_MODE_DELAY);
600 		/* mode register is 0x01 when in factory mode */
601 		ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
602 		if (ret == 0x03)
603 			break;
604 	} while (--retries > 0);
605 
606 	if (retries == 0) {
607 		dev_err(&client->dev, "not in factory mode after %dms.\n",
608 			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
609 		error = -EIO;
610 		goto err_out;
611 	}
612 
613 	return 0;
614 
615 err_out:
616 	kfree(tsdata->raw_buffer);
617 	tsdata->raw_buffer = NULL;
618 	tsdata->factory_mode = false;
619 	enable_irq(client->irq);
620 
621 	return error;
622 }
623 
624 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
625 {
626 	struct i2c_client *client = tsdata->client;
627 	int retries = EDT_SWITCH_MODE_RETRIES;
628 	int ret;
629 	int error;
630 
631 	/* mode register is 0x01 when in the factory mode */
632 	error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
633 	if (error) {
634 		dev_err(&client->dev,
635 			"failed to switch to work mode, error: %d\n", error);
636 		return error;
637 	}
638 
639 	tsdata->factory_mode = false;
640 
641 	do {
642 		mdelay(EDT_SWITCH_MODE_DELAY);
643 		/* mode register is 0x01 when in factory mode */
644 		ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
645 		if (ret == 0x01)
646 			break;
647 	} while (--retries > 0);
648 
649 	if (retries == 0) {
650 		dev_err(&client->dev, "not in work mode after %dms.\n",
651 			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
652 		tsdata->factory_mode = true;
653 		return -EIO;
654 	}
655 
656 	kfree(tsdata->raw_buffer);
657 	tsdata->raw_buffer = NULL;
658 
659 	edt_ft5x06_restore_reg_parameters(tsdata);
660 	enable_irq(client->irq);
661 
662 	return 0;
663 }
664 
665 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
666 {
667 	struct edt_ft5x06_ts_data *tsdata = data;
668 
669 	*mode = tsdata->factory_mode;
670 
671 	return 0;
672 };
673 
674 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
675 {
676 	struct edt_ft5x06_ts_data *tsdata = data;
677 	int retval = 0;
678 
679 	if (mode > 1)
680 		return -ERANGE;
681 
682 	mutex_lock(&tsdata->mutex);
683 
684 	if (mode != tsdata->factory_mode) {
685 		retval = mode ? edt_ft5x06_factory_mode(tsdata) :
686 				edt_ft5x06_work_mode(tsdata);
687 	}
688 
689 	mutex_unlock(&tsdata->mutex);
690 
691 	return retval;
692 };
693 
694 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
695 			edt_ft5x06_debugfs_mode_set, "%llu\n");
696 
697 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
698 				char __user *buf, size_t count, loff_t *off)
699 {
700 	struct edt_ft5x06_ts_data *tsdata = file->private_data;
701 	struct i2c_client *client = tsdata->client;
702 	int retries  = EDT_RAW_DATA_RETRIES;
703 	int val, i, error;
704 	size_t read = 0;
705 	int colbytes;
706 	char wrbuf[3];
707 	u8 *rdbuf;
708 
709 	if (*off < 0 || *off >= tsdata->raw_bufsize)
710 		return 0;
711 
712 	mutex_lock(&tsdata->mutex);
713 
714 	if (!tsdata->factory_mode || !tsdata->raw_buffer) {
715 		error = -EIO;
716 		goto out;
717 	}
718 
719 	error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
720 	if (error) {
721 		dev_dbg(&client->dev,
722 			"failed to write 0x08 register, error %d\n", error);
723 		goto out;
724 	}
725 
726 	do {
727 		usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
728 		val = edt_ft5x06_register_read(tsdata, 0x08);
729 		if (val < 1)
730 			break;
731 	} while (--retries > 0);
732 
733 	if (val < 0) {
734 		error = val;
735 		dev_dbg(&client->dev,
736 			"failed to read 0x08 register, error %d\n", error);
737 		goto out;
738 	}
739 
740 	if (retries == 0) {
741 		dev_dbg(&client->dev,
742 			"timed out waiting for register to settle\n");
743 		error = -ETIMEDOUT;
744 		goto out;
745 	}
746 
747 	rdbuf = tsdata->raw_buffer;
748 	colbytes = tsdata->num_y * sizeof(u16);
749 
750 	wrbuf[0] = 0xf5;
751 	wrbuf[1] = 0x0e;
752 	for (i = 0; i < tsdata->num_x; i++) {
753 		wrbuf[2] = i;  /* column index */
754 		error = edt_ft5x06_ts_readwrite(tsdata->client,
755 						sizeof(wrbuf), wrbuf,
756 						colbytes, rdbuf);
757 		if (error)
758 			goto out;
759 
760 		rdbuf += colbytes;
761 	}
762 
763 	read = min_t(size_t, count, tsdata->raw_bufsize - *off);
764 	if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
765 		error = -EFAULT;
766 		goto out;
767 	}
768 
769 	*off += read;
770 out:
771 	mutex_unlock(&tsdata->mutex);
772 	return error ?: read;
773 };
774 
775 static const struct file_operations debugfs_raw_data_fops = {
776 	.open = simple_open,
777 	.read = edt_ft5x06_debugfs_raw_data_read,
778 };
779 
780 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
781 					  const char *debugfs_name)
782 {
783 	tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
784 
785 	debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
786 	debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
787 
788 	debugfs_create_file("mode", S_IRUSR | S_IWUSR,
789 			    tsdata->debug_dir, tsdata, &debugfs_mode_fops);
790 	debugfs_create_file("raw_data", S_IRUSR,
791 			    tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
792 }
793 
794 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
795 {
796 	debugfs_remove_recursive(tsdata->debug_dir);
797 	kfree(tsdata->raw_buffer);
798 }
799 
800 #else
801 
802 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
803 {
804 	return -ENOSYS;
805 }
806 
807 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
808 					  const char *debugfs_name)
809 {
810 }
811 
812 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
813 {
814 }
815 
816 #endif /* CONFIG_DEBUGFS */
817 
818 static int edt_ft5x06_ts_identify(struct i2c_client *client,
819 					struct edt_ft5x06_ts_data *tsdata,
820 					char *fw_version)
821 {
822 	u8 rdbuf[EDT_NAME_LEN];
823 	char *p;
824 	int error;
825 	char *model_name = tsdata->name;
826 
827 	/* see what we find if we assume it is a M06 *
828 	 * if we get less than EDT_NAME_LEN, we don't want
829 	 * to have garbage in there
830 	 */
831 	memset(rdbuf, 0, sizeof(rdbuf));
832 	error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
833 					EDT_NAME_LEN - 1, rdbuf);
834 	if (error)
835 		return error;
836 
837 	/* Probe content for something consistent.
838 	 * M06 starts with a response byte, M12 gives the data directly.
839 	 * M09/Generic does not provide model number information.
840 	 */
841 	if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
842 		tsdata->version = EDT_M06;
843 
844 		/* remove last '$' end marker */
845 		rdbuf[EDT_NAME_LEN - 1] = '\0';
846 		if (rdbuf[EDT_NAME_LEN - 2] == '$')
847 			rdbuf[EDT_NAME_LEN - 2] = '\0';
848 
849 		/* look for Model/Version separator */
850 		p = strchr(rdbuf, '*');
851 		if (p)
852 			*p++ = '\0';
853 		strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
854 		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
855 	} else if (!strncasecmp(rdbuf, "EP0", 3)) {
856 		tsdata->version = EDT_M12;
857 
858 		/* remove last '$' end marker */
859 		rdbuf[EDT_NAME_LEN - 2] = '\0';
860 		if (rdbuf[EDT_NAME_LEN - 3] == '$')
861 			rdbuf[EDT_NAME_LEN - 3] = '\0';
862 
863 		/* look for Model/Version separator */
864 		p = strchr(rdbuf, '*');
865 		if (p)
866 			*p++ = '\0';
867 		strlcpy(model_name, rdbuf, EDT_NAME_LEN);
868 		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
869 	} else {
870 		/* If it is not an EDT M06/M12 touchscreen, then the model
871 		 * detection is a bit hairy. The different ft5x06
872 		 * firmares around don't reliably implement the
873 		 * identification registers. Well, we'll take a shot.
874 		 *
875 		 * The main difference between generic focaltec based
876 		 * touches and EDT M09 is that we know how to retrieve
877 		 * the max coordinates for the latter.
878 		 */
879 		tsdata->version = GENERIC_FT;
880 
881 		error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
882 						2, rdbuf);
883 		if (error)
884 			return error;
885 
886 		strlcpy(fw_version, rdbuf, 2);
887 
888 		error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
889 						1, rdbuf);
890 		if (error)
891 			return error;
892 
893 		/* This "model identification" is not exact. Unfortunately
894 		 * not all firmwares for the ft5x06 put useful values in
895 		 * the identification registers.
896 		 */
897 		switch (rdbuf[0]) {
898 		case 0x35:   /* EDT EP0350M09 */
899 		case 0x43:   /* EDT EP0430M09 */
900 		case 0x50:   /* EDT EP0500M09 */
901 		case 0x57:   /* EDT EP0570M09 */
902 		case 0x70:   /* EDT EP0700M09 */
903 			tsdata->version = EDT_M09;
904 			snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
905 				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
906 			break;
907 		case 0xa1:   /* EDT EP1010ML00 */
908 			tsdata->version = EDT_M09;
909 			snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
910 				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
911 			break;
912 		case 0x5a:   /* Solomon Goldentek Display */
913 			snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
914 			break;
915 		case 0x59:  /* Evervision Display with FT5xx6 TS */
916 			tsdata->version = EV_FT;
917 			error = edt_ft5x06_ts_readwrite(client, 1, "\x53",
918 							1, rdbuf);
919 			if (error)
920 				return error;
921 			strlcpy(fw_version, rdbuf, 1);
922 			snprintf(model_name, EDT_NAME_LEN,
923 				 "EVERVISION-FT5726NEi");
924 			break;
925 		default:
926 			snprintf(model_name, EDT_NAME_LEN,
927 				 "generic ft5x06 (%02x)",
928 				 rdbuf[0]);
929 			break;
930 		}
931 	}
932 
933 	return 0;
934 }
935 
936 static void edt_ft5x06_ts_get_defaults(struct device *dev,
937 				       struct edt_ft5x06_ts_data *tsdata)
938 {
939 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
940 	u32 val;
941 	int error;
942 
943 	error = device_property_read_u32(dev, "threshold", &val);
944 	if (!error) {
945 		edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
946 		tsdata->threshold = val;
947 	}
948 
949 	error = device_property_read_u32(dev, "gain", &val);
950 	if (!error) {
951 		edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
952 		tsdata->gain = val;
953 	}
954 
955 	error = device_property_read_u32(dev, "offset", &val);
956 	if (!error) {
957 		if (reg_addr->reg_offset != NO_REGISTER)
958 			edt_ft5x06_register_write(tsdata,
959 						  reg_addr->reg_offset, val);
960 		tsdata->offset = val;
961 	}
962 
963 	error = device_property_read_u32(dev, "offset-x", &val);
964 	if (!error) {
965 		if (reg_addr->reg_offset_x != NO_REGISTER)
966 			edt_ft5x06_register_write(tsdata,
967 						  reg_addr->reg_offset_x, val);
968 		tsdata->offset_x = val;
969 	}
970 
971 	error = device_property_read_u32(dev, "offset-y", &val);
972 	if (!error) {
973 		if (reg_addr->reg_offset_y != NO_REGISTER)
974 			edt_ft5x06_register_write(tsdata,
975 						  reg_addr->reg_offset_y, val);
976 		tsdata->offset_y = val;
977 	}
978 }
979 
980 static void
981 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
982 {
983 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
984 
985 	tsdata->threshold = edt_ft5x06_register_read(tsdata,
986 						     reg_addr->reg_threshold);
987 	tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
988 	if (reg_addr->reg_offset != NO_REGISTER)
989 		tsdata->offset =
990 			edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
991 	if (reg_addr->reg_offset_x != NO_REGISTER)
992 		tsdata->offset_x = edt_ft5x06_register_read(tsdata,
993 						reg_addr->reg_offset_x);
994 	if (reg_addr->reg_offset_y != NO_REGISTER)
995 		tsdata->offset_y = edt_ft5x06_register_read(tsdata,
996 						reg_addr->reg_offset_y);
997 	if (reg_addr->reg_report_rate != NO_REGISTER)
998 		tsdata->report_rate = edt_ft5x06_register_read(tsdata,
999 						reg_addr->reg_report_rate);
1000 	if (tsdata->version == EDT_M06 ||
1001 	    tsdata->version == EDT_M09 ||
1002 	    tsdata->version == EDT_M12) {
1003 		tsdata->num_x = edt_ft5x06_register_read(tsdata,
1004 							 reg_addr->reg_num_x);
1005 		tsdata->num_y = edt_ft5x06_register_read(tsdata,
1006 							 reg_addr->reg_num_y);
1007 	} else {
1008 		tsdata->num_x = -1;
1009 		tsdata->num_y = -1;
1010 	}
1011 }
1012 
1013 static void
1014 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
1015 {
1016 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
1017 
1018 	switch (tsdata->version) {
1019 	case EDT_M06:
1020 		reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
1021 		reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
1022 		reg_addr->reg_gain = WORK_REGISTER_GAIN;
1023 		reg_addr->reg_offset = WORK_REGISTER_OFFSET;
1024 		reg_addr->reg_offset_x = NO_REGISTER;
1025 		reg_addr->reg_offset_y = NO_REGISTER;
1026 		reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1027 		reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1028 		break;
1029 
1030 	case EDT_M09:
1031 	case EDT_M12:
1032 		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1033 		reg_addr->reg_report_rate = NO_REGISTER;
1034 		reg_addr->reg_gain = M09_REGISTER_GAIN;
1035 		reg_addr->reg_offset = M09_REGISTER_OFFSET;
1036 		reg_addr->reg_offset_x = NO_REGISTER;
1037 		reg_addr->reg_offset_y = NO_REGISTER;
1038 		reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1039 		reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1040 		break;
1041 
1042 	case EV_FT:
1043 		reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1044 		reg_addr->reg_gain = EV_REGISTER_GAIN;
1045 		reg_addr->reg_offset = NO_REGISTER;
1046 		reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1047 		reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1048 		reg_addr->reg_num_x = NO_REGISTER;
1049 		reg_addr->reg_num_y = NO_REGISTER;
1050 		reg_addr->reg_report_rate = NO_REGISTER;
1051 		break;
1052 
1053 	case GENERIC_FT:
1054 		/* this is a guesswork */
1055 		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1056 		reg_addr->reg_gain = M09_REGISTER_GAIN;
1057 		reg_addr->reg_offset = M09_REGISTER_OFFSET;
1058 		reg_addr->reg_offset_x = NO_REGISTER;
1059 		reg_addr->reg_offset_y = NO_REGISTER;
1060 		break;
1061 	}
1062 }
1063 
1064 static void edt_ft5x06_disable_regulator(void *arg)
1065 {
1066 	struct edt_ft5x06_ts_data *data = arg;
1067 
1068 	regulator_disable(data->vcc);
1069 }
1070 
1071 static int edt_ft5x06_ts_probe(struct i2c_client *client,
1072 					 const struct i2c_device_id *id)
1073 {
1074 	const struct edt_i2c_chip_data *chip_data;
1075 	struct edt_ft5x06_ts_data *tsdata;
1076 	u8 buf[2] = { 0xfc, 0x00 };
1077 	struct input_dev *input;
1078 	unsigned long irq_flags;
1079 	int error;
1080 	char fw_version[EDT_NAME_LEN];
1081 
1082 	dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1083 
1084 	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1085 	if (!tsdata) {
1086 		dev_err(&client->dev, "failed to allocate driver data.\n");
1087 		return -ENOMEM;
1088 	}
1089 
1090 	chip_data = device_get_match_data(&client->dev);
1091 	if (!chip_data)
1092 		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1093 	if (!chip_data || !chip_data->max_support_points) {
1094 		dev_err(&client->dev, "invalid or missing chip data\n");
1095 		return -EINVAL;
1096 	}
1097 
1098 	tsdata->max_support_points = chip_data->max_support_points;
1099 
1100 	tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
1101 	if (IS_ERR(tsdata->vcc)) {
1102 		error = PTR_ERR(tsdata->vcc);
1103 		if (error != -EPROBE_DEFER)
1104 			dev_err(&client->dev,
1105 				"failed to request regulator: %d\n", error);
1106 		return error;
1107 	}
1108 
1109 	error = regulator_enable(tsdata->vcc);
1110 	if (error < 0) {
1111 		dev_err(&client->dev, "failed to enable vcc: %d\n", error);
1112 		return error;
1113 	}
1114 
1115 	error = devm_add_action_or_reset(&client->dev,
1116 					 edt_ft5x06_disable_regulator,
1117 					 tsdata);
1118 	if (error)
1119 		return error;
1120 
1121 	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1122 						     "reset", GPIOD_OUT_HIGH);
1123 	if (IS_ERR(tsdata->reset_gpio)) {
1124 		error = PTR_ERR(tsdata->reset_gpio);
1125 		dev_err(&client->dev,
1126 			"Failed to request GPIO reset pin, error %d\n", error);
1127 		return error;
1128 	}
1129 
1130 	tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1131 						    "wake", GPIOD_OUT_LOW);
1132 	if (IS_ERR(tsdata->wake_gpio)) {
1133 		error = PTR_ERR(tsdata->wake_gpio);
1134 		dev_err(&client->dev,
1135 			"Failed to request GPIO wake pin, error %d\n", error);
1136 		return error;
1137 	}
1138 
1139 	/*
1140 	 * Check which sleep modes we can support. Power-off requieres the
1141 	 * reset-pin to ensure correct power-down/power-up behaviour. Start with
1142 	 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep
1143 	 * mode.
1144 	 */
1145 	if (tsdata->reset_gpio)
1146 		tsdata->suspend_mode = EDT_PMODE_POWEROFF;
1147 	else if (tsdata->wake_gpio)
1148 		tsdata->suspend_mode = EDT_PMODE_HIBERNATE;
1149 	else
1150 		tsdata->suspend_mode = EDT_PMODE_NOT_SUPPORTED;
1151 
1152 	if (tsdata->wake_gpio) {
1153 		usleep_range(5000, 6000);
1154 		gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1155 	}
1156 
1157 	if (tsdata->reset_gpio) {
1158 		usleep_range(5000, 6000);
1159 		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1160 		msleep(300);
1161 	}
1162 
1163 	input = devm_input_allocate_device(&client->dev);
1164 	if (!input) {
1165 		dev_err(&client->dev, "failed to allocate input device.\n");
1166 		return -ENOMEM;
1167 	}
1168 
1169 	mutex_init(&tsdata->mutex);
1170 	tsdata->client = client;
1171 	tsdata->input = input;
1172 	tsdata->factory_mode = false;
1173 
1174 	error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1175 	if (error) {
1176 		dev_err(&client->dev, "touchscreen probe failed\n");
1177 		return error;
1178 	}
1179 
1180 	/*
1181 	 * Dummy read access. EP0700MLP1 returns bogus data on the first
1182 	 * register read access and ignores writes.
1183 	 */
1184 	edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf);
1185 
1186 	edt_ft5x06_ts_set_regs(tsdata);
1187 	edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1188 	edt_ft5x06_ts_get_parameters(tsdata);
1189 
1190 	dev_dbg(&client->dev,
1191 		"Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1192 		tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1193 
1194 	input->name = tsdata->name;
1195 	input->id.bustype = BUS_I2C;
1196 	input->dev.parent = &client->dev;
1197 
1198 	if (tsdata->version == EDT_M06 ||
1199 	    tsdata->version == EDT_M09 ||
1200 	    tsdata->version == EDT_M12) {
1201 		input_set_abs_params(input, ABS_MT_POSITION_X,
1202 				     0, tsdata->num_x * 64 - 1, 0, 0);
1203 		input_set_abs_params(input, ABS_MT_POSITION_Y,
1204 				     0, tsdata->num_y * 64 - 1, 0, 0);
1205 	} else {
1206 		/* Unknown maximum values. Specify via devicetree */
1207 		input_set_abs_params(input, ABS_MT_POSITION_X,
1208 				     0, 65535, 0, 0);
1209 		input_set_abs_params(input, ABS_MT_POSITION_Y,
1210 				     0, 65535, 0, 0);
1211 	}
1212 
1213 	touchscreen_parse_properties(input, true, &tsdata->prop);
1214 
1215 	error = input_mt_init_slots(input, tsdata->max_support_points,
1216 				INPUT_MT_DIRECT);
1217 	if (error) {
1218 		dev_err(&client->dev, "Unable to init MT slots.\n");
1219 		return error;
1220 	}
1221 
1222 	i2c_set_clientdata(client, tsdata);
1223 
1224 	irq_flags = irq_get_trigger_type(client->irq);
1225 	if (irq_flags == IRQF_TRIGGER_NONE)
1226 		irq_flags = IRQF_TRIGGER_FALLING;
1227 	irq_flags |= IRQF_ONESHOT;
1228 
1229 	error = devm_request_threaded_irq(&client->dev, client->irq,
1230 					NULL, edt_ft5x06_ts_isr, irq_flags,
1231 					client->name, tsdata);
1232 	if (error) {
1233 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1234 		return error;
1235 	}
1236 
1237 	error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1238 	if (error)
1239 		return error;
1240 
1241 	error = input_register_device(input);
1242 	if (error)
1243 		return error;
1244 
1245 	edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1246 
1247 	dev_dbg(&client->dev,
1248 		"EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1249 		client->irq,
1250 		tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1251 		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1252 
1253 	return 0;
1254 }
1255 
1256 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1257 {
1258 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1259 
1260 	edt_ft5x06_ts_teardown_debugfs(tsdata);
1261 
1262 	return 0;
1263 }
1264 
1265 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1266 {
1267 	struct i2c_client *client = to_i2c_client(dev);
1268 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1269 	struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1270 	int ret;
1271 
1272 	if (device_may_wakeup(dev))
1273 		return 0;
1274 
1275 	if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1276 		return 0;
1277 
1278 	/* Enter hibernate mode. */
1279 	ret = edt_ft5x06_register_write(tsdata, PMOD_REGISTER_OPMODE,
1280 					PMOD_REGISTER_HIBERNATE);
1281 	if (ret)
1282 		dev_warn(dev, "Failed to set hibernate mode\n");
1283 
1284 	if (tsdata->suspend_mode == EDT_PMODE_HIBERNATE)
1285 		return 0;
1286 
1287 	/*
1288 	 * Power-off according the datasheet. Cut the power may leaf the irq
1289 	 * line in an undefined state depending on the host pull resistor
1290 	 * settings. Disable the irq to avoid adjusting each host till the
1291 	 * device is back in a full functional state.
1292 	 */
1293 	disable_irq(tsdata->client->irq);
1294 
1295 	gpiod_set_value_cansleep(reset_gpio, 1);
1296 	usleep_range(1000, 2000);
1297 
1298 	ret = regulator_disable(tsdata->vcc);
1299 	if (ret)
1300 		dev_warn(dev, "Failed to disable vcc\n");
1301 
1302 	return 0;
1303 }
1304 
1305 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1306 {
1307 	struct i2c_client *client = to_i2c_client(dev);
1308 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1309 	int ret = 0;
1310 
1311 	if (device_may_wakeup(dev))
1312 		return 0;
1313 
1314 	if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1315 		return 0;
1316 
1317 	if (tsdata->suspend_mode == EDT_PMODE_POWEROFF) {
1318 		struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1319 
1320 		/*
1321 		 * We can't check if the regulator is a dummy or a real
1322 		 * regulator. So we need to specify the 5ms reset time (T_rst)
1323 		 * here instead of the 100us T_rtp time. We also need to wait
1324 		 * 300ms in case it was a real supply and the power was cutted
1325 		 * of. Toggle the reset pin is also a way to exit the hibernate
1326 		 * mode.
1327 		 */
1328 		gpiod_set_value_cansleep(reset_gpio, 1);
1329 		usleep_range(5000, 6000);
1330 
1331 		ret = regulator_enable(tsdata->vcc);
1332 		if (ret) {
1333 			dev_err(dev, "Failed to enable vcc\n");
1334 			return ret;
1335 		}
1336 
1337 		usleep_range(1000, 2000);
1338 		gpiod_set_value_cansleep(reset_gpio, 0);
1339 		msleep(300);
1340 
1341 		edt_ft5x06_restore_reg_parameters(tsdata);
1342 		enable_irq(tsdata->client->irq);
1343 
1344 		if (tsdata->factory_mode)
1345 			ret = edt_ft5x06_factory_mode(tsdata);
1346 	} else {
1347 		struct gpio_desc *wake_gpio = tsdata->wake_gpio;
1348 
1349 		gpiod_set_value_cansleep(wake_gpio, 0);
1350 		usleep_range(5000, 6000);
1351 		gpiod_set_value_cansleep(wake_gpio, 1);
1352 	}
1353 
1354 
1355 	return ret;
1356 }
1357 
1358 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1359 			 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1360 
1361 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1362 	.max_support_points = 5,
1363 };
1364 
1365 static const struct edt_i2c_chip_data edt_ft5506_data = {
1366 	.max_support_points = 10,
1367 };
1368 
1369 static const struct edt_i2c_chip_data edt_ft6236_data = {
1370 	.max_support_points = 2,
1371 };
1372 
1373 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1374 	{ .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1375 	{ .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1376 	{ .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1377 	/* Note no edt- prefix for compatibility with the ft6236.c driver */
1378 	{ .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1379 	{ /* sentinel */ }
1380 };
1381 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1382 
1383 static const struct of_device_id edt_ft5x06_of_match[] = {
1384 	{ .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1385 	{ .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1386 	{ .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1387 	{ .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1388 	{ .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1389 	/* Note focaltech vendor prefix for compatibility with ft6236.c */
1390 	{ .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1391 	{ /* sentinel */ }
1392 };
1393 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1394 
1395 static struct i2c_driver edt_ft5x06_ts_driver = {
1396 	.driver = {
1397 		.name = "edt_ft5x06",
1398 		.of_match_table = edt_ft5x06_of_match,
1399 		.pm = &edt_ft5x06_ts_pm_ops,
1400 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1401 	},
1402 	.id_table = edt_ft5x06_ts_id,
1403 	.probe    = edt_ft5x06_ts_probe,
1404 	.remove   = edt_ft5x06_ts_remove,
1405 };
1406 
1407 module_i2c_driver(edt_ft5x06_ts_driver);
1408 
1409 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1410 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1411 MODULE_LICENSE("GPL v2");
1412