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