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