1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5 
6 #ifndef __PINCTRL_H
7 #define __PINCTRL_H
8 
9 /**
10  * struct pinconf_param - pin config parameters
11  *
12  * @property: property name in DT nodes
13  * @param: ID for this config parameter
14  * @default_value: default value for this config parameter used in case
15  *	no value is specified in DT nodes
16  */
17 struct pinconf_param {
18 	const char * const property;
19 	unsigned int param;
20 	u32 default_value;
21 };
22 
23 /**
24  * struct pinctrl_ops - pin control operations, to be implemented by
25  * pin controller drivers.
26  *
27  * The @set_state is the only mandatory operation.  You can implement your
28  * pinctrl driver with its own @set_state.  In this case, the other callbacks
29  * are not required.  Otherwise, generic pinctrl framework is also available;
30  * use pinctrl_generic_set_state for @set_state, and implement other operations
31  * depending on your necessity.
32  *
33  * @get_pins_count: return number of selectable named pins available
34  *	in this driver.  (necessary to parse "pins" property in DTS)
35  * @get_pin_name: return the pin name of the pin selector,
36  *	called by the core to figure out which pin it shall do
37  *	operations to.  (necessary to parse "pins" property in DTS)
38  * @get_groups_count: return number of selectable named groups available
39  *	in this driver.  (necessary to parse "groups" property in DTS)
40  * @get_group_name: return the group name of the group selector,
41  *	called by the core to figure out which pin group it shall do
42  *	operations to.  (necessary to parse "groups" property in DTS)
43  * @get_functions_count: return number of selectable named functions available
44  *	in this driver.  (necessary for pin-muxing)
45  * @get_function_name: return the function name of the muxing selector,
46  *	called by the core to figure out which mux setting it shall map a
47  *	certain device to.  (necessary for pin-muxing)
48  * @pinmux_set: enable a certain muxing function with a certain pin.
49  *	The @func_selector selects a certain function whereas @pin_selector
50  *	selects a certain pin to be used. On simple controllers one of them
51  *	may be ignored.  (necessary for pin-muxing against a single pin)
52  * @pinmux_group_set: enable a certain muxing function with a certain pin
53  *	group.  The @func_selector selects a certain function whereas
54  *	@group_selector selects a certain set of pins to be used. On simple
55  *	controllers one of them may be ignored.
56  *	(necessary for pin-muxing against a pin group)
57  * @pinconf_num_params: number of driver-specific parameters to be parsed
58  *	from device trees  (necessary for pin-configuration)
59  * @pinconf_params: list of driver_specific parameters to be parsed from
60  *	device trees  (necessary for pin-configuration)
61  * @pinconf_set: configure an individual pin with a given parameter.
62  *	(necessary for pin-configuration against a single pin)
63  * @pinconf_group_set: configure all pins in a group with a given parameter.
64  *	(necessary for pin-configuration against a pin group)
65  * @set_state: do pinctrl operations specified by @config, a pseudo device
66  *	pointing a config node. (necessary for pinctrl_full)
67  * @set_state_simple: do needed pinctrl operations for a peripherl @periph.
68  *	(necessary for pinctrl_simple)
69  */
70 struct pinctrl_ops {
71 	int (*get_pins_count)(struct udevice *dev);
72 	const char *(*get_pin_name)(struct udevice *dev, unsigned selector);
73 	int (*get_groups_count)(struct udevice *dev);
74 	const char *(*get_group_name)(struct udevice *dev, unsigned selector);
75 	int (*get_functions_count)(struct udevice *dev);
76 	const char *(*get_function_name)(struct udevice *dev,
77 					 unsigned selector);
78 	int (*pinmux_set)(struct udevice *dev, unsigned pin_selector,
79 			  unsigned func_selector);
80 	int (*pinmux_group_set)(struct udevice *dev, unsigned group_selector,
81 				unsigned func_selector);
82 	unsigned int pinconf_num_params;
83 	const struct pinconf_param *pinconf_params;
84 	int (*pinconf_set)(struct udevice *dev, unsigned pin_selector,
85 			   unsigned param, unsigned argument);
86 	int (*pinconf_group_set)(struct udevice *dev, unsigned group_selector,
87 				 unsigned param, unsigned argument);
88 	int (*set_state)(struct udevice *dev, struct udevice *config);
89 
90 	/* for pinctrl-simple */
91 	int (*set_state_simple)(struct udevice *dev, struct udevice *periph);
92 	/**
93 	 * request() - Request a particular pinctrl function
94 	 *
95 	 * This activates the selected function.
96 	 *
97 	 * @dev:	Device to adjust (UCLASS_PINCTRL)
98 	 * @func:	Function number (driver-specific)
99 	 * @return 0 if OK, -ve on error
100 	 */
101 	int (*request)(struct udevice *dev, int func, int flags);
102 
103 	/**
104 	* get_periph_id() - get the peripheral ID for a device
105 	*
106 	* This generally looks at the peripheral's device tree node to work
107 	* out the peripheral ID. The return value is normally interpreted as
108 	* enum periph_id. so long as this is defined by the platform (which it
109 	* should be).
110 	*
111 	* @dev:		Pinctrl device to use for decoding
112 	* @periph:	Device to check
113 	* @return peripheral ID of @periph, or -ENOENT on error
114 	*/
115 	int (*get_periph_id)(struct udevice *dev, struct udevice *periph);
116 
117 	/**
118 	 * get_gpio_mux() - get the mux value for a particular GPIO
119 	 *
120 	 * This allows the raw mux value for a GPIO to be obtained. It is
121 	 * useful for displaying the function being used by that GPIO, such
122 	 * as with the 'gpio' command. This function is internal to the GPIO
123 	 * subsystem and should not be used by generic code. Typically it is
124 	 * used by a GPIO driver with knowledge of the SoC pinctrl setup.
125 	 *
126 	* @dev:		Pinctrl device to use
127 	* @banknum:	GPIO bank number
128 	* @index:	GPIO index within the bank
129 	* @return mux value (SoC-specific, e.g. 0 for input, 1 for output)
130 	 */
131 	int (*get_gpio_mux)(struct udevice *dev, int banknum, int index);
132 };
133 
134 #define pinctrl_get_ops(dev)	((struct pinctrl_ops *)(dev)->driver->ops)
135 
136 /**
137  * Generic pin configuration paramters
138  *
139  * enum pin_config_param - possible pin configuration parameters
140  * @PIN_CONFIG_BIAS_BUS_HOLD: the pin will be set to weakly latch so that it
141  *	weakly drives the last value on a tristate bus, also known as a "bus
142  *	holder", "bus keeper" or "repeater". This allows another device on the
143  *	bus to change the value by driving the bus high or low and switching to
144  *	tristate. The argument is ignored.
145  * @PIN_CONFIG_BIAS_DISABLE: disable any pin bias on the pin, a
146  *	transition from say pull-up to pull-down implies that you disable
147  *	pull-up in the process, this setting disables all biasing.
148  * @PIN_CONFIG_BIAS_HIGH_IMPEDANCE: the pin will be set to a high impedance
149  *	mode, also know as "third-state" (tristate) or "high-Z" or "floating".
150  *	On output pins this effectively disconnects the pin, which is useful
151  *	if for example some other pin is going to drive the signal connected
152  *	to it for a while. Pins used for input are usually always high
153  *	impedance.
154  * @PIN_CONFIG_BIAS_PULL_DOWN: the pin will be pulled down (usually with high
155  *	impedance to GROUND). If the argument is != 0 pull-down is enabled,
156  *	if it is 0, pull-down is total, i.e. the pin is connected to GROUND.
157  * @PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: the pin will be pulled up or down based
158  *	on embedded knowledge of the controller hardware, like current mux
159  *	function. The pull direction and possibly strength too will normally
160  *	be decided completely inside the hardware block and not be readable
161  *	from the kernel side.
162  *	If the argument is != 0 pull up/down is enabled, if it is 0, the
163  *	configuration is ignored. The proper way to disable it is to use
164  *	@PIN_CONFIG_BIAS_DISABLE.
165  * @PIN_CONFIG_BIAS_PULL_UP: the pin will be pulled up (usually with high
166  *	impedance to VDD). If the argument is != 0 pull-up is enabled,
167  *	if it is 0, pull-up is total, i.e. the pin is connected to VDD.
168  * @PIN_CONFIG_DRIVE_OPEN_DRAIN: the pin will be driven with open drain (open
169  *	collector) which means it is usually wired with other output ports
170  *	which are then pulled up with an external resistor. Setting this
171  *	config will enable open drain mode, the argument is ignored.
172  * @PIN_CONFIG_DRIVE_OPEN_SOURCE: the pin will be driven with open source
173  *	(open emitter). Setting this config will enable open source mode, the
174  *	argument is ignored.
175  * @PIN_CONFIG_DRIVE_PUSH_PULL: the pin will be driven actively high and
176  *	low, this is the most typical case and is typically achieved with two
177  *	active transistors on the output. Setting this config will enable
178  *	push-pull mode, the argument is ignored.
179  * @PIN_CONFIG_DRIVE_STRENGTH: the pin will sink or source at most the current
180  *	passed as argument. The argument is in mA.
181  * @PIN_CONFIG_INPUT_DEBOUNCE: this will configure the pin to debounce mode,
182  *	which means it will wait for signals to settle when reading inputs. The
183  *	argument gives the debounce time in usecs. Setting the
184  *	argument to zero turns debouncing off.
185  * @PIN_CONFIG_INPUT_ENABLE: enable the pin's input.  Note that this does not
186  *	affect the pin's ability to drive output.  1 enables input, 0 disables
187  *	input.
188  * @PIN_CONFIG_INPUT_SCHMITT: this will configure an input pin to run in
189  *	schmitt-trigger mode. If the schmitt-trigger has adjustable hysteresis,
190  *	the threshold value is given on a custom format as argument when
191  *	setting pins to this mode.
192  * @PIN_CONFIG_INPUT_SCHMITT_ENABLE: control schmitt-trigger mode on the pin.
193  *      If the argument != 0, schmitt-trigger mode is enabled. If it's 0,
194  *      schmitt-trigger mode is disabled.
195  * @PIN_CONFIG_LOW_POWER_MODE: this will configure the pin for low power
196  *	operation, if several modes of operation are supported these can be
197  *	passed in the argument on a custom form, else just use argument 1
198  *	to indicate low power mode, argument 0 turns low power mode off.
199  * @PIN_CONFIG_OUTPUT_ENABLE: this will enable the pin's output mode
200  *	without driving a value there. For most platforms this reduces to
201  *	enable the output buffers and then let the pin controller current
202  *	configuration (eg. the currently selected mux function) drive values on
203  *	the line. Use argument 1 to enable output mode, argument 0 to disable
204  *	it.
205  * @PIN_CONFIG_OUTPUT: this will configure the pin as an output and drive a
206  *	value on the line. Use argument 1 to indicate high level, argument 0 to
207  *	indicate low level. (Please see Documentation/driver-api/pinctl.rst,
208  *	section "GPIO mode pitfalls" for a discussion around this parameter.)
209  * @PIN_CONFIG_POWER_SOURCE: if the pin can select between different power
210  *	supplies, the argument to this parameter (on a custom format) tells
211  *	the driver which alternative power source to use.
212  * @PIN_CONFIG_SLEEP_HARDWARE_STATE: indicate this is sleep related state.
213  * @PIN_CONFIG_SLEW_RATE: if the pin can select slew rate, the argument to
214  *	this parameter (on a custom format) tells the driver which alternative
215  *	slew rate to use.
216  * @PIN_CONFIG_SKEW_DELAY: if the pin has programmable skew rate (on inputs)
217  *	or latch delay (on outputs) this parameter (in a custom format)
218  *	specifies the clock skew or latch delay. It typically controls how
219  *	many double inverters are put in front of the line.
220  * @PIN_CONFIG_END: this is the last enumerator for pin configurations, if
221  *	you need to pass in custom configurations to the pin controller, use
222  *	PIN_CONFIG_END+1 as the base offset.
223  * @PIN_CONFIG_MAX: this is the maximum configuration value that can be
224  *	presented using the packed format.
225  */
226 enum pin_config_param {
227 	PIN_CONFIG_BIAS_BUS_HOLD,
228 	PIN_CONFIG_BIAS_DISABLE,
229 	PIN_CONFIG_BIAS_HIGH_IMPEDANCE,
230 	PIN_CONFIG_BIAS_PULL_DOWN,
231 	PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
232 	PIN_CONFIG_BIAS_PULL_UP,
233 	PIN_CONFIG_DRIVE_OPEN_DRAIN,
234 	PIN_CONFIG_DRIVE_OPEN_SOURCE,
235 	PIN_CONFIG_DRIVE_PUSH_PULL,
236 	PIN_CONFIG_DRIVE_STRENGTH,
237 	PIN_CONFIG_INPUT_DEBOUNCE,
238 	PIN_CONFIG_INPUT_ENABLE,
239 	PIN_CONFIG_INPUT_SCHMITT,
240 	PIN_CONFIG_INPUT_SCHMITT_ENABLE,
241 	PIN_CONFIG_LOW_POWER_MODE,
242 	PIN_CONFIG_OUTPUT_ENABLE,
243 	PIN_CONFIG_OUTPUT,
244 	PIN_CONFIG_POWER_SOURCE,
245 	PIN_CONFIG_SLEEP_HARDWARE_STATE,
246 	PIN_CONFIG_SLEW_RATE,
247 	PIN_CONFIG_SKEW_DELAY,
248 	PIN_CONFIG_END = 0x7F,
249 	PIN_CONFIG_MAX = 0xFF,
250 };
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
pinctrl_generic_set_state(struct udevice * pctldev,struct udevice * config)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 #else
pinctrl_select_state(struct udevice * dev,const char * statename)281 static inline int pinctrl_select_state(struct udevice *dev,
282 				       const char *statename)
283 {
284 	return -EINVAL;
285 }
286 #endif
287 
288 /**
289  * pinctrl_request() - Request a particular pinctrl function
290  *
291  * @dev:	Device to check (UCLASS_PINCTRL)
292  * @func:	Function number (driver-specific)
293  * @flags:	Flags (driver-specific)
294  * @return 0 if OK, -ve on error
295  */
296 int pinctrl_request(struct udevice *dev, int func, int flags);
297 
298 /**
299  * pinctrl_request_noflags() - Request a particular pinctrl function
300  *
301  * This is similar to pinctrl_request() but uses 0 for @flags.
302  *
303  * @dev:	Device to check (UCLASS_PINCTRL)
304  * @func:	Function number (driver-specific)
305  * @return 0 if OK, -ve on error
306  */
307 int pinctrl_request_noflags(struct udevice *dev, int func);
308 
309 /**
310  * pinctrl_get_periph_id() - get the peripheral ID for a device
311  *
312  * This generally looks at the peripheral's device tree node to work out the
313  * peripheral ID. The return value is normally interpreted as enum periph_id.
314  * so long as this is defined by the platform (which it should be).
315  *
316  * @dev:	Pinctrl device to use for decoding
317  * @periph:	Device to check
318  * @return peripheral ID of @periph, or -ENOENT on error
319  */
320 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph);
321 
322 /**
323  * pinctrl_decode_pin_config() - decode pin configuration flags
324  *
325  * This decodes some of the PIN_CONFIG values into flags, with each value
326  * being (1 << pin_cfg). This does not support things with values like the
327  * slew rate.
328  *
329  * @blob:	Device tree blob
330  * @node:	Node containing the PIN_CONFIG values
331  * @return decoded flag value, or -ve on error
332  */
333 int pinctrl_decode_pin_config(const void *blob, int node);
334 
335 /**
336  * pinctrl_get_gpio_mux() - get the mux value for a particular GPIO
337  *
338  * This allows the raw mux value for a GPIO to be obtained. It is
339  * useful for displaying the function being used by that GPIO, such
340  * as with the 'gpio' command. This function is internal to the GPIO
341  * subsystem and should not be used by generic code. Typically it is
342  * used by a GPIO driver with knowledge of the SoC pinctrl setup.
343  *
344  * @dev:	Pinctrl device to use
345  * @banknum:	GPIO bank number
346  * @index:	GPIO index within the bank
347  * @return mux value (SoC-specific, e.g. 0 for input, 1 for output)
348 */
349 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index);
350 
351 #endif /* __PINCTRL_H */
352