Merge remote-tracking branch 'linux-2.6.32.y/master' into develop
[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 //      unsigned char i2c_control_buf[3] = {0x06,0x92,0x03};
154
155         #if 1
156         u8 config_info[] = {
157         0x06,0xA2,
158         0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
159         0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
160         0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
161         0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
162         0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
163         0x00,0x00,0x01,0x13,0x90,0x90,0x90,0x38,
164         0x38,0x38,0x0F,0x0E,0x0A,0x40   ,0x30,0x00,
165         0x3f,00,MAX_FINGER_NUM,0x00,0x14,0x00,0x1C,0x01,
166         0x01,0x3E,0x35,0x68,0x58,0x00,0x00,0x06,
167         0x19,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
168         0x14,0x10,0x51,0x02,0x00,0x00,0x00,0x00,
169         0x00,0x00,0x20,0x40,0x60,0x90,0x08,0x40,
170         0x30,0x32,0x20,0x00,0x00,0x00,0x00,0x00,
171         0x00,0x01
172         };
173         #endif
174                 
175         #if 0
176         u8 config_info[] = {
177         0x06,0xA2,
178         0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
179         0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
180         0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
181         0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
182         0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
183         0x00,0x00,0x11,0x13,0x80,0x80,0x80,0x12,
184         0x12,0x12,0x0F,0x0F,0x0A,0x50,0x3C,0x49,
185         0x03,0x00,0x05,0x00,0x14,0xFA,0x1B,0x00,
186         0x00,0x4D,0x3D,0x81,0x65,0x00,0x00,0x06,
187         0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
188         0x14,0x10,0xAB,0x02,0x00,0x00,0x00,0x00,
189         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,
190         0x00,0x3C,0x28,0x00,0x00,0x00,0x00,0x00,
191         0x00,0x01
192
193         };                                                              
194         #endif
195 //      ret = i2c_write_bytes(ts->client, i2c_control_buf, (sizeof(i2c_control_buf)/sizeof(i2c_control_buf[0])));
196         ret = i2c_write_bytes(ts->client, config_info, (sizeof(config_info)/sizeof(config_info[0])));
197         if (ret < 0) 
198                 return ret;
199         msleep(10);
200         return 0;
201
202 }
203
204
205
206 static int  goodix_read_version(struct gt818_ts_data *ts)
207 {
208         int ret;
209         u8 version_data[5] = {0};       //store touchscreen version infomation
210         memset(version_data, 0, 5);
211         version_data[0] = 0x07;
212         version_data[1] = 0x17;
213         msleep(2);
214         ret = i2c_read_bytes(ts->client, version_data, 4);
215         if (ret < 0) 
216                 return ret;
217         dev_info(&ts->client->dev," Guitar Version: %d.%d\n",version_data[3],version_data[2]);
218         return 0;
219         
220 }
221
222
223
224 static void goodix_ts_work_func(struct work_struct *work)
225 {       
226         u8  touch_status[8*MAX_FINGER_NUM + 18] = {READ_TOUCH_ADDR_H, READ_TOUCH_ADDR_L, 0};
227         u8  *key_value = NULL;
228         u8  *point_data = NULL;
229         static u8 finger_last[MAX_FINGER_NUM + 1]={0};
230         u8  finger_current[MAX_FINGER_NUM + 1] = {0};
231         u8  coor_data[6*MAX_FINGER_NUM] = {0};
232         static u8  last_key = 0;
233
234         u8  finger = 0;
235         u8  key = 0;
236         u8 retry = 0;
237         unsigned int  count = 0;
238         unsigned int position = 0;      
239         int ret = -1;
240         int temp = 0;
241         int x = 0, y = 0 , pressure;
242
243         u16 *coor_point;
244
245         int syn_flag = 0;
246
247         struct gt818_ts_data *ts = container_of(work, struct gt818_ts_data, work);
248
249         ret = i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
250
251         if(ret <= 0) {
252                 for(retry = 0; retry < 3; retry++)
253                 {
254                         ret = i2c_pre_cmd(ts);
255                         if(ret <= 0)
256                                 continue;
257                         else
258                                 break;
259                 }
260                 if(ret <= 0) {
261                         dev_err(&(ts->client->dev),"I2C transfer error. Number:%d\n ", ret);
262                         ts->bad_data = 1;
263                         ts->retry++;
264                         goto XFER_ERROR;
265                 }
266                 else{
267                         i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
268                 }
269         }
270         //judge whether the data is ready
271         if((touch_status[2] & 0x30) != 0x20)
272         {
273                 printk("%s:DATA_NO_READY\n", __func__);
274                 goto DATA_NO_READY;
275         }
276         //judge whether it is large area touch
277         if(touch_status[13] & 0x0f)
278         {
279                 goto DATA_NO_READY;
280         }
281
282         ts->bad_data = 0;
283         finger = touch_status[2] & 0x07;
284         key_value = touch_status + 15;
285         key = key_value[2] & 0x0f;
286
287         if(finger > 0)
288         {
289                 point_data = key_value + 3;
290
291                 for(position = 0; position < (finger*8); position += 8)
292                 {
293                         temp = point_data[position];
294                         //printk("track:%d\n", temp);
295                         if(temp < (MAX_FINGER_NUM + 1))
296                         {
297                                 finger_current[temp] = 1;
298                                 for(count = 0; count < 6; count++)
299                                 {
300                                         coor_data[(temp - 1) * 6 + count] = point_data[position+1+count];
301                                 }
302                         }
303                         else
304                         {
305                                 //dev_err(&(ts->client->dev),"Track Id error:%d\n ",);
306                                 ts->bad_data = 1;
307                                 ts->retry++;
308                                 goto XFER_ERROR;
309                         }               
310                 }
311         
312         }
313         
314         else
315         {
316                 for(position = 1; position < MAX_FINGER_NUM+1; position++)
317                 {
318                         finger_current[position] = 0;
319                 }
320         }
321
322         coor_point = (u16 *)coor_data;
323
324         for(position = 1; position < MAX_FINGER_NUM + 1; position++)
325         {
326                 //printk("%s:positon:%d\n", __func__, position);
327                 if((finger_current[position] == 0) && (finger_last[position] != 0))
328                 {
329                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, 0);
330                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, 0);
331                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
332                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
333                         input_mt_sync(ts->input_dev);
334                         syn_flag = 1;
335                 }
336                 else if(finger_current[position])
337                 {
338
339                         x = (*(coor_point+3*(position-1)))*SCREEN_MAX_WIDTH/(TOUCH_MAX_WIDTH);
340                         y = (*(coor_point+3*(position-1)+1))*SCREEN_MAX_HEIGHT/(TOUCH_MAX_HEIGHT);
341                         pressure = (*(coor_point+3*(position-1)+2));
342                         if(x < SCREEN_MAX_WIDTH){
343                                 x = SCREEN_MAX_WIDTH - x;
344                         }
345
346                         if(y < SCREEN_MAX_HEIGHT){
347                         //      y = SCREEN_MAX_HEIGHT-y;
348                         }
349                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, position - 1);
350                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
351                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
352                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
353                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure);
354                         input_mt_sync(ts->input_dev);
355                         syn_flag = 1;
356                 }
357         }
358
359
360 #ifdef HAVE_TOUCH_KEY
361         if((last_key == 0) && (key == 0))
362                 goto NO_KEY_PRESS;
363         else
364         {
365                 syn_flag = 1;
366                 switch(key){
367                         case 1:
368                                 key = 4;
369                                 break;
370                         case 2:
371                                 key = 3;
372                                 break;
373                         case 4:
374                                 key = 2;
375                                 break;
376                         case 8:
377                                 key = 1;
378                                 break;
379                         default:
380                                 key = 0;
381                                 break;
382                 }
383                 if(key != 0){
384                         input_report_key(ts->input_dev, gt818_key_array[key - 1], 1);
385                 }
386                 else{
387                         input_report_key(ts->input_dev, gt818_key_array[last_key - 1], 0);
388                 }
389                 last_key = key;
390         }               
391
392 #endif
393
394
395 NO_KEY_PRESS:
396         if(syn_flag){
397                 input_sync(ts->input_dev);
398         }
399
400         for(position = 1; position < MAX_FINGER_NUM + 1; position++)
401         {
402                 finger_last[position] = finger_current[position];
403         }
404
405 DATA_NO_READY:
406 XFER_ERROR:
407 //      i2c_end_cmd(ts);
408         if(ts->use_irq)
409                 enable_irq(ts->client->irq);
410
411 }
412
413
414 static enum hrtimer_restart goodix_ts_timer_func(struct hrtimer *timer)
415 {
416         struct gt818_ts_data *ts = container_of(timer, struct gt818_ts_data, timer);
417         queue_work(goodix_wq, &ts->work);
418         hrtimer_start(&ts->timer, ktime_set(0, (POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
419         return HRTIMER_NORESTART;
420 }
421
422
423 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
424 {
425         struct gt818_ts_data *ts = dev_id;
426         disable_irq_nosync(ts->client->irq);
427         queue_work(goodix_wq, &ts->work);
428         return IRQ_HANDLED;
429 }
430
431 static int goodix_ts_power(struct gt818_ts_data * ts, int on)
432 {
433         int ret = -1;
434         struct gt818_platform_data      *pdata = ts->client->dev.platform_data;
435         unsigned char i2c_control_buf[3] = {0x06,0x92,0x01};            //suspend cmd
436         unsigned char i2c_config_buf[3] = {0x06,0x92,0x01};
437         
438         #ifdef INT_PORT 
439         if(ts != NULL && !ts->use_irq)
440                 return -2;
441 #endif          
442         switch(on)
443         {
444                 case 0:
445                         i2c_pre_cmd(ts);
446                         // set the io port high level to avoid level change which might stop gt818 from sleeping
447                         gpio_direction_output(pdata->gpio_reset, 1);
448                         gpio_direction_output(pdata->gpio_pendown, 1);
449                         msleep(5);
450                         ret = i2c_write_bytes(ts->client, i2c_control_buf, 3);
451                         if(ret < 0)
452                         {
453                                 printk(KERN_INFO"**gt818 suspend fail**\n");
454                         }
455                         else
456                         {
457                                 printk(KERN_INFO"**gt818 suspend**\n");
458                                 ret = 0;
459                         }
460 //                      i2c_end_cmd(ts);
461                         return ret;
462                         
463                 case 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 #ifdef INT_PORT
522         ret = gpio_request(INT_PORT, "TS_INT"); //Request IO
523         if (ret){
524                 dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(int)INT_PORT, ret);
525                 goto err_gpio_request_failed;
526         }
527         rk29_mux_api_set(pdata->pendown_iomux_name, pdata->pendown_iomux_mode);
528         gpio_direction_input(INT_PORT);
529         gpio_pull_updown(INT_PORT, 0);
530 #endif
531
532         ret = gpio_request(pdata->gpio_reset, "gt818_resetPin");
533         if(ret){
534                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n", pdata->gpio_reset);
535                 goto err_gpio_request_failed;
536         }
537         rk29_mux_api_set(pdata->resetpin_iomux_name, pdata->resetpin_iomux_mode);
538 #if 1
539         for(retry = 0; retry < 4; retry++)
540         {
541                 gpio_direction_output(pdata->gpio_reset, 0);
542                 msleep(2);
543                 gpio_direction_input(pdata->gpio_reset);
544                 gpio_pull_updown(pdata->gpio_reset, 0);
545                 msleep(30);
546                 ret = i2c_pre_cmd(ts);
547                 if (ret > 0)
548                         break;
549                 msleep(50);
550         }
551
552         if(ret <= 0)
553         {
554                 dev_err(&client->dev, "Warnning: I2C communication might be ERROR!\n");
555                 goto err_i2c_failed;
556         }       
557
558 #endif
559         for(retry = 0; retry < 3; retry++)
560         {
561                 ret = goodix_init_panel(ts);
562                 dev_info(&client->dev,"the config ret is :%d\n", ret);
563                 msleep(20);
564                 if(ret < 0)     //Initiall failed
565                         continue;
566                 else
567                         break;
568         }
569
570         if(ret < 0) {
571                 ts->bad_data = 1;
572                 goto err_init_godix_ts;
573         }
574         goodix_read_version(ts);
575 //      i2c_end_cmd(ts);
576         INIT_WORK(&ts->work, goodix_ts_work_func);              //init work_struct
577         ts->input_dev = input_allocate_device();
578         if (ts->input_dev == NULL) {
579                 ret = -ENOMEM;
580                 dev_dbg(&client->dev,"goodix_ts_probe: Failed to allocate input device\n");
581                 goto err_input_dev_alloc_failed;
582         }
583
584         ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
585         ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
586 //      ts->input_dev->absbit[0] = BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) |
587 //                      BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR);  // for android
588
589
590 #ifdef HAVE_TOUCH_KEY
591         for(retry = 0; retry < MAX_KEY_NUM; retry++)
592         {
593                 input_set_capability(ts->input_dev, EV_KEY, gt818_key_array[retry]);
594         }
595 #endif
596
597         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
598         snprintf(ts->name, sizeof(ts->name), "gt818-touchscreen");
599
600         ts->input_dev->name = "gt818_ts";//ts->name;
601         ts->input_dev->phys = ts->phys;
602         ts->input_dev->dev.parent = &client->dev;
603         ts->input_dev->id.bustype = BUS_I2C;
604         ts->input_dev->id.vendor = 0xDEAD;
605         ts->input_dev->id.product = 0xBEEF;
606         ts->input_dev->id.version = 10427;      //screen firmware version
607
608 #ifdef GOODIX_MULTI_TOUCH
609
610         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
611         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
612         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_WIDTH, 0, 0);
613         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_HEIGHT, 0, 0);
614         input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, MAX_FINGER_NUM, 0, 0);
615 #else
616         input_set_abs_params(ts->input_dev, ABS_X, 0, SCREEN_MAX_HEIGHT, 0, 0);
617         input_set_abs_params(ts->input_dev, ABS_Y, 0, SCREEN_MAX_WIDTH, 0, 0);
618         input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, 0, 0);
619 #endif  
620         
621         ret = input_register_device(ts->input_dev);
622         if (ret) {
623                 dev_err(&client->dev,"Probe: Unable to register %s input device\n", ts->input_dev->name);
624                 goto err_input_register_device_failed;
625         }
626         ts->bad_data = 0;
627 //      finger_list.length = 0;
628
629 #ifdef INT_PORT 
630
631         client->irq = TS_INT;           //If not defined in client
632         if (client->irq)
633         {
634
635         #if INT_TRIGGER==0
636                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_RISING
637         #elif INT_TRIGGER==1
638                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_FALLING
639         #elif INT_TRIGGER==2
640                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_LOW
641         #elif INT_TRIGGER==3
642                 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_HIGH
643         #endif
644
645                 ret = request_irq(client->irq, goodix_ts_irq_handler, GT801_PLUS_IRQ_TYPE,
646                         client->name, ts);
647                 if (ret != 0) {
648                         dev_err(&client->dev,"Cannot allocate ts INT!ERRNO:%d\n", ret);
649                         gpio_direction_input(INT_PORT);
650                         gpio_free(INT_PORT);
651                         goto err_gpio_request_failed;
652                 }
653                 else 
654                 {       
655                         disable_irq(client->irq);
656                         ts->use_irq = 1;
657                         dev_dbg(&client->dev,"Reques EIRQ %d succesd on GPIO:%d\n",TS_INT,INT_PORT);
658                 }       
659         }
660 #endif  
661
662 err_gpio_request_failed:
663         ts->power = goodix_ts_power;
664 #ifdef CONFIG_HAS_EARLYSUSPEND
665         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
666         ts->early_suspend.suspend = goodix_ts_early_suspend;
667         ts->early_suspend.resume = goodix_ts_late_resume;
668         register_early_suspend(&ts->early_suspend);
669 #endif
670         dev_info(&client->dev,"Start %s in %s mode\n", 
671                 ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
672
673         if (ts->use_irq)
674         {
675                 enable_irq(client->irq);
676         }
677         else
678         {
679                 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
680                 ts->timer.function = goodix_ts_timer_func;
681                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
682         }
683         return 0;
684
685 err_init_godix_ts:
686 //      i2c_end_cmd(ts);
687         if(ts->use_irq)
688         {
689                 ts->use_irq = 0;
690                 free_irq(client->irq,ts);
691         #ifdef INT_PORT 
692                 gpio_direction_input(INT_PORT);
693                 gpio_free(INT_PORT);
694         #endif  
695         }
696         else 
697                 hrtimer_cancel(&ts->timer);
698
699 err_input_register_device_failed:
700         input_free_device(ts->input_dev);
701
702 err_input_dev_alloc_failed:
703         i2c_set_clientdata(client, NULL);
704 err_i2c_failed: 
705         kfree(ts);
706 err_alloc_data_failed:
707 err_check_functionality_failed:
708 err_create_proc_entry:
709         return ret;
710 }
711
712
713 static int goodix_ts_remove(struct i2c_client *client)
714 {
715         struct gt818_ts_data *ts = i2c_get_clientdata(client);
716         struct gt818_platform_data      *pdata = client->dev.platform_data;
717
718 #ifdef CONFIG_HAS_EARLYSUSPEND
719         unregister_early_suspend(&ts->early_suspend);
720 #endif
721 #ifdef CONFIG_TOUCHSCREEN_GOODIX_IAP
722         remove_proc_entry("goodix-update", NULL);
723 #endif
724         if (ts && ts->use_irq) 
725         {
726         #ifdef INT_PORT
727                 gpio_direction_input(INT_PORT);
728                 gpio_free(INT_PORT);
729         #endif  
730                 free_irq(client->irq, ts);
731         }       
732         else if(ts)
733                 hrtimer_cancel(&ts->timer);
734         
735         dev_notice(&client->dev,"The driver is removing...\n");
736         i2c_set_clientdata(client, NULL);
737         input_unregister_device(ts->input_dev);
738         kfree(ts);
739         return 0;
740 }
741
742
743 static int goodix_ts_suspend(struct i2c_client *client, pm_message_t mesg)
744 {
745         int ret;
746         struct gt818_ts_data *ts = i2c_get_clientdata(client);
747
748         if (ts->use_irq)
749                 disable_irq(client->irq);
750         else
751                 hrtimer_cancel(&ts->timer);
752         //ret = cancel_work_sync(&ts->work);
753         //if(ret && ts->use_irq)        
754                 //enable_irq(client->irq);
755         if (ts->power) {
756                 ret = ts->power(ts, 0);
757                 if (ret < 0)
758                         printk(KERN_ERR "goodix_ts_resume power off failed\n");
759         }
760         return 0;
761 }
762
763
764 static int goodix_ts_resume(struct i2c_client *client)
765 {
766         int ret;
767         struct gt818_ts_data *ts = i2c_get_clientdata(client);
768
769         if (ts->power) {
770                 ret = ts->power(ts, 1);
771                 if (ret < 0)
772                         printk(KERN_ERR "goodix_ts_resume power on failed\n");
773         }
774
775         if (ts->use_irq)
776                 enable_irq(client->irq);
777         else
778                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
779
780         return 0;
781 }
782
783 #ifdef CONFIG_HAS_EARLYSUSPEND
784 static void goodix_ts_early_suspend(struct early_suspend *h)
785 {
786         struct gt818_ts_data *ts;
787         ts = container_of(h, struct gt818_ts_data, early_suspend);
788         goodix_ts_suspend(ts->client, PMSG_SUSPEND);
789 }
790
791 static void goodix_ts_late_resume(struct early_suspend *h)
792 {
793         struct gt818_ts_data *ts;
794         ts = container_of(h, struct gt818_ts_data, early_suspend);
795         goodix_ts_resume(ts->client);
796 }
797 #endif
798
799
800 //only one client
801 static const struct i2c_device_id goodix_ts_id[] = {
802         { GOODIX_I2C_NAME, 0 },
803         { }
804 };
805
806
807 static struct i2c_driver goodix_ts_driver = {
808         .probe          = goodix_ts_probe,
809         .remove         = goodix_ts_remove,
810 #ifndef CONFIG_HAS_EARLYSUSPEND
811         .suspend        = goodix_ts_suspend,
812         .resume         = goodix_ts_resume,
813 #endif
814         .id_table       = goodix_ts_id,
815         .driver = {
816                 .name   = GOODIX_I2C_NAME,
817                 .owner = THIS_MODULE,
818         },
819 };
820
821
822 static int __devinit goodix_ts_init(void)
823 {
824         int ret;
825         goodix_wq = create_singlethread_workqueue("goodix_wq");         //create a work queue and worker thread
826         if (!goodix_wq) {
827                 printk(KERN_ALERT "creat workqueue faiked\n");
828                 return -ENOMEM;
829         }
830         ret = i2c_add_driver(&goodix_ts_driver);
831         return ret; 
832 }
833
834
835 static void __exit goodix_ts_exit(void)
836 {
837         printk(KERN_ALERT "Touchscreen driver of guitar exited.\n");
838         i2c_del_driver(&goodix_ts_driver);
839         if (goodix_wq)
840                 destroy_workqueue(goodix_wq);           //release our work queue
841 }
842
843 late_initcall(goodix_ts_init);
844 module_exit(goodix_ts_exit);
845
846 MODULE_DESCRIPTION("Goodix Touchscreen Driver");
847 MODULE_AUTHOR("hhb@rock-chips.com")
848 MODULE_LICENSE("GPL");
849