Merge tag 'v4.4-rc5'
[firefly-linux-kernel-4.4.55.git] / drivers / input / ts / ts-auto.c
1 /* drivers/input/ts/ts-auto.c - handle all touchscreen in this file\r
2  *\r
3  * Copyright (C) 2012-2015 ROCKCHIP.\r
4  * Author: luowei <lw@rock-chips.com>\r
5  *\r
6  * This software is licensed under the terms of the GNU General Public\r
7  * License version 2, as published by the Free Software Foundation, and\r
8  * may be copied, distributed, and modified under those terms.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  */\r
16 \r
17 #include <linux/interrupt.h>\r
18 #include <linux/i2c.h>\r
19 #include <linux/slab.h>\r
20 #include <linux/irq.h>\r
21 #include <linux/miscdevice.h>\r
22 #include <linux/gpio.h>\r
23 #include <asm/uaccess.h>\r
24 #include <asm/atomic.h>\r
25 #include <linux/delay.h>\r
26 #include <linux/input.h>\r
27 #include <linux/workqueue.h>\r
28 #include <linux/freezer.h>\r
29 #include <linux/input/mt.h>\r
30 #include <mach/gpio.h>\r
31 #include <mach/board.h> \r
32 #ifdef CONFIG_HAS_EARLYSUSPEND\r
33 #include <linux/earlysuspend.h>\r
34 #endif\r
35 \r
36 #include <linux/ts-auto.h>\r
37 \r
38 \r
39 #if 0\r
40 #define DBG(x...)  printk(x)\r
41 #else\r
42 #define DBG(x...)\r
43 #endif\r
44 \r
45 struct ts_private_data *g_ts;\r
46 static struct class *g_ts_class;\r
47 static struct ts_operate *g_ts_ops[TS_NUM_ID]; \r
48 \r
49 \r
50 /**\r
51  * ts_reg_read: Read a single ts register.\r
52  *\r
53  * @ts: Device to read from.\r
54  * @reg: Register to read.\r
55  */\r
56 int ts_reg_read(struct ts_private_data *ts, unsigned short reg)\r
57 {\r
58         unsigned short val;\r
59         int ret;\r
60 \r
61         mutex_lock(&ts->io_lock);\r
62 \r
63         ret = ts->read_dev(ts, reg, ts->ops->reg_size, &val, ts->ops->reg_size);\r
64 \r
65         mutex_unlock(&ts->io_lock);\r
66 \r
67         if (ret < 0)\r
68                 return ret;\r
69         else\r
70                 return val;\r
71 }\r
72 EXPORT_SYMBOL_GPL(ts_reg_read);\r
73 \r
74 /**\r
75  * ts_bulk_read: Read multiple ts registers\r
76  *\r
77  * @ts: Device to read from\r
78  * @reg: First register\r
79  * @count: Number of registers\r
80  * @buf: Buffer to fill.\r
81  */\r
82 int ts_bulk_read(struct ts_private_data *ts, unsigned short reg,\r
83                      int count, unsigned char *buf)\r
84 {\r
85         int ret;\r
86 \r
87         mutex_lock(&ts->io_lock);\r
88 \r
89         ret = ts->read_dev(ts, reg, count, buf, ts->ops->reg_size);\r
90 \r
91         mutex_unlock(&ts->io_lock);\r
92 \r
93         return ret;\r
94 }\r
95 EXPORT_SYMBOL_GPL(ts_bulk_read);\r
96 \r
97 \r
98 /**\r
99  * ts_reg_write: Write a single ts register.\r
100  *\r
101  * @ts: Device to write to.\r
102  * @reg: Register to write to.\r
103  * @val: Value to write.\r
104  */\r
105 int ts_reg_write(struct ts_private_data *ts, unsigned short reg,\r
106                      unsigned short val)\r
107 {\r
108         int ret;\r
109 \r
110         mutex_lock(&ts->io_lock);\r
111 \r
112         ret = ts->write_dev(ts, reg, ts->ops->reg_size, &val, ts->ops->reg_size);\r
113 \r
114         mutex_unlock(&ts->io_lock);\r
115 \r
116         return ret;\r
117 }\r
118 EXPORT_SYMBOL_GPL(ts_reg_write);\r
119 \r
120 \r
121 int ts_bulk_write(struct ts_private_data *ts, unsigned short reg,\r
122                      int count, unsigned char *buf)\r
123 {\r
124         int ret;\r
125 \r
126         mutex_lock(&ts->io_lock);\r
127 \r
128         ret = ts->write_dev(ts, reg, count, buf, ts->ops->reg_size);\r
129 \r
130         mutex_unlock(&ts->io_lock);\r
131 \r
132         return ret;\r
133 }\r
134 EXPORT_SYMBOL_GPL(ts_bulk_write);\r
135 \r
136 \r
137 \r
138 /**\r
139  * ts_set_bits: Set the value of a bitfield in a ts register\r
140  *\r
141  * @ts: Device to write to.\r
142  * @reg: Register to write to.\r
143  * @mask: Mask of bits to set.\r
144  * @val: Value to set (unshifted)\r
145  */\r
146 int ts_set_bits(struct ts_private_data *ts, unsigned short reg,\r
147                     unsigned short mask, unsigned short val)\r
148 {\r
149         int ret;\r
150         u16 r;\r
151 \r
152         mutex_lock(&ts->io_lock);\r
153 \r
154         ret = ts->read_dev(ts, reg, ts->ops->reg_size, &r, ts->ops->reg_size);\r
155         if (ret < 0)\r
156                 goto out;\r
157 \r
158         r &= ~mask;\r
159         r |= val;\r
160 \r
161         ret = ts->write_dev(ts, reg, ts->ops->reg_size, &r, ts->ops->reg_size);\r
162 \r
163 out:\r
164         mutex_unlock(&ts->io_lock);\r
165 \r
166         return ret;\r
167 }\r
168 EXPORT_SYMBOL_GPL(ts_set_bits);\r
169 \r
170 static int ts_get_id(struct ts_operate *ops, struct ts_private_data *ts, int *value)\r
171 {       \r
172         int result = 0;\r
173         char temp[4] = {ops->id_reg & 0xff};\r
174         int i = 0;\r
175         \r
176         DBG("%s:start\n",__func__);\r
177         if(ops->id_reg >= 0)\r
178         {\r
179                 for(i=0; i<2; i++)\r
180                 {\r
181                         if(ops->reg_size == 2)\r
182                         {\r
183                                 temp[0] = ops->id_reg >> 8;\r
184                                 temp[1] = ops->id_reg & 0xff;\r
185                                 result = ts->read_dev(ts, ops->id_reg, 2, temp, ops->reg_size);\r
186                                 *value = (temp[0] << 8) | temp[1];\r
187                         }\r
188                         else\r
189                         {\r
190                                 result = ts->read_dev(ts, ops->id_reg, 1, temp, ops->reg_size);\r
191                                 *value = temp[0];\r
192                         }\r
193                         if(!result)\r
194                         break;\r
195 \r
196                 }\r
197 \r
198                 if(result)\r
199                         return result;\r
200                 \r
201                 if((ops->id_data != TS_UNKNOW_DATA)&&(ops->id_data != *value)) \r
202                 {\r
203                         printk("%s:id=0x%x is not 0x%x\n",__func__,*value, ops->id_data);\r
204                         result = -1;\r
205                 }\r
206                         \r
207                 DBG("%s:devid=0x%x\n",__func__,*value);\r
208         }\r
209 \r
210         return result;\r
211 }\r
212 \r
213 \r
214 static int ts_get_version(struct ts_operate *ops, struct ts_private_data *ts)\r
215 {\r
216         int result = 0;\r
217         char temp[TS_MAX_VER_LEN + 1] = {0};\r
218         int i = 0;\r
219         \r
220         DBG("%s:start\n",__func__);\r
221         \r
222         if(ops->version_reg >= 0)\r
223         {\r
224                 if((ops->version_len < 0) || (ops->version_len > TS_MAX_VER_LEN))\r
225                 {\r
226                         printk("%s:version_len %d is error\n",__func__,ops->version_len);\r
227                         ops->version_len = TS_MAX_VER_LEN;\r
228                 }\r
229         \r
230                 result = ts->read_dev(ts, ops->version_reg, ops->version_len, temp, ops->reg_size);\r
231                 if(result)\r
232                         return result;\r
233                 \r
234                 if(ops->version_data)\r
235                 {\r
236                         for(i=0; i<ops->version_len; i++)\r
237                         {\r
238                                 if(temp[i] == ops->version_data[i])\r
239                                         continue;\r
240                                 printk("%s:version %s is not %s\n",__func__,temp, ops->version_data);\r
241                                 result = -1;\r
242                         }\r
243                 }\r
244                         \r
245                 DBG("%s:%s version: %s\n",__func__,ops->name, temp);\r
246         }\r
247 \r
248         return result;\r
249 }\r
250 \r
251 \r
252 static int ts_chip_init(struct ts_private_data *ts, int type)\r
253 {\r
254         struct ts_operate *ops = NULL;\r
255         int result = 0;\r
256         int i = 0;\r
257 \r
258         if((type <= TS_BUS_TYPE_INVALID) || (type >= TS_BUS_TYPE_NUM_ID))\r
259         {\r
260                 printk("%s:type=%d is error\n",__func__,type);\r
261                 return -1;      \r
262         }\r
263         \r
264         if(ts->pdata->init_platform_hw)\r
265                 ts->pdata->init_platform_hw();\r
266         \r
267         for(i=TS_ID_INVALID+1; i<TS_NUM_ID; i++)\r
268         {\r
269                 ops = g_ts_ops[i];\r
270                 if(!ops)\r
271                 {\r
272                         printk("%s:error:%p\n",__func__,ops);\r
273                         result = -1;    \r
274                         continue;\r
275                 }\r
276                 \r
277                 if(ops->bus_type == type)\r
278                 {\r
279                 \r
280                         if(!ops->init || !ops->report)\r
281                         {\r
282                                 printk("%s:error:%p,%p\n",__func__,ops->init,ops->report);\r
283                                 result = -1;\r
284                                 continue;\r
285                         }\r
286                                 \r
287                         ts->ops = ops;  //save ops\r
288 \r
289                         result = ts_get_id(ops, ts, &ts->devid);//get id\r
290                         if(result < 0)\r
291                         {       \r
292                                 printk("%s:fail to read %s devid:0x%x\n",__func__, ops->name, ts->devid);       \r
293                                 continue;\r
294                         }\r
295                         \r
296                         result = ts_get_version(ops, ts);       //get version\r
297                         if(result < 0)\r
298                         {       \r
299                                 printk("%s:fail to read %s version\n",__func__, ops->name);     \r
300                                 continue;\r
301                         }\r
302                         \r
303                         result = ops->init(ts);\r
304                         if(result < 0)\r
305                         {\r
306                                 printk("%s:fail to init ts\n",__func__);        \r
307                                 continue;\r
308                         }\r
309                         \r
310                         if(ops->firmware)\r
311                         {\r
312                                 result = ops->firmware(ts);\r
313                                 if(result < 0)\r
314                                 {\r
315                                         printk("%s:fail to updata firmware ts\n",__func__);\r
316                                         return result;\r
317                                 }\r
318                         }\r
319                 \r
320                         printk("%s:%s devid:0x%x\n",__func__, ts->ops->name, ts->devid);\r
321                 }\r
322 \r
323                 break;\r
324                                         \r
325         }\r
326 \r
327         \r
328         return result;\r
329 \r
330 }\r
331 \r
332 static int ts_get_data(struct ts_private_data *ts)\r
333 {               \r
334         return ts->ops->report(ts);     \r
335 }\r
336 \r
337 \r
338 static void  ts_delaywork_func(struct work_struct *work)\r
339 {\r
340         struct delayed_work *delaywork = container_of(work, struct delayed_work, work);\r
341         struct ts_private_data *ts = container_of(delaywork, struct ts_private_data, delaywork);\r
342 \r
343         mutex_lock(&ts->ts_lock);       \r
344         if (ts_get_data(ts) < 0) \r
345                 DBG(KERN_ERR "%s: Get data failed\n",__func__);\r
346         \r
347         if(!ts->ops->irq_enable)//restart work while polling\r
348         schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));\r
349         else\r
350         {\r
351                 if(ts->ops->check_irq)\r
352                 {\r
353                         ts->ops->check_irq(ts);         \r
354                 }\r
355                 else\r
356                 {\r
357                         if((ts->ops->trig & IRQF_TRIGGER_LOW) || (ts->ops->trig & IRQF_TRIGGER_HIGH))\r
358                         enable_irq(ts->irq);\r
359                 }\r
360         }\r
361         mutex_unlock(&ts->ts_lock);\r
362         \r
363         DBG("%s:%s\n",__func__,ts->i2c_id->name);\r
364 }\r
365 \r
366 /*\r
367  * This is a threaded IRQ handler so can access I2C/SPI.  Since all\r
368  * interrupts are clear on read the IRQ line will be reasserted and\r
369  * the physical IRQ will be handled again if another interrupt is\r
370  * asserted while we run - in the normal course of events this is a\r
371  * rare occurrence so we save I2C/SPI reads.  We're also assuming that\r
372  * it's rare to get lots of interrupts firing simultaneously so try to\r
373  * minimise I/O.\r
374  */\r
375 static irqreturn_t ts_interrupt(int irq, void *dev_id)\r
376 {\r
377         struct ts_private_data *ts = (struct ts_private_data *)dev_id;\r
378 \r
379         //use threaded IRQ\r
380         //if (ts_get_data(ts->client) < 0) \r
381         //      DBG(KERN_ERR "%s: Get data failed\n",__func__);\r
382         //msleep(ts->ops->poll_delay_ms);\r
383         if(ts->ops->check_irq)\r
384         {\r
385                 disable_irq_nosync(irq);\r
386         }\r
387         else\r
388         {\r
389                 if((ts->ops->trig & IRQF_TRIGGER_LOW) || (ts->ops->trig & IRQF_TRIGGER_HIGH))\r
390                 disable_irq_nosync(irq);\r
391         }\r
392         schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));\r
393         DBG("%s:irq=%d\n",__func__,irq);\r
394         return IRQ_HANDLED;\r
395 }\r
396 \r
397 \r
398 static int ts_irq_init(struct ts_private_data *ts)\r
399 {\r
400         int result = 0;\r
401         int irq;\r
402         if((ts->ops->irq_enable)&&(ts->ops->trig != TS_UNKNOW_DATA))\r
403         {\r
404                 INIT_DELAYED_WORK(&ts->delaywork, ts_delaywork_func);\r
405                 if(ts->ops->poll_delay_ms < 0)\r
406                         ts->ops->poll_delay_ms = 30;\r
407                 \r
408                 //result = gpio_request(ts->irq, ts->i2c_id->name);\r
409                 //if (result)\r
410                 //{\r
411                 //      printk("%s:fail to request gpio :%d\n",__func__,ts->irq);\r
412                 //}\r
413         \r
414                 gpio_pull_updown(ts->irq, PullEnable);\r
415                 irq = gpio_to_irq(ts->irq);\r
416                 result = request_irq(irq, ts_interrupt, ts->ops->trig, ts->ops->name, ts);\r
417                 //result = request_threaded_irq(irq, NULL, ts_interrupt, ts->ops->trig, ts->ops->name, ts);\r
418                 if (result) {\r
419                         printk(KERN_ERR "%s:fail to request irq = %d, ret = 0x%x\n",__func__, irq, result);            \r
420                         goto error;            \r
421                 }\r
422                 ts->irq = irq;\r
423                 printk("%s:use irq=%d\n",__func__,irq);\r
424         }\r
425         else if(!ts->ops->irq_enable)\r
426         {               \r
427                 INIT_DELAYED_WORK(&ts->delaywork, ts_delaywork_func);\r
428                 if(ts->ops->poll_delay_ms < 0)\r
429                         ts->ops->poll_delay_ms = 30;\r
430                 \r
431                 schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));\r
432                 printk("%s:use polling,delay=%d ms\n",__func__,ts->ops->poll_delay_ms);\r
433         }\r
434 \r
435 error:  \r
436         return result;\r
437 }\r
438 \r
439 \r
440 \r
441 int ts_device_init(struct ts_private_data *ts, int type, int irq)\r
442 {\r
443         struct ts_platform_data *pdata = ts->dev->platform_data;\r
444         int result = -1, i;\r
445 \r
446         mutex_init(&ts->io_lock);\r
447         mutex_init(&ts->ts_lock);\r
448         dev_set_drvdata(ts->dev, ts);\r
449         \r
450         ts->pdata = pdata;\r
451         result = ts_chip_init(ts, type);\r
452         if(result < 0)\r
453         {\r
454                 printk("%s:touch screen with bus type %d is not exist\n",__func__,type);\r
455                 goto out_free_memory;\r
456         }\r
457 \r
458         ts->input_dev = input_allocate_device();\r
459         if (!ts->input_dev) {\r
460                 result = -ENOMEM;\r
461                 dev_err(ts->dev,\r
462                         "Failed to allocate input device %s\n", ts->input_dev->name);\r
463                 goto out_free_memory;\r
464         }       \r
465         \r
466         ts->input_dev->dev.parent = ts->dev;\r
467         ts->input_dev->name = ts->ops->name;\r
468         \r
469         result = input_register_device(ts->input_dev);\r
470         if (result) {\r
471                 dev_err(ts->dev,\r
472                         "Unable to register input device %s\n", ts->input_dev->name);\r
473                 goto out_input_register_device_failed;\r
474         }\r
475         \r
476         result = ts_irq_init(ts);\r
477         if (result) {\r
478                 dev_err(ts->dev,\r
479                         "fail to init ts irq,ret=%d\n",result);\r
480                 goto out_input_register_device_failed;\r
481         }\r
482         \r
483         __set_bit(EV_ABS, ts->input_dev->evbit);\r
484         __set_bit(EV_KEY, ts->input_dev->evbit);\r
485         __set_bit(EV_REP,  ts->input_dev->evbit);\r
486         __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);\r
487         set_bit(ABS_MT_POSITION_X, ts->input_dev->absbit);\r
488         set_bit(ABS_MT_POSITION_Y, ts->input_dev->absbit);\r
489         set_bit(ABS_MT_TOUCH_MAJOR, ts->input_dev->absbit);\r
490         set_bit(ABS_MT_WIDTH_MAJOR, ts->input_dev->absbit);\r
491 \r
492         if(ts->ops->max_point <= 0)\r
493                 ts->ops->max_point = 1;\r
494         \r
495         input_mt_init_slots(ts->input_dev, ts->ops->max_point);\r
496
497         if((ts->ops->range[0] <= 0) || (ts->ops->range[1] <= 0))\r
498         {
499                 ts->ops->range[0] = 1024;\r
500                 ts->ops->range[1] = 600;\r
501         }\r
502         \r
503         input_set_abs_params(ts->input_dev,ABS_MT_POSITION_X, 0, ts->ops->range[0], 0, 0);\r
504         input_set_abs_params(ts->input_dev,ABS_MT_POSITION_Y, 0, ts->ops->range[1], 0, 0);\r
505         input_set_abs_params(ts->input_dev,ABS_MT_TOUCH_MAJOR, 0, 10, 0, 0);\r
506         input_set_abs_params(ts->input_dev,ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0);\r
507         \r
508         g_ts = ts;\r
509         \r
510         printk("%s:initialized ok,ts name:%s,devid=%d\n\n",__func__,ts->ops->name,ts->devid);\r
511 \r
512         return result;\r
513         \r
514 out_misc_device_register_device_failed:\r
515         input_unregister_device(ts->input_dev); \r
516 out_input_register_device_failed:\r
517         input_free_device(ts->input_dev);       \r
518 out_free_memory:        \r
519         kfree(ts);\r
520         \r
521         printk("%s:line=%d\n",__func__,__LINE__);\r
522         return result;\r
523 }\r
524 \r
525 \r
526 void ts_device_exit(struct ts_private_data *ts)\r
527 {\r
528         if(!ts->ops->irq_enable)        \r
529         cancel_delayed_work_sync(&ts->delaywork);\r
530         input_unregister_device(ts->input_dev); \r
531         input_free_device(ts->input_dev);       \r
532 #ifdef CONFIG_HAS_EARLYSUSPEND\r
533         if((ts->ops->suspend) && (ts->ops->resume))\r
534                 unregister_early_suspend(&ts->early_suspend);\r
535 #endif  \r
536         kfree(ts);      \r
537 }\r
538 \r
539 \r
540 int ts_device_suspend(struct ts_private_data *ts)\r
541 {\r
542         if(ts->ops->suspend)\r
543                 ts->ops->suspend(ts);\r
544 \r
545         if(ts->ops->irq_enable) \r
546                 disable_irq_nosync(ts->irq);\r
547         else\r
548                 cancel_delayed_work_sync(&ts->delaywork);\r
549 \r
550         return 0;\r
551 }\r
552 \r
553 \r
554 int ts_device_resume(struct ts_private_data *ts)\r
555 {\r
556         if(ts->ops->resume)\r
557                 ts->ops->resume(ts);\r
558 \r
559         if(ts->ops->irq_enable) \r
560                 enable_irq(ts->irq);\r
561         else\r
562         {\r
563                 PREPARE_DELAYED_WORK(&ts->delaywork, ts_delaywork_func);\r
564                 schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));\r
565         }\r
566         \r
567         return 0;\r
568 }\r
569 \r
570 \r
571 \r
572 int ts_register_slave(struct ts_private_data *ts,\r
573                         struct ts_platform_data *slave_pdata,\r
574                         struct ts_operate *(*get_ts_ops)(void))\r
575 {\r
576         int result = 0;\r
577         struct ts_operate *ops = get_ts_ops();\r
578         if((ops->ts_id >= TS_NUM_ID) || (ops->ts_id <= TS_ID_INVALID))\r
579         {       \r
580                 printk("%s:%s id is error %d\n", __func__, ops->name, ops->ts_id);\r
581                 return -1;      \r
582         }\r
583         g_ts_ops[ops->ts_id] = ops;\r
584         printk("%s:%s,id=%d\n",__func__,g_ts_ops[ops->ts_id]->name, ops->ts_id);\r
585         return result;\r
586 }\r
587 \r
588 \r
589 int ts_unregister_slave(struct ts_private_data *ts,\r
590                         struct ts_platform_data *slave_pdata,\r
591                         struct ts_operate *(*get_ts_ops)(void))\r
592 {\r
593         int result = 0;\r
594         struct ts_operate *ops = get_ts_ops();\r
595         if((ops->ts_id >= TS_NUM_ID) || (ops->ts_id <= TS_ID_INVALID))\r
596         {       \r
597                 printk("%s:%s id is error %d\n", __func__, ops->name, ops->ts_id);\r
598                 return -1;      \r
599         }\r
600         printk("%s:%s,id=%d\n",__func__,g_ts_ops[ops->ts_id]->name, ops->ts_id);\r
601         g_ts_ops[ops->ts_id] = NULL;    \r
602         return result;\r
603 }\r
604 \r
605 \r
606 MODULE_AUTHOR("ROCKCHIP Corporation:lw@rock-chips.com");\r
607 MODULE_DESCRIPTION("device interface for auto touch screen");\r
608 MODULE_LICENSE("GPL");\r
609 \r
610 \r