rk29:touch screen -> modify gt818's INT port and SHUTDOWN port control when resume...
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / gt818_ts.c
1 /* drivers/input/touchscreen/gt818_ts.c
2  *
3  * Copyright (C) 2011 Rockcip, Inc.
4  * 
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * Author: hhb@rock-chips.com
15  * Date: 2011.06.20
16  */
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/time.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/earlysuspend.h>
23 #include <linux/hrtimer.h>
24 #include <linux/i2c.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/platform_device.h>
29
30 #include <linux/gpio.h>
31 #include <mach/iomux.h>
32
33 #include <linux/irq.h>
34 #include <linux/syscalls.h>
35 #include <linux/reboot.h>
36 #include <linux/proc_fs.h>
37
38 #include <linux/vmalloc.h>
39 #include <linux/fs.h>
40 #include <linux/string.h>
41 #include <linux/completion.h>
42 #include <asm/uaccess.h>
43
44 #include "gt818_ts.h"
45
46
47
48 #if !defined(GT801_PLUS) && !defined(GT801_NUVOTON)
49 #error The code does not match this touchscreen.
50 #endif
51
52 static struct workqueue_struct *goodix_wq;
53
54 static const char *gt818_ts_name = "Goodix Capacitive TouchScreen";
55
56 static struct point_queue finger_list;
57
58 struct i2c_client * i2c_connect_client = NULL;
59
60 //EXPORT_SYMBOL(i2c_connect_client);
61
62 static struct proc_dir_entry *goodix_proc_entry;
63         
64 #ifdef CONFIG_HAS_EARLYSUSPEND
65 static void goodix_ts_early_suspend(struct early_suspend *h);
66 static void goodix_ts_late_resume(struct early_suspend *h);
67 #endif
68
69 #ifdef HAVE_TOUCH_KEY
70         const uint16_t gt818_key_array[]={
71                                                                           KEY_MENU,
72                                                                           KEY_HOME,
73                                                                           KEY_BACK,
74                                                                           KEY_SEARCH
75                                                                          };
76         #define MAX_KEY_NUM      (sizeof(gt818_key_array)/sizeof(gt818_key_array[0]))
77 #endif
78
79
80 /*Function as i2c_master_send */
81 static int i2c_read_bytes(struct i2c_client *client, u8 *buf, int len)
82 {
83         struct i2c_msg msgs[2];
84         int ret = -1;
85
86         msgs[0].addr = client->addr;
87         msgs[0].flags = client->flags;
88         msgs[0].len = 2;
89         msgs[0].buf = &buf[0];
90         msgs[0].scl_rate = GT818_I2C_SCL;
91         msgs[0].udelay = client->udelay;
92
93         msgs[1].addr = client->addr;
94         msgs[1].flags = client->flags | I2C_M_RD;
95         msgs[1].len = len-2;
96         msgs[1].buf = &buf[2];
97         msgs[1].scl_rate = GT818_I2C_SCL;
98         msgs[1].udelay = client->udelay;
99
100         ret = i2c_transfer(client->adapter, msgs, 2);
101         if(ret < 0)
102                 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
103         return ret;
104 }
105
106 /*Function as i2c_master_send */
107 static int i2c_write_bytes(struct i2c_client *client,u8 *data,int len)
108 {
109         struct i2c_msg msg;
110         int ret = -1;
111         //鍙戦�璁惧鍦板潃
112         msg.addr = client->addr;
113         msg.flags = client->flags;   //鍐欐秷鎭�
114         msg.len = len;
115         msg.buf = data;
116         msg.scl_rate = GT818_I2C_SCL;
117         msg.udelay = client->udelay;
118         ret = i2c_transfer(client->adapter, &msg, 1);
119         if(ret < 0)
120                 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
121         return ret;
122 }
123
124
125 static int i2c_pre_cmd(struct gt818_ts_data *ts)
126 {
127         int ret;
128         u8 pre_cmd_data[2] = {0};
129         pre_cmd_data[0] = 0x0f;
130         pre_cmd_data[1] = 0xff;
131         ret = i2c_write_bytes(ts->client,pre_cmd_data,2);
132         udelay(20);
133         return ret;
134 }
135
136
137 static int i2c_end_cmd(struct gt818_ts_data *ts)
138 {
139         int ret;
140         u8 end_cmd_data[2] = {0};
141         end_cmd_data[0] = 0x80;
142         end_cmd_data[1] = 0x00;
143         ret = i2c_write_bytes(ts->client,end_cmd_data,2);
144         //msleep(2);
145         return ret;
146 }
147
148
149
150 static int goodix_init_panel(struct gt818_ts_data *ts)
151 {
152         int ret = -1;
153
154         #if 1
155         u8 config_info[] = {
156         0x06,0xA2,
157         0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
158         0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
159         0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
160         0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
161         0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
162         0x00,0x00,0x01,0x13,0x90,0x90,0x90,0x38,
163         0x38,0x38,0x0F,0x0E,0x0A,0x40   ,0x30,0x00,
164         0x3f,00,MAX_FINGER_NUM,0x00,0x14,0x00,0x1C,0x01,
165         0x01,0x3E,0x35,0x68,0x58,0x00,0x00,0x06,
166         0x19,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
167         0x14,0x10,0x51,0x02,0x00,0x00,0x00,0x00,
168         0x00,0x00,0x20,0x40,0x60,0x90,0x08,0x40,
169         0x30,0x32,0x20,0x00,0x00,0x00,0x00,0x00,
170         0x00,0x01
171         };
172         #endif
173                 
174         #if 0
175         u8 config_info[] = {
176         0x06,0xA2,
177         0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
178         0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
179         0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
180         0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
181         0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
182         0x00,0x00,0x11,0x13,0x80,0x80,0x80,0x12,
183         0x12,0x12,0x0F,0x0F,0x0A,0x50,0x3C,0x49,
184         0x03,0x00,0x05,0x00,0x14,0xFA,0x1B,0x00,
185         0x00,0x4D,0x3D,0x81,0x65,0x00,0x00,0x06,
186         0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
187         0x14,0x10,0xAB,0x02,0x00,0x00,0x00,0x00,
188         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,
189         0x00,0x3C,0x28,0x00,0x00,0x00,0x00,0x00,
190         0x00,0x01
191
192         };                                                              
193         #endif
194         ret = i2c_write_bytes(ts->client, config_info, (sizeof(config_info)/sizeof(config_info[0])));
195         if (ret < 0) 
196                 return ret;
197         msleep(10);
198         return 0;
199
200 }
201
202
203
204 static int  goodix_read_version(struct gt818_ts_data *ts)
205 {
206         int ret;
207         u8 version_data[5] = {0};       //store touchscreen version infomation
208         memset(version_data, 0, 5);
209         version_data[0] = 0x07;
210         version_data[1] = 0x17;
211         msleep(2);
212         ret = i2c_read_bytes(ts->client, version_data, 4);
213         if (ret < 0) 
214                 return ret;
215         dev_info(&ts->client->dev," Guitar Version: %d.%d\n",version_data[3],version_data[2]);
216         return 0;
217         
218 }
219
220
221
222 static void goodix_ts_work_func(struct work_struct *work)
223 {       
224         u8  touch_status[8*MAX_FINGER_NUM + 18] = {READ_TOUCH_ADDR_H, READ_TOUCH_ADDR_L, 0};
225         u8  *key_value = NULL;
226         u8  *point_data = NULL;
227         static u8 finger_last[MAX_FINGER_NUM + 1]={0};
228         u8  finger_current[MAX_FINGER_NUM + 1] = {0};
229         u8  coor_data[6*MAX_FINGER_NUM] = {0};
230         static u8  last_key = 0;
231
232         u8  finger = 0;
233         u8  key = 0;
234         u8 retry = 0;
235         unsigned int  count = 0;
236         unsigned int position = 0;      
237         int ret = -1;
238         int temp = 0;
239         int x = 0, y = 0 , pressure;
240
241         u16 *coor_point;
242
243         int syn_flag = 0;
244
245         struct gt818_ts_data *ts = container_of(work, struct gt818_ts_data, work);
246
247         ret = i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
248
249         if(ret <= 0) {
250                 for(retry = 0; retry < 3; retry++)
251                 {
252                         ret = i2c_pre_cmd(ts);
253                         if(ret <= 0)
254                                 continue;
255                         else
256                                 break;
257                 }
258                 if(ret <= 0) {
259                         dev_err(&(ts->client->dev),"I2C transfer error. Number:%d\n ", ret);
260                         ts->bad_data = 1;
261                         ts->retry++;
262                         goto XFER_ERROR;
263                 }
264                 else{
265                         i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
266                 }
267         }
268         //judge whether the data is ready
269         if((touch_status[2] & 0x30) != 0x20)
270         {
271                 printk("%s:DATA_NO_READY\n", __func__);
272                 goto DATA_NO_READY;
273         }
274         //judge whether it is large area touch
275         if(touch_status[13] & 0x0f)
276         {
277                 goto DATA_NO_READY;
278         }
279
280         ts->bad_data = 0;
281         finger = touch_status[2] & 0x07;
282         key_value = touch_status + 15;
283         key = key_value[2] & 0x0f;
284
285         if(finger > 0)
286         {
287                 point_data = key_value + 3;
288
289                 for(position = 0; position < (finger*8); position += 8)
290                 {
291                         temp = point_data[position];
292                         //printk("track:%d\n", temp);
293                         if(temp < (MAX_FINGER_NUM + 1))
294                         {
295                                 finger_current[temp] = 1;
296                                 for(count = 0; count < 6; count++)
297                                 {
298                                         coor_data[(temp - 1) * 6 + count] = point_data[position+1+count];
299                                 }
300                         }
301                         else
302                         {
303                                 //dev_err(&(ts->client->dev),"Track Id error:%d\n ",);
304                                 ts->bad_data = 1;
305                                 ts->retry++;
306                                 goto XFER_ERROR;
307                         }               
308                 }
309         
310         }
311         
312         else
313         {
314                 for(position = 1; position < MAX_FINGER_NUM+1; position++)
315                 {
316                         finger_current[position] = 0;
317                 }
318         }
319
320         coor_point = (u16 *)coor_data;
321
322         for(position = 1; position < MAX_FINGER_NUM + 1; position++)
323         {
324                 //printk("%s:positon:%d\n", __func__, position);
325                 if((finger_current[position] == 0) && (finger_last[position] != 0))
326                 {
327                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, 0);
328                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, 0);
329                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
330                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
331                         input_mt_sync(ts->input_dev);
332                         syn_flag = 1;
333                 }
334                 else if(finger_current[position])
335                 {
336
337                         x = (*(coor_point+3*(position-1)))*SCREEN_MAX_WIDTH/(TOUCH_MAX_WIDTH);
338                         y = (*(coor_point+3*(position-1)+1))*SCREEN_MAX_HEIGHT/(TOUCH_MAX_HEIGHT);
339                         pressure = (*(coor_point+3*(position-1)+2));
340                         if(x < SCREEN_MAX_WIDTH){
341                                 x = SCREEN_MAX_WIDTH - x;
342                         }
343
344                         if(y < SCREEN_MAX_HEIGHT){
345                         //      y = SCREEN_MAX_HEIGHT-y;
346                         }
347                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, position - 1);
348                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
349                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
350                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
351                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure);
352                         input_mt_sync(ts->input_dev);
353                         syn_flag = 1;
354                 }
355         }
356
357
358 #ifdef HAVE_TOUCH_KEY
359         if((last_key == 0) && (key == 0))
360                 goto NO_KEY_PRESS;
361         else
362         {
363                 syn_flag = 1;
364                 switch(key){
365                         case 1:
366                                 key = 4;
367                                 break;
368                         case 2:
369                                 key = 3;
370                                 break;
371                         case 4:
372                                 key = 2;
373                                 break;
374                         case 8:
375                                 key = 1;
376                                 break;
377                         default:
378                                 key = 0;
379                                 break;
380                 }
381                 if(key != 0){
382                         input_report_key(ts->input_dev, gt818_key_array[key - 1], 1);
383                 }
384                 else{
385                         input_report_key(ts->input_dev, gt818_key_array[last_key - 1], 0);
386                 }
387                 last_key = key;
388         }               
389
390 #endif
391
392
393 NO_KEY_PRESS:
394         if(syn_flag){
395                 input_sync(ts->input_dev);
396         }
397
398         for(position = 1; position < MAX_FINGER_NUM + 1; position++)
399         {
400                 finger_last[position] = finger_current[position];
401         }
402
403 DATA_NO_READY:
404 XFER_ERROR:
405 //      i2c_end_cmd(ts);
406         if(ts->use_irq)
407                 enable_irq(ts->client->irq);
408
409 }
410
411
412 static enum hrtimer_restart goodix_ts_timer_func(struct hrtimer *timer)
413 {
414         struct gt818_ts_data *ts = container_of(timer, struct gt818_ts_data, timer);
415         queue_work(goodix_wq, &ts->work);
416         hrtimer_start(&ts->timer, ktime_set(0, (POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
417         return HRTIMER_NORESTART;
418 }
419
420
421 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
422 {
423         struct gt818_ts_data *ts = dev_id;
424         disable_irq_nosync(ts->client->irq);
425         queue_work(goodix_wq, &ts->work);
426         return IRQ_HANDLED;
427 }
428
429 static int goodix_ts_power(struct gt818_ts_data * ts, int on)
430 {
431         int ret = -1;
432         struct gt818_platform_data      *pdata = ts->client->dev.platform_data;
433         unsigned char i2c_control_buf[3] = {0x06,0x92,0x01};            //suspend cmd
434         if(ts != NULL && !ts->use_irq)
435                 return -2;
436         switch(on)
437         {
438                 case 0:
439                         i2c_pre_cmd(ts);
440                         // set the io port high level to avoid level change which might stop gt818 from sleeping
441                         gpio_direction_output(pdata->gpio_reset, 1);
442                         gpio_direction_output(pdata->gpio_pendown, 1);
443                         msleep(5);
444                         ret = i2c_write_bytes(ts->client, i2c_control_buf, 3);
445                         if(ret < 0)
446                         {
447                                 printk(KERN_INFO"**gt818 suspend fail**\n");
448                         }
449                         else
450                         {
451                                 printk(KERN_INFO"**gt818 suspend**\n");
452                                 ret = 0;
453                         }
454 //                      i2c_end_cmd(ts);
455                         return ret;
456                         
457                 case 1:
458
459                         gpio_pull_updown(pdata->gpio_pendown, 1);
460                         gpio_direction_output(pdata->gpio_pendown, 0);
461                         msleep(1);
462                         gpio_direction_output(pdata->gpio_pendown, 1);
463                         msleep(1);
464                         gpio_direction_input(pdata->gpio_pendown);
465                         gpio_pull_updown(pdata->gpio_pendown, 0);
466                         msleep(2);
467                         gpio_direction_output(pdata->gpio_reset, 0);
468                         msleep(2);
469                         gpio_direction_input(pdata->gpio_reset);
470                         gpio_pull_updown(pdata->gpio_reset, 0);
471                         msleep(30);
472
473                         ret = i2c_pre_cmd(ts);
474                         if(ret >0){
475                                 printk(KERN_INFO"**gt818 resume**\n");
476                         }
477                         else{
478                                 printk(KERN_INFO"**gt818 resume fail**\n");
479                         }
480
481                         return ret;
482                                 
483                 default:
484                         printk(KERN_DEBUG "%s: Cant't support this command.", gt818_ts_name);
485                         return -EINVAL;
486         }
487
488 }
489
490
491 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
492 {
493         int ret = 0;
494         int retry=0;
495         u8 goodix_id[3] = {0,0xff,0};
496         struct gt818_ts_data *ts;
497
498         struct gt818_platform_data *pdata;
499         dev_dbg(&client->dev,"Install touch driver.\n");
500         printk("gt818: Install touch driver.\n");
501         //Check I2C function
502         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 
503         {
504                 dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
505                 ret = -ENODEV;
506                 goto err_check_functionality_failed;
507         }
508
509         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
510         if (ts == NULL) {
511                 ret = -ENOMEM;
512                 goto err_alloc_data_failed;
513         }
514
515         i2c_connect_client = client;    //used by Guitar_Update
516         pdata = client->dev.platform_data;
517         ts->client = client;
518         i2c_set_clientdata(client, ts);
519
520         //init int and reset ports
521         ret = gpio_request(pdata->gpio_pendown, "TS_INT");      //Request IO
522         if (ret){
523                 dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(int)pdata->gpio_pendown, ret);
524                 goto err_gpio_request_failed;
525         }
526         rk29_mux_api_set(pdata->pendown_iomux_name, pdata->pendown_iomux_mode);
527         gpio_direction_input(pdata->gpio_pendown);
528         gpio_pull_updown(pdata->gpio_pendown, 0);
529
530         ret = gpio_request(pdata->gpio_reset, "gt818_resetPin");
531         if(ret){
532                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n", pdata->gpio_reset);
533                 goto err_gpio_request_failed;
534         }
535         rk29_mux_api_set(pdata->resetpin_iomux_name, pdata->resetpin_iomux_mode);
536 #if 1
537         for(retry = 0; retry < 4; retry++)
538         {
539                 gpio_pull_updown(pdata->gpio_reset, 1);
540                 gpio_direction_output(pdata->gpio_reset, 0);
541                 msleep(2);
542                 gpio_direction_input(pdata->gpio_reset);
543                 gpio_pull_updown(pdata->gpio_reset, 0);
544                 msleep(30);
545                 ret = i2c_pre_cmd(ts);
546                 if (ret > 0)
547                         break;
548                 msleep(50);
549         }
550
551         if(ret <= 0)
552         {
553                 dev_err(&client->dev, "Warnning: I2C communication might be ERROR!\n");
554                 goto err_i2c_failed;
555         }       
556
557 #endif
558         for(retry = 0; retry < 3; retry++)
559         {
560                 ret = goodix_init_panel(ts);
561                 dev_info(&client->dev,"the config ret is :%d\n", ret);
562                 msleep(20);
563                 if(ret < 0)     //Initiall failed
564                         continue;
565                 else
566                         break;
567         }
568
569         if(ret < 0) {
570                 ts->bad_data = 1;
571                 goto err_init_godix_ts;
572         }
573         goodix_read_version(ts);
574 //      i2c_end_cmd(ts);
575         INIT_WORK(&ts->work, goodix_ts_work_func);              //init work_struct
576         ts->input_dev = input_allocate_device();
577         if (ts->input_dev == NULL) {
578                 ret = -ENOMEM;
579                 dev_dbg(&client->dev,"goodix_ts_probe: Failed to allocate input device\n");
580                 goto err_input_dev_alloc_failed;
581         }
582
583         ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
584         ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
585         ts->input_dev->absbit[0] = BIT_MASK(ABS_MT_POSITION_X) | BIT_MASK(ABS_MT_POSITION_Y) |
586                         BIT_MASK(ABS_MT_TOUCH_MAJOR) | BIT_MASK(ABS_MT_WIDTH_MAJOR);  // for android
587
588
589 #ifdef HAVE_TOUCH_KEY
590         for(retry = 0; retry < MAX_KEY_NUM; retry++)
591         {
592                 input_set_capability(ts->input_dev, EV_KEY, gt818_key_array[retry]);
593         }
594 #endif
595
596         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
597         snprintf(ts->name, sizeof(ts->name), "gt818-touchscreen");
598
599         ts->input_dev->name = "gt818_ts";//ts->name;
600         ts->input_dev->phys = ts->phys;
601         ts->input_dev->dev.parent = &client->dev;
602         ts->input_dev->id.bustype = BUS_I2C;
603         ts->input_dev->id.vendor = 0xDEAD;
604         ts->input_dev->id.product = 0xBEEF;
605         ts->input_dev->id.version = 10427;      //screen firmware version
606
607 #ifdef GOODIX_MULTI_TOUCH
608
609         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
610         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
611         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_WIDTH, 0, 0);
612         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_HEIGHT, 0, 0);
613         input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, MAX_FINGER_NUM, 0, 0);
614 #else
615         input_set_abs_params(ts->input_dev, ABS_X, 0, SCREEN_MAX_HEIGHT, 0, 0);
616         input_set_abs_params(ts->input_dev, ABS_Y, 0, SCREEN_MAX_WIDTH, 0, 0);
617         input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, 0, 0);
618 #endif  
619         
620         ret = input_register_device(ts->input_dev);
621         if (ret) {
622                 dev_err(&client->dev,"Probe: Unable to register %s input device\n", ts->input_dev->name);
623                 goto err_input_register_device_failed;
624         }
625         ts->bad_data = 0;
626 //      finger_list.length = 0;
627
628         client->irq = gpio_to_irq(pdata->gpio_pendown);         //If not defined in client
629         if (client->irq)
630         {
631
632         #if INT_TRIGGER==0
633                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_RISING
634         #elif INT_TRIGGER==1
635                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_FALLING
636         #elif INT_TRIGGER==2
637                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_LOW
638         #elif INT_TRIGGER==3
639                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_HIGH
640         #endif
641
642                 ret = request_irq(client->irq, goodix_ts_irq_handler, GT801_PLUS_IRQ_TYPE,
643                         client->name, ts);
644                 if (ret != 0) {
645                         dev_err(&client->dev,"Cannot allocate ts INT!ERRNO:%d\n", ret);
646                         gpio_direction_input(pdata->gpio_pendown);
647                         gpio_free(pdata->gpio_pendown);
648                         goto err_gpio_request_failed;
649                 }
650                 else 
651                 {       
652                         disable_irq(client->irq);
653                         ts->use_irq = 1;
654                         dev_dbg(&client->dev,"Reques EIRQ %d succesd on GPIO:%d\n", client->irq, pdata->gpio_pendown);
655                 }       
656         }
657
658 err_gpio_request_failed:
659         ts->power = goodix_ts_power;
660 #ifdef CONFIG_HAS_EARLYSUSPEND
661         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
662         ts->early_suspend.suspend = goodix_ts_early_suspend;
663         ts->early_suspend.resume = goodix_ts_late_resume;
664         register_early_suspend(&ts->early_suspend);
665 #endif
666         dev_info(&client->dev,"Start %s in %s mode\n", 
667                 ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
668
669         if (ts->use_irq)
670         {
671                 enable_irq(client->irq);
672         }
673         else
674         {
675                 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
676                 ts->timer.function = goodix_ts_timer_func;
677                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
678         }
679         return 0;
680
681 err_init_godix_ts:
682 //      i2c_end_cmd(ts);
683         if(ts->use_irq)
684         {
685                 ts->use_irq = 0;
686                 free_irq(client->irq,ts);
687                 gpio_direction_input(pdata->gpio_pendown);
688                 gpio_free(pdata->gpio_pendown);
689         }
690         else 
691                 hrtimer_cancel(&ts->timer);
692
693 err_input_register_device_failed:
694         input_free_device(ts->input_dev);
695
696 err_input_dev_alloc_failed:
697         i2c_set_clientdata(client, NULL);
698 err_i2c_failed: 
699         kfree(ts);
700 err_alloc_data_failed:
701 err_check_functionality_failed:
702 err_create_proc_entry:
703         return ret;
704 }
705
706
707 static int goodix_ts_remove(struct i2c_client *client)
708 {
709         struct gt818_ts_data *ts = i2c_get_clientdata(client);
710         struct gt818_platform_data      *pdata = client->dev.platform_data;
711
712 #ifdef CONFIG_HAS_EARLYSUSPEND
713         unregister_early_suspend(&ts->early_suspend);
714 #endif
715 #ifdef CONFIG_TOUCHSCREEN_GOODIX_IAP
716         remove_proc_entry("goodix-update", NULL);
717 #endif
718         if (ts && ts->use_irq) 
719         {
720                 gpio_direction_input(pdata->gpio_pendown);
721                 gpio_free(pdata->gpio_pendown);
722                 free_irq(client->irq, ts);
723         }       
724         else if(ts)
725                 hrtimer_cancel(&ts->timer);
726         
727         dev_notice(&client->dev,"The driver is removing...\n");
728         i2c_set_clientdata(client, NULL);
729         input_unregister_device(ts->input_dev);
730         kfree(ts);
731         return 0;
732 }
733
734
735 static int goodix_ts_suspend(struct i2c_client *client, pm_message_t mesg)
736 {
737         int ret;
738         struct gt818_ts_data *ts = i2c_get_clientdata(client);
739
740         if (ts->use_irq)
741                 disable_irq(client->irq);
742         else
743                 hrtimer_cancel(&ts->timer);
744         //ret = cancel_work_sync(&ts->work);
745         //if(ret && ts->use_irq)        
746                 //enable_irq(client->irq);
747         if (ts->power) {
748                 ret = ts->power(ts, 0);
749                 if (ret < 0)
750                         printk(KERN_ERR "goodix_ts_resume power off failed\n");
751         }
752         return 0;
753 }
754
755
756 static int goodix_ts_resume(struct i2c_client *client)
757 {
758         int ret;
759         struct gt818_ts_data *ts = i2c_get_clientdata(client);
760
761         if (ts->power) {
762                 ret = ts->power(ts, 1);
763                 if (ret < 0)
764                         printk(KERN_ERR "goodix_ts_resume power on failed\n");
765         }
766
767         if (ts->use_irq)
768                 enable_irq(client->irq);
769         else
770                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
771
772         return 0;
773 }
774
775 #ifdef CONFIG_HAS_EARLYSUSPEND
776 static void goodix_ts_early_suspend(struct early_suspend *h)
777 {
778         struct gt818_ts_data *ts;
779         ts = container_of(h, struct gt818_ts_data, early_suspend);
780         goodix_ts_suspend(ts->client, PMSG_SUSPEND);
781 }
782
783 static void goodix_ts_late_resume(struct early_suspend *h)
784 {
785         struct gt818_ts_data *ts;
786         ts = container_of(h, struct gt818_ts_data, early_suspend);
787         goodix_ts_resume(ts->client);
788 }
789 #endif
790
791
792 //only one client
793 static const struct i2c_device_id goodix_ts_id[] = {
794         { GOODIX_I2C_NAME, 0 },
795         { }
796 };
797
798
799 static struct i2c_driver goodix_ts_driver = {
800         .probe          = goodix_ts_probe,
801         .remove         = goodix_ts_remove,
802 #ifndef CONFIG_HAS_EARLYSUSPEND
803         .suspend        = goodix_ts_suspend,
804         .resume         = goodix_ts_resume,
805 #endif
806         .id_table       = goodix_ts_id,
807         .driver = {
808                 .name   = GOODIX_I2C_NAME,
809                 .owner = THIS_MODULE,
810         },
811 };
812
813
814 static int __devinit goodix_ts_init(void)
815 {
816         int ret;
817         goodix_wq = create_singlethread_workqueue("goodix_wq");         //create a work queue and worker thread
818         if (!goodix_wq) {
819                 printk(KERN_ALERT "creat workqueue faiked\n");
820                 return -ENOMEM;
821         }
822         ret = i2c_add_driver(&goodix_ts_driver);
823         return ret; 
824 }
825
826
827 static void __exit goodix_ts_exit(void)
828 {
829         printk(KERN_ALERT "Touchscreen driver of guitar exited.\n");
830         i2c_del_driver(&goodix_ts_driver);
831         if (goodix_wq)
832                 destroy_workqueue(goodix_wq);           //release our work queue
833 }
834
835 late_initcall(goodix_ts_init);
836 module_exit(goodix_ts_exit);
837
838 MODULE_DESCRIPTION("Goodix Touchscreen Driver");
839 MODULE_AUTHOR("hhb@rock-chips.com")
840 MODULE_LICENSE("GPL");
841