[PATCH v2 4/9] gpio: regmap: add the .fixed_direction_output configuration parameter

Ioana Ciornei posted 9 patches 2 weeks, 3 days ago
There is a newer version of this series
[PATCH v2 4/9] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Ioana Ciornei 2 weeks, 3 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

 drivers/gpio/gpio-regmap.c  | 12 ++++++++++++
 include/linux/gpio/regmap.h |  2 ++
 2 files changed, 14 insertions(+)

diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
index e8a32dfebdcb..2489768686d3 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,16 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
 	unsigned int base, val, reg, mask;
 	int invert, ret;
 
+	if (offset >= chip->ngpio)
+		return -EINVAL;
+
+	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)
@@ -247,6 +258,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	gpio->reg_clr_base = config->reg_clr_base;
 	gpio->reg_dir_in_base = config->reg_dir_in_base;
 	gpio->reg_dir_out_base = config->reg_dir_out_base;
+	gpio->fixed_direction_output = config->fixed_direction_output;
 
 	chip = &gpio->gpio_chip;
 	chip->parent = config->parent;
diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h
index c722c67668c6..34c143aca42d 100644
--- a/include/linux/gpio/regmap.h
+++ b/include/linux/gpio/regmap.h
@@ -78,6 +78,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 v2 4/9] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Michael Walle 2 weeks, 3 days ago
Hi Ioana,

On Mon Sep 15, 2025 at 2:23 PM CEST, Ioana Ciornei wrote:
> 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.

I wonder about the ownership of that allocated memory in the config
structure (and btw, I guess you leak the memory in your driver) and
if it's not better and more error proof to allocate and copy the
bitmap in gpio-regmap too (and maybe use devm_bitmap_alloc()) and
leave it to the caller to handle the passed bitmap. I.e. it could
also be on the stack.

Otherwise, this looks good.

> Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
> ---
> Changes in v2:
> - Add the fixed_direction_output bitmap to the gpio_regmap_config
>
>  drivers/gpio/gpio-regmap.c  | 12 ++++++++++++
>  include/linux/gpio/regmap.h |  2 ++
>  2 files changed, 14 insertions(+)
>
> diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
> index e8a32dfebdcb..2489768686d3 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,16 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
>  	unsigned int base, val, reg, mask;
>  	int invert, ret;
>  
> +	if (offset >= chip->ngpio)
> +		return -EINVAL;

Not sure this can happen. I tried to look into gpiolib.c but
couldn't find anything obvious that it can't happen. Maybe Linus or
Bartosz can comment on that.

> +
> +	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)
> @@ -247,6 +258,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
>  	gpio->reg_clr_base = config->reg_clr_base;
>  	gpio->reg_dir_in_base = config->reg_dir_in_base;
>  	gpio->reg_dir_out_base = config->reg_dir_out_base;
> +	gpio->fixed_direction_output = config->fixed_direction_output;
>  
>  	chip = &gpio->gpio_chip;
>  	chip->parent = config->parent;
> diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h
> index c722c67668c6..34c143aca42d 100644
> --- a/include/linux/gpio/regmap.h
> +++ b/include/linux/gpio/regmap.h
> @@ -78,6 +78,8 @@ struct gpio_regmap_config {
>  	int ngpio_per_reg;
>  	struct irq_domain *irq_domain;
>  
> +	unsigned long *fixed_direction_output;

Please add some documentation.

-michael

> +
>  	int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base,
>  			      unsigned int offset, unsigned int *reg,
>  			      unsigned int *mask);

Re: [PATCH v2 4/9] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Bartosz Golaszewski 2 weeks, 2 days ago
On Mon, 15 Sep 2025 14:45:58 +0200, Michael Walle <mwalle@kernel.org> said:
> Hi Ioana,
>
> On Mon Sep 15, 2025 at 2:23 PM CEST, Ioana Ciornei wrote:
>> 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.
>
> I wonder about the ownership of that allocated memory in the config
> structure (and btw, I guess you leak the memory in your driver) and
> if it's not better and more error proof to allocate and copy the
> bitmap in gpio-regmap too (and maybe use devm_bitmap_alloc()) and
> leave it to the caller to handle the passed bitmap. I.e. it could
> also be on the stack.
>

