mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <ahmad@a3f.at>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 1/5] gpiolib: add Linux-like gpiod_get() helper
Date: Tue, 13 Apr 2021 08:12:17 +0200	[thread overview]
Message-ID: <20210413061217.GC19819@pengutronix.de> (raw)
In-Reply-To: <20210410103511.2073504-1-ahmad@a3f.at>

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@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@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 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


      parent reply	other threads:[~2021-04-13  6:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-10 10:35 Ahmad Fatoum
2021-04-10 10:35 ` [PATCH 2/5] sound: gpio-beeper: simplify using new gpiod_get helper Ahmad Fatoum
2021-04-10 10:35 ` [PATCH 3/5] power: reset: add GPIO poweroff driver Ahmad Fatoum
2021-04-10 10:35 ` [PATCH 4/5] power: reset: add GPIO restart driver Ahmad Fatoum
2021-04-12 12:14   ` Lars Pedersen
2021-04-10 10:35 ` [PATCH 5/5] watchdog: add GPIO watchdog driver Ahmad Fatoum
2021-04-13  6:12 ` Sascha Hauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210413061217.GC19819@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox