linux/drivers/input/touchscreen/raydium_i2c_ts.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Raydium touchscreen I2C driver.
   4 *
   5 * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
   6 *
   7 * Raydium reserves the right to make changes without further notice
   8 * to the materials described herein. Raydium does not assume any
   9 * liability arising out of the application described herein.
  10 *
  11 * Contact Raydium Semiconductor Corporation at www.rad-ic.com
  12 */
  13
  14#include <linux/acpi.h>
  15#include <linux/delay.h>
  16#include <linux/firmware.h>
  17#include <linux/gpio/consumer.h>
  18#include <linux/i2c.h>
  19#include <linux/input.h>
  20#include <linux/input/mt.h>
  21#include <linux/interrupt.h>
  22#include <linux/module.h>
  23#include <linux/of.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/slab.h>
  26#include <asm/unaligned.h>
  27
  28/* Slave I2C mode */
  29#define RM_BOOT_BLDR            0x02
  30#define RM_BOOT_MAIN            0x03
  31
  32/* I2C bootoloader commands */
  33#define RM_CMD_BOOT_PAGE_WRT    0x0B            /* send bl page write */
  34#define RM_CMD_BOOT_WRT         0x11            /* send bl write */
  35#define RM_CMD_BOOT_ACK         0x22            /* send ack*/
  36#define RM_CMD_BOOT_CHK         0x33            /* send data check */
  37#define RM_CMD_BOOT_READ        0x44            /* send wait bl data ready*/
  38
  39#define RM_BOOT_RDY             0xFF            /* bl data ready */
  40#define RM_BOOT_CMD_READHWID    0x0E            /* read hwid */
  41
  42/* I2C main commands */
  43#define RM_CMD_QUERY_BANK       0x2B
  44#define RM_CMD_DATA_BANK        0x4D
  45#define RM_CMD_ENTER_SLEEP      0x4E
  46#define RM_CMD_BANK_SWITCH      0xAA
  47
  48#define RM_RESET_MSG_ADDR       0x40000004
  49
  50#define RM_MAX_READ_SIZE        56
  51#define RM_PACKET_CRC_SIZE      2
  52
  53/* Touch relative info */
  54#define RM_MAX_RETRIES          3
  55#define RM_RETRY_DELAY_MS       20
  56#define RM_MAX_TOUCH_NUM        10
  57#define RM_BOOT_DELAY_MS        100
  58
  59/* Offsets in contact data */
  60#define RM_CONTACT_STATE_POS    0
  61#define RM_CONTACT_X_POS        1
  62#define RM_CONTACT_Y_POS        3
  63#define RM_CONTACT_PRESSURE_POS 5
  64#define RM_CONTACT_WIDTH_X_POS  6
  65#define RM_CONTACT_WIDTH_Y_POS  7
  66
  67/* Bootloader relative info */
  68#define RM_BL_WRT_CMD_SIZE      3       /* bl flash wrt cmd size */
  69#define RM_BL_WRT_PKG_SIZE      32      /* bl wrt pkg size */
  70#define RM_BL_WRT_LEN           (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
  71#define RM_FW_PAGE_SIZE         128
  72#define RM_MAX_FW_RETRIES       30
  73#define RM_MAX_FW_SIZE          0xD000
  74
  75#define RM_POWERON_DELAY_USEC   500
  76#define RM_RESET_DELAY_MSEC     50
  77
  78enum raydium_bl_cmd {
  79        BL_HEADER = 0,
  80        BL_PAGE_STR,
  81        BL_PKG_IDX,
  82        BL_DATA_STR,
  83};
  84
  85enum raydium_bl_ack {
  86        RAYDIUM_ACK_NULL = 0,
  87        RAYDIUM_WAIT_READY,
  88        RAYDIUM_PATH_READY,
  89};
  90
  91enum raydium_boot_mode {
  92        RAYDIUM_TS_MAIN = 0,
  93        RAYDIUM_TS_BLDR,
  94};
  95
  96/* Response to RM_CMD_DATA_BANK request */
  97struct raydium_data_info {
  98        __le32 data_bank_addr;
  99        u8 pkg_size;
 100        u8 tp_info_size;
 101};
 102
 103struct raydium_info {
 104        __le32 hw_ver;          /*device version */
 105        u8 main_ver;
 106        u8 sub_ver;
 107        __le16 ft_ver;          /* test version */
 108        u8 x_num;
 109        u8 y_num;
 110        __le16 x_max;
 111        __le16 y_max;
 112        u8 x_res;               /* units/mm */
 113        u8 y_res;               /* units/mm */
 114};
 115
 116/* struct raydium_data - represents state of Raydium touchscreen device */
 117struct raydium_data {
 118        struct i2c_client *client;
 119        struct input_dev *input;
 120
 121        struct regulator *avdd;
 122        struct regulator *vccio;
 123        struct gpio_desc *reset_gpio;
 124
 125        struct raydium_info info;
 126
 127        struct mutex sysfs_mutex;
 128
 129        u8 *report_data;
 130
 131        u32 data_bank_addr;
 132        u8 report_size;
 133        u8 contact_size;
 134        u8 pkg_size;
 135
 136        enum raydium_boot_mode boot_mode;
 137
 138        bool wake_irq_enabled;
 139};
 140
 141/*
 142 * Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
 143 * raydium_i2c_{read|send} below.
 144 */
 145struct __packed raydium_bank_switch_header {
 146        u8 cmd;
 147        __be32 be_addr;
 148};
 149
 150static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
 151                            struct i2c_msg *xfer, size_t xfer_count)
 152{
 153        int ret;
 154        /*
 155         * If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
 156         * sent first. Else, skip the header i.e. xfer[0].
 157         */
 158        int xfer_start_idx = (addr > 0xff) ? 0 : 1;
 159        xfer_count -= xfer_start_idx;
 160
 161        ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
 162        if (likely(ret == xfer_count))
 163                return 0;
 164
 165        return ret < 0 ? ret : -EIO;
 166}
 167
 168static int raydium_i2c_send(struct i2c_client *client,
 169                            u32 addr, const void *data, size_t len)
 170{
 171        int tries = 0;
 172        int error;
 173        u8 *tx_buf;
 174        u8 reg_addr = addr & 0xff;
 175
 176        tx_buf = kmalloc(len + 1, GFP_KERNEL);
 177        if (!tx_buf)
 178                return -ENOMEM;
 179
 180        tx_buf[0] = reg_addr;
 181        memcpy(tx_buf + 1, data, len);
 182
 183        do {
 184                struct raydium_bank_switch_header header = {
 185                        .cmd = RM_CMD_BANK_SWITCH,
 186                        .be_addr = cpu_to_be32(addr),
 187                };
 188
 189                /*
 190                 * Perform as a single i2c_transfer transaction to ensure that
 191                 * no other I2C transactions are initiated on the bus to any
 192                 * other device in between. Initiating transacations to other
 193                 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
 194                 * issues. This is also why regmap infrastructure cannot be used
 195                 * for this driver. Regmap handles page(bank) switch and reads
 196                 * as separate i2c_transfer() operations. This can result in
 197                 * problems if the Raydium device is on a shared I2C bus.
 198                 */
 199                struct i2c_msg xfer[] = {
 200                        {
 201                                .addr = client->addr,
 202                                .len = sizeof(header),
 203                                .buf = (u8 *)&header,
 204                        },
 205                        {
 206                                .addr = client->addr,
 207                                .len = len + 1,
 208                                .buf = tx_buf,
 209                        },
 210                };
 211
 212                error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
 213                if (likely(!error))
 214                        return 0;
 215
 216                msleep(RM_RETRY_DELAY_MS);
 217        } while (++tries < RM_MAX_RETRIES);
 218
 219        dev_err(&client->dev, "%s failed: %d\n", __func__, error);
 220        return error;
 221}
 222
 223static int raydium_i2c_read(struct i2c_client *client,
 224                            u32 addr, void *data, size_t len)
 225{
 226        int error;
 227
 228        while (len) {
 229                u8 reg_addr = addr & 0xff;
 230                struct raydium_bank_switch_header header = {
 231                        .cmd = RM_CMD_BANK_SWITCH,
 232                        .be_addr = cpu_to_be32(addr),
 233                };
 234                size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
 235
 236                /*
 237                 * Perform as a single i2c_transfer transaction to ensure that
 238                 * no other I2C transactions are initiated on the bus to any
 239                 * other device in between. Initiating transacations to other
 240                 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
 241                 * issues. This is also why regmap infrastructure cannot be used
 242                 * for this driver. Regmap handles page(bank) switch and writes
 243                 * as separate i2c_transfer() operations. This can result in
 244                 * problems if the Raydium device is on a shared I2C bus.
 245                 */
 246                struct i2c_msg xfer[] = {
 247                        {
 248                                .addr = client->addr,
 249                                .len = sizeof(header),
 250                                .buf = (u8 *)&header,
 251                        },
 252                        {
 253                                .addr = client->addr,
 254                                .len = 1,
 255                                .buf = &reg_addr,
 256                        },
 257                        {
 258                                .addr = client->addr,
 259                                .len = xfer_len,
 260                                .buf = data,
 261                                .flags = I2C_M_RD,
 262                        }
 263                };
 264
 265                error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
 266                if (unlikely(error))
 267                        return error;
 268
 269                len -= xfer_len;
 270                data += xfer_len;
 271                addr += xfer_len;
 272        }
 273
 274        return 0;
 275}
 276
 277static int raydium_i2c_sw_reset(struct i2c_client *client)
 278{
 279        const u8 soft_rst_cmd = 0x01;
 280        int error;
 281
 282        error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
 283                                 sizeof(soft_rst_cmd));
 284        if (error) {
 285                dev_err(&client->dev, "software reset failed: %d\n", error);
 286                return error;
 287        }
 288
 289        msleep(RM_RESET_DELAY_MSEC);
 290
 291        return 0;
 292}
 293
 294static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
 295{
 296        struct i2c_client *client = ts->client;
 297        static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
 298                                       0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
 299        u8 rbuf[5] = { 0 };
 300        u32 hw_ver;
 301        int error;
 302
 303        error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
 304                                 get_hwid, sizeof(get_hwid));
 305        if (error) {
 306                dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
 307                return error;
 308        }
 309
 310        error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
 311        if (error) {
 312                dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
 313                return error;
 314        }
 315
 316        error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
 317        if (error) {
 318                dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
 319                        error, rbuf + 1);
 320                hw_ver = 0xffffffffUL;
 321        } else {
 322                hw_ver = get_unaligned_be32(rbuf + 1);
 323        }
 324
 325        ts->info.hw_ver = cpu_to_le32(hw_ver);
 326        ts->info.main_ver = 0xff;
 327        ts->info.sub_ver = 0xff;
 328
 329        return error;
 330}
 331
 332static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 333{
 334        struct i2c_client *client = ts->client;
 335        struct raydium_data_info data_info;
 336        __le32 query_bank_addr;
 337
 338        int error, retry_cnt;
 339
 340        for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
 341                error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
 342                                         &data_info, sizeof(data_info));
 343                if (error)
 344                        continue;
 345
 346                /*
 347                 * Warn user if we already allocated memory for reports and
 348                 * then the size changed (due to firmware update?) and keep
 349                 * old size instead.
 350                 */
 351                if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
 352                        dev_warn(&client->dev,
 353                                 "report size changes, was: %d, new: %d\n",
 354                                 ts->pkg_size, data_info.pkg_size);
 355                } else {
 356                        ts->pkg_size = data_info.pkg_size;
 357                        ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
 358                }
 359
 360                ts->contact_size = data_info.tp_info_size;
 361                ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
 362
 363                dev_dbg(&client->dev,
 364                        "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
 365                        ts->data_bank_addr, ts->report_size, ts->contact_size);
 366
 367                error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
 368                                         &query_bank_addr,
 369                                         sizeof(query_bank_addr));
 370                if (error)
 371                        continue;
 372
 373                error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
 374                                         &ts->info, sizeof(ts->info));
 375                if (error)
 376                        continue;
 377
 378                return 0;
 379        }
 380
 381        dev_err(&client->dev, "failed to query device parameters: %d\n", error);
 382        return error;
 383}
 384
 385static int raydium_i2c_check_fw_status(struct raydium_data *ts)
 386{
 387        struct i2c_client *client = ts->client;
 388        static const u8 bl_ack = 0x62;
 389        static const u8 main_ack = 0x66;
 390        u8 buf[4];
 391        int error;
 392
 393        error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
 394        if (!error) {
 395                if (buf[0] == bl_ack)
 396                        ts->boot_mode = RAYDIUM_TS_BLDR;
 397                else if (buf[0] == main_ack)
 398                        ts->boot_mode = RAYDIUM_TS_MAIN;
 399                return 0;
 400        }
 401
 402        return error;
 403}
 404
 405static int raydium_i2c_initialize(struct raydium_data *ts)
 406{
 407        struct i2c_client *client = ts->client;
 408        int error, retry_cnt;
 409
 410        for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
 411                /* Wait for Hello packet */
 412                msleep(RM_BOOT_DELAY_MS);
 413
 414                error = raydium_i2c_check_fw_status(ts);
 415                if (error) {
 416                        dev_err(&client->dev,
 417                                "failed to read 'hello' packet: %d\n", error);
 418                        continue;
 419                }
 420
 421                if (ts->boot_mode == RAYDIUM_TS_BLDR ||
 422                    ts->boot_mode == RAYDIUM_TS_MAIN) {
 423                        break;
 424                }
 425        }
 426
 427        if (error)
 428                ts->boot_mode = RAYDIUM_TS_BLDR;
 429
 430        if (ts->boot_mode == RAYDIUM_TS_BLDR)
 431                raydium_i2c_query_ts_bootloader_info(ts);
 432        else
 433                raydium_i2c_query_ts_info(ts);
 434
 435        return error;
 436}
 437
 438static int raydium_i2c_bl_chk_state(struct i2c_client *client,
 439                                    enum raydium_bl_ack state)
 440{
 441        static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
 442        u8 rbuf[sizeof(ack_ok)];
 443        u8 retry;
 444        int error;
 445
 446        for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
 447                switch (state) {
 448                case RAYDIUM_ACK_NULL:
 449                        return 0;
 450
 451                case RAYDIUM_WAIT_READY:
 452                        error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
 453                                                 &rbuf[0], 1);
 454                        if (!error && rbuf[0] == RM_BOOT_RDY)
 455                                return 0;
 456
 457                        break;
 458
 459                case RAYDIUM_PATH_READY:
 460                        error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
 461                                                 rbuf, sizeof(rbuf));
 462                        if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
 463                                return 0;
 464
 465                        break;
 466
 467                default:
 468                        dev_err(&client->dev, "%s: invalid target state %d\n",
 469                                __func__, state);
 470                        return -EINVAL;
 471                }
 472
 473                msleep(20);
 474        }
 475
 476        return -ETIMEDOUT;
 477}
 478
 479static int raydium_i2c_write_object(struct i2c_client *client,
 480                                    const void *data, size_t len,
 481                                    enum raydium_bl_ack state)
 482{
 483        int error;
 484        static const u8 cmd[] = { 0xFF, 0x39 };
 485
 486        error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
 487        if (error) {
 488                dev_err(&client->dev, "WRT obj command failed: %d\n",
 489                        error);
 490                return error;
 491        }
 492
 493        error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
 494        if (error) {
 495                dev_err(&client->dev, "Ack obj command failed: %d\n", error);
 496                return error;
 497        }
 498
 499        error = raydium_i2c_bl_chk_state(client, state);
 500        if (error) {
 501                dev_err(&client->dev, "BL check state failed: %d\n", error);
 502                return error;
 503        }
 504        return 0;
 505}
 506
 507static int raydium_i2c_boot_trigger(struct i2c_client *client)
 508{
 509        static const u8 cmd[7][6] = {
 510                { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
 511                { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
 512                { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
 513                { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
 514                { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
 515                { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
 516                { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
 517        };
 518        int i;
 519        int error;
 520
 521        for (i = 0; i < 7; i++) {
 522                error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
 523                                                 RAYDIUM_WAIT_READY);
 524                if (error) {
 525                        dev_err(&client->dev,
 526                                "boot trigger failed at step %d: %d\n",
 527                                i, error);
 528                        return error;
 529                }
 530        }
 531
 532        return 0;
 533}
 534
 535static int raydium_i2c_fw_trigger(struct i2c_client *client)
 536{
 537        static const u8 cmd[5][11] = {
 538                { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
 539                { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
 540                { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
 541                { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
 542                { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
 543        };
 544        int i;
 545        int error;
 546
 547        for (i = 0; i < 5; i++) {
 548                error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
 549                                                 RAYDIUM_ACK_NULL);
 550                if (error) {
 551                        dev_err(&client->dev,
 552                                "fw trigger failed at step %d: %d\n",
 553                                i, error);
 554                        return error;
 555                }
 556        }
 557
 558        return 0;
 559}
 560
 561static int raydium_i2c_check_path(struct i2c_client *client)
 562{
 563        static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
 564        int error;
 565
 566        error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
 567                                         RAYDIUM_PATH_READY);
 568        if (error) {
 569                dev_err(&client->dev, "check path command failed: %d\n", error);
 570                return error;
 571        }
 572
 573        return 0;
 574}
 575
 576static int raydium_i2c_enter_bl(struct i2c_client *client)
 577{
 578        static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
 579        int error;
 580
 581        error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
 582                                         RAYDIUM_ACK_NULL);
 583        if (error) {
 584                dev_err(&client->dev, "enter bl command failed: %d\n", error);
 585                return error;
 586        }
 587
 588        msleep(RM_BOOT_DELAY_MS);
 589        return 0;
 590}
 591
 592static int raydium_i2c_leave_bl(struct i2c_client *client)
 593{
 594        static const u8 leave_cmd[] = { 0x05, 0x00 };
 595        int error;
 596
 597        error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
 598                                         RAYDIUM_ACK_NULL);
 599        if (error) {
 600                dev_err(&client->dev, "leave bl command failed: %d\n", error);
 601                return error;
 602        }
 603
 604        msleep(RM_BOOT_DELAY_MS);
 605        return 0;
 606}
 607
 608static int raydium_i2c_write_checksum(struct i2c_client *client,
 609                                      size_t length, u16 checksum)
 610{
 611        u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
 612        int error;
 613
 614        put_unaligned_le16(length, &checksum_cmd[3]);
 615        put_unaligned_le16(checksum, &checksum_cmd[5]);
 616
 617        error = raydium_i2c_write_object(client,
 618                                         checksum_cmd, sizeof(checksum_cmd),
 619                                         RAYDIUM_ACK_NULL);
 620        if (error) {
 621                dev_err(&client->dev, "failed to write checksum: %d\n",
 622                        error);
 623                return error;
 624        }
 625
 626        return 0;
 627}
 628
 629static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
 630{
 631        static const u8 cmd[] = { 0x0A, 0xAA };
 632        int error;
 633
 634        error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
 635                                         RAYDIUM_WAIT_READY);
 636        if (error) {
 637                dev_err(&client->dev, "disable watchdog command failed: %d\n",
 638                        error);
 639                return error;
 640        }
 641
 642        return 0;
 643}
 644
 645static int raydium_i2c_fw_write_page(struct i2c_client *client,
 646                                     u16 page_idx, const void *data, size_t len)
 647{
 648        u8 buf[RM_BL_WRT_LEN];
 649        size_t xfer_len;
 650        int error;
 651        int i;
 652
 653        BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
 654
 655        for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
 656                buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
 657                buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
 658                buf[BL_PKG_IDX] = i + 1;
 659
 660                xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
 661                memcpy(&buf[BL_DATA_STR], data, xfer_len);
 662                if (len < RM_BL_WRT_PKG_SIZE)
 663                        memset(&buf[BL_DATA_STR + xfer_len], 0xff,
 664                                RM_BL_WRT_PKG_SIZE - xfer_len);
 665
 666                error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
 667                                                 RAYDIUM_WAIT_READY);
 668                if (error) {
 669                        dev_err(&client->dev,
 670                                "page write command failed for page %d, chunk %d: %d\n",
 671                                page_idx, i, error);
 672                        return error;
 673                }
 674
 675                data += xfer_len;
 676                len -= xfer_len;
 677        }
 678
 679        return error;
 680}
 681
 682static u16 raydium_calc_chksum(const u8 *buf, u16 len)
 683{
 684        u16 checksum = 0;
 685        u16 i;
 686
 687        for (i = 0; i < len; i++)
 688                checksum += buf[i];
 689
 690        return checksum;
 691}
 692
 693static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
 694                                         const struct firmware *fw)
 695{
 696        struct i2c_client *client = ts->client;
 697        const void *data;
 698        size_t data_len;
 699        size_t len;
 700        int page_nr;
 701        int i;
 702        int error;
 703        u16 fw_checksum;
 704
 705        if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
 706                dev_err(&client->dev, "Invalid firmware length\n");
 707                return -EINVAL;
 708        }
 709
 710        error = raydium_i2c_check_fw_status(ts);
 711        if (error) {
 712                dev_err(&client->dev, "Unable to access IC %d\n", error);
 713                return error;
 714        }
 715
 716        if (ts->boot_mode == RAYDIUM_TS_MAIN) {
 717                for (i = 0; i < RM_MAX_RETRIES; i++) {
 718                        error = raydium_i2c_enter_bl(client);
 719                        if (!error) {
 720                                error = raydium_i2c_check_fw_status(ts);
 721                                if (error) {
 722                                        dev_err(&client->dev,
 723                                                "unable to access IC: %d\n",
 724                                                error);
 725                                        return error;
 726                                }
 727
 728                                if (ts->boot_mode == RAYDIUM_TS_BLDR)
 729                                        break;
 730                        }
 731                }
 732
 733                if (ts->boot_mode == RAYDIUM_TS_MAIN) {
 734                        dev_err(&client->dev,
 735                                "failed to jump to boot loader: %d\n",
 736                                error);
 737                        return -EIO;
 738                }
 739        }
 740
 741        error = raydium_i2c_disable_watch_dog(client);
 742        if (error)
 743                return error;
 744
 745        error = raydium_i2c_check_path(client);
 746        if (error)
 747                return error;
 748
 749        error = raydium_i2c_boot_trigger(client);
 750        if (error) {
 751                dev_err(&client->dev, "send boot trigger fail: %d\n", error);
 752                return error;
 753        }
 754
 755        msleep(RM_BOOT_DELAY_MS);
 756
 757        data = fw->data;
 758        data_len = fw->size;
 759        page_nr = 0;
 760
 761        while (data_len) {
 762                len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
 763
 764                error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
 765                if (error)
 766                        return error;
 767
 768                msleep(20);
 769
 770                data += len;
 771                data_len -= len;
 772        }
 773
 774        error = raydium_i2c_leave_bl(client);
 775        if (error) {
 776                dev_err(&client->dev,
 777                        "failed to leave boot loader: %d\n", error);
 778                return error;
 779        }
 780
 781        dev_dbg(&client->dev, "left boot loader mode\n");
 782        msleep(RM_BOOT_DELAY_MS);
 783
 784        error = raydium_i2c_check_fw_status(ts);
 785        if (error) {
 786                dev_err(&client->dev,
 787                        "failed to check fw status after write: %d\n",
 788                        error);
 789                return error;
 790        }
 791
 792        if (ts->boot_mode != RAYDIUM_TS_MAIN) {
 793                dev_err(&client->dev,
 794                        "failed to switch to main fw after writing firmware: %d\n",
 795                        error);
 796                return -EINVAL;
 797        }
 798
 799        error = raydium_i2c_fw_trigger(client);
 800        if (error) {
 801                dev_err(&client->dev, "failed to trigger fw: %d\n", error);
 802                return error;
 803        }
 804
 805        fw_checksum = raydium_calc_chksum(fw->data, fw->size);
 806
 807        error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
 808        if (error)
 809                return error;
 810
 811        return 0;
 812}
 813
 814static int raydium_i2c_fw_update(struct raydium_data *ts)
 815{
 816        struct i2c_client *client = ts->client;
 817        const struct firmware *fw = NULL;
 818        char *fw_file;
 819        int error;
 820
 821        fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
 822                            le32_to_cpu(ts->info.hw_ver));
 823        if (!fw_file)
 824                return -ENOMEM;
 825
 826        dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
 827
 828        error = request_firmware(&fw, fw_file, &client->dev);
 829        if (error) {
 830                dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
 831                goto out_free_fw_file;
 832        }
 833
 834        disable_irq(client->irq);
 835
 836        error = raydium_i2c_do_update_firmware(ts, fw);
 837        if (error) {
 838                dev_err(&client->dev, "firmware update failed: %d\n", error);
 839                ts->boot_mode = RAYDIUM_TS_BLDR;
 840                goto out_enable_irq;
 841        }
 842
 843        error = raydium_i2c_initialize(ts);
 844        if (error) {
 845                dev_err(&client->dev,
 846                        "failed to initialize device after firmware update: %d\n",
 847                        error);
 848                ts->boot_mode = RAYDIUM_TS_BLDR;
 849                goto out_enable_irq;
 850        }
 851
 852        ts->boot_mode = RAYDIUM_TS_MAIN;
 853
 854out_enable_irq:
 855        enable_irq(client->irq);
 856        msleep(100);
 857
 858        release_firmware(fw);
 859
 860out_free_fw_file:
 861        kfree(fw_file);
 862
 863        return error;
 864}
 865
 866static void raydium_mt_event(struct raydium_data *ts)
 867{
 868        int i;
 869
 870        for (i = 0; i < ts->report_size / ts->contact_size; i++) {
 871                u8 *contact = &ts->report_data[ts->contact_size * i];
 872                bool state = contact[RM_CONTACT_STATE_POS];
 873                u8 wx, wy;
 874
 875                input_mt_slot(ts->input, i);
 876                input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
 877
 878                if (!state)
 879                        continue;
 880
 881                input_report_abs(ts->input, ABS_MT_POSITION_X,
 882                                get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
 883                input_report_abs(ts->input, ABS_MT_POSITION_Y,
 884                                get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
 885                input_report_abs(ts->input, ABS_MT_PRESSURE,
 886                                contact[RM_CONTACT_PRESSURE_POS]);
 887
 888                wx = contact[RM_CONTACT_WIDTH_X_POS];
 889                wy = contact[RM_CONTACT_WIDTH_Y_POS];
 890
 891                input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
 892                input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
 893        }
 894
 895        input_mt_sync_frame(ts->input);
 896        input_sync(ts->input);
 897}
 898
 899static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
 900{
 901        struct raydium_data *ts = _dev;
 902        int error;
 903        u16 fw_crc;
 904        u16 calc_crc;
 905
 906        if (ts->boot_mode != RAYDIUM_TS_MAIN)
 907                goto out;
 908
 909        error = raydium_i2c_read(ts->client, ts->data_bank_addr,
 910                                 ts->report_data, ts->pkg_size);
 911        if (error)
 912                goto out;
 913
 914        fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
 915        calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
 916        if (unlikely(fw_crc != calc_crc)) {
 917                dev_warn(&ts->client->dev,
 918                         "%s: invalid packet crc %#04x vs %#04x\n",
 919                         __func__, calc_crc, fw_crc);
 920                goto out;
 921        }
 922
 923        raydium_mt_event(ts);
 924
 925out:
 926        return IRQ_HANDLED;
 927}
 928
 929static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
 930                                       struct device_attribute *attr, char *buf)
 931{
 932        struct i2c_client *client = to_i2c_client(dev);
 933        struct raydium_data *ts = i2c_get_clientdata(client);
 934
 935        return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
 936}
 937
 938static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
 939                                       struct device_attribute *attr, char *buf)
 940{
 941        struct i2c_client *client = to_i2c_client(dev);
 942        struct raydium_data *ts = i2c_get_clientdata(client);
 943
 944        return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
 945}
 946
 947static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
 948                                          struct device_attribute *attr,
 949                                          char *buf)
 950{
 951        struct i2c_client *client = to_i2c_client(dev);
 952        struct raydium_data *ts = i2c_get_clientdata(client);
 953
 954        return sprintf(buf, "%s\n",
 955                       ts->boot_mode == RAYDIUM_TS_MAIN ?
 956                                "Normal" : "Recovery");
 957}
 958
 959static ssize_t raydium_i2c_update_fw_store(struct device *dev,
 960                                           struct device_attribute *attr,
 961                                           const char *buf, size_t count)
 962{
 963        struct i2c_client *client = to_i2c_client(dev);
 964        struct raydium_data *ts = i2c_get_clientdata(client);
 965        int error;
 966
 967        error = mutex_lock_interruptible(&ts->sysfs_mutex);
 968        if (error)
 969                return error;
 970
 971        error = raydium_i2c_fw_update(ts);
 972
 973        mutex_unlock(&ts->sysfs_mutex);
 974
 975        return error ?: count;
 976}
 977
 978static ssize_t raydium_i2c_calibrate_store(struct device *dev,
 979                                           struct device_attribute *attr,
 980                                           const char *buf, size_t count)
 981{
 982        struct i2c_client *client = to_i2c_client(dev);
 983        struct raydium_data *ts = i2c_get_clientdata(client);
 984        static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
 985        int error;
 986
 987        error = mutex_lock_interruptible(&ts->sysfs_mutex);
 988        if (error)
 989                return error;
 990
 991        error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
 992                                         RAYDIUM_WAIT_READY);
 993        if (error)
 994                dev_err(&client->dev, "calibrate command failed: %d\n", error);
 995
 996        mutex_unlock(&ts->sysfs_mutex);
 997        return error ?: count;
 998}
 999
