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