I was under the impression that whatever is in the config structure for GPIO
regmap init function is only required to stay alive until that call returns?

If so, then yes, a deep copy of everything from this structure is required.

> Otherwise, this looks good.
>

[snip]

>>
>> +	if (offset >= chip->ngpio)
>> +		return -EINVAL;
>
> Not sure this can happen. I tried to look into gpiolib.c but
> couldn't find anything obvious that it can't happen. Maybe Linus or
> Bartosz can comment on that.
>

Indeed, this is handed by GPIO core, please drop it.

[snip]

Bartosz
Re: [PATCH v2 4/9] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Ioana Ciornei 2 weeks, 3 days ago
On Mon, Sep 15, 2025 at 02:45:58PM +0200, Michael Walle wrote:
> Hi Ioana,
> 
> On Mon Sep 15, 2025 at 2:23 PM CEST, Ioana Ciornei wrote:
> > 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.
> 
> I wonder about the ownership of that allocated memory in the config
> structure (and btw, I guess you leak the memory in your driver)

Yes, I missed that memory leak. Sorry.

> and
> if it's not better and more error proof to allocate and copy the
> bitmap in gpio-regmap too (and maybe use devm_bitmap_alloc()) and
> leave it to the caller to handle the passed bitmap. I.e. it could
> also be on the stack.

So you are suggesting gpio-regmap to allocate the bitmap using
devm_bitmap_alloc() and base its size on config->ngpio, then copy into
it the bitmap passed by the caller, right?  Yes, that does seem more
error proof in terms of memory handling. Will change it in the next
version.

> 
> Otherwise, this looks good.
> 
> > Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
> > ---
> > Changes in v2:
> > - Add the fixed_direction_output bitmap to the gpio_regmap_config
> >
> >  drivers/gpio/gpio-regmap.c  | 12 ++++++++++++
> >  include/linux/gpio/regmap.h |  2 ++
> >  2 files changed, 14 insertions(+)
> >
> > diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
> > index e8a32dfebdcb..2489768686d3 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,16 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
> >  	unsigned int base, val, reg, mask;
> >  	int invert, ret;
> >  
> > +	if (offset >= chip->ngpio)
> > +		return -EINVAL;
> 
> Not sure this can happen. I tried to look into gpiolib.c but
> couldn't find anything obvious that it can't happen. Maybe Linus or
> Bartosz can comment on that.
> 
> > +
> > +	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)
> > @@ -247,6 +258,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
> >  	gpio->reg_clr_base = config->reg_clr_base;
> >  	gpio->reg_dir_in_base = config->reg_dir_in_base;
> >  	gpio->reg_dir_out_base = config->reg_dir_out_base;
> > +	gpio->fixed_direction_output = config->fixed_direction_output;
> >  
> >  	chip = &gpio->gpio_chip;
> >  	chip->parent = config->parent;
> > diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h
> > index c722c67668c6..34c143aca42d 100644
> > --- a/include/linux/gpio/regmap.h
> > +++ b/include/linux/gpio/regmap.h
> > @@ -78,6 +78,8 @@ struct gpio_regmap_config {
> >  	int ngpio_per_reg;
> >  	struct irq_domain *irq_domain;
> >  
> > +	unsigned long *fixed_direction_output;
> 
> Please add some documentation.

Ok, sure.

Ioana
Re: [PATCH v2 4/9] gpio: regmap: add the .fixed_direction_output configuration parameter
Posted by Michael Walle 2 weeks, 3 days ago
Hi Ioana,

> So you are suggesting gpio-regmap to allocate the bitmap using
> devm_bitmap_alloc() and base its size on config->ngpio, then copy into
> it the bitmap passed by the caller, right?  Yes, that does seem more
> error proof in terms of memory handling. Will change it in the next
> version.

Yes exactly.

-michael