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