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