Linux GPIO Consumer Driver¶
Overview¶
Linux GPIO subsystem separates GPIO providers and GPIO consumers.
GPIO Consumer Driver
↓
gpio_desc
↓
gpiolib
↓
gpio_chip
↓
GPIO Controller Driver
↓
Hardware Registers
The GPIO controller driver exposes GPIO lines through gpio_chip.
Consumer drivers acquire GPIO resources from Device Tree and access them through GPIO descriptors.
GPIO Controller vs GPIO Consumer¶
GPIO Controller Driver¶
Responsible for:
- Registering GPIO lines
- Managing GPIO hardware
- Implementing GPIO operations
Typical structure:
struct gpio_chip {
int (*direction_input)(...);
int (*direction_output)(...);
int (*get)(...);
void (*set)(...);
};
Examples:
- SoC GPIO controller driver
- I2C GPIO expander driver
- SPI GPIO expander driver
GPIO Consumer Driver¶
Responsible for:
- Requesting GPIO resources
- Configuring GPIO direction
- Reading GPIO inputs
- Controlling GPIO outputs
Consumer drivers never access GPIO controller hardware directly.
Instead they use GPIO descriptor APIs.
Device Tree GPIO Binding¶
GPIO resources are described in Device Tree.
Example:
my-device {
reset-gpios = <&gpio0 7 GPIO_ACTIVE_LOW>;
enable-gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>;
};
A GPIO specifier contains:
Meaning:
| Field | Description |
|---|---|
| GPIO controller | GPIO provider |
| offset | GPIO line number within controller |
| flags | Active-high or active-low configuration |
GPIO Descriptor Model¶
Consumer drivers do not operate GPIO numbers directly.
Instead:
Each GPIO line is represented by a descriptor.
Conceptually:
The descriptor layer provides:
- Ownership tracking
- Active-low translation
- GPIO abstraction
GPIO Consumer APIs¶
Request GPIO¶
Mandatory GPIO:
Looks up:
from Device Tree.
Optional GPIO¶
Optional GPIO:
Looks up:
If the property does not exist:
is returned.
Configure Direction¶
Output:
Input:
Set Output Value¶
Logical values are used.
For active-low GPIOs:
Read Input Value¶
Returned value is a logical value.
GPIO Consumer Driver Flow¶
Typical probe flow:
Example:
reset_gpio =
devm_gpiod_get(dev, "reset");
enable_gpio =
devm_gpiod_get_optional(dev, "enable");
gpiod_direction_output(reset_gpio, 1);
gpiod_direction_output(enable_gpio, 1);
gpiod_set_value(reset_gpio, 0);
Remove Flow¶
Typical remove flow:
Example:
Relationship with Platform Drivers¶
GPIO consumer APIs are commonly used inside platform drivers.
This is the standard GPIO access model used throughout the Linux kernel.