input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / keyboard / rk_keys.c
1 /*
2  * Driver for keys on GPIO lines capable of generating interrupts.
3  *
4  * Copyright (C) 2015, Fuzhou Rockchip Electronics Co., Ltd
5  * Copyright 2005 Phil Blundell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
18 #include <linux/pm.h>
19 #include <linux/sysctl.h>
20 #include <linux/proc_fs.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/input.h>
24 #include <linux/adc.h>
25 #include <linux/slab.h>
26 #include <linux/wakelock.h>
27
28 #include <linux/iio/iio.h>
29 #include <linux/iio/machine.h>
30 #include <linux/iio/driver.h>
31 #include <linux/iio/consumer.h>
32
33 #include <linux/gpio.h>
34 #include <linux/of.h>
35 #include <linux/of_irq.h>
36 #include <linux/of_gpio.h>
37 #include <linux/of_platform.h>
38 #include <linux/rk_keys.h>
39
40 #define EMPTY_DEFAULT_ADVALUE           1024
41 #define DRIFT_DEFAULT_ADVALUE           70
42 #define INVALID_ADVALUE                 -1
43 #define EV_ENCALL                       KEY_F4
44 #define EV_MENU                         KEY_F1
45
46 #if 0
47 #define key_dbg(bdata, format, arg...)          \
48         dev_info(&bdata->input->dev, format, ##arg)
49 #else
50 #define key_dbg(bdata, format, arg...)
51 #endif
52
53 #define DEBOUNCE_JIFFIES        (10 / (MSEC_PER_SEC / HZ))      /* 10ms */
54 #define ADC_SAMPLE_JIFFIES      (100 / (MSEC_PER_SEC / HZ))     /* 100ms */
55 #define WAKE_LOCK_JIFFIES       (1 * HZ)                        /* 1s */
56
57 enum rk_key_type {
58         TYPE_GPIO = 1,
59         TYPE_ADC
60 };
61
62 struct rk_keys_button {
63         struct device *dev;
64         u32 type;               /* TYPE_GPIO, TYPE_ADC */
65         u32 code;               /* key code */
66         const char *desc;       /* key label */
67         u32 state;              /* key up & down state */
68         int gpio;               /* gpio only */
69         int adc_value;          /* adc only */
70         int adc_state;          /* adc only */
71         int active_low;         /* gpio only */
72         int wakeup;             /* gpio only */
73         struct timer_list timer;
74 };
75
76 struct rk_keys_drvdata {
77         int nbuttons;
78         /* flag to indicate if we're suspending/resuming */
79         bool in_suspend;
80         int result;
81         int rep;
82         int drift_advalue;
83         struct wake_lock wake_lock;
84         struct input_dev *input;
85         struct delayed_work adc_poll_work;
86         struct iio_channel *chan;
87         struct rk_keys_button button[0];
88 };
89
90 static struct input_dev *sinput_dev;
91
92 void rk_send_power_key(int state)
93 {
94         if (!sinput_dev)
95                 return;
96         if (state) {
97                 input_report_key(sinput_dev, KEY_POWER, 1);
98                 input_sync(sinput_dev);
99         } else {
100                 input_report_key(sinput_dev, KEY_POWER, 0);
101                 input_sync(sinput_dev);
102         }
103 }
104 EXPORT_SYMBOL(rk_send_power_key);
105
106 void rk_send_wakeup_key(void)
107 {
108         if (!sinput_dev)
109                 return;
110
111         input_report_key(sinput_dev, KEY_WAKEUP, 1);
112         input_sync(sinput_dev);
113         input_report_key(sinput_dev, KEY_WAKEUP, 0);
114         input_sync(sinput_dev);
115 }
116 EXPORT_SYMBOL(rk_send_wakeup_key);
117
118 static void keys_timer(unsigned long _data)
119 {
120         struct rk_keys_button *button = (struct rk_keys_button *)_data;
121         struct rk_keys_drvdata *pdata = dev_get_drvdata(button->dev);
122         struct input_dev *input = pdata->input;
123         int state;
124
125         if (button->type == TYPE_GPIO)
126                 state = !!((gpio_get_value(button->gpio) ? 1 : 0) ^
127                            button->active_low);
128         else
129                 state = !!button->adc_state;
130
131         if (button->state != state) {
132                 button->state = state;
133                 input_event(input, EV_KEY, button->code, button->state);
134                 key_dbg(pdata, "%skey[%s]: report event[%d] state[%d]\n",
135                         button->type == TYPE_ADC ? "adc" : "gpio",
136                         button->desc, button->code, button->state);
137                 input_event(input, EV_KEY, button->code, button->state);
138                 input_sync(input);
139         }
140
141         if (state)
142                 mod_timer(&button->timer, jiffies + DEBOUNCE_JIFFIES);
143 }
144
145 static irqreturn_t keys_isr(int irq, void *dev_id)
146 {
147         struct rk_keys_button *button = (struct rk_keys_button *)dev_id;
148         struct rk_keys_drvdata *pdata = dev_get_drvdata(button->dev);
149         struct input_dev *input = pdata->input;
150
151         BUG_ON(irq != gpio_to_irq(button->gpio));
152
153         if (button->wakeup && pdata->in_suspend) {
154                 button->state = 1;
155                 key_dbg(pdata,
156                         "wakeup: %skey[%s]: report event[%d] state[%d]\n",
157                         (button->type == TYPE_ADC) ? "adc" : "gpio",
158                         button->desc, button->code, button->state);
159                 input_event(input, EV_KEY, button->code, button->state);
160                 input_sync(input);
161         }
162         if (button->wakeup)
163                 wake_lock_timeout(&pdata->wake_lock, WAKE_LOCK_JIFFIES);
164         mod_timer(&button->timer, jiffies + DEBOUNCE_JIFFIES);
165
166         return IRQ_HANDLED;
167 }
168
169 /*
170 static ssize_t adc_value_show(struct device *dev, struct device_attribute *attr,
171                               char *buf)
172 {
173         struct rk_keys_drvdata *ddata = dev_get_drvdata(dev);
174
175         return sprintf(buf, "adc_value: %d\n", ddata->result);
176 }
177 static DEVICE_ATTR(get_adc_value, S_IRUGO | S_IWUSR, adc_value_show, NULL);
178 */
179
180 static const struct of_device_id rk_key_match[] = {
181         { .compatible = "rockchip,key", .data = NULL},
182         {},
183 };
184 MODULE_DEVICE_TABLE(of, rk_key_match);
185
186 static int rk_key_adc_iio_read(struct rk_keys_drvdata *data)
187 {
188         struct iio_channel *channel = data->chan;
189         int val, ret;
190
191         if (!channel)
192                 return INVALID_ADVALUE;
193         ret = iio_read_channel_raw(channel, &val);
194         if (ret < 0) {
195                 pr_err("read channel() error: %d\n", ret);
196                 return ret;
197         }
198         return val;
199 }
200
201 static void adc_key_poll(struct work_struct *work)
202 {
203         struct rk_keys_drvdata *ddata;
204         int i, result = -1;
205
206         ddata = container_of(work, struct rk_keys_drvdata, adc_poll_work.work);
207         if (!ddata->in_suspend) {
208                 result = rk_key_adc_iio_read(ddata);
209                 if (result > INVALID_ADVALUE &&
210                     result < (EMPTY_DEFAULT_ADVALUE - ddata->drift_advalue))
211                         ddata->result = result;
212                 for (i = 0; i < ddata->nbuttons; i++) {
213                         struct rk_keys_button *button = &ddata->button[i];
214
215                         if (!button->adc_value)
216                                 continue;
217                         if (result < button->adc_value + ddata->drift_advalue &&
218                             result > button->adc_value - ddata->drift_advalue)
219                                 button->adc_state = 1;
220                         else
221                                 button->adc_state = 0;
222                         if (button->state != button->adc_state)
223                                 mod_timer(&button->timer,
224                                           jiffies + DEBOUNCE_JIFFIES);
225                 }
226         }
227
228         schedule_delayed_work(&ddata->adc_poll_work, ADC_SAMPLE_JIFFIES);
229 }
230
231 static int rk_key_type_get(struct device_node *node,
232                            struct rk_keys_button *button)
233 {
234         u32 adc_value;
235
236         if (!of_property_read_u32(node, "rockchip,adc_value", &adc_value))
237                 return TYPE_ADC;
238         else if (of_get_gpio(node, 0) >= 0)
239                 return TYPE_GPIO;
240         else
241                 return -1;
242 }
243
244 static int rk_keys_parse_dt(struct rk_keys_drvdata *pdata,
245                             struct platform_device *pdev)
246 {
247         struct device_node *node = pdev->dev.of_node;
248         struct device_node *child_node;
249         struct iio_channel *chan;
250         int ret, gpio, i = 0;
251         u32 code, adc_value, flags, drift;
252
253         if (of_property_read_u32(node, "adc-drift", &drift))
254                 pdata->drift_advalue = DRIFT_DEFAULT_ADVALUE;
255         else
256                 pdata->drift_advalue = (int)drift;
257
258         chan = iio_channel_get(&pdev->dev, NULL);
259         if (IS_ERR(chan)) {
260                 dev_info(&pdev->dev, "no io-channels defined\n");
261                 chan = NULL;
262         }
263         pdata->chan = chan;
264
265         for_each_child_of_node(node, child_node) {
266                 if (of_property_read_u32(child_node, "linux,code", &code)) {
267                         dev_err(&pdev->dev,
268                                 "Missing linux,code property in the DT.\n");
269                         ret = -EINVAL;
270                         goto error_ret;
271                 }
272                 pdata->button[i].code = code;
273                 pdata->button[i].desc =
274                     of_get_property(child_node, "label", NULL);
275                 pdata->button[i].type =
276                     rk_key_type_get(child_node, &pdata->button[i]);
277                 switch (pdata->button[i].type) {
278                 case TYPE_GPIO:
279                         gpio = of_get_gpio_flags(child_node, 0, &flags);
280                         if (gpio < 0) {
281                                 ret = gpio;
282                                 if (ret != -EPROBE_DEFER)
283                                         dev_err(&pdev->dev,
284                                                 "Failed to get gpio flags, error: %d\n",
285                                                 ret);
286                                 goto error_ret;
287                         }
288
289                         pdata->button[i].gpio = gpio;
290                         pdata->button[i].active_low =
291                             flags & OF_GPIO_ACTIVE_LOW;
292                         pdata->button[i].wakeup =
293                             !!of_get_property(child_node, "gpio-key,wakeup",
294                                               NULL);
295                         break;
296
297                 case TYPE_ADC:
298                         if (of_property_read_u32
299                             (child_node, "rockchip,adc_value", &adc_value)) {
300                                 dev_err(&pdev->dev,
301                                         "Missing rockchip,adc_value property in the DT.\n");
302                                 ret = -EINVAL;
303                                 goto error_ret;
304                         }
305                         pdata->button[i].adc_value = adc_value;
306                         break;
307
308                 default:
309                         dev_err(&pdev->dev,
310                                 "Error rockchip,type property in the DT.\n");
311                         ret = -EINVAL;
312                         goto error_ret;
313                 }
314                 i++;
315         }
316
317         return 0;
318
319 error_ret:
320         return ret;
321 }
322
323 static int keys_probe(struct platform_device *pdev)
324 {
325         struct device *dev = &pdev->dev;
326         struct device_node *np = pdev->dev.of_node;
327         struct rk_keys_drvdata *ddata = NULL;
328         struct input_dev *input = NULL;
329         int i, error = 0;
330         int wakeup, key_num = 0;
331
332         key_num = of_get_child_count(np);
333         if (key_num == 0)
334                 dev_info(&pdev->dev, "no key defined\n");
335
336         ddata = devm_kzalloc(dev, sizeof(struct rk_keys_drvdata) +
337                              key_num * sizeof(struct rk_keys_button),
338                              GFP_KERNEL);
339
340         input = devm_input_allocate_device(dev);
341         if (!ddata || !input) {
342                 error = -ENOMEM;
343                 return error;
344         }
345         platform_set_drvdata(pdev, ddata);
346         dev_set_drvdata(&pdev->dev, ddata);
347
348         input->name = "rk29-keypad";    /* pdev->name; */
349         input->phys = "gpio-keys/input0";
350         input->dev.parent = dev;
351
352         input->id.bustype = BUS_HOST;
353         input->id.vendor = 0x0001;
354         input->id.product = 0x0001;
355         input->id.version = 0x0100;
356         ddata->input = input;
357
358         /* parse info from dt */
359         ddata->nbuttons = key_num;
360         error = rk_keys_parse_dt(ddata, pdev);
361         if (error)
362                 goto fail0;
363
364         /* Enable auto repeat feature of Linux input subsystem */
365         if (ddata->rep)
366                 __set_bit(EV_REP, input->evbit);
367
368         error = input_register_device(input);
369         if (error) {
370                 pr_err("gpio-keys: Unable to register input device, error: %d\n",
371                        error);
372                 goto fail0;
373         }
374         sinput_dev = input;
375
376         for (i = 0; i < ddata->nbuttons; i++) {
377                 struct rk_keys_button *button = &ddata->button[i];
378
379                 if (button->code) {
380                         setup_timer(&button->timer,
381                                     keys_timer, (unsigned long)button);
382                 }
383
384                 if (button->wakeup)
385                         wakeup = 1;
386
387                 input_set_capability(input, EV_KEY, button->code);
388         }
389
390         wake_lock_init(&ddata->wake_lock, WAKE_LOCK_SUSPEND, input->name);
391         device_init_wakeup(dev, wakeup);
392
393         for (i = 0; i < ddata->nbuttons; i++) {
394                 struct rk_keys_button *button = &ddata->button[i];
395
396                 button->dev = &pdev->dev;
397                 if (button->type == TYPE_GPIO) {
398                         int irq;
399
400                         error =
401                             devm_gpio_request(dev, button->gpio,
402                                               button->desc ? : "keys");
403                         if (error < 0) {
404                                 pr_err("gpio-keys: failed to request GPIO %d, error %d\n",
405                                        button->gpio, error);
406                                 goto fail1;
407                         }
408
409                         error = gpio_direction_input(button->gpio);
410                         if (error < 0) {
411                                 pr_err("gpio-keys: failed to configure input direction for GPIO %d, error %d\n",
412                                        button->gpio, error);
413                                 gpio_free(button->gpio);
414                                 goto fail1;
415                         }
416
417                         irq = gpio_to_irq(button->gpio);
418                         if (irq < 0) {
419                                 error = irq;
420                                 pr_err("gpio-keys: Unable to get irq number for GPIO %d, error %d\n",
421                                        button->gpio, error);
422                                 gpio_free(button->gpio);
423                                 goto fail1;
424                         }
425
426                         error = devm_request_irq(dev, irq, keys_isr,
427                                                  button->active_low ?
428                                                  IRQF_TRIGGER_FALLING :
429                                                  IRQF_TRIGGER_RISING,
430                                                  button->desc ?
431                                                  button->desc : "keys",
432                                                  button);
433                         if (error) {
434                                 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
435                                        irq, error);
436                                 gpio_free(button->gpio);
437                                 goto fail1;
438                         }
439                 }
440         }
441
442         input_set_capability(input, EV_KEY, KEY_WAKEUP);
443         /* adc polling work */
444         if (ddata->chan) {
445                 INIT_DELAYED_WORK(&ddata->adc_poll_work, adc_key_poll);
446                 schedule_delayed_work(&ddata->adc_poll_work,
447                                       ADC_SAMPLE_JIFFIES);
448         }
449
450         return error;
451
452 fail1:
453         while (--i >= 0)
454                 del_timer_sync(&ddata->button[i].timer);
455         device_init_wakeup(dev, 0);
456         wake_lock_destroy(&ddata->wake_lock);
457 fail0:
458         platform_set_drvdata(pdev, NULL);
459
460         return error;
461 }
462
463 static int keys_remove(struct platform_device *pdev)
464 {
465         struct device *dev = &pdev->dev;
466         struct rk_keys_drvdata *ddata = dev_get_drvdata(dev);
467         struct input_dev *input = ddata->input;
468         int i;
469
470         device_init_wakeup(dev, 0);
471         for (i = 0; i < ddata->nbuttons; i++)
472                 del_timer_sync(&ddata->button[i].timer);
473         if (ddata->chan)
474                 cancel_delayed_work_sync(&ddata->adc_poll_work);
475         input_unregister_device(input);
476         wake_lock_destroy(&ddata->wake_lock);
477
478         sinput_dev = NULL;
479
480         return 0;
481 }
482
483 #ifdef CONFIG_PM
484 static int keys_suspend(struct device *dev)
485 {
486         struct rk_keys_drvdata *ddata = dev_get_drvdata(dev);
487         int i;
488
489         ddata->in_suspend = true;
490         if (device_may_wakeup(dev)) {
491                 for (i = 0; i < ddata->nbuttons; i++) {
492                         struct rk_keys_button *button = ddata->button + i;
493
494                         if (button->wakeup)
495                                 enable_irq_wake(gpio_to_irq(button->gpio));
496                 }
497         }
498
499         return 0;
500 }
501
502 static int keys_resume(struct device *dev)
503 {
504         struct rk_keys_drvdata *ddata = dev_get_drvdata(dev);
505         int i;
506
507         if (device_may_wakeup(dev)) {
508                 for (i = 0; i < ddata->nbuttons; i++) {
509                         struct rk_keys_button *button = ddata->button + i;
510
511                         if (button->wakeup)
512                                 disable_irq_wake(gpio_to_irq(button->gpio));
513                 }
514                 preempt_disable();
515                 /* for call resend_irqs, which may call keys_isr */
516                 if (local_softirq_pending())
517                         do_softirq();
518                 preempt_enable_no_resched();
519         }
520
521         ddata->in_suspend = false;
522
523         return 0;
524 }
525
526 static const struct dev_pm_ops keys_pm_ops = {
527         .suspend        = keys_suspend,
528         .resume         = keys_resume,
529 };
530 #endif
531
532 static struct platform_driver keys_device_driver = {
533         .probe          = keys_probe,
534         .remove         = keys_remove,
535         .driver         = {
536                 .name   = "rk-keypad",
537                 .owner  = THIS_MODULE,
538                 .of_match_table = rk_key_match,
539 #ifdef CONFIG_PM
540                 .pm     = &keys_pm_ops,
541 #endif
542         }
543 };
544
545 static int __init rk_keys_driver_init(void)
546 {
547         return platform_driver_register(&keys_device_driver);
548 }
549
550 static void __exit rk_keys_driver_exit(void)
551 {
552         platform_driver_unregister(&keys_device_driver);
553 }
554
555 late_initcall_sync(rk_keys_driver_init);
556 module_exit(rk_keys_driver_exit);