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