mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] gpiolib: Add of_xlate support
@ 2022-08-05 13:59 Jules Maselbas
  2022-08-08 12:05 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Jules Maselbas @ 2022-08-05 13:59 UTC (permalink / raw)
  To: barebox; +Cc: Jules Maselbas

The of_xlate callback is used to tanslate a device-tree GPIO specifier
into a chip relative GPIO number and flags. This callback needs to be
implemented by GPIO controller driver which doesn't uses the "classic"
device-tree description, which is:
    gpio = <&gpio-phandle gpio_num gpio_flags>;

But uses something else, like this (but not limited to):
    gpio = <&gpio-phandle gpio_bank gpio_num gpio_flags>;

Currently no GPIO driver require this of_xlate callback, but this will
be used for sunxi's GPIO support.

Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
---
 drivers/gpio/gpiolib.c | 60 ++++++++++++++++++++++--------------------
 drivers/of/of_gpio.c   |  5 ++--
 include/gpio.h         |  3 ++-
 3 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index f845a57394..97a99b84e3 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -444,44 +444,27 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
 		       unsigned int idx)
 {
 	struct device_node *chip_np = chip->dev->device_node;
+	struct of_phandle_args gpiospec;
 	unsigned long flags = 0;
-	u32 gpio_cells, gpio_num, gpio_flags;
+	u32 gpio_flags;
 	int ret, gpio;
 	const char *name = NULL;
 
-	ret = of_property_read_u32(chip_np, "#gpio-cells", &gpio_cells);
+	ret = of_parse_phandle_with_args(chip_np, "gpios", "#gpio-cells", idx,
+					&gpiospec);
 	if (ret)
 		return ret;
 
-	/*
-	 * Support for GPIOs that don't have #gpio-cells set to 2 is
-	 * not implemented
-	 */
-	if (WARN_ON(gpio_cells != 2))
-		return -ENOTSUPP;
-
-	ret = of_property_read_u32_index(np, "gpios", idx * gpio_cells,
-					 &gpio_num);
-	if (ret)
-		return ret;
-
-	ret = of_property_read_u32_index(np, "gpios", idx * gpio_cells + 1,
-					 &gpio_flags);
-	if (ret)
-		return ret;
-
-	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
-		flags |= GPIOF_ACTIVE_LOW;
-
-	gpio = gpio_get_num(chip->dev, gpio_num);
+	gpio = gpio_of_xlate(chip->dev, &gpiospec, &gpio_flags);
 	if (gpio == -EPROBE_DEFER)
 		return gpio;
-
 	if (gpio < 0) {
-		dev_err(chip->dev, "unable to get gpio %u\n", gpio_num);
+		dev_err(chip->dev, "unable to get gpio: %d\n", gpio);
 		return gpio;
 	}
 
+	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
+		flags |= GPIOF_ACTIVE_LOW;
 
 	/*
 	 * Note that, in order to be compatible with Linux, the code
@@ -636,7 +619,24 @@ void gpiochip_remove(struct gpio_chip *chip)
 	list_del(&chip->list);
 }
 
-int gpio_get_num(struct device_d *dev, int gpio)
+static int of_gpio_simple_xlate(struct gpio_chip *chip,
+				const struct of_phandle_args *gpiospec,
+				u32 *flags)
+{
+	/*
+	 * Support for GPIOs that don't have #gpio-cells set to 2 is
+	 * not implemented
+	 */
+	if (WARN_ON(gpiospec->args_count != 2))
+		return -ENOTSUPP;
+
+	if (flags)
+		*flags = gpiospec->args[1];
+
+	return chip->base + gpiospec->args[0];
+}
+
+int gpio_of_xlate(struct device_d *dev, struct of_phandle_args *gpiospec, int *flags)
 {
 	struct gpio_chip *chip;
 
@@ -644,8 +644,12 @@ int gpio_get_num(struct device_d *dev, int gpio)
 		return -ENODEV;
 
 	list_for_each_entry(chip, &chip_list, list) {
-		if (chip->dev == dev)
-			return chip->base + gpio;
+		if (chip->dev != dev)
+			continue;
+		if (chip->ops->of_xlate)
+			return chip->ops->of_xlate(chip, gpiospec, flags);
+		else
+			return of_gpio_simple_xlate(chip, gpiospec, flags);
 	}
 
 	return -EPROBE_DEFER;
diff --git a/drivers/of/of_gpio.c b/drivers/of/of_gpio.c
index 42e0347529..c23923b425 100644
--- a/drivers/of/of_gpio.c
+++ b/drivers/of/of_gpio.c
@@ -62,6 +62,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
 {
 	struct of_phandle_args out_args;
 	struct device_d *dev;
+	int of_flags;
 	int ret;
 
 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
@@ -79,7 +80,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
 		return -EPROBE_DEFER;
 	}
 
-	ret = gpio_get_num(dev, out_args.args[0]);
+	ret = gpio_of_xlate(dev, &out_args, &of_flags);
 	if (ret == -EPROBE_DEFER)
 		return ret;
 	if (ret < 0) {
@@ -89,7 +90,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
 	}
 
 	if (flags) {
-		*flags = out_args.args[1];
+		*flags = of_flags;
 		of_gpio_flags_quirks(np, propname, flags, index);
 	}
 
diff --git a/include/gpio.h b/include/gpio.h
index 6134190138..8218722dcb 100644
--- a/include/gpio.h
+++ b/include/gpio.h
@@ -171,6 +171,7 @@ struct gpio_ops {
 	int (*get_direction)(struct gpio_chip *chip, unsigned offset);
 	int (*get)(struct gpio_chip *chip, unsigned offset);
 	void (*set)(struct gpio_chip *chip, unsigned offset, int value);
+	int (*of_xlate)(struct gpio_chip *chip, const struct of_phandle_args *gpiospec, u32 *flags);
 };
 
 struct gpio_chip {
@@ -187,7 +188,7 @@ struct gpio_chip {
 int gpiochip_add(struct gpio_chip *chip);
 void gpiochip_remove(struct gpio_chip *chip);
 
-int gpio_get_num(struct device_d *dev, int gpio);
+int gpio_of_xlate(struct device_d *dev, struct of_phandle_args *gpiospec, int *flags);
 struct gpio_chip *gpio_get_chip(int gpio);
 
 #endif /* __GPIO_H */
-- 
2.17.1




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] gpiolib: Add of_xlate support
  2022-08-05 13:59 [PATCH] gpiolib: Add of_xlate support Jules Maselbas
@ 2022-08-08 12:05 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-08-08 12:05 UTC (permalink / raw)
  To: Jules Maselbas; +Cc: barebox

On Fri, Aug 05, 2022 at 03:59:28PM +0200, Jules Maselbas wrote:
> The of_xlate callback is used to tanslate a device-tree GPIO specifier
> into a chip relative GPIO number and flags. This callback needs to be
> implemented by GPIO controller driver which doesn't uses the "classic"
> device-tree description, which is:
>     gpio = <&gpio-phandle gpio_num gpio_flags>;
> 
> But uses something else, like this (but not limited to):
>     gpio = <&gpio-phandle gpio_bank gpio_num gpio_flags>;
> 
> Currently no GPIO driver require this of_xlate callback, but this will
> be used for sunxi's GPIO support.
> 
> Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
> ---
>  drivers/gpio/gpiolib.c | 60 ++++++++++++++++++++++--------------------
>  drivers/of/of_gpio.c   |  5 ++--
>  include/gpio.h         |  3 ++-
>  3 files changed, 37 insertions(+), 31 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index f845a57394..97a99b84e3 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -444,44 +444,27 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
>  		       unsigned int idx)
>  {
>  	struct device_node *chip_np = chip->dev->device_node;
> +	struct of_phandle_args gpiospec;
>  	unsigned long flags = 0;
> -	u32 gpio_cells, gpio_num, gpio_flags;
> +	u32 gpio_flags;
>  	int ret, gpio;
>  	const char *name = NULL;
>  
> -	ret = of_property_read_u32(chip_np, "#gpio-cells", &gpio_cells);
> +	ret = of_parse_phandle_with_args(chip_np, "gpios", "#gpio-cells", idx,
> +					&gpiospec);
>  	if (ret)
>  		return ret;
>  
> -	/*
> -	 * Support for GPIOs that don't have #gpio-cells set to 2 is
> -	 * not implemented
> -	 */
> -	if (WARN_ON(gpio_cells != 2))
> -		return -ENOTSUPP;
> -
> -	ret = of_property_read_u32_index(np, "gpios", idx * gpio_cells,
> -					 &gpio_num);
> -	if (ret)
> -		return ret;
> -
> -	ret = of_property_read_u32_index(np, "gpios", idx * gpio_cells + 1,
> -					 &gpio_flags);
> -	if (ret)
> -		return ret;
> -
> -	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
> -		flags |= GPIOF_ACTIVE_LOW;
> -
> -	gpio = gpio_get_num(chip->dev, gpio_num);
> +	gpio = gpio_of_xlate(chip->dev, &gpiospec, &gpio_flags);
>  	if (gpio == -EPROBE_DEFER)
>  		return gpio;
> -
>  	if (gpio < 0) {
> -		dev_err(chip->dev, "unable to get gpio %u\n", gpio_num);
> +		dev_err(chip->dev, "unable to get gpio: %d\n", gpio);
>  		return gpio;
>  	}
>  
> +	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
> +		flags |= GPIOF_ACTIVE_LOW;
>  
>  	/*
>  	 * Note that, in order to be compatible with Linux, the code
> @@ -636,7 +619,24 @@ void gpiochip_remove(struct gpio_chip *chip)
>  	list_del(&chip->list);
>  }
>  
> -int gpio_get_num(struct device_d *dev, int gpio)
> +static int of_gpio_simple_xlate(struct gpio_chip *chip,
> +				const struct of_phandle_args *gpiospec,
> +				u32 *flags)
> +{
> +	/*
> +	 * Support for GPIOs that don't have #gpio-cells set to 2 is
> +	 * not implemented
> +	 */
> +	if (WARN_ON(gpiospec->args_count != 2))
> +		return -ENOTSUPP;
> +
> +	if (flags)
> +		*flags = gpiospec->args[1];
> +
> +	return chip->base + gpiospec->args[0];
> +}
> +
> +int gpio_of_xlate(struct device_d *dev, struct of_phandle_args *gpiospec, int *flags)
>  {
>  	struct gpio_chip *chip;
>  
> @@ -644,8 +644,12 @@ int gpio_get_num(struct device_d *dev, int gpio)
>  		return -ENODEV;
>  
>  	list_for_each_entry(chip, &chip_list, list) {
> -		if (chip->dev == dev)
> -			return chip->base + gpio;
> +		if (chip->dev != dev)
> +			continue;
> +		if (chip->ops->of_xlate)
> +			return chip->ops->of_xlate(chip, gpiospec, flags);
> +		else
> +			return of_gpio_simple_xlate(chip, gpiospec, flags);
>  	}
>  
>  	return -EPROBE_DEFER;
> diff --git a/drivers/of/of_gpio.c b/drivers/of/of_gpio.c
> index 42e0347529..c23923b425 100644
> --- a/drivers/of/of_gpio.c
> +++ b/drivers/of/of_gpio.c
> @@ -62,6 +62,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
>  {
>  	struct of_phandle_args out_args;
>  	struct device_d *dev;
> +	int of_flags;
>  	int ret;
>  
>  	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
> @@ -79,7 +80,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
>  		return -EPROBE_DEFER;
>  	}
>  
> -	ret = gpio_get_num(dev, out_args.args[0]);
> +	ret = gpio_of_xlate(dev, &out_args, &of_flags);
>  	if (ret == -EPROBE_DEFER)
>  		return ret;
>  	if (ret < 0) {
> @@ -89,7 +90,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
>  	}
>  
>  	if (flags) {
> -		*flags = out_args.args[1];
> +		*flags = of_flags;
>  		of_gpio_flags_quirks(np, propname, flags, index);
>  	}
>  
> diff --git a/include/gpio.h b/include/gpio.h
> index 6134190138..8218722dcb 100644
> --- a/include/gpio.h
> +++ b/include/gpio.h
> @@ -171,6 +171,7 @@ struct gpio_ops {
>  	int (*get_direction)(struct gpio_chip *chip, unsigned offset);
>  	int (*get)(struct gpio_chip *chip, unsigned offset);
>  	void (*set)(struct gpio_chip *chip, unsigned offset, int value);
> +	int (*of_xlate)(struct gpio_chip *chip, const struct of_phandle_args *gpiospec, u32 *flags);
>  };
>  
>  struct gpio_chip {
> @@ -187,7 +188,7 @@ struct gpio_chip {
>  int gpiochip_add(struct gpio_chip *chip);
>  void gpiochip_remove(struct gpio_chip *chip);
>  
> -int gpio_get_num(struct device_d *dev, int gpio);
> +int gpio_of_xlate(struct device_d *dev, struct of_phandle_args *gpiospec, int *flags);
>  struct gpio_chip *gpio_get_chip(int gpio);
>  
>  #endif /* __GPIO_H */
> -- 
> 2.17.1
> 
> 
> 

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-08 12:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-05 13:59 [PATCH] gpiolib: Add of_xlate support Jules Maselbas
2022-08-08 12:05 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox