brintos

brintos / linux-shallow public Read only

0
0
Text · 6.5 KiB · bd64896 Raw
172 lines · plain
1GPIO Sysfs Interface for Userspace2==================================3 4.. warning::5   This API is obsoleted by the chardev.rst and the ABI documentation has6   been moved to Documentation/ABI/obsolete/sysfs-gpio.7 8   New developments should use the chardev.rst, and existing developments are9   encouraged to migrate as soon as possible, as this API will be removed10   in the future.11 12   This interface will continue to be maintained for the migration period,13   but new features will only be added to the new API.14 15The obsolete sysfs ABI16----------------------17Platforms which use the "gpiolib" implementors framework may choose to18configure a sysfs user interface to GPIOs. This is different from the19debugfs interface, since it provides control over GPIO direction and20value instead of just showing a gpio state summary. Plus, it could be21present on production systems without debugging support.22 23Given appropriate hardware documentation for the system, userspace could24know for example that GPIO #23 controls the write protect line used to25protect boot loader segments in flash memory. System upgrade procedures26may need to temporarily remove that protection, first importing a GPIO,27then changing its output state, then updating the code before re-enabling28the write protection. In normal use, GPIO #23 would never be touched,29and the kernel would have no need to know about it.30 31Again depending on appropriate hardware documentation, on some systems32userspace GPIO can be used to determine system configuration data that33standard kernels won't know about. And for some tasks, simple userspace34GPIO drivers could be all that the system really needs.35 36.. note::37   Do NOT abuse sysfs to control hardware that has proper kernel drivers.38   Please read Documentation/driver-api/gpio/drivers-on-gpio.rst39   to avoid reinventing kernel wheels in userspace.40 41   I MEAN IT. REALLY.42 43Paths in Sysfs44--------------45There are three kinds of entries in /sys/class/gpio:46 47   -	Control interfaces used to get userspace control over GPIOs;48 49   -	GPIOs themselves; and50 51   -	GPIO controllers ("gpio_chip" instances).52 53That's in addition to standard files including the "device" symlink.54 55The control interfaces are write-only:56 57    /sys/class/gpio/58 59	"export" ...60		Userspace may ask the kernel to export control of61		a GPIO to userspace by writing its number to this file.62 63		Example:  "echo 19 > export" will create a "gpio19" node64		for GPIO #19, if that's not requested by kernel code.65 66	"unexport" ...67		Reverses the effect of exporting to userspace.68 69		Example:  "echo 19 > unexport" will remove a "gpio19"70		node exported using the "export" file.71 72GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)73and have the following read/write attributes:74 75    /sys/class/gpio/gpioN/76 77	"direction" ...78		reads as either "in" or "out". This value may79		normally be written. Writing as "out" defaults to80		initializing the value as low. To ensure glitch free81		operation, values "low" and "high" may be written to82		configure the GPIO as an output with that initial value.83 84		Note that this attribute *will not exist* if the kernel85		doesn't support changing the direction of a GPIO, or86		it was exported by kernel code that didn't explicitly87		allow userspace to reconfigure this GPIO's direction.88 89	"value" ...90		reads as either 0 (inactive) or 1 (active). If the GPIO91		is configured as an output, this value may be written;92		any nonzero value is treated as active.93 94		If the pin can be configured as interrupt-generating interrupt95		and if it has been configured to generate interrupts (see the96		description of "edge"), you can poll(2) on that file and97		poll(2) will return whenever the interrupt was triggered. If98		you use poll(2), set the events POLLPRI and POLLERR. If you99		use select(2), set the file descriptor in exceptfds. After100		poll(2) returns, use pread(2) to read the value at offset101		zero. Alternatively, either lseek(2) to the beginning of the102		sysfs file and read the new value or close the file and103		re-open it to read the value.104 105	"edge" ...106		reads as either "none", "rising", "falling", or107		"both". Write these strings to select the signal edge(s)108		that will make poll(2) on the "value" file return.109 110		This file exists only if the pin can be configured as an111		interrupt generating input pin.112 113	"active_low" ...114		reads as either 0 (false) or 1 (true). Write115		any nonzero value to invert the value attribute both116		for reading and writing. Existing and subsequent117		poll(2) support configuration via the edge attribute118		for "rising" and "falling" edges will follow this119		setting.120 121GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the122controller implementing GPIOs starting at #42) and have the following123read-only attributes:124 125    /sys/class/gpio/gpiochipN/126 127	"base" ...128		same as N, the first GPIO managed by this chip129 130	"label" ...131		provided for diagnostics (not always unique)132 133	"ngpio" ...134		how many GPIOs this manages (N to N + ngpio - 1)135 136Board documentation should in most cases cover what GPIOs are used for137what purposes. However, those numbers are not always stable; GPIOs on138a daughtercard might be different depending on the base board being used,139or other cards in the stack. In such cases, you may need to use the140gpiochip nodes (possibly in conjunction with schematics) to determine141the correct GPIO number to use for a given signal.142 143 144Exporting from Kernel code145--------------------------146Kernel code can explicitly manage exports of GPIOs which have already been147requested using gpio_request()::148 149	/* export the GPIO to userspace */150	int gpiod_export(struct gpio_desc *desc, bool direction_may_change);151 152	/* reverse gpiod_export() */153	void gpiod_unexport(struct gpio_desc *desc);154 155	/* create a sysfs link to an exported GPIO node */156	int gpiod_export_link(struct device *dev, const char *name,157		      struct gpio_desc *desc);158 159After a kernel driver requests a GPIO, it may only be made available in160the sysfs interface by gpiod_export(). The driver can control whether the161signal direction may change. This helps drivers prevent userspace code162from accidentally clobbering important system state.163 164This explicit exporting can help with debugging (by making some kinds165of experiments easier), or can provide an always-there interface that's166suitable for documenting as part of a board support package.167 168After the GPIO has been exported, gpiod_export_link() allows creating169symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can170use this to provide the interface under their own device in sysfs with171a descriptive name.172