huangcm
2024-10-12 1f4dc0fe687f74148f03d6ac2ead47c9b45b9463
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
/* drivers/input/touchscreen/gt9xx.c
 * 
 * 2010 - 2012 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.4
 * Author:andrew@goodix.com
 * Release Date:2012/12/12
 * Revision record:
 *      V1.0:2012/08/31,first Release
 *      V1.2:2012/10/15,modify gtp_reset_guitar,slot report,tracking_id & 0x0F
 *      V1.4:2012/12/12,modify gt9xx_update.c
 *      
 */
 
#include <linux/irq.h>
#include "gt9xx_ts.h"
#include <linux/pm.h>
 
#if GTP_ICS_SLOT_REPORT
#include <linux/input/mt.h>
#endif
 
static const char *goodix_ts_name = "gt9xx";
static struct workqueue_struct *goodix_wq;
struct i2c_client * i2c_connect_client = NULL; 
static 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]))
#endif
 
static s8 gtp_i2c_test(struct i2c_client *client);
void gtp_reset_guitar(struct i2c_client *client, s32 ms);
void gtp_int_sync(s32 ms);
 
#ifdef CONFIG_HAS_EARLYSUSPEND
static void goodix_ts_early_suspend(struct early_suspend *h);
static void goodix_ts_late_resume(struct early_suspend *h);
#endif
 
#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 *);
s32 gtp_init_ext_watchdog(struct i2c_client *client);
#endif
 
///////////////////////////////////////////////
//specific tp related macro: need be configured for specific tp
 
#define CTP_IRQ_NUMBER          (config_info.int_number)
#define CTP_IRQ_MODE        (IRQF_TRIGGER_FALLING)
#define CTP_NAME        ("gt9xx_ts")
#define SCREEN_MAX_X    (screen_max_x)
#define SCREEN_MAX_Y    (screen_max_y)
#define PRESS_MAX        (255)
 
 
static int screen_max_x = 1024;
static int screen_max_y = 600;
static int revert_x_flag = 0;
static int revert_y_flag = 0;
static int exchange_x_y_flag = 0;
static __u32 twi_id = 0;
static char irq_pin_name[8];
 
static u32 debug_mask = 0;
 
enum{    
   DEBUG_INIT = 1U << 0,    
   DEBUG_SUSPEND = 1U << 1,
   DEBUG_INT_INFO = 1U << 2,
   DEBUG_X_Y_INFO = 1U << 3,
   DEBUG_KEY_INFO = 1U << 4,
   DEBUG_WAKEUP_INFO = 1U << 5,
   DEBUG_OTHERS_INFO = 1U << 6,
};
 
#define dprintk(level_mask,fmt,arg...)    if(unlikely(debug_mask & level_mask)) \
   printk("***CTP***"fmt, ## arg)
module_param_named(debug_mask,debug_mask,int,S_IRUGO | S_IWUSR | S_IWGRP);
 
static const unsigned short normal_i2c[3] = {0x14, 0x5d, I2C_CLIENT_END};
//static const int chip_id_value[3] = {57};
//static uint8_t read_chip_value[3] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff,0};
struct ctp_config_info config_info = {
   .input_type = CTP_TYPE,
   .name = NULL,
   .int_number = 0,
};
 
//static void goodix_init_events(struct work_struct *work);
static void goodix_resume_events(struct work_struct *work);
static struct workqueue_struct *goodix_wq;
//static struct workqueue_struct *goodix_init_wq;
static struct workqueue_struct *goodix_resume_wq;
//static DECLARE_WORK(goodix_init_work, goodix_init_events);
static DECLARE_WORK(goodix_resume_work, goodix_resume_events);
 
/**
 * ctp_detect - Device detection callback for automatic device creation
 * return value:  
 *                    = 0; success;
 *                    < 0; err
 */
static int ctp_detect(struct i2c_client *client, struct i2c_board_info *info)
{
   struct i2c_adapter *adapter = client->adapter;
   int  ret = -1;
 
   if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)){
       printk("======return=====\n");
       return -ENODEV;
   }
 
   if(twi_id == adapter->nr){
       dprintk(DEBUG_INIT,"%s: addr = %x\n", __func__, client->addr);
       ret = gtp_i2c_test(client);
       printk("detect ret %d\n",ret);
       if(!ret){
           printk("%s:I2C connection might be something wrong \n", __func__);
           return -ENODEV;
       }else{                   
           strlcpy(info->type, CTP_NAME, I2C_NAME_SIZE);
           printk("======detect ok !=====\n");
           return 0;    
       }
   }else{
       return -ENODEV;
   }
}
 
/**
 * ctp_print_info - sysconfig print function
 * return value:
 *
 */
void ctp_print_info(struct ctp_config_info info,int debug_level)
{
   if(debug_level == DEBUG_INIT)
   {
       dprintk(DEBUG_INIT,"info.ctp_used:%d\n",info.ctp_used);
       dprintk(DEBUG_INIT,"info.twi_id:%d\n",info.twi_id);
       dprintk(DEBUG_INIT,"info.screen_max_x:%d\n",info.screen_max_x);
       dprintk(DEBUG_INIT,"info.screen_max_y:%d\n",info.screen_max_y);
       dprintk(DEBUG_INIT,"info.revert_x_flag:%d\n",info.revert_x_flag);
       dprintk(DEBUG_INIT,"info.revert_y_flag:%d\n",info.revert_y_flag);
       dprintk(DEBUG_INIT,"info.exchange_x_y_flag:%d\n",info.exchange_x_y_flag);
       dprintk(DEBUG_INIT,"info.irq_gpio_number:%d\n",info.irq_gpio.gpio);
       dprintk(DEBUG_INIT,"info.wakeup_gpio_number:%d\n",info.wakeup_gpio.gpio);
   }
}
 
/**
 * ctp_wakeup - function
 *
 */
