xnux.eu - site map - news

Linux sysfs GPIO interface

The SoC has multiple banks of GPIO pins. If the bank is powered, which it usually is, you'll be able to control the pins that are not claimed by Linux drivers via files in /sys/class/gpio.

Enumerating available pins

Every GPIO pin has a unique number in Linux that doesn't correspond well to the pin/pad names on the SoC chip. Or even outside, for example PMIC also has some GPIOs, that you can control this way.

You can see the human readable names if you query the debugfs/pinctrl.

cat /sys/kernel/debug/pinctrl/*/pinmux-pins

Sample output (PinePhone):

Pinmux settings per pin
Format: pin (name): mux_owner|gpio_owner (strict) hog?
pin 32 (PB0): UNCLAIMED
pin 33 (PB1): UNCLAIMED
pin 34 (PB2): UNCLAIMED
pin 35 (PB3): UNCLAIMED
pin 36 (PB4): device 1c22e00.codec function aif2 group PB4
pin 37 (PB5): device 1c22e00.codec function aif2 group PB5
pin 38 (PB6): device 1c22e00.codec function aif2 group PB6
pin 39 (PB7): device 1c22e00.codec function aif2 group PB7
pin 40 (PB8): device 1c28000.serial function uart0 group PB8
pin 41 (PB9): device 1c28000.serial function uart0 group PB9
pin 64 (PC0): GPIO 1c20800.pinctrl:64
pin 65 (PC1): UNCLAIMED
pin 66 (PC2): UNCLAIMED
pin 67 (PC3): UNCLAIMED
pin 68 (PC4): UNCLAIMED
pin 69 (PC5): device 1c11000.mmc function mmc2 group PC5
pin 70 (PC6): device 1c11000.mmc function mmc2 group PC6
pin 71 (PC7): GPIO 1c20800.pinctrl:71
pin 72 (PC8): device 1c11000.mmc function mmc2 group PC8
pin 73 (PC9): device 1c11000.mmc function mmc2 group PC9
pin 74 (PC10): device 1c11000.mmc function mmc2 group PC10
pin 75 (PC11): device 1c11000.mmc function mmc2 group PC11
pin 76 (PC12): device 1c11000.mmc function mmc2 group PC12
pin 77 (PC13): device 1c11000.mmc function mmc2 group PC13
pin 78 (PC14): device 1c11000.mmc function mmc2 group PC14
pin 79 (PC15): device 1c11000.mmc function mmc2 group PC15
pin 80 (PC16): device 1c11000.mmc function mmc2 group PC16
pin 96 (PD0): device 1c28c00.serial function uart3 group PD0
pin 97 (PD1): device 1c28c00.serial function uart3 group PD1
pin 98 (PD2): GPIO 1c20800.pinctrl:98
pin 99 (PD3): GPIO 1c20800.pinctrl:99
pin 100 (PD4): UNCLAIMED
pin 101 (PD5): UNCLAIMED
pin 102 (PD6): GPIO 1c20800.pinctrl:102
pin 103 (PD7): UNCLAIMED
......

The output shows GPIO number first, then the human readable name, then whether the GPIO pin is currently claimed by a driver and what function it is configured to.

You'll be able to use only the unclaimed GPIO pins from sysfs.

Claiming the GPIO pin

You need to claim the pin for use via sysfs first, so that no other driver can take the pin over from you:

echo $pin_number > /sys/class/gpio/export

After this a new subdirectory will appear under /sys/class/gpio called /sys/class/gpio/gpio$pin_number. This directory contains a bunch of files that can be used to control the pin.

Output

GPIOs are bi-directional. You can set them to output high, low or to input and read the logic level.

To drive the GPIO pin high:

echo high > /sys/class/gpio/gpio$pin_number/direction

To drive the GPIO pin low:

echo low > /sys/class/gpio/gpio$pin_number/direction

Input

To make the pin an input and read the logical voltage level:

echo in > /sys/class/gpio/gpio$pin_number/direction
cat /sys/class/gpio/gpio$pin_number/value