aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/gpio/sysfs.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/gpio/sysfs.txt')
-rw-r--r--Documentation/gpio/sysfs.txt155
1 files changed, 155 insertions, 0 deletions
diff --git a/Documentation/gpio/sysfs.txt b/Documentation/gpio/sysfs.txt
new file mode 100644
index 000000000000..c2c3a97f8ff7
--- /dev/null
+++ b/Documentation/gpio/sysfs.txt
@@ -0,0 +1,155 @@
1GPIO Sysfs Interface for Userspace
2==================================
3
4Platforms which use the "gpiolib" implementors framework may choose to
5configure a sysfs user interface to GPIOs. This is different from the
6debugfs interface, since it provides control over GPIO direction and
7value instead of just showing a gpio state summary. Plus, it could be
8present on production systems without debugging support.
9
10Given appropriate hardware documentation for the system, userspace could
11know for example that GPIO #23 controls the write protect line used to
12protect boot loader segments in flash memory. System upgrade procedures
13may need to temporarily remove that protection, first importing a GPIO,
14then changing its output state, then updating the code before re-enabling
15the write protection. In normal use, GPIO #23 would never be touched,
16and the kernel would have no need to know about it.
17
18Again depending on appropriate hardware documentation, on some systems
19userspace GPIO can be used to determine system configuration data that
20standard kernels won't know about. And for some tasks, simple userspace
21GPIO drivers could be all that the system really needs.
22
23Note that standard kernel drivers exist for common "LEDs and Buttons"
24GPIO tasks: "leds-gpio" and "gpio_keys", respectively. Use those
25instead of talking directly to the GPIOs; they integrate with kernel
26frameworks better than your userspace code could.
27
28
29Paths in Sysfs
30--------------
31There are three kinds of entry in /sys/class/gpio:
32
33 - Control interfaces used to get userspace control over GPIOs;
34
35 - GPIOs themselves; and
36
37 - GPIO controllers ("gpio_chip" instances).
38
39That's in addition to standard files including the "device" symlink.
40
41The control interfaces are write-only:
42
43 /sys/class/gpio/
44
45 "export" ... Userspace may ask the kernel to export control of
46 a GPIO to userspace by writing its number to this file.
47
48 Example: "echo 19 > export" will create a "gpio19" node
49 for GPIO #19, if that's not requested by kernel code.
50
51 "unexport" ... Reverses the effect of exporting to userspace.
52
53 Example: "echo 19 > unexport" will remove a "gpio19"
54 node exported using the "export" file.
55
56GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
57and have the following read/write attributes:
58
59 /sys/class/gpio/gpioN/
60
61 "direction" ... reads as either "in" or "out". This value may
62 normally be written. Writing as "out" defaults to
63 initializing the value as low. To ensure glitch free
64 operation, values "low" and "high" may be written to
65 configure the GPIO as an output with that initial value.
66
67 Note that this attribute *will not exist* if the kernel
68 doesn't support changing the direction of a GPIO, or
69 it was exported by kernel code that didn't explicitly
70 allow userspace to reconfigure this GPIO's direction.
71
72 "value" ... reads as either 0 (low) or 1 (high). If the GPIO
73 is configured as an output, this value may be written;
74 any nonzero value is treated as high.
75
76 If the pin can be configured as interrupt-generating interrupt
77 and if it has been configured to generate interrupts (see the
78 description of "edge"), you can poll(2) on that file and
79 poll(2) will return whenever the interrupt was triggered. If
80 you use poll(2), set the events POLLPRI and POLLERR. If you
81 use select(2), set the file descriptor in exceptfds. After
82 poll(2) returns, either lseek(2) to the beginning of the sysfs
83 file and read the new value or close the file and re-open it
84 to read the value.
85
86 "edge" ... reads as either "none", "rising", "falling", or
87 "both". Write these strings to select the signal edge(s)
88 that will make poll(2) on the "value" file return.
89
90 This file exists only if the pin can be configured as an
91 interrupt generating input pin.
92
93 "active_low" ... reads as either 0 (false) or 1 (true). Write
94 any nonzero value to invert the value attribute both
95 for reading and writing. Existing and subsequent
96 poll(2) support configuration via the edge attribute
97 for "rising" and "falling" edges will follow this
98 setting.
99
100GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
101controller implementing GPIOs starting at #42) and have the following
102read-only attributes:
103
104 /sys/class/gpio/gpiochipN/
105
106 "base" ... same as N, the first GPIO managed by this chip
107
108 "label" ... provided for diagnostics (not always unique)
109
110 "ngpio" ... how many GPIOs this manges (N to N + ngpio - 1)
111
112Board documentation should in most cases cover what GPIOs are used for
113what purposes. However, those numbers are not always stable; GPIOs on
114a daughtercard might be different depending on the base board being used,
115or other cards in the stack. In such cases, you may need to use the
116gpiochip nodes (possibly in conjunction with schematics) to determine
117the correct GPIO number to use for a given signal.
118
119
120Exporting from Kernel code
121--------------------------
122Kernel code can explicitly manage exports of GPIOs which have already been
123requested using gpio_request():
124
125 /* export the GPIO to userspace */
126 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
127
128 /* reverse gpio_export() */
129 void gpiod_unexport(struct gpio_desc *desc);
130
131 /* create a sysfs link to an exported GPIO node */
132 int gpiod_export_link(struct device *dev, const char *name,
133 struct gpio_desc *desc);
134
135 /* change the polarity of a GPIO node in sysfs */
136 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
137
138After a kernel driver requests a GPIO, it may only be made available in
139the sysfs interface by gpiod_export(). The driver can control whether the
140signal direction may change. This helps drivers prevent userspace code
141from accidentally clobbering important system state.
142
143This explicit exporting can help with debugging (by making some kinds
144of experiments easier), or can provide an always-there interface that's
145suitable for documenting as part of a board support package.
146
147After the GPIO has been exported, gpiod_export_link() allows creating
148symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
149use this to provide the interface under their own device in sysfs with
150a descriptive name.
151
152Drivers can use gpiod_sysfs_set_active_low() to hide GPIO line polarity
153differences between boards from user space. Polarity change can be done both
154before and after gpiod_export(), and previously enabled poll(2) support for
155either rising or falling edge will be reconfigured to follow this setting.