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