gpiomux.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. This document provides an overview of the msm_gpiomux interface, which
  2. is used to provide gpio pin multiplexing and configuration on mach-msm
  3. targets.
  4. History
  5. =======
  6. The first-generation API for gpio configuration & multiplexing on msm
  7. is the function gpio_tlmm_config(). This function has a few notable
  8. shortcomings, which led to its deprecation and replacement by gpiomux:
  9. The 'disable' parameter: Setting the second parameter to
  10. gpio_tlmm_config to GPIO_CFG_DISABLE tells the peripheral
  11. processor in charge of the subsystem to perform a look-up into a
  12. low-power table and apply the low-power/sleep setting for the pin.
  13. As the msm family evolved this became problematic. Not all pins
  14. have sleep settings, not all peripheral processors will accept requests
  15. to apply said sleep settings, and not all msm targets have their gpio
  16. subsystems managed by a peripheral processor. In order to get consistent
  17. behavior on all targets, drivers are forced to ignore this parameter,
  18. rendering it useless.
  19. The 'direction' flag: for all mux-settings other than raw-gpio (0),
  20. the output-enable bit of a gpio is hard-wired to a known
  21. input (usually VDD or ground). For those settings, the direction flag
  22. is meaningless at best, and deceptive at worst. In addition, using the
  23. direction flag to change output-enable (OE) directly can cause trouble in
  24. gpiolib, which has no visibility into gpio direction changes made
  25. in this way. Direction control in gpio mode should be made through gpiolib.
  26. Key Features of gpiomux
  27. =======================
  28. - A consistent interface across all generations of msm. Drivers can expect
  29. the same results on every target.
  30. - gpiomux plays nicely with gpiolib. Functions that should belong to gpiolib
  31. are left to gpiolib and not duplicated here. gpiomux is written with the
  32. intent that gpio_chips will call gpiomux reference-counting methods
  33. from their request() and free() hooks, providing full integration.
  34. - Tabular configuration. Instead of having to call gpio_tlmm_config
  35. hundreds of times, gpio configuration is placed in a single table.
  36. - Per-gpio sleep. Each gpio is individually reference counted, allowing only
  37. those lines which are in use to be put in high-power states.
  38. - 0 means 'do nothing': all flags are designed so that the default memset-zero
  39. equates to a sensible default of 'no configuration', preventing users
  40. from having to provide hundreds of 'no-op' configs for unused or
  41. unwanted lines.
  42. Usage
  43. =====
  44. To use gpiomux, provide configuration information for relevant gpio lines
  45. in the msm_gpiomux_configs table. Since a 0 equates to "unconfigured",
  46. only those lines to be managed by gpiomux need to be specified. Here
  47. is a completely fictional example:
  48. struct msm_gpiomux_config msm_gpiomux_configs[GPIOMUX_NGPIOS] = {
  49. [12] = {
  50. .active = GPIOMUX_VALID | GPIOMUX_DRV_8MA | GPIOMUX_FUNC_1,
  51. .suspended = GPIOMUX_VALID | GPIOMUX_PULL_DOWN,
  52. },
  53. [34] = {
  54. .suspended = GPIOMUX_VALID | GPIOMUX_PULL_DOWN,
  55. },
  56. };
  57. To indicate that a gpio is in use, call msm_gpiomux_get() to increase
  58. its reference count. To decrease the reference count, call msm_gpiomux_put().
  59. The effect of this configuration is as follows:
  60. When the system boots, gpios 12 and 34 will be initialized with their
  61. 'suspended' configurations. All other gpios, which were left unconfigured,
  62. will not be touched.
  63. When msm_gpiomux_get() is called on gpio 12 to raise its reference count
  64. above 0, its active configuration will be applied. Since no other gpio
  65. line has a valid active configuration, msm_gpiomux_get() will have no
  66. effect on any other line.
  67. When msm_gpiomux_put() is called on gpio 12 or 34 to drop their reference
  68. count to 0, their suspended configurations will be applied.
  69. Since no other gpio line has a valid suspended configuration, no other
  70. gpio line will be effected by msm_gpiomux_put(). Since gpio 34 has no valid
  71. active configuration, this is effectively a no-op for gpio 34 as well,
  72. with one small caveat, see the section "About Output-Enable Settings".
  73. All of the GPIOMUX_VALID flags may seem like unnecessary overhead, but
  74. they address some important issues. As unused entries (all those
  75. except 12 and 34) are zero-filled, gpiomux needs a way to distinguish
  76. the used fields from the unused. In addition, the all-zero pattern
  77. is a valid configuration! Therefore, gpiomux defines an additional bit
  78. which is used to indicate when a field is used. This has the pleasant
  79. side-effect of allowing calls to msm_gpiomux_write to use '0' to indicate
  80. that a value should not be changed:
  81. msm_gpiomux_write(0, GPIOMUX_VALID, 0);
  82. replaces the active configuration of gpio 0 with an all-zero configuration,
  83. but leaves the suspended configuration as it was.
  84. Static Configurations
  85. =====================
  86. To install a static configuration, which is applied at boot and does
  87. not change after that, install a configuration with a suspended component
  88. but no active component, as in the previous example:
  89. [34] = {
  90. .suspended = GPIOMUX_VALID | GPIOMUX_PULL_DOWN,
  91. },
  92. The suspended setting is applied during boot, and the lack of any valid
  93. active setting prevents any other setting from being applied at runtime.
  94. If other subsystems attempting to access the line is a concern, one could
  95. *really* anchor the configuration down by calling msm_gpiomux_get on the
  96. line at initialization to move the line into active mode. With the line
  97. held, it will never be re-suspended, and with no valid active configuration,
  98. no new configurations will be applied.
  99. But then, if having other subsystems grabbing for the line is truly a concern,
  100. it should be reserved with gpio_request instead, which carries an implicit
  101. msm_gpiomux_get.
  102. gpiomux and gpiolib
  103. ===================
  104. It is expected that msm gpio_chips will call msm_gpiomux_get() and
  105. msm_gpiomux_put() from their request and free hooks, like this fictional
  106. example:
  107. static int request(struct gpio_chip *chip, unsigned offset)
  108. {
  109. return msm_gpiomux_get(chip->base + offset);
  110. }
  111. static void free(struct gpio_chip *chip, unsigned offset)
  112. {
  113. msm_gpiomux_put(chip->base + offset);
  114. }
  115. ...somewhere in a gpio_chip declaration...
  116. .request = request,
  117. .free = free,
  118. This provides important functionality:
  119. - It guarantees that a gpio line will have its 'active' config applied
  120. when the line is requested, and will not be suspended while the line
  121. remains requested; and
  122. - It guarantees that gpio-direction settings from gpiolib behave sensibly.
  123. See "About Output-Enable Settings."
  124. This mechanism allows for "auto-request" of gpiomux lines via gpiolib
  125. when it is suitable. Drivers wishing more exact control are, of course,
  126. free to also use msm_gpiomux_set and msm_gpiomux_get.
  127. About Output-Enable Settings
  128. ============================
  129. Some msm targets do not have the ability to query the current gpio
  130. configuration setting. This means that changes made to the output-enable
  131. (OE) bit by gpiolib cannot be consistently detected and preserved by gpiomux.
  132. Therefore, when gpiomux applies a configuration setting, any direction
  133. settings which may have been applied by gpiolib are lost and the default
  134. input settings are re-applied.
  135. For this reason, drivers should not assume that gpio direction settings
  136. continue to hold if they free and then re-request a gpio. This seems like
  137. common sense - after all, anybody could have obtained the line in the
  138. meantime - but it needs saying.
  139. This also means that calls to msm_gpiomux_write will reset the OE bit,
  140. which means that if the gpio line is held by a client of gpiolib and
  141. msm_gpiomux_write is called, the direction setting has been lost and
  142. gpiolib's internal state has been broken.
  143. Release gpio lines before reconfiguring them.