1 /*
2  * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
3  * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
4  * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 /*
21  * This is a driver for the EDT "Polytouch" family of touch controllers
22  * based on the FocalTech FT5x06 line of chips.
23  *
24  * Development of this driver has been sponsored by Glyn:
25  *    http://www.glyn.com/Products/Displays
26  */
27 
28 #include <linux/module.h>
29 #include <linux/ratelimit.h>
30 #include <linux/irq.h>
31 #include <linux/interrupt.h>
32 #include <linux/input.h>
33 #include <linux/i2c.h>
34 #include <linux/uaccess.h>
35 #include <linux/delay.h>
36 #include <linux/debugfs.h>
37 #include <linux/slab.h>
38 #include <linux/gpio/consumer.h>
39 #include <linux/input/mt.h>
40 #include <linux/input/touchscreen.h>
41 #include <linux/of_device.h>
42 
43 #define WORK_REGISTER_THRESHOLD		0x00
44 #define WORK_REGISTER_REPORT_RATE	0x08
45 #define WORK_REGISTER_GAIN		0x30
46 #define WORK_REGISTER_OFFSET		0x31
47 #define WORK_REGISTER_NUM_X		0x33
48 #define WORK_REGISTER_NUM_Y		0x34
49 
50 #define M09_REGISTER_THRESHOLD		0x80
51 #define M09_REGISTER_GAIN		0x92
52 #define M09_REGISTER_OFFSET		0x93
53 #define M09_REGISTER_NUM_X		0x94
54 #define M09_REGISTER_NUM_Y		0x95
55 
56 #define NO_REGISTER			0xff
57 
58 #define WORK_REGISTER_OPMODE		0x3c
59 #define FACTORY_REGISTER_OPMODE		0x01
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		1 /* msec */
71 
72 enum edt_ver {
73 	M06,
74 	M09,
75 };
76 
77 struct edt_reg_addr {
78 	int reg_threshold;
79 	int reg_report_rate;
80 	int reg_gain;
81 	int reg_offset;
82 	int reg_num_x;
83 	int reg_num_y;
84 };
85 
86 struct edt_ft5x06_ts_data {
87 	struct i2c_client *client;
88 	struct input_dev *input;
89 	u16 num_x;
90 	u16 num_y;
91 
92 	struct gpio_desc *reset_gpio;
93 	struct gpio_desc *wake_gpio;
94 
95 #if defined(CONFIG_DEBUG_FS)
96 	struct dentry *debug_dir;
97 	u8 *raw_buffer;
98 	size_t raw_bufsize;
99 #endif
100 
101 	struct mutex mutex;
102 	bool factory_mode;
103 	int threshold;
104 	int gain;
105 	int offset;
106 	int report_rate;
107 	int max_support_points;
108 
109 	char name[EDT_NAME_LEN];
110 
111 	struct edt_reg_addr reg_addr;
112 	enum edt_ver version;
113 };
114 
115 struct edt_i2c_chip_data {
116 	int  max_support_points;
117 };
118 
119 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
120 				   u16 wr_len, u8 *wr_buf,
121 				   u16 rd_len, u8 *rd_buf)
122 {
123 	struct i2c_msg wrmsg[2];
124 	int i = 0;
125 	int ret;
126 
127 	if (wr_len) {
128 		wrmsg[i].addr  = client->addr;
129 		wrmsg[i].flags = 0;
130 		wrmsg[i].len = wr_len;
131 		wrmsg[i].buf = wr_buf;
132 		i++;
133 	}
134 	if (rd_len) {
135 		wrmsg[i].addr  = client->addr;
136 		wrmsg[i].flags = I2C_M_RD;
137 		wrmsg[i].len = rd_len;
138 		wrmsg[i].buf = rd_buf;
139 		i++;
140 	}
141 
142 	ret = i2c_transfer(client->adapter, wrmsg, i);
143 	if (ret < 0)
144 		return ret;
145 	if (ret != i)
146 		return -EIO;
147 
148 	return 0;
149 }
150 
151 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
152 				    u8 *buf, int buflen)
153 {
154 	int i;
155 	u8 crc = 0;
156 
157 	for (i = 0; i < buflen - 1; i++)
158 		crc ^= buf[i];
159 
160 	if (crc != buf[buflen-1]) {
161 		dev_err_ratelimited(&tsdata->client->dev,
162 				    "crc error: 0x%02x expected, got 0x%02x\n",
163 				    crc, buf[buflen-1]);
164 		return false;
165 	}
166 
167 	return true;
168 }
169 
170 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
171 {
172 	struct edt_ft5x06_ts_data *tsdata = dev_id;
173 	struct device *dev = &tsdata->client->dev;
174 	u8 cmd;
175 	u8 rdbuf[63];
176 	int i, type, x, y, id;
177 	int offset, tplen, datalen, crclen;
178 	int error;
179 
180 	switch (tsdata->version) {
181 	case M06:
182 		cmd = 0xf9; /* tell the controller to send touch data */
183 		offset = 5; /* where the actual touch data starts */
184 		tplen = 4;  /* data comes in so called frames */
185 		crclen = 1; /* length of the crc data */
186 		break;
187 
188 	case M09:
189 		cmd = 0x0;
190 		offset = 3;
191 		tplen = 6;
192 		crclen = 0;
193 		break;
194 
195 	default:
196 		goto out;
197 	}
198 
199 	memset(rdbuf, 0, sizeof(rdbuf));
200 	datalen = tplen * tsdata->max_support_points + offset + crclen;
201 
202 	error = edt_ft5x06_ts_readwrite(tsdata->client,
203 					sizeof(cmd), &cmd,
204 					datalen, rdbuf);
205 	if (error) {
206 		dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
207 				    error);
208 		goto out;
209 	}
210 
211 	/* M09 does not send header or CRC */
212 	if (tsdata->version == M06) {
213 		if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
214 			rdbuf[2] != datalen) {
215 			dev_err_ratelimited(dev,
216 					"Unexpected header: %02x%02x%02x!\n",
217 					rdbuf[0], rdbuf[1], rdbuf[2]);
218 			goto out;
219 		}
220 
221 		if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
222 			goto out;
223 	}
224 
225 	for (i = 0; i < tsdata->max_support_points; i++) {
226 		u8 *buf = &rdbuf[i * tplen + offset];
227 		bool down;
228 
229 		type = buf[0] >> 6;
230 		/* ignore Reserved events */
231 		if (type == TOUCH_EVENT_RESERVED)
232 			continue;
233 
234 		/* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
235 		if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN)
236 			continue;
237 
238 		x = ((buf[0] << 8) | buf[1]) & 0x0fff;
239 		y = ((buf[2] << 8) | buf[3]) & 0x0fff;
240 		id = (buf[2] >> 4) & 0x0f;
241 		down = type != TOUCH_EVENT_UP;
242 
243 		input_mt_slot(tsdata->input, id);
244 		input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
245 
246 		if (!down)
247 			continue;
248 
249 		input_report_abs(tsdata->input, ABS_MT_POSITION_X, x);
250 		input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y);
251 	}
252 
253 	input_mt_report_pointer_emulation(tsdata->input, true);
254 	input_sync(tsdata->input);
255 
256 out:
257 	return IRQ_HANDLED;
258 }
259 
260 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
261 				     u8 addr, u8 value)
262 {
263 	u8 wrbuf[4];
264 
265 	switch (tsdata->version) {
266 	case M06:
267 		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
268 		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
269 		wrbuf[2] = value;
270 		wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
271 		return edt_ft5x06_ts_readwrite(tsdata->client, 4,
272 					wrbuf, 0, NULL);
273 	case M09:
274 		wrbuf[0] = addr;
275 		wrbuf[1] = value;
276 
277 		return edt_ft5x06_ts_readwrite(tsdata->client, 2,
278 					wrbuf, 0, NULL);
279 
280 	default:
281 		return -EINVAL;
282 	}
283 }
284 
285 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
286 				    u8 addr)
287 {
288 	u8 wrbuf[2], rdbuf[2];
289 	int error;
290 
291 	switch (tsdata->version) {
292 	case M06:
293 		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
294 		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
295 		wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
296 
297 		error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
298 						rdbuf);
299 		if (error)
300 			return error;
301 
302 		if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
303 			dev_err(&tsdata->client->dev,
304 				"crc error: 0x%02x expected, got 0x%02x\n",
305 				wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
306 				rdbuf[1]);
307 			return -EIO;
308 		}
309 		break;
310 
311 	case M09:
312 		wrbuf[0] = addr;
313 		error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
314 						wrbuf, 1, rdbuf);
315 		if (error)
316 			return error;
317 		break;
318 
319 	default:
320 		return -EINVAL;
321 	}
322 
323 	return rdbuf[0];
324 }
325 
326 struct edt_ft5x06_attribute {
327 	struct device_attribute dattr;
328 	size_t field_offset;
329 	u8 limit_low;
330 	u8 limit_high;
331 	u8 addr_m06;
332 	u8 addr_m09;
333 };
334 
335 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09,			\
336 		_limit_low, _limit_high)				\
337 	struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = {	\
338 		.dattr = __ATTR(_field, _mode,				\
339 				edt_ft5x06_setting_show,		\
340 				edt_ft5x06_setting_store),		\
341 		.field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
342 		.addr_m06 = _addr_m06,					\
343 		.addr_m09 = _addr_m09,					\
344 		.limit_low = _limit_low,				\
345 		.limit_high = _limit_high,				\
346 	}
347 
348 static ssize_t edt_ft5x06_setting_show(struct device *dev,
349 				       struct device_attribute *dattr,
350 				       char *buf)
351 {
352 	struct i2c_client *client = to_i2c_client(dev);
353 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
354 	struct edt_ft5x06_attribute *attr =
355 			container_of(dattr, struct edt_ft5x06_attribute, dattr);
356 	u8 *field = (u8 *)tsdata + attr->field_offset;
357 	int val;
358 	size_t count = 0;
359 	int error = 0;
360 	u8 addr;
361 
362 	mutex_lock(&tsdata->mutex);
363 
364 	if (tsdata->factory_mode) {
365 		error = -EIO;
366 		goto out;
367 	}
368 
369 	switch (tsdata->version) {
370 	case M06:
371 		addr = attr->addr_m06;
372 		break;
373 
374 	case M09:
375 		addr = attr->addr_m09;
376 		break;
377 
378 	default:
379 		error = -ENODEV;
380 		goto out;
381 	}
382 
383 	if (addr != NO_REGISTER) {
384 		val = edt_ft5x06_register_read(tsdata, addr);
385 		if (val < 0) {
386 			error = val;
387 			dev_err(&tsdata->client->dev,
388 				"Failed to fetch attribute %s, error %d\n",
389 				dattr->attr.name, error);
390 			goto out;
391 		}
392 	} else {
393 		val = *field;
394 	}
395 
396 	if (val != *field) {
397 		dev_warn(&tsdata->client->dev,
398 			 "%s: read (%d) and stored value (%d) differ\n",
399 			 dattr->attr.name, val, *field);
400 		*field = val;
401 	}
402 
403 	count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
404 out:
405 	mutex_unlock(&tsdata->mutex);
406 	return error ?: count;
407 }
408 
409 static ssize_t edt_ft5x06_setting_store(struct device *dev,
410 					struct device_attribute *dattr,
411 					const char *buf, size_t count)
412 {
413 	struct i2c_client *client = to_i2c_client(dev);
414 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
415 	struct edt_ft5x06_attribute *attr =
416 			container_of(dattr, struct edt_ft5x06_attribute, dattr);
417 	u8 *field = (u8 *)tsdata + attr->field_offset;
418 	unsigned int val;
419 	int error;
420 	u8 addr;
421 
422 	mutex_lock(&tsdata->mutex);
423 
424 	if (tsdata->factory_mode) {
425 		error = -EIO;
426 		goto out;
427 	}
428 
429 	error = kstrtouint(buf, 0, &val);
430 	if (error)
431 		goto out;
432 
433 	if (val < attr->limit_low || val > attr->limit_high) {
434 		error = -ERANGE;
435 		goto out;
436 	}
437 
438 	switch (tsdata->version) {
439 	case M06:
440 		addr = attr->addr_m06;
441 		break;
442 
443 	case M09:
444 		addr = attr->addr_m09;
445 		break;
446 
447 	default:
448 		error = -ENODEV;
449 		goto out;
450 	}
451 
452 	if (addr != NO_REGISTER) {
453 		error = edt_ft5x06_register_write(tsdata, addr, val);
454 		if (error) {
455 			dev_err(&tsdata->client->dev,
456 				"Failed to update attribute %s, error: %d\n",
457 				dattr->attr.name, error);
458 			goto out;
459 		}
460 	}
461 	*field = val;
462 
463 out:
464 	mutex_unlock(&tsdata->mutex);
465 	return error ?: count;
466 }
467 
468 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
469 		M09_REGISTER_GAIN, 0, 31);
470 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
471 		M09_REGISTER_OFFSET, 0, 31);
472 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
473 		M09_REGISTER_THRESHOLD, 20, 80);
474 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
475 		NO_REGISTER, 3, 14);
476 
477 static struct attribute *edt_ft5x06_attrs[] = {
478 	&edt_ft5x06_attr_gain.dattr.attr,
479 	&edt_ft5x06_attr_offset.dattr.attr,
480 	&edt_ft5x06_attr_threshold.dattr.attr,
481 	&edt_ft5x06_attr_report_rate.dattr.attr,
482 	NULL
483 };
484 
485 static const struct attribute_group edt_ft5x06_attr_group = {
486 	.attrs = edt_ft5x06_attrs,
487 };
488 
489 #ifdef CONFIG_DEBUG_FS
490 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
491 {
492 	struct i2c_client *client = tsdata->client;
493 	int retries = EDT_SWITCH_MODE_RETRIES;
494 	int ret;
495 	int error;
496 
497 	disable_irq(client->irq);
498 
499 	if (!tsdata->raw_buffer) {
500 		tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
501 				      sizeof(u16);
502 		tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
503 		if (!tsdata->raw_buffer) {
504 			error = -ENOMEM;
505 			goto err_out;
506 		}
507 	}
508 
509 	/* mode register is 0x3c when in the work mode */
510 	if (tsdata->version == M09)
511 		goto m09_out;
512 
513 	error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
514 	if (error) {
515 		dev_err(&client->dev,
516 			"failed to switch to factory mode, error %d\n", error);
517 		goto err_out;
518 	}
519 
520 	tsdata->factory_mode = true;
521 	do {
522 		mdelay(EDT_SWITCH_MODE_DELAY);
523 		/* mode register is 0x01 when in factory mode */
524 		ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
525 		if (ret == 0x03)
526 			break;
527 	} while (--retries > 0);
528 
529 	if (retries == 0) {
530 		dev_err(&client->dev, "not in factory mode after %dms.\n",
531 			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
532 		error = -EIO;
533 		goto err_out;
534 	}
535 
536 	return 0;
537 
538 err_out:
539 	kfree(tsdata->raw_buffer);
540 	tsdata->raw_buffer = NULL;
541 	tsdata->factory_mode = false;
542 	enable_irq(client->irq);
543 
544 	return error;
545 
546 m09_out:
547 	dev_err(&client->dev, "No factory mode support for M09\n");
548 	return -EINVAL;
549 
550 }
551 
552 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
553 {
554 	struct i2c_client *client = tsdata->client;
555 	int retries = EDT_SWITCH_MODE_RETRIES;
556 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
557 	int ret;
558 	int error;
559 
560 	/* mode register is 0x01 when in the factory mode */
561 	error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
562 	if (error) {
563 		dev_err(&client->dev,
564 			"failed to switch to work mode, error: %d\n", error);
565 		return error;
566 	}
567 
568 	tsdata->factory_mode = false;
569 
570 	do {
571 		mdelay(EDT_SWITCH_MODE_DELAY);
572 		/* mode register is 0x01 when in factory mode */
573 		ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
574 		if (ret == 0x01)
575 			break;
576 	} while (--retries > 0);
577 
578 	if (retries == 0) {
579 		dev_err(&client->dev, "not in work mode after %dms.\n",
580 			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
581 		tsdata->factory_mode = true;
582 		return -EIO;
583 	}
584 
585 	kfree(tsdata->raw_buffer);
586 	tsdata->raw_buffer = NULL;
587 
588 	/* restore parameters */
589 	edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
590 				  tsdata->threshold);
591 	edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
592 				  tsdata->gain);
593 	edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
594 				  tsdata->offset);
595 	if (reg_addr->reg_report_rate)
596 		edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
597 				  tsdata->report_rate);
598 
599 	enable_irq(client->irq);
600 
601 	return 0;
602 }
603 
604 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
605 {
606 	struct edt_ft5x06_ts_data *tsdata = data;
607 
608 	*mode = tsdata->factory_mode;
609 
610 	return 0;
611 };
612 
613 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
614 {
615 	struct edt_ft5x06_ts_data *tsdata = data;
616 	int retval = 0;
617 
618 	if (mode > 1)
619 		return -ERANGE;
620 
621 	mutex_lock(&tsdata->mutex);
622 
623 	if (mode != tsdata->factory_mode) {
624 		retval = mode ? edt_ft5x06_factory_mode(tsdata) :
625 				edt_ft5x06_work_mode(tsdata);
626 	}
627 
628 	mutex_unlock(&tsdata->mutex);
629 
630 	return retval;
631 };
632 
633 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
634 			edt_ft5x06_debugfs_mode_set, "%llu\n");
635 
636 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
637 				char __user *buf, size_t count, loff_t *off)
638 {
639 	struct edt_ft5x06_ts_data *tsdata = file->private_data;
640 	struct i2c_client *client = tsdata->client;
641 	int retries  = EDT_RAW_DATA_RETRIES;
642 	int val, i, error;
643 	size_t read = 0;
644 	int colbytes;
645 	char wrbuf[3];
646 	u8 *rdbuf;
647 
648 	if (*off < 0 || *off >= tsdata->raw_bufsize)
649 		return 0;
650 
651 	mutex_lock(&tsdata->mutex);
652 
653 	if (!tsdata->factory_mode || !tsdata->raw_buffer) {
654 		error = -EIO;
655 		goto out;
656 	}
657 
658 	error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
659 	if (error) {
660 		dev_dbg(&client->dev,
661 			"failed to write 0x08 register, error %d\n", error);
662 		goto out;
663 	}
664 
665 	do {
666 		msleep(EDT_RAW_DATA_DELAY);
667 		val = edt_ft5x06_register_read(tsdata, 0x08);
668 		if (val < 1)
669 			break;
670 	} while (--retries > 0);
671 
672 	if (val < 0) {
673 		error = val;
674 		dev_dbg(&client->dev,
675 			"failed to read 0x08 register, error %d\n", error);
676 		goto out;
677 	}
678 
679 	if (retries == 0) {
680 		dev_dbg(&client->dev,
681 			"timed out waiting for register to settle\n");
682 		error = -ETIMEDOUT;
683 		goto out;
684 	}
685 
686 	rdbuf = tsdata->raw_buffer;
687 	colbytes = tsdata->num_y * sizeof(u16);
688 
689 	wrbuf[0] = 0xf5;
690 	wrbuf[1] = 0x0e;
691 	for (i = 0; i < tsdata->num_x; i++) {
692 		wrbuf[2] = i;  /* column index */
693 		error = edt_ft5x06_ts_readwrite(tsdata->client,
694 						sizeof(wrbuf), wrbuf,
695 						colbytes, rdbuf);
696 		if (error)
697 			goto out;
698 
699 		rdbuf += colbytes;
700 	}
701 
702 	read = min_t(size_t, count, tsdata->raw_bufsize - *off);
703 	if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
704 		error = -EFAULT;
705 		goto out;
706 	}
707 
708 	*off += read;
709 out:
710 	mutex_unlock(&tsdata->mutex);
711 	return error ?: read;
712 };
713 
714 static const struct file_operations debugfs_raw_data_fops = {
715 	.open = simple_open,
716 	.read = edt_ft5x06_debugfs_raw_data_read,
717 };
718 
719 static void
720 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
721 			      const char *debugfs_name)
722 {
723 	tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
724 	if (!tsdata->debug_dir)
725 		return;
726 
727 	debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
728 	debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
729 
730 	debugfs_create_file("mode", S_IRUSR | S_IWUSR,
731 			    tsdata->debug_dir, tsdata, &debugfs_mode_fops);
732 	debugfs_create_file("raw_data", S_IRUSR,
733 			    tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
734 }
735 
736 static void
737 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
738 {
739 	debugfs_remove_recursive(tsdata->debug_dir);
740 	kfree(tsdata->raw_buffer);
741 }
742 
743 #else
744 
745 static inline void
746 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
747 			      const char *debugfs_name)
748 {
749 }
750 
751 static inline void
752 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
753 {
754 }
755 
756 #endif /* CONFIG_DEBUGFS */
757 
758 static int edt_ft5x06_ts_identify(struct i2c_client *client,
759 					struct edt_ft5x06_ts_data *tsdata,
760 					char *fw_version)
761 {
762 	u8 rdbuf[EDT_NAME_LEN];
763 	char *p;
764 	int error;
765 	char *model_name = tsdata->name;
766 
767 	/* see what we find if we assume it is a M06 *
768 	 * if we get less than EDT_NAME_LEN, we don't want
769 	 * to have garbage in there
770 	 */
771 	memset(rdbuf, 0, sizeof(rdbuf));
772 	error = edt_ft5x06_ts_readwrite(client, 1, "\xbb",
773 					EDT_NAME_LEN - 1, rdbuf);
774 	if (error)
775 		return error;
776 
777 	/* if we find something consistent, stay with that assumption
778 	 * at least M09 won't send 3 bytes here
779 	 */
780 	if (!(strncasecmp(rdbuf + 1, "EP0", 3))) {
781 		tsdata->version = M06;
782 
783 		/* remove last '$' end marker */
784 		rdbuf[EDT_NAME_LEN - 1] = '\0';
785 		if (rdbuf[EDT_NAME_LEN - 2] == '$')
786 			rdbuf[EDT_NAME_LEN - 2] = '\0';
787 
788 		/* look for Model/Version separator */
789 		p = strchr(rdbuf, '*');
790 		if (p)
791 			*p++ = '\0';
792 		strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
793 		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
794 	} else {
795 		/* since there are only two versions around (M06, M09) */
796 		tsdata->version = M09;
797 
798 		error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
799 						2, rdbuf);
800 		if (error)
801 			return error;
802 
803 		strlcpy(fw_version, rdbuf, 2);
804 
805 		error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
806 						1, rdbuf);
807 		if (error)
808 			return error;
809 
810 		snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
811 			rdbuf[0] >> 4, rdbuf[0] & 0x0F);
812 	}
813 
814 	return 0;
815 }
816 
817 static void edt_ft5x06_ts_get_defaults(struct device *dev,
818 				       struct edt_ft5x06_ts_data *tsdata)
819 {
820 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
821 	u32 val;
822 	int error;
823 
824 	error = device_property_read_u32(dev, "threshold", &val);
825 	if (!error) {
826 		edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
827 		tsdata->threshold = val;
828 	}
829 
830 	error = device_property_read_u32(dev, "gain", &val);
831 	if (!error) {
832 		edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
833 		tsdata->gain = val;
834 	}
835 
836 	error = device_property_read_u32(dev, "offset", &val);
837 	if (!error) {
838 		edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
839 		tsdata->offset = val;
840 	}
841 }
842 
843 static void
844 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
845 {
846 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
847 
848 	tsdata->threshold = edt_ft5x06_register_read(tsdata,
849 						     reg_addr->reg_threshold);
850 	tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
851 	tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
852 	if (reg_addr->reg_report_rate != NO_REGISTER)
853 		tsdata->report_rate = edt_ft5x06_register_read(tsdata,
854 						reg_addr->reg_report_rate);
855 	tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x);
856 	tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y);
857 }
858 
859 static void
860 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
861 {
862 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
863 
864 	switch (tsdata->version) {
865 	case M06:
866 		reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
867 		reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
868 		reg_addr->reg_gain = WORK_REGISTER_GAIN;
869 		reg_addr->reg_offset = WORK_REGISTER_OFFSET;
870 		reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
871 		reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
872 		break;
873 
874 	case M09:
875 		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
876 		reg_addr->reg_gain = M09_REGISTER_GAIN;
877 		reg_addr->reg_offset = M09_REGISTER_OFFSET;
878 		reg_addr->reg_num_x = M09_REGISTER_NUM_X;
879 		reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
880 		break;
881 	}
882 }
883 
884 static int edt_ft5x06_ts_probe(struct i2c_client *client,
885 					 const struct i2c_device_id *id)
886 {
887 	const struct edt_i2c_chip_data *chip_data;
888 	struct edt_ft5x06_ts_data *tsdata;
889 	struct input_dev *input;
890 	unsigned long irq_flags;
891 	int error;
892 	char fw_version[EDT_NAME_LEN];
893 
894 	dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
895 
896 	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
897 	if (!tsdata) {
898 		dev_err(&client->dev, "failed to allocate driver data.\n");
899 		return -ENOMEM;
900 	}
901 
902 	chip_data = of_device_get_match_data(&client->dev);
903 	if (!chip_data)
904 		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
905 	if (!chip_data || !chip_data->max_support_points) {
906 		dev_err(&client->dev, "invalid or missing chip data\n");
907 		return -EINVAL;
908 	}
909 
910 	tsdata->max_support_points = chip_data->max_support_points;
911 
912 	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
913 						     "reset", GPIOD_OUT_HIGH);
914 	if (IS_ERR(tsdata->reset_gpio)) {
915 		error = PTR_ERR(tsdata->reset_gpio);
916 		dev_err(&client->dev,
917 			"Failed to request GPIO reset pin, error %d\n", error);
918 		return error;
919 	}
920 
921 	tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
922 						    "wake", GPIOD_OUT_LOW);
923 	if (IS_ERR(tsdata->wake_gpio)) {
924 		error = PTR_ERR(tsdata->wake_gpio);
925 		dev_err(&client->dev,
926 			"Failed to request GPIO wake pin, error %d\n", error);
927 		return error;
928 	}
929 
930 	if (tsdata->wake_gpio) {
931 		usleep_range(5000, 6000);
932 		gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
933 	}
934 
935 	if (tsdata->reset_gpio) {
936 		usleep_range(5000, 6000);
937 		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
938 		msleep(300);
939 	}
940 
941 	input = devm_input_allocate_device(&client->dev);
942 	if (!input) {
943 		dev_err(&client->dev, "failed to allocate input device.\n");
944 		return -ENOMEM;
945 	}
946 
947 	mutex_init(&tsdata->mutex);
948 	tsdata->client = client;
949 	tsdata->input = input;
950 	tsdata->factory_mode = false;
951 
952 	error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
953 	if (error) {
954 		dev_err(&client->dev, "touchscreen probe failed\n");
955 		return error;
956 	}
957 
958 	edt_ft5x06_ts_set_regs(tsdata);
959 	edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
960 	edt_ft5x06_ts_get_parameters(tsdata);
961 
962 	dev_dbg(&client->dev,
963 		"Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
964 		tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
965 
966 	input->name = tsdata->name;
967 	input->id.bustype = BUS_I2C;
968 	input->dev.parent = &client->dev;
969 
970 	input_set_abs_params(input, ABS_MT_POSITION_X,
971 			     0, tsdata->num_x * 64 - 1, 0, 0);
972 	input_set_abs_params(input, ABS_MT_POSITION_Y,
973 			     0, tsdata->num_y * 64 - 1, 0, 0);
974 
975 	touchscreen_parse_properties(input, true);
976 
977 	error = input_mt_init_slots(input, tsdata->max_support_points,
978 				INPUT_MT_DIRECT);
979 	if (error) {
980 		dev_err(&client->dev, "Unable to init MT slots.\n");
981 		return error;
982 	}
983 
984 	input_set_drvdata(input, tsdata);
985 	i2c_set_clientdata(client, tsdata);
986 
987 	irq_flags = irq_get_trigger_type(client->irq);
988 	if (irq_flags == IRQF_TRIGGER_NONE)
989 		irq_flags = IRQF_TRIGGER_FALLING;
990 	irq_flags |= IRQF_ONESHOT;
991 
992 	error = devm_request_threaded_irq(&client->dev, client->irq,
993 					NULL, edt_ft5x06_ts_isr, irq_flags,
994 					client->name, tsdata);
995 	if (error) {
996 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
997 		return error;
998 	}
999 
1000 	error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1001 	if (error)
1002 		return error;
1003 
1004 	error = input_register_device(input);
1005 	if (error)
1006 		goto err_remove_attrs;
1007 
1008 	edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1009 	device_init_wakeup(&client->dev, 1);
1010 
1011 	dev_dbg(&client->dev,
1012 		"EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1013 		client->irq,
1014 		tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1015 		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1016 
1017 	return 0;
1018 
1019 err_remove_attrs:
1020 	sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1021 	return error;
1022 }
1023 
1024 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1025 {
1026 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1027 
1028 	edt_ft5x06_ts_teardown_debugfs(tsdata);
1029 	sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1030 
1031 	return 0;
1032 }
1033 
1034 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1035 {
1036 	struct i2c_client *client = to_i2c_client(dev);
1037 
1038 	if (device_may_wakeup(dev))
1039 		enable_irq_wake(client->irq);
1040 
1041 	return 0;
1042 }
1043 
1044 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1045 {
1046 	struct i2c_client *client = to_i2c_client(dev);
1047 
1048 	if (device_may_wakeup(dev))
1049 		disable_irq_wake(client->irq);
1050 
1051 	return 0;
1052 }
1053 
1054 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1055 			 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1056 
1057 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1058 	.max_support_points = 5,
1059 };
1060 
1061 static const struct edt_i2c_chip_data edt_ft5506_data = {
1062 	.max_support_points = 10,
1063 };
1064 
1065 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1066 	{ .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1067 	{ .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1068 	{ /* sentinel */ }
1069 };
1070 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1071 
1072 #ifdef CONFIG_OF
1073 static const struct of_device_id edt_ft5x06_of_match[] = {
1074 	{ .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1075 	{ .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1076 	{ .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1077 	{ .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1078 	{ /* sentinel */ }
1079 };
1080 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1081 #endif
1082 
1083 static struct i2c_driver edt_ft5x06_ts_driver = {
1084 	.driver = {
1085 		.name = "edt_ft5x06",
1086 		.of_match_table = of_match_ptr(edt_ft5x06_of_match),
1087 		.pm = &edt_ft5x06_ts_pm_ops,
1088 	},
1089 	.id_table = edt_ft5x06_ts_id,
1090 	.probe    = edt_ft5x06_ts_probe,
1091 	.remove   = edt_ft5x06_ts_remove,
1092 };
1093 
1094 module_i2c_driver(edt_ft5x06_ts_driver);
1095 
1096 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1097 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1098 MODULE_LICENSE("GPL");
1099