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