[PATCH v2 1/2] drivers: gpio: Add a managed API to get a GPIO from the device-tree

Simon Glass sjg at chromium.org
Fri Jun 26 03:12:00 CEST 2020


On Fri, 29 May 2020 at 15:38, Pratyush Yadav <p.yadav at ti.com> wrote:
>
> From: Jean-Jacques Hiblot <jjhiblot at ti.com>
>
> Add managed functions to get a gpio from the devce-tree, based on a
> property name (minus the '-gpios' suffix) and optionally an index.
>
> When the device is unbound, the GPIO is automatically released and the
> data structure is freed.
>
> Signed-off-by: Jean-Jacques Hiblot <jjhiblot at ti.com>
> ---
>  drivers/gpio/gpio-uclass.c | 70 ++++++++++++++++++++++++++++++++++++++
>  include/asm-generic/gpio.h | 47 +++++++++++++++++++++++++
>  2 files changed, 117 insertions(+)
>

Reviewed-by: Simon Glass <sjg at chromium.org>

nit below

> diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c
> index 9eeab22eef..fd868608fc 100644
> --- a/drivers/gpio/gpio-uclass.c
> +++ b/drivers/gpio/gpio-uclass.c
> @@ -6,6 +6,8 @@
>  #include <common.h>
>  #include <dm.h>
>  #include <log.h>
> +#include <dm/devres.h>
> +#include <dm/device_compat.h>
>  #include <dm/device-internal.h>
>  #include <dm/lists.h>
>  #include <dm/uclass-internal.h>
> @@ -1141,6 +1143,74 @@ int gpio_dev_request_index(struct udevice *dev, const char *nodename,
>                                  flags, 0, dev);
>  }
>
> +static void devm_gpiod_release(struct udevice *dev, void *res)
> +{
> +       dm_gpio_free(dev, res);
> +}
> +
> +static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
> +{
> +       return res == data;
> +}
> +
> +struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
> +                                      unsigned int index, int flags)
> +{
> +       int rc;
> +       struct gpio_desc *desc;
> +       char *propname;
> +       static const char suffix[] = "-gpios";
> +
> +       propname = malloc(strlen(id) + sizeof(suffix));
> +       if (!propname) {
> +               rc = -ENOMEM;
> +               goto end;
> +       }
> +
> +       strcpy(propname, id);
> +       strcat(propname, suffix);
> +
> +       desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
> +                           __GFP_ZERO);
> +       if (unlikely(!desc)) {
> +               rc = -ENOMEM;
> +               goto end;
> +       }
> +
> +       rc = gpio_request_by_name(dev, propname, index, desc, flags);
> +
> +end:
> +       if (propname)
> +               free(propname);
> +
> +       if (rc)
> +               return ERR_PTR(rc);
> +
> +       devres_add(dev, desc);

blank line here

> +       return desc;
> +}
> +
Regards,
Simon


More information about the U-Boot mailing list