Skip to contentSkip to conversation

Support sparse unidirectional GPIO lines.

PATCH Applied Linus Walleij 5 participants 10 messages May 11 – May 12

Subsystems (2)
lore
Share & tools
lore

Linus Walleij

author maintainer
#0 lore raw

v3 cover Support sparse unidirectional GPIO lines.

This adds a fixed_direction_mask bitmap to the regmap GPIO config and state holder. This works the following way:

- If the bitmap is NULL all GPIOs are assumed to be fixed direction and that is specified in fixed_direction_output. This makes sure old drivers keep working as before.

- If the fixed_direction_mask bitmap is present (!= NULL) the lines set to 1 in the bitmask are fixed direction, all other lines can change their direction dynamically.

To be used in a forthcoming submission.

As a consequence, be more elaborate when handling direction setting (patch 2).

---
Changes in v3:
- Rename fixed_direction_sparse to fixed_direction_mask which is more
to the point.
- Make gpio_regmap_set_direction() fail if the requested direction is
not the same as the fixed direction.
- Augment gpio_regmap_direction_output() such that it will first check
if the line is a fixed direction line and only continue if it is
fixed as output in this case.
- Link to v2: https://patch.msgid.link/20260508-regmap-gpio-sparse-fixed-dir-v2-0-deee84df3027@kernel.org

Changes in v2:
- Make a second patch to exclude setting on unidirectional
lines.
- Link to v1: https://patch.msgid.link/20260507-regmap-gpio-sparse-fixed-dir-v1-1-a2e5855e2701@kernel.org

---
Linus Walleij (2):
gpio: regmap: Support sparsed fixed direction
gpio: regmap: Don't set a fixed direction line

 drivers/gpio/gpio-regmap.c  | 74 ++++++++++++++++++++++++++++++++++++++++++---
 include/linux/gpio/regmap.h |  7 +++++
 2 files changed, 77 insertions(+), 4 deletions(-)

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260507-regmap-gpio-sparse-fixed-dir-1d5c0d2e2c6f

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/2 gpio: regmap: Support sparsed fixed direction

On some regmapped GPIOs apparently only a sparser selection of the lines (not all) are actually fixed direction.

Support this situation by adding an optional bitmap indicating which GPIOs are actually fixed direction and which are not.

---
 drivers/gpio/gpio-regmap.c  | 37 +++++++++++++++++++++++++++++++++----
 include/linux/gpio/regmap.h |  7 +++++++
 2 files changed, 40 insertions(+), 4 deletions(-)
drivers/gpio/gpio-regmap.c +33 -4
index 9ae4a41a2427..b3b4e77ec147 100644
@@ -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_mask;
 	unsigned long *fixed_direction_output;
 
 #ifdef CONFIG_REGMAP_IRQ
@@ -138,6 +139,20 @@ static int gpio_regmap_set_with_clear(struct gpio_chip *chip,
 	return regmap_write(gpio->regmap, reg, mask);
 }
 
+static bool gpio_regmap_fixed_direction(struct gpio_regmap *gpio,
+					unsigned int offset)
+{
+	if (!gpio->fixed_direction_output)
+		return false;
+
+	/* In this case only some GPIOs are fixed as input/output */
+	if (gpio->fixed_direction_mask &&
+	    !test_bit(offset, gpio->fixed_direction_mask))
+		return false;
+
+	return true;
+}
+
 static int gpio_regmap_get_direction(struct gpio_chip *chip,
 				     unsigned int offset)
 {
@@ -145,7 +160,7 @@ 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 (gpio_regmap_fixed_direction(gpio, offset)) {
 		if (test_bit(offset, gpio->fixed_direction_output))
 			return GPIO_LINE_DIRECTION_OUT;
 		else
@@ -302,12 +317,23 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 			goto err_free_gpio;
 	}
 
+	if (config->fixed_direction_mask) {
+		gpio->fixed_direction_mask = bitmap_alloc(chip->ngpio,
+							    GFP_KERNEL);
+		if (!gpio->fixed_direction_mask) {
+			ret = -ENOMEM;
+			goto err_free_gpio;
+		}
+		bitmap_copy(gpio->fixed_direction_mask,
+			    config->fixed_direction_mask, chip->ngpio);
+	}
+
 	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;
+			goto err_free_bitmap_dirmask;
 		}
 		bitmap_copy(gpio->fixed_direction_output,
 			    config->fixed_direction_output, chip->ngpio);
