aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeert Uytterhoeven <geert+renesas@glider.be>2017-04-04 15:32:22 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-04-12 06:41:19 -0400
commitdb7c1706fa6d9d1c2f2ab651814130b60bca26d1 (patch)
tree4cc3214eaa4b2d115681573ff9227c17649079d0
parent80b0d7e623d10d4bc9bad2a35d6ce18e08454b3d (diff)
Input: gpio_keys - add support for GPIO descriptors
[ Upstream commit 5feeca3c1e39c01f9ef5abc94dea94021ccf94fc ] 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 633a21d80b4a2cd6 ("input: gpio_keys_polled: Add support for GPIO descriptors") and 1ae5ddb6f8837558 ("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> Signed-off-by: Sasha Levin <alexander.levin@verizon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/input/keyboard/gpio_keys.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 29093657f2ef..9b8079ca0fb4 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -26,6 +26,7 @@
26#include <linux/gpio_keys.h> 26#include <linux/gpio_keys.h>
27#include <linux/workqueue.h> 27#include <linux/workqueue.h>
28#include <linux/gpio.h> 28#include <linux/gpio.h>
29#include <linux/gpio/consumer.h>
29#include <linux/of.h> 30#include <linux/of.h>
30#include <linux/of_platform.h> 31#include <linux/of_platform.h>
31#include <linux/of_gpio.h> 32#include <linux/of_gpio.h>
@@ -35,6 +36,7 @@
35struct gpio_button_data { 36struct gpio_button_data {
36 const struct gpio_keys_button *button; 37 const struct gpio_keys_button *button;
37 struct input_dev *input; 38 struct input_dev *input;
39 struct gpio_desc *gpiod;
38 40
39 struct timer_list release_timer; 41 struct timer_list release_timer;
40 unsigned int release_delay; /* in msecs, for IRQ-only buttons */ 42 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)
140 */ 142 */
141 disable_irq(bdata->irq); 143 disable_irq(bdata->irq);
142 144
143 if (gpio_is_valid(bdata->button->gpio)) 145 if (bdata->gpiod)
144 cancel_delayed_work_sync(&bdata->work); 146 cancel_delayed_work_sync(&bdata->work);
145 else 147 else
146 del_timer_sync(&bdata->release_timer); 148 del_timer_sync(&bdata->release_timer);
@@ -358,19 +360,20 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
358 const struct gpio_keys_button *button = bdata->button; 360 const struct gpio_keys_button *button = bdata->button;
359 struct input_dev *input = bdata->input; 361 struct input_dev *input = bdata->input;
360 unsigned int type = button->type ?: EV_KEY; 362 unsigned int type = button->type ?: EV_KEY;
361 int state = gpio_get_value_cansleep(button->gpio); 363 int state;
362 364
365 state = gpiod_get_value_cansleep(bdata->gpiod);
363 if (state < 0) { 366 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);
365 return; 369 return;
366 } 370 }
367 371
368 state = (state ? 1 : 0) ^ button->active_low;
369 if (type == EV_ABS) { 372 if (type == EV_ABS) {
370 if (state) 373 if (state)
371 input_event(input, type, button->code, button->value); 374 input_event(input, type, button->code, button->value);
372 } else { 375 } else {
373 input_event(input, type, button->code, !!state); 376 input_event(input, type, button->code, state);
374 } 377 }
375 input_sync(input); 378 input_sync(input);
376} 379}
@@ -456,7 +459,7 @@ static void gpio_keys_quiesce_key(void *data)
456{ 459{
457 struct gpio_button_data *bdata = data; 460 struct gpio_button_data *bdata = data;
458 461
459 if (gpio_is_valid(bdata->button->gpio)) 462 if (bdata->gpiod)
460 cancel_delayed_work_sync(&bdata->work); 463 cancel_delayed_work_sync(&bdata->work);
461 else 464 else
462 del_timer_sync(&bdata->release_timer); 465 del_timer_sync(&bdata->release_timer);
@@ -478,18 +481,30 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
478 bdata->button = button; 481 bdata->button = button;
479 spin_lock_init(&bdata->lock); 482 spin_lock_init(&bdata->lock);
480 483
484 /*
485 * Legacy GPIO number, so request the GPIO here and
486 * convert it to descriptor.
487 */
481 if (gpio_is_valid(button->gpio)) { 488 if (gpio_is_valid(button->gpio)) {
489 unsigned flags = GPIOF_IN;
490
491 if (button->active_low)
492 flags |= GPIOF_ACTIVE_LOW;
482 493
483 error = devm_gpio_request_one(&pdev->dev, button->gpio, 494 error = devm_gpio_request_one(&pdev->dev, button->gpio, flags,
484 GPIOF_IN, desc); 495 desc);
485 if (error < 0) { 496 if (error < 0) {
486 dev_err(dev, "Failed to request GPIO %d, error %d\n", 497 dev_err(dev, "Failed to request GPIO %d, error %d\n",
487 button->gpio, error); 498 button->gpio, error);
488 return error; 499 return error;
489 } 500 }
490 501
502 bdata->gpiod = gpio_to_desc(button->gpio);
503 if (!bdata->gpiod)
504 return -EINVAL;
505
491 if (button->debounce_interval) { 506 if (button->debounce_interval) {
492 error = gpio_set_debounce(button->gpio, 507 error = gpiod_set_debounce(bdata->gpiod,
493 button->debounce_interval * 1000); 508 button->debounce_interval * 1000);
494 /* use timer if gpiolib doesn't provide debounce */ 509 /* use timer if gpiolib doesn't provide debounce */
495 if (error < 0) 510 if (error < 0)
@@ -500,7 +515,7 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
500 if (button->irq) { 515 if (button->irq) {
501 bdata->irq = button->irq; 516 bdata->irq = button->irq;
502 } else { 517 } else {
503 irq = gpio_to_irq(button->gpio); 518 irq = gpiod_to_irq(bdata->gpiod);
504 if (irq < 0) { 519 if (irq < 0) {
505 error = irq; 520 error = irq;
506 dev_err(dev, 521 dev_err(dev,
@@ -575,7 +590,7 @@ static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
575 590
576 for (i = 0; i < ddata->pdata->nbuttons; i++) { 591 for (i = 0; i < ddata->pdata->nbuttons; i++) {
577 struct gpio_button_data *bdata = &ddata->data[i]; 592 struct gpio_button_data *bdata = &ddata->data[i];
578 if (gpio_is_valid(bdata->button->gpio)) 593 if (bdata->gpiod)
579 gpio_keys_gpio_report_event(bdata); 594 gpio_keys_gpio_report_event(bdata);
580 } 595 }
581 input_sync(input); 596 input_sync(input);