Skip to content

Commit 633a21d

Browse files
Aaron Lurafaeljw
authored andcommitted
input: gpio_keys_polled: Add support for GPIO descriptors
GPIO descriptors are the preferred way over legacy GPIO numbers nowadays. Convert the driver to use GPIO descriptors internally but still allow passing legacy GPIO numbers from platform data to support existing platforms. Signed-off-by: Aaron Lu <aaron.lu@intel.com> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com> Acked-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Acked-by: Grant Likely <grant.likely@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 5c51277 commit 633a21d

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

drivers/input/keyboard/gpio_keys_polled.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/ioport.h>
2424
#include <linux/platform_device.h>
2525
#include <linux/gpio.h>
26+
#include <linux/gpio/consumer.h>
2627
#include <linux/gpio_keys.h>
2728
#include <linux/of.h>
2829
#include <linux/of_platform.h>
@@ -51,15 +52,14 @@ static void gpio_keys_polled_check_state(struct input_dev *input,
5152
int state;
5253

5354
if (bdata->can_sleep)
54-
state = !!gpio_get_value_cansleep(button->gpio);
55+
state = !!gpiod_get_value_cansleep(button->gpiod);
5556
else
56-
state = !!gpio_get_value(button->gpio);
57+
state = !!gpiod_get_value(button->gpiod);
5758

5859
if (state != bdata->last_state) {
5960
unsigned int type = button->type ?: EV_KEY;
6061

61-
input_event(input, type, button->code,
62-
!!(state ^ button->active_low));
62+
input_event(input, type, button->code, state);
6363
input_sync(input);
6464
bdata->count = 0;
6565
bdata->last_state = state;
@@ -259,23 +259,38 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
259259
for (i = 0; i < pdata->nbuttons; i++) {
260260
struct gpio_keys_button *button = &pdata->buttons[i];
261261
struct gpio_keys_button_data *bdata = &bdev->data[i];
262-
unsigned int gpio = button->gpio;
263262
unsigned int type = button->type ?: EV_KEY;
264263

265264
if (button->wakeup) {
266265
dev_err(dev, DRV_NAME " does not support wakeup\n");
267266
return -EINVAL;
268267
}
269268

270-
error = devm_gpio_request_one(&pdev->dev, gpio, GPIOF_IN,
271-
button->desc ? : DRV_NAME);
272-
if (error) {
273-
dev_err(dev, "unable to claim gpio %u, err=%d\n",
274-
gpio, error);
275-
return error;
269+
/*
270+
* Legacy GPIO number so request the GPIO here and
271+
* convert it to descriptor.
272+
*/
273+
if (!button->gpiod && gpio_is_valid(button->gpio)) {
274+
unsigned flags = 0;
275+
276+
if (button->active_low)
277+
flags |= GPIOF_ACTIVE_LOW;
278+
279+
error = devm_gpio_request_one(&pdev->dev, button->gpio,
280+
flags, button->desc ? : DRV_NAME);
281+
if (error) {
282+
dev_err(dev, "unable to claim gpio %u, err=%d\n",
283+
button->gpio, error);
284+
return error;
285+
}
286+
287+
button->gpiod = gpio_to_desc(button->gpio);
276288
}
277289

278-
bdata->can_sleep = gpio_cansleep(gpio);
290+
if (IS_ERR(button->gpiod))
291+
return PTR_ERR(button->gpiod);
292+
293+
bdata->can_sleep = gpiod_cansleep(button->gpiod);
279294
bdata->last_state = -1;
280295
bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
281296
pdata->poll_interval);

include/linux/gpio_keys.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _GPIO_KEYS_H
33

44
struct device;
5+
struct gpio_desc;
56

67
/**
78
* struct gpio_keys_button - configuration parameters
@@ -17,6 +18,7 @@ struct device;
1718
* disable button via sysfs
1819
* @value: axis value for %EV_ABS
1920
* @irq: Irq number in case of interrupt keys
21+
* @gpiod: GPIO descriptor
2022
*/
2123
struct gpio_keys_button {
2224
unsigned int code;
@@ -29,6 +31,7 @@ struct gpio_keys_button {
2931
bool can_disable;
3032
int value;
3133
unsigned int irq;
34+
struct gpio_desc *gpiod;
3235
};
3336

3437
/**

0 commit comments

Comments
 (0)