2 * Driver for buttons on GPIO lines not capable of generating interrupts
4 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
7 * This file was based on: /drivers/input/misc/cobalt_btns.c
8 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
10 * also was based on: /drivers/input/keyboard/gpio_keys.c
11 * Copyright 2005 Phil Blundell
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/input-polldev.h>
23 #include <linux/ioport.h>
24 #include <linux/platform_device.h>
25 #include <linux/gpio.h>
26 #include <linux/gpio_keys.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_gpio.h>
31 #define DRV_NAME "gpio-keys-polled"
33 struct gpio_keys_button_data
{
40 struct gpio_keys_polled_dev
{
41 struct input_polled_dev
*poll_dev
;
43 const struct gpio_keys_platform_data
*pdata
;
44 struct gpio_keys_button_data data
[0];
47 static void gpio_keys_polled_check_state(struct input_dev
*input
,
48 struct gpio_keys_button
*button
,
49 struct gpio_keys_button_data
*bdata
)
54 state
= !!gpio_get_value_cansleep(button
->gpio
);
56 state
= !!gpio_get_value(button
->gpio
);
58 if (state
!= bdata
->last_state
) {
59 unsigned int type
= button
->type
?: EV_KEY
;
61 input_event(input
, type
, button
->code
,
62 !!(state
^ button
->active_low
));
65 bdata
->last_state
= state
;
69 static void gpio_keys_polled_poll(struct input_polled_dev
*dev
)
71 struct gpio_keys_polled_dev
*bdev
= dev
->private;
72 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
73 struct input_dev
*input
= dev
->input
;
76 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
77 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
79 if (bdata
->count
< bdata
->threshold
)
82 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
87 static void gpio_keys_polled_open(struct input_polled_dev
*dev
)
89 struct gpio_keys_polled_dev
*bdev
= dev
->private;
90 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
93 pdata
->enable(bdev
->dev
);
96 static void gpio_keys_polled_close(struct input_polled_dev
*dev
)
98 struct gpio_keys_polled_dev
*bdev
= dev
->private;
99 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
102 pdata
->disable(bdev
->dev
);
106 static struct gpio_keys_platform_data
*gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
108 struct device_node
*node
, *pp
;
109 struct gpio_keys_platform_data
*pdata
;
110 struct gpio_keys_button
*button
;
119 nbuttons
= of_get_child_count(node
);
123 pdata
= kzalloc(sizeof(*pdata
) + nbuttons
* (sizeof *button
),
130 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
131 pdata
->nbuttons
= nbuttons
;
133 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
134 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
137 for_each_child_of_node(node
, pp
) {
139 enum of_gpio_flags flags
;
141 if (!of_find_property(pp
, "gpios", NULL
)) {
143 dev_warn(dev
, "Found button without gpios\n");
147 gpio
= of_get_gpio_flags(pp
, 0, &flags
);
150 if (error
!= -EPROBE_DEFER
)
152 "Failed to get gpio flags, error: %d\n",
157 button
= &pdata
->buttons
[i
++];
160 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
162 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
163 dev_err(dev
, "Button without keycode: 0x%x\n",
169 button
->desc
= of_get_property(pp
, "label", NULL
);
171 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
172 button
->type
= EV_KEY
;
174 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
176 if (of_property_read_u32(pp
, "debounce-interval",
177 &button
->debounce_interval
))
178 button
->debounce_interval
= 5;
181 if (pdata
->nbuttons
== 0) {
191 return ERR_PTR(error
);
194 static struct of_device_id gpio_keys_polled_of_match
[] = {
195 { .compatible
= "gpio-keys-polled", },
198 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
202 static inline struct gpio_keys_platform_data
*
203 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
209 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
211 struct device
*dev
= &pdev
->dev
;
212 const struct gpio_keys_platform_data
*pdata
= dev_get_platdata(dev
);
213 struct gpio_keys_polled_dev
*bdev
;
214 struct input_polled_dev
*poll_dev
;
215 struct input_dev
*input
;
220 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
222 return PTR_ERR(pdata
);
224 dev_err(dev
, "missing platform data\n");
229 if (!pdata
->poll_interval
) {
230 dev_err(dev
, "missing poll_interval value\n");
235 bdev
= kzalloc(sizeof(struct gpio_keys_polled_dev
) +
236 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
239 dev_err(dev
, "no memory for private data\n");
244 poll_dev
= input_allocate_polled_device();
246 dev_err(dev
, "no memory for polled device\n");
251 poll_dev
->private = bdev
;
252 poll_dev
->poll
= gpio_keys_polled_poll
;
253 poll_dev
->poll_interval
= pdata
->poll_interval
;
254 poll_dev
->open
= gpio_keys_polled_open
;
255 poll_dev
->close
= gpio_keys_polled_close
;
257 input
= poll_dev
->input
;
259 input
->name
= pdev
->name
;
260 input
->phys
= DRV_NAME
"/input0";
261 input
->dev
.parent
= &pdev
->dev
;
263 input
->id
.bustype
= BUS_HOST
;
264 input
->id
.vendor
= 0x0001;
265 input
->id
.product
= 0x0001;
266 input
->id
.version
= 0x0100;
268 __set_bit(EV_KEY
, input
->evbit
);
270 __set_bit(EV_REP
, input
->evbit
);
272 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
273 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
274 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
275 unsigned int gpio
= button
->gpio
;
276 unsigned int type
= button
->type
?: EV_KEY
;
278 if (button
->wakeup
) {
279 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
284 error
= gpio_request_one(gpio
, GPIOF_IN
,
285 button
->desc
?: DRV_NAME
);
287 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
292 bdata
->can_sleep
= gpio_cansleep(gpio
);
293 bdata
->last_state
= -1;
294 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
295 pdata
->poll_interval
);
297 input_set_capability(input
, type
, button
->code
);
300 bdev
->poll_dev
= poll_dev
;
303 platform_set_drvdata(pdev
, bdev
);
305 error
= input_register_polled_device(poll_dev
);
307 dev_err(dev
, "unable to register polled device, err=%d\n",
312 /* report initial state of the buttons */
313 for (i
= 0; i
< pdata
->nbuttons
; i
++)
314 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
321 gpio_free(pdata
->buttons
[i
].gpio
);
323 input_free_polled_device(poll_dev
);
329 /* If we have no platform_data, we allocated pdata dynamically. */
330 if (!dev_get_platdata(&pdev
->dev
))
336 static int gpio_keys_polled_remove(struct platform_device
*pdev
)
338 struct gpio_keys_polled_dev
*bdev
= platform_get_drvdata(pdev
);
339 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
342 input_unregister_polled_device(bdev
->poll_dev
);
344 for (i
= 0; i
< pdata
->nbuttons
; i
++)
345 gpio_free(pdata
->buttons
[i
].gpio
);
347 input_free_polled_device(bdev
->poll_dev
);
350 * If we had no platform_data, we allocated pdata dynamically and
353 if (!dev_get_platdata(&pdev
->dev
))
361 static struct platform_driver gpio_keys_polled_driver
= {
362 .probe
= gpio_keys_polled_probe
,
363 .remove
= gpio_keys_polled_remove
,
366 .owner
= THIS_MODULE
,
367 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
370 module_platform_driver(gpio_keys_polled_driver
);
372 MODULE_LICENSE("GPL v2");
373 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
374 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
375 MODULE_ALIAS("platform:" DRV_NAME
);