Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Aaron Lu and committed by
Rafael J. Wysocki
633a21d8 5c51277a

+30 -12
+27 -12
drivers/input/keyboard/gpio_keys_polled.c
··· 23 23 #include <linux/ioport.h> 24 24 #include <linux/platform_device.h> 25 25 #include <linux/gpio.h> 26 + #include <linux/gpio/consumer.h> 26 27 #include <linux/gpio_keys.h> 27 28 #include <linux/of.h> 28 29 #include <linux/of_platform.h> ··· 52 51 int state; 53 52 54 53 if (bdata->can_sleep) 55 - state = !!gpio_get_value_cansleep(button->gpio); 54 + state = !!gpiod_get_value_cansleep(button->gpiod); 56 55 else 57 - state = !!gpio_get_value(button->gpio); 56 + state = !!gpiod_get_value(button->gpiod); 58 57 59 58 if (state != bdata->last_state) { 60 59 unsigned int type = button->type ?: EV_KEY; 61 60 62 - input_event(input, type, button->code, 63 - !!(state ^ button->active_low)); 61 + input_event(input, type, button->code, state); 64 62 input_sync(input); 65 63 bdata->count = 0; 66 64 bdata->last_state = state; ··· 259 259 for (i = 0; i < pdata->nbuttons; i++) { 260 260 struct gpio_keys_button *button = &pdata->buttons[i]; 261 261 struct gpio_keys_button_data *bdata = &bdev->data[i]; 262 - unsigned int gpio = button->gpio; 263 262 unsigned int type = button->type ?: EV_KEY; 264 263 265 264 if (button->wakeup) { ··· 266 267 return -EINVAL; 267 268 } 268 269 269 - error = devm_gpio_request_one(&pdev->dev, gpio, GPIOF_IN, 270 - button->desc ? : DRV_NAME); 271 - if (error) { 272 - dev_err(dev, "unable to claim gpio %u, err=%d\n", 273 - gpio, error); 274 - return error; 270 + /* 271 + * Legacy GPIO number so request the GPIO here and 272 + * convert it to descriptor. 273 + */ 274 + if (!button->gpiod && gpio_is_valid(button->gpio)) { 275 + unsigned flags = 0; 276 + 277 + if (button->active_low) 278 + flags |= GPIOF_ACTIVE_LOW; 279 + 280 + error = devm_gpio_request_one(&pdev->dev, button->gpio, 281 + flags, button->desc ? : DRV_NAME); 282 + if (error) { 283 + dev_err(dev, "unable to claim gpio %u, err=%d\n", 284 + button->gpio, error); 285 + return error; 286 + } 287 + 288 + button->gpiod = gpio_to_desc(button->gpio); 275 289 } 276 290 277 - bdata->can_sleep = gpio_cansleep(gpio); 291 + if (IS_ERR(button->gpiod)) 292 + return PTR_ERR(button->gpiod); 293 + 294 + bdata->can_sleep = gpiod_cansleep(button->gpiod); 278 295 bdata->last_state = -1; 279 296 bdata->threshold = DIV_ROUND_UP(button->debounce_interval, 280 297 pdata->poll_interval);
+3
include/linux/gpio_keys.h
··· 2 2 #define _GPIO_KEYS_H 3 3 4 4 struct device; 5 + struct gpio_desc; 5 6 6 7 /** 7 8 * struct gpio_keys_button - configuration parameters ··· 18 17 * disable button via sysfs 19 18 * @value: axis value for %EV_ABS 20 19 * @irq: Irq number in case of interrupt keys 20 + * @gpiod: GPIO descriptor 21 21 */ 22 22 struct gpio_keys_button { 23 23 unsigned int code; ··· 31 29 bool can_disable; 32 30 int value; 33 31 unsigned int irq; 32 + struct gpio_desc *gpiod; 34 33 }; 35 34 36 35 /**