int ctp_wakeup(int status,int ms)
{
   dprintk(DEBUG_INIT,"***CTP*** %s:status:%d,ms = %d\n",__func__,status,ms);
 
   if (status == 0) {
 
       if(ms == 0) {
           __gpio_set_value(config_info.wakeup_gpio.gpio, 0);
       }else {
           __gpio_set_value(config_info.wakeup_gpio.gpio, 0);
           msleep(ms);
           __gpio_set_value(config_info.wakeup_gpio.gpio, 1);
       }
   }
   if (status == 1) {
       if(ms == 0) {
           __gpio_set_value(config_info.wakeup_gpio.gpio, 1);
       }else {
           __gpio_set_value(config_info.wakeup_gpio.gpio, 1);
           msleep(ms);
           __gpio_set_value(config_info.wakeup_gpio.gpio, 0);
       }
   }
   msleep(5);
 
   return 0;
}
 
void gtp_set_int_value(int status)
{
   long unsigned int    config;
 
   config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC,0xFFFF);
   pin_config_get(SUNXI_PINCTRL,irq_pin_name,&config);
 
   if (1 != SUNXI_PINCFG_UNPACK_VALUE(config)){
       config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC,1);
       pin_config_set(SUNXI_PINCTRL,irq_pin_name,config);;
   }
 
   __gpio_set_value(CTP_IRQ_NUMBER, status);   
}
 
void gtp_set_io_int(void)
{
   long unsigned int    config;
   config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC,0xFFFF);
   pin_config_get(SUNXI_PINCTRL,irq_pin_name,&config);
 
   if (6 != SUNXI_PINCFG_UNPACK_VALUE(config)) {
       config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC, 6);
       pin_config_set(SUNXI_PINCTRL, irq_pin_name, config);
   }
 
}
 
void gtp_io_init(int ms)
{       
   ctp_wakeup(0, 0);
   msleep(ms);
 
   gtp_set_int_value(0);
   msleep(50);
 
   ctp_wakeup(1, 0);
   msleep(60);
 
 
#if GTP_ESD_PROTECT
   //   gtp_init_ext_watchdog(client);
#endif
 
}
 
/*******************************************************    
Function:
Read data from the i2c slave device.
 
Input:
client:    i2c device.
buf[0]:operate address.
buf[1]~buf[len]:read data buffer.
len:operate length.
 
Output:
numbers of i2c_msgs to transfer
 *********************************************************/
s32 gtp_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
{
   struct i2c_msg msgs[2];
   s32 ret = -1;
   s32 retries = 0;
 
   msgs[0].flags = !I2C_M_RD;
   msgs[0].addr  = client->addr;
   msgs[0].len   = GTP_ADDR_LENGTH;
   msgs[0].buf   = &buf[0];
 
   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];
 
   while(retries < 2) {
       ret = i2c_transfer(client->adapter, msgs, 2);
       if(ret == 2) 
           break;
       retries++;
   }
 
   if(retries >= 2) {
       printk("%s:I2C retry timeout, reset chip. \n", __func__);
   }
   return ret;
}
 
/*******************************************************    
Function:
write data to the i2c slave device.
 
Input:
client:    i2c device.
buf[0]:operate address.
buf[1]~buf[len]:write data buffer.
len:operate length.
 
Output:
numbers of i2c_msgs to transfer.
 *********************************************************/
s32 gtp_i2c_write(struct i2c_client *client,u8 *buf,s32 len)
{
   struct i2c_msg msg;
   s32 ret = -1;
   s32 retries = 0;
 
   msg.flags = !I2C_M_RD;
   msg.addr  = client->addr;
   msg.len   = len;
   msg.buf   = buf;
 
   while(retries < 2) {
       ret = i2c_transfer(client->adapter, &msg, 1);
       if (ret == 1) 
           break;
       retries++;
   }
 
   if(retries >= 2) {
       printk("%s:I2C retry timeout, reset chip.", __func__);
   }
   return ret;
}
 
/*******************************************************
Function:
Send config Function.
 
Input:
client:    i2c client.
 
Output:
Executive outcomes.0--success,non-0--fail.
 *******************************************************/
s32 gtp_send_cfg(struct i2c_client *client)
{
   s32 ret = 0;
 
#if GTP_DRIVER_SEND_CFG
   s32 retry = 0;
 
   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:    i2c client private struct.
 
Output:
None.
 *******************************************************/
void gtp_irq_disable(struct goodix_ts_data *ts)
{
   unsigned long irqflags;
   int ret;
 
   dprintk(DEBUG_INT_INFO, "%s ---start!---\n", __func__);
   spin_lock_irqsave(&ts->irq_lock, irqflags);
   if (!ts->irq_is_disable) {
       ts->irq_is_disable = 1; 
       ret = input_set_int_enable(&(config_info.input_type), 0);
       if (ret < 0)                  
           dprintk(DEBUG_OTHERS_INFO,"%s irq disable failed\n", goodix_ts_name);
   }
   spin_unlock_irqrestore(&ts->irq_lock, irqflags);
}
 
/*******************************************************
Function:
Disable IRQ Function.
 
Input:
ts:    i2c client private struct.
 
Output:
None.
 *******************************************************/
void gtp_irq_enable(struct goodix_ts_data *ts)
{
   unsigned long irqflags = 0;
   int ret;
 
   dprintk(DEBUG_INT_INFO, "%s ---start!---\n", __func__);
 
   spin_lock_irqsave(&ts->irq_lock, irqflags);
   if (ts->irq_is_disable) {
       ts->irq_is_disable = 0; 
       ret = input_set_int_enable(&(config_info.input_type), 1);    
       if (ret < 0)                    
           dprintk(DEBUG_OTHERS_INFO,"%s irq enable failed\n", goodix_ts_name);
   }
   spin_unlock_irqrestore(&ts->irq_lock, irqflags);
}
 
/*******************************************************
Function:
Touch down report function.
 
Input:
ts:private data.
id:tracking id.
x:input x.
y:input y.
w:input weight.
 
Output:
None.
 *******************************************************/
static void gtp_touch_down(struct goodix_ts_data* ts,s32 id,s32 x,s32 y,s32 w)
{
   dprintk(DEBUG_X_Y_INFO, "source data:ID:%d, X:%d, Y:%d, W:%d\n", id, x, y, w);
 
   //printk(KERN_ERR"revert_x_flag=%d, revert_y_flag=%d, GTP_ICS_SLOT_REPORT=%d  V2 SCREEN_MAX_X:SCREEN_MAX_Y=%d:%d\n", revert_x_flag, revert_y_flag, GTP_ICS_SLOT_REPORT, SCREEN_MAX_X, SCREEN_MAX_Y);        
   //printk(KERN_ERR"(%d,%d)--->", x, y);
 
   if(1 == exchange_x_y_flag){
       swap(x, y);
   }
 
   if(1 == revert_x_flag){
       x = SCREEN_MAX_X - x;
   }
 
   if(1 == revert_y_flag){
       y = SCREEN_MAX_Y - y;
   }
   //printk(KERN_ERR"(%d,%d)\n", x, y);
 
   dprintk(DEBUG_X_Y_INFO,"report data:ID:%d, X:%d, Y:%d, W:%d\n", id, x, y, w);
 
#if GTP_ICS_SLOT_REPORT
   input_mt_slot(ts->input_dev, id);
   input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
   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
   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_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);
   input_mt_sync(ts->input_dev);
#endif
 
}
 
