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