Revert "pixcir driver: fix a bug touchscreen cannot hold touching"
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / hannstar_p1003.c
1 /****************************************************************************************
2  * driver/input/touchscreen/hannstar_p1003.c
3  *Copyright     :ROCKCHIP  Inc
4  *Author        :       sfm
5  *Date          :  2010.2.5
6  *This driver use for rk28 chip extern touchscreen. Use i2c IF ,the chip is Hannstar
7  *description£º
8  ********************************************************************************************/
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/earlysuspend.h>
12 #include <linux/hrtimer.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/platform_device.h>
18 #include <linux/async.h>
19 #include <mach/gpio.h>
20 #include <linux/irq.h>
21 #include <mach/board.h>
22
23 #define MAX_SUPPORT_POINT       2// //  4
24 #define PACKGE_BUFLEN           10
25
26 //#define Singltouch_Mode
27 #define SAKURA_DBG                  0
28 #if SAKURA_DBG 
29 #define sakura_dbg_msg(fmt,...)       do {                                      \
30                                    printk("sakura dbg msg------>"                       \
31                                           " (func-->%s ; line-->%d) " fmt, __func__, __LINE__ , ##__VA_ARGS__); \
32                                   } while(0)
33 #define sakura_dbg_report_key_msg(fmt,...)      do{                                                     \
34                                                     printk("sakura report " fmt,##__VA_ARGS__);          \
35                                                 }while(0)
36 #else
37 #define sakura_dbg_msg(fmt,...)       do {} while(0)
38 #define sakura_dbg_report_key_msg(fmt,...)      do{}while(0)
39 #endif
40 struct point_data {     
41         short status;   
42         short x;        
43         short y;
44     short z;
45 };
46
47 struct multitouch_event{
48         struct point_data point_data[MAX_SUPPORT_POINT];
49         int contactid;
50     int validtouch;
51 };
52
53 struct ts_p1003 {
54         struct input_dev        *input;
55         char                    phys[32];
56         struct delayed_work     work;
57         struct workqueue_struct *wq;
58
59         struct i2c_client       *client;
60     struct multitouch_event mt_event;
61         u16                     model;
62
63         bool            pendown;
64         bool            status;
65         int                     irq;
66         int             has_relative_report;
67         int                     (*get_pendown_state)(void);
68         void            (*clear_penirq)(void);
69 };
70
71 int p1003_get_pendown_state(void)
72 {
73         return 0;
74 }
75
76 static void p1003_report_event(struct ts_p1003 *ts,struct multitouch_event *tc)
77 {
78         struct input_dev *input = ts->input;
79     int i,pandown = 0;
80         dev_dbg(&ts->client->dev, "UP\n");
81                 
82     for(i=0; i<MAX_SUPPORT_POINT;i++){                  
83         if(tc->point_data[i].status >= 0){
84             pandown |= tc->point_data[i].status;
85             input_report_abs(input, ABS_MT_TRACKING_ID, i);                                                     
86             input_report_abs(input, ABS_MT_TOUCH_MAJOR, tc->point_data[i].status);                              
87             input_report_abs(input, ABS_MT_WIDTH_MAJOR, 0);     
88             input_report_abs(input, ABS_MT_POSITION_X, tc->point_data[i].x);                            
89             input_report_abs(input, ABS_MT_POSITION_Y, tc->point_data[i].y);                            
90             input_mt_sync(input);       
91
92             sakura_dbg_report_key_msg("ABS_MT_TRACKING_ID = %x, ABS_MT_TOUCH_MAJOR = %x\n ABS_MT_POSITION_X = %x, ABS_MT_POSITION_Y = %x\n",i,tc->point_data[i].status,tc->point_data[i].x,tc->point_data[i].y);
93 #if defined(CONFIG_HANNSTAR_DEBUG)
94                         printk("hannstar p1003 Px = [%d],Py = [%d] \n",tc->point_data[i].x,tc->point_data[i].y);
95 #endif
96
97             if(tc->point_data[i].status == 0)                                   
98                 tc->point_data[i].status--;                     
99         }
100         
101     }   
102
103     ts->pendown = pandown;
104     input_sync(input);
105 }
106
107 #if defined (Singltouch_Mode)
108 static void p1003_report_single_event(struct ts_p1003 *ts,struct multitouch_event *tc)
109 {
110         struct input_dev *input = ts->input;
111     int cid;
112
113     cid = tc->contactid;
114     if (ts->status) {
115         input_report_abs(input, ABS_X, tc->point_data[cid].x);
116         input_report_abs(input, ABS_Y, tc->point_data[cid].y);
117         input_sync(input);
118     }
119     if(ts->pendown != ts->status){
120         ts->pendown = ts->status;
121         input_report_key(input, BTN_TOUCH, ts->status);
122         input_sync(input);
123         sakura_dbg_report_key_msg("%s x =0x%x,y = 0x%x \n",ts->status?"down":"up",tc->point_data[cid].x,tc->point_data[cid].y);
124     }
125 }
126 #endif
127
128 static inline int p1003_check_firmwork(struct ts_p1003 *ts)
129 {
130     int data;
131     int len = 10;
132     char buf[10] = {0x03 , 0x03 , 0x0a , 0x01 , 'D' , 0x00 , 0x00 , 0x00 , 0x00 , 0x00};
133         int i;
134     short contactid=0;
135
136     data = i2c_master_normal_send(ts->client, buf,len, 200*1000);
137
138         if(data < 0){
139                 dev_err(&ts->client->dev, "i2c io error %d \n", data);
140                 return data;
141         }
142
143         data = i2c_master_normal_recv(ts->client, buf,len, 200*1000);
144
145         if(data < 0){
146                 dev_err(&ts->client->dev, "i2c io error %d \n", data);
147                 return data;
148         }
149
150         printk("p1003 reg[5] = %c ,reg[6] = %c, reg[7] = %c, reg[8] = %c\n" , buf[5],buf[6],buf[7],buf[8]);
151         printk("p1003 reg[5] = %x ,reg[6] = %x, reg[7] = %x, reg[8] = %x\n" , buf[5],buf[6],buf[7],buf[8]);
152     return data;
153 }
154
155
156 static inline int p1003_read_values(struct ts_p1003 *ts, struct multitouch_event *tc)
157 {
158     int data, j;
159     int len = 10;
160     char *buf;
161         char tempbuf[(MAX_SUPPORT_POINT * 10) << 1];
162     short contactid=0;
163
164         for(j = 0; j < MAX_SUPPORT_POINT ; j++){
165                 buf = &tempbuf[j*10];
166         data = i2c_master_normal_recv(ts->client, buf, len, 200*1000);
167                 if(data < 0)
168                         return data;
169                 if(tempbuf[j*10] != 0x04){
170                         if(j == 0){
171                                 data = -1;
172                                 ts->pendown = 0;
173                         return data;
174                         }else{
175                                 
176                                 break;
177                         }
178                 }
179                 
180                 contactid = (buf[1]&0x7C)>>2;
181                 tc->contactid = contactid;
182                 tc->point_data[contactid].status = buf[1]&0x01; 
183                 tc->point_data[contactid].x = ((buf[3]<<8) + buf[2])>>4;
184                 tc->point_data[contactid].y = ((buf[5]<<8) + buf[4])>>4;
185                 tc->validtouch = buf[1]&0x80;
186                 ts->status = tc->point_data[contactid].status;
187         }
188         
189 //    printk("validtouch =%d,status= %d,contactid =%d\n",tc->validtouch,tc->point_data[contactid].status,contactid);
190     return 10;
191 }
192
193
194 static void p1003_work(struct work_struct *work)
195 {
196         struct ts_p1003 *ts =
197                 container_of(to_delayed_work(work), struct ts_p1003, work);
198         struct multitouch_event *tc = &ts->mt_event;
199         int rt;
200     
201         rt = p1003_read_values(ts,tc);
202     
203     if(rt < 0)
204         goto out;
205         
206 #if defined (Singltouch_Mode)
207     p1003_report_single_event(ts,tc);
208 #else
209     p1003_report_event(ts,tc);
210 #endif
211
212 out:               
213         if (ts->pendown){
214                 queue_delayed_work(ts->wq, &ts->work, msecs_to_jiffies(10));
215                 ts->pendown = 0;
216         }
217         else{
218                 enable_irq(ts->irq);
219         }
220 }
221
222 static irqreturn_t p1003_irq(int irq, void *handle)
223 {
224         struct ts_p1003 *ts = handle;
225 #if 1
226         if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
227                 disable_irq_nosync(ts->irq);
228                 queue_delayed_work(ts->wq, &ts->work, 0);
229         }
230
231 #endif
232         return IRQ_HANDLED;
233 }
234
235 static void p1003_free_irq(struct ts_p1003 *ts)
236 {
237         free_irq(ts->irq, ts);
238         if (cancel_delayed_work_sync(&ts->work)) {
239                 /*
240                  * Work was pending, therefore we need to enable
241                  * IRQ here to balance the disable_irq() done in the
242                  * interrupt handler.
243                  */
244                 enable_irq(ts->irq);
245         }
246 }
247
248 static int __devinit p1003_probe(struct i2c_client *client,
249                                    const struct i2c_device_id *id)
250 {
251         struct ts_p1003 *ts;
252         struct p1003_platform_data *pdata = pdata = client->dev.platform_data;
253         struct input_dev *input_dev;
254         int err;
255         
256         if (!pdata) {
257                 dev_err(&client->dev, "platform data is required!\n");
258                 return -EINVAL;
259         }
260
261         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
262                 return -EIO;
263
264         ts = kzalloc(sizeof(struct ts_p1003), GFP_KERNEL);
265         input_dev = input_allocate_device();
266         if (!ts || !input_dev) {
267                 err = -ENOMEM;
268                 goto err_free_mem;
269         }
270
271         ts->client = client;
272         ts->irq = client->irq;
273         ts->input = input_dev;
274         ts->status =0 ;// fjp add by 2010-9-30
275         ts->pendown = 0; // fjp add by 2010-10-06
276
277         ts->wq = create_rt_workqueue("p1003_wq");
278         INIT_DELAYED_WORK(&ts->work, p1003_work);
279
280         ts->model             = pdata->model;
281
282         snprintf(ts->phys, sizeof(ts->phys),
283                  "%s/input0", dev_name(&client->dev));
284
285         input_dev->name = "p1003 Touchscreen";
286         input_dev->phys = ts->phys;
287         input_dev->id.bustype = BUS_I2C;
288
289 #if defined (Singltouch_Mode)
290         set_bit(EV_SYN, input_dev->evbit);
291         set_bit(EV_KEY, input_dev->evbit);
292         set_bit(BTN_TOUCH, input_dev->keybit);
293         set_bit(BTN_2, input_dev->keybit);
294         set_bit(EV_ABS, input_dev->evbit);
295         input_set_abs_params(input_dev,ABS_X,0,CONFIG_HANNSTAR_MAX_X,0,0);
296         input_set_abs_params(input_dev,ABS_Y,0,CONFIG_HANNSTAR_MAX_Y,0,0);
297 #else
298         ts->has_relative_report = 0;
299         input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
300         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
301         input_dev->keybit[BIT_WORD(BTN_2)] = BIT_MASK(BTN_2); //jaocbchen for dual
302         input_set_abs_params(input_dev, ABS_X, 0, CONFIG_HANNSTAR_MAX_X, 0, 0);
303         input_set_abs_params(input_dev, ABS_Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
304         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
305         input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
306         input_set_abs_params(input_dev, ABS_HAT0X, 0, CONFIG_HANNSTAR_MAX_X, 0, 0);
307         input_set_abs_params(input_dev, ABS_HAT0Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
308         input_set_abs_params(input_dev, ABS_MT_POSITION_X,0, CONFIG_HANNSTAR_MAX_X, 0, 0);
309         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
310         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
311         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
312         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);   
313 #endif
314
315         if (pdata->init_platform_hw)
316                 pdata->init_platform_hw();
317
318         if (!ts->irq) {
319                 dev_dbg(&ts->client->dev, "no IRQ?\n");
320                 return -ENODEV;
321         }else{
322                 ts->irq = gpio_to_irq(ts->irq);
323         }
324
325         err = request_irq(ts->irq, p1003_irq, 0,
326                         client->dev.driver->name, ts);
327         
328         if (err < 0) {
329                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
330                 goto err_free_mem;
331         }
332         
333         if (err < 0)
334                 goto err_free_irq;
335 #if 0
336         err = set_irq_type(ts->irq,IRQ_TYPE_LEVEL_LOW);
337         if (err < 0) {
338                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
339                 goto err_free_mem;
340         }
341         if (err < 0)
342                 goto err_free_irq;
343 #endif
344         err = input_register_device(input_dev);
345         if (err)
346                 goto err_free_irq;
347
348         i2c_set_clientdata(client, ts);
349
350         p1003_check_firmwork(ts);
351         
352         return 0;
353
354  err_free_irq:
355         p1003_free_irq(ts);
356         if (pdata->exit_platform_hw)
357                 pdata->exit_platform_hw();
358  err_free_mem:
359         input_free_device(input_dev);
360         kfree(ts);
361         return err;
362 }
363
364 static int __devexit p1003_remove(struct i2c_client *client)
365 {
366         struct ts_p1003 *ts = i2c_get_clientdata(client);
367         struct p1003_platform_data *pdata = client->dev.platform_data;
368
369         p1003_free_irq(ts);
370
371         if (pdata->exit_platform_hw)
372                 pdata->exit_platform_hw();
373
374         input_unregister_device(ts->input);
375         kfree(ts);
376
377         return 0;
378 }
379
380 static struct i2c_device_id p1003_idtable[] = {
381         { "p1003_touch", 0 },
382         { }
383 };
384
385 MODULE_DEVICE_TABLE(i2c, p1003_idtable);
386
387 static struct i2c_driver p1003_driver = {
388         .driver = {
389                 .owner  = THIS_MODULE,
390                 .name   = "p1003_touch"
391         },
392         .id_table       = p1003_idtable,
393         .probe          = p1003_probe,
394         .remove         = __devexit_p(p1003_remove),
395 };
396
397 static void __init p1003_init_async(void *unused, async_cookie_t cookie)
398 {
399         printk("--------> %s <-------------\n",__func__);
400         i2c_add_driver(&p1003_driver);
401 }
402
403 static int __init p1003_init(void)
404 {
405         async_schedule(p1003_init_async, NULL);
406         return 0;
407 }
408
409 static void __exit p1003_exit(void)
410 {
411         return i2c_del_driver(&p1003_driver);
412 }
413 module_init(p1003_init);
414 module_exit(p1003_exit);
415 MODULE_LICENSE("GPL");
416