/*******************************************************
Function:
Touch up report function.
 
Input:
ts: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);
   dprintk(DEBUG_X_Y_INFO, "Touch id[%2d] release!", id);
#else
   input_report_key(ts->input_dev, BTN_TOUCH, 0);
   input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
   input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
   input_mt_sync(ts->input_dev);
#endif
}
 
/*******************************************************
Function:
Goodix touchscreen work function.
 
Input:
work:    work_struct of goodix_wq.
 
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;
   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;
   int j = 0;
   s32 ret = -1;
   struct goodix_ts_data *ts = NULL;
 
   dprintk(DEBUG_X_Y_INFO,"===enter %s===\n",__func__);
 
   ts = container_of(work, struct goodix_ts_data, work);
   if (ts->enter_update){
       return;
   }
 
   for(j=0; j<2;j++)
   {
       ret = gtp_i2c_read(ts->client, point_data, 12);
       if (ret < 0){
           printk("I2C transfer error. errno:%d\n ", ret);
           goto exit_work_func;
       }
   }
 
   finger = point_data[GTP_ADDR_LENGTH];    
   if((finger & 0x80) == 0) {
       goto exit_work_func;
   }
 
   touch_num = finger & 0x0f;
   if (touch_num > GTP_MAX_TOUCH) {
       goto exit_work_func;
   }
 
   if (touch_num > 1) {
       u8 buf[8 * GTP_MAX_TOUCH] = {(GTP_READ_COOR_ADDR + 10) >> 8, (GTP_READ_COOR_ADDR + 10) & 0xff};
 
       ret = gtp_i2c_read(ts->client, buf, 2 + 8 * (touch_num - 1)); 
       memcpy(&point_data[12], &buf[2], 8 * (touch_num - 1));
   }
 
#if GTP_HAVE_TOUCH_KEY
   key_value = point_data[3 + 8 * touch_num];
 
   if(key_value || pre_key) {
       for (i = 0; i < GTP_MAX_KEY_NUM; i++) {
           input_report_key(ts->input_dev, touch_key_array[i], key_value & (0x01<<i));   
       }
       touch_num = 0;
       pre_touch = 0;
   }
#endif
   pre_key = key_value;
 
   dprintk(DEBUG_X_Y_INFO, "pre_touch:%02x, finger:%02x.", pre_touch, finger);
 
#if GTP_ICS_SLOT_REPORT
   if (pre_touch || touch_num) {
       s32 pos = 0;
       u16 touch_index = 0;
       coor_data = &point_data[3];
 
       if(touch_num) {
           id = coor_data[pos] & 0x0F;
           touch_index |= (0x01<<id);
       }
 
       dprintk(DEBUG_X_Y_INFO, 
               "id=%d, touch_index=0x%x, pre_touch=0x%x\n", id, touch_index, pre_touch);
 
       for (i = 0; i < GTP_MAX_TOUCH; i++) {
           if (touch_index & (0x01<<i)) {
               input_x  = coor_data[pos + 1] | coor_data[pos + 2] << 8;
               input_y  = coor_data[pos + 3] | coor_data[pos + 4] << 8;
               input_w  = coor_data[pos + 5] | coor_data[pos + 6] << 8;
 
               gtp_touch_down(ts, id, input_x, input_y, input_w);
               pre_touch |= 0x01 << i;
 
               pos += 8;
               id = coor_data[pos] & 0x0F;
               touch_index |= (0x01<<id);
           }else {// if (pre_touch & (0x01 << i))
 
               gtp_touch_up(ts, i);
               pre_touch &= ~(0x01 << i);
           }
       }
   }
 
#else
   if (touch_num ) {
       for (i = 0; i < touch_num; i++) {
           coor_data = &point_data[i * 8 + 3];
 
           id = coor_data[0] & 0x0F;
           input_x  = coor_data[1] | coor_data[2] << 8;
           input_y  = coor_data[3] | coor_data[4] << 8;
           input_w  = coor_data[5] | coor_data[6] << 8;
 
           gtp_touch_down(ts, id, input_x, input_y, input_w);
       }
   }else if(pre_touch){
       dprintk(DEBUG_X_Y_INFO, "Touch Release!");
       gtp_touch_up(ts, 0);
   }
 
   pre_touch = touch_num;
 
#endif
 
   input_sync(ts->input_dev);
 
exit_work_func:
   if(!ts->gtp_rawdiff_mode) {
       ret = gtp_i2c_write(ts->client, end_cmd, 3);
       if (ret < 0) {
           printk("I2C write end_cmd  error!"); 
       }
   }
   return ;
}
 
/*******************************************************
Function:
External interrupt service routine.
 
Input:
irq:    interrupt number.
dev_id: private data pointer.
 
Output:
irq execute status.
 *******************************************************/
 
irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
{        
   struct goodix_ts_data *ts = (struct goodix_ts_data *)dev_id;
   dprintk(DEBUG_INT_INFO, "==========------TS Interrupt-----============\n");  
 
   queue_work(goodix_wq, &ts->work);
   return 0;
}
 
 
static s32 gtp_init_panel(struct goodix_ts_data *ts);
 
