aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/pinctrl.txt950
-rw-r--r--MAINTAINERS5
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile2
-rw-r--r--drivers/pinctrl/Kconfig29
-rw-r--r--drivers/pinctrl/Makefile6
-rw-r--r--drivers/pinctrl/core.c599
-rw-r--r--drivers/pinctrl/core.h72
-rw-r--r--drivers/pinctrl/pinmux.c1180
-rw-r--r--drivers/pinctrl/pinmux.h47
-rw-r--r--include/linux/pinctrl/machine.h107
-rw-r--r--include/linux/pinctrl/pinctrl.h133
-rw-r--r--include/linux/pinctrl/pinmux.h117
13 files changed, 3249 insertions, 0 deletions
diff --git a/Documentation/pinctrl.txt b/Documentation/pinctrl.txt
new file mode 100644
index 00000000000..b04cb7d45a1
--- /dev/null
+++ b/Documentation/pinctrl.txt
@@ -0,0 +1,950 @@
1PINCTRL (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
35When a PIN CONTROLLER is instatiated, it will register a descriptor to the
36pin 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
64const struct pinctrl_pin_desc __refdata foo_pins[] = {
65 PINCTRL_PIN(0, "A1"),
66 PINCTRL_PIN(1, "A2"),
67 PINCTRL_PIN(2, "A3"),
68 ...
69 PINCTRL_PIN(61, "H6"),
70 PINCTRL_PIN(62, "H7"),
71 PINCTRL_PIN(63, "H8"),
72};
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
94the 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
96through 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
136static unsigned int spi0_pins[] = { 0, 8, 16, 24 };
137static unsigned int i2c0_pins[] = { 24, 25 };
138
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,
217 .npins = 16,
218 .gc = &chip_a;
219};
220
221static struct pinctrl_gpio_range gpio_range_a = {
222 .name = "chip b",
223 .id = 0,
224 .base = 48,
225 .npins = 8,
226 .gc = &chip_b;
227};
228
229
230{
231 struct pinctrl_dev *pctl;
232 ...
233 pinctrl_add_gpio_range(pctl, &gpio_range_a);
234 pinctrl_add_gpio_range(pctl, &gpio_range_b);
235}
236
237So this complex system has one pin controller handling two different
238GPIO chips. Chip a has 16 pins and chip b has 8 pins. They are mapped in
239the global GPIO pin space at:
240
241chip a: [32 .. 47]
242chip b: [48 .. 55]
243
244When GPIO-specific functions in the pin control subsystem are called, these
245ranges will be used to look up the apropriate pin controller by inspecting
246and matching the pin to the pin ranges across all controllers. When a
247pin controller handling the matching range is found, GPIO-specific functions
248will be called on that specific pin controller.
249
250For all functionalities dealing with pin biasing, pin muxing etc, the pin
251controller subsystem will subtract the range's .base offset from the passed
252in gpio pin number, and pass that on to the pin control driver, so the driver
253will get an offset into its handled number range. Further it is also passed
254the range ID value, so that the pin controller knows which range it should
255deal with.
256
257For example: if a user issues pinctrl_gpio_set_foo(50), the pin control
258subsystem will find that the second range on this pin controller matches,
259subtract the base 48 and call the
260pinctrl_driver_gpio_set_foo(pinctrl, range, 2) where the latter function has
261this signature:
262
263int pinctrl_driver_gpio_set_foo(struct pinctrl_dev *pctldev,
264 struct pinctrl_gpio_range *rangeid,
265 unsigned offset);
266
267Now the driver knows that we want to do some GPIO-specific operation on the
268second GPIO range handled by "chip b", at offset 2 in that specific range.
269
270(If the GPIO subsystem is ever refactored to use a local per-GPIO controller
271pin space, this mapping will need to be augmented accordingly.)
272
273
274PINMUX interfaces
275=================
276
277These calls use the pinmux_* naming prefix. No other calls should use that
278prefix.
279
280
281What is pinmuxing?
282==================
283
284PINMUX, also known as padmux, ballmux, alternate functions or mission modes
285is a way for chip vendors producing some kind of electrical packages to use
286a certain physical pin (ball, pad, finger, etc) for multiple mutually exclusive
287functions, depending on the application. By "application" in this context
288we usually mean a way of soldering or wiring the package into an electronic
289system, even though the framework makes it possible to also change the function
290at runtime.
291
292Here is an example of a PGA (Pin Grid Array) chip seen from underneath:
293
294 A B C D E F G H
295 +---+
296 8 | o | o o o o o o o
297 | |
298 7 | o | o o o o o o o
299 | |
300 6 | o | o o o o o o o
301 +---+---+
302 5 | o | o | o o o o o o
303 +---+---+ +---+
304 4 o o o o o o | o | o
305 | |
306 3 o o o o o o | o | o
307 | |
308 2 o o o o o o | o | o
309 +-------+-------+-------+---+---+
310 1 | o o | o o | o o | o | o |
311 +-------+-------+-------+---+---+
312
313This is not tetris. The game to think of is chess. Not all PGA/BGA packages
314are chessboard-like, big ones have "holes" in some arrangement according to
315different design patterns, but we're using this as a simple example. Of the
316pins you see some will be taken by things like a few VCC and GND to feed power
317to the chip, and quite a few will be taken by large ports like an external
318memory interface. The remaining pins will often be subject to pin multiplexing.
319
320The example 8x8 PGA package above will have pin numbers 0 thru 63 assigned to
321its physical pins. It will name the pins { A1, A2, A3 ... H6, H7, H8 } using
322pinctrl_register_pins() and a suitable data set as shown earlier.
323
324In this 8x8 BGA package the pins { A8, A7, A6, A5 } can be used as an SPI port
325(these are four pins: CLK, RXD, TXD, FRM). In that case, pin B5 can be used as
326some general-purpose GPIO pin. However, in another setting, pins { A5, B5 } can
327be used as an I2C port (these are just two pins: SCL, SDA). Needless to say,
328we cannot use the SPI port and I2C port at the same time. However in the inside
329of the package the silicon performing the SPI logic can alternatively be routed
330out on pins { G4, G3, G2, G1 }.
331
332On the botton row at { A1, B1, C1, D1, E1, F1, G1, H1 } we have something
333special - it's an external MMC bus that can be 2, 4 or 8 bits wide, and it will
334consume 2, 4 or 8 pins respectively, so either { A1, B1 } are taken or
335{ A1, B1, C1, D1 } or all of them. If we use all 8 bits, we cannot use the SPI
336port on pins { G4, G3, G2, G1 } of course.
337
338This way the silicon blocks present inside the chip can be multiplexed "muxed"
339out on different pin ranges. Often contemporary SoC (systems on chip) will
340contain several I2C, SPI, SDIO/MMC, etc silicon blocks that can be routed to
341different pins by pinmux settings.
342
343Since general-purpose I/O pins (GPIO) are typically always in shortage, it is
344common to be able to use almost any pin as a GPIO pin if it is not currently
345in use by some other I/O port.
346
347
348Pinmux conventions
349==================
350
351The purpose of the pinmux functionality in the pin controller subsystem is to
352abstract and provide pinmux settings to the devices you choose to instantiate
353in your machine configuration. It is inspired by the clk, GPIO and regulator
354subsystems, so devices will request their mux setting, but it's also possible
355to request a single pin for e.g. GPIO.
356
357Definitions:
358
359- FUNCTIONS can be switched in and out by a driver residing with the pin
360 control subsystem in the drivers/pinctrl/* directory of the kernel. The
361 pin control driver knows the possible functions. In the example above you can
362 identify three pinmux functions, one for spi, one for i2c and one for mmc.
363
364- FUNCTIONS are assumed to be enumerable from zero in a one-dimensional array.
365 In this case the array could be something like: { spi0, i2c0, mmc0 }
366 for the three available functions.
367
368- FUNCTIONS have PIN GROUPS as defined on the generic level - so a certain
369 function is *always* associated with a certain set of pin groups, could
370 be just a single one, but could also be many. In the example above the
371 function i2c is associated with the pins { A5, B5 }, enumerated as
372 { 24, 25 } in the controller pin space.
373
374 The Function spi is associated with pin groups { A8, A7, A6, A5 }
375 and { G4, G3, G2, G1 }, which are enumerated as { 0, 8, 16, 24 } and
376 { 38, 46, 54, 62 } respectively.
377
378 Group names must be unique per pin controller, no two groups on the same
379 controller may have the same name.
380
381- The combination of a FUNCTION and a PIN GROUP determine a certain function
382 for a certain set of pins. The knowledge of the functions and pin groups
383 and their machine-specific particulars are kept inside the pinmux driver,
384 from the outside only the enumerators are known, and the driver core can:
385
386 - Request the name of a function with a certain selector (>= 0)
387 - A list of groups associated with a certain function
388 - Request that a certain group in that list to be activated for a certain
389 function
390
391 As already described above, pin groups are in turn self-descriptive, so
392 the core will retrieve the actual pin range in a certain group from the
393 driver.
394
395- FUNCTIONS and GROUPS on a certain PIN CONTROLLER are MAPPED to a certain
396 device by the board file, device tree or similar machine setup configuration
397 mechanism, similar to how regulators are connected to devices, usually by
398 name. Defining a pin controller, function and group thus uniquely identify
399 the set of pins to be used by a certain device. (If only one possible group
400 of pins is available for the function, no group name need to be supplied -
401 the core will simply select the first and only group available.)
402
403 In the example case we can define that this particular machine shall
404 use device spi0 with pinmux function fspi0 group gspi0 and i2c0 on function
405 fi2c0 group gi2c0, on the primary pin controller, we get mappings
406 like these:
407
408 {
409 {"map-spi0", spi0, pinctrl0, fspi0, gspi0},
410 {"map-i2c0", i2c0, pinctrl0, fi2c0, gi2c0}
411 }
412
413 Every map must be assigned a symbolic name, pin controller and function.
414 The group is not compulsory - if it is omitted the first group presented by
415 the driver as applicable for the function will be selected, which is
416 useful for simple cases.
417
418 The device name is present in map entries tied to specific devices. Maps
419 without device names are referred to as SYSTEM pinmuxes, such as can be taken
420 by the machine implementation on boot and not tied to any specific device.
421
422 It is possible to map several groups to the same combination of device,
423 pin controller and function. This is for cases where a certain function on
424 a certain pin controller may use different sets of pins in different
425 configurations.
426
427- PINS for a certain FUNCTION using a certain PIN GROUP on a certain
428 PIN CONTROLLER are provided on a first-come first-serve basis, so if some
429 other device mux setting or GPIO pin request has already taken your physical
430 pin, you will be denied the use of it. To get (activate) a new setting, the
431 old one has to be put (deactivated) first.
432
433Sometimes the documentation and hardware registers will be oriented around
434pads (or "fingers") rather than pins - these are the soldering surfaces on the
435silicon inside the package, and may or may not match the actual number of
436pins/balls underneath the capsule. Pick some enumeration that makes sense to
437you. Define enumerators only for the pins you can control if that makes sense.
438
439Assumptions:
440
441We assume that the number possible function maps to pin groups is limited by
442the hardware. I.e. we assume that there is no system where any function can be
443mapped to any pin, like in a phone exchange. So the available pins groups for
444a certain function will be limited to a few choices (say up to eight or so),
445not hundreds or any amount of choices. This is the characteristic we have found
446by inspecting available pinmux hardware, and a necessary assumption since we
447expect pinmux drivers to present *all* possible function vs pin group mappings
448to the subsystem.
449
450
451Pinmux drivers
452==============
453
454The pinmux core takes care of preventing conflicts on pins and calling
455the pin controller driver to execute different settings.
456
457It is the responsibility of the pinmux driver to impose further restrictions
458(say for example infer electronic limitations due to load etc) to determine
459whether or not the requested function can actually be allowed, and in case it
460is possible to perform the requested mux setting, poke the hardware so that
461this happens.
462
463Pinmux drivers are required to supply a few callback functions, some are
464optional. Usually the enable() and disable() functions are implemented,
465writing values into some certain registers to activate a certain mux setting
466for a certain pin.
467
468A simple driver for the above example will work by setting bits 0, 1, 2, 3 or 4
469into some register named MUX to select a certain function with a certain
470group of pins would work something like this:
471
472#include <linux/pinctrl/pinctrl.h>
473#include <linux/pinctrl/pinmux.h>
474
475struct foo_group {
476 const char *name;
477 const unsigned int *pins;
478 const unsigned num_pins;
479};
480
481static const unsigned spi0_0_pins[] = { 0, 8, 16, 24 };
482static const unsigned spi0_1_pins[] = { 38, 46, 54, 62 };
483static const unsigned i2c0_pins[] = { 24, 25 };
484static const unsigned mmc0_1_pins[] = { 56, 57 };
485static const unsigned mmc0_2_pins[] = { 58, 59 };
486static const unsigned mmc0_3_pins[] = { 60, 61, 62, 63 };
487
488static const struct foo_group foo_groups[] = {
489 {
490 .name = "spi0_0_grp",
491 .pins = spi0_0_pins,
492 .num_pins = ARRAY_SIZE(spi0_0_pins),
493 },
494 {
495 .name = "spi0_1_grp",
496 .pins = spi0_1_pins,
497 .num_pins = ARRAY_SIZE(spi0_1_pins),
498 },
499 {
500 .name = "i2c0_grp",
501 .pins = i2c0_pins,
502 .num_pins = ARRAY_SIZE(i2c0_pins),
503 },
504 {
505 .name = "mmc0_1_grp",
506 .pins = mmc0_1_pins,
507 .num_pins = ARRAY_SIZE(mmc0_1_pins),
508 },
509 {
510 .name = "mmc0_2_grp",
511 .pins = mmc0_2_pins,
512 .num_pins = ARRAY_SIZE(mmc0_2_pins),
513 },
514 {
515 .name = "mmc0_3_grp",
516 .pins = mmc0_3_pins,
517 .num_pins = ARRAY_SIZE(mmc0_3_pins),
518 },
519};
520
521
522static int foo_list_groups(struct pinctrl_dev *pctldev, unsigned selector)
523{
524 if (selector >= ARRAY_SIZE(foo_groups))
525 return -EINVAL;
526 return 0;
527}
528
529static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
530 unsigned selector)
531{
532 return foo_groups[selector].name;
533}
534
535static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
536 unsigned ** const pins,
537 unsigned * const num_pins)
538{
539 *pins = (unsigned *) foo_groups[selector].pins;
540 *num_pins = foo_groups[selector].num_pins;
541 return 0;
542}
543
544static struct pinctrl_ops foo_pctrl_ops = {
545 .list_groups = foo_list_groups,
546 .get_group_name = foo_get_group_name,
547 .get_group_pins = foo_get_group_pins,
548};
549
550struct foo_pmx_func {
551 const char *name;
552 const char * const *groups;
553 const unsigned num_groups;
554};
555
556static const char * const spi0_groups[] = { "spi0_1_grp" };
557static const char * const i2c0_groups[] = { "i2c0_grp" };
558static const char * const mmc0_groups[] = { "mmc0_1_grp", "mmc0_2_grp",
559 "mmc0_3_grp" };
560
561static const struct foo_pmx_func foo_functions[] = {
562 {
563 .name = "spi0",
564 .groups = spi0_groups,
565 .num_groups = ARRAY_SIZE(spi0_groups),
566 },
567 {
568 .name = "i2c0",
569 .groups = i2c0_groups,
570 .num_groups = ARRAY_SIZE(i2c0_groups),
571 },
572 {
573 .name = "mmc0",
574 .groups = mmc0_groups,
575 .num_groups = ARRAY_SIZE(mmc0_groups),
576 },
577};
578
579int foo_list_funcs(struct pinctrl_dev *pctldev, unsigned selector)
580{
581 if (selector >= ARRAY_SIZE(foo_functions))
582 return -EINVAL;
583 return 0;
584}
585
586const char *foo_get_fname(struct pinctrl_dev *pctldev, unsigned selector)
587{
588 return myfuncs[selector].name;
589}
590
591static int foo_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
592 const char * const **groups,
593 unsigned * const num_groups)
594{
595 *groups = foo_functions[selector].groups;
596 *num_groups = foo_functions[selector].num_groups;
597 return 0;
598}
599
600int foo_enable(struct pinctrl_dev *pctldev, unsigned selector,
601 unsigned group)
602{
603 u8 regbit = (1 << group);
604
605 writeb((readb(MUX)|regbit), MUX)
606 return 0;
607}
608
609int foo_disable(struct pinctrl_dev *pctldev, unsigned selector,
610 unsigned group)
611{
612 u8 regbit = (1 << group);
613
614 writeb((readb(MUX) & ~(regbit)), MUX)
615 return 0;
616}
617
618struct pinmux_ops foo_pmxops = {
619 .list_functions = foo_list_funcs,
620 .get_function_name = foo_get_fname,
621 .get_function_groups = foo_get_groups,
622 .enable = foo_enable,
623 .disable = foo_disable,
624};
625
626/* Pinmux operations are handled by some pin controller */
627static struct pinctrl_desc foo_desc = {
628 ...
629 .pctlops = &foo_pctrl_ops,
630 .pmxops = &foo_pmxops,
631};
632
633In the example activating muxing 0 and 1 at the same time setting bits
6340 and 1, uses one pin in common so they would collide.
635
636The beauty of the pinmux subsystem is that since it keeps track of all
637pins and who is using them, it will already have denied an impossible
638request like that, so the driver does not need to worry about such
639things - when it gets a selector passed in, the pinmux subsystem makes
640sure no other device or GPIO assignment is already using the selected
641pins. Thus bits 0 and 1 in the control register will never be set at the
642same time.
643
644All the above functions are mandatory to implement for a pinmux driver.
645
646
647Pinmux interaction with the GPIO subsystem
648==========================================
649
650The function list could become long, especially if you can convert every
651individual pin into a GPIO pin independent of any other pins, and then try
652the approach to define every pin as a function.
653
654In this case, the function array would become 64 entries for each GPIO
655setting and then the device functions.
656
657For this reason there is an additional function a pinmux driver can implement
658to enable only GPIO on an individual pin: .gpio_request_enable(). The same
659.free() function as for other functions is assumed to be usable also for
660GPIO pins.
661
662This function will pass in the affected GPIO range identified by the pin
663controller core, so you know which GPIO pins are being affected by the request
664operation.
665
666Alternatively it is fully allowed to use named functions for each GPIO
667pin, the pinmux_request_gpio() will attempt to obtain the function "gpioN"
668where "N" is the global GPIO pin number if no special GPIO-handler is
669registered.
670
671
672Pinmux board/machine configuration
673==================================
674
675Boards and machines define how a certain complete running system is put
676together, including how GPIOs and devices are muxed, how regulators are
677constrained and how the clock tree looks. Of course pinmux settings are also
678part of this.
679
680A pinmux config for a machine looks pretty much like a simple regulator
681configuration, so for the example array above we want to enable i2c and
682spi on the second function mapping:
683
684#include <linux/pinctrl/machine.h>
685
686static struct pinmux_map pmx_mapping[] = {
687 {
688 .ctrl_dev_name = "pinctrl.0",
689 .function = "spi0",
690 .dev_name = "foo-spi.0",
691 },
692 {
693 .ctrl_dev_name = "pinctrl.0",
694 .function = "i2c0",
695 .dev_name = "foo-i2c.0",
696 },
697 {
698 .ctrl_dev_name = "pinctrl.0",
699 .function = "mmc0",
700 .dev_name = "foo-mmc.0",
701 },
702};
703
704The dev_name here matches to the unique device name that can be used to look
705up the device struct (just like with clockdev or regulators). The function name
706must match a function provided by the pinmux driver handling this pin range.
707
708As you can see we may have several pin controllers on the system and thus
709we need to specify which one of them that contain the functions we wish
710to map. The map can also use struct device * directly, so there is no
711inherent need to use strings to specify .dev_name or .ctrl_dev_name, these
712are for the situation where you do not have a handle to the struct device *,
713for example if they are not yet instantiated or cumbersome to obtain.
714
715You register this pinmux mapping to the pinmux subsystem by simply:
716
717 ret = pinmux_register_mappings(&pmx_mapping, ARRAY_SIZE(pmx_mapping));
718
719Since the above construct is pretty common there is a helper macro to make
720it even more compact which assumes you want to use pinctrl.0 and position
7210 for mapping, for example:
722
723static struct pinmux_map pmx_mapping[] = {
724 PINMUX_MAP_PRIMARY("I2CMAP", "i2c0", "foo-i2c.0"),
725};
726
727
728Complex mappings
729================
730
731As it is possible to map a function to different groups of pins an optional
732.group can be specified like this:
733
734...
735{
736 .name = "spi0-pos-A",
737 .ctrl_dev_name = "pinctrl.0",
738 .function = "spi0",
739 .group = "spi0_0_grp",
740 .dev_name = "foo-spi.0",
741},
742{
743 .name = "spi0-pos-B",
744 .ctrl_dev_name = "pinctrl.0",
745 .function = "spi0",
746 .group = "spi0_1_grp",
747 .dev_name = "foo-spi.0",
748},
749...
750
751This example mapping is used to switch between two positions for spi0 at
752runtime, as described further below under the heading "Runtime pinmuxing".
753
754Further it is possible to match several groups of pins to the same function
755for a single device, say for example in the mmc0 example above, where you can
756additively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all
757three groups for a total of 2+2+4 = 8 pins (for an 8-bit MMC bus as is the
758case), we define a mapping like this:
759
760...
761{
762 .name "2bit"
763 .ctrl_dev_name = "pinctrl.0",
764 .function = "mmc0",
765 .group = "mmc0_0_grp",
766 .dev_name = "foo-mmc.0",
767},
768{
769 .name "4bit"
770 .ctrl_dev_name = "pinctrl.0",
771 .function = "mmc0",
772 .group = "mmc0_0_grp",
773 .dev_name = "foo-mmc.0",
774},
775{
776 .name "4bit"
777 .ctrl_dev_name = "pinctrl.0",
778 .function = "mmc0",
779 .group = "mmc0_1_grp",
780 .dev_name = "foo-mmc.0",
781},
782{
783 .name "8bit"
784 .ctrl_dev_name = "pinctrl.0",
785 .function = "mmc0",
786 .group = "mmc0_0_grp",
787 .dev_name = "foo-mmc.0",
788},
789{
790 .name "8bit"
791 .ctrl_dev_name = "pinctrl.0",
792 .function = "mmc0",
793 .group = "mmc0_1_grp",
794 .dev_name = "foo-mmc.0",
795},
796{
797 .name "8bit"
798 .ctrl_dev_name = "pinctrl.0",
799 .function = "mmc0",
800 .group = "mmc0_2_grp",
801 .dev_name = "foo-mmc.0",
802},
803...
804
805The result of grabbing this mapping from the device with something like
806this (see next paragraph):
807
808 pmx = pinmux_get(&device, "8bit");
809
810Will be that you activate all the three bottom records in the mapping at
811once. Since they share the same name, pin controller device, funcion and
812device, and since we allow multiple groups to match to a single device, they
813all get selected, and they all get enabled and disable simultaneously by the
814pinmux core.
815
816
817Pinmux requests from drivers
818============================
819
820Generally it is discouraged to let individual drivers get and enable pinmuxes.
821So if possible, handle the pinmuxes in platform code or some other place where
822you have access to all the affected struct device * pointers. In some cases
823where a driver needs to switch between different mux mappings at runtime
824this is not possible.
825
826A driver may request a certain mux to be activated, usually just the default
827mux like this:
828
829#include <linux/pinctrl/pinmux.h>
830
831struct foo_state {
832 struct pinmux *pmx;
833 ...
834};
835
836foo_probe()
837{
838 /* Allocate a state holder named "state" etc */
839 struct pinmux pmx;
840
841 pmx = pinmux_get(&device, NULL);
842 if IS_ERR(pmx)
843 return PTR_ERR(pmx);
844 pinmux_enable(pmx);
845
846 state->pmx = pmx;
847}
848
849foo_remove()
850{
851 pinmux_disable(state->pmx);
852 pinmux_put(state->pmx);
853}
854
855If you want to grab a specific mux mapping and not just the first one found for
856this device you can specify a specific mapping name, for example in the above
857example the second i2c0 setting: pinmux_get(&device, "spi0-pos-B");
858
859This get/enable/disable/put sequence can just as well be handled by bus drivers
860if you don't want each and every driver to handle it and you know the
861arrangement on your bus.
862
863The semantics of the get/enable respective disable/put is as follows:
864
865- pinmux_get() is called in process context to reserve the pins affected with
866 a certain mapping and set up the pinmux core and the driver. It will allocate
867 a struct from the kernel memory to hold the pinmux state.
868
869- pinmux_enable()/pinmux_disable() is quick and can be called from fastpath
870 (irq context) when you quickly want to set up/tear down the hardware muxing
871 when running a device driver. Usually it will just poke some values into a
872 register.
873
874- pinmux_disable() is called in process context to tear down the pin requests
875 and release the state holder struct for the mux setting.
876
877Usually the pinmux core handled the get/put pair and call out to the device
878drivers bookkeeping operations, like checking available functions and the
879associated pins, whereas the enable/disable pass on to the pin controller
880driver which takes care of activating and/or deactivating the mux setting by
881quickly poking some registers.
882
883The pins are allocated for your device when you issue the pinmux_get() call,
884after this you should be able to see this in the debugfs listing of all pins.
885
886
887System pinmux hogging
888=====================
889
890A system pinmux map entry, i.e. a pinmux setting that does not have a device
891associated with it, can be hogged by the core when the pin controller is
892registered. This means that the core will attempt to call pinmux_get() and
893pinmux_enable() on it immediately after the pin control device has been
894registered.
895
896This is enabled by simply setting the .hog_on_boot field in the map to true,
897like this:
898
899{
900 .name "POWERMAP"
901 .ctrl_dev_name = "pinctrl.0",
902 .function = "power_func",
903 .hog_on_boot = true,
904},
905
906Since it may be common to request the core to hog a few always-applicable
907mux settings on the primary pin controller, there is a convenience macro for
908this:
909
910PINMUX_MAP_PRIMARY_SYS_HOG("POWERMAP", "power_func")
911
912This gives the exact same result as the above construction.
913
914
915Runtime pinmuxing
916=================
917
918It is possible to mux a certain function in and out at runtime, say to move
919an SPI port from one set of pins to another set of pins. Say for example for
920spi0 in the example above, we expose two different groups of pins for the same
921function, but with different named in the mapping as described under
922"Advanced mapping" above. So we have two mappings named "spi0-pos-A" and
923"spi0-pos-B".
924
925This snippet first muxes the function in the pins defined by group A, enables
926it, disables and releases it, and muxes it in on the pins defined by group B:
927
928foo_switch()
929{
930 struct pinmux pmx;
931
932 /* Enable on position A */
933 pmx = pinmux_get(&device, "spi0-pos-A");
934 if IS_ERR(pmx)
935 return PTR_ERR(pmx);
936 pinmux_enable(pmx);
937
938 /* This releases the pins again */
939 pinmux_disable(pmx);
940 pinmux_put(pmx);
941
942 /* Enable on position B */
943 pmx = pinmux_get(&device, "spi0-pos-B");
944 if IS_ERR(pmx)
945 return PTR_ERR(pmx);
946 pinmux_enable(pmx);
947 ...
948}
949
950The above has to be done from process context.
diff --git a/MAINTAINERS b/MAINTAINERS
index ae8820e173a..e113be50cc0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5010,6 +5010,11 @@ L: linux-mtd@lists.infradead.org
5010S: Maintained 5010S: Maintained
5011F: drivers/mtd/devices/phram.c 5011F: drivers/mtd/devices/phram.c
5012 5012
5013PIN CONTROL SUBSYSTEM
5014M: Linus Walleij <linus.walleij@linaro.org>
5015S: Maintained
5016F: drivers/pinmux/
5017
5013PKTCDVD DRIVER 5018PKTCDVD DRIVER
5014M: Peter Osterlund <petero2@telia.com> 5019M: Peter Osterlund <petero2@telia.com>
5015S: Maintained 5020S: Maintained
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 95b9e7eefad..e73aaaee013 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -56,6 +56,8 @@ source "drivers/pps/Kconfig"
56 56
57source "drivers/ptp/Kconfig" 57source "drivers/ptp/Kconfig"
58 58
59source "drivers/pinctrl/Kconfig"
60
59source "drivers/gpio/Kconfig" 61source "drivers/gpio/Kconfig"
60 62
61source "drivers/w1/Kconfig" 63source "drivers/w1/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 7fa433a7030..e7afb3acbc6 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -5,6 +5,8 @@
5# Rewritten to use lists instead of if-statements. 5# Rewritten to use lists instead of if-statements.
6# 6#
7 7
8# GPIO must come after pinctrl as gpios may need to mux pins etc
9obj-y += pinctrl/
8obj-y += gpio/ 10obj-y += gpio/
9obj-$(CONFIG_PCI) += pci/ 11obj-$(CONFIG_PCI) += pci/
10obj-$(CONFIG_PARISC) += parisc/ 12obj-$(CONFIG_PARISC) += parisc/
diff --git a/drivers/pinctrl/Kconfig b/drivers/pinctrl/Kconfig
new file mode 100644
index 00000000000..02b4d4e92ff
--- /dev/null
+++ b/drivers/pinctrl/Kconfig
@@ -0,0 +1,29 @@
1#
2# PINCTRL infrastructure and drivers
3#
4
5menuconfig PINCTRL
6 bool "PINCTRL Support"
7 depends on EXPERIMENTAL
8 help
9 This enables the PINCTRL subsystem for controlling pins
10 on chip packages, for example multiplexing pins on primarily
11 PGA and BGA packages for systems on chip.
12
13 If unsure, say N.
14
15if PINCTRL
16
17config PINMUX
18 bool "Support pinmux controllers"
19 help
20 Say Y here if you want the pincontrol subsystem to handle pin
21 multiplexing drivers.
22
23config DEBUG_PINCTRL
24 bool "Debug PINCTRL calls"
25 depends on DEBUG_KERNEL
26 help
27 Say Y here to add some extra checks and diagnostics to PINCTRL calls.
28
29endif
diff --git a/drivers/pinctrl/Makefile b/drivers/pinctrl/Makefile
new file mode 100644
index 00000000000..596ce9f2289
--- /dev/null
+++ b/drivers/pinctrl/Makefile
@@ -0,0 +1,6 @@
1# generic pinmux support
2
3ccflags-$(CONFIG_DEBUG_PINMUX) += -DDEBUG
4
5obj-$(CONFIG_PINCTRL) += core.o
6obj-$(CONFIG_PINMUX) += pinmux.o
diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
new file mode 100644
index 00000000000..b2eaf8d4412
--- /dev/null
+++ b/drivers/pinctrl/core.c
@@ -0,0 +1,599 @@
1/*
2 * Core driver for the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#define pr_fmt(fmt) "pinctrl core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/radix-tree.h>
19#include <linux/err.h>
20#include <linux/list.h>
21#include <linux/mutex.h>
22#include <linux/spinlock.h>
23#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26#include <linux/pinctrl/pinctrl.h>
27#include <linux/pinctrl/machine.h>
28#include "core.h"
29#include "pinmux.h"
30
31/* Global list of pin control devices */
32static DEFINE_MUTEX(pinctrldev_list_mutex);
33static LIST_HEAD(pinctrldev_list);
34
35static void pinctrl_dev_release(struct device *dev)
36{
37 struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
38 kfree(pctldev);
39}
40
41const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
42{
43 /* We're not allowed to register devices without name */
44 return pctldev->desc->name;
45}
46EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
47
48void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
49{
50 return pctldev->driver_data;
51}
52EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
53
54/**
55 * get_pinctrl_dev_from_dev() - look up pin controller device
56 * @dev: a device pointer, this may be NULL but then devname needs to be
57 * defined instead
58 * @devname: the name of a device instance, as returned by dev_name(), this
59 * may be NULL but then dev needs to be defined instead
60 *
61 * Looks up a pin control device matching a certain device name or pure device
62 * pointer, the pure device pointer will take precedence.
63 */
64struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
65 const char *devname)
66{
67 struct pinctrl_dev *pctldev = NULL;
68 bool found = false;
69
70 mutex_lock(&pinctrldev_list_mutex);
71 list_for_each_entry(pctldev, &pinctrldev_list, node) {
72 if (dev && &pctldev->dev == dev) {
73 /* Matched on device pointer */
74 found = true;
75 break;
76 }
77
78 if (devname &&
79 !strcmp(dev_name(&pctldev->dev), devname)) {
80 /* Matched on device name */
81 found = true;
82 break;
83 }
84 }
85 mutex_unlock(&pinctrldev_list_mutex);
86
87 return found ? pctldev : NULL;
88}
89
90struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
91{
92 struct pin_desc *pindesc;
93 unsigned long flags;
94
95 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
96 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
97 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
98
99 return pindesc;
100}
101
102/**
103 * pin_is_valid() - check if pin exists on controller
104 * @pctldev: the pin control device to check the pin on
105 * @pin: pin to check, use the local pin controller index number
106 *
107 * This tells us whether a certain pin exist on a certain pin controller or
108 * not. Pin lists may be sparse, so some pins may not exist.
109 */
110bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
111{
112 struct pin_desc *pindesc;
113
114 if (pin < 0)
115 return false;
116
117 pindesc = pin_desc_get(pctldev, pin);
118 if (pindesc == NULL)
119 return false;
120
121 return true;
122}
123EXPORT_SYMBOL_GPL(pin_is_valid);
124
125/* Deletes a range of pin descriptors */
126static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
127 const struct pinctrl_pin_desc *pins,
128 unsigned num_pins)
129{
130 int i;
131
132 spin_lock(&pctldev->pin_desc_tree_lock);
133 for (i = 0; i < num_pins; i++) {
134 struct pin_desc *pindesc;
135
136 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
137 pins[i].number);
138 if (pindesc != NULL) {
139 radix_tree_delete(&pctldev->pin_desc_tree,
140 pins[i].number);
141 }
142 kfree(pindesc);
143 }
144 spin_unlock(&pctldev->pin_desc_tree_lock);
145}
146
147static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
148 unsigned number, const char *name)
149{
150 struct pin_desc *pindesc;
151
152 pindesc = pin_desc_get(pctldev, number);
153 if (pindesc != NULL) {
154 pr_err("pin %d already registered on %s\n", number,
155 pctldev->desc->name);
156 return -EINVAL;
157 }
158
159 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
160 if (pindesc == NULL)
161 return -ENOMEM;
162 spin_lock_init(&pindesc->lock);
163
164 /* Set owner */
165 pindesc->pctldev = pctldev;
166
167 /* Copy optional basic pin info */
168 if (name)
169 strlcpy(pindesc->name, name, sizeof(pindesc->name));
170
171 spin_lock(&pctldev->pin_desc_tree_lock);
172 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
173 spin_unlock(&pctldev->pin_desc_tree_lock);
174 pr_debug("registered pin %d (%s) on %s\n",
175 number, name ? name : "(unnamed)", pctldev->desc->name);
176 return 0;
177}
178
179static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
180 struct pinctrl_pin_desc const *pins,
181 unsigned num_descs)
182{
183 unsigned i;
184 int ret = 0;
185
186 for (i = 0; i < num_descs; i++) {
187 ret = pinctrl_register_one_pin(pctldev,
188 pins[i].number, pins[i].name);
189 if (ret)
190 return ret;
191 }
192
193 return 0;
194}
195
196/**
197 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
198 * @pctldev: pin controller device to check
199 * @gpio: gpio pin to check taken from the global GPIO pin space
200 *
201 * Tries to match a GPIO pin number to the ranges handled by a certain pin
202 * controller, return the range or NULL
203 */
204static struct pinctrl_gpio_range *
205pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
206{
207 struct pinctrl_gpio_range *range = NULL;
208
209 /* Loop over the ranges */
210 mutex_lock(&pctldev->gpio_ranges_lock);
211 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
212 /* Check if we're in the valid range */
213 if (gpio >= range->base &&
214 gpio < range->base + range->npins) {
215 mutex_unlock(&pctldev->gpio_ranges_lock);
216 return range;
217 }
218 }
219 mutex_unlock(&pctldev->gpio_ranges_lock);
220
221 return NULL;
222}
223
224/**
225 * pinctrl_get_device_gpio_range() - find device for GPIO range
226 * @gpio: the pin to locate the pin controller for
227 * @outdev: the pin control device if found
228 * @outrange: the GPIO range if found
229 *
230 * Find the pin controller handling a certain GPIO pin from the pinspace of
231 * the GPIO subsystem, return the device and the matching GPIO range. Returns
232 * negative if the GPIO range could not be found in any device.
233 */
234int pinctrl_get_device_gpio_range(unsigned gpio,
235 struct pinctrl_dev **outdev,
236 struct pinctrl_gpio_range **outrange)
237{
238 struct pinctrl_dev *pctldev = NULL;
239
240 /* Loop over the pin controllers */
241 mutex_lock(&pinctrldev_list_mutex);
242 list_for_each_entry(pctldev, &pinctrldev_list, node) {
243 struct pinctrl_gpio_range *range;
244
245 range = pinctrl_match_gpio_range(pctldev, gpio);
246 if (range != NULL) {
247 *outdev = pctldev;
248 *outrange = range;
249 mutex_unlock(&pinctrldev_list_mutex);
250 return 0;
251 }
252 }
253 mutex_unlock(&pinctrldev_list_mutex);
254
255 return -EINVAL;
256}
257
258/**
259 * pinctrl_add_gpio_range() - register a GPIO range for a controller
260 * @pctldev: pin controller device to add the range to
261 * @range: the GPIO range to add
262 *
263 * This adds a range of GPIOs to be handled by a certain pin controller. Call
264 * this to register handled ranges after registering your pin controller.
265 */
266void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
267 struct pinctrl_gpio_range *range)
268{
269 mutex_lock(&pctldev->gpio_ranges_lock);
270 list_add(&range->node, &pctldev->gpio_ranges);
271 mutex_unlock(&pctldev->gpio_ranges_lock);
272}
273
274/**
275 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
276 * @pctldev: pin controller device to remove the range from
277 * @range: the GPIO range to remove
278 */
279void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
280 struct pinctrl_gpio_range *range)
281{
282 mutex_lock(&pctldev->gpio_ranges_lock);
283 list_del(&range->node);
284 mutex_unlock(&pctldev->gpio_ranges_lock);
285}
286
287#ifdef CONFIG_DEBUG_FS
288
289static int pinctrl_pins_show(struct seq_file *s, void *what)
290{
291 struct pinctrl_dev *pctldev = s->private;
292 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
293 unsigned pin;
294
295 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
296 seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
297
298 /* The highest pin number need to be included in the loop, thus <= */
299 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
300 struct pin_desc *desc;
301
302 desc = pin_desc_get(pctldev, pin);
303 /* Pin space may be sparse */
304 if (desc == NULL)
305 continue;
306
307 seq_printf(s, "pin %d (%s) ", pin,
308 desc->name ? desc->name : "unnamed");
309
310 /* Driver-specific info per pin */
311 if (ops->pin_dbg_show)
312 ops->pin_dbg_show(pctldev, s, pin);
313
314 seq_puts(s, "\n");
315 }
316
317 return 0;
318}
319
320static int pinctrl_groups_show(struct seq_file *s, void *what)
321{
322 struct pinctrl_dev *pctldev = s->private;
323 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
324 unsigned selector = 0;
325
326 /* No grouping */
327 if (!ops)
328 return 0;
329
330 seq_puts(s, "registered pin groups:\n");
331 while (ops->list_groups(pctldev, selector) >= 0) {
332 unsigned *pins;
333 unsigned num_pins;
334 const char *gname = ops->get_group_name(pctldev, selector);
335 int ret;
336 int i;
337
338 ret = ops->get_group_pins(pctldev, selector,
339 &pins, &num_pins);
340 if (ret)
341 seq_printf(s, "%s [ERROR GETTING PINS]\n",
342 gname);
343 else {
344 seq_printf(s, "group: %s, pins = [ ", gname);
345 for (i = 0; i < num_pins; i++)
346 seq_printf(s, "%d ", pins[i]);
347 seq_puts(s, "]\n");
348 }
349 selector++;
350 }
351
352
353 return 0;
354}
355
356static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
357{
358 struct pinctrl_dev *pctldev = s->private;
359 struct pinctrl_gpio_range *range = NULL;
360
361 seq_puts(s, "GPIO ranges handled:\n");
362
363 /* Loop over the ranges */
364 mutex_lock(&pctldev->gpio_ranges_lock);
365 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
366 seq_printf(s, "%u: %s [%u - %u]\n", range->id, range->name,
367 range->base, (range->base + range->npins - 1));
368 }
369 mutex_unlock(&pctldev->gpio_ranges_lock);
370
371 return 0;
372}
373
374static int pinctrl_devices_show(struct seq_file *s, void *what)
375{
376 struct pinctrl_dev *pctldev;
377
378 seq_puts(s, "name [pinmux]\n");
379 mutex_lock(&pinctrldev_list_mutex);
380 list_for_each_entry(pctldev, &pinctrldev_list, node) {
381 seq_printf(s, "%s ", pctldev->desc->name);
382 if (pctldev->desc->pmxops)
383 seq_puts(s, "yes");
384 else
385 seq_puts(s, "no");
386 seq_puts(s, "\n");
387 }
388 mutex_unlock(&pinctrldev_list_mutex);
389
390 return 0;
391}
392
393static int pinctrl_pins_open(struct inode *inode, struct file *file)
394{
395 return single_open(file, pinctrl_pins_show, inode->i_private);
396}
397
398static int pinctrl_groups_open(struct inode *inode, struct file *file)
399{
400 return single_open(file, pinctrl_groups_show, inode->i_private);
401}
402
403static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
404{
405 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
406}
407
408static int pinctrl_devices_open(struct inode *inode, struct file *file)
409{
410 return single_open(file, pinctrl_devices_show, NULL);
411}
412
413static const struct file_operations pinctrl_pins_ops = {
414 .open = pinctrl_pins_open,
415 .read = seq_read,
416 .llseek = seq_lseek,
417 .release = single_release,
418};
419
420static const struct file_operations pinctrl_groups_ops = {
421 .open = pinctrl_groups_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = single_release,
425};
426
427static const struct file_operations pinctrl_gpioranges_ops = {
428 .open = pinctrl_gpioranges_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = single_release,
432};
433
434static const struct file_operations pinctrl_devices_ops = {
435 .open = pinctrl_devices_open,
436 .read = seq_read,
437 .llseek = seq_lseek,
438 .release = single_release,
439};
440
441static struct dentry *debugfs_root;
442
443static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
444{
445 static struct dentry *device_root;
446
447 device_root = debugfs_create_dir(dev_name(&pctldev->dev),
448 debugfs_root);
449 if (IS_ERR(device_root) || !device_root) {
450 pr_warn("failed to create debugfs directory for %s\n",
451 dev_name(&pctldev->dev));
452 return;
453 }
454 debugfs_create_file("pins", S_IFREG | S_IRUGO,
455 device_root, pctldev, &pinctrl_pins_ops);
456 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
457 device_root, pctldev, &pinctrl_groups_ops);
458 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
459 device_root, pctldev, &pinctrl_gpioranges_ops);
460 pinmux_init_device_debugfs(device_root, pctldev);
461}
462
463static void pinctrl_init_debugfs(void)
464{
465 debugfs_root = debugfs_create_dir("pinctrl", NULL);
466 if (IS_ERR(debugfs_root) || !debugfs_root) {
467 pr_warn("failed to create debugfs directory\n");
468 debugfs_root = NULL;
469 return;
470 }
471
472 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
473 debugfs_root, NULL, &pinctrl_devices_ops);
474 pinmux_init_debugfs(debugfs_root);
475}
476
477#else /* CONFIG_DEBUG_FS */
478
479static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
480{
481}
482
483static void pinctrl_init_debugfs(void)
484{
485}
486
487#endif
488
489/**
490 * pinctrl_register() - register a pin controller device
491 * @pctldesc: descriptor for this pin controller
492 * @dev: parent device for this pin controller
493 * @driver_data: private pin controller data for this pin controller
494 */
495struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
496 struct device *dev, void *driver_data)
497{
498 static atomic_t pinmux_no = ATOMIC_INIT(0);
499 struct pinctrl_dev *pctldev;
500 int ret;
501
502 if (pctldesc == NULL)
503 return NULL;
504 if (pctldesc->name == NULL)
505 return NULL;
506
507 /* If we're implementing pinmuxing, check the ops for sanity */
508 if (pctldesc->pmxops) {
509 ret = pinmux_check_ops(pctldesc->pmxops);
510 if (ret) {
511 pr_err("%s pinmux ops lacks necessary functions\n",
512 pctldesc->name);
513 return NULL;
514 }
515 }
516
517 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
518 if (pctldev == NULL)
519 return NULL;
520
521 /* Initialize pin control device struct */
522 pctldev->owner = pctldesc->owner;
523 pctldev->desc = pctldesc;
524 pctldev->driver_data = driver_data;
525 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
526 spin_lock_init(&pctldev->pin_desc_tree_lock);
527 INIT_LIST_HEAD(&pctldev->gpio_ranges);
528 mutex_init(&pctldev->gpio_ranges_lock);
529
530 /* Register device */
531 pctldev->dev.parent = dev;
532 dev_set_name(&pctldev->dev, "pinctrl.%d",
533 atomic_inc_return(&pinmux_no) - 1);
534 pctldev->dev.release = pinctrl_dev_release;
535 ret = device_register(&pctldev->dev);
536 if (ret != 0) {
537 pr_err("error in device registration\n");
538 goto out_reg_dev_err;
539 }
540 dev_set_drvdata(&pctldev->dev, pctldev);
541
542 /* Register all the pins */
543 pr_debug("try to register %d pins on %s...\n",
544 pctldesc->npins, pctldesc->name);
545 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
546 if (ret) {
547 pr_err("error during pin registration\n");
548 pinctrl_free_pindescs(pctldev, pctldesc->pins,
549 pctldesc->npins);
550 goto out_reg_pins_err;
551 }
552
553 pinctrl_init_device_debugfs(pctldev);
554 mutex_lock(&pinctrldev_list_mutex);
555 list_add(&pctldev->node, &pinctrldev_list);
556 mutex_unlock(&pinctrldev_list_mutex);
557 pinmux_hog_maps(pctldev);
558 return pctldev;
559
560out_reg_pins_err:
561 device_del(&pctldev->dev);
562out_reg_dev_err:
563 put_device(&pctldev->dev);
564 return NULL;
565}
566EXPORT_SYMBOL_GPL(pinctrl_register);
567
568/**
569 * pinctrl_unregister() - unregister pinmux
570 * @pctldev: pin controller to unregister
571 *
572 * Called by pinmux drivers to unregister a pinmux.
573 */
574void pinctrl_unregister(struct pinctrl_dev *pctldev)
575{
576 if (pctldev == NULL)
577 return;
578
579 pinmux_unhog_maps(pctldev);
580 /* TODO: check that no pinmuxes are still active? */
581 mutex_lock(&pinctrldev_list_mutex);
582 list_del(&pctldev->node);
583 mutex_unlock(&pinctrldev_list_mutex);
584 /* Destroy descriptor tree */
585 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
586 pctldev->desc->npins);
587 device_unregister(&pctldev->dev);
588}
589EXPORT_SYMBOL_GPL(pinctrl_unregister);
590
591static int __init pinctrl_init(void)
592{
593 pr_info("initialized pinctrl subsystem\n");
594 pinctrl_init_debugfs();
595 return 0;
596}
597
598/* init early since many drivers really need to initialized pinmux early */
599core_initcall(pinctrl_init);
diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h
new file mode 100644
index 00000000000..17e07772160
--- /dev/null
+++ b/drivers/pinctrl/core.h
@@ -0,0 +1,72 @@
1/*
2 * Core private header for the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 *
7 * Author: Linus Walleij <linus.walleij@linaro.org>
8 *
9 * License terms: GNU General Public License (GPL) version 2
10 */
11
12/**
13 * struct pinctrl_dev - pin control class device
14 * @node: node to include this pin controller in the global pin controller list
15 * @desc: the pin controller descriptor supplied when initializing this pin
16 * controller
17 * @pin_desc_tree: each pin descriptor for this pin controller is stored in
18 * this radix tree
19 * @pin_desc_tree_lock: lock for the descriptor tree
20 * @gpio_ranges: a list of GPIO ranges that is handled by this pin controller,
21 * ranges are added to this list at runtime
22 * @gpio_ranges_lock: lock for the GPIO ranges list
23 * @dev: the device entry for this pin controller
24 * @owner: module providing the pin controller, used for refcounting
25 * @driver_data: driver data for drivers registering to the pin controller
26 * subsystem
27 * @pinmux_hogs_lock: lock for the pinmux hog list
28 * @pinmux_hogs: list of pinmux maps hogged by this device
29 */
30struct pinctrl_dev {
31 struct list_head node;
32 struct pinctrl_desc *desc;
33 struct radix_tree_root pin_desc_tree;
34 spinlock_t pin_desc_tree_lock;
35 struct list_head gpio_ranges;
36 struct mutex gpio_ranges_lock;
37 struct device dev;
38 struct module *owner;
39 void *driver_data;
40#ifdef CONFIG_PINMUX
41 struct mutex pinmux_hogs_lock;
42 struct list_head pinmux_hogs;
43#endif
44};
45
46/**
47 * struct pin_desc - pin descriptor for each physical pin in the arch
48 * @pctldev: corresponding pin control device
49 * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
50 * datasheet or such
51 * @lock: a lock to protect the descriptor structure
52 * @mux_requested: whether the pin is already requested by pinmux or not
53 * @mux_function: a named muxing function for the pin that will be passed to
54 * subdrivers and shown in debugfs etc
55 */
56struct pin_desc {
57 struct pinctrl_dev *pctldev;
58 char name[16];
59 spinlock_t lock;
60 /* These fields only added when supporting pinmux drivers */
61#ifdef CONFIG_PINMUX
62 bool mux_requested;
63 char mux_function[16];
64#endif
65};
66
67struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
68 const char *dev_name);
69struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin);
70int pinctrl_get_device_gpio_range(unsigned gpio,
71 struct pinctrl_dev **outdev,
72 struct pinctrl_gpio_range **outrange);
diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c
new file mode 100644
index 00000000000..6544d98b2cf
--- /dev/null
+++ b/drivers/pinctrl/pinmux.c
@@ -0,0 +1,1180 @@
1/*
2 * Core driver for the pin muxing portions of the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#define pr_fmt(fmt) "pinmux core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
18#include <linux/slab.h>
19#include <linux/radix-tree.h>
20#include <linux/err.h>
21#include <linux/list.h>
22#include <linux/mutex.h>
23#include <linux/spinlock.h>
24#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/pinctrl/machine.h>
28#include <linux/pinctrl/pinmux.h>
29#include "core.h"
30
31/* List of pinmuxes */
32static DEFINE_MUTEX(pinmux_list_mutex);
33static LIST_HEAD(pinmux_list);
34
35/* List of pinmux hogs */
36static DEFINE_MUTEX(pinmux_hoglist_mutex);
37static LIST_HEAD(pinmux_hoglist);
38
39/* Global pinmux maps, we allow one set only */
40static struct pinmux_map const *pinmux_maps;
41static unsigned pinmux_maps_num;
42
43/**
44 * struct pinmux_group - group list item for pinmux groups
45 * @node: pinmux group list node
46 * @group_selector: the group selector for this group
47 */
48struct pinmux_group {
49 struct list_head node;
50 unsigned group_selector;
51};
52
53/**
54 * struct pinmux - per-device pinmux state holder
55 * @node: global list node
56 * @dev: the device using this pinmux
57 * @usecount: the number of active users of this mux setting, used to keep
58 * track of nested use cases
59 * @pins: an array of discrete physical pins used in this mapping, taken
60 * from the global pin enumeration space (copied from pinmux map)
61 * @num_pins: the number of pins in this mapping array, i.e. the number of
62 * elements in .pins so we can iterate over that array (copied from
63 * pinmux map)
64 * @pctldev: pin control device handling this pinmux
65 * @func_selector: the function selector for the pinmux device handling
66 * this pinmux
67 * @groups: the group selectors for the pinmux device and
68 * selector combination handling this pinmux, this is a list that
69 * will be traversed on all pinmux operations such as
70 * get/put/enable/disable
71 * @mutex: a lock for the pinmux state holder
72 */
73struct pinmux {
74 struct list_head node;
75 struct device *dev;
76 unsigned usecount;
77 struct pinctrl_dev *pctldev;
78 unsigned func_selector;
79 struct list_head groups;
80 struct mutex mutex;
81};
82
83/**
84 * struct pinmux_hog - a list item to stash mux hogs
85 * @node: pinmux hog list node
86 * @map: map entry responsible for this hogging
87 * @pmx: the pinmux hogged by this item
88 */
89struct pinmux_hog {
90 struct list_head node;
91 struct pinmux_map const *map;
92 struct pinmux *pmx;
93};
94
95/**
96 * pin_request() - request a single pin to be muxed in, typically for GPIO
97 * @pin: the pin number in the global pin space
98 * @function: a functional name to give to this pin, passed to the driver
99 * so it knows what function to mux in, e.g. the string "gpioNN"
100 * means that you want to mux in the pin for use as GPIO number NN
101 * @gpio: if this request concerns a single GPIO pin
102 * @gpio_range: the range matching the GPIO pin if this is a request for a
103 * single GPIO pin
104 */
105static int pin_request(struct pinctrl_dev *pctldev,
106 int pin, const char *function, bool gpio,
107 struct pinctrl_gpio_range *gpio_range)
108{
109 struct pin_desc *desc;
110 const struct pinmux_ops *ops = pctldev->desc->pmxops;
111 int status = -EINVAL;
112
113 dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function);
114
115 if (!pin_is_valid(pctldev, pin)) {
116 dev_err(&pctldev->dev, "pin is invalid\n");
117 return -EINVAL;
118 }
119
120 if (!function) {
121 dev_err(&pctldev->dev, "no function name given\n");
122 return -EINVAL;
123 }
124
125 desc = pin_desc_get(pctldev, pin);
126 if (desc == NULL) {
127 dev_err(&pctldev->dev,
128 "pin is not registered so it cannot be requested\n");
129 goto out;
130 }
131
132 spin_lock(&desc->lock);
133 if (desc->mux_requested) {
134 spin_unlock(&desc->lock);
135 dev_err(&pctldev->dev,
136 "pin already requested\n");
137 goto out;
138 }
139 desc->mux_requested = true;
140 strncpy(desc->mux_function, function, sizeof(desc->mux_function));
141 spin_unlock(&desc->lock);
142
143 /* Let each pin increase references to this module */
144 if (!try_module_get(pctldev->owner)) {
145 dev_err(&pctldev->dev,
146 "could not increase module refcount for pin %d\n",
147 pin);
148 status = -EINVAL;
149 goto out_free_pin;
150 }
151
152 /*
153 * If there is no kind of request function for the pin we just assume
154 * we got it by default and proceed.
155 */
156 if (gpio && ops->gpio_request_enable)
157 /* This requests and enables a single GPIO pin */
158 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
159 else if (ops->request)
160 status = ops->request(pctldev, pin);
161 else
162 status = 0;
163
164 if (status)
165 dev_err(&pctldev->dev, "->request on device %s failed "
166 "for pin %d\n",
167 pctldev->desc->name, pin);
168out_free_pin:
169 if (status) {
170 spin_lock(&desc->lock);
171 desc->mux_requested = false;
172 desc->mux_function[0] = '\0';
173 spin_unlock(&desc->lock);
174 }
175out:
176 if (status)
177 dev_err(&pctldev->dev, "pin-%d (%s) status %d\n",
178 pin, function ? : "?", status);
179
180 return status;
181}
182
183/**
184 * pin_free() - release a single muxed in pin so something else can be muxed
185 * @pctldev: pin controller device handling this pin
186 * @pin: the pin to free
187 */
188static void pin_free(struct pinctrl_dev *pctldev, int pin)
189{
190 const struct pinmux_ops *ops = pctldev->desc->pmxops;
191 struct pin_desc *desc;
192
193 desc = pin_desc_get(pctldev, pin);
194 if (desc == NULL) {
195 dev_err(&pctldev->dev,
196 "pin is not registered so it cannot be freed\n");
197 return;
198 }
199
200 if (ops->free)
201 ops->free(pctldev, pin);
202
203 spin_lock(&desc->lock);
204 desc->mux_requested = false;
205 desc->mux_function[0] = '\0';
206 spin_unlock(&desc->lock);
207 module_put(pctldev->owner);
208}
209
210/**
211 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
212 * @gpio: the GPIO pin number from the GPIO subsystem number space
213 */
214int pinmux_request_gpio(unsigned gpio)
215{
216 char gpiostr[16];
217 struct pinctrl_dev *pctldev;
218 struct pinctrl_gpio_range *range;
219 int ret;
220 int pin;
221
222 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
223 if (ret)
224 return -EINVAL;
225
226 /* Convert to the pin controllers number space */
227 pin = gpio - range->base;
228
229 /* Conjure some name stating what chip and pin this is taken by */
230 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
231
232 return pin_request(pctldev, pin, gpiostr, true, range);
233}
234EXPORT_SYMBOL_GPL(pinmux_request_gpio);
235
236/**
237 * pinmux_free_gpio() - free a single pin, currently used as GPIO
238 * @gpio: the GPIO pin number from the GPIO subsystem number space
239 */
240void pinmux_free_gpio(unsigned gpio)
241{
242 struct pinctrl_dev *pctldev;
243 struct pinctrl_gpio_range *range;
244 int ret;
245 int pin;
246
247 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
248 if (ret)
249 return;
250
251 /* Convert to the pin controllers number space */
252 pin = gpio - range->base;
253
254 pin_free(pctldev, pin);
255}
256EXPORT_SYMBOL_GPL(pinmux_free_gpio);
257
258/**
259 * pinmux_register_mappings() - register a set of pinmux mappings
260 * @maps: the pinmux mappings table to register
261 * @num_maps: the number of maps in the mapping table
262 *
263 * Only call this once during initialization of your machine, the function is
264 * tagged as __init and won't be callable after init has completed. The map
265 * passed into this function will be owned by the pinmux core and cannot be
266 * free:d.
267 */
268int __init pinmux_register_mappings(struct pinmux_map const *maps,
269 unsigned num_maps)
270{
271 int i;
272
273 if (pinmux_maps != NULL) {
274 pr_err("pinmux mappings already registered, you can only "
275 "register one set of maps\n");
276 return -EINVAL;
277 }
278
279 pr_debug("add %d pinmux maps\n", num_maps);
280 for (i = 0; i < num_maps; i++) {
281 /* Sanity check the mapping */
282 if (!maps[i].name) {
283 pr_err("failed to register map %d: "
284 "no map name given\n", i);
285 return -EINVAL;
286 }
287 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
288 pr_err("failed to register map %s (%d): "
289 "no pin control device given\n",
290 maps[i].name, i);
291 return -EINVAL;
292 }
293 if (!maps[i].function) {
294 pr_err("failed to register map %s (%d): "
295 "no function ID given\n", maps[i].name, i);
296 return -EINVAL;
297 }
298
299 if (!maps[i].dev && !maps[i].dev_name)
300 pr_debug("add system map %s function %s with no device\n",
301 maps[i].name,
302 maps[i].function);
303 else
304 pr_debug("register map %s, function %s\n",
305 maps[i].name,
306 maps[i].function);
307 }
308
309 pinmux_maps = maps;
310 pinmux_maps_num = num_maps;
311
312 return 0;
313}
314
315/**
316 * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
317 * @pctldev: the device to take the pins on
318 * @func_selector: the function selector to acquire the pins for
319 * @group_selector: the group selector containing the pins to acquire
320 */
321static int acquire_pins(struct pinctrl_dev *pctldev,
322 unsigned func_selector,
323 unsigned group_selector)
324{
325 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
326 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
327 const char *func = pmxops->get_function_name(pctldev,
328 func_selector);
329 unsigned *pins;
330 unsigned num_pins;
331 int ret;
332 int i;
333
334 ret = pctlops->get_group_pins(pctldev, group_selector,
335 &pins, &num_pins);
336 if (ret)
337 return ret;
338
339 dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n",
340 num_pins, group_selector);
341
342 /* Try to allocate all pins in this group, one by one */
343 for (i = 0; i < num_pins; i++) {
344 ret = pin_request(pctldev, pins[i], func, false, NULL);
345 if (ret) {
346 dev_err(&pctldev->dev,
347 "could not get pin %d for function %s "
348 "on device %s - conflicting mux mappings?\n",
349 pins[i], func ? : "(undefined)",
350 pinctrl_dev_get_name(pctldev));
351 /* On error release all taken pins */
352 i--; /* this pin just failed */
353 for (; i >= 0; i--)
354 pin_free(pctldev, pins[i]);
355 return -ENODEV;
356 }
357 }
358 return 0;
359}
360
361/**
362 * release_pins() - release pins taken by earlier acquirement
363 * @pctldev: the device to free the pinx on
364 * @group_selector: the group selector containing the pins to free
365 */
366static void release_pins(struct pinctrl_dev *pctldev,
367 unsigned group_selector)
368{
369 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
370 unsigned *pins;
371 unsigned num_pins;
372 int ret;
373 int i;
374
375 ret = pctlops->get_group_pins(pctldev, group_selector,
376 &pins, &num_pins);
377 if (ret) {
378 dev_err(&pctldev->dev, "could not get pins to release for "
379 "group selector %d\n",
380 group_selector);
381 return;
382 }
383 for (i = 0; i < num_pins; i++)
384 pin_free(pctldev, pins[i]);
385}
386
387/**
388 * pinmux_get_group_selector() - returns the group selector for a group
389 * @pctldev: the pin controller handling the group
390 * @pin_group: the pin group to look up
391 */
392static int pinmux_get_group_selector(struct pinctrl_dev *pctldev,
393 const char *pin_group)
394{
395 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
396 unsigned group_selector = 0;
397
398 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
399 const char *gname = pctlops->get_group_name(pctldev,
400 group_selector);
401 if (!strcmp(gname, pin_group)) {
402 dev_dbg(&pctldev->dev,
403 "found group selector %u for %s\n",
404 group_selector,
405 pin_group);
406 return group_selector;
407 }
408
409 group_selector++;
410 }
411
412 dev_err(&pctldev->dev, "does not have pin group %s\n",
413 pin_group);
414
415 return -EINVAL;
416}
417
418/**
419 * pinmux_check_pin_group() - check function and pin group combo
420 * @pctldev: device to check the pin group vs function for
421 * @func_selector: the function selector to check the pin group for, we have
422 * already looked this up in the calling function
423 * @pin_group: the pin group to match to the function
424 *
425 * This function will check that the pinmux driver can supply the
426 * selected pin group for a certain function, returns the group selector if
427 * the group and function selector will work fine together, else returns
428 * negative
429 */
430static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
431 unsigned func_selector,
432 const char *pin_group)
433{
434 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
435 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
436 int ret;
437
438 /*
439 * If the driver does not support different pin groups for the
440 * functions, we only support group 0, and assume this exists.
441 */
442 if (!pctlops || !pctlops->list_groups)
443 return 0;
444
445 /*
446 * Passing NULL (no specific group) will select the first and
447 * hopefully only group of pins available for this function.
448 */
449 if (!pin_group) {
450 char const * const *groups;
451 unsigned num_groups;
452
453 ret = pmxops->get_function_groups(pctldev, func_selector,
454 &groups, &num_groups);
455 if (ret)
456 return ret;
457 if (num_groups < 1)
458 return -EINVAL;
459 ret = pinmux_get_group_selector(pctldev, groups[0]);
460 if (ret < 0) {
461 dev_err(&pctldev->dev,
462 "function %s wants group %s but the pin "
463 "controller does not seem to have that group\n",
464 pmxops->get_function_name(pctldev, func_selector),
465 groups[0]);
466 return ret;
467 }
468
469 if (num_groups > 1)
470 dev_dbg(&pctldev->dev,
471 "function %s support more than one group, "
472 "default-selecting first group %s (%d)\n",
473 pmxops->get_function_name(pctldev, func_selector),
474 groups[0],
475 ret);
476
477 return ret;
478 }
479
480 dev_dbg(&pctldev->dev,
481 "check if we have pin group %s on controller %s\n",
482 pin_group, pinctrl_dev_get_name(pctldev));
483
484 ret = pinmux_get_group_selector(pctldev, pin_group);
485 if (ret < 0) {
486 dev_dbg(&pctldev->dev,
487 "%s does not support pin group %s with function %s\n",
488 pinctrl_dev_get_name(pctldev),
489 pin_group,
490 pmxops->get_function_name(pctldev, func_selector));
491 }
492 return ret;
493}
494
495/**
496 * pinmux_search_function() - check pin control driver for a certain function
497 * @pctldev: device to check for function and position
498 * @map: function map containing the function and position to look for
499 * @func_selector: returns the applicable function selector if found
500 * @group_selector: returns the applicable group selector if found
501 *
502 * This will search the pinmux driver for an applicable
503 * function with a specific pin group, returns 0 if these can be mapped
504 * negative otherwise
505 */
506static int pinmux_search_function(struct pinctrl_dev *pctldev,
507 struct pinmux_map const *map,
508 unsigned *func_selector,
509 unsigned *group_selector)
510{
511 const struct pinmux_ops *ops = pctldev->desc->pmxops;
512 unsigned selector = 0;
513
514 /* See if this pctldev has this function */
515 while (ops->list_functions(pctldev, selector) >= 0) {
516 const char *fname = ops->get_function_name(pctldev,
517 selector);
518 int ret;
519
520 if (!strcmp(map->function, fname)) {
521 /* Found the function, check pin group */
522 ret = pinmux_check_pin_group(pctldev, selector,
523 map->group);
524 if (ret < 0)
525 return ret;
526
527 /* This function and group selector can be used */
528 *func_selector = selector;
529 *group_selector = ret;
530 return 0;
531
532 }
533 selector++;
534 }
535
536 pr_err("%s does not support function %s\n",
537 pinctrl_dev_get_name(pctldev), map->function);
538 return -EINVAL;
539}
540
541/**
542 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
543 */
544static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
545 struct pinmux *pmx,
546 struct device *dev,
547 const char *devname,
548 struct pinmux_map const *map)
549{
550 unsigned func_selector;
551 unsigned group_selector;
552 struct pinmux_group *grp;
553 int ret;
554
555 /*
556 * Note that we're not locking the pinmux mutex here, because
557 * this is only called at pinmux initialization time when it
558 * has not been added to any list and thus is not reachable
559 * by anyone else.
560 */
561
562 if (pmx->pctldev && pmx->pctldev != pctldev) {
563 dev_err(&pctldev->dev,
564 "different pin control devices given for device %s, "
565 "function %s\n",
566 devname,
567 map->function);
568 return -EINVAL;
569 }
570 pmx->dev = dev;
571 pmx->pctldev = pctldev;
572
573 /* Now go into the driver and try to match a function and group */
574 ret = pinmux_search_function(pctldev, map, &func_selector,
575 &group_selector);
576 if (ret < 0)
577 return ret;
578
579 /*
580 * If the function selector is already set, it needs to be identical,
581 * we support several groups with one function but not several
582 * functions with one or several groups in the same pinmux.
583 */
584 if (pmx->func_selector != UINT_MAX &&
585 pmx->func_selector != func_selector) {
586 dev_err(&pctldev->dev,
587 "dual function defines in the map for device %s\n",
588 devname);
589 return -EINVAL;
590 }
591 pmx->func_selector = func_selector;
592
593 /* Now add this group selector, we may have many of them */
594 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
595 if (!grp)
596 return -ENOMEM;
597 grp->group_selector = group_selector;
598 ret = acquire_pins(pctldev, func_selector, group_selector);
599 if (ret) {
600 kfree(grp);
601 return ret;
602 }
603 list_add(&grp->node, &pmx->groups);
604
605 return 0;
606}
607
608static void pinmux_free_groups(struct pinmux *pmx)
609{
610 struct list_head *node, *tmp;
611
612 list_for_each_safe(node, tmp, &pmx->groups) {
613 struct pinmux_group *grp =
614 list_entry(node, struct pinmux_group, node);
615 /* Release all pins taken by this group */
616 release_pins(pmx->pctldev, grp->group_selector);
617 list_del(node);
618 kfree(grp);
619 }
620}
621
622/**
623 * pinmux_get() - retrieves the pinmux for a certain device
624 * @dev: the device to get the pinmux for
625 * @name: an optional specific mux mapping name or NULL, the name is only
626 * needed if you want to have more than one mapping per device, or if you
627 * need an anonymous pinmux (not tied to any specific device)
628 */
629struct pinmux *pinmux_get(struct device *dev, const char *name)
630{
631
632 struct pinmux_map const *map = NULL;
633 struct pinctrl_dev *pctldev = NULL;
634 const char *devname = NULL;
635 struct pinmux *pmx;
636 bool found_map;
637 unsigned num_maps = 0;
638 int ret = -ENODEV;
639 int i;
640
641 /* We must have dev or ID or both */
642 if (!dev && !name)
643 return ERR_PTR(-EINVAL);
644
645 if (dev)
646 devname = dev_name(dev);
647
648 pr_debug("get mux %s for device %s\n", name,
649 devname ? devname : "(none)");
650
651 /*
652 * create the state cookie holder struct pinmux for each
653 * mapping, this is what consumers will get when requesting
654 * a pinmux handle with pinmux_get()
655 */
656 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
657 if (pmx == NULL)
658 return ERR_PTR(-ENOMEM);
659 mutex_init(&pmx->mutex);
660 pmx->func_selector = UINT_MAX;
661 INIT_LIST_HEAD(&pmx->groups);
662
663 /* Iterate over the pinmux maps to locate the right ones */
664 for (i = 0; i < pinmux_maps_num; i++) {
665 map = &pinmux_maps[i];
666 found_map = false;
667
668 /*
669 * First, try to find the pctldev given in the map
670 */
671 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
672 map->ctrl_dev_name);
673 if (!pctldev) {
674 const char *devname = NULL;
675
676 if (map->ctrl_dev)
677 devname = dev_name(map->ctrl_dev);
678 else if (map->ctrl_dev_name)
679 devname = map->ctrl_dev_name;
680
681 pr_warning("could not find a pinctrl device for pinmux "
682 "function %s, fishy, they shall all have one\n",
683 map->function);
684 pr_warning("given pinctrl device name: %s",
685 devname ? devname : "UNDEFINED");
686
687 /* Continue to check the other mappings anyway... */
688 continue;
689 }
690
691 pr_debug("in map, found pctldev %s to handle function %s",
692 dev_name(&pctldev->dev), map->function);
693
694
695 /*
696 * If we're looking for a specific named map, this must match,
697 * else we loop and look for the next.
698 */
699 if (name != NULL) {
700 if (map->name == NULL)
701 continue;
702 if (strcmp(map->name, name))
703 continue;
704 }
705
706 /*
707 * This is for the case where no device name is given, we
708 * already know that the function name matches from above
709 * code.
710 */
711 if (!map->dev_name && (name != NULL))
712 found_map = true;
713
714 /* If the mapping has a device set up it must match */
715 if (map->dev_name &&
716 (!devname || !strcmp(map->dev_name, devname)))
717 /* MATCH! */
718 found_map = true;
719
720 /* If this map is applicable, then apply it */
721 if (found_map) {
722 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
723 devname, map);
724 if (ret) {
725 pinmux_free_groups(pmx);
726 kfree(pmx);
727 return ERR_PTR(ret);
728 }
729 num_maps++;
730 }
731 }
732
733
734 /* We should have atleast one map, right */
735 if (!num_maps) {
736 pr_err("could not find any mux maps for device %s, ID %s\n",
737 devname ? devname : "(anonymous)",
738 name ? name : "(undefined)");
739 kfree(pmx);
740 return ERR_PTR(-EINVAL);
741 }
742
743 pr_debug("found %u mux maps for device %s, UD %s\n",
744 num_maps,
745 devname ? devname : "(anonymous)",
746 name ? name : "(undefined)");
747
748 /* Add the pinmux to the global list */
749 mutex_lock(&pinmux_list_mutex);
750 list_add(&pmx->node, &pinmux_list);
751 mutex_unlock(&pinmux_list_mutex);
752
753 return pmx;
754}
755EXPORT_SYMBOL_GPL(pinmux_get);
756
757/**
758 * pinmux_put() - release a previously claimed pinmux
759 * @pmx: a pinmux previously claimed by pinmux_get()
760 */
761void pinmux_put(struct pinmux *pmx)
762{
763 if (pmx == NULL)
764 return;
765
766 mutex_lock(&pmx->mutex);
767 if (pmx->usecount)
768 pr_warn("releasing pinmux with active users!\n");
769 /* Free the groups and all acquired pins */
770 pinmux_free_groups(pmx);
771 mutex_unlock(&pmx->mutex);
772
773 /* Remove from list */
774 mutex_lock(&pinmux_list_mutex);
775 list_del(&pmx->node);
776 mutex_unlock(&pinmux_list_mutex);
777
778 kfree(pmx);
779}
780EXPORT_SYMBOL_GPL(pinmux_put);
781
782/**
783 * pinmux_enable() - enable a certain pinmux setting
784 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
785 */
786int pinmux_enable(struct pinmux *pmx)
787{
788 int ret = 0;
789
790 if (pmx == NULL)
791 return -EINVAL;
792 mutex_lock(&pmx->mutex);
793 if (pmx->usecount++ == 0) {
794 struct pinctrl_dev *pctldev = pmx->pctldev;
795 const struct pinmux_ops *ops = pctldev->desc->pmxops;
796 struct pinmux_group *grp;
797
798 list_for_each_entry(grp, &pmx->groups, node) {
799 ret = ops->enable(pctldev, pmx->func_selector,
800 grp->group_selector);
801 if (ret) {
802 /*
803 * TODO: call disable() on all groups we called
804 * enable() on to this point?
805 */
806 pmx->usecount--;
807 break;
808 }
809 }
810 }
811 mutex_unlock(&pmx->mutex);
812 return ret;
813}
814EXPORT_SYMBOL_GPL(pinmux_enable);
815
816/**
817 * pinmux_disable() - disable a certain pinmux setting
818 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
819 */
820void pinmux_disable(struct pinmux *pmx)
821{
822 if (pmx == NULL)
823 return;
824
825 mutex_lock(&pmx->mutex);
826 if (--pmx->usecount == 0) {
827 struct pinctrl_dev *pctldev = pmx->pctldev;
828 const struct pinmux_ops *ops = pctldev->desc->pmxops;
829 struct pinmux_group *grp;
830
831 list_for_each_entry(grp, &pmx->groups, node) {
832 ops->disable(pctldev, pmx->func_selector,
833 grp->group_selector);
834 }
835 }
836 mutex_unlock(&pmx->mutex);
837}
838EXPORT_SYMBOL_GPL(pinmux_disable);
839
840int pinmux_check_ops(const struct pinmux_ops *ops)
841{
842 /* Check that we implement required operations */
843 if (!ops->list_functions ||
844 !ops->get_function_name ||
845 !ops->get_function_groups ||
846 !ops->enable ||
847 !ops->disable)
848 return -EINVAL;
849
850 return 0;
851}
852
853/* Hog a single map entry and add to the hoglist */
854static int pinmux_hog_map(struct pinctrl_dev *pctldev,
855 struct pinmux_map const *map)
856{
857 struct pinmux_hog *hog;
858 struct pinmux *pmx;
859 int ret;
860
861 if (map->dev || map->dev_name) {
862 /*
863 * TODO: the day we have device tree support, we can
864 * traverse the device tree and hog to specific device nodes
865 * without any problems, so then we can hog pinmuxes for
866 * all devices that just want a static pin mux at this point.
867 */
868 dev_err(&pctldev->dev, "map %s wants to hog a non-system "
869 "pinmux, this is not going to work\n", map->name);
870 return -EINVAL;
871 }
872
873 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
874 if (!hog)
875 return -ENOMEM;
876
877 pmx = pinmux_get(NULL, map->name);
878 if (IS_ERR(pmx)) {
879 kfree(hog);
880 dev_err(&pctldev->dev,
881 "could not get the %s pinmux mapping for hogging\n",
882 map->name);
883 return PTR_ERR(pmx);
884 }
885
886 ret = pinmux_enable(pmx);
887 if (ret) {
888 pinmux_put(pmx);
889 kfree(hog);
890 dev_err(&pctldev->dev,
891 "could not enable the %s pinmux mapping for hogging\n",
892 map->name);
893 return ret;
894 }
895
896 hog->map = map;
897 hog->pmx = pmx;
898
899 dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
900 map->function);
901 mutex_lock(&pctldev->pinmux_hogs_lock);
902 list_add(&hog->node, &pctldev->pinmux_hogs);
903 mutex_unlock(&pctldev->pinmux_hogs_lock);
904
905 return 0;
906}
907
908/**
909 * pinmux_hog_maps() - hog specific map entries on controller device
910 * @pctldev: the pin control device to hog entries on
911 *
912 * When the pin controllers are registered, there may be some specific pinmux
913 * map entries that need to be hogged, i.e. get+enabled until the system shuts
914 * down.
915 */
916int pinmux_hog_maps(struct pinctrl_dev *pctldev)
917{
918 struct device *dev = &pctldev->dev;
919 const char *devname = dev_name(dev);
920 int ret;
921 int i;
922
923 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
924 mutex_init(&pctldev->pinmux_hogs_lock);
925
926 for (i = 0; i < pinmux_maps_num; i++) {
927 struct pinmux_map const *map = &pinmux_maps[i];
928
929 if (((map->ctrl_dev == dev) ||
930 !strcmp(map->ctrl_dev_name, devname)) &&
931 map->hog_on_boot) {
932 /* OK time to hog! */
933 ret = pinmux_hog_map(pctldev, map);
934 if (ret)
935 return ret;
936 }
937 }
938 return 0;
939}
940
941/**
942 * pinmux_hog_maps() - unhog specific map entries on controller device
943 * @pctldev: the pin control device to unhog entries on
944 */
945void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
946{
947 struct list_head *node, *tmp;
948
949 mutex_lock(&pctldev->pinmux_hogs_lock);
950 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
951 struct pinmux_hog *hog =
952 list_entry(node, struct pinmux_hog, node);
953 pinmux_disable(hog->pmx);
954 pinmux_put(hog->pmx);
955 list_del(node);
956 kfree(hog);
957 }
958 mutex_unlock(&pctldev->pinmux_hogs_lock);
959}
960
961#ifdef CONFIG_DEBUG_FS
962
963/* Called from pincontrol core */
964static int pinmux_functions_show(struct seq_file *s, void *what)
965{
966 struct pinctrl_dev *pctldev = s->private;
967 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
968 unsigned func_selector = 0;
969
970 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
971 const char *func = pmxops->get_function_name(pctldev,
972 func_selector);
973 const char * const *groups;
974 unsigned num_groups;
975 int ret;
976 int i;
977
978 ret = pmxops->get_function_groups(pctldev, func_selector,
979 &groups, &num_groups);
980 if (ret)
981 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
982 func);
983
984 seq_printf(s, "function: %s, groups = [ ", func);
985 for (i = 0; i < num_groups; i++)
986 seq_printf(s, "%s ", groups[i]);
987 seq_puts(s, "]\n");
988
989 func_selector++;
990
991 }
992
993 return 0;
994}
995
996static int pinmux_pins_show(struct seq_file *s, void *what)
997{
998 struct pinctrl_dev *pctldev = s->private;
999 unsigned pin;
1000
1001 seq_puts(s, "Pinmux settings per pin\n");
1002 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1003
1004 /* The highest pin number need to be included in the loop, thus <= */
1005 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
1006
1007 struct pin_desc *desc;
1008
1009 desc = pin_desc_get(pctldev, pin);
1010 /* Pin space may be sparse */
1011 if (desc == NULL)
1012 continue;
1013
1014 seq_printf(s, "pin %d (%s): %s\n", pin,
1015 desc->name ? desc->name : "unnamed",
1016 desc->mux_requested ? desc->mux_function : "UNCLAIMED");
1017 }
1018
1019 return 0;
1020}
1021
1022static int pinmux_hogs_show(struct seq_file *s, void *what)
1023{
1024 struct pinctrl_dev *pctldev = s->private;
1025 struct pinmux_hog *hog;
1026
1027 seq_puts(s, "Pinmux map hogs held by device\n");
1028
1029 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1030 seq_printf(s, "%s\n", hog->map->name);
1031
1032 return 0;
1033}
1034
1035static int pinmux_show(struct seq_file *s, void *what)
1036{
1037 struct pinmux *pmx;
1038
1039 seq_puts(s, "Requested pinmuxes and their maps:\n");
1040 list_for_each_entry(pmx, &pinmux_list, node) {
1041 struct pinctrl_dev *pctldev = pmx->pctldev;
1042 const struct pinmux_ops *pmxops;
1043 const struct pinctrl_ops *pctlops;
1044 struct pinmux_group *grp;
1045
1046 if (!pctldev) {
1047 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1048 continue;
1049 }
1050
1051 pmxops = pctldev->desc->pmxops;
1052 pctlops = pctldev->desc->pctlops;
1053
1054 seq_printf(s, "device: %s function: %s (%u),",
1055 pinctrl_dev_get_name(pmx->pctldev),
1056 pmxops->get_function_name(pctldev, pmx->func_selector),
1057 pmx->func_selector);
1058
1059 seq_printf(s, " groups: [");
1060 list_for_each_entry(grp, &pmx->groups, node) {
1061 seq_printf(s, " %s (%u)",
1062 pctlops->get_group_name(pctldev, grp->group_selector),
1063 grp->group_selector);
1064 }
1065 seq_printf(s, " ]");
1066
1067 seq_printf(s, " users: %u map-> %s\n",
1068 pmx->usecount,
1069 pmx->dev ? dev_name(pmx->dev) : "(system)");
1070 }
1071
1072 return 0;
1073}
1074
1075static int pinmux_maps_show(struct seq_file *s, void *what)
1076{
1077 int i;
1078
1079 seq_puts(s, "Pinmux maps:\n");
1080
1081 for (i = 0; i < pinmux_maps_num; i++) {
1082 struct pinmux_map const *map = &pinmux_maps[i];
1083
1084 seq_printf(s, "%s:\n", map->name);
1085 if (map->dev || map->dev_name)
1086 seq_printf(s, " device: %s\n",
1087 map->dev ? dev_name(map->dev) :
1088 map->dev_name);
1089 else
1090 seq_printf(s, " SYSTEM MUX\n");
1091 seq_printf(s, " controlling device %s\n",
1092 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1093 map->ctrl_dev_name);
1094 seq_printf(s, " function: %s\n", map->function);
1095 seq_printf(s, " group: %s\n", map->group ? map->group :
1096 "(default)");
1097 }
1098 return 0;
1099}
1100
1101static int pinmux_functions_open(struct inode *inode, struct file *file)
1102{
1103 return single_open(file, pinmux_functions_show, inode->i_private);
1104}
1105
1106static int pinmux_pins_open(struct inode *inode, struct file *file)
1107{
1108 return single_open(file, pinmux_pins_show, inode->i_private);
1109}
1110
1111static int pinmux_hogs_open(struct inode *inode, struct file *file)
1112{
1113 return single_open(file, pinmux_hogs_show, inode->i_private);
1114}
1115
1116static int pinmux_open(struct inode *inode, struct file *file)
1117{
1118 return single_open(file, pinmux_show, NULL);
1119}
1120
1121static int pinmux_maps_open(struct inode *inode, struct file *file)
1122{
1123 return single_open(file, pinmux_maps_show, NULL);
1124}
1125
1126static const struct file_operations pinmux_functions_ops = {
1127 .open = pinmux_functions_open,
1128 .read = seq_read,
1129 .llseek = seq_lseek,
1130 .release = single_release,
1131};
1132
1133static const struct file_operations pinmux_pins_ops = {
1134 .open = pinmux_pins_open,
1135 .read = seq_read,
1136 .llseek = seq_lseek,
1137 .release = single_release,
1138};
1139
1140static const struct file_operations pinmux_hogs_ops = {
1141 .open = pinmux_hogs_open,
1142 .read = seq_read,
1143 .llseek = seq_lseek,
1144 .release = single_release,
1145};
1146
1147static const struct file_operations pinmux_ops = {
1148 .open = pinmux_open,
1149 .read = seq_read,
1150 .llseek = seq_lseek,
1151 .release = single_release,
1152};
1153
1154static const struct file_operations pinmux_maps_ops = {
1155 .open = pinmux_maps_open,
1156 .read = seq_read,
1157 .llseek = seq_lseek,
1158 .release = single_release,
1159};
1160
1161void pinmux_init_device_debugfs(struct dentry *devroot,
1162 struct pinctrl_dev *pctldev)
1163{
1164 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1165 devroot, pctldev, &pinmux_functions_ops);
1166 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1167 devroot, pctldev, &pinmux_pins_ops);
1168 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1169 devroot, pctldev, &pinmux_hogs_ops);
1170}
1171
1172void pinmux_init_debugfs(struct dentry *subsys_root)
1173{
1174 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1175 subsys_root, NULL, &pinmux_ops);
1176 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1177 subsys_root, NULL, &pinmux_maps_ops);
1178}
1179
1180#endif /* CONFIG_DEBUG_FS */
diff --git a/drivers/pinctrl/pinmux.h b/drivers/pinctrl/pinmux.h
new file mode 100644
index 00000000000..844500b3331
--- /dev/null
+++ b/drivers/pinctrl/pinmux.h
@@ -0,0 +1,47 @@
1/*
2 * Internal interface between the core pin control system and the
3 * pinmux portions
4 *
5 * Copyright (C) 2011 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
11 * License terms: GNU General Public License (GPL) version 2
12 */
13#ifdef CONFIG_PINMUX
14
15int pinmux_check_ops(const struct pinmux_ops *ops);
16void pinmux_init_device_debugfs(struct dentry *devroot,
17 struct pinctrl_dev *pctldev);
18void pinmux_init_debugfs(struct dentry *subsys_root);
19int pinmux_hog_maps(struct pinctrl_dev *pctldev);
20void pinmux_unhog_maps(struct pinctrl_dev *pctldev);
21
22#else
23
24static inline int pinmux_check_ops(const struct pinmux_ops *ops)
25{
26 return 0;
27}
28
29static inline void pinmux_init_device_debugfs(struct dentry *devroot,
30 struct pinctrl_dev *pctldev)
31{
32}
33
34static inline void pinmux_init_debugfs(struct dentry *subsys_root)
35{
36}
37
38static inline int pinmux_hog_maps(struct pinctrl_dev *pctldev)
39{
40 return 0;
41}
42
43static inline void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
44{
45}
46
47#endif
diff --git a/include/linux/pinctrl/machine.h b/include/linux/pinctrl/machine.h
new file mode 100644
index 00000000000..88863531d86
--- /dev/null
+++ b/include/linux/pinctrl/machine.h
@@ -0,0 +1,107 @@
1/*
2 * Machine interface for the pinctrl subsystem.
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#ifndef __LINUX_PINMUX_MACHINE_H
13#define __LINUX_PINMUX_MACHINE_H
14
15/**
16 * struct pinmux_map - boards/machines shall provide this map for devices
17 * @name: the name of this specific map entry for the particular machine.
18 * This is the second parameter passed to pinmux_get() when you want
19 * to have several mappings to the same device
20 * @ctrl_dev: the pin control device to be used by this mapping, may be NULL
21 * if you provide .ctrl_dev_name instead (this is more common)
22 * @ctrl_dev_name: the name of the device controlling this specific mapping,
23 * the name must be the same as in your struct device*, may be NULL if
24 * you provide .ctrl_dev instead
25 * @function: a function in the driver to use for this mapping, the driver
26 * will lookup the function referenced by this ID on the specified
27 * pin control device
28 * @group: sometimes a function can map to different pin groups, so this
29 * selects a certain specific pin group to activate for the function, if
30 * left as NULL, the first applicable group will be used
31 * @dev: the device using this specific mapping, may be NULL if you provide
32 * .dev_name instead (this is more common)
33 * @dev_name: the name of the device using this specific mapping, the name
34 * must be the same as in your struct device*, may be NULL if you
35 * provide .dev instead
36 * @hog_on_boot: if this is set to true, the pin control subsystem will itself
37 * hog the mappings as the pinmux device drivers are attached, so this is
38 * typically used with system maps (mux mappings without an assigned
39 * device) that you want to get hogged and enabled by default as soon as
40 * a pinmux device supporting it is registered. These maps will not be
41 * disabled and put until the system shuts down.
42 */
43struct pinmux_map {
44 const char *name;
45 struct device *ctrl_dev;
46 const char *ctrl_dev_name;
47 const char *function;
48 const char *group;
49 struct device *dev;
50 const char *dev_name;
51 const bool hog_on_boot;
52};
53
54/*
55 * Convenience macro to set a simple map from a certain pin controller and a
56 * certain function to a named device
57 */
58#define PINMUX_MAP(a, b, c, d) \
59 { .name = a, .ctrl_dev_name = b, .function = c, .dev_name = d }
60
61/*
62 * Convenience macro to map a system function onto a certain pinctrl device.
63 * System functions are not assigned to a particular device.
64 */
65#define PINMUX_MAP_SYS(a, b, c) \
66 { .name = a, .ctrl_dev_name = b, .function = c }
67
68/*
69 * Convenience macro to map a function onto the primary device pinctrl device
70 * this is especially helpful on systems that have only one pin controller
71 * or need to set up a lot of mappings on the primary controller.
72 */
73#define PINMUX_MAP_PRIMARY(a, b, c) \
74 { .name = a, .ctrl_dev_name = "pinctrl.0", .function = b, \
75 .dev_name = c }
76
77/*
78 * Convenience macro to map a system function onto the primary pinctrl device.
79 * System functions are not assigned to a particular device.
80 */
81#define PINMUX_MAP_PRIMARY_SYS(a, b) \
82 { .name = a, .ctrl_dev_name = "pinctrl.0", .function = b }
83
84/*
85 * Convenience macro to map a system function onto the primary pinctrl device,
86 * to be hogged by the pinmux core until the system shuts down.
87 */
88#define PINMUX_MAP_PRIMARY_SYS_HOG(a, b) \
89 { .name = a, .ctrl_dev_name = "pinctrl.0", .function = b, \
90 .hog_on_boot = true }
91
92
93#ifdef CONFIG_PINMUX
94
95extern int pinmux_register_mappings(struct pinmux_map const *map,
96 unsigned num_maps);
97
98#else
99
100static inline int pinmux_register_mappings(struct pinmux_map const *map,
101 unsigned num_maps)
102{
103 return 0;
104}
105
106#endif /* !CONFIG_PINMUX */
107#endif
diff --git a/include/linux/pinctrl/pinctrl.h b/include/linux/pinctrl/pinctrl.h
new file mode 100644
index 00000000000..4f8d2089acc
--- /dev/null
+++ b/include/linux/pinctrl/pinctrl.h
@@ -0,0 +1,133 @@
1/*
2 * Interface the pinctrl subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * This interface is used in the core to keep track of pins.
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#ifndef __LINUX_PINCTRL_PINCTRL_H
13#define __LINUX_PINCTRL_PINCTRL_H
14
15#ifdef CONFIG_PINCTRL
16
17#include <linux/radix-tree.h>
18#include <linux/spinlock.h>
19#include <linux/list.h>
20#include <linux/seq_file.h>
21
22struct pinctrl_dev;
23struct pinmux_ops;
24struct gpio_chip;
25
26/**
27 * struct pinctrl_pin_desc - boards/machines provide information on their
28 * pins, pads or other muxable units in this struct
29 * @number: unique pin number from the global pin number space
30 * @name: a name for this pin
31 */
32struct pinctrl_pin_desc {
33 unsigned number;
34 const char *name;
35};
36
37/* Convenience macro to define a single named or anonymous pin descriptor */
38#define PINCTRL_PIN(a, b) { .number = a, .name = b }
39#define PINCTRL_PIN_ANON(a) { .number = a }
40
41/**
42 * struct pinctrl_gpio_range - each pin controller can provide subranges of
43 * the GPIO number space to be handled by the controller
44 * @node: list node for internal use
45 * @name: a name for the chip in this range
46 * @id: an ID number for the chip in this range
47 * @base: base offset of the GPIO range
48 * @npins: number of pins in the GPIO range, including the base number
49 * @gc: an optional pointer to a gpio_chip
50 */
51struct pinctrl_gpio_range {
52 struct list_head node;
53 const char *name;
54 unsigned int id;
55 unsigned int base;
56 unsigned int npins;
57 struct gpio_chip *gc;
58};
59
60/**
61 * struct pinctrl_ops - global pin control operations, to be implemented by
62 * pin controller drivers.
63 * @list_groups: list the number of selectable named groups available
64 * in this pinmux driver, the core will begin on 0 and call this
65 * repeatedly as long as it returns >= 0 to enumerate the groups
66 * @get_group_name: return the group name of the pin group
67 * @get_group_pins: return an array of pins corresponding to a certain
68 * group selector @pins, and the size of the array in @num_pins
69 * @pin_dbg_show: optional debugfs display hook that will provide per-device
70 * info for a certain pin in debugfs
71 */
72struct pinctrl_ops {
73 int (*list_groups) (struct pinctrl_dev *pctldev, unsigned selector);
74 const char *(*get_group_name) (struct pinctrl_dev *pctldev,
75 unsigned selector);
76 int (*get_group_pins) (struct pinctrl_dev *pctldev,
77 unsigned selector,
78 unsigned ** const pins,
79 unsigned * const num_pins);
80 void (*pin_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,
81 unsigned offset);
82};
83
84/**
85 * struct pinctrl_desc - pin controller descriptor, register this to pin
86 * control subsystem
87 * @name: name for the pin controller
88 * @pins: an array of pin descriptors describing all the pins handled by
89 * this pin controller
90 * @npins: number of descriptors in the array, usually just ARRAY_SIZE()
91 * of the pins field above
92 * @maxpin: since pin spaces may be sparse, there can he "holes" in the
93 * pin range, this attribute gives the maximum pin number in the
94 * total range. This should not be lower than npins for example,
95 * but may be equal to npins if you have no holes in the pin range.
96 * @pctlops: pin control operation vtable, to support global concepts like
97 * grouping of pins, this is optional.
98 * @pmxops: pinmux operation vtable, if you support pinmuxing in your driver
99 * @owner: module providing the pin controller, used for refcounting
100 */
101struct pinctrl_desc {
102 const char *name;
103 struct pinctrl_pin_desc const *pins;
104 unsigned int npins;
105 unsigned int maxpin;
106 struct pinctrl_ops *pctlops;
107 struct pinmux_ops *pmxops;
108 struct module *owner;
109};
110
111/* External interface to pin controller */
112extern struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
113 struct device *dev, void *driver_data);
114extern void pinctrl_unregister(struct pinctrl_dev *pctldev);
115extern bool pin_is_valid(struct pinctrl_dev *pctldev, int pin);
116extern void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
117 struct pinctrl_gpio_range *range);
118extern void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
119 struct pinctrl_gpio_range *range);
120extern const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev);
121extern void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev);
122#else
123
124
125/* Sufficiently stupid default function when pinctrl is not in use */
126static inline bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
127{
128 return pin >= 0;
129}
130
131#endif /* !CONFIG_PINCTRL */
132
133#endif /* __LINUX_PINCTRL_PINCTRL_H */
diff --git a/include/linux/pinctrl/pinmux.h b/include/linux/pinctrl/pinmux.h
new file mode 100644
index 00000000000..3c430e797ef
--- /dev/null
+++ b/include/linux/pinctrl/pinmux.h
@@ -0,0 +1,117 @@
1/*
2 * Interface the pinmux subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#ifndef __LINUX_PINCTRL_PINMUX_H
13#define __LINUX_PINCTRL_PINMUX_H
14
15#include <linux/list.h>
16#include <linux/seq_file.h>
17#include "pinctrl.h"
18
19/* This struct is private to the core and should be regarded as a cookie */
20struct pinmux;
21
22#ifdef CONFIG_PINMUX
23
24struct pinctrl_dev;
25
26/**
27 * struct pinmux_ops - pinmux operations, to be implemented by pin controller
28 * drivers that support pinmuxing
29 * @request: called by the core to see if a certain pin can be made available
30 * available for muxing. This is called by the core to acquire the pins
31 * before selecting any actual mux setting across a function. The driver
32 * is allowed to answer "no" by returning a negative error code
33 * @free: the reverse function of the request() callback, frees a pin after
34 * being requested
35 * @list_functions: list the number of selectable named functions available
36 * in this pinmux driver, the core will begin on 0 and call this
37 * repeatedly as long as it returns >= 0 to enumerate mux settings
38 * @get_function_name: return the function name of the muxing selector,
39 * called by the core to figure out which mux setting it shall map a
40 * certain device to
41 * @get_function_groups: return an array of groups names (in turn
42 * referencing pins) connected to a certain function selector. The group
43 * name can be used with the generic @pinctrl_ops to retrieve the
44 * actual pins affected. The applicable groups will be returned in
45 * @groups and the number of groups in @num_groups
46 * @enable: enable a certain muxing function with a certain pin group. The
47 * driver does not need to figure out whether enabling this function
48 * conflicts some other use of the pins in that group, such collisions
49 * are handled by the pinmux subsystem. The @func_selector selects a
50 * certain function whereas @group_selector selects a certain set of pins
51 * to be used. On simple controllers the latter argument may be ignored
52 * @disable: disable a certain muxing selector with a certain pin group
53 * @gpio_request_enable: requests and enables GPIO on a certain pin.
54 * Implement this only if you can mux every pin individually as GPIO. The
55 * affected GPIO range is passed along with an offset into that
56 * specific GPIO range - function selectors and pin groups are orthogonal
57 * to this, the core will however make sure the pins do not collide
58 */
59struct pinmux_ops {
60 int (*request) (struct pinctrl_dev *pctldev, unsigned offset);
61 int (*free) (struct pinctrl_dev *pctldev, unsigned offset);
62 int (*list_functions) (struct pinctrl_dev *pctldev, unsigned selector);
63 const char *(*get_function_name) (struct pinctrl_dev *pctldev,
64 unsigned selector);
65 int (*get_function_groups) (struct pinctrl_dev *pctldev,
66 unsigned selector,
67 const char * const **groups,
68 unsigned * const num_groups);
69 int (*enable) (struct pinctrl_dev *pctldev, unsigned func_selector,
70 unsigned group_selector);
71 void (*disable) (struct pinctrl_dev *pctldev, unsigned func_selector,
72 unsigned group_selector);
73 int (*gpio_request_enable) (struct pinctrl_dev *pctldev,
74 struct pinctrl_gpio_range *range,
75 unsigned offset);
76};
77
78/* External interface to pinmux */
79extern int pinmux_request_gpio(unsigned gpio);
80extern void pinmux_free_gpio(unsigned gpio);
81extern struct pinmux * __must_check pinmux_get(struct device *dev, const char *name);
82extern void pinmux_put(struct pinmux *pmx);
83extern int pinmux_enable(struct pinmux *pmx);
84extern void pinmux_disable(struct pinmux *pmx);
85
86#else /* !CONFIG_PINMUX */
87
88static inline int pinmux_request_gpio(unsigned gpio)
89{
90 return 0;
91}
92
93static inline void pinmux_free_gpio(unsigned gpio)
94{
95}
96
97static inline struct pinmux * __must_check pinmux_get(struct device *dev, const char *name)
98{
99 return NULL;
100}
101
102static inline void pinmux_put(struct pinmux *pmx)
103{
104}
105
106static inline int pinmux_enable(struct pinmux *pmx)
107{
108 return 0;
109}
110
111static inline void pinmux_disable(struct pinmux *pmx)
112{
113}
114
115#endif /* CONFIG_PINMUX */
116
117#endif /* __LINUX_PINCTRL_PINMUX_H */