Merge tag 'v4.4-rc6'
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / rockchip_gslX680_rk3168.c
1 /*
2  * drivers/input/touchscreen/gslX680.c
3  *
4  * Copyright (c) 2012 Shanghai Basewin
5  *      Guan Yuwei<guanyuwei@basewin.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  */
11
12
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/earlysuspend.h>
16 #include <linux/hrtimer.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/platform_device.h>
22 #include <linux/async.h>
23 #include <mach/gpio.h>
24 #include <mach/iomux.h>
25 #include <linux/irq.h>
26 #include <mach/board.h>
27 #include <linux/workqueue.h>
28 #include <linux/proc_fs.h>
29 #include <linux/input/mt.h>
30 #include "rockchip_gslX680_rk3168.h"
31
32 //#define GSL_DEBUG
33 //#define GSL_TIMER
34 #define REPORT_DATA_ANDROID_4_0
35
36 //#define HAVE_TOUCH_KEY
37
38 #define GSLX680_I2C_NAME        "gslX680"
39 #define GSLX680_I2C_ADDR        0x40
40
41 #define IRQ_PORT                        RK2928_PIN1_PB0
42 #define WAKE_PORT                       RK2928_PIN0_PD3
43
44 #define GSL_DATA_REG            0x80
45 #define GSL_STATUS_REG          0xe0
46 #define GSL_PAGE_REG            0xf0
47
48 #define PRESS_MAX               255
49 #define MAX_FINGERS             5
50 #define MAX_CONTACTS            10
51 #define DMA_TRANS_LEN           0x20
52 //#define FILTER_POINT
53 #ifdef FILTER_POINT
54 #define FILTER_MAX      6
55 #endif
56
57 #define WRITE_I2C_SPEED 350*1000
58 #define I2C_SPEED  200*1000
59
60 //ÐÝÃßʱÊÇ·ñÐèÒª¹Ø±ÕTPµçÔ´
61 //Ò쳣ʱÊÇ·ñÐèÒª¹Ø±ÕµçÔ´
62 #define CLOSE_TP_POWER   1
63
64 #if CLOSE_TP_POWER
65 static void set_tp_power(bool flag);
66 #endif
67
68 #ifdef HAVE_TOUCH_KEY
69 static u16 key = 0;
70 static int key_state_flag = 0;
71
72
73 struct key_data {
74         u16 key;
75         u16 x_min;
76         u16 x_max;
77         u16 y_min;
78         u16 y_max;      
79 };
80
81 const u16 key_array[]={
82                                       KEY_BACK,
83                                       KEY_HOME,
84                                       KEY_MENU,
85                                       KEY_SEARCH,
86                                      }; 
87 #define MAX_KEY_NUM     (sizeof(key_array)/sizeof(key_array[0]))
88
89 struct key_data gsl_key_data[MAX_KEY_NUM] = {
90         {KEY_BACK, 2048, 2048, 2048, 2048},
91         {KEY_HOME, 2048, 2048, 2048, 2048},     
92         {KEY_MENU, 2048, 2048, 2048, 2048},
93         {KEY_SEARCH, 2048, 2048, 2048, 2048},
94 };
95 #endif
96
97 struct gsl_ts_data {
98         u8 x_index;
99         u8 y_index;
100         u8 z_index;
101         u8 id_index;
102         u8 touch_index;
103         u8 data_reg;
104         u8 status_reg;
105         u8 data_size;
106         u8 touch_bytes;
107         u8 update_data;
108         u8 touch_meta_data;
109         u8 finger_size;
110 };
111
112 static struct gsl_ts_data devices[] = {
113         {
114                 .x_index = 6,
115                 .y_index = 4,
116                 .z_index = 5,
117                 .id_index = 7,
118                 .data_reg = GSL_DATA_REG,
119                 .status_reg = GSL_STATUS_REG,
120                 .update_data = 0x4,
121                 .touch_bytes = 4,
122                 .touch_meta_data = 4,
123                 .finger_size = 70,
124         },
125 };
126
127 struct gsl_ts {
128         struct i2c_client *client;
129         struct input_dev *input;
130         struct work_struct work;
131         struct workqueue_struct *wq;
132         struct gsl_ts_data *dd;
133         u8 *touch_data;
134         u8 device_id;
135 //      uint32_t gpio_irq;
136         int irq;
137 #if defined(CONFIG_HAS_EARLYSUSPEND)
138         struct early_suspend early_suspend;
139 #endif
140 #ifdef GSL_TIMER
141         struct timer_list gsl_timer;
142 #endif
143      int                reset_gpio;   //lizhengwei
144         
145 };
146
147 #ifdef GSL_DEBUG 
148 #define print_info(fmt, args...)   \
149         do{                              \
150                 printk(fmt, ##args);     \
151         }while(0)
152 #else
153 #define print_info(fmt, args...)
154 #endif
155
156 static   int  ts_global_reset_pin;              //lizhengwei add
157
158 static u32 id_sign[MAX_CONTACTS+1] = {0};
159 static u8 id_state_flag[MAX_CONTACTS+1] = {0};
160 static u8 id_state_old_flag[MAX_CONTACTS+1] = {0};
161 static u16 x_old[MAX_CONTACTS+1] = {0};
162 static u16 y_old[MAX_CONTACTS+1] = {0};
163 static u16 x_new = 0;
164 static u16 y_new = 0;
165 #if 0
166 static int gslX680_chip_init(void)
167 {
168     if (WAKE_PORT > 0) {
169         gpio_free(WAKE_PORT);
170         if (gpio_request(WAKE_PORT, "gslx680 wake")) {
171                 printk("pjf gpio_request(WAKE_PORT) error\n");
172             goto exit_alloc_gpio_wake_failed;
173         }
174     }
175     gpio_direction_output(WAKE_PORT, 0);
176     gpio_set_value(WAKE_PORT,GPIO_HIGH);
177
178     if (IRQ_PORT > 0) {
179         gpio_free(IRQ_PORT);
180         if (gpio_request(IRQ_PORT, "gslx680 irq")) {
181                 printk("pjf gpio_request(IRQ_PORT) error\n");
182             goto exit_alloc_gpio_irg_failed;
183         }
184     }
185     gpio_pull_updown(IRQ_PORT, 1);
186
187         msleep(20);
188         return 0;
189
190 exit_alloc_gpio_irg_failed:
191     gpio_free(IRQ_PORT);
192 exit_alloc_gpio_wake_failed:
193     gpio_free(WAKE_PORT);
194         return -EIO;
195 }
196 #endif
197 static int gslX680_shutdown_low(void)
198 {
199         gpio_direction_output(ts_global_reset_pin, GPIO_LOW);
200         gpio_set_value(ts_global_reset_pin,GPIO_LOW);
201         return 0;
202 }
203
204 static int gslX680_shutdown_high(void)
205 {
206         gpio_direction_output(ts_global_reset_pin, GPIO_HIGH);
207         gpio_set_value(ts_global_reset_pin,GPIO_HIGH);
208         return 0;
209 }
210
211 static int gslX680_shutdown_realse(void)
212 {
213         gpio_direction_input(ts_global_reset_pin);
214         return 0;
215 }
216
217 static inline u16 join_bytes(u8 a, u8 b)
218 {
219         u16 ab = 0;
220         ab = ab | a;
221         ab = ab << 8 | b;
222         return ab;
223 }
224
225 static u32 gsl_read_interface(struct i2c_client *client, u8 reg, u8 *buf, u32 num)
226 {
227         struct i2c_msg xfer_msg[2];
228
229         xfer_msg[0].addr = client->addr;
230         xfer_msg[0].len = 1;
231         xfer_msg[0].flags = client->flags & I2C_M_TEN;
232         xfer_msg[0].buf = &reg;
233   xfer_msg[0].scl_rate = I2C_SPEED;
234   
235         xfer_msg[1].addr = client->addr;
236         xfer_msg[1].len = num;
237         xfer_msg[1].flags |= I2C_M_RD;
238         xfer_msg[1].buf = buf;
239   xfer_msg[1].scl_rate = I2C_SPEED;
240   
241         if (reg < 0x80) {
242                 i2c_transfer(client->adapter, xfer_msg, ARRAY_SIZE(xfer_msg));
243                 msleep(5);
244         }
245
246         return i2c_transfer(client->adapter, xfer_msg, ARRAY_SIZE(xfer_msg)) == ARRAY_SIZE(xfer_msg) ? 0 : -EFAULT;
247 }
248
249 static u32 gsl_write_interface(struct i2c_client *client, const u8 reg, u8 *buf, u32 num)
250 {
251         struct i2c_msg xfer_msg[1];
252
253         buf[0] = reg;
254
255         xfer_msg[0].addr = client->addr;
256         xfer_msg[0].len = num + 1;
257         xfer_msg[0].flags = client->flags & I2C_M_TEN;
258         xfer_msg[0].buf = buf;
259         xfer_msg[0].scl_rate = WRITE_I2C_SPEED;
260
261         return i2c_transfer(client->adapter, xfer_msg, 1) == 1 ? 0 : -EFAULT;
262 }
263
264 static int gsl_ts_write(struct i2c_client *client, u8 addr, u8 *pdata, int datalen)
265 {
266         int ret = 0;
267         u8 tmp_buf[128];
268         unsigned int bytelen = 0;
269         if (datalen > 125)
270         {
271                 printk("%s too big datalen = %d!\n", __func__, datalen);
272                 return -1;
273         }
274         
275         tmp_buf[0] = addr;
276         bytelen++;
277         
278         if (datalen != 0 && pdata != NULL)
279         {
280                 memcpy(&tmp_buf[bytelen], pdata, datalen);
281                 bytelen += datalen;
282         }
283         
284         ret = i2c_master_normal_send(client, tmp_buf, bytelen,I2C_SPEED);
285         return ret;
286 }
287
288 static int gsl_ts_read(struct i2c_client *client, u8 addr, u8 *pdata, unsigned int datalen)
289 {
290         int ret = 0;
291
292         if (datalen > 126)
293         {
294                 printk("%s too big datalen = %d!\n", __func__, datalen);
295                 return -1;
296         }
297
298         ret = gsl_ts_write(client, addr, NULL, 0);
299         if (ret < 0)
300         {
301                 printk("%s set data address fail!\n", __func__);
302                 return ret;
303         }
304         
305         return i2c_master_normal_recv(client, pdata, datalen,I2C_SPEED);
306 }
307
308
309 static __inline__ void fw2buf(u8 *buf, const u32 *fw)
310 {
311         u32 *u32_buf = (int *)buf;
312         *u32_buf = *fw;
313 }
314
315 static void gsl_load_fw(struct i2c_client *client)
316 {
317         u8 buf[DMA_TRANS_LEN*4 + 1] = {0};
318         u8 send_flag = 1;
319         u8 *cur = buf + 1;
320         u32 source_line = 0;
321         u32 source_len;
322         u8 read_buf[4] = {0};
323         struct fw_data *ptr_fw;
324         
325         printk("=============gsl_load_fw start==============\n");
326
327 #ifdef GSL1680E_COMPATIBLE
328         msleep(50);
329         gsl_ts_read(client, 0xfc, read_buf, 4);
330         //printk("read 0xfc = %x %x %x %x\n", read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
331
332         if(read_buf[2] != 0x82 && read_buf[2] != 0x88)
333         {
334                 msleep(100);
335                 gsl_ts_read(client, 0xfc, read_buf, 4);
336                 //printk("read 0xfc = %x %x %x %x\n", read_buf[3], read_buf[2], read_buf[1], read_buf[0]);              
337         }
338         
339         if(read_buf[2] == 0x82)
340         {
341                 ptr_fw = GSL1680E_FW;
342                 source_len = ARRAY_SIZE(GSL1680E_FW);   
343         }
344         else
345 #endif
346         {
347                 ptr_fw = GSLX680_FW;
348                 source_len = ARRAY_SIZE(GSLX680_FW);
349         }
350
351         for (source_line = 0; source_line < source_len; source_line++) 
352         {
353                 /* init page trans, set the page val */
354                 if (GSL_PAGE_REG == ptr_fw[source_line].offset)
355                 {
356                         fw2buf(cur, &ptr_fw[source_line].val);
357                         gsl_write_interface(client, GSL_PAGE_REG, buf, 4);
358                         send_flag = 1;
359                 }
360                 else 
361                 {
362                         if (1 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20))
363                                 buf[0] = (u8)ptr_fw[source_line].offset;
364
365                         fw2buf(cur, &ptr_fw[source_line].val);
366                         cur += 4;
367
368                         if (0 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20)) 
369                         {
370                                 gsl_write_interface(client, buf[0], buf, cur - buf - 1);
371                                 cur = buf + 1;
372                         }
373
374                         send_flag++;
375                 }
376         }
377
378         printk("=============gsl_load_fw end==============\n");
379
380 }
381
382
383 static void test_i2c(struct i2c_client *client)
384 {
385         u8 read_buf = 0;
386         u8 write_buf = 0x12;
387         int ret;
388         ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
389         if  (ret  < 0)  
390         {
391                 pr_info("I2C transfer error!\n");
392         }
393         else
394         {
395                 pr_info("I read reg 0xf0 is %x\n", read_buf);
396         }
397         msleep(10);
398
399         ret = gsl_ts_write(client, 0xf0, &write_buf, sizeof(write_buf));
400         if  (ret  < 0)  
401         {
402                 pr_info("I2C transfer error!\n");
403         }
404         else
405         {
406                 pr_info("I write reg 0xf0 0x12\n");
407         }
408         msleep(10);
409
410         ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
411         if  (ret  <  0 )
412         {
413                 pr_info("I2C transfer error!\n");
414         }
415         else
416         {
417                 pr_info("I read reg 0xf0 is 0x%x\n", read_buf);
418         }
419         msleep(10);
420
421 }
422
423
424 static void startup_chip(struct i2c_client *client)
425 {
426         u8 tmp = 0x00;
427         u8 buf[4] = {0x00};
428         buf[3] = 0x01;
429         buf[2] = 0xfe;
430         buf[1] = 0x10;
431         buf[0] = 0x00;  
432         gsl_ts_write(client, 0xf0, buf, sizeof(buf));
433         buf[3] = 0x00;
434         buf[2] = 0x00;
435         buf[1] = 0x00;
436         buf[0] = 0x0f;  
437         gsl_ts_write(client, 0x04, buf, sizeof(buf));
438         msleep(20);     
439         gsl_ts_write(client, 0xe0, &tmp, 1);
440         msleep(10);     
441 }
442
443 static void reset_chip(struct i2c_client *client)
444 {
445         u8 tmp = 0x88;
446         u8 buf[4] = {0x00};
447         
448         gsl_ts_write(client, 0xe0, &tmp, sizeof(tmp));
449         msleep(20);
450         tmp = 0x04;
451         gsl_ts_write(client, 0xe4, &tmp, sizeof(tmp));
452         msleep(10);
453         gsl_ts_write(client, 0xbc, buf, sizeof(buf));
454         msleep(10);
455 }
456
457 static void clr_reg(struct i2c_client *client)
458 {
459         u8 write_buf[4] = {0};
460
461         write_buf[0] = 0x88;
462         gsl_ts_write(client, 0xe0, &write_buf[0], 1);   
463         msleep(20);
464         write_buf[0] = 0x01;
465         gsl_ts_write(client, 0x80, &write_buf[0], 1);   
466         msleep(5);
467         write_buf[0] = 0x04;
468         gsl_ts_write(client, 0xe4, &write_buf[0], 1);   
469         msleep(5);
470         write_buf[0] = 0x00;
471         gsl_ts_write(client, 0xe0, &write_buf[0], 1);   
472         msleep(20);
473 }
474
475 static void init_chip(struct i2c_client *client)
476 {
477         gslX680_shutdown_low(); 
478         msleep(20);     
479         gslX680_shutdown_high();        
480         msleep(20);             
481         clr_reg(client);
482         reset_chip(client);
483         gsl_load_fw(client);                    
484         startup_chip(client);
485         reset_chip(client);
486         startup_chip(client);   
487 }
488
489 static void check_mem_data(struct i2c_client *client)
490 {
491         u8 read_buf[4]  = {0};
492
493         msleep(50);
494         gsl_ts_read(client,0xb0, read_buf, sizeof(read_buf));
495         if (read_buf[3] != 0x5a || read_buf[2] != 0x5a || read_buf[1] != 0x5a || read_buf[0] != 0x5a)
496         {
497          printk("#########check mem read 0xb0 = %x %x %x %x #########\n", read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
498    #if CLOSE_TP_POWER
499           set_tp_power(false);
500           msleep(200);
501           set_tp_power(true);
502           msleep(100);
503    #endif
504          init_chip(client);
505         }
506 }
507
508 #ifdef FILTER_POINT
509 static void filter_point(u16 x, u16 y , u8 id)
510 {
511         u16 x_err =0;
512         u16 y_err =0;
513         u16 filter_step_x = 0, filter_step_y = 0;
514         
515         id_sign[id] = id_sign[id] + 1;
516         if(id_sign[id] == 1)
517         {
518                 x_old[id] = x;
519                 y_old[id] = y;
520         }
521         
522         x_err = x > x_old[id] ? (x -x_old[id]) : (x_old[id] - x);
523         y_err = y > y_old[id] ? (y -y_old[id]) : (y_old[id] - y);
524
525         if( (x_err > FILTER_MAX && y_err > FILTER_MAX/3) || (x_err > FILTER_MAX/3 && y_err > FILTER_MAX) )
526         {
527                 filter_step_x = x_err;
528                 filter_step_y = y_err;
529         }
530         else
531         {
532                 if(x_err > FILTER_MAX)
533                         filter_step_x = x_err; 
534                 if(y_err> FILTER_MAX)
535                         filter_step_y = y_err;
536         }
537
538         if(x_err <= 2*FILTER_MAX && y_err <= 2*FILTER_MAX)
539         {
540                 filter_step_x >>= 2; 
541                 filter_step_y >>= 2;
542         }
543         else if(x_err <= 3*FILTER_MAX && y_err <= 3*FILTER_MAX)
544         {
545                 filter_step_x >>= 1; 
546                 filter_step_y >>= 1;
547         }       
548         else if(x_err <= 4*FILTER_MAX && y_err <= 4*FILTER_MAX)
549         {
550                 filter_step_x = filter_step_x*3/4; 
551                 filter_step_y = filter_step_y*3/4;
552         }       
553         x_new = x > x_old[id] ? (x_old[id] + filter_step_x) : (x_old[id] - filter_step_x);
554         y_new = y > y_old[id] ? (y_old[id] + filter_step_y) : (y_old[id] - filter_step_y);
555
556         x_old[id] = x_new;
557         y_old[id] = y_new;
558 }
559 #else
560
561 static void record_point(u16 x, u16 y , u8 id)
562 {
563         u16 x_err =0;
564         u16 y_err =0;
565
566         id_sign[id]=id_sign[id]+1;
567         
568         if(id_sign[id]==1){
569                 x_old[id]=x;
570                 y_old[id]=y;
571         }
572
573         x = (x_old[id] + x)/2;
574         y = (y_old[id] + y)/2;
575                 
576         if(x>x_old[id]){
577                 x_err=x -x_old[id];
578         }
579         else{
580                 x_err=x_old[id]-x;
581         }
582
583         if(y>y_old[id]){
584                 y_err=y -y_old[id];
585         }
586         else{
587                 y_err=y_old[id]-y;
588         }
589
590         if( (x_err > 3 && y_err > 1) || (x_err > 1 && y_err > 3) ){
591                 x_new = x;     x_old[id] = x;
592                 y_new = y;     y_old[id] = y;
593         }
594         else{
595                 if(x_err > 3){
596                         x_new = x;     x_old[id] = x;
597                 }
598                 else
599                         x_new = x_old[id];
600                 if(y_err> 3){
601                         y_new = y;     y_old[id] = y;
602                 }
603                 else
604                         y_new = y_old[id];
605         }
606
607         if(id_sign[id]==1){
608                 x_new= x_old[id];
609                 y_new= y_old[id];
610         }
611         
612 }
613 #endif
614
615 #ifdef HAVE_TOUCH_KEY
616 static void report_key(struct gsl_ts *ts, u16 x, u16 y)
617 {
618         u16 i = 0;
619
620         for(i = 0; i < MAX_KEY_NUM; i++) 
621         {
622                 if((gsl_key_data[i].x_min < x) && (x < gsl_key_data[i].x_max)&&(gsl_key_data[i].y_min < y) && (y < gsl_key_data[i].y_max))
623                 {
624                         key = gsl_key_data[i].key;      
625                         input_report_key(ts->input, key, 1);
626                         input_sync(ts->input);          
627                         key_state_flag = 1;
628                         break;
629                 }
630         }
631 }
632 #endif
633
634 static void report_data(struct gsl_ts *ts, u16 x, u16 y, u8 pressure, u8 id)
635 {
636         swap(x, y);
637
638         print_info("#####id=%d,x=%d,y=%d######\n",id,x,y);
639
640         if(x > SCREEN_MAX_X || y > SCREEN_MAX_Y)
641         {
642         #ifdef HAVE_TOUCH_KEY
643                 report_key(ts,x,y);
644         #endif
645                 return;
646         }
647         
648 #ifdef REPORT_DATA_ANDROID_4_0
649         input_mt_slot(ts->input, id);           
650         input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
651         input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
652         input_report_abs(ts->input, ABS_MT_POSITION_X, x);
653         input_report_abs(ts->input, ABS_MT_POSITION_Y, y);      
654         input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
655 #else
656         input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
657         input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
658         input_report_abs(ts->input, ABS_MT_POSITION_X,x);
659         input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
660         input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
661         input_mt_sync(ts->input);
662 #endif
663 }
664
665 static void process_gslX680_data(struct gsl_ts *ts)
666 {
667         u8 id, touches;
668         u16 x, y;
669         int i = 0;
670
671         touches = ts->touch_data[ts->dd->touch_index];
672         for(i=1;i<=MAX_CONTACTS;i++)
673         {
674                 if(touches == 0)
675                         id_sign[i] = 0; 
676                 id_state_flag[i] = 0;
677         }
678         for(i= 0;i < (touches > MAX_FINGERS ? MAX_FINGERS : touches);i ++)
679         {
680                 x = join_bytes( ( ts->touch_data[ts->dd->x_index  + 4 * i + 1] & 0xf),
681                                 ts->touch_data[ts->dd->x_index + 4 * i]);
682                 y = join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
683                                 ts->touch_data[ts->dd->y_index + 4 * i ]);
684                 id = ts->touch_data[ts->dd->id_index + 4 * i] >> 4;
685
686                 if(1 <=id && id <= MAX_CONTACTS)
687                 {
688                 #ifdef FILTER_POINT
689                         filter_point(x, y ,id);
690                 #else
691                         record_point(x, y , id);
692                 #endif
693                         report_data(ts, x_new, y_new, 50, id);          
694                         id_state_flag[id] = 1;
695                 }
696         }
697         for(i=1;i<=MAX_CONTACTS;i++)
698         {       
699                 if( (0 == touches) || ((0 != id_state_old_flag[i]) && (0 == id_state_flag[i])) )
700                 {
701                 #ifdef REPORT_DATA_ANDROID_4_0
702                         input_mt_slot(ts->input, i);
703                         input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
704                         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
705                 #endif
706                         id_sign[i]=0;
707                 }
708                 id_state_old_flag[i] = id_state_flag[i];
709         }
710 #ifndef REPORT_DATA_ANDROID_4_0
711         if(0 == touches)
712         {       
713                 input_mt_sync(ts->input);
714         #ifdef HAVE_TOUCH_KEY
715                 if(key_state_flag)
716                 {
717                         input_report_key(ts->input, key, 0);
718                         input_sync(ts->input);
719                         key_state_flag = 0;
720                 }
721         #endif                  
722         }
723 #endif
724         input_sync(ts->input);
725 }
726
727
728 static void gsl_ts_xy_worker(struct work_struct *work)
729 {
730         int rc;
731         u8 read_buf[4] = {0};
732
733         struct gsl_ts *ts = container_of(work, struct gsl_ts,work);
734
735         print_info("---gsl_ts_xy_worker---\n");                          
736         /* read data from DATA_REG */
737         rc = gsl_ts_read(ts->client, 0x80, ts->touch_data, ts->dd->data_size);
738         print_info("---touches: %d ---\n",ts->touch_data[0]);           
739                 
740         if (rc < 0) 
741         {
742                 dev_err(&ts->client->dev, "read failed\n");
743                 goto schedule;
744         }
745
746         if (ts->touch_data[ts->dd->touch_index] == 0xff) {
747                 goto schedule;
748         }
749
750         rc = gsl_ts_read( ts->client, 0xbc, read_buf, sizeof(read_buf));
751         if (rc < 0) 
752         {
753                 dev_err(&ts->client->dev, "read 0xbc failed\n");
754                 goto schedule;
755         }
756         print_info("//////// reg %x : %x %x %x %x\n",0xbc, read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
757                 
758         if (read_buf[3] == 0 && read_buf[2] == 0 && read_buf[1] == 0 && read_buf[0] == 0)
759         {
760                 process_gslX680_data(ts);
761         }
762         else
763         {
764                 reset_chip(ts->client);
765                 startup_chip(ts->client);
766         }
767         
768 schedule:
769         enable_irq(ts->irq);
770                 
771 }
772
773 static irqreturn_t gsl_ts_irq(int irq, void *dev_id)
774 {       
775         struct gsl_ts *ts = dev_id;
776
777         print_info("==========GSLX680 Interrupt============\n");                                 
778
779         disable_irq_nosync(ts->irq);
780
781         if (!work_pending(&ts->work)) 
782         {
783                 queue_work(ts->wq, &ts->work);
784         }
785         
786         return IRQ_HANDLED;
787
788 }
789
790 #ifdef GSL_TIMER
791 static void gsl_timer_handle(unsigned long data)
792 {
793         struct gsl_ts *ts = (struct gsl_ts *)data;
794
795 #ifdef GSL_DEBUG        
796         printk("----------------gsl_timer_handle-----------------\n");  
797 #endif
798
799         disable_irq_nosync(ts->irq);    
800         check_mem_data(ts->client);
801         ts->gsl_timer.expires = jiffies + 3 * HZ;
802         add_timer(&ts->gsl_timer);
803         enable_irq(ts->irq);
804         
805 }
806 #endif
807
808 static int gsl_ts_init_ts(struct i2c_client *client, struct gsl_ts *ts)
809 {
810         struct input_dev *input_device;
811 #ifdef CONFIG_MACH_RK_FAC
812         struct tp_platform_data *pdata = client->dev.platform_data;   
813 #endif
814         int i, rc = 0;
815         
816         printk("[GSLX680] Enter %s\n", __func__);
817
818         
819         ts->dd = &devices[ts->device_id];
820
821         if (ts->device_id == 0) {
822                 ts->dd->data_size = MAX_FINGERS * ts->dd->touch_bytes + ts->dd->touch_meta_data;
823                 ts->dd->touch_index = 0;
824         }
825
826         ts->touch_data = kzalloc(ts->dd->data_size, GFP_KERNEL);
827         if (!ts->touch_data) {
828                 pr_err("%s: Unable to allocate memory\n", __func__);
829                 return -ENOMEM;
830         }
831
832
833         input_device = input_allocate_device();
834         if (!input_device) {
835                 rc = -ENOMEM;
836                 goto error_alloc_dev;
837         }
838
839         ts->input = input_device;
840         input_device->name = GSLX680_I2C_NAME;
841         input_device->id.bustype = BUS_I2C;
842         input_device->dev.parent = &client->dev;
843         input_set_drvdata(input_device, ts);
844
845 #ifdef REPORT_DATA_ANDROID_4_0
846         __set_bit(EV_ABS, input_device->evbit);
847         __set_bit(EV_KEY, input_device->evbit);
848         __set_bit(EV_REP, input_device->evbit);
849         __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
850         input_mt_init_slots(input_device, (MAX_CONTACTS+1));
851 #else
852         input_set_abs_params(input_device,ABS_MT_TRACKING_ID, 0, (MAX_CONTACTS+1), 0, 0);
853         set_bit(EV_ABS, input_device->evbit);
854         set_bit(EV_KEY, input_device->evbit);
855 #endif
856
857 #ifdef HAVE_TOUCH_KEY
858         input_device->evbit[0] = BIT_MASK(EV_KEY);
859         for (i = 0; i < MAX_KEY_NUM; i++)
860                 set_bit(key_array[i], input_device->keybit);
861 #endif
862
863         set_bit(ABS_MT_POSITION_X, input_device->absbit);
864         set_bit(ABS_MT_POSITION_Y, input_device->absbit);
865         set_bit(ABS_MT_TOUCH_MAJOR, input_device->absbit);
866         set_bit(ABS_MT_WIDTH_MAJOR, input_device->absbit);
867 #ifdef CONFIG_MACH_RK_FAC
868         input_set_abs_params(input_device,ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
869         input_set_abs_params(input_device,ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
870 #else
871         input_set_abs_params(input_device,ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
872         input_set_abs_params(input_device,ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
873 #endif
874         input_set_abs_params(input_device,ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
875         input_set_abs_params(input_device,ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
876
877         client->irq = ts->irq; //IRQ_PORT,
878
879         ts->wq = create_singlethread_workqueue("kworkqueue_ts");
880         if (!ts->wq) {
881                 dev_err(&client->dev, "Could not create workqueue\n");
882                 goto error_wq_create;
883         }
884         flush_workqueue(ts->wq);        
885
886         INIT_WORK(&ts->work, gsl_ts_xy_worker);
887         
888         rc = input_register_device(input_device);
889         if (rc)
890                 goto error_unreg_device;
891
892         return 0;
893
894 error_unreg_device:
895         destroy_workqueue(ts->wq);
896 error_wq_create:
897         input_free_device(input_device);
898 error_alloc_dev:
899         kfree(ts->touch_data);
900         return rc;
901 }
902
903 #if CLOSE_TP_POWER
904 #include <linux/regulator/rk29-pwm-regulator.h>
905 #include <linux/mfd/tps65910.h>
906 static void set_tp_power(bool flag)
907 {
908          struct regulator *ldo=NULL;
909          ldo = regulator_get(NULL, "vaux33");
910          
911          if(ldo==NULL){
912                  printk("set_tp_power ldo is null\n");
913                  return;
914          }              
915          if(flag){
916                   regulator_set_voltage(ldo, 3300000, 3300000);
917             regulator_enable(ldo);
918             regulator_put(ldo);
919             
920          }else{
921                   regulator_disable(ldo);
922         regulator_put(ldo);
923          }
924 }
925 #endif
926
927 static int gsl_ts_suspend(struct device *dev)
928 {
929         struct gsl_ts *ts = dev_get_drvdata(dev);
930   printk("I'am in gsl_ts_suspend() start\n");
931   flush_workqueue(ts->wq);
932 #ifdef GSL_TIMER
933         printk( "gsl_ts_suspend () : delete gsl_timer\n");
934         del_timer(&ts->gsl_timer);
935 #endif
936         disable_irq_nosync(ts->irq); 
937         gslX680_shutdown_low();
938         return 0;
939 }
940
941 static int gsl_ts_resume(struct device *dev)
942 {
943         struct gsl_ts *ts = dev_get_drvdata(dev);
944         int i,rc = 0;
945   printk("I'am in gsl_ts_resume() start\n");
946         gslX680_shutdown_high();
947         msleep(20);
948         reset_chip(ts->client);   
949         startup_chip(ts->client);
950         check_mem_data(ts->client);
951         //add
952         msleep(100);
953         reset_chip(ts->client);   
954         startup_chip(ts->client);
955         msleep(100);
956         reset_chip(ts->client);   
957         startup_chip(ts->client);       
958 #ifdef GSL_TIMER
959         printk( "gsl_ts_resume () : add gsl_timer\n");
960         init_timer(&ts->gsl_timer);
961         ts->gsl_timer.expires = jiffies + 3 * HZ;
962         ts->gsl_timer.function = &gsl_timer_handle;
963         ts->gsl_timer.data = (unsigned long)ts;
964         add_timer(&ts->gsl_timer);
965 #endif
966
967   for(i=1;i<=MAX_CONTACTS;i++){ 
968                 if(0!= id_state_old_flag[i])
969                 {
970                 #ifdef REPORT_DATA_ANDROID_4_0
971                         input_mt_slot(ts->input, i);
972                         input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
973                         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
974                         input_sync(ts->input);
975                 #endif
976                         id_sign[i]=0;
977                 }
978                 id_state_old_flag[i] = id_state_flag[i]=0;
979         }
980         enable_irq(ts->irq);
981         return 0;
982
983         
984 }
985
986 #ifdef CONFIG_HAS_EARLYSUSPEND
987 static void gsl_ts_early_suspend(struct early_suspend *h)
988 {
989         struct gsl_ts *ts = container_of(h, struct gsl_ts, early_suspend);
990         gsl_ts_suspend(&ts->client->dev);
991 }
992
993 static void gsl_ts_late_resume(struct early_suspend *h)
994 {
995         struct gsl_ts *ts = container_of(h, struct gsl_ts, early_suspend);
996         gsl_ts_resume(&ts->client->dev);
997 }
998 #endif
999
1000 static int __devinit gsl_ts_probe(struct i2c_client *client,
1001                         const struct i2c_device_id *id)
1002 {
1003         #ifdef CONFIG_MACH_RK_FAC
1004         struct tp_platform_data *pdata = client->dev.platform_data;  
1005         #else 
1006         struct ts_hw_data *pdata = client->dev.platform_data;  
1007         #endif  
1008         struct gsl_ts *ts;
1009         int rc;
1010
1011         printk("GSLX680 Enter %s\n", __func__);
1012         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1013                 dev_err(&client->dev, "I2C functionality not supported\n");
1014                 return -ENODEV;
1015         }
1016  
1017         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1018         if (!ts)
1019                 return -ENOMEM;
1020         printk("==kzalloc success=\n");
1021
1022         ts->client = client;
1023         ts->device_id = id->driver_data;
1024
1025 #ifdef CONFIG_MACH_RK_FAC
1026         ts->reset_gpio = pdata->reset_pin;   //lizhengwei
1027         ts->irq= gpio_to_irq(pdata->irq_pin);        //lizhengwei  
1028 #else
1029         ts->reset_gpio = pdata->reset_gpio;   //lizhengwei
1030         ts->irq= gpio_to_irq(pdata->touch_en_gpio);        //lizhengwei  
1031         
1032 #endif
1033   ts_global_reset_pin=ts->reset_gpio;
1034
1035         i2c_set_clientdata(client, ts);
1036         
1037         if (pdata->init_platform_hw)                              
1038                 pdata->init_platform_hw();
1039
1040         rc = gsl_ts_init_ts(client, ts);
1041         if (rc < 0) {
1042                 dev_err(&client->dev, "GSLX680 init failed\n");
1043                 goto error_mutex_destroy;
1044         }       
1045         
1046         init_chip(ts->client);
1047         check_mem_data(ts->client);
1048         
1049         rc=  request_irq(client->irq, gsl_ts_irq, IRQF_TRIGGER_RISING, client->name, ts);
1050         if (rc < 0) {
1051                 printk( "gsl_probe: request irq failed\n");
1052                 goto error_req_irq_fail;
1053         }
1054
1055   //lizhengwei add
1056   if(1)
1057   {
1058   u8 read_buf = 0;
1059         int ret;
1060         ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
1061         if  (ret  < 0)  
1062          {
1063                 pr_info("gslx680  I2C transfer error!\n");
1064                 goto error_req_irq_fail;
1065          }
1066   }
1067         
1068         
1069
1070 #ifdef GSL_TIMER
1071         printk( "gsl_ts_probe () : add gsl_timer\n");
1072
1073         init_timer(&ts->gsl_timer);
1074         ts->gsl_timer.expires = jiffies + 3 * HZ;       //¶¨Ê±3  ÃëÖÓ
1075         ts->gsl_timer.function = &gsl_timer_handle;
1076         ts->gsl_timer.data = (unsigned long)ts;
1077         add_timer(&ts->gsl_timer);
1078 #endif
1079
1080         /* create debug attribute */
1081         //rc = device_create_file(&ts->input->dev, &dev_attr_debug_enable);
1082
1083 #ifdef CONFIG_HAS_EARLYSUSPEND
1084         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1085         ts->early_suspend.suspend = gsl_ts_early_suspend;
1086         ts->early_suspend.resume = gsl_ts_late_resume;
1087         register_early_suspend(&ts->early_suspend);
1088 #endif
1089
1090         printk("[GSLX680] End %s\n", __func__);
1091
1092         return 0;
1093
1094 //exit_set_irq_mode:    
1095 error_req_irq_fail:
1096     free_irq(ts->irq, ts);      
1097
1098 error_mutex_destroy:
1099         input_free_device(ts->input);
1100         kfree(ts);
1101         return rc;
1102 }
1103
1104 static int __devexit gsl_ts_remove(struct i2c_client *client)
1105 {
1106         struct gsl_ts *ts = i2c_get_clientdata(client);
1107         printk("==gsl_ts_remove=\n");
1108
1109 #ifdef CONFIG_HAS_EARLYSUSPEND
1110         unregister_early_suspend(&ts->early_suspend);
1111 #endif
1112
1113         device_init_wakeup(&client->dev, 0);
1114         cancel_work_sync(&ts->work);
1115         free_irq(ts->irq, ts);
1116         destroy_workqueue(ts->wq);
1117         input_unregister_device(ts->input);
1118
1119         //device_remove_file(&ts->input->dev, &dev_attr_debug_enable);
1120         
1121         kfree(ts->touch_data);
1122         kfree(ts);
1123
1124         return 0;
1125 }
1126
1127 static const struct i2c_device_id gsl_ts_id[] = {
1128         {GSLX680_I2C_NAME, 0},
1129         {}
1130 };
1131 MODULE_DEVICE_TABLE(i2c, gsl_ts_id);
1132
1133
1134 static struct i2c_driver gsl_ts_driver = {
1135         .driver = {
1136                 .name = GSLX680_I2C_NAME,
1137                 .owner = THIS_MODULE,
1138         },
1139 #ifndef CONFIG_HAS_EARLYSUSPEND
1140         .suspend        = gsl_ts_suspend,
1141         .resume = gsl_ts_resume,
1142 #endif
1143         .probe          = gsl_ts_probe,
1144         .remove         = __devexit_p(gsl_ts_remove),
1145         .id_table       = gsl_ts_id,
1146 };
1147
1148 static int __init gsl_ts_init(void)
1149 {
1150     int ret;
1151         printk("==gsl_ts_init==\n");
1152         ret = i2c_add_driver(&gsl_ts_driver);
1153         printk("ret=%d\n",ret);
1154         return ret;
1155 }
1156 static void __exit gsl_ts_exit(void)
1157 {
1158         printk("==gsl_ts_exit==\n");
1159         i2c_del_driver(&gsl_ts_driver);
1160         return;
1161 }
1162
1163 module_init(gsl_ts_init);
1164 module_exit(gsl_ts_exit);
1165
1166 MODULE_LICENSE("GPL");
1167 MODULE_DESCRIPTION("GSLX680 touchscreen controller driver");
1168 MODULE_AUTHOR("Guan Yuwei, guanyuwei@basewin.com");
1169 MODULE_ALIAS("platform:gsl_ts");