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