Skip to contentSkip to conversation

gpio: hierarchical IRQ improvements

PATCH In review Brian Masney 2 participants 10 messages Jul 8, 2019 – Aug 7, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover gpio: hierarchical IRQ improvements

This builds on top of Linus Walleij's existing patches that adds hierarchical IRQ support to the GPIO core [1] so that Qualcomm's spmi-gpio and ssbi-gpio can be converted to use these new helpers.

Linus: Feel free to squash these into your existing patches if you'd like to use any of this code. Just give me some kind of mention in the commit description.

[1] https://lore.kernel.org/linux-gpio/[email protected]/

Brian Masney (4):
gpio: introduce gpiochip_populate_parent_fwspec_{two,four}cell
functions
gpio: allow customizing hierarchical IRQ chips
gpio: use handler in gpio_irq_chip instead of handle_bad_irq
qcom: spmi-gpio: convert to hierarchical IRQ helpers in gpio core

 drivers/gpio/gpiolib.c                   | 78 ++++++++++++++++----
 drivers/pinctrl/qcom/Kconfig             |  1 +
 drivers/pinctrl/qcom/pinctrl-spmi-gpio.c | 92 +++++++-----------------
 include/linux/gpio/driver.h              | 65 +++++++++++++++++
 4 files changed, 154 insertions(+), 82 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/4 gpio: introduce gpiochip_populate_parent_fwspec_{two,four}cell functions

Introduce the functions gpiochip_populate_parent_fwspec_{two,four}cell that will be used for hierarchical IRQ support in GPIO drivers.

---
 drivers/gpio/gpiolib.c      | 24 ++++++++++++++++++++++++
 include/linux/gpio/driver.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
drivers/gpio/gpiolib.c +24 -0
index d767e111694d..06c9cf714c99 100644
@@ -1946,6 +1946,30 @@ static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
 	return !!gc->irq.parent_domain;
 }
 
+void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
+					     struct irq_fwspec *fwspec,
+					     unsigned int parent_hwirq,
+					     unsigned int parent_type)
+{
+	fwspec->param_count = 2;
+	fwspec->param[0] = parent_hwirq;
+	fwspec->param[1] = parent_type;
+}
+EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
+
+void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
+					      struct irq_fwspec *fwspec,
+					      unsigned int parent_hwirq,
+					      unsigned int parent_type)
+{
+	fwspec->param_count = 4;
+	fwspec->param[0] = 0;
+	fwspec->param[1] = parent_hwirq;
+	fwspec->param[2] = 0;
+	fwspec->param[3] = parent_type;
+}
+EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
+
 #else
 
 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
include/linux/gpio/driver.h +30 -0
index 682ad09e29f4..6b6bca20c8f9 100644
@@ -493,6 +493,36 @@ struct bgpio_pdata {
 	int ngpio;
 };
 
