[PATCH 3/3] gpio: regmap: Support a custom ->to_irq() hook

From: Aidan MacDonald
Date: Sun Jul 03 2022 - 07:10:26 EST


Some GPIO chips require a custom to_irq() callback for mapping
their IRQs, eg. because their interrupts come from a parent IRQ
chip where the GPIO offset doesn't map 1-to-1 with hwirq number.

Signed-off-by: Aidan MacDonald <aidanmacdonald.0x0@xxxxxxxxx>
---
drivers/gpio/gpio-regmap.c | 17 +++++++++++++++++
include/linux/gpio/regmap.h | 4 ++++
2 files changed, 21 insertions(+)

diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c
index 4bc01329fb14..d11b202e51fd 100644
--- a/drivers/gpio/gpio-regmap.c
+++ b/drivers/gpio/gpio-regmap.c
@@ -34,6 +34,8 @@ struct gpio_regmap {
unsigned int *reg, unsigned int *mask,
unsigned int *values);

+ int (*to_irq)(struct gpio_regmap *gpio, unsigned int offset);
+
void *driver_data;
};

@@ -193,6 +195,13 @@ static int gpio_regmap_direction_output(struct gpio_chip *chip,
return gpio_regmap_set_direction(chip, offset, true);
}

+static int gpio_regmap_to_irq(struct gpio_chip *chip, unsigned int offset)
+{
+ struct gpio_regmap *gpio = gpiochip_get_data(chip);
+
+ return gpio->to_irq(gpio, offset);
+}
+
void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio)
{
return gpio->driver_data;
@@ -242,6 +251,10 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
if (config->reg_field_xlate && config->reg_mask_xlate)
return ERR_PTR(-EINVAL);

+ /* an irq_domain will override the to_irq hook, so don't allow both */
+ if (config->irq_domain && config->to_irq)
+ return ERR_PTR(-EINVAL);
+
gpio = kzalloc(sizeof(*gpio), GFP_KERNEL);
if (!gpio)
return ERR_PTR(-ENOMEM);
@@ -253,6 +266,7 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
gpio->reg_stride = config->reg_stride;
gpio->reg_mask_xlate = config->reg_mask_xlate;
gpio->reg_field_xlate = config->reg_field_xlate;
+ gpio->to_irq = config->to_irq;
gpio->reg_dat_base = config->reg_dat_base;
gpio->reg_set_base = config->reg_set_base;
gpio->reg_clr_base = config->reg_clr_base;
@@ -302,6 +316,9 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config
chip->direction_output = gpio_regmap_direction_output;
}

+ if (gpio->to_irq)
+ chip->to_irq = gpio_regmap_to_irq;
+
ret = gpiochip_add_data(chip, gpio);
if (ret < 0)
goto err_free_gpio;
diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h
index 47acea8cca32..9755854d6747 100644
--- a/include/linux/gpio/regmap.h
+++ b/include/linux/gpio/regmap.h
@@ -45,6 +45,8 @@ struct regmap;
* to register, mask, and field values. If not given
* the default gpio_regmap_field_xlate() is used, which
* is implemented in terms of ->reg_mask_xlate.
+ * @to_irq: (Optional) hook for supporting custom IRQ mappings,
+ * behaves the same as the gpio_chip to_irq hook.
* @drvdata: (Optional) Pointer to driver specific data which is
* not used by gpio-remap but is provided "as is" to the
* driver callback(s).
@@ -102,6 +104,8 @@ struct gpio_regmap_config {
unsigned int *reg, unsigned int *mask,
unsigned int *values);

+ int (*to_irq)(struct gpio_regmap *gpio, unsigned int offset);
+
void *drvdata;
};

--
2.35.1