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