xref: /rk3399_rockchip-uboot/include/dm/pinctrl.h (revision 6b9191d0013c06e5b30cc43eb89b40f854b03cc7)
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  * @get_pin_muxing: display the muxing of a given pin.
71  */
72 struct pinctrl_ops {
73 	int (*get_pins_count)(struct udevice *dev);
74 	const char *(*get_pin_name)(struct udevice *dev, unsigned selector);
75 	int (*get_groups_count)(struct udevice *dev);
76 	const char *(*get_group_name)(struct udevice *dev, unsigned selector);
77 	int (*get_functions_count)(struct udevice *dev);
78 	const char *(*get_function_name)(struct udevice *dev,
79 					 unsigned selector);
80 	int (*pinmux_set)(struct udevice *dev, unsigned pin_selector,
81 			  unsigned func_selector);
82 	int (*pinmux_group_set)(struct udevice *dev, unsigned group_selector,
83 				unsigned func_selector);
84 	unsigned int pinconf_num_params;
85 	const struct pinconf_param *pinconf_params;
86 	int (*pinconf_set)(struct udevice *dev, unsigned pin_selector,
87 			   unsigned param, unsigned argument);
88 	int (*pinconf_group_set)(struct udevice *dev, unsigned group_selector,
89 				 unsigned param, unsigned argument);
90 	int (*set_state)(struct udevice *dev, struct udevice *config);
91 
92 	/* for pinctrl-simple */
93 	int (*set_state_simple)(struct udevice *dev, struct udevice *periph);
94 	/**
95 	 * request() - Request a particular pinctrl function
96 	 *
97 	 * This activates the selected function.
98 	 *
99 	 * @dev:	Device to adjust (UCLASS_PINCTRL)
100 	 * @func:	Function number (driver-specific)
101 	 * @return 0 if OK, -ve on error
102 	 */
103 	int (*request)(struct udevice *dev, int func, int flags);
104 
105 	/**
106 	* get_periph_id() - get the peripheral ID for a device
107 	*
108 	* This generally looks at the peripheral's device tree node to work
109 	* out the peripheral ID. The return value is normally interpreted as
110 	* enum periph_id. so long as this is defined by the platform (which it
111 	* should be).
112 	*
113 	* @dev:		Pinctrl device to use for decoding
114 	* @periph:	Device to check
115 	* @return peripheral ID of @periph, or -ENOENT on error
116 	*/
117 	int (*get_periph_id)(struct udevice *dev, struct udevice *periph);
118 
119 	/**
120 	 * get_gpio_mux() - get the mux value for a particular GPIO
121 	 *
122 	 * This allows the raw mux value for a GPIO to be obtained. It is
123 	 * useful for displaying the function being used by that GPIO, such
124 	 * as with the 'gpio' command. This function is internal to the GPIO
125 	 * subsystem and should not be used by generic code. Typically it is
126 	 * used by a GPIO driver with knowledge of the SoC pinctrl setup.
127 	 *
128 	* @dev:		Pinctrl device to use
129 	* @banknum:	GPIO bank number
130 	* @index:	GPIO index within the bank
131 	* @return mux value (SoC-specific, e.g. 0 for input, 1 for output)
132 	 */
133 	int (*get_gpio_mux)(struct udevice *dev, int banknum, int index);
134 
135 	/**
136 	 * get_pin_muxing() - show pin muxing
137 	 *
138 	 * This allows to display the muxing of a given pin. It's useful for
139 	 * debug purpose to know if a pin is configured as GPIO or as an
140 	 * alternate function and which one.
141 	 * Typically it is used by a PINCTRL driver with knowledge of the SoC
142 	 * pinctrl setup.
143 	 *
144 	 * @dev:	Pinctrl device to use
145 	 * @selector:	Pin selector
146 	 * @buf		Pin's muxing description
147 	 * @size	Pin's muxing description length
148 	 * return 0 if OK, -ve on error
149 	 */
150 	 int (*get_pin_muxing)(struct udevice *dev, unsigned int selector,
151 			       char *buf, int size);
152 };
153 
154 #define pinctrl_get_ops(dev)	((struct pinctrl_ops *)(dev)->driver->ops)
155 
156 /**
157  * Generic pin configuration paramters
158  *
159  * @PIN_CONFIG_BIAS_DISABLE: disable any pin bias on the pin, a
160  *	transition from say pull-up to pull-down implies that you disable
161  *	pull-up in the process, this setting disables all biasing.
162  * @PIN_CONFIG_BIAS_HIGH_IMPEDANCE: the pin will be set to a high impedance
163  *	mode, also know as "third-state" (tristate) or "high-Z" or "floating".
164  *	On output pins this effectively disconnects the pin, which is useful
165  *	if for example some other pin is going to drive the signal connected
166  *	to it for a while. Pins used for input are usually always high
167  *	impedance.
168  * @PIN_CONFIG_BIAS_BUS_HOLD: the pin will be set to weakly latch so that it
169  *	weakly drives the last value on a tristate bus, also known as a "bus
170  *	holder", "bus keeper" or "repeater". This allows another device on the
171  *	bus to change the value by driving the bus high or low and switching to
172  *	tristate. The argument is ignored.
173  * @PIN_CONFIG_BIAS_PULL_UP: the pin will be pulled up (usually with high
174  *	impedance to VDD). If the argument is != 0 pull-up is enabled,
175  *	if it is 0, pull-up is total, i.e. the pin is connected to VDD.
176  * @PIN_CONFIG_BIAS_PULL_DOWN: the pin will be pulled down (usually with high
177  *	impedance to GROUND). If the argument is != 0 pull-down is enabled,
178  *	if it is 0, pull-down is total, i.e. the pin is connected to GROUND.
179  * @PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: the pin will be pulled up or down based
180  *	on embedded knowledge of the controller hardware, like current mux
181  *	function. The pull direction and possibly strength too will normally
182  *	be decided completely inside the hardware block and not be readable
183  *	from the kernel side.
184  *	If the argument is != 0 pull up/down is enabled, if it is 0, the
185  *	configuration is ignored. The proper way to disable it is to use
186  *	@PIN_CONFIG_BIAS_DISABLE.
187  * @PIN_CONFIG_DRIVE_PUSH_PULL: the pin will be driven actively high and
188  *	low, this is the most typical case and is typically achieved with two
189  *	active transistors on the output. Setting this config will enable
190  *	push-pull mode, the argument is ignored.
191  * @PIN_CONFIG_DRIVE_OPEN_DRAIN: the pin will be driven with open drain (open
192  *	collector) which means it is usually wired with other output ports
193  *	which are then pulled up with an external resistor. Setting this
194  *	config will enable open drain mode, the argument is ignored.
195  * @PIN_CONFIG_DRIVE_OPEN_SOURCE: the pin will be driven with open source
196  *	(open emitter). Setting this config will enable open source mode, the
197  *	argument is ignored.
198  * @PIN_CONFIG_DRIVE_STRENGTH: the pin will sink or source at most the current
199  *	passed as argument. The argument is in mA.
200  * @PIN_CONFIG_INPUT_ENABLE: enable the pin's input.  Note that this does not
201  *	affect the pin's ability to drive output.  1 enables input, 0 disables
202  *	input.
203  * @PIN_CONFIG_INPUT_SCHMITT_ENABLE: control schmitt-trigger mode on the pin.
204  *      If the argument != 0, schmitt-trigger mode is enabled. If it's 0,
205  *      schmitt-trigger mode is disabled.
206  * @PIN_CONFIG_INPUT_SCHMITT: this will configure an input pin to run in
207  *	schmitt-trigger mode. If the schmitt-trigger has adjustable hysteresis,
208  *	the threshold value is given on a custom format as argument when
209  *	setting pins to this mode.
210  * @PIN_CONFIG_INPUT_DEBOUNCE: this will configure the pin to debounce mode,
211  *	which means it will wait for signals to settle when reading inputs. The
212  *	argument gives the debounce time in usecs. Setting the
213  *	argument to zero turns debouncing off.
214  * @PIN_CONFIG_POWER_SOURCE: if the pin can select between different power
215  *	supplies, the argument to this parameter (on a custom format) tells
216  *	the driver which alternative power source to use.
217  * @PIN_CONFIG_SLEW_RATE: if the pin can select slew rate, the argument to
218  *	this parameter (on a custom format) tells the driver which alternative
219  *	slew rate to use.
220  * @PIN_CONFIG_LOW_POWER_MODE: this will configure the pin for low power
221  *	operation, if several modes of operation are supported these can be
222  *	passed in the argument on a custom form, else just use argument 1
223  *	to indicate low power mode, argument 0 turns low power mode off.
224  * @PIN_CONFIG_OUTPUT: this will configure the pin as an output. Use argument
225  *	1 to indicate high level, argument 0 to indicate low level. (Please
226  *	see Documentation/pinctrl.txt, section "GPIO mode pitfalls" for a
227  *	discussion around this parameter.)
228  * @PIN_CONFIG_END: this is the last enumerator for pin configurations, if
229  *	you need to pass in custom configurations to the pin controller, use
230  *	PIN_CONFIG_END+1 as the base offset.
231  */
232 #define PIN_CONFIG_BIAS_DISABLE			0
233 #define PIN_CONFIG_BIAS_HIGH_IMPEDANCE		1
234 #define PIN_CONFIG_BIAS_BUS_HOLD		2
235 #define PIN_CONFIG_BIAS_PULL_UP			3
236 #define PIN_CONFIG_BIAS_PULL_DOWN		4
237 #define PIN_CONFIG_BIAS_PULL_PIN_DEFAULT	5
238 #define PIN_CONFIG_DRIVE_PUSH_PULL		6
239 #define PIN_CONFIG_DRIVE_OPEN_DRAIN		7
240 #define PIN_CONFIG_DRIVE_OPEN_SOURCE		8
241 #define PIN_CONFIG_DRIVE_STRENGTH		9
242 #define PIN_CONFIG_INPUT_ENABLE			10
243 #define PIN_CONFIG_INPUT_SCHMITT_ENABLE		11
244 #define PIN_CONFIG_INPUT_SCHMITT		12
245 #define PIN_CONFIG_INPUT_DEBOUNCE		13
246 #define PIN_CONFIG_POWER_SOURCE			14
247 #define PIN_CONFIG_SLEW_RATE			15
248 #define PIN_CONFIG_LOW_POWER_MODE		16
249 #define PIN_CONFIG_OUTPUT			17
250 #define PIN_CONFIG_END				0x7FFF
251 
252 #if CONFIG_IS_ENABLED(PINCTRL_GENERIC)
253 /**
254  * pinctrl_generic_set_state() - generic set_state operation
255  * Parse the DT node of @config and its children and handle generic properties
256  * such as "pins", "groups", "functions", and pin configuration parameters.
257  *
258  * @pctldev: pinctrl device
259  * @config: config device (pseudo device), pointing a config node in DTS
260  * @return: 0 on success, or negative error code on failure
261  */
262 int pinctrl_generic_set_state(struct udevice *pctldev, struct udevice *config);
263 #else
264 static inline int pinctrl_generic_set_state(struct udevice *pctldev,
265 					    struct udevice *config)
266 {
267 	return -EINVAL;
268 }
269 #endif
270 
271 #if CONFIG_IS_ENABLED(PINCTRL)
272 /**
273  * pinctrl_select_state() - set a device to a given state
274  *
275  * @dev: peripheral device
276  * @statename: state name, like "default"
277  * @return: 0 on success, or negative error code on failure
278  */
279 int pinctrl_select_state(struct udevice *dev, const char *statename);
280 
281 /**
282  * pinctrl_request() - Request a particular pinctrl function
283  *
284  * @dev:	Device to check (UCLASS_PINCTRL)
285  * @func:	Function number (driver-specific)
286  * @flags:	Flags (driver-specific)
287  * @return 0 if OK, -ve on error
288  */
289 int pinctrl_request(struct udevice *dev, int func, int flags);
290 
291 /**
292  * pinctrl_request_noflags() - Request a particular pinctrl function
293  *
294  * This is similar to pinctrl_request() but uses 0 for @flags.
295  *
296  * @dev:	Device to check (UCLASS_PINCTRL)
297  * @func:	Function number (driver-specific)
298  * @return 0 if OK, -ve on error
299  */
300 int pinctrl_request_noflags(struct udevice *dev, int func);
301 
302 /**
303  * pinctrl_get_periph_id() - get the peripheral ID for a device
304  *
305  * This generally looks at the peripheral's device tree node to work out the
306  * peripheral ID. The return value is normally interpreted as enum periph_id.
307  * so long as this is defined by the platform (which it should be).
308  *
309  * @dev:	Pinctrl device to use for decoding
310  * @periph:	Device to check
311  * @return peripheral ID of @periph, or -ENOENT on error
312  */
313 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph);
314 
315 /**
316  * pinctrl_decode_pin_config() - decode pin configuration flags
317  *
318  * This decodes some of the PIN_CONFIG values into flags, with each value
319  * being (1 << pin_cfg). This does not support things with values like the
320  * slew rate.
321  *
322  * @blob:	Device tree blob
323  * @node:	Node containing the PIN_CONFIG values
324  * @return decoded flag value, or -ve on error
325  */
326 int pinctrl_decode_pin_config(const void *blob, int node);
327 
328 /**
329  * pinctrl_get_gpio_mux() - get the mux value for a particular GPIO
330  *
331  * This allows the raw mux value for a GPIO to be obtained. It is
332  * useful for displaying the function being used by that GPIO, such
333  * as with the 'gpio' command. This function is internal to the GPIO
334  * subsystem and should not be used by generic code. Typically it is
335  * used by a GPIO driver with knowledge of the SoC pinctrl setup.
336  *
337  * @dev:	Pinctrl device to use
338  * @banknum:	GPIO bank number
339  * @index:	GPIO index within the bank
340  * @return mux value (SoC-specific, e.g. 0 for input, 1 for output)
341 */
342 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index);
343 
344 /**
345  * pinctrl_get_pins_count() - get the total pins count for all GPIOs
346  *
347  * This allows the total pins count for all GPIO to be obtained.
348  *
349  * @dev:	Pinctrl device to use
350  * @return pins count
351 */
352 int pinctrl_get_pins_count(struct udevice *dev);
353 
354 /**
355  * pinctrl_get_pin_name() - Returns the pin's name
356  *
357  * This allows to display the pin's name for debug purpose
358  *
359  * @dev:	Pinctrl device to use
360  * @selector	Pin index within pin-controller
361  * @buf		Pin's name
362  * @return 0 if OK, -ve on error
363  */
364 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
365 			 int size);
366 
367 /**
368  * pinctrl_get_pin_muxing() - Returns the muxing description
369  *
370  * This allows to display the muxing description of the given pin for
371  * debug purpose
372  *
373  * @dev:	Pinctrl device to use
374  * @selector	Pin index within pin-controller
375  * @buf		Pin's muxing description
376  * @size	Pin's muxing description length
377  * @return 0 if OK, -ve on error
378  */
379 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
380 			   int size);
381 
382 #else
383 static inline int pinctrl_select_state(struct udevice *dev,
384 				       const char *statename)
385 {
386 	return -EINVAL;
387 }
388 
389 static inline int pinctrl_request(struct udevice *dev, int func, int flags)
390 {
391 	return -EINVAL;
392 }
393 
394 static inline int pinctrl_request_noflags(struct udevice *dev, int func)
395 {
396 	return -EINVAL;
397 }
398 
399 static inline int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
400 {
401 	return -EINVAL;
402 }
403 
404 static inline int pinctrl_decode_pin_config(const void *blob, int node)
405 {
406 	return -EINVAL;
407 }
408 
409 static inline int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
410 {
411 	return -EINVAL;
412 }
413 
414 static inline int pinctrl_get_pins_count(struct udevice *dev)
415 {
416 	return -EINVAL;
417 }
418 
419 static inline int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
420 			 int size)
421 {
422 	return -EINVAL;
423 }
424 
425 static inline int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
426 			   int size)
427 {
428 	return -EINVAL;
429 }
430 
431 #endif
432 
433 #endif /* __PINCTRL_H */
434