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