1177387Sgnn// SPDX-License-Identifier: GPL-2.0
2177388Sgnn/*
3177387Sgnn * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
4177387Sgnn * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
5177387Sgnn * Lothar Wa��mann <LW@KARO-electronics.de> (DT support)
6177388Sgnn * Dario Binacchi <dario.binacchi@amarulasolutions.com> (regmap support)
7177388Sgnn */
8177388Sgnn
9177388Sgnn/*
10177388Sgnn * This is a driver for the EDT "Polytouch" family of touch controllers
11177388Sgnn * based on the FocalTech FT5x06 line of chips.
12177388Sgnn *
13177388Sgnn * Development of this driver has been sponsored by Glyn:
14177388Sgnn *    http://www.glyn.com/Products/Displays
15177388Sgnn */
16177388Sgnn
17177388Sgnn#include <linux/debugfs.h>
18177388Sgnn#include <linux/delay.h>
19177388Sgnn#include <linux/gpio/consumer.h>
20177388Sgnn#include <linux/i2c.h>
21177388Sgnn#include <linux/interrupt.h>
22177388Sgnn#include <linux/input.h>
23177388Sgnn#include <linux/input/mt.h>
24177388Sgnn#include <linux/input/touchscreen.h>
25177388Sgnn#include <linux/irq.h>
26177388Sgnn#include <linux/kernel.h>
27177387Sgnn#include <linux/module.h>
28177387Sgnn#include <linux/property.h>
29177387Sgnn#include <linux/ratelimit.h>
30177387Sgnn#include <linux/regmap.h>
31177387Sgnn#include <linux/regulator/consumer.h>
32177387Sgnn#include <linux/slab.h>
33177387Sgnn#include <linux/uaccess.h>
34177387Sgnn
35177387Sgnn#include <asm/unaligned.h>
36177387Sgnn
37177387Sgnn#define WORK_REGISTER_THRESHOLD		0x00
38177387Sgnn#define WORK_REGISTER_REPORT_RATE	0x08
39177387Sgnn#define WORK_REGISTER_GAIN		0x30
40177387Sgnn#define WORK_REGISTER_OFFSET		0x31
41213327Sgnn#define WORK_REGISTER_NUM_X		0x33
42213327Sgnn#define WORK_REGISTER_NUM_Y		0x34
43177387Sgnn
44177387Sgnn#define PMOD_REGISTER_ACTIVE		0x00
45177387Sgnn#define PMOD_REGISTER_HIBERNATE		0x03
46177387Sgnn
47177387Sgnn#define M09_REGISTER_THRESHOLD		0x80
48177387Sgnn#define M09_REGISTER_GAIN		0x92
49177387Sgnn#define M09_REGISTER_OFFSET		0x93
50177387Sgnn#define M09_REGISTER_NUM_X		0x94
51177387Sgnn#define M09_REGISTER_NUM_Y		0x95
52177387Sgnn
53177387Sgnn#define M12_REGISTER_REPORT_RATE	0x88
54177387Sgnn
55177387Sgnn#define EV_REGISTER_THRESHOLD		0x40
56177387Sgnn#define EV_REGISTER_GAIN		0x41
57177387Sgnn#define EV_REGISTER_OFFSET_Y		0x45
58177387Sgnn#define EV_REGISTER_OFFSET_X		0x46
59177387Sgnn
60177387Sgnn#define NO_REGISTER			0xff
61177387Sgnn
62177387Sgnn#define WORK_REGISTER_OPMODE		0x3c
63177387Sgnn#define FACTORY_REGISTER_OPMODE		0x01
64177387Sgnn#define PMOD_REGISTER_OPMODE		0xa5
65177387Sgnn
66178456Sgnn#define TOUCH_EVENT_DOWN		0x00
67177387Sgnn#define TOUCH_EVENT_UP			0x01
68177387Sgnn#define TOUCH_EVENT_ON			0x02
69177387Sgnn#define TOUCH_EVENT_RESERVED		0x03
70177387Sgnn
71177387Sgnn#define EDT_NAME_LEN			23
72177387Sgnn#define EDT_SWITCH_MODE_RETRIES		10
73177387Sgnn#define EDT_SWITCH_MODE_DELAY		5 /* msec */
74177387Sgnn#define EDT_RAW_DATA_RETRIES		100
75177387Sgnn#define EDT_RAW_DATA_DELAY		1000 /* usec */
76177387Sgnn
77177387Sgnn#define EDT_DEFAULT_NUM_X		1024
78177387Sgnn#define EDT_DEFAULT_NUM_Y		1024
79177387Sgnn
80177387Sgnn#define M06_REG_CMD(factory) ((factory) ? 0xf3 : 0xfc)
81177387Sgnn#define M06_REG_ADDR(factory, addr) ((factory) ? (addr) & 0x7f : (addr) & 0x3f)
82177387Sgnn
83177387Sgnnenum edt_pmode {
84177387Sgnn	EDT_PMODE_NOT_SUPPORTED,
85177387Sgnn	EDT_PMODE_HIBERNATE,
86177387Sgnn	EDT_PMODE_POWEROFF,
87177387Sgnn};
88177387Sgnn
89177387Sgnnenum edt_ver {
90177387Sgnn	EDT_M06,
91178456Sgnn	EDT_M09,
92178456Sgnn	EDT_M12,
93177387Sgnn	EV_FT,
94177387Sgnn	GENERIC_FT,
95177387Sgnn};
96178456Sgnn
97179528Sgnnstruct edt_reg_addr {
98177387Sgnn	int reg_threshold;
99177387Sgnn	int reg_report_rate;
100177872Sgnn	int reg_gain;
101177387Sgnn	int reg_offset;
102177387Sgnn	int reg_offset_x;
103177387Sgnn	int reg_offset_y;
104177387Sgnn	int reg_num_x;
105177387Sgnn	int reg_num_y;
106177387Sgnn};
107177387Sgnn
108177387Sgnnstruct edt_ft5x06_ts_data {
109177387Sgnn	struct i2c_client *client;
110177387Sgnn	struct input_dev *input;
111177387Sgnn	struct touchscreen_properties prop;
112177387Sgnn	u16 num_x;
113177387Sgnn	u16 num_y;
114177387Sgnn	struct regulator *vcc;
115177387Sgnn	struct regulator *iovcc;
116177387Sgnn
117177387Sgnn	struct gpio_desc *reset_gpio;
118177387Sgnn	struct gpio_desc *wake_gpio;
119177872Sgnn
120177872Sgnn	struct regmap *regmap;
121177872Sgnn
122177872Sgnn#if defined(CONFIG_DEBUG_FS)
123177872Sgnn	struct dentry *debug_dir;
124177387Sgnn	u8 *raw_buffer;
125177387Sgnn	size_t raw_bufsize;
126177387Sgnn#endif
127177387Sgnn
128177387Sgnn	struct mutex mutex;
129177387Sgnn	bool factory_mode;
130177387Sgnn	enum edt_pmode suspend_mode;
131177387Sgnn	int threshold;
132177387Sgnn	int gain;
133177387Sgnn	int offset;
134177387Sgnn	int offset_x;
135177387Sgnn	int offset_y;
136177387Sgnn	int report_rate;
137177387Sgnn	int max_support_points;
138177387Sgnn	int point_len;
139177387Sgnn	u8 tdata_cmd;
140177387Sgnn	int tdata_len;
141177387Sgnn	int tdata_offset;
142177387Sgnn
143177387Sgnn	char name[EDT_NAME_LEN];
144177387Sgnn	char fw_version[EDT_NAME_LEN];
145177387Sgnn
146177387Sgnn	struct edt_reg_addr reg_addr;
147177387Sgnn	enum edt_ver version;
148177387Sgnn	unsigned int crc_errors;
149177387Sgnn	unsigned int header_errors;
150177387Sgnn};
151177387Sgnn
152177387Sgnnstruct edt_i2c_chip_data {
153177387Sgnn	int  max_support_points;
154177387Sgnn};
155177387Sgnn
156177387Sgnnstatic const struct regmap_config edt_ft5x06_i2c_regmap_config = {
157177387Sgnn	.reg_bits = 8,
158177387Sgnn	.val_bits = 8,
159177387Sgnn};
160177387Sgnn
161177387Sgnnstatic bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
162177387Sgnn				    u8 *buf, int buflen)
163177387Sgnn{
164177387Sgnn	int i;
165177387Sgnn	u8 crc = 0;
166177387Sgnn
167177387Sgnn	for (i = 0; i < buflen - 1; i++)
168177387Sgnn		crc ^= buf[i];
169177387Sgnn
170177387Sgnn	if (crc != buf[buflen - 1]) {
171177387Sgnn		tsdata->crc_errors++;
172177872Sgnn		dev_err_ratelimited(&tsdata->client->dev,
173177387Sgnn				    "crc error: 0x%02x expected, got 0x%02x\n",
174177387Sgnn				    crc, buf[buflen - 1]);
175177387Sgnn		return false;
176177387Sgnn	}
177177387Sgnn
178177387Sgnn	return true;
179177387Sgnn}
180178456Sgnn
181178456Sgnnstatic int edt_M06_i2c_read(void *context, const void *reg_buf, size_t reg_size,
182178456Sgnn			    void *val_buf, size_t val_size)
183178456Sgnn{
184178456Sgnn	struct device *dev = context;
185178456Sgnn	struct i2c_client *i2c = to_i2c_client(dev);
186179528Sgnn	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(i2c);
187178456Sgnn	struct i2c_msg xfer[2];
188178456Sgnn	bool reg_read = false;
189178456Sgnn	u8 addr;
190178456Sgnn	u8 wlen;
191178456Sgnn	u8 wbuf[4], rbuf[3];
192177872Sgnn	int ret;
193177387Sgnn
194177387Sgnn	addr = *((u8 *)reg_buf);
195177387Sgnn	wbuf[0] = addr;
196177387Sgnn	switch (addr) {
197177387Sgnn	case 0xf5:
198177387Sgnn		wlen = 3;
199177387Sgnn		wbuf[0] = 0xf5;
200177387Sgnn		wbuf[1] = 0xe;
201177387Sgnn		wbuf[2] = *((u8 *)val_buf);
202177387Sgnn		break;
203177387Sgnn	case 0xf9:
204177387Sgnn		wlen = 1;
205177387Sgnn		break;
206177387Sgnn	default:
207177387Sgnn		wlen = 2;
208177387Sgnn		reg_read = true;
209177387Sgnn		wbuf[0] = M06_REG_CMD(tsdata->factory_mode);
210177387Sgnn		wbuf[1] = M06_REG_ADDR(tsdata->factory_mode, addr);
211177387Sgnn		wbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
212177387Sgnn	}
213177387Sgnn
214177387Sgnn	xfer[0].addr  = i2c->addr;
215177387Sgnn	xfer[0].flags = 0;
216177387Sgnn	xfer[0].len = wlen;
217177387Sgnn	xfer[0].buf = wbuf;
218177387Sgnn
219177387Sgnn	xfer[1].addr = i2c->addr;
220177387Sgnn	xfer[1].flags = I2C_M_RD;
221177872Sgnn	xfer[1].len = reg_read ? 2 : val_size;
222177872Sgnn	xfer[1].buf = reg_read ? rbuf : val_buf;
223178456Sgnn
224177872Sgnn	ret = i2c_transfer(i2c->adapter, xfer, 2);
225177872Sgnn	if (ret != 2) {
226177872Sgnn		if (ret < 0)
227178456Sgnn			return ret;
228177872Sgnn
229177872Sgnn		return -EIO;
230177872Sgnn	}
231177872Sgnn
232177872Sgnn	if (addr == 0xf9) {
233177872Sgnn		u8 *buf = (u8 *)val_buf;
234177872Sgnn
235177872Sgnn		if (buf[0] != 0xaa || buf[1] != 0xaa ||
236177872Sgnn		    buf[2] != val_size) {
237177872Sgnn			tsdata->header_errors++;
238177872Sgnn			dev_err_ratelimited(dev,
239177872Sgnn					    "Unexpected header: %02x%02x%02x\n",
240177872Sgnn					    buf[0], buf[1], buf[2]);
241177872Sgnn			return -EIO;
242177872Sgnn		}
243177872Sgnn
244177872Sgnn		if (!edt_ft5x06_ts_check_crc(tsdata, val_buf, val_size))
245177872Sgnn			return -EIO;
246177872Sgnn	} else if (reg_read) {
247177872Sgnn		wbuf[2] = rbuf[0];
248177872Sgnn		wbuf[3] = rbuf[1];
249177872Sgnn		if (!edt_ft5x06_ts_check_crc(tsdata, wbuf, 4))
250177872Sgnn			return -EIO;
251177872Sgnn
252177872Sgnn		*((u8 *)val_buf) = rbuf[0];
253177872Sgnn	}
254179528Sgnn
255177872Sgnn	return 0;
256177872Sgnn}
257177872Sgnn
258177872Sgnnstatic int edt_M06_i2c_write(void *context, const void *data, size_t count)
259177872Sgnn{
260177872Sgnn	struct device *dev = context;
261177872Sgnn	struct i2c_client *i2c = to_i2c_client(dev);
262177872Sgnn	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(i2c);
263177872Sgnn	u8 addr, val;
264177872Sgnn	u8 wbuf[4];
265177872Sgnn	struct i2c_msg xfer;
266177872Sgnn	int ret;
267177872Sgnn
268177872Sgnn	addr = *((u8 *)data);
269177872Sgnn	val = *((u8 *)data + 1);
270177872Sgnn
271177872Sgnn	wbuf[0] = M06_REG_CMD(tsdata->factory_mode);
272177872Sgnn	wbuf[1] = M06_REG_ADDR(tsdata->factory_mode, addr);
273177872Sgnn	wbuf[2] = val;
274177872Sgnn	wbuf[3] = wbuf[0] ^ wbuf[1] ^ wbuf[2];
275177872Sgnn
276177872Sgnn	xfer.addr  = i2c->addr;
277177872Sgnn	xfer.flags = 0;
278177872Sgnn	xfer.len = 4;
279177872Sgnn	xfer.buf = wbuf;
280177872Sgnn
281177872Sgnn	ret = i2c_transfer(i2c->adapter, &xfer, 1);
282177872Sgnn	if (ret != 1) {
283177872Sgnn		if (ret < 0)
284177872Sgnn			return ret;
285177872Sgnn
286177872Sgnn		return -EIO;
287177872Sgnn	}
288177387Sgnn
289177387Sgnn	return 0;
290177387Sgnn}
291177387Sgnn
292177387Sgnnstatic const struct regmap_config edt_M06_i2c_regmap_config = {
293177387Sgnn	.reg_bits = 8,
294177387Sgnn	.val_bits = 8,
295178456Sgnn	.read = edt_M06_i2c_read,
296177387Sgnn	.write = edt_M06_i2c_write,
297177387Sgnn};
298177387Sgnn
299177387Sgnnstatic irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
300179528Sgnn{
301177387Sgnn	struct edt_ft5x06_ts_data *tsdata = dev_id;
302177387Sgnn	struct device *dev = &tsdata->client->dev;
303177387Sgnn	u8 rdbuf[63];
304177387Sgnn	int i, type, x, y, id;
305177387Sgnn	int error;
306177387Sgnn
307177387Sgnn	memset(rdbuf, 0, sizeof(rdbuf));
308177387Sgnn	error = regmap_bulk_read(tsdata->regmap, tsdata->tdata_cmd, rdbuf,
309177387Sgnn				 tsdata->tdata_len);
310177387Sgnn	if (error) {
311177387Sgnn		dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
312177387Sgnn				    error);
313177387Sgnn		goto out;
314177387Sgnn	}
315177387Sgnn
316177387Sgnn	for (i = 0; i < tsdata->max_support_points; i++) {
317177387Sgnn		u8 *buf = &rdbuf[i * tsdata->point_len + tsdata->tdata_offset];
318177387Sgnn
319177387Sgnn		type = buf[0] >> 6;
320177387Sgnn		/* ignore Reserved events */
321177387Sgnn		if (type == TOUCH_EVENT_RESERVED)
322177387Sgnn			continue;
323177387Sgnn
324177387Sgnn		/* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
325177387Sgnn		if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
326177387Sgnn			continue;
327177387Sgnn
328177387Sgnn		x = get_unaligned_be16(buf) & 0x0fff;
329177387Sgnn		y = get_unaligned_be16(buf + 2) & 0x0fff;
330177387Sgnn		/* The FT5x26 send the y coordinate first */
331177387Sgnn		if (tsdata->version == EV_FT)
332177387Sgnn			swap(x, y);
333177387Sgnn
334177387Sgnn		id = (buf[2] >> 4) & 0x0f;
335177387Sgnn
336177387Sgnn		input_mt_slot(tsdata->input, id);
337177387Sgnn		if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
338177387Sgnn					       type != TOUCH_EVENT_UP))
339177387Sgnn			touchscreen_report_pos(tsdata->input, &tsdata->prop,
340177387Sgnn					       x, y, true);
341177387Sgnn	}
342177387Sgnn
343177387Sgnn	input_mt_report_pointer_emulation(tsdata->input, true);
344177387Sgnn	input_sync(tsdata->input);
345177387Sgnn
346177387Sgnnout:
347177387Sgnn	return IRQ_HANDLED;
348177387Sgnn}
349177387Sgnn
350177387Sgnnstruct edt_ft5x06_attribute {
351177387Sgnn	struct device_attribute dattr;
352177387Sgnn	size_t field_offset;
353177387Sgnn	u8 limit_low;
354177387Sgnn	u8 limit_high;
355177387Sgnn	u8 addr_m06;
356177387Sgnn	u8 addr_m09;
357177387Sgnn	u8 addr_ev;
358177387Sgnn};
359177387Sgnn
360177387Sgnn#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev,		\
361177387Sgnn		_limit_low, _limit_high)				\
362177387Sgnn	struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = {	\
363177872Sgnn		.dattr = __ATTR(_field, _mode,				\
364178456Sgnn				edt_ft5x06_setting_show,		\
365178456Sgnn				edt_ft5x06_setting_store),		\
366178456Sgnn		.field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
367177872Sgnn		.addr_m06 = _addr_m06,					\
368178456Sgnn		.addr_m09 = _addr_m09,					\
369178456Sgnn		.addr_ev  = _addr_ev,					\
370178456Sgnn		.limit_low = _limit_low,				\
371178456Sgnn		.limit_high = _limit_high,				\
372179528Sgnn	}
373203800Sru
374178456Sgnnstatic ssize_t edt_ft5x06_setting_show(struct device *dev,
375178456Sgnn				       struct device_attribute *dattr,
376178456Sgnn				       char *buf)
377178456Sgnn{
378177872Sgnn	struct i2c_client *client = to_i2c_client(dev);
379177387Sgnn	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
380177387Sgnn	struct edt_ft5x06_attribute *attr =
381177387Sgnn			container_of(dattr, struct edt_ft5x06_attribute, dattr);
382177387Sgnn	u8 *field = (u8 *)tsdata + attr->field_offset;
383177387Sgnn	unsigned int val;
384177387Sgnn	size_t count = 0;
385177387Sgnn	int error = 0;
386177387Sgnn	u8 addr;
387177387Sgnn
388177387Sgnn	mutex_lock(&tsdata->mutex);
389177872Sgnn
390177387Sgnn	if (tsdata->factory_mode) {
391177387Sgnn		error = -EIO;
392177387Sgnn		goto out;
393177387Sgnn	}
394177387Sgnn
395177387Sgnn	switch (tsdata->version) {
396177872Sgnn	case EDT_M06:
397178456Sgnn		addr = attr->addr_m06;
398203800Sru		break;
399178456Sgnn
400178456Sgnn	case EDT_M09:
401178456Sgnn	case EDT_M12:
402178456Sgnn	case GENERIC_FT:
403178456Sgnn		addr = attr->addr_m09;
404177872Sgnn		break;
405213327Sgnn
406213327Sgnn	case EV_FT:
407213327Sgnn		addr = attr->addr_ev;
408178456Sgnn		break;
409213327Sgnn
410178456Sgnn	default:
411213327Sgnn		error = -ENODEV;
412178456Sgnn		goto out;
413213327Sgnn	}
414213327Sgnn
415213327Sgnn	if (addr != NO_REGISTER) {
416213327Sgnn		error = regmap_read(tsdata->regmap, addr, &val);
417178456Sgnn		if (error) {
418213327Sgnn			dev_err(&tsdata->client->dev,
419213327Sgnn				"Failed to fetch attribute %s, error %d\n",
420213327Sgnn				dattr->attr.name, error);
421178456Sgnn			goto out;
422215409Sgnn		}
423213327Sgnn	} else {
424213327Sgnn		val = *field;
425213327Sgnn	}
426213327Sgnn
427213327Sgnn	if (val != *field) {
428213327Sgnn		dev_warn(&tsdata->client->dev,
429213327Sgnn			 "%s: read (%d) and stored value (%d) differ\n",
430177872Sgnn			 dattr->attr.name, val, *field);
431177872Sgnn		*field = val;
432177387Sgnn	}
433177387Sgnn
434177387Sgnn	count = sysfs_emit(buf, "%d\n", val);
435177387Sgnnout:
436177387Sgnn	mutex_unlock(&tsdata->mutex);
437177387Sgnn	return error ?: count;
438177387Sgnn}
439177387Sgnn
440177387Sgnnstatic ssize_t edt_ft5x06_setting_store(struct device *dev,
441177387Sgnn					struct device_attribute *dattr,
442177387Sgnn					const char *buf, size_t count)
443177387Sgnn{
444177387Sgnn	struct i2c_client *client = to_i2c_client(dev);
445177387Sgnn	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
446177387Sgnn	struct edt_ft5x06_attribute *attr =
447177387Sgnn			container_of(dattr, struct edt_ft5x06_attribute, dattr);
448177387Sgnn	u8 *field = (u8 *)tsdata + attr->field_offset;
449177387Sgnn	unsigned int val;
450177387Sgnn	int error;
451177387Sgnn	u8 addr;
452177387Sgnn
453177872Sgnn	mutex_lock(&tsdata->mutex);
454177387Sgnn
455177872Sgnn	if (tsdata->factory_mode) {
456177872Sgnn		error = -EIO;
457177872Sgnn		goto out;
458178456Sgnn	}
459178456Sgnn
460178456Sgnn	error = kstrtouint(buf, 0, &val);
461179528Sgnn	if (error)
462177387Sgnn		goto out;
463179528Sgnn
464177387Sgnn	if (val < attr->limit_low || val > attr->limit_high) {
465177387Sgnn		error = -ERANGE;
466179528Sgnn		goto out;
467177387Sgnn	}
468177387Sgnn
469177387Sgnn	switch (tsdata->version) {
470177387Sgnn	case EDT_M06:
471177387Sgnn		addr = attr->addr_m06;
472177387Sgnn		break;
473177387Sgnn
474177387Sgnn	case EDT_M09:
475177387Sgnn	case EDT_M12:
476177387Sgnn	case GENERIC_FT:
477177387Sgnn		addr = attr->addr_m09;
478177387Sgnn		break;
479178456Sgnn
480177387Sgnn	case EV_FT:
481177387Sgnn		addr = attr->addr_ev;
482177387Sgnn		break;
483177387Sgnn
484177387Sgnn	default:
485177387Sgnn		error = -ENODEV;
486177387Sgnn		goto out;
487177387Sgnn	}
488177387Sgnn
489177387Sgnn	if (addr != NO_REGISTER) {
490177387Sgnn		error = regmap_write(tsdata->regmap, addr, val);
491177387Sgnn		if (error) {
492177387Sgnn			dev_err(&tsdata->client->dev,
493177387Sgnn				"Failed to update attribute %s, error: %d\n",
494177387Sgnn				dattr->attr.name, error);
495177387Sgnn			goto out;
496177387Sgnn		}
497177387Sgnn	}
498178456Sgnn	*field = val;
499178456Sgnn
500178456Sgnnout:
501178456Sgnn	mutex_unlock(&tsdata->mutex);
502178456Sgnn	return error ?: count;
503178456Sgnn}
504179528Sgnn
505179528Sgnn/* m06, m09: range 0-31, m12: range 0-5 */
506179528Sgnnstatic EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
507177387Sgnn		M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
508177387Sgnn/* m06, m09: range 0-31, m12: range 0-16 */
509177387Sgnnstatic EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
510177387Sgnn		M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
511177387Sgnn/* m06, m09, m12: no supported, ev_ft: range 0-80 */
512177387Sgnnstatic EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
513177387Sgnn		EV_REGISTER_OFFSET_X, 0, 80);
514178456Sgnn/* m06, m09, m12: no supported, ev_ft: range 0-80 */
515178456Sgnnstatic EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
516179528Sgnn		EV_REGISTER_OFFSET_Y, 0, 80);
517179528Sgnn/* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
518177387Sgnnstatic EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
519178456Sgnn		M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
520178456Sgnn/* m06: range 3 to 14, m12: range 1 to 255 */
521179528Sgnnstatic EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
522179528Sgnn		M12_REGISTER_REPORT_RATE, NO_REGISTER, 0, 255);
523177387Sgnn
524177387Sgnnstatic ssize_t model_show(struct device *dev, struct device_attribute *attr,
525177387Sgnn			  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
533static DEVICE_ATTR_RO(model);
534
535static 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
544static DEVICE_ATTR_RO(fw_version);
545
546/* m06 only */
547static 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
556static DEVICE_ATTR_RO(header_errors);
557
558/* m06 only */
559static 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
568static DEVICE_ATTR_RO(crc_errors);
569
570static 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};
583ATTRIBUTE_GROUPS(edt_ft5x06);
584
585static void edt_ft5x06_restore_reg_parameters(struct edt_ft5x06_ts_data *tsdata)
586{
587	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
588	struct regmap *regmap = tsdata->regmap;
589
590	regmap_write(regmap, reg_addr->reg_threshold, tsdata->threshold);
591	regmap_write(regmap, reg_addr->reg_gain, tsdata->gain);
592	if (reg_addr->reg_offset != NO_REGISTER)
593		regmap_write(regmap, reg_addr->reg_offset, tsdata->offset);
594	if (reg_addr->reg_offset_x != NO_REGISTER)
595		regmap_write(regmap, reg_addr->reg_offset_x, tsdata->offset_x);
596	if (reg_addr->reg_offset_y != NO_REGISTER)
597		regmap_write(regmap, reg_addr->reg_offset_y, tsdata->offset_y);
598	if (reg_addr->reg_report_rate != NO_REGISTER)
599		regmap_write(regmap, reg_addr->reg_report_rate,
600			     tsdata->report_rate);
601}
602
603#ifdef CONFIG_DEBUG_FS
604static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
605{
606	struct i2c_client *client = tsdata->client;
607	int retries = EDT_SWITCH_MODE_RETRIES;
608	unsigned int val;
609	int error;
610
611	if (tsdata->version != EDT_M06) {
612		dev_err(&client->dev,
613			"No factory mode support for non-M06 devices\n");
614		return -EINVAL;
615	}
616
617	disable_irq(client->irq);
618
619	if (!tsdata->raw_buffer) {
620		tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
621				      sizeof(u16);
622		tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
623		if (!tsdata->raw_buffer) {
624			error = -ENOMEM;
625			goto err_out;
626		}
627	}
628
629	/* mode register is 0x3c when in the work mode */
630	error = regmap_write(tsdata->regmap, WORK_REGISTER_OPMODE, 0x03);
631	if (error) {
632		dev_err(&client->dev,
633			"failed to switch to factory mode, error %d\n", error);
634		goto err_out;
635	}
636
637	tsdata->factory_mode = true;
638	do {
639		mdelay(EDT_SWITCH_MODE_DELAY);
640		/* mode register is 0x01 when in factory mode */
641		error = regmap_read(tsdata->regmap, FACTORY_REGISTER_OPMODE,
642				    &val);
643		if (!error && val == 0x03)
644			break;
645	} while (--retries > 0);
646
647	if (retries == 0) {
648		dev_err(&client->dev, "not in factory mode after %dms.\n",
649			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
650		error = -EIO;
651		goto err_out;
652	}
653
654	return 0;
655
656err_out:
657	kfree(tsdata->raw_buffer);
658	tsdata->raw_buffer = NULL;
659	tsdata->factory_mode = false;
660	enable_irq(client->irq);
661
662	return error;
663}
664
665static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
666{
667	struct i2c_client *client = tsdata->client;
668	int retries = EDT_SWITCH_MODE_RETRIES;
669	unsigned int val;
670	int error;
671
672	/* mode register is 0x01 when in the factory mode */
673	error = regmap_write(tsdata->regmap, FACTORY_REGISTER_OPMODE, 0x1);
674	if (error) {
675		dev_err(&client->dev,
676			"failed to switch to work mode, error: %d\n", error);
677		return error;
678	}
679
680	tsdata->factory_mode = false;
681
682	do {
683		mdelay(EDT_SWITCH_MODE_DELAY);
684		/* mode register is 0x01 when in factory mode */
685		error = regmap_read(tsdata->regmap, WORK_REGISTER_OPMODE, &val);
686		if (!error && val == 0x01)
687			break;
688	} while (--retries > 0);
689
690	if (retries == 0) {
691		dev_err(&client->dev, "not in work mode after %dms.\n",
692			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
693		tsdata->factory_mode = true;
694		return -EIO;
695	}
696
697	kfree(tsdata->raw_buffer);
698	tsdata->raw_buffer = NULL;
699
700	edt_ft5x06_restore_reg_parameters(tsdata);
701	enable_irq(client->irq);
702
703	return 0;
704}
705
706static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
707{
708	struct edt_ft5x06_ts_data *tsdata = data;
709
710	*mode = tsdata->factory_mode;
711
712	return 0;
713};
714
715static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
716{
717	struct edt_ft5x06_ts_data *tsdata = data;
718	int retval = 0;
719
720	if (mode > 1)
721		return -ERANGE;
722
723	mutex_lock(&tsdata->mutex);
724
725	if (mode != tsdata->factory_mode) {
726		retval = mode ? edt_ft5x06_factory_mode(tsdata) :
727				edt_ft5x06_work_mode(tsdata);
728	}
729
730	mutex_unlock(&tsdata->mutex);
731
732	return retval;
733};
734
735DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
736			edt_ft5x06_debugfs_mode_set, "%llu\n");
737
738static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
739						char __user *buf, size_t count,
740						loff_t *off)
741{
742	struct edt_ft5x06_ts_data *tsdata = file->private_data;
743	struct i2c_client *client = tsdata->client;
744	int retries  = EDT_RAW_DATA_RETRIES;
745	unsigned int val;
746	int i, error;
747	size_t read = 0;
748	int colbytes;
749	u8 *rdbuf;
750
751	if (*off < 0 || *off >= tsdata->raw_bufsize)
752		return 0;
753
754	mutex_lock(&tsdata->mutex);
755
756	if (!tsdata->factory_mode || !tsdata->raw_buffer) {
757		error = -EIO;
758		goto out;
759	}
760
761	error = regmap_write(tsdata->regmap, 0x08, 0x01);
762	if (error) {
763		dev_err(&client->dev,
764			"failed to write 0x08 register, error %d\n", error);
765		goto out;
766	}
767
768	do {
769		usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
770		error = regmap_read(tsdata->regmap, 0x08, &val);
771		if (error) {
772			dev_err(&client->dev,
773				"failed to read 0x08 register, error %d\n",
774				error);
775			goto out;
776		}
777
778		if (val == 1)
779			break;
780	} while (--retries > 0);
781
782	if (retries == 0) {
783		dev_err(&client->dev,
784			"timed out waiting for register to settle\n");
785		error = -ETIMEDOUT;
786		goto out;
787	}
788
789	rdbuf = tsdata->raw_buffer;
790	colbytes = tsdata->num_y * sizeof(u16);
791
792	for (i = 0; i < tsdata->num_x; i++) {
793		rdbuf[0] = i;  /* column index */
794		error = regmap_bulk_read(tsdata->regmap, 0xf5, rdbuf, colbytes);
795		if (error)
796			goto out;
797
798		rdbuf += colbytes;
799	}
800
801	read = min_t(size_t, count, tsdata->raw_bufsize - *off);
802	if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
803		error = -EFAULT;
804		goto out;
805	}
806
807	*off += read;
808out:
809	mutex_unlock(&tsdata->mutex);
810	return error ?: read;
811};
812
813static const struct file_operations debugfs_raw_data_fops = {
814	.open = simple_open,
815	.read = edt_ft5x06_debugfs_raw_data_read,
816};
817
818static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
819					  const char *debugfs_name)
820{
821	tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
822
823	debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
824	debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
825
826	debugfs_create_file("mode", S_IRUSR | S_IWUSR,
827			    tsdata->debug_dir, tsdata, &debugfs_mode_fops);
828	debugfs_create_file("raw_data", S_IRUSR,
829			    tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
830}
831
832static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
833{
834	debugfs_remove_recursive(tsdata->debug_dir);
835	kfree(tsdata->raw_buffer);
836}
837
838#else
839
840static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
841{
842	return -ENOSYS;
843}
844
845static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
846					  const char *debugfs_name)
847{
848}
849
850static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
851{
852}
853
854#endif /* CONFIG_DEBUGFS */
855
856static int edt_ft5x06_ts_identify(struct i2c_client *client,
857				  struct edt_ft5x06_ts_data *tsdata)
858{
859	u8 rdbuf[EDT_NAME_LEN];
860	char *p;
861	int error;
862	char *model_name = tsdata->name;
863	char *fw_version = tsdata->fw_version;
864
865	/* see what we find if we assume it is a M06 *
866	 * if we get less than EDT_NAME_LEN, we don't want
867	 * to have garbage in there
868	 */
869	memset(rdbuf, 0, sizeof(rdbuf));
870	error = regmap_bulk_read(tsdata->regmap, 0xBB, rdbuf, EDT_NAME_LEN - 1);
871	if (error)
872		return error;
873
874	/* Probe content for something consistent.
875	 * M06 starts with a response byte, M12 gives the data directly.
876	 * M09/Generic does not provide model number information.
877	 */
878	if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
879		tsdata->version = EDT_M06;
880
881		/* remove last '$' end marker */
882		rdbuf[EDT_NAME_LEN - 1] = '\0';
883		if (rdbuf[EDT_NAME_LEN - 2] == '$')
884			rdbuf[EDT_NAME_LEN - 2] = '\0';
885
886		/* look for Model/Version separator */
887		p = strchr(rdbuf, '*');
888		if (p)
889			*p++ = '\0';
890		strscpy(model_name, rdbuf + 1, EDT_NAME_LEN);
891		strscpy(fw_version, p ? p : "", EDT_NAME_LEN);
892
893		regmap_exit(tsdata->regmap);
894		tsdata->regmap = regmap_init_i2c(client,
895						 &edt_M06_i2c_regmap_config);
896		if (IS_ERR(tsdata->regmap)) {
897			dev_err(&client->dev, "regmap allocation failed\n");
898			return PTR_ERR(tsdata->regmap);
899		}
900	} else if (!strncasecmp(rdbuf, "EP0", 3)) {
901		tsdata->version = EDT_M12;
902
903		/* remove last '$' end marker */
904		rdbuf[EDT_NAME_LEN - 2] = '\0';
905		if (rdbuf[EDT_NAME_LEN - 3] == '$')
906			rdbuf[EDT_NAME_LEN - 3] = '\0';
907
908		/* look for Model/Version separator */
909		p = strchr(rdbuf, '*');
910		if (p)
911			*p++ = '\0';
912		strscpy(model_name, rdbuf, EDT_NAME_LEN);
913		strscpy(fw_version, p ? p : "", EDT_NAME_LEN);
914	} else {
915		/* If it is not an EDT M06/M12 touchscreen, then the model
916		 * detection is a bit hairy. The different ft5x06
917		 * firmwares around don't reliably implement the
918		 * identification registers. Well, we'll take a shot.
919		 *
920		 * The main difference between generic focaltec based
921		 * touches and EDT M09 is that we know how to retrieve
922		 * the max coordinates for the latter.
923		 */
924		tsdata->version = GENERIC_FT;
925
926		error = regmap_bulk_read(tsdata->regmap, 0xA6, rdbuf, 2);
927		if (error)
928			return error;
929
930		strscpy(fw_version, rdbuf, 2);
931
932		error = regmap_bulk_read(tsdata->regmap, 0xA8, rdbuf, 1);
933		if (error)
934			return error;
935
936		/* This "model identification" is not exact. Unfortunately
937		 * not all firmwares for the ft5x06 put useful values in
938		 * the identification registers.
939		 */
940		switch (rdbuf[0]) {
941		case 0x11:   /* EDT EP0110M09 */
942		case 0x35:   /* EDT EP0350M09 */
943		case 0x43:   /* EDT EP0430M09 */
944		case 0x50:   /* EDT EP0500M09 */
945		case 0x57:   /* EDT EP0570M09 */
946		case 0x70:   /* EDT EP0700M09 */
947			tsdata->version = EDT_M09;
948			snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
949				 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
950			break;
951		case 0xa1:   /* EDT EP1010ML00 */
952			tsdata->version = EDT_M09;
953			snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
954				 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
955			break;
956		case 0x5a:   /* Solomon Goldentek Display */
957			snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
958			break;
959		case 0x59:  /* Evervision Display with FT5xx6 TS */
960			tsdata->version = EV_FT;
961			error = regmap_bulk_read(tsdata->regmap, 0x53, rdbuf, 1);
962			if (error)
963				return error;
964			strscpy(fw_version, rdbuf, 1);
965			snprintf(model_name, EDT_NAME_LEN,
966				 "EVERVISION-FT5726NEi");
967			break;
968		default:
969			snprintf(model_name, EDT_NAME_LEN,
970				 "generic ft5x06 (%02x)",
971				 rdbuf[0]);
972			break;
973		}
974	}
975
976	return 0;
977}
978
979static void edt_ft5x06_ts_get_defaults(struct device *dev,
980				       struct edt_ft5x06_ts_data *tsdata)
981{
982	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
983	struct regmap *regmap = tsdata->regmap;
984	u32 val;
985	int error;
986
987	error = device_property_read_u32(dev, "threshold", &val);
988	if (!error) {
989		regmap_write(regmap, reg_addr->reg_threshold, val);
990		tsdata->threshold = val;
991	}
992
993	error = device_property_read_u32(dev, "gain", &val);
994	if (!error) {
995		regmap_write(regmap, reg_addr->reg_gain, val);
996		tsdata->gain = val;
997	}
998
999	error = device_property_read_u32(dev, "offset", &val);
1000	if (!error) {
1001		if (reg_addr->reg_offset != NO_REGISTER)
1002			regmap_write(regmap, reg_addr->reg_offset, val);
1003		tsdata->offset = val;
1004	}
1005
1006	error = device_property_read_u32(dev, "offset-x", &val);
1007	if (!error) {
1008		if (reg_addr->reg_offset_x != NO_REGISTER)
1009			regmap_write(regmap, reg_addr->reg_offset_x, val);
1010		tsdata->offset_x = val;
1011	}
1012
1013	error = device_property_read_u32(dev, "offset-y", &val);
1014	if (!error) {
1015		if (reg_addr->reg_offset_y != NO_REGISTER)
1016			regmap_write(regmap, reg_addr->reg_offset_y, val);
1017		tsdata->offset_y = val;
1018	}
1019}
1020
1021static void edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
1022{
1023	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
1024	struct regmap *regmap = tsdata->regmap;
1025	unsigned int val;
1026
1027	regmap_read(regmap, reg_addr->reg_threshold, &tsdata->threshold);
1028	regmap_read(regmap, reg_addr->reg_gain, &tsdata->gain);
1029	if (reg_addr->reg_offset != NO_REGISTER)
1030		regmap_read(regmap, reg_addr->reg_offset, &tsdata->offset);
1031	if (reg_addr->reg_offset_x != NO_REGISTER)
1032		regmap_read(regmap, reg_addr->reg_offset_x, &tsdata->offset_x);
1033	if (reg_addr->reg_offset_y != NO_REGISTER)
1034		regmap_read(regmap, reg_addr->reg_offset_y, &tsdata->offset_y);
1035	if (reg_addr->reg_report_rate != NO_REGISTER)
1036		regmap_read(regmap, reg_addr->reg_report_rate,
1037			    &tsdata->report_rate);
1038	tsdata->num_x = EDT_DEFAULT_NUM_X;
1039	if (reg_addr->reg_num_x != NO_REGISTER) {
1040		if (!regmap_read(regmap, reg_addr->reg_num_x, &val))
1041			tsdata->num_x = val;
1042	}
1043	tsdata->num_y = EDT_DEFAULT_NUM_Y;
1044	if (reg_addr->reg_num_y != NO_REGISTER) {
1045		if (!regmap_read(regmap, reg_addr->reg_num_y, &val))
1046			tsdata->num_y = val;
1047	}
1048}
1049
1050static void edt_ft5x06_ts_set_tdata_parameters(struct edt_ft5x06_ts_data *tsdata)
1051{
1052	int crclen;
1053
1054	if (tsdata->version == EDT_M06) {
1055		tsdata->tdata_cmd = 0xf9;
1056		tsdata->tdata_offset = 5;
1057		tsdata->point_len = 4;
1058		crclen = 1;
1059	} else {
1060		tsdata->tdata_cmd = 0x0;
1061		tsdata->tdata_offset = 3;
1062		tsdata->point_len = 6;
1063		crclen = 0;
1064	}
1065
1066	tsdata->tdata_len = tsdata->point_len * tsdata->max_support_points +
1067		tsdata->tdata_offset + crclen;
1068}
1069
1070static void edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
1071{
1072	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
1073
1074	switch (tsdata->version) {
1075	case EDT_M06:
1076		reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
1077		reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
1078		reg_addr->reg_gain = WORK_REGISTER_GAIN;
1079		reg_addr->reg_offset = WORK_REGISTER_OFFSET;
1080		reg_addr->reg_offset_x = NO_REGISTER;
1081		reg_addr->reg_offset_y = NO_REGISTER;
1082		reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1083		reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1084		break;
1085
1086	case EDT_M09:
1087	case EDT_M12:
1088		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1089		reg_addr->reg_report_rate = tsdata->version == EDT_M12 ?
1090			M12_REGISTER_REPORT_RATE : NO_REGISTER;
1091		reg_addr->reg_gain = M09_REGISTER_GAIN;
1092		reg_addr->reg_offset = M09_REGISTER_OFFSET;
1093		reg_addr->reg_offset_x = NO_REGISTER;
1094		reg_addr->reg_offset_y = NO_REGISTER;
1095		reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1096		reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1097		break;
1098
1099	case EV_FT:
1100		reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1101		reg_addr->reg_report_rate = NO_REGISTER;
1102		reg_addr->reg_gain = EV_REGISTER_GAIN;
1103		reg_addr->reg_offset = NO_REGISTER;
1104		reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1105		reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1106		reg_addr->reg_num_x = NO_REGISTER;
1107		reg_addr->reg_num_y = NO_REGISTER;
1108		break;
1109
1110	case GENERIC_FT:
1111		/* this is a guesswork */
1112		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1113		reg_addr->reg_report_rate = NO_REGISTER;
1114		reg_addr->reg_gain = M09_REGISTER_GAIN;
1115		reg_addr->reg_offset = M09_REGISTER_OFFSET;
1116		reg_addr->reg_offset_x = NO_REGISTER;
1117		reg_addr->reg_offset_y = NO_REGISTER;
1118		reg_addr->reg_num_x = NO_REGISTER;
1119		reg_addr->reg_num_y = NO_REGISTER;
1120		break;
1121	}
1122}
1123
1124static void edt_ft5x06_disable_regulators(void *arg)
1125{
1126	struct edt_ft5x06_ts_data *data = arg;
1127
1128	regulator_disable(data->vcc);
1129	regulator_disable(data->iovcc);
1130}
1131
1132static int edt_ft5x06_ts_probe(struct i2c_client *client)
1133{
1134	const struct i2c_device_id *id = i2c_client_get_device_id(client);
1135	const struct edt_i2c_chip_data *chip_data;
1136	struct edt_ft5x06_ts_data *tsdata;
1137	unsigned int val;
1138	struct input_dev *input;
1139	unsigned long irq_flags;
1140	int error;
1141	u32 report_rate;
1142
1143	dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1144
1145	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1146	if (!tsdata) {
1147		dev_err(&client->dev, "failed to allocate driver data.\n");
1148		return -ENOMEM;
1149	}
1150
1151	tsdata->regmap = regmap_init_i2c(client, &edt_ft5x06_i2c_regmap_config);
1152	if (IS_ERR(tsdata->regmap)) {
1153		dev_err(&client->dev, "regmap allocation failed\n");
1154		return PTR_ERR(tsdata->regmap);
1155	}
1156
1157	chip_data = device_get_match_data(&client->dev);
1158	if (!chip_data)
1159		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1160	if (!chip_data || !chip_data->max_support_points) {
1161		dev_err(&client->dev, "invalid or missing chip data\n");
1162		return -EINVAL;
1163	}
1164
1165	tsdata->max_support_points = chip_data->max_support_points;
1166
1167	tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
1168	if (IS_ERR(tsdata->vcc))
1169		return dev_err_probe(&client->dev, PTR_ERR(tsdata->vcc),
1170				     "failed to request regulator\n");
1171
1172	tsdata->iovcc = devm_regulator_get(&client->dev, "iovcc");
1173	if (IS_ERR(tsdata->iovcc)) {
1174		error = PTR_ERR(tsdata->iovcc);
1175		if (error != -EPROBE_DEFER)
1176			dev_err(&client->dev,
1177				"failed to request iovcc regulator: %d\n", error);
1178		return error;
1179	}
1180
1181	error = regulator_enable(tsdata->iovcc);
1182	if (error < 0) {
1183		dev_err(&client->dev, "failed to enable iovcc: %d\n", error);
1184		return error;
1185	}
1186
1187	/* Delay enabling VCC for > 10us (T_ivd) after IOVCC */
1188	usleep_range(10, 100);
1189
1190	error = regulator_enable(tsdata->vcc);
1191	if (error < 0) {
1192		dev_err(&client->dev, "failed to enable vcc: %d\n", error);
1193		regulator_disable(tsdata->iovcc);
1194		return error;
1195	}
1196
1197	error = devm_add_action_or_reset(&client->dev,
1198					 edt_ft5x06_disable_regulators,
1199					 tsdata);
1200	if (error)
1201		return error;
1202
1203	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1204						     "reset", GPIOD_OUT_HIGH);
1205	if (IS_ERR(tsdata->reset_gpio)) {
1206		error = PTR_ERR(tsdata->reset_gpio);
1207		dev_err(&client->dev,
1208			"Failed to request GPIO reset pin, error %d\n", error);
1209		return error;
1210	}
1211
1212	tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1213						    "wake", GPIOD_OUT_LOW);
1214	if (IS_ERR(tsdata->wake_gpio)) {
1215		error = PTR_ERR(tsdata->wake_gpio);
1216		dev_err(&client->dev,
1217			"Failed to request GPIO wake pin, error %d\n", error);
1218		return error;
1219	}
1220
1221	/*
1222	 * Check which sleep modes we can support. Power-off requieres the
1223	 * reset-pin to ensure correct power-down/power-up behaviour. Start with
1224	 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep
1225	 * mode.
1226	 */
1227	if (tsdata->reset_gpio)
1228		tsdata->suspend_mode = EDT_PMODE_POWEROFF;
1229	else if (tsdata->wake_gpio)
1230		tsdata->suspend_mode = EDT_PMODE_HIBERNATE;
1231	else
1232		tsdata->suspend_mode = EDT_PMODE_NOT_SUPPORTED;
1233
1234	if (tsdata->wake_gpio) {
1235		usleep_range(5000, 6000);
1236		gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1237		usleep_range(5000, 6000);
1238	}
1239
1240	if (tsdata->reset_gpio) {
1241		usleep_range(5000, 6000);
1242		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1243		msleep(300);
1244	}
1245
1246	input = devm_input_allocate_device(&client->dev);
1247	if (!input) {
1248		dev_err(&client->dev, "failed to allocate input device.\n");
1249		return -ENOMEM;
1250	}
1251
1252	mutex_init(&tsdata->mutex);
1253	tsdata->client = client;
1254	tsdata->input = input;
1255	tsdata->factory_mode = false;
1256	i2c_set_clientdata(client, tsdata);
1257
1258	error = edt_ft5x06_ts_identify(client, tsdata);
1259	if (error) {
1260		dev_err(&client->dev, "touchscreen probe failed\n");
1261		return error;
1262	}
1263
1264	/*
1265	 * Dummy read access. EP0700MLP1 returns bogus data on the first
1266	 * register read access and ignores writes.
1267	 */
1268	regmap_read(tsdata->regmap, 0x00, &val);
1269
1270	edt_ft5x06_ts_set_tdata_parameters(tsdata);
1271	edt_ft5x06_ts_set_regs(tsdata);
1272	edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1273	edt_ft5x06_ts_get_parameters(tsdata);
1274
1275	if (tsdata->reg_addr.reg_report_rate != NO_REGISTER &&
1276	    !device_property_read_u32(&client->dev,
1277				      "report-rate-hz", &report_rate)) {
1278		if (tsdata->version == EDT_M06)
1279			tsdata->report_rate = clamp_val(report_rate, 30, 140);
1280		else
1281			tsdata->report_rate = clamp_val(report_rate, 1, 255);
1282
1283		if (report_rate != tsdata->report_rate)
1284			dev_warn(&client->dev,
1285				 "report-rate %dHz is unsupported, use %dHz\n",
1286				 report_rate, tsdata->report_rate);
1287
1288		if (tsdata->version == EDT_M06)
1289			tsdata->report_rate /= 10;
1290
1291		regmap_write(tsdata->regmap, tsdata->reg_addr.reg_report_rate,
1292			     tsdata->report_rate);
1293	}
1294
1295	dev_dbg(&client->dev,
1296		"Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1297		tsdata->name, tsdata->fw_version, tsdata->num_x, tsdata->num_y);
1298
1299	input->name = tsdata->name;
1300	input->id.bustype = BUS_I2C;
1301	input->dev.parent = &client->dev;
1302
1303	input_set_abs_params(input, ABS_MT_POSITION_X,
1304			     0, tsdata->num_x * 64 - 1, 0, 0);
1305	input_set_abs_params(input, ABS_MT_POSITION_Y,
1306			     0, tsdata->num_y * 64 - 1, 0, 0);
1307
1308	touchscreen_parse_properties(input, true, &tsdata->prop);
1309
1310	error = input_mt_init_slots(input, tsdata->max_support_points,
1311				    INPUT_MT_DIRECT);
1312	if (error) {
1313		dev_err(&client->dev, "Unable to init MT slots.\n");
1314		return error;
1315	}
1316
1317	irq_flags = irq_get_trigger_type(client->irq);
1318	if (irq_flags == IRQF_TRIGGER_NONE)
1319		irq_flags = IRQF_TRIGGER_FALLING;
1320	irq_flags |= IRQF_ONESHOT;
1321
1322	error = devm_request_threaded_irq(&client->dev, client->irq,
1323					  NULL, edt_ft5x06_ts_isr, irq_flags,
1324					  client->name, tsdata);
1325	if (error) {
1326		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1327		return error;
1328	}
1329
1330	error = input_register_device(input);
1331	if (error)
1332		return error;
1333
1334	edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1335
1336	dev_dbg(&client->dev,
1337		"EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1338		client->irq,
1339		tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1340		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1341
1342	return 0;
1343}
1344
1345static void edt_ft5x06_ts_remove(struct i2c_client *client)
1346{
1347	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1348
1349	edt_ft5x06_ts_teardown_debugfs(tsdata);
1350	regmap_exit(tsdata->regmap);
1351}
1352
1353static int edt_ft5x06_ts_suspend(struct device *dev)
1354{
1355	struct i2c_client *client = to_i2c_client(dev);
1356	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1357	struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1358	int ret;
1359
1360	if (device_may_wakeup(dev))
1361		return 0;
1362
1363	if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1364		return 0;
1365
1366	/* Enter hibernate mode. */
1367	ret = regmap_write(tsdata->regmap, PMOD_REGISTER_OPMODE,
1368			   PMOD_REGISTER_HIBERNATE);
1369	if (ret)
1370		dev_warn(dev, "Failed to set hibernate mode\n");
1371
1372	if (tsdata->suspend_mode == EDT_PMODE_HIBERNATE)
1373		return 0;
1374
1375	/*
1376	 * Power-off according the datasheet. Cut the power may leaf the irq
1377	 * line in an undefined state depending on the host pull resistor
1378	 * settings. Disable the irq to avoid adjusting each host till the
1379	 * device is back in a full functional state.
1380	 */
1381	disable_irq(tsdata->client->irq);
1382
1383	gpiod_set_value_cansleep(reset_gpio, 1);
1384	usleep_range(1000, 2000);
1385
1386	ret = regulator_disable(tsdata->vcc);
1387	if (ret)
1388		dev_warn(dev, "Failed to disable vcc\n");
1389	ret = regulator_disable(tsdata->iovcc);
1390	if (ret)
1391		dev_warn(dev, "Failed to disable iovcc\n");
1392
1393	return 0;
1394}
1395
1396static int edt_ft5x06_ts_resume(struct device *dev)
1397{
1398	struct i2c_client *client = to_i2c_client(dev);
1399	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1400	int ret = 0;
1401
1402	if (device_may_wakeup(dev))
1403		return 0;
1404
1405	if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1406		return 0;
1407
1408	if (tsdata->suspend_mode == EDT_PMODE_POWEROFF) {
1409		struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1410
1411		/*
1412		 * We can't check if the regulator is a dummy or a real
1413		 * regulator. So we need to specify the 5ms reset time (T_rst)
1414		 * here instead of the 100us T_rtp time. We also need to wait
1415		 * 300ms in case it was a real supply and the power was cutted
1416		 * of. Toggle the reset pin is also a way to exit the hibernate
1417		 * mode.
1418		 */
1419		gpiod_set_value_cansleep(reset_gpio, 1);
1420		usleep_range(5000, 6000);
1421
1422		ret = regulator_enable(tsdata->iovcc);
1423		if (ret) {
1424			dev_err(dev, "Failed to enable iovcc\n");
1425			return ret;
1426		}
1427
1428		/* Delay enabling VCC for > 10us (T_ivd) after IOVCC */
1429		usleep_range(10, 100);
1430
1431		ret = regulator_enable(tsdata->vcc);
1432		if (ret) {
1433			dev_err(dev, "Failed to enable vcc\n");
1434			regulator_disable(tsdata->iovcc);
1435			return ret;
1436		}
1437
1438		usleep_range(1000, 2000);
1439		gpiod_set_value_cansleep(reset_gpio, 0);
1440		msleep(300);
1441
1442		edt_ft5x06_restore_reg_parameters(tsdata);
1443		enable_irq(tsdata->client->irq);
1444
1445		if (tsdata->factory_mode)
1446			ret = edt_ft5x06_factory_mode(tsdata);
1447	} else {
1448		struct gpio_desc *wake_gpio = tsdata->wake_gpio;
1449
1450		gpiod_set_value_cansleep(wake_gpio, 0);
1451		usleep_range(5000, 6000);
1452		gpiod_set_value_cansleep(wake_gpio, 1);
1453	}
1454
1455	return ret;
1456}
1457
1458static DEFINE_SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1459				edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1460
1461static const struct edt_i2c_chip_data edt_ft5x06_data = {
1462	.max_support_points = 5,
1463};
1464
1465static const struct edt_i2c_chip_data edt_ft5506_data = {
1466	.max_support_points = 10,
1467};
1468
1469static const struct edt_i2c_chip_data edt_ft6236_data = {
1470	.max_support_points = 2,
1471};
1472
1473static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1474	{ .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1475	{ .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1476	{ .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1477	/* Note no edt- prefix for compatibility with the ft6236.c driver */
1478	{ .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1479	{ /* sentinel */ }
1480};
1481MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1482
1483static const struct of_device_id edt_ft5x06_of_match[] = {
1484	{ .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1485	{ .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1486	{ .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1487	{ .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1488	{ .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1489	/* Note focaltech vendor prefix for compatibility with ft6236.c */
1490	{ .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1491	{ /* sentinel */ }
1492};
1493MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1494
1495static struct i2c_driver edt_ft5x06_ts_driver = {
1496	.driver = {
1497		.name = "edt_ft5x06",
1498		.dev_groups = edt_ft5x06_groups,
1499		.of_match_table = edt_ft5x06_of_match,
1500		.pm = pm_sleep_ptr(&edt_ft5x06_ts_pm_ops),
1501		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1502	},
1503	.id_table = edt_ft5x06_ts_id,
1504	.probe    = edt_ft5x06_ts_probe,
1505	.remove   = edt_ft5x06_ts_remove,
1506};
1507
1508module_i2c_driver(edt_ft5x06_ts_driver);
1509
1510MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1511MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1512MODULE_LICENSE("GPL v2");
1513