[PATCH v1 1/3] gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()

From: Andy Shevchenko

Date: Wed Oct 21 2026 - 16:51:58 EST


Several drivers already copy'n'paste almost the same code to dispose
IRQ mappings on remove over and over. Deduplicate that by introducing
a devm_gpiochip_dispose_irq_mappings().

Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
drivers/gpio/gpiolib-devres.c | 33 +++++++++++++++++++++++++++++++++
include/linux/gpio/driver.h | 4 ++++
2 files changed, 37 insertions(+)

diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c
index cc0e434a37d3..94690f14a949 100644
--- a/drivers/gpio/gpiolib-devres.c
+++ b/drivers/gpio/gpiolib-devres.c
@@ -10,6 +10,7 @@
#include <linux/err.h>
#include <linux/export.h>
#include <linux/gfp.h>
+#include <linux/irqdomain.h>
#include <linux/types.h>

#include <linux/gpio/consumer.h>
@@ -323,6 +324,38 @@ void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
}
EXPORT_SYMBOL_GPL(devm_gpiod_put_array);

+struct gpiochip_irqdomain {
+ struct gpio_chip *gc;
+ struct irq_domain *domain;
+};
+
+static void gpiochip_dispose_irq_mappings(struct device *dev, void *res)
+{
+ struct gpiochip_irqdomain *gcirq = res;
+
+ for (unsigned int i = 0; i < gcirq->gc->ngpio; i++)
+ irq_dispose_mapping(irq_find_mapping(gcirq->domain, i));
+}
+
+int devm_gpiochip_dispose_irq_mappings(struct device *dev,
+ struct gpio_chip *gc,
+ struct irq_domain *domain)
+{
+ struct gpiochip_irqdomain *gcirq;
+
+ gcirq = devres_alloc(gpiochip_dispose_irq_mappings, sizeof(*gcirq), GFP_KERNEL);
+ if (!gcirq)
+ return -ENOMEM;
+
+ gcirq->gc = gc;
+ gcirq->domain = domain;
+
+ devres_add(dev, gcirq);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(devm_gpiochip_dispose_irq_mappings);
+
static void devm_gpio_chip_release(void *data)
{
struct gpio_chip *gc = data;
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index b3b938ec5332..c610b5a9f8fa 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -707,6 +707,10 @@ static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
}
#endif

+int devm_gpiochip_dispose_irq_mappings(struct device *dev,
+ struct gpio_chip *gc,
+ struct irq_domain *domain);
+
int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
--
2.50.1