summaryrefslogtreecommitdiff
path: root/drivers/gpio
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2021-04-10 12:35:07 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-04-13 07:35:16 +0200
commit72254b8658dce0efdb1750b315876e78be5d6f07 (patch)
tree665fbcacaa5b1ed0a080271b7b90a7eaf3b36f12 /drivers/gpio
parent45804d9208d786f3efe60403761a13e308d0b989 (diff)
downloadbarebox-72254b8658dce0efdb1750b315876e78be5d6f07.tar.gz
gpiolib: add Linux-like gpiod_get() helper
Many Linux drivers use [devm_]gpiod_get to get appropriately configured GPIO descriptors out with little code. Make porting such Linux code easier by providing a semi-compatible gpiod_get function. Main differences: - It returns a gpio index, so it can be passed to any gpio_ function - It's device-tree only, so it should only be used from drivers that themselves probe from device tree. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Link: https://lore.pengutronix.de/20210410103511.2073504-1-ahmad@a3f.at
Diffstat (limited to 'drivers/gpio')
-rw-r--r--drivers/gpio/gpiolib.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 6088cadd8a..7b7261d01f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -6,6 +6,7 @@
#include <complete.h>
#include <gpio.h>
#include <of_gpio.h>
+#include <gpiod.h>
#include <errno.h>
#include <malloc.h>
@@ -554,6 +555,48 @@ static int of_gpiochip_scan_hogs(struct gpio_chip *chip)
return 0;
}
+/* Linux compatibility helper: Get a GPIO descriptor from device tree */
+int gpiod_get(struct device_d *dev, const char *_con_id, enum gpiod_flags flags)
+{
+ struct device_node *np = dev->device_node;
+ enum of_gpio_flags of_flags;
+ const char *con_id = "gpios", *label = dev_name(dev);
+ char *buf = NULL;
+ int gpio;
+ int ret;
+
+ if (!IS_ENABLED(CONFIG_OFDEVICE) || !dev->device_node)
+ return -ENODEV;
+
+ if (_con_id) {
+ con_id = buf = basprintf("%s-gpios", _con_id);
+ if (!buf)
+ return -ENOMEM;
+ }
+
+ gpio = of_get_named_gpio_flags(np, con_id, 0, &of_flags);
+ free(buf);
+
+ if (!gpio_is_valid(gpio))
+ return gpio < 0 ? gpio : -EINVAL;
+
+ if (of_flags & OF_GPIO_ACTIVE_LOW)
+ flags |= GPIOF_ACTIVE_LOW;
+
+ buf = NULL;
+
+ if (_con_id) {
+ label = buf = basprintf("%s-%s", dev_name(dev), _con_id);
+ if (!label)
+ return -ENOMEM;
+ }
+
+ ret = gpio_request_one(gpio, flags, label);
+ free(buf);
+
+ return ret ?: gpio;
+}
+
int gpiochip_add(struct gpio_chip *chip)
{
int base, i;