[PATCH 1/2] gpiolib: add gpio_request and gpio_free support

Sascha Hauer s.hauer at pengutronix.de
Fri Dec 21 02:12:23 EST 2012


On Thu, Dec 20, 2012 at 03:23:59PM +0100, Jean-Christophe PLAGNIOL-VILLARD wrote:
> as today if no request or free provided do not complain
> 
> if the gpio is not request auto requested at first use
> 
> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
> ---
>  drivers/gpio/gpio.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++-----
>  include/gpio.h      |    7 +++++
>  2 files changed, 87 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/gpio/gpio.c b/drivers/gpio/gpio.c
> index 042a062..e29f0ba 100644
> --- a/drivers/gpio/gpio.c
> +++ b/drivers/gpio/gpio.c
> @@ -1,19 +1,71 @@
>  #include <common.h>
>  #include <gpio.h>
>  #include <errno.h>
> +#include <malloc.h>
>  
>  static LIST_HEAD(chip_list);
>  
> -static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS];
> +struct gpio_info {
> +	struct gpio_chip *chip;
> +	bool requested;
> +	char *label;
> +};
> +
> +static struct gpio_info gpio_desc[ARCH_NR_GPIOS];
> +
> +int gpio_request(unsigned gpio, const char *label)
> +{
> +	struct gpio_info *gi = &gpio_desc[gpio];
> +	struct gpio_chip *chip = gi->chip;
> +	int ret;
> +
> +	if (!gpio_is_valid(gpio))
> +		return -EINVAL;
> +	if (!chip)
> +		return -EINVAL;
> +	if (gi->requested)
> +		return -ENODEV;

-EBUSY?

>  void gpio_set_value(unsigned gpio, int value)
>  {
> -	struct gpio_chip *chip = gpio_desc[gpio];
> +	struct gpio_info *gi = &gpio_desc[gpio];
> +	struct gpio_chip *chip = gi->chip;
>  
>  	if (!gpio_is_valid(gpio))
>  		return;
>  	if (!chip)
>  		return;
> +	if (!gi->requested && gpio_request(gpio, "gpio"))
> +		return;

Could you factor out this into a gpio_ensure_requested function? This
way we would have a single point where the implicit gpio_request calls
are handled.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list