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