Linux devm_gpiod_get_index is a superset of devm_gpiod_get.
We already support gpiod_get to simplify kernel code porting, so
reimplement it in terms of a new dev_gpiod_get_index to simplify
porting kernel code using that as well.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 drivers/gpio/gpiolib.c | 23 ++++++++++++++---------
 include/gpiod.h        | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 773d07c1308f..4e8244b25f00 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -542,17 +542,20 @@ static const char *gpio_suffixes[] = {
        "gpio",
 };
 
+#ifdef CONFIG_OFDEVICE
 /* Linux compatibility helper: Get a GPIO descriptor from device tree */
-int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags)
+int dev_gpiod_get_index(struct device *dev,
+                       struct device_node *np,
+                       const char *_con_id, int index,
+                       enum gpiod_flags flags,
+                       const char *label)
 {
-       struct device_node *np = dev->of_node;
        enum of_gpio_flags of_flags;
-       const char *label = dev_name(dev);
        char *buf = NULL, *con_id;
        int gpio;
        int ret, i;
 
-       if (!IS_ENABLED(CONFIG_OFDEVICE) || !dev->of_node)
+       if (!np)
                return -ENODEV;
 
        for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
@@ -564,7 +567,7 @@ int gpiod_get(struct device *dev, const char *_con_id, enum 
gpiod_flags flags)
                if (!con_id)
                        return -ENOMEM;
 
-               gpio = of_get_named_gpio_flags(np, con_id, 0, &of_flags);
+               gpio = of_get_named_gpio_flags(np, con_id, index, &of_flags);
                free(con_id);
 
                if (gpio_is_valid(gpio))
@@ -579,10 +582,11 @@ int gpiod_get(struct device *dev, const char *_con_id, 
enum gpiod_flags flags)
 
        buf = NULL;
 
-       if (_con_id) {
-               label = buf = basprintf("%s-%s", dev_name(dev), _con_id);
-               if (!label)
-                       return -ENOMEM;
+       if (!label) {
+               if (con_id)
+                       label = buf = basprintf("%s-%s", dev_name(dev), 
_con_id);
+               else
+                       label = dev_name(dev);
        }
 
        ret = gpio_request_one(gpio, flags, label);
@@ -590,6 +594,7 @@ int gpiod_get(struct device *dev, const char *_con_id, enum 
gpiod_flags flags)
 
        return ret ?: gpio;
 }
+#endif
 
 int gpiochip_add(struct gpio_chip *chip)
 {
diff --git a/include/gpiod.h b/include/gpiod.h
index cc3fb966a010..f76cc98155e0 100644
--- a/include/gpiod.h
+++ b/include/gpiod.h
@@ -4,6 +4,7 @@
 
 #include <gpio.h>
 #include <of_gpio.h>
+#include <driver.h>
 
 /**
  * Optional flags that can be passed to one of gpiod_* to configure direction
@@ -20,8 +21,41 @@ enum gpiod_flags {
        GPIOD_OUT_HIGH  = GPIOF_OUT_INIT_ACTIVE,
 };
 
+#ifdef CONFIG_OFDEVICE
+
 /* returned gpio descriptor can be passed to any normal gpio_* function */
-int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags);
+int dev_gpiod_get_index(struct device *dev,
+                       struct device_node *np,
+                       const char *_con_id, int index,
+                       enum gpiod_flags flags,
+                       const char *label);
+
+#else
+static inline int dev_gpiod_get_index(struct device *dev,
+               struct device_node *np,
+               const char *_con_id, int index,
+               enum gpiod_flags flags,
+               const char *label)
+{
+       return -ENODEV;
+}
+#endif
+
+static inline int dev_gpiod_get(struct device *dev,
+                               struct device_node *np,
+                               const char *con_id,
+                               enum gpiod_flags flags,
+                               const char *label)
+{
+       return dev_gpiod_get_index(dev, np, con_id, 0, flags, label);
+}
+
+static inline int gpiod_get(struct device *dev,
+                           const char *_con_id,
+                           enum gpiod_flags flags)
+{
+       return dev_gpiod_get(dev, dev->of_node, _con_id, flags, NULL);
+}
 
 static inline void gpiod_set_value(int gpio, bool value)
 {
-- 
2.30.2


Reply via email to