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