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 0x11:   /* EDT EP0110M09 */
903 		case 0x35:   /* EDT EP0350M09 */
904 		case 0x43:   /* EDT EP0430M09 */
905 		case 0x50:   /* EDT EP0500M09 */
906 		case 0x57:   /* EDT EP0570M09 */
907 		case 0x70:   /* EDT EP0700M09 */
908 			tsdata->version = EDT_M09;
909 			snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
910 				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
911 			break;
912 		case 0xa1:   /* EDT EP1010ML00 */
913 			tsdata->version = EDT_M09;
914 			snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
915 				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
916 			break;
917 		case 0x5a:   /* Solomon Goldentek Display */
918 			snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
919 			break;
920 		case 0x59:  /* Evervision Display with FT5xx6 TS */
921 			tsdata->version = EV_FT;
922 			error = edt_ft5x06_ts_readwrite(client, 1, "\x53",
923 							1, rdbuf);
924 			if (error)
925 				return error;
926 			strlcpy(fw_version, rdbuf, 1);
927 			snprintf(model_name, EDT_NAME_LEN,
928 				 "EVERVISION-FT5726NEi");
929 			break;
930 		default:
931 			snprintf(model_name, EDT_NAME_LEN,
932 				 "generic ft5x06 (%02x)",
933 				 rdbuf[0]);
934 			break;
935 		}
936 	}
937 
938 	return 0;
939 }
940 
941 static void edt_ft5x06_ts_get_defaults(struct device *dev,
942 				       struct edt_ft5x06_ts_data *tsdata)
943 {
944 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
945 	u32 val;
946 	int error;
947 
948 	error = device_property_read_u32(dev, "threshold", &val);
949 	if (!error) {
950 		edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
951 		tsdata->threshold = val;
952 	}
953 
954 	error = device_property_read_u32(dev, "gain", &val);
955 	if (!error) {
956 		edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
957 		tsdata->gain = val;
958 	}
959 
960 	error = device_property_read_u32(dev, "offset", &val);
961 	if (!error) {
962 		if (reg_addr->reg_offset != NO_REGISTER)
963 			edt_ft5x06_register_write(tsdata,
964 						  reg_addr->reg_offset, val);
965 		tsdata->offset = val;
966 	}
967 
968 	error = device_property_read_u32(dev, "offset-x", &val);
969 	if (!error) {
970 		if (reg_addr->reg_offset_x != NO_REGISTER)
971 			edt_ft5x06_register_write(tsdata,
972 						  reg_addr->reg_offset_x, val);
973 		tsdata->offset_x = val;
974 	}
975 
976 	error = device_property_read_u32(dev, "offset-y", &val);
977 	if (!error) {
978 		if (reg_addr->reg_offset_y != NO_REGISTER)
979 			edt_ft5x06_register_write(tsdata,
980 						  reg_addr->reg_offset_y, val);
981 		tsdata->offset_y = val;
982 	}
983 }
984 
985 static void edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
986 {
987 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
988 
989 	tsdata->threshold = edt_ft5x06_register_read(tsdata,
990 						     reg_addr->reg_threshold);
991 	tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
992 	if (reg_addr->reg_offset != NO_REGISTER)
993 		tsdata->offset =
994 			edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
995 	if (reg_addr->reg_offset_x != NO_REGISTER)
996 		tsdata->offset_x = edt_ft5x06_register_read(tsdata,
997 						reg_addr->reg_offset_x);
998 	if (reg_addr->reg_offset_y != NO_REGISTER)
999 		tsdata->offset_y = edt_ft5x06_register_read(tsdata,
1000 						reg_addr->reg_offset_y);
1001 	if (reg_addr->reg_report_rate != NO_REGISTER)
1002 		tsdata->report_rate = edt_ft5x06_register_read(tsdata,
1003 						reg_addr->reg_report_rate);
1004 	tsdata->num_x = EDT_DEFAULT_NUM_X;
1005 	if (reg_addr->reg_num_x != NO_REGISTER)
1006 		tsdata->num_x = edt_ft5x06_register_read(tsdata,
1007 							 reg_addr->reg_num_x);
1008 	tsdata->num_y = EDT_DEFAULT_NUM_Y;
1009 	if (reg_addr->reg_num_y != NO_REGISTER)
1010 		tsdata->num_y = edt_ft5x06_register_read(tsdata,
1011 							 reg_addr->reg_num_y);
1012 }
1013 
1014 static void 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_report_rate = NO_REGISTER;
1045 		reg_addr->reg_gain = EV_REGISTER_GAIN;
1046 		reg_addr->reg_offset = NO_REGISTER;
1047 		reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1048 		reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1049 		reg_addr->reg_num_x = NO_REGISTER;
1050 		reg_addr->reg_num_y = 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_report_rate = NO_REGISTER;
1057 		reg_addr->reg_gain = M09_REGISTER_GAIN;
1058 		reg_addr->reg_offset = M09_REGISTER_OFFSET;
1059 		reg_addr->reg_offset_x = NO_REGISTER;
1060 		reg_addr->reg_offset_y = NO_REGISTER;
1061 		reg_addr->reg_num_x = NO_REGISTER;
1062 		reg_addr->reg_num_y = NO_REGISTER;
1063 		break;
1064 	}
1065 }
1066 
1067 static void edt_ft5x06_disable_regulators(void *arg)
1068 {
1069 	struct edt_ft5x06_ts_data *data = arg;
1070 
1071 	regulator_disable(data->vcc);
1072 	regulator_disable(data->iovcc);
1073 }
1074 
1075 static int edt_ft5x06_ts_probe(struct i2c_client *client,
1076 					 const struct i2c_device_id *id)
1077 {
1078 	const struct edt_i2c_chip_data *chip_data;
1079 	struct edt_ft5x06_ts_data *tsdata;
1080 	u8 buf[2] = { 0xfc, 0x00 };
1081 	struct input_dev *input;
1082 	unsigned long irq_flags;
1083 	int error;
1084 	char fw_version[EDT_NAME_LEN];
1085 
1086 	dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1087 
1088 	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1089 	if (!tsdata) {
1090 		dev_err(&client->dev, "failed to allocate driver data.\n");
1091 		return -ENOMEM;
1092 	}
1093 
1094 	chip_data = device_get_match_data(&client->dev);
1095 	if (!chip_data)
1096 		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1097 	if (!chip_data || !chip_data->max_support_points) {
1098 		dev_err(&client->dev, "invalid or missing chip data\n");
1099 		return -EINVAL;
1100 	}
1101 
1102 	tsdata->max_support_points = chip_data->max_support_points;
1103 
1104 	tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
1105 	if (IS_ERR(tsdata->vcc)) {
1106 		error = PTR_ERR(tsdata->vcc);
1107 		if (error != -EPROBE_DEFER)
1108 			dev_err(&client->dev,
1109 				"failed to request regulator: %d\n", error);
1110 		return error;
1111 	}
1112 
1113 	tsdata->iovcc = devm_regulator_get(&client->dev, "iovcc");
1114 	if (IS_ERR(tsdata->iovcc)) {
1115 		error = PTR_ERR(tsdata->iovcc);
1116 		if (error != -EPROBE_DEFER)
1117 			dev_err(&client->dev,
1118 				"failed to request iovcc regulator: %d\n", error);
1119 		return error;
1120 	}
1121 
1122 	error = regulator_enable(tsdata->iovcc);
1123 	if (error < 0) {
1124 		dev_err(&client->dev, "failed to enable iovcc: %d\n", error);
1125 		return error;
1126 	}
1127 
1128 	/* Delay enabling VCC for > 10us (T_ivd) after IOVCC */
1129 	usleep_range(10, 100);
1130 
1131 	error = regulator_enable(tsdata->vcc);
1132 	if (error < 0) {
1133 		dev_err(&client->dev, "failed to enable vcc: %d\n", error);
1134 		regulator_disable(tsdata->iovcc);
1135 		return error;
1136 	}
1137 
1138 	error = devm_add_action_or_reset(&client->dev,
1139 					 edt_ft5x06_disable_regulators,
1140 					 tsdata);
1141 	if (error)
1142 		return error;
1143 
1144 	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1145 						     "reset", GPIOD_OUT_HIGH);
1146 	if (IS_ERR(tsdata->reset_gpio)) {
1147 		error = PTR_ERR(tsdata->reset_gpio);
1148 		dev_err(&client->dev,
1149 			"Failed to request GPIO reset pin, error %d\n", error);
1150 		return error;
1151 	}
1152 
1153 	tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1154 						    "wake", GPIOD_OUT_LOW);
1155 	if (IS_ERR(tsdata->wake_gpio)) {
1156 		error = PTR_ERR(tsdata->wake_gpio);
1157 		dev_err(&client->dev,
1158 			"Failed to request GPIO wake pin, error %d\n", error);
1159 		return error;
1160 	}
1161 
1162 	/*
1163 	 * Check which sleep modes we can support. Power-off requieres the
1164 	 * reset-pin to ensure correct power-down/power-up behaviour. Start with
1165 	 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep
1166 	 * mode.
1167 	 */
1168 	if (tsdata->reset_gpio)
1169 		tsdata->suspend_mode = EDT_PMODE_POWEROFF;
1170 	else if (tsdata->wake_gpio)
1171 		tsdata->suspend_mode = EDT_PMODE_HIBERNATE;
1172 	else
1173 		tsdata->suspend_mode = EDT_PMODE_NOT_SUPPORTED;
1174 
1175 	if (tsdata->wake_gpio) {
1176 		usleep_range(5000, 6000);
1177 		gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1178 	}
1179 
1180 	if (tsdata->reset_gpio) {
1181 		usleep_range(5000, 6000);
1182 		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1183 		msleep(300);
1184 	}
1185 
1186 	input = devm_input_allocate_device(&client->dev);
1187 	if (!input) {
1188 		dev_err(&client->dev, "failed to allocate input device.\n");
1189 		return -ENOMEM;
1190 	}
1191 
1192 	mutex_init(&tsdata->mutex);
1193 	tsdata->client = client;
1194 	tsdata->input = input;
1195 	tsdata->factory_mode = false;
1196 
1197 	error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1198 	if (error) {
1199 		dev_err(&client->dev, "touchscreen probe failed\n");
1200 		return error;
1201 	}
1202 
1203 	/*
1204 	 * Dummy read access. EP0700MLP1 returns bogus data on the first
1205 	 * register read access and ignores writes.
1206 	 */
1207 	edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf);
1208 
1209 	edt_ft5x06_ts_set_regs(tsdata);
1210 	edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1211 	edt_ft5x06_ts_get_parameters(tsdata);
1212 
1213 	dev_dbg(&client->dev,
1214 		"Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1215 		tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1216 
1217 	input->name = tsdata->name;
1218 	input->id.bustype = BUS_I2C;
1219 	input->dev.parent = &client->dev;
1220 
1221 	input_set_abs_params(input, ABS_MT_POSITION_X,
1222 			     0, tsdata->num_x * 64 - 1, 0, 0);
1223 	input_set_abs_params(input, ABS_MT_POSITION_Y,
1224 			     0, tsdata->num_y * 64 - 1, 0, 0);
1225 
1226 	touchscreen_parse_properties(input, true, &tsdata->prop);
1227 
1228 	error = input_mt_init_slots(input, tsdata->max_support_points,
1229 				INPUT_MT_DIRECT);
1230 	if (error) {
1231 		dev_err(&client->dev, "Unable to init MT slots.\n");
1232 		return error;
1233 	}
1234 
1235 	i2c_set_clientdata(client, tsdata);
1236 
1237 	irq_flags = irq_get_trigger_type(client->irq);
1238 	if (irq_flags == IRQF_TRIGGER_NONE)
1239 		irq_flags = IRQF_TRIGGER_FALLING;
1240 	irq_flags |= IRQF_ONESHOT;
1241 
1242 	error = devm_request_threaded_irq(&client->dev, client->irq,
1243 					NULL, edt_ft5x06_ts_isr, irq_flags,
1244 					client->name, tsdata);
1245 	if (error) {
1246 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1247 		return error;
1248 	}
1249 
1250 	error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1251 	if (error)
1252 		return error;
1253 
1254 	error = input_register_device(input);
1255 	if (error)
1256 		return error;
1257 
1258 	edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1259 
1260 	dev_dbg(&client->dev,
1261 		"EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1262 		client->irq,
1263 		tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1264 		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1265 
1266 	return 0;
1267 }
1268 
1269 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1270 {
1271 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1272 
1273 	edt_ft5x06_ts_teardown_debugfs(tsdata);
1274 
1275 	return 0;
1276 }
1277 
1278 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1279 {
1280 	struct i2c_client *client = to_i2c_client(dev);
1281 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1282 	struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1283 	int ret;
1284 
1285 	if (device_may_wakeup(dev))
1286 		return 0;
1287 
1288 	if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1289 		return 0;
1290 
1291 	/* Enter hibernate mode. */
1292 	ret = edt_ft5x06_register_write(tsdata, PMOD_REGISTER_OPMODE,
1293 					PMOD_REGISTER_HIBERNATE);
1294 	if (ret)
1295 		dev_warn(dev, "Failed to set hibernate mode\n");
1296 
1297 	if (tsdata->suspend_mode == EDT_PMODE_HIBERNATE)
1298 		return 0;
1299 
1300 	/*
1301 	 * Power-off according the datasheet. Cut the power may leaf the irq
1302 	 * line in an undefined state depending on the host pull resistor
1303 	 * settings. Disable the irq to avoid adjusting each host till the
1304 	 * device is back in a full functional state.
1305 	 */
1306 	disable_irq(tsdata->client->irq);
1307 
1308 	gpiod_set_value_cansleep(reset_gpio, 1);
1309 	usleep_range(1000, 2000);
1310 
1311 	ret = regulator_disable(tsdata->vcc);
1312 	if (ret)
1313 		dev_warn(dev, "Failed to disable vcc\n");
1314 	ret = regulator_disable(tsdata->iovcc);
1315 	if (ret)
1316 		dev_warn(dev, "Failed to disable iovcc\n");
1317 
1318 	return 0;
1319 }
1320 
1321 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1322 {
1323 	struct i2c_client *client = to_i2c_client(dev);
1324 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1325 	int ret = 0;
1326 
1327 	if (device_may_wakeup(dev))
1328 		return 0;
1329 
1330 	if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1331 		return 0;
1332 
1333 	if (tsdata->suspend_mode == EDT_PMODE_POWEROFF) {
1334 		struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1335 
1336 		/*
1337 		 * We can't check if the regulator is a dummy or a real
1338 		 * regulator. So we need to specify the 5ms reset time (T_rst)
1339 		 * here instead of the 100us T_rtp time. We also need to wait
1340 		 * 300ms in case it was a real supply and the power was cutted
1341 		 * of. Toggle the reset pin is also a way to exit the hibernate
1342 		 * mode.
1343 		 */
1344 		gpiod_set_value_cansleep(reset_gpio, 1);
1345 		usleep_range(5000, 6000);
1346 
1347 		ret = regulator_enable(tsdata->iovcc);
1348 		if (ret) {
1349 			dev_err(dev, "Failed to enable iovcc\n");
1350 			return ret;
1351 		}
1352 
1353 		/* Delay enabling VCC for > 10us (T_ivd) after IOVCC */
1354 		usleep_range(10, 100);
1355 
1356 		ret = regulator_enable(tsdata->vcc);
1357 		if (ret) {
1358 			dev_err(dev, "Failed to enable vcc\n");
1359 			regulator_disable(tsdata->iovcc);
1360 			return ret;
1361 		}
1362 
1363 		usleep_range(1000, 2000);
1364 		gpiod_set_value_cansleep(reset_gpio, 0);
1365 		msleep(300);
1366 
1367 		edt_ft5x06_restore_reg_parameters(tsdata);
1368 		enable_irq(tsdata->client->irq);
1369 
1370 		if (tsdata->factory_mode)
1371 			ret = edt_ft5x06_factory_mode(tsdata);
1372 	} else {
1373 		struct gpio_desc *wake_gpio = tsdata->wake_gpio;
1374 
1375 		gpiod_set_value_cansleep(wake_gpio, 0);
1376 		usleep_range(5000, 6000);
1377 		gpiod_set_value_cansleep(wake_gpio, 1);
1378 	}
1379 
1380 
1381 	return ret;
1382 }
1383 
1384 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1385 			 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1386 
1387 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1388 	.max_support_points = 5,
1389 };
1390 
1391 static const struct edt_i2c_chip_data edt_ft5506_data = {
1392 	.max_support_points = 10,
1393 };
1394 
1395 static const struct edt_i2c_chip_data edt_ft6236_data = {
1396 	.max_support_points = 2,
1397 };
1398 
1399 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1400 	{ .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1401 	{ .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1402 	{ .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1403 	/* Note no edt- prefix for compatibility with the ft6236.c driver */
1404 	{ .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1405 	{ /* sentinel */ }
1406 };
1407 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1408 
1409 static const struct of_device_id edt_ft5x06_of_match[] = {
1410 	{ .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1411 	{ .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1412 	{ .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1413 	{ .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1414 	{ .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1415 	/* Note focaltech vendor prefix for compatibility with ft6236.c */
1416 	{ .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1417 	{ /* sentinel */ }
1418 };
1419 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1420 
1421 static struct i2c_driver edt_ft5x06_ts_driver = {
1422 	.driver = {
1423 		.name = "edt_ft5x06",
1424 		.of_match_table = edt_ft5x06_of_match,
1425 		.pm = &edt_ft5x06_ts_pm_ops,
1426 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1427 	},
1428 	.id_table = edt_ft5x06_ts_id,
1429 	.probe    = edt_ft5x06_ts_probe,
1430 	.remove   = edt_ft5x06_ts_remove,
1431 };
1432 
1433 module_i2c_driver(edt_ft5x06_ts_driver);
1434 
1435 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1436 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1437 MODULE_LICENSE("GPL v2");
1438