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