core.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Core private header for the pin control subsystem
  4. *
  5. * Copyright (C) 2011 ST-Ericsson SA
  6. * Written on behalf of Linaro for ST-Ericsson
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. */
  10. #include <linux/kref.h>
  11. #include <linux/mutex.h>
  12. #include <linux/radix-tree.h>
  13. #include <linux/pinctrl/pinconf.h>
  14. #include <linux/pinctrl/machine.h>
  15. struct pinctrl_gpio_range;
  16. /**
  17. * struct pinctrl_dev - pin control class device
  18. * @node: node to include this pin controller in the global pin controller list
  19. * @desc: the pin controller descriptor supplied when initializing this pin
  20. * controller
  21. * @pin_desc_tree: each pin descriptor for this pin controller is stored in
  22. * this radix tree
  23. * @pin_group_tree: optionally each pin group can be stored in this radix tree
  24. * @num_groups: optionally number of groups can be kept here
  25. * @pin_function_tree: optionally each function can be stored in this radix tree
  26. * @num_functions: optionally number of functions can be kept here
  27. * @gpio_ranges: a list of GPIO ranges that is handled by this pin controller,
  28. * ranges are added to this list at runtime
  29. * @dev: the device entry for this pin controller
  30. * @owner: module providing the pin controller, used for refcounting
  31. * @driver_data: driver data for drivers registering to the pin controller
  32. * subsystem
  33. * @p: result of pinctrl_get() for this device
  34. * @hog_default: default state for pins hogged by this device
  35. * @hog_sleep: sleep state for pins hogged by this device
  36. * @mutex: mutex taken on each pin controller specific action
  37. * @device_root: debugfs root for this device
  38. */
  39. struct pinctrl_dev {
  40. struct list_head node;
  41. struct pinctrl_desc *desc;
  42. struct radix_tree_root pin_desc_tree;
  43. #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
  44. struct radix_tree_root pin_group_tree;
  45. unsigned int num_groups;
  46. #endif
  47. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  48. struct radix_tree_root pin_function_tree;
  49. unsigned int num_functions;
  50. #endif
  51. struct list_head gpio_ranges;
  52. struct device *dev;
  53. struct module *owner;
  54. void *driver_data;
  55. struct pinctrl *p;
  56. struct pinctrl_state *hog_default;
  57. struct pinctrl_state *hog_sleep;
  58. struct mutex mutex;
  59. #ifdef CONFIG_DEBUG_FS
  60. struct dentry *device_root;
  61. #endif
  62. };
  63. /**
  64. * struct pinctrl - per-device pin control state holder
  65. * @node: global list node
  66. * @dev: the device using this pin control handle
  67. * @states: a list of states for this device
  68. * @state: the current state
  69. * @dt_maps: the mapping table chunks dynamically parsed from device tree for
  70. * this device, if any
  71. * @users: reference count
  72. */
  73. struct pinctrl {
  74. struct list_head node;
  75. struct device *dev;
  76. struct list_head states;
  77. struct pinctrl_state *state;
  78. struct list_head dt_maps;
  79. struct kref users;
  80. };
  81. /**
  82. * struct pinctrl_state - a pinctrl state for a device
  83. * @node: list node for struct pinctrl's @states field
  84. * @name: the name of this state
  85. * @settings: a list of settings for this state
  86. */
  87. struct pinctrl_state {
  88. struct list_head node;
  89. const char *name;
  90. struct list_head settings;
  91. };
  92. /**
  93. * struct pinctrl_setting_mux - setting data for MAP_TYPE_MUX_GROUP
  94. * @group: the group selector to program
  95. * @func: the function selector to program
  96. */
  97. struct pinctrl_setting_mux {
  98. unsigned group;
  99. unsigned func;
  100. };
  101. /**
  102. * struct pinctrl_setting_configs - setting data for MAP_TYPE_CONFIGS_*
  103. * @group_or_pin: the group selector or pin ID to program
  104. * @configs: a pointer to an array of config parameters/values to program into
  105. * hardware. Each individual pin controller defines the format and meaning
  106. * of config parameters.
  107. * @num_configs: the number of entries in array @configs
  108. */
  109. struct pinctrl_setting_configs {
  110. unsigned group_or_pin;
  111. unsigned long *configs;
  112. unsigned num_configs;
  113. };
  114. /**
  115. * struct pinctrl_setting - an individual mux or config setting
  116. * @node: list node for struct pinctrl_settings's @settings field
  117. * @type: the type of setting
  118. * @pctldev: pin control device handling to be programmed. Not used for
  119. * PIN_MAP_TYPE_DUMMY_STATE.
  120. * @dev_name: the name of the device using this state
  121. * @data: Data specific to the setting type
  122. */
  123. struct pinctrl_setting {
  124. struct list_head node;
  125. enum pinctrl_map_type type;
  126. struct pinctrl_dev *pctldev;
  127. const char *dev_name;
  128. union {
  129. struct pinctrl_setting_mux mux;
  130. struct pinctrl_setting_configs configs;
  131. } data;
  132. };
  133. /**
  134. * struct pin_desc - pin descriptor for each physical pin in the arch
  135. * @pctldev: corresponding pin control device
  136. * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
  137. * datasheet or such
  138. * @dynamic_name: if the name of this pin was dynamically allocated
  139. * @drv_data: driver-defined per-pin data. pinctrl core does not touch this
  140. * @mux_usecount: If zero, the pin is not claimed, and @owner should be NULL.
  141. * If non-zero, this pin is claimed by @owner. This field is an integer
  142. * rather than a boolean, since pinctrl_get() might process multiple
  143. * mapping table entries that refer to, and hence claim, the same group
  144. * or pin, and each of these will increment the @usecount.
  145. * @mux_owner: The name of device that called pinctrl_get().
  146. * @mux_setting: The most recent selected mux setting for this pin, if any.
  147. * @gpio_owner: If pinctrl_gpio_request() was called for this pin, this is
  148. * the name of the GPIO that "owns" this pin.
  149. */
  150. struct pin_desc {
  151. struct pinctrl_dev *pctldev;
  152. const char *name;
  153. bool dynamic_name;
  154. void *drv_data;
  155. /* These fields only added when supporting pinmux drivers */
  156. #ifdef CONFIG_PINMUX
  157. unsigned mux_usecount;
  158. const char *mux_owner;
  159. const struct pinctrl_setting_mux *mux_setting;
  160. const char *gpio_owner;
  161. #endif
  162. };
  163. /**
  164. * struct pinctrl_maps - a list item containing part of the mapping table
  165. * @node: mapping table list node
  166. * @maps: array of mapping table entries
  167. * @num_maps: the number of entries in @maps
  168. */
  169. struct pinctrl_maps {
  170. struct list_head node;
  171. const struct pinctrl_map *maps;
  172. unsigned num_maps;
  173. };
  174. #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
  175. /**
  176. * struct group_desc - generic pin group descriptor
  177. * @name: name of the pin group
  178. * @pins: array of pins that belong to the group
  179. * @num_pins: number of pins in the group
  180. * @data: pin controller driver specific data
  181. */
  182. struct group_desc {
  183. const char *name;
  184. int *pins;
  185. int num_pins;
  186. void *data;
  187. };
  188. int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev);
  189. const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
  190. unsigned int group_selector);
  191. int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
  192. unsigned int group_selector,
  193. const unsigned int **pins,
  194. unsigned int *npins);
  195. struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
  196. unsigned int group_selector);
  197. int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
  198. int *gpins, int ngpins, void *data);
  199. int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
  200. unsigned int group_selector);
  201. #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
  202. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
  203. struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
  204. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
  205. const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin);
  206. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  207. const char *pin_group);
  208. static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
  209. unsigned int pin)
  210. {
  211. return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
  212. }
  213. extern struct pinctrl_gpio_range *
  214. pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
  215. unsigned int pin);
  216. extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev);
  217. extern int pinctrl_force_default(struct pinctrl_dev *pctldev);
  218. extern struct mutex pinctrl_maps_mutex;
  219. extern struct list_head pinctrl_maps;
  220. #define for_each_maps(_maps_node_, _i_, _map_) \
  221. list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
  222. for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
  223. _i_ < _maps_node_->num_maps; \
  224. _i_++, _map_ = &_maps_node_->maps[_i_])