-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoodix.c
3500 lines (3090 loc) · 92.8 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
/*
* Goodix GT9xx touchscreen driver
*
* Copyright (C) 2010 - 2016 Goodix. Ltd.
*
* 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: 2.4.0.1
* Release Date: 2016/10/26
*/
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <asm/uaccess.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/gpio.h>
#ifdef CONFIG_OF
#include <linux/of_gpio.h>
#include <linux/regulator/consumer.h>
#endif
#ifdef CONFIG_FB
#include <linux/notifier.h>
#include <linux/fb.h>
#endif
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#define GTP_CONFIG_OF
//***************************PART1:ON/OFF define*******************************
#define GTP_CUSTOM_CFG 1
#define GTP_CHANGE_X2Y 0 //swap x y
#define GTP_DRIVER_SEND_CFG 0 //driver send config
#define GTP_HAVE_TOUCH_KEY 0
#define GTP_POWER_CTRL_SLEEP 0 //power off when suspend
#define GTP_ICS_SLOT_REPORT 0 // slot protocol
#define GTP_AUTO_UPDATE 0 // auto update fw by .bin file as default
#define GTP_HEADER_FW_UPDATE 0 // auto update fw by gtp_default_FW in gt9xx_firmware.h, function together with GTP_AUTO_UPDATE
#define GTP_AUTO_UPDATE_CFG 0 // auto update config by .cfg file, function together with GTP_AUTO_UPDATE
#define GTP_COMPATIBLE_MODE 0 // compatible with GT9XXF
#define GTP_CREATE_WR_NODE 0
#define GTP_ESD_PROTECT 0 // esd protection with a cycle of 2 seconds
#define GTP_WITH_HOVER 0 //pen surrport hover or not 1:enable 0 disable
#define GTP_GESTURE_WAKEUP 1 // gesture wakeup
#define GTP_DEBUG_ON 0
#define GTP_DEBUG_ARRAY_ON 0
#define GTP_DEBUG_FUNC_ON 0
#if GTP_COMPATIBLE_MODE
typedef enum
{
CHIP_TYPE_GT9 = 0,
CHIP_TYPE_GT9F = 1,
} CHIP_TYPE_T;
#endif
struct goodix_ts_data {
spinlock_t irq_lock;
struct i2c_client *client;
struct input_dev *input_dev;
struct hrtimer timer;
struct work_struct work;
s32 irq_is_disable;
s32 use_irq;
u16 abs_x_max;
u16 abs_y_max;
u8 max_touch_num;
u8 int_trigger_type;
u8 green_wake_mode;
u8 enter_update;
u8 gtp_is_suspend;
u8 gtp_rawdiff_mode;
int gtp_cfg_len;
u8 fw_error;
u8 pnl_init_error;
#if defined(CONFIG_FB)
struct notifier_block notifier;
#elif defined(CONFIG_HAS_EARLYSUSPEND)
struct early_suspend early_suspend;
#endif
#if GTP_WITH_HOVER
struct input_dev *pen_dev;
#endif
#if GTP_ESD_PROTECT
spinlock_t esd_lock;
u8 esd_running;
s32 clk_tick_cnt;
#endif
#if GTP_COMPATIBLE_MODE
u16 bak_ref_len;
s32 ref_chk_fs_times;
s32 clk_chk_fs_times;
CHIP_TYPE_T chip_type;
u8 rqst_processing;
u8 is_950;
#endif
};
extern u16 show_len;
extern u16 total_len;
extern int gtp_rst_gpio;
extern int gtp_int_gpio;
//*************************** PART2:TODO define **********************************
// STEP_1(REQUIRED): Define Configuration Information Group(s)
// Sensor_ID Map:
/* sensor_opt1 sensor_opt2 Sensor_ID
GND GND 0
VDDIO GND 1
NC GND 2
GND NC/300K 3
VDDIO NC/300K 4
NC NC/300K 5
*/
// TODO: define your own default or for Sensor_ID == 0 config here.
// The predefined one is just a sample config, which is not suitable for your tp in most cases.
#define CTP_CFG_GROUP0 {\
0x42,0xD0,0x02,0x00,0x05,0x05,0x75,0x01,0x01,0x0F,0x24,\
0x0F,0x64,0x3C,0x03,0x05,0x00,0x00,0x00,0x02,0x00,0x00,\
0x00,0x16,0x19,0x1C,0x14,0x8C,0x0E,0x0E,0x24,0x00,0x31,\
0x0D,0x00,0x00,0x00,0x83,0x33,0x1D,0x00,0x41,0x00,0x00,\
0x00,0x00,0x00,0x08,0x0A,0x00,0x2B,0x1C,0x3C,0x94,0xD5,\
0x03,0x08,0x00,0x00,0x04,0x93,0x1E,0x00,0x82,0x23,0x00,\
0x74,0x29,0x00,0x69,0x2F,0x00,0x5F,0x37,0x00,0x5F,0x20,\
0x40,0x60,0x00,0xF0,0x40,0x30,0x55,0x50,0x27,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x19,0x00,0x00,\
0x50,0x50,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,\
0x14,0x16,0x18,0x1A,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,\
0x1E,0x1F,0x20,0x21,0x22,0x24,0x26,0x28,0x29,0x2A,0x1C,\
0x18,0x16,0x14,0x13,0x12,0x10,0x0F,0x0C,0x0A,0x08,0x06,\
0x04,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x9C,0x01\
}
// TODO: define your config for Sensor_ID == 1 here, if needed
#define CTP_CFG_GROUP1 {\
}
// TODO: define your config for Sensor_ID == 2 here, if needed
#define CTP_CFG_GROUP2 {\
0x48,0xD0,0x02,0x00,0x05,0x05,0x75,0x01,0x01,0x0F,0x24,\
0x0F,0x64,0x3C,0x03,0x05,0x00,0x00,0x00,0x02,0x00,0x00,\
0x00,0x16,0x19,0x1C,0x14,0x8C,0x0E,0x0E,0x24,0x00,0x31,\
0x0D,0x00,0x00,0x00,0x83,0x33,0x1D,0x00,0x41,0x00,0x00,\
0x3C,0x0A,0x14,0x08,0x0A,0x00,0x2B,0x1C,0x3C,0x94,0xD5,\
0x03,0x08,0x00,0x00,0x04,0x93,0x1E,0x00,0x82,0x23,0x00,\
0x74,0x29,0x00,0x69,0x2F,0x00,0x5F,0x37,0x00,0x5F,0x20,\
0x40,0x60,0x00,0xF0,0x40,0x30,0x55,0x50,0x27,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x19,0x00,0x00,\
0x50,0x50,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,\
0x14,0x16,0x18,0x1A,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,\
0x1E,0x1F,0x20,0x21,0x22,0x24,0x26,0x28,0x29,0x2A,0x1C,\
0x18,0x16,0x14,0x13,0x12,0x10,0x0F,0x0C,0x0A,0x08,0x06,\
0x04,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x01\
}
// TODO: define your config for Sensor_ID == 3 here, if needed
#define CTP_CFG_GROUP3 {\
}
// TODO: define your config for Sensor_ID == 4 here, if needed
#define CTP_CFG_GROUP4 {\
}
// TODO: define your config for Sensor_ID == 5 here, if needed
#define CTP_CFG_GROUP5 {\
}
// STEP_2(REQUIRED): Customize your I/O ports & I/O operations
#define GTP_RST_PORT 16//S5PV210_GPJ3(6)
#define GTP_INT_PORT 17//S5PV210_GPH1(3)
#define GTP_GPIO_AS_INPUT(pin) do{\
gpio_direction_input(pin);\
}while(0)
#define GTP_GPIO_AS_INT(pin) do{\
GTP_GPIO_AS_INPUT(pin);\
}while(0)
#define GTP_GPIO_GET_VALUE(pin) gpio_get_value(pin)
#define GTP_GPIO_OUTPUT(pin,level) gpio_direction_output(pin,level)
#define GTP_GPIO_REQUEST(pin, label) gpio_request(pin, label)
#define GTP_GPIO_FREE(pin) gpio_free(pin)
#define GTP_IRQ_TAB {IRQ_TYPE_EDGE_RISING, IRQ_TYPE_EDGE_FALLING, IRQ_TYPE_LEVEL_LOW, IRQ_TYPE_LEVEL_HIGH}
// STEP_3(optional): Specify your special config info if needed
#if GTP_CUSTOM_CFG
#define GTP_MAX_HEIGHT 800
#define GTP_MAX_WIDTH 1280
#define GTP_INT_TRIGGER 0 // 0: Rising 1: Falling
#else
#define GTP_MAX_HEIGHT 4096
#define GTP_MAX_WIDTH 4096
#define GTP_INT_TRIGGER 1
#endif
#define GTP_MAX_TOUCH 10
// STEP_4(optional): If keys are available and reported as keys, config your key info here
#if GTP_HAVE_TOUCH_KEY
#define GTP_KEY_TAB {KEY_MENU, KEY_HOME, KEY_BACK, KEY_HOMEPAGE, KEY_F1, KEY_F2, KEY_F3}
#endif
//***************************PART3:OTHER define*********************************
#define GTP_DRIVER_VERSION "V2.4.0.1<2016/10/26>"
#define GTP_I2C_NAME "Goodix-TS"
#define GT91XX_CONFIG_PROC_FILE "gt9xx_config"
#define GTP_POLL_TIME 10
#define GTP_ADDR_LENGTH 2
#define GTP_CONFIG_MIN_LENGTH 186
#define GTP_CONFIG_MAX_LENGTH 240
#define FAIL 0
#define SUCCESS 1
#define SWITCH_OFF 0
#define SWITCH_ON 1
//******************** For GT9XXF Start **********************//
#define GTP_REG_BAK_REF 0x99D0
#define GTP_REG_MAIN_CLK 0x8020
#define GTP_REG_CHIP_TYPE 0x8000
#define GTP_REG_HAVE_KEY 0x804E
#define GTP_REG_MATRIX_DRVNUM 0x8069
#define GTP_REG_MATRIX_SENNUM 0x806A
#define GTP_FL_FW_BURN 0x00
#define GTP_FL_ESD_RECOVERY 0x01
#define GTP_FL_READ_REPAIR 0x02
#define GTP_BAK_REF_SEND 0
#define GTP_BAK_REF_STORE 1
#define CFG_LOC_DRVA_NUM 29
#define CFG_LOC_DRVB_NUM 30
#define CFG_LOC_SENS_NUM 31
#define GTP_CHK_FW_MAX 40
#define GTP_CHK_FS_MNT_MAX 300
#define GTP_BAK_REF_PATH "/data/gtp_ref.bin"
#define GTP_MAIN_CLK_PATH "/data/gtp_clk.bin"
#define GTP_RQST_CONFIG 0x01
#define GTP_RQST_BAK_REF 0x02
#define GTP_RQST_RESET 0x03
#define GTP_RQST_MAIN_CLOCK 0x04
#define GTP_RQST_RESPONDED 0x00
#define GTP_RQST_IDLE 0xFF
//******************** For GT9XXF End **********************//
// Registers define
#define GTP_READ_COOR_ADDR 0x814E
#define GTP_REG_SLEEP 0x8040
#define GTP_REG_SENSOR_ID 0x814A
#define GTP_REG_CONFIG_DATA 0x8047
#define GTP_REG_VERSION 0x8140
#define RESOLUTION_LOC 3
#define TRIGGER_LOC 8
#define CFG_GROUP_LEN(p_cfg_grp) (sizeof(p_cfg_grp) / sizeof(p_cfg_grp[0]))
// Log define
#define GTP_INFO(fmt,arg...) printk("<<-GTP-INFO->> "fmt"\n",##arg)
#define GTP_ERROR(fmt,arg...) printk("<<-GTP-ERROR->> "fmt"\n",##arg)
#define GTP_DEBUG(fmt,arg...) do{\
if(GTP_DEBUG_ON)\
printk("<<-GTP-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
}while(0)
#define GTP_DEBUG_ARRAY(array, num) do{\
s32 i;\
u8* a = array;\
if(GTP_DEBUG_ARRAY_ON)\
{\
printk("<<-GTP-DEBUG-ARRAY->>\n");\
for (i = 0; i < (num); i++)\
{\
printk("%02x ", (a)[i]);\
if ((i + 1 ) %10 == 0)\
{\
printk("\n");\
}\
}\
printk("\n");\
}\
}while(0)
#define GTP_DEBUG_FUNC() do{\
if(GTP_DEBUG_FUNC_ON)\
printk("<<-GTP-FUNC->> Func:%s@Line:%d\n",__func__,__LINE__);\
}while(0)
#define GTP_SWAP(x, y) do{\
typeof(x) z = x;\
x = y;\
y = z;\
}while (0)
//*****************************End of Part III********************************
#ifdef CONFIG_OF
int gtp_parse_dt_cfg(struct device *dev, u8 *cfg, int *cfg_len, u8 sid);
#endif
#if GTP_ICS_SLOT_REPORT
#include <linux/input/mt.h>
#endif
static const char *goodix_ts_name = "goodix-ts";
static const char *goodix_input_phys = "input/ts";
static struct workqueue_struct *goodix_wq;
struct i2c_client * i2c_connect_client = NULL;
int gtp_rst_gpio;
int gtp_int_gpio;
u8 config[GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH]
= {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
#if GTP_HAVE_TOUCH_KEY
static const u16 touch_key_array[] = GTP_KEY_TAB;
#define GTP_MAX_KEY_NUM (sizeof(touch_key_array)/sizeof(touch_key_array[0]))
#if GTP_DEBUG_ON
static const int key_codes[] = {KEY_HOME, KEY_BACK, KEY_MENU, KEY_HOMEPAGE, KEY_F1, KEY_F2, KEY_F3};
static const char *key_names[] = {"Key_Home", "Key_Back", "Key_Menu", "Key_Homepage", "KEY_F1", "KEY_F2", "KEY_F3"};
#endif
#endif
static s8 gtp_i2c_test(struct i2c_client *client);
void gtp_reset_guitar(struct i2c_client *client, s32 ms);
s32 gtp_send_cfg(struct i2c_client *client);
void gtp_int_sync(s32 ms);
static ssize_t gt91xx_config_read_proc(struct file *, char __user *, size_t, loff_t *);
static ssize_t gt91xx_config_write_proc(struct file *, const char __user *, size_t, loff_t *);
static struct proc_dir_entry *gt91xx_config_proc = NULL;
static const struct file_operations config_proc_ops = {
.owner = THIS_MODULE,
.read = gt91xx_config_read_proc,
.write = gt91xx_config_write_proc,
};
static int gtp_register_powermanger(struct goodix_ts_data *ts);
static int gtp_unregister_powermanger(struct goodix_ts_data *ts);
#if GTP_CREATE_WR_NODE
extern s32 init_wr_node(struct i2c_client*);
extern void uninit_wr_node(void);
#endif
#if GTP_AUTO_UPDATE
extern u8 gup_init_update_proc(struct goodix_ts_data *);
#endif
#if GTP_ESD_PROTECT
static struct delayed_work gtp_esd_check_work;
static struct workqueue_struct * gtp_esd_check_workqueue = NULL;
static void gtp_esd_check_func(struct work_struct *);
static s32 gtp_init_ext_watchdog(struct i2c_client *client);
void gtp_esd_switch(struct i2c_client *, s32);
#endif
//*********** For GT9XXF Start **********//
#if GTP_COMPATIBLE_MODE
extern s32 i2c_read_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
extern s32 i2c_write_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
extern s32 gup_clk_calibration(void);
extern s32 gup_fw_download_proc(void *dir, u8 dwn_mode);
extern u8 gup_check_fs_mounted(char *path_name);
void gtp_recovery_reset(struct i2c_client *client);
static s32 gtp_esd_recovery(struct i2c_client *client);
s32 gtp_fw_startup(struct i2c_client *client);
static s32 gtp_main_clk_proc(struct goodix_ts_data *ts);
static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode);
#endif
//********** For GT9XXF End **********//
#if GTP_GESTURE_WAKEUP
typedef enum
{
DOZE_DISABLED = 0,
DOZE_ENABLED = 1,
DOZE_WAKEUP = 2,
}DOZE_T;
static DOZE_T doze_status = DOZE_DISABLED;
static s8 gtp_enter_doze(struct goodix_ts_data *ts);
#endif
#ifdef GTP_CONFIG_OF
int gtp_parse_dt_cfg(struct device *dev, u8 *cfg, int *cfg_len, u8 sid);
#endif
/*******************************************************
Function:
Read data from the i2c slave device.
Input:
client: i2c device.
buf[0~1]: read start address.
buf[2~len-1]: read data buffer.
len: GTP_ADDR_LENGTH + read bytes count
Output:
numbers of i2c_msgs to transfer:
2: succeed, otherwise: failed
*********************************************************/
s32 gtp_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
{
struct i2c_msg msgs[2];
s32 ret=-1;
s32 retries = 0;
GTP_DEBUG_FUNC();
msgs[0].flags = !I2C_M_RD;
msgs[0].addr = client->addr;
msgs[0].len = GTP_ADDR_LENGTH;
msgs[0].buf = &buf[0];
//msgs[0].scl_rate = 300 * 1000; // for Rockchip, etc.
msgs[1].flags = I2C_M_RD;
msgs[1].addr = client->addr;
msgs[1].len = len - GTP_ADDR_LENGTH;
msgs[1].buf = &buf[GTP_ADDR_LENGTH];
//msgs[1].scl_rate = 300 * 1000;
while(retries < 5)
{
ret = i2c_transfer(client->adapter, msgs, 2);
if(ret == 2)break;
retries++;
}
if((retries >= 5))
{
#if GTP_COMPATIBLE_MODE
struct goodix_ts_data *ts = i2c_get_clientdata(client);
#endif
#if GTP_GESTURE_WAKEUP
// reset chip would quit doze mode
if (DOZE_ENABLED == doze_status)
{
return ret;
}
#endif
GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
#if GTP_COMPATIBLE_MODE
if (CHIP_TYPE_GT9F == ts->chip_type)
{
gtp_recovery_reset(client);
}
else
#endif
{
gtp_reset_guitar(client, 10);
}
}
return ret;
}
/*******************************************************
Function:
Write data to the i2c slave device.
Input:
client: i2c device.
buf[0~1]: write start address.
buf[2~len-1]: data buffer
len: GTP_ADDR_LENGTH + write bytes count
Output:
numbers of i2c_msgs to transfer:
1: succeed, otherwise: failed
*********************************************************/
s32 gtp_i2c_write(struct i2c_client *client,u8 *buf,s32 len)
{
struct i2c_msg msg;
s32 ret = -1;
s32 retries = 0;
GTP_DEBUG_FUNC();
msg.flags = !I2C_M_RD;
msg.addr = client->addr;
msg.len = len;
msg.buf = buf;
//msg.scl_rate = 300 * 1000; // for Rockchip, etc
while(retries < 5)
{
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret == 1)break;
retries++;
}
if((retries >= 5))
{
#if GTP_COMPATIBLE_MODE
struct goodix_ts_data *ts = i2c_get_clientdata(client);
#endif
#if GTP_GESTURE_WAKEUP
if (DOZE_ENABLED == doze_status)
{
return ret;
}
#endif
GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
#if GTP_COMPATIBLE_MODE
if (CHIP_TYPE_GT9F == ts->chip_type)
{
gtp_recovery_reset(client);
}
else
#endif
{
gtp_reset_guitar(client, 10);
}
}
return ret;
}
/*******************************************************
Function:
i2c read twice, compare the results
Input:
client: i2c device
addr: operate address
rxbuf: read data to store, if compare successful
len: bytes to read
Output:
FAIL: read failed
SUCCESS: read successful
*********************************************************/
s32 gtp_i2c_read_dbl_check(struct i2c_client *client, u16 addr, u8 *rxbuf, int len)
{
u8 buf[16] = {0};
u8 confirm_buf[16] = {0};
u8 retry = 0;
while (retry++ < 3)
{
memset(buf, 0xAA, 16);
buf[0] = (u8)(addr >> 8);
buf[1] = (u8)(addr & 0xFF);
gtp_i2c_read(client, buf, len + 2);
memset(confirm_buf, 0xAB, 16);
confirm_buf[0] = (u8)(addr >> 8);
confirm_buf[1] = (u8)(addr & 0xFF);
gtp_i2c_read(client, confirm_buf, len + 2);
if (!memcmp(buf, confirm_buf, len+2))
{
memcpy(rxbuf, confirm_buf+2, len);
return SUCCESS;
}
}
GTP_ERROR("I2C read 0x%04X, %d bytes, double check failed!", addr, len);
return FAIL;
}
/*******************************************************
Function:
Send config.
Input:
client: i2c device.
Output:
result of i2c write operation.
1: succeed, otherwise: failed
*********************************************************/
s32 gtp_send_cfg(struct i2c_client *client)
{
s32 ret = 2;
#if GTP_DRIVER_SEND_CFG
s32 retry = 0;
struct goodix_ts_data *ts = i2c_get_clientdata(client);
if (ts->pnl_init_error)
{
GTP_INFO("Error occured in init_panel, no config sent");
return 0;
}
GTP_INFO("Driver send config.");
for (retry = 0; retry < 5; retry++)
{
ret = gtp_i2c_write(client, config , GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH);
if (ret > 0)
{
break;
}
}
#endif
return ret;
}
/*******************************************************
Function:
Disable irq function
Input:
ts: goodix i2c_client private data
Output:
None.
*********************************************************/
void gtp_irq_disable(struct goodix_ts_data *ts)
{
unsigned long irqflags;
GTP_DEBUG_FUNC();
spin_lock_irqsave(&ts->irq_lock, irqflags);
if (!ts->irq_is_disable)
{
ts->irq_is_disable = 1;
disable_irq_nosync(ts->client->irq);
}
spin_unlock_irqrestore(&ts->irq_lock, irqflags);
}
/*******************************************************
Function:
Enable irq function
Input:
ts: goodix i2c_client private data
Output:
None.
*********************************************************/
void gtp_irq_enable(struct goodix_ts_data *ts)
{
unsigned long irqflags = 0;
GTP_DEBUG_FUNC();
spin_lock_irqsave(&ts->irq_lock, irqflags);
if (ts->irq_is_disable)
{
enable_irq(ts->client->irq);
ts->irq_is_disable = 0;
}
spin_unlock_irqrestore(&ts->irq_lock, irqflags);
}
/*******************************************************
Function:
Report touch point event
Input:
ts: goodix i2c_client private data
id: trackId
x: input x coordinate
y: input y coordinate
w: input pressure
Output:
None.
*********************************************************/
static void gtp_touch_down(struct goodix_ts_data* ts,s32 id,s32 x,s32 y,s32 w)
{
#if GTP_CHANGE_X2Y
GTP_SWAP(x, y);
#endif
#if GTP_ICS_SLOT_REPORT
input_mt_slot(ts->input_dev, id);
input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
if(id==9){
input_mt_report_slot_state(ts->input_dev,MT_TOOL_PEN,true);
}else{
input_mt_report_slot_state(ts->input_dev,MT_TOOL_FINGER,true);
}
input_report_abs(ts->input_dev, ABS_MT_PRESSURE, w);
input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
#else
#if 0
if((id & 0x80)) {//pen
id = 9;
input_report_abs(ts->input_dev, ABS_MT_TOOL_TYPE, 1);
} else {//finger
id = id & 0x0F;
input_report_abs(ts->input_dev, ABS_MT_TOOL_TYPE, 0 );
}
input_report_key(ts->input_dev, BTN_TOUCH, 1);
input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
input_report_abs(ts->input_dev, ABS_MT_PRESSURE, w);
input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
input_mt_sync(ts->input_dev);
#else
input_report_key(ts->input_dev, BTN_TOUCH, 1);
input_report_abs(ts->input_dev, ABS_X, x);
input_report_abs(ts->input_dev, ABS_Y, y);
input_report_abs(ts->input_dev, ABS_PRESSURE, 200);
input_sync(ts->input_dev);
#endif
#endif
GTP_DEBUG("ID:%d, X:%d, Y:%d, W:%d", id, x, y, w);
}
/*******************************************************
Function:
Report touch release event
Input:
ts: goodix i2c_client private data
Output:
None.
*********************************************************/
static void gtp_touch_up(struct goodix_ts_data* ts, s32 id)
{
#if GTP_ICS_SLOT_REPORT
input_mt_slot(ts->input_dev, id);
input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
GTP_DEBUG("Touch id[%2d] release!", id);
#else
#if 0
input_report_key(ts->input_dev, BTN_TOUCH, 0);
input_mt_sync(ts->input_dev);
#else
input_report_key(ts->input_dev, BTN_TOUCH, 0);
input_report_abs(ts->input_dev, ABS_PRESSURE, 0);
input_sync(ts->input_dev);
#endif
#endif
}
#if GTP_WITH_HOVER
static void gtp_pen_init(struct goodix_ts_data *ts)
{
s32 ret = 0;
GTP_INFO("Request input device for pen/stylus.");
ts->pen_dev = input_allocate_device();
if (ts->pen_dev == NULL)
{
GTP_ERROR("Failed to allocate input device for pen/stylus.");
return;
}
ts->pen_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
ts->pen_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
set_bit(BTN_TOOL_PEN, ts->pen_dev->keybit);
input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
input_set_abs_params(ts->pen_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
input_set_abs_params(ts->pen_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
input_set_abs_params(ts->pen_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
ts->pen_dev->name = "goodix-pen";
ts->pen_dev->id.bustype = BUS_I2C;
ret = input_register_device(ts->pen_dev);
if (ret)
{
GTP_ERROR("Register %s input device failed", ts->pen_dev->name);
return;
}
}
static void gtp_pen_down(s32 x, s32 y, s32 w, s32 id)
{
struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
#if GTP_CHANGE_X2Y
GTP_SWAP(x, y);
#endif
input_report_key(ts->pen_dev, BTN_TOOL_PEN, 1);
#if GTP_ICS_SLOT_REPORT
input_mt_slot(ts->pen_dev, id);
input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
input_report_key(ts->pen_dev, BTN_TOUCH, 0);
#else
input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
input_report_key(ts->pen_dev, BTN_TOUCH, 0);
input_mt_sync(ts->pen_dev);
#endif
GTP_DEBUG("(%d)(%d, %d)[%d]", id, x, y, w);
}
static void gtp_pen_up(s32 id)
{
struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
input_report_key(ts->pen_dev, BTN_TOOL_PEN, 0);
#if GTP_ICS_SLOT_REPORT
input_mt_slot(ts->pen_dev, id);
// input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, -1);
input_report_key(ts->pen_dev, BTN_TOUCH, 0);
#else
input_report_key(ts->pen_dev, BTN_TOUCH, 0);
#endif
}
#endif
/*******************************************************
Function:
Goodix touchscreen work function
Input:
work: work struct of goodix_workqueue
Output:
None.
*********************************************************/
static void goodix_ts_work_func(struct work_struct *work)
{
u8 end_cmd[3] = {GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF, 0};
u8 point_data[2 + 1 + 8 * GTP_MAX_TOUCH + 1]={GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF};
u8 touch_num = 0;
u8 finger = 0;
static u16 pre_touch = 0;
static u8 pre_key = 0;
#if GTP_WITH_HOVER
u8 pen_active = 0;
static u8 pre_pen = 0;
#endif
static u8 pre_finger = 0;
u8 dev_active = 0;
u8 key_value = 0;
u8* coor_data = NULL;
s32 input_x = 0;
s32 input_y = 0;
s32 input_w = 0;
s32 id = 0;
s32 i = 0;
s32 ret = -1;
struct goodix_ts_data *ts = NULL;
#if GTP_COMPATIBLE_MODE
u8 rqst_buf[3] = {0x80, 0x43}; // for GT9XXF
#endif
#if GTP_GESTURE_WAKEUP
u8 doze_buf[3] = {0x81, 0x4B};
#endif
GTP_DEBUG_FUNC();
ts = container_of(work, struct goodix_ts_data, work);
if (ts->enter_update)
{
return;
}
#if GTP_GESTURE_WAKEUP
if (DOZE_ENABLED == doze_status)
{
ret = gtp_i2c_read(i2c_connect_client, doze_buf, 3);
GTP_DEBUG("0x814B = 0x%02X", doze_buf[2]);
if (ret > 0)
{
if ((doze_buf[2] == 'a') || (doze_buf[2] == 'b') || (doze_buf[2] == 'c') ||
(doze_buf[2] == 'd') || (doze_buf[2] == 'e') || (doze_buf[2] == 'g') ||
(doze_buf[2] == 'h') || (doze_buf[2] == 'm') || (doze_buf[2] == 'o') ||
(doze_buf[2] == 'q') || (doze_buf[2] == 's') || (doze_buf[2] == 'v') ||
(doze_buf[2] == 'w') || (doze_buf[2] == 'y') || (doze_buf[2] == 'z') ||
(doze_buf[2] == 0x5E) || (doze_buf[2] == 0x3E)/* ^ */
)
{
if (doze_buf[2] != 0x5E)
{
GTP_INFO("Wakeup by gesture(%c), light up the screen!", doze_buf[2]);
}
else
{
GTP_INFO("Wakeup by gesture(^), light up the screen!");
}
doze_status = DOZE_WAKEUP;
input_report_key(ts->input_dev, KEY_POWER, 1);
input_sync(ts->input_dev);
input_report_key(ts->input_dev, KEY_POWER, 0);
input_sync(ts->input_dev);
// clear 0x814B
doze_buf[2] = 0x00;
gtp_i2c_write(i2c_connect_client, doze_buf, 3);
}
else if ( (doze_buf[2] == 0xAA) || (doze_buf[2] == 0xBB) ||
(doze_buf[2] == 0xAB) || (doze_buf[2] == 0xBA) )
{
char *direction[4] = {"Right", "Down", "Up", "Left"};
u8 type = ((doze_buf[2] & 0x0F) - 0x0A) + (((doze_buf[2] >> 4) & 0x0F) - 0x0A) * 2;
GTP_INFO("%s slide to light up the screen!", direction[type]);
doze_status = DOZE_WAKEUP;
input_report_key(ts->input_dev, KEY_POWER, 1);
input_sync(ts->input_dev);
input_report_key(ts->input_dev, KEY_POWER, 0);
input_sync(ts->input_dev);
// clear 0x814B
doze_buf[2] = 0x00;
gtp_i2c_write(i2c_connect_client, doze_buf, 3);
}
else if (0xCC == doze_buf[2])
{
GTP_INFO("Double click to light up the screen!");
doze_status = DOZE_WAKEUP;
input_report_key(ts->input_dev, KEY_POWER, 1);
input_sync(ts->input_dev);
input_report_key(ts->input_dev, KEY_POWER, 0);
input_sync(ts->input_dev);
// clear 0x814B
doze_buf[2] = 0x00;
gtp_i2c_write(i2c_connect_client, doze_buf, 3);
}
else
{
// clear 0x814B
doze_buf[2] = 0x00;
gtp_i2c_write(i2c_connect_client, doze_buf, 3);
gtp_enter_doze(ts);
}
}
if (ts->use_irq)
{
gtp_irq_enable(ts);
}
return;
}
#endif
ret = gtp_i2c_read(ts->client, point_data, 12);
if (ret < 0)
{
GTP_ERROR("I2C transfer error. errno:%d\n ", ret);
if (ts->use_irq)
{
gtp_irq_enable(ts);
}
return;
}
finger = point_data[GTP_ADDR_LENGTH];
#if GTP_COMPATIBLE_MODE
// GT9XXF reques event
if ((finger == 0x00) && (CHIP_TYPE_GT9F == ts->chip_type)) // request arrived
{
ret = gtp_i2c_read(ts->client, rqst_buf, 3);
if (ret < 0)
{
GTP_ERROR("Read request status error!");
goto exit_work_func;
}
switch (rqst_buf[2])
{
case GTP_RQST_CONFIG:
GTP_INFO("Request for config.");
ret = gtp_send_cfg(ts->client);
if (ret < 0)
{
GTP_ERROR("Request for config unresponded!");
}