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  * @mutex: mutex taken on each pin controller specific action
41  * @device_root: debugfs root for this device
42  */
43 struct pinctrl_dev {
44 	struct list_head node;
45 	struct pinctrl_desc *desc;
46 	struct radix_tree_root pin_desc_tree;
47 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
48 	struct radix_tree_root pin_group_tree;
49 	unsigned int num_groups;
50 #endif
51 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
52 	struct radix_tree_root pin_function_tree;
53 	unsigned int num_functions;
54 #endif
55 	struct list_head gpio_ranges;
56 	struct device *dev;
57 	struct module *owner;
58 	void *driver_data;
59 	struct pinctrl *p;
60 	struct pinctrl_state *hog_default;
61 	struct pinctrl_state *hog_sleep;
62 	struct mutex mutex;
63 #ifdef CONFIG_DEBUG_FS
64 	struct dentry *device_root;
65 #endif
66 };
67 
68 /**
69  * struct pinctrl - per-device pin control state holder
70  * @node: global list node
71  * @dev: the device using this pin control handle
72  * @states: a list of states for this device
73  * @state: the current state
74  * @dt_maps: the mapping table chunks dynamically parsed from device tree for
75  *	this device, if any
76  * @users: reference count
77  */
78 struct pinctrl {
79 	struct list_head node;
80 	struct device *dev;
81 	struct list_head states;
82 	struct pinctrl_state *state;
83 	struct list_head dt_maps;
84 	struct kref users;
85 };
86 
87 /**
88  * struct pinctrl_state - a pinctrl state for a device
89  * @node: list node for struct pinctrl's @states field
90  * @name: the name of this state
91  * @settings: a list of settings for this state
92  */
93 struct pinctrl_state {
94 	struct list_head node;
95 	const char *name;
96 	struct list_head settings;
97 };
98 
99 /**
100  * struct pinctrl_setting_mux - setting data for MAP_TYPE_MUX_GROUP
101  * @group: the group selector to program
102  * @func: the function selector to program
103  */
104 struct pinctrl_setting_mux {
105 	unsigned group;
106 	unsigned func;
107 };
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 group_or_pin;
119 	unsigned long *configs;
120 	unsigned num_configs;
121 };
122 
123 /**
124  * struct pinctrl_setting - an individual mux or config setting
125  * @node: list node for struct pinctrl_settings's @settings field
126  * @type: the type of setting
127  * @pctldev: pin control device handling to be programmed. Not used for
128  *   PIN_MAP_TYPE_DUMMY_STATE.
129  * @dev_name: the name of the device using this state
130  * @data: Data specific to the setting type
131  */
132 struct pinctrl_setting {
133 	struct list_head node;
134 	enum pinctrl_map_type type;
135 	struct pinctrl_dev *pctldev;
136 	const char *dev_name;
137 	union {
138 		struct pinctrl_setting_mux mux;
139 		struct pinctrl_setting_configs configs;
140 	} data;
141 };
142 
143 /**
144  * struct pin_desc - pin descriptor for each physical pin in the arch
145  * @pctldev: corresponding pin control device
146  * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
147  *	datasheet or such
148  * @dynamic_name: if the name of this pin was dynamically allocated
149  * @drv_data: driver-defined per-pin data. pinctrl core does not touch this
150  * @mux_usecount: If zero, the pin is not claimed, and @owner should be NULL.
151  *	If non-zero, this pin is claimed by @owner. This field is an integer
152  *	rather than a boolean, since pinctrl_get() might process multiple
153  *	mapping table entries that refer to, and hence claim, the same group
154  *	or pin, and each of these will increment the @usecount.
155  * @mux_owner: The name of device that called pinctrl_get().
156  * @mux_setting: The most recent selected mux setting for this pin, if any.
157  * @gpio_owner: If pinctrl_gpio_request() was called for this pin, this is
158  *	the name of the GPIO that "owns" this pin.
159  */
160 struct pin_desc {
161 	struct pinctrl_dev *pctldev;
162 	const char *name;
163 	bool dynamic_name;
164 	void *drv_data;
165 	/* These fields only added when supporting pinmux drivers */
166 #ifdef CONFIG_PINMUX
167 	unsigned mux_usecount;
168 	const char *mux_owner;
169 	const struct pinctrl_setting_mux *mux_setting;
170 	const char *gpio_owner;
171 #endif
172 };
173 
174 /**
175  * struct pinctrl_maps - a list item containing part of the mapping table
176  * @node: mapping table list node
177  * @maps: array of mapping table entries
178  * @num_maps: the number of entries in @maps
179  */
180 struct pinctrl_maps {
181 	struct list_head node;
182 	const struct pinctrl_map *maps;
183 	unsigned num_maps;
184 };
185 
186 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
187 
188 /**
189  * struct group_desc - generic pin group descriptor
190  * @name: name of the pin group
191  * @pins: array of pins that belong to the group
192  * @num_pins: number of pins in the group
193  * @data: pin controller driver specific data
194  */
195 struct group_desc {
196 	const char *name;
197 	int *pins;
198 	int num_pins;
199 	void *data;
200 };
201 
202 int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev);
203 
204 const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
205 					   unsigned int group_selector);
206 
207 int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
208 				   unsigned int group_selector,
209 				   const unsigned int **pins,
210 				   unsigned int *npins);
211 
212 struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
213 					     unsigned int group_selector);
214 
215 int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
216 			      int *gpins, int ngpins, void *data);
217 
218 int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
219 				 unsigned int group_selector);
220 
221 #endif	/* CONFIG_GENERIC_PINCTRL_GROUPS */
222 
223 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
224 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
225 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
226 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin);
227 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
228 			       const char *pin_group);
229 
pin_desc_get(struct pinctrl_dev * pctldev,unsigned int pin)230 static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
231 					    unsigned int pin)
232 {
233 	return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
234 }
235 
236 extern struct pinctrl_gpio_range *
237 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
238 					unsigned int pin);
239 
240 int pinctrl_register_map(const struct pinctrl_map *maps, unsigned num_maps,
241 			 bool dup);
242 void pinctrl_unregister_map(const struct pinctrl_map *map);
243 
244 extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev);
245 extern int pinctrl_force_default(struct pinctrl_dev *pctldev);
246 
247 extern struct mutex pinctrl_maps_mutex;
248 extern struct list_head pinctrl_maps;
249 
250 #define for_each_maps(_maps_node_, _i_, _map_) \
251 	list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
252 		for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
253 			_i_ < _maps_node_->num_maps; \
254 			_i_++, _map_ = &_maps_node_->maps[_i_])
255