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
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* sizeof(*button
),
126 return ERR_PTR(-ENOMEM
);
128 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
129 pdata
->nbuttons
= nbuttons
;
131 pdata
->rep
= !!of_get_property(node
, "autorepeat", NULL
);
132 of_property_read_u32(node
, "poll-interval", &pdata
->poll_interval
);
135 for_each_child_of_node(node
, pp
) {
137 enum of_gpio_flags flags
;
139 if (!of_find_property(pp
, "gpios", NULL
)) {
141 dev_warn(dev
, "Found button without gpios\n");
145 gpio
= of_get_gpio_flags(pp
, 0, &flags
);
148 if (error
!= -EPROBE_DEFER
)
150 "Failed to get gpio flags, error: %d\n",
152 return ERR_PTR(error
);
155 button
= &pdata
->buttons
[i
++];
158 button
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
160 if (of_property_read_u32(pp
, "linux,code", &button
->code
)) {
161 dev_err(dev
, "Button without keycode: 0x%x\n",
163 return ERR_PTR(-EINVAL
);
166 button
->desc
= of_get_property(pp
, "label", NULL
);
168 if (of_property_read_u32(pp
, "linux,input-type", &button
->type
))
169 button
->type
= EV_KEY
;
171 button
->wakeup
= !!of_get_property(pp
, "gpio-key,wakeup", NULL
);
173 if (of_property_read_u32(pp
, "debounce-interval",
174 &button
->debounce_interval
))
175 button
->debounce_interval
= 5;
178 if (pdata
->nbuttons
== 0)
179 return ERR_PTR(-EINVAL
);
184 static const struct of_device_id gpio_keys_polled_of_match
[] = {
185 { .compatible
= "gpio-keys-polled", },
188 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
192 static inline struct gpio_keys_platform_data
*
193 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
199 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
201 struct device
*dev
= &pdev
->dev
;
202 const struct gpio_keys_platform_data
*pdata
= dev_get_platdata(dev
);
203 struct gpio_keys_polled_dev
*bdev
;
204 struct input_polled_dev
*poll_dev
;
205 struct input_dev
*input
;
211 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
213 return PTR_ERR(pdata
);
215 dev_err(dev
, "missing platform data\n");
220 if (!pdata
->poll_interval
) {
221 dev_err(dev
, "missing poll_interval value\n");
225 size
= sizeof(struct gpio_keys_polled_dev
) +
226 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
);
227 bdev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
229 dev_err(dev
, "no memory for private data\n");
233 poll_dev
= devm_input_allocate_polled_device(&pdev
->dev
);
235 dev_err(dev
, "no memory for polled device\n");
239 poll_dev
->private = bdev
;
240 poll_dev
->poll
= gpio_keys_polled_poll
;
241 poll_dev
->poll_interval
= pdata
->poll_interval
;
242 poll_dev
->open
= gpio_keys_polled_open
;
243 poll_dev
->close
= gpio_keys_polled_close
;
245 input
= poll_dev
->input
;
247 input
->name
= pdev
->name
;
248 input
->phys
= DRV_NAME
"/input0";
250 input
->id
.bustype
= BUS_HOST
;
251 input
->id
.vendor
= 0x0001;
252 input
->id
.product
= 0x0001;
253 input
->id
.version
= 0x0100;
255 __set_bit(EV_KEY
, input
->evbit
);
257 __set_bit(EV_REP
, input
->evbit
);
259 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
260 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
261 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
262 unsigned int gpio
= button
->gpio
;
263 unsigned int type
= button
->type
?: EV_KEY
;
265 if (button
->wakeup
) {
266 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
270 error
= devm_gpio_request_one(&pdev
->dev
, gpio
, GPIOF_IN
,
271 button
->desc
? : DRV_NAME
);
273 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
278 bdata
->can_sleep
= gpio_cansleep(gpio
);
279 bdata
->last_state
= -1;
280 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
281 pdata
->poll_interval
);
283 input_set_capability(input
, type
, button
->code
);
286 bdev
->poll_dev
= poll_dev
;
289 platform_set_drvdata(pdev
, bdev
);
291 error
= input_register_polled_device(poll_dev
);
293 dev_err(dev
, "unable to register polled device, err=%d\n",
298 /* report initial state of the buttons */
299 for (i
= 0; i
< pdata
->nbuttons
; i
++)
300 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
306 static struct platform_driver gpio_keys_polled_driver
= {
307 .probe
= gpio_keys_polled_probe
,
310 .owner
= THIS_MODULE
,
311 .of_match_table
= of_match_ptr(gpio_keys_polled_of_match
),
314 module_platform_driver(gpio_keys_polled_driver
);
316 MODULE_LICENSE("GPL v2");
317 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
318 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
319 MODULE_ALIAS("platform:" DRV_NAME
);