Linux gpiolib and GPIO Descriptor¶
Overview¶
Modern Linux GPIO drivers use the GPIO descriptor model rather than the legacy GPIO number model.
The GPIO subsystem is implemented by gpiolib.
gpiolib provides a hardware-independent abstraction layer between GPIO consumers and GPIO controller drivers.
Architecture¶
GPIO Controller Layer¶
GPIO controller drivers expose GPIO operations through a gpio_chip structure.
Typical callbacks:
struct gpio_chip {
int (*direction_input)(...);
int (*direction_output)(...);
int (*get)(...);
void (*set)(...);
};
Responsibilities:
- Configure GPIO direction
- Read GPIO state
- Write GPIO state
- Access GPIO controller registers
GPIO Descriptor Layer¶
A GPIO descriptor represents a single GPIO line.
Conceptually:
A descriptor contains metadata describing how a GPIO line should be accessed.
Typical information:
Why GPIO Descriptors Exist¶
Legacy GPIO APIs use GPIO numbers.
Example:
Problems:
The GPIO descriptor model solves these issues.
Example:
The descriptor already contains all required GPIO metadata.
GPIO Consumer Model¶
A GPIO consumer is a driver that uses a GPIO line.
Examples:
Consumer drivers should not access GPIO controller hardware directly.
Instead:
Active-Low Translation¶
Device Tree can define GPIO polarity.
Example:
Meaning:
Consumer drivers always operate on logical values.
Example:
gpiolib performs polarity translation before invoking gpio_chip callbacks.
Flow:
GPIO Ownership¶
A GPIO line may be owned by a consumer.
Example:
While owned:
Ownership prevents multiple drivers from controlling the same GPIO line simultaneously.
Device Tree GPIO Binding¶
GPIO resources are typically described in Device Tree.
Example:
Components:
GPIO Lookup Flow¶
The GPIO lookup process converts a Device Tree property into a GPIO descriptor.
Conceptually:
GPIO Descriptor Access Flow¶
Typical GPIO write flow:
gpiod_set_value()
↓
gpio_desc
↓
active-low translation
↓
gpio_chip->set()
↓
GPIO Controller Register
Typical GPIO read flow:
Relationship Between gpio_desc and gpio_chip¶
Example:
Each GPIO line is represented by its own descriptor.
Legacy GPIO Number Model¶
Legacy API:
Modern API:
The descriptor API is preferred for new Linux drivers.
Summary¶
gpiolib provides the abstraction layer between GPIO consumers and GPIO controller drivers.
gpio_chip represents a GPIO controller.
gpio_desc represents a GPIO line.
GPIO consumers operate through GPIO descriptors rather than GPIO numbers.