xref: /openbmc/linux/Documentation/admin-guide/gpio/sysfs.rst (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1c2746a1eSMauro Carvalho ChehabGPIO Sysfs Interface for Userspace
2c2746a1eSMauro Carvalho Chehab==================================
3c2746a1eSMauro Carvalho Chehab
4c2746a1eSMauro Carvalho Chehab.. warning::
5c2746a1eSMauro Carvalho Chehab
6c2746a1eSMauro Carvalho Chehab  THIS ABI IS DEPRECATED, THE ABI DOCUMENTATION HAS BEEN MOVED TO
7c2746a1eSMauro Carvalho Chehab  Documentation/ABI/obsolete/sysfs-gpio AND NEW USERSPACE CONSUMERS
8c2746a1eSMauro Carvalho Chehab  ARE SUPPOSED TO USE THE CHARACTER DEVICE ABI. THIS OLD SYSFS ABI WILL
9c2746a1eSMauro Carvalho Chehab  NOT BE DEVELOPED (NO NEW FEATURES), IT WILL JUST BE MAINTAINED.
10c2746a1eSMauro Carvalho Chehab
11c2746a1eSMauro Carvalho ChehabRefer to the examples in tools/gpio/* for an introduction to the new
12c2746a1eSMauro Carvalho Chehabcharacter device ABI. Also see the userspace header in
13c2746a1eSMauro Carvalho Chehabinclude/uapi/linux/gpio.h
14c2746a1eSMauro Carvalho Chehab
15c2746a1eSMauro Carvalho ChehabThe deprecated sysfs ABI
16c2746a1eSMauro Carvalho Chehab------------------------
17c2746a1eSMauro Carvalho ChehabPlatforms which use the "gpiolib" implementors framework may choose to
18c2746a1eSMauro Carvalho Chehabconfigure a sysfs user interface to GPIOs. This is different from the
19c2746a1eSMauro Carvalho Chehabdebugfs interface, since it provides control over GPIO direction and
20c2746a1eSMauro Carvalho Chehabvalue instead of just showing a gpio state summary. Plus, it could be
21c2746a1eSMauro Carvalho Chehabpresent on production systems without debugging support.
22c2746a1eSMauro Carvalho Chehab
23c2746a1eSMauro Carvalho ChehabGiven appropriate hardware documentation for the system, userspace could
24c2746a1eSMauro Carvalho Chehabknow for example that GPIO #23 controls the write protect line used to
25c2746a1eSMauro Carvalho Chehabprotect boot loader segments in flash memory. System upgrade procedures
26c2746a1eSMauro Carvalho Chehabmay need to temporarily remove that protection, first importing a GPIO,
27c2746a1eSMauro Carvalho Chehabthen changing its output state, then updating the code before re-enabling
28c2746a1eSMauro Carvalho Chehabthe write protection. In normal use, GPIO #23 would never be touched,
29c2746a1eSMauro Carvalho Chehaband the kernel would have no need to know about it.
30c2746a1eSMauro Carvalho Chehab
31c2746a1eSMauro Carvalho ChehabAgain depending on appropriate hardware documentation, on some systems
32c2746a1eSMauro Carvalho Chehabuserspace GPIO can be used to determine system configuration data that
33c2746a1eSMauro Carvalho Chehabstandard kernels won't know about. And for some tasks, simple userspace
34c2746a1eSMauro Carvalho ChehabGPIO drivers could be all that the system really needs.
35c2746a1eSMauro Carvalho Chehab
36c2746a1eSMauro Carvalho ChehabDO NOT ABUSE SYSFS TO CONTROL HARDWARE THAT HAS PROPER KERNEL DRIVERS.
37c2746a1eSMauro Carvalho ChehabPLEASE READ THE DOCUMENT AT Documentation/driver-api/gpio/drivers-on-gpio.rst
38c2746a1eSMauro Carvalho ChehabTO AVOID REINVENTING KERNEL WHEELS IN USERSPACE. I MEAN IT. REALLY.
39c2746a1eSMauro Carvalho Chehab
40c2746a1eSMauro Carvalho ChehabPaths in Sysfs
41c2746a1eSMauro Carvalho Chehab--------------
42c2746a1eSMauro Carvalho ChehabThere are three kinds of entries in /sys/class/gpio:
43c2746a1eSMauro Carvalho Chehab
44c2746a1eSMauro Carvalho Chehab   -	Control interfaces used to get userspace control over GPIOs;
45c2746a1eSMauro Carvalho Chehab
46c2746a1eSMauro Carvalho Chehab   -	GPIOs themselves; and
47c2746a1eSMauro Carvalho Chehab
48c2746a1eSMauro Carvalho Chehab   -	GPIO controllers ("gpio_chip" instances).
49c2746a1eSMauro Carvalho Chehab
50c2746a1eSMauro Carvalho ChehabThat's in addition to standard files including the "device" symlink.
51c2746a1eSMauro Carvalho Chehab
52c2746a1eSMauro Carvalho ChehabThe control interfaces are write-only:
53c2746a1eSMauro Carvalho Chehab
54c2746a1eSMauro Carvalho Chehab    /sys/class/gpio/
55c2746a1eSMauro Carvalho Chehab
56c2746a1eSMauro Carvalho Chehab	"export" ...
57c2746a1eSMauro Carvalho Chehab		Userspace may ask the kernel to export control of
58c2746a1eSMauro Carvalho Chehab		a GPIO to userspace by writing its number to this file.
59c2746a1eSMauro Carvalho Chehab
60c2746a1eSMauro Carvalho Chehab		Example:  "echo 19 > export" will create a "gpio19" node
61c2746a1eSMauro Carvalho Chehab		for GPIO #19, if that's not requested by kernel code.
62c2746a1eSMauro Carvalho Chehab
63c2746a1eSMauro Carvalho Chehab	"unexport" ...
64c2746a1eSMauro Carvalho Chehab		Reverses the effect of exporting to userspace.
65c2746a1eSMauro Carvalho Chehab
66c2746a1eSMauro Carvalho Chehab		Example:  "echo 19 > unexport" will remove a "gpio19"
67c2746a1eSMauro Carvalho Chehab		node exported using the "export" file.
68c2746a1eSMauro Carvalho Chehab
69c2746a1eSMauro Carvalho ChehabGPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
70c2746a1eSMauro Carvalho Chehaband have the following read/write attributes:
71c2746a1eSMauro Carvalho Chehab
72c2746a1eSMauro Carvalho Chehab    /sys/class/gpio/gpioN/
73c2746a1eSMauro Carvalho Chehab
74c2746a1eSMauro Carvalho Chehab	"direction" ...
75c2746a1eSMauro Carvalho Chehab		reads as either "in" or "out". This value may
76c2746a1eSMauro Carvalho Chehab		normally be written. Writing as "out" defaults to
77c2746a1eSMauro Carvalho Chehab		initializing the value as low. To ensure glitch free
78c2746a1eSMauro Carvalho Chehab		operation, values "low" and "high" may be written to
79c2746a1eSMauro Carvalho Chehab		configure the GPIO as an output with that initial value.
80c2746a1eSMauro Carvalho Chehab
81c2746a1eSMauro Carvalho Chehab		Note that this attribute *will not exist* if the kernel
82c2746a1eSMauro Carvalho Chehab		doesn't support changing the direction of a GPIO, or
83c2746a1eSMauro Carvalho Chehab		it was exported by kernel code that didn't explicitly
84c2746a1eSMauro Carvalho Chehab		allow userspace to reconfigure this GPIO's direction.
85c2746a1eSMauro Carvalho Chehab
86c2746a1eSMauro Carvalho Chehab	"value" ...
87c2746a1eSMauro Carvalho Chehab		reads as either 0 (low) or 1 (high). If the GPIO
88c2746a1eSMauro Carvalho Chehab		is configured as an output, this value may be written;
89c2746a1eSMauro Carvalho Chehab		any nonzero value is treated as high.
90c2746a1eSMauro Carvalho Chehab
91c2746a1eSMauro Carvalho Chehab		If the pin can be configured as interrupt-generating interrupt
92c2746a1eSMauro Carvalho Chehab		and if it has been configured to generate interrupts (see the
93c2746a1eSMauro Carvalho Chehab		description of "edge"), you can poll(2) on that file and
94c2746a1eSMauro Carvalho Chehab		poll(2) will return whenever the interrupt was triggered. If
95c2746a1eSMauro Carvalho Chehab		you use poll(2), set the events POLLPRI and POLLERR. If you
96c2746a1eSMauro Carvalho Chehab		use select(2), set the file descriptor in exceptfds. After
97c2746a1eSMauro Carvalho Chehab		poll(2) returns, either lseek(2) to the beginning of the sysfs
98c2746a1eSMauro Carvalho Chehab		file and read the new value or close the file and re-open it
99c2746a1eSMauro Carvalho Chehab		to read the value.
100c2746a1eSMauro Carvalho Chehab
101c2746a1eSMauro Carvalho Chehab	"edge" ...
102c2746a1eSMauro Carvalho Chehab		reads as either "none", "rising", "falling", or
103c2746a1eSMauro Carvalho Chehab		"both". Write these strings to select the signal edge(s)
104c2746a1eSMauro Carvalho Chehab		that will make poll(2) on the "value" file return.
105c2746a1eSMauro Carvalho Chehab
106c2746a1eSMauro Carvalho Chehab		This file exists only if the pin can be configured as an
107c2746a1eSMauro Carvalho Chehab		interrupt generating input pin.
108c2746a1eSMauro Carvalho Chehab
109c2746a1eSMauro Carvalho Chehab	"active_low" ...
110c2746a1eSMauro Carvalho Chehab		reads as either 0 (false) or 1 (true). Write
111c2746a1eSMauro Carvalho Chehab		any nonzero value to invert the value attribute both
112c2746a1eSMauro Carvalho Chehab		for reading and writing. Existing and subsequent
113c2746a1eSMauro Carvalho Chehab		poll(2) support configuration via the edge attribute
114c2746a1eSMauro Carvalho Chehab		for "rising" and "falling" edges will follow this
115c2746a1eSMauro Carvalho Chehab		setting.
116c2746a1eSMauro Carvalho Chehab
117c2746a1eSMauro Carvalho ChehabGPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
118c2746a1eSMauro Carvalho Chehabcontroller implementing GPIOs starting at #42) and have the following
119c2746a1eSMauro Carvalho Chehabread-only attributes:
120c2746a1eSMauro Carvalho Chehab
121c2746a1eSMauro Carvalho Chehab    /sys/class/gpio/gpiochipN/
122c2746a1eSMauro Carvalho Chehab
123c2746a1eSMauro Carvalho Chehab	"base" ...
124c2746a1eSMauro Carvalho Chehab		same as N, the first GPIO managed by this chip
125c2746a1eSMauro Carvalho Chehab
126c2746a1eSMauro Carvalho Chehab	"label" ...
127c2746a1eSMauro Carvalho Chehab		provided for diagnostics (not always unique)
128c2746a1eSMauro Carvalho Chehab
129c2746a1eSMauro Carvalho Chehab	"ngpio" ...
130c2746a1eSMauro Carvalho Chehab		how many GPIOs this manages (N to N + ngpio - 1)
131c2746a1eSMauro Carvalho Chehab
132c2746a1eSMauro Carvalho ChehabBoard documentation should in most cases cover what GPIOs are used for
133c2746a1eSMauro Carvalho Chehabwhat purposes. However, those numbers are not always stable; GPIOs on
134c2746a1eSMauro Carvalho Chehaba daughtercard might be different depending on the base board being used,
135c2746a1eSMauro Carvalho Chehabor other cards in the stack. In such cases, you may need to use the
136c2746a1eSMauro Carvalho Chehabgpiochip nodes (possibly in conjunction with schematics) to determine
137c2746a1eSMauro Carvalho Chehabthe correct GPIO number to use for a given signal.
138c2746a1eSMauro Carvalho Chehab
139c2746a1eSMauro Carvalho Chehab
140c2746a1eSMauro Carvalho ChehabExporting from Kernel code
141c2746a1eSMauro Carvalho Chehab--------------------------
142c2746a1eSMauro Carvalho ChehabKernel code can explicitly manage exports of GPIOs which have already been
143c2746a1eSMauro Carvalho Chehabrequested using gpio_request()::
144c2746a1eSMauro Carvalho Chehab
145c2746a1eSMauro Carvalho Chehab	/* export the GPIO to userspace */
146c2746a1eSMauro Carvalho Chehab	int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
147c2746a1eSMauro Carvalho Chehab
148*d74e3166SArnd Bergmann	/* reverse gpiod_export() */
149c2746a1eSMauro Carvalho Chehab	void gpiod_unexport(struct gpio_desc *desc);
150c2746a1eSMauro Carvalho Chehab
151c2746a1eSMauro Carvalho Chehab	/* create a sysfs link to an exported GPIO node */
152c2746a1eSMauro Carvalho Chehab	int gpiod_export_link(struct device *dev, const char *name,
153c2746a1eSMauro Carvalho Chehab		      struct gpio_desc *desc);
154c2746a1eSMauro Carvalho Chehab
155c2746a1eSMauro Carvalho ChehabAfter a kernel driver requests a GPIO, it may only be made available in
156c2746a1eSMauro Carvalho Chehabthe sysfs interface by gpiod_export(). The driver can control whether the
157c2746a1eSMauro Carvalho Chehabsignal direction may change. This helps drivers prevent userspace code
158c2746a1eSMauro Carvalho Chehabfrom accidentally clobbering important system state.
159c2746a1eSMauro Carvalho Chehab
160c2746a1eSMauro Carvalho ChehabThis explicit exporting can help with debugging (by making some kinds
161c2746a1eSMauro Carvalho Chehabof experiments easier), or can provide an always-there interface that's
162c2746a1eSMauro Carvalho Chehabsuitable for documenting as part of a board support package.
163c2746a1eSMauro Carvalho Chehab
164c2746a1eSMauro Carvalho ChehabAfter the GPIO has been exported, gpiod_export_link() allows creating
165c2746a1eSMauro Carvalho Chehabsymlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
166c2746a1eSMauro Carvalho Chehabuse this to provide the interface under their own device in sysfs with
167c2746a1eSMauro Carvalho Chehaba descriptive name.
168