Skip to content

GPIO IRQ Consumer Driver

A GPIO IRQ consumer driver uses a GPIO line as an interrupt source. Instead of polling a GPIO input periodically, the driver converts the GPIO descriptor into an IRQ and registers an interrupt handler.

Motivation

Without interrupts:

Driver
Periodic polling
GPIO input

With interrupts:

GPIO event
IRQ
ISR
Driver action

Interrupt-driven designs reduce CPU usage and improve response latency.

GPIO IRQ Consumer Flow

Device Tree
devm_gpiod_get()
gpio_desc
gpiod_to_irq()
Linux IRQ number
request_irq()
ISR

GPIO and IRQ Relationship

A GPIO line is not the same as a Linux IRQ number.

Example:

GPIO line
    GPIO5
GPIO Controller
IRQ Mapping
Linux IRQ 100

The mapping is provided by the GPIO controller and IRQ subsystem.

GPIO Descriptor to IRQ

Drivers typically obtain a GPIO descriptor first:

button = devm_gpiod_get(dev, "button", GPIOD_IN);

Then convert it into an IRQ:

irq = gpiod_to_irq(button);

The returned IRQ number is used by the Linux IRQ subsystem.

IRQ Registration

After obtaining the IRQ number:

ret = request_irq(
        irq,
        button_isr,
        IRQF_TRIGGER_RISING,
        "button",
        dev);

The driver provides:

  • IRQ number
  • Interrupt handler
  • Trigger type
  • Device context

When the interrupt occurs, the handler is executed.

IRQ Release

During driver removal:

free_irq(irq, dev);

The IRQ handler is unregistered and will no longer receive interrupts.

Probe Flow

probe()
request GPIO
gpiod_to_irq()
request_irq()
device ready

Example:

button = devm_gpiod_get(dev, "button", GPIOD_IN);

irq = gpiod_to_irq(button);

ret = request_irq(
        irq,
        button_isr,
        IRQF_TRIGGER_RISING,
        "button",
        dev);

Remove Flow

remove()
free_irq()
release GPIO
cleanup

Example:

free_irq(irq, dev);

Common Trigger Types

Trigger Type Description
IRQF_TRIGGER_RISING Interrupt on rising edge
IRQF_TRIGGER_FALLING Interrupt on falling edge
IRQF_TRIGGER_HIGH Interrupt while signal is high
IRQF_TRIGGER_LOW Interrupt while signal is low

Raspberry Pi Observation

The Raspberry Pi GPIO debug information and interrupt table show that GPIO numbers and IRQ numbers are different resources.

Example:

GPIO553 (pwr_button)
IRQ Mapping
Linux IRQ 161

This demonstrates the role of gpiod_to_irq() in converting a GPIO resource into an interrupt resource.

  • Linux GPIO Consumer Driver
  • GPIO Controller Architecture
  • GPIO Descriptor and gpiolib
  • IRQ Subsystem
  • Threaded IRQ