xref: /openbmc/u-boot/include/dm/pinctrl.h (revision 23ff8633)
1 /*
2  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #ifndef __PINCTRL_H
8 #define __PINCTRL_H
9 
10 /**
11  * struct pinconf_param - pin config parameters
12  *
13  * @property: property name in DT nodes
14  * @param: ID for this config parameter
15  * @default_value: default value for this config parameter used in case
16  *	no value is specified in DT nodes
17  */
18 struct pinconf_param {
19 	const char * const property;
20 	unsigned int param;
21 	u32 default_value;
22 };
23 
24 /**
25  * struct pinctrl_ops - pin control operations, to be implemented by
26  * pin controller drivers.
27  *
28  * The @set_state is the only mandatory operation.  You can implement your
29  * pinctrl driver with its own @set_state.  In this case, the other callbacks
30  * are not required.  Otherwise, generic pinctrl framework is also available;
31  * use pinctrl_generic_set_state for @set_state, and implement other operations
32  * depending on your necessity.
33  *
34  * @get_pins_count: return number of selectable named pins available
35  *	in this driver.  (necessary to parse "pins" property in DTS)
36  * @get_pin_name: return the pin name of the pin selector,
37  *	called by the core to figure out which pin it shall do
38  *	operations to.  (necessary to parse "pins" property in DTS)
39  * @get_groups_count: return number of selectable named groups available
40  *	in this driver.  (necessary to parse "groups" property in DTS)
41  * @get_group_name: return the group name of the group selector,
42  *	called by the core to figure out which pin group it shall do
43  *	operations to.  (necessary to parse "groups" property in DTS)
44  * @get_functions_count: return number of selectable named functions available
45  *	in this driver.  (necessary for pin-muxing)
46  * @get_function_name: return the function name of the muxing selector,
47  *	called by the core to figure out which mux setting it shall map a
48  *	certain device to.  (necessary for pin-muxing)
49  * @pinmux_set: enable a certain muxing function with a certain pin.
50  *	The @func_selector selects a certain function whereas @pin_selector
51  *	selects a certain pin to be used. On simple controllers one of them
52  *	may be ignored.  (necessary for pin-muxing against a single pin)
53  * @pinmux_group_set: enable a certain muxing function with a certain pin
54  *	group.  The @func_selector selects a certain function whereas
55  *	@group_selector selects a certain set of pins to be used. On simple
56  *	controllers one of them may be ignored.
57  *	(necessary for pin-muxing against a pin group)
58  * @pinconf_num_params: number of driver-specific parameters to be parsed
59  *	from device trees  (necessary for pin-configuration)
60  * @pinconf_params: list of driver_specific parameters to be parsed from
61  *	device trees  (necessary for pin-configuration)
62  * @pinconf_set: configure an individual pin with a given parameter.
63  *	(necessary for pin-configuration against a single pin)
64  * @pinconf_group_set: configure all pins in a group with a given parameter.
65  *	(necessary for pin-configuration against a pin group)
66  * @set_state: do pinctrl operations specified by @config, a pseudo device
67  *	pointing a config node. (necessary for pinctrl_full)
68  * @set_state_simple: do needed pinctrl operations for a peripherl @periph.
69  *	(necessary for pinctrl_simple)
70  */
71 struct pinctrl_ops {
72 	int (*get_pins_count)(struct udevice *dev);
73 	const char *(*get_pin_name)(struct udevice *dev, unsigned selector);
74 	int (*get_groups_count)(struct udevice *dev);
75 	const char *(*get_group_name)(struct udevice *dev, unsigned selector);
76 	int (*get_functions_count)(struct udevice *dev);
77 	const char *(*get_function_name)(struct udevice *dev,
78 					 unsigned selector);
79 	int (*pinmux_set)(struct udevice *dev, unsigned pin_selector,
80 			  unsigned func_selector);
81 	int (*pinmux_group_set)(struct udevice *dev, unsigned group_selector,
82 				unsigned func_selector);
83 	unsigned int pinconf_num_params;
84 	const struct pinconf_param *pinconf_params;
85 	int (*pinconf_set)(struct udevice *dev, unsigned pin_selector,
86 			   unsigned param, unsigned argument);
87 	int (*pinconf_group_set)(struct udevice *dev, unsigned group_selector,
88 				 unsigned param, unsigned argument);
89 	int (*set_state)(struct udevice *dev, struct udevice *config);
90 
91 	/* for pinctrl-simple */
92 	int (*set_state_simple)(struct udevice *dev, struct udevice *periph);
93 	/**
94 	 * request() - Request a particular pinctrl function
95 	 *
96 	 * This activates the selected function.
97 	 *
98 	 * @dev:	Device to adjust (UCLASS_PINCTRL)
99 	 * @func:	Function number (driver-specific)
100 	 * @return 0 if OK, -ve on error
101 	 */
102 	int (*request)(struct udevice *dev, int func, int flags);
103 
104 	/**
105 	* get_periph_id() - get the peripheral ID for a device
106 	*
107 	* This generally looks at the peripheral's device tree node to work
108 	* out the peripheral ID. The return value is normally interpreted as
109 	* enum periph_id. so long as this is defined by the platform (which it
110 	* should be).
111 	*
112 	* @dev:		Pinctrl device to use for decoding
113 	* @periph:	Device to check
114 	* @return peripheral ID of @periph, or -ENOENT on error
115 	*/
116 	int (*get_periph_id)(struct udevice *dev, struct udevice *periph);
117 };
118 
119 #define pinctrl_get_ops(dev)	((struct pinctrl_ops *)(dev)->driver->ops)
120 
121 /**
122  * Generic pin configuration paramters
123  *
124  * @PIN_CONFIG_BIAS_DISABLE: disable any pin bias on the pin, a
125  *	transition from say pull-up to pull-down implies that you disable
126  *	pull-up in the process, this setting disables all biasing.
127  * @PIN_CONFIG_BIAS_HIGH_IMPEDANCE: the pin will be set to a high impedance
128  *	mode, also know as "third-state" (tristate) or "high-Z" or "floating".
129  *	On output pins this effectively disconnects the pin, which is useful
130  *	if for example some other pin is going to drive the signal connected
131  *	to it for a while. Pins used for input are usually always high
132  *	impedance.
133  * @PIN_CONFIG_BIAS_BUS_HOLD: the pin will be set to weakly latch so that it
134  *	weakly drives the last value on a tristate bus, also known as a "bus
135  *	holder", "bus keeper" or "repeater". This allows another device on the
136  *	bus to change the value by driving the bus high or low and switching to
137  *	tristate. The argument is ignored.
138  * @PIN_CONFIG_BIAS_PULL_UP: the pin will be pulled up (usually with high
139  *	impedance to VDD). If the argument is != 0 pull-up is enabled,
140  *	if it is 0, pull-up is total, i.e. the pin is connected to VDD.
141  * @PIN_CONFIG_BIAS_PULL_DOWN: the pin will be pulled down (usually with high
142  *	impedance to GROUND). If the argument is != 0 pull-down is enabled,
143  *	if it is 0, pull-down is total, i.e. the pin is connected to GROUND.
144  * @PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: the pin will be pulled up or down based
145  *	on embedded knowledge of the controller hardware, like current mux
146  *	function. The pull direction and possibly strength too will normally
147  *	be decided completely inside the hardware block and not be readable
148  *	from the kernel side.
149  *	If the argument is != 0 pull up/down is enabled, if it is 0, the
150  *	configuration is ignored. The proper way to disable it is to use
151  *	@PIN_CONFIG_BIAS_DISABLE.
152  * @PIN_CONFIG_DRIVE_PUSH_PULL: the pin will be driven actively high and
153  *	low, this is the most typical case and is typically achieved with two
154  *	active transistors on the output. Setting this config will enable
155  *	push-pull mode, the argument is ignored.
156  * @PIN_CONFIG_DRIVE_OPEN_DRAIN: the pin will be driven with open drain (open
157  *	collector) which means it is usually wired with other output ports
158  *	which are then pulled up with an external resistor. Setting this
159  *	config will enable open drain mode, the argument is ignored.
160  * @PIN_CONFIG_DRIVE_OPEN_SOURCE: the pin will be driven with open source
161  *	(open emitter). Setting this config will enable open source mode, the
162  *	argument is ignored.
163  * @PIN_CONFIG_DRIVE_STRENGTH: the pin will sink or source at most the current
164  *	passed as argument. The argument is in mA.
165  * @PIN_CONFIG_INPUT_ENABLE: enable the pin's input.  Note that this does not
166  *	affect the pin's ability to drive output.  1 enables input, 0 disables
167  *	input.
168  * @PIN_CONFIG_INPUT_SCHMITT_ENABLE: control schmitt-trigger mode on the pin.
169  *      If the argument != 0, schmitt-trigger mode is enabled. If it's 0,
170  *      schmitt-trigger mode is disabled.
171  * @PIN_CONFIG_INPUT_SCHMITT: this will configure an input pin to run in
172  *	schmitt-trigger mode. If the schmitt-trigger has adjustable hysteresis,
173  *	the threshold value is given on a custom format as argument when
174  *	setting pins to this mode.
175  * @PIN_CONFIG_INPUT_DEBOUNCE: this will configure the pin to debounce mode,
176  *	which means it will wait for signals to settle when reading inputs. The
177  *	argument gives the debounce time in usecs. Setting the
178  *	argument to zero turns debouncing off.
179  * @PIN_CONFIG_POWER_SOURCE: if the pin can select between different power
180  *	supplies, the argument to this parameter (on a custom format) tells
181  *	the driver which alternative power source to use.
182  * @PIN_CONFIG_SLEW_RATE: if the pin can select slew rate, the argument to
183  *	this parameter (on a custom format) tells the driver which alternative
184  *	slew rate to use.
185  * @PIN_CONFIG_LOW_POWER_MODE: this will configure the pin for low power
186  *	operation, if several modes of operation are supported these can be
187  *	passed in the argument on a custom form, else just use argument 1
188  *	to indicate low power mode, argument 0 turns low power mode off.
189  * @PIN_CONFIG_OUTPUT: this will configure the pin as an output. Use argument
190  *	1 to indicate high level, argument 0 to indicate low level. (Please
191  *	see Documentation/pinctrl.txt, section "GPIO mode pitfalls" for a
192  *	discussion around this parameter.)
193  * @PIN_CONFIG_END: this is the last enumerator for pin configurations, if
194  *	you need to pass in custom configurations to the pin controller, use
195  *	PIN_CONFIG_END+1 as the base offset.
196  */
197 #define PIN_CONFIG_BIAS_DISABLE			0
198 #define PIN_CONFIG_BIAS_HIGH_IMPEDANCE		1
199 #define PIN_CONFIG_BIAS_BUS_HOLD		2
200 #define PIN_CONFIG_BIAS_PULL_UP			3
201 #define PIN_CONFIG_BIAS_PULL_DOWN		4
202 #define PIN_CONFIG_BIAS_PULL_PIN_DEFAULT	5
203 #define PIN_CONFIG_DRIVE_PUSH_PULL		6
204 #define PIN_CONFIG_DRIVE_OPEN_DRAIN		7
205 #define PIN_CONFIG_DRIVE_OPEN_SOURCE		8
206 #define PIN_CONFIG_DRIVE_STRENGTH		9
207 #define PIN_CONFIG_INPUT_ENABLE			10
208 #define PIN_CONFIG_INPUT_SCHMITT_ENABLE		11
209 #define PIN_CONFIG_INPUT_SCHMITT		12
210 #define PIN_CONFIG_INPUT_DEBOUNCE		13
211 #define PIN_CONFIG_POWER_SOURCE			14
212 #define PIN_CONFIG_SLEW_RATE			15
213 #define PIN_CONFIG_LOW_POWER_MODE		16
214 #define PIN_CONFIG_OUTPUT			17
215 #define PIN_CONFIG_END				0x7FFF
216 
217 #if CONFIG_IS_ENABLED(PINCTRL_GENERIC)
218 /**
219  * pinctrl_generic_set_state() - generic set_state operation
220  * Parse the DT node of @config and its children and handle generic properties
221  * such as "pins", "groups", "functions", and pin configuration parameters.
222  *
223  * @pctldev: pinctrl device
224  * @config: config device (pseudo device), pointing a config node in DTS
225  * @return: 0 on success, or negative error code on failure
226  */
227 int pinctrl_generic_set_state(struct udevice *pctldev, struct udevice *config);
228 #else
229 static inline int pinctrl_generic_set_state(struct udevice *pctldev,
230 					    struct udevice *config)
231 {
232 	return -EINVAL;
233 }
234 #endif
235 
236 #if CONFIG_IS_ENABLED(PINCTRL)
237 /**
238  * pinctrl_select_state() - set a device to a given state
239  *
240  * @dev: peripheral device
241  * @statename: state name, like "default"
242  * @return: 0 on success, or negative error code on failure
243  */
244 int pinctrl_select_state(struct udevice *dev, const char *statename);
245 #else
246 static inline int pinctrl_select_state(struct udevice *dev,
247 				       const char *statename)
248 {
249 	return -EINVAL;
250 }
251 #endif
252 
253 /**
254  * pinctrl_request() - Request a particular pinctrl function
255  *
256  * @dev:	Device to check (UCLASS_PINCTRL)
257  * @func:	Function number (driver-specific)
258  * @flags:	Flags (driver-specific)
259  * @return 0 if OK, -ve on error
260  */
261 int pinctrl_request(struct udevice *dev, int func, int flags);
262 
263 /**
264  * pinctrl_request_noflags() - Request a particular pinctrl function
265  *
266  * This is similar to pinctrl_request() but uses 0 for @flags.
267  *
268  * @dev:	Device to check (UCLASS_PINCTRL)
269  * @func:	Function number (driver-specific)
270  * @return 0 if OK, -ve on error
271  */
272 int pinctrl_request_noflags(struct udevice *dev, int func);
273 
274 /**
275  * pinctrl_get_periph_id() - get the peripheral ID for a device
276  *
277  * This generally looks at the peripheral's device tree node to work out the
278  * peripheral ID. The return value is normally interpreted as enum periph_id.
279  * so long as this is defined by the platform (which it should be).
280  *
281  * @dev:	Pinctrl device to use for decoding
282  * @periph:	Device to check
283  * @return peripheral ID of @periph, or -ENOENT on error
284  */
285 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph);
286 
287 #endif /* __PINCTRL_H */
288