Skip to contentSkip to conversation

ACPI: Translate Linux IRQ number directly from GpioInt

PATCH Superseded Mika Westerberg 4 participants 10 messages Apr 28, 2015 – May 5, 2015

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ACPI: Translate Linux IRQ number directly from GpioInt

Hi,

Currently drivers for ACPI enumerated devices that have their interrupt line connected to a GPIO controller instead of IO-APIC are required to do complete gpiod_get()/gpiod_to_irq() etc. dance themselves. This adds unnecessary lines of code to these drivers.

It turned out that DT solved the problem already with introduction of of_irq_get() which is able to handle GPIO based interrupts as well through irqchip API [1].

Following two patches does the same for ACPI by introducing new function acpi_dev_gpio_irq_get() that is then used in I2C core to automatically translate ACPI GpioInt resource to Linux IRQ number.

This requires that the boot firmware (BIOS/coreboot) configures these pins correctly (input, etc) before handing over to OS. I've tested this on Intel Baytrail, Braswell and Skylake based machines where this is true.

[1] https://lkml.org/lkml/2015/3/25/103

Mika Westerberg (2):
gpio / ACPI: Add support for retrieving GpioInt resources from a device i2c / ACPI: Assign IRQ for devices that have GpioInt automatically

 drivers/gpio/gpiolib-acpi.c | 29 +++++++++++++++++++++++++++++
 drivers/i2c/i2c-core.c      |  9 +++++++--
 include/linux/acpi.h        |  7 +++++++
 3 files changed, 43 insertions(+), 2 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/2 gpio / ACPI: Add support for retrieving GpioInt resources from a device

ACPI specification knows two types of GPIOs: GpioIo and GpioInt. The latter is used to describe that a given device interrupt line is connected to a specific GPIO pin. Typical ACPI _CRS entry for such device looks like below:

    Name (_CRS, ResourceTemplate ()
    {
        I2cSerialBus (0x004A, ControllerInitiated, 0x00061A80,
                      AddressingMode7Bit, "\\_SB.PCI0.I2C6",
                      0x00, ResourceConsumer)
        GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
                IoRestrictionOutputOnly, "\\_SB.GPO0",
                0x00, ResourceConsumer)
        {
            0x004B
        }
        GpioInt (Level, ActiveLow, Shared, PullDefault, 0x0000,
                 "\\_SB.GPO0", 0x00, ResourceConsumer)
        {
            0x004C
        }
    })

Currently drivers need to request a GPIO corresponding to the right GpioInt and then translate that to Linux IRQ number. This adds unnecessary lines of boiler-plate code.

We can ease this a bit by introducing acpi_dev_gpio_irq_get() analogous to of_irq_get(). This function translates given GpioInt resource under the device in question to the suitable Linux IRQ number.

---
 drivers/gpio/gpiolib-acpi.c | 29 +++++++++++++++++++++++++++++
 include/linux/acpi.h        |  7 +++++++
 2 files changed, 36 insertions(+)
drivers/gpio/gpiolib-acpi.c +29 -0
index d2303d50f561..bff29bb0a3fe 100644
@@ -514,6 +514,35 @@ struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 	return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
 }
 
+/**
+ * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
+ * @adev: pointer to a ACPI device to get IRQ from
+ * @index: index of GpioInt resource (starting from %0)
+ *
+ * If the device has one or more GpioInt resources, this function can be
+ * used to translate from the GPIO offset in the resource to the Linux IRQ
+ * number.
+ *
+ * Return: Linux IRQ number (>%0) on success, negative errno on failure.
+ */
+int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
+{
+	int idx, i;
+
+	for (i = 0, idx = 0; idx <= index; i++) {
+		struct acpi_gpio_info info;
+		struct gpio_desc *desc;
+
+		desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
+		if (IS_ERR(desc))
+			break;
+		if (info.gpioint && idx++ == index)
+			return gpiod_to_irq(desc);
+	}
+	return -ENOENT;
+}
+EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
+
 static acpi_status
 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 			    u32 bits, u64 *value, void *handler_context,
include/linux/acpi.h +7 -0
index e4da5e35e29c..f57c440642cd 100644
@@ -721,6 +721,8 @@ static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
 	if (adev)
 		adev->driver_gpios = NULL;
 }
+
+int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index);
 #else
 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 			      const struct acpi_gpio_mapping *gpios)
@@ -728,6 +730,11 @@ static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 	return -ENXIO;
 }
 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
+
+static inline int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
+{
+	return -ENXIO;
+}
 #endif
 
 /* Device properties */
Signature · 1 line
2.1.4
#4 lore raw

PATCH 2/2 i2c / ACPI: Assign IRQ for devices that have GpioInt automatically

In reply to Mika Westerberg

Following what DT already does. If the device does not have ACPI Interrupt resource but instead it has one or more GpioInt resources listed below it, we take the first GpioInt resource, convert it to suitable Linux IRQ number and pass it to the driver instead.

