Merge tag 'v4.4.3'
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / eeti_egalax_i2c.c
1 /*
2  *
3  * Touch Screen I2C Driver for EETI Controller
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 // Release Date: 2010/11/08
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/interrupt.h>
23 #include <linux/wait.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/freezer.h>
27 #include <linux/proc_fs.h>
28 #include <linux/clk.h>
29 #include <linux/i2c.h>
30 #include <linux/gpio.h>
31 #include <linux/device.h>
32 #include <linux/cdev.h>
33 #include <asm/io.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <linux/poll.h>
37 #include <linux/kfifo.h>
38 #include <linux/version.h>
39 #include <linux/input.h>
40 #include <linux/input/mt.h>
41 #include <linux/irq.h>
42 #include <linux/async.h>
43 #include <mach/board.h>
44 #ifdef CONFIG_RK_CONFIG
45 #include <mach/config.h>
46 #endif
47
48 #ifdef CONFIG_HAS_EARLYSUSPEND
49 #include <linux/earlysuspend.h>
50 #endif
51
52 #define TP_MODULE_NAME  "egalax_i2c"
53 #ifdef CONFIG_RK_CONFIG
54
55 enum {
56 #if defined(RK2926_TB_DEFAULT_CONFIG) || defined(RK2928_TB_DEFAULT_CONFIG)
57         DEF_EN = 1,
58 #else
59         DEF_EN = 0,
60 #endif
61 #if defined(RK2926_TB_DEFAULT_CONFIG)
62         DEF_IRQ = 0x008001b0,
63         DEF_RST = 0X000002b0,
64 #elif defined(RK2928_TB_DEFAULT_CONFIG)
65         DEF_IRQ = 0x008003c7,
66         DEF_RST = 0X000003c3,
67 #else
68         DEF_IRQ = -1,
69         DEF_RST = -1,
70 #endif
71         DEF_I2C = 2, 
72         DEF_ADDR = 0x04,
73         DEF_X_MAX = 1087,
74         DEF_Y_MAX = 800,
75 };
76 static int en = DEF_EN;
77 module_param(en, int, 0644);
78
79 static int irq = DEF_IRQ;
80 module_param(irq, int, 0644);
81 static int rst =DEF_RST;
82 module_param(rst, int, 0644);
83
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);
92
93 static int tp_hw_init(void)
94 {
95         int ret = 0;
96
97         ret = gpio_request(get_port_config(irq).gpio, "tp_irq");
98         if(ret < 0){
99                 printk("%s: gpio_request(irq gpio) failed\n", __func__);
100                 return ret;
101         }
102
103         ret = port_output_init(rst, 0, "tp_rst");
104         if(ret < 0){
105                 printk("%s: port(rst) output init faild\n", __func__);
106                 return ret;
107         }
108         port_output_on(rst);
109
110          return 0;
111 }
112 #include "rk_tp.c"
113 #endif
114
115 //#define DEBUG
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__)
119 #else
120         #define TS_DEBUG(fmt,args...)
121         #define DBG()
122 #endif
123
124
125 //#define _NON_INPUT_DEV // define this to disable register input device        
126
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
136
137 /// ioctl command ///
138 #define EGALAX_IOC_MAGIC        0x72
139 #define EGALAX_IOCWAKEUP        _IO(EGALAX_IOC_MAGIC, 1)
140 #define EGALAX_IOC_MAXNR        1
141
142 #define EETI_EARLYSUSPEND_LEVEL (EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1)
143
144 struct point_data {
145         short Status;
146         short X;
147         short Y;
148 };
149
150 struct _egalax_i2c {
151         struct workqueue_struct *ktouch_wq;
152         struct work_struct work;
153         struct mutex mutex_wq;
154         struct i2c_client *client;
155         char work_state;
156         char skip_packet;
157         int irq;
158 };
159
160 #ifdef CONFIG_HAS_EARLYSUSPEND 
161 struct suspend_info {
162         struct early_suspend early_suspend;
163         struct _egalax_i2c *egalax_i2c;
164 };
165 #endif
166
167 struct egalax_char_dev
168 {
169         int OpenCnts;
170         struct cdev cdev;
171 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
172         struct kfifo* pDataKFiFo;
173 #else
174         struct kfifo DataKFiFo;
175 #endif
176         unsigned char *pFiFoBuf;
177         spinlock_t FiFoLock;
178         struct semaphore sem;
179         wait_queue_head_t fifo_inq;
180 };
181
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
190
191 static int egalax_cdev_open(struct inode *inode, struct file *filp)
192 {
193         struct egalax_char_dev *cdev;
194
195         DBG();
196
197         cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
198         if( cdev == NULL )
199         {
200                 TS_DEBUG(" No such char device node \n");
201                 return -ENODEV;
202         }
203         
204         if( !atomic_dec_and_test(&egalax_char_available) )
205         {
206                 atomic_inc(&egalax_char_available);
207                 return -EBUSY; /* already open */
208         }
209
210         cdev->OpenCnts++;
211         filp->private_data = cdev;// Used by the read and write metheds
212
213         TS_DEBUG(" egalax_cdev_open done \n");
214         try_module_get(THIS_MODULE);
215         return 0;
216 }
217
218 static int egalax_cdev_release(struct inode *inode, struct file *filp)
219 {
220         struct egalax_char_dev *cdev; // device information
221
222         DBG();
223
224         cdev = container_of(inode->i_cdev, struct egalax_char_dev, cdev);
225         if( cdev == NULL )
226         {
227                 TS_DEBUG(" No such char device node \n");
228                 return -ENODEV;
229         }
230
231         atomic_inc(&egalax_char_available); /* release the device */
232
233         filp->private_data = NULL;
234         cdev->OpenCnts--;
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
236         kfifo_reset( cdev->pDataKFiFo );
237 #else
238         kfifo_reset( &cdev->DataKFiFo );
239 #endif
240
241         TS_DEBUG(" egalax_cdev_release done \n");
242         module_put(THIS_MODULE);
243         return 0;
244 }
245
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)
249 {
250         int read_cnt, ret, fifoLen;
251         struct egalax_char_dev *cdev = file->private_data;
252
253         DBG();
254         
255         if( down_interruptible(&cdev->sem) )
256                 return -ERESTARTSYS;
257
258 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
259         fifoLen = kfifo_len(cdev->pDataKFiFo);
260 #else
261         fifoLen = kfifo_len(&cdev->DataKFiFo);
262 #endif
263
264         while( fifoLen<1 ) /* nothing to read */
265         {
266                 up(&cdev->sem); /* release the lock */
267                 if( file->f_flags & O_NONBLOCK )
268                         return -EAGAIN;
269
270         #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
271                 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( cdev->pDataKFiFo )>0) )
272         #else
273                 if( wait_event_interruptible(cdev->fifo_inq, kfifo_len( &cdev->DataKFiFo )>0) )
274         #endif
275                 {
276                         return -ERESTARTSYS; /* signal: tell the fs layer to handle it */
277                 }
278
279                 if( down_interruptible(&cdev->sem) )
280                         return -ERESTARTSYS;
281         }
282
283         if(count > MAX_READ_BUF_LEN)
284                 count = MAX_READ_BUF_LEN;
285
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);
289 #else
290         read_cnt = kfifo_out_locked(&cdev->DataKFiFo, fifo_read_buf, count, &cdev->FiFoLock);
291 #endif
292
293         ret = copy_to_user(buf, fifo_read_buf, read_cnt)?-EFAULT:read_cnt;
294
295         up(&cdev->sem);
296         
297         return ret;
298 }
299
300 static ssize_t egalax_cdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
301 {
302         char *tmp;
303         struct egalax_char_dev *cdev = file->private_data;
304         int ret=0;
305
306         DBG();
307
308         if( down_interruptible(&cdev->sem) )
309                 return -ERESTARTSYS;
310
311         if (count > MAX_I2C_LEN)
312                 count = MAX_I2C_LEN;
313
314         tmp = kmalloc(count,GFP_KERNEL);
315         if(tmp==NULL)
316         {
317                 up(&cdev->sem);
318                 return -ENOMEM;
319         }
320
321         if(copy_from_user(tmp, buf, count))
322         {
323                 up(&cdev->sem);
324                 kfree(tmp);
325                 return -EFAULT;
326         }
327         
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);
330
331         kfree(tmp);
332
333         up(&cdev->sem);
334
335         return ret;
336 }
337
338 static int wakeup_controller(int gpio)
339 {
340         int ret=0, i;
341
342         gpio_free(gpio);
343
344         if( (ret=gpio_request(gpio, "Touch Wakeup GPIO"))!=0 )
345         {
346                 printk(KERN_ERR "[egalax_i2c]: Failed to request GPIO for Touch Wakeup GPIO. Err:%d\n", ret);
347                 ret = -EFAULT;
348         }
349         else
350         {
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");
355         }
356         
357         return ret;
358 }
359
360 static long egalax_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long args)
361 {       
362         //struct egalax_char_dev *cdev = file->private_data;
363         int ret=0;
364
365         if(_IOC_TYPE(cmd) != EGALAX_IOC_MAGIC)
366                 return -ENOTTY;
367         if(_IOC_NR(cmd) > EGALAX_IOC_MAXNR)
368                 return -ENOTTY;
369
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));
374
375         if(ret)
376                 return -EFAULT;
377
378         //printk(KERN_ERR "Handle device ioctl command\n");
379         switch (cmd)
380         {
381                 case EGALAX_IOCWAKEUP:
382                         ret = wakeup_controller(irq_to_gpio(p_egalax_i2c_dev->irq));
383                         break;
384                 default:
385                         ret = -ENOTTY;
386                         break;
387         }
388
389         return ret;
390 }
391
392 static unsigned int egalax_cdev_poll(struct file *filp, struct poll_table_struct *wait)
393 {
394         struct egalax_char_dev *cdev = filp->private_data;
395         unsigned int mask = 0;
396         int fifoLen;
397         
398         down(&cdev->sem);
399         poll_wait(filp, &cdev->fifo_inq,  wait);
400
401 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
402         fifoLen = kfifo_len(cdev->pDataKFiFo);
403 #else
404         fifoLen = kfifo_len(&cdev->DataKFiFo);
405 #endif
406
407         if( fifoLen > 0 )
408                 mask |= POLLIN | POLLRDNORM;    /* readable */
409         if( (FIFO_SIZE - fifoLen) > MAX_I2C_LEN )
410                 mask |= POLLOUT | POLLWRNORM;   /* writable */
411
412         up(&cdev->sem);
413         return mask;
414 }
415
416 #ifndef _NON_INPUT_DEV
417 static int LastUpdateID = 0;
418 static void ProcessReport(unsigned char *buf, int buflen)
419 {
420         int i;
421         short X=0, Y=0, ContactID=0, Status=0;
422         if(buflen!=MAX_I2C_LEN || buf[0]!=0x04) // check buffer len & header
423                 return;
424
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;
429         
430         PointBuf[ContactID].Status = Status;
431         PointBuf[ContactID].X = X;
432         PointBuf[ContactID].Y = Y;
433
434         TS_DEBUG("Get Point[%d] Update: Status=%d X=%d Y=%d\n", ContactID, Status, X, Y);
435
436         // Send point report
437         if( !Status || (ContactID <= LastUpdateID) )
438         {
439                 for(i=0; i<MAX_SUPPORT_POINT;i++)
440                 {
441                         if(PointBuf[i].Status > 0)
442                         {
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;
449                         }
450                         else if (PointBuf[i].Status == 0)
451                         {
452                                 input_mt_slot(input_dev, i);
453                                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
454                                 PointBuf[i].Status = -1;
455                         }
456                 }
457                 input_sync(input_dev);
458                 TS_DEBUG("Input sync point data done!\n");
459         }
460
461         LastUpdateID = ContactID;
462 }
463
464 static struct input_dev * allocate_Input_Dev(void)
465 {
466         int ret;
467         struct input_dev *pInputDev=NULL;
468
469         pInputDev = input_allocate_device();
470         if(pInputDev == NULL)
471         {
472                 TS_DEBUG("Failed to allocate input device\n");
473                 return NULL;//-ENOMEM;
474         }
475
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;
481
482         __set_bit(INPUT_PROP_DIRECT, pInputDev->propbit);
483         __set_bit(EV_ABS, pInputDev->evbit);
484
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);
489 #else
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);
492 #endif
493         input_set_abs_params(pInputDev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
494
495         ret = input_register_device(pInputDev);
496         if(ret) 
497         {
498                 TS_DEBUG("Unable to register input device.\n");
499                 input_free_device(pInputDev);
500                 return NULL;
501         }
502         
503         return pInputDev;
504 }
505 #endif //#ifndef _NON_INPUT_DEV
506
507 static int egalax_i2c_measure(struct i2c_client *client, char skip_packet)
508 {
509         u8 x_buf[MAX_I2C_LEN];
510         int count, loop=3;
511         DBG();
512         do{
513                 count = i2c_master_normal_recv(client, x_buf, MAX_I2C_LEN,EETI_I2C_RATE);
514         }while(count==EAGAIN && --loop);
515
516         if( count<0 || (x_buf[0]!=REPORTID_VENDOR && x_buf[0]!=REPORTID_MTOUCH) )
517         {
518                 TS_DEBUG("I2C read error data with Len=%d hedaer=%d\n", count, x_buf[0]);
519                 return -1;
520         }
521
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");
525
526         if( skip_packet > 0 )
527                 return count;
528
529 #ifndef _NON_INPUT_DEV
530         if( count>0 && x_buf[0]==REPORTID_MTOUCH )
531         {
532                 ProcessReport(x_buf, count);
533
534                 return count;
535         }
536 #endif //#ifndef _NON_INPUT_DEV
537
538         if( count>0 && p_char_dev->OpenCnts>0 ) // If someone reading now! put the data into the buffer!
539         {
540         #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
541                 kfifo_put(p_char_dev->pDataKFiFo, x_buf, count);
542         #else
543                 kfifo_in_locked(&p_char_dev->DataKFiFo, x_buf, count, &p_char_dev->FiFoLock);
544         #endif
545                 wake_up_interruptible( &p_char_dev->fifo_inq );
546         }
547
548         return count;
549 }
550
551 static void egalax_i2c_wq(struct work_struct *work)
552 {
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);
558
559         /*continue recv data*/
560         while( !gpio_get_value(gpio) && egalax_i2c->work_state>0 )
561         {
562                 egalax_i2c_measure(client, egalax_i2c->skip_packet);
563                 schedule_timeout_interruptible(HZ/100);
564         }
565
566 #ifndef _NON_INPUT_DEV
567         if (gpio_get_value(gpio) && egalax_i2c->work_state > 0 && !egalax_i2c->skip_packet) {
568                 int i;
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;
578                         }
579                         else if (PointBuf[i].Status == 0)
580                         {
581                                 input_mt_slot(input_dev, i);
582                                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
583                                 PointBuf[i].Status = -1;
584                         }
585                 }
586                 input_sync(input_dev);
587         }
588 #endif
589
590         if( egalax_i2c->skip_packet > 0 )
591                 egalax_i2c->skip_packet = 0;
592
593         mutex_unlock(&egalax_i2c->mutex_wq);
594
595         enable_irq(p_egalax_i2c_dev->irq);
596
597         TS_DEBUG("egalax_i2c_wq leave\n");
598 }
599
600 static irqreturn_t egalax_i2c_interrupt(int irq, void *dev_id)
601 {
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);
606
607         return IRQ_HANDLED;
608 }
609
610
611 void egalax_i2c_set_standby(struct i2c_client *client, int enable)
612 {
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;
617
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)
622     {
623         gpio_direction_output(display_on, 0);
624         gpio_set_value(display_on, enable ? display_on_pol : !display_on_pol);                          
625     }
626     if(lcd_standby != INVALID_GPIO)
627     {
628         gpio_direction_output(lcd_standby, 0);
629         gpio_set_value(lcd_standby, enable ? lcd_standby_pol : !lcd_standby_pol);                         
630     }
631 #endif
632 }
633
634 #ifdef CONFIG_PM
635 static int egalax_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
636 {
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};
639         
640         i2c_master_normal_send(client, cmdbuf, MAX_I2C_LEN, EETI_I2C_RATE);
641
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);
648         }
649
650         printk(KERN_DEBUG "[egalax_i2c]: device suspend done\n");       
651
652         if(device_may_wakeup(&client->dev)) 
653         {
654                 enable_irq_wake(p_egalax_i2c_dev->irq);
655         }
656         else 
657         {
658                 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
659         }
660         egalax_i2c_set_standby(client, 0);
661         return 0;
662 }
663
664 static int egalax_i2c_resume(struct i2c_client *client)
665 {
666         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
667         egalax_i2c_set_standby(client, 1);      
668         if(device_may_wakeup(&client->dev)) 
669         {
670                 disable_irq_wake(p_egalax_i2c_dev->irq);
671         }
672         else 
673         {
674                 printk(KERN_DEBUG "[egalax_i2c]: device_may_wakeup false\n");
675         }
676
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);
680
681         printk(KERN_DEBUG "[egalax_i2c]: device wakeup done\n");
682
683         return 0;
684 }
685
686 #ifdef CONFIG_HAS_EARLYSUSPEND 
687
688 static void egalax_i2c_early_suspend(struct early_suspend *h)
689 {
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);
694
695 }
696 static void egalax_i2c_early_resume(struct early_suspend *h)
697 {
698    
699     struct suspend_info *info = container_of(h,struct suspend_info,early_suspend);
700     struct i2c_client *client = info->egalax_i2c->client;
701
702     egalax_i2c_resume(client);
703
704 }
705
706 #endif
707
708
709
710 #else
711 #define egalax_i2c_suspend       NULL
712 #define egalax_i2c_resume        NULL
713 #endif
714
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,
720 };
721 #endif
722
723
724 static int __devinit egalax_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
725 {
726         int ret;
727         int gpio;
728 #ifdef CONFIG_RK_CONFIG
729         struct port_config irq_cfg = get_port_config(irq);
730
731         client->irq = irq_cfg.gpio;
732         tp_hw_init();
733 #else
734         struct eeti_egalax_platform_data *pdata = pdata = client->dev.platform_data;
735         if (pdata->init_platform_hw)
736                 pdata->init_platform_hw();
737 #endif
738         printk(KERN_DEBUG "[egalax_i2c]: start probe\n");
739         gpio = client->irq;
740
741         p_egalax_i2c_dev = (struct _egalax_i2c *)kzalloc(sizeof(struct _egalax_i2c), GFP_KERNEL);
742         if (!p_egalax_i2c_dev) 
743         {
744                 printk(KERN_ERR "[egalax_i2c]: request memory failed\n");
745                 ret = -ENOMEM;
746                 goto fail1;
747         }
748
749 #ifndef _NON_INPUT_DEV
750         input_dev = allocate_Input_Dev();
751         if(input_dev==NULL)
752         {
753                 printk(KERN_ERR "[egalax_i2c]: allocate_Input_Dev failed\n");
754                 ret = -EINVAL; 
755                 goto fail2;
756         }
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
760
761
762         p_egalax_i2c_dev->client = client;
763         mutex_init(&p_egalax_i2c_dev->mutex_wq);
764
765         p_egalax_i2c_dev->ktouch_wq = create_workqueue("egalax_touch_wq"); 
766         INIT_WORK(&p_egalax_i2c_dev->work, egalax_i2c_wq);
767         
768         i2c_set_clientdata(client, p_egalax_i2c_dev);
769
770         if( gpio_get_value(gpio) )
771                 p_egalax_i2c_dev->skip_packet = 0;
772         else
773                 p_egalax_i2c_dev->skip_packet = 1;
774
775         p_egalax_i2c_dev->work_state = 1;
776         
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);
781 #else
782         ret = request_irq(p_egalax_i2c_dev->irq, egalax_i2c_interrupt, IRQF_TRIGGER_LOW,
783                  client->name, p_egalax_i2c_dev);
784 #endif
785         if( ret ) 
786         {
787                 printk(KERN_ERR "[egalax_i2c]: request irq(%d) failed\n", p_egalax_i2c_dev->irq);
788                 goto fail3;
789         }
790         TS_DEBUG("egalax_i2c request irq(%d) gpio(%d) with result:%d\n", p_egalax_i2c_dev->irq, gpio, ret);
791
792 #ifdef CONFIG_PM
793         device_init_wakeup(&client->dev, 0);
794 #endif
795
796 #ifdef CONFIG_HAS_EARLYSUSPEND
797
798
799         suspend_info.egalax_i2c = p_egalax_i2c_dev;
800         register_early_suspend(&suspend_info.early_suspend);
801 #endif
802
803         printk(KERN_DEBUG "[egalax_i2c]: probe done\n");
804         return 0;
805
806 fail3:
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);
813         input_dev = NULL;
814 #endif //#ifndef _NON_INPUT_DEV
815 fail2:
816 fail1:
817         kfree(p_egalax_i2c_dev);
818         p_egalax_i2c_dev = NULL;
819
820         printk(KERN_DEBUG "[egalax_i2c]: probe failed\n");
821         return ret;
822 }
823
824 static int __devexit egalax_i2c_remove(struct i2c_client *client)
825 {
826         struct _egalax_i2c *egalax_i2c = i2c_get_clientdata(client);
827
828         DBG();
829
830 #ifndef _NON_INPUT_DEV
831         if(input_dev)
832         {
833                 TS_DEBUG("unregister input device\n");
834                 input_unregister_device(input_dev);
835                 input_free_device(input_dev);
836                 input_dev = NULL;
837         }
838 #endif //#ifndef _NON_INPUT_DEV
839
840         if(p_egalax_i2c_dev->ktouch_wq) 
841         {
842                 destroy_workqueue(p_egalax_i2c_dev->ktouch_wq); 
843         }
844
845         if(p_egalax_i2c_dev->irq)
846         {
847                 free_irq(p_egalax_i2c_dev->irq, egalax_i2c);
848         }
849
850         i2c_set_clientdata(client, NULL);
851         kfree(egalax_i2c);
852         p_egalax_i2c_dev = NULL;
853
854         return 0;
855 }
856
857 static struct i2c_device_id egalax_i2c_idtable[] = { 
858         { TP_MODULE_NAME, 0 }, 
859         { } 
860 }; 
861
862 MODULE_DEVICE_TABLE(i2c, egalax_i2c_idtable);
863
864 static struct i2c_driver egalax_i2c_driver = {
865         .driver = {
866                 .name   = TP_MODULE_NAME,
867         },
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,
873 };
874
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,
883 };
884
885 static void egalax_i2c_ts_exit(void)
886 {
887         dev_t devno = MKDEV(global_major, global_minor);
888         DBG();
889
890         if(p_char_dev)
891         {
892                 TS_DEBUG("unregister character device\n");
893                 if( p_char_dev->pFiFoBuf )
894                         kfree(p_char_dev->pFiFoBuf);
895         
896                 cdev_del(&p_char_dev->cdev);
897                 kfree(p_char_dev);
898                 p_char_dev = NULL;
899         }
900
901         unregister_chrdev_region( devno, 1);
902
903         if(!IS_ERR(egalax_class))
904         {
905 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
906                 class_device_destroy(egalax_class, devno);
907 #else
908                 device_destroy(egalax_class, devno);
909 #endif 
910                 class_destroy(egalax_class);
911         }
912         
913         i2c_del_driver(&egalax_i2c_driver);
914
915         printk(KERN_DEBUG "[egalax_i2c]: driver exit\n");
916 }
917
918 static struct egalax_char_dev* setup_chardev(dev_t dev)
919 {
920         struct egalax_char_dev *pCharDev;
921         int result;
922
923         pCharDev = kmalloc(1*sizeof(struct egalax_char_dev), GFP_KERNEL);
924         if(!pCharDev) 
925                 goto fail_cdev;
926         memset(pCharDev, 0, sizeof(struct egalax_char_dev));
927
928         spin_lock_init( &pCharDev->FiFoLock );
929         pCharDev->pFiFoBuf = kmalloc(sizeof(unsigned char)*FIFO_SIZE, GFP_KERNEL);
930         if(!pCharDev->pFiFoBuf)
931                 goto fail_fifobuf;
932         memset(pCharDev->pFiFoBuf, 0, sizeof(unsigned char)*FIFO_SIZE);
933
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 )
937                 goto fail_kfifo;
938 #else
939         kfifo_init(&pCharDev->DataKFiFo, pCharDev->pFiFoBuf, FIFO_SIZE);
940         if( !kfifo_initialized(&pCharDev->DataKFiFo) )
941                 goto fail_kfifo;
942 #endif
943         
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);
949
950         result = cdev_add(&pCharDev->cdev, dev, 1);
951         if(result)
952         {
953                 TS_DEBUG(KERN_ERR "Error cdev ioctldev added\n");
954                 goto fail_kfifo;
955         }
956
957         return pCharDev; 
958
959 fail_kfifo:
960         kfree(pCharDev->pFiFoBuf);
961 fail_fifobuf:
962         kfree(pCharDev);
963 fail_cdev:
964         return NULL;
965 }
966
967 static void __init egalax_i2c_ts_init_async(void *unused, async_cookie_t cookie)
968 {
969         int result;
970         dev_t devno = 0;
971
972         DBG();
973
974         // Asking for a dynamic major unless directed otherwise at load time.
975         if(global_major) 
976         {
977                 devno = MKDEV(global_major, global_minor);
978                 result = register_chrdev_region(devno, 1, "egalax_i2c");
979         } 
980         else 
981         {
982                 result = alloc_chrdev_region(&devno, global_minor, 1, "egalax_i2c");
983                 global_major = MAJOR(devno);
984         }
985
986         if (result < 0)
987         {
988                 TS_DEBUG(" egalax_i2c cdev can't get major number\n");
989                 return;
990         }
991
992         // allocate the character device
993         p_char_dev = setup_chardev(devno);
994         if(!p_char_dev) 
995         {
996                 result = -ENOMEM;
997                 goto fail;
998         }
999
1000         egalax_class = class_create(THIS_MODULE, "egalax_i2c");
1001         if(IS_ERR(egalax_class))
1002         {
1003                 TS_DEBUG("Err: failed in creating class.\n");
1004                 result = -EFAULT;
1005                 goto fail;
1006         }
1007
1008 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
1009         class_device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
1010 #else
1011         device_create(egalax_class, NULL, devno, NULL, "egalax_i2c");
1012 #endif
1013         TS_DEBUG("register egalax_i2c cdev, major: %d \n",global_major);
1014
1015         printk(KERN_DEBUG "[egalax_i2c]: init done\n");
1016         i2c_add_driver(&egalax_i2c_driver);
1017         return;
1018
1019 fail:   
1020         egalax_i2c_ts_exit();
1021 }
1022
1023 static int __init egalax_i2c_ts_init(void)
1024 {
1025 #ifdef CONFIG_RK_CONFIG
1026         int ret = tp_board_init();
1027
1028         if(ret < 0)
1029                 return ret;
1030 #endif
1031         async_schedule(egalax_i2c_ts_init_async, NULL);
1032         return 0;
1033 }
1034 module_init(egalax_i2c_ts_init);
1035 module_exit(egalax_i2c_ts_exit);
1036
1037 MODULE_DESCRIPTION("egalax touch screen i2c driver");
1038 MODULE_LICENSE("GPL");
1039