Day76 - gpiolib and GPIO Descriptor¶
Objective¶
Understand how Linux GPIO consumers access GPIO controllers through gpiolib, GPIO descriptors, and Device Tree GPIO bindings.
The goal is to understand how:
works internally.
Lab1 - Raspberry Pi GPIO Subsystem Investigation¶
Goal¶
Inspect the GPIO subsystem on Raspberry Pi 5.
Inspect GPIO Controllers¶
Example:
gpiochip0: GPIOs 569-622, parent: platform/1f000d0000.gpio, pinctrl-rp1:
gpio-569 (ID_SDA)
gpio-570 (ID_SCL)
...
gpio-601 (ETH_RST_N |phy-reset) out hi ACTIVE LOW
...
Observations¶
The system contains multiple GPIO controllers:
Each GPIO controller owns a range of GPIO lines.
Example:
Consumer Ownership¶
Example:
Meaning:
A GPIO line can be owned by a GPIO consumer.
Active-Low Example¶
Example:
Interpretation:
Lab2 - GPIO Descriptor Simulation¶
Goal¶
Implement a simplified GPIO descriptor layer.
Architecture¶
GPIO Descriptor¶
Implemented:
struct gpio_desc {
struct gpio_chip *chip;
unsigned int offset;
bool requested;
bool active_low;
const char *consumer;
};
Descriptor Table¶
Implemented:
Purpose:
Descriptor APIs¶
Implemented:
Ownership Tracking¶
Only one consumer may request a GPIO line.
Example:
Another request:
Result:
Active-Low Translation¶
Implemented:
Flow:
Verification¶
Active High:
Register:
Active Low:
Register:
Active Low:
Register:
Lab3 - Device Tree GPIO Binding Simulation¶
Goal¶
Simulate how Device Tree GPIO properties become GPIO descriptors.
Device Tree Example¶
Pseudo Device Tree Model¶
Implemented:
struct gpio_property {
const char *name;
struct gpio_chip *chip;
unsigned int offset;
bool active_low;
};
Example:
GPIO DT Specification¶
Implemented:
Lookup Flow¶
Implemented:
Flow:
Descriptor Acquisition¶
Implemented:
Flow:
Complete Flow¶
reset-gpios
↓
demo_of_get_gpio()
↓
gpio_dt_spec
↓
demo_devm_gpiod_get()
↓
gpio_desc
↓
demo_gpiod_set_value()
↓
gpio_chip->set()
↓
GPIO Controller
Results¶
Successfully demonstrated:
- GPIO descriptor ownership
- Active-low translation
- GPIO consumer abstraction
- Device Tree GPIO lookup
- GPIO descriptor acquisition
- GPIO controller access through gpio_chip
Key Concepts¶
gpio_chip¶
Represents:
Provides:
gpio_desc¶
Represents:
Stores:
gpiolib¶
Provides:
Conclusion¶
Modern Linux GPIO drivers should operate through GPIO descriptors.
GPIO consumers should not access GPIO controller hardware directly.
gpiolib provides the abstraction layer between GPIO consumers and GPIO controller drivers.