Skip to content
systemcoding.dev
Kernels

GPIO Sysfs vs the libgpiod Character Device Interface

Compare the deprecated /sys/class/gpio interface with the libgpiod character device on Linux 6.12: ownership, atomic access, events and numbering.

6 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the Linux, GNU and Rust logos.

Every tutorial for toggling a pin on a Raspberry Pi or a BeagleBone used to start with echo 17 > /sys/class/gpio/export. That interface still exists in Linux 6.12, but it has been deprecated since Linux 4.8, and the replacement, a character device per GPIO chip driven by the libgpiod tools and library, fixes problems the old one could not. This post compares them on the points that matter in practice (numbering, ownership, atomic access, events and security), shows the same task in both, and ends in a decision. The version of the tools used is libgpiod 2.1, which changed some command syntax from the 1.x releases in older tutorials.

The kernel's own statement of the deprecation and the new interface is in the GPIO userspace documentation, and the library is documented at libgpiod.

Two interfaces, two models

The sysfs interface works on global GPIO numbers. A pin is a number in one flat space shared by every GPIO controller in the system, and you export it to get a directory:

Shell
echo 529 | sudo tee /sys/class/gpio/export
echo out | sudo tee /sys/class/gpio/gpio529/direction
echo 1 | sudo tee /sys/class/gpio/gpio529/value
cat /sys/class/gpio/gpio529/value
Text
529
out
1
1

The number 529 is not a pin label. It is a base number assigned to the controller plus the pin's offset, and the base is allocated dynamically at boot, so the same physical pin can have a different number after a kernel update or after another GPIO driver loads first. A script that hardcodes the number works until it does not.

The character device interface works on a chip and an offset. Each GPIO controller appears as /dev/gpiochipN, and a line is identified by the chip and its offset inside that chip, or by a name the firmware gave it. The tools from libgpiod do the work:

Shell
gpiodetect
gpioinfo -c gpiochip0 | head -n 4
gpioget -c gpiochip0 17
Text
gpiochip0 [pinctrl-bcm2711] (58 lines)
gpiochip0 - 58 lines:
	line   0:	"ID_SDA"        	input
	line   1:	"ID_SCL"        	input
"17"=inactive

The chip is named by the controller and the offsets are stable, because they are the hardware's own. A line name such as "ID_SDA" comes from the device tree and survives a change in numbering.

Ownership and cleanup

This is the sharpest difference. A sysfs export belongs to the system, not to your process. If a script exports a pin, sets it high and crashes, the pin stays exported and high. The next program that tries to export it fails with Device or resource busy, and nothing tells you who owns it. A pin driving a relay can stay on indefinitely after a crash.

A line requested through the character device belongs to the file descriptor that requested it. When the process exits for any reason, including a crash or kill -9, the kernel closes the descriptor and releases the line to its default state. The release is automatic and cannot be forgotten. The kernel also records the consumer name:

Shell
gpioset -c gpiochip0 17=1 &
gpioinfo -c gpiochip0 | grep '^	line  17'
Text
	line  17:	unnamed         	output consumer="gpioset"

The gpioset command in libgpiod 2.x stays running to hold the line, which is why it is backgrounded here. When it is killed, the line returns to its previous state. That is the behavior sysfs could never offer, and it is why quick shell tests look slightly different with the new tools: either keep the process alive, or use the -t option to hold the value for a time before releasing it.

Atomic access to several lines

Sysfs sets one pin per write, so a parallel bus built from eight pins changes one bit at a time, and a reader can catch it half-updated. The character device sets or reads a whole set of lines in one system call, so the change is as simultaneous as the hardware allows:

Shell
gpioset -c gpiochip0 17=1 18=0 27=1 22=0

For a bit-banged interface such as a shift register or a parallel LCD, that difference decides whether the signal is glitch-free.

Events instead of polling

To react when a pin changes, a sysfs program sets an edge file and polls the value file with poll(), reading it after each wake-up and learning nothing about when the edge happened. The character device delivers edge events as records with a timestamp from the kernel at the moment of the interrupt, so a program can measure the width of a pulse or the time between two edges without being thrown by scheduling latency:

Shell
gpiomon -c gpiochip0 --edges both 17
Text
1234.567890123	rising	"17"
1234.571120456	falling	"17"

The timestamps in the output are in seconds from a monotonic clock, with nanosecond precision, and they are taken in the interrupt handler. The difference of 3.23 milliseconds between the two lines is the pulse width. A sysfs poll loop cannot give you that figure honestly.

Configuration that sysfs lacks

The character device carries line settings that sysfs has no way to express: pull-up and pull-down bias, open-drain and open-source drive, active-low inversion and debounce. They are flags in the request, and libgpiod's tools expose them:

Shell
gpioget -c gpiochip0 --bias=pull-up 17

With sysfs a pull-up needs the pin controller's own interface or a device tree overlay. Debounce for a mechanical switch is another case: the character interface can ask the hardware or the kernel to filter bounces, and sysfs cannot.

The comparison

Pointsysfscharacter device
Pin identityGlobal number, can changeChip plus offset, or a line name
Who owns an exported lineThe system, until someone unexportsThe process, released on exit
Several lines at onceOne write per lineOne call for a whole set
Edge eventspoll() on a file, no timestampEvent records with kernel timestamps
Bias, drive, debounceNot availableRequest flags
PermissionsOne shared directoryPer chip file, set with udev
StatusDeprecated since 4.8, may be disabled in the kernel configThe supported interface

The decision

Use the character device for anything new. It is the interface the kernel developers maintain, and every feature added since 2016 is only there. libgpiod has bindings for Python, C++, Rust and others, so scripts and programs have a native path too.

Use sysfs only when the system you control leaves you no choice: an old kernel without /dev/gpiochipN support, a vendor image where libgpiod is not installable, or an old script that you will not rewrite. Even then, know that the interface can vanish. Some distributions already build kernels without CONFIG_GPIO_SYSFS, and a script that relied on it stops working. Check with ls /sys/class/gpio before depending on it.

The permissions are easier with the character device too. A udev rule can give a group read and write access to /dev/gpiochip0, and every program in that group works without root. Sysfs needs root or a more elaborate rule, and its permissions change when a pin is exported.

What does not change

Neither interface can claim a pin that a kernel driver owns. A pin used by an I2C controller or an LED trigger shows as used in gpioinfo and cannot be requested from user space. The pin multiplexing that makes a pin a GPIO and not a peripheral function is configured by the pin controller, usually from the device tree. A line that exists in gpioinfo but does nothing may be muxed to another function.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.