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 		if (data[0] == 0 && ts->firmware_name) {
330 			if (goodix_handle_fw_request(ts))
331 				return 0;
332 		}
333 
334 		usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
335 	} while (time_before(jiffies, max_timeout));
336 
337 	/*
338 	 * The Goodix panel will send spurious interrupts after a
339 	 * 'finger up' event, which will always cause a timeout.
340 	 */
341 	return -ENOMSG;
342 }
343 
344 static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
345 {
346 	int id = coor_data[0] & 0x0F;
347 	int input_x = get_unaligned_le16(&coor_data[1]);
348 	int input_y = get_unaligned_le16(&coor_data[3]);
349 	int input_w = get_unaligned_le16(&coor_data[5]);
350 
351 	input_mt_slot(ts->input_dev, id);
352 	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
353 	touchscreen_report_pos(ts->input_dev, &ts->prop,
354 			       input_x, input_y, true);
355 	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
356 	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
357 }
358 
359 static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
360 {
361 	int id = coor_data[1] & 0x0F;
362 	int input_x = get_unaligned_le16(&coor_data[3]);
363 	int input_y = get_unaligned_le16(&coor_data[5]);
364 	int input_w = get_unaligned_le16(&coor_data[7]);
365 
366 	input_mt_slot(ts->input_dev, id);
367 	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
368 	touchscreen_report_pos(ts->input_dev, &ts->prop,
369 			       input_x, input_y, true);
370 	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
371 	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
372 }
373 
374 static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
375 {
376 	int touch_num;
377 	u8 key_value;
378 	int i;
379 
380 	if (data[0] & GOODIX_HAVE_KEY) {
381 		touch_num = data[0] & 0x0f;
382 		key_value = data[1 + ts->contact_size * touch_num];
383 		for (i = 0; i < GOODIX_MAX_KEYS; i++)
384 			if (key_value & BIT(i))
385 				input_report_key(ts->input_dev,
386 						 ts->keymap[i], 1);
387 	} else {
388 		for (i = 0; i < GOODIX_MAX_KEYS; i++)
389 			input_report_key(ts->input_dev, ts->keymap[i], 0);
390 	}
391 }
392 
393 /**
394  * goodix_process_events - Process incoming events
395  *
396  * @ts: our goodix_ts_data pointer
397  *
398  * Called when the IRQ is triggered. Read the current device state, and push
399  * the input events to the user space.
400  */
401 static void goodix_process_events(struct goodix_ts_data *ts)
402 {
403 	u8  point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
404 	int touch_num;
405 	int i;
406 
407 	touch_num = goodix_ts_read_input_report(ts, point_data);
408 	if (touch_num < 0)
409 		return;
410 
411 	goodix_ts_report_key(ts, point_data);
412 
413 	for (i = 0; i < touch_num; i++)
414 		if (ts->contact_size == 9)
415 			goodix_ts_report_touch_9b(ts,
416 				&point_data[1 + ts->contact_size * i]);
417 		else
418 			goodix_ts_report_touch_8b(ts,
419 				&point_data[1 + ts->contact_size * i]);
420 
421 	input_mt_sync_frame(ts->input_dev);
422 	input_sync(ts->input_dev);
423 }
424 
425 /**
426  * goodix_ts_irq_handler - The IRQ handler
427  *
428  * @irq: interrupt number.
429  * @dev_id: private data pointer.
430  */
431 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
432 {
433 	struct goodix_ts_data *ts = dev_id;
434 
435 	goodix_process_events(ts);
436 	goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
437 
438 	return IRQ_HANDLED;
439 }
440 
441 static void goodix_free_irq(struct goodix_ts_data *ts)
442 {
443 	devm_free_irq(&ts->client->dev, ts->client->irq, ts);
444 }
445 
446 static int goodix_request_irq(struct goodix_ts_data *ts)
447 {
448 	return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
449 					 NULL, goodix_ts_irq_handler,
450 					 ts->irq_flags, ts->client->name, ts);
451 }
452 
453 static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
454 {
455 	int i, raw_cfg_len = len - 2;
456 	u8 check_sum = 0;
457 
458 	for (i = 0; i < raw_cfg_len; i++)
459 		check_sum += cfg[i];
460 	check_sum = (~check_sum) + 1;
461 	if (check_sum != cfg[raw_cfg_len]) {
462 		dev_err(&ts->client->dev,
463 			"The checksum of the config fw is not correct");
464 		return -EINVAL;
465 	}
466 
467 	if (cfg[raw_cfg_len + 1] != 1) {
468 		dev_err(&ts->client->dev,
469 			"Config fw must have Config_Fresh register set");
470 		return -EINVAL;
471 	}
472 
473 	return 0;
474 }
475 
476 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
477 {
478 	int i, raw_cfg_len = ts->chip->config_len - 2;
479 	u8 check_sum = 0;
480 
481 	for (i = 0; i < raw_cfg_len; i++)
482 		check_sum += ts->config[i];
483 	check_sum = (~check_sum) + 1;
484 
485 	ts->config[raw_cfg_len] = check_sum;
486 	ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
487 }
488 
489 static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
490 			       int len)
491 {
492 	int i, raw_cfg_len = len - 3;
493 	u16 check_sum = 0;
494 
495 	for (i = 0; i < raw_cfg_len; i += 2)
496 		check_sum += get_unaligned_be16(&cfg[i]);
497 	check_sum = (~check_sum) + 1;
498 	if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
499 		dev_err(&ts->client->dev,
500 			"The checksum of the config fw is not correct");
501 		return -EINVAL;
502 	}
503 
504 	if (cfg[raw_cfg_len + 2] != 1) {
505 		dev_err(&ts->client->dev,
506 			"Config fw must have Config_Fresh register set");
507 		return -EINVAL;
508 	}
509 
510 	return 0;
511 }
512 
513 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
514 {
515 	int i, raw_cfg_len = ts->chip->config_len - 3;
516 	u16 check_sum = 0;
517 
518 	for (i = 0; i < raw_cfg_len; i += 2)
519 		check_sum += get_unaligned_be16(&ts->config[i]);
520 	check_sum = (~check_sum) + 1;
521 
522 	put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
523 	ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
524 }
525 
526 /**
527  * goodix_check_cfg - Checks if config fw is valid
528  *
529  * @ts: goodix_ts_data pointer
530  * @cfg: firmware config data
531  * @len: config data length
532  */
533 static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
534 {
535 	if (len < GOODIX_CONFIG_MIN_LENGTH ||
536 	    len > GOODIX_CONFIG_MAX_LENGTH) {
537 		dev_err(&ts->client->dev,
538 			"The length of the config fw is not correct");
539 		return -EINVAL;
540 	}
541 
542 	return ts->chip->check_config(ts, cfg, len);
543 }
544 
545 /**
546  * goodix_send_cfg - Write fw config to device
547  *
548  * @ts: goodix_ts_data pointer
549  * @cfg: config firmware to write to device
550  * @len: config data length
551  */
552 int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
553 {
554 	int error;
555 
556 	error = goodix_check_cfg(ts, cfg, len);
557 	if (error)
558 		return error;
559 
560 	error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
561 	if (error)
562 		return error;
563 
564 	dev_dbg(&ts->client->dev, "Config sent successfully.");
565 
566 	/* Let the firmware reconfigure itself, so sleep for 10ms */
567 	usleep_range(10000, 11000);
568 
569 	return 0;
570 }
571 
572 #ifdef ACPI_GPIO_SUPPORT
573 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
574 {
575 	acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
576 	acpi_status status;
577 
578 	status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
579 	return ACPI_SUCCESS(status) ? 0 : -EIO;
580 }
581 
582 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
583 {
584 	acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
585 	acpi_status status;
586 
587 	status = acpi_execute_simple_method(handle, "INTO", value);
588 	return ACPI_SUCCESS(status) ? 0 : -EIO;
589 }
590 #else
591 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
592 {
593 	dev_err(&ts->client->dev,
594 		"%s called on device without ACPI support\n", __func__);
595 	return -EINVAL;
596 }
597 
598 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
599 {
600 	dev_err(&ts->client->dev,
601 		"%s called on device without ACPI support\n", __func__);
602 	return -EINVAL;
603 }
604 #endif
605 
606 static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
607 {
608 	switch (ts->irq_pin_access_method) {
609 	case IRQ_PIN_ACCESS_NONE:
610 		dev_err(&ts->client->dev,
611 			"%s called without an irq_pin_access_method set\n",
612 			__func__);
613 		return -EINVAL;
614 	case IRQ_PIN_ACCESS_GPIO:
615 		return gpiod_direction_output(ts->gpiod_int, value);
616 	case IRQ_PIN_ACCESS_ACPI_GPIO:
617 		/*
618 		 * The IRQ pin triggers on a falling edge, so its gets marked
619 		 * as active-low, use output_raw to avoid the value inversion.
620 		 */
621 		return gpiod_direction_output_raw(ts->gpiod_int, value);
622 	case IRQ_PIN_ACCESS_ACPI_METHOD:
623 		return goodix_pin_acpi_output_method(ts, value);
624 	}
625 
626 	return -EINVAL; /* Never reached */
627 }
628 
629 static int goodix_irq_direction_input(struct goodix_ts_data *ts)
630 {
631 	switch (ts->irq_pin_access_method) {
632 	case IRQ_PIN_ACCESS_NONE:
633 		dev_err(&ts->client->dev,
634 			"%s called without an irq_pin_access_method set\n",
635 			__func__);
636 		return -EINVAL;
637 	case IRQ_PIN_ACCESS_GPIO:
638 		return gpiod_direction_input(ts->gpiod_int);
639 	case IRQ_PIN_ACCESS_ACPI_GPIO:
640 		return gpiod_direction_input(ts->gpiod_int);
641 	case IRQ_PIN_ACCESS_ACPI_METHOD:
642 		return goodix_pin_acpi_direction_input(ts);
643 	}
644 
645 	return -EINVAL; /* Never reached */
646 }
647 
648 int goodix_int_sync(struct goodix_ts_data *ts)
649 {
650 	int error;
651 
652 	error = goodix_irq_direction_output(ts, 0);
653 	if (error)
654 		goto error;
655 
656 	msleep(50);				/* T5: 50ms */
657 
658 	error = goodix_irq_direction_input(ts);
659 	if (error)
660 		goto error;
661 
662 	return 0;
663 
664 error:
665 	dev_err(&ts->client->dev, "Controller irq sync failed.\n");
666 	return error;
667 }
668 
669 /**
670  * goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode
671  *
672  * @ts: goodix_ts_data pointer
673  */
674 int goodix_reset_no_int_sync(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 		goto 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 		goto error;
689 
690 	usleep_range(100, 2000);		/* T3: > 100us */
691 
692 	error = gpiod_direction_output(ts->gpiod_rst, 1);
693 	if (error)
694 		goto 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 		goto error;
702 
703 	return 0;
704 
705 error:
706 	dev_err(&ts->client->dev, "Controller reset failed.\n");
707 	return error;
708 }
709 
710 /**
711  * goodix_reset - Reset device during power on
712  *
713  * @ts: goodix_ts_data pointer
714  */
715 static int goodix_reset(struct goodix_ts_data *ts)
716 {
717 	int error;
718 
719 	error = goodix_reset_no_int_sync(ts);
720 	if (error)
721 		return error;
722 
723 	return goodix_int_sync(ts);
724 }
725 
726 #ifdef ACPI_GPIO_SUPPORT
727 #include <asm/cpu_device_id.h>
728 #include <asm/intel-family.h>
729 
730 static const struct x86_cpu_id baytrail_cpu_ids[] = {
731 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, X86_FEATURE_ANY, },
732 	{}
733 };
734 
735 static inline bool is_byt(void)
736 {
737 	const struct x86_cpu_id *id = x86_match_cpu(baytrail_cpu_ids);
738 
739 	return !!id;
740 }
741 
742 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
743 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
744 
745 static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
746 	{ GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
747 	{ GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
748 	{ },
749 };
750 
751 static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
752 	{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
753 	{ GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
754 	{ },
755 };
756 
757 static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
758 	{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
759 	{ },
760 };
761 
762 static int goodix_resource(struct acpi_resource *ares, void *data)
763 {
764 	struct goodix_ts_data *ts = data;
765 	struct device *dev = &ts->client->dev;
766 	struct acpi_resource_gpio *gpio;
767 
768 	switch (ares->type) {
769 	case ACPI_RESOURCE_TYPE_GPIO:
770 		gpio = &ares->data.gpio;
771 		if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
772 			if (ts->gpio_int_idx == -1) {
773 				ts->gpio_int_idx = ts->gpio_count;
774 			} else {
775 				dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
776 				ts->gpio_int_idx = -2;
777 			}
778 		}
779 		ts->gpio_count++;
780 		break;
781 	default:
782 		break;
783 	}
784 
785 	return 0;
786 }
787 
788 /*
789  * This function gets called in case we fail to get the irq GPIO directly
790  * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
791  * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
792  * In that case we add our own mapping and then goodix_get_gpio_config()
793  * retries to get the GPIOs based on the added mapping.
794  */
795 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
796 {
797 	const struct acpi_gpio_mapping *gpio_mapping = NULL;
798 	struct device *dev = &ts->client->dev;
799 	LIST_HEAD(resources);
800 	int ret;
801 
802 	ts->gpio_count = 0;
803 	ts->gpio_int_idx = -1;
804 	ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
805 				     goodix_resource, ts);
806 	if (ret < 0) {
807 		dev_err(dev, "Error getting ACPI resources: %d\n", ret);
808 		return ret;
809 	}
810 
811 	acpi_dev_free_resource_list(&resources);
812 
813 	if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) {
814 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
815 		gpio_mapping = acpi_goodix_int_first_gpios;
816 	} else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
817 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
818 		gpio_mapping = acpi_goodix_int_last_gpios;
819 	} else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
820 		   acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
821 		   acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
822 		dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
823 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
824 		gpio_mapping = acpi_goodix_reset_only_gpios;
825 	} else if (is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
826 		dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
827 		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
828 		gpio_mapping = acpi_goodix_int_last_gpios;
829 	} else {
830 		dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
831 			 ts->gpio_count, ts->gpio_int_idx);
832 		return -EINVAL;
833 	}
834 
835 	return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
836 }
837 #else
838 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
839 {
840 	return -EINVAL;
841 }
842 #endif /* CONFIG_X86 && CONFIG_ACPI */
843 
844 /**
845  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
846  *
847  * @ts: goodix_ts_data pointer
848  */
849 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
850 {
851 	int error;
852 	struct device *dev;
853 	struct gpio_desc *gpiod;
854 	bool added_acpi_mappings = false;
855 
856 	if (!ts->client)
857 		return -EINVAL;
858 	dev = &ts->client->dev;
859 
860 	ts->avdd28 = devm_regulator_get(dev, "AVDD28");
861 	if (IS_ERR(ts->avdd28)) {
862 		error = PTR_ERR(ts->avdd28);
863 		if (error != -EPROBE_DEFER)
864 			dev_err(dev,
865 				"Failed to get AVDD28 regulator: %d\n", error);
866 		return error;
867 	}
868 
869 	ts->vddio = devm_regulator_get(dev, "VDDIO");
870 	if (IS_ERR(ts->vddio)) {
871 		error = PTR_ERR(ts->vddio);
872 		if (error != -EPROBE_DEFER)
873 			dev_err(dev,
874 				"Failed to get VDDIO regulator: %d\n", error);
875 		return error;
876 	}
877 
878 retry_get_irq_gpio:
879 	/* Get the interrupt GPIO pin number */
880 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
881 	if (IS_ERR(gpiod)) {
882 		error = PTR_ERR(gpiod);
883 		if (error != -EPROBE_DEFER)
884 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
885 				GOODIX_GPIO_INT_NAME, error);
886 		return error;
887 	}
888 	if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
889 		added_acpi_mappings = true;
890 		if (goodix_add_acpi_gpio_mappings(ts) == 0)
891 			goto retry_get_irq_gpio;
892 	}
893 
894 	ts->gpiod_int = gpiod;
895 
896 	/* Get the reset line GPIO pin number */
897 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
898 	if (IS_ERR(gpiod)) {
899 		error = PTR_ERR(gpiod);
900 		if (error != -EPROBE_DEFER)
901 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
902 				GOODIX_GPIO_RST_NAME, error);
903 		return error;
904 	}
905 
906 	ts->gpiod_rst = gpiod;
907 
908 	switch (ts->irq_pin_access_method) {
909 	case IRQ_PIN_ACCESS_ACPI_GPIO:
910 		/*
911 		 * We end up here if goodix_add_acpi_gpio_mappings() has
912 		 * called devm_acpi_dev_add_driver_gpios() because the ACPI
913 		 * tables did not contain name to index mappings.
914 		 * Check that we successfully got both GPIOs after we've
915 		 * added our own acpi_gpio_mapping and if we did not get both
916 		 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
917 		 */
918 		if (!ts->gpiod_int || !ts->gpiod_rst)
919 			ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
920 		break;
921 	case IRQ_PIN_ACCESS_ACPI_METHOD:
922 		if (!ts->gpiod_rst)
923 			ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
924 		break;
925 	default:
926 		if (ts->gpiod_int && ts->gpiod_rst) {
927 			ts->reset_controller_at_probe = true;
928 			ts->load_cfg_from_disk = true;
929 			ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
930 		}
931 	}
932 
933 	return 0;
934 }
935 
936 /**
937  * goodix_read_config - Read the embedded configuration of the panel
938  *
939  * @ts: our goodix_ts_data pointer
940  *
941  * Must be called during probe
942  */
943 static void goodix_read_config(struct goodix_ts_data *ts)
944 {
945 	int x_max, y_max;
946 	int error;
947 
948 	/*
949 	 * On controllers where we need to upload the firmware
950 	 * (controllers without flash) ts->config already has the config
951 	 * at this point and the controller itself does not have it yet!
952 	 */
953 	if (!ts->firmware_name) {
954 		error = goodix_i2c_read(ts->client, ts->chip->config_addr,
955 					ts->config, ts->chip->config_len);
956 		if (error) {
957 			ts->int_trigger_type = GOODIX_INT_TRIGGER;
958 			ts->max_touch_num = GOODIX_MAX_CONTACTS;
959 			return;
960 		}
961 	}
962 
963 	ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
964 	ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
965 
966 	x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
967 	y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
968 	if (x_max && y_max) {
969 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
970 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
971 	}
972 
973 	ts->chip->calc_config_checksum(ts);
974 }
975 
976 /**
977  * goodix_read_version - Read goodix touchscreen version
978  *
979  * @ts: our goodix_ts_data pointer
980  */
981 static int goodix_read_version(struct goodix_ts_data *ts)
982 {
983 	int error;
984 	u8 buf[6];
985 	char id_str[GOODIX_ID_MAX_LEN + 1];
986 
987 	error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
988 	if (error)
989 		return error;
990 
991 	memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
992 	id_str[GOODIX_ID_MAX_LEN] = 0;
993 	strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
994 
995 	ts->version = get_unaligned_le16(&buf[4]);
996 
997 	dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
998 		 ts->version);
999 
1000 	return 0;
1001 }
1002 
1003 /**
1004  * goodix_i2c_test - I2C test function to check if the device answers.
1005  *
1006  * @client: the i2c client
1007  */
1008 static int goodix_i2c_test(struct i2c_client *client)
1009 {
1010 	int retry = 0;
1011 	int error;
1012 	u8 test;
1013 
1014 	while (retry++ < 2) {
1015 		error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
1016 		if (!error)
1017 			return 0;
1018 
1019 		msleep(20);
1020 	}
1021 
1022 	return error;
1023 }
1024 
1025 /**
1026  * goodix_configure_dev - Finish device initialization
1027  *
1028  * @ts: our goodix_ts_data pointer
1029  *
1030  * Must be called from probe to finish initialization of the device.
1031  * Contains the common initialization code for both devices that
1032  * declare gpio pins and devices that do not. It is either called
1033  * directly from probe or from request_firmware_wait callback.
1034  */
1035 static int goodix_configure_dev(struct goodix_ts_data *ts)
1036 {
1037 	int error;
1038 	int i;
1039 
1040 	ts->int_trigger_type = GOODIX_INT_TRIGGER;
1041 	ts->max_touch_num = GOODIX_MAX_CONTACTS;
1042 
1043 	ts->input_dev = devm_input_allocate_device(&ts->client->dev);
1044 	if (!ts->input_dev) {
1045 		dev_err(&ts->client->dev, "Failed to allocate input device.");
1046 		return -ENOMEM;
1047 	}
1048 
1049 	ts->input_dev->name = "Goodix Capacitive TouchScreen";
1050 	ts->input_dev->phys = "input/ts";
1051 	ts->input_dev->id.bustype = BUS_I2C;
1052 	ts->input_dev->id.vendor = 0x0416;
1053 	if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
1054 		ts->input_dev->id.product = 0x1001;
1055 	ts->input_dev->id.version = ts->version;
1056 
1057 	ts->input_dev->keycode = ts->keymap;
1058 	ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
1059 	ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
1060 
1061 	/* Capacitive Windows/Home button on some devices */
1062 	for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
1063 		if (i == 0)
1064 			ts->keymap[i] = KEY_LEFTMETA;
1065 		else
1066 			ts->keymap[i] = KEY_F1 + (i - 1);
1067 
1068 		input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
1069 	}
1070 
1071 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
1072 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
1073 	input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1074 	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1075 
1076 	/* Read configuration and apply touchscreen parameters */
1077 	goodix_read_config(ts);
1078 
1079 	/* Try overriding touchscreen parameters via device properties */
1080 	touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1081 
1082 	if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1083 		dev_err(&ts->client->dev,
1084 			"Invalid config (%d, %d, %d), using defaults\n",
1085 			ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1086 		ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
1087 		ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
1088 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
1089 		input_abs_set_max(ts->input_dev,
1090 				  ABS_MT_POSITION_X, ts->prop.max_x);
1091 		input_abs_set_max(ts->input_dev,
1092 				  ABS_MT_POSITION_Y, ts->prop.max_y);
1093 	}
1094 
1095 	if (dmi_check_system(rotated_screen)) {
1096 		ts->prop.invert_x = true;
1097 		ts->prop.invert_y = true;
1098 		dev_dbg(&ts->client->dev,
1099 			"Applying '180 degrees rotated screen' quirk\n");
1100 	}
1101 
1102 	if (dmi_check_system(nine_bytes_report)) {
1103 		ts->contact_size = 9;
1104 
1105 		dev_dbg(&ts->client->dev,
1106 			"Non-standard 9-bytes report format quirk\n");
1107 	}
1108 
1109 	if (dmi_check_system(inverted_x_screen)) {
1110 		ts->prop.invert_x = true;
1111 		dev_dbg(&ts->client->dev,
1112 			"Applying 'inverted x screen' quirk\n");
1113 	}
1114 
1115 	error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1116 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1117 	if (error) {
1118 		dev_err(&ts->client->dev,
1119 			"Failed to initialize MT slots: %d", error);
1120 		return error;
1121 	}
1122 
1123 	error = input_register_device(ts->input_dev);
1124 	if (error) {
1125 		dev_err(&ts->client->dev,
1126 			"Failed to register input device: %d", error);
1127 		return error;
1128 	}
1129 
1130 	ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1131 	error = goodix_request_irq(ts);
1132 	if (error) {
1133 		dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1134 		return error;
1135 	}
1136 
1137 	return 0;
1138 }
1139 
1140 /**
1141  * goodix_config_cb - Callback to finish device init
1142  *
1143  * @cfg: firmware config
1144  * @ctx: our goodix_ts_data pointer
1145  *
1146  * request_firmware_wait callback that finishes
1147  * initialization of the device.
1148  */
1149 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1150 {
1151 	struct goodix_ts_data *ts = ctx;
1152 	int error;
1153 
1154 	if (ts->firmware_name) {
1155 		if (!cfg)
1156 			goto err_release_cfg;
1157 
1158 		error = goodix_check_cfg(ts, cfg->data, cfg->size);
1159 		if (error)
1160 			goto err_release_cfg;
1161 
1162 		memcpy(ts->config, cfg->data, cfg->size);
1163 	} else if (cfg) {
1164 		/* send device configuration to the firmware */
1165 		error = goodix_send_cfg(ts, cfg->data, cfg->size);
1166 		if (error)
1167 			goto err_release_cfg;
1168 	}
1169 
1170 	goodix_configure_dev(ts);
1171 
1172 err_release_cfg:
1173 	release_firmware(cfg);
1174 	complete_all(&ts->firmware_loading_complete);
1175 }
1176 
1177 static void goodix_disable_regulators(void *arg)
1178 {
1179 	struct goodix_ts_data *ts = arg;
1180 
1181 	regulator_disable(ts->vddio);
1182 	regulator_disable(ts->avdd28);
1183 }
1184 
1185 static int goodix_ts_probe(struct i2c_client *client,
1186 			   const struct i2c_device_id *id)
1187 {
1188 	struct goodix_ts_data *ts;
1189 	const char *cfg_name;
1190 	int error;
1191 
1192 	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1193 
1194 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1195 		dev_err(&client->dev, "I2C check functionality failed.\n");
1196 		return -ENXIO;
1197 	}
1198 
1199 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1200 	if (!ts)
1201 		return -ENOMEM;
1202 
1203 	ts->client = client;
1204 	i2c_set_clientdata(client, ts);
1205 	init_completion(&ts->firmware_loading_complete);
1206 	ts->contact_size = GOODIX_CONTACT_SIZE;
1207 
1208 	error = goodix_get_gpio_config(ts);
1209 	if (error)
1210 		return error;
1211 
1212 	/* power up the controller */
1213 	error = regulator_enable(ts->avdd28);
1214 	if (error) {
1215 		dev_err(&client->dev,
1216 			"Failed to enable AVDD28 regulator: %d\n",
1217 			error);
1218 		return error;
1219 	}
1220 
1221 	error = regulator_enable(ts->vddio);
1222 	if (error) {
1223 		dev_err(&client->dev,
1224 			"Failed to enable VDDIO regulator: %d\n",
1225 			error);
1226 		regulator_disable(ts->avdd28);
1227 		return error;
1228 	}
1229 
1230 	error = devm_add_action_or_reset(&client->dev,
1231 					 goodix_disable_regulators, ts);
1232 	if (error)
1233 		return error;
1234 
1235 reset:
1236 	if (ts->reset_controller_at_probe) {
1237 		/* reset the controller */
1238 		error = goodix_reset(ts);
1239 		if (error)
1240 			return error;
1241 	}
1242 
1243 	error = goodix_i2c_test(client);
1244 	if (error) {
1245 		if (!ts->reset_controller_at_probe &&
1246 		    ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1247 			/* Retry after a controller reset */
1248 			ts->reset_controller_at_probe = true;
1249 			goto reset;
1250 		}
1251 		dev_err(&client->dev, "I2C communication failure: %d\n", error);
1252 		return error;
1253 	}
1254 
1255 	error = goodix_firmware_check(ts);
1256 	if (error)
1257 		return error;
1258 
1259 	error = goodix_read_version(ts);
1260 	if (error)
1261 		return error;
1262 
1263 	ts->chip = goodix_get_chip_data(ts->id);
1264 
1265 	if (ts->load_cfg_from_disk) {
1266 		/* update device config */
1267 		error = device_property_read_string(&client->dev,
1268 						    "goodix,config-name",
1269 						    &cfg_name);
1270 		if (!error)
1271 			snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1272 				 "goodix/%s", cfg_name);
1273 		else
1274 			snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1275 				 "goodix_%s_cfg.bin", ts->id);
1276 
1277 		error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1278 						&client->dev, GFP_KERNEL, ts,
1279 						goodix_config_cb);
1280 		if (error) {
1281 			dev_err(&client->dev,
1282 				"Failed to invoke firmware loader: %d\n",
1283 				error);
1284 			return error;
1285 		}
1286 
1287 		return 0;
1288 	} else {
1289 		error = goodix_configure_dev(ts);
1290 		if (error)
1291 			return error;
1292 	}
1293 
1294 	return 0;
1295 }
1296 
1297 static int goodix_ts_remove(struct i2c_client *client)
1298 {
1299 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1300 
1301 	if (ts->load_cfg_from_disk)
1302 		wait_for_completion(&ts->firmware_loading_complete);
1303 
1304 	return 0;
1305 }
1306 
1307 static int __maybe_unused goodix_suspend(struct device *dev)
1308 {
1309 	struct i2c_client *client = to_i2c_client(dev);
1310 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1311 	int error;
1312 
1313 	if (ts->load_cfg_from_disk)
1314 		wait_for_completion(&ts->firmware_loading_complete);
1315 
1316 	/* We need gpio pins to suspend/resume */
1317 	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1318 		disable_irq(client->irq);
1319 		return 0;
1320 	}
1321 
1322 	/* Free IRQ as IRQ pin is used as output in the suspend sequence */
1323 	goodix_free_irq(ts);
1324 
1325 	/* Save reference (calibration) info if necessary */
1326 	goodix_save_bak_ref(ts);
1327 
1328 	/* Output LOW on the INT pin for 5 ms */
1329 	error = goodix_irq_direction_output(ts, 0);
1330 	if (error) {
1331 		goodix_request_irq(ts);
1332 		return error;
1333 	}
1334 
1335 	usleep_range(5000, 6000);
1336 
1337 	error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1338 				    GOODIX_CMD_SCREEN_OFF);
1339 	if (error) {
1340 		goodix_irq_direction_input(ts);
1341 		goodix_request_irq(ts);
1342 		return -EAGAIN;
1343 	}
1344 
1345 	/*
1346 	 * The datasheet specifies that the interval between sending screen-off
1347 	 * command and wake-up should be longer than 58 ms. To avoid waking up
1348 	 * sooner, delay 58ms here.
1349 	 */
1350 	msleep(58);
1351 	return 0;
1352 }
1353 
1354 static int __maybe_unused goodix_resume(struct device *dev)
1355 {
1356 	struct i2c_client *client = to_i2c_client(dev);
1357 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1358 	u8 config_ver;
1359 	int error;
1360 
1361 	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1362 		enable_irq(client->irq);
1363 		return 0;
1364 	}
1365 
1366 	/*
1367 	 * Exit sleep mode by outputting HIGH level to INT pin
1368 	 * for 2ms~5ms.
1369 	 */
1370 	error = goodix_irq_direction_output(ts, 1);
1371 	if (error)
1372 		return error;
1373 
1374 	usleep_range(2000, 5000);
1375 
1376 	error = goodix_int_sync(ts);
1377 	if (error)
1378 		return error;
1379 
1380 	error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1381 				&config_ver, 1);
1382 	if (!error && config_ver != ts->config[0])
1383 		dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
1384 			 config_ver, ts->config[0]);
1385 
1386 	if (error != 0 || config_ver != ts->config[0]) {
1387 		error = goodix_reset(ts);
1388 		if (error)
1389 			return error;
1390 
1391 		error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
1392 		if (error)
1393 			return error;
1394 	}
1395 
1396 	error = goodix_request_irq(ts);
1397 	if (error)
1398 		return error;
1399 
1400 	return 0;
1401 }
1402 
1403 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1404 
1405 static const struct i2c_device_id goodix_ts_id[] = {
1406 	{ "GDIX1001:00", 0 },
1407 	{ }
1408 };
1409 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1410 
1411 #ifdef CONFIG_ACPI
1412 static const struct acpi_device_id goodix_acpi_match[] = {
1413 	{ "GDIX1001", 0 },
1414 	{ "GDIX1002", 0 },
1415 	{ }
1416 };
1417 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1418 #endif
1419 
1420 #ifdef CONFIG_OF
1421 static const struct of_device_id goodix_of_match[] = {
1422 	{ .compatible = "goodix,gt1151" },
1423 	{ .compatible = "goodix,gt5663" },
1424 	{ .compatible = "goodix,gt5688" },
1425 	{ .compatible = "goodix,gt911" },
1426 	{ .compatible = "goodix,gt9110" },
1427 	{ .compatible = "goodix,gt912" },
1428 	{ .compatible = "goodix,gt9147" },
1429 	{ .compatible = "goodix,gt917s" },
1430 	{ .compatible = "goodix,gt927" },
1431 	{ .compatible = "goodix,gt9271" },
1432 	{ .compatible = "goodix,gt928" },
1433 	{ .compatible = "goodix,gt9286" },
1434 	{ .compatible = "goodix,gt967" },
1435 	{ }
1436 };
1437 MODULE_DEVICE_TABLE(of, goodix_of_match);
1438 #endif
1439 
1440 static struct i2c_driver goodix_ts_driver = {
1441 	.probe = goodix_ts_probe,
1442 	.remove = goodix_ts_remove,
1443 	.id_table = goodix_ts_id,
1444 	.driver = {
1445 		.name = "Goodix-TS",
1446 		.acpi_match_table = ACPI_PTR(goodix_acpi_match),
1447 		.of_match_table = of_match_ptr(goodix_of_match),
1448 		.pm = &goodix_pm_ops,
1449 	},
1450 };
1451 module_i2c_driver(goodix_ts_driver);
1452 
1453 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1454 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1455 MODULE_DESCRIPTION("Goodix touchscreen driver");
1456 MODULE_LICENSE("GPL v2");
1457