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