-
Notifications
You must be signed in to change notification settings - Fork 1
/
goodix.c
1591 lines (1343 loc) · 41.5 KB
/
goodix.c
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* Driver for Goodix Touchscreens
*
* Copyright (c) 2014 Red Hat Inc.
* Copyright (c) 2015 K. Merker <merker@debian.org>
*
* This code is based on gt9xx.c authored by andrew@goodix.com:
*
* 2010 - 2012 Goodix Technology.
*/
#include <linux/kernel.h>
#include <linux/dmi.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/platform_data/x86/soc.h>
#include <linux/slab.h>
#include <linux/acpi.h>
#include <linux/of.h>
#include <asm/unaligned.h>
#include "goodix.h"
#define GOODIX_GPIO_INT_NAME "irq"
#define GOODIX_GPIO_RST_NAME "reset"
#define GOODIX_MAX_HEIGHT 4096
#define GOODIX_MAX_WIDTH 4096
#define GOODIX_INT_TRIGGER 1
#define GOODIX_CONTACT_SIZE 8
#define GOODIX_MAX_CONTACT_SIZE 9
#define GOODIX_MAX_CONTACTS 10
#define GOODIX_CONFIG_MIN_LENGTH 186
#define GOODIX_CONFIG_911_LENGTH 186
#define GOODIX_CONFIG_967_LENGTH 228
#define GOODIX_CONFIG_GT9X_LENGTH 240
#define GOODIX_BUFFER_STATUS_READY BIT(7)
#define GOODIX_HAVE_KEY BIT(4)
#define GOODIX_BUFFER_STATUS_TIMEOUT 20
#define RESOLUTION_LOC 1
#define MAX_CONTACTS_LOC 5
#define TRIGGER_LOC 6
/* Our special handling for GPIO accesses through ACPI is x86 specific */
#if defined CONFIG_X86 && defined CONFIG_ACPI
#define ACPI_GPIO_SUPPORT
#endif
struct goodix_chip_id {
const char *id;
const struct goodix_chip_data *data;
};
static int goodix_check_cfg_8(struct goodix_ts_data *ts,
const u8 *cfg, int len);
static int goodix_check_cfg_16(struct goodix_ts_data *ts,
const u8 *cfg, int len);
static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
static const struct goodix_chip_data gt1x_chip_data = {
.config_addr = GOODIX_GT1X_REG_CONFIG_DATA,
.config_len = GOODIX_CONFIG_GT9X_LENGTH,
.check_config = goodix_check_cfg_16,
.calc_config_checksum = goodix_calc_cfg_checksum_16,
};
static const struct goodix_chip_data gt911_chip_data = {
.config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
.config_len = GOODIX_CONFIG_911_LENGTH,
.check_config = goodix_check_cfg_8,
.calc_config_checksum = goodix_calc_cfg_checksum_8,
};
static const struct goodix_chip_data gt967_chip_data = {
.config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
.config_len = GOODIX_CONFIG_967_LENGTH,
.check_config = goodix_check_cfg_8,
.calc_config_checksum = goodix_calc_cfg_checksum_8,
};
static const struct goodix_chip_data gt9x_chip_data = {
.config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
.config_len = GOODIX_CONFIG_GT9X_LENGTH,
.check_config = goodix_check_cfg_8,
.calc_config_checksum = goodix_calc_cfg_checksum_8,
};
static const struct goodix_chip_id goodix_chip_ids[] = {
{ .id = "1151", .data = >1x_chip_data },
{ .id = "1158", .data = >1x_chip_data },
{ .id = "5663", .data = >1x_chip_data },
{ .id = "5688", .data = >1x_chip_data },
{ .id = "917S", .data = >1x_chip_data },
{ .id = "9286", .data = >1x_chip_data },
{ .id = "911", .data = >911_chip_data },
{ .id = "9271", .data = >911_chip_data },
{ .id = "9110", .data = >911_chip_data },
{ .id = "9111", .data = >911_chip_data },
{ .id = "927", .data = >911_chip_data },
{ .id = "928", .data = >911_chip_data },
{ .id = "912", .data = >967_chip_data },
{ .id = "9147", .data = >967_chip_data },
{ .id = "967", .data = >967_chip_data },
{ }
};
static const unsigned long goodix_irq_flags[] = {
IRQ_TYPE_EDGE_RISING,
IRQ_TYPE_EDGE_FALLING,
IRQ_TYPE_LEVEL_LOW,
IRQ_TYPE_LEVEL_HIGH,
};
static const struct dmi_system_id nine_bytes_report[] = {
#if defined(CONFIG_DMI) && defined(CONFIG_X86)
{
/* Lenovo Yoga Book X90F / X90L */
.matches = {
DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "CHERRYVIEW D1 PLATFORM"),
DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "YETI-11"),
}
},
{
/* Lenovo Yoga Book X91F / X91L */
.matches = {
/* Non exact match to match F + L versions */
DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91"),
}
},
#endif
{}
};
/*
* Those tablets have their x coordinate inverted
*/
static const struct dmi_system_id inverted_x_screen[] = {
#if defined(CONFIG_DMI) && defined(CONFIG_X86)
{
.ident = "Cube I15-TC",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
},
},
#endif
{}
};
static int invert_x = -1;
module_param(invert_x,int,0440);
static int invert_y = -1;
module_param(invert_y,int,0440);
static int swap_x_y = -1;
module_param(swap_x_y,int,0440);
/**
* goodix_i2c_read - read data from a register of the i2c slave device.
*
* @client: i2c device.
* @reg: the register to read from.
* @buf: raw write data buffer.
* @len: length of the buffer to write
*/
int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len)
{
struct i2c_msg msgs[2];
__be16 wbuf = cpu_to_be16(reg);
int ret;
msgs[0].flags = 0;
msgs[0].addr = client->addr;
msgs[0].len = 2;
msgs[0].buf = (u8 *)&wbuf;
msgs[1].flags = I2C_M_RD;
msgs[1].addr = client->addr;
msgs[1].len = len;
msgs[1].buf = buf;
ret = i2c_transfer(client->adapter, msgs, 2);
if (ret >= 0)
ret = (ret == ARRAY_SIZE(msgs) ? 0 : -EIO);
if (ret)
dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d\n",
len, reg, ret);
return ret;
}
/**
* goodix_i2c_write - write data to a register of the i2c slave device.
*
* @client: i2c device.
* @reg: the register to write to.
* @buf: raw data buffer to write.
* @len: length of the buffer to write
*/
int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, int len)
{
u8 *addr_buf;
struct i2c_msg msg;
int ret;
addr_buf = kmalloc(len + 2, GFP_KERNEL);
if (!addr_buf)
return -ENOMEM;
addr_buf[0] = reg >> 8;
addr_buf[1] = reg & 0xFF;
memcpy(&addr_buf[2], buf, len);
msg.flags = 0;
msg.addr = client->addr;
msg.buf = addr_buf;
msg.len = len + 2;
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret >= 0)
ret = (ret == 1 ? 0 : -EIO);
kfree(addr_buf);
if (ret)
dev_err(&client->dev, "Error writing %d bytes to 0x%04x: %d\n",
len, reg, ret);
return ret;
}
int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
{
return goodix_i2c_write(client, reg, &value, sizeof(value));
}
static const struct goodix_chip_data *goodix_get_chip_data(const char *id)
{
unsigned int i;
for (i = 0; goodix_chip_ids[i].id; i++) {
if (!strcmp(goodix_chip_ids[i].id, id))
return goodix_chip_ids[i].data;
}
return >9x_chip_data;
}
static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
{
unsigned long max_timeout;
int touch_num;
int error;
u16 addr = GOODIX_READ_COOR_ADDR;
/*
* We are going to read 1-byte header,
* ts->contact_size * max(1, touch_num) bytes of coordinates
* and 1-byte footer which contains the touch-key code.
*/
const int header_contact_keycode_size = 1 + ts->contact_size + 1;
/*
* The 'buffer status' bit, which indicates that the data is valid, is
* not set as soon as the interrupt is raised, but slightly after.
* This takes around 10 ms to happen, so we poll for 20 ms.
*/
max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
do {
error = goodix_i2c_read(ts->client, addr, data,
header_contact_keycode_size);
if (error)
return error;
if (data[0] & GOODIX_BUFFER_STATUS_READY) {
touch_num = data[0] & 0x0f;
if (touch_num > ts->max_touch_num)
return -EPROTO;
if (touch_num > 1) {
addr += header_contact_keycode_size;
data += header_contact_keycode_size;
error = goodix_i2c_read(ts->client,
addr, data,
ts->contact_size *
(touch_num - 1));
if (error)
return error;
}
return touch_num;
}
if (data[0] == 0 && ts->firmware_name) {
if (goodix_handle_fw_request(ts))
return 0;
}
usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
} while (time_before(jiffies, max_timeout));
/*
* The Goodix panel will send spurious interrupts after a
* 'finger up' event, which will always cause a timeout.
*/
return -ENOMSG;
}
static int goodix_create_pen_input(struct goodix_ts_data *ts)
{
struct device *dev = &ts->client->dev;
struct input_dev *input;
input = devm_input_allocate_device(dev);
if (!input)
return -ENOMEM;
input_copy_abs(input, ABS_X, ts->input_dev, ABS_MT_POSITION_X);
input_copy_abs(input, ABS_Y, ts->input_dev, ABS_MT_POSITION_Y);
/*
* The resolution of these touchscreens is about 10 units/mm, the actual
* resolution does not matter much since we set INPUT_PROP_DIRECT.
* Userspace wants something here though, so just set it to 10 units/mm.
*/
input_abs_set_res(input, ABS_X, 10);
input_abs_set_res(input, ABS_Y, 10);
input_set_abs_params(input, ABS_PRESSURE, 0, 255, 0, 0);
input_set_capability(input, EV_KEY, BTN_TOUCH);
input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
input_set_capability(input, EV_KEY, BTN_STYLUS);
input_set_capability(input, EV_KEY, BTN_STYLUS2);
__set_bit(INPUT_PROP_DIRECT, input->propbit);
input->name = "Goodix Active Pen";
input->phys = "input/pen";
input->id.bustype = BUS_I2C;
input->id.vendor = 0x0416;
if (kstrtou16(ts->id, 10, &input->id.product))
input->id.product = 0x1001;
input->id.version = ts->version;
ts->input_pen = input;
return 0;
}
static void goodix_ts_report_pen_down(struct goodix_ts_data *ts, u8 *data)
{
int input_x, input_y, input_w, error;
u8 key_value;
if (!ts->pen_input_registered) {
error = input_register_device(ts->input_pen);
ts->pen_input_registered = (error == 0) ? 1 : error;
}
if (ts->pen_input_registered < 0)
return;
if (ts->contact_size == 9) {
input_x = get_unaligned_le16(&data[4]);
input_y = get_unaligned_le16(&data[6]);
input_w = get_unaligned_le16(&data[8]);
} else {
input_x = get_unaligned_le16(&data[2]);
input_y = get_unaligned_le16(&data[4]);
input_w = get_unaligned_le16(&data[6]);
}
touchscreen_report_pos(ts->input_pen, &ts->prop, input_x, input_y, false);
input_report_abs(ts->input_pen, ABS_PRESSURE, input_w);
input_report_key(ts->input_pen, BTN_TOUCH, 1);
input_report_key(ts->input_pen, BTN_TOOL_PEN, 1);
if (data[0] & GOODIX_HAVE_KEY) {
key_value = data[1 + ts->contact_size];
input_report_key(ts->input_pen, BTN_STYLUS, key_value & 0x10);
input_report_key(ts->input_pen, BTN_STYLUS2, key_value & 0x20);
} else {
input_report_key(ts->input_pen, BTN_STYLUS, 0);
input_report_key(ts->input_pen, BTN_STYLUS2, 0);
}
input_sync(ts->input_pen);
}
static void goodix_ts_report_pen_up(struct goodix_ts_data *ts)
{
if (!ts->input_pen)
return;
input_report_key(ts->input_pen, BTN_TOUCH, 0);
input_report_key(ts->input_pen, BTN_TOOL_PEN, 0);
input_report_key(ts->input_pen, BTN_STYLUS, 0);
input_report_key(ts->input_pen, BTN_STYLUS2, 0);
input_sync(ts->input_pen);
}
static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
{
int id = coor_data[0] & 0x0F;
int input_x = get_unaligned_le16(&coor_data[1]);
int input_y = get_unaligned_le16(&coor_data[3]);
int input_w = get_unaligned_le16(&coor_data[5]);
input_mt_slot(ts->input_dev, id);
input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
touchscreen_report_pos(ts->input_dev, &ts->prop,
input_x, input_y, true);
input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
}
static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
{
int id = coor_data[1] & 0x0F;
int input_x = get_unaligned_le16(&coor_data[3]);
int input_y = get_unaligned_le16(&coor_data[5]);
int input_w = get_unaligned_le16(&coor_data[7]);
input_mt_slot(ts->input_dev, id);
input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
touchscreen_report_pos(ts->input_dev, &ts->prop,
input_x, input_y, true);
input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
}
static void goodix_ts_release_keys(struct goodix_ts_data *ts)
{
int i;
for (i = 0; i < GOODIX_MAX_KEYS; i++)
input_report_key(ts->input_dev, ts->keymap[i], 0);
}
static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
{
int touch_num;
u8 key_value;
int i;
if (data[0] & GOODIX_HAVE_KEY) {
touch_num = data[0] & 0x0f;
key_value = data[1 + ts->contact_size * touch_num];
for (i = 0; i < GOODIX_MAX_KEYS; i++)
if (key_value & BIT(i))
input_report_key(ts->input_dev,
ts->keymap[i], 1);
} else {
goodix_ts_release_keys(ts);
}
}
/**
* goodix_process_events - Process incoming events
*
* @ts: our goodix_ts_data pointer
*
* Called when the IRQ is triggered. Read the current device state, and push
* the input events to the user space.
*/
static void goodix_process_events(struct goodix_ts_data *ts)
{
u8 point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
int touch_num;
int i;
touch_num = goodix_ts_read_input_report(ts, point_data);
if (touch_num < 0)
return;
/* The pen being down is always reported as a single touch */
if (touch_num == 1 && (point_data[1] & 0x80)) {
goodix_ts_report_pen_down(ts, point_data);
goodix_ts_release_keys(ts);
goto sync; /* Release any previously registered touches */
} else {
goodix_ts_report_pen_up(ts);
}
goodix_ts_report_key(ts, point_data);
for (i = 0; i < touch_num; i++)
if (ts->contact_size == 9)
goodix_ts_report_touch_9b(ts,
&point_data[1 + ts->contact_size * i]);
else
goodix_ts_report_touch_8b(ts,
&point_data[1 + ts->contact_size * i]);
sync:
input_mt_sync_frame(ts->input_dev);
input_sync(ts->input_dev);
}
/**
* goodix_ts_irq_handler - The IRQ handler
*
* @irq: interrupt number.
* @dev_id: private data pointer.
*/
static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
{
struct goodix_ts_data *ts = dev_id;
goodix_process_events(ts);
goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
return IRQ_HANDLED;
}
static void goodix_free_irq(struct goodix_ts_data *ts)
{
devm_free_irq(&ts->client->dev, ts->client->irq, ts);
}
static int goodix_request_irq(struct goodix_ts_data *ts)
{
return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
NULL, goodix_ts_irq_handler,
ts->irq_flags, ts->client->name, ts);
}
static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
{
int i, raw_cfg_len = len - 2;
u8 check_sum = 0;
for (i = 0; i < raw_cfg_len; i++)
check_sum += cfg[i];
check_sum = (~check_sum) + 1;
if (check_sum != cfg[raw_cfg_len]) {
dev_err(&ts->client->dev,
"The checksum of the config fw is not correct");
return -EINVAL;
}
if (cfg[raw_cfg_len + 1] != 1) {
dev_err(&ts->client->dev,
"Config fw must have Config_Fresh register set");
return -EINVAL;
}
return 0;
}
static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
{
int i, raw_cfg_len = ts->chip->config_len - 2;
u8 check_sum = 0;
for (i = 0; i < raw_cfg_len; i++)
check_sum += ts->config[i];
check_sum = (~check_sum) + 1;
ts->config[raw_cfg_len] = check_sum;
ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
}
static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
int len)
{
int i, raw_cfg_len = len - 3;
u16 check_sum = 0;
for (i = 0; i < raw_cfg_len; i += 2)
check_sum += get_unaligned_be16(&cfg[i]);
check_sum = (~check_sum) + 1;
if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
dev_err(&ts->client->dev,
"The checksum of the config fw is not correct");
return -EINVAL;
}
if (cfg[raw_cfg_len + 2] != 1) {
dev_err(&ts->client->dev,
"Config fw must have Config_Fresh register set");
return -EINVAL;
}
return 0;
}
static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
{
int i, raw_cfg_len = ts->chip->config_len - 3;
u16 check_sum = 0;
for (i = 0; i < raw_cfg_len; i += 2)
check_sum += get_unaligned_be16(&ts->config[i]);
check_sum = (~check_sum) + 1;
put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
}
/**
* goodix_check_cfg - Checks if config fw is valid
*
* @ts: goodix_ts_data pointer
* @cfg: firmware config data
* @len: config data length
*/
static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
{
if (len < GOODIX_CONFIG_MIN_LENGTH ||
len > GOODIX_CONFIG_MAX_LENGTH) {
dev_err(&ts->client->dev,
"The length of the config fw is not correct");
return -EINVAL;
}
return ts->chip->check_config(ts, cfg, len);
}
/**
* goodix_send_cfg - Write fw config to device
*
* @ts: goodix_ts_data pointer
* @cfg: config firmware to write to device
* @len: config data length
*/
int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
{
int error;
error = goodix_check_cfg(ts, cfg, len);
if (error)
return error;
error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
if (error)
return error;
dev_dbg(&ts->client->dev, "Config sent successfully.");
/* Let the firmware reconfigure itself, so sleep for 10ms */
usleep_range(10000, 11000);
return 0;
}
#ifdef ACPI_GPIO_SUPPORT
static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
{
acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
acpi_status status;
status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
return ACPI_SUCCESS(status) ? 0 : -EIO;
}
static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
{
acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
acpi_status status;
status = acpi_execute_simple_method(handle, "INTO", value);
return ACPI_SUCCESS(status) ? 0 : -EIO;
}
#else
static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
{
dev_err(&ts->client->dev,
"%s called on device without ACPI support\n", __func__);
return -EINVAL;
}
static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
{
dev_err(&ts->client->dev,
"%s called on device without ACPI support\n", __func__);
return -EINVAL;
}
#endif
static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
{
switch (ts->irq_pin_access_method) {
case IRQ_PIN_ACCESS_NONE:
dev_err(&ts->client->dev,
"%s called without an irq_pin_access_method set\n",
__func__);
return -EINVAL;
case IRQ_PIN_ACCESS_GPIO:
return gpiod_direction_output(ts->gpiod_int, value);
case IRQ_PIN_ACCESS_ACPI_GPIO:
/*
* The IRQ pin triggers on a falling edge, so its gets marked
* as active-low, use output_raw to avoid the value inversion.
*/
return gpiod_direction_output_raw(ts->gpiod_int, value);
case IRQ_PIN_ACCESS_ACPI_METHOD:
return goodix_pin_acpi_output_method(ts, value);
}
return -EINVAL; /* Never reached */
}
static int goodix_irq_direction_input(struct goodix_ts_data *ts)
{
switch (ts->irq_pin_access_method) {
case IRQ_PIN_ACCESS_NONE:
dev_err(&ts->client->dev,
"%s called without an irq_pin_access_method set\n",
__func__);
return -EINVAL;
case IRQ_PIN_ACCESS_GPIO:
return gpiod_direction_input(ts->gpiod_int);
case IRQ_PIN_ACCESS_ACPI_GPIO:
return gpiod_direction_input(ts->gpiod_int);
case IRQ_PIN_ACCESS_ACPI_METHOD:
return goodix_pin_acpi_direction_input(ts);
}
return -EINVAL; /* Never reached */
}
int goodix_int_sync(struct goodix_ts_data *ts)
{
int error;
error = goodix_irq_direction_output(ts, 0);
if (error)
goto error;
msleep(50); /* T5: 50ms */
error = goodix_irq_direction_input(ts);
if (error)
goto error;
return 0;
error:
dev_err(&ts->client->dev, "Controller irq sync failed.\n");
return error;
}
/**
* goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode
*
* @ts: goodix_ts_data pointer
*/
int goodix_reset_no_int_sync(struct goodix_ts_data *ts)
{
int error;
/* begin select I2C slave addr */
error = gpiod_direction_output(ts->gpiod_rst, 0);
if (error)
goto error;
msleep(20); /* T2: > 10ms */
/* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
if (error)
goto error;
usleep_range(100, 2000); /* T3: > 100us */
error = gpiod_direction_output(ts->gpiod_rst, 1);
if (error)
goto error;
usleep_range(6000, 10000); /* T4: > 5ms */
/*
* Put the reset pin back in to input / high-impedance mode to save
* power. Only do this in the non ACPI case since some ACPI boards
* don't have a pull-up, so there the reset pin must stay active-high.
*/
if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_GPIO) {
error = gpiod_direction_input(ts->gpiod_rst);
if (error)
goto error;
}
return 0;
error:
dev_err(&ts->client->dev, "Controller reset failed.\n");
return error;
}
/**
* goodix_reset - Reset device during power on
*
* @ts: goodix_ts_data pointer
*/
static int goodix_reset(struct goodix_ts_data *ts)
{
int error;
error = goodix_reset_no_int_sync(ts);
if (error)
return error;
return goodix_int_sync(ts);
}
#ifdef ACPI_GPIO_SUPPORT
static const struct acpi_gpio_params first_gpio = { 0, 0, false };
static const struct acpi_gpio_params second_gpio = { 1, 0, false };
static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
{ GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
{ GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
{ },
};
static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
{ GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
{ },
};
static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
{ },
};
static int goodix_resource(struct acpi_resource *ares, void *data)
{
struct goodix_ts_data *ts = data;
struct device *dev = &ts->client->dev;
struct acpi_resource_gpio *gpio;
if (acpi_gpio_get_irq_resource(ares, &gpio)) {
if (ts->gpio_int_idx == -1) {
ts->gpio_int_idx = ts->gpio_count;
} else {
dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
ts->gpio_int_idx = -2;
}
ts->gpio_count++;
} else if (acpi_gpio_get_io_resource(ares, &gpio))
ts->gpio_count++;
return 0;
}
/*
* This function gets called in case we fail to get the irq GPIO directly
* because the ACPI tables lack GPIO-name to APCI _CRS index mappings
* (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
* In that case we add our own mapping and then goodix_get_gpio_config()
* retries to get the GPIOs based on the added mapping.
*/
static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
{
const struct acpi_gpio_mapping *gpio_mapping = NULL;
struct device *dev = &ts->client->dev;
LIST_HEAD(resources);
int irq, ret;
ts->gpio_count = 0;
ts->gpio_int_idx = -1;
ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
goodix_resource, ts);
if (ret < 0) {
dev_err(dev, "Error getting ACPI resources: %d\n", ret);
return ret;
}
acpi_dev_free_resource_list(&resources);
/*
* CHT devices should have a GpioInt + a regular GPIO ACPI resource.
* Some CHT devices have a bug (where the also is bogus Interrupt
* resource copied from a previous BYT based generation). i2c-core-acpi
* will use the non-working Interrupt resource, fix this up.
*/
if (soc_intel_is_cht() && ts->gpio_count == 2 && ts->gpio_int_idx != -1) {
irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
if (irq > 0 && irq != ts->client->irq) {
dev_warn(dev, "Overriding IRQ %d -> %d\n", ts->client->irq, irq);
ts->client->irq = irq;
}
}
/* Some devices with gpio_int_idx 0 list a third unused GPIO */
if ((ts->gpio_count == 2 || ts->gpio_count == 3) && ts->gpio_int_idx == 0) {
ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
gpio_mapping = acpi_goodix_int_first_gpios;
} else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
gpio_mapping = acpi_goodix_int_last_gpios;
} else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
gpio_mapping = acpi_goodix_reset_only_gpios;
} else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
gpio_mapping = acpi_goodix_int_last_gpios;
} else if (ts->gpio_count == 1 && ts->gpio_int_idx == 0) {
/*
* On newer devices there is only 1 GpioInt resource and _PS0
* does the whole reset sequence for us.
*/
acpi_device_fix_up_power(ACPI_COMPANION(dev));
/*
* Before the _PS0 call the int GPIO may have been in output
* mode and the call should have put the int GPIO in input mode,
* but the GPIO subsys cached state may still think it is
* in output mode, causing gpiochip_lock_as_irq() failure.
*
* Add a mapping for the int GPIO to make the
* gpiod_int = gpiod_get(..., GPIOD_IN) call succeed,
* which will explicitly set the direction to input.
*/
ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
gpio_mapping = acpi_goodix_int_first_gpios;
} else {
dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
ts->gpio_count, ts->gpio_int_idx);
/*
* On some devices _PS0 does a reset for us and
* sometimes this is necessary for things to work.
*/
acpi_device_fix_up_power(ACPI_COMPANION(dev));
return -EINVAL;
}
/*
* Normally we put the reset pin in input / high-impedance mode to save
* power. But some x86/ACPI boards don't have a pull-up, so for the ACPI
* case, leave the pin as is. This results in the pin not being touched
* at all on x86/ACPI boards, except when needed for error-recover.
*/
ts->gpiod_rst_flags = GPIOD_ASIS;
return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
}
#else
static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
{
return -EINVAL;
}
#endif /* CONFIG_X86 && CONFIG_ACPI */
/**
* goodix_get_gpio_config - Get GPIO config from ACPI/DT
*
* @ts: goodix_ts_data pointer
*/
static int goodix_get_gpio_config(struct goodix_ts_data *ts)
{
struct device *dev;
struct gpio_desc *gpiod;
bool added_acpi_mappings = false;
if (!ts->client)
return -EINVAL;
dev = &ts->client->dev;
/*
* By default we request the reset pin as input, leaving it in
* high-impedance when not resetting the controller to save power.
*/
ts->gpiod_rst_flags = GPIOD_IN;
ts->avdd28 = devm_regulator_get(dev, "AVDD28");
if (IS_ERR(ts->avdd28))
return dev_err_probe(dev, PTR_ERR(ts->avdd28), "Failed to get AVDD28 regulator\n");
ts->vddio = devm_regulator_get(dev, "VDDIO");
if (IS_ERR(ts->vddio))
return dev_err_probe(dev, PTR_ERR(ts->vddio), "Failed to get VDDIO regulator\n");
retry_get_irq_gpio:
/* Get the interrupt GPIO pin number */
gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
if (IS_ERR(gpiod))
return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
GOODIX_GPIO_INT_NAME);
if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
added_acpi_mappings = true;
if (goodix_add_acpi_gpio_mappings(ts) == 0)
goto retry_get_irq_gpio;
}
ts->gpiod_int = gpiod;