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