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>
32 #define BH_SKB_SIZE 2048
34 #define DRV_NAME "gpio-keys"
35 #define PFX DRV_NAME ": "
48 struct work_struct work
;
56 struct gpio_keys_button_data
{
57 struct delayed_work work
;
64 unsigned int software_debounce
;
65 struct gpio_desc
*gpiod
;
66 const struct gpio_keys_button
*b
;
69 extern u64
uevent_next_seqnum(void);
71 #define BH_MAP(_code, _name) \
77 static struct bh_map button_map
[] = {
78 BH_MAP(BTN_0
, "BTN_0"),
79 BH_MAP(BTN_1
, "BTN_1"),
80 BH_MAP(BTN_2
, "BTN_2"),
81 BH_MAP(BTN_3
, "BTN_3"),
82 BH_MAP(BTN_4
, "BTN_4"),
83 BH_MAP(BTN_5
, "BTN_5"),
84 BH_MAP(BTN_6
, "BTN_6"),
85 BH_MAP(BTN_7
, "BTN_7"),
86 BH_MAP(BTN_8
, "BTN_8"),
87 BH_MAP(BTN_9
, "BTN_9"),
88 BH_MAP(KEY_BRIGHTNESS_ZERO
, "brightness_zero"),
89 BH_MAP(KEY_CONFIG
, "config"),
90 BH_MAP(KEY_COPY
, "copy"),
91 BH_MAP(KEY_EJECTCD
, "eject"),
92 BH_MAP(KEY_HELP
, "help"),
93 BH_MAP(KEY_LIGHTS_TOGGLE
, "lights_toggle"),
94 BH_MAP(KEY_PHONE
, "phone"),
95 BH_MAP(KEY_POWER
, "power"),
96 BH_MAP(KEY_POWER2
, "reboot"),
97 BH_MAP(KEY_RESTART
, "reset"),
98 BH_MAP(KEY_RFKILL
, "rfkill"),
99 BH_MAP(KEY_VIDEO
, "video"),
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 void button_hotplug_event(struct gpio_keys_button_data
*data
,
241 unsigned int type
, int value
)
243 struct bh_priv
*priv
= &data
->bh
;
244 unsigned long seen
= jiffies
;
247 pr_debug(PFX
"event type=%u, code=%u, value=%d\n", type
, data
->b
->code
, value
);
249 if ((type
!= EV_KEY
) && (type
!= EV_SW
))
252 btn
= button_get_index(data
->b
->code
);
259 button_hotplug_create_event(button_map
[btn
].name
, type
,
260 (seen
- priv
->seen
) / HZ
, value
);
264 struct gpio_keys_button_dev
{
266 struct delayed_work work
;
269 struct gpio_keys_platform_data
*pdata
;
270 struct gpio_keys_button_data data
[0];
273 static int gpio_button_get_value(struct gpio_keys_button_data
*bdata
)
277 if (bdata
->can_sleep
)
278 val
= !!gpio_get_value_cansleep(bdata
->b
->gpio
);
280 val
= !!gpio_get_value(bdata
->b
->gpio
);
282 return val
^ bdata
->b
->active_low
;
285 static void gpio_keys_polled_check_state(struct gpio_keys_button_data
*bdata
)
287 int state
= gpio_button_get_value(bdata
);
289 if (state
!= bdata
->last_state
) {
290 unsigned int type
= bdata
->b
->type
?: EV_KEY
;
292 if (bdata
->count
< bdata
->threshold
) {
297 if (bdata
->last_state
!= -1 || type
== EV_SW
)
298 button_hotplug_event(bdata
, type
, state
);
300 bdata
->last_state
= state
;
306 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev
*bdev
)
308 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
309 unsigned long delay
= msecs_to_jiffies(pdata
->poll_interval
);
312 delay
= round_jiffies_relative(delay
);
313 schedule_delayed_work(&bdev
->work
, delay
);
316 static void gpio_keys_polled_poll(struct work_struct
*work
)
318 struct gpio_keys_button_dev
*bdev
=
319 container_of(work
, struct gpio_keys_button_dev
, work
.work
);
322 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
323 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
324 gpio_keys_polled_check_state(bdata
);
326 gpio_keys_polled_queue_work(bdev
);
329 static void gpio_keys_polled_close(struct gpio_keys_button_dev
*bdev
)
331 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
333 cancel_delayed_work_sync(&bdev
->work
);
336 pdata
->disable(bdev
->dev
);
339 static void gpio_keys_irq_work_func(struct work_struct
*work
)
341 struct gpio_keys_button_data
*bdata
= container_of(work
,
342 struct gpio_keys_button_data
, work
.work
);
344 button_hotplug_event(bdata
, bdata
->b
->type
?: EV_KEY
,
345 gpio_button_get_value(bdata
));
348 static irqreturn_t
button_handle_irq(int irq
, void *_bdata
)
350 struct gpio_keys_button_data
*bdata
=
351 (struct gpio_keys_button_data
*) _bdata
;
353 schedule_delayed_work(&bdata
->work
,
354 msecs_to_jiffies(bdata
->software_debounce
));
360 static struct gpio_keys_platform_data
*
361 gpio_keys_get_devtree_pdata(struct device
*dev
)
363 struct device_node
*node
, *pp
;
364 struct gpio_keys_platform_data
*pdata
;
365 struct gpio_keys_button
*button
;
374 nbuttons
= of_get_child_count(node
);
378 pdata
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* (sizeof *button
),
385 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
386 pdata
->nbuttons
= nbuttons
;
388 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
389 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
391 for_each_child_of_node(node
, pp
) {
392 enum of_gpio_flags flags
;
394 if (!of_find_property(pp
, "gpios", NULL
)) {
396 dev_warn(dev
, "Found button without gpios\n");
400 button
= (struct gpio_keys_button
*)(&pdata
->buttons
[i
++]);
402 button
->irq
= irq_of_parse_and_map(pp
, 0);
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
)
410 "Failed to get gpio flags, error: %d\n",
412 return ERR_PTR(error
);
415 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
418 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
419 dev_err(dev
, "Button without keycode: 0x%x\n",
425 button
->desc
= of_get_property(pp
, "label", NULL
);
427 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
428 button
->type
= EV_KEY
;
430 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
432 if (of_property_read_u32(pp
, "debounce-interval",
433 &button
->debounce_interval
))
434 button
->debounce_interval
= 5;
437 if (pdata
->nbuttons
== 0) {
445 return ERR_PTR(error
);
448 static struct of_device_id gpio_keys_of_match
[] = {
449 { .compatible
= "gpio-keys", },
452 MODULE_DEVICE_TABLE(of
, gpio_keys_of_match
);
454 static struct of_device_id gpio_keys_polled_of_match
[] = {
455 { .compatible
= "gpio-keys-polled", },
458 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
462 static inline struct gpio_keys_platform_data
*
463 gpio_keys_get_devtree_pdata(struct device
*dev
)
469 static int gpio_keys_button_probe(struct platform_device
*pdev
,
470 struct gpio_keys_button_dev
**_bdev
, int polled
)
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
;
480 pdata
= gpio_keys_get_devtree_pdata(dev
);
482 return PTR_ERR(pdata
);
484 dev_err(dev
, "missing platform data\n");
489 if (polled
&& !pdata
->poll_interval
) {
490 dev_err(dev
, "missing poll_interval value\n");
494 buttons
= devm_kzalloc(dev
, pdata
->nbuttons
* sizeof(struct gpio_keys_button
),
497 dev_err(dev
, "no memory for button data\n");
500 memcpy(buttons
, pdata
->buttons
, pdata
->nbuttons
* sizeof(struct gpio_keys_button
));
502 bdev
= devm_kzalloc(dev
, sizeof(struct gpio_keys_button_dev
) +
503 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
506 dev_err(dev
, "no memory for private data\n");
510 bdev
->polled
= polled
;
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
;
517 if (button
->wakeup
) {
518 dev_err(dev
, DRV_NAME
"does not support wakeup\n");
522 error
= devm_gpio_request(dev
, gpio
,
523 button
->desc
? button
->desc
: DRV_NAME
);
525 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
529 bdata
->gpiod
= gpio_to_desc(gpio
);
533 error
= gpio_direction_input(gpio
);
536 "unable to set direction on gpio %u, err=%d\n",
541 bdata
->can_sleep
= gpio_cansleep(gpio
);
542 bdata
->last_state
= -1;
545 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
546 pdata
->poll_interval
);
548 bdata
->threshold
= 1;
550 if (button
->debounce_interval
) {
551 error
= gpiod_set_debounce(bdata
->gpiod
,
552 button
->debounce_interval
* 1000);
554 * use timer if gpiolib doesn't provide
558 bdata
->software_debounce
=
559 button
->debounce_interval
;
564 bdata
->b
= &pdata
->buttons
[i
];
567 bdev
->dev
= &pdev
->dev
;
569 platform_set_drvdata(pdev
, bdev
);
576 static int gpio_keys_probe(struct platform_device
*pdev
)
578 struct gpio_keys_platform_data
*pdata
;
579 struct gpio_keys_button_dev
*bdev
;
583 ret
= gpio_keys_button_probe(pdev
, &bdev
, 0);
589 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
590 const struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
591 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
592 unsigned long irqflags
= IRQF_ONESHOT
;
595 bdata
->irq
= gpio_to_irq(button
->gpio
);
597 if (bdata
->irq
< 0) {
598 dev_err(&pdev
->dev
, "failed to get irq for gpio:%d\n",
603 irqflags
|= IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
;
605 bdata
->irq
= button
->irq
;
608 INIT_DELAYED_WORK(&bdata
->work
, gpio_keys_irq_work_func
);
610 ret
= devm_request_threaded_irq(&pdev
->dev
,
611 bdata
->irq
, NULL
, button_handle_irq
,
612 irqflags
, dev_name(&pdev
->dev
), bdata
);
616 dev_err(&pdev
->dev
, "failed to request irq:%d for gpio:%d\n",
617 bdata
->irq
, button
->gpio
);
620 dev_dbg(&pdev
->dev
, "gpio:%d has irq:%d\n",
621 button
->gpio
, bdata
->irq
);
624 if (bdata
->b
->type
== EV_SW
)
625 button_hotplug_event(bdata
, EV_SW
, gpio_button_get_value(bdata
));
631 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
633 struct gpio_keys_platform_data
*pdata
;
634 struct gpio_keys_button_dev
*bdev
;
638 ret
= gpio_keys_button_probe(pdev
, &bdev
, 1);
643 INIT_DELAYED_WORK(&bdev
->work
, gpio_keys_polled_poll
);
648 pdata
->enable(bdev
->dev
);
650 for (i
= 0; i
< pdata
->nbuttons
; i
++)
651 gpio_keys_polled_check_state(&bdev
->data
[i
]);
653 gpio_keys_polled_queue_work(bdev
);
658 static void gpio_keys_irq_close(struct gpio_keys_button_dev
*bdev
)
660 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
663 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
664 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
666 disable_irq(bdata
->irq
);
667 cancel_delayed_work_sync(&bdata
->work
);
671 static int gpio_keys_remove(struct platform_device
*pdev
)
673 struct gpio_keys_button_dev
*bdev
= platform_get_drvdata(pdev
);
675 platform_set_drvdata(pdev
, NULL
);
678 gpio_keys_polled_close(bdev
);
680 gpio_keys_irq_close(bdev
);
685 static struct platform_driver gpio_keys_driver
= {
686 .probe
= gpio_keys_probe
,
687 .remove
= gpio_keys_remove
,
690 .owner
= THIS_MODULE
,
691 .of_match_table
= of_match_ptr(gpio_keys_of_match
),
695 static struct platform_driver gpio_keys_polled_driver
= {
696 .probe
= gpio_keys_polled_probe
,
697 .remove
= gpio_keys_remove
,
699 .name
= "gpio-keys-polled",
700 .owner
= THIS_MODULE
,
701 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
705 static int __init
gpio_button_init(void)
709 ret
= platform_driver_register(&gpio_keys_driver
);
713 ret
= platform_driver_register(&gpio_keys_polled_driver
);
715 platform_driver_unregister(&gpio_keys_driver
);
720 static void __exit
gpio_button_exit(void)
722 platform_driver_unregister(&gpio_keys_driver
);
723 platform_driver_unregister(&gpio_keys_polled_driver
);
726 module_init(gpio_button_init
);
727 module_exit(gpio_button_exit
);
729 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
730 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
731 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
732 MODULE_LICENSE("GPL v2");
733 MODULE_ALIAS("platform:" DRV_NAME
);