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