1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Goodix GT7986U SPI Driver Code for HID. 4 * 5 * Copyright (C) 2024 Godix, Inc. 6 */ 7 #include <linux/unaligned.h> 8 #include <linux/delay.h> 9 #include <linux/hid.h> 10 #include <linux/interrupt.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/of.h> 15 #include <linux/sizes.h> 16 #include <linux/spi/spi.h> 17 18 #define GOODIX_DEV_CONFIRM_ADDR 0x10000 19 #define GOODIX_HID_DESC_ADDR 0x1058C 20 #define GOODIX_HID_REPORT_DESC_ADDR 0x105AA 21 #define GOODIX_HID_SIGN_ADDR 0x10D32 22 23 #define GOODIX_HID_GET_REPORT_CMD 0x02 24 #define GOODIX_HID_SET_REPORT_CMD 0x03 25 26 #define GOODIX_HID_MAX_INBUF_SIZE 128 27 #define GOODIX_HID_ACK_READY_FLAG 0x01 28 #define GOODIX_HID_REPORT_READY_FLAG 0x80 29 30 #define GOODIX_DEV_CONFIRM_VAL 0xAA 31 32 #define GOODIX_SPI_WRITE_FLAG 0xF0 33 #define GOODIX_SPI_READ_FLAG 0xF1 34 #define GOODIX_SPI_TRANS_PREFIX_LEN 1 35 #define GOODIX_REGISTER_WIDTH 4 36 #define GOODIX_SPI_READ_DUMMY_LEN 3 37 #define GOODIX_SPI_READ_PREFIX_LEN (GOODIX_SPI_TRANS_PREFIX_LEN + \ 38 GOODIX_REGISTER_WIDTH + \ 39 GOODIX_SPI_READ_DUMMY_LEN) 40 #define GOODIX_SPI_WRITE_PREFIX_LEN (GOODIX_SPI_TRANS_PREFIX_LEN + \ 41 GOODIX_REGISTER_WIDTH) 42 43 #define GOODIX_CHECKSUM_SIZE sizeof(u16) 44 #define GOODIX_NORMAL_RESET_DELAY_MS 150 45 46 struct goodix_hid_report_header { 47 u8 flag; 48 __le16 size; 49 } __packed; 50 #define GOODIX_HID_ACK_HEADER_SIZE sizeof(struct goodix_hid_report_header) 51 52 struct goodix_hid_report_package { 53 __le16 size; 54 u8 data[]; 55 }; 56 57 #define GOODIX_HID_PKG_LEN_SIZE sizeof(u16) 58 #define GOODIX_HID_COOR_DATA_LEN 82 59 #define GOODIX_HID_COOR_PKG_LEN (GOODIX_HID_PKG_LEN_SIZE + \ 60 GOODIX_HID_COOR_DATA_LEN) 61 62 /* power state */ 63 #define GOODIX_SPI_POWER_ON 0x00 64 #define GOODIX_SPI_POWER_SLEEP 0x01 65 66 /* flags used to record the current device operating state */ 67 #define GOODIX_HID_STARTED 0 68 69 struct goodix_hid_report_event { 70 struct goodix_hid_report_header hdr; 71 u8 data[GOODIX_HID_COOR_PKG_LEN]; 72 } __packed; 73 74 struct goodix_hid_desc { 75 __le16 desc_length; 76 __le16 bcd_version; 77 __le16 report_desc_length; 78 __le16 report_desc_register; 79 __le16 input_register; 80 __le16 max_input_length; 81 __le16 output_register; 82 __le16 max_output_length; 83 __le16 cmd_register; 84 __le16 data_register; 85 __le16 vendor_id; 86 __le16 product_id; 87 __le16 version_id; 88 __le32 reserved; 89 } __packed; 90 91 struct goodix_ts_data { 92 struct device *dev; 93 struct spi_device *spi; 94 struct hid_device *hid; 95 struct goodix_hid_desc hid_desc; 96 97 struct gpio_desc *reset_gpio; 98 u32 hid_report_addr; 99 100 unsigned long flags; 101 /* lock for hid raw request operation */ 102 struct mutex hid_request_lock; 103 /* buffer used to store hid report event */ 104 u8 *event_buf; 105 u32 hid_max_event_sz; 106 /* buffer used to do spi data transfer */ 107 u8 xfer_buf[SZ_2K] ____cacheline_aligned; 108 }; 109 110 static void *goodix_get_event_report(struct goodix_ts_data *ts, u32 addr, 111 u8 *data, size_t len) 112 { 113 struct spi_device *spi = to_spi_device(&ts->spi->dev); 114 struct spi_transfer xfers; 115 struct spi_message spi_msg; 116 int error; 117 118 /* buffer format: 0xF1 + addr(4bytes) + dummy(3bytes) + data */ 119 data[0] = GOODIX_SPI_READ_FLAG; 120 put_unaligned_be32(addr, data + GOODIX_SPI_TRANS_PREFIX_LEN); 121 122 spi_message_init(&spi_msg); 123 memset(&xfers, 0, sizeof(xfers)); 124 xfers.tx_buf = data; 125 xfers.rx_buf = data; 126 xfers.len = GOODIX_SPI_READ_PREFIX_LEN + len; 127 spi_message_add_tail(&xfers, &spi_msg); 128 129 error = spi_sync(spi, &spi_msg); 130 if (error) { 131 dev_err(ts->dev, "spi transfer error: %d", error); 132 return NULL; 133 } 134 135 return data + GOODIX_SPI_READ_PREFIX_LEN; 136 } 137 138 static int goodix_spi_read(struct goodix_ts_data *ts, u32 addr, 139 void *data, size_t len) 140 { 141 struct spi_device *spi = to_spi_device(&ts->spi->dev); 142 struct spi_transfer xfers; 143 struct spi_message spi_msg; 144 int error; 145 146 if (GOODIX_SPI_READ_PREFIX_LEN + len > sizeof(ts->xfer_buf)) { 147 dev_err(ts->dev, "read data len exceed limit %zu", 148 sizeof(ts->xfer_buf) - GOODIX_SPI_READ_PREFIX_LEN); 149 return -EINVAL; 150 } 151 152 /* buffer format: 0xF1 + addr(4bytes) + dummy(3bytes) + data */ 153 ts->xfer_buf[0] = GOODIX_SPI_READ_FLAG; 154 put_unaligned_be32(addr, ts->xfer_buf + GOODIX_SPI_TRANS_PREFIX_LEN); 155 156 spi_message_init(&spi_msg); 157 memset(&xfers, 0, sizeof(xfers)); 158 xfers.tx_buf = ts->xfer_buf; 159 xfers.rx_buf = ts->xfer_buf; 160 xfers.len = GOODIX_SPI_READ_PREFIX_LEN + len; 161 spi_message_add_tail(&xfers, &spi_msg); 162 163 error = spi_sync(spi, &spi_msg); 164 if (error) 165 dev_err(ts->dev, "spi transfer error: %d", error); 166 else 167 memcpy(data, ts->xfer_buf + GOODIX_SPI_READ_PREFIX_LEN, len); 168 169 return error; 170 } 171 172 static int goodix_spi_write(struct goodix_ts_data *ts, u32 addr, 173 const void *data, size_t len) 174 { 175 struct spi_device *spi = to_spi_device(&ts->spi->dev); 176 struct spi_transfer xfers; 177 struct spi_message spi_msg; 178 int error; 179 180 if (GOODIX_SPI_WRITE_PREFIX_LEN + len > sizeof(ts->xfer_buf)) { 181 dev_err(ts->dev, "write data len exceed limit %zu", 182 sizeof(ts->xfer_buf) - GOODIX_SPI_WRITE_PREFIX_LEN); 183 return -EINVAL; 184 } 185 186 /* buffer format: 0xF0 + addr(4bytes) + data */ 187 ts->xfer_buf[0] = GOODIX_SPI_WRITE_FLAG; 188 put_unaligned_be32(addr, ts->xfer_buf + GOODIX_SPI_TRANS_PREFIX_LEN); 189 memcpy(ts->xfer_buf + GOODIX_SPI_WRITE_PREFIX_LEN, data, len); 190 191 spi_message_init(&spi_msg); 192 memset(&xfers, 0, sizeof(xfers)); 193 xfers.tx_buf = ts->xfer_buf; 194 xfers.len = GOODIX_SPI_WRITE_PREFIX_LEN + len; 195 spi_message_add_tail(&xfers, &spi_msg); 196 197 error = spi_sync(spi, &spi_msg); 198 if (error) 199 dev_err(ts->dev, "spi transfer error: %d", error); 200 201 return error; 202 } 203 204 static int goodix_dev_confirm(struct goodix_ts_data *ts) 205 { 206 u8 tx_buf[8], rx_buf[8]; 207 int retry = 3; 208 int error; 209 210 gpiod_set_value_cansleep(ts->reset_gpio, 0); 211 usleep_range(4000, 4100); 212 213 memset(tx_buf, GOODIX_DEV_CONFIRM_VAL, sizeof(tx_buf)); 214 while (retry--) { 215 error = goodix_spi_write(ts, GOODIX_DEV_CONFIRM_ADDR, 216 tx_buf, sizeof(tx_buf)); 217 if (error) 218 return error; 219 220 error = goodix_spi_read(ts, GOODIX_DEV_CONFIRM_ADDR, 221 rx_buf, sizeof(rx_buf)); 222 if (error) 223 return error; 224 225 if (!memcmp(tx_buf, rx_buf, sizeof(tx_buf))) 226 return 0; 227 228 usleep_range(5000, 5100); 229 } 230 231 dev_err(ts->dev, "device confirm failed, rx_buf: %*ph", 8, rx_buf); 232 return -EINVAL; 233 } 234 235 /** 236 * goodix_hid_parse() - hid-core .parse() callback 237 * @hid: hid device instance 238 * 239 * This function gets called during call to hid_add_device 240 * 241 * Return: 0 on success and non zero on error 242 */ 243 static int goodix_hid_parse(struct hid_device *hid) 244 { 245 struct goodix_ts_data *ts = hid->driver_data; 246 u16 rsize; 247 int error; 248 249 rsize = le16_to_cpu(ts->hid_desc.report_desc_length); 250 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { 251 dev_err(ts->dev, "invalid report desc size, %d", rsize); 252 return -EINVAL; 253 } 254 255 u8 *rdesc __free(kfree) = kzalloc(rsize, GFP_KERNEL); 256 if (!rdesc) 257 return -ENOMEM; 258 259 error = goodix_spi_read(ts, GOODIX_HID_REPORT_DESC_ADDR, rdesc, rsize); 260 if (error) { 261 dev_err(ts->dev, "failed get report desc, %d", error); 262 return error; 263 } 264 265 error = hid_parse_report(hid, rdesc, rsize); 266 if (error) { 267 dev_err(ts->dev, "failed parse report, %d", error); 268 return error; 269 } 270 271 return 0; 272 } 273 274 static int goodix_hid_get_report_length(struct hid_report *report) 275 { 276 return ((report->size - 1) >> 3) + 1 + 277 report->device->report_enum[report->type].numbered + 2; 278 } 279 280 static void goodix_hid_find_max_report(struct hid_device *hid, unsigned int type, 281 unsigned int *max) 282 { 283 struct hid_report *report; 284 unsigned int size; 285 286 list_for_each_entry(report, &hid->report_enum[type].report_list, list) { 287 size = goodix_hid_get_report_length(report); 288 if (*max < size) 289 *max = size; 290 } 291 } 292 293 static int goodix_hid_start(struct hid_device *hid) 294 { 295 struct goodix_ts_data *ts = hid->driver_data; 296 unsigned int bufsize = GOODIX_HID_COOR_PKG_LEN; 297 u32 report_size; 298 299 goodix_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); 300 goodix_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); 301 goodix_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); 302 303 report_size = GOODIX_SPI_READ_PREFIX_LEN + 304 GOODIX_HID_ACK_HEADER_SIZE + bufsize; 305 if (report_size <= ts->hid_max_event_sz) 306 return 0; 307 308 ts->event_buf = devm_krealloc(ts->dev, ts->event_buf, 309 report_size, GFP_KERNEL); 310 if (!ts->event_buf) 311 return -ENOMEM; 312 313 ts->hid_max_event_sz = report_size; 314 return 0; 315 } 316 317 static void goodix_hid_stop(struct hid_device *hid) 318 { 319 hid->claimed = 0; 320 } 321 322 static int goodix_hid_open(struct hid_device *hid) 323 { 324 struct goodix_ts_data *ts = hid->driver_data; 325 326 set_bit(GOODIX_HID_STARTED, &ts->flags); 327 return 0; 328 } 329 330 static void goodix_hid_close(struct hid_device *hid) 331 { 332 struct goodix_ts_data *ts = hid->driver_data; 333 334 clear_bit(GOODIX_HID_STARTED, &ts->flags); 335 } 336 337 /* Return date length of response data */ 338 static int goodix_hid_check_ack_status(struct goodix_ts_data *ts, u32 *resp_len) 339 { 340 struct goodix_hid_report_header hdr; 341 int retry = 20; 342 int error; 343 int len; 344 345 while (retry--) { 346 /* 347 * 3 bytes of hid request response data 348 * - byte 0: Ack flag, value of 1 for data ready 349 * - bytes 1-2: Response data length 350 */ 351 error = goodix_spi_read(ts, ts->hid_report_addr, 352 &hdr, sizeof(hdr)); 353 if (!error && (hdr.flag & GOODIX_HID_ACK_READY_FLAG)) { 354 len = le16_to_cpu(hdr.size); 355 if (len < GOODIX_HID_PKG_LEN_SIZE) { 356 dev_err(ts->dev, "hrd.size too short: %d", len); 357 return -EINVAL; 358 } 359 *resp_len = len; 360 return 0; 361 } 362 363 /* Wait 10ms for another try */ 364 usleep_range(10000, 11000); 365 } 366 367 return -EINVAL; 368 } 369 370 /** 371 * goodix_hid_get_raw_report() - Process hidraw GET REPORT operation 372 * @hid: hid device instance 373 * @reportnum: Report ID 374 * @buf: Buffer for store the report date 375 * @len: Length fo report data 376 * @report_type: Report type 377 * 378 * The function for hid_ll_driver.get_raw_report to handle the HIDRAW ioctl 379 * get report request. The transmitted data follows the standard i2c-hid 380 * protocol with a specified header. 381 * 382 * Return: The length of the data in the buf on success, negative error code 383 */ 384 static int goodix_hid_get_raw_report(struct hid_device *hid, 385 unsigned char reportnum, 386 u8 *buf, size_t len, 387 unsigned char report_type) 388 { 389 struct goodix_ts_data *ts = hid->driver_data; 390 u16 data_register = le16_to_cpu(ts->hid_desc.data_register); 391 u16 cmd_register = le16_to_cpu(ts->hid_desc.cmd_register); 392 u8 tmp_buf[GOODIX_HID_MAX_INBUF_SIZE]; 393 int tx_len = 0, args_len = 0; 394 u32 response_data_len; 395 u8 args[3]; 396 int error; 397 398 if (report_type == HID_OUTPUT_REPORT) 399 return -EINVAL; 400 401 if (reportnum == 3) { 402 /* Get win8 signature data */ 403 error = goodix_spi_read(ts, GOODIX_HID_SIGN_ADDR, buf, len); 404 if (error) { 405 dev_err(ts->dev, "failed get win8 sign: %d", error); 406 return -EINVAL; 407 } 408 return len; 409 } 410 411 if (reportnum >= 0x0F) 412 args[args_len++] = reportnum; 413 414 put_unaligned_le16(data_register, args + args_len); 415 args_len += sizeof(data_register); 416 417 /* Clean 3 bytes of hid ack header data */ 418 memset(tmp_buf, 0, GOODIX_HID_ACK_HEADER_SIZE); 419 tx_len += GOODIX_HID_ACK_HEADER_SIZE; 420 421 put_unaligned_le16(cmd_register, tmp_buf + tx_len); 422 tx_len += sizeof(cmd_register); 423 424 tmp_buf[tx_len] = (report_type == HID_FEATURE_REPORT ? 0x03 : 0x01) << 4; 425 tmp_buf[tx_len] |= reportnum >= 0x0F ? 0x0F : reportnum; 426 tx_len++; 427 428 tmp_buf[tx_len++] = GOODIX_HID_GET_REPORT_CMD; 429 430 memcpy(tmp_buf + tx_len, args, args_len); 431 tx_len += args_len; 432 433 /* Step1: write report request info */ 434 error = goodix_spi_write(ts, ts->hid_report_addr, tmp_buf, tx_len); 435 if (error) { 436 dev_err(ts->dev, "failed send read feature cmd, %d", error); 437 return error; 438 } 439 440 /* No need read response data */ 441 if (!len) 442 return 0; 443 444 /* Step2: check response data status */ 445 error = goodix_hid_check_ack_status(ts, &response_data_len); 446 if (error) 447 return error; 448 449 len = min(len, response_data_len - GOODIX_HID_PKG_LEN_SIZE); 450 /* Step3: read response data(skip 2bytes of hid pkg length) */ 451 error = goodix_spi_read(ts, ts->hid_report_addr + 452 GOODIX_HID_ACK_HEADER_SIZE + 453 GOODIX_HID_PKG_LEN_SIZE, buf, len); 454 if (error) { 455 dev_err(ts->dev, "failed read hid response data, %d", error); 456 return error; 457 } 458 459 if (buf[0] != reportnum) { 460 dev_err(ts->dev, "incorrect report (%d vs %d expected)", 461 buf[0], reportnum); 462 return -EINVAL; 463 } 464 return len; 465 } 466 467 /** 468 * goodix_hid_set_raw_report() - process hidraw SET REPORT operation 469 * @hid: HID device 470 * @reportnum: Report ID 471 * @buf: Buffer for communication 472 * @len: Length of data in the buffer 473 * @report_type: Report type 474 * 475 * The function for hid_ll_driver.get_raw_report to handle the HIDRAW ioctl 476 * set report request. The transmitted data follows the standard i2c-hid 477 * protocol with a specified header. 478 * 479 * Return: The length of the data sent, negative error code on failure 480 */ 481 static int goodix_hid_set_raw_report(struct hid_device *hid, 482 unsigned char reportnum, 483 __u8 *buf, size_t len, 484 unsigned char report_type) 485 { 486 struct goodix_ts_data *ts = hid->driver_data; 487 u16 data_register = le16_to_cpu(ts->hid_desc.data_register); 488 u16 cmd_register = le16_to_cpu(ts->hid_desc.cmd_register); 489 int tx_len = 0, args_len = 0; 490 u8 tmp_buf[GOODIX_HID_MAX_INBUF_SIZE]; 491 u8 args[5]; 492 int error; 493 494 if (reportnum >= 0x0F) { 495 args[args_len++] = reportnum; 496 reportnum = 0x0F; 497 } 498 499 put_unaligned_le16(data_register, args + args_len); 500 args_len += sizeof(data_register); 501 502 put_unaligned_le16(GOODIX_HID_PKG_LEN_SIZE + len, args + args_len); 503 args_len += GOODIX_HID_PKG_LEN_SIZE; 504 505 /* Clean 3 bytes of hid ack header data */ 506 memset(tmp_buf, 0, GOODIX_HID_ACK_HEADER_SIZE); 507 tx_len += GOODIX_HID_ACK_HEADER_SIZE; 508 509 put_unaligned_le16(cmd_register, tmp_buf + tx_len); 510 tx_len += sizeof(cmd_register); 511 512 tmp_buf[tx_len++] = ((report_type == HID_FEATURE_REPORT ? 0x03 : 0x02) << 4) | reportnum; 513 tmp_buf[tx_len++] = GOODIX_HID_SET_REPORT_CMD; 514 515 memcpy(tmp_buf + tx_len, args, args_len); 516 tx_len += args_len; 517 518 memcpy(tmp_buf + tx_len, buf, len); 519 tx_len += len; 520 521 error = goodix_spi_write(ts, ts->hid_report_addr, tmp_buf, tx_len); 522 if (error) { 523 dev_err(ts->dev, "failed send report: %*ph", tx_len, tmp_buf); 524 return error; 525 } 526 return len; 527 } 528 529 static int goodix_hid_raw_request(struct hid_device *hid, 530 unsigned char reportnum, 531 __u8 *buf, size_t len, 532 unsigned char rtype, int reqtype) 533 { 534 struct goodix_ts_data *ts = hid->driver_data; 535 int error = -EINVAL; 536 537 guard(mutex)(&ts->hid_request_lock); 538 switch (reqtype) { 539 case HID_REQ_GET_REPORT: 540 error = goodix_hid_get_raw_report(hid, reportnum, buf, 541 len, rtype); 542 break; 543 case HID_REQ_SET_REPORT: 544 if (buf[0] == reportnum) 545 error = goodix_hid_set_raw_report(hid, reportnum, 546 buf, len, rtype); 547 break; 548 default: 549 break; 550 } 551 552 return error; 553 } 554 555 static struct hid_ll_driver goodix_hid_ll_driver = { 556 .parse = goodix_hid_parse, 557 .start = goodix_hid_start, 558 .stop = goodix_hid_stop, 559 .open = goodix_hid_open, 560 .close = goodix_hid_close, 561 .raw_request = goodix_hid_raw_request 562 }; 563 564 static irqreturn_t goodix_hid_irq(int irq, void *data) 565 { 566 struct goodix_ts_data *ts = data; 567 struct goodix_hid_report_event *event; 568 struct goodix_hid_report_package *pkg; 569 u16 report_size; 570 571 if (!test_bit(GOODIX_HID_STARTED, &ts->flags)) 572 return IRQ_HANDLED; 573 /* 574 * First, read buffer with space for header and coordinate package: 575 * - event header = 3 bytes 576 * - coordinate event = GOODIX_HID_COOR_PKG_LEN bytes 577 * 578 * If the data size info in the event header exceeds 579 * GOODIX_HID_COOR_PKG_LEN, it means that there are other packages 580 * besides the coordinate package. 581 */ 582 event = goodix_get_event_report(ts, ts->hid_report_addr, ts->event_buf, 583 GOODIX_HID_ACK_HEADER_SIZE + 584 GOODIX_HID_COOR_PKG_LEN); 585 if (!event) { 586 dev_err(ts->dev, "failed get coordinate data"); 587 return IRQ_HANDLED; 588 } 589 590 /* Check coordinate data valid falg */ 591 if (event->hdr.flag != GOODIX_HID_REPORT_READY_FLAG) 592 return IRQ_HANDLED; 593 594 pkg = (struct goodix_hid_report_package *)event->data; 595 if (le16_to_cpu(pkg->size) < GOODIX_HID_PKG_LEN_SIZE) { 596 dev_err(ts->dev, "invalid coordinate event package size, %d", 597 le16_to_cpu(pkg->size)); 598 return IRQ_HANDLED; 599 } 600 hid_input_report(ts->hid, HID_INPUT_REPORT, pkg->data, 601 le16_to_cpu(pkg->size) - GOODIX_HID_PKG_LEN_SIZE, 1); 602 603 report_size = le16_to_cpu(event->hdr.size); 604 /* Check if there are other packages */ 605 if (report_size <= GOODIX_HID_COOR_PKG_LEN) 606 return IRQ_HANDLED; 607 608 if (report_size >= ts->hid_max_event_sz) { 609 dev_err(ts->dev, "package size exceed limit %d vs %d", 610 report_size, ts->hid_max_event_sz); 611 return IRQ_HANDLED; 612 } 613 614 /* Read the package behind the coordinate data */ 615 pkg = goodix_get_event_report(ts, ts->hid_report_addr + sizeof(*event), 616 ts->event_buf, 617 report_size - GOODIX_HID_COOR_PKG_LEN); 618 if (!pkg) { 619 dev_err(ts->dev, "failed read attachment data content"); 620 return IRQ_HANDLED; 621 } 622 623 hid_input_report(ts->hid, HID_INPUT_REPORT, pkg->data, 624 le16_to_cpu(pkg->size) - GOODIX_HID_PKG_LEN_SIZE, 1); 625 626 return IRQ_HANDLED; 627 } 628 629 static int goodix_hid_init(struct goodix_ts_data *ts) 630 { 631 struct hid_device *hid; 632 int error; 633 634 /* Get hid descriptor */ 635 error = goodix_spi_read(ts, GOODIX_HID_DESC_ADDR, &ts->hid_desc, 636 sizeof(ts->hid_desc)); 637 if (error) { 638 dev_err(ts->dev, "failed get hid desc, %d", error); 639 return error; 640 } 641 642 hid = hid_allocate_device(); 643 if (IS_ERR(hid)) 644 return PTR_ERR(hid); 645 646 hid->driver_data = ts; 647 hid->ll_driver = &goodix_hid_ll_driver; 648 hid->bus = BUS_SPI; 649 hid->dev.parent = &ts->spi->dev; 650 651 hid->version = le16_to_cpu(ts->hid_desc.bcd_version); 652 hid->vendor = le16_to_cpu(ts->hid_desc.vendor_id); 653 hid->product = le16_to_cpu(ts->hid_desc.product_id); 654 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-gdix", 655 hid->vendor, hid->product); 656 657 error = hid_add_device(hid); 658 if (error) { 659 dev_err(ts->dev, "failed add hid device, %d", error); 660 hid_destroy_device(hid); 661 return error; 662 } 663 664 ts->hid = hid; 665 return 0; 666 } 667 668 static int goodix_spi_probe(struct spi_device *spi) 669 { 670 struct device *dev = &spi->dev; 671 struct goodix_ts_data *ts; 672 int error; 673 674 /* init spi_device */ 675 spi->mode = SPI_MODE_0; 676 spi->bits_per_word = 8; 677 error = spi_setup(spi); 678 if (error) 679 return error; 680 681 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 682 if (!ts) 683 return -ENOMEM; 684 685 mutex_init(&ts->hid_request_lock); 686 spi_set_drvdata(spi, ts); 687 ts->spi = spi; 688 ts->dev = dev; 689 ts->hid_max_event_sz = GOODIX_SPI_READ_PREFIX_LEN + 690 GOODIX_HID_ACK_HEADER_SIZE + GOODIX_HID_COOR_PKG_LEN; 691 ts->event_buf = devm_kmalloc(dev, ts->hid_max_event_sz, GFP_KERNEL); 692 if (!ts->event_buf) 693 return -ENOMEM; 694 695 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 696 if (IS_ERR(ts->reset_gpio)) 697 return dev_err_probe(dev, PTR_ERR(ts->reset_gpio), 698 "failed to request reset gpio\n"); 699 700 error = device_property_read_u32(dev, "goodix,hid-report-addr", 701 &ts->hid_report_addr); 702 if (error) 703 return dev_err_probe(dev, error, 704 "failed get hid report addr\n"); 705 706 error = goodix_dev_confirm(ts); 707 if (error) 708 return error; 709 710 /* Waits 150ms for firmware to fully boot */ 711 msleep(GOODIX_NORMAL_RESET_DELAY_MS); 712 713 error = goodix_hid_init(ts); 714 if (error) { 715 dev_err(dev, "failed init hid device"); 716 return error; 717 } 718 719 error = devm_request_threaded_irq(&ts->spi->dev, ts->spi->irq, 720 NULL, goodix_hid_irq, IRQF_ONESHOT, 721 "goodix_spi_hid", ts); 722 if (error) { 723 dev_err(ts->dev, "could not register interrupt, irq = %d, %d", 724 ts->spi->irq, error); 725 goto err_destroy_hid; 726 } 727 728 return 0; 729 730 err_destroy_hid: 731 hid_destroy_device(ts->hid); 732 return error; 733 } 734 735 static void goodix_spi_remove(struct spi_device *spi) 736 { 737 struct goodix_ts_data *ts = spi_get_drvdata(spi); 738 739 disable_irq(spi->irq); 740 hid_destroy_device(ts->hid); 741 } 742 743 static int goodix_spi_set_power(struct goodix_ts_data *ts, int power_state) 744 { 745 u8 power_control_cmd[] = {0x00, 0x00, 0x00, 0x87, 0x02, 0x00, 0x08}; 746 int error; 747 748 /* value 0 for power on, 1 for power sleep */ 749 power_control_cmd[5] = power_state; 750 751 guard(mutex)(&ts->hid_request_lock); 752 error = goodix_spi_write(ts, ts->hid_report_addr, power_control_cmd, 753 sizeof(power_control_cmd)); 754 if (error) { 755 dev_err(ts->dev, "failed set power mode: %s", 756 power_state == GOODIX_SPI_POWER_ON ? "on" : "sleep"); 757 return error; 758 } 759 return 0; 760 } 761 762 static int goodix_spi_suspend(struct device *dev) 763 { 764 struct goodix_ts_data *ts = dev_get_drvdata(dev); 765 766 disable_irq(ts->spi->irq); 767 return goodix_spi_set_power(ts, GOODIX_SPI_POWER_SLEEP); 768 } 769 770 static int goodix_spi_resume(struct device *dev) 771 { 772 struct goodix_ts_data *ts = dev_get_drvdata(dev); 773 774 enable_irq(ts->spi->irq); 775 return goodix_spi_set_power(ts, GOODIX_SPI_POWER_ON); 776 } 777 778 static DEFINE_SIMPLE_DEV_PM_OPS(goodix_spi_pm_ops, 779 goodix_spi_suspend, goodix_spi_resume); 780 781 #ifdef CONFIG_ACPI 782 static const struct acpi_device_id goodix_spi_acpi_match[] = { 783 { "GXTS7986" }, 784 { }, 785 }; 786 MODULE_DEVICE_TABLE(acpi, goodix_spi_acpi_match); 787 #endif 788 789 static const struct spi_device_id goodix_spi_ids[] = { 790 { "gt7986u" }, 791 { }, 792 }; 793 MODULE_DEVICE_TABLE(spi, goodix_spi_ids); 794 795 static struct spi_driver goodix_spi_driver = { 796 .driver = { 797 .name = "goodix-spi-hid", 798 .acpi_match_table = ACPI_PTR(goodix_spi_acpi_match), 799 .pm = pm_sleep_ptr(&goodix_spi_pm_ops), 800 }, 801 .probe = goodix_spi_probe, 802 .remove = goodix_spi_remove, 803 .id_table = goodix_spi_ids, 804 }; 805 module_spi_driver(goodix_spi_driver); 806 807 MODULE_DESCRIPTION("Goodix SPI driver for HID touchscreen"); 808 MODULE_AUTHOR("Goodix, Inc."); 809 MODULE_LICENSE("GPL"); 810