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