1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for buttons on GPIO lines not capable of generating interrupts
5 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
8 * This file was based on: /drivers/input/misc/cobalt_btns.c
9 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
11 * also was based on: /drivers/input/keyboard/gpio_keys.c
12 * Copyright 2005 Phil Blundell
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/input.h>
19 #include <linux/ioport.h>
20 #include <linux/platform_device.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio_keys.h>
24 #include <linux/property.h>
26 #define DRV_NAME "gpio-keys-polled"
28 struct gpio_keys_button_data
{
29 struct gpio_desc
*gpiod
;
35 struct gpio_keys_polled_dev
{
36 struct input_dev
*input
;
38 const struct gpio_keys_platform_data
*pdata
;
39 unsigned long rel_axis_seen
[BITS_TO_LONGS(REL_CNT
)];
40 unsigned long abs_axis_seen
[BITS_TO_LONGS(ABS_CNT
)];
41 struct gpio_keys_button_data data
[];
44 static void gpio_keys_button_event(struct input_dev
*input
,
45 const struct gpio_keys_button
*button
,
48 struct gpio_keys_polled_dev
*bdev
= input_get_drvdata(input
);
49 unsigned int type
= button
->type
?: EV_KEY
;
53 input_event(input
, type
, button
->code
, button
->value
);
54 __set_bit(button
->code
, bdev
->rel_axis_seen
);
56 } else if (type
== EV_ABS
) {
58 input_event(input
, type
, button
->code
, button
->value
);
59 __set_bit(button
->code
, bdev
->abs_axis_seen
);
62 input_event(input
, type
, button
->code
, state
);
67 static void gpio_keys_polled_check_state(struct input_dev
*input
,
68 const struct gpio_keys_button
*button
,
69 struct gpio_keys_button_data
*bdata
)
73 state
= gpiod_get_value_cansleep(bdata
->gpiod
);
75 dev_err(input
->dev
.parent
,
76 "failed to get gpio state: %d\n", state
);
78 gpio_keys_button_event(input
, button
, state
);
80 if (state
!= bdata
->last_state
) {
82 bdata
->last_state
= state
;
87 static void gpio_keys_polled_poll(struct input_dev
*input
)
89 struct gpio_keys_polled_dev
*bdev
= input_get_drvdata(input
);
90 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
93 memset(bdev
->rel_axis_seen
, 0, sizeof(bdev
->rel_axis_seen
));
94 memset(bdev
->abs_axis_seen
, 0, sizeof(bdev
->abs_axis_seen
));
96 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
97 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
99 if (bdata
->count
< bdata
->threshold
) {
101 gpio_keys_button_event(input
, &pdata
->buttons
[i
],
104 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
109 for_each_set_bit(i
, input
->relbit
, REL_CNT
) {
110 if (!test_bit(i
, bdev
->rel_axis_seen
))
111 input_event(input
, EV_REL
, i
, 0);
114 for_each_set_bit(i
, input
->absbit
, ABS_CNT
) {
115 if (!test_bit(i
, bdev
->abs_axis_seen
))
116 input_event(input
, EV_ABS
, i
, 0);
122 static int gpio_keys_polled_open(struct input_dev
*input
)
124 struct gpio_keys_polled_dev
*bdev
= input_get_drvdata(input
);
125 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
128 pdata
->enable(bdev
->dev
);
133 static void gpio_keys_polled_close(struct input_dev
*input
)
135 struct gpio_keys_polled_dev
*bdev
= input_get_drvdata(input
);
136 const struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
139 pdata
->disable(bdev
->dev
);
142 static struct gpio_keys_platform_data
*
143 gpio_keys_polled_get_devtree_pdata(struct device
*dev
)
145 struct gpio_keys_platform_data
*pdata
;
146 struct gpio_keys_button
*button
;
149 nbuttons
= device_get_child_node_count(dev
);
151 return ERR_PTR(-EINVAL
);
153 pdata
= devm_kzalloc(dev
, sizeof(*pdata
) + nbuttons
* sizeof(*button
),
156 return ERR_PTR(-ENOMEM
);
158 button
= (struct gpio_keys_button
*)(pdata
+ 1);
160 pdata
->buttons
= button
;
161 pdata
->nbuttons
= nbuttons
;
163 pdata
->rep
= device_property_present(dev
, "autorepeat");
164 device_property_read_u32(dev
, "poll-interval", &pdata
->poll_interval
);
166 device_property_read_string(dev
, "label", &pdata
->name
);
168 device_for_each_child_node_scoped(dev
, child
) {
169 if (fwnode_property_read_u32(child
, "linux,code",
171 dev_err(dev
, "button without keycode\n");
172 return ERR_PTR(-EINVAL
);
175 fwnode_property_read_string(child
, "label", &button
->desc
);
177 if (fwnode_property_read_u32(child
, "linux,input-type",
179 button
->type
= EV_KEY
;
181 if (fwnode_property_read_u32(child
, "linux,input-value",
182 (u32
*)&button
->value
))
186 fwnode_property_read_bool(child
, "wakeup-source") ||
188 fwnode_property_read_bool(child
, "gpio-key,wakeup");
190 if (fwnode_property_read_u32(child
, "debounce-interval",
191 &button
->debounce_interval
))
192 button
->debounce_interval
= 5;
200 static void gpio_keys_polled_set_abs_params(struct input_dev
*input
,
201 const struct gpio_keys_platform_data
*pdata
, unsigned int code
)
203 int i
, min
= 0, max
= 0;
205 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
206 const struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
208 if (button
->type
!= EV_ABS
|| button
->code
!= code
)
211 if (button
->value
< min
)
213 if (button
->value
> max
)
217 input_set_abs_params(input
, code
, min
, max
, 0, 0);
220 static const struct of_device_id gpio_keys_polled_of_match
[] = {
221 { .compatible
= "gpio-keys-polled", },
224 MODULE_DEVICE_TABLE(of
, gpio_keys_polled_of_match
);
226 static int gpio_keys_polled_probe(struct platform_device
*pdev
)
228 struct device
*dev
= &pdev
->dev
;
229 struct fwnode_handle
*child
= NULL
;
230 const struct gpio_keys_platform_data
*pdata
= dev_get_platdata(dev
);
231 struct gpio_keys_polled_dev
*bdev
;
232 struct input_dev
*input
;
237 pdata
= gpio_keys_polled_get_devtree_pdata(dev
);
239 return PTR_ERR(pdata
);
242 if (!pdata
->poll_interval
) {
243 dev_err(dev
, "missing poll_interval value\n");
247 bdev
= devm_kzalloc(dev
, struct_size(bdev
, data
, pdata
->nbuttons
),
250 dev_err(dev
, "no memory for private data\n");
254 input
= devm_input_allocate_device(dev
);
256 dev_err(dev
, "no memory for input device\n");
260 input_set_drvdata(input
, bdev
);
262 input
->name
= pdata
->name
?: pdev
->name
;
263 input
->phys
= DRV_NAME
"/input0";
265 input
->id
.bustype
= BUS_HOST
;
266 input
->id
.vendor
= 0x0001;
267 input
->id
.product
= 0x0001;
268 input
->id
.version
= 0x0100;
270 input
->open
= gpio_keys_polled_open
;
271 input
->close
= gpio_keys_polled_close
;
273 __set_bit(EV_KEY
, input
->evbit
);
275 __set_bit(EV_REP
, input
->evbit
);
277 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
278 const struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
279 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
280 unsigned int type
= button
->type
?: EV_KEY
;
282 if (button
->wakeup
) {
283 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
284 fwnode_handle_put(child
);
288 if (!dev_get_platdata(dev
)) {
289 /* No legacy static platform data */
290 child
= device_get_next_child_node(dev
, child
);
292 dev_err(dev
, "missing child device node\n");
296 bdata
->gpiod
= devm_fwnode_gpiod_get(dev
, child
,
299 if (IS_ERR(bdata
->gpiod
)) {
300 fwnode_handle_put(child
);
301 return dev_err_probe(dev
, PTR_ERR(bdata
->gpiod
),
302 "failed to get gpio\n");
304 } else if (gpio_is_valid(button
->gpio
)) {
306 * Legacy GPIO number so request the GPIO here and
307 * convert it to descriptor.
309 unsigned flags
= GPIOF_IN
;
311 if (button
->active_low
)
312 flags
|= GPIOF_ACTIVE_LOW
;
314 error
= devm_gpio_request_one(dev
, button
->gpio
,
315 flags
, button
->desc
? : DRV_NAME
);
317 return dev_err_probe(dev
, error
,
318 "unable to claim gpio %u\n",
321 bdata
->gpiod
= gpio_to_desc(button
->gpio
);
324 "unable to convert gpio %u to descriptor\n",
330 bdata
->last_state
= -1;
331 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
332 pdata
->poll_interval
);
334 input_set_capability(input
, type
, button
->code
);
336 gpio_keys_polled_set_abs_params(input
, pdata
,
340 fwnode_handle_put(child
);
346 error
= input_setup_polling(input
, gpio_keys_polled_poll
);
348 dev_err(dev
, "unable to set up polling, err=%d\n", error
);
352 input_set_poll_interval(input
, pdata
->poll_interval
);
354 error
= input_register_device(input
);
356 dev_err(dev
, "unable to register polled device, err=%d\n",
361 /* report initial state of the buttons */
362 for (i
= 0; i
< pdata
->nbuttons
; i
++)
363 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
371 static struct platform_driver gpio_keys_polled_driver
= {
372 .probe
= gpio_keys_polled_probe
,
375 .of_match_table
= gpio_keys_polled_of_match
,
378 module_platform_driver(gpio_keys_polled_driver
);
380 MODULE_LICENSE("GPL v2");
381 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
382 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
383 MODULE_ALIAS("platform:" DRV_NAME
);