Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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