+#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
+
+void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
+					     struct irq_fwspec *fwspec,
+					     unsigned int parent_hwirq,
+					     unsigned int parent_type);
+void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
+					      struct irq_fwspec *fwspec,
+					      unsigned int parent_hwirq,
+					      unsigned int parent_type);
+
+#else
+
+static void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
+						    struct irq_fwspec *fwspec,
+						    unsigned int parent_hwirq,
+						    unsigned int parent_type)
+{
+}
+
+static void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
+						     struct irq_fwspec *fwspec,
+						     unsigned int parent_hwirq,
+						     unsigned int parent_type)
+{
+}
+
+#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
+
+
 #if IS_ENABLED(CONFIG_GPIO_GENERIC)
 
 int bgpio_init(struct gpio_chip *gc, struct device *dev,
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/4 gpio: allow customizing hierarchical IRQ chips

In reply to Brian Masney

Now that the GPIO core has support for hierarchical IRQ chips, let's add support for three new callbacks in struct gpio_irq_chip:

populate_parent_fwspec:
This optional callback populates the struct irq_fwspec for the parent's IRQ domain. If this is not specified, then gpiochip_populate_parent_fwspec_twocell will be used. A four-cell variant named &gpiochip_populate_parent_fwspec_twocell is also available.

child_pin_to_irq:
This optional callback is used to translate the child's GPIO pin number to an IRQ number for the GPIO to_irq() callback. If this is not specified, then a default callback will be provided that returns the pin number.

child_irq_domain_ops:
The IRQ domain operations that will be used for this GPIO IRQ chip. If no operations are provided, then default callbacks will be populated to setup the IRQ hierarchy. Some drivers need to supply their own translate function.

These will be initially used by Qualcomm's spmi-gpio and ssbi-gpio.

---
Note: checkpatch doesn't like that child_irq_domain_ops is not const.

 drivers/gpio/gpiolib.c      | 52 +++++++++++++++++++++++++++----------
 include/linux/gpio/driver.h | 35 +++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 14 deletions(-)
drivers/gpio/gpiolib.c +38 -14
index 06c9cf714c99..5423242deb81 100644
@@ -1778,7 +1778,7 @@ static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
 
 			fwspec.fwnode = gc->irq.fwnode;
 			/* This is the hwirq for the GPIO line side of things */
-			fwspec.param[0] = i;
+			fwspec.param[0] = girq->child_pin_to_irq(gc, i);
 			/* Just pick something */
 			fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
 			fwspec.param_count = 2;
@@ -1841,7 +1841,7 @@ static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
 
 	chip_info(gc, "called %s\n", __func__);
 
-	ret = gpiochip_hierarchy_irq_domain_translate(d, fwspec, &hwirq, &type);
+	ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
 	if (ret)
 		return ret;
 
@@ -1882,10 +1882,9 @@ static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
 		 * all together up the chain.
 		 */
 		parent_fwspec.fwnode = d->parent->fwnode;
-		parent_fwspec.param_count = 2;
-		parent_fwspec.param[0] = parent_hwirq;
 		/* This parent only handles asserted level IRQs */
-		parent_fwspec.param[1] = parent_type;
+		girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq,
+					     parent_type);
 		chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
 			  irq + i, parent_hwirq);
 		ret = irq_domain_alloc_irqs_parent(d, irq + i, 1,
@@ -1899,13 +1898,29 @@ static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
 	return 0;
 }
 
-static const struct irq_domain_ops gpiochip_hierarchy_domain_ops = {
-	.activate = gpiochip_irq_domain_activate,
-	.deactivate = gpiochip_irq_domain_deactivate,
-	.translate = gpiochip_hierarchy_irq_domain_translate,
-	.alloc = gpiochip_hierarchy_irq_domain_alloc,
-	.free = irq_domain_free_irqs_common,
-};
+static unsigned int gpiochip_child_pin_to_irq_noop(struct gpio_chip *chip,
+						   unsigned int pin)
+{
+	return pin;
+}
+
+static void gpiochip_add_default_irq_domain_ops(struct irq_domain_ops *ops)
+{
+	if (!ops->activate)
+		ops->activate = gpiochip_irq_domain_activate;
+
+	if (!ops->deactivate)
+		ops->deactivate = gpiochip_irq_domain_deactivate;
+
+	if (!ops->translate)
+		ops->translate = gpiochip_hierarchy_irq_domain_translate;
+
+	if (!ops->alloc)
+		ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
+
+	if (!ops->free)
+		ops->free = irq_domain_free_irqs_common;
+}
 
 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
 {
@@ -1921,12 +1936,21 @@ static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
 		return -EINVAL;
 	}
 
