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