Day78 - GPIO IRQ Consumer Driver¶
Objective¶
Learn how a GPIO consumer driver obtains interrupt resources from a GPIO descriptor and registers an interrupt handler.
Topics covered:
- GPIO interrupt resources
- GPIO to IRQ mapping
gpiod_to_irq()request_irq()free_irq()- GPIO IRQ consumer driver flow
- Probe and remove lifecycle
Lab1 - Raspberry Pi GPIO IRQ Investigation¶
Goal¶
Investigate the relationship between GPIO resources and Linux IRQ resources on Raspberry Pi.
Commands¶
Observation¶
Example from Raspberry Pi 5:
GPIO debug information:
Finding¶
GPIO numbers and IRQ numbers are different resources.
Lab2 - GPIO Descriptor to IRQ Mapping¶
Goal¶
Simulate Linux gpiod_to_irq().
Architecture¶
IRQ Mapping Structure¶
APIs¶
Test¶
Register mappings:
Convert GPIO descriptor:
Result¶
Conclusion¶
gpiod_to_irq() converts a GPIO descriptor into a Linux IRQ resource.
Lab3 - GPIO IRQ Consumer Driver Simulation¶
Goal¶
Simulate a Linux GPIO interrupt consumer driver.
Driver Structure¶
IRQ Descriptor¶
IRQ APIs¶
ISR¶
static int button_isr(
int irq,
void *dev_id)
{
struct demo_button_device *button = dev_id;
button->irq_count++;
printf(
"[ISR] irq=%d count=%d\n",
irq,
button->irq_count);
return 0;
}
Probe Flow¶
Implementation:
button->button_gpio =
gpiod_get(chip, 5, "button", false);
button->irq =
gpiod_to_irq(button->button_gpio);
request_irq(
button->irq,
button_isr,
"demo-button",
button);
Remove Flow¶
Implementation:
Test Result¶
Output:
[PROBE] button irq=100
[ISR] irq=100 count=1
[ISR] irq=100 count=2
[REMOVE] free IRQ
[TEST] irq_trigger after remove ret=-2
Validation¶
Interrupt registration:
ISR execution:
IRQ release:
IRQ trigger after removal:
Key Takeaways¶
GPIO interrupt consumer drivers follow the sequence:
The GPIO descriptor identifies a GPIO resource, while the IRQ subsystem manages interrupt delivery.
A GPIO line and a Linux IRQ number are different resources connected through GPIO-to-IRQ mapping.