ts: rk29_i2c_goodix: goodix_init_panel retry write cfg info when error
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / eeti_egalax_i2c.c
1 /*
2  *
3  * Touch Screen I2C Driver for EETI Controller
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 // Release Date: 2010/11/08
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/interrupt.h>
23 #include <linux/wait.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/freezer.h>
27 #include <linux/proc_fs.h>
28 #include <linux/clk.h>
29 #include <linux/i2c.h>
30 #include <linux/gpio.h>
31 #include <linux/device.h>
32 #include <linux/cdev.h>
33 #include <asm/io.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <linux/poll.h>
37 #include <linux/kfifo.h>
38 #include <linux/version.h>
39 #include <linux/input.h>
40 #include <linux/input/mt.h>
41 #include <linux/irq.h>
42 #include <linux/async.h>
43 #include <mach/board.h>
44
45 #ifdef CONFIG_HAS_EARLYSUSPEND
46 #include <linux/earlysuspend.h>
47 #endif
48
49 //#define DEBUG
50 #ifdef CONFIG_EETI_EGALAX_DEBUG
51         #define TS_DEBUG(fmt,args...)  printk( KERN_DEBUG "[egalax_i2c]: " fmt, ## args)
52         #define DBG() printk("[%s]:%d => \n",__FUNCTION__,__LINE__)
53 #else
54         #define TS_DEBUG(fmt,args...)
55         #define DBG()
56 #endif
57
58 //#define _NON_INPUT_DEV // define this to disable register input device        
59
60 static int global_major = 0; // dynamic major by default 
61 static int global_minor = 0;
62 #define EETI_I2C_RATE   (200*1000)
63 #define MAX_I2C_LEN             10
64 #define FIFO_SIZE               PAGE_SIZE
65 #define MAX_SUPPORT_POINT       2
66 #define REPORTID_MOUSE          0x01
67 #define REPORTID_VENDOR         0x03
68 #define REPORTID_MTOUCH         0x04
69
70 /// ioctl command ///
71 #define EGALAX_IOC_MAGIC        0x72
72 #define EGALAX_IOCWAKEUP        _IO(EGALAX_IOC_MAGIC, 1)
73 #define EGALAX_IOC_MAXNR        1
74
75 #define EETI_EARLYSUSPEND_LEVEL 151
76
77 struct point_data {
78         short Status;
79         short X;
80         short Y;
81 };
82
83 struct _egalax_i2c {
84         struct workqueue_struct *ktouch_wq;
85         struct work_struct work;
86         struct mutex mutex_wq;
87         struct i2c_client *client;
88         char work_state;
89         char skip_packet;
90         int irq;
91 };
92
93 #ifdef CONFIG_HAS_EARLYSUSPEND 
94 struct suspend_info {
95         struct early_suspend early_suspend;
96         struct _egalax_i2c *egalax_i2c;
97 };
98 #endif
99
100 struct egalax_char_dev
101 {
102         int OpenCnts;
103         struct cdev cdev;
104 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
105         struct kfifo* pDataKFiFo;
106 #else
107         struct kfifo DataKFiFo;
108 #endif
109         unsigned char *pFiFoBuf;
110         spinlock_t FiFoLock;
111         struct semaphore sem;
112         wait_queue_head_t fifo_inq;
113 };
114
115 static struct _egalax_i2c *p_egalax_i2c_dev = NULL;     // allocated in egalax_i2c_probe
116 static struct egalax_char_dev *p_char_dev = NULL;       // allocated in init_module
117 static atomic_t egalax_char_available = ATOMIC_INIT(1);
118 static struct class *egalax_class;
119 #ifndef _NON_INPUT_DEV
120 static struct input_dev *input_dev = NULL;
121 static struct point_data PointBuf[MAX_SUPPORT_POINT];
122 #endif //#ifndef _NON_INPUT_DEV
123
124 static int egalax_cdev_open(struct inode *inode, struct file *filp)
125 {
126         struct egalax_char_dev *cdev;
127
128         DBG();
129
130         cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
131         if( cdev == NULL )
132         {
133                 TS_DEBUG(" No such char device node \n");
134                 return -ENODEV;
135         }
136         
137         if( !atomic_dec_and_test(&egalax_char_available) )
138         {
139                 atomic_inc(&egalax_char_available);
140                 return -EBUSY; /* already open */
141         }
142
143         cdev->OpenCnts++;
144         filp->private_data = cdev;// Used by the read and write metheds
145
146         TS_DEBUG(" egalax_cdev_open done \n");
147         try_module_get(THIS_MODULE);
148         return 0;
149 }
150
151 static int egalax_cdev_release(struct inode *inode, struct file *filp)
152 {
153         struct egalax_char_dev *cdev; // device information
154
155         DBG();
156
157         cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
158         if( cdev == NULL )
159         {
160                 TS_DEBUG(" No such char device node \n");
161                 return -ENODEV;
162         }
163
164         atomic_inc(&egalax_char_available); /* release the device */
165
166         filp->private_data = NULL;
167         cdev->OpenCnts--;
168 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
169         kfifo_reset( cdev->pDataKFiFo );
170 #else
171         kfifo_reset( &cdev->DataKFiFo );
172 #endif
173
174         TS_DEBUG(" egalax_cdev_release done \n");
175         module_put(THIS_MODULE);
176         return 0;
177 }
178
179 #define MAX_READ_BUF_LEN        50
180 static char fifo_read_buf[MAX_READ_BUF_LEN];
181 static ssize_t egalax_cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
182 {
183         int read_cnt, ret, fifoLen;
184         struct egalax_char_dev *cdev = file->private_data;
185
186         DBG();
187         
188         if( down_interruptible(&cdev->sem) )
189                 return -ERESTARTSYS;
190
191 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
192         fifoLen = kfifo_len(cdev->pDataKFiFo);
193 #else
194         fifoLen = kfifo_len(&cdev->DataKFiFo);
195 #endif
196
197         while( fifoLen<1 ) /* nothing to read */
198         {
199                 up(&cdev->sem); /* release the lock */
200                 if( file->f_flags & O_NONBLOCK )
201                         return -EAGAIN;
202
203         #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
204                 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( cdev->pDataKFiFo )>0) )
205         #else
206                 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( &cdev->DataKFiFo )>0) )
207         #endif
208                 {
209                         return -ERESTARTSYS; /* signal: tell the fs layer to handle it */
210                 }
211
212                 if( down_interruptible(&cdev->sem) )
213                         return -ERESTARTSYS;
214         }
215
216         if(count > MAX_READ_BUF_LEN)
217                 count = MAX_READ_BUF_LEN;
218
219         TS_DEBUG("\"%s\" reading: real fifo data\n", current->comm);
220 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
221         read_cnt = kfifo_get(cdev->pDataKFiFo, fifo_read_buf, count);
222 #else
223         read_cnt = kfifo_out_locked(&cdev->DataKFiFo, fifo_read_buf, count, &cdev->FiFoLock);
224 #endif
225
226         ret = copy_to_user(buf, fifo_read_buf, read_cnt)?-EFAULT:read_cnt;
227
228         up(&cdev->sem);
229         
230         return ret;
231 }
232
233 static ssize_t egalax_cdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
234 {
235         char *tmp;
236         struct egalax_char_dev *cdev = file->private_data;
237         int ret=0;
238
239         DBG();
240
241         if( down_interruptible(&cdev->sem) )
242                 return -ERESTARTSYS;
243
244         if (count > MAX_I2C_LEN)
245                 count = MAX_I2C_LEN;
246
247         tmp = kmalloc(count,GFP_KERNEL);
248         if(tmp==NULL)
249         {
250                 up(&cdev->sem);
251                 return -ENOMEM;
252         }
253
254         if(copy_from_user(tmp, buf, count))
255         {
256                 up(&cdev->sem);
257                 kfree(tmp);
258                 return -EFAULT;
259         }
260         
261         ret = i2c_master_normal_send(p_egalax_i2c_dev->client, tmp, count,EETI_I2C_RATE);
262         TS_DEBUG("I2C writing %zu bytes.\n", count);
263
264         kfree(tmp);
265
266         up(&cdev->sem);
267
268         return ret;
269 }
270
271 static int wakeup_controller(int gpio)
272 {
273         int ret=0, i;
274
275         gpio_free(gpio);
276
277         if( (ret=gpio_request(gpio, "Touch Wakeup GPIO"))!=0 )
278         {
279                 printk(KERN_ERR "[egalax_i2c]: Failed to request GPIO for Touch Wakeup GPIO. Err:%d\n", ret);
280                 ret = -EFAULT;
281         }
282         else
283         {
284                 gpio_direction_output(gpio, 0);
285                 for(i=0; i<100; i++);
286                 gpio_direction_input(gpio);
287                 printk(KERN_ERR "[egalax_i2c]: INT wakeup touch controller done\n");
288         }
289         
290         return ret;
291 }
292
293 static long egalax_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long args)
294 {       
295         //struct egalax_char_dev *cdev = file->private_data;
296         int ret=0;
297
298         if(_IOC_TYPE(cmd) != EGALAX_IOC_MAGIC)
299                 return -ENOTTY;
300         if(_IOC_NR(cmd) > EGALAX_IOC_MAXNR)
301                 return -ENOTTY;
302
303         if(_IOC_DIR(cmd) & _IOC_READ)
304                 ret = !access_ok(VERIFY_WRITE, (void __user*)args, _IOC_SIZE(cmd));
305         else if(_IOC_DIR(cmd) & _IOC_WRITE)
306                 ret = !access_ok(VERIFY_READ, (void __user*)args, _IOC_SIZE(cmd));
307
308         if(ret)
309                 return -EFAULT;
310
311         //printk(KERN_ERR "Handle device ioctl command\n");
312         switch (cmd)
313         {
314                 case EGALAX_IOCWAKEUP:
315                         ret = wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
316                         break;
317                 default:
318                         ret = -ENOTTY;
319                         break;
320         }
321
322         return ret;
323 }
324
325 static unsigned int egalax_cdev_poll(struct file *filp, struct poll_table_struct *wait)
326 {
327         struct egalax_char_dev *cdev = filp->private_data;
328         unsigned int mask = 0;
329         int fifoLen;
330         
331         down(&cdev->sem);
332         poll_wait(filp, &cdev->fifo_inq,  wait);
333
334 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
335         fifoLen = kfifo_len(cdev->pDataKFiFo);
336 #else
337         fifoLen = kfifo_len(&cdev->DataKFiFo);
338 #endif
339
340         if( fifoLen > 0 )
341                 mask |= POLLIN | POLLRDNORM;    /* readable */
342         if( (FIFO_SIZE - fifoLen) > MAX_I2C_LEN )
343                 mask |= POLLOUT | POLLWRNORM;   /* writable */
344
345         up(&cdev->sem);
346         return mask;
347 }
348
349 #ifndef _NON_INPUT_DEV
350 static int LastUpdateID = 0;
351 static void ProcessReport(unsigned char *buf, int buflen)
352 {
353         int i;
354         short X=0, Y=0, ContactID=0, Status=0;
355         if(buflen!=MAX_I2C_LEN || buf[0]!=0x04) // check buffer len & header
356                 return;
357
358         Status = buf[1]&0x01;
359         ContactID = (buf[1]&0x7C)>>2;
360         X = ((buf[3]<<8) + buf[2])>>4;
361         Y = ((buf[5]<<8) + buf[4])>>4;
362         
363         PointBuf[ContactID].Status = Status;
364         PointBuf[ContactID].X = X;
365         PointBuf[ContactID].Y = Y;
366
367         TS_DEBUG("Get Point[%d] Update: Status=%d X=%d Y=%d\n", ContactID, Status, X, Y);
368
369         // Send point report
370         if( !Status || (ContactID <= LastUpdateID) )
371         {
372                 for(i=0; i<MAX_SUPPORT_POINT;i++)
373                 {
374                         if(PointBuf[i].Status > 0)
375                         {
376                                 input_mt_slot(input_dev, i);
377                                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, true);
378                                 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, PointBuf[i].Status);
379                                 input_report_abs(input_dev, ABS_MT_POSITION_X, PointBuf[i].X);
380                                 input_report_abs(input_dev, ABS_MT_POSITION_Y, PointBuf[i].Y);
381                                 PointBuf[i].Status = 0;
382                         }
383                         else if (PointBuf[i].Status == 0)
384                         {
385                                 input_mt_slot(input_dev, i);
386                                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
387                                 PointBuf[i].Status = -1;
388                         }
389                 }
390                 input_sync(input_dev);
391                 TS_DEBUG("Input sync point data done!\n");
392         }
393
394         LastUpdateID = ContactID;
395 }
396
397 static struct input_dev * allocate_Input_Dev(void)
398 {
399         int ret;
400         struct input_dev *pInputDev=NULL;
401
402         pInputDev = input_allocate_device();
403         if(pInputDev == NULL)
404         {
405                 TS_DEBUG("Failed to allocate input device\n");
406                 return NULL;//-ENOMEM;
407         }
408
409         pInputDev->name = "eGalax Touch Screen";
410         pInputDev->phys = "I2C";
411         pInputDev->id.bustype = BUS_I2C;
412         pInputDev->id.vendor = 0x0EEF;
413         pInputDev->id.product = 0x0020;
414
415         __set_bit(INPUT_PROP_DIRECT, pInputDev->propbit);
416         __set_bit(EV_ABS, pInputDev->evbit);
417
418         input_mt_init_slots(pInputDev, MAX_SUPPORT_POINT);
419         input_set_abs_params(pInputDev, ABS_MT_POSITION_X, 0, CONFIG_EETI_EGALAX_MAX_X, 0, 0);
420         input_set_abs_params(pInputDev, ABS_MT_POSITION_Y, 0, CONFIG_EETI_EGALAX_MAX_Y, 0, 0);
421         input_set_abs_params(pInputDev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
422
423         ret = input_register_device(pInputDev);
424         if(ret) 
425         {
426                 TS_DEBUG("Unable to register input device.\n");
427                 input_free_device(pInputDev);
428                 return NULL;
429         }
430         
431         return pInputDev;
432 }
433 #endif //#ifndef _NON_INPUT_DEV
434
435 static int egalax_i2c_measure(struct i2c_client *client, char skip_packet)
436 {
437         u8 x_buf[MAX_I2C_LEN];
438         int count, loop=3;
439         DBG();
440         do{
441                 count = i2c_master_normal_recv(client, x_buf, MAX_I2C_LEN,EETI_I2C_RATE);
442         }while(count==EAGAIN && --loop);
443
444         if( count<0 || (x_buf[0]!=REPORTID_VENDOR && x_buf[0]!=REPORTID_MTOUCH) )
445         {
446                 TS_DEBUG("I2C read error data with Len=%d hedaer=%d\n", count, x_buf[0]);
447                 return -1;
448         }
449
450         TS_DEBUG("egalax_i2c read data with Len=%d\n", count);
451         if(x_buf[0]==REPORTID_VENDOR)
452                 TS_DEBUG("egalax_i2c get command packet\n");
453
454         if( skip_packet > 0 )
455                 return count;
456
457 #ifndef _NON_INPUT_DEV
458         if( count>0 && x_buf[0]==REPORTID_MTOUCH )
459         {
460                 ProcessReport(x_buf, count);
461
462                 return count;
463         }
464 #endif //#ifndef _NON_INPUT_DEV
465
466         if( count>0 && p_char_dev->OpenCnts>0 ) // If someone reading now! put the data into the buffer!
467         {
468         #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
469                 kfifo_put(p_char_dev->pDataKFiFo, x_buf, count);
470         #else
471                 kfifo_in_locked(&p_char_dev->DataKFiFo, x_buf, count, &p_char_dev->FiFoLock);
472         #endif
473                 wake_up_interruptible( &p_char_dev->fifo_inq );
474         }
475
476         return count;
477 }
478
479 static void egalax_i2c_wq(struct work_struct *work)
480 {
481         struct _egalax_i2c *egalax_i2c = container_of(work, struct _egalax_i2c, work);
482         struct i2c_client *client = egalax_i2c->client;
483         int gpio = client->irq;
484         TS_DEBUG("egalax_i2c_wq run\n");
485         mutex_lock(&egalax_i2c->mutex_wq);
486
487         /*continue recv data*/
488         while( !gpio_get_value(gpio) && egalax_i2c->work_state>0 )
489         {
490                 egalax_i2c_measure(client, egalax_i2c->skip_packet);
491                 schedule_timeout_interruptible(HZ/100);
492         }
493
494 #ifndef _NON_INPUT_DEV
495         if (gpio_get_value(gpio) && egalax_i2c->work_state > 0 && !egalax_i2c->skip_packet) {
496                 int i;
497                 for(i = 0; i < MAX_SUPPORT_POINT; i++) {
498                         if (PointBuf[i].Status > 0) {
499                                 TS_DEBUG("point %d still down\n", i);
500                                 input_mt_slot(input_dev, i);
501                                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, true);
502                                 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, 0);
503                                 input_report_abs(input_dev, ABS_MT_POSITION_X, PointBuf[i].X);
504                                 input_report_abs(input_dev, ABS_MT_POSITION_Y, PointBuf[i].Y);
505                                 PointBuf[i].Status = 0;
506                         }
507                         else if (PointBuf[i].Status == 0)
508                         {
509                                 input_mt_slot(input_dev, i);
510                                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
511                                 PointBuf[i].Status = -1;
512                         }
513                 }
514                 input_sync(input_dev);
515         }
516 #endif
517
518         if( egalax_i2c->skip_packet > 0 )
519                 egalax_i2c->skip_packet = 0;
520
521         mutex_unlock(&egalax_i2c->mutex_wq);
522
523         enable_irq(p_egalax_i2c_dev->irq);
524
525         TS_DEBUG("egalax_i2c_wq leave\n");
526 }
527
528 static irqreturn_t egalax_i2c_interrupt(int irq, void *dev_id)
529 {
530         struct _egalax_i2c *egalax_i2c = (struct _egalax_i2c *)dev_id;
531         TS_DEBUG("egalax_i2c_interrupt with irq:%d\n", irq);
532         disable_irq_nosync(irq);
533         queue_work(egalax_i2c->ktouch_wq, &egalax_i2c->work);
534
535         return IRQ_HANDLED;
536 }
537
538
539 void egalax_i2c_set_standby(struct i2c_client *client, int enable)
540 {
541         struct eeti_egalax_platform_data *mach_info = client->dev.platform_data;
542         unsigned display_on = mach_info->disp_on_pin;
543         unsigned lcd_standby = mach_info->standby_pin;
544
545         int display_on_pol = mach_info->disp_on_value;
546         int lcd_standby_pol = mach_info->standby_value;
547
548         printk("%s : %s, enable = %d", __FILE__, __FUNCTION__,enable);
549     if(display_on != INVALID_GPIO)
550     {
551         gpio_direction_output(display_on, 0);
552         gpio_set_value(display_on, enable ? display_on_pol : !display_on_pol);                          
553     }
554     if(lcd_standby != INVALID_GPIO)
555     {
556         gpio_direction_output(lcd_standby, 0);
557         gpio_set_value(lcd_standby, enable ? lcd_standby_pol : !lcd_standby_pol);                         
558     }
559 }
560
561 #ifdef CONFIG_PM
562 static int egalax_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
563 {
564         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
565         u8 cmdbuf[MAX_I2C_LEN]={0x03, 0x05, 0x0A, 0x03, 0x36, 0x3F, 0x02, 0, 0, 0};
566         
567         i2c_master_normal_send(client, cmdbuf, MAX_I2C_LEN, EETI_I2C_RATE);
568
569         disable_irq(p_egalax_i2c_dev->irq);
570         egalax_i2c->work_state = 0;
571         if (cancel_work_sync(&egalax_i2c->work)) {
572                 /* if work was pending disable-count is now 2 */
573                 pr_info("%s: work was pending\n", __func__);
574                 enable_irq(p_egalax_i2c_dev->irq);
575         }
576
577         printk(KERN_DEBUG "[egalax_i2c]: device suspend done\n");       
578
579         if(device_may_wakeup(&client->dev)) 
580         {
581                 enable_irq_wake(p_egalax_i2c_dev->irq);
582         }
583         else 
584         {
585                 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
586         }
587         egalax_i2c_set_standby(client, 0);
588         return 0;
589 }
590
591 static int egalax_i2c_resume(struct i2c_client *client)
592 {
593         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
594         egalax_i2c_set_standby(client, 1);      
595         if(device_may_wakeup(&client->dev)) 
596         {
597                 disable_irq_wake(p_egalax_i2c_dev->irq);
598         }
599         else 
600         {
601                 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
602         }
603
604         wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
605         egalax_i2c->work_state = 1;
606         enable_irq(p_egalax_i2c_dev->irq);
607
608         printk(KERN_DEBUG "[egalax_i2c]: device wakeup done\n");
609
610         return 0;
611 }
612
613 #ifdef CONFIG_HAS_EARLYSUSPEND 
614
615 static void egalax_i2c_early_suspend(struct early_suspend *h)
616 {
617         pm_message_t mesg = {.event = 0};
618         struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
619         struct i2c_client *client = info->egalax_i2c->client;
620         egalax_i2c_suspend(client,mesg);
621
622 }
623 static void egalax_i2c_early_resume(struct early_suspend *h)
624 {
625    
626     struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
627     struct i2c_client *client = info->egalax_i2c->client;
628
629     egalax_i2c_resume(client);
630
631 }
632
633 #endif
634
635
636
637 #else
638 #define egalax_i2c_suspend       NULL
639 #define egalax_i2c_resume        NULL
640 #endif
641
642 #ifdef CONFIG_HAS_EARLYSUSPEND 
643 static struct suspend_info suspend_info = {
644         .early_suspend.suspend = egalax_i2c_early_suspend,
645         .early_suspend.resume = egalax_i2c_early_resume,
646         .early_suspend.level = EETI_EARLYSUSPEND_LEVEL,
647 };
648 #endif
649
650
651 static int __devinit egalax_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
652 {
653         int ret;
654         int gpio = client->irq;
655         struct eeti_egalax_platform_data *pdata = pdata = client->dev.platform_data;
656
657         printk(KERN_DEBUG "[egalax_i2c]: start probe\n");
658
659         p_egalax_i2c_dev = (struct _egalax_i2c *)kzalloc(sizeof(struct _egalax_i2c), GFP_KERNEL);
660         if (!p_egalax_i2c_dev) 
661         {
662                 printk(KERN_ERR "[egalax_i2c]: request memory failed\n");
663                 ret = -ENOMEM;
664                 goto fail1;
665         }
666
667 #ifndef _NON_INPUT_DEV
668         input_dev = allocate_Input_Dev();
669         if(input_dev==NULL)
670         {
671                 printk(KERN_ERR "[egalax_i2c]: allocate_Input_Dev failed\n");
672                 ret = -EINVAL; 
673                 goto fail2;
674         }
675         TS_DEBUG("egalax_i2c register input device done\n");
676         memset(PointBuf, 0, sizeof(struct point_data)*MAX_SUPPORT_POINT);
677 #endif //#ifndef _NON_INPUT_DEV
678
679 if (pdata->init_platform_hw)
680                 pdata->init_platform_hw();
681
682         p_egalax_i2c_dev->client = client;
683         mutex_init(&p_egalax_i2c_dev->mutex_wq);
684
685         p_egalax_i2c_dev->ktouch_wq = create_workqueue("egalax_touch_wq"); 
686         INIT_WORK(&p_egalax_i2c_dev->work, egalax_i2c_wq);
687         
688         i2c_set_clientdata(client, p_egalax_i2c_dev);
689
690         if( gpio_get_value(gpio) )
691                 p_egalax_i2c_dev->skip_packet = 0;
692         else
693                 p_egalax_i2c_dev->skip_packet = 1;
694
695         p_egalax_i2c_dev->work_state = 1;
696         
697         p_egalax_i2c_dev->irq = gpio_to_irq(client->irq);
698         
699         ret = request_irq(p_egalax_i2c_dev->irq, egalax_i2c_interrupt, IRQF_TRIGGER_LOW,
700                  client->name, p_egalax_i2c_dev);
701         if( ret ) 
702         {
703                 printk(KERN_ERR "[egalax_i2c]: request irq(%d) failed\n", p_egalax_i2c_dev->irq);
704                 goto fail3;
705         }
706         TS_DEBUG("egalax_i2c request irq(%d) gpio(%d) with result:%d\n", p_egalax_i2c_dev->irq, gpio, ret);
707
708 #ifdef CONFIG_PM
709         device_init_wakeup(&client->dev, 0);
710 #endif
711
712 #ifdef CONFIG_HAS_EARLYSUSPEND
713
714
715         suspend_info.egalax_i2c = p_egalax_i2c_dev;
716         register_early_suspend(&suspend_info.early_suspend);
717 #endif
718
719         printk(KERN_DEBUG "[egalax_i2c]: probe done\n");
720         return 0;
721
722 fail3:
723         i2c_set_clientdata(client, NULL);
724         destroy_workqueue(p_egalax_i2c_dev->ktouch_wq); 
725         free_irq(p_egalax_i2c_dev->irq, p_egalax_i2c_dev);
726 #ifndef _NON_INPUT_DEV
727         input_unregister_device(input_dev);
728         input_free_device(input_dev);
729         input_dev = NULL;
730 #endif //#ifndef _NON_INPUT_DEV
731 fail2:
732 fail1:
733         kfree(p_egalax_i2c_dev);
734         p_egalax_i2c_dev = NULL;
735
736         printk(KERN_DEBUG "[egalax_i2c]: probe failed\n");
737         return ret;
738 }
739
740 static int __devexit egalax_i2c_remove(struct i2c_client *client)
741 {
742         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
743
744         DBG();
745
746 #ifndef _NON_INPUT_DEV
747         if(input_dev)
748         {
749                 TS_DEBUG("unregister input device\n");
750                 input_unregister_device(input_dev);
751                 input_free_device(input_dev);
752                 input_dev = NULL;
753         }
754 #endif //#ifndef _NON_INPUT_DEV
755
756         if(p_egalax_i2c_dev->ktouch_wq) 
757         {
758                 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq); 
759         }
760
761         if(p_egalax_i2c_dev->irq)
762         {
763                 free_irq(p_egalax_i2c_dev->irq, egalax_i2c);
764         }
765
766         i2c_set_clientdata(client, NULL);
767         kfree(egalax_i2c);
768         p_egalax_i2c_dev = NULL;
769
770         return 0;
771 }
772
773 static struct i2c_device_id egalax_i2c_idtable[] = { 
774         { "egalax_i2c", 0 }, 
775         { } 
776 }; 
777
778 MODULE_DEVICE_TABLE(i2c, egalax_i2c_idtable);
779
780 static struct i2c_driver egalax_i2c_driver = {
781         .driver = {
782                 .name   = "egalax_i2c",
783         },
784         .id_table       = egalax_i2c_idtable,
785         .probe          = egalax_i2c_probe,
786         .remove         = __devexit_p(egalax_i2c_remove),
787         //.suspend      = egalax_i2c_suspend,
788         //.resume               = egalax_i2c_resume,
789 };
790
791 static const struct file_operations egalax_cdev_fops = {
792         .owner  = THIS_MODULE,
793         .read   = egalax_cdev_read,
794         .write  = egalax_cdev_write,
795         .unlocked_ioctl = egalax_cdev_ioctl,
796         .poll   = egalax_cdev_poll,
797         .open   = egalax_cdev_open,
798         .release= egalax_cdev_release,
799 };
800
801 static void egalax_i2c_ts_exit(void)
802 {
803         dev_t devno = MKDEV(global_major, global_minor);
804         DBG();
805
806         if(p_char_dev)
807         {
808                 TS_DEBUG("unregister character device\n");
809                 if( p_char_dev->pFiFoBuf )
810                         kfree(p_char_dev->pFiFoBuf);
811         
812                 cdev_del(&p_char_dev->cdev);
813                 kfree(p_char_dev);
814                 p_char_dev = NULL;
815         }
816
817         unregister_chrdev_region( devno, 1);
818
819         if(!IS_ERR(egalax_class))
820         {
821 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
822                 class_device_destroy(egalax_class, devno);
823 #else
824                 device_destroy(egalax_class, devno);
825 #endif 
826                 class_destroy(egalax_class);
827         }
828         
829         i2c_del_driver(&egalax_i2c_driver);
830
831         printk(KERN_DEBUG "[egalax_i2c]: driver exit\n");
832 }
833
834 static struct egalax_char_dev* setup_chardev(dev_t dev)
835 {
836         struct egalax_char_dev *pCharDev;
837         int result;
838
839         pCharDev = kmalloc(1*sizeof(struct egalax_char_dev), GFP_KERNEL);
840         if(!pCharDev) 
841                 goto fail_cdev;
842         memset(pCharDev, 0, sizeof(struct egalax_char_dev));
843
844         spin_lock_init( &pCharDev->FiFoLock );
845         pCharDev->pFiFoBuf = kmalloc(sizeof(unsigned char)*FIFO_SIZE, GFP_KERNEL);
846         if(!pCharDev->pFiFoBuf)
847                 goto fail_fifobuf;
848         memset(pCharDev->pFiFoBuf, 0, sizeof(unsigned char)*FIFO_SIZE);
849
850 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
851         pCharDev->pDataKFiFo = kfifo_init(pCharDev->pFiFoBuf, FIFO_SIZE, GFP_KERNEL, &pCharDev->FiFoLock);
852         if( pCharDev->pDataKFiFo==NULL )
853                 goto fail_kfifo;
854 #else
855         kfifo_init(&pCharDev->DataKFiFo, pCharDev->pFiFoBuf, FIFO_SIZE);
856         if( !kfifo_initialized(&pCharDev->DataKFiFo) )
857                 goto fail_kfifo;
858 #endif
859         
860         pCharDev->OpenCnts = 0;
861         cdev_init(&pCharDev->cdev, &egalax_cdev_fops);
862         pCharDev->cdev.owner = THIS_MODULE;
863         sema_init(&pCharDev->sem, 1);
864         init_waitqueue_head(&pCharDev->fifo_inq);
865
866         result = cdev_add(&pCharDev->cdev, dev, 1);
867         if(result)
868         {
869                 TS_DEBUG(KERN_ERR "Error cdev ioctldev added\n");
870                 goto fail_kfifo;
871         }
872
873         return pCharDev; 
874
875 fail_kfifo:
876         kfree(pCharDev->pFiFoBuf);
877 fail_fifobuf:
878         kfree(pCharDev);
879 fail_cdev:
880         return NULL;
881 }
882
883 static void __init egalax_i2c_ts_init_async(void *unused, async_cookie_t cookie)
884 {
885         int result;
886         dev_t devno = 0;
887
888         DBG();
889
890         // Asking for a dynamic major unless directed otherwise at load time.
891         if(global_major) 
892         {
893                 devno = MKDEV(global_major, global_minor);
894                 result = register_chrdev_region(devno, 1, "egalax_i2c");
895         } 
896         else 
897         {
898                 result = alloc_chrdev_region(&devno, global_minor, 1, "egalax_i2c");
899                 global_major = MAJOR(devno);
900         }
901
902         if (result < 0)
903         {
904                 TS_DEBUG(" egalax_i2c cdev can't get major number\n");
905                 return;
906         }
907
908         // allocate the character device
909         p_char_dev = setup_chardev(devno);
910         if(!p_char_dev) 
911         {
912                 result = -ENOMEM;
913                 goto fail;
914         }
915
916         egalax_class = class_create(THIS_MODULE, "egalax_i2c");
917         if(IS_ERR(egalax_class))
918         {
919                 TS_DEBUG("Err: failed in creating class.\n");
920                 result = -EFAULT;
921                 goto fail;
922         }
923
924 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
925         class_device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
926 #else
927         device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
928 #endif
929         TS_DEBUG("register egalax_i2c cdev, major: %d \n",global_major);
930
931         printk(KERN_DEBUG "[egalax_i2c]: init done\n");
932         i2c_add_driver(&egalax_i2c_driver);
933         return;
934
935 fail:   
936         egalax_i2c_ts_exit();
937 }
938
939 static int __init egalax_i2c_ts_init(void)
940 {
941         async_schedule(egalax_i2c_ts_init_async, NULL);
942         return 0;
943 }
944 module_init(egalax_i2c_ts_init);
945 module_exit(egalax_i2c_ts_exit);
946
947 MODULE_DESCRIPTION("egalax touch screen i2c driver");
948 MODULE_LICENSE("GPL");
949