input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / sintek_3FA16.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 #include "malata.h"
23
24
25 #define MAX_SUPPORT_POINT       2// //  4
26 #define PACKGE_BUFLEN           10
27
28 #if 0
29 #define DBG(x...) printk(KERN_INFO x)
30 #else
31 #define DBG(x...) do { } while (0)
32 #endif
33 //#define Singltouch_Mode
34 #define SAKURA_DBG                  0
35 #if SAKURA_DBG 
36 #define sakura_dbg_msg(fmt,...)       do {                                      \
37                                    printk("sakura dbg msg------>"                       \
38                                           " (func-->%s ; line-->%d) " fmt, __func__, __LINE__ , ##__VA_ARGS__); \
39                                   } while(0)
40 #define sakura_dbg_report_key_msg(fmt,...)      do{                                                     \
41                                                     printk("sakura report " fmt,##__VA_ARGS__);          \
42                                                 }while(0)
43 #else
44 #define sakura_dbg_msg(fmt,...)       do {} while(0)
45 #define sakura_dbg_report_key_msg(fmt,...)      do{}while(0)
46 #endif
47 struct point_data {     
48         short status;   
49         short x;        
50         short y;
51     short z;
52 };
53
54 struct multitouch_event{
55         struct point_data point_data[MAX_SUPPORT_POINT];
56         int contactid;
57     int validtouch;
58 };
59
60 struct ts_p1003 {
61         struct input_dev        *input;
62         char                    phys[32];
63         struct delayed_work     work;
64         struct workqueue_struct *wq;    
65         struct i2c_client       *client;
66     struct multitouch_event mt_event;
67         u16                     model;
68         spinlock_t      lock;
69         bool            pendown;
70         bool            status;
71         int                     irq;
72         int         delayed_work_tp;
73         int             has_relative_report;
74         int                     (*get_pendown_state)(void);
75         void            (*clear_penirq)(void);
76 };
77
78 int p1003_get_pendown_state(void)
79 {
80         return 0;
81 }
82
83 static void p1003_report_event(struct ts_p1003 *ts,struct multitouch_event *tc)
84 {
85         struct input_dev *input = ts->input;
86     int i,pandown = 0;
87         dev_dbg(&ts->client->dev, "UP\n");
88         DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);     
89     for(i=0; i<MAX_SUPPORT_POINT;i++){                  
90         if(tc->point_data[i].status >= 0){
91             pandown |= tc->point_data[i].status;
92             input_report_abs(input, ABS_MT_TRACKING_ID, i);                                                     
93             input_report_abs(input, ABS_MT_TOUCH_MAJOR, tc->point_data[i].status);                              
94             input_report_abs(input, ABS_MT_WIDTH_MAJOR, 0);     
95             input_report_abs(input, ABS_MT_POSITION_X, tc->point_data[i].x);                            
96             input_report_abs(input, ABS_MT_POSITION_Y, tc->point_data[i].y);                            
97             input_mt_sync(input);       
98
99             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);
100 #if defined(CONFIG_HANNSTAR_DEBUG)
101                         //printk("hannstar p1003 Px = [%d],Py = [%d] \n",tc->point_data[i].x,tc->point_data[i].y);
102 #endif
103
104             if(tc->point_data[i].status == 0)                                   
105                 tc->point_data[i].status--;                     
106         }
107         
108     }   
109
110     ts->pendown = pandown;
111     input_sync(input);
112 }
113
114 #if defined (Singltouch_Mode)
115 static void p1003_report_single_event(struct ts_p1003 *ts,struct multitouch_event *tc)
116 {
117         struct input_dev *input = ts->input;
118     int cid;
119
120     cid = tc->contactid;
121     if (ts->status) {
122         input_report_abs(input, ABS_X, tc->point_data[cid].x);
123         input_report_abs(input, ABS_Y, tc->point_data[cid].y);
124         input_sync(input);
125     }
126     if(ts->pendown != ts->status){
127         ts->pendown = ts->status;
128         input_report_key(input, BTN_TOUCH, ts->status);
129         input_sync(input);
130         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);
131     }
132 }
133 #endif
134
135 static inline int p1003_check_firmwork(struct ts_p1003 *ts)
136 {
137     int data;
138     char buf[10];
139         data = i2c_master_reg8_recv(ts->client, 26, buf, 6, 100*1000);
140         if(data < 0){
141                 dev_err(&ts->client->dev, "i2c io error %d \n", data);
142                 return data;
143         }
144         //printk("sintek reg[0] = %x ,reg[1] = %x, reg[2] = %x, reg[3] = %x\n" , buf[0],buf[1],buf[2],buf[3]);
145         //printk("sintek reg[4] = %x ,reg[5] = %x, reg[6] = %x, reg[7] = %x\n" , buf[4],buf[5],buf[6],buf[7]);
146         buf[0] = 0xa4; /*automatically jump sleep mode*/
147         //buf[0] =  0x0;
148         data = i2c_master_reg8_send(ts->client, MALATA_POWER_MODE, buf, 1, 100*1000);
149         if(data < 0){
150                 printk("i2c io error %d line=%d\n", data,__LINE__);
151                 return data;
152         }
153         buf[0] = 0x03;
154         data = i2c_master_reg8_send(ts->client, MALATA_SPECOP, buf, 1, 100*1000);
155         if(data < 0){
156                 printk( "i2c io error %d line=%d\n", data,__LINE__);
157                 return data;
158         }
159         #if 0
160         buf[0] = 0x80;
161         data = i2c_master_reg8_send(ts->client, MALATA_INT_MODE, buf, 1, 200*1000);
162         if(data < 0){
163                 printk( "i2c io error %d line=%d\n", data,__LINE__);
164                 return data;
165         }
166         #endif
167         data = i2c_master_reg8_recv(ts->client, MALATA_DATA_INFO, buf, 4, 100*1000);
168         if(data < 0){
169                 printk( "i2c io error %d line=%d\n", data,__LINE__);
170                 return data;
171         }
172         //printk("sintek reg[0] = %x ,reg[1] = %x, reg[2] = %x, reg[3] = %x\n" , buf[0],buf[1],buf[2],buf[3]);
173         //printk("sintek reg[4] = %x ,reg[5] = %x, reg[6] = %x, reg[7] = %x\n" , buf[4],buf[5],buf[6],buf[7]);
174     return data;
175 }
176
177 #define DATA_START      MALATA_DATA_INFO
178 #define DATA_END        MALATA_DATA_END
179 #define DATA_LEN        (DATA_END - DATA_START)
180 #define DATA_OFF(x) ((x) - DATA_START)
181
182
183 static inline int p1003_read_values(struct ts_p1003 *ts, struct multitouch_event *tc)
184 {
185        int data=0;
186        char buf[10];
187         data = i2c_master_reg8_recv(ts->client, 0x00, buf, 10, 100*1000);
188         if(data < 0){
189                 printk("-->%s i2c io error %d line=%d\n",__FUNCTION__, data,__LINE__);
190                 return data;
191         }
192 #if 0
193         if(buf[0]==0xff){
194                 printk("MALATA_TOUCH_NUM is 0xff full\n");
195                 return -1;
196         }
197 #endif
198         ///printk("MALATA_TOUCH_NUM = %x\n",buf[0]);
199         if(buf[0] == 1)
200         {
201                 ts->pendown = 1;
202                 tc->point_data[0].status = 1;
203                 tc->point_data[1].status = 0;
204         }
205         else if (buf[0] == 2){
206                 ts->pendown = 1;
207                 tc->point_data[0].status = 1;
208                 tc->point_data[1].status = 1;
209         }
210         else {
211                 ts->pendown = 0;
212                 tc->point_data[0].status = 0;
213                 tc->point_data[1].status = 0;
214                 //return 1;
215         }
216         //data = i2c_master_reg8_recv(ts->client, DATA_START, buf, DATA_LEN, 100*1000);
217         //if(data < 0){
218                 //printk("-->%s i2c io error %d line=%d\n",__FUNCTION__, data,__LINE__);
219                 //return data;
220         //}
221         tc->point_data[0].x = buf[DATA_OFF(MALATA_POS_X0_HI+2)] << 8;
222         tc->point_data[0].x |= buf[DATA_OFF(MALATA_POS_X0_LO+2)];
223         tc->point_data[0].y = buf[DATA_OFF(MALATA_POS_Y0_HI+2) ]<< 8;
224         tc->point_data[0].y |= buf[DATA_OFF(MALATA_POS_Y0_LO+2)];
225         tc->point_data[1].x = buf[DATA_OFF(MALATA_POS_X1_HI+2) ]<< 8;
226         tc->point_data[1].x |= buf[DATA_OFF(MALATA_POS_X1_LO+2)];
227         tc->point_data[1].y = buf[DATA_OFF(MALATA_POS_Y1_HI+2) ]<< 8;
228         tc->point_data[1].y |= buf[DATA_OFF(MALATA_POS_Y1_LO+2)];
229         //printk("sintek tc->point_data[0].x= %d tc->point_data[0].y=%d\n ",tc->point_data[0].x,tc->point_data[0].y);
230         //printk("sintek tc->point_data[1].x= %d tc->point_data[1].y=%d\n ",tc->point_data[1].x,tc->point_data[1].y);
231 //      tc->point_data[0].status = 0;
232 //      tc->point_data[1].status = 0;
233 //      if (tc->point_data[0].x ||tc->point_data[0].y)
234 //              tc->point_data[0].status = 1;
235 //      if (tc->point_data[1].x ||tc->point_data[1].y)
236 //              tc->point_data[1].status = 1;
237     return 10;
238 }
239
240
241 static void p1003_work(struct work_struct *work)
242 {
243         struct ts_p1003 *ts =
244                 container_of(to_delayed_work(work), struct ts_p1003, work);
245         struct multitouch_event *tc = &ts->mt_event;
246 #if 0   
247    int data;
248     char buf[10];       
249         buf[0] = 0x03;
250         DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
251         if(ts->delayed_work_tp == 1){
252                 data = i2c_master_reg8_send(ts->client, MALATA_SPECOP, buf, 1, 100*1000);
253                 if(data < 0){
254                         printk( "i2c io error %d line=%d\n", data,__LINE__);
255                 }
256                 ts->delayed_work_tp = 0;
257                 return ;
258                 }
259         #endif
260                 if( p1003_read_values(ts,tc)<0)
261                 {
262                         printk("-->%s p1003_read_values error  line=%d\n",__FUNCTION__,__LINE__);
263                         goto out ;
264                 }
265
266 #if defined (Singltouch_Mode)
267     p1003_report_single_event(ts,tc);
268 #else
269     p1003_report_event(ts,tc);
270 #endif
271
272 out:               
273         if (ts->pendown){
274                 schedule_delayed_work(&ts->work, msecs_to_jiffies(21));
275                 ts->pendown = 0;
276         }
277         else{
278                 enable_irq(ts->irq);
279         }
280
281 }
282
283 static irqreturn_t p1003_irq(int irq, void *handle)
284 {
285         struct ts_p1003 *ts = handle;
286         unsigned long flags;
287         DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
288         
289         spin_lock_irqsave(&ts->lock,flags);
290         if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
291                 disable_irq_nosync(ts->irq);
292                 schedule_delayed_work(&ts->work,msecs_to_jiffies(2));
293         }
294         spin_unlock_irqrestore(&ts->lock,flags);
295         return IRQ_HANDLED;
296 }
297
298 static void p1003_free_irq(struct ts_p1003 *ts)
299 {
300         free_irq(ts->irq, ts);
301         if (cancel_delayed_work_sync(&ts->work)) {
302                 /*
303                  * Work was pending, therefore we need to enable
304                  * IRQ here to balance the disable_irq() done in the
305                  * interrupt handler.
306                  */
307                 enable_irq(ts->irq);
308         }
309 }
310
311 static void Sintek_work_delay(struct work_struct *work)
312 {
313         struct ts_p1003 *ts = container_of(to_delayed_work(work), struct ts_p1003, work);
314         int data;
315     char buf[10];       
316         buf[0] = 0x03;
317         data = i2c_master_reg8_send(ts->client, MALATA_SPECOP, buf, 1, 100*1000);
318         if(data < 0)
319                 printk( "i2c io error %d line=%d\n", data,__LINE__);
320         return;
321 }
322 struct ts_p1003  *ts_pub;
323 static ssize_t pc1003_touchdebug_show(struct device *dev,struct device_attribute *attr,char *_buf)
324 {
325         // struct       ts_p103 *ts  = dev_get_drvdata(dev);
326          int data;
327    char buf[10];        
328          buf[0] = 0x03;
329          printk("Enter:%s %d\n",__FUNCTION__,__LINE__);
330          printk("Touchscreen correct!!\n");
331          data = i2c_master_reg8_send(ts_pub->client, MALATA_SPECOP, buf, 1, 100*1000);
332                  if(data < 0){
333                         printk( "i2c io error %d line=%d\n", data,__LINE__);
334          }
335          return sprintf(_buf, "successful\n");
336 }
337
338 static DEVICE_ATTR(touchdebug, 0666, pc1003_touchdebug_show, NULL);
339
340 static int __devinit sintek_touch_probe(struct i2c_client *client,
341                                    const struct i2c_device_id *id)
342 {
343         struct ts_p1003 *ts;
344         struct p1003_platform_data *pdata = pdata = client->dev.platform_data;
345         struct input_dev *input_dev;
346         int err;
347                 printk(">--------%s\n",__FUNCTION__);
348         if (!pdata) {
349                 dev_err(&client->dev, "platform data is required!\n");
350                 return -EINVAL;
351         }
352
353         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
354                 return -EIO;
355
356         ts = kzalloc(sizeof(struct ts_p1003), GFP_KERNEL);
357         input_dev = input_allocate_device();
358         if (!ts || !input_dev) {
359                 err = -ENOMEM;
360                 goto err_free_mem;
361         }
362
363         ts->client = client;
364         ts->irq = client->irq;
365         ts->input = input_dev;
366         ts->status =0 ;// fjp add by 2010-9-30
367         ts->pendown = 0; // fjp add by 2010-10-06
368         ts->delayed_work_tp = 0; ///1;
369         //ts->wq = create_rt_workqueue("p1003_wq");
370         INIT_DELAYED_WORK(&ts->work, p1003_work);
371
372         ts->model             = pdata->model;
373
374         snprintf(ts->phys, sizeof(ts->phys),
375                  "%s/input0", dev_name(&client->dev));
376
377         input_dev->name = "p1003 Touchscreen";
378         input_dev->phys = ts->phys;
379         input_dev->id.bustype = BUS_I2C;
380
381 #if defined (Singltouch_Mode)
382         set_bit(EV_SYN, input_dev->evbit);
383         set_bit(EV_KEY, input_dev->evbit);
384         set_bit(BTN_TOUCH, input_dev->keybit);
385         set_bit(BTN_2, input_dev->keybit);
386         set_bit(EV_ABS, input_dev->evbit);
387         input_set_abs_params(input_dev,ABS_X,0,CONFIG_HANNSTAR_MAX_X,0,0);
388         input_set_abs_params(input_dev,ABS_Y,0,CONFIG_HANNSTAR_MAX_Y,0,0);
389 #else
390         ts->has_relative_report = 0;
391         input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
392         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
393         input_dev->keybit[BIT_WORD(BTN_2)] = BIT_MASK(BTN_2); //jaocbchen for dual
394         input_set_abs_params(input_dev, ABS_X, 7, 1020/*CONFIG_HANNSTAR_MAX_X*/, 0, 0);
395         input_set_abs_params(input_dev, ABS_Y, 10, 586/* CONFIG_HANNSTAR_MAX_Y*/, 0, 0);
396         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
397         input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
398         input_set_abs_params(input_dev, ABS_HAT0X, 7, 1020/*CONFIG_HANNSTAR_MAX_X*/, 0, 0);
399         input_set_abs_params(input_dev, ABS_HAT0Y, 10,586/* CONFIG_HANNSTAR_MAX_Y*/, 0, 0);
400         input_set_abs_params(input_dev, ABS_MT_POSITION_X,7, 1020/*CONFIG_HANNSTAR_MAX_X*/, 0, 0);
401         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 10, 586/* CONFIG_HANNSTAR_MAX_Y*/, 0, 0);
402         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
403         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
404         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);   
405 #endif
406
407         if (pdata->init_platform_hw)
408                 pdata->init_platform_hw();
409
410         
411 #if 0
412         err = set_irq_type(ts->irq,IRQ_TYPE_LEVEL_LOW);
413         if (err < 0) {
414                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
415                 goto err_free_mem;
416         }
417         if (err < 0)
418                 goto err_free_irq;
419 #endif
420         err = input_register_device(input_dev);
421         if (err)
422                 goto err_free_irq;
423
424         i2c_set_clientdata(client, ts);
425
426         p1003_check_firmwork(ts);
427         //schedule_delayed_work(&ts->work, msecs_to_jiffies(8 * 1000)); 
428         if (!ts->irq) {
429                 dev_dbg(&ts->client->dev, "no IRQ?\n");
430                 return -ENODEV;
431         }else{
432                 ts->irq = gpio_to_irq(ts->irq);
433         }
434
435         err = request_irq(ts->irq, p1003_irq, IRQF_TRIGGER_LOW,
436                         client->dev.driver->name, ts);
437         
438         if (err < 0) {
439                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
440                 goto err_free_mem;
441         }
442
443         
444         if (err < 0)
445                 goto err_free_irq;
446         ts_pub = ts;
447         err = device_create_file(&ts->client->dev,&dev_attr_touchdebug);
448         if(err)
449                 printk("%s->%d cannot create status attribute\n",__FUNCTION__,__LINE__);
450         return 0;
451
452  err_free_irq:
453         p1003_free_irq(ts);
454         if (pdata->exit_platform_hw)
455                 pdata->exit_platform_hw();
456  err_free_mem:
457         input_free_device(input_dev);
458         kfree(ts);
459         return err;
460 }
461
462 static int __devexit p1003_remove(struct i2c_client *client)
463 {
464         struct ts_p1003 *ts = i2c_get_clientdata(client);
465         struct p1003_platform_data *pdata = client->dev.platform_data;
466
467         p1003_free_irq(ts);
468
469         if (pdata->exit_platform_hw)
470                 pdata->exit_platform_hw();
471
472         input_unregister_device(ts->input);
473         kfree(ts);
474
475         return 0;
476 }
477
478 static struct i2c_device_id sintek_idtable[] = {
479         { "sintek_touch", 0 },
480         { }
481 };
482
483 MODULE_DEVICE_TABLE(i2c, sintek_idtable);
484
485 static struct i2c_driver sintek_touch_driver = {
486         .driver = {
487                 .owner  = THIS_MODULE,
488                 .name   = "sintek_touch"
489         },
490         .id_table       = sintek_idtable,
491         .probe          = sintek_touch_probe,
492         .remove         = __devexit_p(p1003_remove),
493 };
494
495 static void __init sintek_touch_init_async(void *unused, async_cookie_t cookie)
496 {
497         printk("--------> %s <-------------\n",__func__);
498         i2c_add_driver(&sintek_touch_driver);
499 }
500
501 static int __init sintek_touch_init(void)
502 {
503         async_schedule(sintek_touch_init_async, NULL);
504         return 0;
505 }
506
507 static void __exit sintek_touch_exit(void)
508 {
509         return i2c_del_driver(&sintek_touch_driver);
510 }
511 module_init(sintek_touch_init);
512 module_exit(sintek_touch_exit);
513 MODULE_LICENSE("GPL");
514