1. add irq enable and disable for gpio.c to support disable_irq_nosync() function;
[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_HIGH:GPIO_LOW);
289         return 0;
290 }
291
292 static void ili2102_ts_work_func(struct work_struct *work)
293 {
294         int i,ret;
295         int syn_flag = 0;
296         unsigned int x, y;
297         struct i2c_msg msg[2];
298         uint8_t start_reg;
299         uint8_t buf[9];//uint32_t buf[4];
300         struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
301
302         DBG("ili2102_ts_work_func\n");
303
304         /*Touch Information Report*/
305         start_reg = 0x10;
306
307         msg[0].addr = ts->client->addr;
308         msg[0].flags = ts->client->flags;
309         msg[0].len = 1;
310         msg[0].buf = &start_reg;
311         msg[0].scl_rate = 400*1000;
312         msg[0].udelay = 150;
313         
314         msg[1].addr = ts->client->addr;
315         msg[1].flags = ts->client->flags | I2C_M_RD;
316         msg[1].len = 9; 
317         msg[1].buf = buf;
318         msg[1].scl_rate = 400*1000;
319         msg[1].udelay = 0;
320         
321         ret = i2c_transfer(ts->client->adapter, msg, 2); 
322         if (ret < 0) 
323         {
324                 printk("%s:i2c_transfer fail, ret=%d\n",__FUNCTION__,ret);
325                 goto out;
326         }
327
328         for(i=0; i<TOUCH_NUMBER; i++)
329         {
330
331                 if(!((buf[0]>>i)&0x01))
332                 {
333                         if (touch_state[i] == TOUCH_DOWN)
334                         {
335                                 DBG("ili2102_ts_work_func:buf[%d]=%d\n",i,buf[i]);
336                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0); //Finger Size
337                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0); //Touch Size
338                                 input_mt_sync(ts->input_dev);
339                                 syn_flag = 1;
340                                 touch_state[i] = TOUCH_UP;
341                                 DBG("touch_up \n");
342                         }
343
344                 }
345                 else
346                 {
347                         if((buf[0]>>i)&0x01)
348                         {
349                                 x = buf[1+(i<<2)] | (buf[2+(i<<2)] << 8);
350                                 y = buf[3+(i<<2)] | (buf[4+(i<<2)] << 8);
351                                 
352                                 if (ts->swap_xy)
353                                 swap(x, y);
354
355                                 if (verify_coord(ts,&x,&y))//goto out;
356                                 {
357                                         printk("err:x=%d,y=%d\n",x,y);
358                                         x = g_x[i];
359                                         y = g_y[i];
360                                 }
361
362                                 g_x[i] = x;
363                                 g_y[i] = y;                     
364                                 input_event(ts->input_dev, EV_ABS, ABS_MT_TRACKING_ID, i);
365                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1); //Finger Size
366                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
367                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
368                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 5); //Touch Size
369                         input_mt_sync(ts->input_dev);
370                                 syn_flag = 1;
371                                 touch_state[i] = TOUCH_DOWN;
372                                  ts->pendown = 1;
373                                 DBG("touch_down i=%d X = %d, Y = %d\n",i, x, y);
374                         }
375                         
376                 }
377         }
378         
379         if(syn_flag)
380         input_sync(ts->input_dev);
381 out:   
382 #if 0
383         if(ts->pendown)
384         {
385                 schedule_delayed_work(&ts->work, msecs_to_jiffies(10));
386                 ts->pendown = 0;
387         }
388         else
389         {
390                 if (ts->use_irq) 
391                 enable_irq(ts->client->irq);
392         }
393 #else
394         enable_irq(ts->client->irq);//intterupt pin will be high after i2c read so could enable irq at once
395 #endif
396         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);
397
398 }
399
400 static irqreturn_t ili2102_ts_irq_handler(int irq, void *dev_id)
401 {
402         struct ili2102_ts_data *ts = dev_id;
403         DBG("ili2102_ts_irq_handler=%d,%d\n",ts->client->irq,ts->use_irq);
404
405         disable_irq_nosync(ts->client->irq); //disable_irq(ts->client->irq);
406         queue_delayed_work(ts->ts_wq, &ts->work, 0);
407         return IRQ_HANDLED;
408 }
409
410 static int __devinit setup_resetPin(struct i2c_client *client, struct ili2102_ts_data *ts)
411 {
412         struct ili2102_platform_data    *pdata = client->dev.platform_data;
413         int err;
414         
415         ts->gpio_reset = pdata->gpio_reset;
416         strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
417         ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
418         ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
419         
420         DBG("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
421
422         if (!gpio_is_valid(ts->gpio_reset)) {
423                 dev_err(&client->dev, "no gpio_reset?\n");
424                 return -EINVAL;
425         }
426
427         rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode); 
428         err = gpio_request(ts->gpio_reset, "ili2102_resetPin");
429         if (err) {
430                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
431                                 ts->gpio_reset);
432                 return err;
433         }
434         
435         //gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
436
437         err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
438         if (err) {
439                 dev_err(&client->dev, "failed to set resetPin GPIO%d\n",
440                                 ts->gpio_reset);
441                 gpio_free(ts->gpio_reset);
442                 return err;
443         }
444         
445         mdelay(5);
446
447         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
448
449         mdelay(200);
450          
451         return 0;
452 }
453
454 static int __devinit setup_pendown(struct i2c_client *client, struct ili2102_ts_data *ts)
455 {
456         int err;
457         struct ili2102_platform_data    *pdata = client->dev.platform_data;
458         
459         if (!client->irq) {
460                 dev_dbg(&client->dev, "no IRQ?\n");
461                 return -ENODEV;
462         }
463         
464         if (!gpio_is_valid(pdata->gpio_pendown)) {
465                 dev_err(&client->dev, "no gpio_pendown?\n");
466                 return -EINVAL;
467         }
468         
469         ts->gpio_pendown = pdata->gpio_pendown;
470         strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
471         ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
472         
473         DBG("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
474         
475         if (!gpio_is_valid(ts->gpio_pendown)) {
476                 dev_err(&client->dev, "no gpio_pendown?\n");
477                 return -EINVAL;
478         }
479         
480         rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode);
481         err = gpio_request(ts->gpio_pendown, "ili2102_pendown");
482         if (err) {
483                 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
484                                 ts->gpio_pendown);
485                 return err;
486         }
487         
488         err = gpio_pull_updown(ts->gpio_pendown, PullDisable);
489         if (err) {
490                 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
491                                 ts->gpio_pendown);
492                 gpio_free(ts->gpio_pendown);
493                 return err;
494         }
495         return 0;
496 }
497
498 static int ili2102_chip_Init(struct i2c_client *client)
499 {       
500         int ret = 0;
501         uint8_t start_reg;
502         uint8_t buf[6];
503         struct i2c_msg msg[2];
504         
505         /* get panel information:6bytes */
506         start_reg = 0x20;
507         msg[0].addr =client->addr;
508         msg[0].flags = client->flags;
509         msg[0].len = 1;
510         msg[0].buf = &start_reg;
511         msg[0].scl_rate = 400*1000;
512         msg[0].udelay = 200;
513
514         ret = i2c_transfer(client->adapter, msg, 1);   
515         if (ret < 0) {
516         printk("%s:err\n",__FUNCTION__);
517         }
518         
519         mdelay(5);//tp need delay
520         
521         msg[0].addr = client->addr;
522         msg[0].flags = client->flags |I2C_M_RD;
523         msg[0].len = 6;
524         msg[0].buf = (u8*)&buf[0];
525         msg[0].scl_rate = 400*1000;
526         msg[0].udelay = 200;
527
528         ret = i2c_transfer(client->adapter, msg, 1);   
529         if (ret < 0) {
530         printk("%s:err\n",__FUNCTION__);
531         }
532
533         printk("%s:max_x=%d,max_y=%d,b[4]=0x%x,b[5]=0x%x\n", 
534                 __FUNCTION__,buf[0]|(buf[1]<<8),buf[2]|(buf[3]<<8),buf[4],buf[5]);
535
536         /*get firmware version:3bytes */        
537         start_reg = 0x40;
538         msg[0].addr =client->addr;
539         msg[0].flags = client->flags;
540         msg[0].len = 1;
541         msg[0].buf = &start_reg;
542         msg[0].scl_rate = 400*1000;
543         msg[0].udelay = 200;
544
545         ret = i2c_transfer(client->adapter, msg, 1);   
546         if (ret < 0) {
547         printk("%s:err\n",__FUNCTION__);
548         }
549         
550         mdelay(5);//tp need delay
551         
552         msg[0].addr = client->addr;
553         msg[0].flags = client->flags | I2C_M_RD;
554         msg[0].len = 3;
555         msg[0].buf = (u8*)&buf[0];
556         msg[0].scl_rate =400*1000;
557         msg[0].udelay = 200;
558
559         ret = i2c_transfer(client->adapter, msg, 1);
560         if (ret < 0) {
561         printk("%s:err\n",__FUNCTION__);
562         }
563
564         printk("%s:Ver %d.%d.%d\n",__FUNCTION__,buf[0],buf[1],buf[2]);
565
566         return ret;
567     
568 }
569
570 static int ili2102_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
571 {
572         struct ili2102_ts_data *ts;
573         struct ili2102_platform_data    *pdata = client->dev.platform_data;
574         int ret = 0;
575
576         printk("ili2102 TS probe\n"); 
577     
578         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
579             printk(KERN_ERR "ili2102_ts_probe: need I2C_FUNC_I2C\n");
580             ret = -ENODEV;
581             goto err_check_functionality_failed;
582         }
583
584         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
585         if (ts == NULL) {
586             ret = -ENOMEM;
587             goto err_alloc_data_failed;
588         }
589         
590         ts->ts_wq = create_singlethread_workqueue("ts_wq");
591         if (!ts->ts_wq)
592         {
593                 printk("%s:fail to create ts_wq,ret=0x%x\n",__FUNCTION__, ENOMEM);
594                 return -ENOMEM;
595         }
596         //INIT_WORK(&ts->work, ili2102_ts_work_func);
597         INIT_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
598         ts->client = client;
599         i2c_set_clientdata(client, ts);
600
601         ret = setup_resetPin(client,ts);
602         if(ret)
603         {
604                  printk("ili2102 TS setup_resetPin fail\n");
605                  goto err_alloc_data_failed;
606         }
607
608         ret=ili2102_chip_Init(ts->client);
609         if(ret<0)
610         {
611                 printk("%s:chips init failed\n",__FUNCTION__);
612                 goto err_resetpin_failed;
613         }
614
615         /* allocate input device */
616         ts->input_dev = input_allocate_device();
617         if (ts->input_dev == NULL) {
618             ret = -ENOMEM;
619             printk(KERN_ERR "ili2102_ts_probe: Failed to allocate input device\n");
620             goto err_input_dev_alloc_failed;
621         }
622
623         ts->model = pdata->model ? : 801;
624         ts->swap_xy = pdata->swap_xy;
625         ts->x_min = pdata->x_min;
626         ts->x_max = pdata->x_max;
627         ts->y_min = pdata->y_min;
628         ts->y_max = pdata->y_max;
629         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
630         snprintf(ts->name, sizeof(ts->name), "ili%d-touchscreen", ts->model);
631         ts->input_dev->phys = ts->phys;
632         ts->input_dev->name = ts->name;
633         ts->input_dev->dev.parent = &client->dev;
634         ts->pendown = 0;
635         ts->valid_i2c_register = 1;
636
637         ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_ABS);
638         //ts->input_dev->absbit[0] = 
639                 //BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) | 
640                 //BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR);  // for android
641         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 
642                     ts->x_min ? : 0,
643                         ts->x_max ? : 480,
644                         0, 0);
645         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
646                         ts->y_min ? : 0,
647                         ts->y_max ? : 800,
648                         0, 0);
649         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0); //Finger Size
650         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0); //Touch Size
651
652         /* ts->input_dev->name = ts->keypad_info->name; */
653         ret = input_register_device(ts->input_dev);
654         if (ret) {
655             printk(KERN_ERR "ili2102_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
656             goto err_input_register_device_failed;
657         }
658
659         client->irq = gpio_to_irq(client->irq);
660         if (client->irq) 
661         {
662                 ret = setup_pendown(client,ts);
663                 if(ret)
664                 {
665                          printk("ili2102 TS setup_pendown fail\n");
666                          goto err_input_register_device_failed;
667                 }
668                 
669         ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, client->name, ts);
670         if (ret == 0) {
671             DBG("ili2102 TS register ISR (irq=%d)\n", client->irq);
672             ts->use_irq = 1;
673         }
674         else 
675                 dev_err(&client->dev, "request_irq failed\n");
676     }
677         
678 #if ILI2102_TS_APK_SUPPORT
679         // initialize global variable
680         g_ts = ts;
681         memset(&g_dev, 0, sizeof(struct dev_data));     
682         
683         // allocate character device driver buffer
684         ret = alloc_chrdev_region(&g_dev.devno, 0, 1, ILITEK_FILE_DRIVER_NAME);
685         if(ret){
686                 printk(ILITEK_ERROR_LEVEL "%s, can't allocate chrdev\n", __func__);
687                 return ret;
688         }
689         printk(ILITEK_DEBUG_LEVEL "%s, register chrdev(%d, %d)\n", __func__, MAJOR(g_dev.devno), MINOR(g_dev.devno));
690         
691         // initialize character device driver
692         cdev_init(&g_dev.cdev, &ilitek_fops);
693         g_dev.cdev.owner = THIS_MODULE;
694         ret = cdev_add(&g_dev.cdev, g_dev.devno, 1);
695         if(ret < 0){
696                 printk(ILITEK_ERROR_LEVEL "%s, add character device error, ret %d\n", __func__, ret);
697                 return ret;
698         }
699         g_dev.class = class_create(THIS_MODULE, ILITEK_FILE_DRIVER_NAME);
700         if(IS_ERR(g_dev.class)){
701                 printk(ILITEK_ERROR_LEVEL "%s, create class, error\n", __func__);
702                 return ret;
703         }
704         device_create(g_dev.class, NULL, g_dev.devno, NULL, "ilitek_ctrl");
705 #endif
706
707 #ifdef CONFIG_HAS_EARLYSUSPEND
708         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
709         ts->early_suspend.suspend = ili2102_ts_early_suspend;
710         ts->early_suspend.resume = ili2102_ts_late_resume;
711         register_early_suspend(&ts->early_suspend);
712 #endif
713
714         printk(KERN_INFO "ili2102_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
715
716         return 0;
717
718         err_input_register_device_failed:
719         input_free_device(ts->input_dev);
720         err_resetpin_failed:
721         gpio_free(ts->gpio_reset);
722         err_input_dev_alloc_failed:
723         kfree(ts);
724         err_alloc_data_failed:
725         err_check_functionality_failed:
726         return ret;
727 }
728
729 static int ili2102_ts_remove(struct i2c_client *client)
730 {
731         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
732         unregister_early_suspend(&ts->early_suspend);
733         if (ts->use_irq)
734         free_irq(client->irq, ts);
735         else
736         hrtimer_cancel(&ts->timer);
737         input_unregister_device(ts->input_dev);
738         if (ts->ts_wq)
739         cancel_delayed_work_sync(&ts->work);
740         kfree(ts);
741         
742 #if ILI2102_TS_APK_SUPPORT
743         // delete character device driver
744         cdev_del(&g_dev.cdev);
745         unregister_chrdev_region(g_dev.devno, 1);
746         device_destroy(g_dev.class, g_dev.devno);
747         class_destroy(g_dev.class);
748 #endif
749
750         return 0;
751 }
752
753 static int ili2102_ts_suspend(struct i2c_client *client, pm_message_t mesg)
754 {
755         int ret;
756         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
757         uint8_t buf[1] = {0x30};
758         struct i2c_msg msg[1];
759         
760         if (ts->use_irq)
761         {
762                 free_irq(client->irq, ts);
763                 //change irq type to IRQF_TRIGGER_FALLING to avoid system death
764                 ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_FALLING, client->name, ts);
765             if (ret == 0) {
766                 disable_irq_nosync(client->irq);
767                 ts->use_irq = 1;
768             }
769             else 
770                 printk("%s:request irq=%d failed,ret=%d\n",__FUNCTION__, ts->client->irq, ret);
771         }
772         else
773         hrtimer_cancel(&ts->timer);
774
775         ret = cancel_delayed_work_sync(&ts->work);
776         //if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
777         //enable_irq(client->irq);
778
779         //to do suspend
780         msg[0].addr =client->addr;
781         msg[0].flags = 0;
782         msg[0].len = 1;
783         msg[0].buf = buf;
784
785         ret = i2c_transfer(client->adapter, msg, 1);
786         if (ret < 0) {
787         printk("%s:err\n",__FUNCTION__);
788         }
789
790         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
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");