core.h 8.3 KB

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