1ca96ea86SBastien Nocera /*
2ca96ea86SBastien Nocera  *  Driver for Goodix Touchscreens
3ca96ea86SBastien Nocera  *
4ca96ea86SBastien Nocera  *  Copyright (c) 2014 Red Hat Inc.
5ad48cf5eSKarsten Merker  *  Copyright (c) 2015 K. Merker <merker@debian.org>
6ca96ea86SBastien Nocera  *
7ca96ea86SBastien Nocera  *  This code is based on gt9xx.c authored by andrew@goodix.com:
8ca96ea86SBastien Nocera  *
9ca96ea86SBastien Nocera  *  2010 - 2012 Goodix Technology.
10ca96ea86SBastien Nocera  */
11ca96ea86SBastien Nocera 
12ca96ea86SBastien Nocera /*
13ca96ea86SBastien Nocera  * This program is free software; you can redistribute it and/or modify it
14ca96ea86SBastien Nocera  * under the terms of the GNU General Public License as published by the Free
15ca96ea86SBastien Nocera  * Software Foundation; version 2 of the License.
16ca96ea86SBastien Nocera  */
17ca96ea86SBastien Nocera 
18ca96ea86SBastien Nocera #include <linux/kernel.h>
198b5a359cSBastien Nocera #include <linux/dmi.h>
2068caf858SIrina Tirdea #include <linux/firmware.h>
21ec6e1b40SIrina Tirdea #include <linux/gpio/consumer.h>
22ca96ea86SBastien Nocera #include <linux/i2c.h>
23ca96ea86SBastien Nocera #include <linux/input.h>
24ca96ea86SBastien Nocera #include <linux/input/mt.h>
25fafef982SMarcin Niestroj #include <linux/input/touchscreen.h>
26ca96ea86SBastien Nocera #include <linux/module.h>
27ca96ea86SBastien Nocera #include <linux/delay.h>
28ca96ea86SBastien Nocera #include <linux/irq.h>
29ca96ea86SBastien Nocera #include <linux/interrupt.h>
30ae97fb58SJagan Teki #include <linux/regulator/consumer.h>
31ca96ea86SBastien Nocera #include <linux/slab.h>
32771d8f1bSAleksei Mamlin #include <linux/acpi.h>
33771d8f1bSAleksei Mamlin #include <linux/of.h>
34ca96ea86SBastien Nocera #include <asm/unaligned.h>
35ca96ea86SBastien Nocera 
3625309004SMarcin Niestroj struct goodix_ts_data;
3725309004SMarcin Niestroj 
3825309004SMarcin Niestroj struct goodix_chip_data {
3925309004SMarcin Niestroj 	u16 config_addr;
4025309004SMarcin Niestroj 	int config_len;
4125309004SMarcin Niestroj 	int (*check_config)(struct goodix_ts_data *, const struct firmware *);
4225309004SMarcin Niestroj };
4325309004SMarcin Niestroj 
44ca96ea86SBastien Nocera struct goodix_ts_data {
45ca96ea86SBastien Nocera 	struct i2c_client *client;
46ca96ea86SBastien Nocera 	struct input_dev *input_dev;
4725309004SMarcin Niestroj 	const struct goodix_chip_data *chip;
48fafef982SMarcin Niestroj 	struct touchscreen_properties prop;
49ca96ea86SBastien Nocera 	unsigned int max_touch_num;
50ca96ea86SBastien Nocera 	unsigned int int_trigger_type;
51ae97fb58SJagan Teki 	struct regulator *avdd28;
52ae97fb58SJagan Teki 	struct regulator *vddio;
53ec6e1b40SIrina Tirdea 	struct gpio_desc *gpiod_int;
54ec6e1b40SIrina Tirdea 	struct gpio_desc *gpiod_rst;
5568caf858SIrina Tirdea 	u16 id;
5668caf858SIrina Tirdea 	u16 version;
5768caf858SIrina Tirdea 	const char *cfg_name;
5868caf858SIrina Tirdea 	struct completion firmware_loading_complete;
595ab09d6aSIrina Tirdea 	unsigned long irq_flags;
60ca96ea86SBastien Nocera };
61ca96ea86SBastien Nocera 
62ec6e1b40SIrina Tirdea #define GOODIX_GPIO_INT_NAME		"irq"
63ec6e1b40SIrina Tirdea #define GOODIX_GPIO_RST_NAME		"reset"
64ec6e1b40SIrina Tirdea 
65ca96ea86SBastien Nocera #define GOODIX_MAX_HEIGHT		4096
66ca96ea86SBastien Nocera #define GOODIX_MAX_WIDTH		4096
67ca96ea86SBastien Nocera #define GOODIX_INT_TRIGGER		1
68ca96ea86SBastien Nocera #define GOODIX_CONTACT_SIZE		8
69ca96ea86SBastien Nocera #define GOODIX_MAX_CONTACTS		10
70ca96ea86SBastien Nocera 
71ca96ea86SBastien Nocera #define GOODIX_CONFIG_MAX_LENGTH	240
72a779fbc6SIrina Tirdea #define GOODIX_CONFIG_911_LENGTH	186
73a779fbc6SIrina Tirdea #define GOODIX_CONFIG_967_LENGTH	228
74ca96ea86SBastien Nocera 
75ca96ea86SBastien Nocera /* Register defines */
765ab09d6aSIrina Tirdea #define GOODIX_REG_COMMAND		0x8040
775ab09d6aSIrina Tirdea #define GOODIX_CMD_SCREEN_OFF		0x05
785ab09d6aSIrina Tirdea 
79ca96ea86SBastien Nocera #define GOODIX_READ_COOR_ADDR		0x814E
8025309004SMarcin Niestroj #define GOODIX_GT1X_REG_CONFIG_DATA	0x8050
8125309004SMarcin Niestroj #define GOODIX_GT9X_REG_CONFIG_DATA	0x8047
82e70b0307SIrina Tirdea #define GOODIX_REG_ID			0x8140
83ca96ea86SBastien Nocera 
849b5db7aaSPaul Cercueil #define GOODIX_BUFFER_STATUS_READY	BIT(7)
859b5db7aaSPaul Cercueil #define GOODIX_BUFFER_STATUS_TIMEOUT	20
869b5db7aaSPaul Cercueil 
87ca96ea86SBastien Nocera #define RESOLUTION_LOC		1
88a7ac7c95SAleksei Mamlin #define MAX_CONTACTS_LOC	5
89ca96ea86SBastien Nocera #define TRIGGER_LOC		6
90ca96ea86SBastien Nocera 
9125309004SMarcin Niestroj static int goodix_check_cfg_8(struct goodix_ts_data *ts,
9225309004SMarcin Niestroj 			const struct firmware *cfg);
9325309004SMarcin Niestroj static int goodix_check_cfg_16(struct goodix_ts_data *ts,
9425309004SMarcin Niestroj 			const struct firmware *cfg);
9525309004SMarcin Niestroj 
9625309004SMarcin Niestroj static const struct goodix_chip_data gt1x_chip_data = {
9725309004SMarcin Niestroj 	.config_addr		= GOODIX_GT1X_REG_CONFIG_DATA,
9825309004SMarcin Niestroj 	.config_len		= GOODIX_CONFIG_MAX_LENGTH,
9925309004SMarcin Niestroj 	.check_config		= goodix_check_cfg_16,
10025309004SMarcin Niestroj };
10125309004SMarcin Niestroj 
10225309004SMarcin Niestroj static const struct goodix_chip_data gt911_chip_data = {
10325309004SMarcin Niestroj 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
10425309004SMarcin Niestroj 	.config_len		= GOODIX_CONFIG_911_LENGTH,
10525309004SMarcin Niestroj 	.check_config		= goodix_check_cfg_8,
10625309004SMarcin Niestroj };
10725309004SMarcin Niestroj 
10825309004SMarcin Niestroj static const struct goodix_chip_data gt967_chip_data = {
10925309004SMarcin Niestroj 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
11025309004SMarcin Niestroj 	.config_len		= GOODIX_CONFIG_967_LENGTH,
11125309004SMarcin Niestroj 	.check_config		= goodix_check_cfg_8,
11225309004SMarcin Niestroj };
11325309004SMarcin Niestroj 
11425309004SMarcin Niestroj static const struct goodix_chip_data gt9x_chip_data = {
11525309004SMarcin Niestroj 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
11625309004SMarcin Niestroj 	.config_len		= GOODIX_CONFIG_MAX_LENGTH,
11725309004SMarcin Niestroj 	.check_config		= goodix_check_cfg_8,
11825309004SMarcin Niestroj };
11925309004SMarcin Niestroj 
120ca96ea86SBastien Nocera static const unsigned long goodix_irq_flags[] = {
121ca96ea86SBastien Nocera 	IRQ_TYPE_EDGE_RISING,
122ca96ea86SBastien Nocera 	IRQ_TYPE_EDGE_FALLING,
123ca96ea86SBastien Nocera 	IRQ_TYPE_LEVEL_LOW,
124ca96ea86SBastien Nocera 	IRQ_TYPE_LEVEL_HIGH,
125ca96ea86SBastien Nocera };
126ca96ea86SBastien Nocera 
1278b5a359cSBastien Nocera /*
1288b5a359cSBastien Nocera  * Those tablets have their coordinates origin at the bottom right
1298b5a359cSBastien Nocera  * of the tablet, as if rotated 180 degrees
1308b5a359cSBastien Nocera  */
1318b5a359cSBastien Nocera static const struct dmi_system_id rotated_screen[] = {
1328b5a359cSBastien Nocera #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1338b5a359cSBastien Nocera 	{
1348b5a359cSBastien Nocera 		.ident = "WinBook TW100",
1358b5a359cSBastien Nocera 		.matches = {
1368b5a359cSBastien Nocera 			DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
1378b5a359cSBastien Nocera 			DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
1388b5a359cSBastien Nocera 		}
1398b5a359cSBastien Nocera 	},
1408b5a359cSBastien Nocera 	{
1418b5a359cSBastien Nocera 		.ident = "WinBook TW700",
1428b5a359cSBastien Nocera 		.matches = {
1438b5a359cSBastien Nocera 			DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
1448b5a359cSBastien Nocera 			DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
1458b5a359cSBastien Nocera 		},
1468b5a359cSBastien Nocera 	},
1478b5a359cSBastien Nocera #endif
1488b5a359cSBastien Nocera 	{}
1498b5a359cSBastien Nocera };
1508b5a359cSBastien Nocera 
151ca96ea86SBastien Nocera /**
152ca96ea86SBastien Nocera  * goodix_i2c_read - read data from a register of the i2c slave device.
153ca96ea86SBastien Nocera  *
154ca96ea86SBastien Nocera  * @client: i2c device.
155ca96ea86SBastien Nocera  * @reg: the register to read from.
156ca96ea86SBastien Nocera  * @buf: raw write data buffer.
157ca96ea86SBastien Nocera  * @len: length of the buffer to write
158ca96ea86SBastien Nocera  */
159ca96ea86SBastien Nocera static int goodix_i2c_read(struct i2c_client *client,
160ca96ea86SBastien Nocera 			   u16 reg, u8 *buf, int len)
161ca96ea86SBastien Nocera {
162ca96ea86SBastien Nocera 	struct i2c_msg msgs[2];
163fafef982SMarcin Niestroj 	__be16 wbuf = cpu_to_be16(reg);
164ca96ea86SBastien Nocera 	int ret;
165ca96ea86SBastien Nocera 
166ca96ea86SBastien Nocera 	msgs[0].flags = 0;
167ca96ea86SBastien Nocera 	msgs[0].addr  = client->addr;
168ca96ea86SBastien Nocera 	msgs[0].len   = 2;
169ca96ea86SBastien Nocera 	msgs[0].buf   = (u8 *)&wbuf;
170ca96ea86SBastien Nocera 
171ca96ea86SBastien Nocera 	msgs[1].flags = I2C_M_RD;
172ca96ea86SBastien Nocera 	msgs[1].addr  = client->addr;
173ca96ea86SBastien Nocera 	msgs[1].len   = len;
174ca96ea86SBastien Nocera 	msgs[1].buf   = buf;
175ca96ea86SBastien Nocera 
176ca96ea86SBastien Nocera 	ret = i2c_transfer(client->adapter, msgs, 2);
177ca96ea86SBastien Nocera 	return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
178ca96ea86SBastien Nocera }
179ca96ea86SBastien Nocera 
18068caf858SIrina Tirdea /**
18168caf858SIrina Tirdea  * goodix_i2c_write - write data to a register of the i2c slave device.
18268caf858SIrina Tirdea  *
18368caf858SIrina Tirdea  * @client: i2c device.
18468caf858SIrina Tirdea  * @reg: the register to write to.
18568caf858SIrina Tirdea  * @buf: raw data buffer to write.
18668caf858SIrina Tirdea  * @len: length of the buffer to write
18768caf858SIrina Tirdea  */
18868caf858SIrina Tirdea static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
18968caf858SIrina Tirdea 			    unsigned len)
19068caf858SIrina Tirdea {
19168caf858SIrina Tirdea 	u8 *addr_buf;
19268caf858SIrina Tirdea 	struct i2c_msg msg;
19368caf858SIrina Tirdea 	int ret;
19468caf858SIrina Tirdea 
19568caf858SIrina Tirdea 	addr_buf = kmalloc(len + 2, GFP_KERNEL);
19668caf858SIrina Tirdea 	if (!addr_buf)
19768caf858SIrina Tirdea 		return -ENOMEM;
19868caf858SIrina Tirdea 
19968caf858SIrina Tirdea 	addr_buf[0] = reg >> 8;
20068caf858SIrina Tirdea 	addr_buf[1] = reg & 0xFF;
20168caf858SIrina Tirdea 	memcpy(&addr_buf[2], buf, len);
20268caf858SIrina Tirdea 
20368caf858SIrina Tirdea 	msg.flags = 0;
20468caf858SIrina Tirdea 	msg.addr = client->addr;
20568caf858SIrina Tirdea 	msg.buf = addr_buf;
20668caf858SIrina Tirdea 	msg.len = len + 2;
20768caf858SIrina Tirdea 
20868caf858SIrina Tirdea 	ret = i2c_transfer(client->adapter, &msg, 1);
20968caf858SIrina Tirdea 	kfree(addr_buf);
21068caf858SIrina Tirdea 	return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
21168caf858SIrina Tirdea }
21268caf858SIrina Tirdea 
2135ab09d6aSIrina Tirdea static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
2145ab09d6aSIrina Tirdea {
2155ab09d6aSIrina Tirdea 	return goodix_i2c_write(client, reg, &value, sizeof(value));
2165ab09d6aSIrina Tirdea }
2175ab09d6aSIrina Tirdea 
21825309004SMarcin Niestroj static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
219a779fbc6SIrina Tirdea {
220a779fbc6SIrina Tirdea 	switch (id) {
22125309004SMarcin Niestroj 	case 1151:
22284ef1b33SGuido Günther 	case 5688:
22325309004SMarcin Niestroj 		return &gt1x_chip_data;
22425309004SMarcin Niestroj 
225a779fbc6SIrina Tirdea 	case 911:
226a779fbc6SIrina Tirdea 	case 9271:
227a779fbc6SIrina Tirdea 	case 9110:
228a779fbc6SIrina Tirdea 	case 927:
229a779fbc6SIrina Tirdea 	case 928:
23025309004SMarcin Niestroj 		return &gt911_chip_data;
231a779fbc6SIrina Tirdea 
232a779fbc6SIrina Tirdea 	case 912:
233a779fbc6SIrina Tirdea 	case 967:
23425309004SMarcin Niestroj 		return &gt967_chip_data;
235a779fbc6SIrina Tirdea 
236a779fbc6SIrina Tirdea 	default:
23725309004SMarcin Niestroj 		return &gt9x_chip_data;
238a779fbc6SIrina Tirdea 	}
239a779fbc6SIrina Tirdea }
240a779fbc6SIrina Tirdea 
241ca96ea86SBastien Nocera static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
242ca96ea86SBastien Nocera {
2439b5db7aaSPaul Cercueil 	unsigned long max_timeout;
244ca96ea86SBastien Nocera 	int touch_num;
245ca96ea86SBastien Nocera 	int error;
246ca96ea86SBastien Nocera 
2479b5db7aaSPaul Cercueil 	/*
2489b5db7aaSPaul Cercueil 	 * The 'buffer status' bit, which indicates that the data is valid, is
2499b5db7aaSPaul Cercueil 	 * not set as soon as the interrupt is raised, but slightly after.
2509b5db7aaSPaul Cercueil 	 * This takes around 10 ms to happen, so we poll for 20 ms.
2519b5db7aaSPaul Cercueil 	 */
2529b5db7aaSPaul Cercueil 	max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
2539b5db7aaSPaul Cercueil 	do {
2549b5db7aaSPaul Cercueil 		error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
2559b5db7aaSPaul Cercueil 					data, GOODIX_CONTACT_SIZE + 1);
256ca96ea86SBastien Nocera 		if (error) {
2579b5db7aaSPaul Cercueil 			dev_err(&ts->client->dev, "I2C transfer error: %d\n",
2589b5db7aaSPaul Cercueil 					error);
259ca96ea86SBastien Nocera 			return error;
260ca96ea86SBastien Nocera 		}
261ca96ea86SBastien Nocera 
2629b5db7aaSPaul Cercueil 		if (data[0] & GOODIX_BUFFER_STATUS_READY) {
263ca96ea86SBastien Nocera 			touch_num = data[0] & 0x0f;
264a7ac7c95SAleksei Mamlin 			if (touch_num > ts->max_touch_num)
265ca96ea86SBastien Nocera 				return -EPROTO;
266ca96ea86SBastien Nocera 
267ca96ea86SBastien Nocera 			if (touch_num > 1) {
268ca96ea86SBastien Nocera 				data += 1 + GOODIX_CONTACT_SIZE;
269ca96ea86SBastien Nocera 				error = goodix_i2c_read(ts->client,
270ca96ea86SBastien Nocera 						GOODIX_READ_COOR_ADDR +
271ca96ea86SBastien Nocera 							1 + GOODIX_CONTACT_SIZE,
272ca96ea86SBastien Nocera 						data,
2739b5db7aaSPaul Cercueil 						GOODIX_CONTACT_SIZE *
2749b5db7aaSPaul Cercueil 							(touch_num - 1));
275ca96ea86SBastien Nocera 				if (error)
276ca96ea86SBastien Nocera 					return error;
277ca96ea86SBastien Nocera 			}
278ca96ea86SBastien Nocera 
279ca96ea86SBastien Nocera 			return touch_num;
280ca96ea86SBastien Nocera 		}
281ca96ea86SBastien Nocera 
2829b5db7aaSPaul Cercueil 		usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
2839b5db7aaSPaul Cercueil 	} while (time_before(jiffies, max_timeout));
2849b5db7aaSPaul Cercueil 
2859b5db7aaSPaul Cercueil 	/*
2869b5db7aaSPaul Cercueil 	 * The Goodix panel will send spurious interrupts after a
2879b5db7aaSPaul Cercueil 	 * 'finger up' event, which will always cause a timeout.
2889b5db7aaSPaul Cercueil 	 */
2899b5db7aaSPaul Cercueil 	return 0;
2909b5db7aaSPaul Cercueil }
2919b5db7aaSPaul Cercueil 
292ca96ea86SBastien Nocera static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
293ca96ea86SBastien Nocera {
294ca96ea86SBastien Nocera 	int id = coor_data[0] & 0x0F;
295ca96ea86SBastien Nocera 	int input_x = get_unaligned_le16(&coor_data[1]);
296ca96ea86SBastien Nocera 	int input_y = get_unaligned_le16(&coor_data[3]);
297ca96ea86SBastien Nocera 	int input_w = get_unaligned_le16(&coor_data[5]);
298ca96ea86SBastien Nocera 
299ca96ea86SBastien Nocera 	input_mt_slot(ts->input_dev, id);
300ca96ea86SBastien Nocera 	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
301fafef982SMarcin Niestroj 	touchscreen_report_pos(ts->input_dev, &ts->prop,
302fafef982SMarcin Niestroj 			       input_x, input_y, true);
303ca96ea86SBastien Nocera 	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
304ca96ea86SBastien Nocera 	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
305ca96ea86SBastien Nocera }
306ca96ea86SBastien Nocera 
307ca96ea86SBastien Nocera /**
308ca96ea86SBastien Nocera  * goodix_process_events - Process incoming events
309ca96ea86SBastien Nocera  *
310ca96ea86SBastien Nocera  * @ts: our goodix_ts_data pointer
311ca96ea86SBastien Nocera  *
312ca96ea86SBastien Nocera  * Called when the IRQ is triggered. Read the current device state, and push
313ca96ea86SBastien Nocera  * the input events to the user space.
314ca96ea86SBastien Nocera  */
315ca96ea86SBastien Nocera static void goodix_process_events(struct goodix_ts_data *ts)
316ca96ea86SBastien Nocera {
3170e0432f0SIrina Tirdea 	u8  point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
318ca96ea86SBastien Nocera 	int touch_num;
319ca96ea86SBastien Nocera 	int i;
320ca96ea86SBastien Nocera 
321ca96ea86SBastien Nocera 	touch_num = goodix_ts_read_input_report(ts, point_data);
322ca96ea86SBastien Nocera 	if (touch_num < 0)
323ca96ea86SBastien Nocera 		return;
324ca96ea86SBastien Nocera 
3254a54feeaSSergei A. Trusov 	/*
3264a54feeaSSergei A. Trusov 	 * Bit 4 of the first byte reports the status of the capacitive
3274a54feeaSSergei A. Trusov 	 * Windows/Home button.
3284a54feeaSSergei A. Trusov 	 */
3294a54feeaSSergei A. Trusov 	input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
3304a54feeaSSergei A. Trusov 
331ca96ea86SBastien Nocera 	for (i = 0; i < touch_num; i++)
332ca96ea86SBastien Nocera 		goodix_ts_report_touch(ts,
333ca96ea86SBastien Nocera 				&point_data[1 + GOODIX_CONTACT_SIZE * i]);
334ca96ea86SBastien Nocera 
335ca96ea86SBastien Nocera 	input_mt_sync_frame(ts->input_dev);
336ca96ea86SBastien Nocera 	input_sync(ts->input_dev);
337ca96ea86SBastien Nocera }
338ca96ea86SBastien Nocera 
339ca96ea86SBastien Nocera /**
340ca96ea86SBastien Nocera  * goodix_ts_irq_handler - The IRQ handler
341ca96ea86SBastien Nocera  *
342ca96ea86SBastien Nocera  * @irq: interrupt number.
343ca96ea86SBastien Nocera  * @dev_id: private data pointer.
344ca96ea86SBastien Nocera  */
345ca96ea86SBastien Nocera static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
346ca96ea86SBastien Nocera {
347ca96ea86SBastien Nocera 	struct goodix_ts_data *ts = dev_id;
348ca96ea86SBastien Nocera 
349ca96ea86SBastien Nocera 	goodix_process_events(ts);
350ca96ea86SBastien Nocera 
3515d655b35SIrina Tirdea 	if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
352ca96ea86SBastien Nocera 		dev_err(&ts->client->dev, "I2C write end_cmd error\n");
353ca96ea86SBastien Nocera 
354ca96ea86SBastien Nocera 	return IRQ_HANDLED;
355ca96ea86SBastien Nocera }
356ca96ea86SBastien Nocera 
3575ab09d6aSIrina Tirdea static void goodix_free_irq(struct goodix_ts_data *ts)
3585ab09d6aSIrina Tirdea {
3595ab09d6aSIrina Tirdea 	devm_free_irq(&ts->client->dev, ts->client->irq, ts);
3605ab09d6aSIrina Tirdea }
3615ab09d6aSIrina Tirdea 
3625ab09d6aSIrina Tirdea static int goodix_request_irq(struct goodix_ts_data *ts)
3635ab09d6aSIrina Tirdea {
3645ab09d6aSIrina Tirdea 	return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
3655ab09d6aSIrina Tirdea 					 NULL, goodix_ts_irq_handler,
3665ab09d6aSIrina Tirdea 					 ts->irq_flags, ts->client->name, ts);
3675ab09d6aSIrina Tirdea }
3685ab09d6aSIrina Tirdea 
36925309004SMarcin Niestroj static int goodix_check_cfg_8(struct goodix_ts_data *ts,
37068caf858SIrina Tirdea 			const struct firmware *cfg)
37168caf858SIrina Tirdea {
37225309004SMarcin Niestroj 	int i, raw_cfg_len = cfg->size - 2;
37368caf858SIrina Tirdea 	u8 check_sum = 0;
37468caf858SIrina Tirdea 
37568caf858SIrina Tirdea 	for (i = 0; i < raw_cfg_len; i++)
37668caf858SIrina Tirdea 		check_sum += cfg->data[i];
37768caf858SIrina Tirdea 	check_sum = (~check_sum) + 1;
37868caf858SIrina Tirdea 	if (check_sum != cfg->data[raw_cfg_len]) {
37968caf858SIrina Tirdea 		dev_err(&ts->client->dev,
38068caf858SIrina Tirdea 			"The checksum of the config fw is not correct");
38168caf858SIrina Tirdea 		return -EINVAL;
38268caf858SIrina Tirdea 	}
38368caf858SIrina Tirdea 
38468caf858SIrina Tirdea 	if (cfg->data[raw_cfg_len + 1] != 1) {
38568caf858SIrina Tirdea 		dev_err(&ts->client->dev,
38668caf858SIrina Tirdea 			"Config fw must have Config_Fresh register set");
38768caf858SIrina Tirdea 		return -EINVAL;
38868caf858SIrina Tirdea 	}
38968caf858SIrina Tirdea 
39068caf858SIrina Tirdea 	return 0;
39168caf858SIrina Tirdea }
39268caf858SIrina Tirdea 
39325309004SMarcin Niestroj static int goodix_check_cfg_16(struct goodix_ts_data *ts,
39425309004SMarcin Niestroj 			const struct firmware *cfg)
39525309004SMarcin Niestroj {
39625309004SMarcin Niestroj 	int i, raw_cfg_len = cfg->size - 3;
39725309004SMarcin Niestroj 	u16 check_sum = 0;
39825309004SMarcin Niestroj 
39925309004SMarcin Niestroj 	for (i = 0; i < raw_cfg_len; i += 2)
40025309004SMarcin Niestroj 		check_sum += get_unaligned_be16(&cfg->data[i]);
40125309004SMarcin Niestroj 	check_sum = (~check_sum) + 1;
40225309004SMarcin Niestroj 	if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) {
40325309004SMarcin Niestroj 		dev_err(&ts->client->dev,
40425309004SMarcin Niestroj 			"The checksum of the config fw is not correct");
40525309004SMarcin Niestroj 		return -EINVAL;
40625309004SMarcin Niestroj 	}
40725309004SMarcin Niestroj 
40825309004SMarcin Niestroj 	if (cfg->data[raw_cfg_len + 2] != 1) {
40925309004SMarcin Niestroj 		dev_err(&ts->client->dev,
41025309004SMarcin Niestroj 			"Config fw must have Config_Fresh register set");
41125309004SMarcin Niestroj 		return -EINVAL;
41225309004SMarcin Niestroj 	}
41325309004SMarcin Niestroj 
41425309004SMarcin Niestroj 	return 0;
41525309004SMarcin Niestroj }
41625309004SMarcin Niestroj 
41725309004SMarcin Niestroj /**
41825309004SMarcin Niestroj  * goodix_check_cfg - Checks if config fw is valid
41925309004SMarcin Niestroj  *
42025309004SMarcin Niestroj  * @ts: goodix_ts_data pointer
42125309004SMarcin Niestroj  * @cfg: firmware config data
42225309004SMarcin Niestroj  */
42325309004SMarcin Niestroj static int goodix_check_cfg(struct goodix_ts_data *ts,
42425309004SMarcin Niestroj 			    const struct firmware *cfg)
42525309004SMarcin Niestroj {
42625309004SMarcin Niestroj 	if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
42725309004SMarcin Niestroj 		dev_err(&ts->client->dev,
42825309004SMarcin Niestroj 			"The length of the config fw is not correct");
42925309004SMarcin Niestroj 		return -EINVAL;
43025309004SMarcin Niestroj 	}
43125309004SMarcin Niestroj 
43225309004SMarcin Niestroj 	return ts->chip->check_config(ts, cfg);
43325309004SMarcin Niestroj }
43425309004SMarcin Niestroj 
43568caf858SIrina Tirdea /**
43668caf858SIrina Tirdea  * goodix_send_cfg - Write fw config to device
43768caf858SIrina Tirdea  *
43868caf858SIrina Tirdea  * @ts: goodix_ts_data pointer
43968caf858SIrina Tirdea  * @cfg: config firmware to write to device
44068caf858SIrina Tirdea  */
44168caf858SIrina Tirdea static int goodix_send_cfg(struct goodix_ts_data *ts,
44268caf858SIrina Tirdea 			   const struct firmware *cfg)
44368caf858SIrina Tirdea {
44468caf858SIrina Tirdea 	int error;
44568caf858SIrina Tirdea 
44668caf858SIrina Tirdea 	error = goodix_check_cfg(ts, cfg);
44768caf858SIrina Tirdea 	if (error)
44868caf858SIrina Tirdea 		return error;
44968caf858SIrina Tirdea 
45025309004SMarcin Niestroj 	error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data,
45168caf858SIrina Tirdea 				 cfg->size);
45268caf858SIrina Tirdea 	if (error) {
45368caf858SIrina Tirdea 		dev_err(&ts->client->dev, "Failed to write config data: %d",
45468caf858SIrina Tirdea 			error);
45568caf858SIrina Tirdea 		return error;
45668caf858SIrina Tirdea 	}
45768caf858SIrina Tirdea 	dev_dbg(&ts->client->dev, "Config sent successfully.");
45868caf858SIrina Tirdea 
45968caf858SIrina Tirdea 	/* Let the firmware reconfigure itself, so sleep for 10ms */
46068caf858SIrina Tirdea 	usleep_range(10000, 11000);
46168caf858SIrina Tirdea 
46268caf858SIrina Tirdea 	return 0;
46368caf858SIrina Tirdea }
46468caf858SIrina Tirdea 
465ec6e1b40SIrina Tirdea static int goodix_int_sync(struct goodix_ts_data *ts)
466ec6e1b40SIrina Tirdea {
467ec6e1b40SIrina Tirdea 	int error;
468ec6e1b40SIrina Tirdea 
469ec6e1b40SIrina Tirdea 	error = gpiod_direction_output(ts->gpiod_int, 0);
470ec6e1b40SIrina Tirdea 	if (error)
471ec6e1b40SIrina Tirdea 		return error;
472ec6e1b40SIrina Tirdea 
473ec6e1b40SIrina Tirdea 	msleep(50);				/* T5: 50ms */
474ec6e1b40SIrina Tirdea 
475ec6e1b40SIrina Tirdea 	error = gpiod_direction_input(ts->gpiod_int);
476ec6e1b40SIrina Tirdea 	if (error)
477ec6e1b40SIrina Tirdea 		return error;
478ec6e1b40SIrina Tirdea 
479ec6e1b40SIrina Tirdea 	return 0;
480ec6e1b40SIrina Tirdea }
481ec6e1b40SIrina Tirdea 
482ec6e1b40SIrina Tirdea /**
483ec6e1b40SIrina Tirdea  * goodix_reset - Reset device during power on
484ec6e1b40SIrina Tirdea  *
485ec6e1b40SIrina Tirdea  * @ts: goodix_ts_data pointer
486ec6e1b40SIrina Tirdea  */
487ec6e1b40SIrina Tirdea static int goodix_reset(struct goodix_ts_data *ts)
488ec6e1b40SIrina Tirdea {
489ec6e1b40SIrina Tirdea 	int error;
490ec6e1b40SIrina Tirdea 
491ec6e1b40SIrina Tirdea 	/* begin select I2C slave addr */
492ec6e1b40SIrina Tirdea 	error = gpiod_direction_output(ts->gpiod_rst, 0);
493ec6e1b40SIrina Tirdea 	if (error)
494ec6e1b40SIrina Tirdea 		return error;
495ec6e1b40SIrina Tirdea 
496ec6e1b40SIrina Tirdea 	msleep(20);				/* T2: > 10ms */
497ec6e1b40SIrina Tirdea 
498ec6e1b40SIrina Tirdea 	/* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
499ec6e1b40SIrina Tirdea 	error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
500ec6e1b40SIrina Tirdea 	if (error)
501ec6e1b40SIrina Tirdea 		return error;
502ec6e1b40SIrina Tirdea 
503ec6e1b40SIrina Tirdea 	usleep_range(100, 2000);		/* T3: > 100us */
504ec6e1b40SIrina Tirdea 
505ec6e1b40SIrina Tirdea 	error = gpiod_direction_output(ts->gpiod_rst, 1);
506ec6e1b40SIrina Tirdea 	if (error)
507ec6e1b40SIrina Tirdea 		return error;
508ec6e1b40SIrina Tirdea 
509ec6e1b40SIrina Tirdea 	usleep_range(6000, 10000);		/* T4: > 5ms */
510ec6e1b40SIrina Tirdea 
511ec6e1b40SIrina Tirdea 	/* end select I2C slave addr */
512ec6e1b40SIrina Tirdea 	error = gpiod_direction_input(ts->gpiod_rst);
513ec6e1b40SIrina Tirdea 	if (error)
514ec6e1b40SIrina Tirdea 		return error;
515ec6e1b40SIrina Tirdea 
516ec6e1b40SIrina Tirdea 	error = goodix_int_sync(ts);
517ec6e1b40SIrina Tirdea 	if (error)
518ec6e1b40SIrina Tirdea 		return error;
519ec6e1b40SIrina Tirdea 
520ec6e1b40SIrina Tirdea 	return 0;
521ec6e1b40SIrina Tirdea }
522ec6e1b40SIrina Tirdea 
523ec6e1b40SIrina Tirdea /**
524ec6e1b40SIrina Tirdea  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
525ec6e1b40SIrina Tirdea  *
526ec6e1b40SIrina Tirdea  * @ts: goodix_ts_data pointer
527ec6e1b40SIrina Tirdea  */
528ec6e1b40SIrina Tirdea static int goodix_get_gpio_config(struct goodix_ts_data *ts)
529ec6e1b40SIrina Tirdea {
530ec6e1b40SIrina Tirdea 	int error;
531ec6e1b40SIrina Tirdea 	struct device *dev;
532ec6e1b40SIrina Tirdea 	struct gpio_desc *gpiod;
533ec6e1b40SIrina Tirdea 
534ec6e1b40SIrina Tirdea 	if (!ts->client)
535ec6e1b40SIrina Tirdea 		return -EINVAL;
536ec6e1b40SIrina Tirdea 	dev = &ts->client->dev;
537ec6e1b40SIrina Tirdea 
538ae97fb58SJagan Teki 	ts->avdd28 = devm_regulator_get(dev, "AVDD28");
539ae97fb58SJagan Teki 	if (IS_ERR(ts->avdd28)) {
540ae97fb58SJagan Teki 		error = PTR_ERR(ts->avdd28);
541ae97fb58SJagan Teki 		if (error != -EPROBE_DEFER)
542ae97fb58SJagan Teki 			dev_err(dev,
543ae97fb58SJagan Teki 				"Failed to get AVDD28 regulator: %d\n", error);
544ae97fb58SJagan Teki 		return error;
545ae97fb58SJagan Teki 	}
546ae97fb58SJagan Teki 
547ae97fb58SJagan Teki 	ts->vddio = devm_regulator_get(dev, "VDDIO");
548ae97fb58SJagan Teki 	if (IS_ERR(ts->vddio)) {
549ae97fb58SJagan Teki 		error = PTR_ERR(ts->vddio);
550ae97fb58SJagan Teki 		if (error != -EPROBE_DEFER)
551ae97fb58SJagan Teki 			dev_err(dev,
552ae97fb58SJagan Teki 				"Failed to get VDDIO regulator: %d\n", error);
553ae97fb58SJagan Teki 		return error;
554ae97fb58SJagan Teki 	}
555ae97fb58SJagan Teki 
556ec6e1b40SIrina Tirdea 	/* Get the interrupt GPIO pin number */
557ec6e1b40SIrina Tirdea 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
558ec6e1b40SIrina Tirdea 	if (IS_ERR(gpiod)) {
559ec6e1b40SIrina Tirdea 		error = PTR_ERR(gpiod);
560ec6e1b40SIrina Tirdea 		if (error != -EPROBE_DEFER)
561ec6e1b40SIrina Tirdea 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
562ec6e1b40SIrina Tirdea 				GOODIX_GPIO_INT_NAME, error);
563ec6e1b40SIrina Tirdea 		return error;
564ec6e1b40SIrina Tirdea 	}
565ec6e1b40SIrina Tirdea 
566ec6e1b40SIrina Tirdea 	ts->gpiod_int = gpiod;
567ec6e1b40SIrina Tirdea 
568ec6e1b40SIrina Tirdea 	/* Get the reset line GPIO pin number */
569ec6e1b40SIrina Tirdea 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
570ec6e1b40SIrina Tirdea 	if (IS_ERR(gpiod)) {
571ec6e1b40SIrina Tirdea 		error = PTR_ERR(gpiod);
572ec6e1b40SIrina Tirdea 		if (error != -EPROBE_DEFER)
573ec6e1b40SIrina Tirdea 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
574ec6e1b40SIrina Tirdea 				GOODIX_GPIO_RST_NAME, error);
575ec6e1b40SIrina Tirdea 		return error;
576ec6e1b40SIrina Tirdea 	}
577ec6e1b40SIrina Tirdea 
578ec6e1b40SIrina Tirdea 	ts->gpiod_rst = gpiod;
579ec6e1b40SIrina Tirdea 
580ec6e1b40SIrina Tirdea 	return 0;
581ec6e1b40SIrina Tirdea }
582ec6e1b40SIrina Tirdea 
583ca96ea86SBastien Nocera /**
584ca96ea86SBastien Nocera  * goodix_read_config - Read the embedded configuration of the panel
585ca96ea86SBastien Nocera  *
586ca96ea86SBastien Nocera  * @ts: our goodix_ts_data pointer
587ca96ea86SBastien Nocera  *
588ca96ea86SBastien Nocera  * Must be called during probe
589ca96ea86SBastien Nocera  */
590ca96ea86SBastien Nocera static void goodix_read_config(struct goodix_ts_data *ts)
591ca96ea86SBastien Nocera {
592ca96ea86SBastien Nocera 	u8 config[GOODIX_CONFIG_MAX_LENGTH];
593fafef982SMarcin Niestroj 	int x_max, y_max;
594ca96ea86SBastien Nocera 	int error;
595ca96ea86SBastien Nocera 
59625309004SMarcin Niestroj 	error = goodix_i2c_read(ts->client, ts->chip->config_addr,
59725309004SMarcin Niestroj 				config, ts->chip->config_len);
598ca96ea86SBastien Nocera 	if (error) {
599fafef982SMarcin Niestroj 		dev_warn(&ts->client->dev, "Error reading config: %d\n",
600ca96ea86SBastien Nocera 			 error);
601ca96ea86SBastien Nocera 		ts->int_trigger_type = GOODIX_INT_TRIGGER;
602a7ac7c95SAleksei Mamlin 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
603ca96ea86SBastien Nocera 		return;
604ca96ea86SBastien Nocera 	}
605ca96ea86SBastien Nocera 
606a7ac7c95SAleksei Mamlin 	ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
607a7ac7c95SAleksei Mamlin 	ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
6088b5a359cSBastien Nocera 
609fafef982SMarcin Niestroj 	x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
610fafef982SMarcin Niestroj 	y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
611fafef982SMarcin Niestroj 	if (x_max && y_max) {
612fafef982SMarcin Niestroj 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
613fafef982SMarcin Niestroj 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
614ca96ea86SBastien Nocera 	}
61557c80e8eSKarsten Merker }
616ca96ea86SBastien Nocera 
617ca96ea86SBastien Nocera /**
618ca96ea86SBastien Nocera  * goodix_read_version - Read goodix touchscreen version
619ca96ea86SBastien Nocera  *
62068caf858SIrina Tirdea  * @ts: our goodix_ts_data pointer
621ca96ea86SBastien Nocera  */
62268caf858SIrina Tirdea static int goodix_read_version(struct goodix_ts_data *ts)
623ca96ea86SBastien Nocera {
624ca96ea86SBastien Nocera 	int error;
625ca96ea86SBastien Nocera 	u8 buf[6];
626e70b0307SIrina Tirdea 	char id_str[5];
627ca96ea86SBastien Nocera 
62868caf858SIrina Tirdea 	error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
629ca96ea86SBastien Nocera 	if (error) {
63068caf858SIrina Tirdea 		dev_err(&ts->client->dev, "read version failed: %d\n", error);
631ca96ea86SBastien Nocera 		return error;
632ca96ea86SBastien Nocera 	}
633ca96ea86SBastien Nocera 
634e70b0307SIrina Tirdea 	memcpy(id_str, buf, 4);
635e70b0307SIrina Tirdea 	id_str[4] = 0;
63668caf858SIrina Tirdea 	if (kstrtou16(id_str, 10, &ts->id))
63768caf858SIrina Tirdea 		ts->id = 0x1001;
638e70b0307SIrina Tirdea 
63968caf858SIrina Tirdea 	ts->version = get_unaligned_le16(&buf[4]);
640ca96ea86SBastien Nocera 
64168caf858SIrina Tirdea 	dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
64268caf858SIrina Tirdea 		 ts->version);
643ca96ea86SBastien Nocera 
644ca96ea86SBastien Nocera 	return 0;
645ca96ea86SBastien Nocera }
646ca96ea86SBastien Nocera 
647ca96ea86SBastien Nocera /**
648ca96ea86SBastien Nocera  * goodix_i2c_test - I2C test function to check if the device answers.
649ca96ea86SBastien Nocera  *
650ca96ea86SBastien Nocera  * @client: the i2c client
651ca96ea86SBastien Nocera  */
652ca96ea86SBastien Nocera static int goodix_i2c_test(struct i2c_client *client)
653ca96ea86SBastien Nocera {
654ca96ea86SBastien Nocera 	int retry = 0;
655ca96ea86SBastien Nocera 	int error;
656ca96ea86SBastien Nocera 	u8 test;
657ca96ea86SBastien Nocera 
658ca96ea86SBastien Nocera 	while (retry++ < 2) {
65925309004SMarcin Niestroj 		error = goodix_i2c_read(client, GOODIX_REG_ID,
660ca96ea86SBastien Nocera 					&test, 1);
661ca96ea86SBastien Nocera 		if (!error)
662ca96ea86SBastien Nocera 			return 0;
663ca96ea86SBastien Nocera 
664ca96ea86SBastien Nocera 		dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
665ca96ea86SBastien Nocera 			retry, error);
666ca96ea86SBastien Nocera 		msleep(20);
667ca96ea86SBastien Nocera 	}
668ca96ea86SBastien Nocera 
669ca96ea86SBastien Nocera 	return error;
670ca96ea86SBastien Nocera }
671ca96ea86SBastien Nocera 
672ca96ea86SBastien Nocera /**
67368caf858SIrina Tirdea  * goodix_configure_dev - Finish device initialization
67468caf858SIrina Tirdea  *
67568caf858SIrina Tirdea  * @ts: our goodix_ts_data pointer
67668caf858SIrina Tirdea  *
67768caf858SIrina Tirdea  * Must be called from probe to finish initialization of the device.
67868caf858SIrina Tirdea  * Contains the common initialization code for both devices that
67968caf858SIrina Tirdea  * declare gpio pins and devices that do not. It is either called
68068caf858SIrina Tirdea  * directly from probe or from request_firmware_wait callback.
68168caf858SIrina Tirdea  */
68268caf858SIrina Tirdea static int goodix_configure_dev(struct goodix_ts_data *ts)
68368caf858SIrina Tirdea {
68468caf858SIrina Tirdea 	int error;
68568caf858SIrina Tirdea 
686fafef982SMarcin Niestroj 	ts->int_trigger_type = GOODIX_INT_TRIGGER;
687fafef982SMarcin Niestroj 	ts->max_touch_num = GOODIX_MAX_CONTACTS;
688ad48cf5eSKarsten Merker 
689fafef982SMarcin Niestroj 	ts->input_dev = devm_input_allocate_device(&ts->client->dev);
690fafef982SMarcin Niestroj 	if (!ts->input_dev) {
691fafef982SMarcin Niestroj 		dev_err(&ts->client->dev, "Failed to allocate input device.");
692fafef982SMarcin Niestroj 		return -ENOMEM;
693fafef982SMarcin Niestroj 	}
694fafef982SMarcin Niestroj 
695fafef982SMarcin Niestroj 	ts->input_dev->name = "Goodix Capacitive TouchScreen";
696fafef982SMarcin Niestroj 	ts->input_dev->phys = "input/ts";
697fafef982SMarcin Niestroj 	ts->input_dev->id.bustype = BUS_I2C;
698fafef982SMarcin Niestroj 	ts->input_dev->id.vendor = 0x0416;
699fafef982SMarcin Niestroj 	ts->input_dev->id.product = ts->id;
700fafef982SMarcin Niestroj 	ts->input_dev->id.version = ts->version;
701fafef982SMarcin Niestroj 
702fafef982SMarcin Niestroj 	/* Capacitive Windows/Home button on some devices */
703fafef982SMarcin Niestroj 	input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
704fafef982SMarcin Niestroj 
705fafef982SMarcin Niestroj 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
706fafef982SMarcin Niestroj 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
707fafef982SMarcin Niestroj 	input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
708fafef982SMarcin Niestroj 	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
709fafef982SMarcin Niestroj 
710fafef982SMarcin Niestroj 	/* Read configuration and apply touchscreen parameters */
71168caf858SIrina Tirdea 	goodix_read_config(ts);
71268caf858SIrina Tirdea 
713fafef982SMarcin Niestroj 	/* Try overriding touchscreen parameters via device properties */
714fafef982SMarcin Niestroj 	touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
715fafef982SMarcin Niestroj 
716fafef982SMarcin Niestroj 	if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
717c1c00aa5SGuido Günther 		dev_err(&ts->client->dev,
718c1c00aa5SGuido Günther 			"Invalid config (%d, %d, %d), using defaults\n",
719c1c00aa5SGuido Günther 			ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
720fafef982SMarcin Niestroj 		ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
721fafef982SMarcin Niestroj 		ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
722fafef982SMarcin Niestroj 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
723fafef982SMarcin Niestroj 		input_abs_set_max(ts->input_dev,
724fafef982SMarcin Niestroj 				  ABS_MT_POSITION_X, ts->prop.max_x);
725fafef982SMarcin Niestroj 		input_abs_set_max(ts->input_dev,
726fafef982SMarcin Niestroj 				  ABS_MT_POSITION_Y, ts->prop.max_y);
727fafef982SMarcin Niestroj 	}
728fafef982SMarcin Niestroj 
729fafef982SMarcin Niestroj 	if (dmi_check_system(rotated_screen)) {
730fafef982SMarcin Niestroj 		ts->prop.invert_x = true;
731fafef982SMarcin Niestroj 		ts->prop.invert_y = true;
732fafef982SMarcin Niestroj 		dev_dbg(&ts->client->dev,
733fafef982SMarcin Niestroj 			"Applying '180 degrees rotated screen' quirk\n");
734fafef982SMarcin Niestroj 	}
735fafef982SMarcin Niestroj 
736fafef982SMarcin Niestroj 	error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
737fafef982SMarcin Niestroj 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
738fafef982SMarcin Niestroj 	if (error) {
739fafef982SMarcin Niestroj 		dev_err(&ts->client->dev,
740fafef982SMarcin Niestroj 			"Failed to initialize MT slots: %d", error);
74168caf858SIrina Tirdea 		return error;
742fafef982SMarcin Niestroj 	}
743fafef982SMarcin Niestroj 
744fafef982SMarcin Niestroj 	error = input_register_device(ts->input_dev);
745fafef982SMarcin Niestroj 	if (error) {
746fafef982SMarcin Niestroj 		dev_err(&ts->client->dev,
747fafef982SMarcin Niestroj 			"Failed to register input device: %d", error);
748fafef982SMarcin Niestroj 		return error;
749fafef982SMarcin Niestroj 	}
75068caf858SIrina Tirdea 
7515ab09d6aSIrina Tirdea 	ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
7525ab09d6aSIrina Tirdea 	error = goodix_request_irq(ts);
75368caf858SIrina Tirdea 	if (error) {
75468caf858SIrina Tirdea 		dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
75568caf858SIrina Tirdea 		return error;
75668caf858SIrina Tirdea 	}
75768caf858SIrina Tirdea 
75868caf858SIrina Tirdea 	return 0;
75968caf858SIrina Tirdea }
76068caf858SIrina Tirdea 
76168caf858SIrina Tirdea /**
76268caf858SIrina Tirdea  * goodix_config_cb - Callback to finish device init
76368caf858SIrina Tirdea  *
76468caf858SIrina Tirdea  * @ts: our goodix_ts_data pointer
76568caf858SIrina Tirdea  *
76668caf858SIrina Tirdea  * request_firmware_wait callback that finishes
76768caf858SIrina Tirdea  * initialization of the device.
76868caf858SIrina Tirdea  */
76968caf858SIrina Tirdea static void goodix_config_cb(const struct firmware *cfg, void *ctx)
77068caf858SIrina Tirdea {
77168caf858SIrina Tirdea 	struct goodix_ts_data *ts = ctx;
77268caf858SIrina Tirdea 	int error;
77368caf858SIrina Tirdea 
77468caf858SIrina Tirdea 	if (cfg) {
77568caf858SIrina Tirdea 		/* send device configuration to the firmware */
77668caf858SIrina Tirdea 		error = goodix_send_cfg(ts, cfg);
77768caf858SIrina Tirdea 		if (error)
77868caf858SIrina Tirdea 			goto err_release_cfg;
77968caf858SIrina Tirdea 	}
78068caf858SIrina Tirdea 
78168caf858SIrina Tirdea 	goodix_configure_dev(ts);
78268caf858SIrina Tirdea 
78368caf858SIrina Tirdea err_release_cfg:
78468caf858SIrina Tirdea 	release_firmware(cfg);
78568caf858SIrina Tirdea 	complete_all(&ts->firmware_loading_complete);
78668caf858SIrina Tirdea }
78768caf858SIrina Tirdea 
788ae97fb58SJagan Teki static void goodix_disable_regulators(void *arg)
789ae97fb58SJagan Teki {
790ae97fb58SJagan Teki 	struct goodix_ts_data *ts = arg;
791ae97fb58SJagan Teki 
792ae97fb58SJagan Teki 	regulator_disable(ts->vddio);
793ae97fb58SJagan Teki 	regulator_disable(ts->avdd28);
794ae97fb58SJagan Teki }
795ae97fb58SJagan Teki 
796ca96ea86SBastien Nocera static int goodix_ts_probe(struct i2c_client *client,
797ca96ea86SBastien Nocera 			   const struct i2c_device_id *id)
798ca96ea86SBastien Nocera {
799ca96ea86SBastien Nocera 	struct goodix_ts_data *ts;
800ca96ea86SBastien Nocera 	int error;
801ca96ea86SBastien Nocera 
802ca96ea86SBastien Nocera 	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
803ca96ea86SBastien Nocera 
804ca96ea86SBastien Nocera 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
805ca96ea86SBastien Nocera 		dev_err(&client->dev, "I2C check functionality failed.\n");
806ca96ea86SBastien Nocera 		return -ENXIO;
807ca96ea86SBastien Nocera 	}
808ca96ea86SBastien Nocera 
809ca96ea86SBastien Nocera 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
810ca96ea86SBastien Nocera 	if (!ts)
811ca96ea86SBastien Nocera 		return -ENOMEM;
812ca96ea86SBastien Nocera 
813ca96ea86SBastien Nocera 	ts->client = client;
814ca96ea86SBastien Nocera 	i2c_set_clientdata(client, ts);
81568caf858SIrina Tirdea 	init_completion(&ts->firmware_loading_complete);
816ca96ea86SBastien Nocera 
817ec6e1b40SIrina Tirdea 	error = goodix_get_gpio_config(ts);
818ec6e1b40SIrina Tirdea 	if (error)
819ec6e1b40SIrina Tirdea 		return error;
820ec6e1b40SIrina Tirdea 
821ae97fb58SJagan Teki 	/* power up the controller */
822ae97fb58SJagan Teki 	error = regulator_enable(ts->avdd28);
823ae97fb58SJagan Teki 	if (error) {
824ae97fb58SJagan Teki 		dev_err(&client->dev,
825ae97fb58SJagan Teki 			"Failed to enable AVDD28 regulator: %d\n",
826ae97fb58SJagan Teki 			error);
827ae97fb58SJagan Teki 		return error;
828ae97fb58SJagan Teki 	}
829ae97fb58SJagan Teki 
830ae97fb58SJagan Teki 	error = regulator_enable(ts->vddio);
831ae97fb58SJagan Teki 	if (error) {
832ae97fb58SJagan Teki 		dev_err(&client->dev,
833ae97fb58SJagan Teki 			"Failed to enable VDDIO regulator: %d\n",
834ae97fb58SJagan Teki 			error);
835ae97fb58SJagan Teki 		regulator_disable(ts->avdd28);
836ae97fb58SJagan Teki 		return error;
837ae97fb58SJagan Teki 	}
838ae97fb58SJagan Teki 
839ae97fb58SJagan Teki 	error = devm_add_action_or_reset(&client->dev,
840ae97fb58SJagan Teki 					 goodix_disable_regulators, ts);
841ae97fb58SJagan Teki 	if (error)
842ae97fb58SJagan Teki 		return error;
843ae97fb58SJagan Teki 
844ec6e1b40SIrina Tirdea 	if (ts->gpiod_int && ts->gpiod_rst) {
845ec6e1b40SIrina Tirdea 		/* reset the controller */
846ec6e1b40SIrina Tirdea 		error = goodix_reset(ts);
847ec6e1b40SIrina Tirdea 		if (error) {
848ec6e1b40SIrina Tirdea 			dev_err(&client->dev, "Controller reset failed.\n");
849ec6e1b40SIrina Tirdea 			return error;
850ec6e1b40SIrina Tirdea 		}
851ec6e1b40SIrina Tirdea 	}
852ec6e1b40SIrina Tirdea 
853ca96ea86SBastien Nocera 	error = goodix_i2c_test(client);
854ca96ea86SBastien Nocera 	if (error) {
855ca96ea86SBastien Nocera 		dev_err(&client->dev, "I2C communication failure: %d\n", error);
856ca96ea86SBastien Nocera 		return error;
857ca96ea86SBastien Nocera 	}
858ca96ea86SBastien Nocera 
85968caf858SIrina Tirdea 	error = goodix_read_version(ts);
860ca96ea86SBastien Nocera 	if (error) {
861ca96ea86SBastien Nocera 		dev_err(&client->dev, "Read version failed.\n");
862ca96ea86SBastien Nocera 		return error;
863ca96ea86SBastien Nocera 	}
864ca96ea86SBastien Nocera 
86525309004SMarcin Niestroj 	ts->chip = goodix_get_chip_data(ts->id);
866a779fbc6SIrina Tirdea 
86768caf858SIrina Tirdea 	if (ts->gpiod_int && ts->gpiod_rst) {
86868caf858SIrina Tirdea 		/* update device config */
86968caf858SIrina Tirdea 		ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
87068caf858SIrina Tirdea 					      "goodix_%d_cfg.bin", ts->id);
87168caf858SIrina Tirdea 		if (!ts->cfg_name)
87268caf858SIrina Tirdea 			return -ENOMEM;
873ca96ea86SBastien Nocera 
87468caf858SIrina Tirdea 		error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
87568caf858SIrina Tirdea 						&client->dev, GFP_KERNEL, ts,
87668caf858SIrina Tirdea 						goodix_config_cb);
877ca96ea86SBastien Nocera 		if (error) {
87868caf858SIrina Tirdea 			dev_err(&client->dev,
87968caf858SIrina Tirdea 				"Failed to invoke firmware loader: %d\n",
88068caf858SIrina Tirdea 				error);
881ca96ea86SBastien Nocera 			return error;
882ca96ea86SBastien Nocera 		}
883ca96ea86SBastien Nocera 
884ca96ea86SBastien Nocera 		return 0;
88568caf858SIrina Tirdea 	} else {
88668caf858SIrina Tirdea 		error = goodix_configure_dev(ts);
88768caf858SIrina Tirdea 		if (error)
88868caf858SIrina Tirdea 			return error;
88968caf858SIrina Tirdea 	}
89068caf858SIrina Tirdea 
89168caf858SIrina Tirdea 	return 0;
89268caf858SIrina Tirdea }
89368caf858SIrina Tirdea 
89468caf858SIrina Tirdea static int goodix_ts_remove(struct i2c_client *client)
89568caf858SIrina Tirdea {
89668caf858SIrina Tirdea 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
89768caf858SIrina Tirdea 
89868caf858SIrina Tirdea 	if (ts->gpiod_int && ts->gpiod_rst)
89968caf858SIrina Tirdea 		wait_for_completion(&ts->firmware_loading_complete);
90068caf858SIrina Tirdea 
90168caf858SIrina Tirdea 	return 0;
902ca96ea86SBastien Nocera }
903ca96ea86SBastien Nocera 
9045ab09d6aSIrina Tirdea static int __maybe_unused goodix_suspend(struct device *dev)
9055ab09d6aSIrina Tirdea {
9065ab09d6aSIrina Tirdea 	struct i2c_client *client = to_i2c_client(dev);
9075ab09d6aSIrina Tirdea 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
9085ab09d6aSIrina Tirdea 	int error;
9095ab09d6aSIrina Tirdea 
9105ab09d6aSIrina Tirdea 	/* We need gpio pins to suspend/resume */
911faec44b6SHans de Goede 	if (!ts->gpiod_int || !ts->gpiod_rst) {
912faec44b6SHans de Goede 		disable_irq(client->irq);
9135ab09d6aSIrina Tirdea 		return 0;
914faec44b6SHans de Goede 	}
9155ab09d6aSIrina Tirdea 
9165ab09d6aSIrina Tirdea 	wait_for_completion(&ts->firmware_loading_complete);
9175ab09d6aSIrina Tirdea 
9185ab09d6aSIrina Tirdea 	/* Free IRQ as IRQ pin is used as output in the suspend sequence */
9195ab09d6aSIrina Tirdea 	goodix_free_irq(ts);
9205ab09d6aSIrina Tirdea 
9215ab09d6aSIrina Tirdea 	/* Output LOW on the INT pin for 5 ms */
9225ab09d6aSIrina Tirdea 	error = gpiod_direction_output(ts->gpiod_int, 0);
9235ab09d6aSIrina Tirdea 	if (error) {
9245ab09d6aSIrina Tirdea 		goodix_request_irq(ts);
9255ab09d6aSIrina Tirdea 		return error;
9265ab09d6aSIrina Tirdea 	}
9275ab09d6aSIrina Tirdea 
9285ab09d6aSIrina Tirdea 	usleep_range(5000, 6000);
9295ab09d6aSIrina Tirdea 
9305ab09d6aSIrina Tirdea 	error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
9315ab09d6aSIrina Tirdea 				    GOODIX_CMD_SCREEN_OFF);
9325ab09d6aSIrina Tirdea 	if (error) {
9335ab09d6aSIrina Tirdea 		dev_err(&ts->client->dev, "Screen off command failed\n");
9345ab09d6aSIrina Tirdea 		gpiod_direction_input(ts->gpiod_int);
9355ab09d6aSIrina Tirdea 		goodix_request_irq(ts);
9365ab09d6aSIrina Tirdea 		return -EAGAIN;
9375ab09d6aSIrina Tirdea 	}
9385ab09d6aSIrina Tirdea 
9395ab09d6aSIrina Tirdea 	/*
9405ab09d6aSIrina Tirdea 	 * The datasheet specifies that the interval between sending screen-off
9415ab09d6aSIrina Tirdea 	 * command and wake-up should be longer than 58 ms. To avoid waking up
9425ab09d6aSIrina Tirdea 	 * sooner, delay 58ms here.
9435ab09d6aSIrina Tirdea 	 */
9445ab09d6aSIrina Tirdea 	msleep(58);
9455ab09d6aSIrina Tirdea 	return 0;
9465ab09d6aSIrina Tirdea }
9475ab09d6aSIrina Tirdea 
9485ab09d6aSIrina Tirdea static int __maybe_unused goodix_resume(struct device *dev)
9495ab09d6aSIrina Tirdea {
9505ab09d6aSIrina Tirdea 	struct i2c_client *client = to_i2c_client(dev);
9515ab09d6aSIrina Tirdea 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
9525ab09d6aSIrina Tirdea 	int error;
9535ab09d6aSIrina Tirdea 
954faec44b6SHans de Goede 	if (!ts->gpiod_int || !ts->gpiod_rst) {
955faec44b6SHans de Goede 		enable_irq(client->irq);
9565ab09d6aSIrina Tirdea 		return 0;
957faec44b6SHans de Goede 	}
9585ab09d6aSIrina Tirdea 
9595ab09d6aSIrina Tirdea 	/*
9605ab09d6aSIrina Tirdea 	 * Exit sleep mode by outputting HIGH level to INT pin
9615ab09d6aSIrina Tirdea 	 * for 2ms~5ms.
9625ab09d6aSIrina Tirdea 	 */
9635ab09d6aSIrina Tirdea 	error = gpiod_direction_output(ts->gpiod_int, 1);
9645ab09d6aSIrina Tirdea 	if (error)
9655ab09d6aSIrina Tirdea 		return error;
9665ab09d6aSIrina Tirdea 
9675ab09d6aSIrina Tirdea 	usleep_range(2000, 5000);
9685ab09d6aSIrina Tirdea 
9695ab09d6aSIrina Tirdea 	error = goodix_int_sync(ts);
9705ab09d6aSIrina Tirdea 	if (error)
9715ab09d6aSIrina Tirdea 		return error;
9725ab09d6aSIrina Tirdea 
9735ab09d6aSIrina Tirdea 	error = goodix_request_irq(ts);
9745ab09d6aSIrina Tirdea 	if (error)
9755ab09d6aSIrina Tirdea 		return error;
9765ab09d6aSIrina Tirdea 
9775ab09d6aSIrina Tirdea 	return 0;
9785ab09d6aSIrina Tirdea }
9795ab09d6aSIrina Tirdea 
9805ab09d6aSIrina Tirdea static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
9815ab09d6aSIrina Tirdea 
982ca96ea86SBastien Nocera static const struct i2c_device_id goodix_ts_id[] = {
983ca96ea86SBastien Nocera 	{ "GDIX1001:00", 0 },
984ca96ea86SBastien Nocera 	{ }
985ca96ea86SBastien Nocera };
9862e9e910eSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
987ca96ea86SBastien Nocera 
988771d8f1bSAleksei Mamlin #ifdef CONFIG_ACPI
989ca96ea86SBastien Nocera static const struct acpi_device_id goodix_acpi_match[] = {
990ca96ea86SBastien Nocera 	{ "GDIX1001", 0 },
9915ca4d1aeSEthan Lee 	{ "GDIX1002", 0 },
992ca96ea86SBastien Nocera 	{ }
993ca96ea86SBastien Nocera };
994ca96ea86SBastien Nocera MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
995771d8f1bSAleksei Mamlin #endif
996771d8f1bSAleksei Mamlin 
997771d8f1bSAleksei Mamlin #ifdef CONFIG_OF
998771d8f1bSAleksei Mamlin static const struct of_device_id goodix_of_match[] = {
99925309004SMarcin Niestroj 	{ .compatible = "goodix,gt1151" },
100084ef1b33SGuido Günther 	{ .compatible = "goodix,gt5688" },
1001771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt911" },
1002771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt9110" },
1003771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt912" },
1004771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt927" },
1005771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt9271" },
1006771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt928" },
1007771d8f1bSAleksei Mamlin 	{ .compatible = "goodix,gt967" },
1008771d8f1bSAleksei Mamlin 	{ }
1009771d8f1bSAleksei Mamlin };
1010771d8f1bSAleksei Mamlin MODULE_DEVICE_TABLE(of, goodix_of_match);
1011771d8f1bSAleksei Mamlin #endif
1012ca96ea86SBastien Nocera 
1013ca96ea86SBastien Nocera static struct i2c_driver goodix_ts_driver = {
1014ca96ea86SBastien Nocera 	.probe = goodix_ts_probe,
101568caf858SIrina Tirdea 	.remove = goodix_ts_remove,
1016ca96ea86SBastien Nocera 	.id_table = goodix_ts_id,
1017ca96ea86SBastien Nocera 	.driver = {
1018ca96ea86SBastien Nocera 		.name = "Goodix-TS",
1019771d8f1bSAleksei Mamlin 		.acpi_match_table = ACPI_PTR(goodix_acpi_match),
1020771d8f1bSAleksei Mamlin 		.of_match_table = of_match_ptr(goodix_of_match),
10215ab09d6aSIrina Tirdea 		.pm = &goodix_pm_ops,
1022ca96ea86SBastien Nocera 	},
1023ca96ea86SBastien Nocera };
1024ca96ea86SBastien Nocera module_i2c_driver(goodix_ts_driver);
1025ca96ea86SBastien Nocera 
1026ca96ea86SBastien Nocera MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1027ca96ea86SBastien Nocera MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1028ca96ea86SBastien Nocera MODULE_DESCRIPTION("Goodix touchscreen driver");
1029ca96ea86SBastien Nocera MODULE_LICENSE("GPL v2");
1030