2 * GPIO Button Hotplug driver
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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@openwrt.org>
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/platform_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/gpio_keys.h>
30 #define DRV_NAME "gpio-keys-polled"
32 #define BH_SKB_SIZE 2048
34 #define PFX DRV_NAME ": "
39 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
41 #define BH_DBG(fmt, args...) do {} while (0)
44 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
57 struct work_struct work
;
65 struct gpio_keys_button_data
{
66 struct delayed_work work
;
74 extern u64
uevent_next_seqnum(void);
76 #define BH_MAP(_code, _name) \
82 static struct bh_map button_map
[] = {
83 BH_MAP(BTN_0
, "BTN_0"),
84 BH_MAP(BTN_1
, "BTN_1"),
85 BH_MAP(BTN_2
, "BTN_2"),
86 BH_MAP(BTN_3
, "BTN_3"),
87 BH_MAP(BTN_4
, "BTN_4"),
88 BH_MAP(BTN_5
, "BTN_5"),
89 BH_MAP(BTN_6
, "BTN_6"),
90 BH_MAP(BTN_7
, "BTN_7"),
91 BH_MAP(BTN_8
, "BTN_8"),
92 BH_MAP(BTN_9
, "BTN_9"),
93 BH_MAP(KEY_RESTART
, "reset"),
94 BH_MAP(KEY_RFKILL
, "rfkill"),
95 BH_MAP(KEY_WPS_BUTTON
, "wps"),
98 /* -------------------------------------------------------------------------*/
100 static int bh_event_add_var(struct bh_event
*event
, int argv
,
101 const char *format
, ...)
103 static char buf
[128];
111 va_start(args
, format
);
112 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
115 if (len
>= sizeof(buf
)) {
116 BH_ERR("buffer size too small\n");
121 s
= skb_put(event
->skb
, len
+ 1);
124 BH_DBG("added variable '%s'\n", s
);
129 static int button_hotplug_fill_event(struct bh_event
*event
)
134 ret
= bh_event_add_var(event
, 0, "HOME=%s", "/");
138 ret
= bh_event_add_var(event
, 0, "PATH=%s",
139 "/sbin:/bin:/usr/sbin:/usr/bin");
143 switch (event
->type
) {
153 ret
= bh_event_add_var(event
, 0, "SUBSYSTEM=%s", s
);
157 ret
= bh_event_add_var(event
, 0, "ACTION=%s", event
->action
);
161 ret
= bh_event_add_var(event
, 0, "BUTTON=%s", event
->name
);
165 ret
= bh_event_add_var(event
, 0, "SEEN=%ld", event
->seen
);
169 ret
= bh_event_add_var(event
, 0, "SEQNUM=%llu", uevent_next_seqnum());
174 static void button_hotplug_work(struct work_struct
*work
)
176 struct bh_event
*event
= container_of(work
, struct bh_event
, work
);
179 event
->skb
= alloc_skb(BH_SKB_SIZE
, GFP_KERNEL
);
183 ret
= bh_event_add_var(event
, 0, "%s@", event
->action
);
187 ret
= button_hotplug_fill_event(event
);
191 NETLINK_CB(event
->skb
).dst_group
= 1;
192 broadcast_uevent(event
->skb
, 0, 1, GFP_KERNEL
);
196 BH_ERR("work error %d\n", ret
);
197 kfree_skb(event
->skb
);
203 static int button_hotplug_create_event(const char *name
, unsigned int type
,
204 unsigned long seen
, int pressed
)
206 struct bh_event
*event
;
208 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
209 name
, seen
, pressed
);
211 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
218 event
->action
= pressed
? "pressed" : "released";
220 INIT_WORK(&event
->work
, (void *)(void *)button_hotplug_work
);
221 schedule_work(&event
->work
);
226 /* -------------------------------------------------------------------------*/
228 #ifdef CONFIG_HOTPLUG
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
, unsigned int code
, int value
)
243 struct bh_priv
*priv
= &data
->bh
;
244 unsigned long seen
= jiffies
;
247 BH_DBG("event type=%u, code=%u, value=%d\n", type
, code
, value
);
249 if ((type
!= EV_KEY
) && (type
!= EV_SW
))
252 btn
= button_get_index(code
);
256 button_hotplug_create_event(button_map
[btn
].name
, type
,
257 (seen
- priv
->seen
) / HZ
, value
);
261 static void button_hotplug_event(struct gpio_keys_button_data
*data
,
262 unsigned int type
, unsigned int code
, int value
)
265 #endif /* CONFIG_HOTPLUG */
267 struct gpio_keys_polled_dev
{
268 struct delayed_work work
;
271 struct gpio_keys_platform_data
*pdata
;
272 struct gpio_keys_button_data data
[0];
275 static int gpio_button_get_value(struct gpio_keys_button
*button
,
276 struct gpio_keys_button_data
*bdata
)
280 if (bdata
->can_sleep
)
281 val
= !!gpio_get_value_cansleep(button
->gpio
);
283 val
= !!gpio_get_value(button
->gpio
);
285 return val
^ button
->active_low
;
288 static void gpio_keys_polled_check_state(struct gpio_keys_button
*button
,
289 struct gpio_keys_button_data
*bdata
)
291 int state
= gpio_button_get_value(button
, bdata
);
293 if (state
!= bdata
->last_state
) {
294 unsigned int type
= button
->type
?: EV_KEY
;
296 if (bdata
->count
< bdata
->threshold
) {
301 if (bdata
->last_state
!= -1)
302 button_hotplug_event(bdata
, type
, button
->code
, state
);
304 bdata
->last_state
= state
;
310 static void gpio_keys_polled_queue_work(struct gpio_keys_polled_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_polled_dev
*bdev
=
323 container_of(work
, struct gpio_keys_polled_dev
, work
.work
);
324 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
327 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
328 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
329 gpio_keys_polled_check_state(&pdata
->buttons
[i
], bdata
);
331 gpio_keys_polled_queue_work(bdev
);
334 static void gpio_keys_polled_open(struct gpio_keys_polled_dev
*bdev
)
336 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
340 pdata
->enable(bdev
->dev
);
342 /* report initial state of the buttons */
343 for (i
= 0; i
< pdata
->nbuttons
; i
++)
344 gpio_keys_polled_check_state(&pdata
->buttons
[i
], &bdev
->data
[i
]);
346 gpio_keys_polled_queue_work(bdev
);
350 static struct gpio_keys_platform_data
*
351 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
353 struct device_node
*node
, *pp
;
354 struct gpio_keys_platform_data
*pdata
;
355 struct gpio_keys_button
*button
;
364 nbuttons
= of_get_child_count(node
);
368 pdata
= kzalloc(sizeof(*pdata
) + nbuttons
* (sizeof *button
),
375 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
376 pdata
->nbuttons
= nbuttons
;
378 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
379 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
382 for_each_child_of_node(node
, pp
) {
383 enum of_gpio_flags flags
;
385 if (!of_find_property(pp
, "gpios", NULL
)) {
387 dev_warn(dev
, "Found button without gpios\n");
391 button
= &pdata
->buttons
[i
++];
393 button
->gpio
= of_get_gpio_flags(pp
, 0, &flags
);
394 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
396 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
397 dev_err(dev
, "Button without keycode: 0x%x\n",
403 button
->desc
= of_get_property(pp
, "label", NULL
);
405 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
406 button
->type
= EV_KEY
;
408 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
410 if (of_property_read_u32(pp
, "debounce-interval",
411 &button
->debounce_interval
))
412 button
->debounce_interval
= 5;
415 if (pdata
->nbuttons
== 0) {
425 return ERR_PTR(error
);
428 static struct of_device_id gpio_keys_polled_of_match
[] = {
429 { .compatible
= "gpio-keys-polled", },
432 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
436 static inline struct gpio_keys_platform_data
*
437 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
443 static void gpio_keys_polled_close(struct gpio_keys_polled_dev
*bdev
)
445 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
447 cancel_delayed_work_sync(&bdev
->work
);
450 pdata
->disable(bdev
->dev
);
453 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
455 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
456 struct device
*dev
= &pdev
->dev
;
457 struct gpio_keys_polled_dev
*bdev
;
462 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
464 return PTR_ERR(pdata
);
466 dev_err(dev
, "missing platform data\n");
471 if (!pdata
->poll_interval
) {
472 dev_err(dev
, "missing poll_interval value\n");
477 bdev
= kzalloc(sizeof(struct gpio_keys_polled_dev
) +
478 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
481 dev_err(dev
, "no memory for private data\n");
485 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
486 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
487 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
488 unsigned int gpio
= button
->gpio
;
490 if (button
->wakeup
) {
491 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
496 error
= gpio_request(gpio
,
497 button
->desc
? button
->desc
: DRV_NAME
);
499 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
504 error
= gpio_direction_input(gpio
);
507 "unable to set direction on gpio %u, err=%d\n",
512 bdata
->can_sleep
= gpio_cansleep(gpio
);
513 bdata
->last_state
= -1;
514 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
515 pdata
->poll_interval
);
518 bdev
->dev
= &pdev
->dev
;
520 platform_set_drvdata(pdev
, bdev
);
522 INIT_DELAYED_WORK(&bdev
->work
, gpio_keys_polled_poll
);
524 gpio_keys_polled_open(bdev
);
530 gpio_free(pdata
->buttons
[i
].gpio
);
533 platform_set_drvdata(pdev
, NULL
);
536 /* If we have no platform_data, we allocated pdata dynamically. */
537 if (!dev_get_platdata(&pdev
->dev
))
543 static int gpio_keys_polled_remove(struct platform_device
*pdev
)
545 struct gpio_keys_polled_dev
*bdev
= platform_get_drvdata(pdev
);
546 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
547 int i
= pdata
->nbuttons
;
549 gpio_keys_polled_close(bdev
);
552 gpio_free(pdata
->buttons
[i
].gpio
);
555 platform_set_drvdata(pdev
, NULL
);
560 static struct platform_driver gpio_keys_polled_driver
= {
561 .probe
= gpio_keys_polled_probe
,
562 .remove
= gpio_keys_polled_remove
,
565 .owner
= THIS_MODULE
,
566 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
570 static int __init
gpio_keys_polled_init(void)
572 return platform_driver_register(&gpio_keys_polled_driver
);
575 static void __exit
gpio_keys_polled_exit(void)
577 platform_driver_unregister(&gpio_keys_polled_driver
);
580 module_init(gpio_keys_polled_init
);
581 module_exit(gpio_keys_polled_exit
);
583 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
584 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
585 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
586 MODULE_LICENSE("GPL v2");
587 MODULE_ALIAS("platform:" DRV_NAME
);