blob: 43ba411d157131d4104954897a913a7aad0a1491 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001PINCTRL (PIN CONTROL) subsystem
2This document outlines the pin control subsystem in Linux
3
4This subsystem deals with:
5
6- Enumerating and naming controllable pins
7
8- Multiplexing of pins, pads, fingers (etc) see below for details
9
10The intention is to also deal with:
11
12- Software-controlled biasing and driving mode specific pins, such as
13 pull-up/down, open drain etc, load capacitance configuration when controlled
14 by software, etc.
15
16
17Top-level interface
18===================
19
20Definition of PIN CONTROLLER:
21
22- A pin controller is a piece of hardware, usually a set of registers, that
23 can control PINs. It may be able to multiplex, bias, set load capacitance,
24 set drive strength etc for individual pins or groups of pins.
25
26Definition of PIN:
27
28- PINS are equal to pads, fingers, balls or whatever packaging input or
29 output line you want to control and these are denoted by unsigned integers
30 in the range 0..maxpin. This numberspace is local to each PIN CONTROLLER, so
31 there may be several such number spaces in a system. This pin space may
32 be sparse - i.e. there may be gaps in the space with numbers where no
33 pin exists.
34
Linus Walleij336cdba02011-11-10 09:27:41 +010035When a PIN CONTROLLER is instantiated, it will register a descriptor to the
Linus Walleij2744e8a2011-05-02 20:50:54 +020036pin control framework, and this descriptor contains an array of pin descriptors
37describing the pins handled by this specific pin controller.
38
39Here is an example of a PGA (Pin Grid Array) chip seen from underneath:
40
41 A B C D E F G H
42
43 8 o o o o o o o o
44
45 7 o o o o o o o o
46
47 6 o o o o o o o o
48
49 5 o o o o o o o o
50
51 4 o o o o o o o o
52
53 3 o o o o o o o o
54
55 2 o o o o o o o o
56
57 1 o o o o o o o o
58
59To register a pin controller and name all the pins on this package we can do
60this in our driver:
61
62#include <linux/pinctrl/pinctrl.h>
63
Linus Walleij336cdba02011-11-10 09:27:41 +010064const struct pinctrl_pin_desc foo_pins[] = {
65 PINCTRL_PIN(0, "A8"),
66 PINCTRL_PIN(1, "B8"),
67 PINCTRL_PIN(2, "C8"),
Linus Walleij2744e8a2011-05-02 20:50:54 +020068 ...
Linus Walleij336cdba02011-11-10 09:27:41 +010069 PINCTRL_PIN(61, "F1"),
70 PINCTRL_PIN(62, "G1"),
71 PINCTRL_PIN(63, "H1"),
Linus Walleij2744e8a2011-05-02 20:50:54 +020072};
73
74static struct pinctrl_desc foo_desc = {
75 .name = "foo",
76 .pins = foo_pins,
77 .npins = ARRAY_SIZE(foo_pins),
78 .maxpin = 63,
79 .owner = THIS_MODULE,
80};
81
82int __init foo_probe(void)
83{
84 struct pinctrl_dev *pctl;
85
86 pctl = pinctrl_register(&foo_desc, <PARENT>, NULL);
87 if (IS_ERR(pctl))
88 pr_err("could not register foo pin driver\n");
89}
90
91Pins usually have fancier names than this. You can find these in the dataheet
92for your chip. Notice that the core pinctrl.h file provides a fancy macro
93called PINCTRL_PIN() to create the struct entries. As you can see I enumerated
Linus Walleij336cdba02011-11-10 09:27:41 +010094the pins from 0 in the upper left corner to 63 in the lower right corner.
95This enumeration was arbitrarily chosen, in practice you need to think
Linus Walleij2744e8a2011-05-02 20:50:54 +020096through your numbering system so that it matches the layout of registers
97and such things in your driver, or the code may become complicated. You must
98also consider matching of offsets to the GPIO ranges that may be handled by
99the pin controller.
100
101For a padring with 467 pads, as opposed to actual pins, I used an enumeration
102like this, walking around the edge of the chip, which seems to be industry
103standard too (all these pads had names, too):
104
105
106 0 ..... 104
107 466 105
108 . .
109 . .
110 358 224
111 357 .... 225
112
113
114Pin groups
115==========
116
117Many controllers need to deal with groups of pins, so the pin controller
118subsystem has a mechanism for enumerating groups of pins and retrieving the
119actual enumerated pins that are part of a certain group.
120
121For example, say that we have a group of pins dealing with an SPI interface
122on { 0, 8, 16, 24 }, and a group of pins dealing with an I2C interface on pins
123on { 24, 25 }.
124
125These two groups are presented to the pin control subsystem by implementing
126some generic pinctrl_ops like this:
127
128#include <linux/pinctrl/pinctrl.h>
129
130struct foo_group {
131 const char *name;
132 const unsigned int *pins;
133 const unsigned num_pins;
134};
135
Linus Walleij336cdba02011-11-10 09:27:41 +0100136static const unsigned int spi0_pins[] = { 0, 8, 16, 24 };
137static const unsigned int i2c0_pins[] = { 24, 25 };
Linus Walleij2744e8a2011-05-02 20:50:54 +0200138
139static const struct foo_group foo_groups[] = {
140 {
141 .name = "spi0_grp",
142 .pins = spi0_pins,
143 .num_pins = ARRAY_SIZE(spi0_pins),
144 },
145 {
146 .name = "i2c0_grp",
147 .pins = i2c0_pins,
148 .num_pins = ARRAY_SIZE(i2c0_pins),
149 },
150};
151
152
153static int foo_list_groups(struct pinctrl_dev *pctldev, unsigned selector)
154{
155 if (selector >= ARRAY_SIZE(foo_groups))
156 return -EINVAL;
157 return 0;
158}
159
160static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
161 unsigned selector)
162{
163 return foo_groups[selector].name;
164}
165
166static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
167 unsigned ** const pins,
168 unsigned * const num_pins)
169{
170 *pins = (unsigned *) foo_groups[selector].pins;
171 *num_pins = foo_groups[selector].num_pins;
172 return 0;
173}
174
175static struct pinctrl_ops foo_pctrl_ops = {
176 .list_groups = foo_list_groups,
177 .get_group_name = foo_get_group_name,
178 .get_group_pins = foo_get_group_pins,
179};
180
181
182static struct pinctrl_desc foo_desc = {
183 ...
184 .pctlops = &foo_pctrl_ops,
185};
186
187The pin control subsystem will call the .list_groups() function repeatedly
188beginning on 0 until it returns non-zero to determine legal selectors, then
189it will call the other functions to retrieve the name and pins of the group.
190Maintaining the data structure of the groups is up to the driver, this is
191just a simple example - in practice you may need more entries in your group
192structure, for example specific register ranges associated with each group
193and so on.
194
195
196Interaction with the GPIO subsystem
197===================================
198
199The GPIO drivers may want to perform operations of various types on the same
200physical pins that are also registered as pin controller pins.
201
202Since the pin controller subsystem have its pinspace local to the pin
203controller we need a mapping so that the pin control subsystem can figure out
204which pin controller handles control of a certain GPIO pin. Since a single
205pin controller may be muxing several GPIO ranges (typically SoCs that have
206one set of pins but internally several GPIO silicon blocks, each modeled as
207a struct gpio_chip) any number of GPIO ranges can be added to a pin controller
208instance like this:
209
210struct gpio_chip chip_a;
211struct gpio_chip chip_b;
212
213static struct pinctrl_gpio_range gpio_range_a = {
214 .name = "chip a",
215 .id = 0,
216 .base = 32,
Chanho Park3c739ad2011-11-11 18:47:58 +0900217 .pin_base = 32,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 .npins = 16,
219 .gc = &chip_a;
220};
221
Chanho Park3c739ad2011-11-11 18:47:58 +0900222static struct pinctrl_gpio_range gpio_range_b = {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200223 .name = "chip b",
224 .id = 0,
225 .base = 48,
Chanho Park3c739ad2011-11-11 18:47:58 +0900226 .pin_base = 64,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200227 .npins = 8,
228 .gc = &chip_b;
229};
230
Linus Walleij2744e8a2011-05-02 20:50:54 +0200231{
232 struct pinctrl_dev *pctl;
233 ...
234 pinctrl_add_gpio_range(pctl, &gpio_range_a);
235 pinctrl_add_gpio_range(pctl, &gpio_range_b);
236}
237
238So this complex system has one pin controller handling two different
Chanho Park3c739ad2011-11-11 18:47:58 +0900239GPIO chips. "chip a" has 16 pins and "chip b" has 8 pins. The "chip a" and
240"chip b" have different .pin_base, which means a start pin number of the
241GPIO range.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200242
Chanho Park3c739ad2011-11-11 18:47:58 +0900243The GPIO range of "chip a" starts from the GPIO base of 32 and actual
244pin range also starts from 32. However "chip b" has different starting
245offset for the GPIO range and pin range. The GPIO range of "chip b" starts
246from GPIO number 48, while the pin range of "chip b" starts from 64.
247
248We can convert a gpio number to actual pin number using this "pin_base".
249They are mapped in the global GPIO pin space at:
250
251chip a:
252 - GPIO range : [32 .. 47]
253 - pin range : [32 .. 47]
254chip b:
255 - GPIO range : [48 .. 55]
256 - pin range : [64 .. 71]
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257
258When GPIO-specific functions in the pin control subsystem are called, these
Linus Walleij336cdba02011-11-10 09:27:41 +0100259ranges will be used to look up the appropriate pin controller by inspecting
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260and matching the pin to the pin ranges across all controllers. When a
261pin controller handling the matching range is found, GPIO-specific functions
262will be called on that specific pin controller.
263
264For all functionalities dealing with pin biasing, pin muxing etc, the pin
265controller subsystem will subtract the range's .base offset from the passed
Chanho Park3c739ad2011-11-11 18:47:58 +0900266in gpio number, and add the ranges's .pin_base offset to retrive a pin number.
267After that, the subsystem passes it on to the pin control driver, so the driver
268will get an pin number into its handled number range. Further it is also passed
Linus Walleij2744e8a2011-05-02 20:50:54 +0200269the range ID value, so that the pin controller knows which range it should
270deal with.
271
Linus Walleij2744e8a2011-05-02 20:50:54 +0200272PINMUX interfaces
273=================
274
275These calls use the pinmux_* naming prefix. No other calls should use that
276prefix.
277
278
279What is pinmuxing?
280==================
281
282PINMUX, also known as padmux, ballmux, alternate functions or mission modes
283is a way for chip vendors producing some kind of electrical packages to use
284a certain physical pin (ball, pad, finger, etc) for multiple mutually exclusive
285functions, depending on the application. By "application" in this context
286we usually mean a way of soldering or wiring the package into an electronic
287system, even though the framework makes it possible to also change the function
288at runtime.
289
290Here is an example of a PGA (Pin Grid Array) chip seen from underneath:
291
292 A B C D E F G H
293 +---+
294 8 | o | o o o o o o o
295 | |
296 7 | o | o o o o o o o
297 | |
298 6 | o | o o o o o o o
299 +---+---+
300 5 | o | o | o o o o o o
301 +---+---+ +---+
302 4 o o o o o o | o | o
303 | |
304 3 o o o o o o | o | o
305 | |
306 2 o o o o o o | o | o
307 +-------+-------+-------+---+---+
308 1 | o o | o o | o o | o | o |
309 +-------+-------+-------+---+---+
310
311This is not tetris. The game to think of is chess. Not all PGA/BGA packages
312are chessboard-like, big ones have "holes" in some arrangement according to
313different design patterns, but we're using this as a simple example. Of the
314pins you see some will be taken by things like a few VCC and GND to feed power
315to the chip, and quite a few will be taken by large ports like an external
316memory interface. The remaining pins will often be subject to pin multiplexing.
317
318The example 8x8 PGA package above will have pin numbers 0 thru 63 assigned to
319its physical pins. It will name the pins { A1, A2, A3 ... H6, H7, H8 } using
320pinctrl_register_pins() and a suitable data set as shown earlier.
321
322In this 8x8 BGA package the pins { A8, A7, A6, A5 } can be used as an SPI port
323(these are four pins: CLK, RXD, TXD, FRM). In that case, pin B5 can be used as
324some general-purpose GPIO pin. However, in another setting, pins { A5, B5 } can
325be used as an I2C port (these are just two pins: SCL, SDA). Needless to say,
326we cannot use the SPI port and I2C port at the same time. However in the inside
327of the package the silicon performing the SPI logic can alternatively be routed
328out on pins { G4, G3, G2, G1 }.
329
330On the botton row at { A1, B1, C1, D1, E1, F1, G1, H1 } we have something
331special - it's an external MMC bus that can be 2, 4 or 8 bits wide, and it will
332consume 2, 4 or 8 pins respectively, so either { A1, B1 } are taken or
333{ A1, B1, C1, D1 } or all of them. If we use all 8 bits, we cannot use the SPI
334port on pins { G4, G3, G2, G1 } of course.
335
336This way the silicon blocks present inside the chip can be multiplexed "muxed"
337out on different pin ranges. Often contemporary SoC (systems on chip) will
338contain several I2C, SPI, SDIO/MMC, etc silicon blocks that can be routed to
339different pins by pinmux settings.
340
341Since general-purpose I/O pins (GPIO) are typically always in shortage, it is
342common to be able to use almost any pin as a GPIO pin if it is not currently
343in use by some other I/O port.
344
345
346Pinmux conventions
347==================
348
349The purpose of the pinmux functionality in the pin controller subsystem is to
350abstract and provide pinmux settings to the devices you choose to instantiate
351in your machine configuration. It is inspired by the clk, GPIO and regulator
352subsystems, so devices will request their mux setting, but it's also possible
353to request a single pin for e.g. GPIO.
354
355Definitions:
356
357- FUNCTIONS can be switched in and out by a driver residing with the pin
358 control subsystem in the drivers/pinctrl/* directory of the kernel. The
359 pin control driver knows the possible functions. In the example above you can
360 identify three pinmux functions, one for spi, one for i2c and one for mmc.
361
362- FUNCTIONS are assumed to be enumerable from zero in a one-dimensional array.
363 In this case the array could be something like: { spi0, i2c0, mmc0 }
364 for the three available functions.
365
366- FUNCTIONS have PIN GROUPS as defined on the generic level - so a certain
367 function is *always* associated with a certain set of pin groups, could
368 be just a single one, but could also be many. In the example above the
369 function i2c is associated with the pins { A5, B5 }, enumerated as
370 { 24, 25 } in the controller pin space.
371
372 The Function spi is associated with pin groups { A8, A7, A6, A5 }
373 and { G4, G3, G2, G1 }, which are enumerated as { 0, 8, 16, 24 } and
374 { 38, 46, 54, 62 } respectively.
375
376 Group names must be unique per pin controller, no two groups on the same
377 controller may have the same name.
378
379- The combination of a FUNCTION and a PIN GROUP determine a certain function
380 for a certain set of pins. The knowledge of the functions and pin groups
381 and their machine-specific particulars are kept inside the pinmux driver,
382 from the outside only the enumerators are known, and the driver core can:
383
384 - Request the name of a function with a certain selector (>= 0)
385 - A list of groups associated with a certain function
386 - Request that a certain group in that list to be activated for a certain
387 function
388
389 As already described above, pin groups are in turn self-descriptive, so
390 the core will retrieve the actual pin range in a certain group from the
391 driver.
392
393- FUNCTIONS and GROUPS on a certain PIN CONTROLLER are MAPPED to a certain
394 device by the board file, device tree or similar machine setup configuration
395 mechanism, similar to how regulators are connected to devices, usually by
396 name. Defining a pin controller, function and group thus uniquely identify
397 the set of pins to be used by a certain device. (If only one possible group
398 of pins is available for the function, no group name need to be supplied -
399 the core will simply select the first and only group available.)
400
401 In the example case we can define that this particular machine shall
402 use device spi0 with pinmux function fspi0 group gspi0 and i2c0 on function
403 fi2c0 group gi2c0, on the primary pin controller, we get mappings
404 like these:
405
406 {
407 {"map-spi0", spi0, pinctrl0, fspi0, gspi0},
408 {"map-i2c0", i2c0, pinctrl0, fi2c0, gi2c0}
409 }
410
411 Every map must be assigned a symbolic name, pin controller and function.
412 The group is not compulsory - if it is omitted the first group presented by
413 the driver as applicable for the function will be selected, which is
414 useful for simple cases.
415
416 The device name is present in map entries tied to specific devices. Maps
417 without device names are referred to as SYSTEM pinmuxes, such as can be taken
418 by the machine implementation on boot and not tied to any specific device.
419
420 It is possible to map several groups to the same combination of device,
421 pin controller and function. This is for cases where a certain function on
422 a certain pin controller may use different sets of pins in different
423 configurations.
424
425- PINS for a certain FUNCTION using a certain PIN GROUP on a certain
426 PIN CONTROLLER are provided on a first-come first-serve basis, so if some
427 other device mux setting or GPIO pin request has already taken your physical
428 pin, you will be denied the use of it. To get (activate) a new setting, the
429 old one has to be put (deactivated) first.
430
431Sometimes the documentation and hardware registers will be oriented around
432pads (or "fingers") rather than pins - these are the soldering surfaces on the
433silicon inside the package, and may or may not match the actual number of
434pins/balls underneath the capsule. Pick some enumeration that makes sense to
435you. Define enumerators only for the pins you can control if that makes sense.
436
437Assumptions:
438
Linus Walleij336cdba02011-11-10 09:27:41 +0100439We assume that the number of possible function maps to pin groups is limited by
Linus Walleij2744e8a2011-05-02 20:50:54 +0200440the hardware. I.e. we assume that there is no system where any function can be
441mapped to any pin, like in a phone exchange. So the available pins groups for
442a certain function will be limited to a few choices (say up to eight or so),
443not hundreds or any amount of choices. This is the characteristic we have found
444by inspecting available pinmux hardware, and a necessary assumption since we
445expect pinmux drivers to present *all* possible function vs pin group mappings
446to the subsystem.
447
448
449Pinmux drivers
450==============
451
452The pinmux core takes care of preventing conflicts on pins and calling
453the pin controller driver to execute different settings.
454
455It is the responsibility of the pinmux driver to impose further restrictions
456(say for example infer electronic limitations due to load etc) to determine
457whether or not the requested function can actually be allowed, and in case it
458is possible to perform the requested mux setting, poke the hardware so that
459this happens.
460
461Pinmux drivers are required to supply a few callback functions, some are
462optional. Usually the enable() and disable() functions are implemented,
463writing values into some certain registers to activate a certain mux setting
464for a certain pin.
465
466A simple driver for the above example will work by setting bits 0, 1, 2, 3 or 4
467into some register named MUX to select a certain function with a certain
468group of pins would work something like this:
469
470#include <linux/pinctrl/pinctrl.h>
471#include <linux/pinctrl/pinmux.h>
472
473struct foo_group {
474 const char *name;
475 const unsigned int *pins;
476 const unsigned num_pins;
477};
478
479static const unsigned spi0_0_pins[] = { 0, 8, 16, 24 };
480static const unsigned spi0_1_pins[] = { 38, 46, 54, 62 };
481static const unsigned i2c0_pins[] = { 24, 25 };
482static const unsigned mmc0_1_pins[] = { 56, 57 };
483static const unsigned mmc0_2_pins[] = { 58, 59 };
484static const unsigned mmc0_3_pins[] = { 60, 61, 62, 63 };
485
486static const struct foo_group foo_groups[] = {
487 {
488 .name = "spi0_0_grp",
489 .pins = spi0_0_pins,
490 .num_pins = ARRAY_SIZE(spi0_0_pins),
491 },
492 {
493 .name = "spi0_1_grp",
494 .pins = spi0_1_pins,
495 .num_pins = ARRAY_SIZE(spi0_1_pins),
496 },
497 {
498 .name = "i2c0_grp",
499 .pins = i2c0_pins,
500 .num_pins = ARRAY_SIZE(i2c0_pins),
501 },
502 {
503 .name = "mmc0_1_grp",
504 .pins = mmc0_1_pins,
505 .num_pins = ARRAY_SIZE(mmc0_1_pins),
506 },
507 {
508 .name = "mmc0_2_grp",
509 .pins = mmc0_2_pins,
510 .num_pins = ARRAY_SIZE(mmc0_2_pins),
511 },
512 {
513 .name = "mmc0_3_grp",
514 .pins = mmc0_3_pins,
515 .num_pins = ARRAY_SIZE(mmc0_3_pins),
516 },
517};
518
519
520static int foo_list_groups(struct pinctrl_dev *pctldev, unsigned selector)
521{
522 if (selector >= ARRAY_SIZE(foo_groups))
523 return -EINVAL;
524 return 0;
525}
526
527static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
528 unsigned selector)
529{
530 return foo_groups[selector].name;
531}
532
533static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
534 unsigned ** const pins,
535 unsigned * const num_pins)
536{
537 *pins = (unsigned *) foo_groups[selector].pins;
538 *num_pins = foo_groups[selector].num_pins;
539 return 0;
540}
541
542static struct pinctrl_ops foo_pctrl_ops = {
543 .list_groups = foo_list_groups,
544 .get_group_name = foo_get_group_name,
545 .get_group_pins = foo_get_group_pins,
546};
547
548struct foo_pmx_func {
549 const char *name;
550 const char * const *groups;
551 const unsigned num_groups;
552};
553
554static const char * const spi0_groups[] = { "spi0_1_grp" };
555static const char * const i2c0_groups[] = { "i2c0_grp" };
556static const char * const mmc0_groups[] = { "mmc0_1_grp", "mmc0_2_grp",
557 "mmc0_3_grp" };
558
559static const struct foo_pmx_func foo_functions[] = {
560 {
561 .name = "spi0",
562 .groups = spi0_groups,
563 .num_groups = ARRAY_SIZE(spi0_groups),
564 },
565 {
566 .name = "i2c0",
567 .groups = i2c0_groups,
568 .num_groups = ARRAY_SIZE(i2c0_groups),
569 },
570 {
571 .name = "mmc0",
572 .groups = mmc0_groups,
573 .num_groups = ARRAY_SIZE(mmc0_groups),
574 },
575};
576
577int foo_list_funcs(struct pinctrl_dev *pctldev, unsigned selector)
578{
579 if (selector >= ARRAY_SIZE(foo_functions))
580 return -EINVAL;
581 return 0;
582}
583
584const char *foo_get_fname(struct pinctrl_dev *pctldev, unsigned selector)
585{
Linus Walleij336cdba02011-11-10 09:27:41 +0100586 return foo_functions[selector].name;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200587}
588
589static int foo_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
590 const char * const **groups,
591 unsigned * const num_groups)
592{
593 *groups = foo_functions[selector].groups;
594 *num_groups = foo_functions[selector].num_groups;
595 return 0;
596}
597
598int foo_enable(struct pinctrl_dev *pctldev, unsigned selector,
599 unsigned group)
600{
Linus Walleij336cdba02011-11-10 09:27:41 +0100601 u8 regbit = (1 << selector + group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200602
603 writeb((readb(MUX)|regbit), MUX)
604 return 0;
605}
606
Linus Walleij336cdba02011-11-10 09:27:41 +0100607void foo_disable(struct pinctrl_dev *pctldev, unsigned selector,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200608 unsigned group)
609{
Linus Walleij336cdba02011-11-10 09:27:41 +0100610 u8 regbit = (1 << selector + group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200611
612 writeb((readb(MUX) & ~(regbit)), MUX)
613 return 0;
614}
615
616struct pinmux_ops foo_pmxops = {
617 .list_functions = foo_list_funcs,
618 .get_function_name = foo_get_fname,
619 .get_function_groups = foo_get_groups,
620 .enable = foo_enable,
621 .disable = foo_disable,
622};
623
624/* Pinmux operations are handled by some pin controller */
625static struct pinctrl_desc foo_desc = {
626 ...
627 .pctlops = &foo_pctrl_ops,
628 .pmxops = &foo_pmxops,
629};
630
631In the example activating muxing 0 and 1 at the same time setting bits
6320 and 1, uses one pin in common so they would collide.
633
634The beauty of the pinmux subsystem is that since it keeps track of all
635pins and who is using them, it will already have denied an impossible
636request like that, so the driver does not need to worry about such
637things - when it gets a selector passed in, the pinmux subsystem makes
638sure no other device or GPIO assignment is already using the selected
639pins. Thus bits 0 and 1 in the control register will never be set at the
640same time.
641
642All the above functions are mandatory to implement for a pinmux driver.
643
644
645Pinmux interaction with the GPIO subsystem
646==========================================
647
648The function list could become long, especially if you can convert every
649individual pin into a GPIO pin independent of any other pins, and then try
650the approach to define every pin as a function.
651
652In this case, the function array would become 64 entries for each GPIO
653setting and then the device functions.
654
655For this reason there is an additional function a pinmux driver can implement
656to enable only GPIO on an individual pin: .gpio_request_enable(). The same
657.free() function as for other functions is assumed to be usable also for
658GPIO pins.
659
660This function will pass in the affected GPIO range identified by the pin
661controller core, so you know which GPIO pins are being affected by the request
662operation.
663
664Alternatively it is fully allowed to use named functions for each GPIO
665pin, the pinmux_request_gpio() will attempt to obtain the function "gpioN"
666where "N" is the global GPIO pin number if no special GPIO-handler is
667registered.
668
669
670Pinmux board/machine configuration
671==================================
672
673Boards and machines define how a certain complete running system is put
674together, including how GPIOs and devices are muxed, how regulators are
675constrained and how the clock tree looks. Of course pinmux settings are also
676part of this.
677
678A pinmux config for a machine looks pretty much like a simple regulator
679configuration, so for the example array above we want to enable i2c and
680spi on the second function mapping:
681
682#include <linux/pinctrl/machine.h>
683
Linus Walleij336cdba02011-11-10 09:27:41 +0100684static const struct pinmux_map pmx_mapping[] = {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200685 {
686 .ctrl_dev_name = "pinctrl.0",
687 .function = "spi0",
688 .dev_name = "foo-spi.0",
689 },
690 {
691 .ctrl_dev_name = "pinctrl.0",
692 .function = "i2c0",
693 .dev_name = "foo-i2c.0",
694 },
695 {
696 .ctrl_dev_name = "pinctrl.0",
697 .function = "mmc0",
698 .dev_name = "foo-mmc.0",
699 },
700};
701
702The dev_name here matches to the unique device name that can be used to look
703up the device struct (just like with clockdev or regulators). The function name
704must match a function provided by the pinmux driver handling this pin range.
705
706As you can see we may have several pin controllers on the system and thus
707we need to specify which one of them that contain the functions we wish
708to map. The map can also use struct device * directly, so there is no
709inherent need to use strings to specify .dev_name or .ctrl_dev_name, these
710are for the situation where you do not have a handle to the struct device *,
711for example if they are not yet instantiated or cumbersome to obtain.
712
713You register this pinmux mapping to the pinmux subsystem by simply:
714
Linus Walleij336cdba02011-11-10 09:27:41 +0100715 ret = pinmux_register_mappings(pmx_mapping, ARRAY_SIZE(pmx_mapping));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200716
717Since the above construct is pretty common there is a helper macro to make
718it even more compact which assumes you want to use pinctrl.0 and position
7190 for mapping, for example:
720
721static struct pinmux_map pmx_mapping[] = {
722 PINMUX_MAP_PRIMARY("I2CMAP", "i2c0", "foo-i2c.0"),
723};
724
725
726Complex mappings
727================
728
729As it is possible to map a function to different groups of pins an optional
730.group can be specified like this:
731
732...
733{
734 .name = "spi0-pos-A",
735 .ctrl_dev_name = "pinctrl.0",
736 .function = "spi0",
737 .group = "spi0_0_grp",
738 .dev_name = "foo-spi.0",
739},
740{
741 .name = "spi0-pos-B",
742 .ctrl_dev_name = "pinctrl.0",
743 .function = "spi0",
744 .group = "spi0_1_grp",
745 .dev_name = "foo-spi.0",
746},
747...
748
749This example mapping is used to switch between two positions for spi0 at
750runtime, as described further below under the heading "Runtime pinmuxing".
751
752Further it is possible to match several groups of pins to the same function
753for a single device, say for example in the mmc0 example above, where you can
754additively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all
755three groups for a total of 2+2+4 = 8 pins (for an 8-bit MMC bus as is the
756case), we define a mapping like this:
757
758...
759{
760 .name "2bit"
761 .ctrl_dev_name = "pinctrl.0",
762 .function = "mmc0",
Linus Walleij336cdba02011-11-10 09:27:41 +0100763 .group = "mmc0_1_grp",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200764 .dev_name = "foo-mmc.0",
765},
766{
767 .name "4bit"
768 .ctrl_dev_name = "pinctrl.0",
769 .function = "mmc0",
770 .group = "mmc0_1_grp",
771 .dev_name = "foo-mmc.0",
772},
773{
Linus Walleij336cdba02011-11-10 09:27:41 +0100774 .name "4bit"
Linus Walleij2744e8a2011-05-02 20:50:54 +0200775 .ctrl_dev_name = "pinctrl.0",
776 .function = "mmc0",
Linus Walleij336cdba02011-11-10 09:27:41 +0100777 .group = "mmc0_2_grp",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200778 .dev_name = "foo-mmc.0",
779},
780{
781 .name "8bit"
782 .ctrl_dev_name = "pinctrl.0",
783 .function = "mmc0",
784 .group = "mmc0_1_grp",
785 .dev_name = "foo-mmc.0",
786},
787{
788 .name "8bit"
789 .ctrl_dev_name = "pinctrl.0",
790 .function = "mmc0",
791 .group = "mmc0_2_grp",
792 .dev_name = "foo-mmc.0",
793},
Linus Walleij336cdba02011-11-10 09:27:41 +0100794{
795 .name "8bit"
796 .ctrl_dev_name = "pinctrl.0",
797 .function = "mmc0",
798 .group = "mmc0_3_grp",
799 .dev_name = "foo-mmc.0",
800},
Linus Walleij2744e8a2011-05-02 20:50:54 +0200801...
802
803The result of grabbing this mapping from the device with something like
804this (see next paragraph):
805
806 pmx = pinmux_get(&device, "8bit");
807
808Will be that you activate all the three bottom records in the mapping at
809once. Since they share the same name, pin controller device, funcion and
810device, and since we allow multiple groups to match to a single device, they
811all get selected, and they all get enabled and disable simultaneously by the
812pinmux core.
813
814
815Pinmux requests from drivers
816============================
817
818Generally it is discouraged to let individual drivers get and enable pinmuxes.
819So if possible, handle the pinmuxes in platform code or some other place where
820you have access to all the affected struct device * pointers. In some cases
821where a driver needs to switch between different mux mappings at runtime
822this is not possible.
823
824A driver may request a certain mux to be activated, usually just the default
825mux like this:
826
827#include <linux/pinctrl/pinmux.h>
828
829struct foo_state {
830 struct pinmux *pmx;
831 ...
832};
833
834foo_probe()
835{
836 /* Allocate a state holder named "state" etc */
837 struct pinmux pmx;
838
839 pmx = pinmux_get(&device, NULL);
840 if IS_ERR(pmx)
841 return PTR_ERR(pmx);
842 pinmux_enable(pmx);
843
844 state->pmx = pmx;
845}
846
847foo_remove()
848{
849 pinmux_disable(state->pmx);
850 pinmux_put(state->pmx);
851}
852
853If you want to grab a specific mux mapping and not just the first one found for
854this device you can specify a specific mapping name, for example in the above
855example the second i2c0 setting: pinmux_get(&device, "spi0-pos-B");
856
857This get/enable/disable/put sequence can just as well be handled by bus drivers
858if you don't want each and every driver to handle it and you know the
859arrangement on your bus.
860
861The semantics of the get/enable respective disable/put is as follows:
862
863- pinmux_get() is called in process context to reserve the pins affected with
864 a certain mapping and set up the pinmux core and the driver. It will allocate
865 a struct from the kernel memory to hold the pinmux state.
866
867- pinmux_enable()/pinmux_disable() is quick and can be called from fastpath
868 (irq context) when you quickly want to set up/tear down the hardware muxing
869 when running a device driver. Usually it will just poke some values into a
870 register.
871
872- pinmux_disable() is called in process context to tear down the pin requests
873 and release the state holder struct for the mux setting.
874
875Usually the pinmux core handled the get/put pair and call out to the device
876drivers bookkeeping operations, like checking available functions and the
877associated pins, whereas the enable/disable pass on to the pin controller
878driver which takes care of activating and/or deactivating the mux setting by
879quickly poking some registers.
880
881The pins are allocated for your device when you issue the pinmux_get() call,
882after this you should be able to see this in the debugfs listing of all pins.
883
884
885System pinmux hogging
886=====================
887
888A system pinmux map entry, i.e. a pinmux setting that does not have a device
889associated with it, can be hogged by the core when the pin controller is
890registered. This means that the core will attempt to call pinmux_get() and
891pinmux_enable() on it immediately after the pin control device has been
892registered.
893
894This is enabled by simply setting the .hog_on_boot field in the map to true,
895like this:
896
897{
898 .name "POWERMAP"
899 .ctrl_dev_name = "pinctrl.0",
900 .function = "power_func",
901 .hog_on_boot = true,
902},
903
904Since it may be common to request the core to hog a few always-applicable
905mux settings on the primary pin controller, there is a convenience macro for
906this:
907
908PINMUX_MAP_PRIMARY_SYS_HOG("POWERMAP", "power_func")
909
910This gives the exact same result as the above construction.
911
912
913Runtime pinmuxing
914=================
915
916It is possible to mux a certain function in and out at runtime, say to move
917an SPI port from one set of pins to another set of pins. Say for example for
918spi0 in the example above, we expose two different groups of pins for the same
919function, but with different named in the mapping as described under
920"Advanced mapping" above. So we have two mappings named "spi0-pos-A" and
921"spi0-pos-B".
922
923This snippet first muxes the function in the pins defined by group A, enables
924it, disables and releases it, and muxes it in on the pins defined by group B:
925
926foo_switch()
927{
928 struct pinmux pmx;
929
930 /* Enable on position A */
931 pmx = pinmux_get(&device, "spi0-pos-A");
932 if IS_ERR(pmx)
933 return PTR_ERR(pmx);
934 pinmux_enable(pmx);
935
936 /* This releases the pins again */
937 pinmux_disable(pmx);
938 pinmux_put(pmx);
939
940 /* Enable on position B */
941 pmx = pinmux_get(&device, "spi0-pos-B");
942 if IS_ERR(pmx)
943 return PTR_ERR(pmx);
944 pinmux_enable(pmx);
945 ...
946}
947
948The above has to be done from process context.