b4a538f772892da77aa78b42ea710f5b17f798ac
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / gt9xx / gt9xx.c
1 /* drivers/input/touchscreen/gt9xx.c
2  * 
3  * 2010 - 2013 Goodix Technology.
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be a reference 
11  * to you, when you are integrating the GOODiX's CTP IC into your system, 
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14  * General Public License for more details.
15  * 
16  * Version: 2.2
17  * Authors: andrew@goodix.com, meta@goodix.com
18  * Release Date: 2014/01/14
19  * Revision record:
20  *      V1.0:   
21  *          first Release. By Andrew, 2012/08/31 
22  *      V1.2:
23  *          modify gtp_reset_guitar,slot report,tracking_id & 0x0F. By Andrew, 2012/10/15
24  *      V1.4:
25  *          modify gt9xx_update.c. By Andrew, 2012/12/12
26  *      V1.6: 
27  *          1. new heartbeat/esd_protect mechanism(add external watchdog)
28  *          2. doze mode, sliding wakeup 
29  *          3. 3 more cfg_group(GT9 Sensor_ID: 0~5) 
30  *          3. config length verification
31  *          4. names & comments
32  *                  By Meta, 2013/03/11
33  *      V1.8:
34  *          1. pen/stylus identification 
35  *          2. read double check & fixed config support
36  *          3. new esd & slide wakeup optimization
37  *                  By Meta, 2013/06/08
38  *      V2.0:
39  *          1. compatible with GT9XXF
40  *          2. send config after resume
41  *                  By Meta, 2013/08/06
42  *      V2.2:
43  *          1. gt9xx_config for debug
44  *          2. gesture wakeup
45  *          3. pen separate input device, active-pen button support
46  *          4. coordinates & keys optimization
47  *                  By Meta, 2014/01/14
48  */
49
50 #include <linux/irq.h>
51 #include "gt9xx.h"
52 #include <linux/regulator/consumer.h>
53
54 #if GTP_ICS_SLOT_REPORT
55     #include <linux/input/mt.h>
56 #endif
57
58 static const char *goodix_ts_name = "goodix-ts";
59 static struct workqueue_struct *goodix_wq;
60 struct i2c_client * i2c_connect_client = NULL; 
61 u8 config[GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH]
62                 = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
63
64 #if GTP_HAVE_TOUCH_KEY
65     static const u16 touch_key_array[] = GTP_KEY_TAB;
66     #define GTP_MAX_KEY_NUM  (sizeof(touch_key_array)/sizeof(touch_key_array[0]))
67     
68 #if GTP_DEBUG_ON
69     static const int  key_codes[] = {KEY_HOME, KEY_BACK, KEY_MENU, KEY_SEARCH};
70     static const char *key_names[] = {"Key_Home", "Key_Back", "Key_Menu", "Key_Search"};
71 #endif
72     
73 #endif
74
75 static s8 gtp_i2c_test(struct i2c_client *client);
76 void gtp_reset_guitar(struct i2c_client *client, s32 ms);
77 s32 gtp_send_cfg(struct i2c_client *client);
78 void gtp_int_sync(s32 ms, struct goodix_ts_data *ts);
79
80 static ssize_t gt91xx_config_read_proc(struct file *, char __user *, size_t, loff_t *);
81 static ssize_t gt91xx_config_write_proc(struct file *, const char __user *, size_t, loff_t *);
82
83 static struct proc_dir_entry *gt91xx_config_proc = NULL;
84 static const struct file_operations config_proc_ops = {
85     .owner = THIS_MODULE,
86     .read = gt91xx_config_read_proc,
87     .write = gt91xx_config_write_proc,
88 };
89
90 #if GTP_CREATE_WR_NODE
91 extern s32 init_wr_node(struct i2c_client*);
92 extern void uninit_wr_node(void);
93 #endif
94
95 #if GTP_AUTO_UPDATE
96 extern u8 gup_init_update_proc(struct goodix_ts_data *);
97 #endif
98
99 #if GTP_ESD_PROTECT
100 static struct delayed_work gtp_esd_check_work;
101 static struct workqueue_struct * gtp_esd_check_workqueue = NULL;
102 static void gtp_esd_check_func(struct work_struct *);
103 static s32 gtp_init_ext_watchdog(struct i2c_client *client);
104 void gtp_esd_switch(struct i2c_client *, s32);
105 #endif
106
107 //*********** For GT9XXF Start **********//
108 #if GTP_COMPATIBLE_MODE
109 extern s32 i2c_read_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
110 extern s32 i2c_write_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
111 extern s32 gup_clk_calibration(void);
112 extern s32 gup_fw_download_proc(void *dir, u8 dwn_mode);
113 extern u8 gup_check_fs_mounted(char *path_name);
114
115 void gtp_recovery_reset(struct i2c_client *client);
116 static s32 gtp_esd_recovery(struct i2c_client *client);
117 s32 gtp_fw_startup(struct i2c_client *client);
118 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts);
119 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode);
120
121 #endif
122 //********** For GT9XXF End **********//
123
124 #if GTP_GESTURE_WAKEUP
125 typedef enum
126 {
127     DOZE_DISABLED = 0,
128     DOZE_ENABLED = 1,
129     DOZE_WAKEUP = 2,
130 }DOZE_T;
131 static DOZE_T doze_status = DOZE_DISABLED;
132 static s8 gtp_enter_doze(struct goodix_ts_data *ts);
133 #endif
134
135 u8 grp_cfg_version = 0;
136
137 /*******************************************************
138 Function:
139     Read data from the i2c slave device.
140 Input:
141     client:     i2c device.
142     buf[0~1]:   read start address.
143     buf[2~len-1]:   read data buffer.
144     len:    GTP_ADDR_LENGTH + read bytes count
145 Output:
146     numbers of i2c_msgs to transfer: 
147       2: succeed, otherwise: failed
148 *********************************************************/
149 s32 gtp_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
150 {
151     struct i2c_msg msgs[2];
152     s32 ret=-1;
153     s32 retries = 0;
154
155     GTP_DEBUG_FUNC();
156
157     msgs[0].flags = !I2C_M_RD;
158     msgs[0].addr  = client->addr;
159     msgs[0].len   = GTP_ADDR_LENGTH;
160     msgs[0].buf   = &buf[0];
161 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
162     msgs[0].scl_rate=200 * 1000;
163     //msgs[0].scl_rate = 300 * 1000;    // for Rockchip, etc.
164 #endif
165     msgs[1].flags = I2C_M_RD;
166     msgs[1].addr  = client->addr;
167     msgs[1].len   = len - GTP_ADDR_LENGTH;
168     msgs[1].buf   = &buf[GTP_ADDR_LENGTH];
169 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
170     msgs[1].scl_rate=200 * 1000;
171     //msgs[1].scl_rate = 300 * 1000;    // for Rockchip, etc.
172 #endif
173
174     while(retries < 5)
175     {
176         ret = i2c_transfer(client->adapter, msgs, 2);
177         if(ret == 2)break;
178         retries++;
179     }
180     if((retries >= 5))
181     {
182     #if GTP_COMPATIBLE_MODE
183         struct goodix_ts_data *ts = i2c_get_clientdata(client);
184     #endif
185     
186     #if GTP_GESTURE_WAKEUP
187         // reset chip would quit doze mode
188         if (DOZE_ENABLED == doze_status)
189         {
190             return ret;
191         }
192     #endif
193         GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
194     #if GTP_COMPATIBLE_MODE
195         if (CHIP_TYPE_GT9F == ts->chip_type)
196         { 
197             gtp_recovery_reset(client);
198         }
199         else
200     #endif
201         {
202             gtp_reset_guitar(client, 10);  
203         }
204     }
205     return ret;
206 }
207
208
209
210 /*******************************************************
211 Function:
212     Write data to the i2c slave device.
213 Input:
214     client:     i2c device.
215     buf[0~1]:   write start address.
216     buf[2~len-1]:   data buffer
217     len:    GTP_ADDR_LENGTH + write bytes count
218 Output:
219     numbers of i2c_msgs to transfer: 
220         1: succeed, otherwise: failed
221 *********************************************************/
222 s32 gtp_i2c_write(struct i2c_client *client,u8 *buf,s32 len)
223 {
224     struct i2c_msg msg;
225     s32 ret = -1;
226     s32 retries = 0;
227
228     GTP_DEBUG_FUNC();
229
230     msg.flags = !I2C_M_RD;
231     msg.addr  = client->addr;
232     msg.len   = len;
233     msg.buf   = buf;
234 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
235     msg.scl_rate=200 * 1000;
236     //msg.scl_rate = 300 * 1000;    // for Rockchip, etc
237 #endif
238     while(retries < 5)
239     {
240         ret = i2c_transfer(client->adapter, &msg, 1);
241         if (ret == 1)break;
242         retries++;
243     }
244     if((retries >= 5))
245     {
246     #if GTP_COMPATIBLE_MODE
247         struct goodix_ts_data *ts = i2c_get_clientdata(client);
248     #endif
249     
250     #if GTP_GESTURE_WAKEUP
251         if (DOZE_ENABLED == doze_status)
252         {
253             return ret;
254         }
255     #endif
256         GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
257     #if GTP_COMPATIBLE_MODE
258         if (CHIP_TYPE_GT9F == ts->chip_type)
259         { 
260             gtp_recovery_reset(client);
261         }
262         else
263     #endif
264         {
265             gtp_reset_guitar(client, 10);  
266         }
267     }
268     return ret;
269 }
270
271
272 /*******************************************************
273 Function:
274     i2c read twice, compare the results
275 Input:
276     client:  i2c device
277     addr:    operate address
278     rxbuf:   read data to store, if compare successful
279     len:     bytes to read
280 Output:
281     FAIL:    read failed
282     SUCCESS: read successful
283 *********************************************************/
284 s32 gtp_i2c_read_dbl_check(struct i2c_client *client, u16 addr, u8 *rxbuf, int len)
285 {
286     u8 buf[16] = {0};
287     u8 confirm_buf[16] = {0};
288     u8 retry = 0;
289     
290     while (retry++ < 3)
291     {
292         memset(buf, 0xAA, 16);
293         buf[0] = (u8)(addr >> 8);
294         buf[1] = (u8)(addr & 0xFF);
295         gtp_i2c_read(client, buf, len + 2);
296         
297         memset(confirm_buf, 0xAB, 16);
298         confirm_buf[0] = (u8)(addr >> 8);
299         confirm_buf[1] = (u8)(addr & 0xFF);
300         gtp_i2c_read(client, confirm_buf, len + 2);
301         
302         if (!memcmp(buf, confirm_buf, len+2))
303         {
304             memcpy(rxbuf, confirm_buf+2, len);
305             return SUCCESS;
306         }
307     }    
308     GTP_ERROR("I2C read 0x%04X, %d bytes, double check failed!", addr, len);
309     return FAIL;
310 }
311
312 /*******************************************************
313 Function:
314     Send config.
315 Input:
316     client: i2c device.
317 Output:
318     result of i2c write operation. 
319         1: succeed, otherwise: failed
320 *********************************************************/
321
322 s32 gtp_send_cfg(struct i2c_client *client)
323 {
324     s32 ret = 2;
325
326 #if GTP_DRIVER_SEND_CFG
327     s32 retry = 0;
328     struct goodix_ts_data *ts = i2c_get_clientdata(client);
329
330     if (ts->fixed_cfg)
331     {
332         GTP_INFO("Ic fixed config, no config sent!");
333         return 0;
334     }
335     else if (ts->pnl_init_error)
336     {
337         GTP_INFO("Error occured in init_panel, no config sent");
338         return 0;
339     }
340     
341     GTP_INFO("Driver send config.");
342     for (retry = 0; retry < 5; retry++)
343     {
344         ret = gtp_i2c_write(client, config , GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH);
345         if (ret > 0)
346         {
347             break;
348         }
349     }
350 #endif
351     return ret;
352 }
353 /*******************************************************
354 Function:
355     Disable irq function
356 Input:
357     ts: goodix i2c_client private data
358 Output:
359     None.
360 *********************************************************/
361 void gtp_irq_disable(struct goodix_ts_data *ts)
362 {
363     unsigned long irqflags;
364
365     GTP_DEBUG_FUNC();
366
367     spin_lock_irqsave(&ts->irq_lock, irqflags);
368     if (!ts->irq_is_disable)
369     {
370         ts->irq_is_disable = 1; 
371         disable_irq_nosync(ts->client->irq);
372     }
373     spin_unlock_irqrestore(&ts->irq_lock, irqflags);
374 }
375
376 /*******************************************************
377 Function:
378     Enable irq function
379 Input:
380     ts: goodix i2c_client private data
381 Output:
382     None.
383 *********************************************************/
384 void gtp_irq_enable(struct goodix_ts_data *ts)
385 {
386     unsigned long irqflags = 0;
387
388     GTP_DEBUG_FUNC();
389     
390     spin_lock_irqsave(&ts->irq_lock, irqflags);
391     if (ts->irq_is_disable) 
392     {
393         enable_irq(ts->client->irq);
394         ts->irq_is_disable = 0; 
395     }
396     spin_unlock_irqrestore(&ts->irq_lock, irqflags);
397 }
398
399
400 /*******************************************************
401 Function:
402     Report touch point event 
403 Input:
404     ts: goodix i2c_client private data
405     id: trackId
406     x:  input x coordinate
407     y:  input y coordinate
408     w:  input pressure
409 Output:
410     None.
411 *********************************************************/
412 static void gtp_touch_down(struct goodix_ts_data* ts,s32 id,s32 x,s32 y,s32 w)
413 {
414     if(mGtpChange_X2Y){
415         GTP_SWAP(x, y);
416     }
417
418         if (!bgt911 && !bgt970) {
419             if(mGtp_X_Reverse){
420                 x = ts->abs_x_max - x;
421             }
422         
423             if(mGtp_Y_Reverse){
424                 y = ts->abs_y_max - y;
425             }
426         }
427
428 #if GTP_ICS_SLOT_REPORT
429     input_mt_slot(ts->input_dev, id);
430     input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
431     input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
432     input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
433     input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
434     input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
435 #else
436     input_report_key(ts->input_dev, BTN_TOUCH, 1);
437     input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
438     input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
439     input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
440     input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
441     input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
442     input_mt_sync(ts->input_dev);
443 #endif
444
445     GTP_DEBUG("ID:%d, X:%d, Y:%d, W:%d", id, x, y, w);
446 }
447
448 /*******************************************************
449 Function:
450     Report touch release event
451 Input:
452     ts: goodix i2c_client private data
453 Output:
454     None.
455 *********************************************************/
456 static void gtp_touch_up(struct goodix_ts_data* ts, s32 id)
457 {
458 #if GTP_ICS_SLOT_REPORT
459     input_mt_slot(ts->input_dev, id);
460     input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
461     GTP_DEBUG("Touch id[%2d] release!", id);
462 #else
463     input_report_key(ts->input_dev, BTN_TOUCH, 0);
464 #endif
465 }
466
467 #if GTP_WITH_PEN
468
469 static void gtp_pen_init(struct goodix_ts_data *ts)
470 {
471     s32 ret = 0;
472     
473     GTP_INFO("Request input device for pen/stylus.");
474     
475     ts->pen_dev = input_allocate_device();
476     if (ts->pen_dev == NULL)
477     {
478         GTP_ERROR("Failed to allocate input device for pen/stylus.");
479         return;
480     }
481     
482     ts->pen_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
483     
484 #if GTP_ICS_SLOT_REPORT
485     input_mt_init_slots(ts->pen_dev, 16, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
486 #else
487     ts->pen_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
488 #endif
489
490     set_bit(BTN_TOOL_PEN, ts->pen_dev->keybit);
491     set_bit(INPUT_PROP_DIRECT, ts->pen_dev->propbit);
492     //set_bit(INPUT_PROP_POINTER, ts->pen_dev->propbit);
493     
494 #if GTP_PEN_HAVE_BUTTON
495     input_set_capability(ts->pen_dev, EV_KEY, BTN_STYLUS);
496     input_set_capability(ts->pen_dev, EV_KEY, BTN_STYLUS2);
497 #endif
498
499     input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
500     input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
501     input_set_abs_params(ts->pen_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
502     input_set_abs_params(ts->pen_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
503     input_set_abs_params(ts->pen_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
504     
505     ts->pen_dev->name = "goodix-pen";
506     ts->pen_dev->id.bustype = BUS_I2C;
507     
508     ret = input_register_device(ts->pen_dev);
509     if (ret)
510     {
511         GTP_ERROR("Register %s input device failed", ts->pen_dev->name);
512         return;
513     }
514 }
515
516 static void gtp_pen_down(s32 x, s32 y, s32 w, s32 id)
517 {
518     struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
519
520     if(mGtpChange_X2Y){
521         GTP_SWAP(x, y);
522     }
523     
524     input_report_key(ts->pen_dev, BTN_TOOL_PEN, 1);
525 #if GTP_ICS_SLOT_REPORT
526     input_mt_slot(ts->pen_dev, id);
527     input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, id);
528     input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
529     input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
530     input_report_abs(ts->pen_dev, ABS_MT_PRESSURE, w);
531     input_report_abs(ts->pen_dev, ABS_MT_TOUCH_MAJOR, w);
532 #else
533     input_report_key(ts->pen_dev, BTN_TOUCH, 1);
534     input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
535     input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
536     input_report_abs(ts->pen_dev, ABS_MT_PRESSURE, w);
537     input_report_abs(ts->pen_dev, ABS_MT_TOUCH_MAJOR, w);
538     input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, id);
539     input_mt_sync(ts->pen_dev);
540 #endif
541     GTP_DEBUG("(%d)(%d, %d)[%d]", id, x, y, w);
542 }
543
544 static void gtp_pen_up(s32 id)
545 {
546     struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
547     
548     input_report_key(ts->pen_dev, BTN_TOOL_PEN, 0);
549     
550 #if GTP_ICS_SLOT_REPORT
551     input_mt_slot(ts->pen_dev, id);
552     input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, -1);
553 #else
554     
555     input_report_key(ts->pen_dev, BTN_TOUCH, 0);
556 #endif
557
558 }
559 #endif
560
561 /*******************************************************
562 Function:
563     Goodix touchscreen work function
564 Input:
565     work: work struct of goodix_workqueue
566 Output:
567     None.
568 *********************************************************/
569 static void goodix_ts_work_func(struct work_struct *work)
570 {
571     u8  end_cmd[3] = {GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF, 0};
572     u8  point_data[2 + 1 + 8 * GTP_MAX_TOUCH + 1]={GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF};
573     u8  touch_num = 0;
574     u8  finger = 0;
575     static u16 pre_touch = 0;
576     static u8 pre_key = 0;
577 #if GTP_WITH_PEN
578     u8 pen_active = 0;
579     static u8 pre_pen = 0;
580 #endif
581     u8  key_value = 0;
582     u8* coor_data = NULL;
583     s32 input_x = 0;
584     s32 input_y = 0;
585     s32 input_w = 0;
586     s32 id = 0;
587     s32 i  = 0;
588     s32 ret = -1;
589     struct goodix_ts_data *ts = NULL;
590
591 #if GTP_COMPATIBLE_MODE
592     u8 rqst_buf[3] = {0x80, 0x43};  // for GT9XXF
593 #endif
594
595 #if GTP_GESTURE_WAKEUP
596     u8 doze_buf[3] = {0x81, 0x4B};
597 #endif
598
599     GTP_DEBUG_FUNC();
600     ts = container_of(work, struct goodix_ts_data, work);
601     if (ts->enter_update)
602     {
603         return;
604     }
605 #if GTP_GESTURE_WAKEUP
606     if (DOZE_ENABLED == doze_status)
607     {               
608         ret = gtp_i2c_read(i2c_connect_client, doze_buf, 3);
609         GTP_DEBUG("0x814B = 0x%02X", doze_buf[2]);
610         if (ret > 0)
611         {     
612             if ((doze_buf[2] == 'a') || (doze_buf[2] == 'b') || (doze_buf[2] == 'c') ||
613                 (doze_buf[2] == 'd') || (doze_buf[2] == 'e') || (doze_buf[2] == 'g') || 
614                 (doze_buf[2] == 'h') || (doze_buf[2] == 'm') || (doze_buf[2] == 'o') ||
615                 (doze_buf[2] == 'q') || (doze_buf[2] == 's') || (doze_buf[2] == 'v') || 
616                 (doze_buf[2] == 'w') || (doze_buf[2] == 'y') || (doze_buf[2] == 'z') ||
617                 (doze_buf[2] == 0x5E) /* ^ */
618                 )
619             {
620                 if (doze_buf[2] != 0x5E)
621                 {
622                     GTP_INFO("Wakeup by gesture(%c), light up the screen!", doze_buf[2]);
623                 }
624                 else
625                 {
626                     GTP_INFO("Wakeup by gesture(^), light up the screen!");
627                 }
628                 doze_status = DOZE_WAKEUP;
629                 input_report_key(ts->input_dev, KEY_POWER, 1);
630                 input_sync(ts->input_dev);
631                 input_report_key(ts->input_dev, KEY_POWER, 0);
632                 input_sync(ts->input_dev);
633                 // clear 0x814B
634                 doze_buf[2] = 0x00;
635                 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
636                         }
637                         else if ( (doze_buf[2] == 0xAA) || (doze_buf[2] == 0xBB) ||
638                                 (doze_buf[2] == 0xAB) || (doze_buf[2] == 0xBA) )
639             {
640                 char *direction[4] = {"Right", "Down", "Up", "Left"};
641                 u8 type = ((doze_buf[2] & 0x0F) - 0x0A) + (((doze_buf[2] >> 4) & 0x0F) - 0x0A) * 2;
642                 
643                 GTP_INFO("%s slide to light up the screen!", direction[type]);
644                 doze_status = DOZE_WAKEUP;
645                 input_report_key(ts->input_dev, KEY_POWER, 1);
646                 input_sync(ts->input_dev);
647                 input_report_key(ts->input_dev, KEY_POWER, 0);
648                 input_sync(ts->input_dev);
649                 // clear 0x814B
650                 doze_buf[2] = 0x00;
651                 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
652             }
653             else if (0xCC == doze_buf[2])
654             {
655                 GTP_INFO("Double click to light up the screen!");
656                 doze_status = DOZE_WAKEUP;
657                 input_report_key(ts->input_dev, KEY_POWER, 1);
658                 input_sync(ts->input_dev);
659                 input_report_key(ts->input_dev, KEY_POWER, 0);
660                 input_sync(ts->input_dev);
661                 // clear 0x814B
662                 doze_buf[2] = 0x00;
663                 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
664             }
665             else
666             {
667                 // clear 0x814B
668                 doze_buf[2] = 0x00;
669                 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
670                 gtp_enter_doze(ts);
671             }
672         }
673         if (ts->use_irq)
674         {
675             gtp_irq_enable(ts);
676         }
677         return;
678     }
679 #endif
680
681     ret = gtp_i2c_read(ts->client, point_data, 12);
682     if (ret < 0)
683     {
684         GTP_ERROR("I2C transfer error. errno:%d\n ", ret);
685         if (ts->use_irq)
686         {
687             gtp_irq_enable(ts);
688         }
689         return;
690     }
691     
692     finger = point_data[GTP_ADDR_LENGTH];
693
694 #if GTP_COMPATIBLE_MODE
695     // GT9XXF
696     if ((finger == 0x00) && (CHIP_TYPE_GT9F == ts->chip_type))     // request arrived
697     {
698         ret = gtp_i2c_read(ts->client, rqst_buf, 3);
699         if (ret < 0)
700         {
701            GTP_ERROR("Read request status error!");
702            goto exit_work_func;
703         } 
704         
705         switch (rqst_buf[2])
706         {
707         case GTP_RQST_CONFIG:
708             GTP_INFO("Request for config.");
709             ret = gtp_send_cfg(ts->client);
710             if (ret < 0)
711             {
712                 GTP_ERROR("Request for config unresponded!");
713             }
714             else
715             {
716                 rqst_buf[2] = GTP_RQST_RESPONDED;
717                 gtp_i2c_write(ts->client, rqst_buf, 3);
718                 GTP_INFO("Request for config responded!");
719             }
720             break;
721             
722         case GTP_RQST_BAK_REF:
723             GTP_INFO("Request for backup reference.");
724             ts->rqst_processing = 1;
725             ret = gtp_bak_ref_proc(ts, GTP_BAK_REF_SEND);
726             if (SUCCESS == ret)
727             {
728                 rqst_buf[2] = GTP_RQST_RESPONDED;
729                 gtp_i2c_write(ts->client, rqst_buf, 3);
730                 ts->rqst_processing = 0;
731                 GTP_INFO("Request for backup reference responded!");
732             }
733             else
734             {
735                 GTP_ERROR("Requeset for backup reference unresponed!");
736             }
737             break;
738             
739         case GTP_RQST_RESET:
740             GTP_INFO("Request for reset.");
741             gtp_recovery_reset(ts->client);
742             break;
743             
744         case GTP_RQST_MAIN_CLOCK:
745             GTP_INFO("Request for main clock.");
746             ts->rqst_processing = 1;
747             ret = gtp_main_clk_proc(ts);
748             if (FAIL == ret)
749             {
750                 GTP_ERROR("Request for main clock unresponded!");
751             }
752             else
753             {
754                 GTP_INFO("Request for main clock responded!");
755                 rqst_buf[2] = GTP_RQST_RESPONDED;
756                 gtp_i2c_write(ts->client, rqst_buf, 3);
757                 ts->rqst_processing = 0;
758                 ts->clk_chk_fs_times = 0;
759             }
760             break;
761             
762         default:
763             GTP_INFO("Undefined request: 0x%02X", rqst_buf[2]);
764             rqst_buf[2] = GTP_RQST_RESPONDED;  
765             gtp_i2c_write(ts->client, rqst_buf, 3);
766             break;
767         }
768     }
769 #endif
770     if (finger == 0x00)
771     {
772         if (ts->use_irq)
773         {
774             gtp_irq_enable(ts);
775         }
776         return;
777     }
778
779     if((finger & 0x80) == 0)
780     {
781         goto exit_work_func;
782     }
783
784     touch_num = finger & 0x0f;
785     if (touch_num > GTP_MAX_TOUCH)
786     {
787         goto exit_work_func;
788     }
789
790     if (touch_num > 1)
791     {
792         u8 buf[8 * GTP_MAX_TOUCH] = {(GTP_READ_COOR_ADDR + 10) >> 8, (GTP_READ_COOR_ADDR + 10) & 0xff};
793
794         ret = gtp_i2c_read(ts->client, buf, 2 + 8 * (touch_num - 1)); 
795         memcpy(&point_data[12], &buf[2], 8 * (touch_num - 1));
796     }
797
798 #if (GTP_HAVE_TOUCH_KEY || GTP_PEN_HAVE_BUTTON)
799     key_value = point_data[3 + 8 * touch_num];
800     
801     if(key_value || pre_key)
802     {
803     #if GTP_PEN_HAVE_BUTTON
804         if (key_value == 0x40)
805         {
806             GTP_DEBUG("BTN_STYLUS & BTN_STYLUS2 Down.");
807             input_report_key(ts->pen_dev, BTN_STYLUS, 1);
808             input_report_key(ts->pen_dev, BTN_STYLUS2, 1);
809             pen_active = 1;
810         }
811         else if (key_value == 0x10)
812         {
813             GTP_DEBUG("BTN_STYLUS Down, BTN_STYLUS2 Up.");
814             input_report_key(ts->pen_dev, BTN_STYLUS, 1);
815             input_report_key(ts->pen_dev, BTN_STYLUS2, 0);
816             pen_active = 1;
817         }
818         else if (key_value == 0x20)
819         {
820             GTP_DEBUG("BTN_STYLUS Up, BTN_STYLUS2 Down.");
821             input_report_key(ts->pen_dev, BTN_STYLUS, 0);
822             input_report_key(ts->pen_dev, BTN_STYLUS2, 1);
823             pen_active = 1;
824         }
825         else
826         {
827             GTP_DEBUG("BTN_STYLUS & BTN_STYLUS2 Up.");
828             input_report_key(ts->pen_dev, BTN_STYLUS, 0);
829             input_report_key(ts->pen_dev, BTN_STYLUS2, 0);
830             if ( (pre_key == 0x40) || (pre_key == 0x20) ||
831                  (pre_key == 0x10) 
832                )
833             {
834                 pen_active = 1;
835             }
836         }
837         if (pen_active)
838         {
839             touch_num = 0;      // shield pen point
840             //pre_touch = 0;    // clear last pen status
841         }
842     #endif
843     
844     #if GTP_HAVE_TOUCH_KEY
845         if (!pre_touch)
846         {
847             for (i = 0; i < GTP_MAX_KEY_NUM; i++)
848             {
849             #if GTP_DEBUG_ON
850                 for (ret = 0; ret < 4; ++ret)
851                 {
852                     if (key_codes[ret] == touch_key_array[i])
853                     {
854                         GTP_DEBUG("Key: %s %s", key_names[ret], (key_value & (0x01 << i)) ? "Down" : "Up");
855                         break;
856                     }
857                 }
858             #endif
859                 input_report_key(ts->input_dev, touch_key_array[i], key_value & (0x01<<i));   
860             }
861             touch_num = 0;  // shield fingers
862         }
863     #endif
864     }
865 #endif
866     pre_key = key_value;
867
868     GTP_DEBUG("pre_touch:%02x, finger:%02x.", pre_touch, finger);
869
870 #if GTP_ICS_SLOT_REPORT
871
872 #if GTP_WITH_PEN
873     if (pre_pen && (touch_num == 0))
874     {
875         GTP_DEBUG("Pen touch UP(Slot)!");
876         gtp_pen_up(0);
877         pen_active = 1;
878         pre_pen = 0;
879     }
880 #endif
881     if (pre_touch || touch_num)
882     {
883         s32 pos = 0;
884         u16 touch_index = 0;
885         u8 report_num = 0;
886         coor_data = &point_data[3];
887         
888         if(touch_num)
889         {
890             id = coor_data[pos] & 0x0F;
891         
892         #if GTP_WITH_PEN
893             id = coor_data[pos];
894             if ((id & 0x80))  
895             {
896                 GTP_DEBUG("Pen touch DOWN(Slot)!");
897                 input_x  = coor_data[pos + 1] | (coor_data[pos + 2] << 8);
898                 input_y  = coor_data[pos + 3] | (coor_data[pos + 4] << 8);
899                 input_w  = coor_data[pos + 5] | (coor_data[pos + 6] << 8);
900                 
901                 gtp_pen_down(input_x, input_y, input_w, 0);
902                 pre_pen = 1;
903                 pre_touch = 0;
904                 pen_active = 1;
905             }    
906         #endif
907         
908             touch_index |= (0x01<<id);
909         }
910         
911         GTP_DEBUG("id = %d,touch_index = 0x%x, pre_touch = 0x%x\n",id, touch_index,pre_touch);
912         for (i = 0; i < GTP_MAX_TOUCH; i++)
913         {
914         #if GTP_WITH_PEN
915             if (pre_pen == 1)
916             {
917                 break;
918             }
919         #endif
920         
921             if ((touch_index & (0x01<<i)))
922             {
923                 input_x  = coor_data[pos + 1] | (coor_data[pos + 2] << 8);
924                 input_y  = coor_data[pos + 3] | (coor_data[pos + 4] << 8);
925                 input_w  = coor_data[pos + 5] | (coor_data[pos + 6] << 8);
926
927                 gtp_touch_down(ts, id, input_x, input_y, input_w);
928                 pre_touch |= 0x01 << i;
929                 
930                 report_num++;
931                 if (report_num < touch_num)
932                 {
933                     pos += 8;
934                     id = coor_data[pos] & 0x0F;
935                     touch_index |= (0x01<<id);
936                 }
937             }
938             else
939             {
940                 gtp_touch_up(ts, i);
941                 pre_touch &= ~(0x01 << i);
942             }
943         }
944     }
945 #else   //end if GTP_ICS_SLOT_REPORT
946
947     if (touch_num)
948     {
949         for (i = 0; i < touch_num; i++)
950         {
951             coor_data = &point_data[i * 8 + 3];
952
953             id = coor_data[0] & 0x0F;
954             input_x  = coor_data[1] | (coor_data[2] << 8);
955             input_y  = coor_data[3] | (coor_data[4] << 8);
956             input_w  = coor_data[5] | (coor_data[6] << 8);
957         
958         #if GTP_WITH_PEN
959             id = coor_data[0];
960             if (id & 0x80)
961             {
962                 GTP_DEBUG("Pen touch DOWN!");
963                 gtp_pen_down(input_x, input_y, input_w, 0);
964                 pre_pen = 1;
965                 pen_active = 1;
966                 break;
967             }
968             else
969         #endif
970             {
971                 gtp_touch_down(ts, id, input_x, input_y, input_w);
972             }
973         }
974     }
975     else if (pre_touch)
976     {
977     #if GTP_WITH_PEN
978         if (pre_pen == 1)
979         {
980             GTP_DEBUG("Pen touch UP!");
981             gtp_pen_up(0);
982             pre_pen = 0;
983             pen_active = 1;
984         }
985         else
986     #endif
987         {
988             GTP_DEBUG("Touch Release!");
989             gtp_touch_up(ts, 0);
990         }
991     }
992
993     pre_touch = touch_num;
994 #endif
995
996 #if GTP_WITH_PEN
997     if (pen_active)
998     {
999         pen_active = 0;
1000         input_sync(ts->pen_dev);
1001     }
1002     else
1003 #endif
1004     {
1005         input_sync(ts->input_dev);
1006     }
1007
1008 exit_work_func:
1009     if(!ts->gtp_rawdiff_mode)
1010     {
1011         ret = gtp_i2c_write(ts->client, end_cmd, 3);
1012         if (ret < 0)
1013         {
1014             GTP_INFO("I2C write end_cmd error!");
1015         }
1016     }
1017     if (ts->use_irq)
1018     {
1019         gtp_irq_enable(ts);
1020     }
1021 }
1022
1023 /*******************************************************
1024 Function:
1025     Timer interrupt service routine for polling mode.
1026 Input:
1027     timer: timer struct pointer
1028 Output:
1029     Timer work mode. 
1030         HRTIMER_NORESTART: no restart mode
1031 *********************************************************/
1032 static enum hrtimer_restart goodix_ts_timer_handler(struct hrtimer *timer)
1033 {
1034     struct goodix_ts_data *ts = container_of(timer, struct goodix_ts_data, timer);
1035
1036     GTP_DEBUG_FUNC();
1037
1038     queue_work(goodix_wq, &ts->work);
1039     hrtimer_start(&ts->timer, ktime_set(0, (GTP_POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
1040     return HRTIMER_NORESTART;
1041 }
1042
1043 /*******************************************************
1044 Function:
1045     External interrupt service routine for interrupt mode.
1046 Input:
1047     irq:  interrupt number.
1048     dev_id: private data pointer
1049 Output:
1050     Handle Result.
1051         IRQ_HANDLED: interrupt handled successfully
1052 *********************************************************/
1053 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
1054 {
1055     struct goodix_ts_data *ts = dev_id;
1056
1057     GTP_DEBUG_FUNC();
1058  
1059     gtp_irq_disable(ts);
1060
1061     queue_work(goodix_wq, &ts->work);
1062     
1063     return IRQ_HANDLED;
1064 }
1065 /*******************************************************
1066 Function:
1067     Synchronization.
1068 Input:
1069     ms: synchronization time in millisecond.
1070 Output:
1071     None.
1072 *******************************************************/
1073 void gtp_int_sync(s32 ms, struct goodix_ts_data *ts)
1074 {
1075     GTP_GPIO_OUTPUT(ts->irq_pin, 0);
1076     msleep(ms);
1077     //GTP_GPIO_AS_INT(GTP_INT_PORT);
1078     gpio_direction_input(ts->irq_pin);
1079     //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1080     //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1081 }
1082
1083
1084 /*******************************************************
1085 Function:
1086     Reset chip.
1087 Input:
1088     ms: reset time in millisecond
1089 Output:
1090     None.
1091 *******************************************************/
1092 void gtp_reset_guitar(struct i2c_client *client, s32 ms)
1093 {
1094     struct goodix_ts_data *ts = i2c_get_clientdata(client);
1095
1096     GTP_DEBUG_FUNC();
1097     GTP_INFO("Guitar reset");
1098     GTP_GPIO_OUTPUT(ts->rst_pin, 0);   // begin select I2C slave addr
1099     msleep(ms);                         // T2: > 10ms
1100     // HIGH: 0x28/0x29, LOW: 0xBA/0xBB
1101     GTP_GPIO_OUTPUT(ts->irq_pin, client->addr == 0x14);
1102
1103     msleep(2);                          // T3: > 100us
1104     GTP_GPIO_OUTPUT(ts->rst_pin, 1);
1105     
1106     msleep(6);                          // T4: > 5ms
1107
1108     //GTP_GPIO_AS_INPUT(GTP_RST_PORT);    // end select I2C slave addr
1109     gpio_direction_input(ts->rst_pin);
1110     //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1111
1112 #if GTP_COMPATIBLE_MODE
1113     if (CHIP_TYPE_GT9F == ts->chip_type)
1114     {
1115         return;
1116     }
1117 #endif
1118
1119     gtp_int_sync(50, ts);  
1120 #if GTP_ESD_PROTECT
1121     gtp_init_ext_watchdog(client);
1122 #endif
1123 }
1124
1125 #if GTP_GESTURE_WAKEUP
1126 /*******************************************************
1127 Function:
1128     Enter doze mode for sliding wakeup.
1129 Input:
1130     ts: goodix tp private data
1131 Output:
1132     1: succeed, otherwise failed
1133 *******************************************************/
1134 static s8 gtp_enter_doze(struct goodix_ts_data *ts)
1135 {
1136     s8 ret = -1;
1137     s8 retry = 0;
1138     u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 8};
1139
1140     GTP_DEBUG_FUNC();
1141
1142     GTP_DEBUG("Entering gesture mode.");
1143     while(retry++ < 5)
1144     {
1145         i2c_control_buf[0] = 0x80;
1146         i2c_control_buf[1] = 0x46;
1147         ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1148         if (ret < 0)
1149         {
1150             GTP_DEBUG("failed to set doze flag into 0x8046, %d", retry);
1151             continue;
1152         }
1153         i2c_control_buf[0] = 0x80;
1154         i2c_control_buf[1] = 0x40;
1155         ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1156         if (ret > 0)
1157         {
1158             doze_status = DOZE_ENABLED;
1159             GTP_INFO("Gesture mode enabled.");
1160             return ret;
1161         }
1162         msleep(10);
1163     }
1164     GTP_ERROR("GTP send gesture cmd failed.");
1165     return ret;
1166 }
1167 #else 
1168 /*******************************************************
1169 Function:
1170     Enter sleep mode.
1171 Input:
1172     ts: private data.
1173 Output:
1174     Executive outcomes.
1175        1: succeed, otherwise failed.
1176 *******************************************************/
1177 static s8 gtp_enter_sleep(struct goodix_ts_data * ts)
1178 {
1179     s8 ret = -1;
1180     s8 retry = 0;
1181     u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 5};
1182 #if GTP_COMPATIBLE_MODE
1183     u8 status_buf[3] = {0x80, 0x44};
1184 #endif
1185     
1186     GTP_DEBUG_FUNC();
1187     
1188 #if GTP_COMPATIBLE_MODE
1189     if (CHIP_TYPE_GT9F == ts->chip_type)
1190     {
1191         // GT9XXF: host interact with ic
1192         ret = gtp_i2c_read(ts->client, status_buf, 3);
1193         if (ret < 0)
1194         {
1195             GTP_ERROR("failed to get backup-reference status");
1196         }
1197         
1198         if (status_buf[2] & 0x80)
1199         {
1200             ret = gtp_bak_ref_proc(ts, GTP_BAK_REF_STORE);
1201             if (FAIL == ret)
1202             {
1203                 GTP_ERROR("failed to store bak_ref");
1204             }
1205         }
1206     }
1207 #endif
1208
1209     GTP_GPIO_OUTPUT(ts->irq_pin, 0);
1210     msleep(5);
1211     
1212     while(retry++ < 5)
1213     {
1214         ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1215         if (ret > 0)
1216         {
1217             GTP_INFO("GTP enter sleep!");
1218             
1219             return ret;
1220         }
1221         msleep(10);
1222     }
1223     GTP_ERROR("GTP send sleep cmd failed.");
1224     return ret;
1225 }
1226 #endif 
1227 /*******************************************************
1228 Function:
1229     Wakeup from sleep.
1230 Input:
1231     ts: private data.
1232 Output:
1233     Executive outcomes.
1234         >0: succeed, otherwise: failed.
1235 *******************************************************/
1236 static s8 gtp_wakeup_sleep(struct goodix_ts_data * ts)
1237 {
1238     u8 retry = 0;
1239     s8 ret = -1;
1240     
1241     GTP_DEBUG_FUNC();
1242
1243 #if GTP_COMPATIBLE_MODE
1244     if (CHIP_TYPE_GT9F == ts->chip_type)
1245     {
1246         u8 opr_buf[3] = {0x41, 0x80};
1247         
1248         GTP_GPIO_OUTPUT(ts->irq_pin, 1);
1249         msleep(5);
1250     
1251         for (retry = 0; retry < 10; ++retry)
1252         {
1253             // hold ss51 & dsp
1254             opr_buf[2] = 0x0C;
1255             ret = gtp_i2c_write(ts->client, opr_buf, 3);
1256             if (FAIL == ret)
1257             {
1258                 GTP_ERROR("failed to hold ss51 & dsp!");
1259                 continue;
1260             }
1261             opr_buf[2] = 0x00;
1262             ret = gtp_i2c_read(ts->client, opr_buf, 3);
1263             if (FAIL == ret)
1264             {
1265                 GTP_ERROR("failed to get ss51 & dsp status!");
1266                 continue;
1267             }
1268             if (0x0C != opr_buf[2])
1269             {
1270                 GTP_DEBUG("ss51 & dsp not been hold, %d", retry+1);
1271                 continue;
1272             }
1273             GTP_DEBUG("ss51 & dsp confirmed hold");
1274             
1275             ret = gtp_fw_startup(ts->client);
1276             if (FAIL == ret)
1277             {
1278                 GTP_ERROR("failed to startup GT9XXF, process recovery");
1279                 gtp_esd_recovery(ts->client);
1280             }
1281             break;
1282         }
1283         if (retry >= 10)
1284         {
1285             GTP_ERROR("failed to wakeup, processing esd recovery");
1286             gtp_esd_recovery(ts->client);
1287         }
1288         else
1289         {
1290             GTP_INFO("GT9XXF gtp wakeup success");
1291         }
1292         return ret;
1293     }
1294 #endif
1295
1296 #if GTP_POWER_CTRL_SLEEP
1297     while(retry++ < 5)
1298     {
1299         gtp_reset_guitar(ts->client, 20);
1300         
1301         GTP_INFO("GTP wakeup sleep.");
1302         return 1;
1303     }
1304 #else
1305     while(retry++ < 10)
1306     {
1307     #if GTP_GESTURE_WAKEUP
1308         if (DOZE_WAKEUP != doze_status)  
1309         {
1310             GTP_INFO("Powerkey wakeup.");
1311         }
1312         else   
1313         {
1314             GTP_INFO("Gesture wakeup.");
1315         }
1316         doze_status = DOZE_DISABLED;
1317         gtp_irq_disable(ts);
1318         gtp_reset_guitar(ts->client, 10);
1319         gtp_irq_enable(ts);
1320         
1321     #else
1322         GTP_GPIO_OUTPUT(ts->irq_pin, 1);
1323         msleep(5);
1324     #endif
1325     
1326         ret = gtp_i2c_test(ts->client);
1327         if (ret > 0)
1328         {
1329             GTP_INFO("GTP wakeup sleep.");
1330             
1331         #if (!GTP_GESTURE_WAKEUP)
1332             {
1333                 gtp_int_sync(25, ts);
1334             #if GTP_ESD_PROTECT
1335                 gtp_init_ext_watchdog(ts->client);
1336             #endif
1337             }
1338         #endif
1339             
1340             return ret;
1341         }
1342         gtp_reset_guitar(ts->client, 20);
1343     }
1344 #endif
1345
1346     GTP_ERROR("GTP wakeup sleep failed.");
1347     return ret;
1348 }
1349 #if GTP_DRIVER_SEND_CFG
1350 static s32 gtp_get_info(struct goodix_ts_data *ts)
1351 {
1352     u8 opr_buf[6] = {0};
1353     s32 ret = 0;
1354     
1355     //ts->abs_x_max = GTP_MAX_WIDTH;
1356     //ts->abs_y_max = GTP_MAX_HEIGHT;
1357
1358     ts->int_trigger_type = GTP_INT_TRIGGER;
1359         
1360     opr_buf[0] = (u8)((GTP_REG_CONFIG_DATA+1) >> 8);
1361     opr_buf[1] = (u8)((GTP_REG_CONFIG_DATA+1) & 0xFF);
1362     
1363     ret = gtp_i2c_read(ts->client, opr_buf, 6);
1364     if (ret < 0)
1365     {
1366         return FAIL;
1367     }
1368     
1369     ts->abs_x_max = (opr_buf[3] << 8) + opr_buf[2];
1370     ts->abs_y_max = (opr_buf[5] << 8) + opr_buf[4];
1371     
1372     opr_buf[0] = (u8)((GTP_REG_CONFIG_DATA+6) >> 8);
1373     opr_buf[1] = (u8)((GTP_REG_CONFIG_DATA+6) & 0xFF);
1374     
1375     ret = gtp_i2c_read(ts->client, opr_buf, 3);
1376     if (ret < 0)
1377     {
1378         return FAIL;
1379     }
1380     ts->int_trigger_type = opr_buf[2] & 0x03;
1381     
1382     GTP_INFO("X_MAX = %d, Y_MAX = %d, TRIGGER = 0x%02x",
1383             ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1384     
1385     return SUCCESS;    
1386 }
1387 #endif 
1388
1389 /*******************************************************
1390 Function:
1391     Initialize gtp.
1392 Input:
1393     ts: goodix private data
1394 Output:
1395     Executive outcomes.
1396         0: succeed, otherwise: failed
1397 *******************************************************/
1398 static s32 gtp_init_panel(struct goodix_ts_data *ts)
1399 {
1400     s32 ret = -1;
1401 #if GTP_DRIVER_SEND_CFG
1402     s32 i = 0;
1403     u8 check_sum = 0;
1404     u8 opr_buf[16] = {0};
1405     u8 sensor_id = 0; 
1406
1407     u8 cfg_info_group2[] = CTP_CFG_GROUP2;
1408     u8 cfg_info_group3[] = CTP_CFG_GROUP3;
1409     u8 cfg_info_group4[] = CTP_CFG_GROUP4;
1410     u8 cfg_info_group5[] = CTP_CFG_GROUP5;
1411     u8 cfg_info_group6[] = CTP_CFG_GROUP6;
1412     u8 *send_cfg_buf[] = {gtp_dat_10_1, cfg_info_group2, cfg_info_group3,
1413                             cfg_info_group4, cfg_info_group5, cfg_info_group6};
1414     u8 cfg_info_len[] = { CFG_GROUP_LEN(gtp_dat_10_1),
1415                               CFG_GROUP_LEN(cfg_info_group2),
1416                               CFG_GROUP_LEN(cfg_info_group3),
1417                               CFG_GROUP_LEN(cfg_info_group4),
1418                               CFG_GROUP_LEN(cfg_info_group5),
1419                               CFG_GROUP_LEN(cfg_info_group6)};;
1420     
1421     GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1422    
1423     if(m89or101){
1424         send_cfg_buf[0] = gtp_dat_8_9;
1425         cfg_info_len[0] =  CFG_GROUP_LEN(gtp_dat_8_9);
1426     }
1427     
1428     if (bgt911) {
1429         send_cfg_buf[0] = gtp_dat_gt11;
1430                 cfg_info_len[0] =  CFG_GROUP_LEN(gtp_dat_gt11);
1431     }
1432
1433         if (bgt970) {
1434                 send_cfg_buf[0] = gtp_dat_9_7;
1435                 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_9_7);
1436         }
1437
1438     GTP_DEBUG_FUNC();
1439     GTP_DEBUG("Config Groups\' Lengths: %d, %d, %d, %d, %d, %d", 
1440         cfg_info_len[0], cfg_info_len[1], cfg_info_len[2], cfg_info_len[3],
1441         cfg_info_len[4], cfg_info_len[5]);
1442
1443     
1444 #if GTP_COMPATIBLE_MODE
1445     if (CHIP_TYPE_GT9F == ts->chip_type)
1446     {
1447         ts->fw_error = 0;
1448     }
1449     else
1450 #endif
1451     {
1452         ret = gtp_i2c_read_dbl_check(ts->client, 0x41E4, opr_buf, 1);
1453         if (SUCCESS == ret) 
1454         {
1455             if (opr_buf[0] != 0xBE)
1456             {
1457                 ts->fw_error = 1;
1458                 GTP_ERROR("Firmware error, no config sent!");
1459                 return -1;
1460             }
1461         }
1462     }
1463
1464     if ((!cfg_info_len[1]) && (!cfg_info_len[2]) && 
1465         (!cfg_info_len[3]) && (!cfg_info_len[4]) && 
1466         (!cfg_info_len[5]))
1467     {
1468         sensor_id = 0; 
1469     }
1470     else
1471     {
1472     #if GTP_COMPATIBLE_MODE
1473         msleep(50);
1474     #endif
1475         ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_SENSOR_ID, &sensor_id, 1);
1476         if (SUCCESS == ret)
1477         {
1478             if (sensor_id >= 0x06)
1479             {
1480                 GTP_ERROR("Invalid sensor_id(0x%02X), No Config Sent!", sensor_id);
1481                 ts->pnl_init_error = 1;
1482                 return -1;
1483             }
1484         }
1485         else
1486         {
1487             GTP_ERROR("Failed to get sensor_id, No config sent!");
1488             ts->pnl_init_error = 1;
1489             return -1;
1490         }
1491         GTP_INFO("Sensor_ID: %d", sensor_id);
1492     }
1493     ts->gtp_cfg_len = cfg_info_len[sensor_id];
1494     GTP_INFO("CTP_CONFIG_GROUP%d used, config length: %d", sensor_id + 1, ts->gtp_cfg_len);
1495     
1496     if (ts->gtp_cfg_len < GTP_CONFIG_MIN_LENGTH)
1497     {
1498         GTP_ERROR("Config Group%d is INVALID CONFIG GROUP(Len: %d)! NO Config Sent! You need to check you header file CFG_GROUP section!", sensor_id+1, ts->gtp_cfg_len);
1499         ts->pnl_init_error = 1;
1500         return -1;
1501     }
1502
1503 #if GTP_COMPATIBLE_MODE
1504     if (CHIP_TYPE_GT9F == ts->chip_type)
1505     {
1506         ts->fixed_cfg = 0;
1507     }
1508     else
1509 #endif
1510     {
1511         ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CONFIG_DATA, &opr_buf[0], 1);
1512         
1513         if (ret == SUCCESS)
1514         {
1515             GTP_DEBUG("CFG_GROUP%d Config Version: %d, 0x%02X; IC Config Version: %d, 0x%02X", sensor_id+1, 
1516                         send_cfg_buf[sensor_id][0], send_cfg_buf[sensor_id][0], opr_buf[0], opr_buf[0]);
1517             
1518             if (opr_buf[0] < 90)    
1519             {
1520                 GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1521                 grp_cfg_version = send_cfg_buf[sensor_id][0];       // backup group config version
1522                 send_cfg_buf[sensor_id][0] = 0x00;
1523                 ts->fixed_cfg = 0;
1524             }
1525             else        // treated as fixed config, not send config
1526             {
1527                 GTP_INFO("Ic fixed config with config version(%d, 0x%02X)", opr_buf[0], opr_buf[0]);
1528                 ts->fixed_cfg = 1;
1529                 gtp_get_info(ts);
1530                 return 0;
1531             }
1532         }
1533         else
1534         {
1535             GTP_ERROR("Failed to get ic config version!No config sent!");
1536             return -1;
1537         }
1538     }
1539     
1540     memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
1541     memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[sensor_id], ts->gtp_cfg_len);
1542
1543 #if GTP_CUSTOM_CFG
1544     config[RESOLUTION_LOC]     = (u8)GTP_MAX_WIDTH;
1545     config[RESOLUTION_LOC + 1] = (u8)(GTP_MAX_WIDTH>>8);
1546     config[RESOLUTION_LOC + 2] = (u8)GTP_MAX_HEIGHT;
1547     config[RESOLUTION_LOC + 3] = (u8)(GTP_MAX_HEIGHT>>8);
1548     
1549     if (GTP_INT_TRIGGER == 0)  //RISING
1550     {
1551         config[TRIGGER_LOC] &= 0xfe; 
1552     }
1553     else if (GTP_INT_TRIGGER == 1)  //FALLING
1554     {
1555         config[TRIGGER_LOC] |= 0x01;
1556     }
1557 #endif  // GTP_CUSTOM_CFG
1558     
1559     check_sum = 0;
1560     for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1561     {
1562         check_sum += config[i];
1563     }
1564     config[ts->gtp_cfg_len] = (~check_sum) + 1;
1565
1566 #else // driver not send config
1567
1568     ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
1569     ret = gtp_i2c_read(ts->client, config, ts->gtp_cfg_len + GTP_ADDR_LENGTH);
1570     if (ret < 0)
1571     {
1572         GTP_ERROR("Read Config Failed, Using Default Resolution & INT Trigger!");
1573         //ts->abs_x_max = GTP_MAX_WIDTH;
1574         //ts->abs_y_max = GTP_MAX_HEIGHT;
1575         ts->int_trigger_type = GTP_INT_TRIGGER;
1576     }
1577     
1578 #endif // GTP_DRIVER_SEND_CFG
1579
1580     if ((ts->abs_x_max == 0) && (ts->abs_y_max == 0))
1581     {
1582         ts->abs_x_max = (config[RESOLUTION_LOC + 1] << 8) + config[RESOLUTION_LOC];
1583         ts->abs_y_max = (config[RESOLUTION_LOC + 3] << 8) + config[RESOLUTION_LOC + 2];
1584         ts->int_trigger_type = (config[TRIGGER_LOC]) & 0x03; 
1585         GTP_INFO("  <%s>_%d    <%d, %d>\n", __func__, __LINE__, ts->abs_x_max, ts->abs_y_max);
1586     }
1587     GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1588 #if GTP_COMPATIBLE_MODE
1589     if (CHIP_TYPE_GT9F == ts->chip_type)
1590     {
1591         u8 sensor_num = 0;
1592         u8 driver_num = 0;
1593         u8 have_key = 0;
1594         
1595         have_key = (config[GTP_REG_HAVE_KEY - GTP_REG_CONFIG_DATA + 2] & 0x01);
1596         
1597         if (1 == ts->is_950)
1598         {
1599             driver_num = config[GTP_REG_MATRIX_DRVNUM - GTP_REG_CONFIG_DATA + 2];
1600             sensor_num = config[GTP_REG_MATRIX_SENNUM - GTP_REG_CONFIG_DATA + 2];
1601             if (have_key)
1602             {
1603                 driver_num--;
1604             }
1605             ts->bak_ref_len = (driver_num * (sensor_num - 1) + 2) * 2 * 6;
1606         }
1607         else
1608         {
1609             driver_num = (config[CFG_LOC_DRVA_NUM] & 0x1F) + (config[CFG_LOC_DRVB_NUM]&0x1F);
1610             if (have_key)
1611             {
1612                 driver_num--;
1613             }
1614             sensor_num = (config[CFG_LOC_SENS_NUM] & 0x0F) + ((config[CFG_LOC_SENS_NUM] >> 4) & 0x0F);
1615             ts->bak_ref_len = (driver_num * (sensor_num - 2) + 2) * 2;
1616         }
1617     
1618         GTP_INFO("Drv * Sen: %d * %d(key: %d), X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x",
1619            driver_num, sensor_num, have_key, ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1620         return 0;
1621     }
1622     else
1623 #endif
1624     {
1625     #if GTP_DRIVER_SEND_CFG
1626         GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1627         ret = gtp_send_cfg(ts->client);
1628         if (ret < 0)
1629         {
1630             GTP_ERROR("Send config error.");
1631         }
1632         // set config version to CTP_CFG_GROUP, for resume to send config
1633         config[GTP_ADDR_LENGTH] = grp_cfg_version;
1634         check_sum = 0;
1635         for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1636         {
1637             check_sum += config[i];
1638         }
1639         config[ts->gtp_cfg_len] = (~check_sum) + 1;
1640     #endif
1641         GTP_INFO("X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x", ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1642     }
1643     
1644     msleep(10);
1645     return 0;
1646 }
1647
1648
1649 static ssize_t gt91xx_config_read_proc(struct file *file, char __user *page, size_t size, loff_t *ppos)
1650 {
1651     char *ptr = page;
1652     char temp_data[GTP_CONFIG_MAX_LENGTH + 2] = {0x80, 0x47};
1653     int i;
1654     
1655     if (*ppos)
1656     {
1657         return 0;
1658     }
1659     ptr += sprintf(ptr, "==== GT9XX config init value====\n");
1660
1661     for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1662     {
1663         ptr += sprintf(ptr, "0x%02X ", config[i + 2]);
1664
1665         if (i % 8 == 7)
1666             ptr += sprintf(ptr, "\n");
1667     }
1668
1669     ptr += sprintf(ptr, "\n");
1670
1671     ptr += sprintf(ptr, "==== GT9XX config real value====\n");
1672     gtp_i2c_read(i2c_connect_client, temp_data, GTP_CONFIG_MAX_LENGTH + 2);
1673     for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1674     {
1675         ptr += sprintf(ptr, "0x%02X ", temp_data[i+2]);
1676
1677         if (i % 8 == 7)
1678             ptr += sprintf(ptr, "\n");
1679     }
1680     *ppos += ptr - page;
1681     return (ptr - page);
1682 }
1683
1684 static ssize_t gt91xx_config_write_proc(struct file *filp, const char __user *buffer, size_t count, loff_t *off)
1685 {
1686     s32 ret = 0;
1687
1688     if (count > GTP_CONFIG_MAX_LENGTH)
1689     {
1690         GTP_ERROR("size not match [%d:%zu]\n", GTP_CONFIG_MAX_LENGTH, count);
1691         return -EFAULT;
1692     }
1693
1694     if (copy_from_user(&config[2], buffer, count))
1695     {
1696         GTP_ERROR("copy from user fail\n");
1697         return -EFAULT;
1698     }
1699
1700     ret = gtp_send_cfg(i2c_connect_client);
1701
1702     if (ret < 0)
1703     {
1704         GTP_ERROR("send config failed.");
1705     }
1706
1707     return count;
1708 }
1709 /*******************************************************
1710 Function:
1711     Read chip version.
1712 Input:
1713     client:  i2c device
1714     version: buffer to keep ic firmware version
1715 Output:
1716     read operation return.
1717         2: succeed, otherwise: failed
1718 *******************************************************/
1719 s32 gtp_read_version(struct i2c_client *client, u16* version)
1720 {
1721     s32 ret = -1;
1722     u8 buf[8] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff};
1723
1724     GTP_DEBUG_FUNC();
1725
1726     ret = gtp_i2c_read(client, buf, sizeof(buf));
1727     if (ret < 0)
1728     {
1729         GTP_ERROR("GTP read version failed");
1730         return ret;
1731     }
1732
1733     if (version)
1734     {
1735         *version = (buf[7] << 8) | buf[6];
1736     }
1737     if (buf[5] == 0x00)
1738     {
1739         GTP_INFO("IC Version: %c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[7], buf[6]);
1740     }
1741     else
1742     {
1743         GTP_INFO("IC Version: %c%c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[5], buf[7], buf[6]);
1744     }
1745     return ret;
1746 }
1747
1748 /*******************************************************
1749 Function:
1750     I2c test Function.
1751 Input:
1752     client:i2c client.
1753 Output:
1754     Executive outcomes.
1755         2: succeed, otherwise failed.
1756 *******************************************************/
1757 static s8 gtp_i2c_test(struct i2c_client *client)
1758 {
1759     u8 test[3] = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
1760     u8 retry = 0;
1761     s8 ret = -1;
1762   
1763     GTP_DEBUG_FUNC();
1764   
1765     while(retry++ < 5)
1766     {
1767         ret = gtp_i2c_read(client, test, 3);
1768         if (ret > 0)
1769         {
1770             return ret;
1771         }
1772         GTP_ERROR("GTP i2c test failed time %d.",retry);
1773         msleep(10);
1774     }
1775     return ret;
1776 }
1777
1778 /*******************************************************
1779 Function:
1780     Request gpio(INT & RST) ports.
1781 Input:
1782     ts: private data.
1783 Output:
1784     Executive outcomes.
1785         >= 0: succeed, < 0: failed
1786 *******************************************************/
1787 static s8 gtp_request_io_port(struct goodix_ts_data *ts)
1788 {
1789     s32 ret = 0;
1790
1791     GTP_DEBUG_FUNC();
1792 /*
1793     ret = GTP_GPIO_REQUEST(ts->tp_select_pin, "GTP_tp_select_PORT");
1794     if (ret < 0) 
1795     {
1796         GTP_ERROR("1Failed to request GPIO:%d, ERRNO:%d",(s32)ts->tp_select_pin, ret);
1797         return -ENODEV;
1798     }
1799     else
1800     {
1801         gpio_direction_input(ts->tp_select_pin);
1802     }
1803 */
1804     ret = GTP_GPIO_REQUEST(ts->rst_pin, "GTP_RST_PORT");
1805     if (ret < 0) 
1806     {
1807         GTP_ERROR("2Failed to request GPIO:%d, ERRNO:%d",(s32)ts->rst_pin, ret);
1808         return -ENODEV;
1809     }
1810     
1811     ret = GTP_GPIO_REQUEST(ts->irq_pin, "GTP_INT_IRQ");
1812     if (ret < 0) 
1813     {
1814         GTP_ERROR("3Failed to request GPIO:%d, ERRNO:%d", (s32)ts->irq_pin, ret);
1815         return -ENODEV;
1816     }
1817     else
1818     {
1819         //GTP_GPIO_AS_INT(GTP_INT_PORT);  
1820         gpio_direction_input(ts->irq_pin);
1821         //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1822         //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1823         
1824         //ts->client->irq = ts->irq_pin;
1825     }
1826
1827     //GTP_GPIO_AS_INPUT(ts->rst_pin);
1828     gpio_direction_input(ts->rst_pin);
1829     //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1830
1831     gtp_reset_guitar(ts->client, 20);
1832     
1833     if(ret < 0)
1834     {
1835         GTP_GPIO_FREE(ts->rst_pin);
1836         GTP_GPIO_FREE(ts->irq_pin);
1837     }
1838
1839     return ret;
1840 }
1841
1842 /*******************************************************
1843 Function:
1844     Request interrupt.
1845 Input:
1846     ts: private data.
1847 Output:
1848     Executive outcomes.
1849         0: succeed, -1: failed.
1850 *******************************************************/
1851 static s8 gtp_request_irq(struct goodix_ts_data *ts)
1852 {
1853     s32 ret = -1;
1854
1855     GTP_DEBUG_FUNC();
1856     GTP_DEBUG("INT trigger type:%x", ts->int_trigger_type);
1857     
1858     ts->irq=gpio_to_irq(ts->irq_pin);       //If not defined in client
1859     if (ts->irq)
1860     {
1861         ts->client->irq = ts->irq;
1862         ret = devm_request_threaded_irq(&(ts->client->dev), ts->irq, NULL, 
1863             goodix_ts_irq_handler, ts->irq_flags | IRQF_ONESHOT /*irq_table[ts->int_trigger_type]*/, 
1864             ts->client->name, ts);
1865         if (ret != 0) {
1866             GTP_ERROR("Cannot allocate ts INT!ERRNO:%d\n", ret);
1867             goto test_pit;
1868         }
1869         //gtp_irq_disable(ts->irq);
1870         GTP_INFO("  <%s>_%d     ts->irq=%d   ret = %d\n", __func__, __LINE__, ts->irq, ret);
1871     }else{
1872         GTP_ERROR("   ts->irq  error \n");
1873         ret = 1;
1874         goto test_pit;
1875     }
1876 /*
1877     ret  = request_irq(ts->client->irq, 
1878                        goodix_ts_irq_handler,
1879                        irq_table[ts->int_trigger_type],
1880                        ts->client->name,
1881                        ts);
1882 */                       
1883 test_pit:
1884     if (ret)
1885     {
1886         GTP_ERROR("Request IRQ failed!ERRNO:%d.", ret);
1887         //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
1888         gpio_direction_input(ts->irq_pin);
1889         //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1890         
1891         GTP_GPIO_FREE(ts->irq_pin);
1892
1893         hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1894         ts->timer.function = goodix_ts_timer_handler;
1895         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
1896         return -1;
1897     }
1898     else 
1899     {
1900         GTP_INFO("  <%s>_%d     ts->irq=%d   ret = %d\n", __func__, __LINE__, ts->irq, ret);
1901         gtp_irq_disable(ts);
1902         ts->use_irq = 1;
1903         return 0;
1904     }
1905 }
1906
1907 /*******************************************************
1908 Function:
1909     Early suspend function.
1910 Input:
1911     h: early_suspend struct.
1912 Output:
1913     None.
1914 *******************************************************/
1915 static int goodix_ts_early_suspend(struct tp_device *tp_d)
1916 {
1917     struct goodix_ts_data *ts;
1918     s8 ret = -1;
1919     int reg = 0;
1920
1921     ts = container_of(tp_d, struct goodix_ts_data, tp);
1922     GTP_DEBUG_FUNC();
1923
1924     GTP_INFO("System suspend.");
1925
1926     ts->gtp_is_suspend = 1;
1927 #if GTP_ESD_PROTECT
1928     gtp_esd_switch(ts->client, SWITCH_OFF);
1929 #endif
1930
1931 #if GTP_GESTURE_WAKEUP
1932     ret = gtp_enter_doze(ts);
1933 #else
1934     if (ts->use_irq)
1935     {
1936         gtp_irq_disable(ts);
1937     }
1938     else
1939     {
1940         hrtimer_cancel(&ts->timer);
1941     }
1942     ret = gtp_enter_sleep(ts);
1943 #endif
1944     if (ret < 0)
1945     {
1946         printk("GTP early suspend failed.");
1947     }
1948     // to avoid waking up while not sleeping
1949     //  delay 48 + 10ms to ensure reliability
1950     msleep(58);
1951
1952         reg = regulator_disable(ts->tp_regulator);
1953         if (reg < 0)
1954                 GTP_ERROR("failed to disable tp regulator\n");
1955         msleep(20);
1956         return 0;
1957 }
1958
1959 /*******************************************************
1960 Function:
1961     Late resume function.
1962 Input:
1963     h: early_suspend struct.
1964 Output:
1965     None.
1966 *******************************************************/
1967 static int goodix_ts_early_resume(struct tp_device *tp_d)
1968 {
1969     struct goodix_ts_data *ts;
1970     s8 ret = -1;
1971     int reg = 0;
1972     ts = container_of(tp_d, struct goodix_ts_data, tp);
1973     GTP_DEBUG_FUNC();
1974
1975     GTP_INFO("System resume.");
1976
1977         reg = regulator_enable(ts->tp_regulator);
1978         if (reg < 0)
1979                 GTP_ERROR("failed to enable tp regulator\n");
1980         msleep(10);
1981
1982     ret = gtp_wakeup_sleep(ts);
1983
1984 #if GTP_GESTURE_WAKEUP
1985     doze_status = DOZE_DISABLED;
1986 #endif
1987
1988     if (ret < 0)
1989     {
1990         GTP_ERROR("GTP later resume failed.");
1991     }
1992 #if (GTP_COMPATIBLE_MODE)
1993     if (CHIP_TYPE_GT9F == ts->chip_type)
1994     {
1995         // do nothing
1996     }
1997     else
1998 #endif
1999     {
2000         gtp_send_cfg(ts->client);
2001     }
2002
2003     if (ts->use_irq)
2004     {
2005         gtp_irq_enable(ts);
2006     }
2007     else
2008     {
2009         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
2010     }
2011
2012     ts->gtp_is_suspend = 0;
2013 #if GTP_ESD_PROTECT
2014     gtp_esd_switch(ts->client, SWITCH_ON);
2015 #endif
2016
2017         return 0;
2018 }
2019
2020 /*******************************************************
2021 Function:
2022     Request input device Function.
2023 Input:
2024     ts:private data.
2025 Output:
2026     Executive outcomes.
2027         0: succeed, otherwise: failed.
2028 *******************************************************/
2029 static s8 gtp_request_input_dev(struct i2c_client *client,
2030                                 struct goodix_ts_data *ts)
2031 {
2032     s8 ret = -1;
2033     s8 phys[32];
2034 #if GTP_HAVE_TOUCH_KEY
2035     u8 index = 0;
2036 #endif
2037     GTP_DEBUG_FUNC();
2038   
2039     ts->input_dev = devm_input_allocate_device(&client->dev);
2040     if (ts->input_dev == NULL)
2041     {
2042         GTP_ERROR("Failed to allocate input device.");
2043         return -ENOMEM;
2044     }
2045
2046     ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
2047 #if GTP_ICS_SLOT_REPORT
2048     input_mt_init_slots(ts->input_dev, 16, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);     // in case of "out of memory"
2049 #else
2050     ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
2051 #endif
2052     __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
2053
2054 #if GTP_HAVE_TOUCH_KEY
2055     for (index = 0; index < GTP_MAX_KEY_NUM; index++)
2056     {
2057         input_set_capability(ts->input_dev, EV_KEY, touch_key_array[index]);  
2058     }
2059 #endif
2060
2061 #if GTP_GESTURE_WAKEUP
2062     input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
2063 #endif 
2064
2065     if(mGtpChange_X2Y){
2066         GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
2067     }
2068
2069 #if defined(CONFIG_CHROME_PLATFORMS)
2070     input_set_abs_params(ts->input_dev, ABS_X, 0, ts->abs_x_max, 0, 0);
2071     input_set_abs_params(ts->input_dev, ABS_Y, 0, ts->abs_y_max, 0, 0);
2072 #endif
2073     input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
2074     input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
2075     input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
2076     input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
2077     input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
2078
2079     sprintf(phys, "input/ts");
2080     ts->input_dev->name = goodix_ts_name;
2081     ts->input_dev->phys = phys;
2082     ts->input_dev->id.bustype = BUS_I2C;
2083     ts->input_dev->id.vendor = 0xDEAD;
2084     ts->input_dev->id.product = 0xBEEF;
2085     ts->input_dev->id.version = 10427;
2086     
2087     ret = input_register_device(ts->input_dev);
2088     if (ret)
2089     {
2090         GTP_ERROR("Register %s input device failed", ts->input_dev->name);
2091         return -ENODEV;
2092     }
2093     
2094     ts->tp.tp_resume = goodix_ts_early_resume;
2095     ts->tp.tp_suspend = goodix_ts_early_suspend;
2096     tp_register_fb(&ts->tp);
2097
2098 #if GTP_WITH_PEN
2099     gtp_pen_init(ts);
2100 #endif
2101
2102     return 0;
2103 }
2104
2105 //************** For GT9XXF Start *************//
2106 #if GTP_COMPATIBLE_MODE
2107
2108 s32 gtp_fw_startup(struct i2c_client *client)
2109 {
2110     u8 opr_buf[4];
2111     s32 ret = 0;
2112     struct goodix_ts_data *ts = i2c_get_clientdata(client);
2113     //init sw WDT
2114         opr_buf[0] = 0xAA;
2115         ret = i2c_write_bytes(client, 0x8041, opr_buf, 1);
2116     if (ret < 0)
2117     {
2118         return FAIL;
2119     }
2120     
2121     //release SS51 & DSP
2122     opr_buf[0] = 0x00;
2123     ret = i2c_write_bytes(client, 0x4180, opr_buf, 1);
2124     if (ret < 0)
2125     {
2126         return FAIL;
2127     }
2128     //int sync
2129     gtp_int_sync(25, ts);  
2130     
2131     //check fw run status
2132     ret = i2c_read_bytes(client, 0x8041, opr_buf, 1);
2133     if (ret < 0)
2134     {
2135         return FAIL;
2136     }
2137     if(0xAA == opr_buf[0])
2138     {
2139         GTP_ERROR("IC works abnormally,startup failed.");
2140         return FAIL;
2141     }
2142     else
2143     {
2144         GTP_INFO("IC works normally, Startup success.");
2145         opr_buf[0] = 0xAA;
2146         i2c_write_bytes(client, 0x8041, opr_buf, 1);
2147         return SUCCESS;
2148     }
2149 }
2150
2151 static s32 gtp_esd_recovery(struct i2c_client *client)
2152 {
2153     s32 retry = 0;
2154     s32 ret = 0;
2155     struct goodix_ts_data *ts;
2156     
2157     ts = i2c_get_clientdata(client);
2158     
2159     gtp_irq_disable(ts);
2160     
2161     GTP_INFO("GT9XXF esd recovery mode");
2162     for (retry = 0; retry < 5; retry++)
2163     {
2164         ret = gup_fw_download_proc(NULL, GTP_FL_ESD_RECOVERY); 
2165         if (FAIL == ret)
2166         {
2167             GTP_ERROR("esd recovery failed %d", retry+1);
2168             continue;
2169         }
2170         ret = gtp_fw_startup(ts->client);
2171         if (FAIL == ret)
2172         {
2173             GTP_ERROR("GT9XXF start up failed %d", retry+1);
2174             continue;
2175         }
2176         break;
2177     }
2178     gtp_irq_enable(ts);
2179     
2180     if (retry >= 5)
2181     {
2182         GTP_ERROR("failed to esd recovery");
2183         return FAIL;
2184     }
2185     
2186     GTP_INFO("Esd recovery successful");
2187     return SUCCESS;
2188 }
2189
2190 void gtp_recovery_reset(struct i2c_client *client)
2191 {
2192 #if GTP_ESD_PROTECT
2193     gtp_esd_switch(client, SWITCH_OFF);
2194 #endif
2195     GTP_DEBUG_FUNC();
2196     
2197     gtp_esd_recovery(client); 
2198     
2199 #if GTP_ESD_PROTECT
2200     gtp_esd_switch(client, SWITCH_ON);
2201 #endif
2202 }
2203
2204 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode)
2205 {
2206     s32 ret = 0;
2207     s32 i = 0;
2208     s32 j = 0;
2209     u16 ref_sum = 0;
2210     u16 learn_cnt = 0;
2211     u16 chksum = 0;
2212     s32 ref_seg_len = 0;
2213     s32 ref_grps = 0;
2214     struct file *ref_filp = NULL;
2215     u8 *p_bak_ref;
2216     
2217     ret = gup_check_fs_mounted("/data");
2218     if (FAIL == ret)
2219     {
2220         ts->ref_chk_fs_times++;
2221         GTP_DEBUG("Ref check /data times/MAX_TIMES: %d / %d", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2222         if (ts->ref_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2223         {
2224             msleep(50);
2225             GTP_INFO("/data not mounted.");
2226             return FAIL;
2227         }
2228         GTP_INFO("check /data mount timeout...");
2229     }
2230     else
2231     {
2232         GTP_INFO("/data mounted!!!(%d/%d)", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2233     }
2234     
2235     p_bak_ref = (u8 *)kzalloc(ts->bak_ref_len, GFP_KERNEL);
2236     
2237     if (NULL == p_bak_ref)
2238     {
2239         GTP_ERROR("Allocate memory for p_bak_ref failed!");
2240         return FAIL;
2241     }
2242     
2243     if (ts->is_950)
2244     {
2245         ref_seg_len = ts->bak_ref_len / 6;
2246         ref_grps = 6;
2247     }
2248     else
2249     {
2250         ref_seg_len = ts->bak_ref_len;
2251         ref_grps = 1;
2252     }
2253     ref_filp = filp_open(GTP_BAK_REF_PATH, O_RDWR | O_CREAT, 0666);
2254     if (IS_ERR(ref_filp))
2255     { 
2256         GTP_ERROR("Failed to open/create %s.", GTP_BAK_REF_PATH);
2257         if (GTP_BAK_REF_SEND == mode)
2258         {
2259             goto bak_ref_default;
2260         }
2261         else
2262         {
2263             goto bak_ref_exit;
2264         }
2265     }
2266     
2267     switch (mode)
2268     {
2269     case GTP_BAK_REF_SEND:
2270         GTP_INFO("Send backup-reference");
2271         ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2272         ret = ref_filp->f_op->read(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2273         if (ret < 0)
2274         {
2275             GTP_ERROR("failed to read bak_ref info from file, sending defualt bak_ref");
2276             goto bak_ref_default;
2277         }
2278         for (j = 0; j < ref_grps; ++j)
2279         {
2280             ref_sum = 0;
2281             for (i = 0; i < (ref_seg_len); i += 2)
2282             {
2283                 ref_sum += (p_bak_ref[i + j * ref_seg_len] << 8) + p_bak_ref[i+1 + j * ref_seg_len];
2284             }
2285             learn_cnt = (p_bak_ref[j * ref_seg_len + ref_seg_len -4] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -3]);
2286             chksum = (p_bak_ref[j * ref_seg_len + ref_seg_len -2] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -1]);
2287             GTP_DEBUG("learn count = %d", learn_cnt);
2288             GTP_DEBUG("chksum = %d", chksum);
2289             GTP_DEBUG("ref_sum = 0x%04X", ref_sum & 0xFFFF);
2290             // Sum(1~ref_seg_len) == 1
2291             if (1 != ref_sum)
2292             {
2293                 GTP_INFO("wrong chksum for bak_ref, reset to 0x00 bak_ref");
2294                 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2295                 p_bak_ref[ref_seg_len + j * ref_seg_len - 1] = 0x01;
2296             }
2297             else
2298             {
2299                 if (j == (ref_grps - 1))
2300                 {
2301                     GTP_INFO("backup-reference data in %s used", GTP_BAK_REF_PATH);
2302                 }
2303             }
2304         }
2305         ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2306         if (FAIL == ret)
2307         {
2308             GTP_ERROR("failed to send bak_ref because of iic comm error");
2309             goto bak_ref_exit;
2310         }
2311         break;
2312         
2313     case GTP_BAK_REF_STORE:
2314         GTP_INFO("Store backup-reference");
2315         ret = i2c_read_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2316         if (ret < 0)
2317         {
2318             GTP_ERROR("failed to read bak_ref info, sending default back-reference");
2319             goto bak_ref_default;
2320         }
2321         ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2322         ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2323         break;
2324         
2325     default:
2326         GTP_ERROR("invalid backup-reference request");
2327         break;
2328     }
2329     ret = SUCCESS;
2330     goto bak_ref_exit;
2331
2332 bak_ref_default:
2333     
2334     for (j = 0; j < ref_grps; ++j)
2335     {
2336         memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2337         p_bak_ref[j * ref_seg_len + ref_seg_len - 1] = 0x01;  // checksum = 1     
2338     }
2339     ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2340     if (!IS_ERR(ref_filp))
2341     {
2342         GTP_INFO("write backup-reference data into %s", GTP_BAK_REF_PATH);
2343         ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2344         ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2345     }
2346     if (ret == FAIL)
2347     {
2348         GTP_ERROR("failed to load the default backup reference");
2349     }
2350     
2351 bak_ref_exit:
2352     
2353     if (p_bak_ref)
2354     {
2355         kfree(p_bak_ref);
2356     }
2357     if (ref_filp && !IS_ERR(ref_filp))
2358     {
2359         filp_close(ref_filp, NULL);
2360     }
2361     return ret;
2362 }
2363
2364
2365 static s32 gtp_verify_main_clk(u8 *p_main_clk)
2366 {
2367     u8 chksum = 0;
2368     u8 main_clock = p_main_clk[0];
2369     s32 i = 0;
2370     
2371     if (main_clock < 50 || main_clock > 120)    
2372     {
2373         return FAIL;
2374     }
2375     
2376     for (i = 0; i < 5; ++i)
2377     {
2378         if (main_clock != p_main_clk[i])
2379         {
2380             return FAIL;
2381         }
2382         chksum += p_main_clk[i];
2383     }
2384     chksum += p_main_clk[5];
2385     if ( (chksum) == 0)
2386     {
2387         return SUCCESS;
2388     }
2389     else
2390     {
2391         return FAIL;
2392     }
2393 }
2394
2395 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts)
2396 {
2397     s32 ret = 0;
2398     s32 i = 0;
2399     s32 clk_chksum = 0;
2400     struct file *clk_filp = NULL;
2401     u8 p_main_clk[6] = {0};
2402
2403     ret = gup_check_fs_mounted("/data");
2404     if (FAIL == ret)
2405     {
2406         ts->clk_chk_fs_times++;
2407         GTP_DEBUG("Clock check /data times/MAX_TIMES: %d / %d", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2408         if (ts->clk_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2409         {
2410             msleep(50);
2411             GTP_INFO("/data not mounted.");
2412             return FAIL;
2413         }
2414         GTP_INFO("Check /data mount timeout!");
2415     }
2416     else
2417     {
2418         GTP_INFO("/data mounted!!!(%d/%d)", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2419     }
2420     
2421     clk_filp = filp_open(GTP_MAIN_CLK_PATH, O_RDWR | O_CREAT, 0666);
2422     if (IS_ERR(clk_filp))
2423     {
2424         GTP_ERROR("%s is unavailable, calculate main clock", GTP_MAIN_CLK_PATH);
2425     }
2426     else
2427     {
2428         clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2429         clk_filp->f_op->read(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2430        
2431         ret = gtp_verify_main_clk(p_main_clk);
2432         if (FAIL == ret)
2433         {
2434             // recalculate main clock & rewrite main clock data to file
2435             GTP_ERROR("main clock data in %s is wrong, recalculate main clock", GTP_MAIN_CLK_PATH);
2436         }
2437         else
2438         { 
2439             GTP_INFO("main clock data in %s used, main clock freq: %d", GTP_MAIN_CLK_PATH, p_main_clk[0]);
2440             filp_close(clk_filp, NULL);
2441             goto update_main_clk;
2442         }
2443     }
2444     
2445 #if GTP_ESD_PROTECT
2446     gtp_esd_switch(ts->client, SWITCH_OFF);
2447 #endif
2448     ret = gup_clk_calibration();
2449     gtp_esd_recovery(ts->client);
2450     
2451 #if GTP_ESD_PROTECT
2452     gtp_esd_switch(ts->client, SWITCH_ON);
2453 #endif
2454
2455     GTP_INFO("calibrate main clock: %d", ret);
2456     if (ret < 50 || ret > 120)
2457     {
2458         GTP_ERROR("wrong main clock: %d", ret);
2459         goto exit_main_clk;
2460     }
2461     
2462     // Sum{0x8020~0x8025} = 0
2463     for (i = 0; i < 5; ++i)
2464     {
2465         p_main_clk[i] = ret;
2466         clk_chksum += p_main_clk[i];
2467     }
2468     p_main_clk[5] = 0 - clk_chksum;
2469     
2470     if (!IS_ERR(clk_filp))
2471     {
2472         GTP_DEBUG("write main clock data into %s", GTP_MAIN_CLK_PATH);
2473         clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2474         clk_filp->f_op->write(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2475         filp_close(clk_filp, NULL);
2476     }
2477     
2478 update_main_clk:
2479     ret = i2c_write_bytes(ts->client, GTP_REG_MAIN_CLK, p_main_clk, 6);
2480     if (FAIL == ret)
2481     {
2482         GTP_ERROR("update main clock failed!");
2483         return FAIL;
2484     }
2485     return SUCCESS;
2486     
2487 exit_main_clk:
2488     if (!IS_ERR(clk_filp))
2489     {
2490         filp_close(clk_filp, NULL);
2491     }
2492     return FAIL;
2493 }
2494
2495
2496 s32 gtp_gt9xxf_init(struct i2c_client *client)
2497 {
2498     s32 ret = 0;
2499     
2500     ret = gup_fw_download_proc(NULL, GTP_FL_FW_BURN); 
2501     if (FAIL == ret)
2502     {
2503         return FAIL;
2504     }
2505     
2506     ret = gtp_fw_startup(client);
2507     if (FAIL == ret)
2508     {
2509         return FAIL;
2510     }
2511     return SUCCESS;
2512 }
2513
2514 void gtp_get_chip_type(struct goodix_ts_data *ts)
2515 {
2516     u8 opr_buf[10] = {0x00};
2517     s32 ret = 0;
2518     
2519     msleep(10);
2520     
2521     ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CHIP_TYPE, opr_buf, 10);
2522     
2523     if (FAIL == ret)
2524     {
2525         GTP_ERROR("Failed to get chip-type, set chip type default: GOODIX_GT9");
2526         ts->chip_type = CHIP_TYPE_GT9;
2527         return;
2528     }
2529     
2530     if (!memcmp(opr_buf, "GOODIX_GT9", 10))
2531     {
2532         ts->chip_type = CHIP_TYPE_GT9;
2533     }
2534     else // GT9XXF
2535     {
2536         ts->chip_type = CHIP_TYPE_GT9F;
2537     }
2538     GTP_INFO("Chip Type: %s", (ts->chip_type == CHIP_TYPE_GT9) ? "GOODIX_GT9" : "GOODIX_GT9F");
2539 }
2540
2541 #endif
2542 //************* For GT9XXF End ************//
2543
2544 /*******************************************************
2545 Function:
2546     I2c probe.
2547 Input:
2548     client: i2c device struct.
2549     id: device id.
2550 Output:
2551     Executive outcomes. 
2552         0: succeed.
2553 *******************************************************/
2554 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
2555 {
2556     s32 ret = -1;
2557     struct goodix_ts_data *ts;
2558     u16 version_info;
2559     
2560     struct device_node *np = client->dev.of_node;
2561     enum of_gpio_flags rst_flags, pwr_flags;
2562     u32 val;
2563         printk("%s() start\n", __func__);
2564
2565     
2566     GTP_DEBUG_FUNC();
2567     
2568     //do NOT remove these logs
2569     GTP_INFO("GTP Driver Version: %s", GTP_DRIVER_VERSION);
2570     GTP_INFO("GTP I2C Address: 0x%02x", client->addr);
2571
2572     i2c_connect_client = client;
2573     
2574     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 
2575     {
2576         GTP_ERROR("I2C check functionality failed.");
2577         return -ENODEV;
2578     }
2579     ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2580     if (ts == NULL)
2581     {
2582         GTP_ERROR("Alloc GFP_KERNEL memory failed.");
2583         return -ENOMEM;
2584     }
2585     
2586     memset(ts, 0, sizeof(*ts));
2587     
2588     if (!np) {
2589         dev_err(&client->dev, "no device tree\n");
2590         return -EINVAL;
2591     }
2592     if (of_property_read_u32(np, "tp-size", &val)) {
2593         dev_err(&client->dev, "no max-x defined\n");
2594         return -EINVAL;
2595     }
2596
2597     if(val == 89){
2598         m89or101 = TRUE;
2599         mGtpChange_X2Y = TRUE;
2600         mGtp_X_Reverse = FALSE;
2601         mGtp_Y_Reverse = TRUE;
2602     }else if(val == 101){
2603         m89or101 = FALSE;
2604         mGtpChange_X2Y = TRUE;
2605         mGtp_X_Reverse = TRUE;
2606         mGtp_Y_Reverse = FALSE;
2607     } else if (val == 911) {
2608         m89or101 = FALSE;
2609         bgt911 = TRUE;
2610         mGtpChange_X2Y = TRUE;
2611         mGtp_X_Reverse = FALSE;
2612         mGtp_Y_Reverse = TRUE;
2613         } else if (val == 970) {
2614                 m89or101 = FALSE;
2615                 bgt911 = FALSE;
2616                 bgt970 = TRUE;
2617                 mGtpChange_X2Y = FALSE;
2618                 mGtp_X_Reverse = FALSE;
2619                 mGtp_Y_Reverse = TRUE;
2620         }
2621
2622         ts->tp_regulator = devm_regulator_get(&client->dev, "tp");
2623         if (IS_ERR(ts->tp_regulator)) {
2624                 dev_err(&client->dev, "failed to get regulator, %ld\n",
2625                         PTR_ERR(ts->tp_regulator));
2626                 return PTR_ERR(ts->tp_regulator);
2627         }
2628
2629     ts->irq_pin = of_get_named_gpio_flags(np, "touch-gpio", 0, (enum of_gpio_flags *)(&ts->irq_flags));
2630     ts->rst_pin = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
2631     ts->pwr_pin = of_get_named_gpio_flags(np, "power-gpio", 0, &pwr_flags);
2632     //ts->tp_select_pin = of_get_named_gpio_flags(np, "tp-select-gpio", 0, &tp_select_flags);
2633     if (of_property_read_u32(np, "max-x", &val)) {
2634         dev_err(&client->dev, "no max-x defined\n");
2635         return -EINVAL;
2636     }
2637     //ts->abs_x_max = val;
2638     if (of_property_read_u32(np, "max-y", &val)) {
2639         dev_err(&client->dev, "no max-y defined\n");
2640         return -EINVAL;
2641     }
2642     //ts->abs_y_max = val;
2643     ts->pendown =PEN_RELEASE;
2644     ts->client = client;
2645     
2646     
2647     INIT_WORK(&ts->work, goodix_ts_work_func);
2648     ts->client = client;
2649     spin_lock_init(&ts->irq_lock);          // 2.6.39 later
2650     // ts->irq_lock = SPIN_LOCK_UNLOCKED;   // 2.6.39 & before
2651 #if GTP_ESD_PROTECT
2652     ts->clk_tick_cnt = 2 * HZ;      // HZ: clock ticks in 1 second generated by system
2653     GTP_DEBUG("Clock ticks for an esd cycle: %d", ts->clk_tick_cnt);  
2654     spin_lock_init(&ts->esd_lock);
2655     // ts->esd_lock = SPIN_LOCK_UNLOCKED;
2656 #endif
2657
2658     i2c_set_clientdata(client, ts);
2659     
2660     ts->gtp_rawdiff_mode = 0;
2661
2662     ret = gtp_request_io_port(ts);
2663     if (ret < 0)
2664     {
2665         GTP_ERROR("GTP request IO port failed.");
2666         //return ret;
2667         goto probe_init_error_requireio;
2668     }
2669 /*
2670     if(gpio_get_value(ts->tp_select_pin))//WGJ
2671     {
2672         printk("tp 11111111111111111111111111111 WGJ\n\n");
2673         mGtp_X_Reverse = FALSE;
2674         mGtp_Y_Reverse = TRUE;
2675     }
2676     else//DPT
2677     {
2678         printk("tp 00000000000000000000000000000 DPT\n\n");
2679         mGtp_X_Reverse = TRUE;//FALSE;
2680         mGtp_Y_Reverse = TRUE;
2681     }
2682  */
2683 #if GTP_COMPATIBLE_MODE
2684     gtp_get_chip_type(ts);
2685     
2686     if (CHIP_TYPE_GT9F == ts->chip_type)
2687     {
2688         ret = gtp_gt9xxf_init(ts->client);
2689         if (FAIL == ret)
2690         {
2691             GTP_INFO("Failed to init GT9XXF.");
2692         }
2693     }
2694 #endif
2695
2696     ret = gtp_i2c_test(client);
2697     if (ret < 0)
2698     {
2699         printk("<%s>_%d    I2C communication ERROR!\n", __func__, __LINE__);
2700         goto probe_init_error;
2701     }
2702
2703     ret = gtp_read_version(client, &version_info);
2704     if (ret < 0)
2705     {
2706         GTP_ERROR("Read version failed.");
2707     }
2708     
2709     ret = gtp_init_panel(ts);
2710     if (ret < 0)
2711     {
2712         GTP_ERROR("GTP init panel failed.");
2713         //ts->abs_x_max = GTP_MAX_WIDTH;
2714         //ts->abs_y_max = GTP_MAX_HEIGHT;
2715         ts->int_trigger_type = GTP_INT_TRIGGER;
2716     }
2717     
2718     // Create proc file system
2719     gt91xx_config_proc = proc_create(GT91XX_CONFIG_PROC_FILE, 0666, NULL, &config_proc_ops);
2720     if (gt91xx_config_proc == NULL)
2721     {
2722         GTP_ERROR("create_proc_entry %s failed\n", GT91XX_CONFIG_PROC_FILE);
2723     }
2724     else
2725     {
2726         GTP_INFO("create proc entry %s success", GT91XX_CONFIG_PROC_FILE);
2727     }
2728     
2729 #if GTP_AUTO_UPDATE
2730     ret = gup_init_update_proc(ts);
2731     if (ret < 0)
2732     {
2733         GTP_ERROR("Create update thread error.");
2734     }
2735 #endif
2736
2737     ret = gtp_request_input_dev(client, ts);
2738     if (ret < 0)
2739     {
2740         GTP_ERROR("GTP request input dev failed");
2741     }
2742     ret = gtp_request_irq(ts); 
2743     if (ret < 0)
2744     {
2745         GTP_INFO("GTP works in polling mode.");
2746     }
2747     else
2748     {
2749         GTP_INFO("GTP works in interrupt mode.");
2750     }
2751
2752     if (ts->use_irq)
2753     {
2754         gtp_irq_enable(ts);
2755     }
2756     
2757 #if GTP_CREATE_WR_NODE
2758     init_wr_node(client);
2759 #endif
2760    
2761 #if GTP_ESD_PROTECT
2762     gtp_esd_switch(client, SWITCH_ON);
2763 #endif
2764     return 0;
2765
2766 probe_init_error:
2767     printk("   <%s>_%d  prob error !!!!!!!!!!!!!!!\n", __func__, __LINE__);    
2768     tp_unregister_fb(&ts->tp);
2769     GTP_GPIO_FREE(ts->rst_pin);
2770     GTP_GPIO_FREE(ts->irq_pin);
2771 probe_init_error_requireio:
2772     tp_unregister_fb(&ts->tp); 
2773     kfree(ts);
2774     return ret;
2775 }
2776
2777
2778 /*******************************************************
2779 Function:
2780     Goodix touchscreen driver release function.
2781 Input:
2782     client: i2c device struct.
2783 Output:
2784     Executive outcomes. 0---succeed.
2785 *******************************************************/
2786 static int goodix_ts_remove(struct i2c_client *client)
2787 {
2788     struct goodix_ts_data *ts = i2c_get_clientdata(client);
2789     
2790     tp_unregister_fb(&ts->tp);
2791
2792     GTP_DEBUG_FUNC();
2793
2794 #if GTP_CREATE_WR_NODE
2795     uninit_wr_node();
2796 #endif
2797
2798 #if GTP_ESD_PROTECT
2799     destroy_workqueue(gtp_esd_check_workqueue);
2800 #endif
2801
2802     if (ts) 
2803     {
2804         if (ts->use_irq)
2805         {
2806             //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
2807             
2808             gpio_direction_input(ts->irq_pin);
2809             //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
2810             
2811             GTP_GPIO_FREE(ts->irq_pin);
2812             free_irq(client->irq, ts);
2813         }
2814         else
2815         {
2816             hrtimer_cancel(&ts->timer);
2817         }
2818     }   
2819     
2820     GTP_INFO("GTP driver removing...");
2821     i2c_set_clientdata(client, NULL);
2822     input_unregister_device(ts->input_dev);
2823     kfree(ts);
2824
2825     return 0;
2826 }
2827
2828
2829
2830
2831
2832 #if GTP_ESD_PROTECT
2833 s32 gtp_i2c_read_no_rst(struct i2c_client *client, u8 *buf, s32 len)
2834 {
2835     struct i2c_msg msgs[2];
2836     s32 ret=-1;
2837     s32 retries = 0;
2838
2839     GTP_DEBUG_FUNC();
2840
2841     msgs[0].flags = !I2C_M_RD;
2842     msgs[0].addr  = client->addr;
2843     msgs[0].len   = GTP_ADDR_LENGTH;
2844     msgs[0].buf   = &buf[0];
2845     //msgs[0].scl_rate = 300 * 1000;    // for Rockchip, etc.
2846     
2847     msgs[1].flags = I2C_M_RD;
2848     msgs[1].addr  = client->addr;
2849     msgs[1].len   = len - GTP_ADDR_LENGTH;
2850     msgs[1].buf   = &buf[GTP_ADDR_LENGTH];
2851     //msgs[1].scl_rate = 300 * 1000;
2852
2853     while(retries < 5)
2854     {
2855         ret = i2c_transfer(client->adapter, msgs, 2);
2856         if(ret == 2)break;
2857         retries++;
2858     }
2859     if ((retries >= 5))
2860     {    
2861         GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2862     }
2863     return ret;
2864 }
2865
2866 s32 gtp_i2c_write_no_rst(struct i2c_client *client,u8 *buf,s32 len)
2867 {
2868     struct i2c_msg msg;
2869     s32 ret = -1;
2870     s32 retries = 0;
2871
2872     GTP_DEBUG_FUNC();
2873
2874     msg.flags = !I2C_M_RD;
2875     msg.addr  = client->addr;
2876     msg.len   = len;
2877     msg.buf   = buf;
2878     //msg.scl_rate = 300 * 1000;    // for Rockchip, etc
2879
2880     while(retries < 5)
2881     {
2882         ret = i2c_transfer(client->adapter, &msg, 1);
2883         if (ret == 1)break;
2884         retries++;
2885     }
2886     if((retries >= 5))
2887     {
2888         GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2889     }
2890     return ret;
2891 }
2892 /*******************************************************
2893 Function:
2894     switch on & off esd delayed work
2895 Input:
2896     client:  i2c device
2897     on:      SWITCH_ON / SWITCH_OFF
2898 Output:
2899     void
2900 *********************************************************/
2901 void gtp_esd_switch(struct i2c_client *client, s32 on)
2902 {
2903     struct goodix_ts_data *ts;
2904     
2905     ts = i2c_get_clientdata(client);
2906     spin_lock(&ts->esd_lock);
2907     
2908     if (SWITCH_ON == on)     // switch on esd 
2909     {
2910         if (!ts->esd_running)
2911         {
2912             ts->esd_running = 1;
2913             spin_unlock(&ts->esd_lock);
2914             GTP_INFO("Esd started");
2915             queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, ts->clk_tick_cnt);
2916         }
2917         else
2918         {
2919             spin_unlock(&ts->esd_lock);
2920         }
2921     }
2922     else    // switch off esd
2923     {
2924         if (ts->esd_running)
2925         {
2926             ts->esd_running = 0;
2927             spin_unlock(&ts->esd_lock);
2928             GTP_INFO("Esd cancelled");
2929             cancel_delayed_work_sync(&gtp_esd_check_work);
2930         }
2931         else
2932         {
2933             spin_unlock(&ts->esd_lock);
2934         }
2935     }
2936 }
2937
2938 /*******************************************************
2939 Function:
2940     Initialize external watchdog for esd protect
2941 Input:
2942     client:  i2c device.
2943 Output:
2944     result of i2c write operation. 
2945         1: succeed, otherwise: failed
2946 *********************************************************/
2947 static s32 gtp_init_ext_watchdog(struct i2c_client *client)
2948 {
2949     u8 opr_buffer[3] = {0x80, 0x41, 0xAA};
2950     GTP_DEBUG("[Esd]Init external watchdog");
2951     return gtp_i2c_write_no_rst(client, opr_buffer, 3);
2952 }
2953
2954 /*******************************************************
2955 Function:
2956     Esd protect function.
2957     External watchdog added by meta, 2013/03/07
2958 Input:
2959     work: delayed work
2960 Output:
2961     None.
2962 *******************************************************/
2963 static void gtp_esd_check_func(struct work_struct *work)
2964 {
2965     s32 i;
2966     s32 ret = -1;
2967     struct goodix_ts_data *ts = NULL;
2968     u8 esd_buf[5] = {0x80, 0x40};
2969     
2970     GTP_DEBUG_FUNC();
2971    
2972     ts = i2c_get_clientdata(i2c_connect_client);
2973
2974     if (ts->gtp_is_suspend)
2975     {
2976         GTP_INFO("Esd suspended!");
2977         return;
2978     }
2979     
2980     for (i = 0; i < 3; i++)
2981     {
2982         ret = gtp_i2c_read_no_rst(ts->client, esd_buf, 4);
2983         
2984         GTP_DEBUG("[Esd]0x8040 = 0x%02X, 0x8041 = 0x%02X", esd_buf[2], esd_buf[3]);
2985         if ((ret < 0))
2986         {
2987             // IIC communication problem
2988             continue;
2989         }
2990         else
2991         { 
2992             if ((esd_buf[2] == 0xAA) || (esd_buf[3] != 0xAA))
2993             {
2994                 // IC works abnormally..
2995                 u8 chk_buf[4] = {0x80, 0x40};
2996                 
2997                 gtp_i2c_read_no_rst(ts->client, chk_buf, 4);
2998                 
2999                 GTP_DEBUG("[Check]0x8040 = 0x%02X, 0x8041 = 0x%02X", chk_buf[2], chk_buf[3]);
3000                 
3001                 if ((chk_buf[2] == 0xAA) || (chk_buf[3] != 0xAA))
3002                 {
3003                     i = 3;
3004                     break;
3005                 }
3006                 else
3007                 {
3008                     continue;
3009                 }
3010             }
3011             else 
3012             {
3013                 // IC works normally, Write 0x8040 0xAA, feed the dog
3014                 esd_buf[2] = 0xAA; 
3015                 gtp_i2c_write_no_rst(ts->client, esd_buf, 3);
3016                 break;
3017             }
3018         }
3019     }
3020     if (i >= 3)
3021     {
3022     #if GTP_COMPATIBLE_MODE
3023         if (CHIP_TYPE_GT9F == ts->chip_type)
3024         {        
3025             if (ts->rqst_processing)
3026             {
3027                 GTP_INFO("Request processing, no esd recovery");
3028             }
3029             else
3030             {
3031                 GTP_ERROR("IC working abnormally! Process esd recovery.");
3032                 esd_buf[0] = 0x42;
3033                 esd_buf[1] = 0x26;
3034                 esd_buf[2] = 0x01;
3035                 esd_buf[3] = 0x01;
3036                 esd_buf[4] = 0x01;
3037                 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3038                 msleep(50);
3039                 gtp_esd_recovery(ts->client);
3040             }
3041         }
3042         else
3043     #endif
3044         {
3045             GTP_ERROR("IC working abnormally! Process reset guitar.");
3046             esd_buf[0] = 0x42;
3047             esd_buf[1] = 0x26;
3048             esd_buf[2] = 0x01;
3049             esd_buf[3] = 0x01;
3050             esd_buf[4] = 0x01;
3051             gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3052             msleep(50);
3053             gtp_reset_guitar(ts->client, 50);
3054             msleep(50);
3055             gtp_send_cfg(ts->client);
3056         }
3057     }
3058
3059     if(!ts->gtp_is_suspend)
3060     {
3061         queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, ts->clk_tick_cnt);
3062     }
3063     else
3064     {
3065         GTP_INFO("Esd suspended!");
3066     }
3067     return;
3068 }
3069 #endif
3070
3071 static const struct i2c_device_id goodix_ts_id[] = {
3072     { GTP_I2C_NAME, 0 },
3073     { }
3074 };
3075
3076 static struct of_device_id goodix_ts_dt_ids[] = {
3077     { .compatible = "goodix,gt9xx" },
3078     { }
3079 };
3080
3081 static struct i2c_driver goodix_ts_driver = {
3082     .probe      = goodix_ts_probe,
3083     .remove     = goodix_ts_remove,
3084     .id_table   = goodix_ts_id,
3085     .driver = {
3086         .name     = GTP_I2C_NAME,
3087         .owner    = THIS_MODULE,
3088          .of_match_table = of_match_ptr(goodix_ts_dt_ids),
3089     },
3090 };
3091
3092 /*******************************************************    
3093 Function:
3094     Driver Install function.
3095 Input:
3096     None.
3097 Output:
3098     Executive Outcomes. 0---succeed.
3099 ********************************************************/
3100 static int goodix_ts_init(void)
3101 {
3102     s32 ret;
3103
3104     GTP_DEBUG_FUNC();   
3105     GTP_INFO("GTP driver installing...");
3106     goodix_wq = create_singlethread_workqueue("goodix_wq");
3107     if (!goodix_wq)
3108     {
3109         GTP_ERROR("Creat workqueue failed.");
3110         return -ENOMEM;
3111     }
3112 #if GTP_ESD_PROTECT
3113     INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
3114     gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
3115 #endif
3116     ret = i2c_add_driver(&goodix_ts_driver);
3117     return ret; 
3118 }
3119
3120 /*******************************************************    
3121 Function:
3122     Driver uninstall function.
3123 Input:
3124     None.
3125 Output:
3126     Executive Outcomes. 0---succeed.
3127 ********************************************************/
3128 static void goodix_ts_exit(void)
3129 {
3130     GTP_DEBUG_FUNC();
3131     GTP_INFO("GTP driver exited.");
3132     i2c_del_driver(&goodix_ts_driver);
3133     if (goodix_wq)
3134     {
3135         destroy_workqueue(goodix_wq);
3136     }
3137 }
3138 //late_initcall(goodix_ts_init);
3139 module_init(goodix_ts_init);
3140
3141 module_exit(goodix_ts_exit);
3142
3143 MODULE_DESCRIPTION("GTP Series Driver");
3144 MODULE_LICENSE("GPL");