@@ -329,7 +355,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_bitmap;
+		goto err_free_bitmap_output;
 
 #ifdef CONFIG_REGMAP_IRQ
 	if (config->regmap_irq_chip) {
@@ -355,8 +381,10 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 
 err_remove_gpiochip:
 	gpiochip_remove(chip);
-err_free_bitmap:
+err_free_bitmap_output:
 	bitmap_free(gpio->fixed_direction_output);
+err_free_bitmap_dirmask:
+	bitmap_free(gpio->fixed_direction_mask);
 err_free_gpio:
 	kfree(gpio);
 	return ERR_PTR(ret);
@@ -376,6 +404,7 @@ void gpio_regmap_unregister(struct gpio_regmap *gpio)
 
 	gpiochip_remove(&gpio->gpio_chip);
 	bitmap_free(gpio->fixed_direction_output);
+	bitmap_free(gpio->fixed_direction_mask);
 	kfree(gpio);
 }
 EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
include/linux/gpio/regmap.h +7 -0
index 12d154732ca9..06255756710d 100644
@@ -38,6 +38,12 @@ struct regmap;
  *			offset to a register/bitmask pair. If not
  *			given the default gpio_regmap_simple_xlate()
  *			is used.
+ * @fixed_direction_mask:
+ *			(Optional) Bitmap representing the GPIO lines that
+ *			make use of the @fixed_direction_output list to
+ *			enforce direction of the GPIO. If this is NULL
+ *			and @fixed_direction_output is defined, ALL GPIOs
+ *			are assumed to be fixed direction (out or in).
  * @fixed_direction_output:
  *			(Optional) Bitmap representing the fixed direction of
  *			the GPIO lines. Useful when there are GPIO lines with a
@@ -89,6 +95,7 @@ struct gpio_regmap_config {
 	int reg_stride;
 	int ngpio_per_reg;
 	struct irq_domain *irq_domain;
+	unsigned long *fixed_direction_mask;
 	unsigned long *fixed_direction_output;
 
 #ifdef CONFIG_REGMAP_IRQ
Signature · 1 line
2.54.0
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] gpio: regmap: Support sparsed fixed direction

Quoted 10 lines · Linus Walleij

On some regmapped GPIOs apparently only a sparser selection of the lines (not all) are actually fixed direction.

Support this situation by adding an optional bitmap indicating which GPIOs are actually fixed direction and which are not.

You renamed "test_direction_sparse" so I had to do that too, but I tested this again as well.

Quoted 127 lines
---
  drivers/gpio/gpio-regmap.c  | 37 +++++++++++++++++++++++++++++++++----
  include/linux/gpio/regmap.h |  7 +++++++
  2 files changed, 40 insertions(+), 4 deletions(-)
drivers/gpio/gpio-regmap.c +33 -4
index 9ae4a41a2427..b3b4e77ec147 100644
@@ -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_mask;
  	unsigned long *fixed_direction_output;
 
  #ifdef CONFIG_REGMAP_IRQ
@@ -138,6 +139,20 @@ static int gpio_regmap_set_with_clear(struct gpio_chip *chip,
  	return regmap_write(gpio->regmap, reg, mask);
  }
 
