gpio-button-hotplug: add volume button handling
[openwrt/openwrt.git] / package / kernel / gpio-button-hotplug / src / gpio-button-hotplug.c
1 /*
2 * GPIO Button Hotplug driver
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@nbd.name>
5 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
6 *
7 * Based on the diag.c - GPIO interface driver for Broadcom boards
8 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
9 * Copyright (C) 2006-2007 Felix Fietkau <nbd@nbd.name>
10 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 */
16
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/kmod.h>
20
21 #include <linux/workqueue.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/kobject.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/of_irq.h>
30 #include <linux/gpio_keys.h>
31 #include <linux/gpio/consumer.h>
32
33 #define BH_SKB_SIZE 2048
34
35 #define DRV_NAME "gpio-keys"
36 #define PFX DRV_NAME ": "
37
38 struct bh_event {
39 const char *name;
40 unsigned int type;
41 char *action;
42 unsigned long seen;
43
44 struct sk_buff *skb;
45 struct work_struct work;
46 };
47
48 struct bh_map {
49 unsigned int code;
50 const char *name;
51 };
52
53 struct gpio_keys_button_data {
54 struct delayed_work work;
55 unsigned long seen;
56 int map_entry;
57 int last_state;
58 int count;
59 int threshold;
60 int can_sleep;
61 int irq;
62 unsigned int software_debounce;
63 struct gpio_desc *gpiod;
64 const struct gpio_keys_button *b;
65 };
66
67 extern u64 uevent_next_seqnum(void);
68
69 #define BH_MAP(_code, _name) \
70 { \
71 .code = (_code), \
72 .name = (_name), \
73 }
74
75 static struct bh_map button_map[] = {
76 BH_MAP(BTN_0, "BTN_0"),
77 BH_MAP(BTN_1, "BTN_1"),
78 BH_MAP(BTN_2, "BTN_2"),
79 BH_MAP(BTN_3, "BTN_3"),
80 BH_MAP(BTN_4, "BTN_4"),
81 BH_MAP(BTN_5, "BTN_5"),
82 BH_MAP(BTN_6, "BTN_6"),
83 BH_MAP(BTN_7, "BTN_7"),
84 BH_MAP(BTN_8, "BTN_8"),
85 BH_MAP(BTN_9, "BTN_9"),
86 BH_MAP(KEY_BRIGHTNESS_ZERO, "brightness_zero"),
87 BH_MAP(KEY_CONFIG, "config"),
88 BH_MAP(KEY_COPY, "copy"),
89 BH_MAP(KEY_EJECTCD, "eject"),
90 BH_MAP(KEY_HELP, "help"),
91 BH_MAP(KEY_LIGHTS_TOGGLE, "lights_toggle"),
92 BH_MAP(KEY_PHONE, "phone"),
93 BH_MAP(KEY_POWER, "power"),
94 BH_MAP(KEY_POWER2, "reboot"),
95 BH_MAP(KEY_RESTART, "reset"),
96 BH_MAP(KEY_RFKILL, "rfkill"),
97 BH_MAP(KEY_VIDEO, "video"),
98 BH_MAP(KEY_VOLUMEDOWN, "volume_down"),
99 BH_MAP(KEY_VOLUMEUP, "volume_up"),
100 BH_MAP(KEY_WIMAX, "wwan"),
101 BH_MAP(KEY_WLAN, "wlan"),
102 BH_MAP(KEY_WPS_BUTTON, "wps"),
103 };
104
105 /* -------------------------------------------------------------------------*/
106
107 static __printf(3, 4)
108 int bh_event_add_var(struct bh_event *event, int argv, const char *format, ...)
109 {
110 static char buf[128];
111 char *s;
112 va_list args;
113 int len;
114
115 if (argv)
116 return 0;
117
118 va_start(args, format);
119 len = vsnprintf(buf, sizeof(buf), format, args);
120 va_end(args);
121
122 if (len >= sizeof(buf)) {
123 WARN(1, "buffer size too small");
124 return -ENOMEM;
125 }
126
127 s = skb_put(event->skb, len + 1);
128 strcpy(s, buf);
129
130 pr_debug(PFX "added variable '%s'\n", s);
131
132 return 0;
133 }
134
135 static int button_hotplug_fill_event(struct bh_event *event)
136 {
137 int ret;
138
139 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
140 if (ret)
141 return ret;
142
143 ret = bh_event_add_var(event, 0, "PATH=%s",
144 "/sbin:/bin:/usr/sbin:/usr/bin");
145 if (ret)
146 return ret;
147
148 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
149 if (ret)
150 return ret;
151
152 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
153 if (ret)
154 return ret;
155
156 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
157 if (ret)
158 return ret;
159
160 if (event->type == EV_SW) {
161 ret = bh_event_add_var(event, 0, "TYPE=%s", "switch");
162 if (ret)
163 return ret;
164 }
165
166 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
167 if (ret)
168 return ret;
169
170 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
171
172 return ret;
173 }
174
175 static void button_hotplug_work(struct work_struct *work)
176 {
177 struct bh_event *event = container_of(work, struct bh_event, work);
178 int ret = 0;
179
180 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
181 if (!event->skb)
182 goto out_free_event;
183
184 ret = bh_event_add_var(event, 0, "%s@", event->action);
185 if (ret)
186 goto out_free_skb;
187
188 ret = button_hotplug_fill_event(event);
189 if (ret)
190 goto out_free_skb;
191
192 NETLINK_CB(event->skb).dst_group = 1;
193 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
194
195 out_free_skb:
196 if (ret) {
197 pr_err(PFX "work error %d\n", ret);
198 kfree_skb(event->skb);
199 }
200 out_free_event:
201 kfree(event);
202 }
203
204 static int button_hotplug_create_event(const char *name, unsigned int type,
205 unsigned long seen, int pressed)
206 {
207 struct bh_event *event;
208
209 pr_debug(PFX "create event, name=%s, seen=%lu, pressed=%d\n",
210 name, seen, pressed);
211
212 event = kzalloc(sizeof(*event), GFP_KERNEL);
213 if (!event)
214 return -ENOMEM;
215
216 event->name = name;
217 event->type = type;
218 event->seen = seen;
219 event->action = pressed ? "pressed" : "released";
220
221 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
222 schedule_work(&event->work);
223
224 return 0;
225 }
226
227 /* -------------------------------------------------------------------------*/
228
229 static int button_get_index(unsigned int code)
230 {
231 int i;
232
233 for (i = 0; i < ARRAY_SIZE(button_map); i++)
234 if (button_map[i].code == code)
235 return i;
236
237 return -1;
238 }
239
240 static int gpio_button_get_value(struct gpio_keys_button_data *bdata)
241 {
242 int val;
243
244 if (bdata->can_sleep)
245 val = !!gpio_get_value_cansleep(bdata->b->gpio);
246 else
247 val = !!gpio_get_value(bdata->b->gpio);
248
249 return val ^ bdata->b->active_low;
250 }
251
252 static void gpio_keys_handle_button(struct gpio_keys_button_data *bdata)
253 {
254 unsigned int type = bdata->b->type ?: EV_KEY;
255 int state = gpio_button_get_value(bdata);
256 unsigned long seen = jiffies;
257
258 pr_debug(PFX "event type=%u, code=%u, pressed=%d\n",
259 type, bdata->b->code, state);
260
261 /* is this the initialization state? */
262 if (bdata->last_state == -1) {
263 /*
264 * Don't advertise unpressed buttons on initialization.
265 * Just save their state and continue otherwise this
266 * can cause OpenWrt to enter failsafe.
267 */
268 if (type == EV_KEY && state == 0)
269 goto set_state;
270 /*
271 * But we are very interested in pressed buttons and
272 * initial switch state. These will be reported to
273 * userland.
274 */
275 } else if (bdata->last_state == state) {
276 /* reset asserted counter (only relevant for polled keys) */
277 bdata->count = 0;
278 return;
279 }
280
281 if (bdata->count < bdata->threshold) {
282 bdata->count++;
283 return;
284 }
285
286 if (bdata->seen == 0)
287 bdata->seen = seen;
288
289 button_hotplug_create_event(button_map[bdata->map_entry].name, type,
290 (seen - bdata->seen) / HZ, state);
291 bdata->seen = seen;
292
293 set_state:
294 bdata->last_state = state;
295 bdata->count = 0;
296 }
297
298 struct gpio_keys_button_dev {
299 int polled;
300 struct delayed_work work;
301
302 struct device *dev;
303 struct gpio_keys_platform_data *pdata;
304 struct gpio_keys_button_data data[0];
305 };
306
307 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev *bdev)
308 {
309 struct gpio_keys_platform_data *pdata = bdev->pdata;
310 unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
311
312 if (delay >= HZ)
313 delay = round_jiffies_relative(delay);
314 schedule_delayed_work(&bdev->work, delay);
315 }
316
317 static void gpio_keys_polled_poll(struct work_struct *work)
318 {
319 struct gpio_keys_button_dev *bdev =
320 container_of(work, struct gpio_keys_button_dev, work.work);
321 int i;
322
323 for (i = 0; i < bdev->pdata->nbuttons; i++) {
324 struct gpio_keys_button_data *bdata = &bdev->data[i];
325
326 if (bdata->gpiod)
327 gpio_keys_handle_button(bdata);
328 }
329 gpio_keys_polled_queue_work(bdev);
330 }
331
332 static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev)
333 {
334 struct gpio_keys_platform_data *pdata = bdev->pdata;
335
336 cancel_delayed_work_sync(&bdev->work);
337
338 if (pdata->disable)
339 pdata->disable(bdev->dev);
340 }
341
342 static void gpio_keys_irq_work_func(struct work_struct *work)
343 {
344 struct gpio_keys_button_data *bdata = container_of(work,
345 struct gpio_keys_button_data, work.work);
346
347 gpio_keys_handle_button(bdata);
348 }
349
350 static irqreturn_t button_handle_irq(int irq, void *_bdata)
351 {
352 struct gpio_keys_button_data *bdata =
353 (struct gpio_keys_button_data *) _bdata;
354
355 mod_delayed_work(system_wq, &bdata->work,
356 msecs_to_jiffies(bdata->software_debounce));
357
358 return IRQ_HANDLED;
359 }
360
361 #ifdef CONFIG_OF
362 static struct gpio_keys_platform_data *
363 gpio_keys_get_devtree_pdata(struct device *dev)
364 {
365 struct device_node *node, *pp;
366 struct gpio_keys_platform_data *pdata;
367 struct gpio_keys_button *button;
368 int error;
369 int nbuttons;
370 int i = 0;
371
372 node = dev->of_node;
373 if (!node)
374 return NULL;
375
376 nbuttons = of_get_child_count(node);
377 if (nbuttons == 0)
378 return NULL;
379
380 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button),
381 GFP_KERNEL);
382 if (!pdata) {
383 error = -ENOMEM;
384 goto err_out;
385 }
386
387 pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
388 pdata->nbuttons = nbuttons;
389
390 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
391 of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
392
393 for_each_child_of_node(node, pp) {
394 enum of_gpio_flags flags;
395
396 if (!of_find_property(pp, "gpios", NULL)) {
397 pdata->nbuttons--;
398 dev_warn(dev, "Found button without gpios\n");
399 continue;
400 }
401
402 button = (struct gpio_keys_button *)(&pdata->buttons[i++]);
403
404 button->irq = irq_of_parse_and_map(pp, 0);
405
406 button->gpio = of_get_gpio_flags(pp, 0, &flags);
407 if (button->gpio < 0) {
408 error = button->gpio;
409 if (error != -ENOENT) {
410 if (error != -EPROBE_DEFER)
411 dev_err(dev,
412 "Failed to get gpio flags, error: %d\n",
413 error);
414 return ERR_PTR(error);
415 }
416 } else {
417 button->active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
418 }
419
420 if (of_property_read_u32(pp, "linux,code", &button->code)) {
421 dev_err(dev, "Button without keycode: 0x%x\n",
422 button->gpio);
423 error = -EINVAL;
424 goto err_out;
425 }
426
427 button->desc = of_get_property(pp, "label", NULL);
428
429 if (of_property_read_u32(pp, "linux,input-type", &button->type))
430 button->type = EV_KEY;
431
432 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
433
434 if (of_property_read_u32(pp, "debounce-interval",
435 &button->debounce_interval))
436 button->debounce_interval = 5;
437 }
438
439 if (pdata->nbuttons == 0) {
440 error = -EINVAL;
441 goto err_out;
442 }
443
444 return pdata;
445
446 err_out:
447 return ERR_PTR(error);
448 }
449
450 static struct of_device_id gpio_keys_of_match[] = {
451 { .compatible = "gpio-keys", },
452 { },
453 };
454 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
455
456 static struct of_device_id gpio_keys_polled_of_match[] = {
457 { .compatible = "gpio-keys-polled", },
458 { },
459 };
460 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
461
462 #else
463
464 static inline struct gpio_keys_platform_data *
465 gpio_keys_get_devtree_pdata(struct device *dev)
466 {
467 return NULL;
468 }
469 #endif
470
471 static int gpio_keys_button_probe(struct platform_device *pdev,
472 struct gpio_keys_button_dev **_bdev, int polled)
473 {
474 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
475 struct device *dev = &pdev->dev;
476 struct gpio_keys_button_dev *bdev;
477 struct gpio_keys_button *buttons;
478 int error;
479 int i;
480
481 if (!pdata) {
482 pdata = gpio_keys_get_devtree_pdata(dev);
483 if (IS_ERR(pdata))
484 return PTR_ERR(pdata);
485 if (!pdata) {
486 dev_err(dev, "missing platform data\n");
487 return -EINVAL;
488 }
489 }
490
491 if (polled && !pdata->poll_interval) {
492 dev_err(dev, "missing poll_interval value\n");
493 return -EINVAL;
494 }
495
496 buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button),
497 GFP_KERNEL);
498 if (!buttons) {
499 dev_err(dev, "no memory for button data\n");
500 return -ENOMEM;
501 }
502 memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button));
503
504 bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_dev) +
505 pdata->nbuttons * sizeof(struct gpio_keys_button_data),
506 GFP_KERNEL);
507 if (!bdev) {
508 dev_err(dev, "no memory for private data\n");
509 return -ENOMEM;
510 }
511
512 bdev->polled = polled;
513
514 for (i = 0; i < pdata->nbuttons; i++) {
515 struct gpio_keys_button *button = &buttons[i];
516 struct gpio_keys_button_data *bdata = &bdev->data[i];
517 unsigned int gpio = button->gpio;
518
519 if (button->wakeup) {
520 dev_err(dev, DRV_NAME "does not support wakeup\n");
521 return -EINVAL;
522 }
523
524 bdata->map_entry = button_get_index(button->code);
525 if (bdata->map_entry < 0) {
526 dev_warn(dev, DRV_NAME "does not support key code:%u\n",
527 button->code);
528 continue;
529 }
530
531 if (!(button->type == 0 || button->type == EV_KEY ||
532 button->type == EV_SW)) {
533 dev_warn(dev, DRV_NAME "only supports buttons or switches\n");
534 continue;
535 }
536
537 error = devm_gpio_request(dev, gpio,
538 button->desc ? button->desc : DRV_NAME);
539 if (error) {
540 dev_err(dev, "unable to claim gpio %u, err=%d\n",
541 gpio, error);
542 return error;
543 }
544 bdata->gpiod = gpio_to_desc(gpio);
545 if (!bdata->gpiod)
546 return -EINVAL;
547
548 error = gpio_direction_input(gpio);
549 if (error) {
550 dev_err(dev,
551 "unable to set direction on gpio %u, err=%d\n",
552 gpio, error);
553 return error;
554 }
555
556 bdata->can_sleep = gpio_cansleep(gpio);
557 bdata->last_state = -1; /* Unknown state on boot */
558
559 if (bdev->polled) {
560 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
561 pdata->poll_interval);
562 } else {
563 /* bdata->threshold = 0; already initialized */
564
565 if (button->debounce_interval) {
566 error = gpiod_set_debounce(bdata->gpiod,
567 button->debounce_interval * 1000);
568 /*
569 * use timer if gpiolib doesn't provide
570 * debounce.
571 */
572 if (error < 0) {
573 bdata->software_debounce =
574 button->debounce_interval;
575 }
576 }
577 }
578
579 bdata->b = &pdata->buttons[i];
580 }
581
582 bdev->dev = &pdev->dev;
583 bdev->pdata = pdata;
584 platform_set_drvdata(pdev, bdev);
585
586 *_bdev = bdev;
587
588 return 0;
589 }
590
591 static int gpio_keys_probe(struct platform_device *pdev)
592 {
593 struct gpio_keys_platform_data *pdata;
594 struct gpio_keys_button_dev *bdev;
595 int ret, i;
596
597
598 ret = gpio_keys_button_probe(pdev, &bdev, 0);
599
600 if (ret)
601 return ret;
602
603 pdata = bdev->pdata;
604 for (i = 0; i < pdata->nbuttons; i++) {
605 const struct gpio_keys_button *button = &pdata->buttons[i];
606 struct gpio_keys_button_data *bdata = &bdev->data[i];
607 unsigned long irqflags = IRQF_ONESHOT;
608
609 INIT_DELAYED_WORK(&bdata->work, gpio_keys_irq_work_func);
610
611 if (!bdata->gpiod)
612 continue;
613
614 if (!button->irq) {
615 bdata->irq = gpio_to_irq(button->gpio);
616
617 if (bdata->irq < 0) {
618 dev_err(&pdev->dev, "failed to get irq for gpio:%d\n",
619 button->gpio);
620 continue;
621 }
622
623 irqflags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
624 } else {
625 bdata->irq = button->irq;
626 }
627
628 schedule_delayed_work(&bdata->work,
629 msecs_to_jiffies(bdata->software_debounce));
630
631 ret = devm_request_threaded_irq(&pdev->dev,
632 bdata->irq, NULL, button_handle_irq,
633 irqflags, dev_name(&pdev->dev), bdata);
634
635 if (ret < 0) {
636 bdata->irq = 0;
637 dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n",
638 bdata->irq, button->gpio);
639 continue;
640 } else {
641 dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n",
642 button->gpio, bdata->irq);
643 }
644 }
645
646 return 0;
647 }
648
649 static int gpio_keys_polled_probe(struct platform_device *pdev)
650 {
651 struct gpio_keys_platform_data *pdata;
652 struct gpio_keys_button_dev *bdev;
653 int ret;
654
655 ret = gpio_keys_button_probe(pdev, &bdev, 1);
656
657 if (ret)
658 return ret;
659
660 INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
661
662 pdata = bdev->pdata;
663
664 if (pdata->enable)
665 pdata->enable(bdev->dev);
666
667 gpio_keys_polled_queue_work(bdev);
668
669 return ret;
670 }
671
672 static void gpio_keys_irq_close(struct gpio_keys_button_dev *bdev)
673 {
674 struct gpio_keys_platform_data *pdata = bdev->pdata;
675 size_t i;
676
677 for (i = 0; i < pdata->nbuttons; i++) {
678 struct gpio_keys_button_data *bdata = &bdev->data[i];
679
680 disable_irq(bdata->irq);
681 cancel_delayed_work_sync(&bdata->work);
682 }
683 }
684
685 static int gpio_keys_remove(struct platform_device *pdev)
686 {
687 struct gpio_keys_button_dev *bdev = platform_get_drvdata(pdev);
688
689 platform_set_drvdata(pdev, NULL);
690
691 if (bdev->polled)
692 gpio_keys_polled_close(bdev);
693 else
694 gpio_keys_irq_close(bdev);
695
696 return 0;
697 }
698
699 static struct platform_driver gpio_keys_driver = {
700 .probe = gpio_keys_probe,
701 .remove = gpio_keys_remove,
702 .driver = {
703 .name = "gpio-keys",
704 .owner = THIS_MODULE,
705 .of_match_table = of_match_ptr(gpio_keys_of_match),
706 },
707 };
708
709 static struct platform_driver gpio_keys_polled_driver = {
710 .probe = gpio_keys_polled_probe,
711 .remove = gpio_keys_remove,
712 .driver = {
713 .name = "gpio-keys-polled",
714 .owner = THIS_MODULE,
715 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
716 },
717 };
718
719 static int __init gpio_button_init(void)
720 {
721 int ret;
722
723 ret = platform_driver_register(&gpio_keys_driver);
724 if (ret)
725 return ret;
726
727 ret = platform_driver_register(&gpio_keys_polled_driver);
728 if (ret)
729 platform_driver_unregister(&gpio_keys_driver);
730
731 return ret;
732 }
733
734 static void __exit gpio_button_exit(void)
735 {
736 platform_driver_unregister(&gpio_keys_driver);
737 platform_driver_unregister(&gpio_keys_polled_driver);
738 }
739
740 module_init(gpio_button_init);
741 module_exit(gpio_button_exit);
742
743 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
744 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
745 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
746 MODULE_LICENSE("GPL v2");
747 MODULE_ALIAS("platform:" DRV_NAME);