1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
|
// SPDX-License-Identifier: GPL-2.0
/*
* ILITEK Touch IC driver for 23XX, 25XX and Lego series
*
* Copyright (C) 2011 ILI Technology Corporation.
* Copyright (C) 2020 Luca Hsu <luca_hsu@ilitek.com>
* Copyright (C) 2021 Joe Hung <joe_hung@ilitek.com>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/errno.h>
#include <linux/acpi.h>
#include <linux/input/touchscreen.h>
#include <asm/unaligned.h>
#define ILITEK_TS_NAME "ilitek_ts"
#define BL_V1_8 0x108
#define BL_V1_7 0x107
#define BL_V1_6 0x106
#define ILITEK_TP_CMD_GET_TP_RES 0x20
#define ILITEK_TP_CMD_GET_SCRN_RES 0x21
#define ILITEK_TP_CMD_SET_IC_SLEEP 0x30
#define ILITEK_TP_CMD_SET_IC_WAKE 0x31
#define ILITEK_TP_CMD_GET_FW_VER 0x40
#define ILITEK_TP_CMD_GET_PRL_VER 0x42
#define ILITEK_TP_CMD_GET_MCU_VER 0x61
#define ILITEK_TP_CMD_GET_IC_MODE 0xC0
#define REPORT_COUNT_ADDRESS 61
#define ILITEK_SUPPORT_MAX_POINT 40
struct ilitek_protocol_info {
u16 ver;
u8 ver_major;
};
struct ilitek_ts_data {
struct i2c_client *client;
struct gpio_desc *reset_gpio;
struct input_dev *input_dev;
struct touchscreen_properties prop;
const struct ilitek_protocol_map *ptl_cb_func;
struct ilitek_protocol_info ptl;
char product_id[30];
u16 mcu_ver;
u8 ic_mode;
u8 firmware_ver[8];
s32 reset_time;
s32 screen_max_x;
s32 screen_max_y;
s32 screen_min_x;
s32 screen_min_y;
s32 max_tp;
};
struct ilitek_protocol_map {
u16 cmd;
const char *name;
int (*func)(struct ilitek_ts_data *ts, u16 cmd, u8 *inbuf, u8 *outbuf);
};
enum ilitek_cmds {
/* common cmds */
GET_PTL_VER = 0,
GET_FW_VER,
GET_SCRN_RES,
GET_TP_RES,
GET_IC_MODE,
GET_MCU_VER,
SET_IC_SLEEP,
SET_IC_WAKE,
/* ALWAYS keep at the end */
MAX_CMD_CNT
};
/* ILITEK I2C R/W APIs */
static int ilitek_i2c_write_and_read(struct ilitek_ts_data *ts,
u8 *cmd, int write_len, int delay,
u8 *data, int read_len)
{
int error;
struct i2c_client *client = ts->client;
struct i2c_msg msgs[] = {
{
.addr = client->addr,
.flags = 0,
.len = write_len,
.buf = cmd,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = read_len,
.buf = data,
},
};
if (delay == 0 && write_len > 0 && read_len > 0) {
error = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
if (error < 0)
return error;
} else {
if (write_len > 0) {
error = i2c_transfer(client->adapter, msgs, 1);
if (error < 0)
return error;
}
if (delay > 0)
mdelay(delay);
if (read_len > 0) {
error = i2c_transfer(client->adapter, msgs + 1, 1);
if (error < 0)
return error;
}
}
return 0;
}
/* ILITEK ISR APIs */
static void ilitek_touch_down(struct ilitek_ts_data *ts, unsigned int id,
unsigned int x, unsigned int y)
{
struct input_dev *input = ts->input_dev;
input_mt_slot(input, id);
input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
touchscreen_report_pos(input, &ts->prop, x, y, true);
}
static int ilitek_process_and_report_v6(struct ilitek_ts_data *ts)
{
int error = 0;
u8 buf[512];
int packet_len = 5;
int packet_max_point = 10;
int report_max_point;
int i, count;
struct input_dev *input = ts->input_dev;
struct device *dev = &ts->client->dev;
unsigned int x, y, status, id;
error = ilitek_i2c_write_and_read(ts, NULL, 0, 0, buf, 64);
if (error) {
dev_err(dev, "get touch info failed, err:%d\n", error);
goto err_sync_frame;
}
report_max_point = buf[REPORT_COUNT_ADDRESS];
if (report_max_point > ts->max_tp) {
dev_err(dev, "FW report max point:%d > panel info. max:%d\n",
report_max_point, ts->max_tp);
error = -EINVAL;
goto err_sync_frame;
}
count = DIV_ROUND_UP(report_max_point, packet_max_point);
for (i = 1; i < count; i++) {
error = ilitek_i2c_write_and_read(ts, NULL, 0, 0,
buf + i * 64, 64);
if (error) {
dev_err(dev, "get touch info. failed, cnt:%d, err:%d\n",
count, error);
goto err_sync_frame;
}
}
for (i = 0; i < report_max_point; i++) {
status = buf[i * packet_len + 1] & 0x40;
if (!status)
continue;
id = buf[i * packet_len + 1] & 0x3F;
x = get_unaligned_le16(buf + i * packet_len + 2);
y = get_unaligned_le16(buf + i * packet_len + 4);
if (x > ts->screen_max_x || x < ts->screen_min_x ||
y > ts->screen_max_y || y < ts->screen_min_y) {
dev_warn(dev, "invalid position, X[%d,%u,%d], Y[%d,%u,%d]\n",
ts->screen_min_x, x, ts->screen_max_x,
ts->screen_min_y, y, ts->screen_max_y);
continue;
}
ilitek_touch_down(ts, id, x, y);
}
err_sync_frame:
input_mt_sync_frame(input);
input_sync(input);
return error;
}
/* APIs of cmds for ILITEK Touch IC */
static int api_protocol_set_cmd(struct ilitek_ts_data *ts,
u16 idx, u8 *inbuf, u8 *outbuf)
{
u16 cmd;
int error;
if (idx >= MAX_CMD_CNT)
return -EINVAL;
cmd = ts->ptl_cb_func[idx].cmd;
error = ts->ptl_cb_func[idx].func(ts, cmd, inbuf, outbuf);
if (error)
return error;
return 0;
}
static int api_protocol_get_ptl_ver(struct ilitek_ts_data *ts,
u16 cmd, u8 *inbuf, u8 *outbuf)
{
int error;
u8 buf[64];
buf[0] = cmd;
error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 3);
if (error)
return error;
ts->ptl.ver = get_unaligned_be16(outbuf);
ts->ptl.ver_major = outbuf[0];
return 0;
}
static int api_protocol_get_mcu_ver(struct ilitek_ts_data *ts,
u16 cmd, u8 *inbuf, u8 *outbuf)
{
int error;
u8 buf[64];
buf[0] = cmd;
error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 32);
if (error)
return error;
ts->mcu_ver = get_unaligned_le16(outbuf);
memset(ts->product_id, 0, sizeof(ts->product_id));
memcpy(ts->product_id, outbuf + 6, 26);
return 0;
}
static int api_protocol_get_fw_ver(struct ilitek_ts_data *ts,
u16 cmd, u8 *inbuf, u8 *outbuf)
{
int error;
u8 buf[64];
buf[0] = cmd;
error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 8);
if (error)
return error;
memcpy(ts->firmware_ver, outbuf, 8);
return 0;
|