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