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