This makes drivers simpler because the don't need to care about GPIOs at all if only thing they need is interrupt.

---
 drivers/i2c/i2c-core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/i2c/i2c-core.c +7 -2
index 987c124432c5..01ef731281e2 100644
@@ -632,8 +632,13 @@ static int i2c_device_probe(struct device *dev)
 	if (!client)
 		return 0;
 
-	if (!client->irq && dev->of_node) {
-		int irq = of_irq_get(dev->of_node, 0);
+	if (client->irq <= 0) {
+		int irq = -ENOENT;
+
+		if (dev->of_node)
+			irq = of_irq_get(dev->of_node, 0);
+		else if (ACPI_COMPANION(dev))
+			irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
 
 		if (irq == -EPROBE_DEFER)
 			return irq;
Signature · 1 line
2.1.4
#7 lore raw
In reply to Mika Westerberg
Quoted 11 lines · Mika Westerberg

Hi,

Currently drivers for ACPI enumerated devices that have their interrupt line connected to a GPIO controller instead of IO-APIC are required to do complete gpiod_get()/gpiod_to_irq() etc. dance themselves. This adds unnecessary lines of code to these drivers.

It turned out that DT solved the problem already with introduction of of_irq_get() which is able to handle GPIO based interrupts as well through irqchip API [1].

Quoted 7 lines

Following two patches does the same for ACPI by introducing new function acpi_dev_gpio_irq_get() that is then used in I2C core to automatically translate ACPI GpioInt resource to Linux IRQ number.

This requires that the boot firmware (BIOS/coreboot) configures these pins correctly (input, etc) before handing over to OS. I've tested this on Intel Baytrail, Braswell and Skylake based machines where this is true.

Why not configure the GPIO pin as input in the API itself ?

Quoted 12 lines

[1] https://lkml.org/lkml/2015/3/25/103

Mika Westerberg (2):
gpio / ACPI: Add support for retrieving GpioInt resources from a device i2c / ACPI: Assign IRQ for devices that have GpioInt automatically

  drivers/gpio/gpiolib-acpi.c | 29 +++++++++++++++++++++++++++++
  drivers/i2c/i2c-core.c      |  9 +++++++--
  include/linux/acpi.h        |  7 +++++++
  3 files changed, 43 insertions(+), 2 deletions(-)
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Android kernel developer
#8 lore raw
Quoted 5 lines · sathyanarayanan kuppuswamy
Quoted 3 lines

This requires that the boot firmware (BIOS/coreboot) configures these pins correctly (input, etc) before handing over to OS. I've tested this on Intel Baytrail, Braswell and Skylake based machines where this is true.

Why not configure the GPIO pin as input in the API itself ?

IIRC Octavian tried to do just that but it turned out that some of the irqchip functions are called with spinlock held, which means you can't call all gpiod_* functions there (as they might sleep).

#5 lore raw

Re: [PATCH 2/2] i2c / ACPI: Assign IRQ for devices that have GpioInt automatically

In reply to Mika Westerberg

drivers/i2c/i2c-core.c static int i2c_device_probe(struct device *dev) · line 636

Quoted 5 lines
drivers/i2c/i2c-core.c +2 -2
-	if (!client->irq && dev->of_node) {
-		int irq = of_irq_get(dev->of_node, 0);
+	if (client->irq <= 0) {
+		int irq = -ENOENT;

Why the move from !client->irq to <= 0? If I didn't miss something, interrupt numbers are still a sleeping dog with all the unsigned vs signed fuzz. If this change is needed, this needs proper description and ideally a seperate patch.

#6 lore raw

Re: [PATCH 2/2] i2c / ACPI: Assign IRQ for devices that have GpioInt automatically

Quoted 10 lines · Wolfram Sang

drivers/i2c/i2c-core.c static int i2c_device_probe(struct device *dev) · line 636

Quoted 5 lines
drivers/i2c/i2c-core.c +2 -2
-	if (!client->irq && dev->of_node) {
-		int irq = of_irq_get(dev->of_node, 0);
+	if (client->irq <= 0) {
+		int irq = -ENOENT;

Why the move from !client->irq to <= 0? If I didn't miss something, interrupt numbers are still a sleeping dog with all the unsigned vs signed fuzz. If this change is needed, this needs proper description and ideally a seperate patch.

It is there because ACPI parts of I2C client enumeration code initializes client->irq with -1. Alternatively we can change that code to use 0 for missing IRQ.

#9 lore raw
In reply to Mika Westerberg
Quoted 8 lines · Mika Westerberg
Quoted 4 lines · sathyanarayanan kuppuswamy
Quoted 3 lines

This requires that the boot firmware (BIOS/coreboot) configures these pins correctly (input, etc) before handing over to OS. I've tested this on Intel Baytrail, Braswell and Skylake based machines where this is true.

Why not configure the GPIO pin as input in the API itself ?

IIRC Octavian tried to do just that but it turned out that some of the irqchip functions are called with spinlock held, which means you can't call all gpiod_* functions there (as they might sleep).

Got it. Thanks.

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Android kernel developer
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] gpio / ACPI: Add support for retrieving GpioInt resources from a device

In reply to Mika Westerberg
Quoted 27 lines · Mika Westerberg

ACPI specification knows two types of GPIOs: GpioIo and GpioInt. The latter is used to describe that a given device interrupt line is connected to a specific GPIO pin. Typical ACPI _CRS entry for such device looks like below:

    Name (_CRS, ResourceTemplate ()
    {
        I2cSerialBus (0x004A, ControllerInitiated, 0x00061A80,
                      AddressingMode7Bit, "\\_SB.PCI0.I2C6",
                      0x00, ResourceConsumer)
        GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
                IoRestrictionOutputOnly, "\\_SB.GPO0",
                0x00, ResourceConsumer)
        {
            0x004B
        }
        GpioInt (Level, ActiveLow, Shared, PullDefault, 0x0000,
                 "\\_SB.GPO0", 0x00, ResourceConsumer)
        {
            0x004C
        }
    })

Currently drivers need to request a GPIO corresponding to the right GpioInt and then translate that to Linux IRQ number. This adds unnecessary lines of boiler-plate code.

Quoted 5 lines

We can ease this a bit by introducing acpi_dev_gpio_irq_get() analogous to of_irq_get(). This function translates given GpioInt resource under the device in question to the suitable Linux IRQ number.

Quoted 71 lines
---
 drivers/gpio/gpiolib-acpi.c | 29 +++++++++++++++++++++++++++++
 include/linux/acpi.h        |  7 +++++++
 2 files changed, 36 insertions(+)
drivers/gpio/gpiolib-acpi.c +29 -0
index d2303d50f561..bff29bb0a3fe 100644
@@ -514,6 +514,35 @@ struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 	return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
 }
 
+/**
+ * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
+ * @adev: pointer to a ACPI device to get IRQ from
+ * @index: index of GpioInt resource (starting from %0)
+ *
+ * If the device has one or more GpioInt resources, this function can be
+ * used to translate from the GPIO offset in the resource to the Linux IRQ
+ * number.
+ *
+ * Return: Linux IRQ number (>%0) on success, negative errno on failure.
+ */
+int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
+{
+	int idx, i;
+
+	for (i = 0, idx = 0; idx <= index; i++) {
+		struct acpi_gpio_info info;
+		struct gpio_desc *desc;
+
+		desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
+		if (IS_ERR(desc))
+			break;
+		if (info.gpioint && idx++ == index)
+			return gpiod_to_irq(desc);
+	}
+	return -ENOENT;
+}
+EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
+
 static acpi_status
 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 			    u32 bits, u64 *value, void *handler_context,
include/linux/acpi.h +7 -0
index e4da5e35e29c..f57c440642cd 100644
@@ -721,6 +721,8 @@ static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
 	if (adev)
 		adev->driver_gpios = NULL;
 }
+
+int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index);
 #else
 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 			      const struct acpi_gpio_mapping *gpios)
