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