pinctrl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #ifndef __PINCTRL_H
  7. #define __PINCTRL_H
  8. /**
  9. * struct pinconf_param - pin config parameters
  10. *
  11. * @property: property name in DT nodes
  12. * @param: ID for this config parameter
  13. * @default_value: default value for this config parameter used in case
  14. * no value is specified in DT nodes
  15. */
  16. struct pinconf_param {
  17. const char * const property;
  18. unsigned int param;
  19. u32 default_value;
  20. };
  21. /**
  22. * struct pinctrl_ops - pin control operations, to be implemented by
  23. * pin controller drivers.
  24. *
  25. * The @set_state is the only mandatory operation. You can implement your
  26. * pinctrl driver with its own @set_state. In this case, the other callbacks
  27. * are not required. Otherwise, generic pinctrl framework is also available;
  28. * use pinctrl_generic_set_state for @set_state, and implement other operations
  29. * depending on your necessity.
  30. *
  31. * @get_pins_count: return number of selectable named pins available
  32. * in this driver. (necessary to parse "pins" property in DTS)
  33. * @get_pin_name: return the pin name of the pin selector,
  34. * called by the core to figure out which pin it shall do
  35. * operations to. (necessary to parse "pins" property in DTS)
  36. * @get_groups_count: return number of selectable named groups available
  37. * in this driver. (necessary to parse "groups" property in DTS)
  38. * @get_group_name: return the group name of the group selector,
  39. * called by the core to figure out which pin group it shall do
  40. * operations to. (necessary to parse "groups" property in DTS)
  41. * @get_functions_count: return number of selectable named functions available
  42. * in this driver. (necessary for pin-muxing)
  43. * @get_function_name: return the function name of the muxing selector,
  44. * called by the core to figure out which mux setting it shall map a
  45. * certain device to. (necessary for pin-muxing)
  46. * @pinmux_set: enable a certain muxing function with a certain pin.
  47. * The @func_selector selects a certain function whereas @pin_selector
  48. * selects a certain pin to be used. On simple controllers one of them
  49. * may be ignored. (necessary for pin-muxing against a single pin)
  50. * @pinmux_group_set: enable a certain muxing function with a certain pin
  51. * group. The @func_selector selects a certain function whereas
  52. * @group_selector selects a certain set of pins to be used. On simple
  53. * controllers one of them may be ignored.
  54. * (necessary for pin-muxing against a pin group)
  55. * @pinconf_num_params: number of driver-specific parameters to be parsed
  56. * from device trees (necessary for pin-configuration)
  57. * @pinconf_params: list of driver_specific parameters to be parsed from
  58. * device trees (necessary for pin-configuration)
  59. * @pinconf_set: configure an individual pin with a given parameter.
  60. * (necessary for pin-configuration against a single pin)
  61. * @pinconf_group_set: configure all pins in a group with a given parameter.
  62. * (necessary for pin-configuration against a pin group)
  63. * @set_state: do pinctrl operations specified by @config, a pseudo device
  64. * pointing a config node. (necessary for pinctrl_full)
  65. * @set_state_simple: do needed pinctrl operations for a peripherl @periph.
  66. * (necessary for pinctrl_simple)
  67. */
  68. struct pinctrl_ops {
  69. int (*get_pins_count)(struct udevice *dev);
  70. const char *(*get_pin_name)(struct udevice *dev, unsigned selector);
  71. int (*get_groups_count)(struct udevice *dev);
  72. const char *(*get_group_name)(struct udevice *dev, unsigned selector);
  73. int (*get_functions_count)(struct udevice *dev);
  74. const char *(*get_function_name)(struct udevice *dev,
  75. unsigned selector);
  76. int (*pinmux_set)(struct udevice *dev, unsigned pin_selector,
  77. unsigned func_selector);
  78. int (*pinmux_group_set)(struct udevice *dev, unsigned group_selector,
  79. unsigned func_selector);
  80. unsigned int pinconf_num_params;
  81. const struct pinconf_param *pinconf_params;
  82. int (*pinconf_set)(struct udevice *dev, unsigned pin_selector,
  83. unsigned param, unsigned argument);
  84. int (*pinconf_group_set)(struct udevice *dev, unsigned group_selector,
  85. unsigned param, unsigned argument);
  86. int (*set_state)(struct udevice *dev, struct udevice *config);
  87. /* for pinctrl-simple */
  88. int (*set_state_simple)(struct udevice *dev, struct udevice *periph);
  89. /**
  90. * request() - Request a particular pinctrl function
  91. *
  92. * This activates the selected function.
  93. *
  94. * @dev: Device to adjust (UCLASS_PINCTRL)
  95. * @func: Function number (driver-specific)
  96. * @return 0 if OK, -ve on error
  97. */
  98. int (*request)(struct udevice *dev, int func, int flags);
  99. /**
  100. * get_periph_id() - get the peripheral ID for a device
  101. *
  102. * This generally looks at the peripheral's device tree node to work
  103. * out the peripheral ID. The return value is normally interpreted as
  104. * enum periph_id. so long as this is defined by the platform (which it
  105. * should be).
  106. *
  107. * @dev: Pinctrl device to use for decoding
  108. * @periph: Device to check
  109. * @return peripheral ID of @periph, or -ENOENT on error
  110. */
  111. int (*get_periph_id)(struct udevice *dev, struct udevice *periph);
  112. /**
  113. * get_gpio_mux() - get the mux value for a particular GPIO
  114. *
  115. * This allows the raw mux value for a GPIO to be obtained. It is
  116. * useful for displaying the function being used by that GPIO, such
  117. * as with the 'gpio' command. This function is internal to the GPIO
  118. * subsystem and should not be used by generic code. Typically it is
  119. * used by a GPIO driver with knowledge of the SoC pinctrl setup.
  120. *
  121. * @dev: Pinctrl device to use
  122. * @banknum: GPIO bank number
  123. * @index: GPIO index within the bank
  124. * @return mux value (SoC-specific, e.g. 0 for input, 1 for output)
  125. */
  126. int (*get_gpio_mux)(struct udevice *dev, int banknum, int index);
  127. };
  128. #define pinctrl_get_ops(dev) ((struct pinctrl_ops *)(dev)->driver->ops)
  129. /**
  130. * Generic pin configuration paramters
  131. *
  132. * @PIN_CONFIG_BIAS_DISABLE: disable any pin bias on the pin, a
  133. * transition from say pull-up to pull-down implies that you disable
  134. * pull-up in the process, this setting disables all biasing.
  135. * @PIN_CONFIG_BIAS_HIGH_IMPEDANCE: the pin will be set to a high impedance
  136. * mode, also know as "third-state" (tristate) or "high-Z" or "floating".
  137. * On output pins this effectively disconnects the pin, which is useful
  138. * if for example some other pin is going to drive the signal connected
  139. * to it for a while. Pins used for input are usually always high
  140. * impedance.
  141. * @PIN_CONFIG_BIAS_BUS_HOLD: the pin will be set to weakly latch so that it
  142. * weakly drives the last value on a tristate bus, also known as a "bus
  143. * holder", "bus keeper" or "repeater". This allows another device on the
  144. * bus to change the value by driving the bus high or low and switching to
  145. * tristate. The argument is ignored.
  146. * @PIN_CONFIG_BIAS_PULL_UP: the pin will be pulled up (usually with high
  147. * impedance to VDD). If the argument is != 0 pull-up is enabled,
  148. * if it is 0, pull-up is total, i.e. the pin is connected to VDD.
  149. * @PIN_CONFIG_BIAS_PULL_DOWN: the pin will be pulled down (usually with high
  150. * impedance to GROUND). If the argument is != 0 pull-down is enabled,
  151. * if it is 0, pull-down is total, i.e. the pin is connected to GROUND.
  152. * @PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: the pin will be pulled up or down based
  153. * on embedded knowledge of the controller hardware, like current mux
  154. * function. The pull direction and possibly strength too will normally
  155. * be decided completely inside the hardware block and not be readable
  156. * from the kernel side.
  157. * If the argument is != 0 pull up/down is enabled, if it is 0, the
  158. * configuration is ignored. The proper way to disable it is to use
  159. * @PIN_CONFIG_BIAS_DISABLE.
  160. * @PIN_CONFIG_DRIVE_PUSH_PULL: the pin will be driven actively high and
  161. * low, this is the most typical case and is typically achieved with two
  162. * active transistors on the output. Setting this config will enable
  163. * push-pull mode, the argument is ignored.
  164. * @PIN_CONFIG_DRIVE_OPEN_DRAIN: the pin will be driven with open drain (open
  165. * collector) which means it is usually wired with other output ports
  166. * which are then pulled up with an external resistor. Setting this
  167. * config will enable open drain mode, the argument is ignored.
  168. * @PIN_CONFIG_DRIVE_OPEN_SOURCE: the pin will be driven with open source
  169. * (open emitter). Setting this config will enable open source mode, the
  170. * argument is ignored.
  171. * @PIN_CONFIG_DRIVE_STRENGTH: the pin will sink or source at most the current
  172. * passed as argument. The argument is in mA.
  173. * @PIN_CONFIG_INPUT_ENABLE: enable the pin's input. Note that this does not
  174. * affect the pin's ability to drive output. 1 enables input, 0 disables
  175. * input.
  176. * @PIN_CONFIG_INPUT_SCHMITT_ENABLE: control schmitt-trigger mode on the pin.
  177. * If the argument != 0, schmitt-trigger mode is enabled. If it's 0,
  178. * schmitt-trigger mode is disabled.
  179. * @PIN_CONFIG_INPUT_SCHMITT: this will configure an input pin to run in
  180. * schmitt-trigger mode. If the schmitt-trigger has adjustable hysteresis,
  181. * the threshold value is given on a custom format as argument when
  182. * setting pins to this mode.
  183. * @PIN_CONFIG_INPUT_DEBOUNCE: this will configure the pin to debounce mode,
  184. * which means it will wait for signals to settle when reading inputs. The
  185. * argument gives the debounce time in usecs. Setting the
  186. * argument to zero turns debouncing off.
  187. * @PIN_CONFIG_POWER_SOURCE: if the pin can select between different power
  188. * supplies, the argument to this parameter (on a custom format) tells
  189. * the driver which alternative power source to use.
  190. * @PIN_CONFIG_SLEW_RATE: if the pin can select slew rate, the argument to
  191. * this parameter (on a custom format) tells the driver which alternative
  192. * slew rate to use.
  193. * @PIN_CONFIG_LOW_POWER_MODE: this will configure the pin for low power
  194. * operation, if several modes of operation are supported these can be
  195. * passed in the argument on a custom form, else just use argument 1
  196. * to indicate low power mode, argument 0 turns low power mode off.
  197. * @PIN_CONFIG_OUTPUT: this will configure the pin as an output. Use argument
  198. * 1 to indicate high level, argument 0 to indicate low level. (Please
  199. * see Documentation/pinctrl.txt, section "GPIO mode pitfalls" for a
  200. * discussion around this parameter.)
  201. * @PIN_CONFIG_END: this is the last enumerator for pin configurations, if
  202. * you need to pass in custom configurations to the pin controller, use
  203. * PIN_CONFIG_END+1 as the base offset.
  204. */
  205. #define PIN_CONFIG_BIAS_DISABLE 0
  206. #define PIN_CONFIG_BIAS_HIGH_IMPEDANCE 1
  207. #define PIN_CONFIG_BIAS_BUS_HOLD 2
  208. #define PIN_CONFIG_BIAS_PULL_UP 3
  209. #define PIN_CONFIG_BIAS_PULL_DOWN 4
  210. #define PIN_CONFIG_BIAS_PULL_PIN_DEFAULT 5
  211. #define PIN_CONFIG_DRIVE_PUSH_PULL 6
  212. #define PIN_CONFIG_DRIVE_OPEN_DRAIN 7
  213. #define PIN_CONFIG_DRIVE_OPEN_SOURCE 8
  214. #define PIN_CONFIG_DRIVE_STRENGTH 9
  215. #define PIN_CONFIG_INPUT_ENABLE 10
  216. #define PIN_CONFIG_INPUT_SCHMITT_ENABLE 11
  217. #define PIN_CONFIG_INPUT_SCHMITT 12
  218. #define PIN_CONFIG_INPUT_DEBOUNCE 13
  219. #define PIN_CONFIG_POWER_SOURCE 14
  220. #define PIN_CONFIG_SLEW_RATE 15
  221. #define PIN_CONFIG_LOW_POWER_MODE 16
  222. #define PIN_CONFIG_OUTPUT 17
  223. #define PIN_CONFIG_END 0x7FFF
  224. #if CONFIG_IS_ENABLED(PINCTRL_GENERIC)
  225. /**
  226. * pinctrl_generic_set_state() - generic set_state operation
  227. * Parse the DT node of @config and its children and handle generic properties
  228. * such as "pins", "groups", "functions", and pin configuration parameters.
  229. *
  230. * @pctldev: pinctrl device
  231. * @config: config device (pseudo device), pointing a config node in DTS
  232. * @return: 0 on success, or negative error code on failure
  233. */
  234. int pinctrl_generic_set_state(struct udevice *pctldev, struct udevice *config);
  235. #else
  236. static inline int pinctrl_generic_set_state(struct udevice *pctldev,
  237. struct udevice *config)
  238. {
  239. return -EINVAL;
  240. }
  241. #endif
  242. #if CONFIG_IS_ENABLED(PINCTRL)
  243. /**
  244. * pinctrl_select_state() - set a device to a given state
  245. *
  246. * @dev: peripheral device
  247. * @statename: state name, like "default"
  248. * @return: 0 on success, or negative error code on failure
  249. */
  250. int pinctrl_select_state(struct udevice *dev, const char *statename);
  251. #else
  252. static inline int pinctrl_select_state(struct udevice *dev,
  253. const char *statename)
  254. {
  255. return -EINVAL;
  256. }
  257. #endif
  258. /**
  259. * pinctrl_request() - Request a particular pinctrl function
  260. *
  261. * @dev: Device to check (UCLASS_PINCTRL)
  262. * @func: Function number (driver-specific)
  263. * @flags: Flags (driver-specific)
  264. * @return 0 if OK, -ve on error
  265. */
  266. int pinctrl_request(struct udevice *dev, int func, int flags);
  267. /**
  268. * pinctrl_request_noflags() - Request a particular pinctrl function
  269. *
  270. * This is similar to pinctrl_request() but uses 0 for @flags.
  271. *
  272. * @dev: Device to check (UCLASS_PINCTRL)
  273. * @func: Function number (driver-specific)
  274. * @return 0 if OK, -ve on error
  275. */
  276. int pinctrl_request_noflags(struct udevice *dev, int func);
  277. /**
  278. * pinctrl_get_periph_id() - get the peripheral ID for a device
  279. *
  280. * This generally looks at the peripheral's device tree node to work out the
  281. * peripheral ID. The return value is normally interpreted as enum periph_id.
  282. * so long as this is defined by the platform (which it should be).
  283. *
  284. * @dev: Pinctrl device to use for decoding
  285. * @periph: Device to check
  286. * @return peripheral ID of @periph, or -ENOENT on error
  287. */
  288. int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph);
  289. /**
  290. * pinctrl_decode_pin_config() - decode pin configuration flags
  291. *
  292. * This decodes some of the PIN_CONFIG values into flags, with each value
  293. * being (1 << pin_cfg). This does not support things with values like the
  294. * slew rate.
  295. *
  296. * @blob: Device tree blob
  297. * @node: Node containing the PIN_CONFIG values
  298. * @return decoded flag value, or -ve on error
  299. */
  300. int pinctrl_decode_pin_config(const void *blob, int node);
  301. /**
  302. * pinctrl_get_gpio_mux() - get the mux value for a particular GPIO
  303. *
  304. * This allows the raw mux value for a GPIO to be obtained. It is
  305. * useful for displaying the function being used by that GPIO, such
  306. * as with the 'gpio' command. This function is internal to the GPIO
  307. * subsystem and should not be used by generic code. Typically it is
  308. * used by a GPIO driver with knowledge of the SoC pinctrl setup.
  309. *
  310. * @dev: Pinctrl device to use
  311. * @banknum: GPIO bank number
  312. * @index: GPIO index within the bank
  313. * @return mux value (SoC-specific, e.g. 0 for input, 1 for output)
  314. */
  315. int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index);
  316. #endif /* __PINCTRL_H */