+static bool gpio_regmap_fixed_direction(struct gpio_regmap *gpio,
+					unsigned int offset)
+{
+	if (!gpio->fixed_direction_output)
+		return false;
+
+	/* In this case only some GPIOs are fixed as input/output */
+	if (gpio->fixed_direction_mask &&
+	    !test_bit(offset, gpio->fixed_direction_mask))
+		return false;
+
+	return true;
+}
+
  static int gpio_regmap_get_direction(struct gpio_chip *chip,
  				     unsigned int offset)
  {
@@ -145,7 +160,7 @@ 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 (gpio_regmap_fixed_direction(gpio, offset)) {
  		if (test_bit(offset, gpio->fixed_direction_output))
  			return GPIO_LINE_DIRECTION_OUT;
  		else
@@ -302,12 +317,23 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
  			goto err_free_gpio;
  	}
 
+	if (config->fixed_direction_mask) {
+		gpio->fixed_direction_mask = bitmap_alloc(chip->ngpio,
+							    GFP_KERNEL);
+		if (!gpio->fixed_direction_mask) {
+			ret = -ENOMEM;
+			goto err_free_gpio;
+		}
+		bitmap_copy(gpio->fixed_direction_mask,
+			    config->fixed_direction_mask, chip->ngpio);
+	}
+
  	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;
+			goto err_free_bitmap_dirmask;
  		}
  		bitmap_copy(gpio->fixed_direction_output,
  			    config->fixed_direction_output, chip->ngpio);
@@ -329,7 +355,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_bitmap;
+		goto err_free_bitmap_output;
 
  #ifdef CONFIG_REGMAP_IRQ
  	if (config->regmap_irq_chip) {
@@ -355,8 +381,10 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 
  err_remove_gpiochip:
  	gpiochip_remove(chip);
-err_free_bitmap:
+err_free_bitmap_output:
  	bitmap_free(gpio->fixed_direction_output);
+err_free_bitmap_dirmask:
+	bitmap_free(gpio->fixed_direction_mask);
  err_free_gpio:
  	kfree(gpio);
  	return ERR_PTR(ret);
@@ -376,6 +404,7 @@ void gpio_regmap_unregister(struct gpio_regmap *gpio)
 
  	gpiochip_remove(&gpio->gpio_chip);
  	bitmap_free(gpio->fixed_direction_output);
+	bitmap_free(gpio->fixed_direction_mask);
  	kfree(gpio);
  }
  EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
include/linux/gpio/regmap.h +7 -0
index 12d154732ca9..06255756710d 100644
@@ -38,6 +38,12 @@ struct regmap;
   *			offset to a register/bitmask pair. If not
   *			given the default gpio_regmap_simple_xlate()
   *			is used.
+ * @fixed_direction_mask:
+ *			(Optional) Bitmap representing the GPIO lines that
+ *			make use of the @fixed_direction_output list to
+ *			enforce direction of the GPIO. If this is NULL
+ *			and @fixed_direction_output is defined, ALL GPIOs
+ *			are assumed to be fixed direction (out or in).
   * @fixed_direction_output:
   *			(Optional) Bitmap representing the fixed direction of
   *			the GPIO lines. Useful when there are GPIO lines with a
@@ -89,6 +95,7 @@ struct gpio_regmap_config {
  	int reg_stride;
  	int ngpio_per_reg;
  	struct irq_domain *irq_domain;
+	unsigned long *fixed_direction_mask;
  	unsigned long *fixed_direction_output;
 
  #ifdef CONFIG_REGMAP_IRQ
maintainer Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] gpio: regmap: Support sparsed fixed direction

In reply to Linus Walleij
Quoted 10 lines · Linus Walleij

On some regmapped GPIOs apparently only a sparser selection of the lines (not all) are actually fixed direction.

Support this situation by adding an optional bitmap indicating which GPIOs are actually fixed direction and which are not.

-michael

author maintainer
#4 lore raw

PATCH v3 2/2 gpio: regmap: Don't set a fixed direction line

In reply to Linus Walleij

If a GPIO line has a fixed direction, report an error is a consumer anyway tries to set the direction to something other than what it is hardcoded to.

This didn't happen much before because what we supported was all lines input or output and then the implementer would probably not specify the direction registers, but with sparse fixed direction we can have a mixture so let's take this into account.

