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