3 * Touch Screen I2C Driver for EETI Controller
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.
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.
17 // Release Date: 2010/11/08
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>
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>
45 #ifdef CONFIG_HAS_EARLYSUSPEND
46 #include <linux/earlysuspend.h>
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__)
54 #define TS_DEBUG(fmt,args...)
58 //#define _NON_INPUT_DEV // define this to disable register input device
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
71 #define EGALAX_IOC_MAGIC 0x72
72 #define EGALAX_IOCWAKEUP _IO(EGALAX_IOC_MAGIC, 1)
73 #define EGALAX_IOC_MAXNR 1
75 #define EETI_EARLYSUSPEND_LEVEL 151
84 struct workqueue_struct *ktouch_wq;
85 struct work_struct work;
86 struct mutex mutex_wq;
87 struct i2c_client *client;
93 #ifdef CONFIG_HAS_EARLYSUSPEND
95 struct early_suspend early_suspend;
96 struct _egalax_i2c *egalax_i2c;
100 struct egalax_char_dev
104 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
105 struct kfifo* pDataKFiFo;
107 struct kfifo DataKFiFo;
109 unsigned char *pFiFoBuf;
111 struct semaphore sem;
112 wait_queue_head_t fifo_inq;
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
124 static int egalax_cdev_open(struct inode *inode, struct file *filp)
126 struct egalax_char_dev *cdev;
130 cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
133 TS_DEBUG(" No such char device node \n");
137 if( !atomic_dec_and_test(&egalax_char_available) )
139 atomic_inc(&egalax_char_available);
140 return -EBUSY; /* already open */
144 filp->private_data = cdev;// Used by the read and write metheds
146 TS_DEBUG(" egalax_cdev_open done \n");
147 try_module_get(THIS_MODULE);
151 static int egalax_cdev_release(struct inode *inode, struct file *filp)
153 struct egalax_char_dev *cdev; // device information
157 cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
160 TS_DEBUG(" No such char device node \n");
164 atomic_inc(&egalax_char_available); /* release the device */
166 filp->private_data = NULL;
168 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
169 kfifo_reset( cdev->pDataKFiFo );
171 kfifo_reset( &cdev->DataKFiFo );
174 TS_DEBUG(" egalax_cdev_release done \n");
175 module_put(THIS_MODULE);
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)
183 int read_cnt, ret, fifoLen;
184 struct egalax_char_dev *cdev = file->private_data;
188 if( down_interruptible(&cdev->sem) )
191 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
192 fifoLen = kfifo_len(cdev->pDataKFiFo);
194 fifoLen = kfifo_len(&cdev->DataKFiFo);
197 while( fifoLen<1 ) /* nothing to read */
199 up(&cdev->sem); /* release the lock */
200 if( file->f_flags & O_NONBLOCK )
203 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
204 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( cdev->pDataKFiFo )>0) )
206 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( &cdev->DataKFiFo )>0) )
209 return -ERESTARTSYS; /* signal: tell the fs layer to handle it */
212 if( down_interruptible(&cdev->sem) )
216 if(count > MAX_READ_BUF_LEN)
217 count = MAX_READ_BUF_LEN;
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);
223 read_cnt = kfifo_out_locked(&cdev->DataKFiFo, fifo_read_buf, count, &cdev->FiFoLock);
226 ret = copy_to_user(buf, fifo_read_buf, read_cnt)?-EFAULT:read_cnt;
233 static ssize_t egalax_cdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
236 struct egalax_char_dev *cdev = file->private_data;
241 if( down_interruptible(&cdev->sem) )
244 if (count > MAX_I2C_LEN)
247 tmp = kmalloc(count,GFP_KERNEL);
254 if(copy_from_user(tmp, buf, count))
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);
271 static int wakeup_controller(int gpio)
277 if( (ret=gpio_request(gpio, "Touch Wakeup GPIO"))!=0 )
279 printk(KERN_ERR "[egalax_i2c]: Failed to request GPIO for Touch Wakeup GPIO. Err:%d\n", ret);
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");
293 static long egalax_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long args)
295 //struct egalax_char_dev *cdev = file->private_data;
298 if(_IOC_TYPE(cmd) != EGALAX_IOC_MAGIC)
300 if(_IOC_NR(cmd) > EGALAX_IOC_MAXNR)
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));
311 //printk(KERN_ERR "Handle device ioctl command\n");
314 case EGALAX_IOCWAKEUP:
315 ret = wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
325 static unsigned int egalax_cdev_poll(struct file *filp, struct poll_table_struct *wait)
327 struct egalax_char_dev *cdev = filp->private_data;
328 unsigned int mask = 0;
332 poll_wait(filp, &cdev->fifo_inq, wait);
334 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
335 fifoLen = kfifo_len(cdev->pDataKFiFo);
337 fifoLen = kfifo_len(&cdev->DataKFiFo);
341 mask |= POLLIN | POLLRDNORM; /* readable */
342 if( (FIFO_SIZE - fifoLen) > MAX_I2C_LEN )
343 mask |= POLLOUT | POLLWRNORM; /* writable */
349 #ifndef _NON_INPUT_DEV
350 static int LastUpdateID = 0;
351 static void ProcessReport(unsigned char *buf, int buflen)
354 short X=0, Y=0, ContactID=0, Status=0;
355 if(buflen!=MAX_I2C_LEN || buf[0]!=0x04) // check buffer len & header
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;
363 PointBuf[ContactID].Status = Status;
364 PointBuf[ContactID].X = X;
365 PointBuf[ContactID].Y = Y;
367 TS_DEBUG("Get Point[%d] Update: Status=%d X=%d Y=%d\n", ContactID, Status, X, Y);
370 if( !Status || (ContactID <= LastUpdateID) )
372 for(i=0; i<MAX_SUPPORT_POINT;i++)
374 if(PointBuf[i].Status >= 0)
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_PRESSURE, 100);
380 input_report_abs(input_dev, ABS_MT_POSITION_X, PointBuf[i].X);
381 input_report_abs(input_dev, ABS_MT_POSITION_Y, PointBuf[i].Y);
383 if(PointBuf[i].Status == 0)
384 PointBuf[i].Status--;
388 input_mt_slot(input_dev, i);
389 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
392 input_sync(input_dev);
393 TS_DEBUG("Input sync point data done!\n");
396 LastUpdateID = ContactID;
399 static struct input_dev * allocate_Input_Dev(void)
402 struct input_dev *pInputDev=NULL;
404 pInputDev = input_allocate_device();
405 if(pInputDev == NULL)
407 TS_DEBUG("Failed to allocate input device\n");
408 return NULL;//-ENOMEM;
411 pInputDev->name = "eGalax Touch Screen";
412 pInputDev->phys = "I2C";
413 pInputDev->id.bustype = BUS_I2C;
414 pInputDev->id.vendor = 0x0EEF;
415 pInputDev->id.product = 0x0020;
417 __set_bit(INPUT_PROP_DIRECT, pInputDev->propbit);
418 __set_bit(EV_ABS, pInputDev->evbit);
420 input_mt_init_slots(pInputDev, MAX_SUPPORT_POINT);
421 input_set_abs_params(pInputDev, ABS_MT_POSITION_X, 0, CONFIG_EETI_EGALAX_MAX_X, 0, 0);
422 input_set_abs_params(pInputDev, ABS_MT_POSITION_Y, 0, CONFIG_EETI_EGALAX_MAX_Y, 0, 0);
423 input_set_abs_params(pInputDev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
424 input_set_abs_params(pInputDev, ABS_MT_PRESSURE, 0, 255, 0, 0);
426 ret = input_register_device(pInputDev);
429 TS_DEBUG("Unable to register input device.\n");
430 input_free_device(pInputDev);
436 #endif //#ifndef _NON_INPUT_DEV
438 static int egalax_i2c_measure(struct i2c_client *client, char skip_packet)
440 u8 x_buf[MAX_I2C_LEN];
444 count = i2c_master_normal_recv(client, x_buf, MAX_I2C_LEN,EETI_I2C_RATE);
445 }while(count==EAGAIN && --loop);
447 if( count<0 || (x_buf[0]!=REPORTID_VENDOR && x_buf[0]!=REPORTID_MTOUCH) )
449 TS_DEBUG("I2C read error data with Len=%d hedaer=%d\n", count, x_buf[0]);
453 TS_DEBUG("egalax_i2c read data with Len=%d\n", count);
454 if(x_buf[0]==REPORTID_VENDOR)
455 TS_DEBUG("egalax_i2c get command packet\n");
457 if( skip_packet > 0 )
460 #ifndef _NON_INPUT_DEV
461 if( count>0 && x_buf[0]==REPORTID_MTOUCH )
463 ProcessReport(x_buf, count);
467 #endif //#ifndef _NON_INPUT_DEV
469 if( count>0 && p_char_dev->OpenCnts>0 ) // If someone reading now! put the data into the buffer!
471 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
472 kfifo_put(p_char_dev->pDataKFiFo, x_buf, count);
474 kfifo_in_locked(&p_char_dev->DataKFiFo, x_buf, count, &p_char_dev->FiFoLock);
476 wake_up_interruptible( &p_char_dev->fifo_inq );
482 static void egalax_i2c_wq(struct work_struct *work)
484 struct _egalax_i2c *egalax_i2c = container_of(work, struct _egalax_i2c, work);
485 struct i2c_client *client = egalax_i2c->client;
486 int gpio = client->irq;
487 TS_DEBUG("egalax_i2c_wq run\n");
488 mutex_lock(&egalax_i2c->mutex_wq);
490 /*continue recv data*/
491 while( !gpio_get_value(gpio) && egalax_i2c->work_state>0 )
493 egalax_i2c_measure(client, egalax_i2c->skip_packet);
494 schedule_timeout_interruptible(HZ/100);
497 #ifndef _NON_INPUT_DEV
498 if (gpio_get_value(gpio) && egalax_i2c->work_state > 0 && !egalax_i2c->skip_packet) {
500 for(i = 0; i < MAX_SUPPORT_POINT; i++) {
501 if (PointBuf[i].Status > 0) {
502 TS_DEBUG("point %d still down\n", i);
503 input_mt_slot(input_dev, i);
504 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, true);
505 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, 0);
506 input_report_abs(input_dev, ABS_MT_PRESSURE, 100);
507 input_report_abs(input_dev, ABS_MT_POSITION_X, PointBuf[i].X);
508 input_report_abs(input_dev, ABS_MT_POSITION_Y, PointBuf[i].Y);
509 PointBuf[i].Status = 0;
513 input_mt_slot(input_dev, i);
514 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
517 input_sync(input_dev);
521 if( egalax_i2c->skip_packet > 0 )
522 egalax_i2c->skip_packet = 0;
524 mutex_unlock(&egalax_i2c->mutex_wq);
526 enable_irq(p_egalax_i2c_dev->irq);
528 TS_DEBUG("egalax_i2c_wq leave\n");
531 static irqreturn_t egalax_i2c_interrupt(int irq, void *dev_id)
533 struct _egalax_i2c *egalax_i2c = (struct _egalax_i2c *)dev_id;
534 TS_DEBUG("egalax_i2c_interrupt with irq:%d\n", irq);
535 disable_irq_nosync(irq);
536 queue_work(egalax_i2c->ktouch_wq, &egalax_i2c->work);
542 void egalax_i2c_set_standby(struct i2c_client *client, int enable)
544 struct eeti_egalax_platform_data *mach_info = client->dev.platform_data;
545 unsigned display_on = mach_info->disp_on_pin;
546 unsigned lcd_standby = mach_info->standby_pin;
548 int display_on_pol = mach_info->disp_on_value;
549 int lcd_standby_pol = mach_info->standby_value;
551 printk("%s : %s, enable = %d", __FILE__, __FUNCTION__,enable);
552 if(display_on != INVALID_GPIO)
554 gpio_direction_output(display_on, 0);
555 gpio_set_value(display_on, enable ? display_on_pol : !display_on_pol);
557 if(lcd_standby != INVALID_GPIO)
559 gpio_direction_output(lcd_standby, 0);
560 gpio_set_value(lcd_standby, enable ? lcd_standby_pol : !lcd_standby_pol);
565 static int egalax_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
567 struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
568 u8 cmdbuf[MAX_I2C_LEN]={0x03, 0x05, 0x0A, 0x03, 0x36, 0x3F, 0x02, 0, 0, 0};
570 i2c_master_normal_send(client, cmdbuf, MAX_I2C_LEN, EETI_I2C_RATE);
572 disable_irq(p_egalax_i2c_dev->irq);
573 egalax_i2c->work_state = 0;
574 if (cancel_work_sync(&egalax_i2c->work)) {
575 /* if work was pending disable-count is now 2 */
576 pr_info("%s: work was pending\n", __func__);
577 enable_irq(p_egalax_i2c_dev->irq);
580 printk(KERN_DEBUG "[egalax_i2c]: device suspend done\n");
582 if(device_may_wakeup(&client->dev))
584 enable_irq_wake(p_egalax_i2c_dev->irq);
588 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
590 egalax_i2c_set_standby(client, 0);
594 static int egalax_i2c_resume(struct i2c_client *client)
596 struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
597 egalax_i2c_set_standby(client, 1);
598 if(device_may_wakeup(&client->dev))
600 disable_irq_wake(p_egalax_i2c_dev->irq);
604 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
607 wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
608 egalax_i2c->work_state = 1;
609 enable_irq(p_egalax_i2c_dev->irq);
611 printk(KERN_DEBUG "[egalax_i2c]: device wakeup done\n");
616 #ifdef CONFIG_HAS_EARLYSUSPEND
618 static void egalax_i2c_early_suspend(struct early_suspend *h)
620 pm_message_t mesg = {.event = 0};
621 struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
622 struct i2c_client *client = info->egalax_i2c->client;
623 egalax_i2c_suspend(client,mesg);
626 static void egalax_i2c_early_resume(struct early_suspend *h)
629 struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
630 struct i2c_client *client = info->egalax_i2c->client;
632 egalax_i2c_resume(client);
641 #define egalax_i2c_suspend NULL
642 #define egalax_i2c_resume NULL
645 #ifdef CONFIG_HAS_EARLYSUSPEND
646 static struct suspend_info suspend_info = {
647 .early_suspend.suspend = egalax_i2c_early_suspend,
648 .early_suspend.resume = egalax_i2c_early_resume,
649 .early_suspend.level = EETI_EARLYSUSPEND_LEVEL,
654 static int __devinit egalax_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
657 int gpio = client->irq;
658 struct eeti_egalax_platform_data *pdata = pdata = client->dev.platform_data;
660 printk(KERN_DEBUG "[egalax_i2c]: start probe\n");
662 p_egalax_i2c_dev = (struct _egalax_i2c *)kzalloc(sizeof(struct _egalax_i2c), GFP_KERNEL);
663 if (!p_egalax_i2c_dev)
665 printk(KERN_ERR "[egalax_i2c]: request memory failed\n");
670 #ifndef _NON_INPUT_DEV
671 input_dev = allocate_Input_Dev();
674 printk(KERN_ERR "[egalax_i2c]: allocate_Input_Dev failed\n");
678 TS_DEBUG("egalax_i2c register input device done\n");
679 memset(PointBuf, 0, sizeof(struct point_data)*MAX_SUPPORT_POINT);
680 #endif //#ifndef _NON_INPUT_DEV
682 if (pdata->init_platform_hw)
683 pdata->init_platform_hw();
685 p_egalax_i2c_dev->client = client;
686 mutex_init(&p_egalax_i2c_dev->mutex_wq);
688 p_egalax_i2c_dev->ktouch_wq = create_workqueue("egalax_touch_wq");
689 INIT_WORK(&p_egalax_i2c_dev->work, egalax_i2c_wq);
691 i2c_set_clientdata(client, p_egalax_i2c_dev);
693 if( gpio_get_value(gpio) )
694 p_egalax_i2c_dev->skip_packet = 0;
696 p_egalax_i2c_dev->skip_packet = 1;
698 p_egalax_i2c_dev->work_state = 1;
700 p_egalax_i2c_dev->irq = gpio_to_irq(client->irq);
702 ret = request_irq(p_egalax_i2c_dev->irq, egalax_i2c_interrupt, IRQF_TRIGGER_LOW,
703 client->name, p_egalax_i2c_dev);
706 printk(KERN_ERR "[egalax_i2c]: request irq(%d) failed\n", p_egalax_i2c_dev->irq);
709 TS_DEBUG("egalax_i2c request irq(%d) gpio(%d) with result:%d\n", p_egalax_i2c_dev->irq, gpio, ret);
712 device_init_wakeup(&client->dev, 0);
715 #ifdef CONFIG_HAS_EARLYSUSPEND
718 suspend_info.egalax_i2c = p_egalax_i2c_dev;
719 register_early_suspend(&suspend_info.early_suspend);
722 printk(KERN_DEBUG "[egalax_i2c]: probe done\n");
726 i2c_set_clientdata(client, NULL);
727 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq);
728 free_irq(p_egalax_i2c_dev->irq, p_egalax_i2c_dev);
729 #ifndef _NON_INPUT_DEV
730 input_unregister_device(input_dev);
731 input_free_device(input_dev);
733 #endif //#ifndef _NON_INPUT_DEV
736 kfree(p_egalax_i2c_dev);
737 p_egalax_i2c_dev = NULL;
739 printk(KERN_DEBUG "[egalax_i2c]: probe failed\n");
743 static int __devexit egalax_i2c_remove(struct i2c_client *client)
745 struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
749 #ifndef _NON_INPUT_DEV
752 TS_DEBUG("unregister input device\n");
753 input_unregister_device(input_dev);
754 input_free_device(input_dev);
757 #endif //#ifndef _NON_INPUT_DEV
759 if(p_egalax_i2c_dev->ktouch_wq)
761 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq);
764 if(p_egalax_i2c_dev->irq)
766 free_irq(p_egalax_i2c_dev->irq, egalax_i2c);
769 i2c_set_clientdata(client, NULL);
771 p_egalax_i2c_dev = NULL;
776 static struct i2c_device_id egalax_i2c_idtable[] = {
781 MODULE_DEVICE_TABLE(i2c, egalax_i2c_idtable);
783 static struct i2c_driver egalax_i2c_driver = {
785 .name = "egalax_i2c",
787 .id_table = egalax_i2c_idtable,
788 .probe = egalax_i2c_probe,
789 .remove = __devexit_p(egalax_i2c_remove),
790 //.suspend = egalax_i2c_suspend,
791 //.resume = egalax_i2c_resume,
794 static const struct file_operations egalax_cdev_fops = {
795 .owner = THIS_MODULE,
796 .read = egalax_cdev_read,
797 .write = egalax_cdev_write,
798 .unlocked_ioctl = egalax_cdev_ioctl,
799 .poll = egalax_cdev_poll,
800 .open = egalax_cdev_open,
801 .release= egalax_cdev_release,
804 static void egalax_i2c_ts_exit(void)
806 dev_t devno = MKDEV(global_major, global_minor);
811 TS_DEBUG("unregister character device\n");
812 if( p_char_dev->pFiFoBuf )
813 kfree(p_char_dev->pFiFoBuf);
815 cdev_del(&p_char_dev->cdev);
820 unregister_chrdev_region( devno, 1);
822 if(!IS_ERR(egalax_class))
824 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
825 class_device_destroy(egalax_class, devno);
827 device_destroy(egalax_class, devno);
829 class_destroy(egalax_class);
832 i2c_del_driver(&egalax_i2c_driver);
834 printk(KERN_DEBUG "[egalax_i2c]: driver exit\n");
837 static struct egalax_char_dev* setup_chardev(dev_t dev)
839 struct egalax_char_dev *pCharDev;
842 pCharDev = kmalloc(1*sizeof(struct egalax_char_dev), GFP_KERNEL);
845 memset(pCharDev, 0, sizeof(struct egalax_char_dev));
847 spin_lock_init( &pCharDev->FiFoLock );
848 pCharDev->pFiFoBuf = kmalloc(sizeof(unsigned char)*FIFO_SIZE, GFP_KERNEL);
849 if(!pCharDev->pFiFoBuf)
851 memset(pCharDev->pFiFoBuf, 0, sizeof(unsigned char)*FIFO_SIZE);
853 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
854 pCharDev->pDataKFiFo = kfifo_init(pCharDev->pFiFoBuf, FIFO_SIZE, GFP_KERNEL, &pCharDev->FiFoLock);
855 if( pCharDev->pDataKFiFo==NULL )
858 kfifo_init(&pCharDev->DataKFiFo, pCharDev->pFiFoBuf, FIFO_SIZE);
859 if( !kfifo_initialized(&pCharDev->DataKFiFo) )
863 pCharDev->OpenCnts = 0;
864 cdev_init(&pCharDev->cdev, &egalax_cdev_fops);
865 pCharDev->cdev.owner = THIS_MODULE;
866 sema_init(&pCharDev->sem, 1);
867 init_waitqueue_head(&pCharDev->fifo_inq);
869 result = cdev_add(&pCharDev->cdev, dev, 1);
872 TS_DEBUG(KERN_ERR "Error cdev ioctldev added\n");
879 kfree(pCharDev->pFiFoBuf);
886 static void __init egalax_i2c_ts_init_async(void *unused, async_cookie_t cookie)
893 // Asking for a dynamic major unless directed otherwise at load time.
896 devno = MKDEV(global_major, global_minor);
897 result = register_chrdev_region(devno, 1, "egalax_i2c");
901 result = alloc_chrdev_region(&devno, global_minor, 1, "egalax_i2c");
902 global_major = MAJOR(devno);
907 TS_DEBUG(" egalax_i2c cdev can't get major number\n");
911 // allocate the character device
912 p_char_dev = setup_chardev(devno);
919 egalax_class = class_create(THIS_MODULE, "egalax_i2c");
920 if(IS_ERR(egalax_class))
922 TS_DEBUG("Err: failed in creating class.\n");
927 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
928 class_device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
930 device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
932 TS_DEBUG("register egalax_i2c cdev, major: %d \n",global_major);
934 printk(KERN_DEBUG "[egalax_i2c]: init done\n");
935 i2c_add_driver(&egalax_i2c_driver);
939 egalax_i2c_ts_exit();
942 static int __init egalax_i2c_ts_init(void)
944 async_schedule(egalax_i2c_ts_init_async, NULL);
947 module_init(egalax_i2c_ts_init);
948 module_exit(egalax_i2c_ts_exit);
950 MODULE_DESCRIPTION("egalax touch screen i2c driver");
951 MODULE_LICENSE("GPL");