143c4d13eSSimon Budig /*
243c4d13eSSimon Budig  * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
3fd335ab0SLothar Waßmann  * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
4dac90dc2SLothar Waßmann  * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
543c4d13eSSimon Budig  *
643c4d13eSSimon Budig  * This software is licensed under the terms of the GNU General Public
743c4d13eSSimon Budig  * License version 2, as published by the Free Software Foundation, and
843c4d13eSSimon Budig  * may be copied, distributed, and modified under those terms.
943c4d13eSSimon Budig  *
1043c4d13eSSimon Budig  * This program is distributed in the hope that it will be useful,
1143c4d13eSSimon Budig  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1243c4d13eSSimon Budig  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1343c4d13eSSimon Budig  * GNU General Public License for more details.
1443c4d13eSSimon Budig  *
1543c4d13eSSimon Budig  * You should have received a copy of the GNU General Public
1643c4d13eSSimon Budig  * License along with this library; if not, write to the Free Software
1743c4d13eSSimon Budig  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1843c4d13eSSimon Budig  */
1943c4d13eSSimon Budig 
2043c4d13eSSimon Budig /*
2143c4d13eSSimon Budig  * This is a driver for the EDT "Polytouch" family of touch controllers
2243c4d13eSSimon Budig  * based on the FocalTech FT5x06 line of chips.
2343c4d13eSSimon Budig  *
2443c4d13eSSimon Budig  * Development of this driver has been sponsored by Glyn:
2543c4d13eSSimon Budig  *    http://www.glyn.com/Products/Displays
2643c4d13eSSimon Budig  */
2743c4d13eSSimon Budig 
2843c4d13eSSimon Budig #include <linux/module.h>
2943c4d13eSSimon Budig #include <linux/ratelimit.h>
30f0bef75cSDmitry Torokhov #include <linux/irq.h>
3143c4d13eSSimon Budig #include <linux/interrupt.h>
3243c4d13eSSimon Budig #include <linux/input.h>
3343c4d13eSSimon Budig #include <linux/i2c.h>
3443c4d13eSSimon Budig #include <linux/uaccess.h>
3543c4d13eSSimon Budig #include <linux/delay.h>
3643c4d13eSSimon Budig #include <linux/debugfs.h>
3743c4d13eSSimon Budig #include <linux/slab.h>
3813c23cd1SFranklin S Cooper Jr #include <linux/gpio/consumer.h>
3943c4d13eSSimon Budig #include <linux/input/mt.h>
402c005598SMaxime Ripard #include <linux/input/touchscreen.h>
41b1d2a3ecSFranklin S Cooper Jr #include <linux/of_device.h>
4243c4d13eSSimon Budig 
4343c4d13eSSimon Budig #define WORK_REGISTER_THRESHOLD		0x00
4443c4d13eSSimon Budig #define WORK_REGISTER_REPORT_RATE	0x08
4543c4d13eSSimon Budig #define WORK_REGISTER_GAIN		0x30
4643c4d13eSSimon Budig #define WORK_REGISTER_OFFSET		0x31
4743c4d13eSSimon Budig #define WORK_REGISTER_NUM_X		0x33
4843c4d13eSSimon Budig #define WORK_REGISTER_NUM_Y		0x34
4943c4d13eSSimon Budig 
50fd335ab0SLothar Waßmann #define M09_REGISTER_THRESHOLD		0x80
51fd335ab0SLothar Waßmann #define M09_REGISTER_GAIN		0x92
52fd335ab0SLothar Waßmann #define M09_REGISTER_OFFSET		0x93
53fd335ab0SLothar Waßmann #define M09_REGISTER_NUM_X		0x94
54fd335ab0SLothar Waßmann #define M09_REGISTER_NUM_Y		0x95
55fd335ab0SLothar Waßmann 
56fd335ab0SLothar Waßmann #define NO_REGISTER			0xff
57fd335ab0SLothar Waßmann 
5843c4d13eSSimon Budig #define WORK_REGISTER_OPMODE		0x3c
5943c4d13eSSimon Budig #define FACTORY_REGISTER_OPMODE		0x01
6043c4d13eSSimon Budig 
6143c4d13eSSimon Budig #define TOUCH_EVENT_DOWN		0x00
6243c4d13eSSimon Budig #define TOUCH_EVENT_UP			0x01
6343c4d13eSSimon Budig #define TOUCH_EVENT_ON			0x02
6443c4d13eSSimon Budig #define TOUCH_EVENT_RESERVED		0x03
6543c4d13eSSimon Budig 
6643c4d13eSSimon Budig #define EDT_NAME_LEN			23
6743c4d13eSSimon Budig #define EDT_SWITCH_MODE_RETRIES		10
6843c4d13eSSimon Budig #define EDT_SWITCH_MODE_DELAY		5 /* msec */
6943c4d13eSSimon Budig #define EDT_RAW_DATA_RETRIES		100
700eeecf60SAniroop Mathur #define EDT_RAW_DATA_DELAY		1000 /* usec */
7143c4d13eSSimon Budig 
72fd335ab0SLothar Waßmann enum edt_ver {
73169110c3SSimon Budig 	EDT_M06,
74169110c3SSimon Budig 	EDT_M09,
75aed5d0eeSSimon Budig 	EDT_M12,
76169110c3SSimon Budig 	GENERIC_FT,
77fd335ab0SLothar Waßmann };
78fd335ab0SLothar Waßmann 
79fd335ab0SLothar Waßmann struct edt_reg_addr {
80fd335ab0SLothar Waßmann 	int reg_threshold;
81fd335ab0SLothar Waßmann 	int reg_report_rate;
82fd335ab0SLothar Waßmann 	int reg_gain;
83fd335ab0SLothar Waßmann 	int reg_offset;
84fd335ab0SLothar Waßmann 	int reg_num_x;
85fd335ab0SLothar Waßmann 	int reg_num_y;
86fd335ab0SLothar Waßmann };
87fd335ab0SLothar Waßmann 
8843c4d13eSSimon Budig struct edt_ft5x06_ts_data {
8943c4d13eSSimon Budig 	struct i2c_client *client;
9043c4d13eSSimon Budig 	struct input_dev *input;
91ad368eb2SHans de Goede 	struct touchscreen_properties prop;
9243c4d13eSSimon Budig 	u16 num_x;
9343c4d13eSSimon Budig 	u16 num_y;
9443c4d13eSSimon Budig 
9513c23cd1SFranklin S Cooper Jr 	struct gpio_desc *reset_gpio;
9613c23cd1SFranklin S Cooper Jr 	struct gpio_desc *wake_gpio;
97dac90dc2SLothar Waßmann 
9843c4d13eSSimon Budig #if defined(CONFIG_DEBUG_FS)
9943c4d13eSSimon Budig 	struct dentry *debug_dir;
10043c4d13eSSimon Budig 	u8 *raw_buffer;
10143c4d13eSSimon Budig 	size_t raw_bufsize;
10243c4d13eSSimon Budig #endif
10343c4d13eSSimon Budig 
10443c4d13eSSimon Budig 	struct mutex mutex;
10543c4d13eSSimon Budig 	bool factory_mode;
10643c4d13eSSimon Budig 	int threshold;
10743c4d13eSSimon Budig 	int gain;
10843c4d13eSSimon Budig 	int offset;
10943c4d13eSSimon Budig 	int report_rate;
110b1d2a3ecSFranklin S Cooper Jr 	int max_support_points;
11143c4d13eSSimon Budig 
11243c4d13eSSimon Budig 	char name[EDT_NAME_LEN];
113fd335ab0SLothar Waßmann 
114fd335ab0SLothar Waßmann 	struct edt_reg_addr reg_addr;
115fd335ab0SLothar Waßmann 	enum edt_ver version;
11643c4d13eSSimon Budig };
11743c4d13eSSimon Budig 
118b1d2a3ecSFranklin S Cooper Jr struct edt_i2c_chip_data {
119b1d2a3ecSFranklin S Cooper Jr 	int  max_support_points;
120b1d2a3ecSFranklin S Cooper Jr };
121b1d2a3ecSFranklin S Cooper Jr 
12243c4d13eSSimon Budig static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
12343c4d13eSSimon Budig 				   u16 wr_len, u8 *wr_buf,
12443c4d13eSSimon Budig 				   u16 rd_len, u8 *rd_buf)
12543c4d13eSSimon Budig {
12643c4d13eSSimon Budig 	struct i2c_msg wrmsg[2];
12743c4d13eSSimon Budig 	int i = 0;
12843c4d13eSSimon Budig 	int ret;
12943c4d13eSSimon Budig 
13043c4d13eSSimon Budig 	if (wr_len) {
13143c4d13eSSimon Budig 		wrmsg[i].addr  = client->addr;
13243c4d13eSSimon Budig 		wrmsg[i].flags = 0;
13343c4d13eSSimon Budig 		wrmsg[i].len = wr_len;
13443c4d13eSSimon Budig 		wrmsg[i].buf = wr_buf;
13543c4d13eSSimon Budig 		i++;
13643c4d13eSSimon Budig 	}
13743c4d13eSSimon Budig 	if (rd_len) {
13843c4d13eSSimon Budig 		wrmsg[i].addr  = client->addr;
13943c4d13eSSimon Budig 		wrmsg[i].flags = I2C_M_RD;
14043c4d13eSSimon Budig 		wrmsg[i].len = rd_len;
14143c4d13eSSimon Budig 		wrmsg[i].buf = rd_buf;
14243c4d13eSSimon Budig 		i++;
14343c4d13eSSimon Budig 	}
14443c4d13eSSimon Budig 
14543c4d13eSSimon Budig 	ret = i2c_transfer(client->adapter, wrmsg, i);
14643c4d13eSSimon Budig 	if (ret < 0)
14743c4d13eSSimon Budig 		return ret;
14843c4d13eSSimon Budig 	if (ret != i)
14943c4d13eSSimon Budig 		return -EIO;
15043c4d13eSSimon Budig 
15143c4d13eSSimon Budig 	return 0;
15243c4d13eSSimon Budig }
15343c4d13eSSimon Budig 
15443c4d13eSSimon Budig static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
15543c4d13eSSimon Budig 				    u8 *buf, int buflen)
15643c4d13eSSimon Budig {
15743c4d13eSSimon Budig 	int i;
15843c4d13eSSimon Budig 	u8 crc = 0;
15943c4d13eSSimon Budig 
16043c4d13eSSimon Budig 	for (i = 0; i < buflen - 1; i++)
16143c4d13eSSimon Budig 		crc ^= buf[i];
16243c4d13eSSimon Budig 
16343c4d13eSSimon Budig 	if (crc != buf[buflen-1]) {
16443c4d13eSSimon Budig 		dev_err_ratelimited(&tsdata->client->dev,
16543c4d13eSSimon Budig 				    "crc error: 0x%02x expected, got 0x%02x\n",
16643c4d13eSSimon Budig 				    crc, buf[buflen-1]);
16743c4d13eSSimon Budig 		return false;
16843c4d13eSSimon Budig 	}
16943c4d13eSSimon Budig 
17043c4d13eSSimon Budig 	return true;
17143c4d13eSSimon Budig }
17243c4d13eSSimon Budig 
17343c4d13eSSimon Budig static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
17443c4d13eSSimon Budig {
17543c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = dev_id;
17643c4d13eSSimon Budig 	struct device *dev = &tsdata->client->dev;
177fd335ab0SLothar Waßmann 	u8 cmd;
1789378c025SFranklin S Cooper Jr 	u8 rdbuf[63];
17943c4d13eSSimon Budig 	int i, type, x, y, id;
180c789f1fbSFranklin S Cooper Jr 	int offset, tplen, datalen, crclen;
18143c4d13eSSimon Budig 	int error;
18243c4d13eSSimon Budig 
183fd335ab0SLothar Waßmann 	switch (tsdata->version) {
184169110c3SSimon Budig 	case EDT_M06:
185fd335ab0SLothar Waßmann 		cmd = 0xf9; /* tell the controller to send touch data */
186fd335ab0SLothar Waßmann 		offset = 5; /* where the actual touch data starts */
187fd335ab0SLothar Waßmann 		tplen = 4;  /* data comes in so called frames */
188c789f1fbSFranklin S Cooper Jr 		crclen = 1; /* length of the crc data */
189fd335ab0SLothar Waßmann 		break;
190fd335ab0SLothar Waßmann 
191169110c3SSimon Budig 	case EDT_M09:
192aed5d0eeSSimon Budig 	case EDT_M12:
193169110c3SSimon Budig 	case GENERIC_FT:
1949378c025SFranklin S Cooper Jr 		cmd = 0x0;
1959378c025SFranklin S Cooper Jr 		offset = 3;
196fd335ab0SLothar Waßmann 		tplen = 6;
197c789f1fbSFranklin S Cooper Jr 		crclen = 0;
198fd335ab0SLothar Waßmann 		break;
199fd335ab0SLothar Waßmann 
200fd335ab0SLothar Waßmann 	default:
201fd335ab0SLothar Waßmann 		goto out;
202fd335ab0SLothar Waßmann 	}
203fd335ab0SLothar Waßmann 
20443c4d13eSSimon Budig 	memset(rdbuf, 0, sizeof(rdbuf));
205b1d2a3ecSFranklin S Cooper Jr 	datalen = tplen * tsdata->max_support_points + offset + crclen;
20643c4d13eSSimon Budig 
20743c4d13eSSimon Budig 	error = edt_ft5x06_ts_readwrite(tsdata->client,
20843c4d13eSSimon Budig 					sizeof(cmd), &cmd,
209fd335ab0SLothar Waßmann 					datalen, rdbuf);
21043c4d13eSSimon Budig 	if (error) {
21143c4d13eSSimon Budig 		dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
21243c4d13eSSimon Budig 				    error);
21343c4d13eSSimon Budig 		goto out;
21443c4d13eSSimon Budig 	}
21543c4d13eSSimon Budig 
216aed5d0eeSSimon Budig 	/* M09/M12 does not send header or CRC */
217169110c3SSimon Budig 	if (tsdata->version == EDT_M06) {
218fd335ab0SLothar Waßmann 		if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
219fd335ab0SLothar Waßmann 			rdbuf[2] != datalen) {
220fd335ab0SLothar Waßmann 			dev_err_ratelimited(dev,
221fd335ab0SLothar Waßmann 					"Unexpected header: %02x%02x%02x!\n",
22243c4d13eSSimon Budig 					rdbuf[0], rdbuf[1], rdbuf[2]);
22343c4d13eSSimon Budig 			goto out;
22443c4d13eSSimon Budig 		}
22543c4d13eSSimon Budig 
226fd335ab0SLothar Waßmann 		if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
22743c4d13eSSimon Budig 			goto out;
228fd335ab0SLothar Waßmann 	}
22943c4d13eSSimon Budig 
230b1d2a3ecSFranklin S Cooper Jr 	for (i = 0; i < tsdata->max_support_points; i++) {
231fd335ab0SLothar Waßmann 		u8 *buf = &rdbuf[i * tplen + offset];
23243c4d13eSSimon Budig 		bool down;
23343c4d13eSSimon Budig 
23443c4d13eSSimon Budig 		type = buf[0] >> 6;
23543c4d13eSSimon Budig 		/* ignore Reserved events */
23643c4d13eSSimon Budig 		if (type == TOUCH_EVENT_RESERVED)
23743c4d13eSSimon Budig 			continue;
23843c4d13eSSimon Budig 
239fd335ab0SLothar Waßmann 		/* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
240169110c3SSimon Budig 		if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
241ee3e946eSLothar Waßmann 			continue;
242ee3e946eSLothar Waßmann 
24343c4d13eSSimon Budig 		x = ((buf[0] << 8) | buf[1]) & 0x0fff;
24443c4d13eSSimon Budig 		y = ((buf[2] << 8) | buf[3]) & 0x0fff;
24543c4d13eSSimon Budig 		id = (buf[2] >> 4) & 0x0f;
2461730d814SLothar Waßmann 		down = type != TOUCH_EVENT_UP;
24743c4d13eSSimon Budig 
24843c4d13eSSimon Budig 		input_mt_slot(tsdata->input, id);
24943c4d13eSSimon Budig 		input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
25043c4d13eSSimon Budig 
25143c4d13eSSimon Budig 		if (!down)
25243c4d13eSSimon Budig 			continue;
25343c4d13eSSimon Budig 
254ad368eb2SHans de Goede 		touchscreen_report_pos(tsdata->input, &tsdata->prop, x, y,
255ad368eb2SHans de Goede 				       true);
25643c4d13eSSimon Budig 	}
25743c4d13eSSimon Budig 
25843c4d13eSSimon Budig 	input_mt_report_pointer_emulation(tsdata->input, true);
25943c4d13eSSimon Budig 	input_sync(tsdata->input);
26043c4d13eSSimon Budig 
26143c4d13eSSimon Budig out:
26243c4d13eSSimon Budig 	return IRQ_HANDLED;
26343c4d13eSSimon Budig }
26443c4d13eSSimon Budig 
26543c4d13eSSimon Budig static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
26643c4d13eSSimon Budig 				     u8 addr, u8 value)
26743c4d13eSSimon Budig {
26843c4d13eSSimon Budig 	u8 wrbuf[4];
26943c4d13eSSimon Budig 
270fd335ab0SLothar Waßmann 	switch (tsdata->version) {
271169110c3SSimon Budig 	case EDT_M06:
27243c4d13eSSimon Budig 		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
27343c4d13eSSimon Budig 		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
27443c4d13eSSimon Budig 		wrbuf[2] = value;
27543c4d13eSSimon Budig 		wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
276fd335ab0SLothar Waßmann 		return edt_ft5x06_ts_readwrite(tsdata->client, 4,
277fd335ab0SLothar Waßmann 					wrbuf, 0, NULL);
278169110c3SSimon Budig 	case EDT_M09:
279aed5d0eeSSimon Budig 	case EDT_M12:
280169110c3SSimon Budig 	case GENERIC_FT:
281fd335ab0SLothar Waßmann 		wrbuf[0] = addr;
282fd335ab0SLothar Waßmann 		wrbuf[1] = value;
28343c4d13eSSimon Budig 
284cc071acaSRobert Woerle 		return edt_ft5x06_ts_readwrite(tsdata->client, 2,
285fd335ab0SLothar Waßmann 					wrbuf, 0, NULL);
286fd335ab0SLothar Waßmann 
287fd335ab0SLothar Waßmann 	default:
288fd335ab0SLothar Waßmann 		return -EINVAL;
289fd335ab0SLothar Waßmann 	}
29043c4d13eSSimon Budig }
29143c4d13eSSimon Budig 
29243c4d13eSSimon Budig static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
29343c4d13eSSimon Budig 				    u8 addr)
29443c4d13eSSimon Budig {
29543c4d13eSSimon Budig 	u8 wrbuf[2], rdbuf[2];
29643c4d13eSSimon Budig 	int error;
29743c4d13eSSimon Budig 
298fd335ab0SLothar Waßmann 	switch (tsdata->version) {
299169110c3SSimon Budig 	case EDT_M06:
30043c4d13eSSimon Budig 		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
30143c4d13eSSimon Budig 		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
30243c4d13eSSimon Budig 		wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
30343c4d13eSSimon Budig 
304e2c3ecf0SDan Carpenter 		error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
305e2c3ecf0SDan Carpenter 						rdbuf);
306e2c3ecf0SDan Carpenter 		if (error)
30743c4d13eSSimon Budig 			return error;
30843c4d13eSSimon Budig 
30943c4d13eSSimon Budig 		if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
31043c4d13eSSimon Budig 			dev_err(&tsdata->client->dev,
31143c4d13eSSimon Budig 				"crc error: 0x%02x expected, got 0x%02x\n",
312fd335ab0SLothar Waßmann 				wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
313fd335ab0SLothar Waßmann 				rdbuf[1]);
31443c4d13eSSimon Budig 			return -EIO;
31543c4d13eSSimon Budig 		}
316fd335ab0SLothar Waßmann 		break;
317fd335ab0SLothar Waßmann 
318169110c3SSimon Budig 	case EDT_M09:
319aed5d0eeSSimon Budig 	case EDT_M12:
320169110c3SSimon Budig 	case GENERIC_FT:
321fd335ab0SLothar Waßmann 		wrbuf[0] = addr;
322fd335ab0SLothar Waßmann 		error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
323fd335ab0SLothar Waßmann 						wrbuf, 1, rdbuf);
324fd335ab0SLothar Waßmann 		if (error)
325fd335ab0SLothar Waßmann 			return error;
326fd335ab0SLothar Waßmann 		break;
327fd335ab0SLothar Waßmann 
328fd335ab0SLothar Waßmann 	default:
329fd335ab0SLothar Waßmann 		return -EINVAL;
330fd335ab0SLothar Waßmann 	}
33143c4d13eSSimon Budig 
33243c4d13eSSimon Budig 	return rdbuf[0];
33343c4d13eSSimon Budig }
33443c4d13eSSimon Budig 
33543c4d13eSSimon Budig struct edt_ft5x06_attribute {
33643c4d13eSSimon Budig 	struct device_attribute dattr;
33743c4d13eSSimon Budig 	size_t field_offset;
33843c4d13eSSimon Budig 	u8 limit_low;
33943c4d13eSSimon Budig 	u8 limit_high;
340fd335ab0SLothar Waßmann 	u8 addr_m06;
341fd335ab0SLothar Waßmann 	u8 addr_m09;
34243c4d13eSSimon Budig };
34343c4d13eSSimon Budig 
344fd335ab0SLothar Waßmann #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09,			\
345fd335ab0SLothar Waßmann 		_limit_low, _limit_high)				\
34643c4d13eSSimon Budig 	struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = {	\
34743c4d13eSSimon Budig 		.dattr = __ATTR(_field, _mode,				\
34843c4d13eSSimon Budig 				edt_ft5x06_setting_show,		\
34943c4d13eSSimon Budig 				edt_ft5x06_setting_store),		\
350fd335ab0SLothar Waßmann 		.field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
351fd335ab0SLothar Waßmann 		.addr_m06 = _addr_m06,					\
352fd335ab0SLothar Waßmann 		.addr_m09 = _addr_m09,					\
35343c4d13eSSimon Budig 		.limit_low = _limit_low,				\
35443c4d13eSSimon Budig 		.limit_high = _limit_high,				\
35543c4d13eSSimon Budig 	}
35643c4d13eSSimon Budig 
35743c4d13eSSimon Budig static ssize_t edt_ft5x06_setting_show(struct device *dev,
35843c4d13eSSimon Budig 				       struct device_attribute *dattr,
35943c4d13eSSimon Budig 				       char *buf)
36043c4d13eSSimon Budig {
36143c4d13eSSimon Budig 	struct i2c_client *client = to_i2c_client(dev);
36243c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
36343c4d13eSSimon Budig 	struct edt_ft5x06_attribute *attr =
36443c4d13eSSimon Budig 			container_of(dattr, struct edt_ft5x06_attribute, dattr);
3651730d814SLothar Waßmann 	u8 *field = (u8 *)tsdata + attr->field_offset;
36643c4d13eSSimon Budig 	int val;
36743c4d13eSSimon Budig 	size_t count = 0;
36843c4d13eSSimon Budig 	int error = 0;
369fd335ab0SLothar Waßmann 	u8 addr;
37043c4d13eSSimon Budig 
37143c4d13eSSimon Budig 	mutex_lock(&tsdata->mutex);
37243c4d13eSSimon Budig 
37343c4d13eSSimon Budig 	if (tsdata->factory_mode) {
37443c4d13eSSimon Budig 		error = -EIO;
37543c4d13eSSimon Budig 		goto out;
37643c4d13eSSimon Budig 	}
37743c4d13eSSimon Budig 
378fd335ab0SLothar Waßmann 	switch (tsdata->version) {
379169110c3SSimon Budig 	case EDT_M06:
380fd335ab0SLothar Waßmann 		addr = attr->addr_m06;
381fd335ab0SLothar Waßmann 		break;
382fd335ab0SLothar Waßmann 
383169110c3SSimon Budig 	case EDT_M09:
384aed5d0eeSSimon Budig 	case EDT_M12:
385169110c3SSimon Budig 	case GENERIC_FT:
386fd335ab0SLothar Waßmann 		addr = attr->addr_m09;
387fd335ab0SLothar Waßmann 		break;
388fd335ab0SLothar Waßmann 
389fd335ab0SLothar Waßmann 	default:
390fd335ab0SLothar Waßmann 		error = -ENODEV;
391fd335ab0SLothar Waßmann 		goto out;
392fd335ab0SLothar Waßmann 	}
393fd335ab0SLothar Waßmann 
394fd335ab0SLothar Waßmann 	if (addr != NO_REGISTER) {
395fd335ab0SLothar Waßmann 		val = edt_ft5x06_register_read(tsdata, addr);
39643c4d13eSSimon Budig 		if (val < 0) {
39743c4d13eSSimon Budig 			error = val;
39843c4d13eSSimon Budig 			dev_err(&tsdata->client->dev,
39943c4d13eSSimon Budig 				"Failed to fetch attribute %s, error %d\n",
40043c4d13eSSimon Budig 				dattr->attr.name, error);
40143c4d13eSSimon Budig 			goto out;
40243c4d13eSSimon Budig 		}
403fd335ab0SLothar Waßmann 	} else {
404fd335ab0SLothar Waßmann 		val = *field;
405fd335ab0SLothar Waßmann 	}
40643c4d13eSSimon Budig 
40743c4d13eSSimon Budig 	if (val != *field) {
40843c4d13eSSimon Budig 		dev_warn(&tsdata->client->dev,
40943c4d13eSSimon Budig 			 "%s: read (%d) and stored value (%d) differ\n",
41043c4d13eSSimon Budig 			 dattr->attr.name, val, *field);
41143c4d13eSSimon Budig 		*field = val;
41243c4d13eSSimon Budig 	}
41343c4d13eSSimon Budig 
41443c4d13eSSimon Budig 	count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
41543c4d13eSSimon Budig out:
41643c4d13eSSimon Budig 	mutex_unlock(&tsdata->mutex);
41743c4d13eSSimon Budig 	return error ?: count;
41843c4d13eSSimon Budig }
41943c4d13eSSimon Budig 
42043c4d13eSSimon Budig static ssize_t edt_ft5x06_setting_store(struct device *dev,
42143c4d13eSSimon Budig 					struct device_attribute *dattr,
42243c4d13eSSimon Budig 					const char *buf, size_t count)
42343c4d13eSSimon Budig {
42443c4d13eSSimon Budig 	struct i2c_client *client = to_i2c_client(dev);
42543c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
42643c4d13eSSimon Budig 	struct edt_ft5x06_attribute *attr =
42743c4d13eSSimon Budig 			container_of(dattr, struct edt_ft5x06_attribute, dattr);
4281730d814SLothar Waßmann 	u8 *field = (u8 *)tsdata + attr->field_offset;
42943c4d13eSSimon Budig 	unsigned int val;
43043c4d13eSSimon Budig 	int error;
431fd335ab0SLothar Waßmann 	u8 addr;
43243c4d13eSSimon Budig 
43343c4d13eSSimon Budig 	mutex_lock(&tsdata->mutex);
43443c4d13eSSimon Budig 
43543c4d13eSSimon Budig 	if (tsdata->factory_mode) {
43643c4d13eSSimon Budig 		error = -EIO;
43743c4d13eSSimon Budig 		goto out;
43843c4d13eSSimon Budig 	}
43943c4d13eSSimon Budig 
44043c4d13eSSimon Budig 	error = kstrtouint(buf, 0, &val);
44143c4d13eSSimon Budig 	if (error)
44243c4d13eSSimon Budig 		goto out;
44343c4d13eSSimon Budig 
44443c4d13eSSimon Budig 	if (val < attr->limit_low || val > attr->limit_high) {
44543c4d13eSSimon Budig 		error = -ERANGE;
44643c4d13eSSimon Budig 		goto out;
44743c4d13eSSimon Budig 	}
44843c4d13eSSimon Budig 
449fd335ab0SLothar Waßmann 	switch (tsdata->version) {
450169110c3SSimon Budig 	case EDT_M06:
451fd335ab0SLothar Waßmann 		addr = attr->addr_m06;
452fd335ab0SLothar Waßmann 		break;
453fd335ab0SLothar Waßmann 
454169110c3SSimon Budig 	case EDT_M09:
455aed5d0eeSSimon Budig 	case EDT_M12:
456169110c3SSimon Budig 	case GENERIC_FT:
457fd335ab0SLothar Waßmann 		addr = attr->addr_m09;
458fd335ab0SLothar Waßmann 		break;
459fd335ab0SLothar Waßmann 
460fd335ab0SLothar Waßmann 	default:
461fd335ab0SLothar Waßmann 		error = -ENODEV;
462fd335ab0SLothar Waßmann 		goto out;
463fd335ab0SLothar Waßmann 	}
464fd335ab0SLothar Waßmann 
465fd335ab0SLothar Waßmann 	if (addr != NO_REGISTER) {
466fd335ab0SLothar Waßmann 		error = edt_ft5x06_register_write(tsdata, addr, val);
46743c4d13eSSimon Budig 		if (error) {
46843c4d13eSSimon Budig 			dev_err(&tsdata->client->dev,
46943c4d13eSSimon Budig 				"Failed to update attribute %s, error: %d\n",
47043c4d13eSSimon Budig 				dattr->attr.name, error);
47143c4d13eSSimon Budig 			goto out;
47243c4d13eSSimon Budig 		}
473fd335ab0SLothar Waßmann 	}
47443c4d13eSSimon Budig 	*field = val;
47543c4d13eSSimon Budig 
47643c4d13eSSimon Budig out:
47743c4d13eSSimon Budig 	mutex_unlock(&tsdata->mutex);
47843c4d13eSSimon Budig 	return error ?: count;
47943c4d13eSSimon Budig }
48043c4d13eSSimon Budig 
481aed5d0eeSSimon Budig /* m06, m09: range 0-31, m12: range 0-5 */
482fd335ab0SLothar Waßmann static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
483fd335ab0SLothar Waßmann 		M09_REGISTER_GAIN, 0, 31);
484aed5d0eeSSimon Budig /* m06, m09: range 0-31, m12: range 0-16 */
485fd335ab0SLothar Waßmann static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
486fd335ab0SLothar Waßmann 		M09_REGISTER_OFFSET, 0, 31);
487aed5d0eeSSimon Budig /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
488fd335ab0SLothar Waßmann static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
489aed5d0eeSSimon Budig 		M09_REGISTER_THRESHOLD, 0, 255);
490aed5d0eeSSimon Budig /* m06: range 3 to 14, m12: (0x64: 100Hz) */
491fd335ab0SLothar Waßmann static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
492aed5d0eeSSimon Budig 		NO_REGISTER, 0, 255);
49343c4d13eSSimon Budig 
49443c4d13eSSimon Budig static struct attribute *edt_ft5x06_attrs[] = {
49543c4d13eSSimon Budig 	&edt_ft5x06_attr_gain.dattr.attr,
49643c4d13eSSimon Budig 	&edt_ft5x06_attr_offset.dattr.attr,
49743c4d13eSSimon Budig 	&edt_ft5x06_attr_threshold.dattr.attr,
49843c4d13eSSimon Budig 	&edt_ft5x06_attr_report_rate.dattr.attr,
49943c4d13eSSimon Budig 	NULL
50043c4d13eSSimon Budig };
50143c4d13eSSimon Budig 
50243c4d13eSSimon Budig static const struct attribute_group edt_ft5x06_attr_group = {
50343c4d13eSSimon Budig 	.attrs = edt_ft5x06_attrs,
50443c4d13eSSimon Budig };
50543c4d13eSSimon Budig 
50643c4d13eSSimon Budig #ifdef CONFIG_DEBUG_FS
50743c4d13eSSimon Budig static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
50843c4d13eSSimon Budig {
50943c4d13eSSimon Budig 	struct i2c_client *client = tsdata->client;
51043c4d13eSSimon Budig 	int retries = EDT_SWITCH_MODE_RETRIES;
51143c4d13eSSimon Budig 	int ret;
51243c4d13eSSimon Budig 	int error;
51343c4d13eSSimon Budig 
5144b3e910dSDmitry Torokhov 	if (tsdata->version != EDT_M06) {
5154b3e910dSDmitry Torokhov 		dev_err(&client->dev,
5164b3e910dSDmitry Torokhov 			"No factory mode support for non-M06 devices\n");
5174b3e910dSDmitry Torokhov 		return -EINVAL;
5184b3e910dSDmitry Torokhov 	}
5194b3e910dSDmitry Torokhov 
52043c4d13eSSimon Budig 	disable_irq(client->irq);
52143c4d13eSSimon Budig 
52243c4d13eSSimon Budig 	if (!tsdata->raw_buffer) {
52343c4d13eSSimon Budig 		tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
52443c4d13eSSimon Budig 				      sizeof(u16);
52543c4d13eSSimon Budig 		tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
52643c4d13eSSimon Budig 		if (!tsdata->raw_buffer) {
52743c4d13eSSimon Budig 			error = -ENOMEM;
52843c4d13eSSimon Budig 			goto err_out;
52943c4d13eSSimon Budig 		}
53043c4d13eSSimon Budig 	}
53143c4d13eSSimon Budig 
53243c4d13eSSimon Budig 	/* mode register is 0x3c when in the work mode */
53343c4d13eSSimon Budig 	error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
53443c4d13eSSimon Budig 	if (error) {
53543c4d13eSSimon Budig 		dev_err(&client->dev,
53643c4d13eSSimon Budig 			"failed to switch to factory mode, error %d\n", error);
53743c4d13eSSimon Budig 		goto err_out;
53843c4d13eSSimon Budig 	}
53943c4d13eSSimon Budig 
54043c4d13eSSimon Budig 	tsdata->factory_mode = true;
54143c4d13eSSimon Budig 	do {
54243c4d13eSSimon Budig 		mdelay(EDT_SWITCH_MODE_DELAY);
54343c4d13eSSimon Budig 		/* mode register is 0x01 when in factory mode */
54443c4d13eSSimon Budig 		ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
54543c4d13eSSimon Budig 		if (ret == 0x03)
54643c4d13eSSimon Budig 			break;
54743c4d13eSSimon Budig 	} while (--retries > 0);
54843c4d13eSSimon Budig 
54943c4d13eSSimon Budig 	if (retries == 0) {
55043c4d13eSSimon Budig 		dev_err(&client->dev, "not in factory mode after %dms.\n",
55143c4d13eSSimon Budig 			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
55243c4d13eSSimon Budig 		error = -EIO;
55343c4d13eSSimon Budig 		goto err_out;
55443c4d13eSSimon Budig 	}
55543c4d13eSSimon Budig 
55643c4d13eSSimon Budig 	return 0;
55743c4d13eSSimon Budig 
55843c4d13eSSimon Budig err_out:
55943c4d13eSSimon Budig 	kfree(tsdata->raw_buffer);
56043c4d13eSSimon Budig 	tsdata->raw_buffer = NULL;
56143c4d13eSSimon Budig 	tsdata->factory_mode = false;
56243c4d13eSSimon Budig 	enable_irq(client->irq);
56343c4d13eSSimon Budig 
56443c4d13eSSimon Budig 	return error;
56543c4d13eSSimon Budig }
56643c4d13eSSimon Budig 
56743c4d13eSSimon Budig static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
56843c4d13eSSimon Budig {
56943c4d13eSSimon Budig 	struct i2c_client *client = tsdata->client;
57043c4d13eSSimon Budig 	int retries = EDT_SWITCH_MODE_RETRIES;
571fd335ab0SLothar Waßmann 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
57243c4d13eSSimon Budig 	int ret;
57343c4d13eSSimon Budig 	int error;
57443c4d13eSSimon Budig 
57543c4d13eSSimon Budig 	/* mode register is 0x01 when in the factory mode */
57643c4d13eSSimon Budig 	error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
57743c4d13eSSimon Budig 	if (error) {
57843c4d13eSSimon Budig 		dev_err(&client->dev,
57943c4d13eSSimon Budig 			"failed to switch to work mode, error: %d\n", error);
58043c4d13eSSimon Budig 		return error;
58143c4d13eSSimon Budig 	}
58243c4d13eSSimon Budig 
58343c4d13eSSimon Budig 	tsdata->factory_mode = false;
58443c4d13eSSimon Budig 
58543c4d13eSSimon Budig 	do {
58643c4d13eSSimon Budig 		mdelay(EDT_SWITCH_MODE_DELAY);
58743c4d13eSSimon Budig 		/* mode register is 0x01 when in factory mode */
58843c4d13eSSimon Budig 		ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
58943c4d13eSSimon Budig 		if (ret == 0x01)
59043c4d13eSSimon Budig 			break;
59143c4d13eSSimon Budig 	} while (--retries > 0);
59243c4d13eSSimon Budig 
59343c4d13eSSimon Budig 	if (retries == 0) {
59443c4d13eSSimon Budig 		dev_err(&client->dev, "not in work mode after %dms.\n",
59543c4d13eSSimon Budig 			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
59643c4d13eSSimon Budig 		tsdata->factory_mode = true;
59743c4d13eSSimon Budig 		return -EIO;
59843c4d13eSSimon Budig 	}
59943c4d13eSSimon Budig 
60043c4d13eSSimon Budig 	kfree(tsdata->raw_buffer);
60143c4d13eSSimon Budig 	tsdata->raw_buffer = NULL;
60243c4d13eSSimon Budig 
60343c4d13eSSimon Budig 	/* restore parameters */
604fd335ab0SLothar Waßmann 	edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
60543c4d13eSSimon Budig 				  tsdata->threshold);
606fd335ab0SLothar Waßmann 	edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
60743c4d13eSSimon Budig 				  tsdata->gain);
608fd335ab0SLothar Waßmann 	edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
60943c4d13eSSimon Budig 				  tsdata->offset);
61047014752SLuca Ceresoli 	if (reg_addr->reg_report_rate != NO_REGISTER)
611fd335ab0SLothar Waßmann 		edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
61243c4d13eSSimon Budig 				  tsdata->report_rate);
61343c4d13eSSimon Budig 
61443c4d13eSSimon Budig 	enable_irq(client->irq);
61543c4d13eSSimon Budig 
61643c4d13eSSimon Budig 	return 0;
61743c4d13eSSimon Budig }
61843c4d13eSSimon Budig 
61943c4d13eSSimon Budig static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
62043c4d13eSSimon Budig {
62143c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = data;
62243c4d13eSSimon Budig 
62343c4d13eSSimon Budig 	*mode = tsdata->factory_mode;
62443c4d13eSSimon Budig 
62543c4d13eSSimon Budig 	return 0;
62643c4d13eSSimon Budig };
62743c4d13eSSimon Budig 
62843c4d13eSSimon Budig static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
62943c4d13eSSimon Budig {
63043c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = data;
63143c4d13eSSimon Budig 	int retval = 0;
63243c4d13eSSimon Budig 
63343c4d13eSSimon Budig 	if (mode > 1)
63443c4d13eSSimon Budig 		return -ERANGE;
63543c4d13eSSimon Budig 
63643c4d13eSSimon Budig 	mutex_lock(&tsdata->mutex);
63743c4d13eSSimon Budig 
63843c4d13eSSimon Budig 	if (mode != tsdata->factory_mode) {
63943c4d13eSSimon Budig 		retval = mode ? edt_ft5x06_factory_mode(tsdata) :
64043c4d13eSSimon Budig 				edt_ft5x06_work_mode(tsdata);
64143c4d13eSSimon Budig 	}
64243c4d13eSSimon Budig 
64343c4d13eSSimon Budig 	mutex_unlock(&tsdata->mutex);
64443c4d13eSSimon Budig 
64543c4d13eSSimon Budig 	return retval;
64643c4d13eSSimon Budig };
64743c4d13eSSimon Budig 
64843c4d13eSSimon Budig DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
64943c4d13eSSimon Budig 			edt_ft5x06_debugfs_mode_set, "%llu\n");
65043c4d13eSSimon Budig 
65143c4d13eSSimon Budig static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
65243c4d13eSSimon Budig 				char __user *buf, size_t count, loff_t *off)
65343c4d13eSSimon Budig {
65443c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = file->private_data;
65543c4d13eSSimon Budig 	struct i2c_client *client = tsdata->client;
65643c4d13eSSimon Budig 	int retries  = EDT_RAW_DATA_RETRIES;
65743c4d13eSSimon Budig 	int val, i, error;
65843c4d13eSSimon Budig 	size_t read = 0;
65943c4d13eSSimon Budig 	int colbytes;
66043c4d13eSSimon Budig 	char wrbuf[3];
66143c4d13eSSimon Budig 	u8 *rdbuf;
66243c4d13eSSimon Budig 
66343c4d13eSSimon Budig 	if (*off < 0 || *off >= tsdata->raw_bufsize)
66443c4d13eSSimon Budig 		return 0;
66543c4d13eSSimon Budig 
66643c4d13eSSimon Budig 	mutex_lock(&tsdata->mutex);
66743c4d13eSSimon Budig 
66843c4d13eSSimon Budig 	if (!tsdata->factory_mode || !tsdata->raw_buffer) {
66943c4d13eSSimon Budig 		error = -EIO;
67043c4d13eSSimon Budig 		goto out;
67143c4d13eSSimon Budig 	}
67243c4d13eSSimon Budig 
67343c4d13eSSimon Budig 	error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
67443c4d13eSSimon Budig 	if (error) {
67543c4d13eSSimon Budig 		dev_dbg(&client->dev,
67643c4d13eSSimon Budig 			"failed to write 0x08 register, error %d\n", error);
67743c4d13eSSimon Budig 		goto out;
67843c4d13eSSimon Budig 	}
67943c4d13eSSimon Budig 
68043c4d13eSSimon Budig 	do {
6810eeecf60SAniroop Mathur 		usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
68243c4d13eSSimon Budig 		val = edt_ft5x06_register_read(tsdata, 0x08);
68343c4d13eSSimon Budig 		if (val < 1)
68443c4d13eSSimon Budig 			break;
68543c4d13eSSimon Budig 	} while (--retries > 0);
68643c4d13eSSimon Budig 
68743c4d13eSSimon Budig 	if (val < 0) {
68843c4d13eSSimon Budig 		error = val;
68943c4d13eSSimon Budig 		dev_dbg(&client->dev,
69043c4d13eSSimon Budig 			"failed to read 0x08 register, error %d\n", error);
69143c4d13eSSimon Budig 		goto out;
69243c4d13eSSimon Budig 	}
69343c4d13eSSimon Budig 
69443c4d13eSSimon Budig 	if (retries == 0) {
69543c4d13eSSimon Budig 		dev_dbg(&client->dev,
69643c4d13eSSimon Budig 			"timed out waiting for register to settle\n");
69743c4d13eSSimon Budig 		error = -ETIMEDOUT;
69843c4d13eSSimon Budig 		goto out;
69943c4d13eSSimon Budig 	}
70043c4d13eSSimon Budig 
70143c4d13eSSimon Budig 	rdbuf = tsdata->raw_buffer;
70243c4d13eSSimon Budig 	colbytes = tsdata->num_y * sizeof(u16);
70343c4d13eSSimon Budig 
70443c4d13eSSimon Budig 	wrbuf[0] = 0xf5;
70543c4d13eSSimon Budig 	wrbuf[1] = 0x0e;
70643c4d13eSSimon Budig 	for (i = 0; i < tsdata->num_x; i++) {
70743c4d13eSSimon Budig 		wrbuf[2] = i;  /* column index */
70843c4d13eSSimon Budig 		error = edt_ft5x06_ts_readwrite(tsdata->client,
70943c4d13eSSimon Budig 						sizeof(wrbuf), wrbuf,
71043c4d13eSSimon Budig 						colbytes, rdbuf);
71143c4d13eSSimon Budig 		if (error)
71243c4d13eSSimon Budig 			goto out;
71343c4d13eSSimon Budig 
71443c4d13eSSimon Budig 		rdbuf += colbytes;
71543c4d13eSSimon Budig 	}
71643c4d13eSSimon Budig 
71743c4d13eSSimon Budig 	read = min_t(size_t, count, tsdata->raw_bufsize - *off);
71835b1da4eSAxel Lin 	if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
71935b1da4eSAxel Lin 		error = -EFAULT;
72035b1da4eSAxel Lin 		goto out;
72135b1da4eSAxel Lin 	}
72235b1da4eSAxel Lin 
72343c4d13eSSimon Budig 	*off += read;
72443c4d13eSSimon Budig out:
72543c4d13eSSimon Budig 	mutex_unlock(&tsdata->mutex);
72643c4d13eSSimon Budig 	return error ?: read;
72743c4d13eSSimon Budig };
72843c4d13eSSimon Budig 
72943c4d13eSSimon Budig static const struct file_operations debugfs_raw_data_fops = {
730f6c0df6aSWei Yongjun 	.open = simple_open,
73143c4d13eSSimon Budig 	.read = edt_ft5x06_debugfs_raw_data_read,
73243c4d13eSSimon Budig };
73343c4d13eSSimon Budig 
7345298cc4cSBill Pemberton static void
73543c4d13eSSimon Budig edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
73643c4d13eSSimon Budig 			      const char *debugfs_name)
73743c4d13eSSimon Budig {
73843c4d13eSSimon Budig 	tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
73943c4d13eSSimon Budig 	if (!tsdata->debug_dir)
74043c4d13eSSimon Budig 		return;
74143c4d13eSSimon Budig 
74243c4d13eSSimon Budig 	debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
74343c4d13eSSimon Budig 	debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
74443c4d13eSSimon Budig 
74543c4d13eSSimon Budig 	debugfs_create_file("mode", S_IRUSR | S_IWUSR,
74643c4d13eSSimon Budig 			    tsdata->debug_dir, tsdata, &debugfs_mode_fops);
74743c4d13eSSimon Budig 	debugfs_create_file("raw_data", S_IRUSR,
74843c4d13eSSimon Budig 			    tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
74943c4d13eSSimon Budig }
75043c4d13eSSimon Budig 
751e2619cf7SBill Pemberton static void
75243c4d13eSSimon Budig edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
75343c4d13eSSimon Budig {
75443c4d13eSSimon Budig 	debugfs_remove_recursive(tsdata->debug_dir);
755a1d0fa77SGuenter Roeck 	kfree(tsdata->raw_buffer);
75643c4d13eSSimon Budig }
75743c4d13eSSimon Budig 
75843c4d13eSSimon Budig #else
75943c4d13eSSimon Budig 
76043c4d13eSSimon Budig static inline void
76143c4d13eSSimon Budig edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
76243c4d13eSSimon Budig 			      const char *debugfs_name)
76343c4d13eSSimon Budig {
76443c4d13eSSimon Budig }
76543c4d13eSSimon Budig 
76643c4d13eSSimon Budig static inline void
76743c4d13eSSimon Budig edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
76843c4d13eSSimon Budig {
76943c4d13eSSimon Budig }
77043c4d13eSSimon Budig 
77143c4d13eSSimon Budig #endif /* CONFIG_DEBUGFS */
77243c4d13eSSimon Budig 
7735298cc4cSBill Pemberton static int edt_ft5x06_ts_identify(struct i2c_client *client,
774fd335ab0SLothar Waßmann 					struct edt_ft5x06_ts_data *tsdata,
77543c4d13eSSimon Budig 					char *fw_version)
77643c4d13eSSimon Budig {
77743c4d13eSSimon Budig 	u8 rdbuf[EDT_NAME_LEN];
77843c4d13eSSimon Budig 	char *p;
77943c4d13eSSimon Budig 	int error;
780fd335ab0SLothar Waßmann 	char *model_name = tsdata->name;
78143c4d13eSSimon Budig 
782fd335ab0SLothar Waßmann 	/* see what we find if we assume it is a M06 *
783fd335ab0SLothar Waßmann 	 * if we get less than EDT_NAME_LEN, we don't want
784fd335ab0SLothar Waßmann 	 * to have garbage in there
785fd335ab0SLothar Waßmann 	 */
786fd335ab0SLothar Waßmann 	memset(rdbuf, 0, sizeof(rdbuf));
787aed5d0eeSSimon Budig 	error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
78843c4d13eSSimon Budig 					EDT_NAME_LEN - 1, rdbuf);
78943c4d13eSSimon Budig 	if (error)
79043c4d13eSSimon Budig 		return error;
79143c4d13eSSimon Budig 
792aed5d0eeSSimon Budig 	/* Probe content for something consistent.
793aed5d0eeSSimon Budig 	 * M06 starts with a response byte, M12 gives the data directly.
794aed5d0eeSSimon Budig 	 * M09/Generic does not provide model number information.
795fd335ab0SLothar Waßmann 	 */
796aed5d0eeSSimon Budig 	if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
797169110c3SSimon Budig 		tsdata->version = EDT_M06;
798fd335ab0SLothar Waßmann 
79943c4d13eSSimon Budig 		/* remove last '$' end marker */
80043c4d13eSSimon Budig 		rdbuf[EDT_NAME_LEN - 1] = '\0';
80143c4d13eSSimon Budig 		if (rdbuf[EDT_NAME_LEN - 2] == '$')
80243c4d13eSSimon Budig 			rdbuf[EDT_NAME_LEN - 2] = '\0';
80343c4d13eSSimon Budig 
80443c4d13eSSimon Budig 		/* look for Model/Version separator */
80543c4d13eSSimon Budig 		p = strchr(rdbuf, '*');
80643c4d13eSSimon Budig 		if (p)
80743c4d13eSSimon Budig 			*p++ = '\0';
80843c4d13eSSimon Budig 		strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
80943c4d13eSSimon Budig 		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
810aed5d0eeSSimon Budig 	} else if (!strncasecmp(rdbuf, "EP0", 3)) {
811aed5d0eeSSimon Budig 		tsdata->version = EDT_M12;
812aed5d0eeSSimon Budig 
813aed5d0eeSSimon Budig 		/* remove last '$' end marker */
814aed5d0eeSSimon Budig 		rdbuf[EDT_NAME_LEN - 2] = '\0';
815aed5d0eeSSimon Budig 		if (rdbuf[EDT_NAME_LEN - 3] == '$')
816aed5d0eeSSimon Budig 			rdbuf[EDT_NAME_LEN - 3] = '\0';
817aed5d0eeSSimon Budig 
818aed5d0eeSSimon Budig 		/* look for Model/Version separator */
819aed5d0eeSSimon Budig 		p = strchr(rdbuf, '*');
820aed5d0eeSSimon Budig 		if (p)
821aed5d0eeSSimon Budig 			*p++ = '\0';
822aed5d0eeSSimon Budig 		strlcpy(model_name, rdbuf, EDT_NAME_LEN);
823aed5d0eeSSimon Budig 		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
824fd335ab0SLothar Waßmann 	} else {
825aed5d0eeSSimon Budig 		/* If it is not an EDT M06/M12 touchscreen, then the model
826169110c3SSimon Budig 		 * detection is a bit hairy. The different ft5x06
827169110c3SSimon Budig 		 * firmares around don't reliably implement the
828169110c3SSimon Budig 		 * identification registers. Well, we'll take a shot.
829169110c3SSimon Budig 		 *
830169110c3SSimon Budig 		 * The main difference between generic focaltec based
831169110c3SSimon Budig 		 * touches and EDT M09 is that we know how to retrieve
832169110c3SSimon Budig 		 * the max coordinates for the latter.
833169110c3SSimon Budig 		 */
834169110c3SSimon Budig 		tsdata->version = GENERIC_FT;
835fd335ab0SLothar Waßmann 
836fd335ab0SLothar Waßmann 		error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
837fd335ab0SLothar Waßmann 						2, rdbuf);
838fd335ab0SLothar Waßmann 		if (error)
839fd335ab0SLothar Waßmann 			return error;
840fd335ab0SLothar Waßmann 
841fd335ab0SLothar Waßmann 		strlcpy(fw_version, rdbuf, 2);
842fd335ab0SLothar Waßmann 
843fd335ab0SLothar Waßmann 		error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
844fd335ab0SLothar Waßmann 						1, rdbuf);
845fd335ab0SLothar Waßmann 		if (error)
846fd335ab0SLothar Waßmann 			return error;
847fd335ab0SLothar Waßmann 
848169110c3SSimon Budig 		/* This "model identification" is not exact. Unfortunately
849169110c3SSimon Budig 		 * not all firmwares for the ft5x06 put useful values in
850169110c3SSimon Budig 		 * the identification registers.
851169110c3SSimon Budig 		 */
852169110c3SSimon Budig 		switch (rdbuf[0]) {
853169110c3SSimon Budig 		case 0x35:   /* EDT EP0350M09 */
854169110c3SSimon Budig 		case 0x43:   /* EDT EP0430M09 */
855169110c3SSimon Budig 		case 0x50:   /* EDT EP0500M09 */
856169110c3SSimon Budig 		case 0x57:   /* EDT EP0570M09 */
857169110c3SSimon Budig 		case 0x70:   /* EDT EP0700M09 */
858169110c3SSimon Budig 			tsdata->version = EDT_M09;
859fd335ab0SLothar Waßmann 			snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
860fd335ab0SLothar Waßmann 				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
861169110c3SSimon Budig 			break;
862169110c3SSimon Budig 		case 0xa1:   /* EDT EP1010ML00 */
863169110c3SSimon Budig 			tsdata->version = EDT_M09;
864169110c3SSimon Budig 			snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
865169110c3SSimon Budig 				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
866169110c3SSimon Budig 			break;
867169110c3SSimon Budig 		case 0x5a:   /* Solomon Goldentek Display */
868169110c3SSimon Budig 			snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
869169110c3SSimon Budig 			break;
870169110c3SSimon Budig 		default:
871169110c3SSimon Budig 			snprintf(model_name, EDT_NAME_LEN,
872169110c3SSimon Budig 				 "generic ft5x06 (%02x)",
873169110c3SSimon Budig 				 rdbuf[0]);
874169110c3SSimon Budig 			break;
875169110c3SSimon Budig 		}
876fd335ab0SLothar Waßmann 	}
87743c4d13eSSimon Budig 
87843c4d13eSSimon Budig 	return 0;
87943c4d13eSSimon Budig }
88043c4d13eSSimon Budig 
8812e23b7a9SDmitry Torokhov static void edt_ft5x06_ts_get_defaults(struct device *dev,
882dac90dc2SLothar Waßmann 				       struct edt_ft5x06_ts_data *tsdata)
883dac90dc2SLothar Waßmann {
884fd335ab0SLothar Waßmann 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
8852e23b7a9SDmitry Torokhov 	u32 val;
8862e23b7a9SDmitry Torokhov 	int error;
887fd335ab0SLothar Waßmann 
8882e23b7a9SDmitry Torokhov 	error = device_property_read_u32(dev, "threshold", &val);
889dc262dfaSPhilipp Zabel 	if (!error) {
890dc262dfaSPhilipp Zabel 		edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
891dc262dfaSPhilipp Zabel 		tsdata->threshold = val;
892dc262dfaSPhilipp Zabel 	}
8932e23b7a9SDmitry Torokhov 
8942e23b7a9SDmitry Torokhov 	error = device_property_read_u32(dev, "gain", &val);
895dc262dfaSPhilipp Zabel 	if (!error) {
896dc262dfaSPhilipp Zabel 		edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
897dc262dfaSPhilipp Zabel 		tsdata->gain = val;
898dc262dfaSPhilipp Zabel 	}
8992e23b7a9SDmitry Torokhov 
9002e23b7a9SDmitry Torokhov 	error = device_property_read_u32(dev, "offset", &val);
901dc262dfaSPhilipp Zabel 	if (!error) {
902dc262dfaSPhilipp Zabel 		edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
903dc262dfaSPhilipp Zabel 		tsdata->offset = val;
904dc262dfaSPhilipp Zabel 	}
905dac90dc2SLothar Waßmann }
906dac90dc2SLothar Waßmann 
9075298cc4cSBill Pemberton static void
90843c4d13eSSimon Budig edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
90943c4d13eSSimon Budig {
910fd335ab0SLothar Waßmann 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
911fd335ab0SLothar Waßmann 
91243c4d13eSSimon Budig 	tsdata->threshold = edt_ft5x06_register_read(tsdata,
913fd335ab0SLothar Waßmann 						     reg_addr->reg_threshold);
914fd335ab0SLothar Waßmann 	tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
915fd335ab0SLothar Waßmann 	tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
916fd335ab0SLothar Waßmann 	if (reg_addr->reg_report_rate != NO_REGISTER)
91743c4d13eSSimon Budig 		tsdata->report_rate = edt_ft5x06_register_read(tsdata,
918fd335ab0SLothar Waßmann 						reg_addr->reg_report_rate);
919169110c3SSimon Budig 	if (tsdata->version == EDT_M06 ||
920aed5d0eeSSimon Budig 	    tsdata->version == EDT_M09 ||
921aed5d0eeSSimon Budig 	    tsdata->version == EDT_M12) {
922169110c3SSimon Budig 		tsdata->num_x = edt_ft5x06_register_read(tsdata,
923169110c3SSimon Budig 							 reg_addr->reg_num_x);
924169110c3SSimon Budig 		tsdata->num_y = edt_ft5x06_register_read(tsdata,
925169110c3SSimon Budig 							 reg_addr->reg_num_y);
926169110c3SSimon Budig 	} else {
927169110c3SSimon Budig 		tsdata->num_x = -1;
928169110c3SSimon Budig 		tsdata->num_y = -1;
929169110c3SSimon Budig 	}
930fd335ab0SLothar Waßmann }
931fd335ab0SLothar Waßmann 
932fd335ab0SLothar Waßmann static void
933fd335ab0SLothar Waßmann edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
934fd335ab0SLothar Waßmann {
935fd335ab0SLothar Waßmann 	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
936fd335ab0SLothar Waßmann 
937fd335ab0SLothar Waßmann 	switch (tsdata->version) {
938169110c3SSimon Budig 	case EDT_M06:
939fd335ab0SLothar Waßmann 		reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
940fd335ab0SLothar Waßmann 		reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
941fd335ab0SLothar Waßmann 		reg_addr->reg_gain = WORK_REGISTER_GAIN;
942fd335ab0SLothar Waßmann 		reg_addr->reg_offset = WORK_REGISTER_OFFSET;
943fd335ab0SLothar Waßmann 		reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
944fd335ab0SLothar Waßmann 		reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
945fd335ab0SLothar Waßmann 		break;
946fd335ab0SLothar Waßmann 
947169110c3SSimon Budig 	case EDT_M09:
948aed5d0eeSSimon Budig 	case EDT_M12:
949fd335ab0SLothar Waßmann 		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
95047014752SLuca Ceresoli 		reg_addr->reg_report_rate = NO_REGISTER;
951fd335ab0SLothar Waßmann 		reg_addr->reg_gain = M09_REGISTER_GAIN;
952fd335ab0SLothar Waßmann 		reg_addr->reg_offset = M09_REGISTER_OFFSET;
953fd335ab0SLothar Waßmann 		reg_addr->reg_num_x = M09_REGISTER_NUM_X;
954fd335ab0SLothar Waßmann 		reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
955fd335ab0SLothar Waßmann 		break;
956169110c3SSimon Budig 
957169110c3SSimon Budig 	case GENERIC_FT:
958169110c3SSimon Budig 		/* this is a guesswork */
959169110c3SSimon Budig 		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
960169110c3SSimon Budig 		reg_addr->reg_gain = M09_REGISTER_GAIN;
961169110c3SSimon Budig 		reg_addr->reg_offset = M09_REGISTER_OFFSET;
962169110c3SSimon Budig 		break;
963fd335ab0SLothar Waßmann 	}
96443c4d13eSSimon Budig }
96543c4d13eSSimon Budig 
9665298cc4cSBill Pemberton static int edt_ft5x06_ts_probe(struct i2c_client *client,
96743c4d13eSSimon Budig 					 const struct i2c_device_id *id)
96843c4d13eSSimon Budig {
969b1d2a3ecSFranklin S Cooper Jr 	const struct edt_i2c_chip_data *chip_data;
97043c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata;
97143c4d13eSSimon Budig 	struct input_dev *input;
972f0bef75cSDmitry Torokhov 	unsigned long irq_flags;
97343c4d13eSSimon Budig 	int error;
97443c4d13eSSimon Budig 	char fw_version[EDT_NAME_LEN];
97543c4d13eSSimon Budig 
97643c4d13eSSimon Budig 	dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
97743c4d13eSSimon Budig 
97802300bd6SLothar Waßmann 	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
97902300bd6SLothar Waßmann 	if (!tsdata) {
98043c4d13eSSimon Budig 		dev_err(&client->dev, "failed to allocate driver data.\n");
98102300bd6SLothar Waßmann 		return -ENOMEM;
98202300bd6SLothar Waßmann 	}
98302300bd6SLothar Waßmann 
984b1d2a3ecSFranklin S Cooper Jr 	chip_data = of_device_get_match_data(&client->dev);
985b1d2a3ecSFranklin S Cooper Jr 	if (!chip_data)
986b1d2a3ecSFranklin S Cooper Jr 		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
987b1d2a3ecSFranklin S Cooper Jr 	if (!chip_data || !chip_data->max_support_points) {
988b1d2a3ecSFranklin S Cooper Jr 		dev_err(&client->dev, "invalid or missing chip data\n");
989b1d2a3ecSFranklin S Cooper Jr 		return -EINVAL;
990b1d2a3ecSFranklin S Cooper Jr 	}
991b1d2a3ecSFranklin S Cooper Jr 
992b1d2a3ecSFranklin S Cooper Jr 	tsdata->max_support_points = chip_data->max_support_points;
993b1d2a3ecSFranklin S Cooper Jr 
99413c23cd1SFranklin S Cooper Jr 	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
99513c23cd1SFranklin S Cooper Jr 						     "reset", GPIOD_OUT_HIGH);
99613c23cd1SFranklin S Cooper Jr 	if (IS_ERR(tsdata->reset_gpio)) {
99713c23cd1SFranklin S Cooper Jr 		error = PTR_ERR(tsdata->reset_gpio);
998dac90dc2SLothar Waßmann 		dev_err(&client->dev,
99913c23cd1SFranklin S Cooper Jr 			"Failed to request GPIO reset pin, error %d\n", error);
1000dac90dc2SLothar Waßmann 		return error;
1001dac90dc2SLothar Waßmann 	}
1002dac90dc2SLothar Waßmann 
100313c23cd1SFranklin S Cooper Jr 	tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
100413c23cd1SFranklin S Cooper Jr 						    "wake", GPIOD_OUT_LOW);
100513c23cd1SFranklin S Cooper Jr 	if (IS_ERR(tsdata->wake_gpio)) {
100613c23cd1SFranklin S Cooper Jr 		error = PTR_ERR(tsdata->wake_gpio);
1007dac90dc2SLothar Waßmann 		dev_err(&client->dev,
100813c23cd1SFranklin S Cooper Jr 			"Failed to request GPIO wake pin, error %d\n", error);
1009dac90dc2SLothar Waßmann 		return error;
1010dac90dc2SLothar Waßmann 	}
101113c23cd1SFranklin S Cooper Jr 
101213c23cd1SFranklin S Cooper Jr 	if (tsdata->wake_gpio) {
101313c23cd1SFranklin S Cooper Jr 		usleep_range(5000, 6000);
101413c23cd1SFranklin S Cooper Jr 		gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
101513c23cd1SFranklin S Cooper Jr 	}
101613c23cd1SFranklin S Cooper Jr 
101713c23cd1SFranklin S Cooper Jr 	if (tsdata->reset_gpio) {
101813c23cd1SFranklin S Cooper Jr 		usleep_range(5000, 6000);
101913c23cd1SFranklin S Cooper Jr 		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
102013c23cd1SFranklin S Cooper Jr 		msleep(300);
1021dac90dc2SLothar Waßmann 	}
1022dac90dc2SLothar Waßmann 
102302300bd6SLothar Waßmann 	input = devm_input_allocate_device(&client->dev);
102402300bd6SLothar Waßmann 	if (!input) {
102502300bd6SLothar Waßmann 		dev_err(&client->dev, "failed to allocate input device.\n");
102602300bd6SLothar Waßmann 		return -ENOMEM;
102743c4d13eSSimon Budig 	}
102843c4d13eSSimon Budig 
102943c4d13eSSimon Budig 	mutex_init(&tsdata->mutex);
103043c4d13eSSimon Budig 	tsdata->client = client;
103143c4d13eSSimon Budig 	tsdata->input = input;
103243c4d13eSSimon Budig 	tsdata->factory_mode = false;
103343c4d13eSSimon Budig 
1034fd335ab0SLothar Waßmann 	error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
103543c4d13eSSimon Budig 	if (error) {
103643c4d13eSSimon Budig 		dev_err(&client->dev, "touchscreen probe failed\n");
103702300bd6SLothar Waßmann 		return error;
103843c4d13eSSimon Budig 	}
103943c4d13eSSimon Budig 
1040fd335ab0SLothar Waßmann 	edt_ft5x06_ts_set_regs(tsdata);
10412e23b7a9SDmitry Torokhov 	edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
104243c4d13eSSimon Budig 	edt_ft5x06_ts_get_parameters(tsdata);
104343c4d13eSSimon Budig 
104443c4d13eSSimon Budig 	dev_dbg(&client->dev,
104543c4d13eSSimon Budig 		"Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
104643c4d13eSSimon Budig 		tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
104743c4d13eSSimon Budig 
104843c4d13eSSimon Budig 	input->name = tsdata->name;
104943c4d13eSSimon Budig 	input->id.bustype = BUS_I2C;
105043c4d13eSSimon Budig 	input->dev.parent = &client->dev;
105143c4d13eSSimon Budig 
1052169110c3SSimon Budig 	if (tsdata->version == EDT_M06 ||
1053aed5d0eeSSimon Budig 	    tsdata->version == EDT_M09 ||
1054aed5d0eeSSimon Budig 	    tsdata->version == EDT_M12) {
105543c4d13eSSimon Budig 		input_set_abs_params(input, ABS_MT_POSITION_X,
105643c4d13eSSimon Budig 				     0, tsdata->num_x * 64 - 1, 0, 0);
105743c4d13eSSimon Budig 		input_set_abs_params(input, ABS_MT_POSITION_Y,
105843c4d13eSSimon Budig 				     0, tsdata->num_y * 64 - 1, 0, 0);
1059169110c3SSimon Budig 	} else {
1060169110c3SSimon Budig 		/* Unknown maximum values. Specify via devicetree */
1061169110c3SSimon Budig 		input_set_abs_params(input, ABS_MT_POSITION_X,
1062169110c3SSimon Budig 				     0, 65535, 0, 0);
1063169110c3SSimon Budig 		input_set_abs_params(input, ABS_MT_POSITION_Y,
1064169110c3SSimon Budig 				     0, 65535, 0, 0);
1065169110c3SSimon Budig 	}
10662c005598SMaxime Ripard 
1067ad368eb2SHans de Goede 	touchscreen_parse_properties(input, true, &tsdata->prop);
10682c005598SMaxime Ripard 
1069b1d2a3ecSFranklin S Cooper Jr 	error = input_mt_init_slots(input, tsdata->max_support_points,
1070b1d2a3ecSFranklin S Cooper Jr 				INPUT_MT_DIRECT);
107143c4d13eSSimon Budig 	if (error) {
107243c4d13eSSimon Budig 		dev_err(&client->dev, "Unable to init MT slots.\n");
107302300bd6SLothar Waßmann 		return error;
107443c4d13eSSimon Budig 	}
107543c4d13eSSimon Budig 
107643c4d13eSSimon Budig 	i2c_set_clientdata(client, tsdata);
107743c4d13eSSimon Budig 
1078f0bef75cSDmitry Torokhov 	irq_flags = irq_get_trigger_type(client->irq);
1079f0bef75cSDmitry Torokhov 	if (irq_flags == IRQF_TRIGGER_NONE)
1080f0bef75cSDmitry Torokhov 		irq_flags = IRQF_TRIGGER_FALLING;
1081f0bef75cSDmitry Torokhov 	irq_flags |= IRQF_ONESHOT;
1082f0bef75cSDmitry Torokhov 
1083f0bef75cSDmitry Torokhov 	error = devm_request_threaded_irq(&client->dev, client->irq,
1084f0bef75cSDmitry Torokhov 					NULL, edt_ft5x06_ts_isr, irq_flags,
108543c4d13eSSimon Budig 					client->name, tsdata);
108643c4d13eSSimon Budig 	if (error) {
108743c4d13eSSimon Budig 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
108802300bd6SLothar Waßmann 		return error;
108943c4d13eSSimon Budig 	}
109043c4d13eSSimon Budig 
1091e3adf559SAndi Shyti 	error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
109243c4d13eSSimon Budig 	if (error)
109302300bd6SLothar Waßmann 		return error;
109443c4d13eSSimon Budig 
109543c4d13eSSimon Budig 	error = input_register_device(input);
1096dac90dc2SLothar Waßmann 	if (error)
1097e3adf559SAndi Shyti 		return error;
109843c4d13eSSimon Budig 
109943c4d13eSSimon Budig 	edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
110043c4d13eSSimon Budig 	device_init_wakeup(&client->dev, 1);
110143c4d13eSSimon Budig 
110243c4d13eSSimon Budig 	dev_dbg(&client->dev,
1103dac90dc2SLothar Waßmann 		"EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
11048b58cc36SFranklin S Cooper Jr 		client->irq,
11058b58cc36SFranklin S Cooper Jr 		tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
11068b58cc36SFranklin S Cooper Jr 		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
110743c4d13eSSimon Budig 
110843c4d13eSSimon Budig 	return 0;
110943c4d13eSSimon Budig }
111043c4d13eSSimon Budig 
1111e2619cf7SBill Pemberton static int edt_ft5x06_ts_remove(struct i2c_client *client)
111243c4d13eSSimon Budig {
111343c4d13eSSimon Budig 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
111443c4d13eSSimon Budig 
111543c4d13eSSimon Budig 	edt_ft5x06_ts_teardown_debugfs(tsdata);
111643c4d13eSSimon Budig 
111743c4d13eSSimon Budig 	return 0;
111843c4d13eSSimon Budig }
111943c4d13eSSimon Budig 
112002b6a58bSJingoo Han static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
112143c4d13eSSimon Budig {
112243c4d13eSSimon Budig 	struct i2c_client *client = to_i2c_client(dev);
112343c4d13eSSimon Budig 
112443c4d13eSSimon Budig 	if (device_may_wakeup(dev))
112543c4d13eSSimon Budig 		enable_irq_wake(client->irq);
112643c4d13eSSimon Budig 
112743c4d13eSSimon Budig 	return 0;
112843c4d13eSSimon Budig }
112943c4d13eSSimon Budig 
113002b6a58bSJingoo Han static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
113143c4d13eSSimon Budig {
113243c4d13eSSimon Budig 	struct i2c_client *client = to_i2c_client(dev);
113343c4d13eSSimon Budig 
113443c4d13eSSimon Budig 	if (device_may_wakeup(dev))
113543c4d13eSSimon Budig 		disable_irq_wake(client->irq);
113643c4d13eSSimon Budig 
113743c4d13eSSimon Budig 	return 0;
113843c4d13eSSimon Budig }
113943c4d13eSSimon Budig 
114043c4d13eSSimon Budig static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
114143c4d13eSSimon Budig 			 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
114243c4d13eSSimon Budig 
1143b1d2a3ecSFranklin S Cooper Jr static const struct edt_i2c_chip_data edt_ft5x06_data = {
1144b1d2a3ecSFranklin S Cooper Jr 	.max_support_points = 5,
1145b1d2a3ecSFranklin S Cooper Jr };
1146b1d2a3ecSFranklin S Cooper Jr 
1147af33e0adSFranklin S Cooper Jr static const struct edt_i2c_chip_data edt_ft5506_data = {
1148af33e0adSFranklin S Cooper Jr 	.max_support_points = 10,
1149af33e0adSFranklin S Cooper Jr };
1150af33e0adSFranklin S Cooper Jr 
1151d1871654SHans de Goede static const struct edt_i2c_chip_data edt_ft6236_data = {
1152d1871654SHans de Goede 	.max_support_points = 2,
1153d1871654SHans de Goede };
1154d1871654SHans de Goede 
115543c4d13eSSimon Budig static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1156b1d2a3ecSFranklin S Cooper Jr 	{ .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1157af33e0adSFranklin S Cooper Jr 	{ .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1158d1871654SHans de Goede 	/* Note no edt- prefix for compatibility with the ft6236.c driver */
1159d1871654SHans de Goede 	{ .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
11601730d814SLothar Waßmann 	{ /* sentinel */ }
116143c4d13eSSimon Budig };
116243c4d13eSSimon Budig MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
116343c4d13eSSimon Budig 
1164dac90dc2SLothar Waßmann #ifdef CONFIG_OF
1165dac90dc2SLothar Waßmann static const struct of_device_id edt_ft5x06_of_match[] = {
1166b1d2a3ecSFranklin S Cooper Jr 	{ .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1167b1d2a3ecSFranklin S Cooper Jr 	{ .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1168b1d2a3ecSFranklin S Cooper Jr 	{ .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1169af33e0adSFranklin S Cooper Jr 	{ .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1170d1871654SHans de Goede 	/* Note focaltech vendor prefix for compatibility with ft6236.c */
1171d1871654SHans de Goede 	{ .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1172dac90dc2SLothar Waßmann 	{ /* sentinel */ }
1173dac90dc2SLothar Waßmann };
1174dac90dc2SLothar Waßmann MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1175dac90dc2SLothar Waßmann #endif
1176dac90dc2SLothar Waßmann 
117743c4d13eSSimon Budig static struct i2c_driver edt_ft5x06_ts_driver = {
117843c4d13eSSimon Budig 	.driver = {
117943c4d13eSSimon Budig 		.name = "edt_ft5x06",
1180dac90dc2SLothar Waßmann 		.of_match_table = of_match_ptr(edt_ft5x06_of_match),
118143c4d13eSSimon Budig 		.pm = &edt_ft5x06_ts_pm_ops,
118243c4d13eSSimon Budig 	},
118343c4d13eSSimon Budig 	.id_table = edt_ft5x06_ts_id,
118443c4d13eSSimon Budig 	.probe    = edt_ft5x06_ts_probe,
11851cb0aa88SBill Pemberton 	.remove   = edt_ft5x06_ts_remove,
118643c4d13eSSimon Budig };
118743c4d13eSSimon Budig 
118843c4d13eSSimon Budig module_i2c_driver(edt_ft5x06_ts_driver);
118943c4d13eSSimon Budig 
119043c4d13eSSimon Budig MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
119143c4d13eSSimon Budig MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
119243c4d13eSSimon Budig MODULE_LICENSE("GPL");
1193