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.
drivers/gpio/gpio-regmap.c | 18 ++++++++++++++++++
include/linux/gpio/regmap.h | 6 ++++++
2 files changed, 24 insertions(+)
diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
index e8a32dfebdcb..9edd79b5c10e 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,16 @@ 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 = devm_bitmap_alloc(config->parent,
+ chip->ngpio,
+ GFP_KERNEL);
+ if (!gpio->fixed_direction_output)
+ return ERR_PTR(-ENOMEM);
+ 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)
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
On Wed, 17 Sep 2025 11:04:17 +0200, Ioana Ciornei <ioana.ciornei@nxp.com> said: > 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. > > drivers/gpio/gpio-regmap.c | 18 ++++++++++++++++++ > include/linux/gpio/regmap.h | 6 ++++++ > 2 files changed, 24 insertions(+) > > diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c > index e8a32dfebdcb..9edd79b5c10e 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,16 @@ 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 = devm_bitmap_alloc(config->parent, > + chip->ngpio, > + GFP_KERNEL); Please don't use devres hidden in what is effectively a library function for drivers. You have no guarantee this will always be used in probe or that the life-time of this will end with the associated device's detach. Just use a regular bitmap_alloc(). Bart > + if (!gpio->fixed_direction_output) > + return ERR_PTR(-ENOMEM); > + 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) > 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 > >
On Thu, Sep 18, 2025 at 03:55:16AM -0700, Bartosz Golaszewski wrote: > On Wed, 17 Sep 2025 11:04:17 +0200, Ioana Ciornei <ioana.ciornei@nxp.com> said: > > 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. > > > > drivers/gpio/gpio-regmap.c | 18 ++++++++++++++++++ > > include/linux/gpio/regmap.h | 6 ++++++ > > 2 files changed, 24 insertions(+) > > > > diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c > > index e8a32dfebdcb..9edd79b5c10e 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,16 @@ 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 = devm_bitmap_alloc(config->parent, > > + chip->ngpio, > > + GFP_KERNEL); > > Please don't use devres hidden in what is effectively a library function for > drivers. You have no guarantee this will always be used in probe or that the > life-time of this will end with the associated device's detach. Just use > a regular bitmap_alloc(). Ok, sure. I will switch to bitmap_alloc() and bitmap_free(). Ioana
© 2016 - 2025 Red Hat, Inc.