sysfs.txt 5.9 KB

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