1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Driver for Goodix Touchscreens
4  *
5  *  Copyright (c) 2014 Red Hat Inc.
6  *  Copyright (c) 2015 K. Merker <merker@debian.org>
7  *
8  *  This code is based on gt9xx.c authored by andrew@goodix.com:
9  *
10  *  2010 - 2012 Goodix Technology.
11  */
12 
13 
14 #include <linux/kernel.h>
15 #include <linux/dmi.h>
16 #include <linux/firmware.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
29 #include <linux/of.h>
30 #include <asm/unaligned.h>
31 
32 #define GOODIX_GPIO_INT_NAME		"irq"
33 #define GOODIX_GPIO_RST_NAME		"reset"
34 
35 #define GOODIX_MAX_HEIGHT		4096
36 #define GOODIX_MAX_WIDTH		4096
37 #define GOODIX_INT_TRIGGER		1
38 #define GOODIX_CONTACT_SIZE		8
39 #define GOODIX_MAX_CONTACT_SIZE		9
40 #define GOODIX_MAX_CONTACTS		10
41 #define GOODIX_MAX_KEYS			7
42 
43 #define GOODIX_CONFIG_MIN_LENGTH	186
44 #define GOODIX_CONFIG_911_LENGTH	186
45 #define GOODIX_CONFIG_967_LENGTH	228
46 #define GOODIX_CONFIG_GT9X_LENGTH	240
47 #define GOODIX_CONFIG_MAX_LENGTH	240
48 
49 /* Register defines */
50 #define GOODIX_REG_COMMAND		0x8040
51 #define GOODIX_CMD_SCREEN_OFF		0x05
52 
53 #define GOODIX_READ_COOR_ADDR		0x814E
54 #define GOODIX_GT1X_REG_CONFIG_DATA	0x8050
55 #define GOODIX_GT9X_REG_CONFIG_DATA	0x8047
56 #define GOODIX_REG_ID			0x8140
57 
58 #define GOODIX_BUFFER_STATUS_READY	BIT(7)
59 #define GOODIX_HAVE_KEY			BIT(4)
60 #define GOODIX_BUFFER_STATUS_TIMEOUT	20
61 
62 #define RESOLUTION_LOC		1
63 #define MAX_CONTACTS_LOC	5
64 #define TRIGGER_LOC		6
65 
66 /* Our special handling for GPIO accesses through ACPI is x86 specific */
67 #if defined CONFIG_X86 && defined CONFIG_ACPI
68 #define ACPI_GPIO_SUPPORT
69 #endif
70 
71 struct goodix_ts_data;
72 
73 enum goodix_irq_pin_access_method {
74 	IRQ_PIN_ACCESS_NONE,
75 	IRQ_PIN_ACCESS_GPIO,
76 	IRQ_PIN_ACCESS_ACPI_GPIO,
77 	IRQ_PIN_ACCESS_ACPI_METHOD,
78 };
79 
80 struct goodix_chip_data {
81 	u16 config_addr;
82 	int config_len;
83 	int (*check_config)(struct goodix_ts_data *ts, const u8 *cfg, int len);
84 	void (*calc_config_checksum)(struct goodix_ts_data *ts);
85 };
86 
87 struct goodix_chip_id {
88 	const char *id;
89 	const struct goodix_chip_data *data;
90 };
91 
92 #define GOODIX_ID_MAX_LEN	4
93 
94 struct goodix_ts_data {
95 	struct i2c_client *client;
96 	struct input_dev *input_dev;
97 	const struct goodix_chip_data *chip;
98 	struct touchscreen_properties prop;
99 	unsigned int max_touch_num;
100 	unsigned int int_trigger_type;
101 	struct regulator *avdd28;
102 	struct regulator *vddio;
103 	struct gpio_desc *gpiod_int;
104 	struct gpio_desc *gpiod_rst;
105 	int gpio_count;
106 	int gpio_int_idx;
107 	char id[GOODIX_ID_MAX_LEN + 1];
108 	u16 version;
109 	const char *cfg_name;
110 	bool reset_controller_at_probe;
111 	bool load_cfg_from_disk;
112 	struct completion firmware_loading_complete;
113 	unsigned long irq_flags;
114 	enum goodix_irq_pin_access_method irq_pin_access_method;
115 	unsigned int contact_size;
116 	u8 config[GOODIX_CONFIG_MAX_LENGTH];
117 	unsigned short keymap[GOODIX_MAX_KEYS];
118 };
119 
120 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
121 			      const u8 *cfg, int len);
122 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
123 			       const u8 *cfg, int len);
124 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
125 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
126 
127 static const struct goodix_chip_data gt1x_chip_data = {
128 	.config_addr		= GOODIX_GT1X_REG_CONFIG_DATA,
129 	.config_len		= GOODIX_CONFIG_GT9X_LENGTH,
130 	.check_config		= goodix_check_cfg_16,
131 	.calc_config_checksum	= goodix_calc_cfg_checksum_16,
132 };
133 
134 static const struct goodix_chip_data gt911_chip_data = {
135 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
136 	.config_len		= GOODIX_CONFIG_911_LENGTH,
137 	.check_config		= goodix_check_cfg_8,
138 	.calc_config_checksum	= goodix_calc_cfg_checksum_8,
139 };
140 
141 static const struct goodix_chip_data gt967_chip_data = {
142 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
143 	.config_len		= GOODIX_CONFIG_967_LENGTH,
144 	.check_config		= goodix_check_cfg_8,
145 	.calc_config_checksum	= goodix_calc_cfg_checksum_8,
146 };
147 
148 static const struct goodix_chip_data gt9x_chip_data = {
149 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
150 	.config_len		= GOODIX_CONFIG_GT9X_LENGTH,
151 	.check_config		= goodix_check_cfg_8,
152 	.calc_config_checksum	= goodix_calc_cfg_checksum_8,
153 };
154 
155 static const struct goodix_chip_id goodix_chip_ids[] = {
156 	{ .id = "1151", .data = &gt1x_chip_data },
157 	{ .id = "5663", .data = &gt1x_chip_data },
158 	{ .id = "5688", .data = &gt1x_chip_data },
159 	{ .id = "917S", .data = &gt1x_chip_data },
160 	{ .id = "9286", .data = &gt1x_chip_data },
161 
162 	{ .id = "911", .data = &gt911_chip_data },
163 	{ .id = "9271", .data = &gt911_chip_data },
164 	{ .id = "9110", .data = &gt911_chip_data },
165 	{ .id = "927", .data = &gt911_chip_data },
166 	{ .id = "928", .data = &gt911_chip_data },
167 
168 	{ .id = "912", .data = &gt967_chip_data },
169 	{ .id = "9147", .data = &gt967_chip_data },
170 	{ .id = "967", .data = &gt967_chip_data },
171 	{ }
172 };
173 
174 static const unsigned long goodix_irq_flags[] = {
175 	IRQ_TYPE_EDGE_RISING,
176 	IRQ_TYPE_EDGE_FALLING,
177 	IRQ_TYPE_LEVEL_LOW,
178 	IRQ_TYPE_LEVEL_HIGH,
179 };
180 
181 static const struct dmi_system_id nine_bytes_report[] = {
182 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
183 	{
184 		.ident = "Lenovo YogaBook",
185 		/* YB1-X91L/F and YB1-X90L/F */
186 		.matches = {
187 			DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9")
188 		}
189 	},
190 #endif
191 	{}
192 };
193 
194 /*
195  * Those tablets have their x coordinate inverted
196  */
197 static const struct dmi_system_id inverted_x_screen[] = {
198 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
199 	{
200 		.ident = "Cube I15-TC",
201 		.matches = {
202 			DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
203 			DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
204 		},
205 	},
206 #endif
207 	{}
208 };
209 
210 /**
211  * goodix_i2c_read - read data from a register of the i2c slave device.
212  *
213  * @client: i2c device.
214  * @reg: the register to read from.
215  * @buf: raw write data buffer.
216  * @len: length of the buffer to write
217  */
218 static int goodix_i2c_read(struct i2c_client *client,
219 			   u16 reg, u8 *buf, int len)
220 {
221 	struct i2c_msg msgs[2];
222 	__be16 wbuf = cpu_to_be16(reg);
223 	int ret;
224 
225 	msgs[0].flags = 0;
226 	msgs[0].addr  = client->addr;
227 	msgs[0].len   = 2;
228 	msgs[0].buf   = (u8 *)&wbuf;
229 
230 	msgs[1].flags = I2C_M_RD;
231 	msgs[1].addr  = client->addr;
232 	msgs[1].len   = len;
233 	msgs[1].buf   = buf;
234 
235 	ret = i2c_transfer(client->adapter, msgs, 2);
236 	return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
237 }
238 
239 /**
240  * goodix_i2c_write - write data to a register of the i2c slave device.
241  *
242  * @client: i2c device.
243  * @reg: the register to write to.
244  * @buf: raw data buffer to write.
245  * @len: length of the buffer to write
246  */
247 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
248 			    unsigned len)
249 {
250 	u8 *addr_buf;
251 	struct i2c_msg msg;
252 	int ret;
253 
254 	addr_buf = kmalloc(len + 2, GFP_KERNEL);
255 	if (!addr_buf)
256 		return -ENOMEM;
257 
258 	addr_buf[0] = reg >> 8;
259 	addr_buf[1] = reg & 0xFF;
260 	memcpy(&addr_buf[2], buf, len);
261 
262 	msg.flags = 0;
263 	msg.addr = client->addr;
264 	msg.buf = addr_buf;
265 	msg.len = len + 2;
266 
267 	ret = i2c_transfer(client->adapter, &msg, 1);
268 	kfree(addr_buf);
269 	return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
270 }
271 
272 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
273 {
274 	return goodix_i2c_write(client, reg, &value, sizeof(value));
275 }
276 
277 static const struct goodix_chip_data *goodix_get_chip_data(const char *id)
278 {
279 	unsigned int i;
280 
281 	for (i = 0; goodix_chip_ids[i].id; i++) {
282 		if (!strcmp(goodix_chip_ids[i].id, id))
283 			return goodix_chip_ids[i].data;
284 	}
285 
286 	return &gt9x_chip_data;
287 }
288 
289 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
290 {
291 	unsigned long max_timeout;
292 	int touch_num;
293 	int error;
294 	u16 addr = GOODIX_READ_COOR_ADDR;
295 	/*
296 	 * We are going to read 1-byte header,
297 	 * ts->contact_size * max(1, touch_num) bytes of coordinates
298 	 * and 1-byte footer which contains the touch-key code.
299 	 */
300 	const int header_contact_keycode_size = 1 + ts->contact_size + 1;
301 
302 	/*
303 	 * The 'buffer status' bit, which indicates that the data is valid, is
304 	 * not set as soon as the interrupt is raised, but slightly after.
305 	 * This takes around 10 ms to happen, so we poll for 20 ms.
306 	 */
307 	max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
308 	do {
309 		error = goodix_i2c_read(ts->client, addr, data,
310 					header_contact_keycode_size);
311 		if (error) {
312 			dev_err(&ts->client->dev, "I2C transfer error: %d\n",
313 					error);
314 			return error;
315 		}
316 
317 		if (data[0] & GOODIX_BUFFER_STATUS_READY) {
318 			touch_num = data[0] & 0x0f;
319 			if (touch_num > ts->max_touch_num)
320 				return -EPROTO;
321 
322 			if (touch_num > 1) {
323 				addr += header_contact_keycode_size;
324 				data += header_contact_keycode_size;
325 				error = goodix_i2c_read(ts->client,
326 						addr, data,
327 						ts->contact_size *
328 							(touch_num - 1));
329 				if (error)
330 					return error;
331 			}
332 
333 			return touch_num;
334 		}
335 
336 		usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
337 	} while (time_before(jiffies, max_timeout));
338 
339 	/*
340 	 * The Goodix panel will send spurious interrupts after a
341 	 * 'finger up' event, which will always cause a timeout.
342 	 */
343 	return -ENOMSG;
344 }
345 
346 static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
347 {
348 	int id = coor_data[0] & 0x0F;
349 	int input_x = get_unaligned_le16(&coor_data[1]);
350 	int input_y = get_unaligned_le16(&coor_data[3]);
351 	int input_w = get_unaligned_le16(&coor_data[5]);
352 
353 	input_mt_slot(ts->input_dev, id);
354 	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
355 	touchscreen_report_pos(ts->input_dev, &ts->prop,
356 			       input_x, input_y, true);
357 	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
358 	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
359 }
360 
361 static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
362 {
363 	int id = coor_data[1] & 0x0F;
364 	int input_x = get_unaligned_le16(&coor_data[3]);
365 	int input_y = get_unaligned_le16(&coor_data[5]);
366 	int input_w = get_unaligned_le16(&coor_data[7]);
367 
368 	input_mt_slot(ts->input_dev, id);
369 	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
370 	touchscreen_report_pos(ts->input_dev, &ts->prop,
371 			       input_x, input_y, true);
372 	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
373 	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
374 }
375 
376 static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
377 {
378 	int touch_num;
379 	u8 key_value;
380 	int i;
381 
382 	if (data[0] & GOODIX_HAVE_KEY) {
383 		touch_num = data[0] & 0x0f;
384 		key_value = data[1 + ts->contact_size * touch_num];
385 		for (i = 0; i < GOODIX_MAX_KEYS; i++)
386 			if (key_value & BIT(i))
387 				input_report_key(ts->input_dev,
388 						 ts->keymap[i], 1);
389 	} else {
390 		for (i = 0; i < GOODIX_MAX_KEYS; i++)
391 			input_report_key(ts->input_dev, ts->keymap[i], 0);
392 	}
393 }
394 
395 /**
396  * goodix_process_events - Process incoming events
397  *
398  * @ts: our goodix_ts_data pointer
399  *
400  * Called when the IRQ is triggered. Read the current device state, and push
401  * the input events to the user space.
402  */
403 static void goodix_process_events(struct goodix_ts_data *ts)
404 {
405 	u8  point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
406 	int touch_num;
407 	int i;
408 
409 	touch_num = goodix_ts_read_input_report(ts, point_data);
410 	if (touch_num < 0)
411 		return;
412 
413 	goodix_ts_report_key(ts, point_data);
414 
415 	for (i = 0; i < touch_num; i++)
416 		if (ts->contact_size == 9)
417 			goodix_ts_report_touch_9b(ts,
418 				&point_data[1 + ts->contact_size * i]);
419 		else
420 			goodix_ts_report_touch_8b(ts,
421 				&point_data[1 + ts->contact_size * i]);
422 
423 	input_mt_sync_frame(ts->input_dev);
424 	input_sync(ts->input_dev);
425 }
426 
427 /**
428  * goodix_ts_irq_handler - The IRQ handler
429  *
430  * @irq: interrupt number.
431  * @dev_id: private data pointer.
432  */
433 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
434 {
435 	struct goodix_ts_data *ts = dev_id;
436 
437 	goodix_process_events(ts);
438 
439 	if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
440 		dev_err(&ts->client->dev, "I2C write end_cmd error\n");
441 
442 	return IRQ_HANDLED;
443 }
444 
445 static void goodix_free_irq(struct goodix_ts_data *ts)
446 {
447 	devm_free_irq(&ts->client->dev, ts->client->irq, ts);
448 }
449 
450 static int goodix_request_irq(struct goodix_ts_data *ts)
451 {
452 	return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
453 					 NULL, goodix_ts_irq_handler,
454 					 ts->irq_flags, ts->client->name, ts);
455 }
456 
457 static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
458 {
459 	int i, raw_cfg_len = len - 2;
460 	u8 check_sum = 0;
461 
462 	for (i = 0; i < raw_cfg_len; i++)
463 		check_sum += cfg[i];
464 	check_sum = (~check_sum) + 1;
465 	if (check_sum != cfg[raw_cfg_len]) {
466 		dev_err(&ts->client->dev,
467 			"The checksum of the config fw is not correct");
468 		return -EINVAL;
469 	}
470 
471 	if (cfg[raw_cfg_len + 1] != 1) {
472 		dev_err(&ts->client->dev,
473 			"Config fw must have Config_Fresh register set");
474 		return -EINVAL;
475 	}
476 
477 	return 0;
478 }
479 
480 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
481 {
482 	int i, raw_cfg_len = ts->chip->config_len - 2;
483 	u8 check_sum = 0;
484 
485 	for (i = 0; i < raw_cfg_len; i++)
486 		check_sum += ts->config[i];
487 	check_sum = (~check_sum) + 1;
488 
489 	ts->config[raw_cfg_len] = check_sum;
490 	ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
491 }
492 
493 static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
494 			       int len)
495 {
496 	int i, raw_cfg_len = len - 3;
497 	u16 check_sum = 0;
498 
499 	for (i = 0; i < raw_cfg_len; i += 2)
500 		check_sum += get_unaligned_be16(&cfg[i]);
501 	check_sum = (~check_sum) + 1;
502 	if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
503 		dev_err(&ts->client->dev,
504 			"The checksum of the config fw is not correct");
505 		return -EINVAL;
506 	}
507 
508 	if (cfg[raw_cfg_len + 2] != 1) {
509 		dev_err(&ts->client->dev,
510 			"Config fw must have Config_Fresh register set");
511 		return -EINVAL;
512 	}
513 
514 	return 0;
515 }
516 
517 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
518 {
519 	int i, raw_cfg_len = ts->chip->config_len - 3;
520 	u16 check_sum = 0;
521 
522 	for (i = 0; i < raw_cfg_len; i += 2)
523 		check_sum += get_unaligned_be16(&ts->config[i]);
524 	check_sum = (~check_sum) + 1;
525 
526 	put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
527 	ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
528 }
529 
530 /**
531  * goodix_check_cfg - Checks if config fw is valid
532  *
533  * @ts: goodix_ts_data pointer
534  * @cfg: firmware config data
535  */
536 static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
537 {
538 	if (len < GOODIX_CONFIG_MIN_LENGTH ||
539 	    len > GOODIX_CONFIG_MAX_LENGTH) {
540 		dev_err(&ts->client->dev,
541 			"The length of the config fw is not correct");
542 		return -EINVAL;
543 	}
544 
545 	return ts->chip->check_config(ts, cfg, len);
546 }
547 
548 /**
549  * goodix_send_cfg - Write fw config to device
550  *
551  * @ts: goodix_ts_data pointer
552  * @cfg: config firmware to write to device
553  */
554 static int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
555 {
556 	int error;
557 
558 	error = goodix_check_cfg(ts, cfg, len);
559 	if (error)
560 		return error;
561 
562 	error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
563 	if (error) {
564 		dev_err(&ts->client->dev, "Failed to write config data: %d",
565 			error);
566 		return error;
567 	}
568 	dev_dbg(&ts->client->dev, "Config sent successfully.");
569 
570 	/* Let the firmware reconfigure itself, so sleep for 10ms */
571 	usleep_range(10000, 11000);
572 
573 	return 0;
574 }
575 
576 #ifdef ACPI_GPIO_SUPPORT
577 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
578 {
579 	acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
580 	acpi_status status;
581 
582 	status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
583 	return ACPI_SUCCESS(status) ? 0 : -EIO;
584 }
585 
586 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
587 {
588 	acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
589 	acpi_status status;
590 
591 	status = acpi_execute_simple_method(handle, "INTO", value);
592 	return ACPI_SUCCESS(status) ? 0 : -EIO;
593 }
594 #else
595 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
596 {
597 	dev_err(&ts->client->dev,
598 		"%s called on device without ACPI support\n", __func__);
599 	return -EINVAL;
600 }
601 
602 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
603 {
604 	dev_err(&ts->client->dev,
605 		"%s called on device without ACPI support\n", __func__);
606 	return -EINVAL;
607 }
608 #endif
609 
610 static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
611 {
612 	switch (ts->irq_pin_access_method) {
613 	case IRQ_PIN_ACCESS_NONE:
614 		dev_err(&ts->client->dev,
615 			"%s called without an irq_pin_access_method set\n",
616 			__func__);
617 		return -EINVAL;
618 	case IRQ_PIN_ACCESS_GPIO:
619 		return gpiod_direction_output(ts->gpiod_int, value);
620 	case IRQ_PIN_ACCESS_ACPI_GPIO:
621 		/*
622 		 * The IRQ pin triggers on a falling edge, so its gets marked
623 		 * as active-low, use output_raw to avoid the value inversion.
624 		 */
625 		return gpiod_direction_output_raw(ts->gpiod_int, value);
626 	case IRQ_PIN_ACCESS_ACPI_METHOD:
627 		return goodix_pin_acpi_output_method(ts, value);
628 	}
629 
630 	return -EINVAL; /* Never reached */
631 }
632 
633 static int goodix_irq_direction_input(struct goodix_ts_data *ts)
634 {
635 	switch (ts->irq_pin_access_method) {
636 	case IRQ_PIN_ACCESS_NONE:
637 		dev_err(&ts->client->dev,
638 			"%s called without an irq_pin_access_method set\n",
639 			__func__);
640 		return -EINVAL;
641 	case IRQ_PIN_ACCESS_GPIO:
642 		return gpiod_direction_input(ts->gpiod_int);
643 	case IRQ_PIN_ACCESS_ACPI_GPIO:
644 		return gpiod_direction_input(ts->gpiod_int);
645 	case IRQ_PIN_ACCESS_ACPI_METHOD:
646 		return goodix_pin_acpi_direction_input(ts);
647 	}
648 
649 	return -EINVAL; /* Never reached */
650 }
651 
652 static int goodix_int_sync(struct goodix_ts_data *ts)
653 {
654 	int error;
655 
656 	error = goodix_irq_direction_output(ts, 0);
657 	if (error)
658 		return error;
659 
660 	msleep(50);				/* T5: 50ms */
661 
662 	error = goodix_irq_direction_input(ts);
663 	if (error)
664 		return error;
665 
666 	return 0;
667 }
668 
669 /**
670  * goodix_reset - Reset device during power on
671  *
672  * @ts: goodix_ts_data pointer
673  */
674 static int goodix_reset(struct goodix_ts_data *ts)
675 {
676 	int error;
677 
678 	/* begin select I2C slave addr */
679 	error = gpiod_direction_output(ts->gpiod_rst, 0);
680 	if (error)
681 		return error;
682 
683 	msleep(20);				/* T2: > 10ms */
684 
685 	/* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
686 	error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
687 	if (error)
688 		return error;
689 
690 	usleep_range(100, 2000);		/* T3: > 100us */
691 
692 	error = gpiod_direction_output(ts->gpiod_rst, 1);
693 	if (error)
694 		return error;
695 
696 	usleep_range(6000, 10000);		/* T4: > 5ms */
697 
698 	/* end select I2C slave addr */
699 	error = gpiod_direction_input(ts->gpiod_rst);
700 	if (error)
701 		return error;
702 
703 	error = goodix_int_sync(ts);
704 	if (error)
705 		return error;
706 
707 	return 0;
708 }
709 
710 #ifdef ACPI_GPIO_SUPPORT
711 #include <asm/cpu_device_id.h>
712 #include <asm/intel-family.h>
713 
714 static const struct x86_cpu_id baytrail_cpu_ids[] = {
715 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, X86_FEATURE_ANY, },
716 	{}
717 };
718 
719 static inline bool is_byt(void)
720 {
721 	const struct x86_cpu_id *id = x86_match_cpu(baytrail_cpu_ids);
722 
723 	return !!id;
724 }
725 
726 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
727 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
728 
729 static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
730 	{ GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
731 	{ GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
732 	{ },
733 };
734 
735 static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
736 	{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
737 	{ GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
738 	{ },
739 };
740 
741 static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
742 	{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
743 	{ },
744 };
745 
746 static int goodix_resource(struct acpi_resource *ares, void *data)
747 {
748 	struct goodix_ts_data *ts = data;
749 	struct device *dev = &ts->client->dev;
750 	struct acpi_resource_gpio *gpio;
751 
752 	switch (ares->type) {
753 	case ACPI_RESOURCE_TYPE_GPIO:
754 		gpio = &ares->data.gpio;
755 		if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
756 			if (ts->gpio_int_idx == -1) {
757 				ts->gpio_int_idx = ts->gpio_count;
758 			} else {
759 				dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
760 				ts->gpio_int_idx = -2;
761 			}
762 		}
763 		ts->gpio_count++;
764 		break;
765 	default:
766 		break;
767 	}
768 
769 	return 0;
770 }
771 
772 /*
773  * This function gets called in case we fail to get the irq GPIO directly
774  * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
775  * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
776  * In that case we add our own mapping and then goodix_get_gpio_config()
777  * retries to get the GPIOs based on the added mapping.
778  */
779 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
780 {
781 	const struct acpi_gpio_mapping *gpio_mapping = NULL;
782 	struct device *dev = &ts->client->dev;
783 	LIST_HEAD(resources);
784 	int ret;
785 
786 	ts->gpio_count = 0;
787 	ts->gpio_int_idx = -1;
788 	ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
789 				     goodix_resource, ts);
790 	if (ret < 0) {
791 		dev_err(dev, "Error getting ACPI resources: %d\n", ret);
792 		return ret;
793 	}
794 
795 	acpi_dev_free_resource_list(&resources);
796 
797 	if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) {
798 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
799 		gpio_mapping = acpi_goodix_int_first_gpios;
800 	} else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
801 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
802 		gpio_mapping = acpi_goodix_int_last_gpios;
803 	} else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
804 		   acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
805 		   acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
806 		dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
807 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
808 		gpio_mapping = acpi_goodix_reset_only_gpios;
809 	} else if (is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
810 		dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
811 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
812 		gpio_mapping = acpi_goodix_int_last_gpios;
813 	} else {
814 		dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
815 			 ts->gpio_count, ts->gpio_int_idx);
816 		return -EINVAL;
817 	}
818 
819 	return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
820 }
821 #else
822 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
823 {
824 	return -EINVAL;
825 }
826 #endif /* CONFIG_X86 && CONFIG_ACPI */
827 
828 /**
829  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
830  *
831  * @ts: goodix_ts_data pointer
832  */
833 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
834 {
835 	int error;
836 	struct device *dev;
837 	struct gpio_desc *gpiod;
838 	bool added_acpi_mappings = false;
839 
840 	if (!ts->client)
841 		return -EINVAL;
842 	dev = &ts->client->dev;
843 
844 	ts->avdd28 = devm_regulator_get(dev, "AVDD28");
845 	if (IS_ERR(ts->avdd28)) {
846 		error = PTR_ERR(ts->avdd28);
847 		if (error != -EPROBE_DEFER)
848 			dev_err(dev,
849 				"Failed to get AVDD28 regulator: %d\n", error);
850 		return error;
851 	}
852 
853 	ts->vddio = devm_regulator_get(dev, "VDDIO");
854 	if (IS_ERR(ts->vddio)) {
855 		error = PTR_ERR(ts->vddio);
856 		if (error != -EPROBE_DEFER)
857 			dev_err(dev,
858 				"Failed to get VDDIO regulator: %d\n", error);
859 		return error;
860 	}
861 
862 retry_get_irq_gpio:
863 	/* Get the interrupt GPIO pin number */
864 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
865 	if (IS_ERR(gpiod)) {
866 		error = PTR_ERR(gpiod);
867 		if (error != -EPROBE_DEFER)
868 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
869 				GOODIX_GPIO_INT_NAME, error);
870 		return error;
871 	}
872 	if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
873 		added_acpi_mappings = true;
874 		if (goodix_add_acpi_gpio_mappings(ts) == 0)
875 			goto retry_get_irq_gpio;
876 	}
877 
878 	ts->gpiod_int = gpiod;
879 
880 	/* Get the reset line GPIO pin number */
881 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
882 	if (IS_ERR(gpiod)) {
883 		error = PTR_ERR(gpiod);
884 		if (error != -EPROBE_DEFER)
885 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
886 				GOODIX_GPIO_RST_NAME, error);
887 		return error;
888 	}
889 
890 	ts->gpiod_rst = gpiod;
891 
892 	switch (ts->irq_pin_access_method) {
893 	case IRQ_PIN_ACCESS_ACPI_GPIO:
894 		/*
895 		 * We end up here if goodix_add_acpi_gpio_mappings() has
896 		 * called devm_acpi_dev_add_driver_gpios() because the ACPI
897 		 * tables did not contain name to index mappings.
898 		 * Check that we successfully got both GPIOs after we've
899 		 * added our own acpi_gpio_mapping and if we did not get both
900 		 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
901 		 */
902 		if (!ts->gpiod_int || !ts->gpiod_rst)
903 			ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
904 		break;
905 	case IRQ_PIN_ACCESS_ACPI_METHOD:
906 		if (!ts->gpiod_rst)
907 			ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
908 		break;
909 	default:
910 		if (ts->gpiod_int && ts->gpiod_rst) {
911 			ts->reset_controller_at_probe = true;
912 			ts->load_cfg_from_disk = true;
913 			ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
914 		}
915 	}
916 
917 	return 0;
918 }
919 
920 /**
921  * goodix_read_config - Read the embedded configuration of the panel
922  *
923  * @ts: our goodix_ts_data pointer
924  *
925  * Must be called during probe
926  */
927 static void goodix_read_config(struct goodix_ts_data *ts)
928 {
929 	int x_max, y_max;
930 	int error;
931 
932 	error = goodix_i2c_read(ts->client, ts->chip->config_addr,
933 				ts->config, ts->chip->config_len);
934 	if (error) {
935 		dev_warn(&ts->client->dev, "Error reading config: %d\n",
936 			 error);
937 		ts->int_trigger_type = GOODIX_INT_TRIGGER;
938 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
939 		return;
940 	}
941 
942 	ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
943 	ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
944 
945 	x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
946 	y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
947 	if (x_max && y_max) {
948 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
949 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
950 	}
951 
952 	ts->chip->calc_config_checksum(ts);
953 }
954 
955 /**
956  * goodix_read_version - Read goodix touchscreen version
957  *
958  * @ts: our goodix_ts_data pointer
959  */
960 static int goodix_read_version(struct goodix_ts_data *ts)
961 {
962 	int error;
963 	u8 buf[6];
964 	char id_str[GOODIX_ID_MAX_LEN + 1];
965 
966 	error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
967 	if (error) {
968 		dev_err(&ts->client->dev, "read version failed: %d\n", error);
969 		return error;
970 	}
971 
972 	memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
973 	id_str[GOODIX_ID_MAX_LEN] = 0;
974 	strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
975 
976 	ts->version = get_unaligned_le16(&buf[4]);
977 
978 	dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
979 		 ts->version);
980 
981 	return 0;
982 }
983 
984 /**
985  * goodix_i2c_test - I2C test function to check if the device answers.
986  *
987  * @client: the i2c client
988  */
989 static int goodix_i2c_test(struct i2c_client *client)
990 {
991 	int retry = 0;
992 	int error;
993 	u8 test;
994 
995 	while (retry++ < 2) {
996 		error = goodix_i2c_read(client, GOODIX_REG_ID,
997 					&test, 1);
998 		if (!error)
999 			return 0;
1000 
1001 		dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
1002 			retry, error);
1003 		msleep(20);
1004 	}
1005 
1006 	return error;
1007 }
1008 
1009 /**
1010  * goodix_configure_dev - Finish device initialization
1011  *
1012  * @ts: our goodix_ts_data pointer
1013  *
1014  * Must be called from probe to finish initialization of the device.
1015  * Contains the common initialization code for both devices that
1016  * declare gpio pins and devices that do not. It is either called
1017  * directly from probe or from request_firmware_wait callback.
1018  */
1019 static int goodix_configure_dev(struct goodix_ts_data *ts)
1020 {
1021 	int error;
1022 	int i;
1023 
1024 	ts->int_trigger_type = GOODIX_INT_TRIGGER;
1025 	ts->max_touch_num = GOODIX_MAX_CONTACTS;
1026 
1027 	ts->input_dev = devm_input_allocate_device(&ts->client->dev);
1028 	if (!ts->input_dev) {
1029 		dev_err(&ts->client->dev, "Failed to allocate input device.");
1030 		return -ENOMEM;
1031 	}
1032 
1033 	ts->input_dev->name = "Goodix Capacitive TouchScreen";
1034 	ts->input_dev->phys = "input/ts";
1035 	ts->input_dev->id.bustype = BUS_I2C;
1036 	ts->input_dev->id.vendor = 0x0416;
1037 	if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
1038 		ts->input_dev->id.product = 0x1001;
1039 	ts->input_dev->id.version = ts->version;
1040 
1041 	ts->input_dev->keycode = ts->keymap;
1042 	ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
1043 	ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
1044 
1045 	/* Capacitive Windows/Home button on some devices */
1046 	for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
1047 		if (i == 0)
1048 			ts->keymap[i] = KEY_LEFTMETA;
1049 		else
1050 			ts->keymap[i] = KEY_F1 + (i - 1);
1051 
1052 		input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
1053 	}
1054 
1055 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
1056 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
1057 	input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1058 	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1059 
1060 	/* Read configuration and apply touchscreen parameters */
1061 	goodix_read_config(ts);
1062 
1063 	/* Try overriding touchscreen parameters via device properties */
1064 	touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1065 
1066 	if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1067 		dev_err(&ts->client->dev,
1068 			"Invalid config (%d, %d, %d), using defaults\n",
1069 			ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1070 		ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
1071 		ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
1072 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
1073 		input_abs_set_max(ts->input_dev,
1074 				  ABS_MT_POSITION_X, ts->prop.max_x);
1075 		input_abs_set_max(ts->input_dev,
1076 				  ABS_MT_POSITION_Y, ts->prop.max_y);
1077 	}
1078 
1079 	if (dmi_check_system(nine_bytes_report)) {
1080 		ts->contact_size = 9;
1081 
1082 		dev_dbg(&ts->client->dev,
1083 			"Non-standard 9-bytes report format quirk\n");
1084 	}
1085 
1086 	if (dmi_check_system(inverted_x_screen)) {
1087 		ts->prop.invert_x = true;
1088 		dev_dbg(&ts->client->dev,
1089 			"Applying 'inverted x screen' quirk\n");
1090 	}
1091 
1092 	error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1093 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1094 	if (error) {
1095 		dev_err(&ts->client->dev,
1096 			"Failed to initialize MT slots: %d", error);
1097 		return error;
1098 	}
1099 
1100 	error = input_register_device(ts->input_dev);
1101 	if (error) {
1102 		dev_err(&ts->client->dev,
1103 			"Failed to register input device: %d", error);
1104 		return error;
1105 	}
1106 
1107 	ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1108 	error = goodix_request_irq(ts);
1109 	if (error) {
1110 		dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1111 		return error;
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 /**
1118  * goodix_config_cb - Callback to finish device init
1119  *
1120  * @ts: our goodix_ts_data pointer
1121  *
1122  * request_firmware_wait callback that finishes
1123  * initialization of the device.
1124  */
1125 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1126 {
1127 	struct goodix_ts_data *ts = ctx;
1128 	int error;
1129 
1130 	if (cfg) {
1131 		/* send device configuration to the firmware */
1132 		error = goodix_send_cfg(ts, cfg->data, cfg->size);
1133 		if (error)
1134 			goto err_release_cfg;
1135 	}
1136 
1137 	goodix_configure_dev(ts);
1138 
1139 err_release_cfg:
1140 	release_firmware(cfg);
1141 	complete_all(&ts->firmware_loading_complete);
1142 }
1143 
1144 static void goodix_disable_regulators(void *arg)
1145 {
1146 	struct goodix_ts_data *ts = arg;
1147 
1148 	regulator_disable(ts->vddio);
1149 	regulator_disable(ts->avdd28);
1150 }
1151 
1152 static int goodix_ts_probe(struct i2c_client *client,
1153 			   const struct i2c_device_id *id)
1154 {
1155 	struct goodix_ts_data *ts;
1156 	int error;
1157 
1158 	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1159 
1160 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1161 		dev_err(&client->dev, "I2C check functionality failed.\n");
1162 		return -ENXIO;
1163 	}
1164 
1165 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1166 	if (!ts)
1167 		return -ENOMEM;
1168 
1169 	ts->client = client;
1170 	i2c_set_clientdata(client, ts);
1171 	init_completion(&ts->firmware_loading_complete);
1172 	ts->contact_size = GOODIX_CONTACT_SIZE;
1173 
1174 	error = goodix_get_gpio_config(ts);
1175 	if (error)
1176 		return error;
1177 
1178 	/* power up the controller */
1179 	error = regulator_enable(ts->avdd28);
1180 	if (error) {
1181 		dev_err(&client->dev,
1182 			"Failed to enable AVDD28 regulator: %d\n",
1183 			error);
1184 		return error;
1185 	}
1186 
1187 	error = regulator_enable(ts->vddio);
1188 	if (error) {
1189 		dev_err(&client->dev,
1190 			"Failed to enable VDDIO regulator: %d\n",
1191 			error);
1192 		regulator_disable(ts->avdd28);
1193 		return error;
1194 	}
1195 
1196 	error = devm_add_action_or_reset(&client->dev,
1197 					 goodix_disable_regulators, ts);
1198 	if (error)
1199 		return error;
1200 
1201 reset:
1202 	if (ts->reset_controller_at_probe) {
1203 		/* reset the controller */
1204 		error = goodix_reset(ts);
1205 		if (error) {
1206 			dev_err(&client->dev, "Controller reset failed.\n");
1207 			return error;
1208 		}
1209 	}
1210 
1211 	error = goodix_i2c_test(client);
1212 	if (error) {
1213 		if (!ts->reset_controller_at_probe &&
1214 		    ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1215 			/* Retry after a controller reset */
1216 			ts->reset_controller_at_probe = true;
1217 			goto reset;
1218 		}
1219 		dev_err(&client->dev, "I2C communication failure: %d\n", error);
1220 		return error;
1221 	}
1222 
1223 	error = goodix_read_version(ts);
1224 	if (error) {
1225 		dev_err(&client->dev, "Read version failed.\n");
1226 		return error;
1227 	}
1228 
1229 	ts->chip = goodix_get_chip_data(ts->id);
1230 
1231 	if (ts->load_cfg_from_disk) {
1232 		/* update device config */
1233 		ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
1234 					      "goodix_%s_cfg.bin", ts->id);
1235 		if (!ts->cfg_name)
1236 			return -ENOMEM;
1237 
1238 		error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1239 						&client->dev, GFP_KERNEL, ts,
1240 						goodix_config_cb);
1241 		if (error) {
1242 			dev_err(&client->dev,
1243 				"Failed to invoke firmware loader: %d\n",
1244 				error);
1245 			return error;
1246 		}
1247 
1248 		return 0;
1249 	} else {
1250 		error = goodix_configure_dev(ts);
1251 		if (error)
1252 			return error;
1253 	}
1254 
1255 	return 0;
1256 }
1257 
1258 static int goodix_ts_remove(struct i2c_client *client)
1259 {
1260 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1261 
1262 	if (ts->load_cfg_from_disk)
1263 		wait_for_completion(&ts->firmware_loading_complete);
1264 
1265 	return 0;
1266 }
1267 
1268 static int __maybe_unused goodix_suspend(struct device *dev)
1269 {
1270 	struct i2c_client *client = to_i2c_client(dev);
1271 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1272 	int error;
1273 
1274 	if (ts->load_cfg_from_disk)
1275 		wait_for_completion(&ts->firmware_loading_complete);
1276 
1277 	/* We need gpio pins to suspend/resume */
1278 	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1279 		disable_irq(client->irq);
1280 		return 0;
1281 	}
1282 
1283 	/* Free IRQ as IRQ pin is used as output in the suspend sequence */
1284 	goodix_free_irq(ts);
1285 
1286 	/* Output LOW on the INT pin for 5 ms */
1287 	error = goodix_irq_direction_output(ts, 0);
1288 	if (error) {
1289 		goodix_request_irq(ts);
1290 		return error;
1291 	}
1292 
1293 	usleep_range(5000, 6000);
1294 
1295 	error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1296 				    GOODIX_CMD_SCREEN_OFF);
1297 	if (error) {
1298 		dev_err(&ts->client->dev, "Screen off command failed\n");
1299 		goodix_irq_direction_input(ts);
1300 		goodix_request_irq(ts);
1301 		return -EAGAIN;
1302 	}
1303 
1304 	/*
1305 	 * The datasheet specifies that the interval between sending screen-off
1306 	 * command and wake-up should be longer than 58 ms. To avoid waking up
1307 	 * sooner, delay 58ms here.
1308 	 */
1309 	msleep(58);
1310 	return 0;
1311 }
1312 
1313 static int __maybe_unused goodix_resume(struct device *dev)
1314 {
1315 	struct i2c_client *client = to_i2c_client(dev);
1316 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1317 	u8 config_ver;
1318 	int error;
1319 
1320 	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1321 		enable_irq(client->irq);
1322 		return 0;
1323 	}
1324 
1325 	/*
1326 	 * Exit sleep mode by outputting HIGH level to INT pin
1327 	 * for 2ms~5ms.
1328 	 */
1329 	error = goodix_irq_direction_output(ts, 1);
1330 	if (error)
1331 		return error;
1332 
1333 	usleep_range(2000, 5000);
1334 
1335 	error = goodix_int_sync(ts);
1336 	if (error)
1337 		return error;
1338 
1339 	error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1340 				&config_ver, 1);
1341 	if (error)
1342 		dev_warn(dev, "Error reading config version: %d, resetting controller\n",
1343 			 error);
1344 	else if (config_ver != ts->config[0])
1345 		dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
1346 			 config_ver, ts->config[0]);
1347 
1348 	if (error != 0 || config_ver != ts->config[0]) {
1349 		error = goodix_reset(ts);
1350 		if (error) {
1351 			dev_err(dev, "Controller reset failed.\n");
1352 			return error;
1353 		}
1354 
1355 		error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
1356 		if (error)
1357 			return error;
1358 	}
1359 
1360 	error = goodix_request_irq(ts);
1361 	if (error)
1362 		return error;
1363 
1364 	return 0;
1365 }
1366 
1367 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1368 
1369 static const struct i2c_device_id goodix_ts_id[] = {
1370 	{ "GDIX1001:00", 0 },
1371 	{ }
1372 };
1373 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1374 
1375 #ifdef CONFIG_ACPI
1376 static const struct acpi_device_id goodix_acpi_match[] = {
1377 	{ "GDIX1001", 0 },
1378 	{ "GDIX1002", 0 },
1379 	{ }
1380 };
1381 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1382 #endif
1383 
1384 #ifdef CONFIG_OF
1385 static const struct of_device_id goodix_of_match[] = {
1386 	{ .compatible = "goodix,gt1151" },
1387 	{ .compatible = "goodix,gt5663" },
1388 	{ .compatible = "goodix,gt5688" },
1389 	{ .compatible = "goodix,gt911" },
1390 	{ .compatible = "goodix,gt9110" },
1391 	{ .compatible = "goodix,gt912" },
1392 	{ .compatible = "goodix,gt9147" },
1393 	{ .compatible = "goodix,gt917s" },
1394 	{ .compatible = "goodix,gt927" },
1395 	{ .compatible = "goodix,gt9271" },
1396 	{ .compatible = "goodix,gt928" },
1397 	{ .compatible = "goodix,gt9286" },
1398 	{ .compatible = "goodix,gt967" },
1399 	{ }
1400 };
1401 MODULE_DEVICE_TABLE(of, goodix_of_match);
1402 #endif
1403 
1404 static struct i2c_driver goodix_ts_driver = {
1405 	.probe = goodix_ts_probe,
1406 	.remove = goodix_ts_remove,
1407 	.id_table = goodix_ts_id,
1408 	.driver = {
1409 		.name = "Goodix-TS",
1410 		.acpi_match_table = ACPI_PTR(goodix_acpi_match),
1411 		.of_match_table = of_match_ptr(goodix_of_match),
1412 		.pm = &goodix_pm_ops,
1413 	},
1414 };
1415 module_i2c_driver(goodix_ts_driver);
1416 
1417 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1418 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1419 MODULE_DESCRIPTION("Goodix touchscreen driver");
1420 MODULE_LICENSE("GPL v2");
1421