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_POWER
, "power"),
94 BH_MAP(KEY_RESTART
, "reset"),
95 BH_MAP(KEY_RFKILL
, "rfkill"),
96 BH_MAP(KEY_WPS_BUTTON
, "wps"),
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
)
135 ret
= bh_event_add_var(event
, 0, "HOME=%s", "/");
139 ret
= bh_event_add_var(event
, 0, "PATH=%s",
140 "/sbin:/bin:/usr/sbin:/usr/bin");
144 ret
= bh_event_add_var(event
, 0, "SUBSYSTEM=%s", "button");
148 ret
= bh_event_add_var(event
, 0, "ACTION=%s", event
->action
);
152 ret
= bh_event_add_var(event
, 0, "BUTTON=%s", event
->name
);
156 if (event
->type
== EV_SW
) {
157 ret
= bh_event_add_var(event
, 0, "TYPE=%s", "switch");
162 ret
= bh_event_add_var(event
, 0, "SEEN=%ld", event
->seen
);
166 ret
= bh_event_add_var(event
, 0, "SEQNUM=%llu", uevent_next_seqnum());
171 static void button_hotplug_work(struct work_struct
*work
)
173 struct bh_event
*event
= container_of(work
, struct bh_event
, work
);
176 event
->skb
= alloc_skb(BH_SKB_SIZE
, GFP_KERNEL
);
180 ret
= bh_event_add_var(event
, 0, "%s@", event
->action
);
184 ret
= button_hotplug_fill_event(event
);
188 NETLINK_CB(event
->skb
).dst_group
= 1;
189 broadcast_uevent(event
->skb
, 0, 1, GFP_KERNEL
);
193 BH_ERR("work error %d\n", ret
);
194 kfree_skb(event
->skb
);
200 static int button_hotplug_create_event(const char *name
, unsigned int type
,
201 unsigned long seen
, int pressed
)
203 struct bh_event
*event
;
205 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
206 name
, seen
, pressed
);
208 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
215 event
->action
= pressed
? "pressed" : "released";
217 INIT_WORK(&event
->work
, (void *)(void *)button_hotplug_work
);
218 schedule_work(&event
->work
);
223 /* -------------------------------------------------------------------------*/
225 #ifdef CONFIG_HOTPLUG
226 static int button_get_index(unsigned int code
)
230 for (i
= 0; i
< ARRAY_SIZE(button_map
); i
++)
231 if (button_map
[i
].code
== code
)
237 static void button_hotplug_event(struct gpio_keys_button_data
*data
,
238 unsigned int type
, unsigned int code
, int value
)
240 struct bh_priv
*priv
= &data
->bh
;
241 unsigned long seen
= jiffies
;
244 BH_DBG("event type=%u, code=%u, value=%d\n", type
, code
, value
);
246 if ((type
!= EV_KEY
) && (type
!= EV_SW
))
249 btn
= button_get_index(code
);
253 button_hotplug_create_event(button_map
[btn
].name
, type
,
254 (seen
- priv
->seen
) / HZ
, value
);
258 static void button_hotplug_event(struct gpio_keys_button_data
*data
,
259 unsigned int type
, unsigned int code
, int value
)
262 #endif /* CONFIG_HOTPLUG */
264 struct gpio_keys_polled_dev
{
265 struct delayed_work work
;
268 struct gpio_keys_platform_data
*pdata
;
269 struct gpio_keys_button_data data
[0];
272 static int gpio_button_get_value(struct gpio_keys_button
*button
,
273 struct gpio_keys_button_data
*bdata
)
277 if (bdata
->can_sleep
)
278 val
= !!gpio_get_value_cansleep(button
->gpio
);
280 val
= !!gpio_get_value(button
->gpio
);
282 return val
^ button
->active_low
;
285 static void gpio_keys_polled_check_state(struct gpio_keys_button
*button
,
286 struct gpio_keys_button_data
*bdata
)
288 int state
= gpio_button_get_value(button
, bdata
);
290 if (state
!= bdata
->last_state
) {
291 unsigned int type
= button
->type
?: EV_KEY
;
293 if (bdata
->count
< bdata
->threshold
) {
298 if ((bdata
->last_state
!= -1) || (type
== EV_SW
))
299 button_hotplug_event(bdata
, type
, button
->code
, state
);
301 bdata
->last_state
= state
;
307 static void gpio_keys_polled_queue_work(struct gpio_keys_polled_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_polled_dev
*bdev
=
320 container_of(work
, struct gpio_keys_polled_dev
, work
.work
);
321 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
324 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
325 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
326 gpio_keys_polled_check_state(&pdata
->buttons
[i
], bdata
);
328 gpio_keys_polled_queue_work(bdev
);
331 static void gpio_keys_polled_open(struct gpio_keys_polled_dev
*bdev
)
333 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
337 pdata
->enable(bdev
->dev
);
339 /* report initial state of the buttons */
340 for (i
= 0; i
< pdata
->nbuttons
; i
++)
341 gpio_keys_polled_check_state(&pdata
->buttons
[i
], &bdev
->data
[i
]);
343 gpio_keys_polled_queue_work(bdev
);
347 static struct gpio_keys_platform_data
*
348 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
350 struct device_node
*node
, *pp
;
351 struct gpio_keys_platform_data
*pdata
;
352 struct gpio_keys_button
*button
;
361 nbuttons
= of_get_child_count(node
);
365 pdata
= kzalloc(sizeof(*pdata
) + nbuttons
* (sizeof *button
),
372 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
373 pdata
->nbuttons
= nbuttons
;
375 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
376 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
379 for_each_child_of_node(node
, pp
) {
380 enum of_gpio_flags flags
;
382 if (!of_find_property(pp
, "gpios", NULL
)) {
384 dev_warn(dev
, "Found button without gpios\n");
388 button
= &pdata
->buttons
[i
++];
390 button
->gpio
= of_get_gpio_flags(pp
, 0, &flags
);
391 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
393 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
394 dev_err(dev
, "Button without keycode: 0x%x\n",
400 button
->desc
= of_get_property(pp
, "label", NULL
);
402 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
403 button
->type
= EV_KEY
;
405 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
407 if (of_property_read_u32(pp
, "debounce-interval",
408 &button
->debounce_interval
))
409 button
->debounce_interval
= 5;
412 if (pdata
->nbuttons
== 0) {
422 return ERR_PTR(error
);
425 static struct of_device_id gpio_keys_polled_of_match
[] = {
426 { .compatible
= "gpio-keys-polled", },
429 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
433 static inline struct gpio_keys_platform_data
*
434 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
440 static void gpio_keys_polled_close(struct gpio_keys_polled_dev
*bdev
)
442 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
444 cancel_delayed_work_sync(&bdev
->work
);
447 pdata
->disable(bdev
->dev
);
450 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
452 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
453 struct device
*dev
= &pdev
->dev
;
454 struct gpio_keys_polled_dev
*bdev
;
459 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
461 return PTR_ERR(pdata
);
463 dev_err(dev
, "missing platform data\n");
468 if (!pdata
->poll_interval
) {
469 dev_err(dev
, "missing poll_interval value\n");
474 bdev
= kzalloc(sizeof(struct gpio_keys_polled_dev
) +
475 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
478 dev_err(dev
, "no memory for private data\n");
482 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
483 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
484 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
485 unsigned int gpio
= button
->gpio
;
487 if (button
->wakeup
) {
488 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
493 error
= gpio_request(gpio
,
494 button
->desc
? button
->desc
: DRV_NAME
);
496 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
501 error
= gpio_direction_input(gpio
);
504 "unable to set direction on gpio %u, err=%d\n",
509 bdata
->can_sleep
= gpio_cansleep(gpio
);
510 bdata
->last_state
= -1;
511 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
512 pdata
->poll_interval
);
515 bdev
->dev
= &pdev
->dev
;
517 platform_set_drvdata(pdev
, bdev
);
519 INIT_DELAYED_WORK(&bdev
->work
, gpio_keys_polled_poll
);
521 gpio_keys_polled_open(bdev
);
527 gpio_free(pdata
->buttons
[i
].gpio
);
530 platform_set_drvdata(pdev
, NULL
);
533 /* If we have no platform_data, we allocated pdata dynamically. */
534 if (!dev_get_platdata(&pdev
->dev
))
540 static int gpio_keys_polled_remove(struct platform_device
*pdev
)
542 struct gpio_keys_polled_dev
*bdev
= platform_get_drvdata(pdev
);
543 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
544 int i
= pdata
->nbuttons
;
546 gpio_keys_polled_close(bdev
);
549 gpio_free(pdata
->buttons
[i
].gpio
);
552 platform_set_drvdata(pdev
, NULL
);
557 static struct platform_driver gpio_keys_polled_driver
= {
558 .probe
= gpio_keys_polled_probe
,
559 .remove
= gpio_keys_polled_remove
,
562 .owner
= THIS_MODULE
,
563 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
567 static int __init
gpio_keys_polled_init(void)
569 return platform_driver_register(&gpio_keys_polled_driver
);
572 static void __exit
gpio_keys_polled_exit(void)
574 platform_driver_unregister(&gpio_keys_polled_driver
);
577 module_init(gpio_keys_polled_init
);
578 module_exit(gpio_keys_polled_exit
);
580 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
581 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
582 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
583 MODULE_LICENSE("GPL v2");
584 MODULE_ALIAS("platform:" DRV_NAME
);