/*******************************************************
Function:
Eter sleep function.
 
Input:
ts:private data.
 
Output:
Executive outcomes.0--success,non-0--fail.
 *******************************************************/
#if defined(CONFIG_HAS_EARLYSUSPEND) | defined(CONFIG_PM)
static s8 gtp_enter_sleep(struct goodix_ts_data * ts)
{
   s8 ret = -1;
   s8 retry = 0;
   u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 5};
 
   dprintk(DEBUG_SUSPEND, "%s start!\n", __func__);
 
   gtp_set_int_value(0);
 
   while(retry++ < 2) {
       ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
       if (ret > 0) {
           dprintk(DEBUG_SUSPEND, "GTP enter sleep!");
           return ret;
       }
       msleep(10);
   }
   dprintk(DEBUG_SUSPEND, "GTP send sleep cmd failed.");
 
   return ret;
}
#endif
 
/*******************************************************
Function:
Wakeup from sleep mode Function.
 
Input:
ts:    private data.
 
Output:
Executive outcomes.0--success,non-0--fail.
 *******************************************************/
static s8 gtp_wakeup_sleep(struct goodix_ts_data * ts)
{
   //u8 retry = 0;
   s8 ret = 0;
   printk("hcm gtp_wakeup_sleep !\n");
   gtp_io_init(20);
   gtp_set_io_int();
 
//#if GTP_POWER_CTRL_SLEEP
#if 0
   while(retry++ < 5)
   {
       ret = gtp_send_cfg(ts->client);
       if (ret > 0)
       {
           dprintk(DEBUG_SUSPEND, "Wakeup sleep send config success.");
           return ret;
       }
   }
 
   printk("GTP wakeup sleep failed.");
 
#endif
   
   ret = gtp_i2c_test(ts->client);
   if (ret < 0){
       printk("hcm I2C communication ERROR!\n");
       msleep(50);
       gtp_set_io_int();
       ret = gtp_i2c_test(ts->client);
       if (ret < 0){
           printk("I2C communication ERROR 2!\n");
       }
   }
 
   ret = gtp_init_panel(ts);
   if (ret < 0) {
       printk("GTP init panel failed.\n");
   }
 
 
#if GTP_CREATE_WR_NODE
   init_wr_node(ts->client);
#endif
 
   return ret;
}
 
 
/*******************************************************
Function:
GTP initialize function.
 
Input:
ts:    i2c client private struct.
 
Output:
Executive outcomes.0---succeed.
 *******************************************************/
static s32 gtp_init_panel(struct goodix_ts_data *ts)
{
   s32 ret = -1;
 
#if GTP_DRIVER_SEND_CFG
   s32 i;
   u8 check_sum = 0;
   u8 rd_cfg_buf[16];
   int index =0;
 
#if 1
   u8 cfg_info_group1[] = CTP_CFG_GROUP1; 
   u8 cfg_info_group2[] = CTP_CFG_GROUP2; 
   u8 cfg_info_group3[] = CTP_CFG_GROUP3; 
   u8 cfg_info_group4[] = CTP_CFG_GROUP4;
   u8 cfg_info_group5[] = CTP_CFG_GROUP5;
   u8 cfg_info_group6[] = CTP_CFG_GROUP6;
   //         u8 cfg_info_group7[] = CTP_CFG_GROUP7;
#else
   u8 cfg_info_group1[] = {}; 
   u8 cfg_info_group2[] = {}; 
   u8 cfg_info_group3[] = {}; 
   u8 cfg_info_group4[] = {};
   u8 cfg_info_group5[] = {};
   u8 cfg_info_group6[] = {};
   //         u8 cfg_info_group7[] = {};
#endif
 
   u8 *send_cfg_buf[] = {cfg_info_group1,cfg_info_group2,cfg_info_group3,cfg_info_group4,cfg_info_group5,cfg_info_group6};
   u8 cfg_info_len[] = {sizeof(cfg_info_group1)/sizeof(cfg_info_group1[0]),\
       sizeof(cfg_info_group2)/sizeof(cfg_info_group2[0]),\
           sizeof(cfg_info_group3)/sizeof(cfg_info_group3[0]),\
           sizeof(cfg_info_group4)/sizeof(cfg_info_group4[0]),\
           sizeof(cfg_info_group5)/sizeof(cfg_info_group5[0]),\
           sizeof(cfg_info_group6)/sizeof(cfg_info_group6[0])};
   //sizeof(cfg_info_group7)/sizeof(cfg_info_group7[0])};
#if 0 //gandy
for(i=0; i<3; i++)
{
   if(cfg_info_len[i] > ts->gtp_cfg_len)
   {
       ts->gtp_cfg_len = cfg_info_len[i];
   }
}
#endif
 
GTP_DEBUG("len1=%d,len2=%d,len3=%d,send_len:%d",cfg_info_len[0],cfg_info_len[1],cfg_info_len[2],ts->gtp_cfg_len);
#if 0
if ((!cfg_info_len[1]) && (!cfg_info_len[2]))
{
   rd_cfg_buf[GTP_ADDR_LENGTH] = 0; 
}
else
#endif
printk("gtp_i2c_read GTP_REG_SENSOR_ID \n");
{
   rd_cfg_buf[0] = GTP_REG_SENSOR_ID >> 8;
   rd_cfg_buf[1] = GTP_REG_SENSOR_ID & 0xff;
   ret = gtp_i2c_read(ts->client, rd_cfg_buf, 3);
   if (ret < 0)
   {
       GTP_ERROR("Read SENSOR ID failed,default use group1 config!");
       rd_cfg_buf[GTP_ADDR_LENGTH] = 0;
   }
   rd_cfg_buf[GTP_ADDR_LENGTH] &= 0x07;
}
 
#if 0        
if(screen_max_x == 800 && screen_max_y == 480)
{
   if(rd_cfg_buf[GTP_ADDR_LENGTH] == 3)
       index = 0;
   else if(rd_cfg_buf[GTP_ADDR_LENGTH] == 4)
       index = 1;
   else if(rd_cfg_buf[GTP_ADDR_LENGTH] == 5)
       index = 3;
   else if(rd_cfg_buf[GTP_ADDR_LENGTH] == 0)
       index = 6;
}
else if(screen_max_x == 1024 && screen_max_y == 600)
{
   if(rd_cfg_buf[GTP_ADDR_LENGTH] == 0)
       index = 5;
   else if(rd_cfg_buf[GTP_ADDR_LENGTH] == 3)
       index = 2;
}
#endif  
GTP_DEBUG("CTP name : %s\n",config_info.name);
if (!strcmp(config_info.name,"gt911_805d5")){
   index = 0;
   GTP_DEBUG("gt9xx:index = %d\n",index);
 
} else if (!strcmp(config_info.name,"gt911_g912")){
   index = 2;
   GTP_DEBUG("gt9xx:index = %d\n",index);
 
} else if (!strcmp(config_info.name,"gt911_xw785")){
   index = 3;
   GTP_DEBUG("gt9xx:index = %d\n",index);
 
} else {//default gt9xx_ts            
   index = 1; //default  p4
   GTP_DEBUG("gt9xx:index = %d\n",index);
}
 
printk("config send_cfg_buf******** \n");
//index = rd_cfg_buf[GTP_ADDR_LENGTH];
ts->gtp_cfg_len = cfg_info_len[index];
GTP_DEBUG("gandy---SENSOR ID:%d\n", rd_cfg_buf[GTP_ADDR_LENGTH]);
memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[index], ts->gtp_cfg_len);
 
