[PATCH v4 06/11] gpio: regmap: add the .fixed_direction_output configuration parameter

Ioana Ciornei posted 11 patches 1 week, 5 days ago
There is a newer version of this series
[PATCH v4 06/11] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Ioana Ciornei 1 week, 5 days ago
There are GPIO controllers such as the one present in the LX2160ARDB
QIXIS FPGA which have fixed-direction input and output GPIO lines mixed
together in a single register. This cannot be modeled using the
gpio-regmap as-is since there is no way to present the true direction of
a GPIO line.

In order to make this use case possible, add a new configuration
parameter - fixed_direction_output - into the gpio_regmap_config
structure. This will enable user drivers to provide a bitmap that
represents the fixed direction of the GPIO lines.

Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
---
Changes in v2:
- Add the fixed_direction_output bitmap to the gpio_regmap_config
Changes in v3:
- Make a deep copy of the new bitmap.
- Remove the offset check against the ngpio.
- Added documentation for the new config field.
Changes in v4:
- Replace devres bitmap allocation with bitmap_alloc() and
  bitmap_free().

 drivers/gpio/gpio-regmap.c  | 24 +++++++++++++++++++++++-
 include/linux/gpio/regmap.h |  6 ++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
index e8a32dfebdcb..eaffd76adc56 100644
--- a/drivers/gpio/gpio-regmap.c
+++ b/drivers/gpio/gpio-regmap.c
@@ -31,6 +31,7 @@ struct gpio_regmap {
 	unsigned int reg_clr_base;
 	unsigned int reg_dir_in_base;
 	unsigned int reg_dir_out_base;
+	unsigned long *fixed_direction_output;
 
 	int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base,
 			      unsigned int offset, unsigned int *reg,
@@ -129,6 +130,13 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
 	unsigned int base, val, reg, mask;
 	int invert, ret;
 
+	if (gpio->fixed_direction_output) {
+		if (test_bit(offset, gpio->fixed_direction_output))
+			return GPIO_LINE_DIRECTION_OUT;
+		else
+			return GPIO_LINE_DIRECTION_IN;
+	}
+
 	if (gpio->reg_dat_base && !gpio->reg_set_base)
 		return GPIO_LINE_DIRECTION_IN;
 	if (gpio->reg_set_base && !gpio->reg_dat_base)
@@ -277,6 +285,17 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 			return ERR_PTR(ret);
 	}
 
+	if (config->fixed_direction_output) {
+		gpio->fixed_direction_output = bitmap_alloc(chip->ngpio,
+							    GFP_KERNEL);
+		if (!gpio->fixed_direction_output) {
+			ret = -ENOMEM;
+			goto err_free_gpio;
+		}
+		bitmap_copy(gpio->fixed_direction_output,
+			    config->fixed_direction_output, chip->ngpio);
+	}
+
 	/* if not set, assume there is only one register */
 	gpio->ngpio_per_reg = config->ngpio_per_reg;
 	if (!gpio->ngpio_per_reg)
@@ -293,7 +312,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 
 	ret = gpiochip_add_data(chip, gpio);
 	if (ret < 0)
-		goto err_free_gpio;
+		goto err_free_bitmap;
 
 	if (config->irq_domain) {
 		ret = gpiochip_irqchip_add_domain(chip, config->irq_domain);
@@ -305,6 +324,8 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 
 err_remove_gpiochip:
 	gpiochip_remove(chip);
+err_free_bitmap:
+	bitmap_free(gpio->fixed_direction_output);
 err_free_gpio:
 	kfree(gpio);
 	return ERR_PTR(ret);
@@ -318,6 +339,7 @@ EXPORT_SYMBOL_GPL(gpio_regmap_register);
 void gpio_regmap_unregister(struct gpio_regmap *gpio)
 {
 	gpiochip_remove(&gpio->gpio_chip);
+	bitmap_free(gpio->fixed_direction_output);
 	kfree(gpio);
 }
 EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h
index c722c67668c6..8d3d595bfdd3 100644
--- a/include/linux/gpio/regmap.h
+++ b/include/linux/gpio/regmap.h
@@ -37,6 +37,10 @@ struct regmap;
  *			offset to a register/bitmask pair. If not
  *			given the default gpio_regmap_simple_xlate()
  *			is used.
+ * @fixed_direction_output:
+ *			(Optional) Bitmap representing the fixed direction of
+ *			the GPIO lines. Useful when there are GPIO lines with a
+ *			fixed direction mixed together in the same register.
  * @drvdata:		(Optional) Pointer to driver specific data which is
  *			not used by gpio-remap but is provided "as is" to the
  *			driver callback(s).
@@ -78,6 +82,8 @@ struct gpio_regmap_config {
 	int ngpio_per_reg;
 	struct irq_domain *irq_domain;
 
+	unsigned long *fixed_direction_output;
+
 	int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base,
 			      unsigned int offset, unsigned int *reg,
 			      unsigned int *mask);
-- 
2.25.1
Re: [PATCH v4 06/11] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Michael Walle 1 week, 5 days ago
Hi,

> @@ -129,6 +130,13 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
>  	unsigned int base, val, reg, mask;
>  	int invert, ret;
>  
> +	if (gpio->fixed_direction_output) {
> +		if (test_bit(offset, gpio->fixed_direction_output))
> +			return GPIO_LINE_DIRECTION_OUT;
> +		else
> +			return GPIO_LINE_DIRECTION_IN;
> +	}
> +
>  	if (gpio->reg_dat_base && !gpio->reg_set_base)
>  		return GPIO_LINE_DIRECTION_IN;
>  	if (gpio->reg_set_base && !gpio->reg_dat_base)
> @@ -277,6 +285,17 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
>  			return ERR_PTR(ret);

Not related to your patch, but this line above is wrong. That should
be "goto err_free_gpio". Would you mind adding a patch for it? I
could do it myself, but it will probably conflict with this series.
I'm fine either way (if you do it, don't forget the Fixes: tag).