1000static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
1001static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
1002static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
1003static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
1004static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
1005
1006static struct attribute *raydium_i2c_attributes[] = {
1007        &dev_attr_update_fw.attr,
1008        &dev_attr_boot_mode.attr,
1009        &dev_attr_fw_version.attr,
1010        &dev_attr_hw_version.attr,
1011        &dev_attr_calibrate.attr,
1012        NULL
1013};
1014
1015static const struct attribute_group raydium_i2c_attribute_group = {
1016        .attrs = raydium_i2c_attributes,
1017};
1018
1019static int raydium_i2c_power_on(struct raydium_data *ts)
1020{
1021        int error;
1022
1023        if (!ts->reset_gpio)
1024                return 0;
1025
1026        gpiod_set_value_cansleep(ts->reset_gpio, 1);
1027
1028        error = regulator_enable(ts->avdd);
1029        if (error) {
1030                dev_err(&ts->client->dev,
1031                        "failed to enable avdd regulator: %d\n", error);
1032                goto release_reset_gpio;
1033        }
1034
1035        error = regulator_enable(ts->vccio);
1036        if (error) {
1037                regulator_disable(ts->avdd);
1038                dev_err(&ts->client->dev,
1039                        "failed to enable vccio regulator: %d\n", error);
1040                goto release_reset_gpio;
1041        }
1042
1043        udelay(RM_POWERON_DELAY_USEC);
1044
1045release_reset_gpio:
1046        gpiod_set_value_cansleep(ts->reset_gpio, 0);
1047
1048        if (error)
1049                return error;
1050
1051        msleep(RM_RESET_DELAY_MSEC);
1052
1053        return 0;
1054}
1055
1056static void raydium_i2c_power_off(void *_data)
1057{
1058        struct raydium_data *ts = _data;
1059
1060        if (ts->reset_gpio) {
1061                gpiod_set_value_cansleep(ts->reset_gpio, 1);
1062                regulator_disable(ts->vccio);
1063                regulator_disable(ts->avdd);
1064        }
1065}
1066
1067static int raydium_i2c_probe(struct i2c_client *client,
1068                             const struct i2c_device_id *id)
1069{
1070        union i2c_smbus_data dummy;
1071        struct raydium_data *ts;
1072        int error;
1073
1074        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1075                dev_err(&client->dev,
1076                        "i2c check functionality error (need I2C_FUNC_I2C)\n");
1077                return -ENXIO;
1078        }
1079
1080        ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1081        if (!ts)
1082                return -ENOMEM;
1083
1084        mutex_init(&ts->sysfs_mutex);
1085
1086        ts->client = client;
1087        i2c_set_clientdata(client, ts);
1088
1089        ts->avdd = devm_regulator_get(&client->dev, "avdd");
1090        if (IS_ERR(ts->avdd)) {
1091                error = PTR_ERR(ts->avdd);
1092                if (error != -EPROBE_DEFER)
1093                        dev_err(&client->dev,
1094                                "Failed to get 'avdd' regulator: %d\n", error);
1095                return error;
1096        }
1097
1098        ts->vccio = devm_regulator_get(&client->dev, "vccio");
1099        if (IS_ERR(ts->vccio)) {
1100                error = PTR_ERR(ts->vccio);
1101                if (error != -EPROBE_DEFER)
1102                        dev_err(&client->dev,
1103                                "Failed to get 'vccio' regulator: %d\n", error);
1104                return error;
1105        }
1106
1107        ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1108                                                 GPIOD_OUT_LOW);
1109        if (IS_ERR(ts->reset_gpio)) {
1110                error = PTR_ERR(ts->reset_gpio);
1111                if (error != -EPROBE_DEFER)
1112                        dev_err(&client->dev,
1113                                "failed to get reset gpio: %d\n", error);
1114                return error;
1115        }
1116
1117        error = raydium_i2c_power_on(ts);
1118        if (error)
1119                return error;
1120
1121        error = devm_add_action_or_reset(&client->dev,
1122                                         raydium_i2c_power_off, ts);
1123        if (error) {
1124                dev_err(&client->dev,
1125                        "failed to install power off action: %d\n", error);
1126                return error;
1127        }
1128
1129        /* Make sure there is something at this address */
1130        if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1131                           I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1132                dev_err(&client->dev, "nothing at this address\n");
1133                return -ENXIO;
1134        }
1135
1136        error = raydium_i2c_initialize(ts);
1137        if (error) {
1138                dev_err(&client->dev, "failed to initialize: %d\n", error);
1139                return error;
1140        }
1141
1142        ts->report_data = devm_kmalloc(&client->dev,
1143                                       ts->pkg_size, GFP_KERNEL);
1144        if (!ts->report_data)
1145                return -ENOMEM;
1146
1147        ts->input = devm_input_allocate_device(&client->dev);
1148        if (!ts->input) {
1149                dev_err(&client->dev, "Failed to allocate input device\n");
1150                return -ENOMEM;
1151        }
1152
1153        ts->input->name = "Raydium Touchscreen";
1154        ts->input->id.bustype = BUS_I2C;
1155
1156        input_set_abs_params(ts->input, ABS_MT_POSITION_X,
1157                             0, le16_to_cpu(ts->info.x_max), 0, 0);
1158        input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
1159                             0, le16_to_cpu(ts->info.y_max), 0, 0);
1160        input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
1161        input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
1162
1163        input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1164        input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1165
1166        error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
1167                                    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1168        if (error) {
1169                dev_err(&client->dev,
1170                        "failed to initialize MT slots: %d\n", error);
1171                return error;
1172        }
1173
1174        error = input_register_device(ts->input);
1175        if (error) {
1176                dev_err(&client->dev,
1177                        "unable to register input device: %d\n", error);
1178                return error;
1179        }
1180
1181        error = devm_request_threaded_irq(&client->dev, client->irq,
1182                                          NULL, raydium_i2c_irq,
1183                                          IRQF_ONESHOT, client->name, ts);
1184        if (error) {
1185                dev_err(&client->dev, "Failed to register interrupt\n");
1186                return error;
1187        }
1188
1189        error = devm_device_add_group(&client->dev,
1190                                   &raydium_i2c_attribute_group);
1191        if (error) {
1192                dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1193                        error);
1194                return error;
1195        }
1196
1197        return 0;
1198}
1199
1200static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
1201{
1202        static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
1203        int error;
1204
1205        error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
1206                                 sleep_cmd, sizeof(sleep_cmd));
1207        if (error)
1208                dev_err(&client->dev,
1209                        "sleep command failed: %d\n", error);
1210}
1211
1212static int __maybe_unused raydium_i2c_suspend(struct device *dev)
1213{
1214        struct i2c_client *client = to_i2c_client(dev);
1215        struct raydium_data *ts = i2c_get_clientdata(client);
1216
1217        /* Sleep is not available in BLDR recovery mode */
1218        if (ts->boot_mode != RAYDIUM_TS_MAIN)
1219                return -EBUSY;
1220
1221        disable_irq(client->irq);
1222
1223        if (device_may_wakeup(dev)) {
1224                raydium_enter_sleep(client);
1225
1226                ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1227        } else {
1228                raydium_i2c_power_off(ts);
1229        }
1230
1231        return 0;
1232}
1233
1234static int __maybe_unused raydium_i2c_resume(struct device *dev)
1235{
1236        struct i2c_client *client = to_i2c_client(dev);
1237        struct raydium_data *ts = i2c_get_clientdata(client);
1238
1239        if (device_may_wakeup(dev)) {
1240                if (ts->wake_irq_enabled)
1241                        disable_irq_wake(client->irq);
1242                raydium_i2c_sw_reset(client);
1243        } else {
1244                raydium_i2c_power_on(ts);
1245                raydium_i2c_initialize(ts);
1246        }
1247
1248        enable_irq(client->irq);
1249
1250        return 0;
1251}
1252
1253static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1254                         raydium_i2c_suspend, raydium_i2c_resume);
1255
1256static const struct i2c_device_id raydium_i2c_id[] = {
1257        { "raydium_i2c", 0 },
1258        { "rm32380", 0 },
1259        { /* sentinel */ }
1260};
1261MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1262
1263#ifdef CONFIG_ACPI
1264static const struct acpi_device_id raydium_acpi_id[] = {
1265        { "RAYD0001", 0 },
1266        { /* sentinel */ }
1267};
1268MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1269#endif
1270
1271#ifdef CONFIG_OF
1272static const struct of_device_id raydium_of_match[] = {
1273        { .compatible = "raydium,rm32380", },
1274        { /* sentinel */ }
1275};
1276MODULE_DEVICE_TABLE(of, raydium_of_match);
1277#endif
1278
1279static struct i2c_driver raydium_i2c_driver = {
1280        .probe = raydium_i2c_probe,
1281        .id_table = raydium_i2c_id,
1282        .driver = {
1283                .name = "raydium_ts",
1284                .pm = &raydium_i2c_pm_ops,
1285                .acpi_match_table = ACPI_PTR(raydium_acpi_id),
1286                .of_match_table = of_match_ptr(raydium_of_match),
1287        },
1288};
1289module_i2c_driver(raydium_i2c_driver);
1290
1291MODULE_AUTHOR("Raydium");
1292MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1293MODULE_LICENSE("GPL v2");
1294