[PATCH 1/5] gpiolib: add Linux-like gpiod_get() helper

Sascha Hauer sha at pengutronix.de
Tue Apr 13 07:12:17 BST 2021


On Sat, Apr 10, 2021 at 12:35:07PM +0200, Ahmad Fatoum wrote:
> 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 at a3f.at>
> ---
>  drivers/gpio/gpiolib.c | 43 ++++++++++++++++++++++++++++++++++++++++++
>  include/gpiod.h        | 26 +++++++++++++++++++++++++
>  2 files changed, 69 insertions(+)
>  create mode 100644 include/gpiod.h

Applied, thanks

Sascha

> 
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index 6088cadd8a18..7b7261d01f24 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;
> diff --git a/include/gpiod.h b/include/gpiod.h
> new file mode 100644
> index 000000000000..c8b2cd47a3cb
> --- /dev/null
> +++ b/include/gpiod.h
> @@ -0,0 +1,26 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef __GPIOD_H_
> +#define __GPIOD_H_
> +
> +#include <gpio.h>
> +#include <of_gpio.h>
> +
> +/**
> + * Optional flags that can be passed to one of gpiod_* to configure direction
> + * and output value. These values cannot be OR'd.
> + */
> +enum gpiod_flags {
> +	GPIOD_ASIS	= 0,
> +	GPIOD_IN	= GPIOF_IN,
> +	/*
> +	 * To change this later to a different logic level (i.e. taking
> +	 * active low into account), use gpio_direction_active()
> +	 */
> +	GPIOD_OUT_LOW	= GPIOF_OUT_INIT_INACTIVE,
> +	GPIOD_OUT_HIGH	= GPIOF_OUT_INIT_ACTIVE,
> +};
> +
> +/* returned gpio descriptor can be passed to any normal gpio_* function */
> +int gpiod_get(struct device_d *dev, const char *_con_id, enum gpiod_flags flags);
> +
> +#endif
> -- 
> 2.30.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list