update board-rk29-a22.c from board-rk29-phonesdk.c
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ili2102_ts.c
1 #include <linux/module.h>
2 #include <linux/delay.h>
3 #include <linux/earlysuspend.h>
4 #include <linux/hrtimer.h>
5 #include <linux/i2c.h>
6 #include <linux/input.h>
7 #include <linux/interrupt.h>
8 #include <linux/io.h>
9 #include <linux/gpio.h>
10 #include <linux/types.h>
11 #include <mach/iomux.h>
12 #include <linux/platform_device.h>
13
14 #include "ili2102_ts.h"
15
16
17 #if 0
18         #define DBG(msg...)     printk(msg);
19 #else
20         #define DBG(msg...)
21 #endif
22
23 #define TOUCH_NUMBER 2
24
25 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
26 static unsigned int g_x[TOUCH_NUMBER] =  {0},g_y[TOUCH_NUMBER] = {0};
27
28 struct ili2102_ts_data {
29         u16             model;                  /* 801. */      
30         bool    swap_xy;                /* swap x and y axes */ 
31         u16             x_min, x_max;   
32         u16             y_min, y_max;
33         uint16_t addr;
34         int     use_irq;
35         int     pendown;
36         int     gpio_pendown;
37         int     gpio_reset;
38         int     gpio_reset_active_low;
39         int             pendown_iomux_mode;     
40         int             resetpin_iomux_mode;
41         char    pendown_iomux_name[IOMUX_NAME_SIZE];    
42         char    resetpin_iomux_name[IOMUX_NAME_SIZE];   
43         char    phys[32];
44         char    name[32];
45         struct  i2c_client *client;
46         struct  input_dev *input_dev;
47         struct  hrtimer timer;
48         struct  delayed_work    work;
49         struct  workqueue_struct *ts_wq;        
50         struct  early_suspend early_suspend;
51 };
52
53 #ifdef CONFIG_HAS_EARLYSUSPEND
54 static void ili2102_ts_early_suspend(struct early_suspend *h);
55 static void ili2102_ts_late_resume(struct early_suspend *h);
56 #endif
57
58 static int verify_coord(struct ili2102_ts_data *ts,unsigned int *x,unsigned int *y)
59 {
60
61         DBG("%s:(%d/%d)\n",__FUNCTION__,*x, *y);
62         if((*x< ts->x_min) || (*x > ts->x_max))
63                 return -1;
64
65         if((*y< ts->y_min) || (*y > ts->y_max))
66                 return -1;
67
68         return 0;
69 }
70 static int ili2102_init_panel(struct ili2102_ts_data *ts)
71 {       
72         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
73         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
74         mdelay(1);
75         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
76         mdelay(100);//need?
77         return 0;
78 }
79
80 static void ili2102_ts_work_func(struct work_struct *work)
81 {
82         int i,ret;
83         int syn_flag = 0;
84         unsigned int x, y;
85         struct i2c_msg msg[2];
86         uint8_t start_reg;
87         uint8_t buf[9];//uint32_t buf[4];
88         struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
89
90         DBG("ili2102_ts_work_func\n");
91
92         /*Touch Information Report*/
93         start_reg = 0x10;
94
95         msg[0].addr = ts->client->addr;
96         msg[0].flags = 0;
97         msg[0].len = 1;
98         msg[0].buf = &start_reg;
99         msg[0].scl_rate = 200*1000;
100
101         msg[1].addr = ts->client->addr;
102         msg[1].flags = I2C_M_RD;
103         msg[1].len = 9; 
104         msg[1].buf = buf;//msg[1].buf = (u8*)&buf[0];
105         msg[1].scl_rate = 200*1000;
106
107         ret = i2c_transfer(ts->client->adapter, msg, 2); 
108         if (ret < 0) 
109         {
110                 //for(i=0; i<msg[1].len; i++) 
111                 //buf[i] = 0xff;
112                 printk("%s:i2c_transfer fail, ret=%d\n",__FUNCTION__,ret);
113                 goto out;
114         }
115
116         for(i=0; i<TOUCH_NUMBER; i++)
117         {
118
119                 if(!((buf[0]>>i)&0x01))
120                 {
121                         if (touch_state[i] == TOUCH_DOWN)
122                         {
123                                 DBG("ili2102_ts_work_func:buf[%d]=%d\n",i,buf[i]);
124                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0); //Finger Size
125                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0); //Touch Size
126                                 input_mt_sync(ts->input_dev);
127                                 syn_flag = 1;
128                                 touch_state[i] = TOUCH_UP;
129                                 printk("touch_up \n");
130                         }
131
132                 }
133                 else
134                 {
135                         if((buf[0]>>i)&0x01)
136                         {
137                                 x = buf[1+(i<<2)] | (buf[2+(i<<2)] << 8);
138                                 y = buf[3+(i<<2)] | (buf[4+(i<<2)] << 8);
139                                 
140                                 if (ts->swap_xy)
141                                 swap(x, y);
142
143                                 if (verify_coord(ts,&x,&y))//goto out;
144                                 {
145                                         x = g_x[i];
146                                         y = g_y[i];
147                                 }
148
149                                 g_x[i] = x;
150                                 g_y[i] = y;                     
151                                 input_event(ts->input_dev, EV_ABS, ABS_MT_TRACKING_ID, i);
152                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1); //Finger Size
153                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
154                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
155                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 5); //Touch Size
156                                 input_mt_sync(ts->input_dev);
157                                 syn_flag = 1;
158                                 touch_state[i] = TOUCH_DOWN;
159                                  ts->pendown = 1;
160                                 printk("touch_down X = %d, Y = %d\n", x, y);
161                         }
162                         
163                 }
164         }
165         
166         if(syn_flag)
167         input_sync(ts->input_dev);
168 out:   
169         if(ts->pendown)
170         {
171                 schedule_delayed_work(&ts->work, msecs_to_jiffies(20));
172                 ts->pendown = 0;
173         }
174         else
175         {
176                 if (ts->use_irq) 
177                 enable_irq(ts->client->irq);
178         }
179
180         DBG("pin=%d,level=%d,irq=%d\n",irq_to_gpio(ts->client->irq),gpio_get_value(irq_to_gpio(ts->client->irq)),ts->client->irq);
181
182 }
183
184 static irqreturn_t ili2102_ts_irq_handler(int irq, void *dev_id)
185 {
186         struct ili2102_ts_data *ts = dev_id;
187         DBG("ili2102_ts_irq_handler=%d,%d\n",ts->client->irq,ts->use_irq);
188
189         disable_irq_nosync(ts->client->irq); //disable_irq(ts->client->irq);
190         schedule_delayed_work(&ts->work, 0);
191         return IRQ_HANDLED;
192 }
193
194 static int __devinit setup_resetPin(struct i2c_client *client, struct ili2102_ts_data *ts)
195 {
196         struct ili2102_platform_data    *pdata = client->dev.platform_data;
197         int err;
198         
199         ts->gpio_reset = pdata->gpio_reset;
200         strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
201         ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
202         ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
203         
204         DBG("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
205
206         if (!gpio_is_valid(ts->gpio_reset)) {
207                 dev_err(&client->dev, "no gpio_reset?\n");
208                 return -EINVAL;
209         }
210
211         rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode); 
212         err = gpio_request(ts->gpio_reset, "ili2102_resetPin");
213         if (err) {
214                 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
215                                 ts->gpio_reset);
216                 return err;
217         }
218         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
219         mdelay(100);
220
221         err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
222         if (err) {
223                 dev_err(&client->dev, "failed to pulldown resetPin GPIO%d\n",
224                                 ts->gpio_reset);
225                 gpio_free(ts->gpio_reset);
226                 return err;
227         }
228         
229         mdelay(1);
230
231         gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
232
233         mdelay(100);
234          
235         return 0;
236 }
237
238 static int __devinit setup_pendown(struct i2c_client *client, struct ili2102_ts_data *ts)
239 {
240         int err;
241         struct ili2102_platform_data    *pdata = client->dev.platform_data;
242         
243         if (!client->irq) {
244                 dev_dbg(&client->dev, "no IRQ?\n");
245                 return -ENODEV;
246         }
247         
248         if (!gpio_is_valid(pdata->gpio_pendown)) {
249                 dev_err(&client->dev, "no gpio_pendown?\n");
250                 return -EINVAL;
251         }
252         
253         ts->gpio_pendown = pdata->gpio_pendown;
254         strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
255         ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
256         
257         DBG("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
258         
259         if (!gpio_is_valid(ts->gpio_pendown)) {
260                 dev_err(&client->dev, "no gpio_pendown?\n");
261                 return -EINVAL;
262         }
263         
264         rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode);
265         err = gpio_request(ts->gpio_pendown, "ili2102_pendown");
266         if (err) {
267                 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
268                                 ts->gpio_pendown);
269                 return err;
270         }
271         
272         err = gpio_pull_updown(ts->gpio_pendown, GPIOPullUp);
273         if (err) {
274                 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
275                                 ts->gpio_pendown);
276                 gpio_free(ts->gpio_pendown);
277                 return err;
278         }
279         return 0;
280 }
281
282 static int ili2102_chip_Init(struct i2c_client *client)
283 {       
284         int ret = 0;
285         uint8_t start_reg;
286         uint8_t buf[6];
287         struct i2c_msg msg[2];
288         
289         /* get panel information:6bytes */
290         start_reg = 0x20;
291         msg[0].addr =client->addr;
292         msg[0].flags = 0;
293         msg[0].len = 1;
294         msg[0].buf = &start_reg;
295         msg[0].scl_rate = 200*1000;
296
297         msg[1].addr = client->addr;
298         msg[1].flags = I2C_M_RD;
299         msg[1].len = 6;
300         msg[1].buf = (u8*)&buf[0];
301         msg[1].scl_rate = 200*1000;
302
303         ret = i2c_transfer(client->adapter, msg, 2);   
304         if (ret < 0) {
305         printk("%s:err\n",__FUNCTION__);
306         }
307
308         printk("%s:b[0]=0x%x,b[1]=0x%x,b[2]=0x%x,b[3]=0x%x,b[4]=0x%x,b[5]=0x%x\n", 
309                 __FUNCTION__,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
310
311         /*get firmware version:3bytes */        
312         start_reg = 0x40;
313         msg[0].addr =client->addr;
314         msg[0].flags = 0;
315         msg[0].len = 1;
316         msg[0].buf = &start_reg;
317         msg[0].scl_rate = 200*1000;
318
319         msg[1].addr = client->addr;
320         msg[1].flags = I2C_M_RD;
321         msg[1].len = 3;
322         msg[1].buf = (u8*)&buf[0];
323         msg[1].scl_rate =200*1000;
324
325         ret = i2c_transfer(client->adapter, msg, 2);
326         if (ret < 0) {
327         printk("%s:err\n",__FUNCTION__);
328         }
329
330         printk("%s:Ver %d.%d.%d\n",__FUNCTION__,buf[0],buf[1],buf[2]);
331
332         return 0;
333     
334 }
335
336 static int ili2102_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
337 {
338         struct ili2102_ts_data *ts;
339         struct ili2102_platform_data    *pdata = client->dev.platform_data;
340         int ret = 0;
341
342         printk("ili2102 TS probe\n"); 
343     
344         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
345             printk(KERN_ERR "ili2102_ts_probe: need I2C_FUNC_I2C\n");
346             ret = -ENODEV;
347             goto err_check_functionality_failed;
348         }
349
350         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
351         if (ts == NULL) {
352             ret = -ENOMEM;
353             goto err_alloc_data_failed;
354         }
355         
356         ts->ts_wq = create_singlethread_workqueue("ts_wq");
357         if (!ts->ts_wq)
358         {
359                 printk("%s:fail to create ts_wq,ret=0x%x\n",__FUNCTION__, ENOMEM);
360                 return -ENOMEM;
361         }
362         //INIT_WORK(&ts->work, ili2102_ts_work_func);
363         INIT_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
364         ts->client = client;
365         i2c_set_clientdata(client, ts);
366
367         ret = setup_resetPin(client,ts);
368         if(ret)
369         {
370                  printk("ili2102 TS setup_resetPin fail\n");
371                  goto err_alloc_data_failed;
372         }
373
374         ret=ili2102_chip_Init(ts->client);
375         if(ret<0)
376         {
377                 printk("%s:chips init failed\n",__FUNCTION__);
378                 goto err_resetpin_failed;
379         }
380
381         /* allocate input device */
382         ts->input_dev = input_allocate_device();
383         if (ts->input_dev == NULL) {
384             ret = -ENOMEM;
385             printk(KERN_ERR "ili2102_ts_probe: Failed to allocate input device\n");
386             goto err_input_dev_alloc_failed;
387         }
388
389         ts->model = pdata->model ? : 801;
390         ts->swap_xy = pdata->swap_xy;
391         ts->x_min = pdata->x_min;
392         ts->x_max = pdata->x_max;
393         ts->y_min = pdata->y_min;
394         ts->y_max = pdata->y_max;
395         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
396         snprintf(ts->name, sizeof(ts->name), "ili%d-touchscreen", ts->model);
397         ts->input_dev->phys = ts->phys;
398         ts->input_dev->name = ts->name;
399         ts->input_dev->dev.parent = &client->dev;
400         ts->pendown = 0;
401
402         ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_ABS);
403         //ts->input_dev->absbit[0] = 
404                 //BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) | 
405                 //BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR);  // for android
406         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 
407                     ts->x_min ? : 0,
408                         ts->x_max ? : 480,
409                         0, 0);
410         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
411                         ts->y_min ? : 0,
412                         ts->y_max ? : 800,
413                         0, 0);
414         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0); //Finger Size
415         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0); //Touch Size
416
417         /* ts->input_dev->name = ts->keypad_info->name; */
418         ret = input_register_device(ts->input_dev);
419         if (ret) {
420             printk(KERN_ERR "ili2102_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
421             goto err_input_register_device_failed;
422         }
423
424         client->irq = gpio_to_irq(client->irq);
425         if (client->irq) 
426         {
427                 ret = setup_pendown(client,ts);
428                 if(ret)
429                 {
430                          printk("ili2102 TS setup_pendown fail\n");
431                          goto err_input_register_device_failed;
432                 }
433                 ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, client->name, ts);
434                 if (ret == 0) {
435                     DBG("ili2102 TS register ISR (irq=%d)\n", client->irq);
436                     ts->use_irq = 1;
437                 }
438                 else 
439                 dev_err(&client->dev, "request_irq failed\n");
440         }
441
442 #ifdef CONFIG_HAS_EARLYSUSPEND
443         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
444         ts->early_suspend.suspend = ili2102_ts_early_suspend;
445         ts->early_suspend.resume = ili2102_ts_late_resume;
446         register_early_suspend(&ts->early_suspend);
447 #endif
448
449         printk(KERN_INFO "ili2102_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
450
451         return 0;
452
453         err_input_register_device_failed:
454         input_free_device(ts->input_dev);
455         err_resetpin_failed:
456         gpio_free(ts->gpio_reset);
457         err_input_dev_alloc_failed:
458         kfree(ts);
459         err_alloc_data_failed:
460         err_check_functionality_failed:
461         return ret;
462 }
463
464 static int ili2102_ts_remove(struct i2c_client *client)
465 {
466         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
467         unregister_early_suspend(&ts->early_suspend);
468         if (ts->use_irq)
469         free_irq(client->irq, ts);
470         else
471         hrtimer_cancel(&ts->timer);
472         input_unregister_device(ts->input_dev);
473         if (ts->ts_wq)
474         cancel_delayed_work_sync(&ts->work);
475         kfree(ts);
476         return 0;
477 }
478
479 static int ili2102_ts_suspend(struct i2c_client *client, pm_message_t mesg)
480 {
481         int ret;
482         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
483         uint8_t buf[2] = {0x30,0x30};
484         struct i2c_msg msg[1];
485         
486         if (ts->use_irq)
487         disable_irq(client->irq);
488         else
489         hrtimer_cancel(&ts->timer);
490
491         ret = cancel_delayed_work_sync(&ts->work);
492         if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
493         enable_irq(client->irq);
494
495         //to do suspend
496         msg[0].addr =client->addr;
497         msg[0].flags = 0;
498         msg[0].len = 2;
499         msg[0].buf = buf;
500
501         ret = i2c_transfer(client->adapter, msg, 1);
502         if (ret < 0) {
503         printk("%s:err\n",__FUNCTION__);
504         }
505         
506         DBG("%s\n",__FUNCTION__);
507         
508         return 0;
509 }
510
511 static int ili2102_ts_resume(struct i2c_client *client)
512 {
513         struct ili2102_ts_data *ts = i2c_get_clientdata(client);
514         
515         //to do resume
516         ili2102_init_panel(ts);
517         
518         if (ts->use_irq) {
519                 printk("enabling IRQ %d\n", client->irq);
520                 enable_irq(client->irq);
521         }
522         //else
523         //hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
524
525         DBG("%s\n",__FUNCTION__);
526
527     return 0;
528 }
529
530 #ifdef CONFIG_HAS_EARLYSUSPEND
531 static void ili2102_ts_early_suspend(struct early_suspend *h)
532 {
533     struct ili2102_ts_data *ts;
534     ts = container_of(h, struct ili2102_ts_data, early_suspend);
535     ili2102_ts_suspend(ts->client, PMSG_SUSPEND);
536 }
537
538 static void ili2102_ts_late_resume(struct early_suspend *h)
539 {
540     struct ili2102_ts_data *ts;
541     ts = container_of(h, struct ili2102_ts_data, early_suspend);
542     ili2102_ts_resume(ts->client);
543 }
544 #endif
545
546 #define ILI2102_TS_NAME "ili2102_ts"
547
548 static const struct i2c_device_id ili2102_ts_id[] = {
549     { ILI2102_TS_NAME, 0 },
550     { }
551 };
552
553 static struct i2c_driver ili2102_ts_driver = {
554     .probe      = ili2102_ts_probe,
555     .remove     = ili2102_ts_remove,
556 #ifndef CONFIG_HAS_EARLYSUSPEND
557     .suspend    = ili2102_ts_early_suspend,
558     .resume     = ili2102_ts_late_resume,
559 #endif
560     .id_table   = ili2102_ts_id,
561     .driver = {
562         .name   = ILI2102_TS_NAME,
563     },
564 };
565
566 static int __devinit ili2102_ts_init(void)
567 {
568     return i2c_add_driver(&ili2102_ts_driver);
569 }
570
571 static void __exit ili2102_ts_exit(void)
572 {
573         i2c_del_driver(&ili2102_ts_driver);
574 }
575
576 module_init(ili2102_ts_init);
577 module_exit(ili2102_ts_exit);
578
579 MODULE_DESCRIPTION("ili2102 Touchscreen Driver");
580 MODULE_LICENSE("GPL");