GPIO and gpiod¶
The descriptor-based GPIO API (gpiod) is the modern Linux kernel interface for GPIO access. Instead of hardcoding GPIO numbers in a driver, the driver requests a GPIO by function name and lets firmware data, such as Device Tree, describe the actual hardware mapping.
What Problem It Solves¶
Legacy GPIO code often hardcoded board-specific GPIO numbers:
This is not portable across boards. With gpiod, the driver requests a function:
The Device Tree provides the mapping:
Core Concepts¶
| Concept | Description |
|---|---|
| GPIO descriptor | Kernel object representing one GPIO line. |
con_id |
Function name used by the driver, such as "led". |
xxx-gpios |
Device Tree property matched from con_id. |
| Active high/low | Logical polarity described by Device Tree flags. |
gpiod_set_value() |
Sets an output GPIO using logical value semantics. |
Naming Rule¶
Examples:
Driver con_id |
Device Tree Property |
|---|---|
"led" |
led-gpios |
"reset" |
reset-gpios |
"irq" |
irq-gpios |
GPIO IRQ Flow¶
GPIO can also be used as an interrupt source:
Common Pitfalls¶
- Do not mix legacy GPIO numbers and descriptor-based GPIO APIs in new drivers.
- Make sure the
con_idand Device Tree property name match. - Remember that
GPIO_ACTIVE_LOWaffects logical value semantics. - Use
devm_*helpers when the resource lifetime is tied to the device.