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