Skip to content

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:

gpio_request(17, "led");

This is not portable across boards. With gpiod, the driver requests a function:

led = devm_gpiod_get(dev, "led", GPIOD_OUT_LOW);

The Device Tree provides the mapping:

led-gpios = <&gpio 17 GPIO_ACTIVE_HIGH>;

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

"xxx" in driver → xxx-gpios in Device Tree

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:

devm_gpiod_get()
gpiod_to_irq()
devm_request_threaded_irq()
IRQ handler / threaded handler

Common Pitfalls

  • Do not mix legacy GPIO numbers and descriptor-based GPIO APIs in new drivers.
  • Make sure the con_id and Device Tree property name match.
  • Remember that GPIO_ACTIVE_LOW affects logical value semantics.
  • Use devm_* helpers when the resource lifetime is tied to the device.