Skip to content

Commit 700a38b

Browse files
committed
Input: gpio_keys - switch to using generic device properties
Make use of the device property API in this driver so that both OF based systems and ACPI based systems can use this driver. Suggested-by: Geert Uytterhoeven <geert+renesas@glider.be> Suggested-by: Mika Westerberg <mika.westerberg@linux.intel.com> Tested-by: Mika Westerberg <mika.westerberg@linux.intel.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 5feeca3 commit 700a38b

File tree

1 file changed

+69
-72
lines changed

1 file changed

+69
-72
lines changed

drivers/input/keyboard/gpio_keys.c

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#include <linux/gpio.h>
2929
#include <linux/gpio/consumer.h>
3030
#include <linux/of.h>
31-
#include <linux/of_platform.h>
32-
#include <linux/of_gpio.h>
3331
#include <linux/of_irq.h>
3432
#include <linux/spinlock.h>
3533

@@ -468,7 +466,8 @@ static void gpio_keys_quiesce_key(void *data)
468466
static int gpio_keys_setup_key(struct platform_device *pdev,
469467
struct input_dev *input,
470468
struct gpio_button_data *bdata,
471-
const struct gpio_keys_button *button)
469+
const struct gpio_keys_button *button,
470+
struct fwnode_handle *child)
472471
{
473472
const char *desc = button->desc ? button->desc : "gpio_keys";
474473
struct device *dev = &pdev->dev;
@@ -481,11 +480,28 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
481480
bdata->button = button;
482481
spin_lock_init(&bdata->lock);
483482

484-
/*
485-
* Legacy GPIO number, so request the GPIO here and
486-
* convert it to descriptor.
487-
*/
488-
if (gpio_is_valid(button->gpio)) {
483+
if (child) {
484+
bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child);
485+
if (IS_ERR(bdata->gpiod)) {
486+
error = PTR_ERR(bdata->gpiod);
487+
if (error == -ENOENT) {
488+
/*
489+
* GPIO is optional, we may be dealing with
490+
* purely interrupt-driven setup.
491+
*/
492+
bdata->gpiod = NULL;
493+
} else {
494+
if (error != -EPROBE_DEFER)
495+
dev_err(dev, "failed to get gpio: %d\n",
496+
error);
497+
return error;
498+
}
499+
}
500+
} else if (gpio_is_valid(button->gpio)) {
501+
/*
502+
* Legacy GPIO number, so request the GPIO here and
503+
* convert it to descriptor.
504+
*/
489505
unsigned flags = GPIOF_IN;
490506

491507
if (button->active_low)
@@ -502,7 +518,9 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
502518
bdata->gpiod = gpio_to_desc(button->gpio);
503519
if (!bdata->gpiod)
504520
return -EINVAL;
521+
}
505522

523+
if (bdata->gpiod) {
506524
if (button->debounce_interval) {
507525
error = gpiod_set_debounce(bdata->gpiod,
508526
button->debounce_interval * 1000);
@@ -533,9 +551,10 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
533551

534552
} else {
535553
if (!button->irq) {
536-
dev_err(dev, "No IRQ specified\n");
554+
dev_err(dev, "Found button without gpio or irq\n");
537555
return -EINVAL;
538556
}
557+
539558
bdata->irq = button->irq;
540559

541560
if (button->type && button->type != EV_KEY) {
@@ -627,24 +646,18 @@ static void gpio_keys_close(struct input_dev *input)
627646
* Handlers for alternative sources of platform_data
628647
*/
629648

630-
#ifdef CONFIG_OF
631649
/*
632-
* Translate OpenFirmware node properties into platform_data
650+
* Translate properties into platform_data
633651
*/
634652
static struct gpio_keys_platform_data *
635653
gpio_keys_get_devtree_pdata(struct device *dev)
636654
{
637-
struct device_node *node, *pp;
638655
struct gpio_keys_platform_data *pdata;
639656
struct gpio_keys_button *button;
640-
int error;
657+
struct fwnode_handle *child;
641658
int nbuttons;
642659

643-
node = dev->of_node;
644-
if (!node)
645-
return ERR_PTR(-ENODEV);
646-
647-
nbuttons = of_get_available_child_count(node);
660+
nbuttons = device_get_child_node_count(dev);
648661
if (nbuttons == 0)
649662
return ERR_PTR(-ENODEV);
650663

@@ -659,64 +672,43 @@ gpio_keys_get_devtree_pdata(struct device *dev)
659672
pdata->buttons = button;
660673
pdata->nbuttons = nbuttons;
661674

662-
pdata->rep = !!of_get_property(node, "autorepeat", NULL);
675+
pdata->rep = device_property_read_bool(dev, "autorepeat");
663676

664-
of_property_read_string(node, "label", &pdata->name);
677+
device_property_read_string(dev, "label", &pdata->name);
665678

666-
for_each_available_child_of_node(node, pp) {
667-
enum of_gpio_flags flags;
679+
device_for_each_child_node(dev, child) {
680+
if (is_of_node(child))
681+
button->irq =
682+
irq_of_parse_and_map(to_of_node(child), 0);
668683

669-
button->gpio = of_get_gpio_flags(pp, 0, &flags);
670-
if (button->gpio < 0) {
671-
error = button->gpio;
672-
if (error != -ENOENT) {
673-
if (error != -EPROBE_DEFER)
674-
dev_err(dev,
675-
"Failed to get gpio flags, error: %d\n",
676-
error);
677-
of_node_put(pp);
678-
return ERR_PTR(error);
679-
}
680-
} else {
681-
button->active_low = flags & OF_GPIO_ACTIVE_LOW;
682-
}
683-
684-
button->irq = irq_of_parse_and_map(pp, 0);
685-
686-
if (!gpio_is_valid(button->gpio) && !button->irq) {
687-
dev_err(dev, "Found button without gpios or irqs\n");
688-
of_node_put(pp);
689-
return ERR_PTR(-EINVAL);
690-
}
691-
692-
if (of_property_read_u32(pp, "linux,code", &button->code)) {
693-
dev_err(dev, "Button without keycode: 0x%x\n",
694-
button->gpio);
695-
of_node_put(pp);
684+
if (fwnode_property_read_u32(child, "linux,code",
685+
&button->code)) {
686+
dev_err(dev, "Button without keycode\n");
687+
fwnode_handle_put(child);
696688
return ERR_PTR(-EINVAL);
697689
}
698690

699-
button->desc = of_get_property(pp, "label", NULL);
691+
fwnode_property_read_string(child, "label", &button->desc);
700692

701-
if (of_property_read_u32(pp, "linux,input-type", &button->type))
693+
if (fwnode_property_read_u32(child, "linux,input-type",
694+
&button->type))
702695
button->type = EV_KEY;
703696

704-
button->wakeup = of_property_read_bool(pp, "wakeup-source") ||
705-
/* legacy name */
706-
of_property_read_bool(pp, "gpio-key,wakeup");
697+
button->wakeup =
698+
fwnode_property_read_bool(child, "wakeup-source") ||
699+
/* legacy name */
700+
fwnode_property_read_bool(child, "gpio-key,wakeup");
707701

708-
button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
702+
button->can_disable =
703+
fwnode_property_read_bool(child, "linux,can-disable");
709704

710-
if (of_property_read_u32(pp, "debounce-interval",
705+
if (fwnode_property_read_u32(child, "debounce-interval",
711706
&button->debounce_interval))
712707
button->debounce_interval = 5;
713708

714709
button++;
715710
}
716711

717-
if (pdata->nbuttons == 0)
718-
return ERR_PTR(-EINVAL);
719-
720712
return pdata;
721713
}
722714

@@ -726,20 +718,11 @@ static const struct of_device_id gpio_keys_of_match[] = {
726718
};
727719
MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
728720

729-
#else
730-
731-
static inline struct gpio_keys_platform_data *
732-
gpio_keys_get_devtree_pdata(struct device *dev)
733-
{
734-
return ERR_PTR(-ENODEV);
735-
}
736-
737-
#endif
738-
739721
static int gpio_keys_probe(struct platform_device *pdev)
740722
{
741723
struct device *dev = &pdev->dev;
742724
const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
725+
struct fwnode_handle *child = NULL;
743726
struct gpio_keys_drvdata *ddata;
744727
struct input_dev *input;
745728
size_t size;
@@ -792,14 +775,28 @@ static int gpio_keys_probe(struct platform_device *pdev)
792775
const struct gpio_keys_button *button = &pdata->buttons[i];
793776
struct gpio_button_data *bdata = &ddata->data[i];
794777

795-
error = gpio_keys_setup_key(pdev, input, bdata, button);
796-
if (error)
778+
if (!dev_get_platdata(dev)) {
779+
child = device_get_next_child_node(&pdev->dev, child);
780+
if (!child) {
781+
dev_err(&pdev->dev,
782+
"missing child device node for entry %d\n",
783+
i);
784+
return -EINVAL;
785+
}
786+
}
787+
788+
error = gpio_keys_setup_key(pdev, input, bdata, button, child);
789+
if (error) {
790+
fwnode_handle_put(child);
797791
return error;
792+
}
798793

799794
if (button->wakeup)
800795
wakeup = 1;
801796
}
802797

798+
fwnode_handle_put(child);
799+
803800
error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
804801
if (error) {
805802
dev_err(dev, "Unable to export keys/switches, error: %d\n",
@@ -889,7 +886,7 @@ static struct platform_driver gpio_keys_device_driver = {
889886
.driver = {
890887
.name = "gpio-keys",
891888
.pm = &gpio_keys_pm_ops,
892-
.of_match_table = of_match_ptr(gpio_keys_of_match),
889+
.of_match_table = gpio_keys_of_match,
893890
}
894891
};
895892

0 commit comments

Comments
 (0)