1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Elan Microelectronics touch panels with I2C interface
4  *
5  * Copyright (C) 2014 Elan Microelectronics Corporation.
6  * Scott Liu <scott.liu@emc.com.tw>
7  *
8  * This code is partly based on hid-multitouch.c:
9  *
10  *  Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
11  *  Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
12  *  Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
13  *
14  * This code is partly based on i2c-hid.c:
15  *
16  * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
17  * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
18  * Copyright (c) 2012 Red Hat, Inc
19  */
20 
21 
22 #include <linux/bits.h>
23 #include <linux/module.h>
24 #include <linux/input.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/platform_device.h>
28 #include <linux/async.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/uaccess.h>
32 #include <linux/buffer_head.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/input/mt.h>
36 #include <linux/input/touchscreen.h>
37 #include <linux/acpi.h>
38 #include <linux/of.h>
39 #include <linux/gpio/consumer.h>
40 #include <linux/regulator/consumer.h>
41 #include <linux/uuid.h>
42 #include <asm/unaligned.h>
43 
44 /* Device, Driver information */
45 #define DEVICE_NAME	"elants_i2c"
46 
47 /* Convert from rows or columns into resolution */
48 #define ELAN_TS_RESOLUTION(n, m)   (((n) - 1) * (m))
49 
50 /* FW header data */
51 #define HEADER_SIZE		4
52 #define FW_HDR_TYPE		0
53 #define FW_HDR_COUNT		1
54 #define FW_HDR_LENGTH		2
55 
56 /* Buffer mode Queue Header information */
57 #define QUEUE_HEADER_SINGLE	0x62
58 #define QUEUE_HEADER_NORMAL	0X63
59 #define QUEUE_HEADER_WAIT	0x64
60 #define QUEUE_HEADER_NORMAL2	0x66
61 
62 /* Command header definition */
63 #define CMD_HEADER_WRITE	0x54
64 #define CMD_HEADER_READ		0x53
65 #define CMD_HEADER_6B_READ	0x5B
66 #define CMD_HEADER_ROM_READ	0x96
67 #define CMD_HEADER_RESP		0x52
68 #define CMD_HEADER_6B_RESP	0x9B
69 #define CMD_HEADER_ROM_RESP	0x95
70 #define CMD_HEADER_HELLO	0x55
71 #define CMD_HEADER_REK		0x66
72 
73 /* FW position data */
74 #define PACKET_SIZE_OLD		40
75 #define PACKET_SIZE		55
76 #define MAX_CONTACT_NUM		10
77 #define FW_POS_HEADER		0
78 #define FW_POS_STATE		1
79 #define FW_POS_TOTAL		2
80 #define FW_POS_XY		3
81 #define FW_POS_TOOL_TYPE	33
82 #define FW_POS_CHECKSUM		34
83 #define FW_POS_WIDTH		35
84 #define FW_POS_PRESSURE		45
85 
86 #define HEADER_REPORT_10_FINGER	0x62
87 
88 /* Header (4 bytes) plus 3 full 10-finger packets */
89 #define MAX_PACKET_SIZE		169
90 
91 #define BOOT_TIME_DELAY_MS	50
92 
93 /* FW read command, 0x53 0x?? 0x0, 0x01 */
94 #define E_ELAN_INFO_FW_VER	0x00
95 #define E_ELAN_INFO_BC_VER	0x10
96 #define E_ELAN_INFO_X_RES	0x60
97 #define E_ELAN_INFO_Y_RES	0x63
98 #define E_ELAN_INFO_REK		0xD0
99 #define E_ELAN_INFO_TEST_VER	0xE0
100 #define E_ELAN_INFO_FW_ID	0xF0
101 #define E_INFO_OSR		0xD6
102 #define E_INFO_PHY_SCAN		0xD7
103 #define E_INFO_PHY_DRIVER	0xD8
104 
105 /* FW write command, 0x54 0x?? 0x0, 0x01 */
106 #define E_POWER_STATE_SLEEP	0x50
107 #define E_POWER_STATE_RESUME	0x58
108 
109 #define MAX_RETRIES		3
110 #define MAX_FW_UPDATE_RETRIES	30
111 
112 #define ELAN_FW_PAGESIZE	132
113 
114 /* calibration timeout definition */
115 #define ELAN_CALI_TIMEOUT_MSEC	12000
116 
117 #define ELAN_POWERON_DELAY_USEC	500
118 #define ELAN_RESET_DELAY_MSEC	20
119 
120 enum elants_chip_id {
121 	EKTH3500,
122 	EKTF3624,
123 };
124 
125 enum elants_state {
126 	ELAN_STATE_NORMAL,
127 	ELAN_WAIT_QUEUE_HEADER,
128 	ELAN_WAIT_RECALIBRATION,
129 };
130 
131 enum elants_iap_mode {
132 	ELAN_IAP_OPERATIONAL,
133 	ELAN_IAP_RECOVERY,
134 };
135 
136 /* struct elants_data - represents state of Elan touchscreen device */
137 struct elants_data {
138 	struct i2c_client *client;
139 	struct input_dev *input;
140 
141 	struct regulator *vcc33;
142 	struct regulator *vccio;
143 	struct gpio_desc *reset_gpio;
144 
145 	u16 fw_version;
146 	u8 test_version;
147 	u8 solution_version;
148 	u8 bc_version;
149 	u8 iap_version;
150 	u16 hw_version;
151 	u8 major_res;
152 	unsigned int x_res;	/* resolution in units/mm */
153 	unsigned int y_res;
154 	unsigned int x_max;
155 	unsigned int y_max;
156 	unsigned int phy_x;
157 	unsigned int phy_y;
158 	struct touchscreen_properties prop;
159 
160 	enum elants_state state;
161 	enum elants_chip_id chip_id;
162 	enum elants_iap_mode iap_mode;
163 
164 	/* Guards against concurrent access to the device via sysfs */
165 	struct mutex sysfs_mutex;
166 
167 	u8 cmd_resp[HEADER_SIZE];
168 	struct completion cmd_done;
169 
170 	bool wake_irq_enabled;
171 	bool keep_power_in_suspend;
172 
173 	/* Must be last to be used for DMA operations */
174 	u8 buf[MAX_PACKET_SIZE] ____cacheline_aligned;
175 };
176 
177 static int elants_i2c_send(struct i2c_client *client,
178 			   const void *data, size_t size)
179 {
180 	int ret;
181 
182 	ret = i2c_master_send(client, data, size);
183 	if (ret == size)
184 		return 0;
185 
186 	if (ret >= 0)
187 		ret = -EIO;
188 
189 	dev_err(&client->dev, "%s failed (%*ph): %d\n",
190 		__func__, (int)size, data, ret);
191 
192 	return ret;
193 }
194 
195 static int elants_i2c_read(struct i2c_client *client, void *data, size_t size)
196 {
197 	int ret;
198 
199 	ret = i2c_master_recv(client, data, size);
200 	if (ret == size)
201 		return 0;
202 
203 	if (ret >= 0)
204 		ret = -EIO;
205 
206 	dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
207 
208 	return ret;
209 }
210 
211 static int elants_i2c_execute_command(struct i2c_client *client,
212 				      const u8 *cmd, size_t cmd_size,
213 				      u8 *resp, size_t resp_size,
214 				      int retries, const char *cmd_name)
215 {
216 	struct i2c_msg msgs[2];
217 	int ret;
218 	u8 expected_response;
219 
220 	switch (cmd[0]) {
221 	case CMD_HEADER_READ:
222 		expected_response = CMD_HEADER_RESP;
223 		break;
224 
225 	case CMD_HEADER_6B_READ:
226 		expected_response = CMD_HEADER_6B_RESP;
227 		break;
228 
229 	case CMD_HEADER_ROM_READ:
230 		expected_response = CMD_HEADER_ROM_RESP;
231 		break;
232 
233 	default:
234 		dev_err(&client->dev, "(%s): invalid command: %*ph\n",
235 			cmd_name, (int)cmd_size, cmd);
236 		return -EINVAL;
237 	}
238 
239 	for (;;) {
240 		msgs[0].addr = client->addr;
241 		msgs[0].flags = client->flags & I2C_M_TEN;
242 		msgs[0].len = cmd_size;
243 		msgs[0].buf = (u8 *)cmd;
244 
245 		msgs[1].addr = client->addr;
246 		msgs[1].flags = (client->flags & I2C_M_TEN) | I2C_M_RD;
247 		msgs[1].flags |= I2C_M_RD;
248 		msgs[1].len = resp_size;
249 		msgs[1].buf = resp;
250 
251 		ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
252 		if (ret < 0) {
253 			if (--retries > 0) {
254 				dev_dbg(&client->dev,
255 					"(%s) I2C transfer failed: %pe (retrying)\n",
256 					cmd_name, ERR_PTR(ret));
257 				continue;
258 			}
259 
260 			dev_err(&client->dev,
261 				"(%s) I2C transfer failed: %pe\n",
262 				cmd_name, ERR_PTR(ret));
263 			return ret;
264 		}
265 
266 		if (ret != ARRAY_SIZE(msgs) ||
267 		    resp[FW_HDR_TYPE] != expected_response) {
268 			if (--retries > 0) {
269 				dev_dbg(&client->dev,
270 					"(%s) unexpected response: %*ph (retrying)\n",
271 					cmd_name, ret, resp);
272 				continue;
273 			}
274 
275 			dev_err(&client->dev,
276 				"(%s) unexpected response: %*ph\n",
277 				cmd_name, ret, resp);
278 			return -EIO;
279 		}
280 
281 		return 0;
282 	}
283 }
284 
285 static int elants_i2c_calibrate(struct elants_data *ts)
286 {
287 	struct i2c_client *client = ts->client;
288 	int ret, error;
289 	static const u8 w_flashkey[] = { CMD_HEADER_WRITE, 0xC0, 0xE1, 0x5A };
290 	static const u8 rek[] = { CMD_HEADER_WRITE, 0x29, 0x00, 0x01 };
291 	static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 };
292 
293 	disable_irq(client->irq);
294 
295 	ts->state = ELAN_WAIT_RECALIBRATION;
296 	reinit_completion(&ts->cmd_done);
297 
298 	elants_i2c_send(client, w_flashkey, sizeof(w_flashkey));
299 	elants_i2c_send(client, rek, sizeof(rek));
300 
301 	enable_irq(client->irq);
302 
303 	ret = wait_for_completion_interruptible_timeout(&ts->cmd_done,
304 				msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC));
305 
306 	ts->state = ELAN_STATE_NORMAL;
307 
308 	if (ret <= 0) {
309 		error = ret < 0 ? ret : -ETIMEDOUT;
310 		dev_err(&client->dev,
311 			"error while waiting for calibration to complete: %d\n",
312 			error);
313 		return error;
314 	}
315 
316 	if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) {
317 		dev_err(&client->dev,
318 			"unexpected calibration response: %*ph\n",
319 			(int)sizeof(ts->cmd_resp), ts->cmd_resp);
320 		return -EINVAL;
321 	}
322 
323 	return 0;
324 }
325 
326 static int elants_i2c_sw_reset(struct i2c_client *client)
327 {
328 	const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 };
329 	int error;
330 
331 	error = elants_i2c_send(client, soft_rst_cmd,
332 				sizeof(soft_rst_cmd));
333 	if (error) {
334 		dev_err(&client->dev, "software reset failed: %d\n", error);
335 		return error;
336 	}
337 
338 	/*
339 	 * We should wait at least 10 msec (but no more than 40) before
340 	 * sending fastboot or IAP command to the device.
341 	 */
342 	msleep(30);
343 
344 	return 0;
345 }
346 
347 static u16 elants_i2c_parse_version(u8 *buf)
348 {
349 	return get_unaligned_be32(buf) >> 4;
350 }
351 
352 static int elants_i2c_query_hw_version(struct elants_data *ts)
353 {
354 	struct i2c_client *client = ts->client;
355 	int retry_cnt = MAX_RETRIES;
356 	const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 };
357 	u8 resp[HEADER_SIZE];
358 	int error;
359 
360 	while (retry_cnt--) {
361 		error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
362 						   resp, sizeof(resp), 1,
363 						   "read fw id");
364 		if (error)
365 			return error;
366 
367 		ts->hw_version = elants_i2c_parse_version(resp);
368 		if (ts->hw_version != 0xffff)
369 			return 0;
370 	}
371 
372 	dev_err(&client->dev, "Invalid fw id: %#04x\n", ts->hw_version);
373 
374 	return -EINVAL;
375 }
376 
377 static int elants_i2c_query_fw_version(struct elants_data *ts)
378 {
379 	struct i2c_client *client = ts->client;
380 	int retry_cnt = MAX_RETRIES;
381 	const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 };
382 	u8 resp[HEADER_SIZE];
383 	int error;
384 
385 	while (retry_cnt--) {
386 		error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
387 						   resp, sizeof(resp), 1,
388 						   "read fw version");
389 		if (error)
390 			return error;
391 
392 		ts->fw_version = elants_i2c_parse_version(resp);
393 		if (ts->fw_version != 0x0000 && ts->fw_version != 0xffff)
394 			return 0;
395 
396 		dev_dbg(&client->dev, "(read fw version) resp %*phC\n",
397 			(int)sizeof(resp), resp);
398 	}
399 
400 	dev_err(&client->dev, "Invalid fw ver: %#04x\n", ts->fw_version);
401 
402 	return -EINVAL;
403 }
404 
405 static int elants_i2c_query_test_version(struct elants_data *ts)
406 {
407 	struct i2c_client *client = ts->client;
408 	int error;
409 	u16 version;
410 	const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 };
411 	u8 resp[HEADER_SIZE];
412 
413 	error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
414 					   resp, sizeof(resp), MAX_RETRIES,
415 					   "read test version");
416 	if (error) {
417 		dev_err(&client->dev, "Failed to read test version\n");
418 		return error;
419 	}
420 
421 	version = elants_i2c_parse_version(resp);
422 	ts->test_version = version >> 8;
423 	ts->solution_version = version & 0xff;
424 
425 	return 0;
426 }
427 
428 static int elants_i2c_query_bc_version(struct elants_data *ts)
429 {
430 	struct i2c_client *client = ts->client;
431 	const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 };
432 	u8 resp[HEADER_SIZE];
433 	u16 version;
434 	int error;
435 
436 	error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
437 					   resp, sizeof(resp), 1,
438 					   "read BC version");
439 	if (error)
440 		return error;
441 
442 	version = elants_i2c_parse_version(resp);
443 	ts->bc_version = version >> 8;
444 	ts->iap_version = version & 0xff;
445 
446 	return 0;
447 }
448 
449 static int elants_i2c_query_ts_info_ektf(struct elants_data *ts)
450 {
451 	struct i2c_client *client = ts->client;
452 	int error;
453 	u8 resp[4];
454 	u16 phy_x, phy_y;
455 	const u8 get_xres_cmd[] = {
456 		CMD_HEADER_READ, E_ELAN_INFO_X_RES, 0x00, 0x00
457 	};
458 	const u8 get_yres_cmd[] = {
459 		CMD_HEADER_READ, E_ELAN_INFO_Y_RES, 0x00, 0x00
460 	};
461 
462 	/* Get X/Y size in mm */
463 	error = elants_i2c_execute_command(client, get_xres_cmd,
464 					   sizeof(get_xres_cmd),
465 					   resp, sizeof(resp), 1,
466 					   "get X size");
467 	if (error)
468 		return error;
469 
470 	phy_x = resp[2] | ((resp[3] & 0xF0) << 4);
471 
472 	error = elants_i2c_execute_command(client, get_yres_cmd,
473 					   sizeof(get_yres_cmd),
474 					   resp, sizeof(resp), 1,
475 					   "get Y size");
476 	if (error)
477 		return error;
478 
479 	phy_y = resp[2] | ((resp[3] & 0xF0) << 4);
480 
481 	dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
482 
483 	ts->phy_x = phy_x;
484 	ts->phy_y = phy_y;
485 
486 	/* eKTF doesn't report max size, set it to default values */
487 	ts->x_max = 2240 - 1;
488 	ts->y_max = 1408 - 1;
489 
490 	return 0;
491 }
492 
493 static int elants_i2c_query_ts_info_ekth(struct elants_data *ts)
494 {
495 	struct i2c_client *client = ts->client;
496 	int error;
497 	u8 resp[17];
498 	u16 phy_x, phy_y, rows, cols, osr;
499 	const u8 get_resolution_cmd[] = {
500 		CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00
501 	};
502 	const u8 get_osr_cmd[] = {
503 		CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01
504 	};
505 	const u8 get_physical_scan_cmd[] = {
506 		CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01
507 	};
508 	const u8 get_physical_drive_cmd[] = {
509 		CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01
510 	};
511 
512 	/* Get trace number */
513 	error = elants_i2c_execute_command(client,
514 					   get_resolution_cmd,
515 					   sizeof(get_resolution_cmd),
516 					   resp, sizeof(resp), 1,
517 					   "get resolution");
518 	if (error)
519 		return error;
520 
521 	rows = resp[2] + resp[6] + resp[10];
522 	cols = resp[3] + resp[7] + resp[11];
523 
524 	/* Get report resolution value of ABS_MT_TOUCH_MAJOR */
525 	ts->major_res = resp[16];
526 
527 	/* Process mm_to_pixel information */
528 	error = elants_i2c_execute_command(client,
529 					   get_osr_cmd, sizeof(get_osr_cmd),
530 					   resp, sizeof(resp), 1, "get osr");
531 	if (error)
532 		return error;
533 
534 	osr = resp[3];
535 
536 	error = elants_i2c_execute_command(client,
537 					   get_physical_scan_cmd,
538 					   sizeof(get_physical_scan_cmd),
539 					   resp, sizeof(resp), 1,
540 					   "get physical scan");
541 	if (error)
542 		return error;
543 
544 	phy_x = get_unaligned_be16(&resp[2]);
545 
546 	error = elants_i2c_execute_command(client,
547 					   get_physical_drive_cmd,
548 					   sizeof(get_physical_drive_cmd),
549 					   resp, sizeof(resp), 1,
550 					   "get physical drive");
551 	if (error)
552 		return error;
553 
554 	phy_y = get_unaligned_be16(&resp[2]);
555 
556 	dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
557 
558 	if (rows == 0 || cols == 0 || osr == 0) {
559 		dev_warn(&client->dev,
560 			 "invalid trace number data: %d, %d, %d\n",
561 			 rows, cols, osr);
562 	} else {
563 		/* translate trace number to TS resolution */
564 		ts->x_max = ELAN_TS_RESOLUTION(rows, osr);
565 		ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
566 		ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
567 		ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
568 		ts->phy_x = phy_x;
569 		ts->phy_y = phy_y;
570 	}
571 
572 	return 0;
573 }
574 
575 static int elants_i2c_fastboot(struct i2c_client *client)
576 {
577 	const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E };
578 	int error;
579 
580 	error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd));
581 	if (error) {
582 		dev_err(&client->dev, "boot failed: %d\n", error);
583 		return error;
584 	}
585 
586 	dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr);
587 	return 0;
588 }
589 
590 static int elants_i2c_initialize(struct elants_data *ts)
591 {
592 	struct i2c_client *client = ts->client;
593 	int error, error2, retry_cnt;
594 	const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 };
595 	const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 };
596 	u8 buf[HEADER_SIZE];
597 
598 	for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
599 		error = elants_i2c_sw_reset(client);
600 		if (error) {
601 			/* Continue initializing if it's the last try */
602 			if (retry_cnt < MAX_RETRIES - 1)
603 				continue;
604 		}
605 
606 		error = elants_i2c_fastboot(client);
607 		if (error) {
608 			/* Continue initializing if it's the last try */
609 			if (retry_cnt < MAX_RETRIES - 1)
610 				continue;
611 		}
612 
613 		/* Wait for Hello packet */
614 		msleep(BOOT_TIME_DELAY_MS);
615 
616 		error = elants_i2c_read(client, buf, sizeof(buf));
617 		if (error) {
618 			dev_err(&client->dev,
619 				"failed to read 'hello' packet: %d\n", error);
620 		} else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) {
621 			ts->iap_mode = ELAN_IAP_OPERATIONAL;
622 			break;
623 		} else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) {
624 			/*
625 			 * Setting error code will mark device
626 			 * in recovery mode below.
627 			 */
628 			error = -EIO;
629 			break;
630 		} else {
631 			error = -EINVAL;
632 			dev_err(&client->dev,
633 				"invalid 'hello' packet: %*ph\n",
634 				(int)sizeof(buf), buf);
635 		}
636 	}
637 
638 	/* hw version is available even if device in recovery state */
639 	error2 = elants_i2c_query_hw_version(ts);
640 	if (!error2)
641 		error2 = elants_i2c_query_bc_version(ts);
642 	if (!error)
643 		error = error2;
644 
645 	if (!error)
646 		error = elants_i2c_query_fw_version(ts);
647 	if (!error)
648 		error = elants_i2c_query_test_version(ts);
649 
650 	switch (ts->chip_id) {
651 	case EKTH3500:
652 		if (!error)
653 			error = elants_i2c_query_ts_info_ekth(ts);
654 		break;
655 	case EKTF3624:
656 		if (!error)
657 			error = elants_i2c_query_ts_info_ektf(ts);
658 		break;
659 	default:
660 		BUG();
661 	}
662 
663 	if (error)
664 		ts->iap_mode = ELAN_IAP_RECOVERY;
665 
666 	return 0;
667 }
668 
669 /*
670  * Firmware update interface.
671  */
672 
673 static int elants_i2c_fw_write_page(struct i2c_client *client,
674 				    const void *page)
675 {
676 	const u8 ack_ok[] = { 0xaa, 0xaa };
677 	u8 buf[2];
678 	int retry;
679 	int error;
680 
681 	for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) {
682 		error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE);
683 		if (error) {
684 			dev_err(&client->dev,
685 				"IAP Write Page failed: %d\n", error);
686 			continue;
687 		}
688 
689 		error = elants_i2c_read(client, buf, 2);
690 		if (error) {
691 			dev_err(&client->dev,
692 				"IAP Ack read failed: %d\n", error);
693 			return error;
694 		}
695 
696 		if (!memcmp(buf, ack_ok, sizeof(ack_ok)))
697 			return 0;
698 
699 		error = -EIO;
700 		dev_err(&client->dev,
701 			"IAP Get Ack Error [%02x:%02x]\n",
702 			buf[0], buf[1]);
703 	}
704 
705 	return error;
706 }
707 
708 static int elants_i2c_validate_remark_id(struct elants_data *ts,
709 					 const struct firmware *fw)
710 {
711 	struct i2c_client *client = ts->client;
712 	int error;
713 	const u8 cmd[] = { CMD_HEADER_ROM_READ, 0x80, 0x1F, 0x00, 0x00, 0x21 };
714 	u8 resp[6] = { 0 };
715 	u16 ts_remark_id = 0;
716 	u16 fw_remark_id = 0;
717 
718 	/* Compare TS Remark ID and FW Remark ID */
719 	error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
720 					   resp, sizeof(resp),
721 					   1, "read Remark ID");
722 	if (error)
723 		return error;
724 
725 	ts_remark_id = get_unaligned_be16(&resp[3]);
726 
727 	fw_remark_id = get_unaligned_le16(&fw->data[fw->size - 4]);
728 
729 	if (fw_remark_id != ts_remark_id) {
730 		dev_err(&client->dev,
731 			"Remark ID Mismatched: ts_remark_id=0x%04x, fw_remark_id=0x%04x.\n",
732 			ts_remark_id, fw_remark_id);
733 		return -EINVAL;
734 	}
735 
736 	return 0;
737 }
738 
739 static int elants_i2c_do_update_firmware(struct i2c_client *client,
740 					 const struct firmware *fw,
741 					 bool force)
742 {
743 	struct elants_data *ts = i2c_get_clientdata(client);
744 	const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 };
745 	const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 };
746 	const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc };
747 	const u8 close_idle[] = { 0x54, 0x2c, 0x01, 0x01 };
748 	u8 buf[HEADER_SIZE];
749 	u16 send_id;
750 	int page, n_fw_pages;
751 	int error;
752 	bool check_remark_id = ts->iap_version >= 0x60;
753 
754 	/* Recovery mode detection! */
755 	if (force) {
756 		dev_dbg(&client->dev, "Recovery mode procedure\n");
757 
758 		if (check_remark_id) {
759 			error = elants_i2c_validate_remark_id(ts, fw);
760 			if (error)
761 				return error;
762 		}
763 
764 		error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2));
765 		if (error) {
766 			dev_err(&client->dev, "failed to enter IAP mode: %d\n",
767 				error);
768 			return error;
769 		}
770 	} else {
771 		/* Start IAP Procedure */
772 		dev_dbg(&client->dev, "Normal IAP procedure\n");
773 
774 		/* Close idle mode */
775 		error = elants_i2c_send(client, close_idle, sizeof(close_idle));
776 		if (error)
777 			dev_err(&client->dev, "Failed close idle: %d\n", error);
778 		msleep(60);
779 
780 		elants_i2c_sw_reset(client);
781 		msleep(20);
782 
783 		if (check_remark_id) {
784 			error = elants_i2c_validate_remark_id(ts, fw);
785 			if (error)
786 				return error;
787 		}
788 
789 		error = elants_i2c_send(client, enter_iap, sizeof(enter_iap));
790 		if (error) {
791 			dev_err(&client->dev, "failed to enter IAP mode: %d\n",
792 				error);
793 			return error;
794 		}
795 	}
796 
797 	msleep(20);
798 
799 	/* check IAP state */
800 	error = elants_i2c_read(client, buf, 4);
801 	if (error) {
802 		dev_err(&client->dev,
803 			"failed to read IAP acknowledgement: %d\n",
804 			error);
805 		return error;
806 	}
807 
808 	if (memcmp(buf, iap_ack, sizeof(iap_ack))) {
809 		dev_err(&client->dev,
810 			"failed to enter IAP: %*ph (expected %*ph)\n",
811 			(int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack);
812 		return -EIO;
813 	}
814 
815 	dev_info(&client->dev, "successfully entered IAP mode");
816 
817 	send_id = client->addr;
818 	error = elants_i2c_send(client, &send_id, 1);
819 	if (error) {
820 		dev_err(&client->dev, "sending dummy byte failed: %d\n",
821 			error);
822 		return error;
823 	}
824 
825 	/* Clear the last page of Master */
826 	error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE);
827 	if (error) {
828 		dev_err(&client->dev, "clearing of the last page failed: %d\n",
829 			error);
830 		return error;
831 	}
832 
833 	error = elants_i2c_read(client, buf, 2);
834 	if (error) {
835 		dev_err(&client->dev,
836 			"failed to read ACK for clearing the last page: %d\n",
837 			error);
838 		return error;
839 	}
840 
841 	n_fw_pages = fw->size / ELAN_FW_PAGESIZE;
842 	dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages);
843 
844 	for (page = 0; page < n_fw_pages; page++) {
845 		error = elants_i2c_fw_write_page(client,
846 					fw->data + page * ELAN_FW_PAGESIZE);
847 		if (error) {
848 			dev_err(&client->dev,
849 				"failed to write FW page %d: %d\n",
850 				page, error);
851 			return error;
852 		}
853 	}
854 
855 	/* Old iap needs to wait 200ms for WDT and rest is for hello packets */
856 	msleep(300);
857 
858 	dev_info(&client->dev, "firmware update completed\n");
859 	return 0;
860 }
861 
862 static int elants_i2c_fw_update(struct elants_data *ts)
863 {
864 	struct i2c_client *client = ts->client;
865 	const struct firmware *fw;
866 	char *fw_name;
867 	int error;
868 
869 	fw_name = kasprintf(GFP_KERNEL, "elants_i2c_%04x.bin", ts->hw_version);
870 	if (!fw_name)
871 		return -ENOMEM;
872 
873 	dev_info(&client->dev, "requesting fw name = %s\n", fw_name);
874 	error = request_firmware(&fw, fw_name, &client->dev);
875 	kfree(fw_name);
876 	if (error) {
877 		dev_err(&client->dev, "failed to request firmware: %d\n",
878 			error);
879 		return error;
880 	}
881 
882 	if (fw->size % ELAN_FW_PAGESIZE) {
883 		dev_err(&client->dev, "invalid firmware length: %zu\n",
884 			fw->size);
885 		error = -EINVAL;
886 		goto out;
887 	}
888 
889 	disable_irq(client->irq);
890 
891 	error = elants_i2c_do_update_firmware(client, fw,
892 					ts->iap_mode == ELAN_IAP_RECOVERY);
893 	if (error) {
894 		dev_err(&client->dev, "firmware update failed: %d\n", error);
895 		ts->iap_mode = ELAN_IAP_RECOVERY;
896 		goto out_enable_irq;
897 	}
898 
899 	error = elants_i2c_initialize(ts);
900 	if (error) {
901 		dev_err(&client->dev,
902 			"failed to initialize device after firmware update: %d\n",
903 			error);
904 		ts->iap_mode = ELAN_IAP_RECOVERY;
905 		goto out_enable_irq;
906 	}
907 
908 	ts->iap_mode = ELAN_IAP_OPERATIONAL;
909 
910 out_enable_irq:
911 	ts->state = ELAN_STATE_NORMAL;
912 	enable_irq(client->irq);
913 	msleep(100);
914 
915 	if (!error)
916 		elants_i2c_calibrate(ts);
917 out:
918 	release_firmware(fw);
919 	return error;
920 }
921 
922 /*
923  * Event reporting.
924  */
925 
926 static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf,
927 				size_t packet_size)
928 {
929 	struct input_dev *input = ts->input;
930 	unsigned int n_fingers;
931 	unsigned int tool_type;
932 	u16 finger_state;
933 	int i;
934 
935 	n_fingers = buf[FW_POS_STATE + 1] & 0x0f;
936 	finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) |
937 			buf[FW_POS_STATE];
938 
939 	dev_dbg(&ts->client->dev,
940 		"n_fingers: %u, state: %04x\n",  n_fingers, finger_state);
941 
942 	/* Note: all fingers have the same tool type */
943 	tool_type = buf[FW_POS_TOOL_TYPE] & BIT(0) ?
944 			MT_TOOL_FINGER : MT_TOOL_PALM;
945 
946 	for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) {
947 		if (finger_state & 1) {
948 			unsigned int x, y, p, w;
949 			u8 *pos;
950 
951 			pos = &buf[FW_POS_XY + i * 3];
952 			x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
953 			y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
954 
955 			/*
956 			 * eKTF3624 may have use "old" touch-report format,
957 			 * depending on a device and TS firmware version.
958 			 * For example, ASUS Transformer devices use the "old"
959 			 * format, while ASUS Nexus 7 uses the "new" formant.
960 			 */
961 			if (packet_size == PACKET_SIZE_OLD &&
962 			    ts->chip_id == EKTF3624) {
963 				w = buf[FW_POS_WIDTH + i / 2];
964 				w >>= 4 * (~i & 1);
965 				w |= w << 4;
966 				w |= !w;
967 				p = w;
968 			} else {
969 				p = buf[FW_POS_PRESSURE + i];
970 				w = buf[FW_POS_WIDTH + i];
971 			}
972 
973 			dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
974 				i, x, y, p, w);
975 
976 			input_mt_slot(input, i);
977 			input_mt_report_slot_state(input, tool_type, true);
978 			touchscreen_report_pos(input, &ts->prop, x, y, true);
979 			input_event(input, EV_ABS, ABS_MT_PRESSURE, p);
980 			input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w);
981 
982 			n_fingers--;
983 		}
984 
985 		finger_state >>= 1;
986 	}
987 
988 	input_mt_sync_frame(input);
989 	input_sync(input);
990 }
991 
992 static u8 elants_i2c_calculate_checksum(u8 *buf)
993 {
994 	u8 checksum = 0;
995 	u8 i;
996 
997 	for (i = 0; i < FW_POS_CHECKSUM; i++)
998 		checksum += buf[i];
999 
1000 	return checksum;
1001 }
1002 
1003 static void elants_i2c_event(struct elants_data *ts, u8 *buf,
1004 			     size_t packet_size)
1005 {
1006 	u8 checksum = elants_i2c_calculate_checksum(buf);
1007 
1008 	if (unlikely(buf[FW_POS_CHECKSUM] != checksum))
1009 		dev_warn(&ts->client->dev,
1010 			 "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
1011 			 __func__, buf[FW_POS_HEADER],
1012 			 checksum, buf[FW_POS_CHECKSUM]);
1013 	else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER))
1014 		dev_warn(&ts->client->dev,
1015 			 "%s: unknown packet type: %02x\n",
1016 			 __func__, buf[FW_POS_HEADER]);
1017 	else
1018 		elants_i2c_mt_event(ts, buf, packet_size);
1019 }
1020 
1021 static irqreturn_t elants_i2c_irq(int irq, void *_dev)
1022 {
1023 	const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 };
1024 	struct elants_data *ts = _dev;
1025 	struct i2c_client *client = ts->client;
1026 	int report_count, report_len;
1027 	int i;
1028 	int len;
1029 
1030 	len = i2c_master_recv_dmasafe(client, ts->buf, sizeof(ts->buf));
1031 	if (len < 0) {
1032 		dev_err(&client->dev, "%s: failed to read data: %d\n",
1033 			__func__, len);
1034 		goto out;
1035 	}
1036 
1037 	dev_dbg(&client->dev, "%s: packet %*ph\n",
1038 		__func__, HEADER_SIZE, ts->buf);
1039 
1040 	switch (ts->state) {
1041 	case ELAN_WAIT_RECALIBRATION:
1042 		if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) {
1043 			memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp));
1044 			complete(&ts->cmd_done);
1045 			ts->state = ELAN_STATE_NORMAL;
1046 		}
1047 		break;
1048 
1049 	case ELAN_WAIT_QUEUE_HEADER:
1050 		if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL)
1051 			break;
1052 
1053 		ts->state = ELAN_STATE_NORMAL;
1054 		fallthrough;
1055 
1056 	case ELAN_STATE_NORMAL:
1057 
1058 		switch (ts->buf[FW_HDR_TYPE]) {
1059 		case CMD_HEADER_HELLO:
1060 		case CMD_HEADER_RESP:
1061 			break;
1062 
1063 		case QUEUE_HEADER_WAIT:
1064 			if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) {
1065 				dev_err(&client->dev,
1066 					"invalid wait packet %*ph\n",
1067 					HEADER_SIZE, ts->buf);
1068 			} else {
1069 				ts->state = ELAN_WAIT_QUEUE_HEADER;
1070 				udelay(30);
1071 			}
1072 			break;
1073 
1074 		case QUEUE_HEADER_SINGLE:
1075 			elants_i2c_event(ts, &ts->buf[HEADER_SIZE],
1076 					 ts->buf[FW_HDR_LENGTH]);
1077 			break;
1078 
1079 		case QUEUE_HEADER_NORMAL2: /* CMD_HEADER_REK */
1080 			/*
1081 			 * Depending on firmware version, eKTF3624 touchscreens
1082 			 * may utilize one of these opcodes for the touch events:
1083 			 * 0x63 (NORMAL) and 0x66 (NORMAL2).  The 0x63 is used by
1084 			 * older firmware version and differs from 0x66 such that
1085 			 * touch pressure value needs to be adjusted.  The 0x66
1086 			 * opcode of newer firmware is equal to 0x63 of eKTH3500.
1087 			 */
1088 			if (ts->chip_id != EKTF3624)
1089 				break;
1090 
1091 			fallthrough;
1092 
1093 		case QUEUE_HEADER_NORMAL:
1094 			report_count = ts->buf[FW_HDR_COUNT];
1095 			if (report_count == 0 || report_count > 3) {
1096 				dev_err(&client->dev,
1097 					"bad report count: %*ph\n",
1098 					HEADER_SIZE, ts->buf);
1099 				break;
1100 			}
1101 
1102 			report_len = ts->buf[FW_HDR_LENGTH] / report_count;
1103 
1104 			if (report_len == PACKET_SIZE_OLD &&
1105 			    ts->chip_id == EKTF3624) {
1106 				dev_dbg_once(&client->dev,
1107 					     "using old report format\n");
1108 			} else if (report_len != PACKET_SIZE) {
1109 				dev_err(&client->dev,
1110 					"mismatching report length: %*ph\n",
1111 					HEADER_SIZE, ts->buf);
1112 				break;
1113 			}
1114 
1115 			for (i = 0; i < report_count; i++) {
1116 				u8 *buf = ts->buf + HEADER_SIZE +
1117 							i * report_len;
1118 				elants_i2c_event(ts, buf, report_len);
1119 			}
1120 			break;
1121 
1122 		default:
1123 			dev_err(&client->dev, "unknown packet %*ph\n",
1124 				HEADER_SIZE, ts->buf);
1125 			break;
1126 		}
1127 		break;
1128 	}
1129 
1130 out:
1131 	return IRQ_HANDLED;
1132 }
1133 
1134 /*
1135  * sysfs interface
1136  */
1137 static ssize_t calibrate_store(struct device *dev,
1138 			       struct device_attribute *attr,
1139 			       const char *buf, size_t count)
1140 {
1141 	struct i2c_client *client = to_i2c_client(dev);
1142 	struct elants_data *ts = i2c_get_clientdata(client);
1143 	int error;
1144 
1145 	error = mutex_lock_interruptible(&ts->sysfs_mutex);
1146 	if (error)
1147 		return error;
1148 
1149 	error = elants_i2c_calibrate(ts);
1150 
1151 	mutex_unlock(&ts->sysfs_mutex);
1152 	return error ?: count;
1153 }
1154 
1155 static ssize_t write_update_fw(struct device *dev,
1156 			       struct device_attribute *attr,
1157 			       const char *buf, size_t count)
1158 {
1159 	struct i2c_client *client = to_i2c_client(dev);
1160 	struct elants_data *ts = i2c_get_clientdata(client);
1161 	int error;
1162 
1163 	error = mutex_lock_interruptible(&ts->sysfs_mutex);
1164 	if (error)
1165 		return error;
1166 
1167 	error = elants_i2c_fw_update(ts);
1168 	dev_dbg(dev, "firmware update result: %d\n", error);
1169 
1170 	mutex_unlock(&ts->sysfs_mutex);
1171 	return error ?: count;
1172 }
1173 
1174 static ssize_t show_iap_mode(struct device *dev,
1175 			     struct device_attribute *attr, char *buf)
1176 {
1177 	struct i2c_client *client = to_i2c_client(dev);
1178 	struct elants_data *ts = i2c_get_clientdata(client);
1179 
1180 	return sprintf(buf, "%s\n",
1181 		       ts->iap_mode == ELAN_IAP_OPERATIONAL ?
1182 				"Normal" : "Recovery");
1183 }
1184 
1185 static ssize_t show_calibration_count(struct device *dev,
1186 				      struct device_attribute *attr, char *buf)
1187 {
1188 	struct i2c_client *client = to_i2c_client(dev);
1189 	const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_REK, 0x00, 0x01 };
1190 	u8 resp[HEADER_SIZE];
1191 	u16 rek_count;
1192 	int error;
1193 
1194 	error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
1195 					   resp, sizeof(resp), 1,
1196 					   "read ReK status");
1197 	if (error)
1198 		return sprintf(buf, "%d\n", error);
1199 
1200 	rek_count = get_unaligned_be16(&resp[2]);
1201 	return sprintf(buf, "0x%04x\n", rek_count);
1202 }
1203 
1204 static DEVICE_ATTR_WO(calibrate);
1205 static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL);
1206 static DEVICE_ATTR(calibration_count, S_IRUGO, show_calibration_count, NULL);
1207 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw);
1208 
1209 struct elants_version_attribute {
1210 	struct device_attribute dattr;
1211 	size_t field_offset;
1212 	size_t field_size;
1213 };
1214 
1215 #define __ELANTS_FIELD_SIZE(_field)					\
1216 	sizeof(((struct elants_data *)NULL)->_field)
1217 #define __ELANTS_VERIFY_SIZE(_field)					\
1218 	(BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) +		\
1219 	 __ELANTS_FIELD_SIZE(_field))
1220 #define ELANTS_VERSION_ATTR(_field)					\
1221 	struct elants_version_attribute elants_ver_attr_##_field = {	\
1222 		.dattr = __ATTR(_field, S_IRUGO,			\
1223 				elants_version_attribute_show, NULL),	\
1224 		.field_offset = offsetof(struct elants_data, _field),	\
1225 		.field_size = __ELANTS_VERIFY_SIZE(_field),		\
1226 	}
1227 
1228 static ssize_t elants_version_attribute_show(struct device *dev,
1229 					     struct device_attribute *dattr,
1230 					     char *buf)
1231 {
1232 	struct i2c_client *client = to_i2c_client(dev);
1233 	struct elants_data *ts = i2c_get_clientdata(client);
1234 	struct elants_version_attribute *attr =
1235 		container_of(dattr, struct elants_version_attribute, dattr);
1236 	u8 *field = (u8 *)((char *)ts + attr->field_offset);
1237 	unsigned int fmt_size;
1238 	unsigned int val;
1239 
1240 	if (attr->field_size == 1) {
1241 		val = *field;
1242 		fmt_size = 2; /* 2 HEX digits */
1243 	} else {
1244 		val = *(u16 *)field;
1245 		fmt_size = 4; /* 4 HEX digits */
1246 	}
1247 
1248 	return sprintf(buf, "%0*x\n", fmt_size, val);
1249 }
1250 
1251 static ELANTS_VERSION_ATTR(fw_version);
1252 static ELANTS_VERSION_ATTR(hw_version);
1253 static ELANTS_VERSION_ATTR(test_version);
1254 static ELANTS_VERSION_ATTR(solution_version);
1255 static ELANTS_VERSION_ATTR(bc_version);
1256 static ELANTS_VERSION_ATTR(iap_version);
1257 
1258 static struct attribute *elants_attributes[] = {
1259 	&dev_attr_calibrate.attr,
1260 	&dev_attr_update_fw.attr,
1261 	&dev_attr_iap_mode.attr,
1262 	&dev_attr_calibration_count.attr,
1263 
1264 	&elants_ver_attr_fw_version.dattr.attr,
1265 	&elants_ver_attr_hw_version.dattr.attr,
1266 	&elants_ver_attr_test_version.dattr.attr,
1267 	&elants_ver_attr_solution_version.dattr.attr,
1268 	&elants_ver_attr_bc_version.dattr.attr,
1269 	&elants_ver_attr_iap_version.dattr.attr,
1270 	NULL
1271 };
1272 
1273 static const struct attribute_group elants_attribute_group = {
1274 	.attrs = elants_attributes,
1275 };
1276 
1277 static int elants_i2c_power_on(struct elants_data *ts)
1278 {
1279 	int error;
1280 
1281 	/*
1282 	 * If we do not have reset gpio assume platform firmware
1283 	 * controls regulators and does power them on for us.
1284 	 */
1285 	if (IS_ERR_OR_NULL(ts->reset_gpio))
1286 		return 0;
1287 
1288 	gpiod_set_value_cansleep(ts->reset_gpio, 1);
1289 
1290 	error = regulator_enable(ts->vcc33);
1291 	if (error) {
1292 		dev_err(&ts->client->dev,
1293 			"failed to enable vcc33 regulator: %d\n",
1294 			error);
1295 		goto release_reset_gpio;
1296 	}
1297 
1298 	error = regulator_enable(ts->vccio);
1299 	if (error) {
1300 		dev_err(&ts->client->dev,
1301 			"failed to enable vccio regulator: %d\n",
1302 			error);
1303 		regulator_disable(ts->vcc33);
1304 		goto release_reset_gpio;
1305 	}
1306 
1307 	/*
1308 	 * We need to wait a bit after powering on controller before
1309 	 * we are allowed to release reset GPIO.
1310 	 */
1311 	udelay(ELAN_POWERON_DELAY_USEC);
1312 
1313 release_reset_gpio:
1314 	gpiod_set_value_cansleep(ts->reset_gpio, 0);
1315 	if (error)
1316 		return error;
1317 
1318 	msleep(ELAN_RESET_DELAY_MSEC);
1319 
1320 	return 0;
1321 }
1322 
1323 static void elants_i2c_power_off(void *_data)
1324 {
1325 	struct elants_data *ts = _data;
1326 
1327 	if (!IS_ERR_OR_NULL(ts->reset_gpio)) {
1328 		/*
1329 		 * Activate reset gpio to prevent leakage through the
1330 		 * pin once we shut off power to the controller.
1331 		 */
1332 		gpiod_set_value_cansleep(ts->reset_gpio, 1);
1333 		regulator_disable(ts->vccio);
1334 		regulator_disable(ts->vcc33);
1335 	}
1336 }
1337 
1338 #ifdef CONFIG_ACPI
1339 static const struct acpi_device_id i2c_hid_ids[] = {
1340 	{"ACPI0C50", 0 },
1341 	{"PNP0C50", 0 },
1342 	{ },
1343 };
1344 
1345 static const guid_t i2c_hid_guid =
1346 	GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
1347 		  0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
1348 
1349 static bool elants_acpi_is_hid_device(struct device *dev)
1350 {
1351 	acpi_handle handle = ACPI_HANDLE(dev);
1352 	union acpi_object *obj;
1353 
1354 	if (acpi_match_device_ids(ACPI_COMPANION(dev), i2c_hid_ids))
1355 		return false;
1356 
1357 	obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL, ACPI_TYPE_INTEGER);
1358 	if (obj) {
1359 		ACPI_FREE(obj);
1360 		return true;
1361 	}
1362 
1363 	return false;
1364 }
1365 #else
1366 static bool elants_acpi_is_hid_device(struct device *dev)
1367 {
1368 	return false;
1369 }
1370 #endif
1371 
1372 static int elants_i2c_probe(struct i2c_client *client)
1373 {
1374 	union i2c_smbus_data dummy;
1375 	struct elants_data *ts;
1376 	unsigned long irqflags;
1377 	int error;
1378 
1379 	/* Don't bind to i2c-hid compatible devices, these are handled by the i2c-hid drv. */
1380 	if (elants_acpi_is_hid_device(&client->dev)) {
1381 		dev_warn(&client->dev, "This device appears to be an I2C-HID device, not binding\n");
1382 		return -ENODEV;
1383 	}
1384 
1385 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1386 		dev_err(&client->dev, "I2C check functionality error\n");
1387 		return -ENXIO;
1388 	}
1389 
1390 	ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL);
1391 	if (!ts)
1392 		return -ENOMEM;
1393 
1394 	mutex_init(&ts->sysfs_mutex);
1395 	init_completion(&ts->cmd_done);
1396 
1397 	ts->client = client;
1398 	ts->chip_id = (enum elants_chip_id)(uintptr_t)device_get_match_data(&client->dev);
1399 	i2c_set_clientdata(client, ts);
1400 
1401 	ts->vcc33 = devm_regulator_get(&client->dev, "vcc33");
1402 	if (IS_ERR(ts->vcc33)) {
1403 		error = PTR_ERR(ts->vcc33);
1404 		if (error != -EPROBE_DEFER)
1405 			dev_err(&client->dev,
1406 				"Failed to get 'vcc33' regulator: %d\n",
1407 				error);
1408 		return error;
1409 	}
1410 
1411 	ts->vccio = devm_regulator_get(&client->dev, "vccio");
1412 	if (IS_ERR(ts->vccio)) {
1413 		error = PTR_ERR(ts->vccio);
1414 		if (error != -EPROBE_DEFER)
1415 			dev_err(&client->dev,
1416 				"Failed to get 'vccio' regulator: %d\n",
1417 				error);
1418 		return error;
1419 	}
1420 
1421 	ts->reset_gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW);
1422 	if (IS_ERR(ts->reset_gpio)) {
1423 		error = PTR_ERR(ts->reset_gpio);
1424 
1425 		if (error == -EPROBE_DEFER)
1426 			return error;
1427 
1428 		if (error != -ENOENT && error != -ENOSYS) {
1429 			dev_err(&client->dev,
1430 				"failed to get reset gpio: %d\n",
1431 				error);
1432 			return error;
1433 		}
1434 
1435 		ts->keep_power_in_suspend = true;
1436 	}
1437 
1438 	error = elants_i2c_power_on(ts);
1439 	if (error)
1440 		return error;
1441 
1442 	error = devm_add_action_or_reset(&client->dev,
1443 					 elants_i2c_power_off, ts);
1444 	if (error) {
1445 		dev_err(&client->dev,
1446 			"failed to install power off action: %d\n", error);
1447 		return error;
1448 	}
1449 
1450 	/* Make sure there is something at this address */
1451 	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1452 			   I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1453 		dev_err(&client->dev, "nothing at this address\n");
1454 		return -ENXIO;
1455 	}
1456 
1457 	error = elants_i2c_initialize(ts);
1458 	if (error) {
1459 		dev_err(&client->dev, "failed to initialize: %d\n", error);
1460 		return error;
1461 	}
1462 
1463 	ts->input = devm_input_allocate_device(&client->dev);
1464 	if (!ts->input) {
1465 		dev_err(&client->dev, "Failed to allocate input device\n");
1466 		return -ENOMEM;
1467 	}
1468 
1469 	ts->input->name = "Elan Touchscreen";
1470 	ts->input->id.bustype = BUS_I2C;
1471 
1472 	/* Multitouch input params setup */
1473 
1474 	input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
1475 	input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
1476 	input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1477 	input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1478 	input_set_abs_params(ts->input, ABS_MT_TOOL_TYPE,
1479 			     0, MT_TOOL_PALM, 0, 0);
1480 
1481 	touchscreen_parse_properties(ts->input, true, &ts->prop);
1482 
1483 	if (ts->chip_id == EKTF3624 && ts->phy_x && ts->phy_y) {
1484 		/* calculate resolution from size */
1485 		ts->x_res = DIV_ROUND_CLOSEST(ts->prop.max_x, ts->phy_x);
1486 		ts->y_res = DIV_ROUND_CLOSEST(ts->prop.max_y, ts->phy_y);
1487 	}
1488 
1489 	input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
1490 	input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
1491 	input_abs_set_res(ts->input, ABS_MT_TOUCH_MAJOR, ts->major_res);
1492 
1493 	error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
1494 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1495 	if (error) {
1496 		dev_err(&client->dev,
1497 			"failed to initialize MT slots: %d\n", error);
1498 		return error;
1499 	}
1500 
1501 	error = input_register_device(ts->input);
1502 	if (error) {
1503 		dev_err(&client->dev,
1504 			"unable to register input device: %d\n", error);
1505 		return error;
1506 	}
1507 
1508 	/*
1509 	 * Platform code (ACPI, DTS) should normally set up interrupt
1510 	 * for us, but in case it did not let's fall back to using falling
1511 	 * edge to be compatible with older Chromebooks.
1512 	 */
1513 	irqflags = irq_get_trigger_type(client->irq);
1514 	if (!irqflags)
1515 		irqflags = IRQF_TRIGGER_FALLING;
1516 
1517 	error = devm_request_threaded_irq(&client->dev, client->irq,
1518 					  NULL, elants_i2c_irq,
1519 					  irqflags | IRQF_ONESHOT,
1520 					  client->name, ts);
1521 	if (error) {
1522 		dev_err(&client->dev, "Failed to register interrupt\n");
1523 		return error;
1524 	}
1525 
1526 	/*
1527 	 * Systems using device tree should set up wakeup via DTS,
1528 	 * the rest will configure device as wakeup source by default.
1529 	 */
1530 	if (!client->dev.of_node)
1531 		device_init_wakeup(&client->dev, true);
1532 
1533 	error = devm_device_add_group(&client->dev, &elants_attribute_group);
1534 	if (error) {
1535 		dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1536 			error);
1537 		return error;
1538 	}
1539 
1540 	return 0;
1541 }
1542 
1543 static int __maybe_unused elants_i2c_suspend(struct device *dev)
1544 {
1545 	struct i2c_client *client = to_i2c_client(dev);
1546 	struct elants_data *ts = i2c_get_clientdata(client);
1547 	const u8 set_sleep_cmd[] = {
1548 		CMD_HEADER_WRITE, E_POWER_STATE_SLEEP, 0x00, 0x01
1549 	};
1550 	int retry_cnt;
1551 	int error;
1552 
1553 	/* Command not support in IAP recovery mode */
1554 	if (ts->iap_mode != ELAN_IAP_OPERATIONAL)
1555 		return -EBUSY;
1556 
1557 	disable_irq(client->irq);
1558 
1559 	if (device_may_wakeup(dev)) {
1560 		/*
1561 		 * The device will automatically enter idle mode
1562 		 * that has reduced power consumption.
1563 		 */
1564 		ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1565 	} else if (ts->keep_power_in_suspend) {
1566 		for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1567 			error = elants_i2c_send(client, set_sleep_cmd,
1568 						sizeof(set_sleep_cmd));
1569 			if (!error)
1570 				break;
1571 
1572 			dev_err(&client->dev,
1573 				"suspend command failed: %d\n", error);
1574 		}
1575 	} else {
1576 		elants_i2c_power_off(ts);
1577 	}
1578 
1579 	return 0;
1580 }
1581 
1582 static int __maybe_unused elants_i2c_resume(struct device *dev)
1583 {
1584 	struct i2c_client *client = to_i2c_client(dev);
1585 	struct elants_data *ts = i2c_get_clientdata(client);
1586 	const u8 set_active_cmd[] = {
1587 		CMD_HEADER_WRITE, E_POWER_STATE_RESUME, 0x00, 0x01
1588 	};
1589 	int retry_cnt;
1590 	int error;
1591 
1592 	if (device_may_wakeup(dev)) {
1593 		if (ts->wake_irq_enabled)
1594 			disable_irq_wake(client->irq);
1595 		elants_i2c_sw_reset(client);
1596 	} else if (ts->keep_power_in_suspend) {
1597 		for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1598 			error = elants_i2c_send(client, set_active_cmd,
1599 						sizeof(set_active_cmd));
1600 			if (!error)
1601 				break;
1602 
1603 			dev_err(&client->dev,
1604 				"resume command failed: %d\n", error);
1605 		}
1606 	} else {
1607 		elants_i2c_power_on(ts);
1608 		elants_i2c_initialize(ts);
1609 	}
1610 
1611 	ts->state = ELAN_STATE_NORMAL;
1612 	enable_irq(client->irq);
1613 
1614 	return 0;
1615 }
1616 
1617 static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
1618 			 elants_i2c_suspend, elants_i2c_resume);
1619 
1620 static const struct i2c_device_id elants_i2c_id[] = {
1621 	{ DEVICE_NAME, EKTH3500 },
1622 	{ "ekth3500", EKTH3500 },
1623 	{ "ektf3624", EKTF3624 },
1624 	{ }
1625 };
1626 MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
1627 
1628 #ifdef CONFIG_ACPI
1629 static const struct acpi_device_id elants_acpi_id[] = {
1630 	{ "ELAN0001", EKTH3500 },
1631 	{ }
1632 };
1633 MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
1634 #endif
1635 
1636 #ifdef CONFIG_OF
1637 static const struct of_device_id elants_of_match[] = {
1638 	{ .compatible = "elan,ekth3500", .data = (void *)EKTH3500 },
1639 	{ .compatible = "elan,ektf3624", .data = (void *)EKTF3624 },
1640 	{ /* sentinel */ }
1641 };
1642 MODULE_DEVICE_TABLE(of, elants_of_match);
1643 #endif
1644 
1645 static struct i2c_driver elants_i2c_driver = {
1646 	.probe_new = elants_i2c_probe,
1647 	.id_table = elants_i2c_id,
1648 	.driver = {
1649 		.name = DEVICE_NAME,
1650 		.pm = &elants_i2c_pm_ops,
1651 		.acpi_match_table = ACPI_PTR(elants_acpi_id),
1652 		.of_match_table = of_match_ptr(elants_of_match),
1653 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1654 	},
1655 };
1656 module_i2c_driver(elants_i2c_driver);
1657 
1658 MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
1659 MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
1660 MODULE_LICENSE("GPL");
1661