Skip to content

Commit 5feeca3

Browse files
geertudtor
authored andcommitted
Input: gpio_keys - 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. Based on commits 633a21d ("input: gpio_keys_polled: Add support for GPIO descriptors") and 1ae5ddb ("Input: gpio_keys_polled - request GPIO pin as input."). Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Tested-by: Mika Westerberg <mika.westerberg@linux.intel.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 0860913 commit 5feeca3

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

drivers/input/keyboard/gpio_keys.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/gpio_keys.h>
2727
#include <linux/workqueue.h>
2828
#include <linux/gpio.h>
29+
#include <linux/gpio/consumer.h>
2930
#include <linux/of.h>
3031
#include <linux/of_platform.h>
3132
#include <linux/of_gpio.h>
@@ -35,6 +36,7 @@
3536
struct gpio_button_data {
3637
const struct gpio_keys_button *button;
3738
struct input_dev *input;
39+
struct gpio_desc *gpiod;
3840

3941
struct timer_list release_timer;
4042
unsigned int release_delay; /* in msecs, for IRQ-only buttons */
@@ -140,7 +142,7 @@ static void gpio_keys_disable_button(struct gpio_button_data *bdata)
140142
*/
141143
disable_irq(bdata->irq);
142144

143-
if (gpio_is_valid(bdata->button->gpio))
145+
if (bdata->gpiod)
144146
cancel_delayed_work_sync(&bdata->work);
145147
else
146148
del_timer_sync(&bdata->release_timer);
@@ -358,19 +360,20 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
358360
const struct gpio_keys_button *button = bdata->button;
359361
struct input_dev *input = bdata->input;
360362
unsigned int type = button->type ?: EV_KEY;
361-
int state = gpio_get_value_cansleep(button->gpio);
363+
int state;
362364

365+
state = gpiod_get_value_cansleep(bdata->gpiod);
363366
if (state < 0) {
364-
dev_err(input->dev.parent, "failed to get gpio state\n");
367+
dev_err(input->dev.parent,
368+
"failed to get gpio state: %d\n", state);
365369
return;
366370
}
367371

368-
state = (state ? 1 : 0) ^ button->active_low;
369372
if (type == EV_ABS) {
370373
if (state)
371374
input_event(input, type, button->code, button->value);
372375
} else {
373-
input_event(input, type, button->code, !!state);
376+
input_event(input, type, button->code, state);
374377
}
375378
input_sync(input);
376379
}
@@ -456,7 +459,7 @@ static void gpio_keys_quiesce_key(void *data)
456459
{
457460
struct gpio_button_data *bdata = data;
458461

459-
if (gpio_is_valid(bdata->button->gpio))
462+
if (bdata->gpiod)
460463
cancel_delayed_work_sync(&bdata->work);
461464
else
462465
del_timer_sync(&bdata->release_timer);
@@ -478,18 +481,30 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
478481
bdata->button = button;
479482
spin_lock_init(&bdata->lock);
480483

484+
/*
485+
* Legacy GPIO number, so request the GPIO here and
486+
* convert it to descriptor.
487+
*/
481488
if (gpio_is_valid(button->gpio)) {
489+
unsigned flags = GPIOF_IN;
490+
491+
if (button->active_low)
492+
flags |= GPIOF_ACTIVE_LOW;
482493

483-
error = devm_gpio_request_one(&pdev->dev, button->gpio,
484-
GPIOF_IN, desc);
494+
error = devm_gpio_request_one(&pdev->dev, button->gpio, flags,
495+
desc);
485496
if (error < 0) {
486497
dev_err(dev, "Failed to request GPIO %d, error %d\n",
487498
button->gpio, error);
488499
return error;
489500
}
490501

502+
bdata->gpiod = gpio_to_desc(button->gpio);
503+
if (!bdata->gpiod)
504+
return -EINVAL;
505+
491506
if (button->debounce_interval) {
492-
error = gpio_set_debounce(button->gpio,
507+
error = gpiod_set_debounce(bdata->gpiod,
493508
button->debounce_interval * 1000);
494509
/* use timer if gpiolib doesn't provide debounce */
495510
if (error < 0)
@@ -500,7 +515,7 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
500515
if (button->irq) {
501516
bdata->irq = button->irq;
502517
} else {
503-
irq = gpio_to_irq(button->gpio);
518+
irq = gpiod_to_irq(bdata->gpiod);
504519
if (irq < 0) {
505520
error = irq;
506521
dev_err(dev,
@@ -575,7 +590,7 @@ static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
575590

576591
for (i = 0; i < ddata->pdata->nbuttons; i++) {
577592
struct gpio_button_data *bdata = &ddata->data[i];
578-
if (gpio_is_valid(bdata->button->gpio))
593+
if (bdata->gpiod)
579594
gpio_keys_gpio_report_event(bdata);
580595
}
581596
input_sync(input);

0 commit comments

Comments
 (0)