2 * GPIO Button Hotplug driver
4 * Copyright (C) 2012 Felix Fietkau <nbd@nbd.name>
5 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
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>
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.
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/kmod.h>
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>
33 #define BH_SKB_SIZE 2048
35 #define DRV_NAME "gpio-keys"
36 #define PFX DRV_NAME ": "
45 struct work_struct work
;
53 struct gpio_keys_button_data
{
54 struct delayed_work work
;
62 unsigned int software_debounce
;
63 struct gpio_desc
*gpiod
;
64 const struct gpio_keys_button
*b
;
67 extern u64
uevent_next_seqnum(void);
69 #define BH_MAP(_code, _name) \
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"),
105 /* -------------------------------------------------------------------------*/
107 static __printf(3, 4)
108 int bh_event_add_var(struct bh_event
*event
, int argv
, const char *format
, ...)
110 static char buf
[128];
118 va_start(args
, format
);
119 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
122 if (len
>= sizeof(buf
)) {
123 WARN(1, "buffer size too small");
127 s
= skb_put(event
->skb
, len
+ 1);
130 pr_debug(PFX
"added variable '%s'\n", s
);
135 static int button_hotplug_fill_event(struct bh_event
*event
)
139 ret
= bh_event_add_var(event
, 0, "HOME=%s", "/");
143 ret
= bh_event_add_var(event
, 0, "PATH=%s",
144 "/sbin:/bin:/usr/sbin:/usr/bin");
148 ret
= bh_event_add_var(event
, 0, "SUBSYSTEM=%s", "button");
152 ret
= bh_event_add_var(event
, 0, "ACTION=%s", event
->action
);
156 ret
= bh_event_add_var(event
, 0, "BUTTON=%s", event
->name
);
160 if (event
->type
== EV_SW
) {
161 ret
= bh_event_add_var(event
, 0, "TYPE=%s", "switch");
166 ret
= bh_event_add_var(event
, 0, "SEEN=%ld", event
->seen
);
170 ret
= bh_event_add_var(event
, 0, "SEQNUM=%llu", uevent_next_seqnum());
175 static void button_hotplug_work(struct work_struct
*work
)
177 struct bh_event
*event
= container_of(work
, struct bh_event
, work
);
180 event
->skb
= alloc_skb(BH_SKB_SIZE
, GFP_KERNEL
);
184 ret
= bh_event_add_var(event
, 0, "%s@", event
->action
);
188 ret
= button_hotplug_fill_event(event
);
192 NETLINK_CB(event
->skb
).dst_group
= 1;
193 broadcast_uevent(event
->skb
, 0, 1, GFP_KERNEL
);
197 pr_err(PFX
"work error %d\n", ret
);
198 kfree_skb(event
->skb
);
204 static int button_hotplug_create_event(const char *name
, unsigned int type
,
205 unsigned long seen
, int pressed
)
207 struct bh_event
*event
;
209 pr_debug(PFX
"create event, name=%s, seen=%lu, pressed=%d\n",
210 name
, seen
, pressed
);
212 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
219 event
->action
= pressed
? "pressed" : "released";
221 INIT_WORK(&event
->work
, (void *)(void *)button_hotplug_work
);
222 schedule_work(&event
->work
);
227 /* -------------------------------------------------------------------------*/
229 static int button_get_index(unsigned int code
)
233 for (i
= 0; i
< ARRAY_SIZE(button_map
); i
++)
234 if (button_map
[i
].code
== code
)
240 static int gpio_button_get_value(struct gpio_keys_button_data
*bdata
)
244 if (bdata
->can_sleep
)
245 val
= !!gpio_get_value_cansleep(bdata
->b
->gpio
);
247 val
= !!gpio_get_value(bdata
->b
->gpio
);
249 return val
^ bdata
->b
->active_low
;
252 static void gpio_keys_handle_button(struct gpio_keys_button_data
*bdata
)
254 unsigned int type
= bdata
->b
->type
?: EV_KEY
;
255 int state
= gpio_button_get_value(bdata
);
256 unsigned long seen
= jiffies
;
258 pr_debug(PFX
"event type=%u, code=%u, pressed=%d\n",
259 type
, bdata
->b
->code
, state
);
261 /* is this the initialization state? */
262 if (bdata
->last_state
== -1) {
264 * Don't advertise unpressed buttons on initialization.
265 * Just save their state and continue otherwise this
266 * can cause OpenWrt to enter failsafe.
268 if (type
== EV_KEY
&& state
== 0)
271 * But we are very interested in pressed buttons and
272 * initial switch state. These will be reported to
275 } else if (bdata
->last_state
== state
) {
276 /* reset asserted counter (only relevant for polled keys) */
281 if (bdata
->count
< bdata
->threshold
) {
286 if (bdata
->seen
== 0)
289 button_hotplug_create_event(button_map
[bdata
->map_entry
].name
, type
,
290 (seen
- bdata
->seen
) / HZ
, state
);
294 bdata
->last_state
= state
;
298 struct gpio_keys_button_dev
{
300 struct delayed_work work
;
303 struct gpio_keys_platform_data
*pdata
;
304 struct gpio_keys_button_data data
[0];
307 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev
*bdev
)
309 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
310 unsigned long delay
= msecs_to_jiffies(pdata
->poll_interval
);
313 delay
= round_jiffies_relative(delay
);
314 schedule_delayed_work(&bdev
->work
, delay
);
317 static void gpio_keys_polled_poll(struct work_struct
*work
)
319 struct gpio_keys_button_dev
*bdev
=
320 container_of(work
, struct gpio_keys_button_dev
, work
.work
);
323 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
324 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
327 gpio_keys_handle_button(bdata
);
329 gpio_keys_polled_queue_work(bdev
);
332 static void gpio_keys_polled_close(struct gpio_keys_button_dev
*bdev
)
334 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
336 cancel_delayed_work_sync(&bdev
->work
);
339 pdata
->disable(bdev
->dev
);
342 static void gpio_keys_irq_work_func(struct work_struct
*work
)
344 struct gpio_keys_button_data
*bdata
= container_of(work
,
345 struct gpio_keys_button_data
, work
.work
);
347 gpio_keys_handle_button(bdata
);
350 static irqreturn_t
button_handle_irq(int irq
, void *_bdata
)
352 struct gpio_keys_button_data
*bdata
=
353 (struct gpio_keys_button_data
*) _bdata
;
355 mod_delayed_work(system_wq
, &bdata
->work
,
356 msecs_to_jiffies(bdata
->software_debounce
));
362 static struct gpio_keys_platform_data
*
363 gpio_keys_get_devtree_pdata(struct device
*dev
)
365 struct device_node
*node
, *pp
;
366 struct gpio_keys_platform_data
*pdata
;
367 struct gpio_keys_button
*button
;
376 nbuttons
= of_get_child_count(node
);
380 pdata
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* (sizeof *button
),
387 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
388 pdata
->nbuttons
= nbuttons
;
390 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
391 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
393 for_each_child_of_node(node
, pp
) {
394 enum of_gpio_flags flags
;
396 if (!of_find_property(pp
, "gpios", NULL
)) {
398 dev_warn(dev
, "Found button without gpios\n");
402 button
= (struct gpio_keys_button
*)(&pdata
->buttons
[i
++]);
404 button
->irq
= irq_of_parse_and_map(pp
, 0);
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
)
412 "Failed to get gpio flags, error: %d\n",
414 return ERR_PTR(error
);
417 button
->active_low
= !!(flags
& OF_GPIO_ACTIVE_LOW
);
420 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
421 dev_err(dev
, "Button without keycode: 0x%x\n",
427 button
->desc
= of_get_property(pp
, "label", NULL
);
429 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
430 button
->type
= EV_KEY
;
432 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
434 if (of_property_read_u32(pp
, "debounce-interval",
435 &button
->debounce_interval
))
436 button
->debounce_interval
= 5;
439 if (pdata
->nbuttons
== 0) {
447 return ERR_PTR(error
);
450 static struct of_device_id gpio_keys_of_match
[] = {
451 { .compatible
= "gpio-keys", },
454 MODULE_DEVICE_TABLE(of
, gpio_keys_of_match
);
456 static struct of_device_id gpio_keys_polled_of_match
[] = {
457 { .compatible
= "gpio-keys-polled", },
460 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
464 static inline struct gpio_keys_platform_data
*
465 gpio_keys_get_devtree_pdata(struct device
*dev
)
471 static int gpio_keys_button_probe(struct platform_device
*pdev
,
472 struct gpio_keys_button_dev
**_bdev
, int polled
)
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
;
482 pdata
= gpio_keys_get_devtree_pdata(dev
);
484 return PTR_ERR(pdata
);
486 dev_err(dev
, "missing platform data\n");
491 if (polled
&& !pdata
->poll_interval
) {
492 dev_err(dev
, "missing poll_interval value\n");
496 buttons
= devm_kzalloc(dev
, pdata
->nbuttons
* sizeof(struct gpio_keys_button
),
499 dev_err(dev
, "no memory for button data\n");
502 memcpy(buttons
, pdata
->buttons
, pdata
->nbuttons
* sizeof(struct gpio_keys_button
));
504 bdev
= devm_kzalloc(dev
, sizeof(struct gpio_keys_button_dev
) +
505 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
508 dev_err(dev
, "no memory for private data\n");
512 bdev
->polled
= polled
;
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
;
519 if (button
->wakeup
) {
520 dev_err(dev
, DRV_NAME
"does not support wakeup\n");
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",
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");
537 error
= devm_gpio_request(dev
, gpio
,
538 button
->desc
? button
->desc
: DRV_NAME
);
540 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
544 bdata
->gpiod
= gpio_to_desc(gpio
);
548 error
= gpio_direction_input(gpio
);
551 "unable to set direction on gpio %u, err=%d\n",
556 bdata
->can_sleep
= gpio_cansleep(gpio
);
557 bdata
->last_state
= -1; /* Unknown state on boot */
560 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
561 pdata
->poll_interval
);
563 /* bdata->threshold = 0; already initialized */
565 if (button
->debounce_interval
) {
566 error
= gpiod_set_debounce(bdata
->gpiod
,
567 button
->debounce_interval
* 1000);
569 * use timer if gpiolib doesn't provide
573 bdata
->software_debounce
=
574 button
->debounce_interval
;
579 bdata
->b
= &pdata
->buttons
[i
];
582 bdev
->dev
= &pdev
->dev
;
584 platform_set_drvdata(pdev
, bdev
);
591 static int gpio_keys_probe(struct platform_device
*pdev
)
593 struct gpio_keys_platform_data
*pdata
;
594 struct gpio_keys_button_dev
*bdev
;
598 ret
= gpio_keys_button_probe(pdev
, &bdev
, 0);
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
;
609 INIT_DELAYED_WORK(&bdata
->work
, gpio_keys_irq_work_func
);
615 bdata
->irq
= gpio_to_irq(button
->gpio
);
617 if (bdata
->irq
< 0) {
618 dev_err(&pdev
->dev
, "failed to get irq for gpio:%d\n",
623 irqflags
|= IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
;
625 bdata
->irq
= button
->irq
;
628 schedule_delayed_work(&bdata
->work
,
629 msecs_to_jiffies(bdata
->software_debounce
));
631 ret
= devm_request_threaded_irq(&pdev
->dev
,
632 bdata
->irq
, NULL
, button_handle_irq
,
633 irqflags
, dev_name(&pdev
->dev
), bdata
);
637 dev_err(&pdev
->dev
, "failed to request irq:%d for gpio:%d\n",
638 bdata
->irq
, button
->gpio
);
641 dev_dbg(&pdev
->dev
, "gpio:%d has irq:%d\n",
642 button
->gpio
, bdata
->irq
);
649 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
651 struct gpio_keys_platform_data
*pdata
;
652 struct gpio_keys_button_dev
*bdev
;
655 ret
= gpio_keys_button_probe(pdev
, &bdev
, 1);
660 INIT_DELAYED_WORK(&bdev
->work
, gpio_keys_polled_poll
);
665 pdata
->enable(bdev
->dev
);
667 gpio_keys_polled_queue_work(bdev
);
672 static void gpio_keys_irq_close(struct gpio_keys_button_dev
*bdev
)
674 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
677 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
678 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
680 disable_irq(bdata
->irq
);
681 cancel_delayed_work_sync(&bdata
->work
);
685 static int gpio_keys_remove(struct platform_device
*pdev
)
687 struct gpio_keys_button_dev
*bdev
= platform_get_drvdata(pdev
);
689 platform_set_drvdata(pdev
, NULL
);
692 gpio_keys_polled_close(bdev
);
694 gpio_keys_irq_close(bdev
);
699 static struct platform_driver gpio_keys_driver
= {
700 .probe
= gpio_keys_probe
,
701 .remove
= gpio_keys_remove
,
704 .owner
= THIS_MODULE
,
705 .of_match_table
= of_match_ptr(gpio_keys_of_match
),
709 static struct platform_driver gpio_keys_polled_driver
= {
710 .probe
= gpio_keys_polled_probe
,
711 .remove
= gpio_keys_remove
,
713 .name
= "gpio-keys-polled",
714 .owner
= THIS_MODULE
,
715 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
719 static int __init
gpio_button_init(void)
723 ret
= platform_driver_register(&gpio_keys_driver
);
727 ret
= platform_driver_register(&gpio_keys_polled_driver
);
729 platform_driver_unregister(&gpio_keys_driver
);
734 static void __exit
gpio_button_exit(void)
736 platform_driver_unregister(&gpio_keys_driver
);
737 platform_driver_unregister(&gpio_keys_polled_driver
);
740 module_init(gpio_button_init
);
741 module_exit(gpio_button_exit
);
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
);