1 /*
2  *  Driver for Goodix Touchscreens
3  *
4  *  Copyright (c) 2014 Red Hat Inc.
5  *  Copyright (c) 2015 K. Merker <merker@debian.org>
6  *
7  *  This code is based on gt9xx.c authored by andrew@goodix.com:
8  *
9  *  2010 - 2012 Goodix Technology.
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; version 2 of the License.
16  */
17 
18 #include <linux/kernel.h>
19 #include <linux/dmi.h>
20 #include <linux/firmware.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/i2c.h>
23 #include <linux/input.h>
24 #include <linux/input/mt.h>
25 #include <linux/input/touchscreen.h>
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/acpi.h>
32 #include <linux/of.h>
33 #include <asm/unaligned.h>
34 
35 struct goodix_ts_data;
36 
37 struct goodix_chip_data {
38 	u16 config_addr;
39 	int config_len;
40 	int (*check_config)(struct goodix_ts_data *, const struct firmware *);
41 };
42 
43 struct goodix_ts_data {
44 	struct i2c_client *client;
45 	struct input_dev *input_dev;
46 	const struct goodix_chip_data *chip;
47 	struct touchscreen_properties prop;
48 	unsigned int max_touch_num;
49 	unsigned int int_trigger_type;
50 	struct gpio_desc *gpiod_int;
51 	struct gpio_desc *gpiod_rst;
52 	u16 id;
53 	u16 version;
54 	const char *cfg_name;
55 	struct completion firmware_loading_complete;
56 	unsigned long irq_flags;
57 };
58 
59 #define GOODIX_GPIO_INT_NAME		"irq"
60 #define GOODIX_GPIO_RST_NAME		"reset"
61 
62 #define GOODIX_MAX_HEIGHT		4096
63 #define GOODIX_MAX_WIDTH		4096
64 #define GOODIX_INT_TRIGGER		1
65 #define GOODIX_CONTACT_SIZE		8
66 #define GOODIX_MAX_CONTACTS		10
67 
68 #define GOODIX_CONFIG_MAX_LENGTH	240
69 #define GOODIX_CONFIG_911_LENGTH	186
70 #define GOODIX_CONFIG_967_LENGTH	228
71 
72 /* Register defines */
73 #define GOODIX_REG_COMMAND		0x8040
74 #define GOODIX_CMD_SCREEN_OFF		0x05
75 
76 #define GOODIX_READ_COOR_ADDR		0x814E
77 #define GOODIX_GT1X_REG_CONFIG_DATA	0x8050
78 #define GOODIX_GT9X_REG_CONFIG_DATA	0x8047
79 #define GOODIX_REG_ID			0x8140
80 
81 #define GOODIX_BUFFER_STATUS_READY	BIT(7)
82 #define GOODIX_BUFFER_STATUS_TIMEOUT	20
83 
84 #define RESOLUTION_LOC		1
85 #define MAX_CONTACTS_LOC	5
86 #define TRIGGER_LOC		6
87 
88 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
89 			const struct firmware *cfg);
90 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
91 			const struct firmware *cfg);
92 
93 static const struct goodix_chip_data gt1x_chip_data = {
94 	.config_addr		= GOODIX_GT1X_REG_CONFIG_DATA,
95 	.config_len		= GOODIX_CONFIG_MAX_LENGTH,
96 	.check_config		= goodix_check_cfg_16,
97 };
98 
99 static const struct goodix_chip_data gt911_chip_data = {
100 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
101 	.config_len		= GOODIX_CONFIG_911_LENGTH,
102 	.check_config		= goodix_check_cfg_8,
103 };
104 
105 static const struct goodix_chip_data gt967_chip_data = {
106 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
107 	.config_len		= GOODIX_CONFIG_967_LENGTH,
108 	.check_config		= goodix_check_cfg_8,
109 };
110 
111 static const struct goodix_chip_data gt9x_chip_data = {
112 	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
113 	.config_len		= GOODIX_CONFIG_MAX_LENGTH,
114 	.check_config		= goodix_check_cfg_8,
115 };
116 
117 static const unsigned long goodix_irq_flags[] = {
118 	IRQ_TYPE_EDGE_RISING,
119 	IRQ_TYPE_EDGE_FALLING,
120 	IRQ_TYPE_LEVEL_LOW,
121 	IRQ_TYPE_LEVEL_HIGH,
122 };
123 
124 /*
125  * Those tablets have their coordinates origin at the bottom right
126  * of the tablet, as if rotated 180 degrees
127  */
128 static const struct dmi_system_id rotated_screen[] = {
129 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
130 	{
131 		.ident = "WinBook TW100",
132 		.matches = {
133 			DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
134 			DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
135 		}
136 	},
137 	{
138 		.ident = "WinBook TW700",
139 		.matches = {
140 			DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
141 			DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
142 		},
143 	},
144 #endif
145 	{}
146 };
147 
148 /**
149  * goodix_i2c_read - read data from a register of the i2c slave device.
150  *
151  * @client: i2c device.
152  * @reg: the register to read from.
153  * @buf: raw write data buffer.
154  * @len: length of the buffer to write
155  */
156 static int goodix_i2c_read(struct i2c_client *client,
157 			   u16 reg, u8 *buf, int len)
158 {
159 	struct i2c_msg msgs[2];
160 	__be16 wbuf = cpu_to_be16(reg);
161 	int ret;
162 
163 	msgs[0].flags = 0;
164 	msgs[0].addr  = client->addr;
165 	msgs[0].len   = 2;
166 	msgs[0].buf   = (u8 *)&wbuf;
167 
168 	msgs[1].flags = I2C_M_RD;
169 	msgs[1].addr  = client->addr;
170 	msgs[1].len   = len;
171 	msgs[1].buf   = buf;
172 
173 	ret = i2c_transfer(client->adapter, msgs, 2);
174 	return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
175 }
176 
177 /**
178  * goodix_i2c_write - write data to a register of the i2c slave device.
179  *
180  * @client: i2c device.
181  * @reg: the register to write to.
182  * @buf: raw data buffer to write.
183  * @len: length of the buffer to write
184  */
185 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
186 			    unsigned len)
187 {
188 	u8 *addr_buf;
189 	struct i2c_msg msg;
190 	int ret;
191 
192 	addr_buf = kmalloc(len + 2, GFP_KERNEL);
193 	if (!addr_buf)
194 		return -ENOMEM;
195 
196 	addr_buf[0] = reg >> 8;
197 	addr_buf[1] = reg & 0xFF;
198 	memcpy(&addr_buf[2], buf, len);
199 
200 	msg.flags = 0;
201 	msg.addr = client->addr;
202 	msg.buf = addr_buf;
203 	msg.len = len + 2;
204 
205 	ret = i2c_transfer(client->adapter, &msg, 1);
206 	kfree(addr_buf);
207 	return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
208 }
209 
210 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
211 {
212 	return goodix_i2c_write(client, reg, &value, sizeof(value));
213 }
214 
215 static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
216 {
217 	switch (id) {
218 	case 1151:
219 	case 5688:
220 		return &gt1x_chip_data;
221 
222 	case 911:
223 	case 9271:
224 	case 9110:
225 	case 927:
226 	case 928:
227 		return &gt911_chip_data;
228 
229 	case 912:
230 	case 967:
231 		return &gt967_chip_data;
232 
233 	default:
234 		return &gt9x_chip_data;
235 	}
236 }
237 
238 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
239 {
240 	unsigned long max_timeout;
241 	int touch_num;
242 	int error;
243 
244 	/*
245 	 * The 'buffer status' bit, which indicates that the data is valid, is
246 	 * not set as soon as the interrupt is raised, but slightly after.
247 	 * This takes around 10 ms to happen, so we poll for 20 ms.
248 	 */
249 	max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
250 	do {
251 		error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
252 					data, GOODIX_CONTACT_SIZE + 1);
253 		if (error) {
254 			dev_err(&ts->client->dev, "I2C transfer error: %d\n",
255 					error);
256 			return error;
257 		}
258 
259 		if (data[0] & GOODIX_BUFFER_STATUS_READY) {
260 			touch_num = data[0] & 0x0f;
261 			if (touch_num > ts->max_touch_num)
262 				return -EPROTO;
263 
264 			if (touch_num > 1) {
265 				data += 1 + GOODIX_CONTACT_SIZE;
266 				error = goodix_i2c_read(ts->client,
267 						GOODIX_READ_COOR_ADDR +
268 							1 + GOODIX_CONTACT_SIZE,
269 						data,
270 						GOODIX_CONTACT_SIZE *
271 							(touch_num - 1));
272 				if (error)
273 					return error;
274 			}
275 
276 			return touch_num;
277 		}
278 
279 		usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
280 	} while (time_before(jiffies, max_timeout));
281 
282 	/*
283 	 * The Goodix panel will send spurious interrupts after a
284 	 * 'finger up' event, which will always cause a timeout.
285 	 */
286 	return 0;
287 }
288 
289 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
290 {
291 	int id = coor_data[0] & 0x0F;
292 	int input_x = get_unaligned_le16(&coor_data[1]);
293 	int input_y = get_unaligned_le16(&coor_data[3]);
294 	int input_w = get_unaligned_le16(&coor_data[5]);
295 
296 	input_mt_slot(ts->input_dev, id);
297 	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
298 	touchscreen_report_pos(ts->input_dev, &ts->prop,
299 			       input_x, input_y, true);
300 	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
301 	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
302 }
303 
304 /**
305  * goodix_process_events - Process incoming events
306  *
307  * @ts: our goodix_ts_data pointer
308  *
309  * Called when the IRQ is triggered. Read the current device state, and push
310  * the input events to the user space.
311  */
312 static void goodix_process_events(struct goodix_ts_data *ts)
313 {
314 	u8  point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
315 	int touch_num;
316 	int i;
317 
318 	touch_num = goodix_ts_read_input_report(ts, point_data);
319 	if (touch_num < 0)
320 		return;
321 
322 	/*
323 	 * Bit 4 of the first byte reports the status of the capacitive
324 	 * Windows/Home button.
325 	 */
326 	input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
327 
328 	for (i = 0; i < touch_num; i++)
329 		goodix_ts_report_touch(ts,
330 				&point_data[1 + GOODIX_CONTACT_SIZE * i]);
331 
332 	input_mt_sync_frame(ts->input_dev);
333 	input_sync(ts->input_dev);
334 }
335 
336 /**
337  * goodix_ts_irq_handler - The IRQ handler
338  *
339  * @irq: interrupt number.
340  * @dev_id: private data pointer.
341  */
342 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
343 {
344 	struct goodix_ts_data *ts = dev_id;
345 
346 	goodix_process_events(ts);
347 
348 	if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
349 		dev_err(&ts->client->dev, "I2C write end_cmd error\n");
350 
351 	return IRQ_HANDLED;
352 }
353 
354 static void goodix_free_irq(struct goodix_ts_data *ts)
355 {
356 	devm_free_irq(&ts->client->dev, ts->client->irq, ts);
357 }
358 
359 static int goodix_request_irq(struct goodix_ts_data *ts)
360 {
361 	return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
362 					 NULL, goodix_ts_irq_handler,
363 					 ts->irq_flags, ts->client->name, ts);
364 }
365 
366 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
367 			const struct firmware *cfg)
368 {
369 	int i, raw_cfg_len = cfg->size - 2;
370 	u8 check_sum = 0;
371 
372 	for (i = 0; i < raw_cfg_len; i++)
373 		check_sum += cfg->data[i];
374 	check_sum = (~check_sum) + 1;
375 	if (check_sum != cfg->data[raw_cfg_len]) {
376 		dev_err(&ts->client->dev,
377 			"The checksum of the config fw is not correct");
378 		return -EINVAL;
379 	}
380 
381 	if (cfg->data[raw_cfg_len + 1] != 1) {
382 		dev_err(&ts->client->dev,
383 			"Config fw must have Config_Fresh register set");
384 		return -EINVAL;
385 	}
386 
387 	return 0;
388 }
389 
390 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
391 			const struct firmware *cfg)
392 {
393 	int i, raw_cfg_len = cfg->size - 3;
394 	u16 check_sum = 0;
395 
396 	for (i = 0; i < raw_cfg_len; i += 2)
397 		check_sum += get_unaligned_be16(&cfg->data[i]);
398 	check_sum = (~check_sum) + 1;
399 	if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) {
400 		dev_err(&ts->client->dev,
401 			"The checksum of the config fw is not correct");
402 		return -EINVAL;
403 	}
404 
405 	if (cfg->data[raw_cfg_len + 2] != 1) {
406 		dev_err(&ts->client->dev,
407 			"Config fw must have Config_Fresh register set");
408 		return -EINVAL;
409 	}
410 
411 	return 0;
412 }
413 
414 /**
415  * goodix_check_cfg - Checks if config fw is valid
416  *
417  * @ts: goodix_ts_data pointer
418  * @cfg: firmware config data
419  */
420 static int goodix_check_cfg(struct goodix_ts_data *ts,
421 			    const struct firmware *cfg)
422 {
423 	if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
424 		dev_err(&ts->client->dev,
425 			"The length of the config fw is not correct");
426 		return -EINVAL;
427 	}
428 
429 	return ts->chip->check_config(ts, cfg);
430 }
431 
432 /**
433  * goodix_send_cfg - Write fw config to device
434  *
435  * @ts: goodix_ts_data pointer
436  * @cfg: config firmware to write to device
437  */
438 static int goodix_send_cfg(struct goodix_ts_data *ts,
439 			   const struct firmware *cfg)
440 {
441 	int error;
442 
443 	error = goodix_check_cfg(ts, cfg);
444 	if (error)
445 		return error;
446 
447 	error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data,
448 				 cfg->size);
449 	if (error) {
450 		dev_err(&ts->client->dev, "Failed to write config data: %d",
451 			error);
452 		return error;
453 	}
454 	dev_dbg(&ts->client->dev, "Config sent successfully.");
455 
456 	/* Let the firmware reconfigure itself, so sleep for 10ms */
457 	usleep_range(10000, 11000);
458 
459 	return 0;
460 }
461 
462 static int goodix_int_sync(struct goodix_ts_data *ts)
463 {
464 	int error;
465 
466 	error = gpiod_direction_output(ts->gpiod_int, 0);
467 	if (error)
468 		return error;
469 
470 	msleep(50);				/* T5: 50ms */
471 
472 	error = gpiod_direction_input(ts->gpiod_int);
473 	if (error)
474 		return error;
475 
476 	return 0;
477 }
478 
479 /**
480  * goodix_reset - Reset device during power on
481  *
482  * @ts: goodix_ts_data pointer
483  */
484 static int goodix_reset(struct goodix_ts_data *ts)
485 {
486 	int error;
487 
488 	/* begin select I2C slave addr */
489 	error = gpiod_direction_output(ts->gpiod_rst, 0);
490 	if (error)
491 		return error;
492 
493 	msleep(20);				/* T2: > 10ms */
494 
495 	/* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
496 	error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
497 	if (error)
498 		return error;
499 
500 	usleep_range(100, 2000);		/* T3: > 100us */
501 
502 	error = gpiod_direction_output(ts->gpiod_rst, 1);
503 	if (error)
504 		return error;
505 
506 	usleep_range(6000, 10000);		/* T4: > 5ms */
507 
508 	/* end select I2C slave addr */
509 	error = gpiod_direction_input(ts->gpiod_rst);
510 	if (error)
511 		return error;
512 
513 	error = goodix_int_sync(ts);
514 	if (error)
515 		return error;
516 
517 	return 0;
518 }
519 
520 /**
521  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
522  *
523  * @ts: goodix_ts_data pointer
524  */
525 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
526 {
527 	int error;
528 	struct device *dev;
529 	struct gpio_desc *gpiod;
530 
531 	if (!ts->client)
532 		return -EINVAL;
533 	dev = &ts->client->dev;
534 
535 	/* Get the interrupt GPIO pin number */
536 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
537 	if (IS_ERR(gpiod)) {
538 		error = PTR_ERR(gpiod);
539 		if (error != -EPROBE_DEFER)
540 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
541 				GOODIX_GPIO_INT_NAME, error);
542 		return error;
543 	}
544 
545 	ts->gpiod_int = gpiod;
546 
547 	/* Get the reset line GPIO pin number */
548 	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
549 	if (IS_ERR(gpiod)) {
550 		error = PTR_ERR(gpiod);
551 		if (error != -EPROBE_DEFER)
552 			dev_dbg(dev, "Failed to get %s GPIO: %d\n",
553 				GOODIX_GPIO_RST_NAME, error);
554 		return error;
555 	}
556 
557 	ts->gpiod_rst = gpiod;
558 
559 	return 0;
560 }
561 
562 /**
563  * goodix_read_config - Read the embedded configuration of the panel
564  *
565  * @ts: our goodix_ts_data pointer
566  *
567  * Must be called during probe
568  */
569 static void goodix_read_config(struct goodix_ts_data *ts)
570 {
571 	u8 config[GOODIX_CONFIG_MAX_LENGTH];
572 	int x_max, y_max;
573 	int error;
574 
575 	error = goodix_i2c_read(ts->client, ts->chip->config_addr,
576 				config, ts->chip->config_len);
577 	if (error) {
578 		dev_warn(&ts->client->dev, "Error reading config: %d\n",
579 			 error);
580 		ts->int_trigger_type = GOODIX_INT_TRIGGER;
581 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
582 		return;
583 	}
584 
585 	ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
586 	ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
587 
588 	x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
589 	y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
590 	if (x_max && y_max) {
591 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
592 		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
593 	}
594 }
595 
596 /**
597  * goodix_read_version - Read goodix touchscreen version
598  *
599  * @ts: our goodix_ts_data pointer
600  */
601 static int goodix_read_version(struct goodix_ts_data *ts)
602 {
603 	int error;
604 	u8 buf[6];
605 	char id_str[5];
606 
607 	error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
608 	if (error) {
609 		dev_err(&ts->client->dev, "read version failed: %d\n", error);
610 		return error;
611 	}
612 
613 	memcpy(id_str, buf, 4);
614 	id_str[4] = 0;
615 	if (kstrtou16(id_str, 10, &ts->id))
616 		ts->id = 0x1001;
617 
618 	ts->version = get_unaligned_le16(&buf[4]);
619 
620 	dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
621 		 ts->version);
622 
623 	return 0;
624 }
625 
626 /**
627  * goodix_i2c_test - I2C test function to check if the device answers.
628  *
629  * @client: the i2c client
630  */
631 static int goodix_i2c_test(struct i2c_client *client)
632 {
633 	int retry = 0;
634 	int error;
635 	u8 test;
636 
637 	while (retry++ < 2) {
638 		error = goodix_i2c_read(client, GOODIX_REG_ID,
639 					&test, 1);
640 		if (!error)
641 			return 0;
642 
643 		dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
644 			retry, error);
645 		msleep(20);
646 	}
647 
648 	return error;
649 }
650 
651 /**
652  * goodix_configure_dev - Finish device initialization
653  *
654  * @ts: our goodix_ts_data pointer
655  *
656  * Must be called from probe to finish initialization of the device.
657  * Contains the common initialization code for both devices that
658  * declare gpio pins and devices that do not. It is either called
659  * directly from probe or from request_firmware_wait callback.
660  */
661 static int goodix_configure_dev(struct goodix_ts_data *ts)
662 {
663 	int error;
664 
665 	ts->int_trigger_type = GOODIX_INT_TRIGGER;
666 	ts->max_touch_num = GOODIX_MAX_CONTACTS;
667 
668 	ts->input_dev = devm_input_allocate_device(&ts->client->dev);
669 	if (!ts->input_dev) {
670 		dev_err(&ts->client->dev, "Failed to allocate input device.");
671 		return -ENOMEM;
672 	}
673 
674 	ts->input_dev->name = "Goodix Capacitive TouchScreen";
675 	ts->input_dev->phys = "input/ts";
676 	ts->input_dev->id.bustype = BUS_I2C;
677 	ts->input_dev->id.vendor = 0x0416;
678 	ts->input_dev->id.product = ts->id;
679 	ts->input_dev->id.version = ts->version;
680 
681 	/* Capacitive Windows/Home button on some devices */
682 	input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
683 
684 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
685 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
686 	input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
687 	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
688 
689 	/* Read configuration and apply touchscreen parameters */
690 	goodix_read_config(ts);
691 
692 	/* Try overriding touchscreen parameters via device properties */
693 	touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
694 
695 	if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
696 		dev_err(&ts->client->dev,
697 			"Invalid config (%d, %d, %d), using defaults\n",
698 			ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
699 		ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
700 		ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
701 		ts->max_touch_num = GOODIX_MAX_CONTACTS;
702 		input_abs_set_max(ts->input_dev,
703 				  ABS_MT_POSITION_X, ts->prop.max_x);
704 		input_abs_set_max(ts->input_dev,
705 				  ABS_MT_POSITION_Y, ts->prop.max_y);
706 	}
707 
708 	if (dmi_check_system(rotated_screen)) {
709 		ts->prop.invert_x = true;
710 		ts->prop.invert_y = true;
711 		dev_dbg(&ts->client->dev,
712 			"Applying '180 degrees rotated screen' quirk\n");
713 	}
714 
715 	error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
716 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
717 	if (error) {
718 		dev_err(&ts->client->dev,
719 			"Failed to initialize MT slots: %d", error);
720 		return error;
721 	}
722 
723 	error = input_register_device(ts->input_dev);
724 	if (error) {
725 		dev_err(&ts->client->dev,
726 			"Failed to register input device: %d", error);
727 		return error;
728 	}
729 
730 	ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
731 	error = goodix_request_irq(ts);
732 	if (error) {
733 		dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
734 		return error;
735 	}
736 
737 	return 0;
738 }
739 
740 /**
741  * goodix_config_cb - Callback to finish device init
742  *
743  * @ts: our goodix_ts_data pointer
744  *
745  * request_firmware_wait callback that finishes
746  * initialization of the device.
747  */
748 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
749 {
750 	struct goodix_ts_data *ts = ctx;
751 	int error;
752 
753 	if (cfg) {
754 		/* send device configuration to the firmware */
755 		error = goodix_send_cfg(ts, cfg);
756 		if (error)
757 			goto err_release_cfg;
758 	}
759 
760 	goodix_configure_dev(ts);
761 
762 err_release_cfg:
763 	release_firmware(cfg);
764 	complete_all(&ts->firmware_loading_complete);
765 }
766 
767 static int goodix_ts_probe(struct i2c_client *client,
768 			   const struct i2c_device_id *id)
769 {
770 	struct goodix_ts_data *ts;
771 	int error;
772 
773 	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
774 
775 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
776 		dev_err(&client->dev, "I2C check functionality failed.\n");
777 		return -ENXIO;
778 	}
779 
780 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
781 	if (!ts)
782 		return -ENOMEM;
783 
784 	ts->client = client;
785 	i2c_set_clientdata(client, ts);
786 	init_completion(&ts->firmware_loading_complete);
787 
788 	error = goodix_get_gpio_config(ts);
789 	if (error)
790 		return error;
791 
792 	if (ts->gpiod_int && ts->gpiod_rst) {
793 		/* reset the controller */
794 		error = goodix_reset(ts);
795 		if (error) {
796 			dev_err(&client->dev, "Controller reset failed.\n");
797 			return error;
798 		}
799 	}
800 
801 	error = goodix_i2c_test(client);
802 	if (error) {
803 		dev_err(&client->dev, "I2C communication failure: %d\n", error);
804 		return error;
805 	}
806 
807 	error = goodix_read_version(ts);
808 	if (error) {
809 		dev_err(&client->dev, "Read version failed.\n");
810 		return error;
811 	}
812 
813 	ts->chip = goodix_get_chip_data(ts->id);
814 
815 	if (ts->gpiod_int && ts->gpiod_rst) {
816 		/* update device config */
817 		ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
818 					      "goodix_%d_cfg.bin", ts->id);
819 		if (!ts->cfg_name)
820 			return -ENOMEM;
821 
822 		error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
823 						&client->dev, GFP_KERNEL, ts,
824 						goodix_config_cb);
825 		if (error) {
826 			dev_err(&client->dev,
827 				"Failed to invoke firmware loader: %d\n",
828 				error);
829 			return error;
830 		}
831 
832 		return 0;
833 	} else {
834 		error = goodix_configure_dev(ts);
835 		if (error)
836 			return error;
837 	}
838 
839 	return 0;
840 }
841 
842 static int goodix_ts_remove(struct i2c_client *client)
843 {
844 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
845 
846 	if (ts->gpiod_int && ts->gpiod_rst)
847 		wait_for_completion(&ts->firmware_loading_complete);
848 
849 	return 0;
850 }
851 
852 static int __maybe_unused goodix_suspend(struct device *dev)
853 {
854 	struct i2c_client *client = to_i2c_client(dev);
855 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
856 	int error;
857 
858 	/* We need gpio pins to suspend/resume */
859 	if (!ts->gpiod_int || !ts->gpiod_rst) {
860 		disable_irq(client->irq);
861 		return 0;
862 	}
863 
864 	wait_for_completion(&ts->firmware_loading_complete);
865 
866 	/* Free IRQ as IRQ pin is used as output in the suspend sequence */
867 	goodix_free_irq(ts);
868 
869 	/* Output LOW on the INT pin for 5 ms */
870 	error = gpiod_direction_output(ts->gpiod_int, 0);
871 	if (error) {
872 		goodix_request_irq(ts);
873 		return error;
874 	}
875 
876 	usleep_range(5000, 6000);
877 
878 	error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
879 				    GOODIX_CMD_SCREEN_OFF);
880 	if (error) {
881 		dev_err(&ts->client->dev, "Screen off command failed\n");
882 		gpiod_direction_input(ts->gpiod_int);
883 		goodix_request_irq(ts);
884 		return -EAGAIN;
885 	}
886 
887 	/*
888 	 * The datasheet specifies that the interval between sending screen-off
889 	 * command and wake-up should be longer than 58 ms. To avoid waking up
890 	 * sooner, delay 58ms here.
891 	 */
892 	msleep(58);
893 	return 0;
894 }
895 
896 static int __maybe_unused goodix_resume(struct device *dev)
897 {
898 	struct i2c_client *client = to_i2c_client(dev);
899 	struct goodix_ts_data *ts = i2c_get_clientdata(client);
900 	int error;
901 
902 	if (!ts->gpiod_int || !ts->gpiod_rst) {
903 		enable_irq(client->irq);
904 		return 0;
905 	}
906 
907 	/*
908 	 * Exit sleep mode by outputting HIGH level to INT pin
909 	 * for 2ms~5ms.
910 	 */
911 	error = gpiod_direction_output(ts->gpiod_int, 1);
912 	if (error)
913 		return error;
914 
915 	usleep_range(2000, 5000);
916 
917 	error = goodix_int_sync(ts);
918 	if (error)
919 		return error;
920 
921 	error = goodix_request_irq(ts);
922 	if (error)
923 		return error;
924 
925 	return 0;
926 }
927 
928 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
929 
930 static const struct i2c_device_id goodix_ts_id[] = {
931 	{ "GDIX1001:00", 0 },
932 	{ }
933 };
934 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
935 
936 #ifdef CONFIG_ACPI
937 static const struct acpi_device_id goodix_acpi_match[] = {
938 	{ "GDIX1001", 0 },
939 	{ "GDIX1002", 0 },
940 	{ }
941 };
942 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
943 #endif
944 
945 #ifdef CONFIG_OF
946 static const struct of_device_id goodix_of_match[] = {
947 	{ .compatible = "goodix,gt1151" },
948 	{ .compatible = "goodix,gt5688" },
949 	{ .compatible = "goodix,gt911" },
950 	{ .compatible = "goodix,gt9110" },
951 	{ .compatible = "goodix,gt912" },
952 	{ .compatible = "goodix,gt927" },
953 	{ .compatible = "goodix,gt9271" },
954 	{ .compatible = "goodix,gt928" },
955 	{ .compatible = "goodix,gt967" },
956 	{ }
957 };
958 MODULE_DEVICE_TABLE(of, goodix_of_match);
959 #endif
960 
961 static struct i2c_driver goodix_ts_driver = {
962 	.probe = goodix_ts_probe,
963 	.remove = goodix_ts_remove,
964 	.id_table = goodix_ts_id,
965 	.driver = {
966 		.name = "Goodix-TS",
967 		.acpi_match_table = ACPI_PTR(goodix_acpi_match),
968 		.of_match_table = of_match_ptr(goodix_of_match),
969 		.pm = &goodix_pm_ops,
970 	},
971 };
972 module_i2c_driver(goodix_ts_driver);
973 
974 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
975 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
976 MODULE_DESCRIPTION("Goodix touchscreen driver");
977 MODULE_LICENSE("GPL v2");
978