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