gpiolib: Convert fwnode_get_named_gpiod() to configure GPIO
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Mon, 9 Jan 2017 14:02:28 +0000 (16:02 +0200)
committerLinus Walleij <linus.walleij@linaro.org>
Thu, 26 Jan 2017 08:59:32 +0000 (09:59 +0100)
Make fwnode_get_named_gpiod() consistent with the rest of
gpiod_get() like API, i.e. configure GPIO pin immediately after
request.

Besides obvious clean up it will help to configure pins based
on firmware provided resources.

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/gpio/devres.c
drivers/gpio/gpiolib.c
drivers/input/keyboard/gpio_keys.c
drivers/input/keyboard/gpio_keys_polled.c
drivers/leds/leds-gpio.c
drivers/video/fbdev/amba-clcd-nomadik.c
include/linux/gpio/consumer.h

index b760cbbb41d824908e1532f384de8c447b16b603..5e0b41b5e5541c47c1b5cbc9050b9dfe48bf989c 100644 (file)
@@ -127,13 +127,18 @@ EXPORT_SYMBOL(devm_gpiod_get_index);
  * @dev:       GPIO consumer
  * @con_id:    function within the GPIO consumer
  * @child:     firmware node (child of @dev)
+ * @flags:     GPIO initialization flags
  *
  * GPIO descriptors returned from this function are automatically disposed on
  * driver detach.
+ *
+ * On successfull request the GPIO pin is configured in accordance with
+ * provided @flags.
  */
 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
                                            const char *con_id,
-                                           struct fwnode_handle *child)
+                                           struct fwnode_handle *child,
+                                           enum gpiod_flags flags)
 {
        static const char * const suffixes[] = { "gpios", "gpio" };
        char prop_name[32]; /* 32 is max size of property name */
@@ -154,7 +159,7 @@ struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
                        snprintf(prop_name, sizeof(prop_name), "%s",
                                                               suffixes[i]);
 
-               desc = fwnode_get_named_gpiod(child, prop_name);
+               desc = fwnode_get_named_gpiod(child, prop_name, flags);
                if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
                        break;
        }
index f4c26c7826cdfc0b5163632e596c0e664586549b..bb3d3a7edc455a2dddb25535bafd32760e43fd91 100644 (file)
@@ -3309,6 +3309,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
  * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  * @fwnode:    handle of the firmware node
  * @propname:  name of the firmware property representing the GPIO
+ * @dflags:    GPIO initialization flags
  *
  * This function can be used for drivers that get their configuration
  * from firmware.
@@ -3317,12 +3318,17 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
  * underlying firmware interface and then makes sure that the GPIO
  * descriptor is requested before it is returned to the caller.
  *
+ * On successfull request the GPIO pin is configured in accordance with
+ * provided @dflags.
+ *
  * In case of error an ERR_PTR() is returned.
  */
 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
-                                        const char *propname)
+                                        const char *propname,
+                                        enum gpiod_flags dflags)
 {
        struct gpio_desc *desc = ERR_PTR(-ENODEV);
+       unsigned long lflags = 0;
        bool active_low = false;
        bool single_ended = false;
        int ret;
@@ -3355,13 +3361,19 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
                return ERR_PTR(ret);
 
        if (active_low)
-               set_bit(FLAG_ACTIVE_LOW, &desc->flags);
+               lflags |= GPIO_ACTIVE_LOW;
 
        if (single_ended) {
                if (active_low)
-                       set_bit(FLAG_OPEN_DRAIN, &desc->flags);
+                       lflags |= GPIO_OPEN_DRAIN;
                else
-                       set_bit(FLAG_OPEN_SOURCE, &desc->flags);
+                       lflags |= GPIO_OPEN_SOURCE;
+       }
+
+       ret = gpiod_configure_flags(desc, propname, lflags, dflags);
+       if (ret < 0) {
+               gpiod_put(desc);
+               return ERR_PTR(ret);
        }
 
        return desc;
index 582462d0af758566a3611b3b7bac7328b6d491a0..9de4b876100a651024a7fe899f3a8fa93e21e090 100644 (file)
@@ -481,7 +481,7 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
        spin_lock_init(&bdata->lock);
 
        if (child) {
-               bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child);
+               bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child, GPIOD_IN);
                if (IS_ERR(bdata->gpiod)) {
                        error = PTR_ERR(bdata->gpiod);
                        if (error == -ENOENT) {
@@ -496,13 +496,6 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
                                                error);
                                return error;
                        }
