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>
44 #ifdef CONFIG_RK_CONFIG
45 #include <mach/config.h>
48 #ifdef CONFIG_HAS_EARLYSUSPEND
49 #include <linux/earlysuspend.h>
52 #define TP_MODULE_NAME "egalax_i2c"
53 #ifdef CONFIG_RK_CONFIG
56 #if defined(RK2926_TB_DEFAULT_CONFIG) || defined(RK2928_TB_DEFAULT_CONFIG)
61 #if defined(RK2926_TB_DEFAULT_CONFIG)
64 #elif defined(RK2928_TB_DEFAULT_CONFIG)
76 static int en = DEF_EN;
77 module_param(en, int, 0644);
79 static int irq = DEF_IRQ;
80 module_param(irq, int, 0644);
81 static int rst =DEF_RST;
82 module_param(rst, int, 0644);
84 static int i2c = DEF_I2C; // i2c channel
85 module_param(i2c, int, 0644);
86 static int addr = DEF_ADDR; // i2c addr
87 module_param(addr, int, 0644);
88 static int x_max = DEF_X_MAX;
89 module_param(x_max, int, 0644);
90 static int y_max = DEF_Y_MAX;
91 module_param(y_max, int, 0644);
93 static int tp_hw_init(void)
97 ret = gpio_request(get_port_config(irq).gpio, "tp_irq");
99 printk("%s: gpio_request(irq gpio) failed\n", __func__);
103 ret = port_output_init(rst, 0, "tp_rst");
105 printk("%s: port(rst) output init faild\n", __func__);
116 #ifdef CONFIG_EETI_EGALAX_DEBUG
117 #define TS_DEBUG(fmt,args...) printk( KERN_DEBUG "[egalax_i2c]: " fmt, ## args)
118 #define DBG() printk("[%s]:%d => \n",__FUNCTION__,__LINE__)
120 #define TS_DEBUG(fmt,args...)
125 //#define _NON_INPUT_DEV // define this to disable register input device
127 static int global_major = 0; // dynamic major by default
128 static int global_minor = 0;
129 #define EETI_I2C_RATE (200*1000)
130 #define MAX_I2C_LEN 10
131 #define FIFO_SIZE PAGE_SIZE
132 #define MAX_SUPPORT_POINT 2
133 #define REPORTID_MOUSE 0x01
134 #define REPORTID_VENDOR 0x03
135 #define REPORTID_MTOUCH 0x04
137 /// ioctl command ///
138 #define EGALAX_IOC_MAGIC 0x72
139 #define EGALAX_IOCWAKEUP _IO(EGALAX_IOC_MAGIC, 1)
140 #define EGALAX_IOC_MAXNR 1
142 #define EETI_EARLYSUSPEND_LEVEL (EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1)
151 struct workqueue_struct *ktouch_wq;
152 struct work_struct work;
153 struct mutex mutex_wq;
154 struct i2c_client *client;
160 #ifdef CONFIG_HAS_EARLYSUSPEND
161 struct suspend_info {
162 struct early_suspend early_suspend;
163 struct _egalax_i2c *egalax_i2c;
167 struct egalax_char_dev
171 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
172 struct kfifo* pDataKFiFo;
174 struct kfifo DataKFiFo;
176 unsigned char *pFiFoBuf;
178 struct semaphore sem;
179 wait_queue_head_t fifo_inq;
182 static struct _egalax_i2c *p_egalax_i2c_dev = NULL; // allocated in egalax_i2c_probe
183 static struct egalax_char_dev *p_char_dev = NULL; // allocated in init_module
184 static atomic_t egalax_char_available = ATOMIC_INIT(1);
185 static struct class *egalax_class;
186 #ifndef _NON_INPUT_DEV
187 static struct input_dev *input_dev = NULL;
188 static struct point_data PointBuf[MAX_SUPPORT_POINT];
189 #endif //#ifndef _NON_INPUT_DEV
191 static int egalax_cdev_open(struct inode *inode, struct file *filp)
193 struct egalax_char_dev *cdev;
197 cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
200 TS_DEBUG(" No such char device node \n");
204 if( !atomic_dec_and_test(&egalax_char_available) )
206 atomic_inc(&egalax_char_available);
207 return -EBUSY; /* already open */
211 filp->private_data = cdev;// Used by the read and write metheds
213 TS_DEBUG(" egalax_cdev_open done \n");
214 try_module_get(THIS_MODULE);
218 static int egalax_cdev_release(struct inode *inode, struct file *filp)
220 struct egalax_char_dev *cdev; // device information
224 cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
227 TS_DEBUG(" No such char device node \n");
231 atomic_inc(&egalax_char_available); /* release the device */
233 filp->private_data = NULL;
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
236 kfifo_reset( cdev->pDataKFiFo );
238 kfifo_reset( &cdev->DataKFiFo );
241 TS_DEBUG(" egalax_cdev_release done \n");
242 module_put(THIS_MODULE);
246 #define MAX_READ_BUF_LEN 50
247 static char fifo_read_buf[MAX_READ_BUF_LEN];
248 static ssize_t egalax_cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
250 int read_cnt, ret, fifoLen;
251 struct egalax_char_dev *cdev = file->private_data;
255 if( down_interruptible(&cdev->sem) )
258 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
259 fifoLen = kfifo_len(cdev->pDataKFiFo);
261 fifoLen = kfifo_len(&cdev->DataKFiFo);
264 while( fifoLen<1 ) /* nothing to read */
266 up(&cdev->sem); /* release the lock */
267 if( file->f_flags & O_NONBLOCK )
270 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
271 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( cdev->pDataKFiFo )>0) )
273 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( &cdev->DataKFiFo )>0) )
276 return -ERESTARTSYS; /* signal: tell the fs layer to handle it */
279 if( down_interruptible(&cdev->sem) )
283 if(count > MAX_READ_BUF_LEN)
284 count = MAX_READ_BUF_LEN;
286 TS_DEBUG("\"%s\" reading: real fifo data\n", current->comm);
287 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
288 read_cnt = kfifo_get(cdev->pDataKFiFo, fifo_read_buf, count);
290 read_cnt = kfifo_out_locked(&cdev->DataKFiFo, fifo_read_buf, count, &cdev->FiFoLock);
293 ret = copy_to_user(buf, fifo_read_buf, read_cnt)?-EFAULT:read_cnt;
300 static ssize_t egalax_cdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
303 struct egalax_char_dev *cdev = file->private_data;
308 if( down_interruptible(&cdev->sem) )
311 if (count > MAX_I2C_LEN)
314 tmp = kmalloc(count,GFP_KERNEL);
321 if(copy_from_user(tmp, buf, count))
328 ret = i2c_master_normal_send(p_egalax_i2c_dev->client, tmp, count,EETI_I2C_RATE);
329 TS_DEBUG("I2C writing %zu bytes.\n", count);
338 static int wakeup_controller(int gpio)
344 if( (ret=gpio_request(gpio, "Touch Wakeup GPIO"))!=0 )
346 printk(KERN_ERR "[egalax_i2c]: Failed to request GPIO for Touch Wakeup GPIO. Err:%d\n", ret);
351 gpio_direction_output(gpio, 0);
352 for(i=0; i<100; i++);
353 gpio_direction_input(gpio);
354 printk(KERN_ERR "[egalax_i2c]: INT wakeup touch controller done\n");
360 static long egalax_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long args)
362 //struct egalax_char_dev *cdev = file->private_data;
365 if(_IOC_TYPE(cmd) != EGALAX_IOC_MAGIC)
367 if(_IOC_NR(cmd) > EGALAX_IOC_MAXNR)
370 if(_IOC_DIR(cmd) & _IOC_READ)
371 ret = !access_ok(VERIFY_WRITE, (void __user*)args, _IOC_SIZE(cmd));
372 else if(_IOC_DIR(cmd) & _IOC_WRITE)
373 ret = !access_ok(VERIFY_READ, (void __user*)args, _IOC_SIZE(cmd));
378 //printk(KERN_ERR "Handle device ioctl command\n");
381 case EGALAX_IOCWAKEUP:
382 ret = wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
392 static unsigned int egalax_cdev_poll(struct file *filp, struct poll_table_struct *wait)
394 struct egalax_char_dev *cdev = filp->private_data;
395 unsigned int mask = 0;
399 poll_wait(filp, &cdev->fifo_inq, wait);
401 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
402 fifoLen = kfifo_len(cdev->pDataKFiFo);
404 fifoLen = kfifo_len(&cdev->DataKFiFo);
408 mask |= POLLIN | POLLRDNORM; /* readable */
409 if( (FIFO_SIZE - fifoLen) > MAX_I2C_LEN )
410 mask |= POLLOUT | POLLWRNORM; /* writable */
416 #ifndef _NON_INPUT_DEV
417 static int LastUpdateID = 0;
418 static void ProcessReport(unsigned char *buf, int buflen)
421 short X=0, Y=0, ContactID=0, Status=0;
422 if(buflen!=MAX_I2C_LEN || buf[0]!=0x04) // check buffer len & header
425 Status = buf[1]&0x01;
426 ContactID = (buf[1]&0x7C)>>2;
427 X = ((buf[3]<<8) + buf[2])>>4;
428 Y = ((buf[5]<<8) + buf[4])>>4;
430 PointBuf[ContactID].Status = Status;
431 PointBuf[ContactID].X = X;
432 PointBuf[ContactID].Y = Y;
434 TS_DEBUG("Get Point[%d] Update: Status=%d X=%d Y=%d\n", ContactID, Status, X, Y);
437 if( !Status || (ContactID <= LastUpdateID) )
439 for(i=0; i<MAX_SUPPORT_POINT;i++)
441 if(PointBuf[i].Status > 0)
443 input_mt_slot(input_dev, i);
444 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, true);
445 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, PointBuf[i].Status);
446 input_report_abs(input_dev, ABS_MT_POSITION_X, PointBuf[i].X);
447 input_report_abs(input_dev, ABS_MT_POSITION_Y, PointBuf[i].Y);
448 PointBuf[i].Status = 0;
450 else if (PointBuf[i].Status == 0)
452 input_mt_slot(input_dev, i);
453 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
454 PointBuf[i].Status = -1;
457 input_sync(input_dev);
458 TS_DEBUG("Input sync point data done!\n");
461 LastUpdateID = ContactID;
464 static struct input_dev * allocate_Input_Dev(void)
467 struct input_dev *pInputDev=NULL;
469 pInputDev = input_allocate_device();
470 if(pInputDev == NULL)
472 TS_DEBUG("Failed to allocate input device\n");
473 return NULL;//-ENOMEM;
476 pInputDev->name = "eGalax Touch Screen";
477 pInputDev->phys = "I2C";
478 pInputDev->id.bustype = BUS_I2C;
479 pInputDev->id.vendor = 0x0EEF;
480 pInputDev->id.product = 0x0020;
482 __set_bit(INPUT_PROP_DIRECT, pInputDev->propbit);
483 __set_bit(EV_ABS, pInputDev->evbit);
485 input_mt_init_slots(pInputDev, MAX_SUPPORT_POINT);
486 #ifdef CONFIG_RK_CONFIG
487 input_set_abs_params(pInputDev, ABS_MT_POSITION_X, 0, x_max, 0, 0);
488 input_set_abs_params(pInputDev, ABS_MT_POSITION_Y, 0, y_max, 0, 0);
490 input_set_abs_params(pInputDev, ABS_MT_POSITION_X, 0, CONFIG_EETI_EGALAX_MAX_X, 0, 0);
491 input_set_abs_params(pInputDev, ABS_MT_POSITION_Y, 0, CONFIG_EETI_EGALAX_MAX_Y, 0, 0);
493 input_set_abs_params(pInputDev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
495 ret = input_register_device(pInputDev);
498 TS_DEBUG("Unable to register input device.\n");
499 input_free_device(pInputDev);
505 #endif //#ifndef _NON_INPUT_DEV
507 static int egalax_i2c_measure(struct i2c_client *client, char skip_packet)
509 u8 x_buf[MAX_I2C_LEN];
513 count = i2c_master_normal_recv(client, x_buf, MAX_I2C_LEN,EETI_I2C_RATE);
514 }while(count==EAGAIN && --loop);
516 if( count<0 || (x_buf[0]!=REPORTID_VENDOR && x_buf[0]!=REPORTID_MTOUCH) )
518 TS_DEBUG("I2C read error data with Len=%d hedaer=%d\n", count, x_buf[0]);
522 TS_DEBUG("egalax_i2c read data with Len=%d\n", count);
523 if(x_buf[0]==REPORTID_VENDOR)
524 TS_DEBUG("egalax_i2c get command packet\n");
526 if( skip_packet > 0 )
529 #ifndef _NON_INPUT_DEV
530 if( count>0 && x_buf[0]==REPORTID_MTOUCH )
532 ProcessReport(x_buf, count);
536 #endif //#ifndef _NON_INPUT_DEV
538 if( count>0 && p_char_dev->OpenCnts>0 ) // If someone reading now! put the data into the buffer!
540 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
541 kfifo_put(p_char_dev->pDataKFiFo, x_buf, count);
543 kfifo_in_locked(&p_char_dev->DataKFiFo, x_buf, count, &p_char_dev->FiFoLock);
545 wake_up_interruptible( &p_char_dev->fifo_inq );
551 static void egalax_i2c_wq(struct work_struct *work)
553 struct _egalax_i2c *egalax_i2c = container_of(work, struct _egalax_i2c, work);
554 struct i2c_client *client = egalax_i2c->client;
555 int gpio = client->irq;
556 TS_DEBUG("egalax_i2c_wq run\n");
557 mutex_lock(&egalax_i2c->mutex_wq);
559 /*continue recv data*/
560 while( !gpio_get_value(gpio) && egalax_i2c->work_state>0 )
562 egalax_i2c_measure(client, egalax_i2c->skip_packet);
563 schedule_timeout_interruptible(HZ/100);
566 #ifndef _NON_INPUT_DEV
567 if (gpio_get_value(gpio) && egalax_i2c->work_state > 0 && !egalax_i2c->skip_packet) {
569 for(i = 0; i < MAX_SUPPORT_POINT; i++) {
570 if (PointBuf[i].Status > 0) {
571 TS_DEBUG("point %d still down\n", i);
572 input_mt_slot(input_dev, i);
573 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, true);
574 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, 0);
575 input_report_abs(input_dev, ABS_MT_POSITION_X, PointBuf[i].X);
576 input_report_abs(input_dev, ABS_MT_POSITION_Y, PointBuf[i].Y);
577 PointBuf[i].Status = 0;
579 else if (PointBuf[i].Status == 0)
581 input_mt_slot(input_dev, i);
582 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
583 PointBuf[i].Status = -1;
586 input_sync(input_dev);
590 if( egalax_i2c->skip_packet > 0 )
591 egalax_i2c->skip_packet = 0;
593 mutex_unlock(&egalax_i2c->mutex_wq);
595 enable_irq(p_egalax_i2c_dev->irq);
597 TS_DEBUG("egalax_i2c_wq leave\n");
600 static irqreturn_t egalax_i2c_interrupt(int irq, void *dev_id)
602 struct _egalax_i2c *egalax_i2c = (struct _egalax_i2c *)dev_id;
603 TS_DEBUG("egalax_i2c_interrupt with irq:%d\n", irq);
604 disable_irq_nosync(irq);
605 queue_work(egalax_i2c->ktouch_wq, &egalax_i2c->work);
611 void egalax_i2c_set_standby(struct i2c_client *client, int enable)
613 #ifndef CONFIG_RK_CONFIG
614 struct eeti_egalax_platform_data *mach_info = client->dev.platform_data;
615 unsigned display_on = mach_info->disp_on_pin;
616 unsigned lcd_standby = mach_info->standby_pin;
618 int display_on_pol = mach_info->disp_on_value;
619 int lcd_standby_pol = mach_info->standby_value;
620 printk("%s : %s, enable = %d", __FILE__, __FUNCTION__,enable);
621 if(display_on != INVALID_GPIO)
623 gpio_direction_output(display_on, 0);
624 gpio_set_value(display_on, enable ? display_on_pol : !display_on_pol);
626 if(lcd_standby != INVALID_GPIO)
628 gpio_direction_output(lcd_standby, 0);
629 gpio_set_value(lcd_standby, enable ? lcd_standby_pol : !lcd_standby_pol);
635 static int egalax_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
637 struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
638 u8 cmdbuf[MAX_I2C_LEN]={0x03, 0x05, 0x0A, 0x03, 0x36, 0x3F, 0x02, 0, 0, 0};
640 i2c_master_normal_send(client, cmdbuf, MAX_I2C_LEN, EETI_I2C_RATE);
642 disable_irq(p_egalax_i2c_dev->irq);
643 egalax_i2c->work_state = 0;
644 if (cancel_work_sync(&egalax_i2c->work)) {
645 /* if work was pending disable-count is now 2 */
646 pr_info("%s: work was pending\n", __func__);
647 enable_irq(p_egalax_i2c_dev->irq);
650 printk(KERN_DEBUG "[egalax_i2c]: device suspend done\n");
652 if(device_may_wakeup(&client->dev))
654 enable_irq_wake(p_egalax_i2c_dev->irq);
658 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
660 egalax_i2c_set_standby(client, 0);
664 static int egalax_i2c_resume(struct i2c_client *client)
666 struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
667 egalax_i2c_set_standby(client, 1);
668 if(device_may_wakeup(&client->dev))
670 disable_irq_wake(p_egalax_i2c_dev->irq);
674 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
677 wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
678 egalax_i2c->work_state = 1;
679 enable_irq(p_egalax_i2c_dev->irq);
681 printk(KERN_DEBUG "[egalax_i2c]: device wakeup done\n");
686 #ifdef CONFIG_HAS_EARLYSUSPEND
688 static void egalax_i2c_early_suspend(struct early_suspend *h)
690 pm_message_t mesg = {.event = 0};
691 struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
692 struct i2c_client *client = info->egalax_i2c->client;
693 egalax_i2c_suspend(client,mesg);
696 static void egalax_i2c_early_resume(struct early_suspend *h)
699 struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
700 struct i2c_client *client = info->egalax_i2c->client;
702 egalax_i2c_resume(client);
711 #define egalax_i2c_suspend NULL
712 #define egalax_i2c_resume NULL
715 #ifdef CONFIG_HAS_EARLYSUSPEND
716 static struct suspend_info suspend_info = {
717 .early_suspend.suspend = egalax_i2c_early_suspend,
718 .early_suspend.resume = egalax_i2c_early_resume,
719 .early_suspend.level = EETI_EARLYSUSPEND_LEVEL,
724 static int __devinit egalax_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
728 #ifdef CONFIG_RK_CONFIG
729 struct port_config irq_cfg = get_port_config(irq);
731 client->irq = irq_cfg.gpio;
734 struct eeti_egalax_platform_data *pdata = pdata = client->dev.platform_data;
735 if (pdata->init_platform_hw)
736 pdata->init_platform_hw();
738 printk(KERN_DEBUG "[egalax_i2c]: start probe\n");
741 p_egalax_i2c_dev = (struct _egalax_i2c *)kzalloc(sizeof(struct _egalax_i2c), GFP_KERNEL);
742 if (!p_egalax_i2c_dev)
744 printk(KERN_ERR "[egalax_i2c]: request memory failed\n");
749 #ifndef _NON_INPUT_DEV
750 input_dev = allocate_Input_Dev();
753 printk(KERN_ERR "[egalax_i2c]: allocate_Input_Dev failed\n");
757 TS_DEBUG("egalax_i2c register input device done\n");
758 memset(PointBuf, 0, sizeof(struct point_data)*MAX_SUPPORT_POINT);
759 #endif //#ifndef _NON_INPUT_DEV
762 p_egalax_i2c_dev->client = client;
763 mutex_init(&p_egalax_i2c_dev->mutex_wq);
765 p_egalax_i2c_dev->ktouch_wq = create_workqueue("egalax_touch_wq");
766 INIT_WORK(&p_egalax_i2c_dev->work, egalax_i2c_wq);
768 i2c_set_clientdata(client, p_egalax_i2c_dev);
770 if( gpio_get_value(gpio) )
771 p_egalax_i2c_dev->skip_packet = 0;
773 p_egalax_i2c_dev->skip_packet = 1;
775 p_egalax_i2c_dev->work_state = 1;
777 p_egalax_i2c_dev->irq = gpio_to_irq(client->irq);
778 #ifdef CONFIG_RK_CONFIG
779 ret = request_irq(p_egalax_i2c_dev->irq, egalax_i2c_interrupt, irq_cfg.irq.irq_flags,
780 client->name, p_egalax_i2c_dev);
782 ret = request_irq(p_egalax_i2c_dev->irq, egalax_i2c_interrupt, IRQF_TRIGGER_LOW,
783 client->name, p_egalax_i2c_dev);
787 printk(KERN_ERR "[egalax_i2c]: request irq(%d) failed\n", p_egalax_i2c_dev->irq);
790 TS_DEBUG("egalax_i2c request irq(%d) gpio(%d) with result:%d\n", p_egalax_i2c_dev->irq, gpio, ret);
793 device_init_wakeup(&client->dev, 0);
796 #ifdef CONFIG_HAS_EARLYSUSPEND
799 suspend_info.egalax_i2c = p_egalax_i2c_dev;
800 register_early_suspend(&suspend_info.early_suspend);
803 printk(KERN_DEBUG "[egalax_i2c]: probe done\n");
807 i2c_set_clientdata(client, NULL);
808 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq);
809 free_irq(p_egalax_i2c_dev->irq, p_egalax_i2c_dev);
810 #ifndef _NON_INPUT_DEV
811 input_unregister_device(input_dev);
812 input_free_device(input_dev);
814 #endif //#ifndef _NON_INPUT_DEV
817 kfree(p_egalax_i2c_dev);
818 p_egalax_i2c_dev = NULL;
820 printk(KERN_DEBUG "[egalax_i2c]: probe failed\n");
824 static int __devexit egalax_i2c_remove(struct i2c_client *client)
826 struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
830 #ifndef _NON_INPUT_DEV
833 TS_DEBUG("unregister input device\n");
834 input_unregister_device(input_dev);
835 input_free_device(input_dev);
838 #endif //#ifndef _NON_INPUT_DEV
840 if(p_egalax_i2c_dev->ktouch_wq)
842 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq);
845 if(p_egalax_i2c_dev->irq)
847 free_irq(p_egalax_i2c_dev->irq, egalax_i2c);
850 i2c_set_clientdata(client, NULL);
852 p_egalax_i2c_dev = NULL;
857 static struct i2c_device_id egalax_i2c_idtable[] = {
858 { TP_MODULE_NAME, 0 },
862 MODULE_DEVICE_TABLE(i2c, egalax_i2c_idtable);
864 static struct i2c_driver egalax_i2c_driver = {
866 .name = TP_MODULE_NAME,
868 .id_table = egalax_i2c_idtable,
869 .probe = egalax_i2c_probe,
870 .remove = __devexit_p(egalax_i2c_remove),
871 //.suspend = egalax_i2c_suspend,
872 //.resume = egalax_i2c_resume,
875 static const struct file_operations egalax_cdev_fops = {
876 .owner = THIS_MODULE,
877 .read = egalax_cdev_read,
878 .write = egalax_cdev_write,
879 .unlocked_ioctl = egalax_cdev_ioctl,
880 .poll = egalax_cdev_poll,
881 .open = egalax_cdev_open,
882 .release= egalax_cdev_release,
885 static void egalax_i2c_ts_exit(void)
887 dev_t devno = MKDEV(global_major, global_minor);
892 TS_DEBUG("unregister character device\n");
893 if( p_char_dev->pFiFoBuf )
894 kfree(p_char_dev->pFiFoBuf);
896 cdev_del(&p_char_dev->cdev);
901 unregister_chrdev_region( devno, 1);
903 if(!IS_ERR(egalax_class))
905 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
906 class_device_destroy(egalax_class, devno);
908 device_destroy(egalax_class, devno);
910 class_destroy(egalax_class);
913 i2c_del_driver(&egalax_i2c_driver);
915 printk(KERN_DEBUG "[egalax_i2c]: driver exit\n");
918 static struct egalax_char_dev* setup_chardev(dev_t dev)
920 struct egalax_char_dev *pCharDev;
923 pCharDev = kmalloc(1*sizeof(struct egalax_char_dev), GFP_KERNEL);
926 memset(pCharDev, 0, sizeof(struct egalax_char_dev));
928 spin_lock_init( &pCharDev->FiFoLock );
929 pCharDev->pFiFoBuf = kmalloc(sizeof(unsigned char)*FIFO_SIZE, GFP_KERNEL);
930 if(!pCharDev->pFiFoBuf)
932 memset(pCharDev->pFiFoBuf, 0, sizeof(unsigned char)*FIFO_SIZE);
934 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
935 pCharDev->pDataKFiFo = kfifo_init(pCharDev->pFiFoBuf, FIFO_SIZE, GFP_KERNEL, &pCharDev->FiFoLock);
936 if( pCharDev->pDataKFiFo==NULL )
939 kfifo_init(&pCharDev->DataKFiFo, pCharDev->pFiFoBuf, FIFO_SIZE);
940 if( !kfifo_initialized(&pCharDev->DataKFiFo) )
944 pCharDev->OpenCnts = 0;
945 cdev_init(&pCharDev->cdev, &egalax_cdev_fops);
946 pCharDev->cdev.owner = THIS_MODULE;
947 sema_init(&pCharDev->sem, 1);
948 init_waitqueue_head(&pCharDev->fifo_inq);
950 result = cdev_add(&pCharDev->cdev, dev, 1);
953 TS_DEBUG(KERN_ERR "Error cdev ioctldev added\n");
960 kfree(pCharDev->pFiFoBuf);
967 static void __init egalax_i2c_ts_init_async(void *unused, async_cookie_t cookie)
974 // Asking for a dynamic major unless directed otherwise at load time.
977 devno = MKDEV(global_major, global_minor);
978 result = register_chrdev_region(devno, 1, "egalax_i2c");
982 result = alloc_chrdev_region(&devno, global_minor, 1, "egalax_i2c");
983 global_major = MAJOR(devno);
988 TS_DEBUG(" egalax_i2c cdev can't get major number\n");
992 // allocate the character device
993 p_char_dev = setup_chardev(devno);
1000 egalax_class = class_create(THIS_MODULE, "egalax_i2c");
1001 if(IS_ERR(egalax_class))
1003 TS_DEBUG("Err: failed in creating class.\n");
1008 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
1009 class_device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
1011 device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
1013 TS_DEBUG("register egalax_i2c cdev, major: %d \n",global_major);
1015 printk(KERN_DEBUG "[egalax_i2c]: init done\n");
1016 i2c_add_driver(&egalax_i2c_driver);
1020 egalax_i2c_ts_exit();
1023 static int __init egalax_i2c_ts_init(void)
1025 #ifdef CONFIG_RK_CONFIG
1026 int ret = tp_board_init();
1031 async_schedule(egalax_i2c_ts_init_async, NULL);
1034 module_init(egalax_i2c_ts_init);
1035 module_exit(egalax_i2c_ts_exit);
1037 MODULE_DESCRIPTION("egalax touch screen i2c driver");
1038 MODULE_LICENSE("GPL");