modify eeti i2c rate to 200kHZ
[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 #ifdef CONFIG_PM
500 static int egalax_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
501 {
502         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
503         u8 cmdbuf[MAX_I2C_LEN]={0x03, 0x05, 0x0A, 0x03, 0x36, 0x3F, 0x02, 0, 0, 0};
504         
505         i2c_master_normal_send(client, cmdbuf, MAX_I2C_LEN, EETI_I2C_RATE);
506
507         egalax_i2c->work_state = 0;
508         disable_irq(p_egalax_i2c_dev->irq);
509         cancel_work_sync(&egalax_i2c->work);
510
511         printk(KERN_DEBUG "[egalax_i2c]: device suspend done\n");       
512
513         if(device_may_wakeup(&client->dev)) 
514         {
515                 enable_irq_wake(p_egalax_i2c_dev->irq);
516         }
517         else 
518         {
519                 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
520         }
521
522         return 0;
523 }
524
525 static int egalax_i2c_resume(struct i2c_client *client)
526 {
527         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
528         
529         if(device_may_wakeup(&client->dev)) 
530         {
531                 disable_irq_wake(p_egalax_i2c_dev->irq);
532         }
533         else 
534         {
535                 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
536         }
537
538         wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
539         egalax_i2c->work_state = 1;
540         enable_irq(p_egalax_i2c_dev->irq);
541
542         printk(KERN_DEBUG "[egalax_i2c]: device wakeup done\n");
543
544         return 0;
545 }
546 #else
547 #define egalax_i2c_suspend       NULL
548 #define egalax_i2c_resume        NULL
549 #endif
550
551 static int __devinit egalax_i2c_probe(struct i2c_client *client)
552 {
553         int ret;
554         int gpio = client->irq;
555         struct eeti_egalax_platform_data *pdata = pdata = client->dev.platform_data;
556         DBG();
557         printk(KERN_DEBUG "[egalax_i2c]: start probe\n");
558
559         p_egalax_i2c_dev = (struct _egalax_i2c *)kzalloc(sizeof(struct _egalax_i2c), GFP_KERNEL);
560         if (!p_egalax_i2c_dev) 
561         {
562                 printk(KERN_ERR "[egalax_i2c]: request memory failed\n");
563                 ret = -ENOMEM;
564                 goto fail1;
565         }
566
567 #ifndef _NON_INPUT_DEV
568         input_dev = allocate_Input_Dev();
569         if(input_dev==NULL)
570         {
571                 printk(KERN_ERR "[egalax_i2c]: allocate_Input_Dev failed\n");
572                 ret = -EINVAL; 
573                 goto fail2;
574         }
575         TS_DEBUG("egalax_i2c register input device done\n");
576         memset(PointBuf, 0, sizeof(struct point_data)*MAX_SUPPORT_POINT);
577 #endif //#ifndef _NON_INPUT_DEV
578
579 if (pdata->init_platform_hw)
580                 pdata->init_platform_hw();
581
582         p_egalax_i2c_dev->client = client;
583         mutex_init(&p_egalax_i2c_dev->mutex_wq);
584
585         p_egalax_i2c_dev->ktouch_wq = create_workqueue("egalax_touch_wq"); 
586         INIT_WORK(&p_egalax_i2c_dev->work, egalax_i2c_wq);
587         
588         i2c_set_clientdata(client, p_egalax_i2c_dev);
589
590         if( gpio_get_value(gpio) )
591                 p_egalax_i2c_dev->skip_packet = 0;
592         else
593                 p_egalax_i2c_dev->skip_packet = 1;
594
595         p_egalax_i2c_dev->work_state = 1;
596         
597         p_egalax_i2c_dev->irq = gpio_to_irq(client->irq);
598         
599         ret = request_irq(p_egalax_i2c_dev->irq, egalax_i2c_interrupt, 0,
600                  client->name, p_egalax_i2c_dev);
601         if( ret ) 
602         {
603                 printk(KERN_ERR "[egalax_i2c]: request irq(%d) failed\n", p_egalax_i2c_dev->irq);
604                 goto fail3;
605         }
606         TS_DEBUG("egalax_i2c request irq(%d) gpio(%d) with result:%d\n", p_egalax_i2c_dev->irq, gpio, ret);
607
608 #ifdef CONFIG_PM
609         device_init_wakeup(&client->dev, 1);
610 #endif
611
612         printk(KERN_DEBUG "[egalax_i2c]: probe done\n");
613         return 0;
614
615 fail3:
616         i2c_set_clientdata(client, NULL);
617         destroy_workqueue(p_egalax_i2c_dev->ktouch_wq); 
618         free_irq(p_egalax_i2c_dev->irq, p_egalax_i2c_dev);
619 #ifndef _NON_INPUT_DEV
620         input_unregister_device(input_dev);
621         input_free_device(input_dev);
622         input_dev = NULL;
623 #endif //#ifndef _NON_INPUT_DEV
624 fail2:
625 fail1:
626         kfree(p_egalax_i2c_dev);
627         p_egalax_i2c_dev = NULL;
628
629         printk(KERN_DEBUG "[egalax_i2c]: probe failed\n");
630         return ret;
631 }
632
633 static int __devexit egalax_i2c_remove(struct i2c_client *client)
634 {
635         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
636
637         DBG();
638
639 #ifndef _NON_INPUT_DEV
640         if(input_dev)
641         {
642                 TS_DEBUG("unregister input device\n");
643                 input_unregister_device(input_dev);
644                 input_free_device(input_dev);
645                 input_dev = NULL;
646         }
647 #endif //#ifndef _NON_INPUT_DEV
648
649         if(p_egalax_i2c_dev->ktouch_wq) 
650         {
651                 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq); 
652         }
653
654         if(p_egalax_i2c_dev->irq)
655         {
656                 free_irq(p_egalax_i2c_dev->irq, egalax_i2c);
657         }
658
659         i2c_set_clientdata(client, NULL);
660         kfree(egalax_i2c);
661         p_egalax_i2c_dev = NULL;
662
663         return 0;
664 }
665
666 static struct i2c_device_id egalax_i2c_idtable[] = { 
667         { "egalax_i2c", 0 }, 
668         { } 
669 }; 
670
671 MODULE_DEVICE_TABLE(i2c, egalax_i2c_idtable);
672
673 static struct i2c_driver egalax_i2c_driver = {
674         .driver = {
675                 .name   = "egalax_i2c",
676         },
677         .id_table       = egalax_i2c_idtable,
678         .probe          = egalax_i2c_probe,
679         .remove         = __devexit_p(egalax_i2c_remove),
680         .suspend        = egalax_i2c_suspend,
681         .resume         = egalax_i2c_resume,
682 };
683
684 static const struct file_operations egalax_cdev_fops = {
685         .owner  = THIS_MODULE,
686         .read   = egalax_cdev_read,
687         .write  = egalax_cdev_write,
688         .ioctl  = egalax_cdev_ioctl,
689         .poll   = egalax_cdev_poll,
690         .open   = egalax_cdev_open,
691         .release= egalax_cdev_release,
692 };
693
694 static void egalax_i2c_ts_exit(void)
695 {
696         dev_t devno = MKDEV(global_major, global_minor);
697         DBG();
698
699         if(p_char_dev)
700         {
701                 TS_DEBUG("unregister character device\n");
702                 if( p_char_dev->pFiFoBuf )
703                         kfree(p_char_dev->pFiFoBuf);
704         
705                 cdev_del(&p_char_dev->cdev);
706                 kfree(p_char_dev);
707                 p_char_dev = NULL;
708         }
709
710         unregister_chrdev_region( devno, 1);
711
712         if(!IS_ERR(egalax_class))
713         {
714 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
715                 class_device_destroy(egalax_class, devno);
716 #else
717                 device_destroy(egalax_class, devno);
718 #endif 
719                 class_destroy(egalax_class);
720         }
721         
722         i2c_del_driver(&egalax_i2c_driver);
723
724         printk(KERN_DEBUG "[egalax_i2c]: driver exit\n");
725 }
726
727 static struct egalax_char_dev* setup_chardev(dev_t dev)
728 {
729         struct egalax_char_dev *pCharDev;
730         int result;
731
732         pCharDev = kmalloc(1*sizeof(struct egalax_char_dev), GFP_KERNEL);
733         if(!pCharDev) 
734                 goto fail_cdev;
735         memset(pCharDev, 0, sizeof(struct egalax_char_dev));
736
737         spin_lock_init( &pCharDev->FiFoLock );
738         pCharDev->pFiFoBuf = kmalloc(sizeof(unsigned char)*FIFO_SIZE, GFP_KERNEL);
739         if(!pCharDev->pFiFoBuf)
740                 goto fail_fifobuf;
741         memset(pCharDev->pFiFoBuf, 0, sizeof(unsigned char)*FIFO_SIZE);
742
743 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
744         pCharDev->pDataKFiFo = kfifo_init(pCharDev->pFiFoBuf, FIFO_SIZE, GFP_KERNEL, &pCharDev->FiFoLock);
745         if( pCharDev->pDataKFiFo==NULL )
746                 goto fail_kfifo;
747 #else
748         kfifo_init(&pCharDev->DataKFiFo, pCharDev->pFiFoBuf, FIFO_SIZE);
749         if( !kfifo_initialized(&pCharDev->DataKFiFo) )
750                 goto fail_kfifo;
751 #endif
752         
753         pCharDev->OpenCnts = 0;
754         cdev_init(&pCharDev->cdev, &egalax_cdev_fops);
755         pCharDev->cdev.owner = THIS_MODULE;
756         sema_init(&pCharDev->sem, 1);
757         init_waitqueue_head(&pCharDev->fifo_inq);
758
759         result = cdev_add(&pCharDev->cdev, dev, 1);
760         if(result)
761         {
762                 TS_DEBUG(KERN_ERR "Error cdev ioctldev added\n");
763                 goto fail_kfifo;
764         }
765
766         return pCharDev; 
767
768 fail_kfifo:
769         kfree(pCharDev->pFiFoBuf);
770 fail_fifobuf:
771         kfree(pCharDev);
772 fail_cdev:
773         return NULL;
774 }
775
776 static void __init egalax_i2c_ts_init_async(void *unused, async_cookie_t cookie)
777 {
778         int result;
779         dev_t devno = 0;
780
781         DBG();
782
783         // Asking for a dynamic major unless directed otherwise at load time.
784         if(global_major) 
785         {
786                 devno = MKDEV(global_major, global_minor);
787                 result = register_chrdev_region(devno, 1, "egalax_i2c");
788         } 
789         else 
790         {
791                 result = alloc_chrdev_region(&devno, global_minor, 1, "egalax_i2c");
792                 global_major = MAJOR(devno);
793         }
794
795         if (result < 0)
796         {
797                 TS_DEBUG(" egalax_i2c cdev can't get major number\n");
798                 return 0;
799         }
800
801         // allocate the character device
802         p_char_dev = setup_chardev(devno);
803         if(!p_char_dev) 
804         {
805                 result = -ENOMEM;
806                 goto fail;
807         }
808
809         egalax_class = class_create(THIS_MODULE, "egalax_i2c");
810         if(IS_ERR(egalax_class))
811         {
812                 TS_DEBUG("Err: failed in creating class.\n");
813                 result = -EFAULT;
814                 goto fail;
815         }
816
817 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
818         class_device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
819 #else
820         device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
821 #endif
822         TS_DEBUG("register egalax_i2c cdev, major: %d \n",global_major);
823
824         printk(KERN_DEBUG "[egalax_i2c]: init done\n");
825         return i2c_add_driver(&egalax_i2c_driver);
826
827 fail:   
828         egalax_i2c_ts_exit();
829         return result;
830 }
831
832 static int __init egalax_i2c_ts_init(void)
833 {
834         async_schedule(egalax_i2c_ts_init_async, NULL);
835         return 0;
836 }
837 module_init(egalax_i2c_ts_init);
838 module_exit(egalax_i2c_ts_exit);
839
840 MODULE_DESCRIPTION("egalax touch screen i2c driver");
841 MODULE_LICENSE("GPL");
842