#if GTP_CUSTOM_CFG
config[RESOLUTION_LOC]       = (u8)GTP_MAX_WIDTH;
config[RESOLUTION_LOC + 1] = (u8)(GTP_MAX_WIDTH>>8);
config[RESOLUTION_LOC + 2] = (u8)GTP_MAX_HEIGHT;
config[RESOLUTION_LOC + 3] = (u8)(GTP_MAX_HEIGHT>>8);
 
if (GTP_INT_TRIGGER == 0)  //RISING
{
   config[TRIGGER_LOC] &= 0xfe; 
}
else if (GTP_INT_TRIGGER == 1)    //FALLING
{
   config[TRIGGER_LOC] |= 0x01;
}
#endif  //endif GTP_CUSTOM_CFG
 
check_sum = 0;
for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
{
   check_sum += config[i];
}
config[ts->gtp_cfg_len] = (~check_sum) + 1;
 
#else //else DRIVER NEED NOT SEND CONFIG
 
if(ts->gtp_cfg_len == 0)
{
   ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
}
ret = gtp_i2c_read(ts->client, config, ts->gtp_cfg_len + GTP_ADDR_LENGTH);
if (ret < 0)
{
   GTP_ERROR("GTP read resolution & max_touch_num failed, use default value!");
   ts->abs_x_max = GTP_MAX_WIDTH;
   ts->abs_y_max = GTP_MAX_HEIGHT;
   ts->int_trigger_type = GTP_INT_TRIGGER;
}
#endif //endif GTP_DRIVER_SEND_CFG
 
GTP_DEBUG_FUNC();
 
ts->abs_x_max = (config[RESOLUTION_LOC + 1] << 8) + config[RESOLUTION_LOC];
ts->abs_y_max = (config[RESOLUTION_LOC + 3] << 8) + config[RESOLUTION_LOC + 2];
ts->int_trigger_type = (config[TRIGGER_LOC]) & 0x03;
if ((!ts->abs_x_max)||(!ts->abs_y_max))
{
   GTP_ERROR("GTP resolution & max_touch_num invalid, use default value!");
   ts->abs_x_max = GTP_MAX_WIDTH;
   ts->abs_y_max = GTP_MAX_HEIGHT;
}
 
msleep(100);
ret = gtp_send_cfg(ts->client);
if (ret < 0)
{
   printk("\ngandy-----send config error.ret=%d\n",ret);
   GTP_ERROR("Send config error.");
}
printk("X_MAX = %d,Y_MAX = %d,TRIGGER = 0x%02x",
       ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
 
msleep(10);
 
return 0;
}
 
/*******************************************************
Function:
Read goodix touchscreen version function.
 
Input:
client:    i2c client struct.
version:address to store version info
 
Output:
Executive outcomes.0---succeed.
 *******************************************************/
s32 gtp_read_version(struct i2c_client *client, u16* version)
{
   s32 ret = -1;
   u8 buf[8] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff};
 
   dprintk(DEBUG_INIT, "%s ---start!.---\n", __func__);
 
   ret = gtp_i2c_read(client, buf, sizeof(buf));
   if (ret < 0) {
       printk("GTP read version failed");
       return ret;
   }
 
   if (version) {
       *version = (buf[7] << 8) | buf[6];
   }
 
   if (buf[5] == 0x00) {
       printk("IC Version: %c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[7], buf[6]);
   }
   else {
       printk("IC Version: %c%c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[5], buf[7], buf[6]);
   }
   return ret;
}
 
/*******************************************************
Function:
I2c test Function.
 
Input:
client:i2c client.
 
Output:
Executive outcomes.0--success,non-0--fail.
 *******************************************************/
static s8 gtp_i2c_test(struct i2c_client *client)
{
   u8 test[3] = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
   u8 retry = 0;
   s8 ret = -1;
 
   while(retry++ < 2) {
       ret = gtp_i2c_read(client, test, 3);
       if (ret > 0) {
           return ret;
       }
       printk("GTP i2c test failed time %d. \n",retry);
       msleep(10);
   }
   return ret;
}
 
 
/*******************************************************
Function:
Request input device Function.
 
Input:
ts:private data.
 
Output:
Executive outcomes.0--success,non-0--fail.
 *******************************************************/
static s8 gtp_request_input_dev(struct goodix_ts_data *ts)
{
   s8 ret = -1;
#if GTP_HAVE_TOUCH_KEY
   u8 index = 0;
#endif
 
   ts->input_dev = input_allocate_device();
   if (ts->input_dev == NULL) {
       GTP_ERROR("Failed to allocate input device.");
       return -ENOMEM;
   }
 
   ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
#if GTP_ICS_SLOT_REPORT
   __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
   input_mt_init_slots(ts->input_dev, 255, 1);
#else
   ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
#endif
 
#if GTP_HAVE_TOUCH_KEY
   for (index = 0; index < GTP_MAX_KEY_NUM; index++) {
       input_set_capability(ts->input_dev,EV_KEY,touch_key_array[index]);    
   }
#endif
 
   //#if GTP_CHANGE_X2Y
   //        GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
   //#endif
 
   set_bit(ABS_MT_POSITION_X, ts->input_dev->absbit);
   set_bit(ABS_MT_POSITION_Y, ts->input_dev->absbit);
   set_bit(ABS_MT_TOUCH_MAJOR, ts->input_dev->absbit);
   set_bit(ABS_MT_WIDTH_MAJOR, ts->input_dev->absbit);
 
 
 
   input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
   input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
   input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
   input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);    
   input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
   __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
 
   ts->input_dev->name = CTP_NAME;
   ts->input_dev->phys = "input/goodix-ts";
   ts->input_dev->id.bustype = BUS_I2C;
   ts->input_dev->id.vendor = 0xDEAD;
   ts->input_dev->id.product = 0xBEEF;
   ts->input_dev->id.version = 10427;
   ret = input_register_device(ts->input_dev);
   if (ret) {
       printk("Register %s input device failed", ts->input_dev->name);
       return -ENODEV;
   }
#ifdef CONFIG_HAS_EARLYSUSPEND
   ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
   ts->early_suspend.suspend = goodix_ts_early_suspend;
   ts->early_suspend.resume = goodix_ts_late_resume;
   register_early_suspend(&ts->early_suspend);
#endif
   return 0;
}
 