-               } else {
-                       error = gpiod_direction_input(bdata->gpiod);
-                       if (error) {
-                               dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
-                                       desc_to_gpio(bdata->gpiod), error);
-                               return error;
-                       }
                }
        } else if (gpio_is_valid(button->gpio)) {
                /*
index bed4f2086158e3073b325dcc75fbcac3c51df4da..fd7ab4dad87bc5c34f30d36e5da7f50c50d68494 100644 (file)
@@ -304,7 +304,8 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
                        }
 
                        bdata->gpiod = devm_get_gpiod_from_child(dev, NULL,
-                                                                child);
+                                                                child,
+                                                                GPIOD_IN);
                        if (IS_ERR(bdata->gpiod)) {
                                error = PTR_ERR(bdata->gpiod);
                                if (error != -EPROBE_DEFER)
@@ -314,14 +315,6 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
                                fwnode_handle_put(child);
                                return error;
                        }
-
-                       error = gpiod_direction_input(bdata->gpiod);
-                       if (error) {
-                               dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
-                                       desc_to_gpio(bdata->gpiod), error);
-                               fwnode_handle_put(child);
-                               return error;
-                       }
                } else if (gpio_is_valid(button->gpio)) {
                        /*
                         * Legacy GPIO number so request the GPIO here and
index d400dcaf4d296444967065e98812c210c48a192d..00cc671cddcc16297391806b25bca95151ba0569 100644 (file)
@@ -174,7 +174,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                const char *state = NULL;
                struct device_node *np = to_of_node(child);
 
-               led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
+               led.gpiod = devm_get_gpiod_from_child(dev, NULL, child, GPIOD_ASIS);
                if (IS_ERR(led.gpiod)) {
                        fwnode_handle_put(child);
                        return ERR_CAST(led.gpiod);
index 0c06fcaaa6e82b481422a8ca0b2faf4fe042ab19..2e800d70b0e5a52ce52ce7282f60be5c224a4d6c 100644 (file)
@@ -184,32 +184,27 @@ static void tpg110_init(struct device *dev, struct device_node *np,
 {
        dev_info(dev, "TPG110 display init\n");
 
-       grestb = devm_get_gpiod_from_child(dev, "grestb", &np->fwnode);
+       /* This asserts the GRESTB signal, putting the display into reset */
+       grestb = devm_get_gpiod_from_child(dev, "grestb", &np->fwnode, GPIOD_OUT_HIGH);
        if (IS_ERR(grestb)) {
                dev_err(dev, "no GRESTB GPIO\n");
                return;
        }
-       /* This asserts the GRESTB signal, putting the display into reset */
-       gpiod_direction_output(grestb, 1);
-
-       scen = devm_get_gpiod_from_child(dev, "scen", &np->fwnode);
+       scen = devm_get_gpiod_from_child(dev, "scen", &np->fwnode, GPIOD_OUT_LOW);
        if (IS_ERR(scen)) {
                dev_err(dev, "no SCEN GPIO\n");
                return;
        }
-       gpiod_direction_output(scen, 0);
-       scl = devm_get_gpiod_from_child(dev, "scl", &np->fwnode);
+       scl = devm_get_gpiod_from_child(dev, "scl", &np->fwnode, GPIOD_OUT_LOW);
        if (IS_ERR(scl)) {
                dev_err(dev, "no SCL GPIO\n");
                return;
        }
-       gpiod_direction_output(scl, 0);
-       sda = devm_get_gpiod_from_child(dev, "sda", &np->fwnode);
+       sda = devm_get_gpiod_from_child(dev, "sda", &np->fwnode, GPIOD_OUT_LOW);
        if (IS_ERR(sda)) {
                dev_err(dev, "no SDA GPIO\n");
                return;
        }
-       gpiod_direction_output(sda, 0);
        board->enable = tpg110_enable;
        board->disable = tpg110_disable;
 }
index fb0fde686cb1f80717c544ac18b380f6501afd39..930d10049d8d50ac4d8c29e402c4471de19b5b6b 100644 (file)
@@ -135,10 +135,12 @@ int desc_to_gpio(const struct gpio_desc *desc);
 struct fwnode_handle;
 
 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
-                                        const char *propname);
+                                        const char *propname,
+                                        enum gpiod_flags dflags);
 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
                                            const char *con_id,
-                                           struct fwnode_handle *child);
+                                           struct fwnode_handle *child,
+                                           enum gpiod_flags flags);
 #else /* CONFIG_GPIOLIB */
 
 static inline int gpiod_count(struct device *dev, const char *con_id)
@@ -411,14 +413,19 @@ static inline int desc_to_gpio(const struct gpio_desc *desc)
 /* Child properties interface */
 struct fwnode_handle;
 
-static inline struct gpio_desc *fwnode_get_named_gpiod(
-       struct fwnode_handle *fwnode, const char *propname)
+static inline
+struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
+                                        const char *propname,
+                                        enum gpiod_flags dflags)
 {
        return ERR_PTR(-ENOSYS);
 }
 
-static inline struct gpio_desc *devm_get_gpiod_from_child(
-       struct device *dev, const char *con_id, struct fwnode_handle *child)
+static inline
+struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
+                                           const char *con_id,
+                                           struct fwnode_handle *child,
+                                           enum gpiod_flags flags)
 {
        return ERR_PTR(-ENOSYS);
 }