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