ts: rk29_i2c_goodix: goodix_init_panel retry write cfg info when error
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / goodix_touch_82x.c
1 /*
2  * 
3  * Copyright (C) 2011 Goodix, Inc.
4  * 
5  * Author: Scott
6  * Date: 2012.01.05
7  */
8  
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <mach/gpio.h>
17 //#include <plat/gpio-cfg.h>
18 #include <linux/irq.h>
19 #include <linux/err.h>
20 #include<linux/workqueue.h>
21 #include<linux/slab.h>
22 #include <linux/goodix_touch_82x.h>    //w++
23
24 #include <linux/input/mt.h>
25
26 #define READ_TOUCH_ADDR_H   0x0F
27 #define READ_TOUCH_ADDR_L   0x40
28 #define READ_KEY_ADDR_H     0x0F
29 #define READ_KEY_ADDR_L     0x41
30 #define READ_COOR_ADDR_H    0x0F
31 #define READ_COOR_ADDR_L    0x42
32 #define RESOLUTION_LOC      71
33 #define TRIGGER_LOC         66
34 //#typedef s32 int    //w++
35
36
37 struct goodix_ts_data {
38         uint16_t addr;
39         struct i2c_client *client;
40         struct input_dev *input_dev;
41         bool use_irq;
42         bool use_reset;
43         bool init_finished;
44         struct hrtimer timer;
45         struct work_struct  work;
46         char phys[32];
47         int bad_data;
48         int retry;
49         s32 (*power)(struct goodix_ts_data * ts, s32 on);
50         struct early_suspend early_suspend;
51         int xmax;
52         int ymax;
53         bool swap_xy;
54         bool xpol;
55         bool ypol;
56         int irq_is_disable;
57         int interrupt_port;
58         int reset_port;
59         
60 };
61 struct goodix_ts_data *ts82x_temp;              //w++
62
63
64
65
66
67
68 static struct workqueue_struct *goodix_wq;
69 static const char *goodix_ts_name ="Goodix TouchScreen of Guitar ";//"Goodix Capacitive TouchScreen";
70
71 static s32 goodix_ts_remove(struct i2c_client *);
72
73 #ifdef CONFIG_HAS_EARLYSUSPEND
74 static void goodix_ts_early_suspend(struct early_suspend *h);
75 static void goodix_ts_late_resume(struct early_suspend *h);
76 #endif
77
78 #ifdef CREATE_WR_NODE
79 extern s32 init_wr_node(struct i2c_client*);
80 extern void uninit_wr_node(void);
81 #endif
82
83 #ifdef AUTO_UPDATE_GUITAR
84 extern s32 init_update_proc(struct goodix_ts_data *);
85 #else
86 static void guitar_reset( struct goodix_ts_data *ts,u8 ms);
87 #endif
88
89 static int err_gt82x = 0;   //w++记载有没有此设备的检测
90 //#define LONGPRESS_LOCK_SPECKEY   //ÊÇ·ñʹÓ󀰎ijŒü(ÈçsearchŒü)Ëøס¹ŠÄÜŒüµÄ¹ŠÄÜ
91 #ifdef LONGPRESS_LOCK_SPECKEY
92 #define KEY_LOCK KEY_F13
93 //#define ORIGIN_KEY KEY_MENU
94 #define LOCK_LONG_PRESS_CNT 20
95 static int Origin2LockPressCnt = 0;
96 static int lockflag =0;
97 //static int touch_key_hold_press = 0;
98
99 ssize_t glock_status_show_gt82x(struct device *dev, char *buf)
100 {
101         return sprintf(buf, "%d", lockflag);
102 }
103 struct timer_list longkey_timer_82x;
104 #endif
105 #ifdef LONGPRESS_LOCK_SPECKEY
106 static DRIVER_ATTR(get_lock_status, 0777, glock_status_show_gt82x, NULL);
107 #endif
108 /*
109 struct goodix_i2c_rmi_platform_data {
110     uint32_t version;    // Use this entry for panels with //
111     //reservation
112    
113 };
114 */
115
116 #if 0
117 #define TOUCH_MAX_HEIGHT   1024   //w++2
118 #define TOUCH_MAX_WIDTH     768
119 #else
120 #define AUTO_SET
121 u16 TOUCH_MAX_HEIGHT;
122 u16 TOUCH_MAX_WIDTH;
123 #endif
124
125 #define GT828_I2C_RATE 200000
126
127
128 /*******************************************************        
129 ���ܣ�  
130         ��ȡ�ӻ����
131         ÿ��������������i2c_msg��ɣ���1����Ϣ���ڷ��ʹӻ��ַ��
132         ��2�����ڷ��Ͷ�ȡ��ַ��ȡ����ݣ�ÿ����Ϣǰ������ʼ�ź�
133 ����
134         client: i2c�豸�����豸��ַ
135         buf[0]~buf[1]��      ���ֽ�Ϊ��ȡ��ַ
136         buf[2]~buf[len]����ݻ�����
137         len��       ��ȡ��ݳ���
138 return��
139         ִ����Ϣ��
140 *********************************************************/
141 /*Function as i2c_master_send */
142
143 static s32 i2c_read_bytes(struct i2c_client *client, u8 *buf, s32 len)
144 {
145     struct i2c_msg msgs[2];
146     s32 ret=-1;
147
148     //����д��ַ
149     msgs[0].flags=!I2C_M_RD; //д��Ϣ
150     msgs[0].addr=client->addr;
151     msgs[0].len=2;
152     msgs[0].buf=&buf[0];
153     msgs[0].scl_rate=100000;
154     msgs[0].udelay=2000;
155     //�������
156     msgs[1].flags=I2C_M_RD;//����Ϣ
157     msgs[1].addr=client->addr;
158     msgs[1].len=len - ADDR_LENGTH;
159     msgs[1].buf=&buf[2];
160     msgs[1].scl_rate=100000;
161     msgs[1].udelay=2000;        
162
163     ret=i2c_transfer(client->adapter,msgs, 2);
164
165     return ret;
166 }
167
168 /*******************************************************        
169 ���ܣ�
170         ��ӻ�д���
171 ����
172         client: i2c�豸�����豸��ַ
173         buf[0]~buf[1]��      ���ֽ�Ϊд��ַ
174         buf[2]~buf[len]����ݻ�����
175         len��       ��ݳ���       
176 return��
177         ִ����Ϣ��
178 *******************************************************/
179 /*Function as i2c_master_send */
180
181 static s32 i2c_write_bytes(struct i2c_client *client,u8 *data,s32 len)
182 {
183     struct i2c_msg msg;
184     s32 ret=-1;
185     
186     //�����豸��ַ
187     msg.flags=!I2C_M_RD;//д��Ϣ
188     msg.addr=client->addr;
189     msg.len=len;
190     msg.buf=data;        
191     msg.scl_rate=100000;
192     msg.udelay=2000;
193     ret=i2c_transfer(client->adapter,&msg, 1);
194
195     return ret;
196 }
197
198 /*******************************************************
199 ���ܣ�
200         ����ǰ׺����
201         
202         ts:     client˽����ݽṹ��
203 return��
204     �ɹ�����1
205 *******************************************************/
206 static s32 i2c_pre_cmd(struct goodix_ts_data *ts)
207 {
208     s32 ret;
209     u8 pre_cmd_data[2]={0x0f, 0xff};
210
211     ret=i2c_write_bytes(ts->client,pre_cmd_data,2);
212     return ret;//*/
213 }
214
215 /*******************************************************
216 ���ܣ�
217         ���ͺ�׺����
218         
219         ts:     client˽����ݽṹ��
220 return��
221     �ɹ�����1
222 *******************************************************/
223 static s32 i2c_end_cmd(struct goodix_ts_data *ts)
224 {
225     s32 ret;
226     u8 end_cmd_data[2]={0x80, 0x00};    
227
228     ret=i2c_write_bytes(ts->client,end_cmd_data,2);
229     return ret;//*/
230 }
231
232 /*******************************************************
233 ���ܣ�
234         Guitar��ʼ���������ڷ���������Ϣ����ȡ�汾��Ϣ
235 ����
236         ts:     client˽����ݽṹ��
237 return��
238         ִ�н���룬0��ʾ��ִ��
239 *******************************************************/
240 s32 goodix_init_panel(struct goodix_ts_data *ts, u8 send)
241 {
242     s32 ret = -1;
243     u8 config_info[]=
244     {
245         0x0F,0x80,/*config address*/
246 #if 1   
247 // 300-N3216E-A00               
248 //0x02,0x11,0x03,0x12,0x04,0x13,0x05,0x14,0x06,0x15,0x07,0x16,0x08,0x17,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x11,0x07,0x10,0x06,0x0F,0x05,0x0E,0x04,0x0D,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,0x03,0xE0,0x10,0x10,0x21,0x00,0x00,0x09,0x00,0x00,0x02,0x45,0x2D,0x1C,0x03,0x00,0x05,0x00,0x02,0x58,0x03,0x20,0x25,0x29,0x27,0x2B,0x25,0x00,0x06,0x19,0x25,0x14,0x10,0x00,0x13,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
249 //300-N3788E-A00-V1.0             1024*768
250 //0x02,0x11,0x03,0x12,0x04,0x13,0x05,0x14,0x06,0x15,0x07,0x16,0x08,0x17,0x09,0x18,0x0A,0x19,0xFF,0x13,0xFF,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x12,0x08,0x11,0x07,0x10,0x06,0x0F,0x05,0x0E,0x04,0x0D,0x03,0xFF,0x00,0xFF,0x0E,0x0F,0x10,0x11,0x12,0x09,0x03,0x88,0x88,0x88,0x25,0x00,0x00,0x08,0x00,0x00,0x02,0x3C,0x28,0x1C,0x03,0x00,0x05,0x00,0x02,0x58,0x03,0x20,0x33,0x38,0x30,0x35,0x25,0x00,0x25,0x19,0x05,0x14,0x10,0x02,0x30,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
251 0x02,0x11,0x03,0x12,0x04,0x13,0x05,0x14,0x06,0x15,0x07,0x16,0x08,0x17,0x09,0x18,0x0A,0x19,0x0B,0x1A,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x12,0x08,0x11,0x07,0x10,0x06,0x0F,0x05,0x0E,0x04,0x0D,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,0x03,0xE0,0x10,0x10,0x19,0x00,0x00,0x08,0x00,0x00,0x02,0x45,0x2D,0x1C,0x03,0x00,0x05,0x00,0x02,0x58,0x03,0x20,0x2D,0x38,0x2F,0x3B,0x25,0x00,0x06,0x19,0x25,0x14,0x10,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
252 #else
253 0x02,0x11,0x03,0x12,0x04,0x13,0x05,0x14,0x06,0x15,0x07,0x16,0x08,0x17,0x09,0x18,0x0A,0x19,0x0B,0x1A,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x12,0x08,0x11,0x07,0x10,0x06,0x0F,0x05,0x0E,0x04,0x0D,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,0x03,0xE0,0x10,0x10,0x19,0x00,0x00,0x08,0x00,0x00,0x02,0x45,0x2D,0x1C,0x03,0x00,0x05,0x00,0x02,0x58,0x03,0x20,0x2D,0x38,0x2F,0x3B,0x25,0x00,0x06,0x19,0x25,0x14,0x10,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
254 #endif
255     };
256
257 #ifdef AUTO_SET
258     TOUCH_MAX_WIDTH  = ((config_info[RESOLUTION_LOC] << 8)|config_info[RESOLUTION_LOC + 1]);
259     TOUCH_MAX_HEIGHT = ((config_info[RESOLUTION_LOC + 2] << 8)|config_info[RESOLUTION_LOC + 3]);
260
261     GTDEBUG_MSG("TOUCH_MAX_WIDTH  : 0x%d\n", (s32)TOUCH_MAX_WIDTH);
262     GTDEBUG_MSG("TOUCH_MAX_HEIGHT : 0x%d\n", (s32)TOUCH_MAX_HEIGHT);
263 #else
264 /*    config_info[RESOLUTION_LOC]     = TOUCH_MAX_WIDTH >> 8;
265     config_info[RESOLUTION_LOC + 1] = TOUCH_MAX_WIDTH & 0xff;
266     config_info[RESOLUTION_LOC + 2] = TOUCH_MAX_HEIGHT >> 8;
267     config_info[RESOLUTION_LOC + 3] = TOUCH_MAX_HEIGHT & 0xff;*/
268 #endif
269
270     if (INT_TRIGGER == GT_IRQ_FALLING)
271     {
272         config_info[TRIGGER_LOC] &= 0xf7; 
273     }
274     else if (INT_TRIGGER == GT_IRQ_RISING)
275     {
276         config_info[TRIGGER_LOC] |= 0x08;
277     }
278
279     if (send)
280     {
281         ret=i2c_write_bytes(ts->client,config_info, (sizeof(config_info)/sizeof(config_info[0])));
282         if (ret <= 0)
283         {
284             return fail;
285         }
286         i2c_end_cmd(ts);
287         msleep(10);
288     }
289     return success;
290 }
291
292 static s32 touch_num(u8 value, s32 max)
293 {
294     s32 tmp = 0;
295
296     while((tmp < max) && value)
297     {
298         if ((value & 0x01) == 1)
299         {
300             tmp++;
301         }
302         value = value >> 1;
303     }
304
305     return tmp;
306 }
307
308 /*******************************************************        
309 ���ܣ�
310         ��������������
311         ���жϴ���������1�������ݣ�У����ٷ������
312 ����
313         ts:     client˽����ݽṹ��
314 return��
315     void
316 ********************************************************/
317 #ifdef LONGPRESS_LOCK_SPECKEY   //w++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
318 static void open_longkey_timer_82x(unsigned long data)
319 {
320     if((++Origin2LockPressCnt>LOCK_LONG_PRESS_CNT)&&(Origin2LockPressCnt<LOCK_LONG_PRESS_CNT+2))
321                 {  //w++超过一个时间,发长按消息
322                                 Origin2LockPressCnt = LOCK_LONG_PRESS_CNT + 3;
323                                 //if(lockflag ==0)  {
324                                         input_report_key(ts82x_temp->input_dev, KEY_LOCK, 1);
325                                         lockflag = (lockflag)? 0 : 1;
326                                         input_sync(ts82x_temp->input_dev);
327                                 //}
328                 }
329                 longkey_timer_82x.expires=jiffies+msecs_to_jiffies(100);
330     add_timer(&longkey_timer_82x);
331                 printk("w++++++++   Origin2LockPressCnt = %d\n",Origin2LockPressCnt); 
332                 printk("lockflag = %d\n",lockflag);
333 }
334 #endif
335
336 static void goodix_ts_work_func(struct work_struct *work)
337 {
338     u8 finger = 0;
339     u8 chk_sum = 0;
340     u8 key = 0;
341     static u8 last_key = 0;
342     u16 X_value;
343     u16 Y_value;
344         u16 value_tmp;
345     u32 count = 0;
346     u32 position = 0;
347     s32 ret = -1;
348     s32 tmp = 0;
349     s32 i;
350     u8 *coor_point;
351     u8 touch_data[2 + 2 + 5*MAX_FINGER_NUM + 1] = {READ_TOUCH_ADDR_H,READ_TOUCH_ADDR_L,0, 0};
352     static u8 finger_last[MAX_FINGER_NUM]={0};        //�ϴδ����������ָ����
353     u8 finger_current[MAX_FINGER_NUM] = {0};        //��ǰ�����������ָ����
354
355     struct goodix_ts_data *ts = container_of(work, struct goodix_ts_data, work);
356     struct i2c_client *client = ts->client;
357         struct goodix_i2c_rmi_platform_data *pdata = client->dev.platform_data;
358         
359 #ifndef INT_PORT
360 COORDINATE_POLL:
361 #endif
362     if( tmp > 9)
363     {
364         dev_info(&(ts->client->dev), "Because of transfer error,touchscreen stop working.\n");
365         goto XFER_ERROR ;
366     }
367
368     //���齫���һ���Զ�ȡ��
369     ret=i2c_read_bytes(ts->client, touch_data,sizeof(touch_data)/sizeof(touch_data[0])); 
370     i2c_end_cmd(ts);
371     if(ret <= 0) 
372     {
373         dev_err(&(ts->client->dev),"I2C transfer error. Number:%d\n ", ret);
374         ts->bad_data = 1;
375         tmp ++;
376 #ifndef INT_PORT
377         goto COORDINATE_POLL;
378 #else
379         goto XFER_ERROR;
380 #endif
381     }
382
383     if(ts->bad_data)
384     {
385         //TODO:Is sending config once again (to reset the chip) useful?    
386         ts->bad_data = 0;
387         msleep(20);
388     }
389
390     if((touch_data[2]&0xC0)!=0x80)
391     {
392         goto DATA_NO_READY;        
393     }
394
395
396     key = touch_data[3]&0x0f; // 1, 2, 4, 8
397     if (key == 0x0f)
398     {
399         if (fail == goodix_init_panel(ts, 1))
400         {
401 /**/        GTDEBUG_COOR("Reload config failed!\n");
402         }
403         else
404         {   
405             GTDEBUG_COOR("Reload config successfully!\n");
406         }
407         goto XFER_ERROR;
408     }
409
410     finger = (u8)touch_num(touch_data[2]&0x1f, MAX_FINGER_NUM);
411
412 /**/GTDEBUG_COOR("touch num:%x\n", finger);
413
414     for (i = 0;i < MAX_FINGER_NUM; i++)        
415     {
416         finger_current[i] = !!(touch_data[2] & (0x01<<(i)));
417     }
418
419
420     //����У���    
421     coor_point = &touch_data[4];
422     chk_sum = 0;
423     for ( i = 0; i < 5*finger; i++)
424     {
425         chk_sum += coor_point[i];
426 /**/ //  GTDEBUG_COOR("%5x", coor_point[i]);
427     }
428 /**///GTDEBUG_COOR("\ncheck sum:%x\n", chk_sum);
429 /**///GTDEBUG_COOR("check sum byte:%x\n", coor_point[5*finger]);
430     if (chk_sum != coor_point[5*finger])
431     {
432         goto XFER_ERROR;
433     }
434
435     //�������//
436     if (finger)
437     {
438         for(i = 0, position=0;position < MAX_FINGER_NUM; position++)
439         {  
440             if(finger_current[position])
441             {     
442                 X_value = (coor_point[i] << 8) | coor_point[i + 1];
443                 Y_value = (coor_point[i + 2] << 8) | coor_point[i + 3];
444
445                 input_mt_slot(ts->input_dev, position);
446                 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
447                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, FLAG_DOWN);
448                                 
449               //  input_report_key(ts->input_dev, BTN_TOUCH, 1);
450               //  input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, position - 1);
451                 if(pdata->swap_xy){
452                                         value_tmp = X_value;
453                                         X_value = Y_value;
454                                         Y_value = value_tmp;
455                                 }
456                                 //printk("goodix_touch_82x X_value=%d,Y_value=%d\n",X_value,Y_value);
457                 if(pdata->xpol)
458                                         X_value = pdata->xmax - X_value;
459                                 if(pdata->ypol)
460                                         Y_value = pdata->ymax - Y_value;
461                                 
462                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, X_value);  //can change x-y!!!
463                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, Y_value);
464               //  input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR,15);
465              //   input_mt_sync(ts->input_dev);
466                 i += 5;
467
468   //  /**/        GTDEBUG_COOR("FI:%x X:%04d Y:%04d\n",position, (s32)Y_value,(s32)X_value);
469 //    printk("-----------touch_num:%d;--------FI:%x----------- X:%04d ----------Y:%04d\n",finger,position, (s32)X_value,(s32)Y_value);
470     /**/    //    GTDEBUG_COOR("Y:%d\n", (s32)Y_value);
471             }
472           else if(finger_last[position])
473                 {
474                   input_mt_slot(ts->input_dev, position);
475                   input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
476                   GTDEBUG_COOR(" Finger  %d  release!!\n",position);
477                }
478         }
479     }
480     else
481     {
482               for(position=0;position < MAX_FINGER_NUM; position++)
483                 {
484                 if( finger_last[position])
485                    {
486                         input_mt_slot(ts->input_dev, position);
487                         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
488                          GTDEBUG_COOR(" Finger  %d  release!!\n",position);
489                     }
490               }
491   }
492
493     input_sync(ts->input_dev);
494     
495     for(position=0;position<MAX_FINGER_NUM; position++)
496     {
497         finger_last[position] = finger_current[position];
498     }
499
500 DATA_NO_READY:
501 XFER_ERROR:
502     if(ts->use_irq && ts->irq_is_disable == 1)
503     {
504         ts->irq_is_disable = 0;
505         enable_irq(ts->client->irq);
506     }
507 }
508
509 /*******************************************************        
510 ���ܣ�
511         ��ʱ����Ӧ����
512         �ɼ�ʱ�����������ȴ����������������У�֮�����¼�ʱ
513 ����
514         timer����������ļ�ʱ��        
515 return��
516         ��ʱ������ģʽ��HRTIMER_NORESTART��ʾ����Ҫ�Զ�����
517 ********************************************************/
518 static enum hrtimer_restart goodix_ts_timer_func(struct hrtimer *timer)
519 {
520     struct goodix_ts_data *ts = container_of(timer, struct goodix_ts_data, timer);
521
522     queue_work(goodix_wq, &ts->work);
523     hrtimer_start(&ts->timer, ktime_set(0, (POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
524
525     return HRTIMER_NORESTART;
526 }
527
528 /*******************************************************        
529 ���ܣ�
530         �ж���Ӧ����
531         ���жϴ��������ȴ��������?������
532 ����
533 return��
534     IRQ_HANDLED:interrupt was handled by this device
535 ********************************************************/
536 static irqreturn_t goodix_ts_irq_handler(s32 irq, void *dev_id)
537 {
538     struct goodix_ts_data *ts = (struct goodix_ts_data*)dev_id;
539         
540     if (ts->use_irq && (!ts->irq_is_disable))
541     {
542         disable_irq_nosync(ts->client->irq);
543         ts->irq_is_disable = 1;
544     }
545     
546     queue_work(goodix_wq, &ts->work);
547
548     return IRQ_HANDLED;
549 }
550
551 /*******************************************************        
552 ���ܣ�
553         �����Դ������IC����˯�߻��份��
554 ����
555         on:     0��ʾʹ��˯�ߣ�1Ϊ����
556 return��
557         �Ƿ����óɹ���successΪ�ɹ�
558         �����룺-1Ϊi2c����-2ΪGPIO����-EINVALΪ����on����
559 ********************************************************/
560 //#if defined(INT_PORT)    // 0 : sleep  1 wake up
561 static s32 goodix_ts_power(struct goodix_ts_data * ts, s32 on)
562 {
563     s32 ret = -1;
564
565     u8 i2c_control_buf[3] = {0x0f,0xf2,0xc0};        //suspend cmd
566
567     if(ts == NULL || !ts->use_irq)
568         return -2;
569
570     switch(on)
571     {
572     case 0:
573         ret = i2c_write_bytes(ts->client, i2c_control_buf, 3);
574         return ret;
575
576     case 1:
577         GPIO_DIRECTION_OUTPUT(ts->interrupt_port, 0);
578         mdelay(10);
579         GPIO_SET_VALUE(ts->interrupt_port, 1);
580         GPIO_DIRECTION_INPUT(ts->interrupt_port);
581         GPIO_PULL_UPDOWN(ts->interrupt_port, 0);
582
583         return success;
584
585     default:
586         GTDEBUG_MSG(KERN_DEBUG "%s: Cant't support this command.", goodix_ts_name);
587         return -EINVAL;
588     }
589
590 }
591
592 static s32 init_input_dev(struct goodix_ts_data *ts)
593 {
594     s32 i;
595     s32 ret = 0;
596
597     ts->input_dev = input_allocate_device();
598     if (ts->input_dev == NULL)
599     {
600         dev_dbg(&ts->client->dev,"goodix_ts_probe: Failed to allocate input device\n");
601         return fail;
602     }
603  
604 //    ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
605 //    ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
606  //   ts->input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);// absolute coor (x,y)
607
608
609     goodix_init_panel(ts, 0);
610  
611 #ifdef GOODIX_MULTI_TOUCH
612
613         __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
614         __set_bit(EV_ABS, ts->input_dev->evbit);
615         input_mt_init_slots(ts->input_dev, MAX_FINGER_NUM);
616         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
617         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, TOUCH_MAX_HEIGHT, 0, 0);
618         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, TOUCH_MAX_WIDTH, 0, 0);
619
620   /* input_mt_init_slots(ts->input_dev, MAX_FINGER_NUM);
621     input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
622     input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
623     input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, TOUCH_MAX_HEIGHT, 0, 0);
624     input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, TOUCH_MAX_WIDTH, 0, 0);
625     */
626 #else
627     input_set_abs_params(ts->input_dev, ABS_X, 0, TOUCH_MAX_HEIGHT, 0, 0);
628     input_set_abs_params(ts->input_dev, ABS_Y, 0, TOUCH_MAX_WIDTH, 0, 0);
629     input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, 0, 0);
630 #endif    
631
632     memcpy(ts->phys, "input/ts", 8);
633     ts->input_dev->name = goodix_ts_name;
634     ts->input_dev->phys = ts->phys;
635     ts->input_dev->id.bustype = BUS_I2C;
636     ts->input_dev->id.vendor = 0xDEAD;
637     ts->input_dev->id.product = 0xBEEF;
638     ts->input_dev->id.version = 10427;    //screen firmware version
639
640 #ifdef LONGPRESS_LOCK_SPECKEY    //w++
641     set_bit(KEY_LOCK, ts->input_dev->keybit);
642 #endif
643     ret = input_register_device(ts->input_dev);
644     if (ret) 
645     {
646         dev_err(&ts->client->dev,"Probe: Unable to register %s input device\n", ts->input_dev->name);
647         input_free_device(ts->input_dev);
648         return fail;
649     }
650     GTDEBUG_MSG("Register input device successfully!\n");
651
652     return success;
653 }
654
655 static s32 set_pins(struct goodix_ts_data *ts)
656 {
657     s32 ret = -1;
658     
659   //  ts->client->irq=TS_INT;        //If not defined in client
660     if (ts->client->irq)
661     {
662         ret = GPIO_REQUEST(ts->interrupt_port, "TS_INT");    //Request IO
663         if (ret < 0) 
664         {
665             dev_err(&ts->client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(s32)ts->interrupt_port,ret);
666             goto err_gpio_request_failed;
667         }
668         GTDEBUG_MSG("Request int port successfully!\n");
669         
670         GPIO_DIRECTION_INPUT(ts->interrupt_port);
671         GPIO_PULL_UPDOWN(ts->interrupt_port, 0);
672 //        GPIO_CFG_PIN(INT_PORT, INT_CFG);        //Set IO port function    
673
674
675 ret  = request_irq(gpio_to_irq(ts->interrupt_port), goodix_ts_irq_handler,
676                                 IRQF_TRIGGER_FALLING, ts->client->name, ts);
677
678      //   ret = request_irq(ts->client->irq, goodix_ts_irq_handler, INT_TRIGGER,    ts->client->name, ts);
679                       
680         if (ret != 0) 
681         {
682             dev_err(&ts->client->dev,"Cannot allocate ts INT!ERRNO:%d\n", ret);
683             GPIO_DIRECTION_INPUT(ts->interrupt_port);
684             GPIO_FREE(ts->interrupt_port);
685             goto err_gpio_request_failed;
686         }
687         else 
688         {
689             disable_irq(ts->client->irq);
690             ts->use_irq = 1;
691             ts->irq_is_disable = 1;
692             dev_dbg(&ts->client->dev, "Reques EIRQ %d successed on GPIO:%d\n", gpio_to_irq(ts->interrupt_port), ts->interrupt_port);
693         }
694     }
695
696 err_gpio_request_failed:
697     if (!ts->use_irq) 
698     {
699         hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
700         ts->timer.function = goodix_ts_timer_func;
701         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
702         GTDEBUG_MSG("Use timer!\n");
703     }
704
705     ret = GPIO_REQUEST(ts->reset_port, "TS_RESET");    //Request IO
706     if (ret < 0) 
707     {
708         dev_err(&ts->client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(s32)ts->reset_port,ret);
709     }
710     else
711     {
712         ts->use_reset = 1;
713         GPIO_DIRECTION_OUTPUT(ts->reset_port,1);
714         GPIO_PULL_UPDOWN(ts->reset_port, 0);
715         GTDEBUG_MSG("Request reset port successfully!\n");      
716     }
717
718     dev_info(&ts->client->dev,"Start %s in %s mode\n", 
719               ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
720
721     return ts->use_irq;
722 }
723
724 /*******************************************************        
725 ���ܣ�
726         ������̽�⺯��
727         ��ע����ʱ����(Ҫ����ڶ�Ӧ��client)��
728         ����IO,�жϵ���Դ���룻�豸ע�᣻��������ʼ���ȹ���
729 ����
730         client��������豸�ṹ��
731         id���豸ID
732 return��
733         ִ�н���룬0��ʾ��ִ��
734 ********************************************************/
735 static s32 goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
736 {
737     s32 ret = 0;
738     s32 retry=0;
739     struct goodix_ts_data *ts = NULL;
740     struct goodix_i2c_rmi_platform_data *pdata = client->dev.platform_data;
741                 //struct ft5406_platform_data *pdata = pdata = client->dev.platform_data;
742     printk("w++++++goodix_ts_probe gt28x ");
743
744         if (pdata->init_platform_hw)
745         {
746                 pdata->init_platform_hw();
747         }
748
749 //    GTDEBUG("Start to install Goodix Capacitive TouchScreen driver.\n");
750 //    GTDEBUG("*DRIVER INFORMATION\n");
751 //    GTDEBUG("**RELEASE DATE:%s.\n", RELEASE_DATE);
752 //    GTDEBUG("**COMPILE TIME:%s, %s.\n", __DATE__, __TIME__);
753 //    printk("gt82x---0000000000000000\n");
754     //Check I2C function
755     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 
756     {
757         dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
758         return -ENODEV;
759     }
760
761     ts = kzalloc(sizeof(*ts), GFP_KERNEL);
762     if (ts == NULL)
763     {
764         return -ENOMEM;
765     }
766       pdata = client->dev.platform_data;
767
768     INIT_WORK(&ts->work, goodix_ts_work_func);        //init work_struct
769     ts->client = client;
770     ts->power = goodix_ts_power;
771     ts->bad_data = 0;
772     ts->use_irq = 1;
773     ts->use_reset =1;
774     ts->irq_is_disable = 0;
775     ts->interrupt_port=pdata->gpio_irq;
776     ts->reset_port=pdata->gpio_reset;
777     i2c_set_clientdata(client, ts);
778  
779     if (fail == init_input_dev(ts))
780     {   kfree(ts);
781         return -1;
782     }
783     set_pins(ts);
784 #ifdef CONFIG_HAS_EARLYSUSPEND
785     ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
786     ts->early_suspend.suspend = goodix_ts_early_suspend;
787     ts->early_suspend.resume = goodix_ts_late_resume;
788     register_early_suspend(&ts->early_suspend);
789 #endif
790 #ifdef CREATE_WR_NODE
791     init_wr_node(client);
792 #endif
793 #ifdef AUTO_UPDATE_GUITAR
794     if (0xff == init_update_proc(ts))
795     {
796         GTDEBUG_MSG("Need update!\n");
797         return 0;
798     }
799 #else
800     msleep(5);
801     guitar_reset(ts,10);
802 #endif
803
804     //Test I2C connection. 
805 //    GTDEBUG_MSG("GT82X++++++   Testing I2C connection...\n");
806     for(retry = 0;retry < 3; retry++)
807     while(1)            //For GTDEBUG use!
808     {
809         ret = i2c_pre_cmd(ts);
810         if (ret > 0)
811             break;
812         msleep(20);
813     }
814     if(ret <= 0)
815     {
816         dev_err(&client->dev, "Warnning: I2C communication might be ERROR!\n");
817         GTDEBUG_MSG("I2C test failed. I2C addr:%x\n", client->addr);
818         goodix_ts_remove(ts->client);  kfree(ts);
819         return -1;
820     }
821 //    printk("gt82x---777777777777777\n");
822     //Send config
823     for (retry = 0; retry < 3; retry++)
824     {
825         if (success == goodix_init_panel(ts, 1))
826         {
827             GTDEBUG_MSG("Initialize successfully!\n");
828             break;
829         }
830     }
831     if (retry >= 3)
832     {
833         ts->bad_data=1;
834         GTDEBUG_MSG("Initialize failed!\n");
835         goodix_ts_remove(ts->client);  kfree(ts);
836         return -1;
837     }
838     //Enable interrupt
839     if(ts->use_irq && ts->irq_is_disable == 1)
840     {
841         ts->irq_is_disable = 0;
842         enable_irq(client->irq);
843     }
844
845     return 0;
846 }
847
848
849 /*******************************************************        
850 ���ܣ�
851         ����Դ�ͷ�
852 ����
853         client���豸�ṹ��
854 return��
855         ִ�н���룬success��ʾ��ִ��
856 ********************************************************/
857 static s32 goodix_ts_remove(struct i2c_client *client)
858 {
859     struct goodix_ts_data *ts = i2c_get_clientdata(client);
860
861     dev_notice(&client->dev,"The driver is removing...\n");
862     
863 #ifdef CONFIG_HAS_EARLYSUSPEND
864     unregister_early_suspend(&ts->early_suspend);
865 #endif
866
867 #ifdef CREATE_WR_NODE
868     uninit_wr_node();
869 #endif
870
871     if (ts && ts->use_irq) 
872     {
873         free_irq(client->irq, ts);
874         GPIO_DIRECTION_INPUT(ts->interrupt_port);
875         GPIO_FREE(ts->interrupt_port);
876     }
877     else if(ts)
878         hrtimer_cancel(&ts->timer);
879
880     if (ts && ts->use_reset)
881     {
882         GPIO_DIRECTION_INPUT(ts->reset_port);
883         GPIO_FREE(ts->interrupt_port);
884     }
885     
886     i2c_set_clientdata(client, NULL);
887     input_unregister_device(ts->input_dev);
888     input_free_device(ts->input_dev);
889     kfree(ts);
890     return success;
891 }
892
893 //ͣ���豸
894 static s32 goodix_ts_suspend(struct i2c_client *client, pm_message_t mesg)
895 {
896     s32 ret;
897     struct goodix_ts_data *ts = i2c_get_clientdata(client);
898
899     if (ts->irq_is_disable == 2)
900     {
901         return 0;
902     }
903
904     if (ts->use_irq)
905     {
906         if (!ts->irq_is_disable)
907         {
908             disable_irq(client->irq);
909             ts->irq_is_disable = 1;
910         }
911     }
912     else
913     {
914         hrtimer_cancel(&ts->timer);
915     }
916     
917     if (ts->power) 
918     {
919         ret = ts->power(ts, 0);
920         if (ret <= 0)
921             GTDEBUG_MSG(KERN_ERR "goodix_ts_resume power off failed\n");
922     }
923                 printk("-----gt813------suspend.\n");
924     return 0;
925 }
926
927 static s32 goodix_ts_resume(struct i2c_client *client)
928 {
929     s32 ret;
930     struct goodix_ts_data *ts = i2c_get_clientdata(client);
931
932     if (ts->irq_is_disable == 2)
933     {
934         return 0;
935     }
936
937     if (ts->power) 
938     {
939         ret = ts->power(ts, 1);
940         if (ret <= 0)
941             GTDEBUG_MSG(KERN_ERR "goodix_ts_resume power on failed\n");
942     }
943
944     if (ts->use_irq)
945     {
946         ts->irq_is_disable = 0;
947         enable_irq(client->irq);
948     }
949     else
950     {
951         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
952     }
953                 printk("-----gt813------goodix_ts_resume.\n");
954
955     return success;
956 }
957
958 #ifdef CONFIG_HAS_EARLYSUSPEND
959 static void goodix_ts_early_suspend(struct early_suspend *h)
960 {
961     struct goodix_ts_data *ts;
962     ts = container_of(h, struct goodix_ts_data, early_suspend);
963     goodix_ts_suspend(ts->client, PMSG_SUSPEND);
964 }
965
966 static void goodix_ts_late_resume(struct early_suspend *h)
967 {
968     struct goodix_ts_data *ts;
969     ts = container_of(h, struct goodix_ts_data, early_suspend);
970     goodix_ts_resume(ts->client);
971 }
972 #endif
973 //******************************Begin of firmware update surpport*******************************
974
975 #ifndef AUTO_UPDATE_GUITAR
976 static void guitar_reset( struct goodix_ts_data *ts,u8 ms)
977 {
978    
979     GPIO_DIRECTION_OUTPUT(ts->reset_port, 0);
980     GPIO_SET_VALUE(ts->reset_port, 0);
981     msleep(ms);
982
983    // GPIO_DIRECTION_OUTPUT(ts->reset_port);
984     GPIO_SET_VALUE(ts->reset_port, 1);
985     GPIO_PULL_UPDOWN(ts->reset_port, 0);
986
987     msleep(20);
988 err_gpio_request_failed:        
989     return;
990 }
991 #endif
992
993
994 //�����ڸ���� �豸���豸ID �б�
995 //only one client
996 static const struct i2c_device_id goodix_ts_id[] = {
997     {"Goodix-TS-82X", 0 },
998     { }
999 };
1000
1001 //�豸��ṹ��
1002 static struct i2c_driver goodix_ts_driver = {
1003     .probe      = goodix_ts_probe,
1004     .remove     = goodix_ts_remove,
1005 #ifndef CONFIG_HAS_EARLYSUSPEND
1006     .suspend    = goodix_ts_suspend,
1007     .resume     = goodix_ts_resume,
1008 #endif
1009     .id_table   = goodix_ts_id,
1010     .driver     = {
1011         .name   =  "Goodix-TS-82X",
1012         .owner  = THIS_MODULE,
1013     },
1014 };
1015 /*******************************************************        
1016 ���ܣ�
1017         ����غ���
1018 return��
1019         ִ�н���룬0��ʾ��ִ��
1020 ********************************************************/
1021 static s32 __devinit goodix_ts_init(void)
1022 {
1023         int ret; 
1024                 printk("+++++++++++++++++++++++++++++liqing.\n");
1025     goodix_wq = create_workqueue("goodix_wq");        //create a work queue and worker thread
1026     if (!goodix_wq)
1027     {
1028         GTDEBUG_MSG(KERN_ALERT "creat workqueue faiked\n");
1029         return -ENOMEM;
1030     }
1031     ret = i2c_add_driver(&goodix_ts_driver);
1032         if (ret)
1033         {
1034             printk("Register raydium_ts driver failed gt82x.\n");
1035                 return ret;   //w++
1036         }
1037 #ifdef LONGPRESS_LOCK_SPECKEY
1038         if(err_gt82x>=0)   //w++
1039
1040         ret =driver_create_file(&goodix_ts_driver.driver, &driver_attr_get_lock_status);
1041 #endif
1042     return ret;
1043 }
1044
1045 /*******************************************************        
1046 ���ܣ�
1047         ��ж�غ���
1048 ����
1049         client���豸�ṹ��
1050 ********************************************************/
1051 static void __exit goodix_ts_exit(void)
1052 {
1053     GTDEBUG_MSG(KERN_ALERT "Touchscreen driver of guitar exited.\n");
1054     i2c_del_driver(&goodix_ts_driver);
1055     if (goodix_wq)
1056         destroy_workqueue(goodix_wq);        //release our work queue
1057 #ifdef LONGPRESS_LOCK_SPECKEY   //w++
1058         driver_remove_file(&goodix_ts_driver.driver, &driver_attr_get_lock_status);
1059 #endif
1060 }
1061
1062 module_init(goodix_ts_init);                //����ʼ����
1063 module_exit(goodix_ts_exit);
1064
1065 MODULE_DESCRIPTION("Goodix Touchscreen Driver");
1066 MODULE_LICENSE("GPL");
1067