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
{
33 struct gpio_desc
*gpiod
;
39 struct gpio_keys_polled_dev
{
40 struct input_polled_dev
*poll_dev
;
42 const struct gpio_keys_platform_data
*pdata
;
43 unsigned long rel_axis_seen
[BITS_TO_LONGS(REL_CNT
)];
44 unsigned long abs_axis_seen
[BITS_TO_LONGS(ABS_CNT
)];
45 struct gpio_keys_button_data data
[0];
48 static void gpio_keys_button_event(struct input_polled_dev
*dev
,
49 const struct gpio_keys_button
*button
,
52 struct gpio_keys_polled_dev
*bdev
= dev
->private;
53 struct input_dev
*input
= dev
->input
;
54 unsigned int type
= button
->type
?: EV_KEY
;
58 input_event(input
, type
, button
->code
, button
->value
);
59 __set_bit(button
->code
, bdev
->rel_axis_seen
);
61 } else if (type
== EV_ABS
) {
63 input_event(input
, type
, button
->code
, button
->value
);
64 __set_bit(button
->code
, bdev
->abs_axis_seen
);
67 input_event(input
, type
, button
->code
, state
);
72 static void gpio_keys_polled_check_state(struct input_polled_dev
*dev
,
73 const struct gpio_keys_button
*button
,
74 struct gpio_keys_button_data
*bdata
)
78 state
= gpiod_get_value_cansleep(bdata
->gpiod
);
80 dev_err(dev
->input
->dev
.parent
,
81 "failed to get gpio state: %d\n", state
);
83 gpio_keys_button_event(dev
, button
, state
);
85 if (state
!= bdata
->last_state
) {
87 bdata
->last_state
= state
;
92 static void gpio_keys_polled_poll(struct input_polled_dev
*dev
)
94 struct gpio_keys_polled_dev
*bdev
= dev
->private;
95 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
96 struct input_dev
*input
= dev
->input
;
99 memset(bdev
->rel_axis_seen
, 0, sizeof(bdev
->rel_axis_seen
));
100 memset(bdev
->abs_axis_seen
, 0, sizeof(bdev
->abs_axis_seen
));
102 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
103 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
105 if (bdata
->count
< bdata
->threshold
) {
107 gpio_keys_button_event(dev
, &pdata
->buttons
[i
],
110 gpio_keys_polled_check_state(dev
, &pdata
->buttons
[i
],
115 for_each_set_bit(i
, input
->relbit
, REL_CNT
) {
116 if (!test_bit(i
, bdev
->rel_axis_seen
))
117 input_event(input
, EV_REL
, i
, 0);
120 for_each_set_bit(i
, input
->absbit
, ABS_CNT
) {
121 if (!test_bit(i
, bdev
->abs_axis_seen
))
122 input_event(input
, EV_ABS
, i
, 0);
128 static void gpio_keys_polled_open(struct input_polled_dev
*dev
)
130 struct gpio_keys_polled_dev
*bdev
= dev
->private;
131 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
134 pdata
->enable(bdev
->dev
);
137 static void gpio_keys_polled_close(struct input_polled_dev
*dev
)
139 struct gpio_keys_polled_dev
*bdev
= dev
->private;
140 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
143 pdata
->disable(bdev
->dev
);
146 static struct gpio_keys_platform_data
*
147 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
149 struct gpio_keys_platform_data
*pdata
;
150 struct gpio_keys_button
*button
;
151 struct fwnode_handle
*child
;
154 nbuttons
= device_get_child_node_count(dev
);
156 return ERR_PTR(-EINVAL
);
158 pdata
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* sizeof(*button
),
161 return ERR_PTR(-ENOMEM
);
163 button
= (struct gpio_keys_button
*)(pdata
+ 1);
165 pdata
->buttons
= button
;
166 pdata
->nbuttons
= nbuttons
;
168 pdata
->rep
= device_property_present(dev
, "autorepeat");
169 device_property_read_u32(dev
, "poll-interval", &pdata
->poll_interval
);
171 device_for_each_child_node(dev
, child
) {
172 if (fwnode_property_read_u32(child
, "linux,code",
174 dev_err(dev
, "button without keycode\n");
175 fwnode_handle_put(child
);
176 return ERR_PTR(-EINVAL
);
179 fwnode_property_read_string(child
, "label", &button
->desc
);
181 if (fwnode_property_read_u32(child
, "linux,input-type",
183 button
->type
= EV_KEY
;
185 if (fwnode_property_read_u32(child
, "linux,input-value",
186 (u32
*)&button
->value
))
190 fwnode_property_read_bool(child
, "wakeup-source") ||
192 fwnode_property_read_bool(child
, "gpio-key,wakeup");
194 if (fwnode_property_read_u32(child
, "debounce-interval",
195 &button
->debounce_interval
))
196 button
->debounce_interval
= 5;
204 static void gpio_keys_polled_set_abs_params(struct input_dev
*input
,
205 const struct gpio_keys_platform_data
*pdata
, unsigned int code
)
207 int i
, min
= 0, max
= 0;
209 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
210 const struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
212 if (button
->type
!= EV_ABS
|| button
->code
!= code
)
215 if (button
->value
< min
)
217 if (button
->value
> max
)
221 input_set_abs_params(input
, code
, min
, max
, 0, 0);
224 static const struct of_device_id gpio_keys_polled_of_match
[] = {
225 { .compatible
= "gpio-keys-polled", },
228 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
230 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
232 struct device
*dev
= &pdev
->dev
;
233 struct fwnode_handle
*child
= NULL
;
234 const struct gpio_keys_platform_data
*pdata
= dev_get_platdata(dev
);
235 struct gpio_keys_polled_dev
*bdev
;
236 struct input_polled_dev
*poll_dev
;
237 struct input_dev
*input
;
243 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
245 return PTR_ERR(pdata
);
248 if (!pdata
->poll_interval
) {
249 dev_err(dev
, "missing poll_interval value\n");
253 size
= sizeof(struct gpio_keys_polled_dev
) +
254 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
);
255 bdev
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
257 dev_err(dev
, "no memory for private data\n");
261 poll_dev
= devm_input_allocate_polled_device(dev
);
263 dev_err(dev
, "no memory for polled device\n");
267 poll_dev
->private = bdev
;
268 poll_dev
->poll
= gpio_keys_polled_poll
;
269 poll_dev
->poll_interval
= pdata
->poll_interval
;
270 poll_dev
->open
= gpio_keys_polled_open
;
271 poll_dev
->close
= gpio_keys_polled_close
;
273 input
= poll_dev
->input
;
275 input
->name
= pdev
->name
;
276 input
->phys
= DRV_NAME
"/input0";
278 input
->id
.bustype
= BUS_HOST
;
279 input
->id
.vendor
= 0x0001;
280 input
->id
.product
= 0x0001;
281 input
->id
.version
= 0x0100;
283 __set_bit(EV_KEY
, input
->evbit
);
285 __set_bit(EV_REP
, input
->evbit
);
287 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
288 const struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
289 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
290 unsigned int type
= button
->type
?: EV_KEY
;
292 if (button
->wakeup
) {
293 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
294 fwnode_handle_put(child
);
298 if (!dev_get_platdata(dev
)) {
299 /* No legacy static platform data */
300 child
= device_get_next_child_node(dev
, child
);
302 dev_err(dev
, "missing child device node\n");
306 bdata
->gpiod
= devm_fwnode_get_gpiod_from_child(dev
,
310 if (IS_ERR(bdata
->gpiod
)) {
311 error
= PTR_ERR(bdata
->gpiod
);
312 if (error
!= -EPROBE_DEFER
)
314 "failed to get gpio: %d\n",
316 fwnode_handle_put(child
);
319 } else if (gpio_is_valid(button
->gpio
)) {
321 * Legacy GPIO number so request the GPIO here and
322 * convert it to descriptor.
324 unsigned flags
= GPIOF_IN
;
326 if (button
->active_low
)
327 flags
|= GPIOF_ACTIVE_LOW
;
329 error
= devm_gpio_request_one(dev
, button
->gpio
,
330 flags
, button
->desc
? : DRV_NAME
);
333 "unable to claim gpio %u, err=%d\n",
334 button
->gpio
, error
);
338 bdata
->gpiod
= gpio_to_desc(button
->gpio
);
341 "unable to convert gpio %u to descriptor\n",
347 bdata
->last_state
= -1;
348 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
349 pdata
->poll_interval
);
351 input_set_capability(input
, type
, button
->code
);
353 gpio_keys_polled_set_abs_params(input
, pdata
,
357 fwnode_handle_put(child
);
359 bdev
->poll_dev
= poll_dev
;
363 error
= input_register_polled_device(poll_dev
);
365 dev_err(dev
, "unable to register polled device, err=%d\n",
370 /* report initial state of the buttons */
371 for (i
= 0; i
< pdata
->nbuttons
; i
++)
372 gpio_keys_polled_check_state(poll_dev
, &pdata
->buttons
[i
],
380 static struct platform_driver gpio_keys_polled_driver
= {
381 .probe
= gpio_keys_polled_probe
,
384 .of_match_table
= gpio_keys_polled_of_match
,
387 module_platform_driver(gpio_keys_polled_driver
);
389 MODULE_LICENSE("GPL v2");
390 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
391 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
392 MODULE_ALIAS("platform:" DRV_NAME
);