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