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