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