static int goodix_ts_suspend(struct device *dev);
static int goodix_ts_resume(struct device *dev);
extern void gt9xx_resume_extern(void);
extern void gt9xx_suspend_extern(void);
struct goodix_ts_data *cur_ts;
 
void gt9xx_suspend_extern(void)
{
   // goodix_ts_suspend(config_info.dev);
   
   gtp_irq_disable(cur_ts);
   cancel_work_sync(&goodix_resume_work);
   flush_workqueue(goodix_resume_wq);
   cancel_work_sync(&cur_ts->work);
   flush_workqueue(goodix_wq);
 
}
EXPORT_SYMBOL(gt9xx_suspend_extern);
 
void gt9xx_resume_extern(void)
{
   // goodix_ts_resume(config_info.dev);
   printk("%s goodix_ts_resume v1\n", goodix_ts_name);
   __gpio_set_value(config_info.wakeup_gpio.gpio, 1);
   input_set_power_enable(&(config_info.input_type), 1);
   msleep(10);
   // goodix_resume_events
   queue_work(goodix_resume_wq, &goodix_resume_work);//gandy
 
}
EXPORT_SYMBOL(gt9xx_resume_extern);
 
/*******************************************************
Function:
Goodix touchscreen probe function.
 
Input:
client:    i2c device struct.
id:device id.
 
Output:
Executive outcomes. 0---succeed.
 *******************************************************/
static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
   s32 ret = -1;
   struct goodix_ts_data *ts;
   u16 version_info;   
 
   dprintk(DEBUG_INIT, "GTP Driver Version:%s\n",GTP_DRIVER_VERSION);
   printk("GTP I2C Address:0x%02x\n", client->addr);
 
   i2c_connect_client = client;
   if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
       printk("I2C check functionality failed.\n");
       return -ENODEV;
   }
 
   ts = kzalloc(sizeof(*ts), GFP_KERNEL);
   if (ts == NULL) {
       printk("Alloc GFP_KERNEL memory failed.\n");
       return -ENOMEM;
   }
 
   memset(ts, 0, sizeof(*ts));
   INIT_WORK(&ts->work, goodix_ts_work_func);
   ts->client = client;
   i2c_set_clientdata(client, ts);
   //ts->irq_lock = SPIN_LOCK_UNLOCKED;
   ts->gtp_rawdiff_mode = 0;
 
 
   ret = gtp_i2c_test(client);
   if (ret < 0 && client->addr == 0x5d){
       printk("I2C communication ERROR! reset chip and retry \n");
       gtp_set_io_int();
       ret = gtp_i2c_test(client);
       if (ret < 0){
           printk("I2C communication ERROR 2!\n");
           goto exit_device_detect;
       }
   } else if (ret < 0) {
       printk("I2C communication ERROR %02x !\n", client->addr);
       goto exit_device_detect;
   }
 
   printk(KERN_ALERT "create_singlethread_workqueue goodix_resume.\n");
   goodix_resume_wq = create_singlethread_workqueue("goodix_resume");
   if (goodix_resume_wq == NULL) {
       printk("create goodix_resume_wq fail!\n");
       return -ENOMEM;
   }
 
   printk(KERN_ALERT "create_singlethread_workqueue goodix_wq.\n");
 
   goodix_wq = create_singlethread_workqueue("goodix_wq");
   if (!goodix_wq) {
       printk(KERN_ALERT "Creat goodix_wq workqueue failed.\n");
       return -ENOMEM;
   }
 
#if GTP_AUTO_UPDATE
   //ret = gup_init_update_proc(ts);
   // if (ret < 0) {
   //         printk("Create update thread error.");
   // }
#endif
   ret = gtp_init_panel(ts);
   if (ret < 0) {
       printk("GTP init panel failed.\n");
   }
 
 
   ret = gtp_request_input_dev(ts);
   if (ret < 0) {
       printk("GTP request input dev failed\n");
       goto exit_device_detect;
   }
 
   ret = gtp_read_version(client, &version_info);
   if (ret < 0) {
       printk("Read version failed.");
   }
 
   config_info.dev = &(ts->input_dev->dev);
 
   ret = input_request_int(&(config_info.input_type), goodix_ts_irq_handler,CTP_IRQ_MODE, ts);       
   if (ret) {
       printk("Request irq fail!.\n");
   }
 
 
   spin_lock_init(&ts->irq_lock);
 
#if GTP_CREATE_WR_NODE
   init_wr_node(client);
#endif
 
#if GTP_ESD_PROTECT
   INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
   gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
   queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE); 
