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