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/gpio_keys.h>
31 #define DRV_NAME "gpio-keys"
33 #define BH_SKB_SIZE 2048
35 #define PFX DRV_NAME ": "
40 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
42 #define BH_DBG(fmt, args...) do {} while (0)
45 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
58 struct work_struct work
;
66 struct gpio_keys_button_data
{
67 struct delayed_work work
;
73 struct gpio_keys_button
*b
;
76 extern u64
uevent_next_seqnum(void);
78 #define BH_MAP(_code, _name) \
84 static struct bh_map button_map
[] = {
85 BH_MAP(BTN_0
, "BTN_0"),
86 BH_MAP(BTN_1
, "BTN_1"),
87 BH_MAP(BTN_2
, "BTN_2"),
88 BH_MAP(BTN_3
, "BTN_3"),
89 BH_MAP(BTN_4
, "BTN_4"),
90 BH_MAP(BTN_5
, "BTN_5"),
91 BH_MAP(BTN_6
, "BTN_6"),
92 BH_MAP(BTN_7
, "BTN_7"),
93 BH_MAP(BTN_8
, "BTN_8"),
94 BH_MAP(BTN_9
, "BTN_9"),
95 BH_MAP(KEY_BRIGHTNESS_ZERO
, "brightness_zero"),
96 BH_MAP(KEY_CONFIG
, "config"),
97 BH_MAP(KEY_COPY
, "copy"),
98 BH_MAP(KEY_EJECTCD
, "eject"),
99 BH_MAP(KEY_HELP
, "help"),
100 BH_MAP(KEY_LIGHTS_TOGGLE
, "lights_toggle"),
101 BH_MAP(KEY_PHONE
, "phone"),
102 BH_MAP(KEY_POWER
, "power"),
103 BH_MAP(KEY_POWER2
, "reboot"),
104 BH_MAP(KEY_RESTART
, "reset"),
105 BH_MAP(KEY_RFKILL
, "rfkill"),
106 BH_MAP(KEY_VIDEO
, "video"),
107 BH_MAP(KEY_WIMAX
, "wwan"),
108 BH_MAP(KEY_WLAN
, "wlan"),
109 BH_MAP(KEY_WPS_BUTTON
, "wps"),
112 /* -------------------------------------------------------------------------*/
114 static __printf(3, 4)
115 int bh_event_add_var(struct bh_event
*event
, int argv
, const char *format
, ...)
117 static char buf
[128];
125 va_start(args
, format
);
126 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
129 if (len
>= sizeof(buf
)) {
130 WARN(1, "buffer size too small");
134 s
= skb_put(event
->skb
, len
+ 1);
137 BH_DBG("added variable '%s'\n", s
);
142 static int button_hotplug_fill_event(struct bh_event
*event
)
146 ret
= bh_event_add_var(event
, 0, "HOME=%s", "/");
150 ret
= bh_event_add_var(event
, 0, "PATH=%s",
151 "/sbin:/bin:/usr/sbin:/usr/bin");
155 ret
= bh_event_add_var(event
, 0, "SUBSYSTEM=%s", "button");
159 ret
= bh_event_add_var(event
, 0, "ACTION=%s", event
->action
);
163 ret
= bh_event_add_var(event
, 0, "BUTTON=%s", event
->name
);
167 if (event
->type
== EV_SW
) {
168 ret
= bh_event_add_var(event
, 0, "TYPE=%s", "switch");
173 ret
= bh_event_add_var(event
, 0, "SEEN=%ld", event
->seen
);
177 ret
= bh_event_add_var(event
, 0, "SEQNUM=%llu", uevent_next_seqnum());
182 static void button_hotplug_work(struct work_struct
*work
)
184 struct bh_event
*event
= container_of(work
, struct bh_event
, work
);
187 event
->skb
= alloc_skb(BH_SKB_SIZE
, GFP_KERNEL
);
191 ret
= bh_event_add_var(event
, 0, "%s@", event
->action
);
195 ret
= button_hotplug_fill_event(event
);
199 NETLINK_CB(event
->skb
).dst_group
= 1;
200 broadcast_uevent(event
->skb
, 0, 1, GFP_KERNEL
);
204 BH_ERR("work error %d\n", ret
);
205 kfree_skb(event
->skb
);
211 static int button_hotplug_create_event(const char *name
, unsigned int type
,
212 unsigned long seen
, int pressed
)
214 struct bh_event
*event
;
216 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
217 name
, seen
, pressed
);
219 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
226 event
->action
= pressed
? "pressed" : "released";
228 INIT_WORK(&event
->work
, (void *)(void *)button_hotplug_work
);
229 schedule_work(&event
->work
);
234 /* -------------------------------------------------------------------------*/
236 static int button_get_index(unsigned int code
)
240 for (i
= 0; i
< ARRAY_SIZE(button_map
); i
++)
241 if (button_map
[i
].code
== code
)
247 static void button_hotplug_event(struct gpio_keys_button_data
*data
,
248 unsigned int type
, int value
)
250 struct bh_priv
*priv
= &data
->bh
;
251 unsigned long seen
= jiffies
;
254 BH_DBG("event type=%u, code=%u, value=%d\n", type
, data
->b
->code
, value
);
256 if ((type
!= EV_KEY
) && (type
!= EV_SW
))
259 btn
= button_get_index(data
->b
->code
);
263 button_hotplug_create_event(button_map
[btn
].name
, type
,
264 (seen
- priv
->seen
) / HZ
, value
);
268 struct gpio_keys_button_dev
{
270 struct delayed_work work
;
273 struct gpio_keys_platform_data
*pdata
;
274 struct gpio_keys_button_data data
[0];
277 static int gpio_button_get_value(struct gpio_keys_button_data
*bdata
)
281 if (bdata
->can_sleep
)
282 val
= !!gpio_get_value_cansleep(bdata
->b
->gpio
);
284 val
= !!gpio_get_value(bdata
->b
->gpio
);
286 return val
^ bdata
->b
->active_low
;
289 static void gpio_keys_polled_check_state(struct gpio_keys_button_data
*bdata
)
291 int state
= gpio_button_get_value(bdata
);
293 if (state
!= bdata
->last_state
) {
294 unsigned int type
= bdata
->b
->type
?: EV_KEY
;
296 if (bdata
->count
< bdata
->threshold
) {
301 if ((bdata
->last_state
!= -1) || (type
== EV_SW
))
302 button_hotplug_event(bdata
, type
, state
);
304 bdata
->last_state
= state
;
310 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev
*bdev
)
312 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
313 unsigned long delay
= msecs_to_jiffies(pdata
->poll_interval
);
316 delay
= round_jiffies_relative(delay
);
317 schedule_delayed_work(&bdev
->work
, delay
);
320 static void gpio_keys_polled_poll(struct work_struct
*work
)
322 struct gpio_keys_button_dev
*bdev
=
323 container_of(work
, struct gpio_keys_button_dev
, work
.work
);
326 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
327 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
328 gpio_keys_polled_check_state(bdata
);
330 gpio_keys_polled_queue_work(bdev
);
333 static void gpio_keys_polled_close(struct gpio_keys_button_dev
*bdev
)
335 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
337 cancel_delayed_work_sync(&bdev
->work
);
340 pdata
->disable(bdev
->dev
);
343 static irqreturn_t
button_handle_irq(int irq
, void *_bdata
)
345 struct gpio_keys_button_data
*bdata
= (struct gpio_keys_button_data
*) _bdata
;
347 button_hotplug_event(bdata
, bdata
->b
->type
?: EV_KEY
, gpio_button_get_value(bdata
));
353 static struct gpio_keys_platform_data
*
354 gpio_keys_get_devtree_pdata(struct device
*dev
)
356 struct device_node
*node
, *pp
;
357 struct gpio_keys_platform_data
*pdata
;
358 struct gpio_keys_button
*button
;
367 nbuttons
= of_get_child_count(node
);
371 pdata
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* (sizeof *button
),
378 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
379 pdata
->nbuttons
= nbuttons
;
381 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
382 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
384 for_each_child_of_node(node
, pp
) {
385 enum of_gpio_flags flags
;
387 if (!of_find_property(pp
, "gpios", NULL
)) {
389 dev_warn(dev
, "Found button without gpios\n");
393 button
= &pdata
->buttons
[i
++];
395 button
->gpio
= of_get_gpio_flags(pp
, 0, &flags
);
396 if (button
->gpio
< 0) {
397 error
= button
->gpio
;
398 if (error
!= -ENOENT
) {
399 if (error
!= -EPROBE_DEFER
)
401 "Failed to get gpio flags, error: %d\n",
403 return ERR_PTR(error
);
406 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
409 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
410 dev_err(dev
, "Button without keycode: 0x%x\n",
416 button
->desc
= of_get_property(pp
, "label", NULL
);
418 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
419 button
->type
= EV_KEY
;
421 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
423 if (of_property_read_u32(pp
, "debounce-interval",
424 &button
->debounce_interval
))
425 button
->debounce_interval
= 5;
428 if (pdata
->nbuttons
== 0) {
436 return ERR_PTR(error
);
439 static struct of_device_id gpio_keys_of_match
[] = {
440 { .compatible
= "gpio-keys", },
443 MODULE_DEVICE_TABLE(of
, gpio_keys_of_match
);
445 static struct of_device_id gpio_keys_polled_of_match
[] = {
446 { .compatible
= "gpio-keys-polled", },
449 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
453 static inline struct gpio_keys_platform_data
*
454 gpio_keys_get_devtree_pdata(struct device
*dev
)
460 static int gpio_keys_button_probe(struct platform_device
*pdev
,
461 struct gpio_keys_button_dev
**_bdev
, int polled
)
463 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
464 struct device
*dev
= &pdev
->dev
;
465 struct gpio_keys_button_dev
*bdev
;
466 struct gpio_keys_button
*buttons
;
471 pdata
= gpio_keys_get_devtree_pdata(dev
);
473 return PTR_ERR(pdata
);
475 dev_err(dev
, "missing platform data\n");
480 if (polled
&& !pdata
->poll_interval
) {
481 dev_err(dev
, "missing poll_interval value\n");
485 buttons
= devm_kzalloc(dev
, pdata
->nbuttons
* sizeof(struct gpio_keys_button
),
488 dev_err(dev
, "no memory for button data\n");
491 memcpy(buttons
, pdata
->buttons
, pdata
->nbuttons
* sizeof(struct gpio_keys_button
));
493 bdev
= devm_kzalloc(dev
, sizeof(struct gpio_keys_button_dev
) +
494 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
497 dev_err(dev
, "no memory for private data\n");
501 bdev
->polled
= polled
;
503 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
504 struct gpio_keys_button
*button
= &buttons
[i
];
505 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
506 unsigned int gpio
= button
->gpio
;
508 if (button
->wakeup
) {
509 dev_err(dev
, DRV_NAME
"does not support wakeup\n");
513 error
= devm_gpio_request(dev
, gpio
,
514 button
->desc
? button
->desc
: DRV_NAME
);
516 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
521 error
= gpio_direction_input(gpio
);
524 "unable to set direction on gpio %u, err=%d\n",
529 bdata
->can_sleep
= gpio_cansleep(gpio
);
530 bdata
->last_state
= -1;
533 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
534 pdata
->poll_interval
);
536 bdata
->threshold
= 1;
538 bdata
->b
= &pdata
->buttons
[i
];
541 bdev
->dev
= &pdev
->dev
;
543 platform_set_drvdata(pdev
, bdev
);
550 static int gpio_keys_probe(struct platform_device
*pdev
)
552 struct gpio_keys_platform_data
*pdata
;
553 struct gpio_keys_button_dev
*bdev
;
557 ret
= gpio_keys_button_probe(pdev
, &bdev
, 0);
563 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
564 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
565 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
568 button
->irq
= gpio_to_irq(button
->gpio
);
569 if (button
->irq
< 0) {
570 dev_err(&pdev
->dev
, "failed to get irq for gpio:%d\n", button
->gpio
);
574 ret
= devm_request_threaded_irq(&pdev
->dev
, button
->irq
, NULL
, button_handle_irq
,
575 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
576 dev_name(&pdev
->dev
), bdata
);
578 dev_err(&pdev
->dev
, "failed to request irq:%d for gpio:%d\n", button
->irq
, button
->gpio
);
580 dev_dbg(&pdev
->dev
, "gpio:%d has irq:%d\n", button
->gpio
, button
->irq
);
582 if (bdata
->b
->type
== EV_SW
)
583 button_hotplug_event(bdata
, EV_SW
, gpio_button_get_value(bdata
));
589 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
591 struct gpio_keys_platform_data
*pdata
;
592 struct gpio_keys_button_dev
*bdev
;
596 ret
= gpio_keys_button_probe(pdev
, &bdev
, 1);
601 INIT_DELAYED_WORK(&bdev
->work
, gpio_keys_polled_poll
);
606 pdata
->enable(bdev
->dev
);
608 for (i
= 0; i
< pdata
->nbuttons
; i
++)
609 gpio_keys_polled_check_state(&bdev
->data
[i
]);
611 gpio_keys_polled_queue_work(bdev
);
616 static int gpio_keys_remove(struct platform_device
*pdev
)
618 struct gpio_keys_button_dev
*bdev
= platform_get_drvdata(pdev
);
620 platform_set_drvdata(pdev
, NULL
);
623 gpio_keys_polled_close(bdev
);
628 static struct platform_driver gpio_keys_driver
= {
629 .probe
= gpio_keys_probe
,
630 .remove
= gpio_keys_remove
,
633 .owner
= THIS_MODULE
,
634 .of_match_table
= of_match_ptr(gpio_keys_of_match
),
638 static struct platform_driver gpio_keys_polled_driver
= {
639 .probe
= gpio_keys_polled_probe
,
640 .remove
= gpio_keys_remove
,
642 .name
= "gpio-keys-polled",
643 .owner
= THIS_MODULE
,
644 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
648 static int __init
gpio_button_init(void)
652 ret
= platform_driver_register(&gpio_keys_driver
);
656 ret
= platform_driver_register(&gpio_keys_polled_driver
);
658 platform_driver_unregister(&gpio_keys_driver
);
663 static void __exit
gpio_button_exit(void)
665 platform_driver_unregister(&gpio_keys_driver
);
666 platform_driver_unregister(&gpio_keys_polled_driver
);
669 module_init(gpio_button_init
);
670 module_exit(gpio_button_exit
);
672 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
673 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
674 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
675 MODULE_LICENSE("GPL v2");
676 MODULE_ALIAS("platform:" DRV_NAME
);