+	if (!gc->irq.child_pin_to_irq)
+		gc->irq.child_pin_to_irq = gpiochip_child_pin_to_irq_noop;
+
+	if (!gc->irq.populate_parent_fwspec)
+		gc->irq.populate_parent_fwspec =
+			gpiochip_populate_parent_fwspec_twocell;
+
+	gpiochip_add_default_irq_domain_ops(&gc->irq.child_irq_domain_ops);
+
 	gc->irq.domain = irq_domain_create_hierarchy(
 		gc->irq.parent_domain,
 		IRQ_DOMAIN_FLAG_HIERARCHY,
 		gc->ngpio,
 		gc->irq.fwnode,
-		&gpiochip_hierarchy_domain_ops,
+		&gc->irq.child_irq_domain_ops,
 		gc);
 
 	if (!gc->irq.domain) {
@@ -2106,7 +2130,7 @@ static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
 
 		spec.fwnode = domain->fwnode;
 		spec.param_count = 2;
-		spec.param[0] = offset;
+		spec.param[0] = chip->irq.child_pin_to_irq(chip, offset);
 		spec.param[1] = IRQ_TYPE_NONE;
 
 		return irq_create_fwspec_mapping(&spec);
include/linux/gpio/driver.h +35 -0
index 6b6bca20c8f9..fb9c126dd8f0 100644
@@ -93,6 +93,41 @@ struct gpio_irq_chip {
 				     unsigned int child_type,
 				     unsigned int *parent_hwirq,
 				     unsigned int *parent_type);
+
+	/**
+	 * @populate_parent_fwspec:
+	 *
+	 * This optional callback populates the &struct irq_fwspec for the
+	 * parent's IRQ domain. If this is not specified, then
+	 * &gpiochip_populate_parent_fwspec_twocell will be used. A four-cell
+	 * variant named &gpiochip_populate_parent_fwspec_twocell is also
+	 * available.
+	 */
+	void (*populate_parent_fwspec)(struct gpio_chip *chip,
+				       struct irq_fwspec *fwspec,
+				       unsigned int parent_hwirq,
+				       unsigned int parent_type);
+
+	/**
+	 * @child_pin_to_irq:
+	 *
+	 * This optional callback is used to translate the child's GPIO pin
+	 * number to an IRQ number for the GPIO to_irq() callback. If this is
+	 * not specified, then a default callback will be provided that
+	 * returns the pin number.
+	 */
+	unsigned int (*child_pin_to_irq)(struct gpio_chip *chip,
+					 unsigned int pin);
+
+	/**
+	 * @child_irq_domain_ops:
+	 *
+	 * The IRQ domain operations that will be used for this GPIO IRQ
+	 * chip. If no operations are provided, then default callbacks will
+	 * be populated to setup the IRQ hierarchy. Some drivers need to
+	 * supply their own translate function.
+	 */
+	struct irq_domain_ops child_irq_domain_ops;
 #endif
 
 	/**
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH 2/4] gpio: allow customizing hierarchical IRQ chips

Quoted 22 lines · Brian Masney

Now that the GPIO core has support for hierarchical IRQ chips, let's add support for three new callbacks in struct gpio_irq_chip:

populate_parent_fwspec:
This optional callback populates the struct irq_fwspec for the parent's IRQ domain. If this is not specified, then gpiochip_populate_parent_fwspec_twocell will be used. A four-cell variant named &gpiochip_populate_parent_fwspec_twocell is also available.

child_pin_to_irq:
This optional callback is used to translate the child's GPIO pin number to an IRQ number for the GPIO to_irq() callback. If this is not specified, then a default callback will be provided that returns the pin number.

child_irq_domain_ops:
The IRQ domain operations that will be used for this GPIO IRQ chip. If no operations are provided, then default callbacks will be populated to setup the IRQ hierarchy. Some drivers need to supply their own translate function.

Quoted 3 lines

These will be initially used by Qualcomm's spmi-gpio and ssbi-gpio.

This is overall looking very appetizing!

I want to apply this on top of my patch and respin it with some of Masahiro's comments as well and then let's try to just apply all of this.

Quoted 1 line

Note: checkpatch doesn't like that child_irq_domain_ops is not const.

Hm? I suspect some janitor will find the problem and patch it for us.

Quoted 7 lines
drivers/gpio/gpiolib.c +7 -0
+static void gpiochip_add_default_irq_domain_ops(struct irq_domain_ops *ops)
+{
+       if (!ops->activate)
+               ops->activate = gpiochip_irq_domain_activate;
+
+       if (!ops->deactivate)
+               ops->deactivate = gpiochip_irq_domain_deactivate;

drivers/gpio/gpiolib.c static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d, · line 1923

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+
+       if (!ops->translate)
+               ops->translate = gpiochip_hierarchy_irq_domain_translate;
+
+       if (!ops->alloc)
+               ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
+
+       if (!ops->free)
+               ops->free = irq_domain_free_irqs_common;
+}

I'm fine with translate(), this seems to be what Lina needs too.

But do we really need to make them all optional? activate() and deactivate() will require the driver to lock the irq etc which is hairy.

Yours,
Linus Walleij

#4 lore raw

Re: [PATCH 2/4] gpio: allow customizing hierarchical IRQ chips

Quoted 21 lines · Linus Walleij
Quoted 7 lines · Brian Masney
drivers/gpio/gpiolib.c +7 -0
+static void gpiochip_add_default_irq_domain_ops(struct irq_domain_ops *ops)
+{
+       if (!ops->activate)
+               ops->activate = gpiochip_irq_domain_activate;
+
+       if (!ops->deactivate)
+               ops->deactivate = gpiochip_irq_domain_deactivate;

drivers/gpio/gpiolib.c static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d, · line 1923

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+
+       if (!ops->translate)
+               ops->translate = gpiochip_hierarchy_irq_domain_translate;
+
+       if (!ops->alloc)
+               ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
+
+       if (!ops->free)
+               ops->free = irq_domain_free_irqs_common;
+}

I'm fine with translate(), this seems to be what Lina needs too.

Quoted 2 lines

But do we really need to make them all optional? activate() and deactivate() will require the driver to lock the irq etc which is hairy.

I can't think of a reason right now that we'd need to override the others. Since the structure was there, I made all of them optional to try to future proof this a little bit.

It probably would be better to only make translate optional at this point. If needed, someone can submit a patch for one or more of the others with their use case.

Brian

#5 lore raw

PATCH 3/4 gpio: use handler in gpio_irq_chip instead of handle_bad_irq

In reply to Brian Masney

Use the IRQ handler field that's available in the struct gpio_irq_chip when allocating an IRQ rather than hardcoding the handler to handle_bad_irq(). The kernel reboots without any messages when testing this using spmi-gpio on the Nexus 5.

---
I didn't have time to dig into more detail about why this is happening. I suspect the issue is that __irq_do_set_handler() has a special check for handle_bad_irq:

https://elixir.bootlin.com/linux/latest/source/kernel/irq/chip.c#L974

My post about this:
https://lore.kernel.org/linux-gpio/[email protected]/

 drivers/gpio/gpiolib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib.c +1 -1
index 5423242deb81..bc68ebb8f40e 100644
@@ -1872,7 +1872,7 @@ static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
 				    hwirq + i,
 				    gc->irq.chip,
 				    gc,
-				    handle_bad_irq,
+				    girq->handler,
 				    NULL, NULL);
 		irq_set_probe(irq + i);
 
Signature · 1 line
2.20.1
#6 lore raw

PATCH 4/4 qcom: spmi-gpio: convert to hierarchical IRQ helpers in gpio core

In reply to Brian Masney

Now that the GPIO core has support for hierarchical IRQ chips, convert Qualcomm's spmi-gpio over to use these new helpers to reduce duplicated code across drivers.

This change was tested on a LG Nexus 5 (hammerhead) phone.

---
 drivers/pinctrl/qcom/Kconfig             |  1 +
 drivers/pinctrl/qcom/pinctrl-spmi-gpio.c | 92 +++++++-----------------
 2 files changed, 26 insertions(+), 67 deletions(-)
drivers/pinctrl/qcom/Kconfig +1 -0
index 27ab585a639c..9b36da702925 100644
@@ -138,6 +138,7 @@ config PINCTRL_QCOM_SPMI_PMIC
        select PINMUX
        select PINCONF
        select GENERIC_PINCONF
+       select GPIOLIB_IRQCHIP
        select IRQ_DOMAIN_HIERARCHY
        help
          This is the pinctrl, pinmux, pinconf and gpiolib driver for the
drivers/pinctrl/qcom/pinctrl-spmi-gpio.c +25 -67

149 lines not on this page · raw message

Signature · 1 line
2.20.1
#7 lore raw
In reply to Brian Masney
Quoted 8 lines · Brian Masney

This builds on top of Linus Walleij's existing patches that adds hierarchical IRQ support to the GPIO core [1] so that Qualcomm's spmi-gpio and ssbi-gpio can be converted to use these new helpers.

Linus: Feel free to squash these into your existing patches if you'd like to use any of this code. Just give me some kind of mention in the commit description.

Quoted 8 lines

[1] https://lore.kernel.org/linux-gpio/[email protected]/

Brian Masney (4):
gpio: introduce gpiochip_populate_parent_fwspec_{two,four}cell
functions
gpio: allow customizing hierarchical IRQ chips
gpio: use handler in gpio_irq_chip instead of handle_bad_irq
qcom: spmi-gpio: convert to hierarchical IRQ helpers in gpio core

I solved things like this:

- I kept patches 1 & 4 as-is
- I squashed patches 2 and 3 into the main patch with minor modifications.
- I added Co-developed-by: for your contributions

Now I need to address Masahiro's comments on top and let's see if the result looks acceptable!

Yours,
Linus Walleij

#8 lore raw
Quoted 19 lines · Linus Walleij
Quoted 8 lines · Brian Masney

This builds on top of Linus Walleij's existing patches that adds hierarchical IRQ support to the GPIO core [1] so that Qualcomm's spmi-gpio and ssbi-gpio can be converted to use these new helpers.

Linus: Feel free to squash these into your existing patches if you'd like to use any of this code. Just give me some kind of mention in the commit description.

Quoted 8 lines

[1] https://lore.kernel.org/linux-gpio/[email protected]/

Brian Masney (4):
gpio: introduce gpiochip_populate_parent_fwspec_{two,four}cell
functions
gpio: allow customizing hierarchical IRQ chips
gpio: use handler in gpio_irq_chip instead of handle_bad_irq
qcom: spmi-gpio: convert to hierarchical IRQ helpers in gpio core

Quoted 3 lines

I solved things like this:

- I kept patches 1 & 4 as-is

Ooops had to squash patch 1 as well...

Yours,
Linus Walleij

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

This builds on top of Linus Walleij's existing patches that adds hierarchical IRQ support to the GPIO core [1] so that Qualcomm's spmi-gpio and ssbi-gpio can be converted to use these new helpers.

Linus: Feel free to squash these into your existing patches if you'd like to use any of this code. Just give me some kind of mention in the commit description.

Quoted 8 lines

[1] https://lore.kernel.org/linux-gpio/[email protected]/

Brian Masney (4):
gpio: introduce gpiochip_populate_parent_fwspec_{two,four}cell
functions
gpio: allow customizing hierarchical IRQ chips
gpio: use handler in gpio_irq_chip instead of handle_bad_irq
qcom: spmi-gpio: convert to hierarchical IRQ helpers in gpio core

I solved things like this:

Quoted 8 lines

- I kept patches 1 & 4 as-is
- I squashed patches 2 and 3 into the main patch with minor modifications.
- I added Co-developed-by: for your contributions

Now I need to address Masahiro's comments on top and let's see if the result looks acceptable!

Ooops had to squash patch 1 as well...

All of this sounds good. I'll retest once you send out the updated series.

Brian