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