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/input/touchscreen.h> 26 #include <linux/module.h> 27 #include <linux/delay.h> 28 #include <linux/irq.h> 29 #include <linux/interrupt.h> 30 #include <linux/regulator/consumer.h> 31 #include <linux/slab.h> 32 #include <linux/acpi.h> 33 #include <linux/of.h> 34 #include <asm/unaligned.h> 35 36 struct goodix_ts_data; 37 38 struct goodix_chip_data { 39 u16 config_addr; 40 int config_len; 41 int (*check_config)(struct goodix_ts_data *, const struct firmware *); 42 }; 43 44 struct goodix_ts_data { 45 struct i2c_client *client; 46 struct input_dev *input_dev; 47 const struct goodix_chip_data *chip; 48 struct touchscreen_properties prop; 49 unsigned int max_touch_num; 50 unsigned int int_trigger_type; 51 struct regulator *avdd28; 52 struct regulator *vddio; 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 __be16 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 case 5663: 223 case 5688: 224 return >1x_chip_data; 225 226 case 911: 227 case 9271: 228 case 9110: 229 case 927: 230 case 928: 231 return >911_chip_data; 232 233 case 912: 234 case 967: 235 return >967_chip_data; 236 237 default: 238 return >9x_chip_data; 239 } 240 } 241 242 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data) 243 { 244 unsigned long max_timeout; 245 int touch_num; 246 int error; 247 248 /* 249 * The 'buffer status' bit, which indicates that the data is valid, is 250 * not set as soon as the interrupt is raised, but slightly after. 251 * This takes around 10 ms to happen, so we poll for 20 ms. 252 */ 253 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT); 254 do { 255 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, 256 data, GOODIX_CONTACT_SIZE + 1); 257 if (error) { 258 dev_err(&ts->client->dev, "I2C transfer error: %d\n", 259 error); 260 return error; 261 } 262 263 if (data[0] & GOODIX_BUFFER_STATUS_READY) { 264 touch_num = data[0] & 0x0f; 265 if (touch_num > ts->max_touch_num) 266 return -EPROTO; 267 268 if (touch_num > 1) { 269 data += 1 + GOODIX_CONTACT_SIZE; 270 error = goodix_i2c_read(ts->client, 271 GOODIX_READ_COOR_ADDR + 272 1 + GOODIX_CONTACT_SIZE, 273 data, 274 GOODIX_CONTACT_SIZE * 275 (touch_num - 1)); 276 if (error) 277 return error; 278 } 279 280 return touch_num; 281 } 282 283 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */ 284 } while (time_before(jiffies, max_timeout)); 285 286 /* 287 * The Goodix panel will send spurious interrupts after a 288 * 'finger up' event, which will always cause a timeout. 289 */ 290 return 0; 291 } 292 293 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data) 294 { 295 int id = coor_data[0] & 0x0F; 296 int input_x = get_unaligned_le16(&coor_data[1]); 297 int input_y = get_unaligned_le16(&coor_data[3]); 298 int input_w = get_unaligned_le16(&coor_data[5]); 299 300 input_mt_slot(ts->input_dev, id); 301 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); 302 touchscreen_report_pos(ts->input_dev, &ts->prop, 303 input_x, input_y, true); 304 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); 305 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); 306 } 307 308 /** 309 * goodix_process_events - Process incoming events 310 * 311 * @ts: our goodix_ts_data pointer 312 * 313 * Called when the IRQ is triggered. Read the current device state, and push 314 * the input events to the user space. 315 */ 316 static void goodix_process_events(struct goodix_ts_data *ts) 317 { 318 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; 319 int touch_num; 320 int i; 321 322 touch_num = goodix_ts_read_input_report(ts, point_data); 323 if (touch_num < 0) 324 return; 325 326 /* 327 * Bit 4 of the first byte reports the status of the capacitive 328 * Windows/Home button. 329 */ 330 input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4)); 331 332 for (i = 0; i < touch_num; i++) 333 goodix_ts_report_touch(ts, 334 &point_data[1 + GOODIX_CONTACT_SIZE * i]); 335 336 input_mt_sync_frame(ts->input_dev); 337 input_sync(ts->input_dev); 338 } 339 340 /** 341 * goodix_ts_irq_handler - The IRQ handler 342 * 343 * @irq: interrupt number. 344 * @dev_id: private data pointer. 345 */ 346 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) 347 { 348 struct goodix_ts_data *ts = dev_id; 349 350 goodix_process_events(ts); 351 352 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0) 353 dev_err(&ts->client->dev, "I2C write end_cmd error\n"); 354 355 return IRQ_HANDLED; 356 } 357 358 static void goodix_free_irq(struct goodix_ts_data *ts) 359 { 360 devm_free_irq(&ts->client->dev, ts->client->irq, ts); 361 } 362 363 static int goodix_request_irq(struct goodix_ts_data *ts) 364 { 365 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq, 366 NULL, goodix_ts_irq_handler, 367 ts->irq_flags, ts->client->name, ts); 368 } 369 370 static int goodix_check_cfg_8(struct goodix_ts_data *ts, 371 const struct firmware *cfg) 372 { 373 int i, raw_cfg_len = cfg->size - 2; 374 u8 check_sum = 0; 375 376 for (i = 0; i < raw_cfg_len; i++) 377 check_sum += cfg->data[i]; 378 check_sum = (~check_sum) + 1; 379 if (check_sum != cfg->data[raw_cfg_len]) { 380 dev_err(&ts->client->dev, 381 "The checksum of the config fw is not correct"); 382 return -EINVAL; 383 } 384 385 if (cfg->data[raw_cfg_len + 1] != 1) { 386 dev_err(&ts->client->dev, 387 "Config fw must have Config_Fresh register set"); 388 return -EINVAL; 389 } 390 391 return 0; 392 } 393 394 static int goodix_check_cfg_16(struct goodix_ts_data *ts, 395 const struct firmware *cfg) 396 { 397 int i, raw_cfg_len = cfg->size - 3; 398 u16 check_sum = 0; 399 400 for (i = 0; i < raw_cfg_len; i += 2) 401 check_sum += get_unaligned_be16(&cfg->data[i]); 402 check_sum = (~check_sum) + 1; 403 if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) { 404 dev_err(&ts->client->dev, 405 "The checksum of the config fw is not correct"); 406 return -EINVAL; 407 } 408 409 if (cfg->data[raw_cfg_len + 2] != 1) { 410 dev_err(&ts->client->dev, 411 "Config fw must have Config_Fresh register set"); 412 return -EINVAL; 413 } 414 415 return 0; 416 } 417 418 /** 419 * goodix_check_cfg - Checks if config fw is valid 420 * 421 * @ts: goodix_ts_data pointer 422 * @cfg: firmware config data 423 */ 424 static int goodix_check_cfg(struct goodix_ts_data *ts, 425 const struct firmware *cfg) 426 { 427 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) { 428 dev_err(&ts->client->dev, 429 "The length of the config fw is not correct"); 430 return -EINVAL; 431 } 432 433 return ts->chip->check_config(ts, cfg); 434 } 435 436 /** 437 * goodix_send_cfg - Write fw config to device 438 * 439 * @ts: goodix_ts_data pointer 440 * @cfg: config firmware to write to device 441 */ 442 static int goodix_send_cfg(struct goodix_ts_data *ts, 443 const struct firmware *cfg) 444 { 445 int error; 446 447 error = goodix_check_cfg(ts, cfg); 448 if (error) 449 return error; 450 451 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data, 452 cfg->size); 453 if (error) { 454 dev_err(&ts->client->dev, "Failed to write config data: %d", 455 error); 456 return error; 457 } 458 dev_dbg(&ts->client->dev, "Config sent successfully."); 459 460 /* Let the firmware reconfigure itself, so sleep for 10ms */ 461 usleep_range(10000, 11000); 462 463 return 0; 464 } 465 466 static int goodix_int_sync(struct goodix_ts_data *ts) 467 { 468 int error; 469 470 error = gpiod_direction_output(ts->gpiod_int, 0); 471 if (error) 472 return error; 473 474 msleep(50); /* T5: 50ms */ 475 476 error = gpiod_direction_input(ts->gpiod_int); 477 if (error) 478 return error; 479 480 return 0; 481 } 482 483 /** 484 * goodix_reset - Reset device during power on 485 * 486 * @ts: goodix_ts_data pointer 487 */ 488 static int goodix_reset(struct goodix_ts_data *ts) 489 { 490 int error; 491 492 /* begin select I2C slave addr */ 493 error = gpiod_direction_output(ts->gpiod_rst, 0); 494 if (error) 495 return error; 496 497 msleep(20); /* T2: > 10ms */ 498 499 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */ 500 error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14); 501 if (error) 502 return error; 503 504 usleep_range(100, 2000); /* T3: > 100us */ 505 506 error = gpiod_direction_output(ts->gpiod_rst, 1); 507 if (error) 508 return error; 509 510 usleep_range(6000, 10000); /* T4: > 5ms */ 511 512 /* end select I2C slave addr */ 513 error = gpiod_direction_input(ts->gpiod_rst); 514 if (error) 515 return error; 516 517 error = goodix_int_sync(ts); 518 if (error) 519 return error; 520 521 return 0; 522 } 523 524 /** 525 * goodix_get_gpio_config - Get GPIO config from ACPI/DT 526 * 527 * @ts: goodix_ts_data pointer 528 */ 529 static int goodix_get_gpio_config(struct goodix_ts_data *ts) 530 { 531 int error; 532 struct device *dev; 533 struct gpio_desc *gpiod; 534 535 if (!ts->client) 536 return -EINVAL; 537 dev = &ts->client->dev; 538 539 ts->avdd28 = devm_regulator_get(dev, "AVDD28"); 540 if (IS_ERR(ts->avdd28)) { 541 error = PTR_ERR(ts->avdd28); 542 if (error != -EPROBE_DEFER) 543 dev_err(dev, 544 "Failed to get AVDD28 regulator: %d\n", error); 545 return error; 546 } 547 548 ts->vddio = devm_regulator_get(dev, "VDDIO"); 549 if (IS_ERR(ts->vddio)) { 550 error = PTR_ERR(ts->vddio); 551 if (error != -EPROBE_DEFER) 552 dev_err(dev, 553 "Failed to get VDDIO regulator: %d\n", error); 554 return error; 555 } 556 557 /* Get the interrupt GPIO pin number */ 558 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_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_INT_NAME, error); 564 return error; 565 } 566 567 ts->gpiod_int = gpiod; 568 569 /* Get the reset line GPIO pin number */ 570 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN); 571 if (IS_ERR(gpiod)) { 572 error = PTR_ERR(gpiod); 573 if (error != -EPROBE_DEFER) 574 dev_dbg(dev, "Failed to get %s GPIO: %d\n", 575 GOODIX_GPIO_RST_NAME, error); 576 return error; 577 } 578 579 ts->gpiod_rst = gpiod; 580 581 return 0; 582 } 583 584 /** 585 * goodix_read_config - Read the embedded configuration of the panel 586 * 587 * @ts: our goodix_ts_data pointer 588 * 589 * Must be called during probe 590 */ 591 static void goodix_read_config(struct goodix_ts_data *ts) 592 { 593 u8 config[GOODIX_CONFIG_MAX_LENGTH]; 594 int x_max, y_max; 595 int error; 596 597 error = goodix_i2c_read(ts->client, ts->chip->config_addr, 598 config, ts->chip->config_len); 599 if (error) { 600 dev_warn(&ts->client->dev, "Error reading config: %d\n", 601 error); 602 ts->int_trigger_type = GOODIX_INT_TRIGGER; 603 ts->max_touch_num = GOODIX_MAX_CONTACTS; 604 return; 605 } 606 607 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03; 608 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f; 609 610 x_max = get_unaligned_le16(&config[RESOLUTION_LOC]); 611 y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]); 612 if (x_max && y_max) { 613 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1); 614 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1); 615 } 616 } 617 618 /** 619 * goodix_read_version - Read goodix touchscreen version 620 * 621 * @ts: our goodix_ts_data pointer 622 */ 623 static int goodix_read_version(struct goodix_ts_data *ts) 624 { 625 int error; 626 u8 buf[6]; 627 char id_str[5]; 628 629 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf)); 630 if (error) { 631 dev_err(&ts->client->dev, "read version failed: %d\n", error); 632 return error; 633 } 634 635 memcpy(id_str, buf, 4); 636 id_str[4] = 0; 637 if (kstrtou16(id_str, 10, &ts->id)) 638 ts->id = 0x1001; 639 640 ts->version = get_unaligned_le16(&buf[4]); 641 642 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id, 643 ts->version); 644 645 return 0; 646 } 647 648 /** 649 * goodix_i2c_test - I2C test function to check if the device answers. 650 * 651 * @client: the i2c client 652 */ 653 static int goodix_i2c_test(struct i2c_client *client) 654 { 655 int retry = 0; 656 int error; 657 u8 test; 658 659 while (retry++ < 2) { 660 error = goodix_i2c_read(client, GOODIX_REG_ID, 661 &test, 1); 662 if (!error) 663 return 0; 664 665 dev_err(&client->dev, "i2c test failed attempt %d: %d\n", 666 retry, error); 667 msleep(20); 668 } 669 670 return error; 671 } 672 673 /** 674 * goodix_configure_dev - Finish device initialization 675 * 676 * @ts: our goodix_ts_data pointer 677 * 678 * Must be called from probe to finish initialization of the device. 679 * Contains the common initialization code for both devices that 680 * declare gpio pins and devices that do not. It is either called 681 * directly from probe or from request_firmware_wait callback. 682 */ 683 static int goodix_configure_dev(struct goodix_ts_data *ts) 684 { 685 int error; 686 687 ts->int_trigger_type = GOODIX_INT_TRIGGER; 688 ts->max_touch_num = GOODIX_MAX_CONTACTS; 689 690 ts->input_dev = devm_input_allocate_device(&ts->client->dev); 691 if (!ts->input_dev) { 692 dev_err(&ts->client->dev, "Failed to allocate input device."); 693 return -ENOMEM; 694 } 695 696 ts->input_dev->name = "Goodix Capacitive TouchScreen"; 697 ts->input_dev->phys = "input/ts"; 698 ts->input_dev->id.bustype = BUS_I2C; 699 ts->input_dev->id.vendor = 0x0416; 700 ts->input_dev->id.product = ts->id; 701 ts->input_dev->id.version = ts->version; 702 703 /* Capacitive Windows/Home button on some devices */ 704 input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA); 705 706 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X); 707 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y); 708 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); 709 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 710 711 /* Read configuration and apply touchscreen parameters */ 712 goodix_read_config(ts); 713 714 /* Try overriding touchscreen parameters via device properties */ 715 touchscreen_parse_properties(ts->input_dev, true, &ts->prop); 716 717 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) { 718 dev_err(&ts->client->dev, 719 "Invalid config (%d, %d, %d), using defaults\n", 720 ts->prop.max_x, ts->prop.max_y, ts->max_touch_num); 721 ts->prop.max_x = GOODIX_MAX_WIDTH - 1; 722 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1; 723 ts->max_touch_num = GOODIX_MAX_CONTACTS; 724 input_abs_set_max(ts->input_dev, 725 ABS_MT_POSITION_X, ts->prop.max_x); 726 input_abs_set_max(ts->input_dev, 727 ABS_MT_POSITION_Y, ts->prop.max_y); 728 } 729 730 if (dmi_check_system(rotated_screen)) { 731 ts->prop.invert_x = true; 732 ts->prop.invert_y = true; 733 dev_dbg(&ts->client->dev, 734 "Applying '180 degrees rotated screen' quirk\n"); 735 } 736 737 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num, 738 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 739 if (error) { 740 dev_err(&ts->client->dev, 741 "Failed to initialize MT slots: %d", error); 742 return error; 743 } 744 745 error = input_register_device(ts->input_dev); 746 if (error) { 747 dev_err(&ts->client->dev, 748 "Failed to register input device: %d", error); 749 return error; 750 } 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 void goodix_disable_regulators(void *arg) 790 { 791 struct goodix_ts_data *ts = arg; 792 793 regulator_disable(ts->vddio); 794 regulator_disable(ts->avdd28); 795 } 796 797 static int goodix_ts_probe(struct i2c_client *client, 798 const struct i2c_device_id *id) 799 { 800 struct goodix_ts_data *ts; 801 int error; 802 803 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr); 804 805 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 806 dev_err(&client->dev, "I2C check functionality failed.\n"); 807 return -ENXIO; 808 } 809 810 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 811 if (!ts) 812 return -ENOMEM; 813 814 ts->client = client; 815 i2c_set_clientdata(client, ts); 816 init_completion(&ts->firmware_loading_complete); 817 818 error = goodix_get_gpio_config(ts); 819 if (error) 820 return error; 821 822 /* power up the controller */ 823 error = regulator_enable(ts->avdd28); 824 if (error) { 825 dev_err(&client->dev, 826 "Failed to enable AVDD28 regulator: %d\n", 827 error); 828 return error; 829 } 830 831 error = regulator_enable(ts->vddio); 832 if (error) { 833 dev_err(&client->dev, 834 "Failed to enable VDDIO regulator: %d\n", 835 error); 836 regulator_disable(ts->avdd28); 837 return error; 838 } 839 840 error = devm_add_action_or_reset(&client->dev, 841 goodix_disable_regulators, ts); 842 if (error) 843 return error; 844 845 if (ts->gpiod_int && ts->gpiod_rst) { 846 /* reset the controller */ 847 error = goodix_reset(ts); 848 if (error) { 849 dev_err(&client->dev, "Controller reset failed.\n"); 850 return error; 851 } 852 } 853 854 error = goodix_i2c_test(client); 855 if (error) { 856 dev_err(&client->dev, "I2C communication failure: %d\n", error); 857 return error; 858 } 859 860 error = goodix_read_version(ts); 861 if (error) { 862 dev_err(&client->dev, "Read version failed.\n"); 863 return error; 864 } 865 866 ts->chip = goodix_get_chip_data(ts->id); 867 868 if (ts->gpiod_int && ts->gpiod_rst) { 869 /* update device config */ 870 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL, 871 "goodix_%d_cfg.bin", ts->id); 872 if (!ts->cfg_name) 873 return -ENOMEM; 874 875 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name, 876 &client->dev, GFP_KERNEL, ts, 877 goodix_config_cb); 878 if (error) { 879 dev_err(&client->dev, 880 "Failed to invoke firmware loader: %d\n", 881 error); 882 return error; 883 } 884 885 return 0; 886 } else { 887 error = goodix_configure_dev(ts); 888 if (error) 889 return error; 890 } 891 892 return 0; 893 } 894 895 static int goodix_ts_remove(struct i2c_client *client) 896 { 897 struct goodix_ts_data *ts = i2c_get_clientdata(client); 898 899 if (ts->gpiod_int && ts->gpiod_rst) 900 wait_for_completion(&ts->firmware_loading_complete); 901 902 return 0; 903 } 904 905 static int __maybe_unused goodix_suspend(struct device *dev) 906 { 907 struct i2c_client *client = to_i2c_client(dev); 908 struct goodix_ts_data *ts = i2c_get_clientdata(client); 909 int error; 910 911 /* We need gpio pins to suspend/resume */ 912 if (!ts->gpiod_int || !ts->gpiod_rst) { 913 disable_irq(client->irq); 914 return 0; 915 } 916 917 wait_for_completion(&ts->firmware_loading_complete); 918 919 /* Free IRQ as IRQ pin is used as output in the suspend sequence */ 920 goodix_free_irq(ts); 921 922 /* Output LOW on the INT pin for 5 ms */ 923 error = gpiod_direction_output(ts->gpiod_int, 0); 924 if (error) { 925 goodix_request_irq(ts); 926 return error; 927 } 928 929 usleep_range(5000, 6000); 930 931 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND, 932 GOODIX_CMD_SCREEN_OFF); 933 if (error) { 934 dev_err(&ts->client->dev, "Screen off command failed\n"); 935 gpiod_direction_input(ts->gpiod_int); 936 goodix_request_irq(ts); 937 return -EAGAIN; 938 } 939 940 /* 941 * The datasheet specifies that the interval between sending screen-off 942 * command and wake-up should be longer than 58 ms. To avoid waking up 943 * sooner, delay 58ms here. 944 */ 945 msleep(58); 946 return 0; 947 } 948 949 static int __maybe_unused goodix_resume(struct device *dev) 950 { 951 struct i2c_client *client = to_i2c_client(dev); 952 struct goodix_ts_data *ts = i2c_get_clientdata(client); 953 int error; 954 955 if (!ts->gpiod_int || !ts->gpiod_rst) { 956 enable_irq(client->irq); 957 return 0; 958 } 959 960 /* 961 * Exit sleep mode by outputting HIGH level to INT pin 962 * for 2ms~5ms. 963 */ 964 error = gpiod_direction_output(ts->gpiod_int, 1); 965 if (error) 966 return error; 967 968 usleep_range(2000, 5000); 969 970 error = goodix_int_sync(ts); 971 if (error) 972 return error; 973 974 error = goodix_request_irq(ts); 975 if (error) 976 return error; 977 978 return 0; 979 } 980 981 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume); 982 983 static const struct i2c_device_id goodix_ts_id[] = { 984 { "GDIX1001:00", 0 }, 985 { } 986 }; 987 MODULE_DEVICE_TABLE(i2c, goodix_ts_id); 988 989 #ifdef CONFIG_ACPI 990 static const struct acpi_device_id goodix_acpi_match[] = { 991 { "GDIX1001", 0 }, 992 { "GDIX1002", 0 }, 993 { } 994 }; 995 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); 996 #endif 997 998 #ifdef CONFIG_OF 999 static const struct of_device_id goodix_of_match[] = { 1000 { .compatible = "goodix,gt1151" }, 1001 { .compatible = "goodix,gt5663" }, 1002 { .compatible = "goodix,gt5688" }, 1003 { .compatible = "goodix,gt911" }, 1004 { .compatible = "goodix,gt9110" }, 1005 { .compatible = "goodix,gt912" }, 1006 { .compatible = "goodix,gt927" }, 1007 { .compatible = "goodix,gt9271" }, 1008 { .compatible = "goodix,gt928" }, 1009 { .compatible = "goodix,gt967" }, 1010 { } 1011 }; 1012 MODULE_DEVICE_TABLE(of, goodix_of_match); 1013 #endif 1014 1015 static struct i2c_driver goodix_ts_driver = { 1016 .probe = goodix_ts_probe, 1017 .remove = goodix_ts_remove, 1018 .id_table = goodix_ts_id, 1019 .driver = { 1020 .name = "Goodix-TS", 1021 .acpi_match_table = ACPI_PTR(goodix_acpi_match), 1022 .of_match_table = of_match_ptr(goodix_of_match), 1023 .pm = &goodix_pm_ops, 1024 }, 1025 }; 1026 module_i2c_driver(goodix_ts_driver); 1027 1028 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 1029 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); 1030 MODULE_DESCRIPTION("Goodix touchscreen driver"); 1031 MODULE_LICENSE("GPL v2"); 1032