td8801: fixup multitouch of pixcir_tp in kernel-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / pixcir_i2c_ts.c
1 /*
2  * Driver for Pixcir I2C touchscreen controllers.
3  *
4  * Copyright (C) 2010-2011 Pixcir, Inc.
5  *
6  * pixcir_i2c_ts.c V3.0  from v3.0 support TangoC solution and remove the previous soltutions
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/earlysuspend.h>
24 #include <linux/hrtimer.h>
25 #include <linux/i2c.h>
26 #include <linux/input.h>
27 #include <linux/input/mt.h>
28 #include <linux/interrupt.h>
29 #include <linux/io.h>
30 #include <linux/gpio.h>
31 #include <mach/iomux.h>
32 #include <linux/platform_device.h>
33
34 #include <linux/slab.h>
35 #include <asm/uaccess.h>
36 #include <linux/proc_fs.h>
37
38 #include "pixcir_i2c_ts.h"
39
40 #define PIXCIR_DEBUG                    0
41 #if PIXCIR_DEBUG
42         #define pixcir_dbg(msg...)      printk(msg);
43 #else
44         #define pixcir_dbg(msg...)
45 #endif
46
47 static int  ts_dbg_enable = 0;
48
49 #define DBG(msg...) \
50         ({if(ts_dbg_enable == 1) printk(msg);})
51 /*********************************Bee-0928-TOP****************************************/
52
53 #define SLAVE_ADDR              0x5c
54
55 #ifndef I2C_MAJOR
56 #define I2C_MAJOR               125
57 #endif
58
59 #define I2C_MINORS              256
60
61 #define  CALIBRATION_FLAG       1
62
63 static unsigned char status_reg = 0;
64 static struct workqueue_struct *pixcir_wq;
65 static struct pixcir_i2c_ts_data *this_data;
66
67 struct point_data{
68         unsigned char   brn;  //broken line number
69         unsigned char   id;    //finger ID
70         int     posx;
71         int     posy;
72         unsigned char active;
73         unsigned char pre_active;
74 };
75
76 static struct point_data point[MAX_SUPPORT_POINT];
77
78
79 struct i2c_dev
80 {
81         struct list_head list;
82         struct i2c_adapter *adap;
83         struct device *dev;
84 };
85
86 static struct i2c_driver pixcir_i2c_ts_driver;
87 static struct class *i2c_dev_class;
88 static LIST_HEAD( i2c_dev_list);
89 static DEFINE_SPINLOCK( i2c_dev_list_lock);
90
91 static void return_i2c_dev(struct i2c_dev *i2c_dev)
92 {
93         spin_lock(&i2c_dev_list_lock);
94         list_del(&i2c_dev->list);
95         spin_unlock(&i2c_dev_list_lock);
96         kfree(i2c_dev);
97 }
98
99 static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
100 {
101         struct i2c_dev *i2c_dev;
102         i2c_dev = NULL;
103
104         spin_lock(&i2c_dev_list_lock);
105         list_for_each_entry(i2c_dev, &i2c_dev_list, list)
106         {
107                 if (i2c_dev->adap->nr == index)
108                         goto found;
109         }
110         i2c_dev = NULL;
111         found: spin_unlock(&i2c_dev_list_lock);
112         return i2c_dev;
113 }
114
115 static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
116 {
117         struct i2c_dev *i2c_dev;
118
119         if (adap->nr >= I2C_MINORS) {
120                 printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n",
121                                 adap->nr);
122                 return ERR_PTR(-ENODEV);
123         }
124
125         i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
126         if (!i2c_dev)
127                 return ERR_PTR(-ENOMEM);
128
129         i2c_dev->adap = adap;
130
131         spin_lock(&i2c_dev_list_lock);
132         list_add_tail(&i2c_dev->list, &i2c_dev_list);
133         spin_unlock(&i2c_dev_list_lock);
134         return i2c_dev;
135 }
136 /*********************************Bee-0928-bottom**************************************/
137
138 struct pixcir_i2c_ts_data {
139         struct i2c_client *client;
140         struct input_dev *input,*input_key_dev;
141         int use_irq;
142         int     gpio_pendown;
143         int     gpio_reset;
144         int     gpio_reset_active_low;
145         int             pendown_iomux_mode;     
146         int             resetpin_iomux_mode;
147         char    pendown_iomux_name[IOMUX_NAME_SIZE];    
148         char    resetpin_iomux_name[IOMUX_NAME_SIZE];           
149         struct  work_struct  work;
150         //const struct pixcir_ts_platform_data *chip;
151         bool exiting;
152     struct      early_suspend early_suspend;
153 };
154
155 #ifdef CONFIG_HAS_EARLYSUSPEND
156 static void pixcir_ts_early_suspend(struct early_suspend *h);
157 static void pixcir_ts_late_resume(struct early_suspend *h);
158 #endif
159
160 int tp_pixcir_write_reg(struct i2c_client *client,const char *buf ,int count)
161 {
162   struct i2c_msg msg[] = {
163     {
164       .addr  = client->addr,
165       .flags = 0,
166       .len   = count,
167       .buf   = (char *)buf,
168     }
169   };
170   
171         //ret = i2c_transfer(adap, &msg, 1);
172         if (i2c_transfer(client->adapter, msg, 1) < 0)
173         {
174                 printk("write the address (0x%x) of the ssd2533 fail.",buf[0]);
175                 return -1;
176         }
177         return 0;
178 }
179
180 int tp_pixcir_read_reg(struct i2c_client *client,u8 addr,u8 *buf,u8 len)
181 {
182         u8 msgbuf[1] = { addr };
183         struct i2c_msg msgs[] = {
184                 {
185                         .addr    = client->addr,
186                         .flags = 0, //Write
187                         .len     = 1,
188                         .buf     = msgbuf,
189                 },
190                 {
191                         .addr    = client->addr,
192                         .flags = I2C_M_RD,
193                         .len     = len,
194                         .buf     = buf,
195                 },
196         };
197         if (i2c_transfer(client->adapter, msgs, 2) < 0)
198         {
199                 printk("read the address (0x%x) of the ssd2533 fail.",addr);
200                 return -1;
201         }
202         return 0;
203 }
204
205 static void pixcir_ts_poscheck(struct pixcir_i2c_ts_data *data)
206 {
207         struct pixcir_i2c_ts_data *tsdata = data;
208         
209         u8 *p;
210         u8 touch, button, pix_id;
211         u8 rdbuf[27], wrbuf[1] = { 0 };
212         int ret, i;
213         int ignore_cnt = 0;
214         
215         ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
216         if (ret != sizeof(wrbuf)) {
217                 dev_err(&tsdata->client->dev,
218                         "%s: i2c_master_send failed(), ret=%d\n",
219                         __func__, ret);
220                 return;
221         }
222
223         ret = i2c_master_recv(tsdata->client, rdbuf, sizeof(rdbuf));
224         if (ret != sizeof(rdbuf)) {
225                 dev_err(&tsdata->client->dev,
226                         "%s: i2c_master_recv failed(), ret=%d\n",
227                         __func__, ret);
228                 return;
229         }
230         
231         touch = rdbuf[0] & 0x07;
232         button = rdbuf[1];
233         p = &rdbuf[2];
234
235         for (i = 0; i < MAX_SUPPORT_POINT; i++) {
236                 point[i].pre_active = point[i].active;
237                 point[i].active = 0;
238         }
239
240         for (i = 0; i < touch; i++) {
241                 pix_id = (*(p + 4)) & 0x7;
242
243                 point[pix_id].brn = (*(p + 4)) >> 3;            
244                 point[pix_id].id = (*(p + 4)) & 0x7;    
245                 point[pix_id].posx = (*(p + 1) << 8) + (*(p));  
246                 point[pix_id].posy = (*(p + 3) << 8) + (*(p + 2));
247
248                 if (point[pix_id].posy < 40 || point[pix_id].posy > 520 || point[pix_id].posx < 40) {
249                         //point[i].active = 0;
250                 } else {
251                         point[pix_id].active = 1;
252                         
253                         point[pix_id].posy -= 40;
254                         point[pix_id].posx -= 40;
255
256                         if(point[pix_id].posy < 0)
257                                 point[pix_id].posy=1;
258
259                         if(point[pix_id].posx < 0)
260                                 point[pix_id].posx=1;
261                 }
262                 p+=5;
263
264                 DBG("[id = %d]x = %d y = %d\n", pix_id, point[pix_id].posy, point[pix_id].posx);
265         }
266         
267
268         for (i = 0; i < MAX_SUPPORT_POINT; i++) {
269                 if (!point[i].active && point[i].pre_active) {
270                         input_mt_slot(tsdata->input, point[i].id);
271                         input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, false);
272                 }
273
274                 if (point[i].active) {
275                         input_mt_slot(tsdata->input, point[i].id);
276                         input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, true);
277                         input_report_abs(tsdata->input, ABS_MT_TOUCH_MAJOR, 1);
278                         input_report_abs(tsdata->input, ABS_MT_POSITION_X, point[i].posy);
279                         input_report_abs(tsdata->input, ABS_MT_POSITION_Y, point[i].posx);
280                 }
281
282                 DBG("%d[id = %d] = %2d active= %d pre_active = %d x = %5d y = %5d \n",
283                         i, point[i].id, point[i].brn, point[i].active, point[i].pre_active, point[i].posy, point[i].posx);
284         }
285
286         input_sync(tsdata->input);
287
288 }
289
290 static void pixcir_ts_work_func(struct work_struct *work)
291 {
292         struct pixcir_i2c_ts_data *tsdata = this_data;
293         int i = 0;
294
295         while (!tsdata->exiting) {
296         
297                 pixcir_ts_poscheck(tsdata);
298                 if (attb_read_val()){
299                         DBG("%s:  >>>>>touch release\n\n",__FUNCTION__);
300                         for (i = 0; i < MAX_SUPPORT_POINT; i++) {
301                                 point[i].pre_active = point[i].active;
302                                 point[i].active = 0;
303                                 if (!point[i].active && point[i].pre_active) {
304                                         input_mt_slot(tsdata->input, point[i].id);
305                                         input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, false);
306                                 }
307                         }
308                         input_sync(tsdata->input);
309                         enable_irq(tsdata->client->irq);
310                         break;
311                 }
312
313                 msleep(1);
314         }
315 }
316
317 static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
318 {
319     struct pixcir_i2c_ts_data *ts = dev_id;
320     DBG("%s: >>>>>>>>>\n\n",__FUNCTION__);
321
322         if(ts->use_irq){
323         disable_irq_nosync(ts->client->irq);
324         }
325         queue_work(pixcir_wq, &ts->work);
326
327     return IRQ_HANDLED;
328 }
329
330 #if 0
331 #ifdef CONFIG_PM_SLEEP
332 static int pixcir_i2c_ts_suspend(struct device *dev)
333 {
334         struct i2c_client *client = to_i2c_client(dev);
335         pixcir_dbg("%s\n",__FUNCTION__);
336
337         if (device_may_wakeup(&client->dev))
338                 enable_irq_wake(client->irq);
339
340         return 0;
341 }
342
343 static int pixcir_i2c_ts_resume(struct device *dev)
344 {
345         struct i2c_client *client = to_i2c_client(dev);
346         pixcir_dbg("%s\n",__FUNCTION__);
347
348         if (device_may_wakeup(&client->dev))
349                 disable_irq_wake(client->irq);
350
351         return 0;
352 }
353 #endif
354 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
355                          pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
356
357 static int __devinit setup_resetPin(struct i2c_client *client, struct pixcir_i2c_ts_data *ts)
358 {
359         struct pixcir_platform_data     *pdata = client->dev.platform_data;
360         int err;
361         
362         pixcir_dbg("%s\n",__FUNCTION__);
363
364         
365         ts->gpio_reset = pdata->gpio_reset;
366     ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
367     ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
368         ///*
369
370     if(pdata->resetpin_iomux_name != NULL)
371             strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
372         
373         //pixcir_dbg("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
374         if (!gpio_is_valid(ts->gpio_reset)) {
375                 dev_err(&client->dev, "no gpio_reset?\n");
376                 return -EINVAL;
377         }
378
379     rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode); 
380         //*/
381
382         err = gpio_request(ts->gpio_reset, "pixcir_resetPin");
383         if (err) {
384                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
385                                 ts->gpio_reset);
386                 return err;
387         }
388         
389         err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
390         if (err) {
391                 dev_err(&client->dev, "failed to pulldown resetPin GPIO%d,err%d\n",
392                                 ts->gpio_reset,err);
393                 gpio_free(ts->gpio_reset);
394                 return err;
395         }
396         mdelay(100);
397         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
398         mdelay(100);
399
400         return 0;
401 }
402
403 static int __devinit setup_pendown(struct i2c_client *client, struct pixcir_i2c_ts_data *ts)
404 {
405         int err;
406         struct pixcir_i2c_ts_data       *pdata = client->dev.platform_data;
407
408         pixcir_dbg("%s\n",__FUNCTION__);
409         if (!client->irq) {
410                 dev_dbg(&client->dev, "no IRQ?\n");
411                 return -ENODEV;
412         }
413         
414         if (!gpio_is_valid(pdata->gpio_pendown)) {
415                 dev_err(&client->dev, "no gpio_pendown?\n");
416                 return -EINVAL;
417         }
418         
419         ts->gpio_pendown = pdata->gpio_pendown;
420         strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
421         ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
422         
423         pixcir_dbg("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
424         
425         if (!gpio_is_valid(ts->gpio_pendown)) {
426                 dev_err(&client->dev, "no gpio_pendown?\n");
427                 return -EINVAL;
428         }
429         
430     rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode); 
431         err = gpio_request(ts->gpio_pendown, "gt801_pendown");
432         if (err) {
433                 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
434                                 ts->gpio_pendown);
435                 return err;
436         }
437         
438         err = gpio_pull_updown(ts->gpio_pendown, GPIOPullUp);
439         if (err) {
440                 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
441                                 ts->gpio_pendown);
442                 gpio_free(ts->gpio_pendown);
443                 return err;
444         }
445         return 0;
446 }
447 #endif
448
449 static ssize_t pixcir_proc_write(struct file *file, const char __user *buffer,
450                            size_t count, loff_t *data)
451 {
452         char c;
453         int rc;
454         
455         rc = get_user(c, buffer);
456         if (rc)
457                 return rc; 
458         
459         if (c == '1')
460                 ts_dbg_enable = 1; 
461         else if (c == '0')
462                 ts_dbg_enable = 0; 
463
464         return count; 
465 }
466
467 static const struct file_operations pixcir_proc_fops = {
468         .owner          = THIS_MODULE, 
469         .write          = pixcir_proc_write,
470 }; 
471 static int __devinit pixcir_i2c_ts_probe(struct i2c_client *client,
472                                          const struct i2c_device_id *id)
473 {
474         //const struct pixcir_ts_platform_data *pdata = client->dev.platform_data;
475         struct pixcir_i2c_ts_data *tsdata;
476         struct pixcir_platform_data *pdata;
477         struct device *dev;
478         struct i2c_dev *i2c_dev;
479         int error = 0;
480         struct proc_dir_entry *pixcir_proc_entry;       
481
482         //if (!pdata) {
483         //      dev_err(&client->dev, "platform data not defined\n");
484         //      return -EINVAL;
485         //}
486         pixcir_dbg("%s\n",__FUNCTION__);
487
488         tsdata = kzalloc(sizeof(*tsdata), GFP_KERNEL);
489         tsdata->input = input_allocate_device();
490         if (!tsdata || !(tsdata->input)) {
491                 dev_err(&client->dev, "Failed to allocate driver data!\n");
492                 error = -ENOMEM;
493                 goto err_free_mem;
494         }
495         
496         pixcir_wq = create_singlethread_workqueue("pixcir_tp_wq");
497         if (!pixcir_wq) {
498                 printk(KERN_ERR"%s: create workqueue failed\n", __FUNCTION__);
499                 error = -ENOMEM;
500                 goto err_free_mem;
501         }
502         INIT_WORK(&tsdata->work, pixcir_ts_work_func);
503         
504         this_data = tsdata;
505         tsdata->exiting = false;
506         //tsdata->input = input;
507         //tsdata->chip = pdata;
508         
509         tsdata->client = client;
510         i2c_set_clientdata(client, tsdata);
511         pdata = client->dev.platform_data;
512
513         //error = setup_resetPin(client,tsdata);
514         if(error)
515         {
516                  printk("%s:setup_resetPin fail\n",__FUNCTION__);
517                  goto err_free_mem;
518         }
519         tsdata->input->phys = "/dev/input/event2";
520         tsdata->input->name = "pixcir_ts-touchscreen";//client->name;
521         tsdata->input->id.bustype = BUS_I2C;
522         tsdata->input->dev.parent = &client->dev;
523         
524         
525         ///*
526         /*set_bit(EV_SYN,    tsdata->input->evbit);
527         set_bit(EV_KEY,    tsdata->input->evbit);
528         set_bit(EV_ABS,    tsdata->input->evbit);
529         set_bit(BTN_TOUCH, tsdata->input->keybit);
530         set_bit(BTN_2,     tsdata->input->keybit);//*/
531         
532         //tsdata->input->evbit[0] = BIT_MASK(EV_SYN) |  BIT_MASK(EV_ABS) ;
533         //tsdata->input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
534         /*tsdata->input->absbit[0] = BIT_MASK(ABS_MT_POSITION_X) | BIT_MASK(ABS_MT_POSITION_Y) |
535                         BIT_MASK(ABS_MT_TOUCH_MAJOR) | BIT_MASK(ABS_MT_WIDTH_MAJOR);  // for android*/
536         //tsdata->input->keybit[BIT_WORD(BTN_START)] = BIT_MASK(BTN_START);
537
538         //input_set_abs_params(input, ABS_X, 0, X_MAX, 0, 0);
539         //input_set_abs_params(input, ABS_Y, 0, Y_MAX, 0, 0);
540
541         __set_bit(INPUT_PROP_DIRECT, tsdata->input->propbit);
542         __set_bit(EV_ABS, tsdata->input->evbit);
543
544         input_mt_init_slots(tsdata->input, MAX_SUPPORT_POINT);
545         input_set_abs_params(tsdata->input, ABS_MT_POSITION_X,  pdata->x_min,  pdata->x_max, 0, 0);
546         input_set_abs_params(tsdata->input, ABS_MT_POSITION_Y,  pdata->y_min, pdata->y_max, 0, 0);
547         //input_set_abs_params(tsdata->input, ABS_MT_WIDTH_MAJOR, 0,   16, 0, 0);
548         //input_set_abs_params(tsdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
549         input_set_abs_params(tsdata->input, ABS_MT_TOUCH_MAJOR, 0,    1, 0, 0);
550         //input_set_abs_params(tsdata->input, ABS_MT_TRACKING_ID, 0,    5, 0, 0);
551         input_set_drvdata(tsdata->input, tsdata);
552         //init int and reset ports
553         error = gpio_request(client->irq, "TS_INT");    //Request IO
554         if (error){
555                 dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(int)client->irq, error);
556                 goto err_free_mem;
557         }
558         rk29_mux_api_set(pdata->pendown_iomux_name, pdata->pendown_iomux_mode);
559         
560         gpio_direction_input(client->irq);
561         gpio_set_value(client->irq,GPIO_HIGH);
562         gpio_pull_updown(client->irq, 0);
563
564         error = gpio_request(pdata->gpio_reset, "pixcir_resetPin");
565         if(error){
566                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n", pdata->gpio_reset);
567                 goto err_free_mem;
568         }
569         rk29_mux_api_set(pdata->resetpin_iomux_name, pdata->resetpin_iomux_mode);
570         /*{
571                 gpio_pull_updown(pdata->gpio_reset, 1);
572                 gpio_direction_output(pdata->gpio_reset, 0);
573                 msleep(20);     //delay at least 1ms
574                 gpio_direction_input(pdata->gpio_reset);
575                 gpio_pull_updown(pdata->gpio_reset, 0);
576                 msleep(120);
577         }*/
578     gpio_pull_updown(pdata->gpio_reset, 1);
579     mdelay(20);
580     gpio_direction_output(pdata->gpio_reset, 0);
581     gpio_set_value(pdata->gpio_reset,GPIO_HIGH);//GPIO_LOW
582     mdelay(100);
583     gpio_set_value(pdata->gpio_reset,GPIO_LOW);//GPIO_HIGH
584     mdelay(120);
585     // gpio_direction_input(pdata->gpio_reset);
586         printk("pdata->gpio_reset = %d\n",gpio_get_value(pdata->gpio_reset));
587     //printk("ts->gpio_irq = %d\n",gpio_get_value(pdata->gpio_pendown));
588         printk("pdata->gpio_pendown = %d\n",gpio_get_value(client->irq));
589
590 #if 0
591         //**********************************************
592    char buffer[2];
593         buffer[0] = 0x3A;
594         buffer[1] = 0x03;
595         tp_pixcir_write_reg(client,buffer,2);
596         ssleep(6);
597         //********************************************************//
598 #endif
599         client->irq = gpio_to_irq(client->irq); 
600         error = request_irq(client->irq, pixcir_ts_isr, IRQF_TRIGGER_FALLING, client->name, (void *)tsdata);
601         if (error)
602                 dev_err(&client->dev, "request_irq failed\n");
603 /*
604         error = request_threaded_irq(client->irq, NULL, pixcir_ts_isr,
605                                      IRQF_TRIGGER_FALLING,
606                                      client->name, tsdata);*/
607         tsdata->use_irq = 1;
608         if (error) {
609                 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
610                 tsdata->use_irq = 0;
611                 goto err_free_mem;
612         }
613
614         error = input_register_device(tsdata->input);
615         if (error)
616                 goto err_free_irq;
617
618         i2c_set_clientdata(client, tsdata);
619         device_init_wakeup(&client->dev, 1);
620
621         /*********************************Bee-0928-TOP****************************************/
622         i2c_dev = get_free_i2c_dev(client->adapter);
623         if (IS_ERR(i2c_dev)) {
624                 error = PTR_ERR(i2c_dev);
625                 return error;
626         }
627
628         dev = device_create(i2c_dev_class, &client->adapter->dev, MKDEV(I2C_MAJOR,
629                         client->adapter->nr), NULL, "pixcir_i2c_ts%d", 0);
630         if (IS_ERR(dev)) {
631                 error = PTR_ERR(dev);
632                 return error;
633         }
634         /*********************************Bee-0928-BOTTOM****************************************/
635 #ifdef CONFIG_HAS_EARLYSUSPEND
636     tsdata->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
637     tsdata->early_suspend.suspend = pixcir_ts_early_suspend;
638     tsdata->early_suspend.resume = pixcir_ts_late_resume;
639     register_early_suspend(&tsdata->early_suspend);
640 #endif
641         pixcir_proc_entry = proc_create("driver/pixcir", 0777, NULL, &pixcir_proc_fops); 
642
643         dev_err(&tsdata->client->dev, "insmod successfully!\n");
644
645         return 0;
646
647 err_free_irq:
648         free_irq(client->irq, tsdata);
649 err_free_mem:
650         input_free_device(tsdata->input);
651         kfree(tsdata);
652         return error;
653 }
654
655 static int __devexit pixcir_i2c_ts_remove(struct i2c_client *client)
656 {
657         int error;
658         struct i2c_dev *i2c_dev;
659         struct pixcir_i2c_ts_data *tsdata = i2c_get_clientdata(client);
660
661     unregister_early_suspend(&tsdata->early_suspend);
662         device_init_wakeup(&client->dev, 0);
663
664         tsdata->exiting = true;
665         mb();
666         free_irq(client->irq, tsdata);
667
668         /*********************************Bee-0928-TOP****************************************/
669         i2c_dev = get_free_i2c_dev(client->adapter);
670         if (IS_ERR(i2c_dev)) {
671                 error = PTR_ERR(i2c_dev);
672                 return error;
673         }
674
675         return_i2c_dev(i2c_dev);
676         device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, client->adapter->nr));
677         /*********************************Bee-0928-BOTTOM****************************************/
678
679         input_unregister_device(tsdata->input);
680         kfree(tsdata);
681
682         return 0;
683 }
684
685 /*************************************Bee-0928****************************************/
686 /*                                   pixcir_open                                     */
687 /*************************************Bee-0928****************************************/
688 static int pixcir_open(struct inode *inode, struct file *file)
689 {
690         int subminor;
691         struct i2c_client *client;
692         struct i2c_adapter *adapter;
693         struct i2c_dev *i2c_dev;
694         int ret = 0;
695 #if PIXCIR_DEBUG
696         printk("enter pixcir_open function\n");
697 #endif
698         subminor = iminor(inode);
699
700         //lock_kernel();
701         i2c_dev = i2c_dev_get_by_minor(subminor);
702         if (!i2c_dev) {
703                 printk("error i2c_dev\n");
704                 return -ENODEV;
705         }
706
707         adapter = i2c_get_adapter(i2c_dev->adap->nr);
708         if (!adapter) {
709                 return -ENODEV;
710         }
711         
712         client = kzalloc(sizeof(*client), GFP_KERNEL);
713         if (!client) {
714                 i2c_put_adapter(adapter);
715                 ret = -ENOMEM;
716         }
717
718         snprintf(client->name, I2C_NAME_SIZE, "pixcir_i2c_ts%d", adapter->nr);
719         client->driver = &pixcir_i2c_ts_driver;
720         client->adapter = adapter;
721         
722         file->private_data = client;
723
724         return 0;
725 }
726
727 /*************************************Bee-0928****************************************/
728 /*                                   pixcir_ioctl                                    */
729 /*************************************Bee-0928****************************************/
730 static long pixcir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
731 {
732         //printk("ioctl function\n");
733         struct i2c_client *client = (struct i2c_client *) file->private_data;
734 #if PIXCIR_DEBUG
735         printk("cmd = %d,arg = %d\n", cmd, arg);
736 #endif
737
738         switch (cmd)
739         {
740         case CALIBRATION_FLAG: //CALIBRATION_FLAG = 1
741 #if PIXCIR_DEBUG
742                 printk("CALIBRATION\n");
743 #endif
744                 client->addr = SLAVE_ADDR;
745                 status_reg = 0;
746                 status_reg = CALIBRATION_FLAG;
747                 break;
748
749         default:
750                 break;//return -ENOTTY;
751         }
752         return 0;
753 }
754
755
756 /***********************************Bee-0928****************************************/
757 /*                                pixcir_write                                     */
758 /***********************************Bee-0928****************************************/
759 static ssize_t pixcir_write(struct file *file,const char __user *buf,size_t count, loff_t *ppos)
760 {
761         struct i2c_client *client;
762         char *tmp;
763         static int ret=0;
764         
765         pixcir_dbg("%s\n",__FUNCTION__);
766
767         client = file->private_data;
768
769         //printk("pixcir_write function\n");
770         switch(status_reg)
771         {
772                 case CALIBRATION_FLAG: //CALIBRATION_FLAG=1
773                 tmp = kmalloc(count,GFP_KERNEL);
774                 if (tmp==NULL)
775                         return -ENOMEM;
776
777                 if (copy_from_user(tmp,buf,count)) {    
778                         printk("CALIBRATION_FLAG copy_from_user error\n");
779                         kfree(tmp);
780                         return -EFAULT;
781                 }
782
783                 ret = i2c_master_send(client,tmp,count);
784                 if (ret!=count ) {
785                         dev_err(&client->dev,
786                                 "%s: i2c_master_recv failed(), ret=%d\n",
787                                 __func__, ret);
788                 }
789
790                 while(!attb_read_val());//waiting to finish the calibration.(pixcir application_note_710_v3 p43)
791
792                 kfree(tmp);
793
794                 status_reg = 0;
795                 break;
796
797                 default:
798                 break;
799         }
800         return ret;
801 }
802
803 /***********************************Bee-0928****************************************/
804 /*                                pixcir_release                                   */
805 /***********************************Bee-0928****************************************/
806 static int pixcir_release(struct inode *inode, struct file *file)
807 {
808         struct i2c_client *client = file->private_data;
809    #if PIXCIR_DEBUG
810         printk("enter pixcir_release funtion\n");
811    #endif
812         i2c_put_adapter(client->adapter);
813         kfree(client);
814         file->private_data = NULL;
815
816         return 0;
817 }
818
819 /*********************************Bee-0928-TOP****************************************/
820 static const struct file_operations pixcir_i2c_ts_fops =
821 {       .owner          = THIS_MODULE,
822         .write          = pixcir_write,
823         .open           = pixcir_open,
824         .unlocked_ioctl = pixcir_ioctl,
825         .release        = pixcir_release,
826 };
827 /*********************************Bee-0928-BOTTOM****************************************/
828
829 static int pixcir_ts_suspend(struct i2c_client *client, pm_message_t mesg)
830 {
831     struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
832         DBG("%s\n",__FUNCTION__);
833
834         if (ts->use_irq)
835                 disable_irq(client->irq);
836         //gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
837         return 0;
838 }
839
840 static int pixcir_ts_resume(struct i2c_client *client)
841 {
842     struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
843         DBG("%s\n",__FUNCTION__);
844
845         if (ts->use_irq)
846                 enable_irq(client->irq);
847     //gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
848         return 0;
849 }
850
851 #ifdef CONFIG_HAS_EARLYSUSPEND
852 static void pixcir_ts_early_suspend(struct early_suspend *h)
853 {
854     struct pixcir_i2c_ts_data *ts;
855         DBG("%s\n",__FUNCTION__);
856     ts = container_of(h, struct pixcir_i2c_ts_data, early_suspend);
857     pixcir_ts_suspend(ts->client, PMSG_SUSPEND);
858 }
859
860 static void pixcir_ts_late_resume(struct early_suspend *h)
861 {
862     struct pixcir_i2c_ts_data *ts;
863         DBG("%s\n",__FUNCTION__);
864     ts = container_of(h, struct pixcir_i2c_ts_data, early_suspend);
865     pixcir_ts_resume(ts->client);
866 }
867 #endif
868
869 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
870         { "pixcir_ts", 0 },
871         { }
872 };
873 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
874
875 static struct i2c_driver pixcir_i2c_ts_driver = {
876         .driver = {
877                 .owner  = THIS_MODULE,
878                 .name   = "pixcir_ts",
879                 //.pm   = &pixcir_dev_pm_ops,
880         },
881 #ifndef CONFIG_HAS_EARLYSUSPEND
882     .suspend    = pixcir_ts_suspend,
883     .resume     = pixcir_ts_resume,
884 #endif
885         .probe          = pixcir_i2c_ts_probe,
886         .remove         = __devexit_p(pixcir_i2c_ts_remove),
887         .id_table       = pixcir_i2c_ts_id,
888 };
889
890 static int __init pixcir_i2c_ts_init(void)
891 {
892         int ret;
893
894         pixcir_dbg("%s\n",__FUNCTION__);
895         
896     pixcir_wq = create_singlethread_workqueue("pixcir_wq");
897     if (!pixcir_wq)
898         return -ENOMEM;
899
900         /*********************************Bee-0928-TOP****************************************/
901         ret = register_chrdev(I2C_MAJOR,"pixcir_i2c_ts",&pixcir_i2c_ts_fops);
902         if (ret) {
903                 printk(KERN_ERR "%s:register chrdev failed\n",__FILE__);
904                 return ret;
905         }
906
907         i2c_dev_class = class_create(THIS_MODULE, "pixcir_i2c_dev");
908         if (IS_ERR(i2c_dev_class)) {
909                 ret = PTR_ERR(i2c_dev_class);
910                 class_destroy(i2c_dev_class);
911         }
912         /********************************Bee-0928-BOTTOM******************************************/
913
914         //tangoC_init();
915
916         return i2c_add_driver(&pixcir_i2c_ts_driver);
917 }
918 module_init(pixcir_i2c_ts_init);
919
920 static void __exit pixcir_i2c_ts_exit(void)
921 {
922         i2c_del_driver(&pixcir_i2c_ts_driver);
923     if (pixcir_wq)
924         destroy_workqueue(pixcir_wq);
925         /********************************Bee-0928-TOP******************************************/
926         class_destroy(i2c_dev_class);
927         unregister_chrdev(I2C_MAJOR,"pixcir_i2c_ts");
928         /********************************Bee-0928-BOTTOM******************************************/
929 }
930 module_exit(pixcir_i2c_ts_exit);
931
932 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>");
933 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
934 MODULE_LICENSE("GPL");