>  	}
>  
> +	if (config->fixed_direction_output) {
> +		gpio->fixed_direction_output = bitmap_alloc(chip->ngpio,
> +							    GFP_KERNEL);
> +		if (!gpio->fixed_direction_output) {
> +			ret = -ENOMEM;
> +			goto err_free_gpio;
> +		}
> +		bitmap_copy(gpio->fixed_direction_output,
> +			    config->fixed_direction_output, chip->ngpio);
> +	}
> +
>  	/* if not set, assume there is only one register */
>  	gpio->ngpio_per_reg = config->ngpio_per_reg;
>  	if (!gpio->ngpio_per_reg)
> @@ -293,7 +312,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
>  
>  	ret = gpiochip_add_data(chip, gpio);
>  	if (ret < 0)
> -		goto err_free_gpio;
> +		goto err_free_bitmap;

There's also an err_free_gpio jump below, that should also be
replaced with err_free_bitmap.

Otherwise looks good.

-michael
Re: [PATCH v4 06/11] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Ioana Ciornei 1 week, 5 days ago
On Fri, Sep 19, 2025 at 03:41:00PM +0200, Michael Walle wrote:
> Hi,
> 
> > @@ -129,6 +130,13 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
> >  	unsigned int base, val, reg, mask;
> >  	int invert, ret;
> >  
> > +	if (gpio->fixed_direction_output) {
> > +		if (test_bit(offset, gpio->fixed_direction_output))
> > +			return GPIO_LINE_DIRECTION_OUT;
> > +		else
> > +			return GPIO_LINE_DIRECTION_IN;
> > +	}
> > +
> >  	if (gpio->reg_dat_base && !gpio->reg_set_base)
> >  		return GPIO_LINE_DIRECTION_IN;
> >  	if (gpio->reg_set_base && !gpio->reg_dat_base)
> > @@ -277,6 +285,17 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
> >  			return ERR_PTR(ret);
> 
> Not related to your patch, but this line above is wrong. That should
> be "goto err_free_gpio". Would you mind adding a patch for it? I
> could do it myself, but it will probably conflict with this series.
> I'm fine either way (if you do it, don't forget the Fixes: tag).

If this would be the only change, I would not do a v5. If there are more
things to change, of course.

> 
> >  	}
> >  
> > +	if (config->fixed_direction_output) {
> > +		gpio->fixed_direction_output = bitmap_alloc(chip->ngpio,
> > +							    GFP_KERNEL);
> > +		if (!gpio->fixed_direction_output) {
> > +			ret = -ENOMEM;
> > +			goto err_free_gpio;
> > +		}
> > +		bitmap_copy(gpio->fixed_direction_output,
> > +			    config->fixed_direction_output, chip->ngpio);
> > +	}
> > +
> >  	/* if not set, assume there is only one register */
> >  	gpio->ngpio_per_reg = config->ngpio_per_reg;
> >  	if (!gpio->ngpio_per_reg)
> > @@ -293,7 +312,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
> >  
> >  	ret = gpiochip_add_data(chip, gpio);
> >  	if (ret < 0)
> > -		goto err_free_gpio;
> > +		goto err_free_bitmap;
> 
> There's also an err_free_gpio jump below, that should also be
> replaced with err_free_bitmap.

