sysfs.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. GPIO Sysfs Interface for Userspace
  2. ==================================
  3. .. warning::
  4. This API is obsoleted by the chardev.rst and the ABI documentation has
  5. been moved to Documentation/ABI/obsolete/sysfs-gpio.
  6. New developments should use the chardev.rst, and existing developments are
  7. encouraged to migrate as soon as possible, as this API will be removed
  8. in the future.
  9. This interface will continue to be maintained for the migration period,
  10. but new features will only be added to the new API.
  11. The obsolete sysfs ABI
  12. ----------------------
  13. Platforms which use the "gpiolib" implementors framework may choose to
  14. configure a sysfs user interface to GPIOs. This is different from the
  15. debugfs interface, since it provides control over GPIO direction and
  16. value instead of just showing a gpio state summary. Plus, it could be
  17. present on production systems without debugging support.
  18. Given appropriate hardware documentation for the system, userspace could
  19. know for example that GPIO #23 controls the write protect line used to
  20. protect boot loader segments in flash memory. System upgrade procedures
  21. may need to temporarily remove that protection, first importing a GPIO,
  22. then changing its output state, then updating the code before re-enabling
  23. the write protection. In normal use, GPIO #23 would never be touched,
  24. and the kernel would have no need to know about it.
  25. Again depending on appropriate hardware documentation, on some systems
  26. userspace GPIO can be used to determine system configuration data that
  27. standard kernels won't know about. And for some tasks, simple userspace
  28. GPIO drivers could be all that the system really needs.
  29. .. note::
  30. Do NOT abuse sysfs to control hardware that has proper kernel drivers.
  31. Please read Documentation/driver-api/gpio/drivers-on-gpio.rst
  32. to avoid reinventing kernel wheels in userspace.
  33. I MEAN IT. REALLY.
  34. Paths in Sysfs
  35. --------------
  36. There are three kinds of entries in /sys/class/gpio:
  37. - Control interfaces used to get userspace control over GPIOs;
  38. - GPIOs themselves; and
  39. - GPIO controllers ("gpio_chip" instances).
  40. That's in addition to standard files including the "device" symlink.
  41. The control interfaces are write-only:
  42. /sys/class/gpio/
  43. "export" ...
  44. Userspace may ask the kernel to export control of
  45. a GPIO to userspace by writing its number to this file.
  46. Example: "echo 19 > export" will create a "gpio19" node
  47. for GPIO #19, if that's not requested by kernel code.
  48. "unexport" ...
  49. Reverses the effect of exporting to userspace.
  50. Example: "echo 19 > unexport" will remove a "gpio19"
  51. node exported using the "export" file.
  52. GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
  53. and have the following read/write attributes:
  54. /sys/class/gpio/gpioN/
  55. "direction" ...
  56. reads as either "in" or "out". This value may
  57. normally be written. Writing as "out" defaults to
  58. initializing the value as low. To ensure glitch free
  59. operation, values "low" and "high" may be written to
  60. configure the GPIO as an output with that initial value.
  61. Note that this attribute *will not exist* if the kernel
  62. doesn't support changing the direction of a GPIO, or
  63. it was exported by kernel code that didn't explicitly
  64. allow userspace to reconfigure this GPIO's direction.
  65. "value" ...
  66. reads as either 0 (inactive) or 1 (active). If the GPIO
  67. is configured as an output, this value may be written;
  68. any nonzero value is treated as active.
  69. If the pin can be configured as interrupt-generating interrupt
  70. and if it has been configured to generate interrupts (see the
  71. description of "edge"), you can poll(2) on that file and
  72. poll(2) will return whenever the interrupt was triggered. If
  73. you use poll(2), set the events POLLPRI and POLLERR. If you
  74. use select(2), set the file descriptor in exceptfds. After
  75. poll(2) returns, use pread(2) to read the value at offset
  76. zero. Alternatively, either lseek(2) to the beginning of the
  77. sysfs file and read the new value or close the file and
  78. re-open it to read the value.
  79. "edge" ...
  80. reads as either "none", "rising", "falling", or
  81. "both". Write these strings to select the signal edge(s)
  82. that will make poll(2) on the "value" file return.
  83. This file exists only if the pin can be configured as an
  84. interrupt generating input pin.
  85. "active_low" ...
  86. reads as either 0 (false) or 1 (true). Write
  87. any nonzero value to invert the value attribute both
  88. for reading and writing. Existing and subsequent
  89. poll(2) support configuration via the edge attribute
  90. for "rising" and "falling" edges will follow this
  91. setting.
  92. GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
  93. controller implementing GPIOs starting at #42) and have the following
  94. read-only attributes:
  95. /sys/class/gpio/gpiochipN/
  96. "base" ...
  97. same as N, the first GPIO managed by this chip
  98. "label" ...
  99. provided for diagnostics (not always unique)
  100. "ngpio" ...
  101. how many GPIOs this manages (N to N + ngpio - 1)
  102. Board documentation should in most cases cover what GPIOs are used for
  103. what purposes. However, those numbers are not always stable; GPIOs on
  104. a daughtercard might be different depending on the base board being used,
  105. or other cards in the stack. In such cases, you may need to use the
  106. gpiochip nodes (possibly in conjunction with schematics) to determine
  107. the correct GPIO number to use for a given signal.
  108. Exporting from Kernel code
  109. --------------------------
  110. Kernel code can explicitly manage exports of GPIOs which have already been
  111. requested using gpio_request()::
  112. /* export the GPIO to userspace */
  113. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  114. /* reverse gpiod_export() */
  115. void gpiod_unexport(struct gpio_desc *desc);
  116. /* create a sysfs link to an exported GPIO node */
  117. int gpiod_export_link(struct device *dev, const char *name,
  118. struct gpio_desc *desc);
  119. After a kernel driver requests a GPIO, it may only be made available in
  120. the sysfs interface by gpiod_export(). The driver can control whether the
  121. signal direction may change. This helps drivers prevent userspace code
  122. from accidentally clobbering important system state.
  123. This explicit exporting can help with debugging (by making some kinds
  124. of experiments easier), or can provide an always-there interface that's
  125. suitable for documenting as part of a board support package.
  126. After the GPIO has been exported, gpiod_export_link() allows creating
  127. symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
  128. use this to provide the interface under their own device in sysfs with
  129. a descriptive name.