xref: /linux/Documentation/userspace-api/gpio/sysfs.rst (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
15054626aSKent GibsonGPIO Sysfs Interface for Userspace
25054626aSKent Gibson==================================
35054626aSKent Gibson
45054626aSKent Gibson.. warning::
5e10b6417SKent Gibson   This API is obsoleted by the chardev.rst and the ABI documentation has
6e10b6417SKent Gibson   been moved to Documentation/ABI/obsolete/sysfs-gpio.
75054626aSKent Gibson
8e10b6417SKent Gibson   New developments should use the chardev.rst, and existing developments are
9e10b6417SKent Gibson   encouraged to migrate as soon as possible, as this API will be removed
10e10b6417SKent Gibson   in the future.
115054626aSKent Gibson
12e10b6417SKent Gibson   This interface will continue to be maintained for the migration period,
13e10b6417SKent Gibson   but new features will only be added to the new API.
145054626aSKent Gibson
15e10b6417SKent GibsonThe obsolete sysfs ABI
16e10b6417SKent Gibson----------------------
175054626aSKent GibsonPlatforms which use the "gpiolib" implementors framework may choose to
185054626aSKent Gibsonconfigure a sysfs user interface to GPIOs. This is different from the
195054626aSKent Gibsondebugfs interface, since it provides control over GPIO direction and
205054626aSKent Gibsonvalue instead of just showing a gpio state summary. Plus, it could be
215054626aSKent Gibsonpresent on production systems without debugging support.
225054626aSKent Gibson
235054626aSKent GibsonGiven appropriate hardware documentation for the system, userspace could
245054626aSKent Gibsonknow for example that GPIO #23 controls the write protect line used to
255054626aSKent Gibsonprotect boot loader segments in flash memory. System upgrade procedures
265054626aSKent Gibsonmay need to temporarily remove that protection, first importing a GPIO,
275054626aSKent Gibsonthen changing its output state, then updating the code before re-enabling
285054626aSKent Gibsonthe write protection. In normal use, GPIO #23 would never be touched,
295054626aSKent Gibsonand the kernel would have no need to know about it.
305054626aSKent Gibson
315054626aSKent GibsonAgain depending on appropriate hardware documentation, on some systems
325054626aSKent Gibsonuserspace GPIO can be used to determine system configuration data that
335054626aSKent Gibsonstandard kernels won't know about. And for some tasks, simple userspace
345054626aSKent GibsonGPIO drivers could be all that the system really needs.
355054626aSKent Gibson
36e10b6417SKent Gibson.. note::
37e10b6417SKent Gibson   Do NOT abuse sysfs to control hardware that has proper kernel drivers.
38e10b6417SKent Gibson   Please read Documentation/driver-api/gpio/drivers-on-gpio.rst
39e10b6417SKent Gibson   to avoid reinventing kernel wheels in userspace.
40e10b6417SKent Gibson
41e10b6417SKent Gibson   I MEAN IT. REALLY.
425054626aSKent Gibson
435054626aSKent GibsonPaths in Sysfs
445054626aSKent Gibson--------------
455054626aSKent GibsonThere are three kinds of entries in /sys/class/gpio:
465054626aSKent Gibson
475054626aSKent Gibson   -	Control interfaces used to get userspace control over GPIOs;
485054626aSKent Gibson
495054626aSKent Gibson   -	GPIOs themselves; and
505054626aSKent Gibson
515054626aSKent Gibson   -	GPIO controllers ("gpio_chip" instances).
525054626aSKent Gibson
535054626aSKent GibsonThat's in addition to standard files including the "device" symlink.
545054626aSKent Gibson
555054626aSKent GibsonThe control interfaces are write-only:
565054626aSKent Gibson
575054626aSKent Gibson    /sys/class/gpio/
585054626aSKent Gibson
595054626aSKent Gibson	"export" ...
605054626aSKent Gibson		Userspace may ask the kernel to export control of
615054626aSKent Gibson		a GPIO to userspace by writing its number to this file.
625054626aSKent Gibson
635054626aSKent Gibson		Example:  "echo 19 > export" will create a "gpio19" node
645054626aSKent Gibson		for GPIO #19, if that's not requested by kernel code.
655054626aSKent Gibson
665054626aSKent Gibson	"unexport" ...
675054626aSKent Gibson		Reverses the effect of exporting to userspace.
685054626aSKent Gibson
695054626aSKent Gibson		Example:  "echo 19 > unexport" will remove a "gpio19"
705054626aSKent Gibson		node exported using the "export" file.
715054626aSKent Gibson
725054626aSKent GibsonGPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
735054626aSKent Gibsonand have the following read/write attributes:
745054626aSKent Gibson
755054626aSKent Gibson    /sys/class/gpio/gpioN/
765054626aSKent Gibson
775054626aSKent Gibson	"direction" ...
785054626aSKent Gibson		reads as either "in" or "out". This value may
795054626aSKent Gibson		normally be written. Writing as "out" defaults to
805054626aSKent Gibson		initializing the value as low. To ensure glitch free
815054626aSKent Gibson		operation, values "low" and "high" may be written to
825054626aSKent Gibson		configure the GPIO as an output with that initial value.
835054626aSKent Gibson
845054626aSKent Gibson		Note that this attribute *will not exist* if the kernel
855054626aSKent Gibson		doesn't support changing the direction of a GPIO, or
865054626aSKent Gibson		it was exported by kernel code that didn't explicitly
875054626aSKent Gibson		allow userspace to reconfigure this GPIO's direction.
885054626aSKent Gibson
895054626aSKent Gibson	"value" ...
909e69d6d8SKent Gibson		reads as either 0 (inactive) or 1 (active). If the GPIO
915054626aSKent Gibson		is configured as an output, this value may be written;
929e69d6d8SKent Gibson		any nonzero value is treated as active.
935054626aSKent Gibson
945054626aSKent Gibson		If the pin can be configured as interrupt-generating interrupt
955054626aSKent Gibson		and if it has been configured to generate interrupts (see the
965054626aSKent Gibson		description of "edge"), you can poll(2) on that file and
975054626aSKent Gibson		poll(2) will return whenever the interrupt was triggered. If
985054626aSKent Gibson		you use poll(2), set the events POLLPRI and POLLERR. If you
995054626aSKent Gibson		use select(2), set the file descriptor in exceptfds. After
100*7f1e45f4SHuichun Feng		poll(2) returns, use pread(2) to read the value at offset
101*7f1e45f4SHuichun Feng		zero. Alternatively, either lseek(2) to the beginning of the
102*7f1e45f4SHuichun Feng		sysfs file and read the new value or close the file and
103*7f1e45f4SHuichun Feng		re-open it to read the value.
1045054626aSKent Gibson
1055054626aSKent Gibson	"edge" ...
1065054626aSKent Gibson		reads as either "none", "rising", "falling", or
1075054626aSKent Gibson		"both". Write these strings to select the signal edge(s)
1085054626aSKent Gibson		that will make poll(2) on the "value" file return.
1095054626aSKent Gibson
1105054626aSKent Gibson		This file exists only if the pin can be configured as an
1115054626aSKent Gibson		interrupt generating input pin.
1125054626aSKent Gibson
1135054626aSKent Gibson	"active_low" ...
1145054626aSKent Gibson		reads as either 0 (false) or 1 (true). Write
1155054626aSKent Gibson		any nonzero value to invert the value attribute both
1165054626aSKent Gibson		for reading and writing. Existing and subsequent
1175054626aSKent Gibson		poll(2) support configuration via the edge attribute
1185054626aSKent Gibson		for "rising" and "falling" edges will follow this
1195054626aSKent Gibson		setting.
1205054626aSKent Gibson
1215054626aSKent GibsonGPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
1225054626aSKent Gibsoncontroller implementing GPIOs starting at #42) and have the following
1235054626aSKent Gibsonread-only attributes:
1245054626aSKent Gibson
1255054626aSKent Gibson    /sys/class/gpio/gpiochipN/
1265054626aSKent Gibson
1275054626aSKent Gibson	"base" ...
1285054626aSKent Gibson		same as N, the first GPIO managed by this chip
1295054626aSKent Gibson
1305054626aSKent Gibson	"label" ...
1315054626aSKent Gibson		provided for diagnostics (not always unique)
1325054626aSKent Gibson
1335054626aSKent Gibson	"ngpio" ...
1345054626aSKent Gibson		how many GPIOs this manages (N to N + ngpio - 1)
1355054626aSKent Gibson
1365054626aSKent GibsonBoard documentation should in most cases cover what GPIOs are used for
1375054626aSKent Gibsonwhat purposes. However, those numbers are not always stable; GPIOs on
1385054626aSKent Gibsona daughtercard might be different depending on the base board being used,
1395054626aSKent Gibsonor other cards in the stack. In such cases, you may need to use the
1405054626aSKent Gibsongpiochip nodes (possibly in conjunction with schematics) to determine
1415054626aSKent Gibsonthe correct GPIO number to use for a given signal.
1425054626aSKent Gibson
1435054626aSKent Gibson
1445054626aSKent GibsonExporting from Kernel code
1455054626aSKent Gibson--------------------------
1465054626aSKent GibsonKernel code can explicitly manage exports of GPIOs which have already been
1475054626aSKent Gibsonrequested using gpio_request()::
1485054626aSKent Gibson
1495054626aSKent Gibson	/* export the GPIO to userspace */
1505054626aSKent Gibson	int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
1515054626aSKent Gibson
1525054626aSKent Gibson	/* reverse gpiod_export() */
1535054626aSKent Gibson	void gpiod_unexport(struct gpio_desc *desc);
1545054626aSKent Gibson
1555054626aSKent Gibson	/* create a sysfs link to an exported GPIO node */
1565054626aSKent Gibson	int gpiod_export_link(struct device *dev, const char *name,
1575054626aSKent Gibson		      struct gpio_desc *desc);
1585054626aSKent Gibson
1595054626aSKent GibsonAfter a kernel driver requests a GPIO, it may only be made available in
1605054626aSKent Gibsonthe sysfs interface by gpiod_export(). The driver can control whether the
1615054626aSKent Gibsonsignal direction may change. This helps drivers prevent userspace code
1625054626aSKent Gibsonfrom accidentally clobbering important system state.
1635054626aSKent Gibson
1645054626aSKent GibsonThis explicit exporting can help with debugging (by making some kinds
1655054626aSKent Gibsonof experiments easier), or can provide an always-there interface that's
1665054626aSKent Gibsonsuitable for documenting as part of a board support package.
1675054626aSKent Gibson
1685054626aSKent GibsonAfter the GPIO has been exported, gpiod_export_link() allows creating
1695054626aSKent Gibsonsymlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
1705054626aSKent Gibsonuse this to provide the interface under their own device in sysfs with
1715054626aSKent Gibsona descriptive name.
172