I am a bit confused. With this patch applied there is only one 'goto
err_free_gpio' in gpio-regmap.c and that's the one added by me above.

What am I missing?

Ioana
Re: [PATCH v4 06/11] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Michael Walle 1 week, 5 days ago
Hi,

> > > @@ -129,6 +130,13 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
> > >  	unsigned int base, val, reg, mask;
> > >  	int invert, ret;
> > >  
> > > +	if (gpio->fixed_direction_output) {
> > > +		if (test_bit(offset, gpio->fixed_direction_output))
> > > +			return GPIO_LINE_DIRECTION_OUT;
> > > +		else
> > > +			return GPIO_LINE_DIRECTION_IN;
> > > +	}
> > > +
> > >  	if (gpio->reg_dat_base && !gpio->reg_set_base)
> > >  		return GPIO_LINE_DIRECTION_IN;
> > >  	if (gpio->reg_set_base && !gpio->reg_dat_base)
> > > @@ -277,6 +285,17 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
> > >  			return ERR_PTR(ret);
> > 
> > Not related to your patch, but this line above is wrong. That should
> > be "goto err_free_gpio". Would you mind adding a patch for it? I
> > could do it myself, but it will probably conflict with this series.
> > I'm fine either way (if you do it, don't forget the Fixes: tag).
>
> If this would be the only change, I would not do a v5. If there are more
> things to change, of course.

Fine by me.

> > >  	}
> > >  
> > > +	if (config->fixed_direction_output) {
> > > +		gpio->fixed_direction_output = bitmap_alloc(chip->ngpio,
> > > +							    GFP_KERNEL);
> > > +		if (!gpio->fixed_direction_output) {
> > > +			ret = -ENOMEM;
> > > +			goto err_free_gpio;
> > > +		}
> > > +		bitmap_copy(gpio->fixed_direction_output,
> > > +			    config->fixed_direction_output, chip->ngpio);
> > > +	}
> > > +
> > >  	/* if not set, assume there is only one register */
> > >  	gpio->ngpio_per_reg = config->ngpio_per_reg;
> > >  	if (!gpio->ngpio_per_reg)
> > > @@ -293,7 +312,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
> > >  
> > >  	ret = gpiochip_add_data(chip, gpio);
> > >  	if (ret < 0)
> > > -		goto err_free_gpio;
> > > +		goto err_free_bitmap;
> > 
> > There's also an err_free_gpio jump below, that should also be
> > replaced with err_free_bitmap.
>
> I am a bit confused. With this patch applied there is only one 'goto
> err_free_gpio' in gpio-regmap.c and that's the one added by me above.
>
> What am I missing?

Probably commit 553b75d4bfe9 ("gpio: regmap: Allow to allocate
regmap-irq device") which was added to the mfd/gpio/next tree a
couple of days ago.

-michael
Re: [PATCH v4 06/11] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Ioana Ciornei 1 week, 5 days ago
On Fri, Sep 19, 2025 at 04:07:00PM +0200, Michael Walle wrote:

[snip] 
> > > >  	}
> > > >  
> > > > +	if (config->fixed_direction_output) {
> > > > +		gpio->fixed_direction_output = bitmap_alloc(chip->ngpio,
> > > > +							    GFP_KERNEL);
> > > > +		if (!gpio->fixed_direction_output) {
> > > > +			ret = -ENOMEM;
> > > > +			goto err_free_gpio;
> > > > +		}
> > > > +		bitmap_copy(gpio->fixed_direction_output,
> > > > +			    config->fixed_direction_output, chip->ngpio);
> > > > +	}
> > > > +
> > > >  	/* if not set, assume there is only one register */
> > > >  	gpio->ngpio_per_reg = config->ngpio_per_reg;
> > > >  	if (!gpio->ngpio_per_reg)
> > > > @@ -293,7 +312,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
> > > >  
> > > >  	ret = gpiochip_add_data(chip, gpio);
> > > >  	if (ret < 0)
> > > > -		goto err_free_gpio;
> > > > +		goto err_free_bitmap;
> > > 
> > > There's also an err_free_gpio jump below, that should also be
> > > replaced with err_free_bitmap.
> >
> > I am a bit confused. With this patch applied there is only one 'goto
> > err_free_gpio' in gpio-regmap.c and that's the one added by me above.
> >
> > What am I missing?
> 
> Probably commit 553b75d4bfe9 ("gpio: regmap: Allow to allocate
> regmap-irq device") which was added to the mfd/gpio/next tree a
> couple of days ago.
> 

Yes, I was on a 1-day old linux-next. Thanks for pointing this out.

In this case, I will have to do a v5 to also change that goto. I will
also add the patch that you requested.

Ioana