ARM64: dts: rockchip: support gt9xx for Rockchip platform
[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) {
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);               // 
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     GTP_DEBUG_FUNC();
1434     GTP_DEBUG("Config Groups\' Lengths: %d, %d, %d, %d, %d, %d", 
1435         cfg_info_len[0], cfg_info_len[1], cfg_info_len[2], cfg_info_len[3],
1436         cfg_info_len[4], cfg_info_len[5]);
1437
1438     
1439 #if GTP_COMPATIBLE_MODE
1440     if (CHIP_TYPE_GT9F == ts->chip_type)
1441     {
1442         ts->fw_error = 0;
1443     }
1444     else
1445 #endif
1446     {
1447         ret = gtp_i2c_read_dbl_check(ts->client, 0x41E4, opr_buf, 1);
1448         if (SUCCESS == ret) 
1449         {
1450             if (opr_buf[0] != 0xBE)
1451             {
1452                 ts->fw_error = 1;
1453                 GTP_ERROR("Firmware error, no config sent!");
1454                 return -1;
1455             }
1456         }
1457     }
1458
1459     if ((!cfg_info_len[1]) && (!cfg_info_len[2]) && 
1460         (!cfg_info_len[3]) && (!cfg_info_len[4]) && 
1461         (!cfg_info_len[5]))
1462     {
1463         sensor_id = 0; 
1464     }
1465     else
1466     {
1467     #if GTP_COMPATIBLE_MODE
1468         msleep(50);
1469     #endif
1470         ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_SENSOR_ID, &sensor_id, 1);
1471         if (SUCCESS == ret)
1472         {
1473             if (sensor_id >= 0x06)
1474             {
1475                 GTP_ERROR("Invalid sensor_id(0x%02X), No Config Sent!", sensor_id);
1476                 ts->pnl_init_error = 1;
1477                 return -1;
1478             }
1479         }
1480         else
1481         {
1482             GTP_ERROR("Failed to get sensor_id, No config sent!");
1483             ts->pnl_init_error = 1;
1484             return -1;
1485         }
1486         GTP_INFO("Sensor_ID: %d", sensor_id);
1487     }
1488     ts->gtp_cfg_len = cfg_info_len[sensor_id];
1489     GTP_INFO("CTP_CONFIG_GROUP%d used, config length: %d", sensor_id + 1, ts->gtp_cfg_len);
1490     
1491     if (ts->gtp_cfg_len < GTP_CONFIG_MIN_LENGTH)
1492     {
1493         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);
1494         ts->pnl_init_error = 1;
1495         return -1;
1496     }
1497
1498 #if GTP_COMPATIBLE_MODE
1499     if (CHIP_TYPE_GT9F == ts->chip_type)
1500     {
1501         ts->fixed_cfg = 0;
1502     }
1503     else
1504 #endif
1505     {
1506         ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CONFIG_DATA, &opr_buf[0], 1);
1507         
1508         if (ret == SUCCESS)
1509         {
1510             GTP_DEBUG("CFG_GROUP%d Config Version: %d, 0x%02X; IC Config Version: %d, 0x%02X", sensor_id+1, 
1511                         send_cfg_buf[sensor_id][0], send_cfg_buf[sensor_id][0], opr_buf[0], opr_buf[0]);
1512             
1513             if (opr_buf[0] < 90)    
1514             {
1515                 GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1516                 grp_cfg_version = send_cfg_buf[sensor_id][0];       // backup group config version
1517                 send_cfg_buf[sensor_id][0] = 0x00;
1518                 ts->fixed_cfg = 0;
1519             }
1520             else        // treated as fixed config, not send config
1521             {
1522                 GTP_INFO("Ic fixed config with config version(%d, 0x%02X)", opr_buf[0], opr_buf[0]);
1523                 ts->fixed_cfg = 1;
1524                 gtp_get_info(ts);
1525                 return 0;
1526             }
1527         }
1528         else
1529         {
1530             GTP_ERROR("Failed to get ic config version!No config sent!");
1531             return -1;
1532         }
1533     }
1534     
1535     memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
1536     memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[sensor_id], ts->gtp_cfg_len);
1537
1538 #if GTP_CUSTOM_CFG
1539     config[RESOLUTION_LOC]     = (u8)GTP_MAX_WIDTH;
1540     config[RESOLUTION_LOC + 1] = (u8)(GTP_MAX_WIDTH>>8);
1541     config[RESOLUTION_LOC + 2] = (u8)GTP_MAX_HEIGHT;
1542     config[RESOLUTION_LOC + 3] = (u8)(GTP_MAX_HEIGHT>>8);
1543     
1544     if (GTP_INT_TRIGGER == 0)  //RISING
1545     {
1546         config[TRIGGER_LOC] &= 0xfe; 
1547     }
1548     else if (GTP_INT_TRIGGER == 1)  //FALLING
1549     {
1550         config[TRIGGER_LOC] |= 0x01;
1551     }
1552 #endif  // GTP_CUSTOM_CFG
1553     
1554     check_sum = 0;
1555     for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1556     {
1557         check_sum += config[i];
1558     }
1559     config[ts->gtp_cfg_len] = (~check_sum) + 1;
1560
1561 #else // driver not send config
1562
1563     ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
1564     ret = gtp_i2c_read(ts->client, config, ts->gtp_cfg_len + GTP_ADDR_LENGTH);
1565     if (ret < 0)
1566     {
1567         GTP_ERROR("Read Config Failed, Using Default Resolution & INT Trigger!");
1568         //ts->abs_x_max = GTP_MAX_WIDTH;
1569         //ts->abs_y_max = GTP_MAX_HEIGHT;
1570         ts->int_trigger_type = GTP_INT_TRIGGER;
1571     }
1572     
1573 #endif // GTP_DRIVER_SEND_CFG
1574
1575     if ((ts->abs_x_max == 0) && (ts->abs_y_max == 0))
1576     {
1577         ts->abs_x_max = (config[RESOLUTION_LOC + 1] << 8) + config[RESOLUTION_LOC];
1578         ts->abs_y_max = (config[RESOLUTION_LOC + 3] << 8) + config[RESOLUTION_LOC + 2];
1579         ts->int_trigger_type = (config[TRIGGER_LOC]) & 0x03; 
1580         GTP_INFO("  <%s>_%d    <%d, %d>\n", __func__, __LINE__, ts->abs_x_max, ts->abs_y_max);
1581     }
1582     GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1583 #if GTP_COMPATIBLE_MODE
1584     if (CHIP_TYPE_GT9F == ts->chip_type)
1585     {
1586         u8 sensor_num = 0;
1587         u8 driver_num = 0;
1588         u8 have_key = 0;
1589         
1590         have_key = (config[GTP_REG_HAVE_KEY - GTP_REG_CONFIG_DATA + 2] & 0x01);
1591         
1592         if (1 == ts->is_950)
1593         {
1594             driver_num = config[GTP_REG_MATRIX_DRVNUM - GTP_REG_CONFIG_DATA + 2];
1595             sensor_num = config[GTP_REG_MATRIX_SENNUM - GTP_REG_CONFIG_DATA + 2];
1596             if (have_key)
1597             {
1598                 driver_num--;
1599             }
1600             ts->bak_ref_len = (driver_num * (sensor_num - 1) + 2) * 2 * 6;
1601         }
1602         else
1603         {
1604             driver_num = (config[CFG_LOC_DRVA_NUM] & 0x1F) + (config[CFG_LOC_DRVB_NUM]&0x1F);
1605             if (have_key)
1606             {
1607                 driver_num--;
1608             }
1609             sensor_num = (config[CFG_LOC_SENS_NUM] & 0x0F) + ((config[CFG_LOC_SENS_NUM] >> 4) & 0x0F);
1610             ts->bak_ref_len = (driver_num * (sensor_num - 2) + 2) * 2;
1611         }
1612     
1613         GTP_INFO("Drv * Sen: %d * %d(key: %d), X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x",
1614            driver_num, sensor_num, have_key, ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1615         return 0;
1616     }
1617     else
1618 #endif
1619     {
1620     #if GTP_DRIVER_SEND_CFG
1621         GTP_INFO("  <%s>_%d \n", __func__, __LINE__);
1622         ret = gtp_send_cfg(ts->client);
1623         if (ret < 0)
1624         {
1625             GTP_ERROR("Send config error.");
1626         }
1627         // set config version to CTP_CFG_GROUP, for resume to send config
1628         config[GTP_ADDR_LENGTH] = grp_cfg_version;
1629         check_sum = 0;
1630         for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1631         {
1632             check_sum += config[i];
1633         }
1634         config[ts->gtp_cfg_len] = (~check_sum) + 1;
1635     #endif
1636         GTP_INFO("X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x", ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1637     }
1638     
1639     msleep(10);
1640     return 0;
1641 }
1642
1643
1644 static ssize_t gt91xx_config_read_proc(struct file *file, char __user *page, size_t size, loff_t *ppos)
1645 {
1646     char *ptr = page;
1647     char temp_data[GTP_CONFIG_MAX_LENGTH + 2] = {0x80, 0x47};
1648     int i;
1649     
1650     if (*ppos)
1651     {
1652         return 0;
1653     }
1654     ptr += sprintf(ptr, "==== GT9XX config init value====\n");
1655
1656     for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1657     {
1658         ptr += sprintf(ptr, "0x%02X ", config[i + 2]);
1659
1660         if (i % 8 == 7)
1661             ptr += sprintf(ptr, "\n");
1662     }
1663
1664     ptr += sprintf(ptr, "\n");
1665
1666     ptr += sprintf(ptr, "==== GT9XX config real value====\n");
1667     gtp_i2c_read(i2c_connect_client, temp_data, GTP_CONFIG_MAX_LENGTH + 2);
1668     for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1669     {
1670         ptr += sprintf(ptr, "0x%02X ", temp_data[i+2]);
1671
1672         if (i % 8 == 7)
1673             ptr += sprintf(ptr, "\n");
1674     }
1675     *ppos += ptr - page;
1676     return (ptr - page);
1677 }
1678
1679 static ssize_t gt91xx_config_write_proc(struct file *filp, const char __user *buffer, size_t count, loff_t *off)
1680 {
1681     s32 ret = 0;
1682
1683     if (count > GTP_CONFIG_MAX_LENGTH)
1684     {
1685         GTP_ERROR("size not match [%d:%zu]\n", GTP_CONFIG_MAX_LENGTH, count);
1686         return -EFAULT;
1687     }
1688
1689     if (copy_from_user(&config[2], buffer, count))
1690     {
1691         GTP_ERROR("copy from user fail\n");
1692         return -EFAULT;
1693     }
1694
1695     ret = gtp_send_cfg(i2c_connect_client);
1696
1697     if (ret < 0)
1698     {
1699         GTP_ERROR("send config failed.");
1700     }
1701
1702     return count;
1703 }
1704 /*******************************************************
1705 Function:
1706     Read chip version.
1707 Input:
1708     client:  i2c device
1709     version: buffer to keep ic firmware version
1710 Output:
1711     read operation return.
1712         2: succeed, otherwise: failed
1713 *******************************************************/
1714 s32 gtp_read_version(struct i2c_client *client, u16* version)
1715 {
1716     s32 ret = -1;
1717     u8 buf[8] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff};
1718
1719     GTP_DEBUG_FUNC();
1720
1721     ret = gtp_i2c_read(client, buf, sizeof(buf));
1722     if (ret < 0)
1723     {
1724         GTP_ERROR("GTP read version failed");
1725         return ret;
1726     }
1727
1728     if (version)
1729     {
1730         *version = (buf[7] << 8) | buf[6];
1731     }
1732     if (buf[5] == 0x00)
1733     {
1734         GTP_INFO("IC Version: %c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[7], buf[6]);
1735     }
1736     else
1737     {
1738         GTP_INFO("IC Version: %c%c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[5], buf[7], buf[6]);
1739     }
1740     return ret;
1741 }
1742
1743 /*******************************************************
1744 Function:
1745     I2c test Function.
1746 Input:
1747     client:i2c client.
1748 Output:
1749     Executive outcomes.
1750         2: succeed, otherwise failed.
1751 *******************************************************/
1752 static s8 gtp_i2c_test(struct i2c_client *client)
1753 {
1754     u8 test[3] = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
1755     u8 retry = 0;
1756     s8 ret = -1;
1757   
1758     GTP_DEBUG_FUNC();
1759   
1760     while(retry++ < 5)
1761     {
1762         ret = gtp_i2c_read(client, test, 3);
1763         if (ret > 0)
1764         {
1765             return ret;
1766         }
1767         GTP_ERROR("GTP i2c test failed time %d.",retry);
1768         msleep(10);
1769     }
1770     return ret;
1771 }
1772
1773 /*******************************************************
1774 Function:
1775     Request gpio(INT & RST) ports.
1776 Input:
1777     ts: private data.
1778 Output:
1779     Executive outcomes.
1780         >= 0: succeed, < 0: failed
1781 *******************************************************/
1782 static s8 gtp_request_io_port(struct goodix_ts_data *ts)
1783 {
1784     s32 ret = 0;
1785
1786     GTP_DEBUG_FUNC();
1787 /*
1788     ret = GTP_GPIO_REQUEST(ts->tp_select_pin, "GTP_tp_select_PORT");
1789     if (ret < 0) 
1790     {
1791         GTP_ERROR("1Failed to request GPIO:%d, ERRNO:%d",(s32)ts->tp_select_pin, ret);
1792         return -ENODEV;
1793     }
1794     else
1795     {
1796         gpio_direction_input(ts->tp_select_pin);
1797     }
1798 */
1799     ret = GTP_GPIO_REQUEST(ts->rst_pin, "GTP_RST_PORT");
1800     if (ret < 0) 
1801     {
1802         GTP_ERROR("2Failed to request GPIO:%d, ERRNO:%d",(s32)ts->rst_pin, ret);
1803         return -ENODEV;
1804     }
1805     
1806     ret = GTP_GPIO_REQUEST(ts->irq_pin, "GTP_INT_IRQ");
1807     if (ret < 0) 
1808     {
1809         GTP_ERROR("3Failed to request GPIO:%d, ERRNO:%d", (s32)ts->irq_pin, ret);
1810         return -ENODEV;
1811     }
1812     else
1813     {
1814         //GTP_GPIO_AS_INT(GTP_INT_PORT);  
1815         gpio_direction_input(ts->irq_pin);
1816         //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1817         //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1818         
1819         //ts->client->irq = ts->irq_pin;
1820     }
1821
1822     //GTP_GPIO_AS_INPUT(ts->rst_pin);
1823     gpio_direction_input(ts->rst_pin);
1824     //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1825
1826     gtp_reset_guitar(ts->client, 20);
1827     
1828     if(ret < 0)
1829     {
1830         GTP_GPIO_FREE(ts->rst_pin);
1831         GTP_GPIO_FREE(ts->irq_pin);
1832     }
1833
1834     return ret;
1835 }
1836
1837 /*******************************************************
1838 Function:
1839     Request interrupt.
1840 Input:
1841     ts: private data.
1842 Output:
1843     Executive outcomes.
1844         0: succeed, -1: failed.
1845 *******************************************************/
1846 static s8 gtp_request_irq(struct goodix_ts_data *ts)
1847 {
1848     s32 ret = -1;
1849
1850     GTP_DEBUG_FUNC();
1851     GTP_DEBUG("INT trigger type:%x", ts->int_trigger_type);
1852     
1853     ts->irq=gpio_to_irq(ts->irq_pin);       //If not defined in client
1854     if (ts->irq)
1855     {
1856         ts->client->irq = ts->irq;
1857         ret = devm_request_threaded_irq(&(ts->client->dev), ts->irq, NULL, 
1858             goodix_ts_irq_handler, ts->irq_flags | IRQF_ONESHOT /*irq_table[ts->int_trigger_type]*/, 
1859             ts->client->name, ts);
1860         if (ret != 0) {
1861             GTP_ERROR("Cannot allocate ts INT!ERRNO:%d\n", ret);
1862             goto test_pit;
1863         }
1864         //gtp_irq_disable(ts->irq);
1865         GTP_INFO("  <%s>_%d     ts->irq=%d   ret = %d\n", __func__, __LINE__, ts->irq, ret);
1866     }else{
1867         GTP_ERROR("   ts->irq  error \n");
1868         ret = 1;
1869         goto test_pit;
1870     }
1871 /*
1872     ret  = request_irq(ts->client->irq, 
1873                        goodix_ts_irq_handler,
1874                        irq_table[ts->int_trigger_type],
1875                        ts->client->name,
1876                        ts);
1877 */                       
1878 test_pit:
1879     if (ret)
1880     {
1881         GTP_ERROR("Request IRQ failed!ERRNO:%d.", ret);
1882         //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
1883         gpio_direction_input(ts->irq_pin);
1884         //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1885         
1886         GTP_GPIO_FREE(ts->irq_pin);
1887
1888         hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1889         ts->timer.function = goodix_ts_timer_handler;
1890         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
1891         return -1;
1892     }
1893     else 
1894     {
1895         GTP_INFO("  <%s>_%d     ts->irq=%d   ret = %d\n", __func__, __LINE__, ts->irq, ret);
1896         gtp_irq_disable(ts);
1897         ts->use_irq = 1;
1898         return 0;
1899     }
1900 }
1901
1902 /*******************************************************
1903 Function:
1904     Early suspend function.
1905 Input:
1906     h: early_suspend struct.
1907 Output:
1908     None.
1909 *******************************************************/
1910 static int goodix_ts_early_suspend(struct tp_device *tp_d)
1911 {
1912     struct goodix_ts_data *ts;
1913     s8 ret = -1;
1914     struct regulator *regulator_tp = NULL;
1915     int reg = 0;
1916
1917     ts = container_of(tp_d, struct goodix_ts_data, tp);
1918     GTP_DEBUG_FUNC();
1919
1920     GTP_INFO("System suspend.");
1921
1922     ts->gtp_is_suspend = 1;
1923 #if GTP_ESD_PROTECT
1924     gtp_esd_switch(ts->client, SWITCH_OFF);
1925 #endif
1926
1927 #if GTP_GESTURE_WAKEUP
1928     ret = gtp_enter_doze(ts);
1929 #else
1930     if (ts->use_irq)
1931     {
1932         gtp_irq_disable(ts);
1933     }
1934     else
1935     {
1936         hrtimer_cancel(&ts->timer);
1937     }
1938     ret = gtp_enter_sleep(ts);
1939 #endif
1940     if (ret < 0)
1941     {
1942         printk("GTP early suspend failed.");
1943     }
1944     // to avoid waking up while not sleeping
1945     //  delay 48 + 10ms to ensure reliability
1946     msleep(58);
1947         regulator_tp = regulator_get(NULL,"vcc_tp");
1948         if(IS_ERR(regulator_tp))
1949         {
1950                 printk("!!!!%s:%d:get regulator_tp failed\n",__func__,__LINE__);
1951                 return 0;
1952         }
1953         while(regulator_is_enabled(regulator_tp))
1954         {
1955                 reg = regulator_disable(regulator_tp);
1956                 msleep(10);
1957         }
1958         regulator_put(regulator_tp);
1959         msleep(20);
1960         return 0;
1961 }
1962
1963 /*******************************************************
1964 Function:
1965     Late resume function.
1966 Input:
1967     h: early_suspend struct.
1968 Output:
1969     None.
1970 *******************************************************/
1971 static int goodix_ts_early_resume(struct tp_device *tp_d)
1972 {
1973     struct goodix_ts_data *ts;
1974     s8 ret = -1;
1975     struct regulator *regulator_tp = NULL;
1976     int reg = 0;
1977     ts = container_of(tp_d, struct goodix_ts_data, tp);
1978     GTP_DEBUG_FUNC();
1979
1980     GTP_INFO("System resume.");
1981
1982         regulator_tp = regulator_get(NULL, "vcc_tp");
1983         if(IS_ERR(regulator_tp))
1984         {
1985                 printk("!!!!%s:%d:get regulator_tp failed\n",__func__,__LINE__);
1986                 return 0;
1987         }
1988         while(!(regulator_is_enabled(regulator_tp)))
1989         {
1990                 reg = regulator_enable(regulator_tp);
1991                 msleep(10);
1992         }
1993         regulator_put(regulator_tp);
1994         msleep(10);
1995
1996     ret = gtp_wakeup_sleep(ts);
1997
1998 #if GTP_GESTURE_WAKEUP
1999     doze_status = DOZE_DISABLED;
2000 #endif
2001
2002     if (ret < 0)
2003     {
2004         GTP_ERROR("GTP later resume failed.");
2005     }
2006 #if (GTP_COMPATIBLE_MODE)
2007     if (CHIP_TYPE_GT9F == ts->chip_type)
2008     {
2009         // do nothing
2010     }
2011     else
2012 #endif
2013     {
2014         gtp_send_cfg(ts->client);
2015     }
2016
2017     if (ts->use_irq)
2018     {
2019         gtp_irq_enable(ts);
2020     }
2021     else
2022     {
2023         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
2024     }
2025
2026     ts->gtp_is_suspend = 0;
2027 #if GTP_ESD_PROTECT
2028     gtp_esd_switch(ts->client, SWITCH_ON);
2029 #endif
2030
2031         return 0;
2032 }
2033
2034 /*******************************************************
2035 Function:
2036     Request input device Function.
2037 Input:
2038     ts:private data.
2039 Output:
2040     Executive outcomes.
2041         0: succeed, otherwise: failed.
2042 *******************************************************/
2043 static s8 gtp_request_input_dev(struct goodix_ts_data *ts)
2044 {
2045     s8 ret = -1;
2046     s8 phys[32];
2047 #if GTP_HAVE_TOUCH_KEY
2048     u8 index = 0;
2049 #endif
2050     GTP_DEBUG_FUNC();
2051   
2052     ts->input_dev = input_allocate_device();
2053     if (ts->input_dev == NULL)
2054     {
2055         GTP_ERROR("Failed to allocate input device.");
2056         return -ENOMEM;
2057     }
2058
2059     ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
2060 #if GTP_ICS_SLOT_REPORT
2061     input_mt_init_slots(ts->input_dev, 16);     // in case of "out of memory"
2062 #else
2063     ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
2064 #endif
2065     __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
2066
2067 #if GTP_HAVE_TOUCH_KEY
2068     for (index = 0; index < GTP_MAX_KEY_NUM; index++)
2069     {
2070         input_set_capability(ts->input_dev, EV_KEY, touch_key_array[index]);  
2071     }
2072 #endif
2073
2074 #if GTP_GESTURE_WAKEUP
2075     input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
2076 #endif 
2077
2078     if(mGtpChange_X2Y){
2079         GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
2080     }
2081
2082     input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
2083     input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
2084     input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
2085     input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
2086     input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
2087
2088     sprintf(phys, "input/ts");
2089     ts->input_dev->name = goodix_ts_name;
2090     ts->input_dev->phys = phys;
2091     ts->input_dev->id.bustype = BUS_I2C;
2092     ts->input_dev->id.vendor = 0xDEAD;
2093     ts->input_dev->id.product = 0xBEEF;
2094     ts->input_dev->id.version = 10427;
2095     
2096     ret = input_register_device(ts->input_dev);
2097     if (ret)
2098     {
2099         GTP_ERROR("Register %s input device failed", ts->input_dev->name);
2100         return -ENODEV;
2101     }
2102     
2103     ts->tp.tp_resume = goodix_ts_early_resume;
2104     ts->tp.tp_suspend = goodix_ts_early_suspend;
2105     tp_register_fb(&ts->tp);
2106
2107 #if GTP_WITH_PEN
2108     gtp_pen_init(ts);
2109 #endif
2110
2111     return 0;
2112 }
2113
2114 //************** For GT9XXF Start *************//
2115 #if GTP_COMPATIBLE_MODE
2116
2117 s32 gtp_fw_startup(struct i2c_client *client)
2118 {
2119     u8 opr_buf[4];
2120     s32 ret = 0;
2121     struct goodix_ts_data *ts = i2c_get_clientdata(client);
2122     //init sw WDT
2123         opr_buf[0] = 0xAA;
2124         ret = i2c_write_bytes(client, 0x8041, opr_buf, 1);
2125     if (ret < 0)
2126     {
2127         return FAIL;
2128     }
2129     
2130     //release SS51 & DSP
2131     opr_buf[0] = 0x00;
2132     ret = i2c_write_bytes(client, 0x4180, opr_buf, 1);
2133     if (ret < 0)
2134     {
2135         return FAIL;
2136     }
2137     //int sync
2138     gtp_int_sync(25, ts);  
2139     
2140     //check fw run status
2141     ret = i2c_read_bytes(client, 0x8041, opr_buf, 1);
2142     if (ret < 0)
2143     {
2144         return FAIL;
2145     }
2146     if(0xAA == opr_buf[0])
2147     {
2148         GTP_ERROR("IC works abnormally,startup failed.");
2149         return FAIL;
2150     }
2151     else
2152     {
2153         GTP_INFO("IC works normally, Startup success.");
2154         opr_buf[0] = 0xAA;
2155         i2c_write_bytes(client, 0x8041, opr_buf, 1);
2156         return SUCCESS;
2157     }
2158 }
2159
2160 static s32 gtp_esd_recovery(struct i2c_client *client)
2161 {
2162     s32 retry = 0;
2163     s32 ret = 0;
2164     struct goodix_ts_data *ts;
2165     
2166     ts = i2c_get_clientdata(client);
2167     
2168     gtp_irq_disable(ts);
2169     
2170     GTP_INFO("GT9XXF esd recovery mode");
2171     for (retry = 0; retry < 5; retry++)
2172     {
2173         ret = gup_fw_download_proc(NULL, GTP_FL_ESD_RECOVERY); 
2174         if (FAIL == ret)
2175         {
2176             GTP_ERROR("esd recovery failed %d", retry+1);
2177             continue;
2178         }
2179         ret = gtp_fw_startup(ts->client);
2180         if (FAIL == ret)
2181         {
2182             GTP_ERROR("GT9XXF start up failed %d", retry+1);
2183             continue;
2184         }
2185         break;
2186     }
2187     gtp_irq_enable(ts);
2188     
2189     if (retry >= 5)
2190     {
2191         GTP_ERROR("failed to esd recovery");
2192         return FAIL;
2193     }
2194     
2195     GTP_INFO("Esd recovery successful");
2196     return SUCCESS;
2197 }
2198
2199 void gtp_recovery_reset(struct i2c_client *client)
2200 {
2201 #if GTP_ESD_PROTECT
2202     gtp_esd_switch(client, SWITCH_OFF);
2203 #endif
2204     GTP_DEBUG_FUNC();
2205     
2206     gtp_esd_recovery(client); 
2207     
2208 #if GTP_ESD_PROTECT
2209     gtp_esd_switch(client, SWITCH_ON);
2210 #endif
2211 }
2212
2213 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode)
2214 {
2215     s32 ret = 0;
2216     s32 i = 0;
2217     s32 j = 0;
2218     u16 ref_sum = 0;
2219     u16 learn_cnt = 0;
2220     u16 chksum = 0;
2221     s32 ref_seg_len = 0;
2222     s32 ref_grps = 0;
2223     struct file *ref_filp = NULL;
2224     u8 *p_bak_ref;
2225     
2226     ret = gup_check_fs_mounted("/data");
2227     if (FAIL == ret)
2228     {
2229         ts->ref_chk_fs_times++;
2230         GTP_DEBUG("Ref check /data times/MAX_TIMES: %d / %d", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2231         if (ts->ref_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2232         {
2233             msleep(50);
2234             GTP_INFO("/data not mounted.");
2235             return FAIL;
2236         }
2237         GTP_INFO("check /data mount timeout...");
2238     }
2239     else
2240     {
2241         GTP_INFO("/data mounted!!!(%d/%d)", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2242     }
2243     
2244     p_bak_ref = (u8 *)kzalloc(ts->bak_ref_len, GFP_KERNEL);
2245     
2246     if (NULL == p_bak_ref)
2247     {
2248         GTP_ERROR("Allocate memory for p_bak_ref failed!");
2249         return FAIL;
2250     }
2251     
2252     if (ts->is_950)
2253     {
2254         ref_seg_len = ts->bak_ref_len / 6;
2255         ref_grps = 6;
2256     }
2257     else
2258     {
2259         ref_seg_len = ts->bak_ref_len;
2260         ref_grps = 1;
2261     }
2262     ref_filp = filp_open(GTP_BAK_REF_PATH, O_RDWR | O_CREAT, 0666);
2263     if (IS_ERR(ref_filp))
2264     { 
2265         GTP_ERROR("Failed to open/create %s.", GTP_BAK_REF_PATH);
2266         if (GTP_BAK_REF_SEND == mode)
2267         {
2268             goto bak_ref_default;
2269         }
2270         else
2271         {
2272             goto bak_ref_exit;
2273         }
2274     }
2275     
2276     switch (mode)
2277     {
2278     case GTP_BAK_REF_SEND:
2279         GTP_INFO("Send backup-reference");
2280         ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2281         ret = ref_filp->f_op->read(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2282         if (ret < 0)
2283         {
2284             GTP_ERROR("failed to read bak_ref info from file, sending defualt bak_ref");
2285             goto bak_ref_default;
2286         }
2287         for (j = 0; j < ref_grps; ++j)
2288         {
2289             ref_sum = 0;
2290             for (i = 0; i < (ref_seg_len); i += 2)
2291             {
2292                 ref_sum += (p_bak_ref[i + j * ref_seg_len] << 8) + p_bak_ref[i+1 + j * ref_seg_len];
2293             }
2294             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]);
2295             chksum = (p_bak_ref[j * ref_seg_len + ref_seg_len -2] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -1]);
2296             GTP_DEBUG("learn count = %d", learn_cnt);
2297             GTP_DEBUG("chksum = %d", chksum);
2298             GTP_DEBUG("ref_sum = 0x%04X", ref_sum & 0xFFFF);
2299             // Sum(1~ref_seg_len) == 1
2300             if (1 != ref_sum)
2301             {
2302                 GTP_INFO("wrong chksum for bak_ref, reset to 0x00 bak_ref");
2303                 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2304                 p_bak_ref[ref_seg_len + j * ref_seg_len - 1] = 0x01;
2305             }
2306             else
2307             {
2308                 if (j == (ref_grps - 1))
2309                 {
2310                     GTP_INFO("backup-reference data in %s used", GTP_BAK_REF_PATH);
2311                 }
2312             }
2313         }
2314         ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2315         if (FAIL == ret)
2316         {
2317             GTP_ERROR("failed to send bak_ref because of iic comm error");
2318             goto bak_ref_exit;
2319         }
2320         break;
2321         
2322     case GTP_BAK_REF_STORE:
2323         GTP_INFO("Store backup-reference");
2324         ret = i2c_read_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2325         if (ret < 0)
2326         {
2327             GTP_ERROR("failed to read bak_ref info, sending default back-reference");
2328             goto bak_ref_default;
2329         }
2330         ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2331         ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2332         break;
2333         
2334     default:
2335         GTP_ERROR("invalid backup-reference request");
2336         break;
2337     }
2338     ret = SUCCESS;
2339     goto bak_ref_exit;
2340
2341 bak_ref_default:
2342     
2343     for (j = 0; j < ref_grps; ++j)
2344     {
2345         memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2346         p_bak_ref[j * ref_seg_len + ref_seg_len - 1] = 0x01;  // checksum = 1     
2347     }
2348     ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2349     if (!IS_ERR(ref_filp))
2350     {
2351         GTP_INFO("write backup-reference data into %s", GTP_BAK_REF_PATH);
2352         ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2353         ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2354     }
2355     if (ret == FAIL)
2356     {
2357         GTP_ERROR("failed to load the default backup reference");
2358     }
2359     
2360 bak_ref_exit:
2361     
2362     if (p_bak_ref)
2363     {
2364         kfree(p_bak_ref);
2365     }
2366     if (ref_filp && !IS_ERR(ref_filp))
2367     {
2368         filp_close(ref_filp, NULL);
2369     }
2370     return ret;
2371 }
2372
2373
2374 static s32 gtp_verify_main_clk(u8 *p_main_clk)
2375 {
2376     u8 chksum = 0;
2377     u8 main_clock = p_main_clk[0];
2378     s32 i = 0;
2379     
2380     if (main_clock < 50 || main_clock > 120)    
2381     {
2382         return FAIL;
2383     }
2384     
2385     for (i = 0; i < 5; ++i)
2386     {
2387         if (main_clock != p_main_clk[i])
2388         {
2389             return FAIL;
2390         }
2391         chksum += p_main_clk[i];
2392     }
2393     chksum += p_main_clk[5];
2394     if ( (chksum) == 0)
2395     {
2396         return SUCCESS;
2397     }
2398     else
2399     {
2400         return FAIL;
2401     }
2402 }
2403
2404 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts)
2405 {
2406     s32 ret = 0;
2407     s32 i = 0;
2408     s32 clk_chksum = 0;
2409     struct file *clk_filp = NULL;
2410     u8 p_main_clk[6] = {0};
2411
2412     ret = gup_check_fs_mounted("/data");
2413     if (FAIL == ret)
2414     {
2415         ts->clk_chk_fs_times++;
2416         GTP_DEBUG("Clock check /data times/MAX_TIMES: %d / %d", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2417         if (ts->clk_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2418         {
2419             msleep(50);
2420             GTP_INFO("/data not mounted.");
2421             return FAIL;
2422         }
2423         GTP_INFO("Check /data mount timeout!");
2424     }
2425     else
2426     {
2427         GTP_INFO("/data mounted!!!(%d/%d)", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2428     }
2429     
2430     clk_filp = filp_open(GTP_MAIN_CLK_PATH, O_RDWR | O_CREAT, 0666);
2431     if (IS_ERR(clk_filp))
2432     {
2433         GTP_ERROR("%s is unavailable, calculate main clock", GTP_MAIN_CLK_PATH);
2434     }
2435     else
2436     {
2437         clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2438         clk_filp->f_op->read(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2439        
2440         ret = gtp_verify_main_clk(p_main_clk);
2441         if (FAIL == ret)
2442         {
2443             // recalculate main clock & rewrite main clock data to file
2444             GTP_ERROR("main clock data in %s is wrong, recalculate main clock", GTP_MAIN_CLK_PATH);
2445         }
2446         else
2447         { 
2448             GTP_INFO("main clock data in %s used, main clock freq: %d", GTP_MAIN_CLK_PATH, p_main_clk[0]);
2449             filp_close(clk_filp, NULL);
2450             goto update_main_clk;
2451         }
2452     }
2453     
2454 #if GTP_ESD_PROTECT
2455     gtp_esd_switch(ts->client, SWITCH_OFF);
2456 #endif
2457     ret = gup_clk_calibration();
2458     gtp_esd_recovery(ts->client);
2459     
2460 #if GTP_ESD_PROTECT
2461     gtp_esd_switch(ts->client, SWITCH_ON);
2462 #endif
2463
2464     GTP_INFO("calibrate main clock: %d", ret);
2465     if (ret < 50 || ret > 120)
2466     {
2467         GTP_ERROR("wrong main clock: %d", ret);
2468         goto exit_main_clk;
2469     }
2470     
2471     // Sum{0x8020~0x8025} = 0
2472     for (i = 0; i < 5; ++i)
2473     {
2474         p_main_clk[i] = ret;
2475         clk_chksum += p_main_clk[i];
2476     }
2477     p_main_clk[5] = 0 - clk_chksum;
2478     
2479     if (!IS_ERR(clk_filp))
2480     {
2481         GTP_DEBUG("write main clock data into %s", GTP_MAIN_CLK_PATH);
2482         clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2483         clk_filp->f_op->write(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2484         filp_close(clk_filp, NULL);
2485     }
2486     
2487 update_main_clk:
2488     ret = i2c_write_bytes(ts->client, GTP_REG_MAIN_CLK, p_main_clk, 6);
2489     if (FAIL == ret)
2490     {
2491         GTP_ERROR("update main clock failed!");
2492         return FAIL;
2493     }
2494     return SUCCESS;
2495     
2496 exit_main_clk:
2497     if (!IS_ERR(clk_filp))
2498     {
2499         filp_close(clk_filp, NULL);
2500     }
2501     return FAIL;
2502 }
2503
2504
2505 s32 gtp_gt9xxf_init(struct i2c_client *client)
2506 {
2507     s32 ret = 0;
2508     
2509     ret = gup_fw_download_proc(NULL, GTP_FL_FW_BURN); 
2510     if (FAIL == ret)
2511     {
2512         return FAIL;
2513     }
2514     
2515     ret = gtp_fw_startup(client);
2516     if (FAIL == ret)
2517     {
2518         return FAIL;
2519     }
2520     return SUCCESS;
2521 }
2522
2523 void gtp_get_chip_type(struct goodix_ts_data *ts)
2524 {
2525     u8 opr_buf[10] = {0x00};
2526     s32 ret = 0;
2527     
2528     msleep(10);
2529     
2530     ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CHIP_TYPE, opr_buf, 10);
2531     
2532     if (FAIL == ret)
2533     {
2534         GTP_ERROR("Failed to get chip-type, set chip type default: GOODIX_GT9");
2535         ts->chip_type = CHIP_TYPE_GT9;
2536         return;
2537     }
2538     
2539     if (!memcmp(opr_buf, "GOODIX_GT9", 10))
2540     {
2541         ts->chip_type = CHIP_TYPE_GT9;
2542     }
2543     else // GT9XXF
2544     {
2545         ts->chip_type = CHIP_TYPE_GT9F;
2546     }
2547     GTP_INFO("Chip Type: %s", (ts->chip_type == CHIP_TYPE_GT9) ? "GOODIX_GT9" : "GOODIX_GT9F");
2548 }
2549
2550 #endif
2551 //************* For GT9XXF End ************//
2552
2553 /*******************************************************
2554 Function:
2555     I2c probe.
2556 Input:
2557     client: i2c device struct.
2558     id: device id.
2559 Output:
2560     Executive outcomes. 
2561         0: succeed.
2562 *******************************************************/
2563 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
2564 {
2565     s32 ret = -1;
2566     struct goodix_ts_data *ts;
2567     u16 version_info;
2568     
2569     struct device_node *np = client->dev.of_node;
2570     enum of_gpio_flags rst_flags, pwr_flags;
2571     u32 val;
2572         printk("%s() start\n", __func__);
2573
2574     
2575     GTP_DEBUG_FUNC();
2576     
2577     //do NOT remove these logs
2578     GTP_INFO("GTP Driver Version: %s", GTP_DRIVER_VERSION);
2579     GTP_INFO("GTP I2C Address: 0x%02x", client->addr);
2580
2581     i2c_connect_client = client;
2582     
2583     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 
2584     {
2585         GTP_ERROR("I2C check functionality failed.");
2586         return -ENODEV;
2587     }
2588     ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2589     if (ts == NULL)
2590     {
2591         GTP_ERROR("Alloc GFP_KERNEL memory failed.");
2592         return -ENOMEM;
2593     }
2594     
2595     memset(ts, 0, sizeof(*ts));
2596     
2597     if (!np) {
2598         dev_err(&client->dev, "no device tree\n");
2599         return -EINVAL;
2600     }
2601     if (of_property_read_u32(np, "tp-size", &val)) {
2602         dev_err(&client->dev, "no max-x defined\n");
2603         return -EINVAL;
2604     }
2605
2606     if(val == 89){
2607         m89or101 = TRUE;
2608         mGtpChange_X2Y = TRUE;
2609         mGtp_X_Reverse = FALSE;
2610         mGtp_Y_Reverse = TRUE;
2611     }else if(val == 101){
2612         m89or101 = FALSE;
2613         mGtpChange_X2Y = TRUE;
2614         mGtp_X_Reverse = TRUE;
2615         mGtp_Y_Reverse = FALSE;
2616     } else if (val == 911) {
2617         m89or101 = FALSE;
2618         bgt911 = TRUE;
2619         mGtpChange_X2Y = TRUE;
2620         mGtp_X_Reverse = FALSE;
2621         mGtp_Y_Reverse = TRUE;
2622     }
2623
2624     ts->irq_pin = of_get_named_gpio_flags(np, "touch-gpio", 0, (enum of_gpio_flags *)(&ts->irq_flags));
2625     ts->rst_pin = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
2626     ts->pwr_pin = of_get_named_gpio_flags(np, "power-gpio", 0, &pwr_flags);
2627     //ts->tp_select_pin = of_get_named_gpio_flags(np, "tp-select-gpio", 0, &tp_select_flags);
2628     if (of_property_read_u32(np, "max-x", &val)) {
2629         dev_err(&client->dev, "no max-x defined\n");
2630         return -EINVAL;
2631     }
2632     //ts->abs_x_max = val;
2633     if (of_property_read_u32(np, "max-y", &val)) {
2634         dev_err(&client->dev, "no max-y defined\n");
2635         return -EINVAL;
2636     }
2637     //ts->abs_y_max = val;
2638     ts->pendown =PEN_RELEASE;
2639     ts->client = client;
2640     
2641     
2642     INIT_WORK(&ts->work, goodix_ts_work_func);
2643     ts->client = client;
2644     spin_lock_init(&ts->irq_lock);          // 2.6.39 later
2645     // ts->irq_lock = SPIN_LOCK_UNLOCKED;   // 2.6.39 & before
2646 #if GTP_ESD_PROTECT
2647     ts->clk_tick_cnt = 2 * HZ;      // HZ: clock ticks in 1 second generated by system
2648     GTP_DEBUG("Clock ticks for an esd cycle: %d", ts->clk_tick_cnt);  
2649     spin_lock_init(&ts->esd_lock);
2650     // ts->esd_lock = SPIN_LOCK_UNLOCKED;
2651 #endif
2652
2653     i2c_set_clientdata(client, ts);
2654     
2655     ts->gtp_rawdiff_mode = 0;
2656
2657     ret = gtp_request_io_port(ts);
2658     if (ret < 0)
2659     {
2660         GTP_ERROR("GTP request IO port failed.");
2661         //return ret;
2662         goto probe_init_error_requireio;
2663     }
2664 /*
2665     if(gpio_get_value(ts->tp_select_pin))//WGJ
2666     {
2667         printk("tp 11111111111111111111111111111 WGJ\n\n");
2668         mGtp_X_Reverse = FALSE;
2669         mGtp_Y_Reverse = TRUE;
2670     }
2671     else//DPT
2672     {
2673         printk("tp 00000000000000000000000000000 DPT\n\n");
2674         mGtp_X_Reverse = TRUE;//FALSE;
2675         mGtp_Y_Reverse = TRUE;
2676     }
2677  */
2678 #if GTP_COMPATIBLE_MODE
2679     gtp_get_chip_type(ts);
2680     
2681     if (CHIP_TYPE_GT9F == ts->chip_type)
2682     {
2683         ret = gtp_gt9xxf_init(ts->client);
2684         if (FAIL == ret)
2685         {
2686             GTP_INFO("Failed to init GT9XXF.");
2687         }
2688     }
2689 #endif
2690
2691     ret = gtp_i2c_test(client);
2692     if (ret < 0)
2693     {
2694         printk("<%s>_%d    I2C communication ERROR!\n", __func__, __LINE__);
2695         goto probe_init_error;
2696     }
2697
2698     ret = gtp_read_version(client, &version_info);
2699     if (ret < 0)
2700     {
2701         GTP_ERROR("Read version failed.");
2702     }
2703     
2704     ret = gtp_init_panel(ts);
2705     if (ret < 0)
2706     {
2707         GTP_ERROR("GTP init panel failed.");
2708         //ts->abs_x_max = GTP_MAX_WIDTH;
2709         //ts->abs_y_max = GTP_MAX_HEIGHT;
2710         ts->int_trigger_type = GTP_INT_TRIGGER;
2711     }
2712     
2713     // Create proc file system
2714     gt91xx_config_proc = proc_create(GT91XX_CONFIG_PROC_FILE, 0666, NULL, &config_proc_ops);
2715     if (gt91xx_config_proc == NULL)
2716     {
2717         GTP_ERROR("create_proc_entry %s failed\n", GT91XX_CONFIG_PROC_FILE);
2718     }
2719     else
2720     {
2721         GTP_INFO("create proc entry %s success", GT91XX_CONFIG_PROC_FILE);
2722     }
2723     
2724 #if GTP_AUTO_UPDATE
2725     ret = gup_init_update_proc(ts);
2726     if (ret < 0)
2727     {
2728         GTP_ERROR("Create update thread error.");
2729     }
2730 #endif
2731
2732     ret = gtp_request_input_dev(ts);
2733     if (ret < 0)
2734     {
2735         GTP_ERROR("GTP request input dev failed");
2736     }
2737     ret = gtp_request_irq(ts); 
2738     if (ret < 0)
2739     {
2740         GTP_INFO("GTP works in polling mode.");
2741     }
2742     else
2743     {
2744         GTP_INFO("GTP works in interrupt mode.");
2745     }
2746
2747     if (ts->use_irq)
2748     {
2749         gtp_irq_enable(ts);
2750     }
2751     
2752 #if GTP_CREATE_WR_NODE
2753     init_wr_node(client);
2754 #endif
2755     
2756 #if GTP_ESD_PROTECT
2757     gtp_esd_switch(client, SWITCH_ON);
2758 #endif
2759     return 0;
2760
2761 probe_init_error:
2762     printk("   <%s>_%d  prob error !!!!!!!!!!!!!!!\n", __func__, __LINE__);    
2763     tp_unregister_fb(&ts->tp);
2764     GTP_GPIO_FREE(ts->rst_pin);
2765     GTP_GPIO_FREE(ts->irq_pin);
2766 probe_init_error_requireio:
2767     tp_unregister_fb(&ts->tp); 
2768     kfree(ts);
2769     return ret;
2770 }
2771
2772
2773 /*******************************************************
2774 Function:
2775     Goodix touchscreen driver release function.
2776 Input:
2777     client: i2c device struct.
2778 Output:
2779     Executive outcomes. 0---succeed.
2780 *******************************************************/
2781 static int goodix_ts_remove(struct i2c_client *client)
2782 {
2783     struct goodix_ts_data *ts = i2c_get_clientdata(client);
2784     
2785     tp_unregister_fb(&ts->tp);
2786
2787     GTP_DEBUG_FUNC();
2788
2789 #if GTP_CREATE_WR_NODE
2790     uninit_wr_node();
2791 #endif
2792
2793 #if GTP_ESD_PROTECT
2794     destroy_workqueue(gtp_esd_check_workqueue);
2795 #endif
2796
2797     if (ts) 
2798     {
2799         if (ts->use_irq)
2800         {
2801             //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
2802             
2803             gpio_direction_input(ts->irq_pin);
2804             //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
2805             
2806             GTP_GPIO_FREE(ts->irq_pin);
2807             free_irq(client->irq, ts);
2808         }
2809         else
2810         {
2811             hrtimer_cancel(&ts->timer);
2812         }
2813     }   
2814     
2815     GTP_INFO("GTP driver removing...");
2816     i2c_set_clientdata(client, NULL);
2817     input_unregister_device(ts->input_dev);
2818     kfree(ts);
2819
2820     return 0;
2821 }
2822
2823
2824
2825
2826
2827 #if GTP_ESD_PROTECT
2828 s32 gtp_i2c_read_no_rst(struct i2c_client *client, u8 *buf, s32 len)
2829 {
2830     struct i2c_msg msgs[2];
2831     s32 ret=-1;
2832     s32 retries = 0;
2833
2834     GTP_DEBUG_FUNC();
2835
2836     msgs[0].flags = !I2C_M_RD;
2837     msgs[0].addr  = client->addr;
2838     msgs[0].len   = GTP_ADDR_LENGTH;
2839     msgs[0].buf   = &buf[0];
2840     //msgs[0].scl_rate = 300 * 1000;    // for Rockchip, etc.
2841     
2842     msgs[1].flags = I2C_M_RD;
2843     msgs[1].addr  = client->addr;
2844     msgs[1].len   = len - GTP_ADDR_LENGTH;
2845     msgs[1].buf   = &buf[GTP_ADDR_LENGTH];
2846     //msgs[1].scl_rate = 300 * 1000;
2847
2848     while(retries < 5)
2849     {
2850         ret = i2c_transfer(client->adapter, msgs, 2);
2851         if(ret == 2)break;
2852         retries++;
2853     }
2854     if ((retries >= 5))
2855     {    
2856         GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2857     }
2858     return ret;
2859 }
2860
2861 s32 gtp_i2c_write_no_rst(struct i2c_client *client,u8 *buf,s32 len)
2862 {
2863     struct i2c_msg msg;
2864     s32 ret = -1;
2865     s32 retries = 0;
2866
2867     GTP_DEBUG_FUNC();
2868
2869     msg.flags = !I2C_M_RD;
2870     msg.addr  = client->addr;
2871     msg.len   = len;
2872     msg.buf   = buf;
2873     //msg.scl_rate = 300 * 1000;    // for Rockchip, etc
2874
2875     while(retries < 5)
2876     {
2877         ret = i2c_transfer(client->adapter, &msg, 1);
2878         if (ret == 1)break;
2879         retries++;
2880     }
2881     if((retries >= 5))
2882     {
2883         GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2884     }
2885     return ret;
2886 }
2887 /*******************************************************
2888 Function:
2889     switch on & off esd delayed work
2890 Input:
2891     client:  i2c device
2892     on:      SWITCH_ON / SWITCH_OFF
2893 Output:
2894     void
2895 *********************************************************/
2896 void gtp_esd_switch(struct i2c_client *client, s32 on)
2897 {
2898     struct goodix_ts_data *ts;
2899     
2900     ts = i2c_get_clientdata(client);
2901     spin_lock(&ts->esd_lock);
2902     
2903     if (SWITCH_ON == on)     // switch on esd 
2904     {
2905         if (!ts->esd_running)
2906         {
2907             ts->esd_running = 1;
2908             spin_unlock(&ts->esd_lock);
2909             GTP_INFO("Esd started");
2910             queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, ts->clk_tick_cnt);
2911         }
2912         else
2913         {
2914             spin_unlock(&ts->esd_lock);
2915         }
2916     }
2917     else    // switch off esd
2918     {
2919         if (ts->esd_running)
2920         {
2921             ts->esd_running = 0;
2922             spin_unlock(&ts->esd_lock);
2923             GTP_INFO("Esd cancelled");
2924             cancel_delayed_work_sync(&gtp_esd_check_work);
2925         }
2926         else
2927         {
2928             spin_unlock(&ts->esd_lock);
2929         }
2930     }
2931 }
2932
2933 /*******************************************************
2934 Function:
2935     Initialize external watchdog for esd protect
2936 Input:
2937     client:  i2c device.
2938 Output:
2939     result of i2c write operation. 
2940         1: succeed, otherwise: failed
2941 *********************************************************/
2942 static s32 gtp_init_ext_watchdog(struct i2c_client *client)
2943 {
2944     u8 opr_buffer[3] = {0x80, 0x41, 0xAA};
2945     GTP_DEBUG("[Esd]Init external watchdog");
2946     return gtp_i2c_write_no_rst(client, opr_buffer, 3);
2947 }
2948
2949 /*******************************************************
2950 Function:
2951     Esd protect function.
2952     External watchdog added by meta, 2013/03/07
2953 Input:
2954     work: delayed work
2955 Output:
2956     None.
2957 *******************************************************/
2958 static void gtp_esd_check_func(struct work_struct *work)
2959 {
2960     s32 i;
2961     s32 ret = -1;
2962     struct goodix_ts_data *ts = NULL;
2963     u8 esd_buf[5] = {0x80, 0x40};
2964     
2965     GTP_DEBUG_FUNC();
2966    
2967     ts = i2c_get_clientdata(i2c_connect_client);
2968
2969     if (ts->gtp_is_suspend)
2970     {
2971         GTP_INFO("Esd suspended!");
2972         return;
2973     }
2974     
2975     for (i = 0; i < 3; i++)
2976     {
2977         ret = gtp_i2c_read_no_rst(ts->client, esd_buf, 4);
2978         
2979         GTP_DEBUG("[Esd]0x8040 = 0x%02X, 0x8041 = 0x%02X", esd_buf[2], esd_buf[3]);
2980         if ((ret < 0))
2981         {
2982             // IIC communication problem
2983             continue;
2984         }
2985         else
2986         { 
2987             if ((esd_buf[2] == 0xAA) || (esd_buf[3] != 0xAA))
2988             {
2989                 // IC works abnormally..
2990                 u8 chk_buf[4] = {0x80, 0x40};
2991                 
2992                 gtp_i2c_read_no_rst(ts->client, chk_buf, 4);
2993                 
2994                 GTP_DEBUG("[Check]0x8040 = 0x%02X, 0x8041 = 0x%02X", chk_buf[2], chk_buf[3]);
2995                 
2996                 if ((chk_buf[2] == 0xAA) || (chk_buf[3] != 0xAA))
2997                 {
2998                     i = 3;
2999                     break;
3000                 }
3001                 else
3002                 {
3003                     continue;
3004                 }
3005             }
3006             else 
3007             {
3008                 // IC works normally, Write 0x8040 0xAA, feed the dog
3009                 esd_buf[2] = 0xAA; 
3010                 gtp_i2c_write_no_rst(ts->client, esd_buf, 3);
3011                 break;
3012             }
3013         }
3014     }
3015     if (i >= 3)
3016     {
3017     #if GTP_COMPATIBLE_MODE
3018         if (CHIP_TYPE_GT9F == ts->chip_type)
3019         {        
3020             if (ts->rqst_processing)
3021             {
3022                 GTP_INFO("Request processing, no esd recovery");
3023             }
3024             else
3025             {
3026                 GTP_ERROR("IC working abnormally! Process esd recovery.");
3027                 esd_buf[0] = 0x42;
3028                 esd_buf[1] = 0x26;
3029                 esd_buf[2] = 0x01;
3030                 esd_buf[3] = 0x01;
3031                 esd_buf[4] = 0x01;
3032                 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3033                 msleep(50);
3034                 gtp_esd_recovery(ts->client);
3035             }
3036         }
3037         else
3038     #endif
3039         {
3040             GTP_ERROR("IC working abnormally! Process reset guitar.");
3041             esd_buf[0] = 0x42;
3042             esd_buf[1] = 0x26;
3043             esd_buf[2] = 0x01;
3044             esd_buf[3] = 0x01;
3045             esd_buf[4] = 0x01;
3046             gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3047             msleep(50);
3048             gtp_reset_guitar(ts->client, 50);
3049             msleep(50);
3050             gtp_send_cfg(ts->client);
3051         }
3052     }
3053
3054     if(!ts->gtp_is_suspend)
3055     {
3056         queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, ts->clk_tick_cnt);
3057     }
3058     else
3059     {
3060         GTP_INFO("Esd suspended!");
3061     }
3062     return;
3063 }
3064 #endif
3065
3066 static const struct i2c_device_id goodix_ts_id[] = {
3067     { GTP_I2C_NAME, 0 },
3068     { }
3069 };
3070
3071 static struct of_device_id goodix_ts_dt_ids[] = {
3072     { .compatible = "goodix,gt9xx" },
3073     { }
3074 };
3075
3076 static struct i2c_driver goodix_ts_driver = {
3077     .probe      = goodix_ts_probe,
3078     .remove     = goodix_ts_remove,
3079     .id_table   = goodix_ts_id,
3080     .driver = {
3081         .name     = GTP_I2C_NAME,
3082         .owner    = THIS_MODULE,
3083          .of_match_table = of_match_ptr(goodix_ts_dt_ids),
3084     },
3085 };
3086
3087 /*******************************************************    
3088 Function:
3089     Driver Install function.
3090 Input:
3091     None.
3092 Output:
3093     Executive Outcomes. 0---succeed.
3094 ********************************************************/
3095 static int goodix_ts_init(void)
3096 {
3097     s32 ret;
3098
3099     GTP_DEBUG_FUNC();   
3100     GTP_INFO("GTP driver installing...");
3101     goodix_wq = create_singlethread_workqueue("goodix_wq");
3102     if (!goodix_wq)
3103     {
3104         GTP_ERROR("Creat workqueue failed.");
3105         return -ENOMEM;
3106     }
3107 #if GTP_ESD_PROTECT
3108     INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
3109     gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
3110 #endif
3111     ret = i2c_add_driver(&goodix_ts_driver);
3112     return ret; 
3113 }
3114
3115 /*******************************************************    
3116 Function:
3117     Driver uninstall function.
3118 Input:
3119     None.
3120 Output:
3121     Executive Outcomes. 0---succeed.
3122 ********************************************************/
3123 static void goodix_ts_exit(void)
3124 {
3125     GTP_DEBUG_FUNC();
3126     GTP_INFO("GTP driver exited.");
3127     i2c_del_driver(&goodix_ts_driver);
3128     if (goodix_wq)
3129     {
3130         destroy_workqueue(goodix_wq);
3131     }
3132 }
3133 //late_initcall(goodix_ts_init);
3134 module_init(goodix_ts_init);
3135
3136 module_exit(goodix_ts_exit);
3137
3138 MODULE_DESCRIPTION("GTP Series Driver");
3139 MODULE_LICENSE("GPL");