#endif
   dprintk(DEBUG_INIT, "gt9xx probe success!\n");
 
   cur_ts = ts;
 
   return 0;
exit_device_detect:
   i2c_set_clientdata(client, NULL);
   kfree(ts);
   return ret;
}
 
 
/*******************************************************
Function:
Goodix touchscreen driver release function.
 
Input:
client:    i2c device struct.
 
Output:
Executive outcomes. 0---succeed.
 *******************************************************/
static int goodix_ts_remove(struct i2c_client *client)
{
   struct goodix_ts_data *ts = i2c_get_clientdata(client);
 
   dprintk(DEBUG_INIT,"%s start!\n", __func__);
#ifdef CONFIG_HAS_EARLYSUSPEND
   unregister_early_suspend(&ts->early_suspend);
#endif
 
#if GTP_CREATE_WR_NODE
   uninit_wr_node();
#endif
 
#if GTP_ESD_PROTECT
   flush_workqueue(gtp_esd_check_workqueue);
   if(gtp_esd_check_workqueue)
       destroy_workqueue(gtp_esd_check_workqueue);
#endif
   input_free_int(&(config_info.input_type), ts);
   flush_workqueue(goodix_wq);
   //cancel_work_sync(&goodix_init_work);
   cancel_work_sync(&goodix_resume_work);
   if(goodix_wq)
       destroy_workqueue(goodix_wq);
   //destroy_workqueue(goodix_init_wq);
   if(goodix_resume_wq)
       destroy_workqueue(goodix_resume_wq);
   i2c_set_clientdata(ts->client, NULL);
   input_unregister_device(ts->input_dev);
   kfree(ts);
 
   return 0;
}
 
static void goodix_resume_events (struct work_struct *work)
{
   int ret;
   struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
   printk("goodix_resume_events \n");    
   ret = gtp_wakeup_sleep(ts);
   if (ret < 0)
       printk("resume power on failed\n");        
   gtp_irq_enable(ts);
}
 
/*******************************************************
Function:
Early suspend function.
 
Input:
h:early_suspend struct.
 
Output:
None.
 *******************************************************/
#ifdef CONFIG_HAS_EARLYSUSPEND
static void goodix_ts_early_suspend(struct early_suspend *h)
{
   struct goodix_ts_data *ts;
   s8 ret = -1;    
   ts = container_of(h, struct goodix_ts_data, early_suspend);
 
#if GTP_ESD_PROTECT
   ts->gtp_is_suspend = 1;
   cancel_delayed_work_sync(&gtp_esd_check_work);
#endif
 
   gtp_irq_disable(ts);
 
 
   cancel_work_sync(&goodix_resume_work);
   flush_workqueue(goodix_resume_wq);
   ret = cancel_work_sync(&ts->work);
   flush_workqueue(goodix_wq);
 
   ret = gtp_enter_sleep(ts);
   if (ret < 0) {
       printk("GTP early suspend failed.");
   }
}
 
/*******************************************************
Function:
Late resume function.
 
Input:
h:early_suspend struct.
 
Output:
None.
 *******************************************************/
static void goodix_ts_late_resume(struct early_suspend *h)
{
   struct goodix_ts_data *ts;
   ts = container_of(h, struct goodix_ts_data, early_suspend);
 
   queue_work(goodix_resume_wq, &goodix_resume_work);//gandy
 
#if GTP_ESD_PROTECT
   ts->gtp_is_suspend = 0;
   queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
#endif
}
#else
#ifdef CONFIG_PM
static int goodix_ts_suspend(struct device *dev)
{
   struct goodix_ts_data *ts = dev_get_drvdata(dev);
   s8 ret = -1;
 
   printk("%s goodix_ts_suspend\n", goodix_ts_name);
#if GTP_ESD_PROTECT
   ts->gtp_is_suspend = 1;
   cancel_delayed_work_sync(&gtp_esd_check_work);
#endif
 
   /*
      ret = input_set_int_enable(&(config_info.input_type), 0);
      if (ret < 0)
      dprintk(DEBUG_SUSPEND,"%s irq disable failed\n", goodix_ts_name);
      */
   gtp_irq_disable(ts);
   cancel_work_sync(&goodix_resume_work);
   flush_workqueue(goodix_resume_wq);
   ret = cancel_work_sync(&ts->work);
   flush_workqueue(goodix_wq);
 
   ret = gtp_enter_sleep(ts);
   if (ret < 0) {
       printk("GTP suspend failed.");
   }
 
   msleep(58);
 
   input_set_power_enable(&(config_info.input_type), 0);
   __gpio_set_value(config_info.wakeup_gpio.gpio, 0);
   return 0;
}
 
static int goodix_ts_resume(struct device *dev)
{
   printk("%s goodix_ts_resume\n", goodix_ts_name);
   __gpio_set_value(config_info.wakeup_gpio.gpio, 1);
   input_set_power_enable(&(config_info.input_type), 1);
   msleep(10);
   queue_work(goodix_resume_wq, &goodix_resume_work);//gandy
 
#if GTP_ESD_PROTECT
   struct goodix_ts_data *ts = dev_get_drvdata(dev);
   ts->gtp_is_suspend = 0;
   queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
#endif
   return 0;
}
#endif
#endif
 
#if GTP_ESD_PROTECT
/*******************************************************
Function:
Initialize external watchdog for esd protect
Input:
client:  i2c device.
Output:
result of i2c write operation. 
1: succeed, otherwise: failed
 *********************************************************/
s32 gtp_init_ext_watchdog(struct i2c_client *client)
{
   u8 opr_buffer[4] = {0x80, 0x40, 0xAA, 0xAA};
   dprintk(DEBUG_INIT, "Init external watchdog...");
   return gtp_i2c_write(client, opr_buffer, 4);
}
/*******************************************************
Function:
Esd protect function.
Added external watchdog by meta, 2013/03/07
Input:
work: delayed work
Output:
None.
 *******************************************************/
