xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/pinctl.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun===============================
2*4882a593SmuzhiyunPINCTRL (PIN CONTROL) subsystem
3*4882a593Smuzhiyun===============================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThis document outlines the pin control subsystem in Linux
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThis subsystem deals with:
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun- Enumerating and naming controllable pins
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun- Multiplexing of pins, pads, fingers (etc) see below for details
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun- Configuration of pins, pads, fingers (etc), such as software-controlled
14*4882a593Smuzhiyun  biasing and driving mode specific pins, such as pull-up/down, open drain,
15*4882a593Smuzhiyun  load capacitance etc.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunTop-level interface
18*4882a593Smuzhiyun===================
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunDefinition of PIN CONTROLLER:
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun- A pin controller is a piece of hardware, usually a set of registers, that
23*4882a593Smuzhiyun  can control PINs. It may be able to multiplex, bias, set load capacitance,
24*4882a593Smuzhiyun  set drive strength, etc. for individual pins or groups of pins.
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunDefinition of PIN:
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun- PINS are equal to pads, fingers, balls or whatever packaging input or
29*4882a593Smuzhiyun  output line you want to control and these are denoted by unsigned integers
30*4882a593Smuzhiyun  in the range 0..maxpin. This numberspace is local to each PIN CONTROLLER, so
31*4882a593Smuzhiyun  there may be several such number spaces in a system. This pin space may
32*4882a593Smuzhiyun  be sparse - i.e. there may be gaps in the space with numbers where no
33*4882a593Smuzhiyun  pin exists.
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunWhen a PIN CONTROLLER is instantiated, it will register a descriptor to the
36*4882a593Smuzhiyunpin control framework, and this descriptor contains an array of pin descriptors
37*4882a593Smuzhiyundescribing the pins handled by this specific pin controller.
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunHere is an example of a PGA (Pin Grid Array) chip seen from underneath::
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun        A   B   C   D   E   F   G   H
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun   8    o   o   o   o   o   o   o   o
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun   7    o   o   o   o   o   o   o   o
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun   6    o   o   o   o   o   o   o   o
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun   5    o   o   o   o   o   o   o   o
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun   4    o   o   o   o   o   o   o   o
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun   3    o   o   o   o   o   o   o   o
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun   2    o   o   o   o   o   o   o   o
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun   1    o   o   o   o   o   o   o   o
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunTo register a pin controller and name all the pins on this package we can do
60*4882a593Smuzhiyunthis in our driver::
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun	#include <linux/pinctrl/pinctrl.h>
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun	const struct pinctrl_pin_desc foo_pins[] = {
65*4882a593Smuzhiyun		PINCTRL_PIN(0, "A8"),
66*4882a593Smuzhiyun		PINCTRL_PIN(1, "B8"),
67*4882a593Smuzhiyun		PINCTRL_PIN(2, "C8"),
68*4882a593Smuzhiyun		...
69*4882a593Smuzhiyun		PINCTRL_PIN(61, "F1"),
70*4882a593Smuzhiyun		PINCTRL_PIN(62, "G1"),
71*4882a593Smuzhiyun		PINCTRL_PIN(63, "H1"),
72*4882a593Smuzhiyun	};
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun	static struct pinctrl_desc foo_desc = {
75*4882a593Smuzhiyun		.name = "foo",
76*4882a593Smuzhiyun		.pins = foo_pins,
77*4882a593Smuzhiyun		.npins = ARRAY_SIZE(foo_pins),
78*4882a593Smuzhiyun		.owner = THIS_MODULE,
79*4882a593Smuzhiyun	};
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun	int __init foo_probe(void)
82*4882a593Smuzhiyun	{
83*4882a593Smuzhiyun		int error;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun		struct pinctrl_dev *pctl;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun		error = pinctrl_register_and_init(&foo_desc, <PARENT>,
88*4882a593Smuzhiyun						  NULL, &pctl);
89*4882a593Smuzhiyun		if (error)
90*4882a593Smuzhiyun			return error;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun		return pinctrl_enable(pctl);
93*4882a593Smuzhiyun	}
94*4882a593Smuzhiyun
95*4882a593SmuzhiyunTo enable the pinctrl subsystem and the subgroups for PINMUX and PINCONF and
96*4882a593Smuzhiyunselected drivers, you need to select them from your machine's Kconfig entry,
97*4882a593Smuzhiyunsince these are so tightly integrated with the machines they are used on.
98*4882a593SmuzhiyunSee for example arch/arm/mach-u300/Kconfig for an example.
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunPins usually have fancier names than this. You can find these in the datasheet
101*4882a593Smuzhiyunfor your chip. Notice that the core pinctrl.h file provides a fancy macro
102*4882a593Smuzhiyuncalled PINCTRL_PIN() to create the struct entries. As you can see I enumerated
103*4882a593Smuzhiyunthe pins from 0 in the upper left corner to 63 in the lower right corner.
104*4882a593SmuzhiyunThis enumeration was arbitrarily chosen, in practice you need to think
105*4882a593Smuzhiyunthrough your numbering system so that it matches the layout of registers
106*4882a593Smuzhiyunand such things in your driver, or the code may become complicated. You must
107*4882a593Smuzhiyunalso consider matching of offsets to the GPIO ranges that may be handled by
108*4882a593Smuzhiyunthe pin controller.
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunFor a padring with 467 pads, as opposed to actual pins, I used an enumeration
111*4882a593Smuzhiyunlike this, walking around the edge of the chip, which seems to be industry
112*4882a593Smuzhiyunstandard too (all these pads had names, too)::
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun     0 ..... 104
116*4882a593Smuzhiyun   466        105
117*4882a593Smuzhiyun     .        .
118*4882a593Smuzhiyun     .        .
119*4882a593Smuzhiyun   358        224
120*4882a593Smuzhiyun    357 .... 225
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunPin groups
124*4882a593Smuzhiyun==========
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunMany controllers need to deal with groups of pins, so the pin controller
127*4882a593Smuzhiyunsubsystem has a mechanism for enumerating groups of pins and retrieving the
128*4882a593Smuzhiyunactual enumerated pins that are part of a certain group.
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunFor example, say that we have a group of pins dealing with an SPI interface
131*4882a593Smuzhiyunon { 0, 8, 16, 24 }, and a group of pins dealing with an I2C interface on pins
132*4882a593Smuzhiyunon { 24, 25 }.
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunThese two groups are presented to the pin control subsystem by implementing
135*4882a593Smuzhiyunsome generic pinctrl_ops like this::
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun	#include <linux/pinctrl/pinctrl.h>
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun	struct foo_group {
140*4882a593Smuzhiyun		const char *name;
141*4882a593Smuzhiyun		const unsigned int *pins;
142*4882a593Smuzhiyun		const unsigned num_pins;
143*4882a593Smuzhiyun	};
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun	static const unsigned int spi0_pins[] = { 0, 8, 16, 24 };
146*4882a593Smuzhiyun	static const unsigned int i2c0_pins[] = { 24, 25 };
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun	static const struct foo_group foo_groups[] = {
149*4882a593Smuzhiyun		{
150*4882a593Smuzhiyun			.name = "spi0_grp",
151*4882a593Smuzhiyun			.pins = spi0_pins,
152*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(spi0_pins),
153*4882a593Smuzhiyun		},
154*4882a593Smuzhiyun		{
155*4882a593Smuzhiyun			.name = "i2c0_grp",
156*4882a593Smuzhiyun			.pins = i2c0_pins,
157*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(i2c0_pins),
158*4882a593Smuzhiyun		},
159*4882a593Smuzhiyun	};
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun	static int foo_get_groups_count(struct pinctrl_dev *pctldev)
163*4882a593Smuzhiyun	{
164*4882a593Smuzhiyun		return ARRAY_SIZE(foo_groups);
165*4882a593Smuzhiyun	}
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun	static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
168*4882a593Smuzhiyun					unsigned selector)
169*4882a593Smuzhiyun	{
170*4882a593Smuzhiyun		return foo_groups[selector].name;
171*4882a593Smuzhiyun	}
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun	static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
174*4882a593Smuzhiyun				const unsigned **pins,
175*4882a593Smuzhiyun				unsigned *num_pins)
176*4882a593Smuzhiyun	{
177*4882a593Smuzhiyun		*pins = (unsigned *) foo_groups[selector].pins;
178*4882a593Smuzhiyun		*num_pins = foo_groups[selector].num_pins;
179*4882a593Smuzhiyun		return 0;
180*4882a593Smuzhiyun	}
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun	static struct pinctrl_ops foo_pctrl_ops = {
183*4882a593Smuzhiyun		.get_groups_count = foo_get_groups_count,
184*4882a593Smuzhiyun		.get_group_name = foo_get_group_name,
185*4882a593Smuzhiyun		.get_group_pins = foo_get_group_pins,
186*4882a593Smuzhiyun	};
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun	static struct pinctrl_desc foo_desc = {
190*4882a593Smuzhiyun	...
191*4882a593Smuzhiyun	.pctlops = &foo_pctrl_ops,
192*4882a593Smuzhiyun	};
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunThe pin control subsystem will call the .get_groups_count() function to
195*4882a593Smuzhiyundetermine the total number of legal selectors, then it will call the other functions
196*4882a593Smuzhiyunto retrieve the name and pins of the group. Maintaining the data structure of
197*4882a593Smuzhiyunthe groups is up to the driver, this is just a simple example - in practice you
198*4882a593Smuzhiyunmay need more entries in your group structure, for example specific register
199*4882a593Smuzhiyunranges associated with each group and so on.
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunPin configuration
203*4882a593Smuzhiyun=================
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunPins can sometimes be software-configured in various ways, mostly related
206*4882a593Smuzhiyunto their electronic properties when used as inputs or outputs. For example you
207*4882a593Smuzhiyunmay be able to make an output pin high impedance, or "tristate" meaning it is
208*4882a593Smuzhiyuneffectively disconnected. You may be able to connect an input pin to VDD or GND
209*4882a593Smuzhiyunusing a certain resistor value - pull up and pull down - so that the pin has a
210*4882a593Smuzhiyunstable value when nothing is driving the rail it is connected to, or when it's
211*4882a593Smuzhiyununconnected.
212*4882a593Smuzhiyun
213*4882a593SmuzhiyunPin configuration can be programmed by adding configuration entries into the
214*4882a593Smuzhiyunmapping table; see section "Board/machine configuration" below.
215*4882a593Smuzhiyun
216*4882a593SmuzhiyunThe format and meaning of the configuration parameter, PLATFORM_X_PULL_UP
217*4882a593Smuzhiyunabove, is entirely defined by the pin controller driver.
218*4882a593Smuzhiyun
219*4882a593SmuzhiyunThe pin configuration driver implements callbacks for changing pin
220*4882a593Smuzhiyunconfiguration in the pin controller ops like this::
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun	#include <linux/pinctrl/pinctrl.h>
223*4882a593Smuzhiyun	#include <linux/pinctrl/pinconf.h>
224*4882a593Smuzhiyun	#include "platform_x_pindefs.h"
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun	static int foo_pin_config_get(struct pinctrl_dev *pctldev,
227*4882a593Smuzhiyun			unsigned offset,
228*4882a593Smuzhiyun			unsigned long *config)
229*4882a593Smuzhiyun	{
230*4882a593Smuzhiyun		struct my_conftype conf;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun		... Find setting for pin @ offset ...
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun		*config = (unsigned long) conf;
235*4882a593Smuzhiyun	}
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun	static int foo_pin_config_set(struct pinctrl_dev *pctldev,
238*4882a593Smuzhiyun			unsigned offset,
239*4882a593Smuzhiyun			unsigned long config)
240*4882a593Smuzhiyun	{
241*4882a593Smuzhiyun		struct my_conftype *conf = (struct my_conftype *) config;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun		switch (conf) {
244*4882a593Smuzhiyun			case PLATFORM_X_PULL_UP:
245*4882a593Smuzhiyun			...
246*4882a593Smuzhiyun			}
247*4882a593Smuzhiyun		}
248*4882a593Smuzhiyun	}
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun	static int foo_pin_config_group_get (struct pinctrl_dev *pctldev,
251*4882a593Smuzhiyun			unsigned selector,
252*4882a593Smuzhiyun			unsigned long *config)
253*4882a593Smuzhiyun	{
254*4882a593Smuzhiyun		...
255*4882a593Smuzhiyun	}
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun	static int foo_pin_config_group_set (struct pinctrl_dev *pctldev,
258*4882a593Smuzhiyun			unsigned selector,
259*4882a593Smuzhiyun			unsigned long config)
260*4882a593Smuzhiyun	{
261*4882a593Smuzhiyun		...
262*4882a593Smuzhiyun	}
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun	static struct pinconf_ops foo_pconf_ops = {
265*4882a593Smuzhiyun		.pin_config_get = foo_pin_config_get,
266*4882a593Smuzhiyun		.pin_config_set = foo_pin_config_set,
267*4882a593Smuzhiyun		.pin_config_group_get = foo_pin_config_group_get,
268*4882a593Smuzhiyun		.pin_config_group_set = foo_pin_config_group_set,
269*4882a593Smuzhiyun	};
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun	/* Pin config operations are handled by some pin controller */
272*4882a593Smuzhiyun	static struct pinctrl_desc foo_desc = {
273*4882a593Smuzhiyun		...
274*4882a593Smuzhiyun		.confops = &foo_pconf_ops,
275*4882a593Smuzhiyun	};
276*4882a593Smuzhiyun
277*4882a593SmuzhiyunInteraction with the GPIO subsystem
278*4882a593Smuzhiyun===================================
279*4882a593Smuzhiyun
280*4882a593SmuzhiyunThe GPIO drivers may want to perform operations of various types on the same
281*4882a593Smuzhiyunphysical pins that are also registered as pin controller pins.
282*4882a593Smuzhiyun
283*4882a593SmuzhiyunFirst and foremost, the two subsystems can be used as completely orthogonal,
284*4882a593Smuzhiyunsee the section named "pin control requests from drivers" and
285*4882a593Smuzhiyun"drivers needing both pin control and GPIOs" below for details. But in some
286*4882a593Smuzhiyunsituations a cross-subsystem mapping between pins and GPIOs is needed.
287*4882a593Smuzhiyun
288*4882a593SmuzhiyunSince the pin controller subsystem has its pinspace local to the pin controller
289*4882a593Smuzhiyunwe need a mapping so that the pin control subsystem can figure out which pin
290*4882a593Smuzhiyuncontroller handles control of a certain GPIO pin. Since a single pin controller
291*4882a593Smuzhiyunmay be muxing several GPIO ranges (typically SoCs that have one set of pins,
292*4882a593Smuzhiyunbut internally several GPIO silicon blocks, each modelled as a struct
293*4882a593Smuzhiyungpio_chip) any number of GPIO ranges can be added to a pin controller instance
294*4882a593Smuzhiyunlike this::
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun	struct gpio_chip chip_a;
297*4882a593Smuzhiyun	struct gpio_chip chip_b;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun	static struct pinctrl_gpio_range gpio_range_a = {
300*4882a593Smuzhiyun		.name = "chip a",
301*4882a593Smuzhiyun		.id = 0,
302*4882a593Smuzhiyun		.base = 32,
303*4882a593Smuzhiyun		.pin_base = 32,
304*4882a593Smuzhiyun		.npins = 16,
305*4882a593Smuzhiyun		.gc = &chip_a;
306*4882a593Smuzhiyun	};
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun	static struct pinctrl_gpio_range gpio_range_b = {
309*4882a593Smuzhiyun		.name = "chip b",
310*4882a593Smuzhiyun		.id = 0,
311*4882a593Smuzhiyun		.base = 48,
312*4882a593Smuzhiyun		.pin_base = 64,
313*4882a593Smuzhiyun		.npins = 8,
314*4882a593Smuzhiyun		.gc = &chip_b;
315*4882a593Smuzhiyun	};
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun	{
318*4882a593Smuzhiyun		struct pinctrl_dev *pctl;
319*4882a593Smuzhiyun		...
320*4882a593Smuzhiyun		pinctrl_add_gpio_range(pctl, &gpio_range_a);
321*4882a593Smuzhiyun		pinctrl_add_gpio_range(pctl, &gpio_range_b);
322*4882a593Smuzhiyun	}
323*4882a593Smuzhiyun
324*4882a593SmuzhiyunSo this complex system has one pin controller handling two different
325*4882a593SmuzhiyunGPIO chips. "chip a" has 16 pins and "chip b" has 8 pins. The "chip a" and
326*4882a593Smuzhiyun"chip b" have different .pin_base, which means a start pin number of the
327*4882a593SmuzhiyunGPIO range.
328*4882a593Smuzhiyun
329*4882a593SmuzhiyunThe GPIO range of "chip a" starts from the GPIO base of 32 and actual
330*4882a593Smuzhiyunpin range also starts from 32. However "chip b" has different starting
331*4882a593Smuzhiyunoffset for the GPIO range and pin range. The GPIO range of "chip b" starts
332*4882a593Smuzhiyunfrom GPIO number 48, while the pin range of "chip b" starts from 64.
333*4882a593Smuzhiyun
334*4882a593SmuzhiyunWe can convert a gpio number to actual pin number using this "pin_base".
335*4882a593SmuzhiyunThey are mapped in the global GPIO pin space at:
336*4882a593Smuzhiyun
337*4882a593Smuzhiyunchip a:
338*4882a593Smuzhiyun - GPIO range : [32 .. 47]
339*4882a593Smuzhiyun - pin range  : [32 .. 47]
340*4882a593Smuzhiyunchip b:
341*4882a593Smuzhiyun - GPIO range : [48 .. 55]
342*4882a593Smuzhiyun - pin range  : [64 .. 71]
343*4882a593Smuzhiyun
344*4882a593SmuzhiyunThe above examples assume the mapping between the GPIOs and pins is
345*4882a593Smuzhiyunlinear. If the mapping is sparse or haphazard, an array of arbitrary pin
346*4882a593Smuzhiyunnumbers can be encoded in the range like this::
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun	static const unsigned range_pins[] = { 14, 1, 22, 17, 10, 8, 6, 2 };
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun	static struct pinctrl_gpio_range gpio_range = {
351*4882a593Smuzhiyun		.name = "chip",
352*4882a593Smuzhiyun		.id = 0,
353*4882a593Smuzhiyun		.base = 32,
354*4882a593Smuzhiyun		.pins = &range_pins,
355*4882a593Smuzhiyun		.npins = ARRAY_SIZE(range_pins),
356*4882a593Smuzhiyun		.gc = &chip;
357*4882a593Smuzhiyun	};
358*4882a593Smuzhiyun
359*4882a593SmuzhiyunIn this case the pin_base property will be ignored. If the name of a pin
360*4882a593Smuzhiyungroup is known, the pins and npins elements of the above structure can be
361*4882a593Smuzhiyuninitialised using the function pinctrl_get_group_pins(), e.g. for pin
362*4882a593Smuzhiyungroup "foo"::
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun	pinctrl_get_group_pins(pctl, "foo", &gpio_range.pins,
365*4882a593Smuzhiyun			       &gpio_range.npins);
366*4882a593Smuzhiyun
367*4882a593SmuzhiyunWhen GPIO-specific functions in the pin control subsystem are called, these
368*4882a593Smuzhiyunranges will be used to look up the appropriate pin controller by inspecting
369*4882a593Smuzhiyunand matching the pin to the pin ranges across all controllers. When a
370*4882a593Smuzhiyunpin controller handling the matching range is found, GPIO-specific functions
371*4882a593Smuzhiyunwill be called on that specific pin controller.
372*4882a593Smuzhiyun
373*4882a593SmuzhiyunFor all functionalities dealing with pin biasing, pin muxing etc, the pin
374*4882a593Smuzhiyuncontroller subsystem will look up the corresponding pin number from the passed
375*4882a593Smuzhiyunin gpio number, and use the range's internals to retrieve a pin number. After
376*4882a593Smuzhiyunthat, the subsystem passes it on to the pin control driver, so the driver
377*4882a593Smuzhiyunwill get a pin number into its handled number range. Further it is also passed
378*4882a593Smuzhiyunthe range ID value, so that the pin controller knows which range it should
379*4882a593Smuzhiyundeal with.
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunCalling pinctrl_add_gpio_range from pinctrl driver is DEPRECATED. Please see
382*4882a593Smuzhiyunsection 2.1 of Documentation/devicetree/bindings/gpio/gpio.txt on how to bind
383*4882a593Smuzhiyunpinctrl and gpio drivers.
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun
386*4882a593SmuzhiyunPINMUX interfaces
387*4882a593Smuzhiyun=================
388*4882a593Smuzhiyun
389*4882a593SmuzhiyunThese calls use the pinmux_* naming prefix.  No other calls should use that
390*4882a593Smuzhiyunprefix.
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun
393*4882a593SmuzhiyunWhat is pinmuxing?
394*4882a593Smuzhiyun==================
395*4882a593Smuzhiyun
396*4882a593SmuzhiyunPINMUX, also known as padmux, ballmux, alternate functions or mission modes
397*4882a593Smuzhiyunis a way for chip vendors producing some kind of electrical packages to use
398*4882a593Smuzhiyuna certain physical pin (ball, pad, finger, etc) for multiple mutually exclusive
399*4882a593Smuzhiyunfunctions, depending on the application. By "application" in this context
400*4882a593Smuzhiyunwe usually mean a way of soldering or wiring the package into an electronic
401*4882a593Smuzhiyunsystem, even though the framework makes it possible to also change the function
402*4882a593Smuzhiyunat runtime.
403*4882a593Smuzhiyun
404*4882a593SmuzhiyunHere is an example of a PGA (Pin Grid Array) chip seen from underneath::
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun        A   B   C   D   E   F   G   H
407*4882a593Smuzhiyun      +---+
408*4882a593Smuzhiyun   8  | o | o   o   o   o   o   o   o
409*4882a593Smuzhiyun      |   |
410*4882a593Smuzhiyun   7  | o | o   o   o   o   o   o   o
411*4882a593Smuzhiyun      |   |
412*4882a593Smuzhiyun   6  | o | o   o   o   o   o   o   o
413*4882a593Smuzhiyun      +---+---+
414*4882a593Smuzhiyun   5  | o | o | o   o   o   o   o   o
415*4882a593Smuzhiyun      +---+---+               +---+
416*4882a593Smuzhiyun   4    o   o   o   o   o   o | o | o
417*4882a593Smuzhiyun                              |   |
418*4882a593Smuzhiyun   3    o   o   o   o   o   o | o | o
419*4882a593Smuzhiyun                              |   |
420*4882a593Smuzhiyun   2    o   o   o   o   o   o | o | o
421*4882a593Smuzhiyun      +-------+-------+-------+---+---+
422*4882a593Smuzhiyun   1  | o   o | o   o | o   o | o | o |
423*4882a593Smuzhiyun      +-------+-------+-------+---+---+
424*4882a593Smuzhiyun
425*4882a593SmuzhiyunThis is not tetris. The game to think of is chess. Not all PGA/BGA packages
426*4882a593Smuzhiyunare chessboard-like, big ones have "holes" in some arrangement according to
427*4882a593Smuzhiyundifferent design patterns, but we're using this as a simple example. Of the
428*4882a593Smuzhiyunpins you see some will be taken by things like a few VCC and GND to feed power
429*4882a593Smuzhiyunto the chip, and quite a few will be taken by large ports like an external
430*4882a593Smuzhiyunmemory interface. The remaining pins will often be subject to pin multiplexing.
431*4882a593Smuzhiyun
432*4882a593SmuzhiyunThe example 8x8 PGA package above will have pin numbers 0 through 63 assigned
433*4882a593Smuzhiyunto its physical pins. It will name the pins { A1, A2, A3 ... H6, H7, H8 } using
434*4882a593Smuzhiyunpinctrl_register_pins() and a suitable data set as shown earlier.
435*4882a593Smuzhiyun
436*4882a593SmuzhiyunIn this 8x8 BGA package the pins { A8, A7, A6, A5 } can be used as an SPI port
437*4882a593Smuzhiyun(these are four pins: CLK, RXD, TXD, FRM). In that case, pin B5 can be used as
438*4882a593Smuzhiyunsome general-purpose GPIO pin. However, in another setting, pins { A5, B5 } can
439*4882a593Smuzhiyunbe used as an I2C port (these are just two pins: SCL, SDA). Needless to say,
440*4882a593Smuzhiyunwe cannot use the SPI port and I2C port at the same time. However in the inside
441*4882a593Smuzhiyunof the package the silicon performing the SPI logic can alternatively be routed
442*4882a593Smuzhiyunout on pins { G4, G3, G2, G1 }.
443*4882a593Smuzhiyun
444*4882a593SmuzhiyunOn the bottom row at { A1, B1, C1, D1, E1, F1, G1, H1 } we have something
445*4882a593Smuzhiyunspecial - it's an external MMC bus that can be 2, 4 or 8 bits wide, and it will
446*4882a593Smuzhiyunconsume 2, 4 or 8 pins respectively, so either { A1, B1 } are taken or
447*4882a593Smuzhiyun{ A1, B1, C1, D1 } or all of them. If we use all 8 bits, we cannot use the SPI
448*4882a593Smuzhiyunport on pins { G4, G3, G2, G1 } of course.
449*4882a593Smuzhiyun
450*4882a593SmuzhiyunThis way the silicon blocks present inside the chip can be multiplexed "muxed"
451*4882a593Smuzhiyunout on different pin ranges. Often contemporary SoC (systems on chip) will
452*4882a593Smuzhiyuncontain several I2C, SPI, SDIO/MMC, etc silicon blocks that can be routed to
453*4882a593Smuzhiyundifferent pins by pinmux settings.
454*4882a593Smuzhiyun
455*4882a593SmuzhiyunSince general-purpose I/O pins (GPIO) are typically always in shortage, it is
456*4882a593Smuzhiyuncommon to be able to use almost any pin as a GPIO pin if it is not currently
457*4882a593Smuzhiyunin use by some other I/O port.
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun
460*4882a593SmuzhiyunPinmux conventions
461*4882a593Smuzhiyun==================
462*4882a593Smuzhiyun
463*4882a593SmuzhiyunThe purpose of the pinmux functionality in the pin controller subsystem is to
464*4882a593Smuzhiyunabstract and provide pinmux settings to the devices you choose to instantiate
465*4882a593Smuzhiyunin your machine configuration. It is inspired by the clk, GPIO and regulator
466*4882a593Smuzhiyunsubsystems, so devices will request their mux setting, but it's also possible
467*4882a593Smuzhiyunto request a single pin for e.g. GPIO.
468*4882a593Smuzhiyun
469*4882a593SmuzhiyunDefinitions:
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun- FUNCTIONS can be switched in and out by a driver residing with the pin
472*4882a593Smuzhiyun  control subsystem in the drivers/pinctrl/* directory of the kernel. The
473*4882a593Smuzhiyun  pin control driver knows the possible functions. In the example above you can
474*4882a593Smuzhiyun  identify three pinmux functions, one for spi, one for i2c and one for mmc.
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun- FUNCTIONS are assumed to be enumerable from zero in a one-dimensional array.
477*4882a593Smuzhiyun  In this case the array could be something like: { spi0, i2c0, mmc0 }
478*4882a593Smuzhiyun  for the three available functions.
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun- FUNCTIONS have PIN GROUPS as defined on the generic level - so a certain
481*4882a593Smuzhiyun  function is *always* associated with a certain set of pin groups, could
482*4882a593Smuzhiyun  be just a single one, but could also be many. In the example above the
483*4882a593Smuzhiyun  function i2c is associated with the pins { A5, B5 }, enumerated as
484*4882a593Smuzhiyun  { 24, 25 } in the controller pin space.
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun  The Function spi is associated with pin groups { A8, A7, A6, A5 }
487*4882a593Smuzhiyun  and { G4, G3, G2, G1 }, which are enumerated as { 0, 8, 16, 24 } and
488*4882a593Smuzhiyun  { 38, 46, 54, 62 } respectively.
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun  Group names must be unique per pin controller, no two groups on the same
491*4882a593Smuzhiyun  controller may have the same name.
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun- The combination of a FUNCTION and a PIN GROUP determine a certain function
494*4882a593Smuzhiyun  for a certain set of pins. The knowledge of the functions and pin groups
495*4882a593Smuzhiyun  and their machine-specific particulars are kept inside the pinmux driver,
496*4882a593Smuzhiyun  from the outside only the enumerators are known, and the driver core can
497*4882a593Smuzhiyun  request:
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun  - The name of a function with a certain selector (>= 0)
500*4882a593Smuzhiyun  - A list of groups associated with a certain function
501*4882a593Smuzhiyun  - That a certain group in that list to be activated for a certain function
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun  As already described above, pin groups are in turn self-descriptive, so
504*4882a593Smuzhiyun  the core will retrieve the actual pin range in a certain group from the
505*4882a593Smuzhiyun  driver.
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun- FUNCTIONS and GROUPS on a certain PIN CONTROLLER are MAPPED to a certain
508*4882a593Smuzhiyun  device by the board file, device tree or similar machine setup configuration
509*4882a593Smuzhiyun  mechanism, similar to how regulators are connected to devices, usually by
510*4882a593Smuzhiyun  name. Defining a pin controller, function and group thus uniquely identify
511*4882a593Smuzhiyun  the set of pins to be used by a certain device. (If only one possible group
512*4882a593Smuzhiyun  of pins is available for the function, no group name need to be supplied -
513*4882a593Smuzhiyun  the core will simply select the first and only group available.)
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun  In the example case we can define that this particular machine shall
516*4882a593Smuzhiyun  use device spi0 with pinmux function fspi0 group gspi0 and i2c0 on function
517*4882a593Smuzhiyun  fi2c0 group gi2c0, on the primary pin controller, we get mappings
518*4882a593Smuzhiyun  like these::
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun	{
521*4882a593Smuzhiyun		{"map-spi0", spi0, pinctrl0, fspi0, gspi0},
522*4882a593Smuzhiyun		{"map-i2c0", i2c0, pinctrl0, fi2c0, gi2c0}
523*4882a593Smuzhiyun	}
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun  Every map must be assigned a state name, pin controller, device and
526*4882a593Smuzhiyun  function. The group is not compulsory - if it is omitted the first group
527*4882a593Smuzhiyun  presented by the driver as applicable for the function will be selected,
528*4882a593Smuzhiyun  which is useful for simple cases.
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun  It is possible to map several groups to the same combination of device,
531*4882a593Smuzhiyun  pin controller and function. This is for cases where a certain function on
532*4882a593Smuzhiyun  a certain pin controller may use different sets of pins in different
533*4882a593Smuzhiyun  configurations.
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun- PINS for a certain FUNCTION using a certain PIN GROUP on a certain
536*4882a593Smuzhiyun  PIN CONTROLLER are provided on a first-come first-serve basis, so if some
537*4882a593Smuzhiyun  other device mux setting or GPIO pin request has already taken your physical
538*4882a593Smuzhiyun  pin, you will be denied the use of it. To get (activate) a new setting, the
539*4882a593Smuzhiyun  old one has to be put (deactivated) first.
540*4882a593Smuzhiyun
541*4882a593SmuzhiyunSometimes the documentation and hardware registers will be oriented around
542*4882a593Smuzhiyunpads (or "fingers") rather than pins - these are the soldering surfaces on the
543*4882a593Smuzhiyunsilicon inside the package, and may or may not match the actual number of
544*4882a593Smuzhiyunpins/balls underneath the capsule. Pick some enumeration that makes sense to
545*4882a593Smuzhiyunyou. Define enumerators only for the pins you can control if that makes sense.
546*4882a593Smuzhiyun
547*4882a593SmuzhiyunAssumptions:
548*4882a593Smuzhiyun
549*4882a593SmuzhiyunWe assume that the number of possible function maps to pin groups is limited by
550*4882a593Smuzhiyunthe hardware. I.e. we assume that there is no system where any function can be
551*4882a593Smuzhiyunmapped to any pin, like in a phone exchange. So the available pin groups for
552*4882a593Smuzhiyuna certain function will be limited to a few choices (say up to eight or so),
553*4882a593Smuzhiyunnot hundreds or any amount of choices. This is the characteristic we have found
554*4882a593Smuzhiyunby inspecting available pinmux hardware, and a necessary assumption since we
555*4882a593Smuzhiyunexpect pinmux drivers to present *all* possible function vs pin group mappings
556*4882a593Smuzhiyunto the subsystem.
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun
559*4882a593SmuzhiyunPinmux drivers
560*4882a593Smuzhiyun==============
561*4882a593Smuzhiyun
562*4882a593SmuzhiyunThe pinmux core takes care of preventing conflicts on pins and calling
563*4882a593Smuzhiyunthe pin controller driver to execute different settings.
564*4882a593Smuzhiyun
565*4882a593SmuzhiyunIt is the responsibility of the pinmux driver to impose further restrictions
566*4882a593Smuzhiyun(say for example infer electronic limitations due to load, etc.) to determine
567*4882a593Smuzhiyunwhether or not the requested function can actually be allowed, and in case it
568*4882a593Smuzhiyunis possible to perform the requested mux setting, poke the hardware so that
569*4882a593Smuzhiyunthis happens.
570*4882a593Smuzhiyun
571*4882a593SmuzhiyunPinmux drivers are required to supply a few callback functions, some are
572*4882a593Smuzhiyunoptional. Usually the set_mux() function is implemented, writing values into
573*4882a593Smuzhiyunsome certain registers to activate a certain mux setting for a certain pin.
574*4882a593Smuzhiyun
575*4882a593SmuzhiyunA simple driver for the above example will work by setting bits 0, 1, 2, 3 or 4
576*4882a593Smuzhiyuninto some register named MUX to select a certain function with a certain
577*4882a593Smuzhiyungroup of pins would work something like this::
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun	#include <linux/pinctrl/pinctrl.h>
580*4882a593Smuzhiyun	#include <linux/pinctrl/pinmux.h>
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun	struct foo_group {
583*4882a593Smuzhiyun		const char *name;
584*4882a593Smuzhiyun		const unsigned int *pins;
585*4882a593Smuzhiyun		const unsigned num_pins;
586*4882a593Smuzhiyun	};
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun	static const unsigned spi0_0_pins[] = { 0, 8, 16, 24 };
589*4882a593Smuzhiyun	static const unsigned spi0_1_pins[] = { 38, 46, 54, 62 };
590*4882a593Smuzhiyun	static const unsigned i2c0_pins[] = { 24, 25 };
591*4882a593Smuzhiyun	static const unsigned mmc0_1_pins[] = { 56, 57 };
592*4882a593Smuzhiyun	static const unsigned mmc0_2_pins[] = { 58, 59 };
593*4882a593Smuzhiyun	static const unsigned mmc0_3_pins[] = { 60, 61, 62, 63 };
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun	static const struct foo_group foo_groups[] = {
596*4882a593Smuzhiyun		{
597*4882a593Smuzhiyun			.name = "spi0_0_grp",
598*4882a593Smuzhiyun			.pins = spi0_0_pins,
599*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(spi0_0_pins),
600*4882a593Smuzhiyun		},
601*4882a593Smuzhiyun		{
602*4882a593Smuzhiyun			.name = "spi0_1_grp",
603*4882a593Smuzhiyun			.pins = spi0_1_pins,
604*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(spi0_1_pins),
605*4882a593Smuzhiyun		},
606*4882a593Smuzhiyun		{
607*4882a593Smuzhiyun			.name = "i2c0_grp",
608*4882a593Smuzhiyun			.pins = i2c0_pins,
609*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(i2c0_pins),
610*4882a593Smuzhiyun		},
611*4882a593Smuzhiyun		{
612*4882a593Smuzhiyun			.name = "mmc0_1_grp",
613*4882a593Smuzhiyun			.pins = mmc0_1_pins,
614*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(mmc0_1_pins),
615*4882a593Smuzhiyun		},
616*4882a593Smuzhiyun		{
617*4882a593Smuzhiyun			.name = "mmc0_2_grp",
618*4882a593Smuzhiyun			.pins = mmc0_2_pins,
619*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(mmc0_2_pins),
620*4882a593Smuzhiyun		},
621*4882a593Smuzhiyun		{
622*4882a593Smuzhiyun			.name = "mmc0_3_grp",
623*4882a593Smuzhiyun			.pins = mmc0_3_pins,
624*4882a593Smuzhiyun			.num_pins = ARRAY_SIZE(mmc0_3_pins),
625*4882a593Smuzhiyun		},
626*4882a593Smuzhiyun	};
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun	static int foo_get_groups_count(struct pinctrl_dev *pctldev)
630*4882a593Smuzhiyun	{
631*4882a593Smuzhiyun		return ARRAY_SIZE(foo_groups);
632*4882a593Smuzhiyun	}
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun	static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
635*4882a593Smuzhiyun					unsigned selector)
636*4882a593Smuzhiyun	{
637*4882a593Smuzhiyun		return foo_groups[selector].name;
638*4882a593Smuzhiyun	}
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun	static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
641*4882a593Smuzhiyun				const unsigned ** pins,
642*4882a593Smuzhiyun				unsigned * num_pins)
643*4882a593Smuzhiyun	{
644*4882a593Smuzhiyun		*pins = (unsigned *) foo_groups[selector].pins;
645*4882a593Smuzhiyun		*num_pins = foo_groups[selector].num_pins;
646*4882a593Smuzhiyun		return 0;
647*4882a593Smuzhiyun	}
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun	static struct pinctrl_ops foo_pctrl_ops = {
650*4882a593Smuzhiyun		.get_groups_count = foo_get_groups_count,
651*4882a593Smuzhiyun		.get_group_name = foo_get_group_name,
652*4882a593Smuzhiyun		.get_group_pins = foo_get_group_pins,
653*4882a593Smuzhiyun	};
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun	struct foo_pmx_func {
656*4882a593Smuzhiyun		const char *name;
657*4882a593Smuzhiyun		const char * const *groups;
658*4882a593Smuzhiyun		const unsigned num_groups;
659*4882a593Smuzhiyun	};
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun	static const char * const spi0_groups[] = { "spi0_0_grp", "spi0_1_grp" };
662*4882a593Smuzhiyun	static const char * const i2c0_groups[] = { "i2c0_grp" };
663*4882a593Smuzhiyun	static const char * const mmc0_groups[] = { "mmc0_1_grp", "mmc0_2_grp",
664*4882a593Smuzhiyun						"mmc0_3_grp" };
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun	static const struct foo_pmx_func foo_functions[] = {
667*4882a593Smuzhiyun		{
668*4882a593Smuzhiyun			.name = "spi0",
669*4882a593Smuzhiyun			.groups = spi0_groups,
670*4882a593Smuzhiyun			.num_groups = ARRAY_SIZE(spi0_groups),
671*4882a593Smuzhiyun		},
672*4882a593Smuzhiyun		{
673*4882a593Smuzhiyun			.name = "i2c0",
674*4882a593Smuzhiyun			.groups = i2c0_groups,
675*4882a593Smuzhiyun			.num_groups = ARRAY_SIZE(i2c0_groups),
676*4882a593Smuzhiyun		},
677*4882a593Smuzhiyun		{
678*4882a593Smuzhiyun			.name = "mmc0",
679*4882a593Smuzhiyun			.groups = mmc0_groups,
680*4882a593Smuzhiyun			.num_groups = ARRAY_SIZE(mmc0_groups),
681*4882a593Smuzhiyun		},
682*4882a593Smuzhiyun	};
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun	static int foo_get_functions_count(struct pinctrl_dev *pctldev)
685*4882a593Smuzhiyun	{
686*4882a593Smuzhiyun		return ARRAY_SIZE(foo_functions);
687*4882a593Smuzhiyun	}
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun	static const char *foo_get_fname(struct pinctrl_dev *pctldev, unsigned selector)
690*4882a593Smuzhiyun	{
691*4882a593Smuzhiyun		return foo_functions[selector].name;
692*4882a593Smuzhiyun	}
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun	static int foo_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
695*4882a593Smuzhiyun				const char * const **groups,
696*4882a593Smuzhiyun				unsigned * const num_groups)
697*4882a593Smuzhiyun	{
698*4882a593Smuzhiyun		*groups = foo_functions[selector].groups;
699*4882a593Smuzhiyun		*num_groups = foo_functions[selector].num_groups;
700*4882a593Smuzhiyun		return 0;
701*4882a593Smuzhiyun	}
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun	static int foo_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
704*4882a593Smuzhiyun			unsigned group)
705*4882a593Smuzhiyun	{
706*4882a593Smuzhiyun		u8 regbit = (1 << selector + group);
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun		writeb((readb(MUX)|regbit), MUX);
709*4882a593Smuzhiyun		return 0;
710*4882a593Smuzhiyun	}
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun	static struct pinmux_ops foo_pmxops = {
713*4882a593Smuzhiyun		.get_functions_count = foo_get_functions_count,
714*4882a593Smuzhiyun		.get_function_name = foo_get_fname,
715*4882a593Smuzhiyun		.get_function_groups = foo_get_groups,
716*4882a593Smuzhiyun		.set_mux = foo_set_mux,
717*4882a593Smuzhiyun		.strict = true,
718*4882a593Smuzhiyun	};
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun	/* Pinmux operations are handled by some pin controller */
721*4882a593Smuzhiyun	static struct pinctrl_desc foo_desc = {
722*4882a593Smuzhiyun		...
723*4882a593Smuzhiyun		.pctlops = &foo_pctrl_ops,
724*4882a593Smuzhiyun		.pmxops = &foo_pmxops,
725*4882a593Smuzhiyun	};
726*4882a593Smuzhiyun
727*4882a593SmuzhiyunIn the example activating muxing 0 and 1 at the same time setting bits
728*4882a593Smuzhiyun0 and 1, uses one pin in common so they would collide.
729*4882a593Smuzhiyun
730*4882a593SmuzhiyunThe beauty of the pinmux subsystem is that since it keeps track of all
731*4882a593Smuzhiyunpins and who is using them, it will already have denied an impossible
732*4882a593Smuzhiyunrequest like that, so the driver does not need to worry about such
733*4882a593Smuzhiyunthings - when it gets a selector passed in, the pinmux subsystem makes
734*4882a593Smuzhiyunsure no other device or GPIO assignment is already using the selected
735*4882a593Smuzhiyunpins. Thus bits 0 and 1 in the control register will never be set at the
736*4882a593Smuzhiyunsame time.
737*4882a593Smuzhiyun
738*4882a593SmuzhiyunAll the above functions are mandatory to implement for a pinmux driver.
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun
741*4882a593SmuzhiyunPin control interaction with the GPIO subsystem
742*4882a593Smuzhiyun===============================================
743*4882a593Smuzhiyun
744*4882a593SmuzhiyunNote that the following implies that the use case is to use a certain pin
745*4882a593Smuzhiyunfrom the Linux kernel using the API in <linux/gpio.h> with gpio_request()
746*4882a593Smuzhiyunand similar functions. There are cases where you may be using something
747*4882a593Smuzhiyunthat your datasheet calls "GPIO mode", but actually is just an electrical
748*4882a593Smuzhiyunconfiguration for a certain device. See the section below named
749*4882a593Smuzhiyun"GPIO mode pitfalls" for more details on this scenario.
750*4882a593Smuzhiyun
751*4882a593SmuzhiyunThe public pinmux API contains two functions named pinctrl_gpio_request()
752*4882a593Smuzhiyunand pinctrl_gpio_free(). These two functions shall *ONLY* be called from
753*4882a593Smuzhiyungpiolib-based drivers as part of their gpio_request() and
754*4882a593Smuzhiyungpio_free() semantics. Likewise the pinctrl_gpio_direction_[input|output]
755*4882a593Smuzhiyunshall only be called from within respective gpio_direction_[input|output]
756*4882a593Smuzhiyungpiolib implementation.
757*4882a593Smuzhiyun
758*4882a593SmuzhiyunNOTE that platforms and individual drivers shall *NOT* request GPIO pins to be
759*4882a593Smuzhiyuncontrolled e.g. muxed in. Instead, implement a proper gpiolib driver and have
760*4882a593Smuzhiyunthat driver request proper muxing and other control for its pins.
761*4882a593Smuzhiyun
762*4882a593SmuzhiyunThe function list could become long, especially if you can convert every
763*4882a593Smuzhiyunindividual pin into a GPIO pin independent of any other pins, and then try
764*4882a593Smuzhiyunthe approach to define every pin as a function.
765*4882a593Smuzhiyun
766*4882a593SmuzhiyunIn this case, the function array would become 64 entries for each GPIO
767*4882a593Smuzhiyunsetting and then the device functions.
768*4882a593Smuzhiyun
769*4882a593SmuzhiyunFor this reason there are two functions a pin control driver can implement
770*4882a593Smuzhiyunto enable only GPIO on an individual pin: .gpio_request_enable() and
771*4882a593Smuzhiyun.gpio_disable_free().
772*4882a593Smuzhiyun
773*4882a593SmuzhiyunThis function will pass in the affected GPIO range identified by the pin
774*4882a593Smuzhiyuncontroller core, so you know which GPIO pins are being affected by the request
775*4882a593Smuzhiyunoperation.
776*4882a593Smuzhiyun
777*4882a593SmuzhiyunIf your driver needs to have an indication from the framework of whether the
778*4882a593SmuzhiyunGPIO pin shall be used for input or output you can implement the
779*4882a593Smuzhiyun.gpio_set_direction() function. As described this shall be called from the
780*4882a593Smuzhiyungpiolib driver and the affected GPIO range, pin offset and desired direction
781*4882a593Smuzhiyunwill be passed along to this function.
782*4882a593Smuzhiyun
783*4882a593SmuzhiyunAlternatively to using these special functions, it is fully allowed to use
784*4882a593Smuzhiyunnamed functions for each GPIO pin, the pinctrl_gpio_request() will attempt to
785*4882a593Smuzhiyunobtain the function "gpioN" where "N" is the global GPIO pin number if no
786*4882a593Smuzhiyunspecial GPIO-handler is registered.
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun
789*4882a593SmuzhiyunGPIO mode pitfalls
790*4882a593Smuzhiyun==================
791*4882a593Smuzhiyun
792*4882a593SmuzhiyunDue to the naming conventions used by hardware engineers, where "GPIO"
793*4882a593Smuzhiyunis taken to mean different things than what the kernel does, the developer
794*4882a593Smuzhiyunmay be confused by a datasheet talking about a pin being possible to set
795*4882a593Smuzhiyuninto "GPIO mode". It appears that what hardware engineers mean with
796*4882a593Smuzhiyun"GPIO mode" is not necessarily the use case that is implied in the kernel
797*4882a593Smuzhiyuninterface <linux/gpio.h>: a pin that you grab from kernel code and then
798*4882a593Smuzhiyuneither listen for input or drive high/low to assert/deassert some
799*4882a593Smuzhiyunexternal line.
800*4882a593Smuzhiyun
801*4882a593SmuzhiyunRather hardware engineers think that "GPIO mode" means that you can
802*4882a593Smuzhiyunsoftware-control a few electrical properties of the pin that you would
803*4882a593Smuzhiyunnot be able to control if the pin was in some other mode, such as muxed in
804*4882a593Smuzhiyunfor a device.
805*4882a593Smuzhiyun
806*4882a593SmuzhiyunThe GPIO portions of a pin and its relation to a certain pin controller
807*4882a593Smuzhiyunconfiguration and muxing logic can be constructed in several ways. Here
808*4882a593Smuzhiyunare two examples::
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun     (A)
811*4882a593Smuzhiyun                       pin config
812*4882a593Smuzhiyun                       logic regs
813*4882a593Smuzhiyun                       |               +- SPI
814*4882a593Smuzhiyun     Physical pins --- pad --- pinmux -+- I2C
815*4882a593Smuzhiyun                               |       +- mmc
816*4882a593Smuzhiyun                               |       +- GPIO
817*4882a593Smuzhiyun                               pin
818*4882a593Smuzhiyun                               multiplex
819*4882a593Smuzhiyun                               logic regs
820*4882a593Smuzhiyun
821*4882a593SmuzhiyunHere some electrical properties of the pin can be configured no matter
822*4882a593Smuzhiyunwhether the pin is used for GPIO or not. If you multiplex a GPIO onto a
823*4882a593Smuzhiyunpin, you can also drive it high/low from "GPIO" registers.
824*4882a593SmuzhiyunAlternatively, the pin can be controlled by a certain peripheral, while
825*4882a593Smuzhiyunstill applying desired pin config properties. GPIO functionality is thus
826*4882a593Smuzhiyunorthogonal to any other device using the pin.
827*4882a593Smuzhiyun
828*4882a593SmuzhiyunIn this arrangement the registers for the GPIO portions of the pin controller,
829*4882a593Smuzhiyunor the registers for the GPIO hardware module are likely to reside in a
830*4882a593Smuzhiyunseparate memory range only intended for GPIO driving, and the register
831*4882a593Smuzhiyunrange dealing with pin config and pin multiplexing get placed into a
832*4882a593Smuzhiyundifferent memory range and a separate section of the data sheet.
833*4882a593Smuzhiyun
834*4882a593SmuzhiyunA flag "strict" in struct pinmux_ops is available to check and deny
835*4882a593Smuzhiyunsimultaneous access to the same pin from GPIO and pin multiplexing
836*4882a593Smuzhiyunconsumers on hardware of this type. The pinctrl driver should set this flag
837*4882a593Smuzhiyunaccordingly.
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun::
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun     (B)
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun                       pin config
844*4882a593Smuzhiyun                       logic regs
845*4882a593Smuzhiyun                       |               +- SPI
846*4882a593Smuzhiyun     Physical pins --- pad --- pinmux -+- I2C
847*4882a593Smuzhiyun                       |       |       +- mmc
848*4882a593Smuzhiyun                       |       |
849*4882a593Smuzhiyun                       GPIO    pin
850*4882a593Smuzhiyun                               multiplex
851*4882a593Smuzhiyun                               logic regs
852*4882a593Smuzhiyun
853*4882a593SmuzhiyunIn this arrangement, the GPIO functionality can always be enabled, such that
854*4882a593Smuzhiyune.g. a GPIO input can be used to "spy" on the SPI/I2C/MMC signal while it is
855*4882a593Smuzhiyunpulsed out. It is likely possible to disrupt the traffic on the pin by doing
856*4882a593Smuzhiyunwrong things on the GPIO block, as it is never really disconnected. It is
857*4882a593Smuzhiyunpossible that the GPIO, pin config and pin multiplex registers are placed into
858*4882a593Smuzhiyunthe same memory range and the same section of the data sheet, although that
859*4882a593Smuzhiyunneed not be the case.
860*4882a593Smuzhiyun
861*4882a593SmuzhiyunIn some pin controllers, although the physical pins are designed in the same
862*4882a593Smuzhiyunway as (B), the GPIO function still can't be enabled at the same time as the
863*4882a593Smuzhiyunperipheral functions. So again the "strict" flag should be set, denying
864*4882a593Smuzhiyunsimultaneous activation by GPIO and other muxed in devices.
865*4882a593Smuzhiyun
866*4882a593SmuzhiyunFrom a kernel point of view, however, these are different aspects of the
867*4882a593Smuzhiyunhardware and shall be put into different subsystems:
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun- Registers (or fields within registers) that control electrical
870*4882a593Smuzhiyun  properties of the pin such as biasing and drive strength should be
871*4882a593Smuzhiyun  exposed through the pinctrl subsystem, as "pin configuration" settings.
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun- Registers (or fields within registers) that control muxing of signals
874*4882a593Smuzhiyun  from various other HW blocks (e.g. I2C, MMC, or GPIO) onto pins should
875*4882a593Smuzhiyun  be exposed through the pinctrl subsystem, as mux functions.
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun- Registers (or fields within registers) that control GPIO functionality
878*4882a593Smuzhiyun  such as setting a GPIO's output value, reading a GPIO's input value, or
879*4882a593Smuzhiyun  setting GPIO pin direction should be exposed through the GPIO subsystem,
880*4882a593Smuzhiyun  and if they also support interrupt capabilities, through the irqchip
881*4882a593Smuzhiyun  abstraction.
882*4882a593Smuzhiyun
883*4882a593SmuzhiyunDepending on the exact HW register design, some functions exposed by the
884*4882a593SmuzhiyunGPIO subsystem may call into the pinctrl subsystem in order to
885*4882a593Smuzhiyunco-ordinate register settings across HW modules. In particular, this may
886*4882a593Smuzhiyunbe needed for HW with separate GPIO and pin controller HW modules, where
887*4882a593Smuzhiyune.g. GPIO direction is determined by a register in the pin controller HW
888*4882a593Smuzhiyunmodule rather than the GPIO HW module.
889*4882a593Smuzhiyun
890*4882a593SmuzhiyunElectrical properties of the pin such as biasing and drive strength
891*4882a593Smuzhiyunmay be placed at some pin-specific register in all cases or as part
892*4882a593Smuzhiyunof the GPIO register in case (B) especially. This doesn't mean that such
893*4882a593Smuzhiyunproperties necessarily pertain to what the Linux kernel calls "GPIO".
894*4882a593Smuzhiyun
895*4882a593SmuzhiyunExample: a pin is usually muxed in to be used as a UART TX line. But during
896*4882a593Smuzhiyunsystem sleep, we need to put this pin into "GPIO mode" and ground it.
897*4882a593Smuzhiyun
898*4882a593SmuzhiyunIf you make a 1-to-1 map to the GPIO subsystem for this pin, you may start
899*4882a593Smuzhiyunto think that you need to come up with something really complex, that the
900*4882a593Smuzhiyunpin shall be used for UART TX and GPIO at the same time, that you will grab
901*4882a593Smuzhiyuna pin control handle and set it to a certain state to enable UART TX to be
902*4882a593Smuzhiyunmuxed in, then twist it over to GPIO mode and use gpio_direction_output()
903*4882a593Smuzhiyunto drive it low during sleep, then mux it over to UART TX again when you
904*4882a593Smuzhiyunwake up and maybe even gpio_request/gpio_free as part of this cycle. This
905*4882a593Smuzhiyunall gets very complicated.
906*4882a593Smuzhiyun
907*4882a593SmuzhiyunThe solution is to not think that what the datasheet calls "GPIO mode"
908*4882a593Smuzhiyunhas to be handled by the <linux/gpio.h> interface. Instead view this as
909*4882a593Smuzhiyuna certain pin config setting. Look in e.g. <linux/pinctrl/pinconf-generic.h>
910*4882a593Smuzhiyunand you find this in the documentation:
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun  PIN_CONFIG_OUTPUT:
913*4882a593Smuzhiyun     this will configure the pin in output, use argument
914*4882a593Smuzhiyun     1 to indicate high level, argument 0 to indicate low level.
915*4882a593Smuzhiyun
916*4882a593SmuzhiyunSo it is perfectly possible to push a pin into "GPIO mode" and drive the
917*4882a593Smuzhiyunline low as part of the usual pin control map. So for example your UART
918*4882a593Smuzhiyundriver may look like this::
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun	#include <linux/pinctrl/consumer.h>
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun	struct pinctrl          *pinctrl;
923*4882a593Smuzhiyun	struct pinctrl_state    *pins_default;
924*4882a593Smuzhiyun	struct pinctrl_state    *pins_sleep;
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun	pins_default = pinctrl_lookup_state(uap->pinctrl, PINCTRL_STATE_DEFAULT);
927*4882a593Smuzhiyun	pins_sleep = pinctrl_lookup_state(uap->pinctrl, PINCTRL_STATE_SLEEP);
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun	/* Normal mode */
930*4882a593Smuzhiyun	retval = pinctrl_select_state(pinctrl, pins_default);
931*4882a593Smuzhiyun	/* Sleep mode */
932*4882a593Smuzhiyun	retval = pinctrl_select_state(pinctrl, pins_sleep);
933*4882a593Smuzhiyun
934*4882a593SmuzhiyunAnd your machine configuration may look like this:
935*4882a593Smuzhiyun--------------------------------------------------
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun::
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun	static unsigned long uart_default_mode[] = {
940*4882a593Smuzhiyun		PIN_CONF_PACKED(PIN_CONFIG_DRIVE_PUSH_PULL, 0),
941*4882a593Smuzhiyun	};
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun	static unsigned long uart_sleep_mode[] = {
944*4882a593Smuzhiyun		PIN_CONF_PACKED(PIN_CONFIG_OUTPUT, 0),
945*4882a593Smuzhiyun	};
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun	static struct pinctrl_map pinmap[] __initdata = {
948*4882a593Smuzhiyun		PIN_MAP_MUX_GROUP("uart", PINCTRL_STATE_DEFAULT, "pinctrl-foo",
949*4882a593Smuzhiyun			"u0_group", "u0"),
950*4882a593Smuzhiyun		PIN_MAP_CONFIGS_PIN("uart", PINCTRL_STATE_DEFAULT, "pinctrl-foo",
951*4882a593Smuzhiyun				"UART_TX_PIN", uart_default_mode),
952*4882a593Smuzhiyun		PIN_MAP_MUX_GROUP("uart", PINCTRL_STATE_SLEEP, "pinctrl-foo",
953*4882a593Smuzhiyun			"u0_group", "gpio-mode"),
954*4882a593Smuzhiyun		PIN_MAP_CONFIGS_PIN("uart", PINCTRL_STATE_SLEEP, "pinctrl-foo",
955*4882a593Smuzhiyun				"UART_TX_PIN", uart_sleep_mode),
956*4882a593Smuzhiyun	};
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun	foo_init(void) {
959*4882a593Smuzhiyun		pinctrl_register_mappings(pinmap, ARRAY_SIZE(pinmap));
960*4882a593Smuzhiyun	}
961*4882a593Smuzhiyun
962*4882a593SmuzhiyunHere the pins we want to control are in the "u0_group" and there is some
963*4882a593Smuzhiyunfunction called "u0" that can be enabled on this group of pins, and then
964*4882a593Smuzhiyuneverything is UART business as usual. But there is also some function
965*4882a593Smuzhiyunnamed "gpio-mode" that can be mapped onto the same pins to move them into
966*4882a593SmuzhiyunGPIO mode.
967*4882a593Smuzhiyun
968*4882a593SmuzhiyunThis will give the desired effect without any bogus interaction with the
969*4882a593SmuzhiyunGPIO subsystem. It is just an electrical configuration used by that device
970*4882a593Smuzhiyunwhen going to sleep, it might imply that the pin is set into something the
971*4882a593Smuzhiyundatasheet calls "GPIO mode", but that is not the point: it is still used
972*4882a593Smuzhiyunby that UART device to control the pins that pertain to that very UART
973*4882a593Smuzhiyundriver, putting them into modes needed by the UART. GPIO in the Linux
974*4882a593Smuzhiyunkernel sense are just some 1-bit line, and is a different use case.
975*4882a593Smuzhiyun
976*4882a593SmuzhiyunHow the registers are poked to attain the push or pull, and output low
977*4882a593Smuzhiyunconfiguration and the muxing of the "u0" or "gpio-mode" group onto these
978*4882a593Smuzhiyunpins is a question for the driver.
979*4882a593Smuzhiyun
980*4882a593SmuzhiyunSome datasheets will be more helpful and refer to the "GPIO mode" as
981*4882a593Smuzhiyun"low power mode" rather than anything to do with GPIO. This often means
982*4882a593Smuzhiyunthe same thing electrically speaking, but in this latter case the
983*4882a593Smuzhiyunsoftware engineers will usually quickly identify that this is some
984*4882a593Smuzhiyunspecific muxing or configuration rather than anything related to the GPIO
985*4882a593SmuzhiyunAPI.
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun
988*4882a593SmuzhiyunBoard/machine configuration
989*4882a593Smuzhiyun===========================
990*4882a593Smuzhiyun
991*4882a593SmuzhiyunBoards and machines define how a certain complete running system is put
992*4882a593Smuzhiyuntogether, including how GPIOs and devices are muxed, how regulators are
993*4882a593Smuzhiyunconstrained and how the clock tree looks. Of course pinmux settings are also
994*4882a593Smuzhiyunpart of this.
995*4882a593Smuzhiyun
996*4882a593SmuzhiyunA pin controller configuration for a machine looks pretty much like a simple
997*4882a593Smuzhiyunregulator configuration, so for the example array above we want to enable i2c
998*4882a593Smuzhiyunand spi on the second function mapping::
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun	#include <linux/pinctrl/machine.h>
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun	static const struct pinctrl_map mapping[] __initconst = {
1003*4882a593Smuzhiyun		{
1004*4882a593Smuzhiyun			.dev_name = "foo-spi.0",
1005*4882a593Smuzhiyun			.name = PINCTRL_STATE_DEFAULT,
1006*4882a593Smuzhiyun			.type = PIN_MAP_TYPE_MUX_GROUP,
1007*4882a593Smuzhiyun			.ctrl_dev_name = "pinctrl-foo",
1008*4882a593Smuzhiyun			.data.mux.function = "spi0",
1009*4882a593Smuzhiyun		},
1010*4882a593Smuzhiyun		{
1011*4882a593Smuzhiyun			.dev_name = "foo-i2c.0",
1012*4882a593Smuzhiyun			.name = PINCTRL_STATE_DEFAULT,
1013*4882a593Smuzhiyun			.type = PIN_MAP_TYPE_MUX_GROUP,
1014*4882a593Smuzhiyun			.ctrl_dev_name = "pinctrl-foo",
1015*4882a593Smuzhiyun			.data.mux.function = "i2c0",
1016*4882a593Smuzhiyun		},
1017*4882a593Smuzhiyun		{
1018*4882a593Smuzhiyun			.dev_name = "foo-mmc.0",
1019*4882a593Smuzhiyun			.name = PINCTRL_STATE_DEFAULT,
1020*4882a593Smuzhiyun			.type = PIN_MAP_TYPE_MUX_GROUP,
1021*4882a593Smuzhiyun			.ctrl_dev_name = "pinctrl-foo",
1022*4882a593Smuzhiyun			.data.mux.function = "mmc0",
1023*4882a593Smuzhiyun		},
1024*4882a593Smuzhiyun	};
1025*4882a593Smuzhiyun
1026*4882a593SmuzhiyunThe dev_name here matches to the unique device name that can be used to look
1027*4882a593Smuzhiyunup the device struct (just like with clockdev or regulators). The function name
1028*4882a593Smuzhiyunmust match a function provided by the pinmux driver handling this pin range.
1029*4882a593Smuzhiyun
1030*4882a593SmuzhiyunAs you can see we may have several pin controllers on the system and thus
1031*4882a593Smuzhiyunwe need to specify which one of them contains the functions we wish to map.
1032*4882a593Smuzhiyun
1033*4882a593SmuzhiyunYou register this pinmux mapping to the pinmux subsystem by simply::
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun       ret = pinctrl_register_mappings(mapping, ARRAY_SIZE(mapping));
1036*4882a593Smuzhiyun
1037*4882a593SmuzhiyunSince the above construct is pretty common there is a helper macro to make
1038*4882a593Smuzhiyunit even more compact which assumes you want to use pinctrl-foo and position
1039*4882a593Smuzhiyun0 for mapping, for example::
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun	static struct pinctrl_map mapping[] __initdata = {
1042*4882a593Smuzhiyun		PIN_MAP_MUX_GROUP("foo-i2c.o", PINCTRL_STATE_DEFAULT,
1043*4882a593Smuzhiyun				  "pinctrl-foo", NULL, "i2c0"),
1044*4882a593Smuzhiyun	};
1045*4882a593Smuzhiyun
1046*4882a593SmuzhiyunThe mapping table may also contain pin configuration entries. It's common for
1047*4882a593Smuzhiyuneach pin/group to have a number of configuration entries that affect it, so
1048*4882a593Smuzhiyunthe table entries for configuration reference an array of config parameters
1049*4882a593Smuzhiyunand values. An example using the convenience macros is shown below::
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun	static unsigned long i2c_grp_configs[] = {
1052*4882a593Smuzhiyun		FOO_PIN_DRIVEN,
1053*4882a593Smuzhiyun		FOO_PIN_PULLUP,
1054*4882a593Smuzhiyun	};
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun	static unsigned long i2c_pin_configs[] = {
1057*4882a593Smuzhiyun		FOO_OPEN_COLLECTOR,
1058*4882a593Smuzhiyun		FOO_SLEW_RATE_SLOW,
1059*4882a593Smuzhiyun	};
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun	static struct pinctrl_map mapping[] __initdata = {
1062*4882a593Smuzhiyun		PIN_MAP_MUX_GROUP("foo-i2c.0", PINCTRL_STATE_DEFAULT,
1063*4882a593Smuzhiyun				  "pinctrl-foo", "i2c0", "i2c0"),
1064*4882a593Smuzhiyun		PIN_MAP_CONFIGS_GROUP("foo-i2c.0", PINCTRL_STATE_DEFAULT,
1065*4882a593Smuzhiyun				      "pinctrl-foo", "i2c0", i2c_grp_configs),
1066*4882a593Smuzhiyun		PIN_MAP_CONFIGS_PIN("foo-i2c.0", PINCTRL_STATE_DEFAULT,
1067*4882a593Smuzhiyun				    "pinctrl-foo", "i2c0scl", i2c_pin_configs),
1068*4882a593Smuzhiyun		PIN_MAP_CONFIGS_PIN("foo-i2c.0", PINCTRL_STATE_DEFAULT,
1069*4882a593Smuzhiyun				    "pinctrl-foo", "i2c0sda", i2c_pin_configs),
1070*4882a593Smuzhiyun	};
1071*4882a593Smuzhiyun
1072*4882a593SmuzhiyunFinally, some devices expect the mapping table to contain certain specific
1073*4882a593Smuzhiyunnamed states. When running on hardware that doesn't need any pin controller
1074*4882a593Smuzhiyunconfiguration, the mapping table must still contain those named states, in
1075*4882a593Smuzhiyunorder to explicitly indicate that the states were provided and intended to
1076*4882a593Smuzhiyunbe empty. Table entry macro PIN_MAP_DUMMY_STATE serves the purpose of defining
1077*4882a593Smuzhiyuna named state without causing any pin controller to be programmed::
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun	static struct pinctrl_map mapping[] __initdata = {
1080*4882a593Smuzhiyun		PIN_MAP_DUMMY_STATE("foo-i2c.0", PINCTRL_STATE_DEFAULT),
1081*4882a593Smuzhiyun	};
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun
1084*4882a593SmuzhiyunComplex mappings
1085*4882a593Smuzhiyun================
1086*4882a593Smuzhiyun
1087*4882a593SmuzhiyunAs it is possible to map a function to different groups of pins an optional
1088*4882a593Smuzhiyun.group can be specified like this::
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun	...
1091*4882a593Smuzhiyun	{
1092*4882a593Smuzhiyun		.dev_name = "foo-spi.0",
1093*4882a593Smuzhiyun		.name = "spi0-pos-A",
1094*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1095*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1096*4882a593Smuzhiyun		.function = "spi0",
1097*4882a593Smuzhiyun		.group = "spi0_0_grp",
1098*4882a593Smuzhiyun	},
1099*4882a593Smuzhiyun	{
1100*4882a593Smuzhiyun		.dev_name = "foo-spi.0",
1101*4882a593Smuzhiyun		.name = "spi0-pos-B",
1102*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1103*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1104*4882a593Smuzhiyun		.function = "spi0",
1105*4882a593Smuzhiyun		.group = "spi0_1_grp",
1106*4882a593Smuzhiyun	},
1107*4882a593Smuzhiyun	...
1108*4882a593Smuzhiyun
1109*4882a593SmuzhiyunThis example mapping is used to switch between two positions for spi0 at
1110*4882a593Smuzhiyunruntime, as described further below under the heading "Runtime pinmuxing".
1111*4882a593Smuzhiyun
1112*4882a593SmuzhiyunFurther it is possible for one named state to affect the muxing of several
1113*4882a593Smuzhiyungroups of pins, say for example in the mmc0 example above, where you can
1114*4882a593Smuzhiyunadditively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all
1115*4882a593Smuzhiyunthree groups for a total of 2+2+4 = 8 pins (for an 8-bit MMC bus as is the
1116*4882a593Smuzhiyuncase), we define a mapping like this::
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun	...
1119*4882a593Smuzhiyun	{
1120*4882a593Smuzhiyun		.dev_name = "foo-mmc.0",
1121*4882a593Smuzhiyun		.name = "2bit"
1122*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1123*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1124*4882a593Smuzhiyun		.function = "mmc0",
1125*4882a593Smuzhiyun		.group = "mmc0_1_grp",
1126*4882a593Smuzhiyun	},
1127*4882a593Smuzhiyun	{
1128*4882a593Smuzhiyun		.dev_name = "foo-mmc.0",
1129*4882a593Smuzhiyun		.name = "4bit"
1130*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1131*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1132*4882a593Smuzhiyun		.function = "mmc0",
1133*4882a593Smuzhiyun		.group = "mmc0_1_grp",
1134*4882a593Smuzhiyun	},
1135*4882a593Smuzhiyun	{
1136*4882a593Smuzhiyun		.dev_name = "foo-mmc.0",
1137*4882a593Smuzhiyun		.name = "4bit"
1138*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1139*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1140*4882a593Smuzhiyun		.function = "mmc0",
1141*4882a593Smuzhiyun		.group = "mmc0_2_grp",
1142*4882a593Smuzhiyun	},
1143*4882a593Smuzhiyun	{
1144*4882a593Smuzhiyun		.dev_name = "foo-mmc.0",
1145*4882a593Smuzhiyun		.name = "8bit"
1146*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1147*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1148*4882a593Smuzhiyun		.function = "mmc0",
1149*4882a593Smuzhiyun		.group = "mmc0_1_grp",
1150*4882a593Smuzhiyun	},
1151*4882a593Smuzhiyun	{
1152*4882a593Smuzhiyun		.dev_name = "foo-mmc.0",
1153*4882a593Smuzhiyun		.name = "8bit"
1154*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1155*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1156*4882a593Smuzhiyun		.function = "mmc0",
1157*4882a593Smuzhiyun		.group = "mmc0_2_grp",
1158*4882a593Smuzhiyun	},
1159*4882a593Smuzhiyun	{
1160*4882a593Smuzhiyun		.dev_name = "foo-mmc.0",
1161*4882a593Smuzhiyun		.name = "8bit"
1162*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1163*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1164*4882a593Smuzhiyun		.function = "mmc0",
1165*4882a593Smuzhiyun		.group = "mmc0_3_grp",
1166*4882a593Smuzhiyun	},
1167*4882a593Smuzhiyun	...
1168*4882a593Smuzhiyun
1169*4882a593SmuzhiyunThe result of grabbing this mapping from the device with something like
1170*4882a593Smuzhiyunthis (see next paragraph)::
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun	p = devm_pinctrl_get(dev);
1173*4882a593Smuzhiyun	s = pinctrl_lookup_state(p, "8bit");
1174*4882a593Smuzhiyun	ret = pinctrl_select_state(p, s);
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyunor more simply::
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun	p = devm_pinctrl_get_select(dev, "8bit");
1179*4882a593Smuzhiyun
1180*4882a593SmuzhiyunWill be that you activate all the three bottom records in the mapping at
1181*4882a593Smuzhiyunonce. Since they share the same name, pin controller device, function and
1182*4882a593Smuzhiyundevice, and since we allow multiple groups to match to a single device, they
1183*4882a593Smuzhiyunall get selected, and they all get enabled and disable simultaneously by the
1184*4882a593Smuzhiyunpinmux core.
1185*4882a593Smuzhiyun
1186*4882a593Smuzhiyun
1187*4882a593SmuzhiyunPin control requests from drivers
1188*4882a593Smuzhiyun=================================
1189*4882a593Smuzhiyun
1190*4882a593SmuzhiyunWhen a device driver is about to probe the device core will automatically
1191*4882a593Smuzhiyunattempt to issue pinctrl_get_select_default() on these devices.
1192*4882a593SmuzhiyunThis way driver writers do not need to add any of the boilerplate code
1193*4882a593Smuzhiyunof the type found below. However when doing fine-grained state selection
1194*4882a593Smuzhiyunand not using the "default" state, you may have to do some device driver
1195*4882a593Smuzhiyunhandling of the pinctrl handles and states.
1196*4882a593Smuzhiyun
1197*4882a593SmuzhiyunSo if you just want to put the pins for a certain device into the default
1198*4882a593Smuzhiyunstate and be done with it, there is nothing you need to do besides
1199*4882a593Smuzhiyunproviding the proper mapping table. The device core will take care of
1200*4882a593Smuzhiyunthe rest.
1201*4882a593Smuzhiyun
1202*4882a593SmuzhiyunGenerally it is discouraged to let individual drivers get and enable pin
1203*4882a593Smuzhiyuncontrol. So if possible, handle the pin control in platform code or some other
1204*4882a593Smuzhiyunplace where you have access to all the affected struct device * pointers. In
1205*4882a593Smuzhiyunsome cases where a driver needs to e.g. switch between different mux mappings
1206*4882a593Smuzhiyunat runtime this is not possible.
1207*4882a593Smuzhiyun
1208*4882a593SmuzhiyunA typical case is if a driver needs to switch bias of pins from normal
1209*4882a593Smuzhiyunoperation and going to sleep, moving from the PINCTRL_STATE_DEFAULT to
1210*4882a593SmuzhiyunPINCTRL_STATE_SLEEP at runtime, re-biasing or even re-muxing pins to save
1211*4882a593Smuzhiyuncurrent in sleep mode.
1212*4882a593Smuzhiyun
1213*4882a593SmuzhiyunA driver may request a certain control state to be activated, usually just the
1214*4882a593Smuzhiyundefault state like this::
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun	#include <linux/pinctrl/consumer.h>
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun	struct foo_state {
1219*4882a593Smuzhiyun	struct pinctrl *p;
1220*4882a593Smuzhiyun	struct pinctrl_state *s;
1221*4882a593Smuzhiyun	...
1222*4882a593Smuzhiyun	};
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun	foo_probe()
1225*4882a593Smuzhiyun	{
1226*4882a593Smuzhiyun		/* Allocate a state holder named "foo" etc */
1227*4882a593Smuzhiyun		struct foo_state *foo = ...;
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun		foo->p = devm_pinctrl_get(&device);
1230*4882a593Smuzhiyun		if (IS_ERR(foo->p)) {
1231*4882a593Smuzhiyun			/* FIXME: clean up "foo" here */
1232*4882a593Smuzhiyun			return PTR_ERR(foo->p);
1233*4882a593Smuzhiyun		}
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun		foo->s = pinctrl_lookup_state(foo->p, PINCTRL_STATE_DEFAULT);
1236*4882a593Smuzhiyun		if (IS_ERR(foo->s)) {
1237*4882a593Smuzhiyun			/* FIXME: clean up "foo" here */
1238*4882a593Smuzhiyun			return PTR_ERR(s);
1239*4882a593Smuzhiyun		}
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun		ret = pinctrl_select_state(foo->s);
1242*4882a593Smuzhiyun		if (ret < 0) {
1243*4882a593Smuzhiyun			/* FIXME: clean up "foo" here */
1244*4882a593Smuzhiyun			return ret;
1245*4882a593Smuzhiyun		}
1246*4882a593Smuzhiyun	}
1247*4882a593Smuzhiyun
1248*4882a593SmuzhiyunThis get/lookup/select/put sequence can just as well be handled by bus drivers
1249*4882a593Smuzhiyunif you don't want each and every driver to handle it and you know the
1250*4882a593Smuzhiyunarrangement on your bus.
1251*4882a593Smuzhiyun
1252*4882a593SmuzhiyunThe semantics of the pinctrl APIs are:
1253*4882a593Smuzhiyun
1254*4882a593Smuzhiyun- pinctrl_get() is called in process context to obtain a handle to all pinctrl
1255*4882a593Smuzhiyun  information for a given client device. It will allocate a struct from the
1256*4882a593Smuzhiyun  kernel memory to hold the pinmux state. All mapping table parsing or similar
1257*4882a593Smuzhiyun  slow operations take place within this API.
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun- devm_pinctrl_get() is a variant of pinctrl_get() that causes pinctrl_put()
1260*4882a593Smuzhiyun  to be called automatically on the retrieved pointer when the associated
1261*4882a593Smuzhiyun  device is removed. It is recommended to use this function over plain
1262*4882a593Smuzhiyun  pinctrl_get().
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun- pinctrl_lookup_state() is called in process context to obtain a handle to a
1265*4882a593Smuzhiyun  specific state for a client device. This operation may be slow, too.
1266*4882a593Smuzhiyun
1267*4882a593Smuzhiyun- pinctrl_select_state() programs pin controller hardware according to the
1268*4882a593Smuzhiyun  definition of the state as given by the mapping table. In theory, this is a
1269*4882a593Smuzhiyun  fast-path operation, since it only involved blasting some register settings
1270*4882a593Smuzhiyun  into hardware. However, note that some pin controllers may have their
1271*4882a593Smuzhiyun  registers on a slow/IRQ-based bus, so client devices should not assume they
1272*4882a593Smuzhiyun  can call pinctrl_select_state() from non-blocking contexts.
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun- pinctrl_put() frees all information associated with a pinctrl handle.
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun- devm_pinctrl_put() is a variant of pinctrl_put() that may be used to
1277*4882a593Smuzhiyun  explicitly destroy a pinctrl object returned by devm_pinctrl_get().
1278*4882a593Smuzhiyun  However, use of this function will be rare, due to the automatic cleanup
1279*4882a593Smuzhiyun  that will occur even without calling it.
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun  pinctrl_get() must be paired with a plain pinctrl_put().
1282*4882a593Smuzhiyun  pinctrl_get() may not be paired with devm_pinctrl_put().
1283*4882a593Smuzhiyun  devm_pinctrl_get() can optionally be paired with devm_pinctrl_put().
1284*4882a593Smuzhiyun  devm_pinctrl_get() may not be paired with plain pinctrl_put().
1285*4882a593Smuzhiyun
1286*4882a593SmuzhiyunUsually the pin control core handled the get/put pair and call out to the
1287*4882a593Smuzhiyundevice drivers bookkeeping operations, like checking available functions and
1288*4882a593Smuzhiyunthe associated pins, whereas select_state pass on to the pin controller
1289*4882a593Smuzhiyundriver which takes care of activating and/or deactivating the mux setting by
1290*4882a593Smuzhiyunquickly poking some registers.
1291*4882a593Smuzhiyun
1292*4882a593SmuzhiyunThe pins are allocated for your device when you issue the devm_pinctrl_get()
1293*4882a593Smuzhiyuncall, after this you should be able to see this in the debugfs listing of all
1294*4882a593Smuzhiyunpins.
1295*4882a593Smuzhiyun
1296*4882a593SmuzhiyunNOTE: the pinctrl system will return -EPROBE_DEFER if it cannot find the
1297*4882a593Smuzhiyunrequested pinctrl handles, for example if the pinctrl driver has not yet
1298*4882a593Smuzhiyunregistered. Thus make sure that the error path in your driver gracefully
1299*4882a593Smuzhiyuncleans up and is ready to retry the probing later in the startup process.
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun
1302*4882a593SmuzhiyunDrivers needing both pin control and GPIOs
1303*4882a593Smuzhiyun==========================================
1304*4882a593Smuzhiyun
1305*4882a593SmuzhiyunAgain, it is discouraged to let drivers lookup and select pin control states
1306*4882a593Smuzhiyunthemselves, but again sometimes this is unavoidable.
1307*4882a593Smuzhiyun
1308*4882a593SmuzhiyunSo say that your driver is fetching its resources like this::
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun	#include <linux/pinctrl/consumer.h>
1311*4882a593Smuzhiyun	#include <linux/gpio.h>
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun	struct pinctrl *pinctrl;
1314*4882a593Smuzhiyun	int gpio;
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun	pinctrl = devm_pinctrl_get_select_default(&dev);
1317*4882a593Smuzhiyun	gpio = devm_gpio_request(&dev, 14, "foo");
1318*4882a593Smuzhiyun
1319*4882a593SmuzhiyunHere we first request a certain pin state and then request GPIO 14 to be
1320*4882a593Smuzhiyunused. If you're using the subsystems orthogonally like this, you should
1321*4882a593Smuzhiyunnominally always get your pinctrl handle and select the desired pinctrl
1322*4882a593Smuzhiyunstate BEFORE requesting the GPIO. This is a semantic convention to avoid
1323*4882a593Smuzhiyunsituations that can be electrically unpleasant, you will certainly want to
1324*4882a593Smuzhiyunmux in and bias pins in a certain way before the GPIO subsystems starts to
1325*4882a593Smuzhiyundeal with them.
1326*4882a593Smuzhiyun
1327*4882a593SmuzhiyunThe above can be hidden: using the device core, the pinctrl core may be
1328*4882a593Smuzhiyunsetting up the config and muxing for the pins right before the device is
1329*4882a593Smuzhiyunprobing, nevertheless orthogonal to the GPIO subsystem.
1330*4882a593Smuzhiyun
1331*4882a593SmuzhiyunBut there are also situations where it makes sense for the GPIO subsystem
1332*4882a593Smuzhiyunto communicate directly with the pinctrl subsystem, using the latter as a
1333*4882a593Smuzhiyunback-end. This is when the GPIO driver may call out to the functions
1334*4882a593Smuzhiyundescribed in the section "Pin control interaction with the GPIO subsystem"
1335*4882a593Smuzhiyunabove. This only involves per-pin multiplexing, and will be completely
1336*4882a593Smuzhiyunhidden behind the gpio_*() function namespace. In this case, the driver
1337*4882a593Smuzhiyunneed not interact with the pin control subsystem at all.
1338*4882a593Smuzhiyun
1339*4882a593SmuzhiyunIf a pin control driver and a GPIO driver is dealing with the same pins
1340*4882a593Smuzhiyunand the use cases involve multiplexing, you MUST implement the pin controller
1341*4882a593Smuzhiyunas a back-end for the GPIO driver like this, unless your hardware design
1342*4882a593Smuzhiyunis such that the GPIO controller can override the pin controller's
1343*4882a593Smuzhiyunmultiplexing state through hardware without the need to interact with the
1344*4882a593Smuzhiyunpin control system.
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun
1347*4882a593SmuzhiyunSystem pin control hogging
1348*4882a593Smuzhiyun==========================
1349*4882a593Smuzhiyun
1350*4882a593SmuzhiyunPin control map entries can be hogged by the core when the pin controller
1351*4882a593Smuzhiyunis registered. This means that the core will attempt to call pinctrl_get(),
1352*4882a593Smuzhiyunlookup_state() and select_state() on it immediately after the pin control
1353*4882a593Smuzhiyundevice has been registered.
1354*4882a593Smuzhiyun
1355*4882a593SmuzhiyunThis occurs for mapping table entries where the client device name is equal
1356*4882a593Smuzhiyunto the pin controller device name, and the state name is PINCTRL_STATE_DEFAULT::
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun	{
1359*4882a593Smuzhiyun		.dev_name = "pinctrl-foo",
1360*4882a593Smuzhiyun		.name = PINCTRL_STATE_DEFAULT,
1361*4882a593Smuzhiyun		.type = PIN_MAP_TYPE_MUX_GROUP,
1362*4882a593Smuzhiyun		.ctrl_dev_name = "pinctrl-foo",
1363*4882a593Smuzhiyun		.function = "power_func",
1364*4882a593Smuzhiyun	},
1365*4882a593Smuzhiyun
1366*4882a593SmuzhiyunSince it may be common to request the core to hog a few always-applicable
1367*4882a593Smuzhiyunmux settings on the primary pin controller, there is a convenience macro for
1368*4882a593Smuzhiyunthis::
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun	PIN_MAP_MUX_GROUP_HOG_DEFAULT("pinctrl-foo", NULL /* group */,
1371*4882a593Smuzhiyun				      "power_func")
1372*4882a593Smuzhiyun
1373*4882a593SmuzhiyunThis gives the exact same result as the above construction.
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun
1376*4882a593SmuzhiyunRuntime pinmuxing
1377*4882a593Smuzhiyun=================
1378*4882a593Smuzhiyun
1379*4882a593SmuzhiyunIt is possible to mux a certain function in and out at runtime, say to move
1380*4882a593Smuzhiyunan SPI port from one set of pins to another set of pins. Say for example for
1381*4882a593Smuzhiyunspi0 in the example above, we expose two different groups of pins for the same
1382*4882a593Smuzhiyunfunction, but with different named in the mapping as described under
1383*4882a593Smuzhiyun"Advanced mapping" above. So that for an SPI device, we have two states named
1384*4882a593Smuzhiyun"pos-A" and "pos-B".
1385*4882a593Smuzhiyun
1386*4882a593SmuzhiyunThis snippet first initializes a state object for both groups (in foo_probe()),
1387*4882a593Smuzhiyunthen muxes the function in the pins defined by group A, and finally muxes it in
1388*4882a593Smuzhiyunon the pins defined by group B::
1389*4882a593Smuzhiyun
1390*4882a593Smuzhiyun	#include <linux/pinctrl/consumer.h>
1391*4882a593Smuzhiyun
1392*4882a593Smuzhiyun	struct pinctrl *p;
1393*4882a593Smuzhiyun	struct pinctrl_state *s1, *s2;
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun	foo_probe()
1396*4882a593Smuzhiyun	{
1397*4882a593Smuzhiyun		/* Setup */
1398*4882a593Smuzhiyun		p = devm_pinctrl_get(&device);
1399*4882a593Smuzhiyun		if (IS_ERR(p))
1400*4882a593Smuzhiyun			...
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun		s1 = pinctrl_lookup_state(foo->p, "pos-A");
1403*4882a593Smuzhiyun		if (IS_ERR(s1))
1404*4882a593Smuzhiyun			...
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun		s2 = pinctrl_lookup_state(foo->p, "pos-B");
1407*4882a593Smuzhiyun		if (IS_ERR(s2))
1408*4882a593Smuzhiyun			...
1409*4882a593Smuzhiyun	}
1410*4882a593Smuzhiyun
1411*4882a593Smuzhiyun	foo_switch()
1412*4882a593Smuzhiyun	{
1413*4882a593Smuzhiyun		/* Enable on position A */
1414*4882a593Smuzhiyun		ret = pinctrl_select_state(s1);
1415*4882a593Smuzhiyun		if (ret < 0)
1416*4882a593Smuzhiyun		...
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun		...
1419*4882a593Smuzhiyun
1420*4882a593Smuzhiyun		/* Enable on position B */
1421*4882a593Smuzhiyun		ret = pinctrl_select_state(s2);
1422*4882a593Smuzhiyun		if (ret < 0)
1423*4882a593Smuzhiyun		...
1424*4882a593Smuzhiyun
1425*4882a593Smuzhiyun		...
1426*4882a593Smuzhiyun	}
1427*4882a593Smuzhiyun
1428*4882a593SmuzhiyunThe above has to be done from process context. The reservation of the pins
1429*4882a593Smuzhiyunwill be done when the state is activated, so in effect one specific pin
1430*4882a593Smuzhiyuncan be used by different functions at different times on a running system.
1431