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