Configure GPIO on Rockchip: Pin Numbering, Device Tree and libgpiod
Convert GPIO3_B5-style names to Linux line numbers, drive pins with libgpiod, and describe them properly in the device tree with pinctrl and overlays.
Rockchip pin names
Rockchip GPIOs are grouped into banks GPIO0 to GPIO4, each with 32 pins in four ports A–D of 8 pins. A schematic net like GPIO3_B5 means bank 3, port B, pin 5.
line offset in bank = port_index × 8 + pin (A=0, B=1, C=2, D=3)
global GPIO number = bank × 32 + line offset
GPIO3_B5 → offset 1×8+5 = 13 → global 3×32+13 = 109
Using a pin from user space (libgpiod)
sudo apt install -y gpiod
gpiodetect # gpiochip0..4 = banks GPIO0..GPIO4
gpioinfo gpiochip3 # shows line 13 and whether it's in use
# libgpiod v1 syntax
gpioset gpiochip3 13=1
gpioget gpiochip3 13
# libgpiod v2 syntax
gpioset -c gpiochip3 13=1
If gpioinfo shows the line as used by a kernel driver, or the pin is muxed to another function (UART, PWM…), you need a device-tree change first.
Describing GPIOs in the device tree
Use the Rockchip macros from dt-bindings/pinctrl/rockchip.h and dt-bindings/gpio/gpio.h. Here is an LED on GPIO3_B5 plus a button on GPIO0_C0:
/ {
leds {
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&status_led_pin>;
status-led {
gpios = <&gpio3 RK_PB5 GPIO_ACTIVE_HIGH>;
linux,default-trigger = "heartbeat";
};
};
};
&pinctrl {
leds {
status_led_pin: status-led-pin {
rockchip,pins = <3 RK_PB5 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};
Doing it as an overlay
Overlays keep one base DTB for every product variant:
/dts-v1/;
/plugin/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
&{/} {
my-leds {
compatible = "gpio-leds";
relay {
gpios = <&gpio3 RK_PB5 GPIO_ACTIVE_HIGH>;
default-state = "off";
};
};
};
# Preprocess (for #include) and compile
cpp -nostdinc -I include -undef -x assembler-with-cpp my.dts > my.pre.dts
dtc -@ -I dts -O dtb -o my.dtbo my.pre.dts
Load it via your image's mechanism: an fdtoverlays line in extlinux.conf, the vendor's overlay config file, or U-Boot's fdt apply.
Voltage domains matter
Each GPIO bank sits in an IO power domain powered at 1.8 V or 3.3 V (see the io-domains node and your schematic). Driving a 3.3 V peripheral from a 1.8 V domain, or the reverse, is a common bring-up mistake. Check before you wire.
Verify what the kernel got
sudo cat /sys/kernel/debug/pinctrl/pinctrl-rockchip-pinctrl/pinmux-pins | grep -i "gpio3"
dtc -I fs /proc/device-tree 2>/dev/null | lessHai trovato un errore o vuoi aggiungere qualcosa? Suggerisci un miglioramento.