@@ -728,6 +730,11 @@ static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 	return -ENXIO;
 }
 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
+
+static inline int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
+{
+	return -ENXIO;
+}
 #endif
 
 /* Device properties */
Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#3 lore raw

Re: [PATCH 1/2] gpio / ACPI: Add support for retrieving GpioInt resources from a device

Quoted 28 lines · Rafael J. Wysocki
Quoted 27 lines · Mika Westerberg

ACPI specification knows two types of GPIOs: GpioIo and GpioInt. The latter is used to describe that a given device interrupt line is connected to a specific GPIO pin. Typical ACPI _CRS entry for such device looks like below:

    Name (_CRS, ResourceTemplate ()
    {
        I2cSerialBus (0x004A, ControllerInitiated, 0x00061A80,
                      AddressingMode7Bit, "\\_SB.PCI0.I2C6",
                      0x00, ResourceConsumer)
        GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
                IoRestrictionOutputOnly, "\\_SB.GPO0",
                0x00, ResourceConsumer)
        {
            0x004B
        }
        GpioInt (Level, ActiveLow, Shared, PullDefault, 0x0000,
                 "\\_SB.GPO0", 0x00, ResourceConsumer)
        {
            0x004C
        }
    })

Currently drivers need to request a GPIO corresponding to the right GpioInt and then translate that to Linux IRQ number. This adds unnecessary lines of boiler-plate code.

Quoted 7 lines
Quoted 5 lines

We can ease this a bit by introducing acpi_dev_gpio_irq_get() analogous to of_irq_get(). This function translates given GpioInt resource under the device in question to the suitable Linux IRQ number.

Thanks Rafael.

I'm going to change the other patch a bit to address conserns from Wolfram so that we will use irq == 0 to indicate an invalid interrupt instead of -1.