[RFT PATCH v2 03/14] gpio: regmap: add callbacks to handle IRQ domains

Michael Walle mwalle at kernel.org
Fri Nov 27 06:52:20 PST 2026


At the moment, an IRQ domain is attached via config->irq_domain.
This means, that it has to be created before the gpio_regmap_register()
call. The callbacks of the IRQ domain might use the gpio_regmap_*_irq()
functions. Consumers can already use the domain as soon as it is
created and this creates a small window where the callbacks dereference
a NULL pointer.

To fix this, create a new create_irq_domain() callback, that is called
after the gpio_chip is added.

Also add a remove_irq_domain() callback, which is called after the
gpio_chip is removed and before the gpio_regmap is freed. It is supposed
to dispose the IRQ mappings and remove the domain.

Also ordering of the resources matter, so we forbid any device managed
resource handling. Any resources allocated in the create_irq_domain()
callback has to be freed manually in the remove_irq_domain() callback.

Signed-off-by: Michael Walle <mwalle at kernel.org>
Tested-by: Joseph Telaak <joseph at telaak.dev>
---
 drivers/gpio/gpio-regmap.c  | 30 ++++++++++++++++++++++++++++--
 include/linux/gpio/regmap.h | 15 ++++++++++++++-
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
index c04b79da5cdb..061d2800d0a0 100644
--- a/drivers/gpio/gpio-regmap.c
+++ b/drivers/gpio/gpio-regmap.c
@@ -39,6 +39,8 @@ struct gpio_regmap {
 	unsigned long *fixed_direction_mask;
 	unsigned long *fixed_direction_output;
 
+	void (*remove_irq_domain)(struct gpio_regmap *gpio);
+
 #ifdef CONFIG_REGMAP_IRQ
 	int regmap_irq_line;
 	struct regmap_irq_chip_data *irq_chip_data;
@@ -454,6 +456,14 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (config->reg_dir_out_base && config->reg_dir_in_base)
 		return ERR_PTR(-EINVAL);
 
+	/* there is only one way to provide an IRQ domain */
+	if (config->create_irq_domain && config->irq_domain)
+		return ERR_PTR(-EINVAL);
+#ifdef CONFIG_REGMAP_IRQ
+	if (config->create_irq_domain && config->regmap_irq_chip)
+		return ERR_PTR(-EINVAL);
+#endif
+
 	gpio = kzalloc_obj(*gpio);
 	if (!gpio)
 		return ERR_PTR(-ENOMEM);
@@ -545,6 +555,17 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 	if (ret < 0)
 		goto err_free_bitmap_output;
 
+	irq_domain = config->irq_domain;
+
+	if (config->create_irq_domain) {
+		irq_domain = config->create_irq_domain(gpio);
+		if (IS_ERR(irq_domain)) {
+			ret = PTR_ERR(irq_domain);
+			goto err_remove_gpiochip;
+		}
+		gpio->remove_irq_domain = config->remove_irq_domain;
+	}
+
 #ifdef CONFIG_REGMAP_IRQ
 	if (config->regmap_irq_chip) {
 		gpio->regmap_irq_line = config->regmap_irq_line;
@@ -558,9 +579,8 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 			goto err_remove_gpiochip;
 
 		irq_domain = regmap_irq_get_domain(gpio->irq_chip_data);
-	} else
+	}
 #endif
-	irq_domain = config->irq_domain;
 
 	if (irq_domain) {
 		ret = gpiochip_irqchip_add_domain(chip, irq_domain);
@@ -573,6 +593,9 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
 err_remove_gpiochip:
 	gpiochip_remove(chip);
 
+	if (gpio->remove_irq_domain)
+		gpio->remove_irq_domain(gpio);
+
 #ifdef CONFIG_REGMAP_IRQ
 	if (gpio->irq_chip_data)
 		regmap_del_irq_chip(gpio->regmap_irq_line, gpio->irq_chip_data);
@@ -601,6 +624,9 @@ void gpio_regmap_unregister(struct gpio_regmap *gpio)
 	 */
 	gpiochip_remove(&gpio->gpio_chip);
 
+	if (gpio->remove_irq_domain)
+		gpio->remove_irq_domain(gpio);
+
 #ifdef CONFIG_REGMAP_IRQ
 	if (gpio->irq_chip_data)
 		regmap_del_irq_chip(gpio->regmap_irq_line, gpio->irq_chip_data);
diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h
index 6fe0877717cb..dee5f8d2c48c 100644
--- a/include/linux/gpio/regmap.h
+++ b/include/linux/gpio/regmap.h
@@ -62,7 +62,18 @@ enum gpio_regmap_operation {
  *			same type, dat, set, etc) are not consecutive.
  * @ngpio_per_reg:	(Optional) Number of GPIOs per register
  * @irq_domain:		(Optional) IRQ domain if the controller is
- *			interrupt-capable
+ *			interrupt-capable. Cannot be used together with the
+ *			gpio_regmap_*_irq() callbacks. Use @create_irq_domain
+ *			instead.
+ * @create_irq_domain:	(Optional) Callback to create the IRQ domain in the
+ *			controller driver. Called after the gpio_chip is added
+ *			so the gpio_regmap_*_irq() have a valid gpio_regmap
+ *			pointer. Do NOT use any resource managed allocations in
+ *			this callback.
+ *			Mutually exclusive with @irq_domain and @regmap_irq_chip.
+ * @remove_irq_domain:	(Optional) Callback to remove the IRQ domain created in
+ *			the .create_irq_domain() callback. Dispose any IRQ
+ *			mappings, remove the IRQ domain and free any resources.
  * @fixed_direction_mask:
  *			(Optional) Bitmap representing the GPIO lines that
  *			make use of the @fixed_direction_output list to
@@ -133,6 +144,8 @@ struct gpio_regmap_config {
 	int reg_stride;
 	int ngpio_per_reg;
 	struct irq_domain *irq_domain;
+	struct irq_domain *(*create_irq_domain)(struct gpio_regmap *gpio);
+	void (*remove_irq_domain)(struct gpio_regmap *gpio);
 	unsigned long *fixed_direction_mask;
 	unsigned long *fixed_direction_output;
 
-- 
2.47.3




More information about the linux-arm-kernel mailing list