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