1 /* 2 * Driver for Goodix Touchscreens 3 * 4 * Copyright (c) 2014 Red Hat Inc. 5 * Copyright (c) 2015 K. Merker <merker@debian.org> 6 * 7 * This code is based on gt9xx.c authored by andrew@goodix.com: 8 * 9 * 2010 - 2012 Goodix Technology. 10 */ 11 12 /* 13 * This program is free software; you can redistribute it and/or modify it 14 * under the terms of the GNU General Public License as published by the Free 15 * Software Foundation; version 2 of the License. 16 */ 17 18 #include <linux/kernel.h> 19 #include <linux/dmi.h> 20 #include <linux/firmware.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/i2c.h> 23 #include <linux/input.h> 24 #include <linux/input/mt.h> 25 #include <linux/module.h> 26 #include <linux/delay.h> 27 #include <linux/irq.h> 28 #include <linux/interrupt.h> 29 #include <linux/slab.h> 30 #include <linux/acpi.h> 31 #include <linux/of.h> 32 #include <asm/unaligned.h> 33 34 struct goodix_ts_data; 35 36 struct goodix_chip_data { 37 u16 config_addr; 38 int config_len; 39 int (*check_config)(struct goodix_ts_data *, const struct firmware *); 40 }; 41 42 struct goodix_ts_data { 43 struct i2c_client *client; 44 struct input_dev *input_dev; 45 const struct goodix_chip_data *chip; 46 int abs_x_max; 47 int abs_y_max; 48 bool swapped_x_y; 49 bool inverted_x; 50 bool inverted_y; 51 unsigned int max_touch_num; 52 unsigned int int_trigger_type; 53 struct gpio_desc *gpiod_int; 54 struct gpio_desc *gpiod_rst; 55 u16 id; 56 u16 version; 57 const char *cfg_name; 58 struct completion firmware_loading_complete; 59 unsigned long irq_flags; 60 }; 61 62 #define GOODIX_GPIO_INT_NAME "irq" 63 #define GOODIX_GPIO_RST_NAME "reset" 64 65 #define GOODIX_MAX_HEIGHT 4096 66 #define GOODIX_MAX_WIDTH 4096 67 #define GOODIX_INT_TRIGGER 1 68 #define GOODIX_CONTACT_SIZE 8 69 #define GOODIX_MAX_CONTACTS 10 70 71 #define GOODIX_CONFIG_MAX_LENGTH 240 72 #define GOODIX_CONFIG_911_LENGTH 186 73 #define GOODIX_CONFIG_967_LENGTH 228 74 75 /* Register defines */ 76 #define GOODIX_REG_COMMAND 0x8040 77 #define GOODIX_CMD_SCREEN_OFF 0x05 78 79 #define GOODIX_READ_COOR_ADDR 0x814E 80 #define GOODIX_GT1X_REG_CONFIG_DATA 0x8050 81 #define GOODIX_GT9X_REG_CONFIG_DATA 0x8047 82 #define GOODIX_REG_ID 0x8140 83 84 #define GOODIX_BUFFER_STATUS_READY BIT(7) 85 #define GOODIX_BUFFER_STATUS_TIMEOUT 20 86 87 #define RESOLUTION_LOC 1 88 #define MAX_CONTACTS_LOC 5 89 #define TRIGGER_LOC 6 90 91 static int goodix_check_cfg_8(struct goodix_ts_data *ts, 92 const struct firmware *cfg); 93 static int goodix_check_cfg_16(struct goodix_ts_data *ts, 94 const struct firmware *cfg); 95 96 static const struct goodix_chip_data gt1x_chip_data = { 97 .config_addr = GOODIX_GT1X_REG_CONFIG_DATA, 98 .config_len = GOODIX_CONFIG_MAX_LENGTH, 99 .check_config = goodix_check_cfg_16, 100 }; 101 102 static const struct goodix_chip_data gt911_chip_data = { 103 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, 104 .config_len = GOODIX_CONFIG_911_LENGTH, 105 .check_config = goodix_check_cfg_8, 106 }; 107 108 static const struct goodix_chip_data gt967_chip_data = { 109 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, 110 .config_len = GOODIX_CONFIG_967_LENGTH, 111 .check_config = goodix_check_cfg_8, 112 }; 113 114 static const struct goodix_chip_data gt9x_chip_data = { 115 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, 116 .config_len = GOODIX_CONFIG_MAX_LENGTH, 117 .check_config = goodix_check_cfg_8, 118 }; 119 120 static const unsigned long goodix_irq_flags[] = { 121 IRQ_TYPE_EDGE_RISING, 122 IRQ_TYPE_EDGE_FALLING, 123 IRQ_TYPE_LEVEL_LOW, 124 IRQ_TYPE_LEVEL_HIGH, 125 }; 126 127 /* 128 * Those tablets have their coordinates origin at the bottom right 129 * of the tablet, as if rotated 180 degrees 130 */ 131 static const struct dmi_system_id rotated_screen[] = { 132 #if defined(CONFIG_DMI) && defined(CONFIG_X86) 133 { 134 .ident = "WinBook TW100", 135 .matches = { 136 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), 137 DMI_MATCH(DMI_PRODUCT_NAME, "TW100") 138 } 139 }, 140 { 141 .ident = "WinBook TW700", 142 .matches = { 143 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), 144 DMI_MATCH(DMI_PRODUCT_NAME, "TW700") 145 }, 146 }, 147 #endif 148 {} 149 }; 150 151 /** 152 * goodix_i2c_read - read data from a register of the i2c slave device. 153 * 154 * @client: i2c device. 155 * @reg: the register to read from. 156 * @buf: raw write data buffer. 157 * @len: length of the buffer to write 158 */ 159 static int goodix_i2c_read(struct i2c_client *client, 160 u16 reg, u8 *buf, int len) 161 { 162 struct i2c_msg msgs[2]; 163 u16 wbuf = cpu_to_be16(reg); 164 int ret; 165 166 msgs[0].flags = 0; 167 msgs[0].addr = client->addr; 168 msgs[0].len = 2; 169 msgs[0].buf = (u8 *)&wbuf; 170 171 msgs[1].flags = I2C_M_RD; 172 msgs[1].addr = client->addr; 173 msgs[1].len = len; 174 msgs[1].buf = buf; 175 176 ret = i2c_transfer(client->adapter, msgs, 2); 177 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0); 178 } 179 180 /** 181 * goodix_i2c_write - write data to a register of the i2c slave device. 182 * 183 * @client: i2c device. 184 * @reg: the register to write to. 185 * @buf: raw data buffer to write. 186 * @len: length of the buffer to write 187 */ 188 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, 189 unsigned len) 190 { 191 u8 *addr_buf; 192 struct i2c_msg msg; 193 int ret; 194 195 addr_buf = kmalloc(len + 2, GFP_KERNEL); 196 if (!addr_buf) 197 return -ENOMEM; 198 199 addr_buf[0] = reg >> 8; 200 addr_buf[1] = reg & 0xFF; 201 memcpy(&addr_buf[2], buf, len); 202 203 msg.flags = 0; 204 msg.addr = client->addr; 205 msg.buf = addr_buf; 206 msg.len = len + 2; 207 208 ret = i2c_transfer(client->adapter, &msg, 1); 209 kfree(addr_buf); 210 return ret < 0 ? ret : (ret != 1 ? -EIO : 0); 211 } 212 213 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value) 214 { 215 return goodix_i2c_write(client, reg, &value, sizeof(value)); 216 } 217 218 static const struct goodix_chip_data *goodix_get_chip_data(u16 id) 219 { 220 switch (id) { 221 case 1151: 222 return >1x_chip_data; 223 224 case 911: 225 case 9271: 226 case 9110: 227 case 927: 228 case 928: 229 return >911_chip_data; 230 231 case 912: 232 case 967: 233 return >967_chip_data; 234 235 default: 236 return >9x_chip_data; 237 } 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 246 /* 247 * The 'buffer status' bit, which indicates that the data is valid, is 248 * not set as soon as the interrupt is raised, but slightly after. 249 * This takes around 10 ms to happen, so we poll for 20 ms. 250 */ 251 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT); 252 do { 253 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, 254 data, GOODIX_CONTACT_SIZE + 1); 255 if (error) { 256 dev_err(&ts->client->dev, "I2C transfer error: %d\n", 257 error); 258 return error; 259 } 260 261 if (data[0] & GOODIX_BUFFER_STATUS_READY) { 262 touch_num = data[0] & 0x0f; 263 if (touch_num > ts->max_touch_num) 264 return -EPROTO; 265 266 if (touch_num > 1) { 267 data += 1 + GOODIX_CONTACT_SIZE; 268 error = goodix_i2c_read(ts->client, 269 GOODIX_READ_COOR_ADDR + 270 1 + GOODIX_CONTACT_SIZE, 271 data, 272 GOODIX_CONTACT_SIZE * 273 (touch_num - 1)); 274 if (error) 275 return error; 276 } 277 278 return touch_num; 279 } 280 281 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */ 282 } while (time_before(jiffies, max_timeout)); 283 284 /* 285 * The Goodix panel will send spurious interrupts after a 286 * 'finger up' event, which will always cause a timeout. 287 */ 288 return 0; 289 } 290 291 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data) 292 { 293 int id = coor_data[0] & 0x0F; 294 int input_x = get_unaligned_le16(&coor_data[1]); 295 int input_y = get_unaligned_le16(&coor_data[3]); 296 int input_w = get_unaligned_le16(&coor_data[5]); 297 298 /* Inversions have to happen before axis swapping */ 299 if (ts->inverted_x) 300 input_x = ts->abs_x_max - input_x; 301 if (ts->inverted_y) 302 input_y = ts->abs_y_max - input_y; 303 if (ts->swapped_x_y) 304 swap(input_x, input_y); 305 306 input_mt_slot(ts->input_dev, id); 307 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); 308 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x); 309 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y); 310 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); 311 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); 312 } 313 314 /** 315 * goodix_process_events - Process incoming events 316 * 317 * @ts: our goodix_ts_data pointer 318 * 319 * Called when the IRQ is triggered. Read the current device state, and push 320 * the input events to the user space. 321 */ 322 static void goodix_process_events(struct goodix_ts_data *ts) 323 { 324 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; 325 int touch_num; 326 int i; 327 328 touch_num = goodix_ts_read_input_report(ts, point_data); 329 if (touch_num < 0) 330 return; 331 332 /* 333 * Bit 4 of the first byte reports the status of the capacitive 334 * Windows/Home button. 335 */ 336 input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4)); 337 338 for (i = 0; i < touch_num; i++) 339 goodix_ts_report_touch(ts, 340 &point_data[1 + GOODIX_CONTACT_SIZE * i]); 341 342 input_mt_sync_frame(ts->input_dev); 343 input_sync(ts->input_dev); 344 } 345 346 /** 347 * goodix_ts_irq_handler - The IRQ handler 348 * 349 * @irq: interrupt number. 350 * @dev_id: private data pointer. 351 */ 352 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) 353 { 354 struct goodix_ts_data *ts = dev_id; 355 356 goodix_process_events(ts); 357 358 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0) 359 dev_err(&ts->client->dev, "I2C write end_cmd error\n"); 360 361 return IRQ_HANDLED; 362 } 363 364 static void goodix_free_irq(struct goodix_ts_data *ts) 365 { 366 devm_free_irq(&ts->client->dev, ts->client->irq, ts); 367 } 368 369 static int goodix_request_irq(struct goodix_ts_data *ts) 370 { 371 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq, 372 NULL, goodix_ts_irq_handler, 373 ts->irq_flags, ts->client->name, ts); 374 } 375 376 static int goodix_check_cfg_8(struct goodix_ts_data *ts, 377 const struct firmware *cfg) 378 { 379 int i, raw_cfg_len = cfg->size - 2; 380 u8 check_sum = 0; 381 382 for (i = 0; i < raw_cfg_len; i++) 383 check_sum += cfg->data[i]; 384 check_sum = (~check_sum) + 1; 385 if (check_sum != cfg->data[raw_cfg_len]) { 386 dev_err(&ts->client->dev, 387 "The checksum of the config fw is not correct"); 388 return -EINVAL; 389 } 390 391 if (cfg->data[raw_cfg_len + 1] != 1) { 392 dev_err(&ts->client->dev, 393 "Config fw must have Config_Fresh register set"); 394 return -EINVAL; 395 } 396 397 return 0; 398 } 399 400 static int goodix_check_cfg_16(struct goodix_ts_data *ts, 401 const struct firmware *cfg) 402 { 403 int i, raw_cfg_len = cfg->size - 3; 404 u16 check_sum = 0; 405 406 for (i = 0; i < raw_cfg_len; i += 2) 407 check_sum += get_unaligned_be16(&cfg->data[i]); 408 check_sum = (~check_sum) + 1; 409 if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) { 410 dev_err(&ts->client->dev, 411 "The checksum of the config fw is not correct"); 412 return -EINVAL; 413 } 414 415 if (cfg->data[raw_cfg_len + 2] != 1) { 416 dev_err(&ts->client->dev, 417 "Config fw must have Config_Fresh register set"); 418 return -EINVAL; 419 } 420 421 return 0; 422 } 423 424 /** 425 * goodix_check_cfg - Checks if config fw is valid 426 * 427 * @ts: goodix_ts_data pointer 428 * @cfg: firmware config data 429 */ 430 static int goodix_check_cfg(struct goodix_ts_data *ts, 431 const struct firmware *cfg) 432 { 433 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) { 434 dev_err(&ts->client->dev, 435 "The length of the config fw is not correct"); 436 return -EINVAL; 437 } 438 439 return ts->chip->check_config(ts, cfg); 440 } 441 442 /** 443 * goodix_send_cfg - Write fw config to device 444 * 445 * @ts: goodix_ts_data pointer 446 * @cfg: config firmware to write to device 447 */ 448 static int goodix_send_cfg(struct goodix_ts_data *ts, 449 const struct firmware *cfg) 450 { 451 int error; 452 453 error = goodix_check_cfg(ts, cfg); 454 if (error) 455 return error; 456 457 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data, 458 cfg->size); 459 if (error) { 460 dev_err(&ts->client->dev, "Failed to write config data: %d", 461 error); 462 return error; 463 } 464 dev_dbg(&ts->client->dev, "Config sent successfully."); 465 466 /* Let the firmware reconfigure itself, so sleep for 10ms */ 467 usleep_range(10000, 11000); 468 469 return 0; 470 } 471 472 static int goodix_int_sync(struct goodix_ts_data *ts) 473 { 474 int error; 475 476 error = gpiod_direction_output(ts->gpiod_int, 0); 477 if (error) 478 return error; 479 480 msleep(50); /* T5: 50ms */ 481 482 error = gpiod_direction_input(ts->gpiod_int); 483 if (error) 484 return error; 485 486 return 0; 487 } 488 489 /** 490 * goodix_reset - Reset device during power on 491 * 492 * @ts: goodix_ts_data pointer 493 */ 494 static int goodix_reset(struct goodix_ts_data *ts) 495 { 496 int error; 497 498 /* begin select I2C slave addr */ 499 error = gpiod_direction_output(ts->gpiod_rst, 0); 500 if (error) 501 return error; 502 503 msleep(20); /* T2: > 10ms */ 504 505 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */ 506 error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14); 507 if (error) 508 return error; 509 510 usleep_range(100, 2000); /* T3: > 100us */ 511 512 error = gpiod_direction_output(ts->gpiod_rst, 1); 513 if (error) 514 return error; 515 516 usleep_range(6000, 10000); /* T4: > 5ms */ 517 518 /* end select I2C slave addr */ 519 error = gpiod_direction_input(ts->gpiod_rst); 520 if (error) 521 return error; 522 523 error = goodix_int_sync(ts); 524 if (error) 525 return error; 526 527 return 0; 528 } 529 530 /** 531 * goodix_get_gpio_config - Get GPIO config from ACPI/DT 532 * 533 * @ts: goodix_ts_data pointer 534 */ 535 static int goodix_get_gpio_config(struct goodix_ts_data *ts) 536 { 537 int error; 538 struct device *dev; 539 struct gpio_desc *gpiod; 540 541 if (!ts->client) 542 return -EINVAL; 543 dev = &ts->client->dev; 544 545 /* Get the interrupt GPIO pin number */ 546 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN); 547 if (IS_ERR(gpiod)) { 548 error = PTR_ERR(gpiod); 549 if (error != -EPROBE_DEFER) 550 dev_dbg(dev, "Failed to get %s GPIO: %d\n", 551 GOODIX_GPIO_INT_NAME, error); 552 return error; 553 } 554 555 ts->gpiod_int = gpiod; 556 557 /* Get the reset line GPIO pin number */ 558 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN); 559 if (IS_ERR(gpiod)) { 560 error = PTR_ERR(gpiod); 561 if (error != -EPROBE_DEFER) 562 dev_dbg(dev, "Failed to get %s GPIO: %d\n", 563 GOODIX_GPIO_RST_NAME, error); 564 return error; 565 } 566 567 ts->gpiod_rst = gpiod; 568 569 return 0; 570 } 571 572 /** 573 * goodix_read_config - Read the embedded configuration of the panel 574 * 575 * @ts: our goodix_ts_data pointer 576 * 577 * Must be called during probe 578 */ 579 static void goodix_read_config(struct goodix_ts_data *ts) 580 { 581 u8 config[GOODIX_CONFIG_MAX_LENGTH]; 582 int error; 583 584 error = goodix_i2c_read(ts->client, ts->chip->config_addr, 585 config, ts->chip->config_len); 586 if (error) { 587 dev_warn(&ts->client->dev, 588 "Error reading config (%d), using defaults\n", 589 error); 590 ts->abs_x_max = GOODIX_MAX_WIDTH; 591 ts->abs_y_max = GOODIX_MAX_HEIGHT; 592 if (ts->swapped_x_y) 593 swap(ts->abs_x_max, ts->abs_y_max); 594 ts->int_trigger_type = GOODIX_INT_TRIGGER; 595 ts->max_touch_num = GOODIX_MAX_CONTACTS; 596 return; 597 } 598 599 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]); 600 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]); 601 if (ts->swapped_x_y) 602 swap(ts->abs_x_max, ts->abs_y_max); 603 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03; 604 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f; 605 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) { 606 dev_err(&ts->client->dev, 607 "Invalid config, using defaults\n"); 608 ts->abs_x_max = GOODIX_MAX_WIDTH; 609 ts->abs_y_max = GOODIX_MAX_HEIGHT; 610 if (ts->swapped_x_y) 611 swap(ts->abs_x_max, ts->abs_y_max); 612 ts->max_touch_num = GOODIX_MAX_CONTACTS; 613 } 614 615 if (dmi_check_system(rotated_screen)) { 616 ts->inverted_x = true; 617 ts->inverted_y = true; 618 dev_dbg(&ts->client->dev, 619 "Applying '180 degrees rotated screen' quirk\n"); 620 } 621 } 622 623 /** 624 * goodix_read_version - Read goodix touchscreen version 625 * 626 * @ts: our goodix_ts_data pointer 627 */ 628 static int goodix_read_version(struct goodix_ts_data *ts) 629 { 630 int error; 631 u8 buf[6]; 632 char id_str[5]; 633 634 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf)); 635 if (error) { 636 dev_err(&ts->client->dev, "read version failed: %d\n", error); 637 return error; 638 } 639 640 memcpy(id_str, buf, 4); 641 id_str[4] = 0; 642 if (kstrtou16(id_str, 10, &ts->id)) 643 ts->id = 0x1001; 644 645 ts->version = get_unaligned_le16(&buf[4]); 646 647 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id, 648 ts->version); 649 650 return 0; 651 } 652 653 /** 654 * goodix_i2c_test - I2C test function to check if the device answers. 655 * 656 * @client: the i2c client 657 */ 658 static int goodix_i2c_test(struct i2c_client *client) 659 { 660 int retry = 0; 661 int error; 662 u8 test; 663 664 while (retry++ < 2) { 665 error = goodix_i2c_read(client, GOODIX_REG_ID, 666 &test, 1); 667 if (!error) 668 return 0; 669 670 dev_err(&client->dev, "i2c test failed attempt %d: %d\n", 671 retry, error); 672 msleep(20); 673 } 674 675 return error; 676 } 677 678 /** 679 * goodix_request_input_dev - Allocate, populate and register the input device 680 * 681 * @ts: our goodix_ts_data pointer 682 * 683 * Must be called during probe 684 */ 685 static int goodix_request_input_dev(struct goodix_ts_data *ts) 686 { 687 int error; 688 689 ts->input_dev = devm_input_allocate_device(&ts->client->dev); 690 if (!ts->input_dev) { 691 dev_err(&ts->client->dev, "Failed to allocate input device."); 692 return -ENOMEM; 693 } 694 695 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 696 0, ts->abs_x_max, 0, 0); 697 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 698 0, ts->abs_y_max, 0, 0); 699 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); 700 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 701 702 input_mt_init_slots(ts->input_dev, ts->max_touch_num, 703 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 704 705 ts->input_dev->name = "Goodix Capacitive TouchScreen"; 706 ts->input_dev->phys = "input/ts"; 707 ts->input_dev->id.bustype = BUS_I2C; 708 ts->input_dev->id.vendor = 0x0416; 709 ts->input_dev->id.product = ts->id; 710 ts->input_dev->id.version = ts->version; 711 712 /* Capacitive Windows/Home button on some devices */ 713 input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA); 714 715 error = input_register_device(ts->input_dev); 716 if (error) { 717 dev_err(&ts->client->dev, 718 "Failed to register input device: %d", error); 719 return error; 720 } 721 722 return 0; 723 } 724 725 /** 726 * goodix_configure_dev - Finish device initialization 727 * 728 * @ts: our goodix_ts_data pointer 729 * 730 * Must be called from probe to finish initialization of the device. 731 * Contains the common initialization code for both devices that 732 * declare gpio pins and devices that do not. It is either called 733 * directly from probe or from request_firmware_wait callback. 734 */ 735 static int goodix_configure_dev(struct goodix_ts_data *ts) 736 { 737 int error; 738 739 ts->swapped_x_y = device_property_read_bool(&ts->client->dev, 740 "touchscreen-swapped-x-y"); 741 ts->inverted_x = device_property_read_bool(&ts->client->dev, 742 "touchscreen-inverted-x"); 743 ts->inverted_y = device_property_read_bool(&ts->client->dev, 744 "touchscreen-inverted-y"); 745 746 goodix_read_config(ts); 747 748 error = goodix_request_input_dev(ts); 749 if (error) 750 return error; 751 752 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT; 753 error = goodix_request_irq(ts); 754 if (error) { 755 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error); 756 return error; 757 } 758 759 return 0; 760 } 761 762 /** 763 * goodix_config_cb - Callback to finish device init 764 * 765 * @ts: our goodix_ts_data pointer 766 * 767 * request_firmware_wait callback that finishes 768 * initialization of the device. 769 */ 770 static void goodix_config_cb(const struct firmware *cfg, void *ctx) 771 { 772 struct goodix_ts_data *ts = ctx; 773 int error; 774 775 if (cfg) { 776 /* send device configuration to the firmware */ 777 error = goodix_send_cfg(ts, cfg); 778 if (error) 779 goto err_release_cfg; 780 } 781 782 goodix_configure_dev(ts); 783 784 err_release_cfg: 785 release_firmware(cfg); 786 complete_all(&ts->firmware_loading_complete); 787 } 788 789 static int goodix_ts_probe(struct i2c_client *client, 790 const struct i2c_device_id *id) 791 { 792 struct goodix_ts_data *ts; 793 int error; 794 795 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr); 796 797 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 798 dev_err(&client->dev, "I2C check functionality failed.\n"); 799 return -ENXIO; 800 } 801 802 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 803 if (!ts) 804 return -ENOMEM; 805 806 ts->client = client; 807 i2c_set_clientdata(client, ts); 808 init_completion(&ts->firmware_loading_complete); 809 810 error = goodix_get_gpio_config(ts); 811 if (error) 812 return error; 813 814 if (ts->gpiod_int && ts->gpiod_rst) { 815 /* reset the controller */ 816 error = goodix_reset(ts); 817 if (error) { 818 dev_err(&client->dev, "Controller reset failed.\n"); 819 return error; 820 } 821 } 822 823 error = goodix_i2c_test(client); 824 if (error) { 825 dev_err(&client->dev, "I2C communication failure: %d\n", error); 826 return error; 827 } 828 829 error = goodix_read_version(ts); 830 if (error) { 831 dev_err(&client->dev, "Read version failed.\n"); 832 return error; 833 } 834 835 ts->chip = goodix_get_chip_data(ts->id); 836 837 if (ts->gpiod_int && ts->gpiod_rst) { 838 /* update device config */ 839 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL, 840 "goodix_%d_cfg.bin", ts->id); 841 if (!ts->cfg_name) 842 return -ENOMEM; 843 844 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name, 845 &client->dev, GFP_KERNEL, ts, 846 goodix_config_cb); 847 if (error) { 848 dev_err(&client->dev, 849 "Failed to invoke firmware loader: %d\n", 850 error); 851 return error; 852 } 853 854 return 0; 855 } else { 856 error = goodix_configure_dev(ts); 857 if (error) 858 return error; 859 } 860 861 return 0; 862 } 863 864 static int goodix_ts_remove(struct i2c_client *client) 865 { 866 struct goodix_ts_data *ts = i2c_get_clientdata(client); 867 868 if (ts->gpiod_int && ts->gpiod_rst) 869 wait_for_completion(&ts->firmware_loading_complete); 870 871 return 0; 872 } 873 874 static int __maybe_unused goodix_suspend(struct device *dev) 875 { 876 struct i2c_client *client = to_i2c_client(dev); 877 struct goodix_ts_data *ts = i2c_get_clientdata(client); 878 int error; 879 880 /* We need gpio pins to suspend/resume */ 881 if (!ts->gpiod_int || !ts->gpiod_rst) 882 return 0; 883 884 wait_for_completion(&ts->firmware_loading_complete); 885 886 /* Free IRQ as IRQ pin is used as output in the suspend sequence */ 887 goodix_free_irq(ts); 888 889 /* Output LOW on the INT pin for 5 ms */ 890 error = gpiod_direction_output(ts->gpiod_int, 0); 891 if (error) { 892 goodix_request_irq(ts); 893 return error; 894 } 895 896 usleep_range(5000, 6000); 897 898 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND, 899 GOODIX_CMD_SCREEN_OFF); 900 if (error) { 901 dev_err(&ts->client->dev, "Screen off command failed\n"); 902 gpiod_direction_input(ts->gpiod_int); 903 goodix_request_irq(ts); 904 return -EAGAIN; 905 } 906 907 /* 908 * The datasheet specifies that the interval between sending screen-off 909 * command and wake-up should be longer than 58 ms. To avoid waking up 910 * sooner, delay 58ms here. 911 */ 912 msleep(58); 913 return 0; 914 } 915 916 static int __maybe_unused goodix_resume(struct device *dev) 917 { 918 struct i2c_client *client = to_i2c_client(dev); 919 struct goodix_ts_data *ts = i2c_get_clientdata(client); 920 int error; 921 922 if (!ts->gpiod_int || !ts->gpiod_rst) 923 return 0; 924 925 /* 926 * Exit sleep mode by outputting HIGH level to INT pin 927 * for 2ms~5ms. 928 */ 929 error = gpiod_direction_output(ts->gpiod_int, 1); 930 if (error) 931 return error; 932 933 usleep_range(2000, 5000); 934 935 error = goodix_int_sync(ts); 936 if (error) 937 return error; 938 939 error = goodix_request_irq(ts); 940 if (error) 941 return error; 942 943 return 0; 944 } 945 946 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume); 947 948 static const struct i2c_device_id goodix_ts_id[] = { 949 { "GDIX1001:00", 0 }, 950 { } 951 }; 952 MODULE_DEVICE_TABLE(i2c, goodix_ts_id); 953 954 #ifdef CONFIG_ACPI 955 static const struct acpi_device_id goodix_acpi_match[] = { 956 { "GDIX1001", 0 }, 957 { } 958 }; 959 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); 960 #endif 961 962 #ifdef CONFIG_OF 963 static const struct of_device_id goodix_of_match[] = { 964 { .compatible = "goodix,gt1151" }, 965 { .compatible = "goodix,gt911" }, 966 { .compatible = "goodix,gt9110" }, 967 { .compatible = "goodix,gt912" }, 968 { .compatible = "goodix,gt927" }, 969 { .compatible = "goodix,gt9271" }, 970 { .compatible = "goodix,gt928" }, 971 { .compatible = "goodix,gt967" }, 972 { } 973 }; 974 MODULE_DEVICE_TABLE(of, goodix_of_match); 975 #endif 976 977 static struct i2c_driver goodix_ts_driver = { 978 .probe = goodix_ts_probe, 979 .remove = goodix_ts_remove, 980 .id_table = goodix_ts_id, 981 .driver = { 982 .name = "Goodix-TS", 983 .acpi_match_table = ACPI_PTR(goodix_acpi_match), 984 .of_match_table = of_match_ptr(goodix_of_match), 985 .pm = &goodix_pm_ops, 986 }, 987 }; 988 module_i2c_driver(goodix_ts_driver); 989 990 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 991 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); 992 MODULE_DESCRIPTION("Goodix touchscreen driver"); 993 MODULE_LICENSE("GPL v2"); 994