Skip to content
systemcoding.dev
Embedded Linux

Why the sysfs GPIO Interface Is Deprecated

The /sys/class/gpio interface is deprecated in Linux 6.12 because of unstable numbering, leaked pins, races and missing features. Here is what replaced it.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, NXP and Debian logos.

If you have followed a tutorial from a few years ago, you have probably written echo 17 > /sys/class/gpio/export to switch an LED on a Linux board. It works on a bench, and it is why the sysfs GPIO interface became so well known. It is also deprecated: the Linux kernel documentation marks it obsolete, new kernels may be built without it, and the replacement, the GPIO character device with the libgpiod library on top, does everything it did and fixes the reasons it was retired. This post explains what the old interface is, the specific defects that led to its deprecation, what the new interface changes and how to move over. It uses Linux 6.12 as the reference kernel. The deprecation is documented in the kernel's GPIO admin guide.

What the sysfs interface is

A GPIO, general-purpose input/output, is a pin whose direction and level software controls. In Linux, the kernel groups pins into chips, each chip driver registers a set of lines, and user space reaches a line through some interface. The sysfs one is a pair of files: you write a number to /sys/class/gpio/export, a directory /sys/class/gpio/gpio17/ appears, and its files direction, value, edge and active_low control the pin.

Shell
echo 17 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio17/direction
echo 1 > /sys/class/gpio/gpio17/value
cat /sys/class/gpio/gpio17/value
echo 17 > /sys/class/gpio/unexport
Text
1

It is easy to use from a shell with nothing installed, which explains its popularity. Everything below is about why that ease is a trap.

Defect one: global, unstable numbers

The number you write to export is a global GPIO number in a single kernel-wide space. A chip claims a range, and the starting number, called the base, is assigned when the chip registers. Early kernels fixed the base per board. Modern kernels assign it dynamically, counting down from the top of the space, so the same pin can have a different number after you add a driver, load a module in a different order or move to a new kernel version. A script that exports 17 on one kernel exports a different pin on the next. Because the number is meaningless without the chip's base, correct code has to read /sys/class/gpio/gpiochipN/base first, and many scripts did not.

The character device identifies a line by chip and offset, such as gpiochip0 and line 17, and the line also has a name from the device tree, so a program can ask for GPIO17 by name. Those identifiers do not shift when other drivers load.

Defect two: pins that outlive the program

An exported pin belongs to the system, not to the process that exported it. If your program crashes between export and unexport, the pin stays exported with its last direction and level. An output stays driven. Nobody owns it, so nobody cleans it up, and the next run fails because export returns "Device or resource busy". Embedded products that lose power or crash have to handle this explicitly. A character-device request is tied to a file descriptor: when the process exits, for any reason, the kernel releases the lines and returns them to their default state, which is the behavior you want from a resource.

Defect three: races and no atomicity

Setting up a pin through sysfs takes several writes: export, wait for the directory to appear, set the direction, set the value. Between those steps the pin sits in an intermediate state. On export the pin is an input, so an output you meant to start high first floats or reads low; the old trick of writing high or low to direction to set the initial level helps, but a program still races udev, which changes the permissions of the new files after they appear. Reading or writing several lines together is impossible: each line is its own file, so a bus with eight data lines changes one at a time with visible glitches between them.

The character device can request several lines in one call and read or set them in one ioctl, with the values changing together, which is what parallel buses and bit-banged protocols need.

Defect four: missing features

The sysfs interface exposes a minimum. There is no way to set internal pull-up or pull-down resistors, to choose open-drain or open-source outputs, to set a debounce period, or to ask which process holds a line. Edge detection exists through the edge file and poll(), but the event carries no timestamp, so the program that is woken up by a button press only knows it happened sometime before it got scheduled, and cannot order two events from two lines. The character device reports edges with a kernel timestamp taken at the interrupt, supports bias and drive options and debounce, and lets you query a line's current use.

Defect five: one interface for the whole world

Access control in sysfs is per file and set by udev rules, which means that giving a service access to its LED usually means a rule that makes every exported pin writable by a group. Exposing a line to a process is an all-or-nothing change of ownership of a directory tree, with no record of who used it. The character device is a normal device node, /dev/gpiochip0, so permissions are the usual user, group and mode, and systemd's DeviceAllow= and namespacing options can restrict a service to one chip.

What the kernel documentation says

The kernel's own admin guide marks the sysfs interface as deprecated, and the ABI documentation lists it under obsolete interfaces, scheduled for removal. The ABI v1 of the character device was added in Linux 4.8, and it was extended by a second version of the interface in Linux 5.10, which added line configuration changes without releasing the line, debounce and the ability to request lines with a set of attributes. The legacy sysfs support is a kernel build option, CONFIG_GPIO_SYSFS, that distributions and Yocto machine configurations can leave off, so a script that depended on it fails with "No such file or directory" on a newer build, which is how many people discover the deprecation.

The replacement: the character device and libgpiod

Each GPIO chip appears as /dev/gpiochipN. Programs open it and request lines with ioctls. Writing those ioctls by hand is possible, but the library libgpiod, with its command-line tools and bindings, is the supported way, and libgpiod version 2 matches the second character device interface. The tools replace the shell snippet above:

Shell
gpioset --chip gpiochip0 17=1
Text
(the command stays running and holds line 17 high until you press Ctrl-C)

Notice what the ownership model means in practice: the line stays high only while the process is alive, because the request lives in that process's file descriptor. For a command-line one-shot you pick the option that keeps it, or for a real product you hold the line from the daemon that uses it. When the program ends, the line returns to its default state.

How to migrate

Take stock of what you use. For each exported pin note whether it is an output or input, its initial level, whether you use edges and the line's identity. Replace the number with the chip and offset, or better with a line name from the device tree, so the code reads the same on different boards. Replace the shell steps with gpioset, gpioget and gpiomon, or with a libgpiod binding in your program. Remove udev rules that fix sysfs permissions and give the group that needs it access to the device node.

There is one case where sysfs remains: a very old kernel, before 4.8, which has no character device. For those, keep sysfs in a small wrapper with clear comments and plan the kernel upgrade.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

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

Unsubscribe in one click. No spam, ever.