-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgt1x_generic.c
2544 lines (2219 loc) · 60.1 KB
/
gt1x_generic.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
/* drivers/input/touchscreen/gt1x_generic.c
*
* 2010 - 2016 Goodix Technology.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be a reference
* to you, when you are integrating the GOODiX's CTP IC into your system,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Version: 1.6
*/
#include "gt1x_generic.h"
#if defined(CONFIG_GTP_PROXIMITY) && defined(PLATFORM_MTK)
#include <hwmsensor.h>
#include <hwmsen_dev.h>
#include <sensors_io.h>
#endif
#ifdef CONFIG_GTP_TYPE_B_PROTOCOL
#include <linux/input/mt.h>
#endif
static struct workqueue_struct *gt1x_workqueue;
struct i2c_client *gt1x_i2c_client;
static u8 gt1x_config[GTP_CONFIG_ORG_LENGTH + GTP_CONFIG_EXT_LENGTH];
static u32 gt1x_cfg_length = GTP_CONFIG_MAX_LENGTH;
gt1x_chip_type_t gt1x_chip_type = CHIP_TYPE_NONE;
struct gt1x_version_info gt1x_version = {{0}};
#ifdef CONFIG_GTP_HAVE_TOUCH_KEY
static const u16 gt1x_touch_key_array[] = GTP_KEY_TAB;
#endif
#if defined(CONFIG_GTP_WITH_STYLUS) && defined(CONFIG_GTP_HAVE_STYLUS_KEY)
static const u16 gt1x_stylus_key_array[] = GTP_STYLUS_KEY_TAB;
#endif
#define GOODIX_SYSFS_DIR "goodix"
static struct kobject *sysfs_rootdir = NULL;
volatile int gt1x_rawdiff_mode = 0;
static u8 gt1x_wakeup_level = 0;
u8 gt1x_init_failed = 0;
u8 gt1x_int_type = 0;
u32 gt1x_abs_x_max = 0;
u32 gt1x_abs_y_max = 0;
int gt1x_halt = 0;
static ssize_t gt1x_debug_read_proc(struct file *, char __user *, size_t,loff_t *);
static ssize_t gt1x_debug_write_proc(struct file *, const char __user *, size_t, loff_t *);
static struct proc_dir_entry *gt1x_debug_proc_entry = NULL;
static const struct file_operations gt1x_debug_fops = {
.owner = THIS_MODULE,
.read = gt1x_debug_read_proc,
.write = gt1x_debug_write_proc,
};
static s32 gt1x_init_debug_node(void)
{
gt1x_debug_proc_entry = proc_create(GT1X_DEBUG_PROC_FILE,
0660, NULL, >1x_debug_fops);
if (gt1x_debug_proc_entry == NULL) {
GTP_ERROR("Create proc entry /proc/%s FAILED!",
GT1X_DEBUG_PROC_FILE);
return -1;
}
GTP_INFO("Created proc entry /proc/%s.", GT1X_DEBUG_PROC_FILE);
return 0;
}
static void gt1x_deinit_debug_node(void)
{
if (gt1x_debug_proc_entry != NULL) {
remove_proc_entry(GT1X_DEBUG_PROC_FILE, NULL);
}
}
static ssize_t gt1x_debug_read_proc(struct file *file, char __user * page,
size_t size, loff_t * ppos)
{
char *ptr = page;
char temp_data[gt1x_cfg_length];
struct irq_desc *irq_desc = NULL;
int i;
if (*ppos) {
return 0;
}
ptr += sprintf(ptr, "==== GT1X default config setting in driver====\n");
for (i = 0; i < gt1x_cfg_length; i++) {
ptr += sprintf(ptr, "0x%02X,", gt1x_config[i]);
if (i % 10 == 9 && i != GTP_CONFIG_ORG_LENGTH)
ptr += sprintf(ptr, "\n");
if (i == GTP_CONFIG_ORG_LENGTH - 1)
ptr += sprintf(ptr, "\n-------------\n");
}
ptr += sprintf(ptr, "\n");
ptr += sprintf(ptr, "==== GT1X config read from chip====\n");
i = gt1x_i2c_read(GTP_REG_CONFIG_DATA, temp_data,
GTP_CONFIG_ORG_LENGTH);
if (i == 0 && gt1x_cfg_length == GTP_CONFIG_ORG_LENGTH +
GTP_CONFIG_EXT_LENGTH) {
i = gt1x_i2c_read(GTP_REG_EXT_CONFIG,
&temp_data[GTP_CONFIG_ORG_LENGTH],
GTP_CONFIG_EXT_LENGTH);
}
for (i = 0; i < gt1x_cfg_length; i++) {
ptr += sprintf(ptr, "0x%02X,", temp_data[i]);
if (i % 10 == 9 && i != GTP_CONFIG_ORG_LENGTH)
ptr += sprintf(ptr, "\n");
if (i == GTP_CONFIG_ORG_LENGTH - 1)
ptr += sprintf(ptr, "\n-------------\n");
}
ptr += sprintf(ptr, "\n");
/* Touch PID & VID */
ptr += sprintf(ptr, "==== GT1X Version Info ====\n");
gt1x_i2c_read(GTP_REG_VERSION, temp_data, 12);
ptr += sprintf(ptr, "ProductID: GT%c%c%c%c\n", temp_data[0],
temp_data[1], temp_data[2], temp_data[3]);
ptr += sprintf(ptr, "PatchID: %02X%02X\n", temp_data[4], temp_data[5]);
ptr += sprintf(ptr, "MaskID: %02X%02X\n", temp_data[7], temp_data[8]);
ptr += sprintf(ptr, "SensorID: %02X\n", temp_data[10] & 0x0F);
irq_desc = irq_to_desc(gt1x_i2c_client->irq);
if (irq_desc) {
ptr += sprintf(ptr, "IRQ: %d, irq_desc->disable-depth:%d\n",
gt1x_i2c_client->irq, irq_desc->depth);
}
*ppos += ptr - page;
return (ptr - page);
}
static ssize_t gt1x_debug_write_proc(struct file *file,
const char *buffer, size_t count, loff_t * ppos)
{
s32 ret = 0;
u8 buf[gt1x_cfg_length];
char mode_str[50] = { 0 };
int mode;
int cfg_len;
char arg1[50] = { 0 };
u8 temp_config[gt1x_cfg_length];
GTP_DEBUG("write count %ld\n", (unsigned long)count);
if (count > gt1x_cfg_length) {
GTP_ERROR("Too much data, buffer size: %d, data:%ld",
gt1x_cfg_length, (unsigned long)count);
return -EFAULT;
}
if (copy_from_user(buf, buffer, count)) {
GTP_ERROR("copy from user fail!");
return -EFAULT;
}
// send config
if (count == gt1x_cfg_length) {
memcpy(gt1x_config, buf, count);
ret = gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
if (ret < 0) {
GTP_ERROR("send gt1x_config failed.");
return -EFAULT;
}
gt1x_abs_x_max = (gt1x_config[RESOLUTION_LOC + 1] << 8) +
gt1x_config[RESOLUTION_LOC];
gt1x_abs_y_max = (gt1x_config[RESOLUTION_LOC + 3] << 8) +
gt1x_config[RESOLUTION_LOC + 2];
return count;
}
sscanf(buf, "%s %d", (char *)&mode_str, &mode);
//force clear gt1x_config
if (strcmp(mode_str, "clear_config") == 0) {
GTP_INFO("Force clear gt1x_config");
gt1x_send_cmd(GTP_CMD_CLEAR_CFG, 0);
return count;
}
if (strcmp(mode_str, "init") == 0) {
GTP_INFO("Init panel");
gt1x_init_panel();
return count;
}
if (strcmp(mode_str, "chip") == 0) {
GTP_INFO("Get chip type:");
gt1x_get_chip_type();
return count;
}
if (strcmp(mode_str, "int") == 0) {
if (mode == 0) {
GTP_INFO("Disable irq.");
gt1x_irq_disable();
} else {
GTP_INFO("Enable irq.");
gt1x_irq_enable();
}
return count;
}
if (strcmp(mode_str, "poweron") == 0) {
gt1x_power_switch(1);
return count;
}
if (strcmp(mode_str, "poweroff") == 0) {
gt1x_power_switch(0);
return count;
}
if (strcmp(mode_str, "version") == 0) {
gt1x_read_version(NULL);
return count;
}
if (strcmp(mode_str, "reset") == 0) {
gt1x_irq_disable();
gt1x_reset_guitar();
gt1x_irq_enable();
return count;
}
#ifdef CONFIG_GTP_CHARGER_SWITCH
if (strcmp(mode_str, "charger") == 0) {
gt1x_charger_config(mode);
return count;
}
#endif
sscanf(buf, "%s %s", (char *)&mode_str, (char *)&arg1);
if (strcmp(mode_str, "update") == 0) {
if(strcmp(arg1, "request") == 0){
GTP_INFO("Update firmware from request!");
gt1x_update_firmware(NULL);
}else{
gt1x_update_firmware(arg1);
}
return count;
}
if (strcmp(mode_str, "sendconfig") == 0) {
cfg_len = gt1x_parse_config(arg1, temp_config);
if (cfg_len < 0) {
return -1;
}
gt1x_send_cfg(temp_config, gt1x_cfg_length);
return count;
}
if (strcmp(mode_str, "debug_gesture") == 0) {
#ifdef CONFIG_GTP_GESTURE_WAKEUP
gt1x_gesture_debug(!!mode);
#endif
}
if (strcmp(mode_str, "force_update") == 0) {
update_info.force_update = !!mode;
}
return gt1x_debug_proc(buf, count);
}
static u8 ascii2hex(u8 a)
{
s8 value = 0;
if (a >= '0' && a <= '9') {
value = a - '0';
} else if (a >= 'A' && a <= 'F') {
value = a - 'A' + 0x0A;
} else if (a >= 'a' && a <= 'f') {
value = a - 'a' + 0x0A;
} else {
value = 0xff;
}
return value;
}
int gt1x_parse_config(char *filename, u8 * config)
{
mm_segment_t old_fs;
struct file *fp = NULL;
u8 *buf;
int i;
int len;
int cur_len = -1;
u8 high, low;
old_fs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open(filename, O_RDONLY, 0);
if (IS_ERR(fp)) {
GTP_ERROR("Open config file error!(file: %s)", filename);
goto parse_cfg_fail1;
}
len = fp->f_op->llseek(fp, 0, SEEK_END);
if (len > gt1x_cfg_length * 6 || len < gt1x_cfg_length) {
GTP_ERROR("Config is invalid!(length: %d)", len);
goto parse_cfg_fail2;
}
buf = (u8 *) kzalloc(len, GFP_KERNEL);
if (buf == NULL) {
GTP_ERROR("Allocate memory failed!(size: %d)", len);
goto parse_cfg_fail2;
}
fp->f_op->llseek(fp, 0, SEEK_SET);
if (fp->f_op->read(fp, (char *)buf, len, &fp->f_pos) != len) {
GTP_ERROR("Read %d bytes from file failed!", len);
}
GTP_INFO("Parse config file: %s (%d bytes)", filename, len);
for (i = 0, cur_len = 0; i < len && cur_len < gt1x_cfg_length;) {
if (buf[i] == ' ' || buf[i] == '\r' || buf[i] == '\n' || buf[i] == ',') {
i++;
continue;
}
if (buf[i] == '0' && (buf[i + 1] == 'x' || buf[i + 1] == 'X')) {
high = ascii2hex(buf[i + 2]);
low = ascii2hex(buf[i + 3]);
if (high != 0xFF && low != 0xFF) {
config[cur_len++] = (high << 4) + low;
i += 4;
continue;
}
}
GTP_ERROR("Illegal config file!");
cur_len = -1;
break;
}
if (cur_len < GTP_CONFIG_MIN_LENGTH || config[cur_len - 1] != 0x01) {
cur_len = -1;
} else {
for (i = 0; i < cur_len; i++) {
if (i % 10 == 0) {
printk("\n<<GTP-DBG>>:");
}
printk("0x%02x,", config[i]);
}
printk("\n");
}
kfree(buf);
parse_cfg_fail2:
filp_close(fp, NULL);
parse_cfg_fail1:
set_fs(old_fs);
return cur_len;
}
s32 _do_i2c_read(struct i2c_msg * msgs, u16 addr, u8 * buffer, s32 len)
{
s32 ret = -1;
s32 pos = 0;
s32 data_length = len;
s32 transfer_length = 0;
u8 *data = NULL;
u16 address = addr;
data = (u8 *) kmalloc(IIC_MAX_TRANSFER_SIZE <
(len + GTP_ADDR_LENGTH) ?
IIC_MAX_TRANSFER_SIZE :
(len + GTP_ADDR_LENGTH), GFP_KERNEL);
if (data == NULL) {
return ERROR_MEM;
}
msgs[1].buf = data;
while (pos != data_length) {
if ((data_length - pos) > IIC_MAX_TRANSFER_SIZE) {
transfer_length = IIC_MAX_TRANSFER_SIZE;
} else {
transfer_length = data_length - pos;
}
msgs[0].buf[0] = (address >> 8) & 0xFF;
msgs[0].buf[1] = address & 0xFF;
msgs[1].len = transfer_length;
ret = i2c_transfer(gt1x_i2c_client->adapter, msgs, 2);
if (ret != 2) {
GTP_ERROR("I2c Transfer error! (%d)", ret);
kfree(data);
return ERROR_IIC;
}
memcpy(&buffer[pos], msgs[1].buf, transfer_length);
pos += transfer_length;
address += transfer_length;
}
kfree(data);
return 0;
}
s32 _do_i2c_write(struct i2c_msg * msg, u16 addr, u8 * buffer, s32 len)
{
s32 ret = -1;
s32 pos = 0;
s32 data_length = len;
s32 transfer_length = 0;
u8 *data = NULL;
u16 address = addr;
data = (u8 *) kmalloc(IIC_MAX_TRANSFER_SIZE <
(len + GTP_ADDR_LENGTH) ? IIC_MAX_TRANSFER_SIZE :
(len + GTP_ADDR_LENGTH), GFP_KERNEL);
if (data == NULL) {
return ERROR_MEM;
}
msg->buf = data;
while (pos != data_length) {
if ((data_length - pos) > (IIC_MAX_TRANSFER_SIZE - GTP_ADDR_LENGTH)) {
transfer_length = IIC_MAX_TRANSFER_SIZE - GTP_ADDR_LENGTH;
} else {
transfer_length = data_length - pos;
}
msg->buf[0] = (address >> 8) & 0xFF;
msg->buf[1] = address & 0xFF;
msg->len = transfer_length + GTP_ADDR_LENGTH;
memcpy(&msg->buf[GTP_ADDR_LENGTH], &buffer[pos], transfer_length);
ret = i2c_transfer(gt1x_i2c_client->adapter, msg, 1);
if (ret != 1) {
GTP_ERROR("I2c transfer error! (%d)", ret);
kfree(data);
return ERROR_IIC;
}
pos += transfer_length;
address += transfer_length;
}
kfree(data);
return 0;
}
/**
* gt1x_i2c_read_dbl_check - read twice and double check
* @addr: register address
* @buffer: data buffer
* @len: bytes to read
* Return <0: i2c error, 0: ok, 1:fail
*/
s32 gt1x_i2c_read_dbl_check(u16 addr, u8 * buffer, s32 len)
{
u8 buf[16] = { 0 };
u8 confirm_buf[16] = { 0 };
int ret;
if (len > 16) {
GTP_ERROR("i2c_read_dbl_check length %d is too long, exceed %zu",
len, sizeof(buf));
return ERROR;
}
memset(buf, 0xAA, sizeof(buf));
ret = gt1x_i2c_read(addr, buf, len);
if (ret < 0) {
return ret;
}
msleep(5);
memset(confirm_buf, 0, sizeof(confirm_buf));
ret = gt1x_i2c_read(addr, confirm_buf, len);
if (ret < 0) {
return ret;
}
if (!memcmp(buf, confirm_buf, len)) {
memcpy(buffer, confirm_buf, len);
return 0;
}
GTP_ERROR("i2c read 0x%04X, %d bytes, double check failed!", addr, len);
return 1;
}
/**
* gt1x_get_info - Get information from ic, such as resolution and
* int trigger type
* Return <0: i2c failed, 0: i2c ok
*/
s32 gt1x_get_info(void)
{
u8 opr_buf[4] = { 0 };
s32 ret = 0;
ret = gt1x_i2c_read(GTP_REG_CONFIG_DATA + 1, opr_buf, 4);
if (ret < 0) {
return ret;
}
gt1x_abs_x_max = (opr_buf[1] << 8) + opr_buf[0];
gt1x_abs_y_max = (opr_buf[3] << 8) + opr_buf[2];
ret = gt1x_i2c_read(GTP_REG_CONFIG_DATA + 6, opr_buf, 1);
if (ret < 0) {
return ret;
}
gt1x_int_type = opr_buf[0] & 0x03;
GTP_INFO("X_MAX = %d, Y_MAX = %d, TRIGGER = 0x%02x",
gt1x_abs_x_max, gt1x_abs_y_max, gt1x_int_type);
return 0;
}
/**
* gt1x_send_cfg - Send gt1x_config Function.
* @config: pointer of the configuration array.
* @cfg_len: length of configuration array.
* Return 0--success,non-0--fail.
*/
s32 gt1x_send_cfg(u8 * config, int cfg_len)
{
#ifdef CONFIG_GTP_DRIVER_SEND_CFG
static DEFINE_MUTEX(mutex_cfg);
int i;
s32 ret = 0;
s32 retry = 0;
u16 checksum = 0;
if (update_info.status) {
GTP_DEBUG("Ignore cfg during fw update.");
return -1;
}
mutex_lock(&mutex_cfg);
GTP_DEBUG("Driver send config, length:%d", cfg_len);
if (cfg_len != GTP_CONFIG_ORG_LENGTH
&& cfg_len != GTP_CONFIG_ORG_LENGTH + GTP_CONFIG_EXT_LENGTH) {
GTP_ERROR("Invalid config length");
mutex_unlock(&mutex_cfg);
return -1;
}
/* Extends config */
if (config[GTP_REG_EXT_CFG_FLAG - GTP_REG_CONFIG_DATA] & 0x40) {
int total_len = GTP_CONFIG_EXT_LENGTH + GTP_CONFIG_ORG_LENGTH;
/* bit0 of first byte of extended config
* must be set to 1, otherwise the firmware
* will not accept the extended config data
*/
config[GTP_CONFIG_ORG_LENGTH] |= 0x01;
GTP_DEBUG("ext_cfg, debug info:%X", config[GTP_CONFIG_ORG_LENGTH]);
for (i = GTP_CONFIG_ORG_LENGTH; i < total_len - 2; i+=2)
checksum += (config[i] << 8)
+ config[i + 1];
if (!checksum){
GTP_ERROR("Invalid config, all of the bytes is zero!");
mutex_unlock(&mutex_cfg);
return -1;
}
checksum = 0 - checksum;
config[total_len - 2] = (checksum >> 8) & 0xFF;
config[total_len - 1] = checksum & 0xFF;
do {
ret = gt1x_i2c_write(GTP_REG_EXT_CONFIG,
&config[GTP_CONFIG_ORG_LENGTH], GTP_CONFIG_EXT_LENGTH);
} while (ret < 0 && retry++ < 3) ;
if (ret < 0) {
GTP_ERROR("Send ext_config failed!");
mutex_unlock(&mutex_cfg);
return -1;
} else {
GTP_DEBUG("Send ext_config successfully");
}
}
/* Original Config */
cfg_len = GTP_CONFIG_ORG_LENGTH;
for (i = 0, checksum = 0; i < cfg_len - 3; i += 2)
checksum += (config[i] << 8) + config[i + 1];
if (!checksum) {
GTP_ERROR("Invalid config, all of the bytes is zero!");
mutex_unlock(&mutex_cfg);
return -1;
}
checksum = 0 - checksum;
GTP_DEBUG("Config checksum: 0x%04X", checksum);
config[cfg_len - 3] = (checksum >> 8) & 0xFF;
config[cfg_len - 2] = checksum & 0xFF;
config[cfg_len - 1] = 0x01;
retry = 0;
while (retry++ < 3) {
ret = gt1x_i2c_write(GTP_REG_CONFIG_DATA, config, cfg_len);
if (!ret) {
/* at least 200ms, wait for storing config into flash. */
msleep(200);
mutex_unlock(&mutex_cfg);
GTP_DEBUG("Send config successfully!");
return 0;
}
}
GTP_ERROR("Send config failed!");
mutex_unlock(&mutex_cfg);
return ret;
#endif
return 0;
}
#ifdef CONFIG_OF
/**
* gt1x_parse_tp_config - get config data of touch panel
* @of_node: device node
* @cfg_name: config name prefix, e.g. "default-config"
* @sensor_id: config group ID
* @config: config data buffer
* return: > 0 size of config data, <0 error
*/
int gt1x_find_tp_config(struct device *dev,
char *cfg_name, u8 sensor_id, u8 *config)
{
struct property *prop;
char name[32];
u8 extcfg_flag;
int size, requ_size;
if (!dev || !dev->of_node || !cfg_name ||
!config || sensor_id > 6)
return -EINVAL;
snprintf(name, sizeof(name), "goodix,%s%d", cfg_name, sensor_id);
prop = of_find_property(dev->of_node, name, &size);
if (!prop || !prop->value || size < GTP_CONFIG_ORG_LENGTH) {
GTP_ERROR("Property %s not found", name);
return -ENOENT;
}
extcfg_flag = ((u8 *)prop->value)[GTP_REG_EXT_CFG_FLAG -
GTP_REG_CONFIG_DATA];
if (extcfg_flag & 0x40) /* has extended config data*/
requ_size = GTP_CONFIG_ORG_LENGTH + GTP_CONFIG_EXT_LENGTH;
else
requ_size = GTP_CONFIG_ORG_LENGTH;
if (size != requ_size) {
GTP_ERROR("Invalid config size:%d", size);
return -EINVAL;
}
memcpy(config, prop->value, size);
GTP_INFO("Find %s, size:%d, ver:%02xh ",
name, size, config[0]);
return size;
}
#else
int gt1x_find_tp_config(struct device *dev,
char *cfg_name, u8 sensor_id, u8 *config)
{
return -ENOENT;
}
#endif
/**
* gt1x_init_panel - Prepare config data for touch ic,
* don't call this function after initialization.
*
* Return 0--success,<0 --fail.
*/
s32 gt1x_init_panel(void)
{
u16 cfg_len = 0;
s32 ret = 0;
#ifdef CONFIG_GTP_DRIVER_SEND_CFG
#ifdef CONFIG_MTK_PLATFORM
struct device *dev = tpd->tpd_dev;
#else
struct device *dev = >1x_i2c_client->dev;
#endif
cfg_len = gt1x_find_tp_config(dev, "default-config",
gt1x_version.sensor_id, gt1x_config);
if (cfg_len < 0) {
GTP_ERROR("Failed to obtain config data:%d", cfg_len);
return -EINVAL;
}
/* clear the flag, avoid failure when send
* the_config of driver. */
gt1x_config[0] &= 0x7F;
#ifdef CONFIG_GTP_CUSTOM_CFG
gt1x_config[RESOLUTION_LOC] = (u8) GTP_MAX_WIDTH;
gt1x_config[RESOLUTION_LOC + 1] = (u8) (GTP_MAX_WIDTH >> 8);
gt1x_config[RESOLUTION_LOC + 2] = (u8) GTP_MAX_HEIGHT;
gt1x_config[RESOLUTION_LOC + 3] = (u8) (GTP_MAX_HEIGHT >> 8);
if (GTP_INT_TRIGGER == 0) { /* RISING */
gt1x_config[TRIGGER_LOC] &= 0xfe;
} else if (GTP_INT_TRIGGER == 1) { /* FALLING */
gt1x_config[TRIGGER_LOC] |= 0x01;
}
set_reg_bit(gt1x_config[MODULE_SWITCH3_LOC], 5, !gt1x_wakeup_level);
#endif /* END GTP_CUSTOM_CFG */
#else /* DRIVER NOT SEND CONFIG */
cfg_len = GTP_CONFIG_ORG_LENGTH;
ret = gt1x_i2c_read(GTP_REG_CONFIG_DATA, gt1x_config, cfg_len);
if (ret < 0)
return ret;
if (gt1x_config[GTP_REG_EXT_CFG_FLAG - GTP_REG_CONFIG_DATA] & 0x40) {
ret = gt1x_i2c_read(GTP_REG_EXT_CONFIG,
>1x_config[cfg_len], GTP_CONFIG_EXT_LENGTH);
if (ret < 0)
return ret;
cfg_len += GTP_CONFIG_EXT_LENGTH;
}
#endif /* END GTP_DRIVER_SEND_CFG */
/* match resolution when gt1x_abs_x_max & gt1x_abs_y_max
* have been set already */
if ((gt1x_abs_x_max == 0) && (gt1x_abs_y_max == 0)) {
gt1x_abs_x_max = (gt1x_config[RESOLUTION_LOC + 1] << 8) +
gt1x_config[RESOLUTION_LOC];
gt1x_abs_y_max = (gt1x_config[RESOLUTION_LOC + 3] << 8) +
gt1x_config[RESOLUTION_LOC + 2];
gt1x_int_type = (gt1x_config[TRIGGER_LOC]) & 0x03;
gt1x_wakeup_level = !(gt1x_config[MODULE_SWITCH3_LOC] & 0x20);
} else {
gt1x_config[RESOLUTION_LOC] = (u8) gt1x_abs_x_max;
gt1x_config[RESOLUTION_LOC + 1] = (u8) (gt1x_abs_x_max >> 8);
gt1x_config[RESOLUTION_LOC + 2] = (u8) gt1x_abs_y_max;
gt1x_config[RESOLUTION_LOC + 3] = (u8) (gt1x_abs_y_max >> 8);
set_reg_bit(gt1x_config[MODULE_SWITCH3_LOC], 5, !gt1x_wakeup_level);
gt1x_config[TRIGGER_LOC] = (gt1x_config[TRIGGER_LOC] & 0xFC) | gt1x_int_type;
}
GTP_INFO("X_MAX=%d,Y_MAX=%d,TRIGGER=0x%02x,WAKEUP_LEVEL=%d",
gt1x_abs_x_max, gt1x_abs_y_max, gt1x_int_type, gt1x_wakeup_level);
gt1x_cfg_length = cfg_len;
ret = gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
return ret;
}
void gt1x_select_addr(void)
{
GTP_GPIO_OUTPUT(GTP_RST_PORT, 0);
#ifdef CONFIG_GTP_INT_SEL_SYNC
GTP_GPIO_OUTPUT(GTP_INT_PORT, gt1x_i2c_client->addr == 0x14);
#endif
usleep_range(3000, 3030);
GTP_GPIO_OUTPUT(GTP_RST_PORT, 1);
usleep_range(2000, 2030);
}
static s32 gt1x_set_reset_status(void)
{
/* 0x8040 ~ 0x8043 */
u8 value[] = {0xAA, 0x00, 0x56, 0xAA};
int ret;
ret = gt1x_i2c_write(GTP_REG_CMD + 1, &value[1], 3);
if (ret < 0)
return ret;
return gt1x_i2c_write(GTP_REG_CMD, value, 1);
}
#ifdef CONFIG_GTP_INCELL_PANEL
int gt1x_write_and_readback(u16 addr, u8 * buffer, s32 len)
{
int ret;
u8 d[len];
ret = gt1x_i2c_write(addr, buffer, len);
if (ret < 0)
return -1;
ret = gt1x_i2c_read(addr, d, len);
if (ret < 0 || memcmp(buffer, d, len))
return -1;
return 0;
}
int gt1x_incell_reset(void)
{
#define RST_RETRY 5
int ret, retry = RST_RETRY;
u8 d[2];
do {
/* select i2c address */
gt1x_select_addr();
/* test i2c */
ret = gt1x_i2c_read(0x4220, d, 1);
} while (--retry && ret < 0);
if (ret < 0) {
return -1;
}
/* Important! */
usleep_range(10000, 11000); /* delay 10ms */
/* Stop cpu of the touch ic */
retry = RST_RETRY;
do {
d[0] = 0x0C;
ret = gt1x_write_and_readback(0x4180, d, 1);
} while (--retry && ret < 0);
if (ret < 0) {
GTP_ERROR("Hold error.");
return -1;
}
/* skip sensor id check. [start] */
retry = RST_RETRY;
do {
d[0] = 0x00;
ret = gt1x_write_and_readback(0x4305, d, 1);
if (ret < 0)
continue;
d[0] = 0x2B;
d[1] = 0x24;
ret = gt1x_write_and_readback(0x42c4, d, 2);
if (ret < 0)
continue;
d[0] = 0xE1;
d[1] = 0xD3;
ret = gt1x_write_and_readback(0x42e4, d, 2);
if (ret < 0)
continue;
d[0] = 0x01;
ret = gt1x_write_and_readback(0x4305, d, 1);
if (ret < 0)
continue;
else
break;
} while (--retry );
if (!retry)
return -1;
/* skip sensor id check. [end] */
/* release hold of cpu */
retry = RST_RETRY;
do {
d[0] = 0x00;
ret = gt1x_write_and_readback(0x4180, d, 1);
} while (--retry && ret < 0);
if (ret < 0)
return -1;
return 0;
}
#endif
s32 gt1x_reset_guitar(void)
{
int ret;
GTP_INFO("GTP RESET!");
#ifdef CONFIG_GTP_INCELL_PANEL
ret = gt1x_incell_reset();
if (ret < 0)
return ret;
#else
gt1x_select_addr();
usleep_range(8000, 8000); //must >= 6ms
#endif
/* INT gpio is used to select i2c slave address
* during hardware reset, and INT synchronization
* flow informs the firmware that address selection
* has finished,if the kernel restricts the output
* of the gpio tied to IRQ line(kernel3.13 and
* later version), do the following steps:
* 1) select N to CONFIG_GTP_INT_SEL_SYNC
* in menuconfig.
* 2) config pinctrl dts, pull-up INT gpio.
* 3) chose falling-edge IRQ trigger type.
*/
#ifdef CONFIG_GTP_INT_SEL_SYNC
GTP_GPIO_OUTPUT(GTP_INT_PORT, 0);
msleep(50);
GTP_GPIO_AS_INT(GTP_INT_PORT);
#else
/* firmware init */
msleep(50);
#endif
ret = gt1x_set_reset_status();
return ret;
}
/**
* gt1x_read_version - Read gt1x version info.
* @ver_info: address to store version info
* Return 0-succeed.
*/
s32 gt1x_read_version(struct gt1x_version_info * ver_info)
{
s32 ret = -1;
u8 buf[12] = { 0 };
u32 mask_id = 0;
u32 patch_id = 0;
u8 product_id[5] = { 0 };
u8 sensor_id = 0;
u8 match_opt = 0;
int i, retry = 3;
u8 checksum = 0;
GTP_DEBUG_FUNC();
while (retry--) {
ret = gt1x_i2c_read_dbl_check(GTP_REG_VERSION, buf, sizeof(buf));
if (!ret) {
checksum = 0;
for (i = 0; i < sizeof(buf); i++) {
checksum += buf[i];
}
if (checksum == 0 && /* first 3 bytes must be number or char */
IS_NUM_OR_CHAR(buf[0]) && IS_NUM_OR_CHAR(buf[1]) &&
IS_NUM_OR_CHAR(buf[2]) && buf[10] != 0xFF) {
/*sensor id == 0xFF, retry */
break;
} else {
GTP_ERROR("Read version failed!(checksum error)");
}
} else {
GTP_ERROR("Read version failed!");
}
GTP_DEBUG("Read version : %d", retry);
msleep(100);
}
if (retry <= 0) {
if (ver_info)
ver_info->sensor_id = 0;
return -1;
}
mask_id = (u32) ((buf[7] << 16) | (buf[8] << 8) | buf[9]);
patch_id = (u32) ((buf[4] << 16) | (buf[5] << 8) | buf[6]);
memcpy(product_id, buf, 4);
sensor_id = buf[10] & 0x0F;
match_opt = (buf[10] >> 4) & 0x0F;
GTP_INFO("IC VERSION:GT%s_%06X(Patch)_%04X(Mask)_%02X(SensorID)",
product_id, patch_id, mask_id >> 8, sensor_id);
if (ver_info != NULL) {
ver_info->mask_id = mask_id;
ver_info->patch_id = patch_id;
memcpy(ver_info->product_id, product_id, 5);
ver_info->sensor_id = sensor_id;
ver_info->match_opt = match_opt;
}
return 0;
}
/**
* gt1x_get_chip_type - get chip type .
*
* different chip synchronize in different way,
*/
s32 gt1x_get_chip_type(void)
{
u8 opr_buf[4] = { 0x00 };
u8 gt1x_data[] = { 0x02, 0x08, 0x90, 0x00 };
u8 gt9l_data[] = { 0x03, 0x10, 0x90, 0x00 };