GPIO Interface
1. GPIO Introduction
GPIO stands for General Purpose I/O, which refers to general-purpose input/output ports. Simply put, these are pins that can be controlled by MCU/CPU. These pins usually have multiple functions, the most basic being high/low level input detection and output. Some pins are also bound to on-chip peripherals of the controller, such as communication pins for serial ports, I2C, network, and voltage detection.
2. GPIO Naming
Rockchip Pin IDs are composed of controller(bank) + port + pin number.
- The number of controllers is consistent with the number of GPIO controllers.
- Ports are fixed at A, B, C, and D, each port has only 8 pin numbers (a=0, b=1, c=2, d=3).
- Pin numbers are fixed at 0, 1, 2, 3, 4, 5, 6, 7.
RK3568 has 5 GPIO controllers, each controller can control 32 IOs. When used as GPIO, port behavior is configured by GPIO controller registers.
Tips
GPIO1_A4 means: controller group 1, port A, pin number 4. The pin number calculation formula is 32 x 1 + 0 x 8 + 4 = 36
3. Using GPIO sysfs Interface to Control IO
Command line method
In Linux, the most common way to read and write GPIO is using GPIO sysfs interface, which is implemented by operating files like export, unexport, gpio{N}/direction, gpio{N}/value in the /sys/class/gpio directory (replace {N} with actual pin numbers), often appearing in shell scripts. Starting from kernel 4.8, libgpiod support was added; the original sysfs-based access method will be gradually abandoned.
Taking M4-R1 as an example, select 3 GPIOs on the 40PIN pins:

| Pin | Controller | Port Number | Pin Number | Calculation Result | PIN |
|---|---|---|---|---|---|
| GPIO0_D3 | 0 | D | 3 | 27 (32 x 0 + 8 x 3 + 3) | 7 |
| GPIO1_B0 | 1 | B | 0 | 40 (32 x 1 + 8 x 1 + 0) | 29 |
| GPIO4_B2 | 4 | B | 2 | 138 (32 x 4 + 8 x 1 + 2) | 36 |
# Note: If the driver occupies this pin, it cannot be exported in user space to control this GPIO
# Enable GPIO4_D2
echo 138 > /sys/class/gpio/export
# Set pin to input mode
echo in > /sys/class/gpio/gpio138/direction
# Read pin value
cat /sys/class/gpio/gpio138/value
# Set pin to output mode
echo out > /sys/class/gpio/gpio138/direction
# Set pin to low level
echo 0 > /sys/class/gpio/gpio138/value
# Set pin to high level
echo 1 > /sys/class/gpio/gpio138/value
# Reset pin
echo 138 > /sys/class/gpio/unexport