core.h 8.3 KB

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