Revert "pixcir driver: fix a bug touchscreen cannot hold touching"
[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 #include <linux/input/mt.h>
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 long ilitek_file_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
206
207 {
208         static unsigned char buffer[64]={0};
209         static int len=0;
210         int ret;
211         struct i2c_msg msg[2];
212         
213         msg[0].addr = g_ts->client->addr;
214         msg[0].flags = g_ts->client->flags;
215         msg[0].len = len;
216         msg[0].buf = buffer;
217         msg[0].scl_rate = 400*1000;
218         msg[0].udelay = 80;
219         
220         // parsing ioctl command
221         switch(cmd){
222         case ILITEK_IOCTL_I2C_WRITE_DATA:
223                 ret = copy_from_user(buffer, (unsigned char*)arg, len);
224                 if(ret < 0){
225         printk(ILITEK_ERROR_LEVEL "%s, copy data from user space, failed\n", __func__);
226         return -1;
227         }
228                 ret = i2c_transfer(g_ts->client->adapter, msg, 1);
229                 if(ret < 0){
230                 printk(ILITEK_ERROR_LEVEL "%s, i2c write, failed\n", __func__);
231                 return -1;
232                 }
233                 break;
234                 
235         case ILITEK_IOCTL_I2C_READ_DATA:
236                 msg[0].addr = g_ts->client->addr;
237                 msg[0].flags = g_ts->client->flags | I2C_M_RD;
238                 msg[0].len = len;       
239                 msg[0].buf = buffer;
240                 msg[0].scl_rate = 400*1000;
241                 msg[0].udelay = 80;
242                 ret = i2c_transfer(g_ts->client->adapter, msg, 1);
243                 if(ret < 0){
244         printk(ILITEK_ERROR_LEVEL "%s, i2c read, failed\n", __func__);
245                 return -1;
246         }
247                 ret = copy_to_user((unsigned char*)arg, buffer, len);
248                 if(ret < 0){
249         printk(ILITEK_ERROR_LEVEL "%s, copy data to user space, failed\n", __func__);
250         return -1;
251         }
252                 break;
253         case ILITEK_IOCTL_I2C_WRITE_LENGTH:
254         case ILITEK_IOCTL_I2C_READ_LENGTH:
255                 len = arg;
256                 break;
257         case ILITEK_IOCTL_I2C_UPDATE_RESOLUTION:
258         case ILITEK_IOCTL_I2C_SET_ADDRESS:
259         case ILITEK_IOCTL_I2C_UPDATE:
260                 break;
261         case ILITEK_IOCTL_START_READ_DATA:
262                 //g_ts.stop_polling = 0;
263                 break;
264         case ILITEK_IOCTL_STOP_READ_DATA:
265                 //g_ts.stop_polling = 1;
266                 break;
267         default:
268                 return -1;
269         }
270         
271         DBG("%s:cmd=0x%x\n",__FUNCTION__,cmd);
272         
273         return 0;
274 }
275
276
277 static ssize_t ilitek_file_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
278 {
279         return 0;
280 }
281
282
283 static int ilitek_file_close(struct inode *inode, struct file *filp)
284 {
285         return 0;
286 }
287
288
289 // declare file operations
290 struct file_operations ilitek_fops = {
291         .unlocked_ioctl = ilitek_file_ioctl,    
292         .read = ilitek_file_read,
293         .write = ilitek_file_write,
294         .open = ilitek_file_open,
295         .release = ilitek_file_close,
296 };
297
298 #endif
299 static int verify_coord(struct ili2102_ts_data *ts,unsigned int *x,unsigned int *y)
300 {
301
302         //DBG("%s:(%d/%d)\n",__FUNCTION__,*x, *y);
303         #ifndef CONFIG_MACH_RK29_TD8801_V2
304         if((*x< ts->x_min) || (*x > ts->x_max))
305                 return -1;
306
307         if((*y< ts->y_min) || (*y > ts->y_max))
308                 return -1;
309     #endif
310
311         /*android do not support min and max value*/
312         if(*x == ts->x_min)
313                 *x = ts->x_min + 1;
314         if(*y == ts->y_min)
315                 *y = ts->y_min + 1;
316         if(*x == ts->x_max)
317                 *x = ts->x_max - 1;
318         if(*y == ts->y_max)
319                 *y = ts->y_max - 1;
320         
321
322         return 0;
323 }
324 static int ili2102_init_panel(struct ili2102_ts_data *ts)
325 {       
326         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
327         mdelay(1);
328         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
329         return 0;
330 }
331
332 static void ili2102_ts_work_func(struct work_struct *work)
333 {
334         int i,ret,num=1;
335         int syn_flag = 0;
336         unsigned int x, y;
337         struct i2c_msg msg[2];
338         uint8_t start_reg;
339         uint8_t buf[9];//uint32_t buf[4];
340         struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
341
342         DBG("ili2102_ts_work_func\n");
343
344         /*Touch Information Report*/
345         start_reg = 0x10;
346
347         msg[0].addr = ts->client->addr;
348         msg[0].flags = ts->client->flags;
349         msg[0].len = 1;
350         msg[0].buf = &start_reg;
351         msg[0].scl_rate = 200*1000;
352         msg[0].udelay = 200;
353         
354         msg[1].addr = ts->client->addr;
355         msg[1].flags = ts->client->flags | I2C_M_RD;
356         msg[1].len = 9; 
357         msg[1].buf = buf;
358         msg[1].scl_rate = 200*1000;
359         msg[1].udelay = 0;
360         
361         ret = i2c_transfer(ts->client->adapter, msg, 2); 
362         if (ret < 0) 
363         {
364                 printk("%s:i2c_transfer fail, ret=%d\n",__FUNCTION__,ret);
365                 goto out;
366         }
367         
368         for(i=0; i<TOUCH_NUMBER; i++)
369         {
370
371                 if(!((buf[0]>>i)&0x01))
372                 {
373                         if (touch_state[i] == TOUCH_DOWN)
374                         {
375                                 DBG("ili2102_ts_work_func:buf[%d]=%d\n",i,buf[i]);
376                                 //input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
377                                 //input_report_abs(ts->input_dev, ABS_MT_PRESSURE, 0);
378                                 input_mt_slot(ts->input_dev, i);
379                                 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
380                                 syn_flag = 1;
381                                 touch_state[i] = TOUCH_UP;
382                                 DBG("i=%d,touch_up \n",i);
383                         }
384
385                 }
386                 else
387                 {
388                         if((buf[0]>>i)&0x01)
389                         {
390                                 x = buf[1+(i<<2)] | (buf[2+(i<<2)] << 8);
391                                 y = buf[3+(i<<2)] | (buf[4+(i<<2)] << 8);
392                                 
393                                 if (ts->swap_xy)
394                                 swap(x, y);
395
396                                 if (verify_coord(ts,&x,&y))//goto out;
397                                 {
398                                         printk("err:x=%d,y=%d\n",x,y);
399                                         x = g_x[i];
400                                         y = g_y[i];
401                                 }
402                                 g_x[i] = x;
403                                 g_y[i] = y;     
404                                 
405                                 input_mt_slot(ts->input_dev, i);
406                                 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
407                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
408                                 //input_report_abs(ts->input_dev, ABS_MT_PRESSURE, 100);
409                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
410                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
411         
412                                 syn_flag = 1;
413                                 touch_state[i] = TOUCH_DOWN;
414                                  ts->pendown = 1;
415                                 DBG("touch_down i=%d X = %d, Y = %d\n",i, x, y);
416                         }
417                         
418                 }
419         }
420         
421         if(syn_flag)
422         input_sync(ts->input_dev);
423 out:   
424 #if 0
425         if(ts->pendown)
426         {
427                 schedule_delayed_work(&ts->work, msecs_to_jiffies(12));
428                 ts->pendown = 0;
429         }
430         else
431         {
432                 if (ts->use_irq) 
433                 enable_irq(ts->client->irq);
434         }
435 #else
436         enable_irq(ts->client->irq);//intterupt pin will be high after i2c read so could enable irq at once
437 #endif
438         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);
439
440 }
441
442 static irqreturn_t ili2102_ts_irq_handler(int irq, void *dev_id)
443 {
444         struct ili2102_ts_data *ts = dev_id;
445         DBG("ili2102_ts_irq_handler=%d,%d\n",ts->client->irq,ts->use_irq);
446
447         disable_irq_nosync(ts->client->irq); //disable_irq(ts->client->irq);
448         queue_delayed_work(ts->ts_wq, &ts->work, 0);
449         return IRQ_HANDLED;
450 }
451
452 static int __devinit setup_resetPin(struct i2c_client *client, struct ili2102_ts_data *ts)
453 {
454         struct ili2102_platform_data    *pdata = client->dev.platform_data;
455         int err;
456         
457         ts->gpio_reset = pdata->gpio_reset;
458         strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
459         ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
460         ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
461         
462         DBG("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
463
464         if (!gpio_is_valid(ts->gpio_reset)) {
465                 dev_err(&client->dev, "no gpio_reset?\n");
466                 return -EINVAL;
467         }
468
469         rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode); 
470         err = gpio_request(ts->gpio_reset, "ili2102_resetPin");
471         if (err) {
472                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
473                                 ts->gpio_reset);
474                 return err;
475         }
476         
477         //gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
478
479         err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
480         if (err) {
481                 dev_err(&client->dev, "failed to set resetPin GPIO%d\n",
482                                 ts->gpio_reset);
483                 gpio_free(ts->gpio_reset);
484                 return err;
485         }
486         
487         mdelay(5);
488
489         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
490
491         mdelay(200);
492          
493         return 0;
494 }
495
496 static int __devinit setup_pendown(struct i2c_client *client, struct ili2102_ts_data *ts)
497 {
498         int err;
499         struct ili2102_platform_data    *pdata = client->dev.platform_data;
500         
501         if (!client->irq) {
502                 dev_dbg(&client->dev, "no IRQ?\n");
503                 return -ENODEV;
504         }
505         
506         if (!gpio_is_valid(pdata->gpio_pendown)) {
507                 dev_err(&client->dev, "no gpio_pendown?\n");
508                 return -EINVAL;
509         }
510         
511         ts->gpio_pendown = pdata->gpio_pendown;
512         strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
513         ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
514         
515         DBG("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
516         
517         if (!gpio_is_valid(ts->gpio_pendown)) {
518                 dev_err(&client->dev, "no gpio_pendown?\n");
519                 return -EINVAL;
520         }
521         
522         rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode);
523         err = gpio_request(ts->gpio_pendown, "ili2102_pendown");
524         if (err) {
525                 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
526                                 ts->gpio_pendown);
527                 return err;
528         }
529         
530         err = gpio_pull_updown(ts->gpio_pendown, PullDisable);
531         if (err) {
532                 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
533                                 ts->gpio_pendown);
534                 gpio_free(ts->gpio_pendown);
535                 return err;
536         }
537         return 0;
538 }
539
540 static int ili2102_chip_Init(struct i2c_client *client)
541 {       
542         int ret = 0;
543         uint8_t start_reg;
544         uint8_t buf[6];
545         struct i2c_msg msg[2];
546         
547         /* get panel information:6bytes */
548         start_reg = 0x20;
549         msg[0].addr =client->addr;
550         msg[0].flags = client->flags;
551         msg[0].len = 1;
552         msg[0].buf = &start_reg;
553         msg[0].scl_rate = 400*1000;
554         msg[0].udelay = 200;
555
556         ret = i2c_transfer(client->adapter, msg, 1);   
557         if (ret < 0) {
558         printk("%s:err\n",__FUNCTION__);
559         }
560         
561         mdelay(5);//tp need delay
562         
563         msg[0].addr = client->addr;
564         msg[0].flags = client->flags |I2C_M_RD;
565         msg[0].len = 6;
566         msg[0].buf = (u8*)&buf[0];
567         msg[0].scl_rate = 400*1000;
568         msg[0].udelay = 200;
569
570         ret = i2c_transfer(client->adapter, msg, 1);   
571         if (ret < 0) {
572         printk("%s:err\n",__FUNCTION__);
573         }
574
575         printk("%s:max_x=%d,max_y=%d,b[4]=0x%x,b[5]=0x%x\n", 
576                 __FUNCTION__,buf[0]|(buf[1]<<8),buf[2]|(buf[3]<<8),buf[4],buf[5]);
577
578         /*get firmware version:3bytes */        
579         start_reg = 0x40;
580         msg[0].addr =client->addr;
581         msg[0].flags = client->flags;
582         msg[0].len = 1;
583         msg[0].buf = &start_reg;
584         msg[0].scl_rate = 400*1000;
585         msg[0].udelay = 200;
586
587         ret = i2c_transfer(client->adapter, msg, 1);   
588         if (ret < 0) {
589         printk("%s:err\n",__FUNCTION__);
590         }
591         
592         mdelay(5);//tp need delay
593         
594         msg[0].addr = client->addr;
595         msg[0].flags = client->flags | I2C_M_RD;
596         msg[0].len = 3;
597         msg[0].buf = (u8*)&buf[0];
598         msg[0].scl_rate =400*1000;
599         msg[0].udelay = 200;
600
601         ret = i2c_transfer(client->adapter, msg, 1);
602         if (ret < 0) {
603         printk("%s:err\n",__FUNCTION__);
604         }
605
606         printk("%s:Ver %d.%d.%d\n",__FUNCTION__,buf[0],buf[1],buf[2]);
607
608         return ret;
609     
610 }
611
612 static int ili2102_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
613 {
614         struct ili2102_ts_data *ts;
615         struct ili2102_platform_data    *pdata = client->dev.platform_data;
616         int ret = 0;
617
618         printk("ili2102 TS probe\n"); 
619     
620         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
621             printk(KERN_ERR "ili2102_ts_probe: need I2C_FUNC_I2C\n");
622             ret = -ENODEV;
623             goto err_check_functionality_failed;
624         }
625
626         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
627         if (ts == NULL) {
628             ret = -ENOMEM;
629             goto err_alloc_data_failed;
630         }
631         
632         ts->ts_wq = create_singlethread_workqueue("ts_wq");
633         if (!ts->ts_wq)
634         {
635                 printk("%s:fail to create ts_wq,ret=0x%x\n",__FUNCTION__, ENOMEM);
636                 return -ENOMEM;
637         }
638         //INIT_WORK(&ts->work, ili2102_ts_work_func);
639         INIT_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
640         ts->client = client;
641         i2c_set_clientdata(client, ts);
642
643         ret = setup_resetPin(client,ts);
644         if(ret)
645         {
646                  printk("ili2102 TS setup_resetPin fail\n");
647                  goto err_alloc_data_failed;
648         }
649
650         ret=ili2102_chip_Init(ts->client);
651         if(ret<0)
652         {
653                 printk("%s:chips init failed\n",__FUNCTION__);
654                 goto err_resetpin_failed;
655         }
656
657         /* allocate input device */
658         ts->input_dev = input_allocate_device();
659         if (ts->input_dev == NULL) {
660             ret = -ENOMEM;
661             printk(KERN_ERR "ili2102_ts_probe: Failed to allocate input device\n");
662             goto err_input_dev_alloc_failed;
663         }
664
665         ts->model = pdata->model ? : 801;
666         ts->swap_xy = pdata->swap_xy;
667         ts->x_min = pdata->x_min;
668         ts->x_max = pdata->x_max;
669         ts->y_min = pdata->y_min;
670         ts->y_max = pdata->y_max;
671         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
672         snprintf(ts->name, sizeof(ts->name), "ili%d-touchscreen", ts->model);
673         ts->input_dev->phys = ts->phys;
674         ts->input_dev->name = ts->name;
675         ts->input_dev->dev.parent = &client->dev;
676         ts->pendown = 0;
677         ts->valid_i2c_register = 1;
678
679         //ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_ABS);
680         //ts->input_dev->absbit[0] = 
681                 //BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) | 
682                 //BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR);  // for android
683
684         __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
685         __set_bit(EV_ABS, ts->input_dev->evbit);
686         
687         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 
688                     ts->x_min ? : 0,
689                         ts->x_max ? : 480,
690                         0, 0);
691         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
692                         ts->y_min ? : 0,
693                         ts->y_max ? : 800,
694                         0, 0);
695         input_mt_init_slots(ts->input_dev, TOUCH_NUMBER);
696         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
697         //input_set_abs_params(ts->input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
698
699         /* ts->input_dev->name = ts->keypad_info->name; */
700         ret = input_register_device(ts->input_dev);
701         if (ret) {
702             printk(KERN_ERR "ili2102_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
703             goto err_input_register_device_failed;
704         }
705
706         client->irq = gpio_to_irq(client->irq);
707         if (client->irq) 
708         {
709                 ret = setup_pendown(client,ts);
710                 if(ret)
711                 {
712                          printk("ili2102 TS setup_pendown fail\n");
713                          goto err_input_register_device_failed;
714                 }
715                 
716         ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, client->name, ts);
717         if (ret == 0) {
718             DBG("ili2102 TS register ISR (irq=%d)\n", client->irq);
719             ts->use_irq = 1;
720         }
721         else 
722                 dev_err(&client->dev, "request_irq failed\n");
723     }
724         
725 #if ILI2102_TS_APK_SUPPORT
726         // initialize global variable
727         g_ts = ts;
728         memset(&g_dev, 0, sizeof(struct dev_data));     
729         
730         // allocate character device driver buffer
731         ret = alloc_chrdev_region(&g_dev.devno, 0, 1, ILITEK_FILE_DRIVER_NAME);
732         if(ret){
733                 printk(ILITEK_ERROR_LEVEL "%s, can't allocate chrdev\n", __func__);
734                 return ret;
735         }
736         printk(ILITEK_DEBUG_LEVEL "%s, register chrdev(%d, %d)\n", __func__, MAJOR(g_dev.devno), MINOR(g_dev.devno));
737         
738         // initialize character device driver
739         cdev_init(&g_dev.cdev, &ilitek_fops);
740         g_dev.cdev.owner = THIS_MODULE;
741         ret = cdev_add(&g_dev.cdev, g_dev.devno, 1);
742         if(ret < 0){
743                 printk(ILITEK_ERROR_LEVEL "%s, add character device error, ret %d\n", __func__, ret);
744                 return ret;
745         }
746         g_dev.class = class_create(THIS_MODULE, ILITEK_FILE_DRIVER_NAME);
747         if(IS_ERR(g_dev.class)){
748                 printk(ILITEK_ERROR_LEVEL "%s, create class, error\n", __func__);
749                 return ret;
750         }
751         device_create(g_dev.class, NULL, g_dev.devno, NULL, "ilitek_ctrl");
752 #endif
753
754 #ifdef CONFIG_HAS_EARLYSUSPEND
755         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
756         ts->early_suspend.suspend = ili2102_ts_early_suspend;
757         ts->early_suspend.resume = ili2102_ts_late_resume;
758         register_early_suspend(&ts->early_suspend);
759 #endif
760                 
761         struct proc_dir_entry *ili2102_proc_entry;      
762         ili2102_proc_entry = proc_create("driver/ili2102", 0777, NULL, &ili2102_proc_fops); 
763
764         printk(KERN_INFO "ili2102_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
765
766         return 0;
767
768         err_input_register_device_failed:
769         input_free_device(ts->input_dev);
770         err_resetpin_failed:
771         gpio_free(ts->gpio_reset);
772         err_input_dev_alloc_failed:
773         kfree(ts);
774         err_alloc_data_failed:
775         err_check_functionality_failed:
776         return ret;
777 }
778
779 static int ili2102_ts_remove(struct i2c_client *client)
780 {
781         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
782         unregister_early_suspend(&ts->early_suspend);
783         if (ts->use_irq)
784         free_irq(client->irq, ts);
785         else
786         hrtimer_cancel(&ts->timer);
787         input_unregister_device(ts->input_dev);
788         if (ts->ts_wq)
789         cancel_delayed_work_sync(&ts->work);
790         kfree(ts);
791         
792 #if ILI2102_TS_APK_SUPPORT
793         // delete character device driver
794         cdev_del(&g_dev.cdev);
795         unregister_chrdev_region(g_dev.devno, 1);
796         device_destroy(g_dev.class, g_dev.devno);
797         class_destroy(g_dev.class);
798 #endif
799
800         return 0;
801 }
802
803 static int ili2102_ts_suspend(struct i2c_client *client, pm_message_t mesg)
804 {
805         int ret;
806         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
807         uint8_t buf[1] = {0x30};
808         struct i2c_msg msg[1];
809
810         //to do suspend
811         msg[0].addr =client->addr;
812         msg[0].flags = 0;
813         msg[0].len = 1;
814         msg[0].buf = buf;
815
816         ret = i2c_transfer(client->adapter, msg, 1);
817         if (ret < 0) {
818         printk("%s:err\n",__FUNCTION__);
819         }
820
821         ret = cancel_delayed_work_sync(&ts->work);
822         if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
823         enable_irq(client->irq);
824         
825         if (ts->use_irq)
826         {
827                 free_irq(client->irq, ts);
828                 //change irq type to IRQF_TRIGGER_FALLING to avoid system death
829                 ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_FALLING, client->name, ts);
830             if (ret == 0) {
831                 disable_irq_nosync(client->irq);
832                 ts->use_irq = 1;
833             }
834             else 
835                 printk("%s:request irq=%d failed,ret=%d\n",__FUNCTION__, ts->client->irq, ret);
836         }
837         else
838         hrtimer_cancel(&ts->timer);
839
840         DBG("%s\n",__FUNCTION__);
841         
842         return 0;
843 }
844
845
846 static void ili2102_ts_resume_work_func(struct work_struct *work)
847 {
848         struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
849         int ret,i;
850
851         PREPARE_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
852         mdelay(100); //wait for 100ms before i2c operation
853         
854         free_irq(ts->client->irq, ts);
855         ret = request_irq(ts->client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, ts->client->name, ts);
856         if (ret == 0) {
857         ts->use_irq = 1;
858         //enable_irq(ts->client->irq);
859         }
860         else 
861         printk("%s:request irq=%d failed,ret=%d\n",__FUNCTION__,ts->client->irq,ret);
862
863         DBG("%s,irq=%d\n",__FUNCTION__,ts->client->irq);
864 }
865
866
867 static int ili2102_ts_resume(struct i2c_client *client)
868 {
869     struct ili2102_ts_data *ts = i2c_get_clientdata(client);
870
871     ili2102_init_panel(ts);
872         
873     if (ts->use_irq) {
874         if(!delayed_work_pending(&ts->work)){
875                 PREPARE_DELAYED_WORK(&ts->work, ili2102_ts_resume_work_func);
876                 queue_delayed_work(ts->ts_wq, &ts->work, 0);
877         }
878     }
879     else {
880         hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
881     }
882
883         DBG("%s\n",__FUNCTION__);
884
885     return 0;
886 }
887
888
889 #ifdef CONFIG_HAS_EARLYSUSPEND
890 static void ili2102_ts_early_suspend(struct early_suspend *h)
891 {
892     struct ili2102_ts_data *ts;
893     ts = container_of(h, struct ili2102_ts_data, early_suspend);
894     ili2102_ts_suspend(ts->client, PMSG_SUSPEND);
895 }
896
897 static void ili2102_ts_late_resume(struct early_suspend *h)
898 {
899     struct ili2102_ts_data *ts;
900     ts = container_of(h, struct ili2102_ts_data, early_suspend);
901     ili2102_ts_resume(ts->client);
902 }
903 #endif
904
905 #define ILI2102_TS_NAME "ili2102_ts"
906
907 static const struct i2c_device_id ili2102_ts_id[] = {
908     { ILI2102_TS_NAME, 0 },
909     { }
910 };
911
912 static struct i2c_driver ili2102_ts_driver = {
913     .probe      = ili2102_ts_probe,
914     .remove     = ili2102_ts_remove,
915 #ifndef CONFIG_HAS_EARLYSUSPEND
916     .suspend    = ili2102_ts_early_suspend,
917     .resume     = ili2102_ts_late_resume,
918 #endif
919     .id_table   = ili2102_ts_id,
920     .driver = {
921         .name   = ILI2102_TS_NAME,
922     },
923 };
924
925 static int __devinit ili2102_ts_init(void)
926 {
927     return i2c_add_driver(&ili2102_ts_driver);
928 }
929
930 static void __exit ili2102_ts_exit(void)
931 {
932         i2c_del_driver(&ili2102_ts_driver);
933 }
934
935 module_init(ili2102_ts_init);
936 module_exit(ili2102_ts_exit);
937
938 MODULE_DESCRIPTION("ili2102 Touchscreen Driver");
939 MODULE_LICENSE("GPL");