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 )
56 struct work_struct work
;
64 struct gpio_keys_button_data
{
65 struct delayed_work work
;
73 extern u64
uevent_next_seqnum(void);
75 #define BH_MAP(_code, _name) \
81 static struct bh_map button_map
[] = {
82 BH_MAP(BTN_0
, "BTN_0"),
83 BH_MAP(BTN_1
, "BTN_1"),
84 BH_MAP(BTN_2
, "BTN_2"),
85 BH_MAP(BTN_3
, "BTN_3"),
86 BH_MAP(BTN_4
, "BTN_4"),
87 BH_MAP(BTN_5
, "BTN_5"),
88 BH_MAP(BTN_6
, "BTN_6"),
89 BH_MAP(BTN_7
, "BTN_7"),
90 BH_MAP(BTN_8
, "BTN_8"),
91 BH_MAP(BTN_9
, "BTN_9"),
92 BH_MAP(KEY_RESTART
, "reset"),
93 BH_MAP(KEY_RFKILL
, "rfkill"),
95 BH_MAP(KEY_WPS_BUTTON
, "wps"),
96 #endif /* KEY_WPS_BUTTON */
99 /* -------------------------------------------------------------------------*/
101 static int bh_event_add_var(struct bh_event
*event
, int argv
,
102 const char *format
, ...)
104 static char buf
[128];
112 va_start(args
, format
);
113 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
116 if (len
>= sizeof(buf
)) {
117 BH_ERR("buffer size too small\n");
122 s
= skb_put(event
->skb
, len
+ 1);
125 BH_DBG("added variable '%s'\n", s
);
130 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 ret
= bh_event_add_var(event
, 0, "SUBSYSTEM=%s", "button");
147 ret
= bh_event_add_var(event
, 0, "ACTION=%s", event
->action
);
151 ret
= bh_event_add_var(event
, 0, "BUTTON=%s", event
->name
);
155 ret
= bh_event_add_var(event
, 0, "SEEN=%ld", event
->seen
);
159 ret
= bh_event_add_var(event
, 0, "SEQNUM=%llu", uevent_next_seqnum());
164 static void button_hotplug_work(struct work_struct
*work
)
166 struct bh_event
*event
= container_of(work
, struct bh_event
, work
);
169 event
->skb
= alloc_skb(BH_SKB_SIZE
, GFP_KERNEL
);
173 ret
= bh_event_add_var(event
, 0, "%s@", event
->action
);
177 ret
= button_hotplug_fill_event(event
);
181 NETLINK_CB(event
->skb
).dst_group
= 1;
182 broadcast_uevent(event
->skb
, 0, 1, GFP_KERNEL
);
186 BH_ERR("work error %d\n", ret
);
187 kfree_skb(event
->skb
);
193 static int button_hotplug_create_event(const char *name
, unsigned long seen
,
196 struct bh_event
*event
;
198 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
199 name
, seen
, pressed
);
201 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
207 event
->action
= pressed
? "pressed" : "released";
209 INIT_WORK(&event
->work
, (void *)(void *)button_hotplug_work
);
210 schedule_work(&event
->work
);
215 /* -------------------------------------------------------------------------*/
217 #ifdef CONFIG_HOTPLUG
218 static int button_get_index(unsigned int code
)
222 for (i
= 0; i
< ARRAY_SIZE(button_map
); i
++)
223 if (button_map
[i
].code
== code
)
228 static void button_hotplug_event(struct gpio_keys_button_data
*data
,
229 unsigned int type
, unsigned int code
, int value
)
231 struct bh_priv
*priv
= &data
->bh
;
232 unsigned long seen
= jiffies
;
235 BH_DBG("event type=%u, code=%u, value=%d\n", type
, code
, value
);
240 btn
= button_get_index(code
);
244 button_hotplug_create_event(button_map
[btn
].name
,
245 (seen
- priv
->seen
) / HZ
, value
);
249 static void button_hotplug_event(struct gpio_keys_button_data
*data
,
250 unsigned int type
, unsigned int code
, int value
)
253 #endif /* CONFIG_HOTPLUG */
255 struct gpio_keys_polled_dev
{
256 struct delayed_work work
;
259 struct gpio_keys_platform_data
*pdata
;
260 struct gpio_keys_button_data data
[0];
263 static void gpio_keys_polled_check_state(struct gpio_keys_button
*button
,
264 struct gpio_keys_button_data
*bdata
)
268 if (bdata
->can_sleep
)
269 state
= !!gpio_get_value_cansleep(button
->gpio
);
271 state
= !!gpio_get_value(button
->gpio
);
273 state
= !!(state
^ button
->active_low
);
274 if (state
!= bdata
->last_state
) {
275 unsigned int type
= button
->type
?: EV_KEY
;
277 button_hotplug_event(bdata
, type
, button
->code
, state
);
279 bdata
->last_state
= state
;
283 static void gpio_keys_polled_queue_work(struct gpio_keys_polled_dev
*bdev
)
285 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
286 unsigned long delay
= msecs_to_jiffies(pdata
->poll_interval
);
289 delay
= round_jiffies_relative(delay
);
290 schedule_delayed_work(&bdev
->work
, delay
);
293 static void gpio_keys_polled_poll(struct work_struct
*work
)
295 struct gpio_keys_polled_dev
*bdev
=
296 container_of(work
, struct gpio_keys_polled_dev
, work
.work
);
297 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
300 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
301 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
303 if (bdata
->count
< bdata
->threshold
)
306 gpio_keys_polled_check_state(&pdata
->buttons
[i
], bdata
);
308 gpio_keys_polled_queue_work(bdev
);
311 static void gpio_keys_polled_open(struct gpio_keys_polled_dev
*bdev
)
313 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
317 pdata
->enable(bdev
->dev
);
319 /* report initial state of the buttons */
320 for (i
= 0; i
< pdata
->nbuttons
; i
++)
321 gpio_keys_polled_check_state(&pdata
->buttons
[i
], &bdev
->data
[i
]);
323 gpio_keys_polled_queue_work(bdev
);
327 static struct gpio_keys_platform_data
*
328 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
330 struct device_node
*node
, *pp
;
331 struct gpio_keys_platform_data
*pdata
;
332 struct gpio_keys_button
*button
;
341 nbuttons
= of_get_child_count(node
);
345 pdata
= kzalloc(sizeof(*pdata
) + nbuttons
* (sizeof *button
),
352 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
353 pdata
->nbuttons
= nbuttons
;
355 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
356 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
359 for_each_child_of_node(node
, pp
) {
360 enum of_gpio_flags flags
;
362 if (!of_find_property(pp
, "gpios", NULL
)) {
364 dev_warn(dev
, "Found button without gpios\n");
368 button
= &pdata
->buttons
[i
++];
370 button
->gpio
= of_get_gpio_flags(pp
, 0, &flags
);
371 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
373 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
374 dev_err(dev
, "Button without keycode: 0x%x\n",
380 button
->desc
= of_get_property(pp
, "label", NULL
);
382 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
383 button
->type
= EV_KEY
;
385 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
387 if (of_property_read_u32(pp
, "debounce-interval",
388 &button
->debounce_interval
))
389 button
->debounce_interval
= 5;
392 if (pdata
->nbuttons
== 0) {
402 return ERR_PTR(error
);
405 static struct of_device_id gpio_keys_polled_of_match
[] = {
406 { .compatible
= "gpio-keys-polled", },
409 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
413 static inline struct gpio_keys_platform_data
*
414 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
420 static void gpio_keys_polled_close(struct gpio_keys_polled_dev
*bdev
)
422 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
424 cancel_delayed_work_sync(&bdev
->work
);
427 pdata
->disable(bdev
->dev
);
430 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
432 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
433 struct device
*dev
= &pdev
->dev
;
434 struct gpio_keys_polled_dev
*bdev
;
439 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
441 return PTR_ERR(pdata
);
443 dev_err(dev
, "missing platform data\n");
448 if (!pdata
->poll_interval
) {
449 dev_err(dev
, "missing poll_interval value\n");
454 bdev
= kzalloc(sizeof(struct gpio_keys_polled_dev
) +
455 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
458 dev_err(dev
, "no memory for private data\n");
462 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
463 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
464 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
465 unsigned int gpio
= button
->gpio
;
467 if (button
->wakeup
) {
468 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
473 error
= gpio_request(gpio
,
474 button
->desc
? button
->desc
: DRV_NAME
);
476 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
481 error
= gpio_direction_input(gpio
);
484 "unable to set direction on gpio %u, err=%d\n",
489 bdata
->can_sleep
= gpio_cansleep(gpio
);
490 bdata
->last_state
= 0;
491 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
492 pdata
->poll_interval
);
495 bdev
->dev
= &pdev
->dev
;
497 platform_set_drvdata(pdev
, bdev
);
499 INIT_DELAYED_WORK(&bdev
->work
, gpio_keys_polled_poll
);
501 gpio_keys_polled_open(bdev
);
507 gpio_free(pdata
->buttons
[i
].gpio
);
510 platform_set_drvdata(pdev
, NULL
);
513 /* If we have no platform_data, we allocated pdata dynamically. */
514 if (!dev_get_platdata(&pdev
->dev
))
520 static int gpio_keys_polled_remove(struct platform_device
*pdev
)
522 struct gpio_keys_polled_dev
*bdev
= platform_get_drvdata(pdev
);
523 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
524 int i
= pdata
->nbuttons
;
526 gpio_keys_polled_close(bdev
);
529 gpio_free(pdata
->buttons
[i
].gpio
);
532 platform_set_drvdata(pdev
, NULL
);
537 static struct platform_driver gpio_keys_polled_driver
= {
538 .probe
= gpio_keys_polled_probe
,
539 .remove
= gpio_keys_polled_remove
,
542 .owner
= THIS_MODULE
,
543 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
547 static int __init
gpio_keys_polled_init(void)
549 return platform_driver_register(&gpio_keys_polled_driver
);
552 static void __exit
gpio_keys_polled_exit(void)
554 platform_driver_unregister(&gpio_keys_polled_driver
);
557 module_init(gpio_keys_polled_init
);
558 module_exit(gpio_keys_polled_exit
);
560 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
561 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
562 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
563 MODULE_LICENSE("GPL v2");
564 MODULE_ALIAS("platform:" DRV_NAME
);