static void gtp_esd_check_func(struct work_struct *work)
{
   s32 i;
   s32 ret = -1;
   struct goodix_ts_data *ts = NULL;
   u8 test[4] = {0x80, 0x40};
 
   dprintk(DEBUG_INIT, "enter %s work!\n", __func__);
 
   ts = i2c_get_clientdata(i2c_connect_client);
 
   if (ts->gtp_is_suspend || ts->enter_update) {
       return;
   }
 
   for (i = 0; i < 3; i++) {
       ret = gtp_i2c_read(ts->client, test, 4);
 
       dprintk(DEBUG_INIT, "0x8040 = 0x%02X, 0x8041 = 0x%02X", test[2], test[3]);
       if ((ret < 0)) {
           // IC works abnormally..
           continue;
       }else { 
           if ((test[2] == 0xAA) || (test[3] != 0xAA)) {
               // IC works abnormally..
               i = 3;
               break;  
           }else {
               // IC works normally, Write 0x8040 0xAA
               test[2] = 0xAA; 
               gtp_i2c_write(ts->client, test, 3);
               break;
           }
       }
   }
 
   if (i >= 3) {
       GTP_DEBUG("IC Working ABNORMALLY, Resetting Guitar...");
       //  gtp_reset_guitar(ts->client, 50);
   }
 
   if(!ts->gtp_is_suspend) {
       queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
   }
 
   return;
}
#endif
 
static const struct i2c_device_id goodix_ts_id[] = {
   { CTP_NAME, 0 },
   { }
};
 
#ifndef CONFIG_HAS_EARLYSUSPEND
#ifdef CONFIG_PM
static const struct dev_pm_ops gt9xx_pm_ops = {
   .suspend = goodix_ts_suspend,
   .resume = goodix_ts_resume,
};
 
#define GT9XX_PM_OPS (&gt9xx_pm_ops)
#endif
#endif
 
static struct i2c_driver goodix_ts_driver = {
   .class          = I2C_CLASS_HWMON,
   .probe          = goodix_ts_probe,
   .remove         = goodix_ts_remove,
   .id_table       = goodix_ts_id,
   .driver = {
       .name   = CTP_NAME,
       .owner  = THIS_MODULE,
// #ifndef CONFIG_HAS_EARLYSUSPEND
// #ifdef CONFIG_PM
//         .pm = GT9XX_PM_OPS,
// #endif
// #endif
   },
   .address_list    = normal_i2c,
};
 
static int ctp_get_system_config(void)
{   
   ctp_print_info(config_info,DEBUG_INIT);
   twi_id = config_info.twi_id;
/*
   screen_max_x = config_info.screen_max_x;
   screen_max_y = config_info.screen_max_y;
   revert_x_flag = config_info.revert_x_flag;
   revert_y_flag = config_info.revert_y_flag;
   exchange_x_y_flag = config_info.exchange_x_y_flag; 
*/
   if((screen_max_x == 0) || (screen_max_y == 0)){
       printk("%s:read config error!\n",__func__);
       return 0;
   }
   return 1;
}
 
/*******************************************************
 * Function:
 *     Driver Install function.
 *     Input:
 *         None.
 *         Output:
 *             Executive Outcomes. 0---succeed.
 *             ********************************************************/
static int __init goodix_ts_init(void)
{
   s32 ret = -1;
   int val = 0;
   struct device_node *np = NULL;
 
   dprintk(DEBUG_INIT,"GTP driver init\n");
   if (!input_sensor_startup(&(config_info.input_type))) {
       ret = input_sensor_init(&(config_info.input_type));
       if (ret != 0) {
           pr_err("%s:ctp_ops.input_sensor_init err.\n", __func__);
           return ret;
       }
       input_set_power_enable(&(config_info.input_type), 1);
   } else {
       pr_err("%s: input_ctp_startup err.\n", __func__);
       return 0;
   }
   if(config_info.ctp_used == 0){
       printk("*** ctp_used set to 0 !\n");
       printk("*** if use ctp,please put the sys_config.fex ctp_used set to 1. \n");
       return 0;
   }
 
   np = of_find_node_by_name(NULL, "ctp");
   if (!np) {
       pr_err("ERROR! get ctp node failed, func:%s, line:%d\n",__FUNCTION__, __LINE__);
       return -1;
   }
 
   ret = of_property_read_u32(np, "ctp_gesture_wakeup", &val);
   if (ret) {
       pr_err("get ctp_screen_max_x is fail, %d\n", ret);
   }
   msleep(10);
#if 0
   if(!ctp_get_system_config()){
       printk("%s:read config fail!\n",__func__);
       return ret;
   }
#else
   //ret = of_property_read_u32(np, "ctp_screen_max_x", &screen_max_x);
   //of_property_read_u32(np, "ctp_screen_max_y", &screen_max_y);
   //of_property_read_u32(np, "ctp_revert_x_flag", &revert_x_flag);
   //of_property_read_u32(np, "ctp_revert_y_flag", &revert_y_flag);
   //of_property_read_u32(np, "ctp_exchange_x_y_flag", &exchange_x_y_flag);
#endif
   sunxi_gpio_to_name(CTP_IRQ_NUMBER,irq_pin_name);
   pr_err("gt %s, line:%d irq_pin_name = %s \n",__FUNCTION__, __LINE__, irq_pin_name);
   gtp_io_init(20);
 
   goodix_wq = create_singlethread_workqueue("goodix_wq");
   if (!goodix_wq)
   {
       GTP_ERROR("Creat workqueue failed.");
       return -ENOMEM;
   }
#if GTP_ESD_PROTECT
   INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
   gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
#endif
   goodix_ts_driver.detect = ctp_detect;
   ret = i2c_add_driver(&goodix_ts_driver);
   return ret;
}
 
 
/*******************************************************    
Function:
Driver uninstall function.
Input:
None.
Output:
Executive Outcomes. 0---succeed.
 ********************************************************/
static void __exit goodix_ts_exit(void)
{
   printk("GTP driver exited.\n");
   i2c_del_driver(&goodix_ts_driver);
   input_sensor_free(&(config_info.input_type));
}
 
late_initcall(goodix_ts_init);
module_exit(goodix_ts_exit);
 
MODULE_DESCRIPTION("GTP Series Driver");
MODULE_LICENSE("GPL");