As a consequence, since gpio_regmap_set_direction() can now fail, alter the semantics in gpio_regmap_direction_output() such that we first check if we can set the direction to output before we set the value and the direction.

---
 drivers/gpio/gpio-regmap.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpio/gpio-regmap.c +37 -0
index b3b4e77ec147..51b4d69b8740 100644
@@ -196,6 +196,22 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
 		return GPIO_LINE_DIRECTION_IN;
 }
 
+static int gpio_regmap_try_direction_fixed(struct gpio_regmap *gpio,
+					   unsigned int offset, bool output)
+{
+	if (test_bit(offset, gpio->fixed_direction_output)) {
+		if (output)
+			return 0;
+		else
+			return -EINVAL;
+	} else {
+		if (output)
+			return -EINVAL;
+		else
+			return 0;
+	}
+}
+
 static int gpio_regmap_set_direction(struct gpio_chip *chip,
 				     unsigned int offset, bool output)
 {
@@ -203,6 +219,13 @@ static int gpio_regmap_set_direction(struct gpio_chip *chip,
 	unsigned int base, val, reg, mask;
 	int invert, ret;
 
+	/*
+	 * If the direction is fixed, only accept the fixed
+	 * direction in this call.
+	 */
+	if (gpio_regmap_fixed_direction(gpio, offset))
+		return gpio_regmap_try_direction_fixed(gpio, offset, output);
+
 	if (gpio->reg_dir_out_base) {
 		base = gpio_regmap_addr(gpio->reg_dir_out_base);
 		invert = 0;
@@ -234,6 +257,20 @@ static int gpio_regmap_direction_input(struct gpio_chip *chip,
 static int gpio_regmap_direction_output(struct gpio_chip *chip,
 					unsigned int offset, int value)
 {
+	struct gpio_regmap *gpio = gpiochip_get_data(chip);
+	int ret;
+
+	/*
+	 * First check if this is gonna work on a fixed direction line,
+	 * if it doesn't (i.e. this is a fixed input line), then do not
+	 * attempt to set the output value either and just bail out.
+	 */
+	if (gpio_regmap_fixed_direction(gpio, offset)) {
+		ret = gpio_regmap_try_direction_fixed(gpio, offset, true);
+		if (ret)
+			return ret;
+	}
+
 	gpio_regmap_set(chip, offset, value);
 
 	return gpio_regmap_set_direction(chip, offset, true);
Signature · 1 line
2.54.0
Reviewed-by Tested-by on v3 2/2
#5 lore raw

Re: [PATCH v3 2/2] gpio: regmap: Don't set a fixed direction line

Quoted 2 lines · Linus Walleij

If a GPIO line has a fixed direction, report an error is a consumer anyway tries to set the direction to

s/is/if/

Quoted 14 lines

something other than what it is hardcoded to.

This didn't happen much before because what we supported was all lines input or output and then the implementer would probably not specify the direction registers, but with sparse fixed direction we can have a mixture so let's take this into account.

As a consequence, since gpio_regmap_set_direction() can now fail, alter the semantics in gpio_regmap_direction_output() such that we first check if we can set the direction to output before we set the value and the direction.

Quoted 3 lines
Quoted 67 lines
---
  drivers/gpio/gpio-regmap.c | 37 +++++++++++++++++++++++++++++++++++++
  1 file changed, 37 insertions(+)
drivers/gpio/gpio-regmap.c +37 -0
index b3b4e77ec147..51b4d69b8740 100644
@@ -196,6 +196,22 @@ static int gpio_regmap_get_direction(struct gpio_chip *chip,
  		return GPIO_LINE_DIRECTION_IN;
  }
 
+static int gpio_regmap_try_direction_fixed(struct gpio_regmap *gpio,
+					   unsigned int offset, bool output)
+{
+	if (test_bit(offset, gpio->fixed_direction_output)) {
+		if (output)
+			return 0;
+		else
+			return -EINVAL;
+	} else {
+		if (output)
+			return -EINVAL;
+		else
+			return 0;
+	}
+}
+
  static int gpio_regmap_set_direction(struct gpio_chip *chip,
  				     unsigned int offset, bool output)
  {
@@ -203,6 +219,13 @@ static int gpio_regmap_set_direction(struct gpio_chip *chip,
  	unsigned int base, val, reg, mask;
  	int invert, ret;
 
+	/*
+	 * If the direction is fixed, only accept the fixed
+	 * direction in this call.
+	 */
+	if (gpio_regmap_fixed_direction(gpio, offset))
+		return gpio_regmap_try_direction_fixed(gpio, offset, output);
+
  	if (gpio->reg_dir_out_base) {
  		base = gpio_regmap_addr(gpio->reg_dir_out_base);
  		invert = 0;
@@ -234,6 +257,20 @@ static int gpio_regmap_direction_input(struct gpio_chip *chip,
  static int gpio_regmap_direction_output(struct gpio_chip *chip,
  					unsigned int offset, int value)
  {
+	struct gpio_regmap *gpio = gpiochip_get_data(chip);
+	int ret;
+
+	/*
+	 * First check if this is gonna work on a fixed direction line,
+	 * if it doesn't (i.e. this is a fixed input line), then do not
+	 * attempt to set the output value either and just bail out.
+	 */
+	if (gpio_regmap_fixed_direction(gpio, offset)) {
+		ret = gpio_regmap_try_direction_fixed(gpio, offset, true);
+		if (ret)
+			return ret;
+	}
+
  	gpio_regmap_set(chip, offset, value);
 
  	return gpio_regmap_set_direction(chip, offset, true);
author maintainer
#6 lore raw

Re: [PATCH v3 2/2] gpio: regmap: Don't set a fixed direction line

Quoted 6 lines · Alex Elder
Quoted 2 lines · Linus Walleij

If a GPIO line has a fixed direction, report an error is a consumer anyway tries to set the direction to

s/is/if/

Ooops hopefully Bartosz can fix when applying if there are no further issues.

Yours,
Linus Walleij

#7 lore raw

Re: [PATCH v3 2/2] gpio: regmap: Don't set a fixed direction line

Quoted 4 lines · Linus Walleij
Quoted 1 line · Alex Elder

On 5/11/26 2:43 PM, Linus Walleij wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

If a GPIO line has a fixed direction, report an error is a consumer anyway tries to set the direction to

s/is/if/

Ooops hopefully Bartosz can fix when applying if there are no further issues.

I did and I also allowed myself to break the lines a little bit less eagerly, the soft limit is 75 characters. :)

Bart

maintainer Reviewed-by on v3 2/2
#8 lore raw

Re: [PATCH v3 2/2] gpio: regmap: Don't set a fixed direction line

In reply to Linus Walleij
Quoted 16 lines · Linus Walleij

If a GPIO line has a fixed direction, report an error is a consumer anyway tries to set the direction to something other than what it is hardcoded to.

This didn't happen much before because what we supported was all lines input or output and then the implementer would probably not specify the direction registers, but with sparse fixed direction we can have a mixture so let's take this into account.

As a consequence, since gpio_regmap_set_direction() can now fail, alter the semantics in gpio_regmap_direction_output() such that we first check if we can set the direction to output before we set the value and the direction.

Quoted 3 lines

-michael

#9 lore raw
In reply to Linus Walleij
Quoted 8 lines · Linus Walleij

This adds a fixed_direction_mask bitmap to the regmap GPIO config and state holder. This works the following way:

- If the bitmap is NULL all GPIOs are assumed to be fixed direction and that is specified in fixed_direction_output. This makes sure old drivers keep working as before.

[...]

Applied, thanks!

[1/2] gpio: regmap: Support sparsed fixed direction

      https://git.kernel.org/brgl/c/68d801eabda5219dcc25c9de98d3bbdb5b51b0a5
[2/2] gpio: regmap: Don't set a fixed direction line
      https://git.kernel.org/brgl/c/806e7acf7f331008637b4f8ecf211eb0a082e6eb

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>