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