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/consumer.h>
27 #include <linux/gpio_keys.h>
28 #include <linux/property.h>
30 #define DRV_NAME "gpio-keys-polled"
32 struct gpio_keys_button_data
{
39 struct gpio_keys_polled_dev
{
40 struct input_polled_dev
*poll_dev
;
42 const struct gpio_keys_platform_data
*pdata
;
43 struct gpio_keys_button_data data
[0];
46 static void gpio_keys_polled_check_state(struct input_dev
*input
,
47 struct gpio_keys_button
*button
,
48 struct gpio_keys_button_data
*bdata
)
53 state
= !!gpiod_get_value_cansleep(button
->gpiod
);
55 state
= !!gpiod_get_value(button
->gpiod
);
57 if (state
!= bdata
->last_state
) {
58 unsigned int type
= button
->type
?: EV_KEY
;
60 input_event(input
, type
, button
->code
, state
);
63 bdata
->last_state
= state
;
67 static void gpio_keys_polled_poll(struct input_polled_dev
*dev
)
69 struct gpio_keys_polled_dev
*bdev
= dev
->private;
70 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
71 struct input_dev
*input
= dev
->input
;
74 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
75 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
77 if (bdata
->count
< bdata
->threshold
)
80 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
85 static void gpio_keys_polled_open(struct input_polled_dev
*dev
)
87 struct gpio_keys_polled_dev
*bdev
= dev
->private;
88 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
91 pdata
->enable(bdev
->dev
);
94 static void gpio_keys_polled_close(struct input_polled_dev
*dev
)
96 struct gpio_keys_polled_dev
*bdev
= dev
->private;
97 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
100 pdata
->disable(bdev
->dev
);
103 static struct gpio_keys_platform_data
*gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
105 struct gpio_keys_platform_data
*pdata
;
106 struct gpio_keys_button
*button
;
107 struct fwnode_handle
*child
;
111 nbuttons
= device_get_child_node_count(dev
);
115 pdata
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* sizeof(*button
),
118 return ERR_PTR(-ENOMEM
);
120 pdata
->buttons
= (struct gpio_keys_button
*)(pdata
+ 1);
122 pdata
->rep
= device_property_present(dev
, "autorepeat");
123 device_property_read_u32(dev
, "poll-interval", &pdata
->poll_interval
);
125 device_for_each_child_node(dev
, child
) {
126 struct gpio_desc
*desc
;
128 desc
= devm_get_gpiod_from_child(dev
, NULL
, child
);
130 error
= PTR_ERR(desc
);
131 if (error
!= -EPROBE_DEFER
)
133 "Failed to get gpio flags, error: %d\n",
135 fwnode_handle_put(child
);
136 return ERR_PTR(error
);
139 button
= &pdata
->buttons
[pdata
->nbuttons
++];
140 button
->gpiod
= desc
;
142 if (fwnode_property_read_u32(child
, "linux,code", &button
->code
)) {
143 dev_err(dev
, "Button without keycode: %d\n",
144 pdata
->nbuttons
- 1);
145 fwnode_handle_put(child
);
146 return ERR_PTR(-EINVAL
);
149 fwnode_property_read_string(child
, "label", &button
->desc
);
151 if (fwnode_property_read_u32(child
, "linux,input-type",
153 button
->type
= EV_KEY
;
155 button
->wakeup
= fwnode_property_present(child
, "gpio-key,wakeup");
157 if (fwnode_property_read_u32(child
, "debounce-interval",
158 &button
->debounce_interval
))
159 button
->debounce_interval
= 5;
162 if (pdata
->nbuttons
== 0)
163 return ERR_PTR(-EINVAL
);
168 static const struct of_device_id gpio_keys_polled_of_match
[] = {
169 { .compatible
= "gpio-keys-polled", },
172 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
174 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
176 struct device
*dev
= &pdev
->dev
;
177 const struct gpio_keys_platform_data
*pdata
= dev_get_platdata(dev
);
178 struct gpio_keys_polled_dev
*bdev
;
179 struct input_polled_dev
*poll_dev
;
180 struct input_dev
*input
;
186 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
188 return PTR_ERR(pdata
);
190 dev_err(dev
, "missing platform data\n");
195 if (!pdata
->poll_interval
) {
196 dev_err(dev
, "missing poll_interval value\n");
200 size
= sizeof(struct gpio_keys_polled_dev
) +
201 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
);
202 bdev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
204 dev_err(dev
, "no memory for private data\n");
208 poll_dev
= devm_input_allocate_polled_device(&pdev
->dev
);
210 dev_err(dev
, "no memory for polled device\n");
214 poll_dev
->private = bdev
;
215 poll_dev
->poll
= gpio_keys_polled_poll
;
216 poll_dev
->poll_interval
= pdata
->poll_interval
;
217 poll_dev
->open
= gpio_keys_polled_open
;
218 poll_dev
->close
= gpio_keys_polled_close
;
220 input
= poll_dev
->input
;
222 input
->name
= pdev
->name
;
223 input
->phys
= DRV_NAME
"/input0";
225 input
->id
.bustype
= BUS_HOST
;
226 input
->id
.vendor
= 0x0001;
227 input
->id
.product
= 0x0001;
228 input
->id
.version
= 0x0100;
230 __set_bit(EV_KEY
, input
->evbit
);
232 __set_bit(EV_REP
, input
->evbit
);
234 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
235 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
236 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
237 unsigned int type
= button
->type
?: EV_KEY
;
239 if (button
->wakeup
) {
240 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
245 * Legacy GPIO number so request the GPIO here and
246 * convert it to descriptor.
248 if (!button
->gpiod
&& gpio_is_valid(button
->gpio
)) {
249 unsigned flags
= GPIOF_IN
;
251 if (button
->active_low
)
252 flags
|= GPIOF_ACTIVE_LOW
;
254 error
= devm_gpio_request_one(&pdev
->dev
, button
->gpio
,
255 flags
, button
->desc
? : DRV_NAME
);
257 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
258 button
->gpio
, error
);
262 button
->gpiod
= gpio_to_desc(button
->gpio
);
265 if (IS_ERR(button
->gpiod
))
266 return PTR_ERR(button
->gpiod
);
268 bdata
->can_sleep
= gpiod_cansleep(button
->gpiod
);
269 bdata
->last_state
= -1;
270 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
271 pdata
->poll_interval
);
273 input_set_capability(input
, type
, button
->code
);
276 bdev
->poll_dev
= poll_dev
;
279 platform_set_drvdata(pdev
, bdev
);
281 error
= input_register_polled_device(poll_dev
);
283 dev_err(dev
, "unable to register polled device, err=%d\n",
288 /* report initial state of the buttons */
289 for (i
= 0; i
< pdata
->nbuttons
; i
++)
290 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
296 static struct platform_driver gpio_keys_polled_driver
= {
297 .probe
= gpio_keys_polled_probe
,
300 .of_match_table
= gpio_keys_polled_of_match
,
303 module_platform_driver(gpio_keys_polled_driver
);
305 MODULE_LICENSE("GPL v2");
306 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
307 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
308 MODULE_ALIAS("platform:" DRV_NAME
);