Skip to content

Day78 - GPIO IRQ Consumer Driver

Goal

Understand how Linux GPIO consumer drivers obtain interrupt resources and register interrupt handlers.


Topics Studied

GPIO Interrupt Resources

A GPIO line can be used as an interrupt source.

Typical flow:

GPIO input
GPIO Controller
IRQ Mapping
Linux IRQ
ISR

GPIO to IRQ Conversion

Learned how a GPIO descriptor is converted into a Linux IRQ number.

irq = gpiod_to_irq(desc);

Key understanding:

GPIO Number
Linux IRQ Number

The GPIO controller and IRQ subsystem provide the mapping.


IRQ Registration

Learned the purpose of:

request_irq()

and:

free_irq()

Driver lifecycle:

probe()
request_irq()
interrupt handling
free_irq()
remove()

Raspberry Pi Investigation

Inspected:

cat /proc/interrupts

and:

sudo cat /sys/kernel/debug/gpio

Observed:

GPIO553 (pwr_button)
Linux IRQ161

This confirms that GPIO resources and IRQ resources are different kernel objects.


Lab1 - GPIO IRQ Investigation

Investigated:

  • /proc/interrupts
  • GPIO controller ownership
  • GPIO consumers
  • Linux IRQ numbers

Key observation:

GPIO553
IRQ Mapping
IRQ161

Lab2 - gpiod_to_irq() Simulation

Implemented:

gpio_irq_register_map()
gpio_irq_unregister_all()
gpiod_to_irq()

Created GPIO-to-IRQ mapping table:

GPIO5 → IRQ100
GPIO7 → IRQ101

Verified descriptor conversion:

button GPIO5: irq=100
reset GPIO10: error=-2

Lab3 - GPIO IRQ Consumer Driver Simulation

Implemented:

request_irq()
free_irq()
irq_trigger()

Created:

struct irq_desc

to simulate IRQ registration.

Implemented simulated driver lifecycle:

probe()
gpiod_get()
gpiod_to_irq()
request_irq()

remove()
free_irq()
gpiod_put()

Validation result:

[PROBE] button irq=100
[ISR] irq=100 count=1
[ISR] irq=100 count=2
[REMOVE] free IRQ
[TEST] irq_trigger after remove ret=-2

Key Takeaways

GPIO interrupt consumer drivers obtain interrupt resources through GPIO descriptors.

Core flow:

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

The GPIO subsystem provides GPIO resources, while the IRQ subsystem provides interrupt delivery.

Both subsystems are connected through GPIO-to-IRQ mapping.


Files Added

Notes

docs/notes/gpio-irq-consumer-driver.md

Labs

docs/labs/day78/day78-gpio-irq-consumer-driver.md

Source Code

d78-gpio-irq-consumer/
utils/gpio-sim/gpio_irq.c
utils/gpio-sim/gpio_irq.h

Next Step

Day79 - IRQ Subsystem Fundamentals

Topics:

  • IRQ architecture
  • irq_desc
  • irq_action
  • Edge vs Level trigger
  • IRQ flow
  • Top Half
  • Bottom Half
  • Threaded IRQ