0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/acpi.h>
0015 #include <linux/delay.h>
0016 #include <linux/firmware.h>
0017 #include <linux/gpio/consumer.h>
0018 #include <linux/i2c.h>
0019 #include <linux/input.h>
0020 #include <linux/input/mt.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/module.h>
0023 #include <linux/of.h>
0024 #include <linux/regulator/consumer.h>
0025 #include <linux/slab.h>
0026 #include <asm/unaligned.h>
0027
0028
0029 #define RM_BOOT_BLDR 0x02
0030 #define RM_BOOT_MAIN 0x03
0031
0032
0033 #define RM_CMD_BOOT_PAGE_WRT 0x0B
0034 #define RM_CMD_BOOT_WRT 0x11
0035 #define RM_CMD_BOOT_ACK 0x22
0036 #define RM_CMD_BOOT_CHK 0x33
0037 #define RM_CMD_BOOT_READ 0x44
0038
0039 #define RM_BOOT_RDY 0xFF
0040 #define RM_BOOT_CMD_READHWID 0x0E
0041
0042
0043 #define RM_CMD_QUERY_BANK 0x2B
0044 #define RM_CMD_DATA_BANK 0x4D
0045 #define RM_CMD_ENTER_SLEEP 0x4E
0046 #define RM_CMD_BANK_SWITCH 0xAA
0047
0048 #define RM_RESET_MSG_ADDR 0x40000004
0049
0050 #define RM_MAX_READ_SIZE 56
0051 #define RM_PACKET_CRC_SIZE 2
0052
0053
0054 #define RM_MAX_RETRIES 3
0055 #define RM_RETRY_DELAY_MS 20
0056 #define RM_MAX_TOUCH_NUM 10
0057 #define RM_BOOT_DELAY_MS 100
0058
0059
0060 #define RM_CONTACT_STATE_POS 0
0061 #define RM_CONTACT_X_POS 1
0062 #define RM_CONTACT_Y_POS 3
0063 #define RM_CONTACT_PRESSURE_POS 5
0064 #define RM_CONTACT_WIDTH_X_POS 6
0065 #define RM_CONTACT_WIDTH_Y_POS 7
0066
0067
0068 #define RM_BL_WRT_CMD_SIZE 3
0069 #define RM_BL_WRT_PKG_SIZE 32
0070 #define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
0071 #define RM_FW_PAGE_SIZE 128
0072 #define RM_MAX_FW_RETRIES 30
0073 #define RM_MAX_FW_SIZE 0xD000
0074
0075 #define RM_POWERON_DELAY_USEC 500
0076 #define RM_RESET_DELAY_MSEC 50
0077
0078 enum raydium_bl_cmd {
0079 BL_HEADER = 0,
0080 BL_PAGE_STR,
0081 BL_PKG_IDX,
0082 BL_DATA_STR,
0083 };
0084
0085 enum raydium_bl_ack {
0086 RAYDIUM_ACK_NULL = 0,
0087 RAYDIUM_WAIT_READY,
0088 RAYDIUM_PATH_READY,
0089 };
0090
0091 enum raydium_boot_mode {
0092 RAYDIUM_TS_MAIN = 0,
0093 RAYDIUM_TS_BLDR,
0094 };
0095
0096
0097 struct raydium_data_info {
0098 __le32 data_bank_addr;
0099 u8 pkg_size;
0100 u8 tp_info_size;
0101 };
0102
0103 struct raydium_info {
0104 __le32 hw_ver;
0105 u8 main_ver;
0106 u8 sub_ver;
0107 __le16 ft_ver;
0108 u8 x_num;
0109 u8 y_num;
0110 __le16 x_max;
0111 __le16 y_max;
0112 u8 x_res;
0113 u8 y_res;
0114 };
0115
0116
0117 struct raydium_data {
0118 struct i2c_client *client;
0119 struct input_dev *input;
0120
0121 struct regulator *avdd;
0122 struct regulator *vccio;
0123 struct gpio_desc *reset_gpio;
0124
0125 struct raydium_info info;
0126
0127 struct mutex sysfs_mutex;
0128
0129 u8 *report_data;
0130
0131 u32 data_bank_addr;
0132 u8 report_size;
0133 u8 contact_size;
0134 u8 pkg_size;
0135
0136 enum raydium_boot_mode boot_mode;
0137
0138 bool wake_irq_enabled;
0139 };
0140
0141
0142
0143
0144
0145 struct __packed raydium_bank_switch_header {
0146 u8 cmd;
0147 __be32 be_addr;
0148 };
0149
0150 static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
0151 struct i2c_msg *xfer, size_t xfer_count)
0152 {
0153 int ret;
0154
0155
0156
0157
0158 int xfer_start_idx = (addr > 0xff) ? 0 : 1;
0159 xfer_count -= xfer_start_idx;
0160
0161 ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
0162 if (likely(ret == xfer_count))
0163 return 0;
0164
0165 return ret < 0 ? ret : -EIO;
0166 }
0167
0168 static int raydium_i2c_send(struct i2c_client *client,
0169 u32 addr, const void *data, size_t len)
0170 {
0171 int tries = 0;
0172 int error;
0173 u8 *tx_buf;
0174 u8 reg_addr = addr & 0xff;
0175
0176 tx_buf = kmalloc(len + 1, GFP_KERNEL);
0177 if (!tx_buf)
0178 return -ENOMEM;
0179
0180 tx_buf[0] = reg_addr;
0181 memcpy(tx_buf + 1, data, len);
0182
0183 do {
0184 struct raydium_bank_switch_header header = {
0185 .cmd = RM_CMD_BANK_SWITCH,
0186 .be_addr = cpu_to_be32(addr),
0187 };
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 struct i2c_msg xfer[] = {
0200 {
0201 .addr = client->addr,
0202 .len = sizeof(header),
0203 .buf = (u8 *)&header,
0204 },
0205 {
0206 .addr = client->addr,
0207 .len = len + 1,
0208 .buf = tx_buf,
0209 },
0210 };
0211
0212 error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
0213 if (likely(!error))
0214 return 0;
0215
0216 msleep(RM_RETRY_DELAY_MS);
0217 } while (++tries < RM_MAX_RETRIES);
0218
0219 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
0220 return error;
0221 }
0222
0223 static int raydium_i2c_read(struct i2c_client *client,
0224 u32 addr, void *data, size_t len)
0225 {
0226 int error;
0227
0228 while (len) {
0229 u8 reg_addr = addr & 0xff;
0230 struct raydium_bank_switch_header header = {
0231 .cmd = RM_CMD_BANK_SWITCH,
0232 .be_addr = cpu_to_be32(addr),
0233 };
0234 size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 struct i2c_msg xfer[] = {
0247 {
0248 .addr = client->addr,
0249 .len = sizeof(header),
0250 .buf = (u8 *)&header,
0251 },
0252 {
0253 .addr = client->addr,
0254 .len = 1,
0255 .buf = ®_addr,
0256 },
0257 {
0258 .addr = client->addr,
0259 .len = xfer_len,
0260 .buf = data,
0261 .flags = I2C_M_RD,
0262 }
0263 };
0264
0265 error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
0266 if (unlikely(error))
0267 return error;
0268
0269 len -= xfer_len;
0270 data += xfer_len;
0271 addr += xfer_len;
0272 }
0273
0274 return 0;
0275 }
0276
0277 static int raydium_i2c_sw_reset(struct i2c_client *client)
0278 {
0279 const u8 soft_rst_cmd = 0x01;
0280 int error;
0281
0282 error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
0283 sizeof(soft_rst_cmd));
0284 if (error) {
0285 dev_err(&client->dev, "software reset failed: %d\n", error);
0286 return error;
0287 }
0288
0289 msleep(RM_RESET_DELAY_MSEC);
0290
0291 return 0;
0292 }
0293
0294 static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
0295 {
0296 struct i2c_client *client = ts->client;
0297 static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
0298 0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
0299 u8 rbuf[5] = { 0 };
0300 u32 hw_ver;
0301 int error;
0302
0303 error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
0304 get_hwid, sizeof(get_hwid));
0305 if (error) {
0306 dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
0307 return error;
0308 }
0309
0310 error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
0311 if (error) {
0312 dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
0313 return error;
0314 }
0315
0316 error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
0317 if (error) {
0318 dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
0319 error, rbuf + 1);
0320 hw_ver = 0xffffffffUL;
0321 } else {
0322 hw_ver = get_unaligned_be32(rbuf + 1);
0323 }
0324
0325 ts->info.hw_ver = cpu_to_le32(hw_ver);
0326 ts->info.main_ver = 0xff;
0327 ts->info.sub_ver = 0xff;
0328
0329 return error;
0330 }
0331
0332 static int raydium_i2c_query_ts_info(struct raydium_data *ts)
0333 {
0334 struct i2c_client *client = ts->client;
0335 struct raydium_data_info data_info;
0336 __le32 query_bank_addr;
0337
0338 int error, retry_cnt;
0339
0340 for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
0341 error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
0342 &data_info, sizeof(data_info));
0343 if (error)
0344 continue;
0345
0346
0347
0348
0349
0350
0351 if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
0352 dev_warn(&client->dev,
0353 "report size changes, was: %d, new: %d\n",
0354 ts->pkg_size, data_info.pkg_size);
0355 } else {
0356 ts->pkg_size = data_info.pkg_size;
0357 ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
0358 }
0359
0360 ts->contact_size = data_info.tp_info_size;
0361 ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
0362
0363 dev_dbg(&client->dev,
0364 "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
0365 ts->data_bank_addr, ts->report_size, ts->contact_size);
0366
0367 error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
0368 &query_bank_addr,
0369 sizeof(query_bank_addr));
0370 if (error)
0371 continue;
0372
0373 error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
0374 &ts->info, sizeof(ts->info));
0375 if (error)
0376 continue;
0377
0378 return 0;
0379 }
0380
0381 dev_err(&client->dev, "failed to query device parameters: %d\n", error);
0382 return error;
0383 }
0384
0385 static int raydium_i2c_check_fw_status(struct raydium_data *ts)
0386 {
0387 struct i2c_client *client = ts->client;
0388 static const u8 bl_ack = 0x62;
0389 static const u8 main_ack = 0x66;
0390 u8 buf[4];
0391 int error;
0392
0393 error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
0394 if (!error) {
0395 if (buf[0] == bl_ack)
0396 ts->boot_mode = RAYDIUM_TS_BLDR;
0397 else if (buf[0] == main_ack)
0398 ts->boot_mode = RAYDIUM_TS_MAIN;
0399 return 0;
0400 }
0401
0402 return error;
0403 }
0404
0405 static int raydium_i2c_initialize(struct raydium_data *ts)
0406 {
0407 struct i2c_client *client = ts->client;
0408 int error, retry_cnt;
0409
0410 for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
0411
0412 msleep(RM_BOOT_DELAY_MS);
0413
0414 error = raydium_i2c_check_fw_status(ts);
0415 if (error) {
0416 dev_err(&client->dev,
0417 "failed to read 'hello' packet: %d\n", error);
0418 continue;
0419 }
0420
0421 if (ts->boot_mode == RAYDIUM_TS_BLDR ||
0422 ts->boot_mode == RAYDIUM_TS_MAIN) {
0423 break;
0424 }
0425 }
0426
0427 if (error)
0428 ts->boot_mode = RAYDIUM_TS_BLDR;
0429
0430 if (ts->boot_mode == RAYDIUM_TS_BLDR)
0431 raydium_i2c_query_ts_bootloader_info(ts);
0432 else
0433 raydium_i2c_query_ts_info(ts);
0434
0435 return error;
0436 }
0437
0438 static int raydium_i2c_bl_chk_state(struct i2c_client *client,
0439 enum raydium_bl_ack state)
0440 {
0441 static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
0442 u8 rbuf[sizeof(ack_ok)];
0443 u8 retry;
0444 int error;
0445
0446 for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
0447 switch (state) {
0448 case RAYDIUM_ACK_NULL:
0449 return 0;
0450
0451 case RAYDIUM_WAIT_READY:
0452 error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
0453 &rbuf[0], 1);
0454 if (!error && rbuf[0] == RM_BOOT_RDY)
0455 return 0;
0456
0457 break;
0458
0459 case RAYDIUM_PATH_READY:
0460 error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
0461 rbuf, sizeof(rbuf));
0462 if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
0463 return 0;
0464
0465 break;
0466
0467 default:
0468 dev_err(&client->dev, "%s: invalid target state %d\n",
0469 __func__, state);
0470 return -EINVAL;
0471 }
0472
0473 msleep(20);
0474 }
0475
0476 return -ETIMEDOUT;
0477 }
0478
0479 static int raydium_i2c_write_object(struct i2c_client *client,
0480 const void *data, size_t len,
0481 enum raydium_bl_ack state)
0482 {
0483 int error;
0484 static const u8 cmd[] = { 0xFF, 0x39 };
0485
0486 error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
0487 if (error) {
0488 dev_err(&client->dev, "WRT obj command failed: %d\n",
0489 error);
0490 return error;
0491 }
0492
0493 error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
0494 if (error) {
0495 dev_err(&client->dev, "Ack obj command failed: %d\n", error);
0496 return error;
0497 }
0498
0499 error = raydium_i2c_bl_chk_state(client, state);
0500 if (error) {
0501 dev_err(&client->dev, "BL check state failed: %d\n", error);
0502 return error;
0503 }
0504 return 0;
0505 }
0506
0507 static int raydium_i2c_boot_trigger(struct i2c_client *client)
0508 {
0509 static const u8 cmd[7][6] = {
0510 { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
0511 { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
0512 { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
0513 { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
0514 { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
0515 { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
0516 { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
0517 };
0518 int i;
0519 int error;
0520
0521 for (i = 0; i < 7; i++) {
0522 error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
0523 RAYDIUM_WAIT_READY);
0524 if (error) {
0525 dev_err(&client->dev,
0526 "boot trigger failed at step %d: %d\n",
0527 i, error);
0528 return error;
0529 }
0530 }
0531
0532 return 0;
0533 }
0534
0535 static int raydium_i2c_fw_trigger(struct i2c_client *client)
0536 {
0537 static const u8 cmd[5][11] = {
0538 { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
0539 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
0540 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
0541 { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
0542 { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
0543 };
0544 int i;
0545 int error;
0546
0547 for (i = 0; i < 5; i++) {
0548 error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
0549 RAYDIUM_ACK_NULL);
0550 if (error) {
0551 dev_err(&client->dev,
0552 "fw trigger failed at step %d: %d\n",
0553 i, error);
0554 return error;
0555 }
0556 }
0557
0558 return 0;
0559 }
0560
0561 static int raydium_i2c_check_path(struct i2c_client *client)
0562 {
0563 static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
0564 int error;
0565
0566 error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
0567 RAYDIUM_PATH_READY);
0568 if (error) {
0569 dev_err(&client->dev, "check path command failed: %d\n", error);
0570 return error;
0571 }
0572
0573 return 0;
0574 }
0575
0576 static int raydium_i2c_enter_bl(struct i2c_client *client)
0577 {
0578 static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
0579 int error;
0580
0581 error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
0582 RAYDIUM_ACK_NULL);
0583 if (error) {
0584 dev_err(&client->dev, "enter bl command failed: %d\n", error);
0585 return error;
0586 }
0587
0588 msleep(RM_BOOT_DELAY_MS);
0589 return 0;
0590 }
0591
0592 static int raydium_i2c_leave_bl(struct i2c_client *client)
0593 {
0594 static const u8 leave_cmd[] = { 0x05, 0x00 };
0595 int error;
0596
0597 error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
0598 RAYDIUM_ACK_NULL);
0599 if (error) {
0600 dev_err(&client->dev, "leave bl command failed: %d\n", error);
0601 return error;
0602 }
0603
0604 msleep(RM_BOOT_DELAY_MS);
0605 return 0;
0606 }
0607
0608 static int raydium_i2c_write_checksum(struct i2c_client *client,
0609 size_t length, u16 checksum)
0610 {
0611 u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
0612 int error;
0613
0614 put_unaligned_le16(length, &checksum_cmd[3]);
0615 put_unaligned_le16(checksum, &checksum_cmd[5]);
0616
0617 error = raydium_i2c_write_object(client,
0618 checksum_cmd, sizeof(checksum_cmd),
0619 RAYDIUM_ACK_NULL);
0620 if (error) {
0621 dev_err(&client->dev, "failed to write checksum: %d\n",
0622 error);
0623 return error;
0624 }
0625
0626 return 0;
0627 }
0628
0629 static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
0630 {
0631 static const u8 cmd[] = { 0x0A, 0xAA };
0632 int error;
0633
0634 error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
0635 RAYDIUM_WAIT_READY);
0636 if (error) {
0637 dev_err(&client->dev, "disable watchdog command failed: %d\n",
0638 error);
0639 return error;
0640 }
0641
0642 return 0;
0643 }
0644
0645 static int raydium_i2c_fw_write_page(struct i2c_client *client,
0646 u16 page_idx, const void *data, size_t len)
0647 {
0648 u8 buf[RM_BL_WRT_LEN];
0649 size_t xfer_len;
0650 int error;
0651 int i;
0652
0653 BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
0654
0655 for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
0656 buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
0657 buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
0658 buf[BL_PKG_IDX] = i + 1;
0659
0660 xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
0661 memcpy(&buf[BL_DATA_STR], data, xfer_len);
0662 if (len < RM_BL_WRT_PKG_SIZE)
0663 memset(&buf[BL_DATA_STR + xfer_len], 0xff,
0664 RM_BL_WRT_PKG_SIZE - xfer_len);
0665
0666 error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
0667 RAYDIUM_WAIT_READY);
0668 if (error) {
0669 dev_err(&client->dev,
0670 "page write command failed for page %d, chunk %d: %d\n",
0671 page_idx, i, error);
0672 return error;
0673 }
0674
0675 data += xfer_len;
0676 len -= xfer_len;
0677 }
0678
0679 return error;
0680 }
0681
0682 static u16 raydium_calc_chksum(const u8 *buf, u16 len)
0683 {
0684 u16 checksum = 0;
0685 u16 i;
0686
0687 for (i = 0; i < len; i++)
0688 checksum += buf[i];
0689
0690 return checksum;
0691 }
0692
0693 static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
0694 const struct firmware *fw)
0695 {
0696 struct i2c_client *client = ts->client;
0697 const void *data;
0698 size_t data_len;
0699 size_t len;
0700 int page_nr;
0701 int i;
0702 int error;
0703 u16 fw_checksum;
0704
0705 if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
0706 dev_err(&client->dev, "Invalid firmware length\n");
0707 return -EINVAL;
0708 }
0709
0710 error = raydium_i2c_check_fw_status(ts);
0711 if (error) {
0712 dev_err(&client->dev, "Unable to access IC %d\n", error);
0713 return error;
0714 }
0715
0716 if (ts->boot_mode == RAYDIUM_TS_MAIN) {
0717 for (i = 0; i < RM_MAX_RETRIES; i++) {
0718 error = raydium_i2c_enter_bl(client);
0719 if (!error) {
0720 error = raydium_i2c_check_fw_status(ts);
0721 if (error) {
0722 dev_err(&client->dev,
0723 "unable to access IC: %d\n",
0724 error);
0725 return error;
0726 }
0727
0728 if (ts->boot_mode == RAYDIUM_TS_BLDR)
0729 break;
0730 }
0731 }
0732
0733 if (ts->boot_mode == RAYDIUM_TS_MAIN) {
0734 dev_err(&client->dev,
0735 "failed to jump to boot loader: %d\n",
0736 error);
0737 return -EIO;
0738 }
0739 }
0740
0741 error = raydium_i2c_disable_watch_dog(client);
0742 if (error)
0743 return error;
0744
0745 error = raydium_i2c_check_path(client);
0746 if (error)
0747 return error;
0748
0749 error = raydium_i2c_boot_trigger(client);
0750 if (error) {
0751 dev_err(&client->dev, "send boot trigger fail: %d\n", error);
0752 return error;
0753 }
0754
0755 msleep(RM_BOOT_DELAY_MS);
0756
0757 data = fw->data;
0758 data_len = fw->size;
0759 page_nr = 0;
0760
0761 while (data_len) {
0762 len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
0763
0764 error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
0765 if (error)
0766 return error;
0767
0768 msleep(20);
0769
0770 data += len;
0771 data_len -= len;
0772 }
0773
0774 error = raydium_i2c_leave_bl(client);
0775 if (error) {
0776 dev_err(&client->dev,
0777 "failed to leave boot loader: %d\n", error);
0778 return error;
0779 }
0780
0781 dev_dbg(&client->dev, "left boot loader mode\n");
0782 msleep(RM_BOOT_DELAY_MS);
0783
0784 error = raydium_i2c_check_fw_status(ts);
0785 if (error) {
0786 dev_err(&client->dev,
0787 "failed to check fw status after write: %d\n",
0788 error);
0789 return error;
0790 }
0791
0792 if (ts->boot_mode != RAYDIUM_TS_MAIN) {
0793 dev_err(&client->dev,
0794 "failed to switch to main fw after writing firmware: %d\n",
0795 error);
0796 return -EINVAL;
0797 }
0798
0799 error = raydium_i2c_fw_trigger(client);
0800 if (error) {
0801 dev_err(&client->dev, "failed to trigger fw: %d\n", error);
0802 return error;
0803 }
0804
0805 fw_checksum = raydium_calc_chksum(fw->data, fw->size);
0806
0807 error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
0808 if (error)
0809 return error;
0810
0811 return 0;
0812 }
0813
0814 static int raydium_i2c_fw_update(struct raydium_data *ts)
0815 {
0816 struct i2c_client *client = ts->client;
0817 const struct firmware *fw = NULL;
0818 char *fw_file;
0819 int error;
0820
0821 fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
0822 le32_to_cpu(ts->info.hw_ver));
0823 if (!fw_file)
0824 return -ENOMEM;
0825
0826 dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
0827
0828 error = request_firmware(&fw, fw_file, &client->dev);
0829 if (error) {
0830 dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
0831 goto out_free_fw_file;
0832 }
0833
0834 disable_irq(client->irq);
0835
0836 error = raydium_i2c_do_update_firmware(ts, fw);
0837 if (error) {
0838 dev_err(&client->dev, "firmware update failed: %d\n", error);
0839 ts->boot_mode = RAYDIUM_TS_BLDR;
0840 goto out_enable_irq;
0841 }
0842
0843 error = raydium_i2c_initialize(ts);
0844 if (error) {
0845 dev_err(&client->dev,
0846 "failed to initialize device after firmware update: %d\n",
0847 error);
0848 ts->boot_mode = RAYDIUM_TS_BLDR;
0849 goto out_enable_irq;
0850 }
0851
0852 ts->boot_mode = RAYDIUM_TS_MAIN;
0853
0854 out_enable_irq:
0855 enable_irq(client->irq);
0856 msleep(100);
0857
0858 release_firmware(fw);
0859
0860 out_free_fw_file:
0861 kfree(fw_file);
0862
0863 return error;
0864 }
0865
0866 static void raydium_mt_event(struct raydium_data *ts)
0867 {
0868 int i;
0869
0870 for (i = 0; i < ts->report_size / ts->contact_size; i++) {
0871 u8 *contact = &ts->report_data[ts->contact_size * i];
0872 bool state = contact[RM_CONTACT_STATE_POS];
0873 u8 wx, wy;
0874
0875 input_mt_slot(ts->input, i);
0876 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
0877
0878 if (!state)
0879 continue;
0880
0881 input_report_abs(ts->input, ABS_MT_POSITION_X,
0882 get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
0883 input_report_abs(ts->input, ABS_MT_POSITION_Y,
0884 get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
0885 input_report_abs(ts->input, ABS_MT_PRESSURE,
0886 contact[RM_CONTACT_PRESSURE_POS]);
0887
0888 wx = contact[RM_CONTACT_WIDTH_X_POS];
0889 wy = contact[RM_CONTACT_WIDTH_Y_POS];
0890
0891 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
0892 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
0893 }
0894
0895 input_mt_sync_frame(ts->input);
0896 input_sync(ts->input);
0897 }
0898
0899 static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
0900 {
0901 struct raydium_data *ts = _dev;
0902 int error;
0903 u16 fw_crc;
0904 u16 calc_crc;
0905
0906 if (ts->boot_mode != RAYDIUM_TS_MAIN)
0907 goto out;
0908
0909 error = raydium_i2c_read(ts->client, ts->data_bank_addr,
0910 ts->report_data, ts->pkg_size);
0911 if (error)
0912 goto out;
0913
0914 fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
0915 calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
0916 if (unlikely(fw_crc != calc_crc)) {
0917 dev_warn(&ts->client->dev,
0918 "%s: invalid packet crc %#04x vs %#04x\n",
0919 __func__, calc_crc, fw_crc);
0920 goto out;
0921 }
0922
0923 raydium_mt_event(ts);
0924
0925 out:
0926 return IRQ_HANDLED;
0927 }
0928
0929 static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
0930 struct device_attribute *attr, char *buf)
0931 {
0932 struct i2c_client *client = to_i2c_client(dev);
0933 struct raydium_data *ts = i2c_get_clientdata(client);
0934
0935 return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
0936 }
0937
0938 static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
0939 struct device_attribute *attr, char *buf)
0940 {
0941 struct i2c_client *client = to_i2c_client(dev);
0942 struct raydium_data *ts = i2c_get_clientdata(client);
0943
0944 return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
0945 }
0946
0947 static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
0948 struct device_attribute *attr,
0949 char *buf)
0950 {
0951 struct i2c_client *client = to_i2c_client(dev);
0952 struct raydium_data *ts = i2c_get_clientdata(client);
0953
0954 return sprintf(buf, "%s\n",
0955 ts->boot_mode == RAYDIUM_TS_MAIN ?
0956 "Normal" : "Recovery");
0957 }
0958
0959 static ssize_t raydium_i2c_update_fw_store(struct device *dev,
0960 struct device_attribute *attr,
0961 const char *buf, size_t count)
0962 {
0963 struct i2c_client *client = to_i2c_client(dev);
0964 struct raydium_data *ts = i2c_get_clientdata(client);
0965 int error;
0966
0967 error = mutex_lock_interruptible(&ts->sysfs_mutex);
0968 if (error)
0969 return error;
0970
0971 error = raydium_i2c_fw_update(ts);
0972
0973 mutex_unlock(&ts->sysfs_mutex);
0974
0975 return error ?: count;
0976 }
0977
0978 static ssize_t raydium_i2c_calibrate_store(struct device *dev,
0979 struct device_attribute *attr,
0980 const char *buf, size_t count)
0981 {
0982 struct i2c_client *client = to_i2c_client(dev);
0983 struct raydium_data *ts = i2c_get_clientdata(client);
0984 static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
0985 int error;
0986
0987 error = mutex_lock_interruptible(&ts->sysfs_mutex);
0988 if (error)
0989 return error;
0990
0991 error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
0992 RAYDIUM_WAIT_READY);
0993 if (error)
0994 dev_err(&client->dev, "calibrate command failed: %d\n", error);
0995
0996 mutex_unlock(&ts->sysfs_mutex);
0997 return error ?: count;
0998 }
0999
1000 static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
1001 static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
1002 static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
1003 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
1004 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
1005
1006 static 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
1015 static const struct attribute_group raydium_i2c_attribute_group = {
1016 .attrs = raydium_i2c_attributes,
1017 };
1018
1019 static 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
1045 release_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
1056 static 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
1067 static 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
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
1200 static 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
1212 static 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
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
1234 static 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
1253 static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1254 raydium_i2c_suspend, raydium_i2c_resume);
1255
1256 static const struct i2c_device_id raydium_i2c_id[] = {
1257 { "raydium_i2c", 0 },
1258 { "rm32380", 0 },
1259 { }
1260 };
1261 MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1262
1263 #ifdef CONFIG_ACPI
1264 static const struct acpi_device_id raydium_acpi_id[] = {
1265 { "RAYD0001", 0 },
1266 { }
1267 };
1268 MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1269 #endif
1270
1271 #ifdef CONFIG_OF
1272 static const struct of_device_id raydium_of_match[] = {
1273 { .compatible = "raydium,rm32380", },
1274 { }
1275 };
1276 MODULE_DEVICE_TABLE(of, raydium_of_match);
1277 #endif
1278
1279 static 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 };
1289 module_i2c_driver(raydium_i2c_driver);
1290
1291 MODULE_AUTHOR("Raydium");
1292 MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1293 MODULE_LICENSE("GPL v2");