1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for Goodix Touchscreens 4 * 5 * Copyright (c) 2014 Red Hat Inc. 6 * Copyright (c) 2015 K. Merker <merker@debian.org> 7 * 8 * This code is based on gt9xx.c authored by andrew@goodix.com: 9 * 10 * 2010 - 2012 Goodix Technology. 11 */ 12 13 14 #include <linux/kernel.h> 15 #include <linux/dmi.h> 16 #include <linux/firmware.h> 17 #include <linux/module.h> 18 #include <linux/delay.h> 19 #include <linux/irq.h> 20 #include <linux/interrupt.h> 21 #include <linux/slab.h> 22 #include <linux/acpi.h> 23 #include <linux/of.h> 24 #include <asm/unaligned.h> 25 #include "goodix.h" 26 27 #define GOODIX_GPIO_INT_NAME "irq" 28 #define GOODIX_GPIO_RST_NAME "reset" 29 30 #define GOODIX_MAX_HEIGHT 4096 31 #define GOODIX_MAX_WIDTH 4096 32 #define GOODIX_INT_TRIGGER 1 33 #define GOODIX_CONTACT_SIZE 8 34 #define GOODIX_MAX_CONTACT_SIZE 9 35 #define GOODIX_MAX_CONTACTS 10 36 37 #define GOODIX_CONFIG_MIN_LENGTH 186 38 #define GOODIX_CONFIG_911_LENGTH 186 39 #define GOODIX_CONFIG_967_LENGTH 228 40 #define GOODIX_CONFIG_GT9X_LENGTH 240 41 42 #define GOODIX_BUFFER_STATUS_READY BIT(7) 43 #define GOODIX_HAVE_KEY BIT(4) 44 #define GOODIX_BUFFER_STATUS_TIMEOUT 20 45 46 #define RESOLUTION_LOC 1 47 #define MAX_CONTACTS_LOC 5 48 #define TRIGGER_LOC 6 49 50 /* Our special handling for GPIO accesses through ACPI is x86 specific */ 51 #if defined CONFIG_X86 && defined CONFIG_ACPI 52 #define ACPI_GPIO_SUPPORT 53 #endif 54 55 struct goodix_chip_id { 56 const char *id; 57 const struct goodix_chip_data *data; 58 }; 59 60 static int goodix_check_cfg_8(struct goodix_ts_data *ts, 61 const u8 *cfg, int len); 62 static int goodix_check_cfg_16(struct goodix_ts_data *ts, 63 const u8 *cfg, int len); 64 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts); 65 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts); 66 67 static const struct goodix_chip_data gt1x_chip_data = { 68 .config_addr = GOODIX_GT1X_REG_CONFIG_DATA, 69 .config_len = GOODIX_CONFIG_GT9X_LENGTH, 70 .check_config = goodix_check_cfg_16, 71 .calc_config_checksum = goodix_calc_cfg_checksum_16, 72 }; 73 74 static const struct goodix_chip_data gt911_chip_data = { 75 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, 76 .config_len = GOODIX_CONFIG_911_LENGTH, 77 .check_config = goodix_check_cfg_8, 78 .calc_config_checksum = goodix_calc_cfg_checksum_8, 79 }; 80 81 static const struct goodix_chip_data gt967_chip_data = { 82 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, 83 .config_len = GOODIX_CONFIG_967_LENGTH, 84 .check_config = goodix_check_cfg_8, 85 .calc_config_checksum = goodix_calc_cfg_checksum_8, 86 }; 87 88 static const struct goodix_chip_data gt9x_chip_data = { 89 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, 90 .config_len = GOODIX_CONFIG_GT9X_LENGTH, 91 .check_config = goodix_check_cfg_8, 92 .calc_config_checksum = goodix_calc_cfg_checksum_8, 93 }; 94 95 static const struct goodix_chip_id goodix_chip_ids[] = { 96 { .id = "1151", .data = >1x_chip_data }, 97 { .id = "5663", .data = >1x_chip_data }, 98 { .id = "5688", .data = >1x_chip_data }, 99 { .id = "917S", .data = >1x_chip_data }, 100 { .id = "9286", .data = >1x_chip_data }, 101 102 { .id = "911", .data = >911_chip_data }, 103 { .id = "9271", .data = >911_chip_data }, 104 { .id = "9110", .data = >911_chip_data }, 105 { .id = "927", .data = >911_chip_data }, 106 { .id = "928", .data = >911_chip_data }, 107 108 { .id = "912", .data = >967_chip_data }, 109 { .id = "9147", .data = >967_chip_data }, 110 { .id = "967", .data = >967_chip_data }, 111 { } 112 }; 113 114 static const unsigned long goodix_irq_flags[] = { 115 IRQ_TYPE_EDGE_RISING, 116 IRQ_TYPE_EDGE_FALLING, 117 IRQ_TYPE_LEVEL_LOW, 118 IRQ_TYPE_LEVEL_HIGH, 119 }; 120 121 /* 122 * Those tablets have their coordinates origin at the bottom right 123 * of the tablet, as if rotated 180 degrees 124 */ 125 static const struct dmi_system_id rotated_screen[] = { 126 #if defined(CONFIG_DMI) && defined(CONFIG_X86) 127 { 128 .ident = "Teclast X89", 129 .matches = { 130 /* tPAD is too generic, also match on bios date */ 131 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), 132 DMI_MATCH(DMI_BOARD_NAME, "tPAD"), 133 DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"), 134 }, 135 }, 136 { 137 .ident = "Teclast X98 Pro", 138 .matches = { 139 /* 140 * Only match BIOS date, because the manufacturers 141 * BIOS does not report the board name at all 142 * (sometimes)... 143 */ 144 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"), 145 DMI_MATCH(DMI_BIOS_DATE, "10/28/2015"), 146 }, 147 }, 148 { 149 .ident = "WinBook TW100", 150 .matches = { 151 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), 152 DMI_MATCH(DMI_PRODUCT_NAME, "TW100") 153 } 154 }, 155 { 156 .ident = "WinBook TW700", 157 .matches = { 158 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), 159 DMI_MATCH(DMI_PRODUCT_NAME, "TW700") 160 }, 161 }, 162 #endif 163 {} 164 }; 165 166 static const struct dmi_system_id nine_bytes_report[] = { 167 #if defined(CONFIG_DMI) && defined(CONFIG_X86) 168 { 169 .ident = "Lenovo YogaBook", 170 /* YB1-X91L/F and YB1-X90L/F */ 171 .matches = { 172 DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9") 173 } 174 }, 175 #endif 176 {} 177 }; 178 179 /* 180 * Those tablets have their x coordinate inverted 181 */ 182 static const struct dmi_system_id inverted_x_screen[] = { 183 #if defined(CONFIG_DMI) && defined(CONFIG_X86) 184 { 185 .ident = "Cube I15-TC", 186 .matches = { 187 DMI_MATCH(DMI_SYS_VENDOR, "Cube"), 188 DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC") 189 }, 190 }, 191 #endif 192 {} 193 }; 194 195 /** 196 * goodix_i2c_read - read data from a register of the i2c slave device. 197 * 198 * @client: i2c device. 199 * @reg: the register to read from. 200 * @buf: raw write data buffer. 201 * @len: length of the buffer to write 202 */ 203 int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len) 204 { 205 struct i2c_msg msgs[2]; 206 __be16 wbuf = cpu_to_be16(reg); 207 int ret; 208 209 msgs[0].flags = 0; 210 msgs[0].addr = client->addr; 211 msgs[0].len = 2; 212 msgs[0].buf = (u8 *)&wbuf; 213 214 msgs[1].flags = I2C_M_RD; 215 msgs[1].addr = client->addr; 216 msgs[1].len = len; 217 msgs[1].buf = buf; 218 219 ret = i2c_transfer(client->adapter, msgs, 2); 220 if (ret >= 0) 221 ret = (ret == ARRAY_SIZE(msgs) ? 0 : -EIO); 222 223 if (ret) 224 dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d\n", 225 len, reg, ret); 226 return ret; 227 } 228 229 /** 230 * goodix_i2c_write - write data to a register of the i2c slave device. 231 * 232 * @client: i2c device. 233 * @reg: the register to write to. 234 * @buf: raw data buffer to write. 235 * @len: length of the buffer to write 236 */ 237 int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, int len) 238 { 239 u8 *addr_buf; 240 struct i2c_msg msg; 241 int ret; 242 243 addr_buf = kmalloc(len + 2, GFP_KERNEL); 244 if (!addr_buf) 245 return -ENOMEM; 246 247 addr_buf[0] = reg >> 8; 248 addr_buf[1] = reg & 0xFF; 249 memcpy(&addr_buf[2], buf, len); 250 251 msg.flags = 0; 252 msg.addr = client->addr; 253 msg.buf = addr_buf; 254 msg.len = len + 2; 255 256 ret = i2c_transfer(client->adapter, &msg, 1); 257 if (ret >= 0) 258 ret = (ret == 1 ? 0 : -EIO); 259 260 kfree(addr_buf); 261 262 if (ret) 263 dev_err(&client->dev, "Error writing %d bytes to 0x%04x: %d\n", 264 len, reg, ret); 265 return ret; 266 } 267 268 int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value) 269 { 270 return goodix_i2c_write(client, reg, &value, sizeof(value)); 271 } 272 273 static const struct goodix_chip_data *goodix_get_chip_data(const char *id) 274 { 275 unsigned int i; 276 277 for (i = 0; goodix_chip_ids[i].id; i++) { 278 if (!strcmp(goodix_chip_ids[i].id, id)) 279 return goodix_chip_ids[i].data; 280 } 281 282 return >9x_chip_data; 283 } 284 285 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data) 286 { 287 unsigned long max_timeout; 288 int touch_num; 289 int error; 290 u16 addr = GOODIX_READ_COOR_ADDR; 291 /* 292 * We are going to read 1-byte header, 293 * ts->contact_size * max(1, touch_num) bytes of coordinates 294 * and 1-byte footer which contains the touch-key code. 295 */ 296 const int header_contact_keycode_size = 1 + ts->contact_size + 1; 297 298 /* 299 * The 'buffer status' bit, which indicates that the data is valid, is 300 * not set as soon as the interrupt is raised, but slightly after. 301 * This takes around 10 ms to happen, so we poll for 20 ms. 302 */ 303 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT); 304 do { 305 error = goodix_i2c_read(ts->client, addr, data, 306 header_contact_keycode_size); 307 if (error) 308 return error; 309 310 if (data[0] & GOODIX_BUFFER_STATUS_READY) { 311 touch_num = data[0] & 0x0f; 312 if (touch_num > ts->max_touch_num) 313 return -EPROTO; 314 315 if (touch_num > 1) { 316 addr += header_contact_keycode_size; 317 data += header_contact_keycode_size; 318 error = goodix_i2c_read(ts->client, 319 addr, data, 320 ts->contact_size * 321 (touch_num - 1)); 322 if (error) 323 return error; 324 } 325 326 return touch_num; 327 } 328 329 if (data[0] == 0 && ts->firmware_name) { 330 if (goodix_handle_fw_request(ts)) 331 return 0; 332 } 333 334 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */ 335 } while (time_before(jiffies, max_timeout)); 336 337 /* 338 * The Goodix panel will send spurious interrupts after a 339 * 'finger up' event, which will always cause a timeout. 340 */ 341 return -ENOMSG; 342 } 343 344 static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data) 345 { 346 int id = coor_data[0] & 0x0F; 347 int input_x = get_unaligned_le16(&coor_data[1]); 348 int input_y = get_unaligned_le16(&coor_data[3]); 349 int input_w = get_unaligned_le16(&coor_data[5]); 350 351 input_mt_slot(ts->input_dev, id); 352 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); 353 touchscreen_report_pos(ts->input_dev, &ts->prop, 354 input_x, input_y, true); 355 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); 356 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); 357 } 358 359 static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data) 360 { 361 int id = coor_data[1] & 0x0F; 362 int input_x = get_unaligned_le16(&coor_data[3]); 363 int input_y = get_unaligned_le16(&coor_data[5]); 364 int input_w = get_unaligned_le16(&coor_data[7]); 365 366 input_mt_slot(ts->input_dev, id); 367 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); 368 touchscreen_report_pos(ts->input_dev, &ts->prop, 369 input_x, input_y, true); 370 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); 371 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); 372 } 373 374 static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data) 375 { 376 int touch_num; 377 u8 key_value; 378 int i; 379 380 if (data[0] & GOODIX_HAVE_KEY) { 381 touch_num = data[0] & 0x0f; 382 key_value = data[1 + ts->contact_size * touch_num]; 383 for (i = 0; i < GOODIX_MAX_KEYS; i++) 384 if (key_value & BIT(i)) 385 input_report_key(ts->input_dev, 386 ts->keymap[i], 1); 387 } else { 388 for (i = 0; i < GOODIX_MAX_KEYS; i++) 389 input_report_key(ts->input_dev, ts->keymap[i], 0); 390 } 391 } 392 393 /** 394 * goodix_process_events - Process incoming events 395 * 396 * @ts: our goodix_ts_data pointer 397 * 398 * Called when the IRQ is triggered. Read the current device state, and push 399 * the input events to the user space. 400 */ 401 static void goodix_process_events(struct goodix_ts_data *ts) 402 { 403 u8 point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; 404 int touch_num; 405 int i; 406 407 touch_num = goodix_ts_read_input_report(ts, point_data); 408 if (touch_num < 0) 409 return; 410 411 goodix_ts_report_key(ts, point_data); 412 413 for (i = 0; i < touch_num; i++) 414 if (ts->contact_size == 9) 415 goodix_ts_report_touch_9b(ts, 416 &point_data[1 + ts->contact_size * i]); 417 else 418 goodix_ts_report_touch_8b(ts, 419 &point_data[1 + ts->contact_size * i]); 420 421 input_mt_sync_frame(ts->input_dev); 422 input_sync(ts->input_dev); 423 } 424 425 /** 426 * goodix_ts_irq_handler - The IRQ handler 427 * 428 * @irq: interrupt number. 429 * @dev_id: private data pointer. 430 */ 431 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) 432 { 433 struct goodix_ts_data *ts = dev_id; 434 435 goodix_process_events(ts); 436 goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0); 437 438 return IRQ_HANDLED; 439 } 440 441 static void goodix_free_irq(struct goodix_ts_data *ts) 442 { 443 devm_free_irq(&ts->client->dev, ts->client->irq, ts); 444 } 445 446 static int goodix_request_irq(struct goodix_ts_data *ts) 447 { 448 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq, 449 NULL, goodix_ts_irq_handler, 450 ts->irq_flags, ts->client->name, ts); 451 } 452 453 static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len) 454 { 455 int i, raw_cfg_len = len - 2; 456 u8 check_sum = 0; 457 458 for (i = 0; i < raw_cfg_len; i++) 459 check_sum += cfg[i]; 460 check_sum = (~check_sum) + 1; 461 if (check_sum != cfg[raw_cfg_len]) { 462 dev_err(&ts->client->dev, 463 "The checksum of the config fw is not correct"); 464 return -EINVAL; 465 } 466 467 if (cfg[raw_cfg_len + 1] != 1) { 468 dev_err(&ts->client->dev, 469 "Config fw must have Config_Fresh register set"); 470 return -EINVAL; 471 } 472 473 return 0; 474 } 475 476 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts) 477 { 478 int i, raw_cfg_len = ts->chip->config_len - 2; 479 u8 check_sum = 0; 480 481 for (i = 0; i < raw_cfg_len; i++) 482 check_sum += ts->config[i]; 483 check_sum = (~check_sum) + 1; 484 485 ts->config[raw_cfg_len] = check_sum; 486 ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */ 487 } 488 489 static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg, 490 int len) 491 { 492 int i, raw_cfg_len = len - 3; 493 u16 check_sum = 0; 494 495 for (i = 0; i < raw_cfg_len; i += 2) 496 check_sum += get_unaligned_be16(&cfg[i]); 497 check_sum = (~check_sum) + 1; 498 if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) { 499 dev_err(&ts->client->dev, 500 "The checksum of the config fw is not correct"); 501 return -EINVAL; 502 } 503 504 if (cfg[raw_cfg_len + 2] != 1) { 505 dev_err(&ts->client->dev, 506 "Config fw must have Config_Fresh register set"); 507 return -EINVAL; 508 } 509 510 return 0; 511 } 512 513 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts) 514 { 515 int i, raw_cfg_len = ts->chip->config_len - 3; 516 u16 check_sum = 0; 517 518 for (i = 0; i < raw_cfg_len; i += 2) 519 check_sum += get_unaligned_be16(&ts->config[i]); 520 check_sum = (~check_sum) + 1; 521 522 put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]); 523 ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */ 524 } 525 526 /** 527 * goodix_check_cfg - Checks if config fw is valid 528 * 529 * @ts: goodix_ts_data pointer 530 * @cfg: firmware config data 531 * @len: config data length 532 */ 533 static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len) 534 { 535 if (len < GOODIX_CONFIG_MIN_LENGTH || 536 len > GOODIX_CONFIG_MAX_LENGTH) { 537 dev_err(&ts->client->dev, 538 "The length of the config fw is not correct"); 539 return -EINVAL; 540 } 541 542 return ts->chip->check_config(ts, cfg, len); 543 } 544 545 /** 546 * goodix_send_cfg - Write fw config to device 547 * 548 * @ts: goodix_ts_data pointer 549 * @cfg: config firmware to write to device 550 * @len: config data length 551 */ 552 int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len) 553 { 554 int error; 555 556 error = goodix_check_cfg(ts, cfg, len); 557 if (error) 558 return error; 559 560 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len); 561 if (error) 562 return error; 563 564 dev_dbg(&ts->client->dev, "Config sent successfully."); 565 566 /* Let the firmware reconfigure itself, so sleep for 10ms */ 567 usleep_range(10000, 11000); 568 569 return 0; 570 } 571 572 #ifdef ACPI_GPIO_SUPPORT 573 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts) 574 { 575 acpi_handle handle = ACPI_HANDLE(&ts->client->dev); 576 acpi_status status; 577 578 status = acpi_evaluate_object(handle, "INTI", NULL, NULL); 579 return ACPI_SUCCESS(status) ? 0 : -EIO; 580 } 581 582 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value) 583 { 584 acpi_handle handle = ACPI_HANDLE(&ts->client->dev); 585 acpi_status status; 586 587 status = acpi_execute_simple_method(handle, "INTO", value); 588 return ACPI_SUCCESS(status) ? 0 : -EIO; 589 } 590 #else 591 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts) 592 { 593 dev_err(&ts->client->dev, 594 "%s called on device without ACPI support\n", __func__); 595 return -EINVAL; 596 } 597 598 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value) 599 { 600 dev_err(&ts->client->dev, 601 "%s called on device without ACPI support\n", __func__); 602 return -EINVAL; 603 } 604 #endif 605 606 static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value) 607 { 608 switch (ts->irq_pin_access_method) { 609 case IRQ_PIN_ACCESS_NONE: 610 dev_err(&ts->client->dev, 611 "%s called without an irq_pin_access_method set\n", 612 __func__); 613 return -EINVAL; 614 case IRQ_PIN_ACCESS_GPIO: 615 return gpiod_direction_output(ts->gpiod_int, value); 616 case IRQ_PIN_ACCESS_ACPI_GPIO: 617 /* 618 * The IRQ pin triggers on a falling edge, so its gets marked 619 * as active-low, use output_raw to avoid the value inversion. 620 */ 621 return gpiod_direction_output_raw(ts->gpiod_int, value); 622 case IRQ_PIN_ACCESS_ACPI_METHOD: 623 return goodix_pin_acpi_output_method(ts, value); 624 } 625 626 return -EINVAL; /* Never reached */ 627 } 628 629 static int goodix_irq_direction_input(struct goodix_ts_data *ts) 630 { 631 switch (ts->irq_pin_access_method) { 632 case IRQ_PIN_ACCESS_NONE: 633 dev_err(&ts->client->dev, 634 "%s called without an irq_pin_access_method set\n", 635 __func__); 636 return -EINVAL; 637 case IRQ_PIN_ACCESS_GPIO: 638 return gpiod_direction_input(ts->gpiod_int); 639 case IRQ_PIN_ACCESS_ACPI_GPIO: 640 return gpiod_direction_input(ts->gpiod_int); 641 case IRQ_PIN_ACCESS_ACPI_METHOD: 642 return goodix_pin_acpi_direction_input(ts); 643 } 644 645 return -EINVAL; /* Never reached */ 646 } 647 648 int goodix_int_sync(struct goodix_ts_data *ts) 649 { 650 int error; 651 652 error = goodix_irq_direction_output(ts, 0); 653 if (error) 654 goto error; 655 656 msleep(50); /* T5: 50ms */ 657 658 error = goodix_irq_direction_input(ts); 659 if (error) 660 goto error; 661 662 return 0; 663 664 error: 665 dev_err(&ts->client->dev, "Controller irq sync failed.\n"); 666 return error; 667 } 668 669 /** 670 * goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode 671 * 672 * @ts: goodix_ts_data pointer 673 */ 674 int goodix_reset_no_int_sync(struct goodix_ts_data *ts) 675 { 676 int error; 677 678 /* begin select I2C slave addr */ 679 error = gpiod_direction_output(ts->gpiod_rst, 0); 680 if (error) 681 goto error; 682 683 msleep(20); /* T2: > 10ms */ 684 685 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */ 686 error = goodix_irq_direction_output(ts, ts->client->addr == 0x14); 687 if (error) 688 goto error; 689 690 usleep_range(100, 2000); /* T3: > 100us */ 691 692 error = gpiod_direction_output(ts->gpiod_rst, 1); 693 if (error) 694 goto error; 695 696 usleep_range(6000, 10000); /* T4: > 5ms */ 697 698 /* 699 * Put the reset pin back in to input / high-impedance mode to save 700 * power. Only do this in the non ACPI case since some ACPI boards 701 * don't have a pull-up, so there the reset pin must stay active-high. 702 */ 703 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_GPIO) { 704 error = gpiod_direction_input(ts->gpiod_rst); 705 if (error) 706 goto error; 707 } 708 709 return 0; 710 711 error: 712 dev_err(&ts->client->dev, "Controller reset failed.\n"); 713 return error; 714 } 715 716 /** 717 * goodix_reset - Reset device during power on 718 * 719 * @ts: goodix_ts_data pointer 720 */ 721 static int goodix_reset(struct goodix_ts_data *ts) 722 { 723 int error; 724 725 error = goodix_reset_no_int_sync(ts); 726 if (error) 727 return error; 728 729 return goodix_int_sync(ts); 730 } 731 732 #ifdef ACPI_GPIO_SUPPORT 733 #include <asm/cpu_device_id.h> 734 #include <asm/intel-family.h> 735 736 static const struct x86_cpu_id baytrail_cpu_ids[] = { 737 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, X86_FEATURE_ANY, }, 738 {} 739 }; 740 741 static inline bool is_byt(void) 742 { 743 const struct x86_cpu_id *id = x86_match_cpu(baytrail_cpu_ids); 744 745 return !!id; 746 } 747 748 static const struct acpi_gpio_params first_gpio = { 0, 0, false }; 749 static const struct acpi_gpio_params second_gpio = { 1, 0, false }; 750 751 static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = { 752 { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 }, 753 { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 }, 754 { }, 755 }; 756 757 static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = { 758 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 }, 759 { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 }, 760 { }, 761 }; 762 763 static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = { 764 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 }, 765 { }, 766 }; 767 768 static int goodix_resource(struct acpi_resource *ares, void *data) 769 { 770 struct goodix_ts_data *ts = data; 771 struct device *dev = &ts->client->dev; 772 struct acpi_resource_gpio *gpio; 773 774 switch (ares->type) { 775 case ACPI_RESOURCE_TYPE_GPIO: 776 gpio = &ares->data.gpio; 777 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) { 778 if (ts->gpio_int_idx == -1) { 779 ts->gpio_int_idx = ts->gpio_count; 780 } else { 781 dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n"); 782 ts->gpio_int_idx = -2; 783 } 784 } 785 ts->gpio_count++; 786 break; 787 default: 788 break; 789 } 790 791 return 0; 792 } 793 794 /* 795 * This function gets called in case we fail to get the irq GPIO directly 796 * because the ACPI tables lack GPIO-name to APCI _CRS index mappings 797 * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data). 798 * In that case we add our own mapping and then goodix_get_gpio_config() 799 * retries to get the GPIOs based on the added mapping. 800 */ 801 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts) 802 { 803 const struct acpi_gpio_mapping *gpio_mapping = NULL; 804 struct device *dev = &ts->client->dev; 805 LIST_HEAD(resources); 806 int ret; 807 808 ts->gpio_count = 0; 809 ts->gpio_int_idx = -1; 810 ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources, 811 goodix_resource, ts); 812 if (ret < 0) { 813 dev_err(dev, "Error getting ACPI resources: %d\n", ret); 814 return ret; 815 } 816 817 acpi_dev_free_resource_list(&resources); 818 819 if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) { 820 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO; 821 gpio_mapping = acpi_goodix_int_first_gpios; 822 } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) { 823 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO; 824 gpio_mapping = acpi_goodix_int_last_gpios; 825 } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 && 826 acpi_has_method(ACPI_HANDLE(dev), "INTI") && 827 acpi_has_method(ACPI_HANDLE(dev), "INTO")) { 828 dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n"); 829 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD; 830 gpio_mapping = acpi_goodix_reset_only_gpios; 831 } else if (is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) { 832 dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n"); 833 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO; 834 gpio_mapping = acpi_goodix_int_last_gpios; 835 } else { 836 dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n", 837 ts->gpio_count, ts->gpio_int_idx); 838 return -EINVAL; 839 } 840 841 /* 842 * Normally we put the reset pin in input / high-impedance mode to save 843 * power. But some x86/ACPI boards don't have a pull-up, so for the ACPI 844 * case, leave the pin as is. This results in the pin not being touched 845 * at all on x86/ACPI boards, except when needed for error-recover. 846 */ 847 ts->gpiod_rst_flags = GPIOD_ASIS; 848 849 return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping); 850 } 851 #else 852 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts) 853 { 854 return -EINVAL; 855 } 856 #endif /* CONFIG_X86 && CONFIG_ACPI */ 857 858 /** 859 * goodix_get_gpio_config - Get GPIO config from ACPI/DT 860 * 861 * @ts: goodix_ts_data pointer 862 */ 863 static int goodix_get_gpio_config(struct goodix_ts_data *ts) 864 { 865 int error; 866 struct device *dev; 867 struct gpio_desc *gpiod; 868 bool added_acpi_mappings = false; 869 870 if (!ts->client) 871 return -EINVAL; 872 dev = &ts->client->dev; 873 874 /* 875 * By default we request the reset pin as input, leaving it in 876 * high-impedance when not resetting the controller to save power. 877 */ 878 ts->gpiod_rst_flags = GPIOD_IN; 879 880 ts->avdd28 = devm_regulator_get(dev, "AVDD28"); 881 if (IS_ERR(ts->avdd28)) { 882 error = PTR_ERR(ts->avdd28); 883 if (error != -EPROBE_DEFER) 884 dev_err(dev, 885 "Failed to get AVDD28 regulator: %d\n", error); 886 return error; 887 } 888 889 ts->vddio = devm_regulator_get(dev, "VDDIO"); 890 if (IS_ERR(ts->vddio)) { 891 error = PTR_ERR(ts->vddio); 892 if (error != -EPROBE_DEFER) 893 dev_err(dev, 894 "Failed to get VDDIO regulator: %d\n", error); 895 return error; 896 } 897 898 retry_get_irq_gpio: 899 /* Get the interrupt GPIO pin number */ 900 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN); 901 if (IS_ERR(gpiod)) { 902 error = PTR_ERR(gpiod); 903 if (error != -EPROBE_DEFER) 904 dev_dbg(dev, "Failed to get %s GPIO: %d\n", 905 GOODIX_GPIO_INT_NAME, error); 906 return error; 907 } 908 if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) { 909 added_acpi_mappings = true; 910 if (goodix_add_acpi_gpio_mappings(ts) == 0) 911 goto retry_get_irq_gpio; 912 } 913 914 ts->gpiod_int = gpiod; 915 916 /* Get the reset line GPIO pin number */ 917 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, ts->gpiod_rst_flags); 918 if (IS_ERR(gpiod)) { 919 error = PTR_ERR(gpiod); 920 if (error != -EPROBE_DEFER) 921 dev_dbg(dev, "Failed to get %s GPIO: %d\n", 922 GOODIX_GPIO_RST_NAME, error); 923 return error; 924 } 925 926 ts->gpiod_rst = gpiod; 927 928 switch (ts->irq_pin_access_method) { 929 case IRQ_PIN_ACCESS_ACPI_GPIO: 930 /* 931 * We end up here if goodix_add_acpi_gpio_mappings() has 932 * called devm_acpi_dev_add_driver_gpios() because the ACPI 933 * tables did not contain name to index mappings. 934 * Check that we successfully got both GPIOs after we've 935 * added our own acpi_gpio_mapping and if we did not get both 936 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE. 937 */ 938 if (!ts->gpiod_int || !ts->gpiod_rst) 939 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE; 940 break; 941 case IRQ_PIN_ACCESS_ACPI_METHOD: 942 if (!ts->gpiod_rst) 943 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE; 944 break; 945 default: 946 if (ts->gpiod_int && ts->gpiod_rst) { 947 ts->reset_controller_at_probe = true; 948 ts->load_cfg_from_disk = true; 949 ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO; 950 } 951 } 952 953 return 0; 954 } 955 956 /** 957 * goodix_read_config - Read the embedded configuration of the panel 958 * 959 * @ts: our goodix_ts_data pointer 960 * 961 * Must be called during probe 962 */ 963 static void goodix_read_config(struct goodix_ts_data *ts) 964 { 965 int x_max, y_max; 966 int error; 967 968 /* 969 * On controllers where we need to upload the firmware 970 * (controllers without flash) ts->config already has the config 971 * at this point and the controller itself does not have it yet! 972 */ 973 if (!ts->firmware_name) { 974 error = goodix_i2c_read(ts->client, ts->chip->config_addr, 975 ts->config, ts->chip->config_len); 976 if (error) { 977 ts->int_trigger_type = GOODIX_INT_TRIGGER; 978 ts->max_touch_num = GOODIX_MAX_CONTACTS; 979 return; 980 } 981 } 982 983 ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03; 984 ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f; 985 986 x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]); 987 y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]); 988 if (x_max && y_max) { 989 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1); 990 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1); 991 } 992 993 ts->chip->calc_config_checksum(ts); 994 } 995 996 /** 997 * goodix_read_version - Read goodix touchscreen version 998 * 999 * @ts: our goodix_ts_data pointer 1000 */ 1001 static int goodix_read_version(struct goodix_ts_data *ts) 1002 { 1003 int error; 1004 u8 buf[6]; 1005 char id_str[GOODIX_ID_MAX_LEN + 1]; 1006 1007 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf)); 1008 if (error) 1009 return error; 1010 1011 memcpy(id_str, buf, GOODIX_ID_MAX_LEN); 1012 id_str[GOODIX_ID_MAX_LEN] = 0; 1013 strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1); 1014 1015 ts->version = get_unaligned_le16(&buf[4]); 1016 1017 dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id, 1018 ts->version); 1019 1020 return 0; 1021 } 1022 1023 /** 1024 * goodix_i2c_test - I2C test function to check if the device answers. 1025 * 1026 * @client: the i2c client 1027 */ 1028 static int goodix_i2c_test(struct i2c_client *client) 1029 { 1030 int retry = 0; 1031 int error; 1032 u8 test; 1033 1034 while (retry++ < 2) { 1035 error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1); 1036 if (!error) 1037 return 0; 1038 1039 msleep(20); 1040 } 1041 1042 return error; 1043 } 1044 1045 /** 1046 * goodix_configure_dev - Finish device initialization 1047 * 1048 * @ts: our goodix_ts_data pointer 1049 * 1050 * Must be called from probe to finish initialization of the device. 1051 * Contains the common initialization code for both devices that 1052 * declare gpio pins and devices that do not. It is either called 1053 * directly from probe or from request_firmware_wait callback. 1054 */ 1055 static int goodix_configure_dev(struct goodix_ts_data *ts) 1056 { 1057 int error; 1058 int i; 1059 1060 ts->int_trigger_type = GOODIX_INT_TRIGGER; 1061 ts->max_touch_num = GOODIX_MAX_CONTACTS; 1062 1063 ts->input_dev = devm_input_allocate_device(&ts->client->dev); 1064 if (!ts->input_dev) { 1065 dev_err(&ts->client->dev, "Failed to allocate input device."); 1066 return -ENOMEM; 1067 } 1068 1069 ts->input_dev->name = "Goodix Capacitive TouchScreen"; 1070 ts->input_dev->phys = "input/ts"; 1071 ts->input_dev->id.bustype = BUS_I2C; 1072 ts->input_dev->id.vendor = 0x0416; 1073 if (kstrtou16(ts->id, 10, &ts->input_dev->id.product)) 1074 ts->input_dev->id.product = 0x1001; 1075 ts->input_dev->id.version = ts->version; 1076 1077 ts->input_dev->keycode = ts->keymap; 1078 ts->input_dev->keycodesize = sizeof(ts->keymap[0]); 1079 ts->input_dev->keycodemax = GOODIX_MAX_KEYS; 1080 1081 /* Capacitive Windows/Home button on some devices */ 1082 for (i = 0; i < GOODIX_MAX_KEYS; ++i) { 1083 if (i == 0) 1084 ts->keymap[i] = KEY_LEFTMETA; 1085 else 1086 ts->keymap[i] = KEY_F1 + (i - 1); 1087 1088 input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]); 1089 } 1090 1091 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X); 1092 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y); 1093 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); 1094 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 1095 1096 /* Read configuration and apply touchscreen parameters */ 1097 goodix_read_config(ts); 1098 1099 /* Try overriding touchscreen parameters via device properties */ 1100 touchscreen_parse_properties(ts->input_dev, true, &ts->prop); 1101 1102 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) { 1103 dev_err(&ts->client->dev, 1104 "Invalid config (%d, %d, %d), using defaults\n", 1105 ts->prop.max_x, ts->prop.max_y, ts->max_touch_num); 1106 ts->prop.max_x = GOODIX_MAX_WIDTH - 1; 1107 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1; 1108 ts->max_touch_num = GOODIX_MAX_CONTACTS; 1109 input_abs_set_max(ts->input_dev, 1110 ABS_MT_POSITION_X, ts->prop.max_x); 1111 input_abs_set_max(ts->input_dev, 1112 ABS_MT_POSITION_Y, ts->prop.max_y); 1113 } 1114 1115 if (dmi_check_system(rotated_screen)) { 1116 ts->prop.invert_x = true; 1117 ts->prop.invert_y = true; 1118 dev_dbg(&ts->client->dev, 1119 "Applying '180 degrees rotated screen' quirk\n"); 1120 } 1121 1122 if (dmi_check_system(nine_bytes_report)) { 1123 ts->contact_size = 9; 1124 1125 dev_dbg(&ts->client->dev, 1126 "Non-standard 9-bytes report format quirk\n"); 1127 } 1128 1129 if (dmi_check_system(inverted_x_screen)) { 1130 ts->prop.invert_x = true; 1131 dev_dbg(&ts->client->dev, 1132 "Applying 'inverted x screen' quirk\n"); 1133 } 1134 1135 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num, 1136 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 1137 if (error) { 1138 dev_err(&ts->client->dev, 1139 "Failed to initialize MT slots: %d", error); 1140 return error; 1141 } 1142 1143 error = input_register_device(ts->input_dev); 1144 if (error) { 1145 dev_err(&ts->client->dev, 1146 "Failed to register input device: %d", error); 1147 return error; 1148 } 1149 1150 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT; 1151 error = goodix_request_irq(ts); 1152 if (error) { 1153 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error); 1154 return error; 1155 } 1156 1157 return 0; 1158 } 1159 1160 /** 1161 * goodix_config_cb - Callback to finish device init 1162 * 1163 * @cfg: firmware config 1164 * @ctx: our goodix_ts_data pointer 1165 * 1166 * request_firmware_wait callback that finishes 1167 * initialization of the device. 1168 */ 1169 static void goodix_config_cb(const struct firmware *cfg, void *ctx) 1170 { 1171 struct goodix_ts_data *ts = ctx; 1172 int error; 1173 1174 if (ts->firmware_name) { 1175 if (!cfg) 1176 goto err_release_cfg; 1177 1178 error = goodix_check_cfg(ts, cfg->data, cfg->size); 1179 if (error) 1180 goto err_release_cfg; 1181 1182 memcpy(ts->config, cfg->data, cfg->size); 1183 } else if (cfg) { 1184 /* send device configuration to the firmware */ 1185 error = goodix_send_cfg(ts, cfg->data, cfg->size); 1186 if (error) 1187 goto err_release_cfg; 1188 } 1189 1190 goodix_configure_dev(ts); 1191 1192 err_release_cfg: 1193 release_firmware(cfg); 1194 complete_all(&ts->firmware_loading_complete); 1195 } 1196 1197 static void goodix_disable_regulators(void *arg) 1198 { 1199 struct goodix_ts_data *ts = arg; 1200 1201 regulator_disable(ts->vddio); 1202 regulator_disable(ts->avdd28); 1203 } 1204 1205 static int goodix_ts_probe(struct i2c_client *client, 1206 const struct i2c_device_id *id) 1207 { 1208 struct goodix_ts_data *ts; 1209 const char *cfg_name; 1210 int error; 1211 1212 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr); 1213 1214 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1215 dev_err(&client->dev, "I2C check functionality failed.\n"); 1216 return -ENXIO; 1217 } 1218 1219 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 1220 if (!ts) 1221 return -ENOMEM; 1222 1223 ts->client = client; 1224 i2c_set_clientdata(client, ts); 1225 init_completion(&ts->firmware_loading_complete); 1226 ts->contact_size = GOODIX_CONTACT_SIZE; 1227 1228 error = goodix_get_gpio_config(ts); 1229 if (error) 1230 return error; 1231 1232 /* power up the controller */ 1233 error = regulator_enable(ts->avdd28); 1234 if (error) { 1235 dev_err(&client->dev, 1236 "Failed to enable AVDD28 regulator: %d\n", 1237 error); 1238 return error; 1239 } 1240 1241 error = regulator_enable(ts->vddio); 1242 if (error) { 1243 dev_err(&client->dev, 1244 "Failed to enable VDDIO regulator: %d\n", 1245 error); 1246 regulator_disable(ts->avdd28); 1247 return error; 1248 } 1249 1250 error = devm_add_action_or_reset(&client->dev, 1251 goodix_disable_regulators, ts); 1252 if (error) 1253 return error; 1254 1255 reset: 1256 if (ts->reset_controller_at_probe) { 1257 /* reset the controller */ 1258 error = goodix_reset(ts); 1259 if (error) 1260 return error; 1261 } 1262 1263 error = goodix_i2c_test(client); 1264 if (error) { 1265 if (!ts->reset_controller_at_probe && 1266 ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) { 1267 /* Retry after a controller reset */ 1268 ts->reset_controller_at_probe = true; 1269 goto reset; 1270 } 1271 dev_err(&client->dev, "I2C communication failure: %d\n", error); 1272 return error; 1273 } 1274 1275 error = goodix_firmware_check(ts); 1276 if (error) 1277 return error; 1278 1279 error = goodix_read_version(ts); 1280 if (error) 1281 return error; 1282 1283 ts->chip = goodix_get_chip_data(ts->id); 1284 1285 if (ts->load_cfg_from_disk) { 1286 /* update device config */ 1287 error = device_property_read_string(&client->dev, 1288 "goodix,config-name", 1289 &cfg_name); 1290 if (!error) 1291 snprintf(ts->cfg_name, sizeof(ts->cfg_name), 1292 "goodix/%s", cfg_name); 1293 else 1294 snprintf(ts->cfg_name, sizeof(ts->cfg_name), 1295 "goodix_%s_cfg.bin", ts->id); 1296 1297 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name, 1298 &client->dev, GFP_KERNEL, ts, 1299 goodix_config_cb); 1300 if (error) { 1301 dev_err(&client->dev, 1302 "Failed to invoke firmware loader: %d\n", 1303 error); 1304 return error; 1305 } 1306 1307 return 0; 1308 } else { 1309 error = goodix_configure_dev(ts); 1310 if (error) 1311 return error; 1312 } 1313 1314 return 0; 1315 } 1316 1317 static int goodix_ts_remove(struct i2c_client *client) 1318 { 1319 struct goodix_ts_data *ts = i2c_get_clientdata(client); 1320 1321 if (ts->load_cfg_from_disk) 1322 wait_for_completion(&ts->firmware_loading_complete); 1323 1324 return 0; 1325 } 1326 1327 static int __maybe_unused goodix_suspend(struct device *dev) 1328 { 1329 struct i2c_client *client = to_i2c_client(dev); 1330 struct goodix_ts_data *ts = i2c_get_clientdata(client); 1331 int error; 1332 1333 if (ts->load_cfg_from_disk) 1334 wait_for_completion(&ts->firmware_loading_complete); 1335 1336 /* We need gpio pins to suspend/resume */ 1337 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) { 1338 disable_irq(client->irq); 1339 return 0; 1340 } 1341 1342 /* Free IRQ as IRQ pin is used as output in the suspend sequence */ 1343 goodix_free_irq(ts); 1344 1345 /* Save reference (calibration) info if necessary */ 1346 goodix_save_bak_ref(ts); 1347 1348 /* Output LOW on the INT pin for 5 ms */ 1349 error = goodix_irq_direction_output(ts, 0); 1350 if (error) { 1351 goodix_request_irq(ts); 1352 return error; 1353 } 1354 1355 usleep_range(5000, 6000); 1356 1357 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND, 1358 GOODIX_CMD_SCREEN_OFF); 1359 if (error) { 1360 goodix_irq_direction_input(ts); 1361 goodix_request_irq(ts); 1362 return -EAGAIN; 1363 } 1364 1365 /* 1366 * The datasheet specifies that the interval between sending screen-off 1367 * command and wake-up should be longer than 58 ms. To avoid waking up 1368 * sooner, delay 58ms here. 1369 */ 1370 msleep(58); 1371 return 0; 1372 } 1373 1374 static int __maybe_unused goodix_resume(struct device *dev) 1375 { 1376 struct i2c_client *client = to_i2c_client(dev); 1377 struct goodix_ts_data *ts = i2c_get_clientdata(client); 1378 u8 config_ver; 1379 int error; 1380 1381 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) { 1382 enable_irq(client->irq); 1383 return 0; 1384 } 1385 1386 /* 1387 * Exit sleep mode by outputting HIGH level to INT pin 1388 * for 2ms~5ms. 1389 */ 1390 error = goodix_irq_direction_output(ts, 1); 1391 if (error) 1392 return error; 1393 1394 usleep_range(2000, 5000); 1395 1396 error = goodix_int_sync(ts); 1397 if (error) 1398 return error; 1399 1400 error = goodix_i2c_read(ts->client, ts->chip->config_addr, 1401 &config_ver, 1); 1402 if (!error && config_ver != ts->config[0]) 1403 dev_info(dev, "Config version mismatch %d != %d, resetting controller\n", 1404 config_ver, ts->config[0]); 1405 1406 if (error != 0 || config_ver != ts->config[0]) { 1407 error = goodix_reset(ts); 1408 if (error) 1409 return error; 1410 1411 error = goodix_send_cfg(ts, ts->config, ts->chip->config_len); 1412 if (error) 1413 return error; 1414 } 1415 1416 error = goodix_request_irq(ts); 1417 if (error) 1418 return error; 1419 1420 return 0; 1421 } 1422 1423 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume); 1424 1425 static const struct i2c_device_id goodix_ts_id[] = { 1426 { "GDIX1001:00", 0 }, 1427 { } 1428 }; 1429 MODULE_DEVICE_TABLE(i2c, goodix_ts_id); 1430 1431 #ifdef CONFIG_ACPI 1432 static const struct acpi_device_id goodix_acpi_match[] = { 1433 { "GDIX1001", 0 }, 1434 { "GDIX1002", 0 }, 1435 { } 1436 }; 1437 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); 1438 #endif 1439 1440 #ifdef CONFIG_OF 1441 static const struct of_device_id goodix_of_match[] = { 1442 { .compatible = "goodix,gt1151" }, 1443 { .compatible = "goodix,gt5663" }, 1444 { .compatible = "goodix,gt5688" }, 1445 { .compatible = "goodix,gt911" }, 1446 { .compatible = "goodix,gt9110" }, 1447 { .compatible = "goodix,gt912" }, 1448 { .compatible = "goodix,gt9147" }, 1449 { .compatible = "goodix,gt917s" }, 1450 { .compatible = "goodix,gt927" }, 1451 { .compatible = "goodix,gt9271" }, 1452 { .compatible = "goodix,gt928" }, 1453 { .compatible = "goodix,gt9286" }, 1454 { .compatible = "goodix,gt967" }, 1455 { } 1456 }; 1457 MODULE_DEVICE_TABLE(of, goodix_of_match); 1458 #endif 1459 1460 static struct i2c_driver goodix_ts_driver = { 1461 .probe = goodix_ts_probe, 1462 .remove = goodix_ts_remove, 1463 .id_table = goodix_ts_id, 1464 .driver = { 1465 .name = "Goodix-TS", 1466 .acpi_match_table = ACPI_PTR(goodix_acpi_match), 1467 .of_match_table = of_match_ptr(goodix_of_match), 1468 .pm = &goodix_pm_ops, 1469 }, 1470 }; 1471 module_i2c_driver(goodix_ts_driver); 1472 1473 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 1474 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); 1475 MODULE_DESCRIPTION("Goodix touchscreen driver"); 1476 MODULE_LICENSE("GPL v2"); 1477