162306a36Sopenharmony_ciGPIO Sysfs Interface for Userspace
262306a36Sopenharmony_ci==================================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. warning::
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci  THIS ABI IS DEPRECATED, THE ABI DOCUMENTATION HAS BEEN MOVED TO
762306a36Sopenharmony_ci  Documentation/ABI/obsolete/sysfs-gpio AND NEW USERSPACE CONSUMERS
862306a36Sopenharmony_ci  ARE SUPPOSED TO USE THE CHARACTER DEVICE ABI. THIS OLD SYSFS ABI WILL
962306a36Sopenharmony_ci  NOT BE DEVELOPED (NO NEW FEATURES), IT WILL JUST BE MAINTAINED.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciRefer to the examples in tools/gpio/* for an introduction to the new
1262306a36Sopenharmony_cicharacter device ABI. Also see the userspace header in
1362306a36Sopenharmony_ciinclude/uapi/linux/gpio.h
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciThe deprecated sysfs ABI
1662306a36Sopenharmony_ci------------------------
1762306a36Sopenharmony_ciPlatforms which use the "gpiolib" implementors framework may choose to
1862306a36Sopenharmony_ciconfigure a sysfs user interface to GPIOs. This is different from the
1962306a36Sopenharmony_cidebugfs interface, since it provides control over GPIO direction and
2062306a36Sopenharmony_civalue instead of just showing a gpio state summary. Plus, it could be
2162306a36Sopenharmony_cipresent on production systems without debugging support.
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciGiven appropriate hardware documentation for the system, userspace could
2462306a36Sopenharmony_ciknow for example that GPIO #23 controls the write protect line used to
2562306a36Sopenharmony_ciprotect boot loader segments in flash memory. System upgrade procedures
2662306a36Sopenharmony_cimay need to temporarily remove that protection, first importing a GPIO,
2762306a36Sopenharmony_cithen changing its output state, then updating the code before re-enabling
2862306a36Sopenharmony_cithe write protection. In normal use, GPIO #23 would never be touched,
2962306a36Sopenharmony_ciand the kernel would have no need to know about it.
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciAgain depending on appropriate hardware documentation, on some systems
3262306a36Sopenharmony_ciuserspace GPIO can be used to determine system configuration data that
3362306a36Sopenharmony_cistandard kernels won't know about. And for some tasks, simple userspace
3462306a36Sopenharmony_ciGPIO drivers could be all that the system really needs.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciDO NOT ABUSE SYSFS TO CONTROL HARDWARE THAT HAS PROPER KERNEL DRIVERS.
3762306a36Sopenharmony_ciPLEASE READ THE DOCUMENT AT Documentation/driver-api/gpio/drivers-on-gpio.rst
3862306a36Sopenharmony_ciTO AVOID REINVENTING KERNEL WHEELS IN USERSPACE. I MEAN IT. REALLY.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciPaths in Sysfs
4162306a36Sopenharmony_ci--------------
4262306a36Sopenharmony_ciThere are three kinds of entries in /sys/class/gpio:
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci   -	Control interfaces used to get userspace control over GPIOs;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci   -	GPIOs themselves; and
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci   -	GPIO controllers ("gpio_chip" instances).
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciThat's in addition to standard files including the "device" symlink.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciThe control interfaces are write-only:
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci    /sys/class/gpio/
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	"export" ...
5762306a36Sopenharmony_ci		Userspace may ask the kernel to export control of
5862306a36Sopenharmony_ci		a GPIO to userspace by writing its number to this file.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci		Example:  "echo 19 > export" will create a "gpio19" node
6162306a36Sopenharmony_ci		for GPIO #19, if that's not requested by kernel code.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	"unexport" ...
6462306a36Sopenharmony_ci		Reverses the effect of exporting to userspace.
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci		Example:  "echo 19 > unexport" will remove a "gpio19"
6762306a36Sopenharmony_ci		node exported using the "export" file.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciGPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
7062306a36Sopenharmony_ciand have the following read/write attributes:
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci    /sys/class/gpio/gpioN/
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	"direction" ...
7562306a36Sopenharmony_ci		reads as either "in" or "out". This value may
7662306a36Sopenharmony_ci		normally be written. Writing as "out" defaults to
7762306a36Sopenharmony_ci		initializing the value as low. To ensure glitch free
7862306a36Sopenharmony_ci		operation, values "low" and "high" may be written to
7962306a36Sopenharmony_ci		configure the GPIO as an output with that initial value.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci		Note that this attribute *will not exist* if the kernel
8262306a36Sopenharmony_ci		doesn't support changing the direction of a GPIO, or
8362306a36Sopenharmony_ci		it was exported by kernel code that didn't explicitly
8462306a36Sopenharmony_ci		allow userspace to reconfigure this GPIO's direction.
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	"value" ...
8762306a36Sopenharmony_ci		reads as either 0 (low) or 1 (high). If the GPIO
8862306a36Sopenharmony_ci		is configured as an output, this value may be written;
8962306a36Sopenharmony_ci		any nonzero value is treated as high.
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci		If the pin can be configured as interrupt-generating interrupt
9262306a36Sopenharmony_ci		and if it has been configured to generate interrupts (see the
9362306a36Sopenharmony_ci		description of "edge"), you can poll(2) on that file and
9462306a36Sopenharmony_ci		poll(2) will return whenever the interrupt was triggered. If
9562306a36Sopenharmony_ci		you use poll(2), set the events POLLPRI and POLLERR. If you
9662306a36Sopenharmony_ci		use select(2), set the file descriptor in exceptfds. After
9762306a36Sopenharmony_ci		poll(2) returns, either lseek(2) to the beginning of the sysfs
9862306a36Sopenharmony_ci		file and read the new value or close the file and re-open it
9962306a36Sopenharmony_ci		to read the value.
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	"edge" ...
10262306a36Sopenharmony_ci		reads as either "none", "rising", "falling", or
10362306a36Sopenharmony_ci		"both". Write these strings to select the signal edge(s)
10462306a36Sopenharmony_ci		that will make poll(2) on the "value" file return.
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci		This file exists only if the pin can be configured as an
10762306a36Sopenharmony_ci		interrupt generating input pin.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	"active_low" ...
11062306a36Sopenharmony_ci		reads as either 0 (false) or 1 (true). Write
11162306a36Sopenharmony_ci		any nonzero value to invert the value attribute both
11262306a36Sopenharmony_ci		for reading and writing. Existing and subsequent
11362306a36Sopenharmony_ci		poll(2) support configuration via the edge attribute
11462306a36Sopenharmony_ci		for "rising" and "falling" edges will follow this
11562306a36Sopenharmony_ci		setting.
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciGPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
11862306a36Sopenharmony_cicontroller implementing GPIOs starting at #42) and have the following
11962306a36Sopenharmony_ciread-only attributes:
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci    /sys/class/gpio/gpiochipN/
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	"base" ...
12462306a36Sopenharmony_ci		same as N, the first GPIO managed by this chip
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	"label" ...
12762306a36Sopenharmony_ci		provided for diagnostics (not always unique)
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	"ngpio" ...
13062306a36Sopenharmony_ci		how many GPIOs this manages (N to N + ngpio - 1)
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciBoard documentation should in most cases cover what GPIOs are used for
13362306a36Sopenharmony_ciwhat purposes. However, those numbers are not always stable; GPIOs on
13462306a36Sopenharmony_cia daughtercard might be different depending on the base board being used,
13562306a36Sopenharmony_cior other cards in the stack. In such cases, you may need to use the
13662306a36Sopenharmony_cigpiochip nodes (possibly in conjunction with schematics) to determine
13762306a36Sopenharmony_cithe correct GPIO number to use for a given signal.
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ciExporting from Kernel code
14162306a36Sopenharmony_ci--------------------------
14262306a36Sopenharmony_ciKernel code can explicitly manage exports of GPIOs which have already been
14362306a36Sopenharmony_cirequested using gpio_request()::
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	/* export the GPIO to userspace */
14662306a36Sopenharmony_ci	int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	/* reverse gpiod_export() */
14962306a36Sopenharmony_ci	void gpiod_unexport(struct gpio_desc *desc);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	/* create a sysfs link to an exported GPIO node */
15262306a36Sopenharmony_ci	int gpiod_export_link(struct device *dev, const char *name,
15362306a36Sopenharmony_ci		      struct gpio_desc *desc);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciAfter a kernel driver requests a GPIO, it may only be made available in
15662306a36Sopenharmony_cithe sysfs interface by gpiod_export(). The driver can control whether the
15762306a36Sopenharmony_cisignal direction may change. This helps drivers prevent userspace code
15862306a36Sopenharmony_cifrom accidentally clobbering important system state.
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciThis explicit exporting can help with debugging (by making some kinds
16162306a36Sopenharmony_ciof experiments easier), or can provide an always-there interface that's
16262306a36Sopenharmony_cisuitable for documenting as part of a board support package.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciAfter the GPIO has been exported, gpiod_export_link() allows creating
16562306a36Sopenharmony_cisymlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
16662306a36Sopenharmony_ciuse this to provide the interface under their own device in sysfs with
16762306a36Sopenharmony_cia descriptive name.
168