Merge remote-tracking branch 'linux-2.6.32.y/master' into develop
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ili2102_ts.c
1 #include <linux/module.h>
2 #include <linux/delay.h>
3 #include <linux/earlysuspend.h>
4 #include <linux/hrtimer.h>
5 #include <linux/i2c.h>
6 #include <linux/input.h>
7 #include <linux/interrupt.h>
8 #include <linux/io.h>
9 #include <linux/gpio.h>
10 #include <linux/types.h>
11 #include <mach/iomux.h>
12 #include <linux/platform_device.h>
13 #include <linux/kthread.h>
14 #include <linux/sched.h>
15 #include <linux/irq.h>
16 #include <linux/cdev.h>
17 #include <asm/uaccess.h>
18
19 #include "ili2102_ts.h"
20
21
22 #if 0
23         #define DBG(msg...)     printk(msg);
24 #else
25         #define DBG(msg...)
26 #endif
27
28 #define TOUCH_NUMBER 2
29
30 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
31 static unsigned int g_x[TOUCH_NUMBER] =  {0},g_y[TOUCH_NUMBER] = {0};
32
33 struct ili2102_ts_data {
34         u16             model;                  /* 801. */      
35         bool    swap_xy;                /* swap x and y axes */ 
36         u16             x_min, x_max;   
37         u16             y_min, y_max;
38         uint16_t addr;
39         int     use_irq;
40         int     pendown;
41         int     gpio_pendown;
42         int     gpio_reset;
43         int     gpio_reset_active_low;
44         int             pendown_iomux_mode;     
45         int             resetpin_iomux_mode;
46         char    pendown_iomux_name[IOMUX_NAME_SIZE];    
47         char    resetpin_iomux_name[IOMUX_NAME_SIZE];   
48         char    phys[32];
49         char    name[32];
50         int             valid_i2c_register;
51         struct  i2c_client *client;
52         struct  input_dev *input_dev;
53         struct  hrtimer timer;
54         struct  delayed_work    work;
55         struct  workqueue_struct *ts_wq;        
56         struct  early_suspend early_suspend;
57 };
58
59 #ifdef CONFIG_HAS_EARLYSUSPEND
60 static void ili2102_ts_early_suspend(struct early_suspend *h);
61 static void ili2102_ts_late_resume(struct early_suspend *h);
62 #endif
63
64 #define ILI2102_TS_APK_SUPPORT 1
65
66 #if ILI2102_TS_APK_SUPPORT
67 // device data
68 struct dev_data {
69         // device number
70         dev_t devno;
71         // character device
72         struct cdev cdev;
73         // class device
74         struct class *class;
75 };
76
77 // global variables
78 static struct ili2102_ts_data *g_ts;
79 static struct dev_data g_dev;
80
81 // definitions
82 #define ILITEK_I2C_RETRY_COUNT                  3
83 #define ILITEK_FILE_DRIVER_NAME                 "ilitek_file"
84 #define ILITEK_DEBUG_LEVEL                      KERN_INFO
85 #define ILITEK_ERROR_LEVEL                      KERN_ALERT
86
87 // i2c command for ilitek touch screen
88 #define ILITEK_TP_CMD_READ_DATA                 0x10
89 #define ILITEK_TP_CMD_READ_SUB_DATA             0x11
90 #define ILITEK_TP_CMD_GET_RESOLUTION            0x20
91 #define ILITEK_TP_CMD_GET_FIRMWARE_VERSION      0x40
92 #define ILITEK_TP_CMD_GET_PROTOCOL_VERSION      0x42
93 #define ILITEK_TP_CMD_CALIBRATION               0xCC
94 #define ILITEK_TP_CMD_ERASE_BACKGROUND          0xCE
95
96 // define the application command
97 #define ILITEK_IOCTL_BASE                       100
98 #define ILITEK_IOCTL_I2C_WRITE_DATA             _IOWR(ILITEK_IOCTL_BASE, 0, unsigned char*)
99 #define ILITEK_IOCTL_I2C_WRITE_LENGTH           _IOWR(ILITEK_IOCTL_BASE, 1, int)
100 #define ILITEK_IOCTL_I2C_READ_DATA              _IOWR(ILITEK_IOCTL_BASE, 2, unsigned char*)
101 #define ILITEK_IOCTL_I2C_READ_LENGTH            _IOWR(ILITEK_IOCTL_BASE, 3, int)
102 #define ILITEK_IOCTL_USB_WRITE_DATA             _IOWR(ILITEK_IOCTL_BASE, 4, unsigned char*)
103 #define ILITEK_IOCTL_USB_WRITE_LENGTH           _IOWR(ILITEK_IOCTL_BASE, 5, int)
104 #define ILITEK_IOCTL_USB_READ_DATA              _IOWR(ILITEK_IOCTL_BASE, 6, unsigned char*)
105 #define ILITEK_IOCTL_USB_READ_LENGTH            _IOWR(ILITEK_IOCTL_BASE, 7, int)
106 #define ILITEK_IOCTL_I2C_UPDATE_RESOLUTION      _IOWR(ILITEK_IOCTL_BASE, 8, int)
107 #define ILITEK_IOCTL_USB_UPDATE_RESOLUTION      _IOWR(ILITEK_IOCTL_BASE, 9, int)
108 #define ILITEK_IOCTL_I2C_SET_ADDRESS            _IOWR(ILITEK_IOCTL_BASE, 10, int)
109 #define ILITEK_IOCTL_I2C_UPDATE                 _IOWR(ILITEK_IOCTL_BASE, 11, int)
110 #define ILITEK_IOCTL_STOP_READ_DATA             _IOWR(ILITEK_IOCTL_BASE, 12, int)
111 #define ILITEK_IOCTL_START_READ_DATA            _IOWR(ILITEK_IOCTL_BASE, 13, int)
112
113 static int ilitek_file_open(struct inode *inode, struct file *filp)
114 {
115         return 0; 
116 }
117
118 static ssize_t ilitek_file_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
119 {
120         int ret;
121         unsigned char buffer[128]={0};
122         struct i2c_msg msg[2];
123
124         msg[0].addr = g_ts->client->addr;
125         msg[0].flags = g_ts->client->flags;
126         msg[0].len = count;
127         msg[0].buf = buffer;
128         msg[0].scl_rate = 400*1000;
129         msg[0].udelay = 80;
130
131         printk("%s:count=0x%x\n",__FUNCTION__,count);
132         
133         // before sending data to touch device, we need to check whether the device is working or not
134         if(g_ts->valid_i2c_register == 0){
135                 printk(ILITEK_ERROR_LEVEL "%s, i2c device driver doesn't be registered\n", __func__);
136                 return -1;
137         }
138
139         // check the buffer size whether it exceeds the local buffer size or not
140         if(count > 128){
141                 printk(ILITEK_ERROR_LEVEL "%s, buffer exceed 128 bytes\n", __func__);
142                 return -1;
143         }
144
145         // copy data from user space
146         ret = copy_from_user(buffer, buf, count-1);
147         if(ret < 0){
148                 printk(ILITEK_ERROR_LEVEL "%s, copy data from user space, failed", __func__);
149                 return -1;
150         }
151
152         // parsing command
153         if(strcmp(buffer, "calibrate") == 0){
154                 buffer[0] = ILITEK_TP_CMD_ERASE_BACKGROUND;
155         msg[0].len = 1;
156         ret = i2c_transfer(g_ts->client->adapter, msg, 1);
157         if(ret < 0){
158                 printk(ILITEK_DEBUG_LEVEL "%s, i2c erase background, failed\n", __func__);
159         }
160         else{
161                 printk(ILITEK_DEBUG_LEVEL "%s, i2c erase background, success\n", __func__);
162         }
163
164                 buffer[0] = ILITEK_TP_CMD_CALIBRATION;
165         msg[0].len = 1;
166         msleep(2000);
167         ret = i2c_transfer(g_ts->client->adapter, msg, 1);
168                 if(ret < 0){
169         printk(ILITEK_DEBUG_LEVEL "%s, i2c calibration, failed\n", __func__);
170         }
171                 else{
172         printk(ILITEK_DEBUG_LEVEL "%s, i2c calibration, success\n", __func__);
173                 }
174                 msleep(1000);
175                 return count;
176         }
177         return -1;
178 }
179
180
181 static int ilitek_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
182 {
183         static unsigned char buffer[64]={0};
184         static int len=0;
185         int ret;
186         struct i2c_msg msg[2];
187         
188         msg[0].addr = g_ts->client->addr;
189         msg[0].flags = g_ts->client->flags;
190         msg[0].len = len;
191         msg[0].buf = buffer;
192         msg[0].scl_rate = 400*1000;
193         msg[0].udelay = 80;
194         
195         // parsing ioctl command
196         switch(cmd){
197         case ILITEK_IOCTL_I2C_WRITE_DATA:
198                 ret = copy_from_user(buffer, (unsigned char*)arg, len);
199                 if(ret < 0){
200         printk(ILITEK_ERROR_LEVEL "%s, copy data from user space, failed\n", __func__);
201         return -1;
202         }
203                 ret = i2c_transfer(g_ts->client->adapter, msg, 1);
204                 if(ret < 0){
205                 printk(ILITEK_ERROR_LEVEL "%s, i2c write, failed\n", __func__);
206                 return -1;
207                 }
208                 break;
209                 
210         case ILITEK_IOCTL_I2C_READ_DATA:
211                 msg[0].addr = g_ts->client->addr;
212                 msg[0].flags = g_ts->client->flags | I2C_M_RD;
213                 msg[0].len = len;       
214                 msg[0].buf = buffer;
215                 msg[0].scl_rate = 400*1000;
216                 msg[0].udelay = 80;
217                 ret = i2c_transfer(g_ts->client->adapter, msg, 1);
218                 if(ret < 0){
219         printk(ILITEK_ERROR_LEVEL "%s, i2c read, failed\n", __func__);
220                 return -1;
221         }
222                 ret = copy_to_user((unsigned char*)arg, buffer, len);
223                 if(ret < 0){
224         printk(ILITEK_ERROR_LEVEL "%s, copy data to user space, failed\n", __func__);
225         return -1;
226         }
227                 break;
228         case ILITEK_IOCTL_I2C_WRITE_LENGTH:
229         case ILITEK_IOCTL_I2C_READ_LENGTH:
230                 len = arg;
231                 break;
232         case ILITEK_IOCTL_I2C_UPDATE_RESOLUTION:
233         case ILITEK_IOCTL_I2C_SET_ADDRESS:
234         case ILITEK_IOCTL_I2C_UPDATE:
235                 break;
236         case ILITEK_IOCTL_START_READ_DATA:
237                 //g_ts.stop_polling = 0;
238                 break;
239         case ILITEK_IOCTL_STOP_READ_DATA:
240                 //g_ts.stop_polling = 1;
241                 break;
242         default:
243                 return -1;
244         }
245         
246         printk("%s:cmd=0x%x\n",__FUNCTION__,cmd);
247         
248         return 0;
249 }
250
251
252 static ssize_t ilitek_file_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
253 {
254         return 0;
255 }
256
257
258 static int ilitek_file_close(struct inode *inode, struct file *filp)
259 {
260         return 0;
261 }
262
263
264 // declare file operations
265 struct file_operations ilitek_fops = {
266         .ioctl = ilitek_file_ioctl,
267         .read = ilitek_file_read,
268         .write = ilitek_file_write,
269         .open = ilitek_file_open,
270         .release = ilitek_file_close,
271 };
272
273 #endif
274 static int verify_coord(struct ili2102_ts_data *ts,unsigned int *x,unsigned int *y)
275 {
276
277         //DBG("%s:(%d/%d)\n",__FUNCTION__,*x, *y);
278         if((*x< ts->x_min) || (*x > ts->x_max))
279                 return -1;
280
281         if((*y< ts->y_min) || (*y > ts->y_max))
282                 return -1;
283
284         return 0;
285 }
286 static int ili2102_init_panel(struct ili2102_ts_data *ts)
287 {       
288         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
289         mdelay(1);
290         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
291         return 0;
292 }
293
294 static void ili2102_ts_work_func(struct work_struct *work)
295 {
296         int i,ret;
297         int syn_flag = 0;
298         unsigned int x, y;
299         struct i2c_msg msg[2];
300         uint8_t start_reg;
301         uint8_t buf[9];//uint32_t buf[4];
302         struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
303
304         DBG("ili2102_ts_work_func\n");
305
306         /*Touch Information Report*/
307         start_reg = 0x10;
308
309         msg[0].addr = ts->client->addr;
310         msg[0].flags = ts->client->flags;
311         msg[0].len = 1;
312         msg[0].buf = &start_reg;
313         msg[0].scl_rate = 400*1000;
314         msg[0].udelay = 200;
315         
316         msg[1].addr = ts->client->addr;
317         msg[1].flags = ts->client->flags | I2C_M_RD;
318         msg[1].len = 9; 
319         msg[1].buf = buf;
320         msg[1].scl_rate = 400*1000;
321         msg[1].udelay = 0;
322         
323         ret = i2c_transfer(ts->client->adapter, msg, 2); 
324         if (ret < 0) 
325         {
326                 printk("%s:i2c_transfer fail, ret=%d\n",__FUNCTION__,ret);
327                 goto out;
328         }
329
330         for(i=0; i<TOUCH_NUMBER; i++)
331         {
332
333                 if(!((buf[0]>>i)&0x01))
334                 {
335                         if (touch_state[i] == TOUCH_DOWN)
336                         {
337                                 DBG("ili2102_ts_work_func:buf[%d]=%d\n",i,buf[i]);
338                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0); //Finger Size
339                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0); //Touch Size
340                                 input_mt_sync(ts->input_dev);
341                                 syn_flag = 1;
342                                 touch_state[i] = TOUCH_UP;
343                                 DBG("touch_up \n");
344                         }
345
346                 }
347                 else
348                 {
349                         if((buf[0]>>i)&0x01)
350                         {
351                                 x = buf[1+(i<<2)] | (buf[2+(i<<2)] << 8);
352                                 y = buf[3+(i<<2)] | (buf[4+(i<<2)] << 8);
353                                 
354                                 if (ts->swap_xy)
355                                 swap(x, y);
356
357                                 if (verify_coord(ts,&x,&y))//goto out;
358                                 {
359                                         printk("err:x=%d,y=%d\n",x,y);
360                                         x = g_x[i];
361                                         y = g_y[i];
362                                 }
363
364                                 g_x[i] = x;
365                                 g_y[i] = y;                     
366                                 input_event(ts->input_dev, EV_ABS, ABS_MT_TRACKING_ID, i);
367                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1); //Finger Size
368                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
369                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
370                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 5); //Touch Size
371                         input_mt_sync(ts->input_dev);
372                                 syn_flag = 1;
373                                 touch_state[i] = TOUCH_DOWN;
374                                  ts->pendown = 1;
375                                 DBG("touch_down i=%d X = %d, Y = %d\n",i, x, y);
376                         }
377                         
378                 }
379         }
380         
381         if(syn_flag)
382         input_sync(ts->input_dev);
383 out:   
384 #if 0
385         if(ts->pendown)
386         {
387                 schedule_delayed_work(&ts->work, msecs_to_jiffies(10));
388                 ts->pendown = 0;
389         }
390         else
391         {
392                 if (ts->use_irq) 
393                 enable_irq(ts->client->irq);
394         }
395 #else
396         enable_irq(ts->client->irq);//intterupt pin will be high after i2c read so could enable irq at once
397 #endif
398         DBG("pin=%d,level=%d,irq=%d\n\n",irq_to_gpio(ts->client->irq),gpio_get_value(irq_to_gpio(ts->client->irq)),ts->client->irq);
399
400 }
401
402 static irqreturn_t ili2102_ts_irq_handler(int irq, void *dev_id)
403 {
404         struct ili2102_ts_data *ts = dev_id;
405         DBG("ili2102_ts_irq_handler=%d,%d\n",ts->client->irq,ts->use_irq);
406
407         disable_irq_nosync(ts->client->irq); //disable_irq(ts->client->irq);
408         queue_delayed_work(ts->ts_wq, &ts->work, 0);
409         return IRQ_HANDLED;
410 }
411
412 static int __devinit setup_resetPin(struct i2c_client *client, struct ili2102_ts_data *ts)
413 {
414         struct ili2102_platform_data    *pdata = client->dev.platform_data;
415         int err;
416         
417         ts->gpio_reset = pdata->gpio_reset;
418         strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
419         ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
420         ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
421         
422         DBG("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
423
424         if (!gpio_is_valid(ts->gpio_reset)) {
425                 dev_err(&client->dev, "no gpio_reset?\n");
426                 return -EINVAL;
427         }
428
429         rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode); 
430         err = gpio_request(ts->gpio_reset, "ili2102_resetPin");
431         if (err) {
432                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
433                                 ts->gpio_reset);
434                 return err;
435         }
436         
437         //gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
438
439         err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
440         if (err) {
441                 dev_err(&client->dev, "failed to set resetPin GPIO%d\n",
442                                 ts->gpio_reset);
443                 gpio_free(ts->gpio_reset);
444                 return err;
445         }
446         
447         mdelay(5);
448
449         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
450
451         mdelay(200);
452          
453         return 0;
454 }
455
456 static int __devinit setup_pendown(struct i2c_client *client, struct ili2102_ts_data *ts)
457 {
458         int err;
459         struct ili2102_platform_data    *pdata = client->dev.platform_data;
460         
461         if (!client->irq) {
462                 dev_dbg(&client->dev, "no IRQ?\n");
463                 return -ENODEV;
464         }
465         
466         if (!gpio_is_valid(pdata->gpio_pendown)) {
467                 dev_err(&client->dev, "no gpio_pendown?\n");
468                 return -EINVAL;
469         }
470         
471         ts->gpio_pendown = pdata->gpio_pendown;
472         strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
473         ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
474         
475         DBG("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
476         
477         if (!gpio_is_valid(ts->gpio_pendown)) {
478                 dev_err(&client->dev, "no gpio_pendown?\n");
479                 return -EINVAL;
480         }
481         
482         rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode);
483         err = gpio_request(ts->gpio_pendown, "ili2102_pendown");
484         if (err) {
485                 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
486                                 ts->gpio_pendown);
487                 return err;
488         }
489         
490         err = gpio_pull_updown(ts->gpio_pendown, PullDisable);
491         if (err) {
492                 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
493                                 ts->gpio_pendown);
494                 gpio_free(ts->gpio_pendown);
495                 return err;
496         }
497         return 0;
498 }
499
500 static int ili2102_chip_Init(struct i2c_client *client)
501 {       
502         int ret = 0;
503         uint8_t start_reg;
504         uint8_t buf[6];
505         struct i2c_msg msg[2];
506         
507         /* get panel information:6bytes */
508         start_reg = 0x20;
509         msg[0].addr =client->addr;
510         msg[0].flags = client->flags;
511         msg[0].len = 1;
512         msg[0].buf = &start_reg;
513         msg[0].scl_rate = 400*1000;
514         msg[0].udelay = 200;
515
516         ret = i2c_transfer(client->adapter, msg, 1);   
517         if (ret < 0) {
518         printk("%s:err\n",__FUNCTION__);
519         }
520         
521         mdelay(5);//tp need delay
522         
523         msg[0].addr = client->addr;
524         msg[0].flags = client->flags |I2C_M_RD;
525         msg[0].len = 6;
526         msg[0].buf = (u8*)&buf[0];
527         msg[0].scl_rate = 400*1000;
528         msg[0].udelay = 200;
529
530         ret = i2c_transfer(client->adapter, msg, 1);   
531         if (ret < 0) {
532         printk("%s:err\n",__FUNCTION__);
533         }
534
535         printk("%s:max_x=%d,max_y=%d,b[4]=0x%x,b[5]=0x%x\n", 
536                 __FUNCTION__,buf[0]|(buf[1]<<8),buf[2]|(buf[3]<<8),buf[4],buf[5]);
537
538         /*get firmware version:3bytes */        
539         start_reg = 0x40;
540         msg[0].addr =client->addr;
541         msg[0].flags = client->flags;
542         msg[0].len = 1;
543         msg[0].buf = &start_reg;
544         msg[0].scl_rate = 400*1000;
545         msg[0].udelay = 200;
546
547         ret = i2c_transfer(client->adapter, msg, 1);   
548         if (ret < 0) {
549         printk("%s:err\n",__FUNCTION__);
550         }
551         
552         mdelay(5);//tp need delay
553         
554         msg[0].addr = client->addr;
555         msg[0].flags = client->flags | I2C_M_RD;
556         msg[0].len = 3;
557         msg[0].buf = (u8*)&buf[0];
558         msg[0].scl_rate =400*1000;
559         msg[0].udelay = 200;
560
561         ret = i2c_transfer(client->adapter, msg, 1);
562         if (ret < 0) {
563         printk("%s:err\n",__FUNCTION__);
564         }
565
566         printk("%s:Ver %d.%d.%d\n",__FUNCTION__,buf[0],buf[1],buf[2]);
567
568         return ret;
569     
570 }
571
572 static int ili2102_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
573 {
574         struct ili2102_ts_data *ts;
575         struct ili2102_platform_data    *pdata = client->dev.platform_data;
576         int ret = 0;
577
578         printk("ili2102 TS probe\n"); 
579     
580         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
581             printk(KERN_ERR "ili2102_ts_probe: need I2C_FUNC_I2C\n");
582             ret = -ENODEV;
583             goto err_check_functionality_failed;
584         }
585
586         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
587         if (ts == NULL) {
588             ret = -ENOMEM;
589             goto err_alloc_data_failed;
590         }
591         
592         ts->ts_wq = create_singlethread_workqueue("ts_wq");
593         if (!ts->ts_wq)
594         {
595                 printk("%s:fail to create ts_wq,ret=0x%x\n",__FUNCTION__, ENOMEM);
596                 return -ENOMEM;
597         }
598         //INIT_WORK(&ts->work, ili2102_ts_work_func);
599         INIT_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
600         ts->client = client;
601         i2c_set_clientdata(client, ts);
602
603         ret = setup_resetPin(client,ts);
604         if(ret)
605         {
606                  printk("ili2102 TS setup_resetPin fail\n");
607                  goto err_alloc_data_failed;
608         }
609
610         ret=ili2102_chip_Init(ts->client);
611         if(ret<0)
612         {
613                 printk("%s:chips init failed\n",__FUNCTION__);
614                 goto err_resetpin_failed;
615         }
616
617         /* allocate input device */
618         ts->input_dev = input_allocate_device();
619         if (ts->input_dev == NULL) {
620             ret = -ENOMEM;
621             printk(KERN_ERR "ili2102_ts_probe: Failed to allocate input device\n");
622             goto err_input_dev_alloc_failed;
623         }
624
625         ts->model = pdata->model ? : 801;
626         ts->swap_xy = pdata->swap_xy;
627         ts->x_min = pdata->x_min;
628         ts->x_max = pdata->x_max;
629         ts->y_min = pdata->y_min;
630         ts->y_max = pdata->y_max;
631         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
632         snprintf(ts->name, sizeof(ts->name), "ili%d-touchscreen", ts->model);
633         ts->input_dev->phys = ts->phys;
634         ts->input_dev->name = ts->name;
635         ts->input_dev->dev.parent = &client->dev;
636         ts->pendown = 0;
637         ts->valid_i2c_register = 1;
638
639         ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_ABS);
640         //ts->input_dev->absbit[0] = 
641                 //BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) | 
642                 //BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR);  // for android
643         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 
644                     ts->x_min ? : 0,
645                         ts->x_max ? : 480,
646                         0, 0);
647         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
648                         ts->y_min ? : 0,
649                         ts->y_max ? : 800,
650                         0, 0);
651         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0); //Finger Size
652         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0); //Touch Size
653
654         /* ts->input_dev->name = ts->keypad_info->name; */
655         ret = input_register_device(ts->input_dev);
656         if (ret) {
657             printk(KERN_ERR "ili2102_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
658             goto err_input_register_device_failed;
659         }
660
661         client->irq = gpio_to_irq(client->irq);
662         if (client->irq) 
663         {
664                 ret = setup_pendown(client,ts);
665                 if(ret)
666                 {
667                          printk("ili2102 TS setup_pendown fail\n");
668                          goto err_input_register_device_failed;
669                 }
670                 
671         ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, client->name, ts);
672         if (ret == 0) {
673             DBG("ili2102 TS register ISR (irq=%d)\n", client->irq);
674             ts->use_irq = 1;
675         }
676         else 
677                 dev_err(&client->dev, "request_irq failed\n");
678     }
679         
680 #if ILI2102_TS_APK_SUPPORT
681         // initialize global variable
682         g_ts = ts;
683         memset(&g_dev, 0, sizeof(struct dev_data));     
684         
685         // allocate character device driver buffer
686         ret = alloc_chrdev_region(&g_dev.devno, 0, 1, ILITEK_FILE_DRIVER_NAME);
687         if(ret){
688                 printk(ILITEK_ERROR_LEVEL "%s, can't allocate chrdev\n", __func__);
689                 return ret;
690         }
691         printk(ILITEK_DEBUG_LEVEL "%s, register chrdev(%d, %d)\n", __func__, MAJOR(g_dev.devno), MINOR(g_dev.devno));
692         
693         // initialize character device driver
694         cdev_init(&g_dev.cdev, &ilitek_fops);
695         g_dev.cdev.owner = THIS_MODULE;
696         ret = cdev_add(&g_dev.cdev, g_dev.devno, 1);
697         if(ret < 0){
698                 printk(ILITEK_ERROR_LEVEL "%s, add character device error, ret %d\n", __func__, ret);
699                 return ret;
700         }
701         g_dev.class = class_create(THIS_MODULE, ILITEK_FILE_DRIVER_NAME);
702         if(IS_ERR(g_dev.class)){
703                 printk(ILITEK_ERROR_LEVEL "%s, create class, error\n", __func__);
704                 return ret;
705         }
706         device_create(g_dev.class, NULL, g_dev.devno, NULL, "ilitek_ctrl");
707 #endif
708
709 #ifdef CONFIG_HAS_EARLYSUSPEND
710         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
711         ts->early_suspend.suspend = ili2102_ts_early_suspend;
712         ts->early_suspend.resume = ili2102_ts_late_resume;
713         register_early_suspend(&ts->early_suspend);
714 #endif
715
716         printk(KERN_INFO "ili2102_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
717
718         return 0;
719
720         err_input_register_device_failed:
721         input_free_device(ts->input_dev);
722         err_resetpin_failed:
723         gpio_free(ts->gpio_reset);
724         err_input_dev_alloc_failed:
725         kfree(ts);
726         err_alloc_data_failed:
727         err_check_functionality_failed:
728         return ret;
729 }
730
731 static int ili2102_ts_remove(struct i2c_client *client)
732 {
733         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
734         unregister_early_suspend(&ts->early_suspend);
735         if (ts->use_irq)
736         free_irq(client->irq, ts);
737         else
738         hrtimer_cancel(&ts->timer);
739         input_unregister_device(ts->input_dev);
740         if (ts->ts_wq)
741         cancel_delayed_work_sync(&ts->work);
742         kfree(ts);
743         
744 #if ILI2102_TS_APK_SUPPORT
745         // delete character device driver
746         cdev_del(&g_dev.cdev);
747         unregister_chrdev_region(g_dev.devno, 1);
748         device_destroy(g_dev.class, g_dev.devno);
749         class_destroy(g_dev.class);
750 #endif
751
752         return 0;
753 }
754
755 static int ili2102_ts_suspend(struct i2c_client *client, pm_message_t mesg)
756 {
757         int ret;
758         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
759         uint8_t buf[1] = {0x30};
760         struct i2c_msg msg[1];
761         
762         if (ts->use_irq)
763         {
764                 free_irq(client->irq, ts);
765                 //change irq type to IRQF_TRIGGER_FALLING to avoid system death
766                 ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_FALLING, client->name, ts);
767             if (ret == 0) {
768                 disable_irq_nosync(client->irq);
769                 ts->use_irq = 1;
770             }
771             else 
772                 printk("%s:request irq=%d failed,ret=%d\n",__FUNCTION__, ts->client->irq, ret);
773         }
774         else
775         hrtimer_cancel(&ts->timer);
776
777         ret = cancel_delayed_work_sync(&ts->work);
778         //if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
779         //enable_irq(client->irq);
780
781         //to do suspend
782         msg[0].addr =client->addr;
783         msg[0].flags = 0;
784         msg[0].len = 1;
785         msg[0].buf = buf;
786
787         ret = i2c_transfer(client->adapter, msg, 1);
788         if (ret < 0) {
789         printk("%s:err\n",__FUNCTION__);
790         }
791         
792         DBG("%s\n",__FUNCTION__);
793         
794         return 0;
795 }
796
797
798 static void ili2102_ts_resume_work_func(struct work_struct *work)
799 {
800         struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
801         int ret;
802         mdelay(100); //wait for 100ms before i2c operation
803         free_irq(ts->client->irq, ts);
804         ret = request_irq(ts->client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, ts->client->name, ts);
805         if (ret == 0) {
806         ts->use_irq = 1;
807         //enable_irq(ts->client->irq);
808         }
809         else 
810         printk("%s:request irq=%d failed,ret=%d\n",__FUNCTION__,ts->client->irq,ret);
811
812         PREPARE_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
813         printk("%s,irq=%d\n",__FUNCTION__,ts->client->irq);
814 }
815
816
817 static int ili2102_ts_resume(struct i2c_client *client)
818 {
819     struct ili2102_ts_data *ts = i2c_get_clientdata(client);
820
821     ili2102_init_panel(ts);
822         
823     if (ts->use_irq) {
824         if(!delayed_work_pending(&ts->work)){
825                 PREPARE_DELAYED_WORK(&ts->work, ili2102_ts_resume_work_func);
826                 queue_delayed_work(ts->ts_wq, &ts->work, 0);
827         }
828     }
829     else {
830         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
831     }
832
833         DBG("%s\n",__FUNCTION__);
834
835     return 0;
836 }
837
838
839 #ifdef CONFIG_HAS_EARLYSUSPEND
840 static void ili2102_ts_early_suspend(struct early_suspend *h)
841 {
842     struct ili2102_ts_data *ts;
843     ts = container_of(h, struct ili2102_ts_data, early_suspend);
844     ili2102_ts_suspend(ts->client, PMSG_SUSPEND);
845 }
846
847 static void ili2102_ts_late_resume(struct early_suspend *h)
848 {
849     struct ili2102_ts_data *ts;
850     ts = container_of(h, struct ili2102_ts_data, early_suspend);
851     ili2102_ts_resume(ts->client);
852 }
853 #endif
854
855 #define ILI2102_TS_NAME "ili2102_ts"
856
857 static const struct i2c_device_id ili2102_ts_id[] = {
858     { ILI2102_TS_NAME, 0 },
859     { }
860 };
861
862 static struct i2c_driver ili2102_ts_driver = {
863     .probe      = ili2102_ts_probe,
864     .remove     = ili2102_ts_remove,
865 #ifndef CONFIG_HAS_EARLYSUSPEND
866     .suspend    = ili2102_ts_early_suspend,
867     .resume     = ili2102_ts_late_resume,
868 #endif
869     .id_table   = ili2102_ts_id,
870     .driver = {
871         .name   = ILI2102_TS_NAME,
872     },
873 };
874
875 static int __devinit ili2102_ts_init(void)
876 {
877     return i2c_add_driver(&ili2102_ts_driver);
878 }
879
880 static void __exit ili2102_ts_exit(void)
881 {
882         i2c_del_driver(&ili2102_ts_driver);
883 }
884
885 module_init(ili2102_ts_init);
886 module_exit(ili2102_ts_exit);
887
888 MODULE_DESCRIPTION("ili2102 Touchscreen Driver");
889 MODULE_LICENSE("GPL");