Re: [PATCH 1/2] drivers: create a pinmux subsystem v3

From: Barry Song
Date: Thu Jul 14 2011 - 01:57:53 EST


2011/6/14 Linus Walleij <linus.walleij@xxxxxxxxxxxxxx>:
> From: Linus Walleij <linus.walleij@xxxxxxxxxx>
>
> This creates a subsystem for handling of pinmux devices. These are
> devices that enable and disable groups of pins on primarily PGA and
> BGA type of chip packages and common in embedded systems.
>
> This is being done to depopulate the arch/arm/* directory of such
> custom drivers and try to abstract the infrastructure they all
> need. See the Documentation/pinmux.txt file that is part of this
> patch for more details.
>
> Cc: Grant Likely <grant.likely@xxxxxxxxxxxx>
> Cc: Stephen Warren <swarren@xxxxxxxxxx>
> Cc: Joe Perches <joe@xxxxxxxxxxx>
> Cc: Russell King <linux@xxxxxxxxxxxxxxxx>
> Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
> ---
> ChangeLog v2->v3:
> - Renamed subsystem folder to "pinctrl" since we will likely
> Âwant to keep other pin control such as biasing in this
> Âsubsystem too, so let us keep to something generic even though
> Âwe're mainly doing pinmux now.
> - As a consequence, register pins as an abstract entity separate
> Âfrom the pinmux. The muxing functions will claim pins out of the
> Âpin pool and make sure they do not collide. Pins can now be
> Ânamed by the pinctrl core.
> - Converted the pin lookup from a static array into a radix tree,
> ÂI agreed with Grant Likely to try to avoid any static allocation
> Â(which is crap for device tree stuff) so I just rewrote this
> Âto be dynamic, just like irq number descriptors. The
> Âplatform-wide definition of number of pins goes away - this is
> Ânow just the sum total of the pins registered to the subsystem.
> - Make sure mappings with only a function name and no device
> Âworks properly.
> ---
> ÂDocumentation/ABI/testing/sysfs-class-pinmux | Â 11 +
> ÂDocumentation/pinctrl.txt          Â| Â397 ++++++++++
> ÂMAINTAINERS Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â| Â Â5 +
> Âdrivers/Kconfig               Â|  Â4 +
> Âdrivers/Makefile               |  Â2 +
> Âdrivers/pinctrl/Kconfig           Â|  29 +
> Âdrivers/pinctrl/Makefile           |  Â6 +
> Âdrivers/pinctrl/core.c            | 1028 ++++++++++++++++++++++++++
> Âinclude/linux/pinctrl/machine.h       Â|  57 ++
> Âinclude/linux/pinctrl/pinmux.h        | Â180 +++++
> Â10 files changed, 1719 insertions(+), 0 deletions(-)
> Âcreate mode 100644 Documentation/ABI/testing/sysfs-class-pinmux
> Âcreate mode 100644 Documentation/pinctrl.txt
> Âcreate mode 100644 drivers/pinctrl/Kconfig
> Âcreate mode 100644 drivers/pinctrl/Makefile
> Âcreate mode 100644 drivers/pinctrl/core.c
> Âcreate mode 100644 include/linux/pinctrl/machine.h
> Âcreate mode 100644 include/linux/pinctrl/pinmux.h
>
> diff --git a/Documentation/ABI/testing/sysfs-class-pinmux b/Documentation/ABI/testing/sysfs-class-pinmux
> new file mode 100644
> index 0000000..c2ea843
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-class-pinmux
> @@ -0,0 +1,11 @@
> +What: Â Â Â Â Â/sys/class/pinmux/.../name
> +Date: Â Â Â Â ÂMay 2011
> +KernelVersion: 3.1

has this been ready for 3.1? we have been planning to write pinmux
driver based on this framework.

> +Contact: Â Â Â Linus Walleij <linus.walleij@xxxxxxxxxx>
> +Description:
> + Â Â Â Â Â Â Â Each pinmux directory will contain a field called
> + Â Â Â Â Â Â Â name. This holds a string identifying the pinmux for
> + Â Â Â Â Â Â Â display purposes.
> +
> + Â Â Â Â Â Â Â NOTE: this will be empty if no suitable name is provided
> + Â Â Â Â Â Â Â by platform or pinmux drivers.
> diff --git a/Documentation/pinctrl.txt b/Documentation/pinctrl.txt
> new file mode 100644
> index 0000000..c4b6f48
> --- /dev/null
> +++ b/Documentation/pinctrl.txt
> @@ -0,0 +1,397 @@
> +PINCTRL (PIN CONTROL) subsystem
> +This document outlines the pin control subsystem in Linux
> +
> +This subsystem deals with:
> +
> +- Multiplexing of pins, pads, fingers (etc) see below for details
> +
> +The intention is to also deal with:
> +
> +- Software-controlled biasing and driving mode specific pins, such as
> + Âpull-up/down, open drain etc, load capacitance configuration when controlled
> + Âby software, etc.
> +
> +
> +Top-level interface
> +===================
> +
> +Definition of PIN:
> +
> +- PINS are equal to pads or fingers or whatever packaging output you want to
> + Âmux and these are denoted by unsigned integers in the range 0..MAX_INT. Every
> + Âpin on your system (or atleast every pin that can be muxed) should have a
> + Âunique number. The numberspace can span several chips if you have more chips
> + Âon your system that can be subject to muxing.
> +
> +All pins, pads, fingers, balls or other controllable units on the system shall
> +be given a unique number in the global number space beginning on zero.
> +
> +All pins on the system need to be registered, typically by board code or a
> +device tree. Board/machine code will #include <linux/pinctrl/machine.h>
> +and use one of:
> +
> + Âpinctrl_register_pins_sparse();
> + Âpinctrl_register_pins_dense();
> +
> +To register all the pins on the system. Both are supplied with a list of
> +descriptor items, the difference is that the _dense version will also
> +register anonymous pins for each "hole" in the pin map, from zero to the
> +supplied extra argument giving the number of pins.
> +
> +Here is an example of a PGA (Pin Grid Array) chip seen from underneath:
> +
> + Â Â Â ÂA Â B Â C Â D Â E Â F Â G Â H
> +
> +  8  Âo  o  o  o  o  o  o  o
> +
> +  7  Âo  o  o  o  o  o  o  o
> +
> +  6  Âo  o  o  o  o  o  o  o
> +
> +  5  Âo  o  o  o  o  o  o  o
> +
> +  4  Âo  o  o  o  o  o  o  o
> +
> +  3  Âo  o  o  o  o  o  o  o
> +
> +  2  Âo  o  o  o  o  o  o  o
> +
> +  1  Âo  o  o  o  o  o  o  o
> +
> +To register and name all the pins on this package we can do this in a board
> +file:
> +
> +#include <linux/pinctrl/machine.h>
> +
> +const struct pinctrl_pin_desc __initdata my_pins[] = {
> + Â Â ÂPINCTRL_PIN(0, "A1"),
> + Â Â ÂPINCTRL_PIN(1, "A2"),
> + Â Â ÂPINCTRL_PIN(2, "A3"),
> + Â Â Â...
> + Â Â ÂPINCTRL_PIN(61, "H6"),
> + Â Â ÂPINCTRL_PIN(62, "H7"),
> + Â Â ÂPINCTRL_PIN(63, "H8"),
> +};
> +
> +int __init register_pins(void)
> +{
> + Â Â Â pinctrl_register_pins_dense(&my_pins, ARRAY_SIZE(my_pins), 64);
> +}
> +
> +Pins usually have fancier names than this. You can find these in the dataheet
> +for your chip. Notice that the core machine.h file provides a fancy macro
> +called PINCTRL_PIN() to create the struct entries.
> +
> +
> +PINMUX interfaces
> +=================
> +
> +These calls use the pinmux_* naming prefix. ÂNo other calls should use that
> +prefix.
> +
> +
> +What is pinmuxing?
> +==================
> +
> +Pinmux, also known as padmux, ballmux, alternate functions or mission modes
> +is a way for chip vendors producing some kind of electrical packages to use
> +a certain physical bin (ball, pad, finger, etc) for multiple mutually exclusive
> +functions, depending on the application. By "application" in this context
> +we usually mean a way of soldering or wiring the package into an electronic
> +system, even though the framework makes it possible to handle also change the
> +function at runtime.
> +
> +Here is an example of a PGA (Pin Grid Array) chip seen from underneath:
> +
> + Â Â Â ÂA Â B Â C Â D Â E Â F Â G Â H
> + Â Â Â+---+
> +  8 Â| o | o  o  o  o  o  o  o
> + Â Â Â| Â |
> +  7 Â| o | o  o  o  o  o  o  o
> + Â Â Â| Â |
> +  6 Â| o | o  o  o  o  o  o  o
> + Â Â Â+---+---+
> +  5 Â| o | o | o  o  o  o  o  o
> + Â Â Â+---+---+ Â Â Â Â Â Â Â +---+
> +  4  Âo  o  o  o  o  o | o | o
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â| Â |
> +  3  Âo  o  o  o  o  o | o | o
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â| Â |
> +  2  Âo  o  o  o  o  o | o | o
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â| Â |
> +  1  Âo  o  o  o  o  o | o | o
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â+---+
> +
> +This is not tetris. The game to think of is chess. Not all PGA/BGA packages
> +are chessboard-like, big ones have "holes" in some arrangement according to
> +different design patterns, but we're using this as a simple example. Of the
> +pins you see some will be taken by things like a few VCC and GND to feed power
> +to the chip, and quite a few will be taken by large ports like an external
> +memory interface. The remaining pins will often be subject to pin multiplexing.
> +
> +The example 8x8 PGA package above will have pin numbers 0 thru 63 assigned to
> +its physical pins. It will name the pins { A1, A2, A3 ... H6, H7, H8 } using
> +pinctrl_register_pins_[sparse|dense]() and a suitable data set as shown
> +earlier.
> +
> +In this 8x8 BGA package the pins { A8, A7, A6, A5 } can be used as an SPI port
> +(these are four pins: CLK, RXD, TXD, FRM). In that case, pin B5 can be used as
> +some general-purpose GPIO pin. However, in another setting, pins { A5, B5 } can
> +be used as an I2C port (these are just two pins: SCL, SDA). Needless to say,
> +we cannot use the SPI port and I2C port at the same time. However in the inside
> +of the package the silicon performing the SPI logic can alternatively be routed
> +out on pins { G4, G3, G2, G1 }.
> +
> +This way the silicon blocks present inside the chip can be multiplexed "muxed"
> +out on different pin ranges. Often contemporary SoC (systems on chip) will
> +contain several I2C, SPI, SDIO/MMC, etc silicon blocks that can be routed to
> +different pins by pinmux settings.
> +
> +Since general-purpose I/O pins (GPIO) are typically always in shortage, it is
> +common to be able to use almost any pin as a GPIO pin if it is not currently
> +in use by some other I/O port.
> +
> +
> +Pinmux conventions
> +==================
> +
> +The purpose of the pinmux subsystem is to abstract and provide pinmux settings
> +to the devices you choose to instantiate in your machine configuration. It is
> +inspired by the clk, GPIO and regulator subsystems, so devices will request
> +their mux setting, but it's also possible to request a single pin for e.g.
> +GPIO.
> +
> +Definitions:
> +
> +- FUNCTIONS can be switched in and out by a driver residing with the pinmux
> + Âsubsystem in the drivers/pinmux/* directory of the kernel. The pinmux driver
> + Âknows the possible functions. In the example above you can identify three
> + Âpinmux functions, two for spi and one for i2c.
> +
> +- FUNCTIONS are assumed to be enumerable from zero in a one-dimensional array.
> + ÂIn this case the array could be something like: { spi0-0, spi0-1, i2c0-0 }
> + Âfor the three available settings. The knowledge of this one-dimensional array
> + Âand it's machine-specific particulars is kept inside the pinmux driver,
> + Âfrom the outside only these enumerators are known, and the driver core
> + Âcan request the name or the list of pins belonging to a certain enumerator.
> +
> +- FUNCTIONS are MAPPED to a certain device by the board file, device tree or
> + Âsimilar machine setup configuration mechanism, similar to how regulators are
> + Âconnected to devices, usually by name. In the example case we can define
> + Âthat this particular machine shall use device spi0 with pinmux setting
> + Âspi0-1 and i2c0 on i2c0-1, something like the two-tuple:
> + Â{ {spi0, spi0-1}, {i2c0, i2c0-1} }
> +
> +- FUNCTIONS are provided on a first-come first-serve basis, so if some other
> + Âdevice mux setting or GPIO pin request has already taken your physical pin,
> + Âyou will be denied the use of it. To get (activate) a new setting, the old
> + Âone has to be put (deactivated) first.
> +
> +Sometimes the documentation and hardware registers will be oriented around
> +pads (or "fingers") rather than pins - these are the soldering surfaces on the
> +silicon inside the package, and may or may not match the actual number of
> +pins/balls underneath the capsule. Pick some enumeration that makes sense to
> +you. Define enumerators only for the pins you can control if that makes sense.
> +
> +
> +Pinmux drivers
> +==============
> +
> +The driver will for all calls be provided an offset pin number into its own
> +pin range. If you have 2 chips with 8x8 pins, the first chips pins will have
> +numbers 0 thru 63 and the second one pins 64 thru 127, but the driver for the
> +second chip will be passed numbers in the range 0 thru 63 anyway, base offset
> +subtracted.
> +
> +Pinmux drivers are required to supply a few callback functions, some are
> +optional. Usually the enable() and disable() functions are implemented,
> +writing values into some certain registers to activate a certain mux setting
> +for a certain pin.
> +
> +A simple driver for the above example will work by setting bits 0, 1 or 2
> +into some register mamed MUX, so it enumerates its available settings and
> +their pin assignments, and expose them like this:
> +
> +#include <linux/pinctrl/pinmux.h>
> +
> +struct foo_pmx_func {
> + Â Â Â char *name;
> + Â Â Â const unsigned int *pins;
> + Â Â Â const unsigned num_pins;
> +};
> +
> +static unsigned int spi0_0_pins[] = { 0, 8, 16, 24 };
> +static unsigned int i2c0_pins[] = { 24, 25 };
> +static unsigned int spi0_1_pins[] = { 38, 46, 54, 62 };
> +
> +static struct foo_pmx_func myfuncs[] = {
> + Â Â Â {
> + Â Â Â Â Â Â Â .name = "spi0-0",
> + Â Â Â Â Â Â Â .pins = spi0_0_pins,
> + Â Â Â Â Â Â Â .num_pins = ARRAY_SIZE(spi0_1_pins),
> + Â Â Â },
> + Â Â Â {
> + Â Â Â Â Â Â Â .name = "i2c0",
> + Â Â Â Â Â Â Â .pins = i2c0_pins,
> + Â Â Â Â Â Â Â .num_pins = ARRAY_SIZE(i2c0_pins),
> + Â Â Â },
> + Â Â Â {
> + Â Â Â Â Â Â Â .name = "spi0-1",
> + Â Â Â Â Â Â Â .pins = spi0_1_pins,
> + Â Â Â Â Â Â Â .num_pins = ARRAY_SIZE(spi0_1_pins),
> + Â Â Â },
> +};
> +
> +int foo_list(struct pinmux_dev *pmxdev, unsigned selector)
> +{
> + Â Â Â if (selector >= ARRAY_SIZE(myfuncs))
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â return 0;
> +}
> +
> +const char *foo_get_fname(struct pinmux_dev *pmxdev, unsigned selector)
> +{
> + Â Â Â if (selector >= ARRAY_SIZE(myfuncs))
> + Â Â Â Â Â Â Â return NULL;
> + Â Â Â return myfuncs[selector].name;
> +}
> +
> +static int foo_get_pins(struct pinmux_dev *pmxdev, unsigned selector,
> + Â Â Â Â Â Â Â Â Â Â Â unsigned ** const pins, unsigned * const num_pins)
> +{
> + Â Â Â if (selector >= ARRAY_SIZE(myfuncs))
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â *pins = myfuncs[selector].pins;
> + Â Â Â *num_pins = myfuncs[selector].num_pins;
> + Â Â Â return 0;
> +}
> +
> +
> +int foo_enable(struct pinmux_dev *pmxdev, unsigned selector)
> +{
> + Â Â Â if (selector < ARRAY_SIZE(myfuncs))
> + Â Â Â Â Â Â Â write((read(MUX)|(1<<selector)), MUX)
> + Â Â Â Â Â Â Â return 0;
> + Â Â Â }
> + Â Â Â return -EINVAL;
> +}
> +
> +int foo_disable(struct pinmux_dev *pmxdev, unsigned selector)
> +{
> + Â Â Â if (selector < ARRAY_SIZE(myfuncs))
> + Â Â Â Â Â Â Â write((read(MUX) & ~(1<<selector)), MUX)
> + Â Â Â Â Â Â Â return 0;
> + Â Â Â }
> + Â Â Â return -EINVAL;
> +}
> +
> +struct pinmux_ops ops = {
> + Â Â Â .list_functions = foo_list,
> + Â Â Â .get_function_name = foo_get_fname,
> + Â Â Â .get_function_pins = foo_get_pins,
> + Â Â Â .enable = foo_enable,
> + Â Â Â .disable = foo_disable,
> +};
> +
> +Now the able reader will say: "wait - the driver needs to make sure it
> +can set this and that bit at the same time, because else it will collide
> +and wreak havoc in my electronics, and make sure noone else is using the
> +other setting that it's incompatible with".
> +
> +In the example activating muxing 0 and 1 at the same time setting bits
> +0 and 1, uses one pin in common so they would collide.
> +
> +The beauty of the pinmux subsystem is that since it keeps track of all
> +pins and who is using them, it will already have denied an impossible
> +request like that, so the driver does not need to worry about such
> +things - when it gets a selector passed in, the pinmux subsystem makes
> +sure no other device or GPIO assignment is already using the selected
> +pins.
> +
> +The above functions are mandatory to implement for a pinmux driver.
> +
> +The function list could become long, especially if you can convert every
> +individual pin into a GPIO pin independent of any other pins, then your
> +function array can become 64 entries for each GPIO setting and then the
> +device functions. For this reason there is an additional function you
> +can implement to enable only GPIO on an individual pin: gpio_enable().
> +
> +
> +Pinmux board/machine configuration
> +==================================
> +
> +Boards and machines define how a certain complete running system is put
> +together, including how GPIOs and devices are muxed, how regulators are
> +constrained and how the clock tree looks. Of course pinmux settings are also
> +part of this.
> +
> +A pinmux config for a machine looks pretty much like a simple regulator
> +configuration, so for the example array above we want to enable i2c and
> +spi on the second function mapping:
> +
> +#include <linux/pinctrl/machine.h>
> +
> +static struct pinmux_map pmx_mapping[] = {
> + Â Â Â {
> + Â Â Â Â Â Â Â .function = "spi0-1",
> + Â Â Â Â Â Â Â .dev_name = "foo-spi.0",
> + Â Â Â },
> + Â Â Â {
> + Â Â Â Â Â Â Â .function = "i2c0",
> + Â Â Â Â Â Â Â .dev_name = "foo-i2c.0",
> + Â Â Â },
> +};
> +
> +Since the above construct is pretty common there is a helper macro to make
> +it even more compact:
> +
> +static struct pinmux_map pmx_mapping[] = {
> + Â Â Â PINMUX_MAP("spi0-1", "foo-spi.0"),
> + Â Â Â PINMUX_MAP("i2c0", "foo-i2c.0"),
> +};
> +
> +The dev_name here matches to the unique device name that can be used to look
> +up the device struct (just like with clockdev or regulators). The function name
> +must match a function provided by the pinmux driver handling this pin range.
> +You register this pinmux mapping to the pinmux subsystem by simply:
> +
> + Â Â Â ret = pinmux_register_mappings(&pmx_mapping, ARRAY_SIZE(pmx_mapping));
> +
> +
> +Pinmux requests from drivers
> +============================
> +
> +A driver may request a certain mux to be activated, usually just the default
> +mux like this:
> +
> +#include <linux/pinctrl/pinmux.h>
> +
> +foo_probe()
> +{
> + Â Â Â /* Allocate a state holder named "state" etc */
> + Â Â Â struct pinmux pmx;
> +
> + Â Â Â pmx = pinmux_get(&device, NULL);
> + Â Â Â if IS_ERR(pmx)
> + Â Â Â Â Â Â Â return PTR_ERR(pmx);
> + Â Â Â pinmux_enable(pmx);
> +
> + Â Â Â state->pmx = pmx;
> +}
> +
> +foo_remove()
> +{
> + Â Â Â pinmux_disable(state->pmx);
> + Â Â Â pinmux_put(state->pmx);
> +}
> +
> +If you want a specific mux setting and not just the first one found for this
> +device you can specify a specific mux setting, for example in the above example
> +the second i2c0 setting: pinmux_get(&device, "spi0-2");
> +
> +This get/enable/disable/put sequence can just as well be handled by bus drivers
> +if you don't want each and every driver to handle it and you know the
> +arrangement on your bus.
> +
> +The pins are allocated for your device when you issue the pinmux_get() call,
> +after this you should be able to see this in the debugfs listing of all pins.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 29801f7..5caea5a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4933,6 +4933,11 @@ L: Â Â Â linux-mtd@xxxxxxxxxxxxxxxxxxx
> ÂS: Â Â Maintained
> ÂF: Â Â drivers/mtd/devices/phram.c
>
> +PINMUX SUBSYSTEM
> +M: Â Â Linus Walleij <linus.walleij@xxxxxxxxxx>
> +S: Â Â Maintained
> +F: Â Â drivers/pinmux/
> +
> ÂPKTCDVD DRIVER
> ÂM: Â Â Peter Osterlund <petero2@xxxxxxxxx>
> ÂS: Â Â Maintained
> diff --git a/drivers/Kconfig b/drivers/Kconfig
> index 3bb154d..6998d78 100644
> --- a/drivers/Kconfig
> +++ b/drivers/Kconfig
> @@ -56,6 +56,10 @@ source "drivers/pps/Kconfig"
>
> Âsource "drivers/ptp/Kconfig"
>
> +# pinctrl before gpio - gpio drivers may need it
> +
> +source "drivers/pinctrl/Kconfig"
> +
> Âsource "drivers/gpio/Kconfig"
>
> Âsource "drivers/w1/Kconfig"
> diff --git a/drivers/Makefile b/drivers/Makefile
> index 09f3232..a590a01 100644
> --- a/drivers/Makefile
> +++ b/drivers/Makefile
> @@ -5,6 +5,8 @@
> Â# Rewritten to use lists instead of if-statements.
> Â#
>
> +# GPIO must come after pinctrl as gpios may need to mux pins etc
> +obj-y             Â+= pinctrl/
> Âobj-y             Â+= gpio/
> Âobj-$(CONFIG_PCI) Â Â Â Â Â Â Â+= pci/
> Âobj-$(CONFIG_PARISC) Â Â Â Â Â += parisc/
> diff --git a/drivers/pinctrl/Kconfig b/drivers/pinctrl/Kconfig
> new file mode 100644
> index 0000000..8050fdf
> --- /dev/null
> +++ b/drivers/pinctrl/Kconfig
> @@ -0,0 +1,29 @@
> +#
> +# PINCTRL infrastructure and drivers
> +#
> +
> +menuconfig PINCTRL
> + Â Â Â bool "PINCTRL Support"
> + Â Â Â depends on SYSFS && EXPERIMENTAL
> + Â Â Â help
> + Â Â Â Â This enables the PINCTRL subsystem for controlling pins
> + Â Â Â Â on chip packages, for example multiplexing pins on primarily
> + Â Â Â Â PGA and BGA packages for systems on chip.
> +
> + Â Â Â Â If unsure, say N.
> +
> +if PINCTRL
> +
> +config DEBUG_PINCTRL
> + Â Â Â bool "Debug PINCTRL calls"
> + Â Â Â depends on DEBUG_KERNEL
> + Â Â Â help
> + Â Â Â Â Say Y here to add some extra checks and diagnostics to PINCTRL calls.
> +
> +config PINMUX_U300
> + Â Â Â bool "U300 pinmux driver"
> + Â Â Â depends on ARCH_U300
> + Â Â Â help
> + Â Â Â Â Say Y here to enable the U300 pinmux driver
> +
> +endif
> diff --git a/drivers/pinctrl/Makefile b/drivers/pinctrl/Makefile
> new file mode 100644
> index 0000000..44d8933
> --- /dev/null
> +++ b/drivers/pinctrl/Makefile
> @@ -0,0 +1,6 @@
> +# generic pinmux support
> +
> +ccflags-$(CONFIG_DEBUG_PINMUX) += -DDEBUG
> +
> +obj-$(CONFIG_PINCTRL) Â Â Â Â Â+= core.o
> +obj-$(CONFIG_PINMUX_U300) Â Â Â+= pinmux-u300.o
> diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
> new file mode 100644
> index 0000000..8fd1437
> --- /dev/null
> +++ b/drivers/pinctrl/core.c
> @@ -0,0 +1,1028 @@
> +/*
> + * Core driver for the pinmux subsystem
> + *
> + * Copyright (C) 2011 ST-Ericsson SA
> + * Written on behalf of Linaro for ST-Ericsson
> + * Based on bits of regulator core, gpio core and clk core
> + *
> + * Author: Linus Walleij <linus.walleij@xxxxxxxxxx>
> + *
> + * License terms: GNU General Public License (GPL) version 2
> + */
> +#define pr_fmt(fmt) "pinctrl core: " fmt
> +
> +#include <linux/kernel.h>
> +#include <linux/init.h>
> +#include <linux/device.h>
> +#include <linux/slab.h>
> +#include <linux/radix-tree.h>
> +#include <linux/err.h>
> +#include <linux/list.h>
> +#include <linux/mutex.h>
> +#include <linux/spinlock.h>
> +#include <linux/sysfs.h>
> +#include <linux/debugfs.h>
> +#include <linux/seq_file.h>
> +#include <linux/pinctrl/machine.h>
> +#include <linux/pinctrl/pinmux.h>
> +
> +/* Global list of pinmuxes */
> +static DEFINE_MUTEX(pinmux_list_mutex);
> +static LIST_HEAD(pinmux_list);
> +
> +/* Global list of pinmux devices */
> +static DEFINE_MUTEX(pinmuxdev_list_mutex);
> +static LIST_HEAD(pinmuxdev_list);
> +
> +/**
> + * struct pin_desc - pin descriptor for each physical pin in the arch
> + * @pmxdev: corresponding pinmux device
> + * @requested: whether the pin is already requested by pinmux or not
> + * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
> + * Â Â datasheet or such
> + * @function: a named muxing function for the pin that will be passed to
> + * Â Â subdrivers and shown in debugfs etc
> + */
> +struct pin_desc {
> + Â Â Â struct pinmux_dev *pmxdev;
> +    bool  Ârequested;
> +    char  Âname[16];
> +    char  Âfunction[16];
> +};
> +/* Global lookup of per-pin descriptors, one for each physical pin */
> +static DEFINE_SPINLOCK(pin_desc_tree_lock);
> +static RADIX_TREE(pin_desc_tree, GFP_KERNEL);
> +static unsigned int num_pins = 0;
> +
> +/**
> + * struct pinmux - per-device pinmux state holder
> + * @node: global list node - only for internal use
> + * @dev: the device using this pinmux
> + * @pmxdev: the pinmux device controlling this pinmux
> + * @map: corresponding pinmux map active for this pinmux setting
> + * @usecount: the number of active users of this mux setting, used to keep
> + * Â Â track of nested use cases
> + * @pins: an array of discrete physical pins used in this mapping, taken
> + * Â Â from the global pin enumeration space (copied from pinmux map)
> + * @num_pins: the number of pins in this mapping array, i.e. the number of
> + * Â Â elements in .pins so we can iterate over that array (copied from
> + * Â Â pinmux map)
> + * @pmxdev: pinmux device handling this pinmux
> + * @pmxdev_selector: the selector for the pinmux device handling this pinmux
> + * @mutex: a lock for the pinmux state holder
> + */
> +struct pinmux {
> + Â Â Â struct list_head node;
> + Â Â Â struct device *dev;
> + Â Â Â struct pinmux_map const *map;
> + Â Â Â unsigned usecount;
> + Â Â Â struct pinmux_dev *pmxdev;
> + Â Â Â unsigned pmxdev_selector;
> + Â Â Â struct mutex mutex;
> +};
> +
> +int pin_is_valid(int pin)
> +{
> + Â Â Â return pin >= 0 && pin < num_pins;
> +}
> +EXPORT_SYMBOL_GPL(pin_is_valid);
> +
> +static ssize_t pinmux_name_show(struct device *dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct device_attribute *attr, char *buf)
> +{
> + Â Â Â struct pinmux_dev *pmxdev = dev_get_drvdata(dev);
> +
> + Â Â Â return sprintf(buf, "%s\n", pmxdev_get_name(pmxdev));
> +}
> +
> +static struct device_attribute pinmux_dev_attrs[] = {
> + Â Â Â __ATTR(name, 0444, pinmux_name_show, NULL),
> + Â Â Â __ATTR_NULL,
> +};
> +
> +static void pinmux_dev_release(struct device *dev)
> +{
> + Â Â Â struct pinmux_dev *pmxdev = dev_get_drvdata(dev);
> + Â Â Â kfree(pmxdev);
> +}
> +
> +static struct class pinmux_class = {
> + Â Â Â .name = "pinmux",
> + Â Â Â .dev_release = pinmux_dev_release,
> + Â Â Â .dev_attrs = pinmux_dev_attrs,
> +};
> +
> +/* Deletes a range of pin descriptors */
> +static void pinctrl_free_pindescs(struct pinctrl_pin_desc const *pins,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned num_pins)
> +{
> + Â Â Â int i;
> +
> + Â Â Â for (i = 0; i < num_pins; i++) {
> + Â Â Â Â Â Â Â struct pin_desc *pindesc;
> +
> + Â Â Â Â Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â pindesc = radix_tree_lookup(&pin_desc_tree, pins[i].number);
> + Â Â Â Â Â Â Â if (pindesc != NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â radix_tree_delete(&pin_desc_tree, pins[i].number);
> + Â Â Â Â Â Â Â Â Â Â Â num_pins --;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â spin_unlock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â kfree(pindesc);
> + Â Â Â }
> +}
> +
> +static int pinctrl_register_one_pin(unsigned number, const char *name)
> +{
> + Â Â Â struct pin_desc *pindesc;
> +
> + Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â pindesc = radix_tree_lookup(&pin_desc_tree, number);
> + Â Â Â spin_unlock(&pin_desc_tree_lock);
> +
> + Â Â Â if (pindesc != NULL) {
> + Â Â Â Â Â Â Â pr_err("pin %d already registered\n", number);
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â }
> +
> + Â Â Â pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
> + Â Â Â if (pindesc == NULL)
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â /* Copy optional basic pin info */
> + Â Â Â if (name) {
> + Â Â Â Â Â Â Â strncpy(pindesc->name, name, 16);
> + Â Â Â Â Â Â Â pindesc->name[15] = '\0';
> + Â Â Â }
> +
> + Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â radix_tree_insert(&pin_desc_tree, number, pindesc);
> + Â Â Â num_pins ++;
> + Â Â Â spin_unlock(&pin_desc_tree_lock);
> + Â Â Â return 0;
> +}
> +
> +/* Passing in 0 num_pins means "sparse" */
> +static int pinctrl_register_pins(struct pinctrl_pin_desc const *pins,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âunsigned num_descs, unsigned num_pins)
> +{
> + Â Â Â unsigned i;
> + Â Â Â int ret = 0;
> +
> + Â Â Â for (i = 0; i < num_descs; i++) {
> + Â Â Â Â Â Â Â ret = pinctrl_register_one_pin(pins[i].number, pins[i].name);
> + Â Â Â Â Â Â Â if (ret)
> + Â Â Â Â Â Â Â Â Â Â Â return ret;
> + Â Â Â }
> +
> + Â Â Â if (num_pins == 0)
> + Â Â Â Â Â Â Â return 0;
> +
> + Â Â Â /*
> + Â Â Â Â* If we are registerering dense pinlists, fill in all holes with
> + Â Â Â Â* anonymous pins.
> + Â Â Â Â*/
> + Â Â Â for (i = 0; i < num_pins; i++) {
> + Â Â Â Â Â Â Â char pinname[16];
> + Â Â Â Â Â Â Â struct pin_desc *pindesc;
> +
> + Â Â Â Â Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â pindesc = radix_tree_lookup(&pin_desc_tree, i);
> + Â Â Â Â Â Â Â spin_unlock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â /* Already registered this one, take next */
> + Â Â Â Â Â Â Â if (pindesc)
> + Â Â Â Â Â Â Â Â Â Â Â continue;
> +
> + Â Â Â Â Â Â Â snprintf(pinname, 15, "anonymous %u", i);
> + Â Â Â Â Â Â Â pinname[15] = '\0';
> +
> + Â Â Â Â Â Â Â ret = pinctrl_register_one_pin(i, pinname);
> + Â Â Â Â Â Â Â if (ret)
> + Â Â Â Â Â Â Â Â Â Â Â return ret;
> + Â Â Â }
> +
> + Â Â Â return 0;
> +}
> +
> +/**
> + * pinctrl_register_pins_sparse() - register a range of pins for a
> + * Â Â board/machine with potential holes in the pin map. The pins in
> + * Â Â the holes will not be usable.
> + * @pins: a range of pins to register
> + * @num_descs: the number of pins descriptors passed in through the previous
> + * Â Â pointer
> + */
> +int pinctrl_register_pins_sparse(struct pinctrl_pin_desc const *pins,
> + Â Â Â Â Â Â Â Â Â Â Â Â unsigned num_descs)
> +{
> + Â Â Â int ret;
> +
> + Â Â Â ret = pinctrl_register_pins(pins, num_descs, 0);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â pinctrl_free_pindescs(pins, num_descs);
> + Â Â Â return ret;
> +
> +}
> +EXPORT_SYMBOL_GPL(pinctrl_register_pins);
> +
> +/**
> + * pinctrl_register_pins_dense() - register a range of pins for a
> + * Â Â board/machine, if there are holes in the pin map, they will be
> + * Â Â allocated by anonymous pins.
> + * @pins: a range of pins to register
> + * @num_descs: the number of pins descriptors passed in through the previous
> + * Â Â pointer
> + * @num_pins: the total number of pins including holes in the pin map and
> + * Â Â any "air" at the end of the map, all pins from 0 to this number
> + * Â Â will be allocated, the ones that does not have descriptors passed
> + * Â Â in will be marked "anonymous"
> + */
> +int pinctrl_register_pins_dense(struct pinctrl_pin_desc const *pins,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âunsigned num_descs, unsigned num_pins)
> +{
> + Â Â Â int ret;
> + Â Â Â unsigned i;
> +
> + Â Â Â ret = pinctrl_register_pins(pins, num_descs, num_pins);
> + Â Â Â if (ret) {
> + Â Â Â Â Â Â Â for (i = 0; i < num_pins; i++) {
> + Â Â Â Â Â Â Â Â Â Â Â struct pin_desc *pindesc;
> +
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â Â Â Â Â pindesc = radix_tree_lookup(&pin_desc_tree, i);
> + Â Â Â Â Â Â Â Â Â Â Â if (pindesc != NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â radix_tree_delete(&pin_desc_tree, i);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â num_pins --;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â Â Â Â Â kfree(pindesc);
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â return ret;
> +}
> +EXPORT_SYMBOL_GPL(pinctrl_register_pins_sparse);
> +
> +/**
> + * pin_request() - request a single pin to be muxed in, typically for GPIO
> + * @pin: the pin number in the global pin space
> + * @function: a functional name to give to this pin, passed to the driver
> + * Â Â so it knows what function to mux in, e.g. the string "gpioNN"
> + * Â Â means that you want to mux in the pin for use as GPIO number NN
> + * @gpio: if this request concerns a single GPIO pin
> + */
> +static int pin_request(int pin, const char *function, bool gpio)
> +{
> + Â Â Â struct pin_desc *desc;
> + Â Â Â struct pinmux_dev *pmxdev;
> + Â Â Â const struct pinmux_ops *ops;
> + Â Â Â int status = -EINVAL;
> + Â Â Â unsigned long flags;
> +
> + Â Â Â pr_debug("request pin %d for %s\n", pin, function);
> + Â Â Â if (!pin_is_valid(pin)) {
> + Â Â Â Â Â Â Â pr_err("pin is invalid\n");
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â }
> +
> + Â Â Â if (!function) {
> + Â Â Â Â Â Â Â pr_err("no function name given\n");
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â }
> +
> + Â Â Â spin_lock_irqsave(&pin_desc_tree_lock, flags);
> + Â Â Â desc = radix_tree_lookup(&pin_desc_tree, pin);
> + Â Â Â if (desc == NULL) {
> + Â Â Â Â Â Â Â pr_err("pin is not registered so it cannot be requested\n");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> + Â Â Â pmxdev = desc->pmxdev;
> + Â Â Â if (desc->requested) {
> + Â Â Â Â Â Â Â pr_err("pin already requested\n");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> + Â Â Â if (!pmxdev) {
> + Â Â Â Â Â Â Â pr_warn("no pinmux device is handling pin %d\n", pin);
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> + Â Â Â ops = pmxdev->desc->ops;
> +
> + Â Â Â /* Let each pin increase references to this module */
> + Â Â Â if (!try_module_get(pmxdev->owner)) {
> + Â Â Â Â Â Â Â pr_err("could not increase module refcount for pin %d\n", pin);
> + Â Â Â Â Â Â Â status = -EINVAL;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â /*
> + Â Â Â Â* If there is no kind of request function for the pin we just assume
> + Â Â Â Â* we got it by default and proceed.
> + Â Â Â Â*/
> + Â Â Â if (gpio && ops->gpio_request_enable)
> + Â Â Â Â Â Â Â /* This requests and enables a single GPIO pin */
> + Â Â Â Â Â Â Â status = ops->gpio_request_enable(pmxdev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pin - pmxdev->desc->base);
> + Â Â Â else if (ops->request)
> + Â Â Â Â Â Â Â status = ops->request(pmxdev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pin - pmxdev->desc->base);
> + Â Â Â else
> + Â Â Â Â Â Â Â status = 0;
> +
> + Â Â Â if (status) {
> + Â Â Â Â Â Â Â pr_err("->request on device %s failed "
> + Â Â Â Â Â Â Â Â Â Â Â"for pin %d (offset %d)\n",
> + Â Â Â Â Â Â Â Â Â Â Âpmxdev->desc->name, pin,
> + Â Â Â Â Â Â Â Â Â Â Âpin - pmxdev->desc->base);
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â desc->requested = true;
> + Â Â Â strncpy(desc->function, function, 16);
> + Â Â Â desc->function[15] = '\0';
> +
> +out:
> + Â Â Â spin_unlock_irqrestore(&pin_desc_tree_lock, flags);
> + Â Â Â if (status)
> + Â Â Â Â Â Â Â pr_err("pin-%d (%s) status %d\n",
> + Â Â Â Â Â Â Â Â Â Â Âpin, function ? : "?", status);
> +
> + Â Â Â return status;
> +}
> +
> +/**
> + * pin_free() - release a single muxed in pin so something else can be muxed in
> + * Â Â instead
> + * @pin: the pin to free
> + */
> +static void pin_free(int pin)
> +{
> + Â Â Â struct pin_desc *desc;
> + Â Â Â struct pinmux_dev *pmxdev;
> + Â Â Â unsigned long flags;
> +
> + Â Â Â if (!pin_is_valid(pin))
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â spin_lock_irqsave(&pin_desc_tree_lock, flags);
> + Â Â Â desc = radix_tree_lookup(&pin_desc_tree, pin);
> + Â Â Â if (desc == NULL) {
> + Â Â Â Â Â Â Â pr_err("pin is not registered so it cannot be freed\n");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> + Â Â Â pmxdev = desc->pmxdev;
> + Â Â Â if (pmxdev) {
> + Â Â Â Â Â Â Â const struct pinmux_ops *ops = pmxdev->desc->ops;
> +
> + Â Â Â Â Â Â Â if (ops->free)
> + Â Â Â Â Â Â Â Â Â Â Â ops->free(pmxdev, pin - pmxdev->desc->base);
> + Â Â Â }
> + Â Â Â desc->requested = false;
> + Â Â Â desc->function[0] = '\0';
> + Â Â Â module_put(pmxdev->owner);
> +out:
> + Â Â Â spin_unlock_irqrestore(&pin_desc_tree_lock, flags);
> +}
> +
> +/**
> + * pinmux_request_gpio() - request a single pin to be muxed in to be used
> + * Â Â as a GPIO pin
> + * @pin: the pin to mux in as GPIO
> + * @gpio: the corresponding GPIO pin number
> + */
> +int pinmux_request_gpio(int pin, unsigned gpio)
> +{
> + Â Â Â char gpiostr[16];
> +
> + Â Â Â snprintf(gpiostr, 15, "gpio%d", gpio);
> + Â Â Â return pin_request(pin, gpiostr, true);
> +}
> +EXPORT_SYMBOL_GPL(pinmux_request_gpio);
> +
> +/**
> + * pinmux_free_gpio() - free a single pin, currently muxed in to be used
> + * Â Â as a GPIO pin
> + * @pin: the pin to mux out from GPIO
> + */
> +void pinmux_free_gpio(int pin)
> +{
> + Â Â Â return pin_free(pin);
> +}
> +EXPORT_SYMBOL_GPL(pinmux_free_gpio);
> +
> +int pinmux_register_mappings(struct pinmux_map const *maps, unsigned num_maps)
> +{
> + Â Â Â int ret = 0;
> + Â Â Â int i;
> +
> + Â Â Â pr_debug("add %d functions\n", num_maps);
> + Â Â Â for (i = 0; i < num_maps; i++) {
> + Â Â Â Â Â Â Â struct pinmux *pmx;
> +
> + Â Â Â Â Â Â Â /* Sanity check the mapping */
> + Â Â Â Â Â Â Â if (!maps[i].function) {
> + Â Â Â Â Â Â Â Â Â Â Â pr_err("failed to register map %d - no function ID given\n", i);
> + Â Â Â Â Â Â Â Â Â Â Â ret = -EINVAL;
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â if (!maps[i].dev && !maps[i].dev_name) {
> + Â Â Â Â Â Â Â Â Â Â Â pr_err("failed to register map %d - no device or device name given\n", i);
> + Â Â Â Â Â Â Â Â Â Â Â ret = -EINVAL;
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â /*
> + Â Â Â Â Â Â Â Â* create the state cookie holder struct pinmux for each
> + Â Â Â Â Â Â Â Â* mapping, this is what consumers will get when requesting
> + Â Â Â Â Â Â Â Â* a pinmux handle with pinmux_get()
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
> + Â Â Â Â Â Â Â if (pmx == NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â mutex_init(&pmx->mutex);
> + Â Â Â Â Â Â Â pmx->map = &maps[i];
> +
> + Â Â Â Â Â Â Â /* Add the pinmux */
> + Â Â Â Â Â Â Â mutex_lock(&pinmux_list_mutex);
> + Â Â Â Â Â Â Â list_add(&pmx->node, &pinmux_list);
> + Â Â Â Â Â Â Â mutex_unlock(&pinmux_list_mutex);
> + Â Â Â Â Â Â Â pr_debug("add function %s\n", maps[i].function);
> + Â Â Â }
> +
> +out:
> + Â Â Â return ret;
> +}
> +
> +/**
> + * acquire_pins() - acquire all the pins for a certain funcion on a certain
> + * Â Â pinmux device
> + * @pmxdev: the device to take the function on
> + * @selector: the selector to acquire the pins for
> + */
> +int acquire_pins(struct pinmux_dev *pmxdev, unsigned selector)
> +{
> + Â Â Â const struct pinmux_ops *ops = pmxdev->desc->ops;
> + Â Â Â unsigned *pins;
> + Â Â Â unsigned num_pins;
> + Â Â Â const char *func = ops->get_function_name(pmxdev, selector);
> + Â Â Â int ret;
> + Â Â Â int i;
> +
> + Â Â Â ret = ops->get_function_pins(pmxdev, selector, &pins, &num_pins);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â return ret;
> +
> + Â Â Â /* Try to allocate all pins in this pinmux map, one by one */
> + Â Â Â for (i = 0; i < num_pins; i++) {
> + Â Â Â Â Â Â Â ret = pin_request(pins[i], func, false);
> + Â Â Â Â Â Â Â if (ret) {
> + Â Â Â Â Â Â Â Â Â Â Â pr_err("could not get pin %d for function %s "
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â"on device %s - conflicting mux mappings?\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âpins[i], func ? : "(undefined)",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âpmxdev->desc->name);
> + Â Â Â Â Â Â Â Â Â Â Â /* On error release all taken pins */
> + Â Â Â Â Â Â Â Â Â Â Â i--; /* this pin just failed */
> + Â Â Â Â Â Â Â Â Â Â Â for (; i >= 0; i--)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pin_free(pins[i]);
> + Â Â Â Â Â Â Â Â Â Â Â return -ENODEV;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â return 0;
> +}
> +
> +/**
> + * pinmux_get() - retrieves the pinmux for a certain device
> + * @dev: the device to get the pinmux for
> + * @func: an optional mux name or NULL, the name is only needed
> + * Â Â if a single device has multiple pinmux settings (i.e. if the
> + * Â Â same device can be muxed out on different sets of pins) or if
> + * Â Â you need an anonymous pinmux (not tied to any specific device)
> + */
> +struct pinmux *pinmux_get(struct device *dev, const char *func)
> +{
> + Â Â Â struct pinmux_map const *map = NULL;
> + Â Â Â struct pinmux_dev *pmxdev = NULL;
> + Â Â Â const char *devname = NULL;
> + Â Â Â struct pinmux *pmx;
> + Â Â Â bool found = false;
> + Â Â Â int ret = -ENODEV;
> +
> + Â Â Â /* We must have dev or ID or both */
> + Â Â Â if (!dev && !func)
> + Â Â Â Â Â Â Â return ERR_PTR(-EINVAL);
> +
> + Â Â Â mutex_lock(&pinmux_list_mutex);
> +
> + Â Â Â if (dev)
> + Â Â Â Â Â Â Â devname = dev_name(dev);
> +
> + Â Â Â /* Locate the pinmux map */
> + Â Â Â list_for_each_entry(pmx, &pinmux_list, node) {
> + Â Â Â Â Â Â Â map = pmx->map;
> +
> + Â Â Â Â Â Â Â /* If an function is given, it MUST match */
> + Â Â Â Â Â Â Â if ((func != NULL) && strcmp(map->function, func))
> + Â Â Â Â Â Â Â Â Â Â Â continue;
> +
> + Â Â Â Â Â Â Â /*
> + Â Â Â Â Â Â Â Â* This is for the case where no device name is given, we
> + Â Â Â Â Â Â Â Â* already know that the function name matches from above
> + Â Â Â Â Â Â Â Â* code.
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â if (!map->dev_name && (func != NULL)) {
> + Â Â Â Â Â Â Â Â Â Â Â found = true;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â /* If the mapping has a device set up it must match */
> + Â Â Â Â Â Â Â if (map->dev_name &&
> + Â Â Â Â Â Â Â Â Â (!devname || !strcmp(map->dev_name, devname))) {
> + Â Â Â Â Â Â Â Â Â Â Â /* MATCH! */
> + Â Â Â Â Â Â Â Â Â Â Â found = true;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â mutex_unlock(&pinmux_list_mutex);
> +
> + Â Â Â if (!found) {
> + Â Â Â Â Â Â Â pr_err("could not find mux map for device %s, ID %s\n",
> + Â Â Â Â Â Â Â Â Â Â Âdevname ? : "(anonymous)", func ? : "(undefined)");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â /* Make sure that noone else is using this function mapping */
> + Â Â Â mutex_lock(&pmx->mutex);
> + Â Â Â if (pmx->dev) {
> + Â Â Â Â Â Â Â if (pmx->dev != dev) {
> + Â Â Â Â Â Â Â Â Â Â Â mutex_unlock(&pmx->mutex);
> + Â Â Â Â Â Â Â Â Â Â Â pr_err("mapping already in use device %s, ID %s\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âdevname ? : "(anonymous)", func ? : "(undefined)");
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â /* We already fetched this and requested pins */
> + Â Â Â Â Â Â Â Â Â Â Â mutex_unlock(&pmx->mutex);
> + Â Â Â Â Â Â Â Â Â Â Â ret = 0;
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â mutex_unlock(&pmx->mutex);
> +
> +
> + Â Â Â /*
> + Â Â Â Â* Iterate over the drivers so see which ones that may handle this
> + Â Â Â Â* specific muxing. NOTE: there can be only one as of now.
> + Â Â Â Â*/
> + Â Â Â list_for_each_entry(pmxdev, &pinmuxdev_list, node) {
> + Â Â Â Â Â Â Â const struct pinmux_ops *ops = pmxdev->desc->ops;
> + Â Â Â Â Â Â Â unsigned selector = 0;
> +
> + Â Â Â Â Â Â Â /* See if this pmxdev has this function */
> + Â Â Â Â Â Â Â while (ops->list_functions(pmxdev, selector) >= 0) {
> + Â Â Â Â Â Â Â Â Â Â Â const char *fname = ops->get_function_name(pmxdev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âselector);
> +
> + Â Â Â Â Â Â Â Â Â Â Â if (!strcmp(map->function, fname)) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ret = acquire_pins(pmxdev, selector);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (ret)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* Found it! */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mutex_lock(&pmx->mutex);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pmx->dev = dev;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pmx->pmxdev = pmxdev;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pmx->pmxdev_selector = selector;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mutex_unlock(&pmx->mutex);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ret = 0;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â selector++;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â /* We couldn't find the driver for this pinmux */
> + Â Â Â ret = -ENODEV;
> +
> +out:
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â pmx = ERR_PTR(ret);
> +
> + Â Â Â return pmx;
> +}
> +EXPORT_SYMBOL_GPL(pinmux_get);
> +
> +/**
> + * pinmux_put() - release a previously claimed pinmux
> + * @pmx: a pinmux previously claimed by pinmux_get()
> + */
> +void pinmux_put(struct pinmux *pmx)
> +{
> + Â Â Â if (pmx == NULL)
> + Â Â Â Â Â Â Â return;
> + Â Â Â mutex_lock(&pmx->mutex);
> + Â Â Â if (pmx->usecount)
> + Â Â Â Â Â Â Â pr_warn("pinmux: releasing pinmux with active users!\n");
> + Â Â Â pmx->dev = NULL;
> + Â Â Â pmx->pmxdev = NULL;
> + Â Â Â pmx->pmxdev_selector = 0;
> + Â Â Â mutex_unlock(&pmx->mutex);
> +}
> +EXPORT_SYMBOL_GPL(pinmux_put);
> +
> +/**
> + * pinmux_enable() - enable a certain pinmux setting
> + * @pmx: the pinmux to enable, previously claimed by pinmux_get()
> + */
> +int pinmux_enable(struct pinmux *pmx)
> +{
> + Â Â Â int ret = 0;
> +
> + Â Â Â if (pmx == NULL)
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â mutex_lock(&pmx->mutex);
> + Â Â Â if (pmx->usecount++ == 0) {
> + Â Â Â Â Â Â Â struct pinmux_dev *pmxdev = pmx->pmxdev;
> + Â Â Â Â Â Â Â const struct pinmux_ops *ops = pmxdev->desc->ops;
> +
> + Â Â Â Â Â Â Â ret = ops->enable(pmxdev, pmx->pmxdev_selector);
> + Â Â Â Â Â Â Â if (ret)
> + Â Â Â Â Â Â Â Â Â Â Â pmx->usecount--;
> + Â Â Â }
> + Â Â Â mutex_unlock(&pmx->mutex);
> + Â Â Â return ret;
> +}
> +EXPORT_SYMBOL_GPL(pinmux_enable);
> +
> +/**
> + * pinmux_disable() - disable a certain pinmux setting
> + * @pmx: the pinmux to disable, previously claimed by pinmux_get()
> + */
> +void pinmux_disable(struct pinmux *pmx)
> +{
> + Â Â Â if (pmx == NULL)
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â mutex_lock(&pmx->mutex);
> + Â Â Â if (--pmx->usecount == 0) {
> + Â Â Â Â Â Â Â struct pinmux_dev *pmxdev = pmx->pmxdev;
> + Â Â Â Â Â Â Â const struct pinmux_ops *ops = pmxdev->desc->ops;
> +
> + Â Â Â Â Â Â Â ops->disable(pmxdev, pmx->pmxdev_selector);
> + Â Â Â }
> + Â Â Â mutex_unlock(&pmx->mutex);
> +}
> +EXPORT_SYMBOL_GPL(pinmux_disable);
> +
> +/**
> + * pinmux_config() - configure a certain pinmux setting
> + * @pmx: the pinmux setting to configure
> + * @param: the parameter to configure
> + * @data: extra data to be passed to the configuration, also works as a
> + * Â Â pointer to data returned from the function on success
> + */
> +int pinmux_config(struct pinmux *pmx, u16 param, unsigned long *data)
> +{
> + Â Â Â struct pinmux_dev *pmxdev;
> + Â Â Â const struct pinmux_ops *ops;
> + Â Â Â int ret = 0;
> +
> + Â Â Â if (pmx == NULL)
> + Â Â Â Â Â Â Â return -ENODEV;
> +
> + Â Â Â pmxdev = pmx->pmxdev;
> + Â Â Â ops = pmxdev->desc->ops;
> +
> + Â Â Â /* This operation is not mandatory to implement */
> + Â Â Â if (ops->config) {
> + Â Â Â Â Â Â Â mutex_lock(&pmx->mutex);
> + Â Â Â Â Â Â Â ret = ops->config(pmxdev, pmx->pmxdev_selector, param, data);
> + Â Â Â Â Â Â Â mutex_unlock(&pmx->mutex);
> + Â Â Â }
> +
> + Â Â Â return 0;
> +}
> +EXPORT_SYMBOL_GPL(pinmux_config);
> +
> +static void pinmux_unclaim_pindescs(int first, int last)
> +{
> + Â Â Â int i;
> +
> + Â Â Â for (i = first; i < last; i++) {
> + Â Â Â Â Â Â Â struct pin_desc *pindesc;
> +
> + Â Â Â Â Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â pindesc = radix_tree_lookup(&pin_desc_tree, i);
> + Â Â Â Â Â Â Â spin_unlock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â if (pindesc) {
> + Â Â Â Â Â Â Â Â Â Â Â /* Mark as unclaimed by drivers or functions */
> + Â Â Â Â Â Â Â Â Â Â Â pindesc->pmxdev = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â pindesc->function[0] = '\0';
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +}
> +
> +/* Helper to claim the individual descs for each pin on a pinmux device */
> +static int pinmux_claim_pindescs(struct pinmux_desc *pmxdesc,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âstruct pinmux_dev *pmxdev)
> +{
> + Â Â Â int i;
> +
> + Â Â Â /* Put self as handler of the indicated pin range */
> + Â Â Â for (i = pmxdesc->base; i < (pmxdesc->base + pmxdesc->npins); i++) {
> + Â Â Â Â Â Â Â struct pin_desc *pindesc;
> +
> + Â Â Â Â Â Â Â /* Check that none of the pins are already there */
> + Â Â Â Â Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â pindesc = radix_tree_lookup(&pin_desc_tree, i);
> + Â Â Â Â Â Â Â spin_unlock(&pin_desc_tree_lock);
> + Â Â Â Â Â Â Â if (pindesc == NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&pmxdev->dev, "pin %d is not registered, yet "
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "attempted to add pinmux driver for it\n", i);
> + Â Â Â Â Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â if (pindesc->pmxdev != NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&pmxdev->dev, "pin %d taken by other pinmux "
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "device\n", i);
> + Â Â Â Â Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â pindesc->pmxdev = pmxdev;
> + Â Â Â }
> + Â Â Â return 0;
> +}
> +
> +/**
> + * pinmux_register() - register a pinmux device
> + * @pmxdesc: descriptor for this pinmux
> + * @dev: parent device for this pinmux
> + * @driver_data: private pinmux data for this pinmux
> + */
> +struct pinmux_dev *pinmux_register(struct pinmux_desc *pmxdesc,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âstruct device *dev, void *driver_data)
> +{
> + Â Â Â static atomic_t pinmux_no = ATOMIC_INIT(0);
> + Â Â Â struct pinmux_dev *pmxdev;
> + Â Â Â int ret;
> +
> + Â Â Â if (pmxdesc == NULL)
> + Â Â Â Â Â Â Â return ERR_PTR(-EINVAL);
> + Â Â Â if (pmxdesc->name == NULL || pmxdesc->ops == NULL)
> + Â Â Â Â Â Â Â return ERR_PTR(-EINVAL);
> + Â Â Â /* These functions are mandatory */
> + Â Â Â if (!pmxdesc->ops->list_functions ||
> + Â Â Â Â Â !pmxdesc->ops->get_function_name ||
> + Â Â Â Â Â !pmxdesc->ops->enable ||
> + Â Â Â Â Â !pmxdesc->ops->disable)
> + Â Â Â Â Â Â Â return ERR_PTR(-EINVAL);
> +
> + Â Â Â pmxdev = kzalloc(sizeof(struct pinmux_dev), GFP_KERNEL);
> + Â Â Â if (pmxdev == NULL)
> + Â Â Â Â Â Â Â return ERR_PTR(-ENOMEM);
> +
> + Â Â Â mutex_lock(&pinmuxdev_list_mutex);
> + Â Â Â pmxdev->owner = pmxdesc->owner;
> + Â Â Â pmxdev->desc = pmxdesc;
> + Â Â Â pmxdev->driver_data = driver_data;
> +
> + Â Â Â /* Register device with sysfs */
> + Â Â Â pmxdev->dev.class = &pinmux_class;
> + Â Â Â pmxdev->dev.parent = dev;
> + Â Â Â dev_set_name(&pmxdev->dev, "pinmux.%d",
> + Â Â Â Â Â Â Â Â Â Âatomic_inc_return(&pinmux_no) - 1);
> + Â Â Â ret = device_register(&pmxdev->dev);
> + Â Â Â if (ret != 0) {
> + Â Â Â Â Â Â Â put_device(&pmxdev->dev);
> + Â Â Â Â Â Â Â kfree(pmxdev);
> + Â Â Â Â Â Â Â goto out_err;
> + Â Â Â }
> + Â Â Â dev_set_drvdata(&pmxdev->dev, pmxdev);
> +
> + Â Â Â ret = pinmux_claim_pindescs(pmxdesc, pmxdev);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â goto out_err;
> +
> + Â Â Â list_add(&pmxdev->node, &pinmuxdev_list);
> + Â Â Â mutex_unlock(&pinmuxdev_list_mutex);
> + Â Â Â return pmxdev;
> +
> +out_err:
> + Â Â Â mutex_unlock(&pinmuxdev_list_mutex);
> + Â Â Â put_device(&pmxdev->dev);
> + Â Â Â kfree(pmxdev);
> + Â Â Â return ERR_PTR(ret);
> +}
> +EXPORT_SYMBOL_GPL(pinmux_register);
> +
> +/**
> + * pinmux_unregister() - unregister pinmux
> + * @pmxdev: pinmux to unregister
> + *
> + * Called by pinmux drivers to unregister a pinmux.
> + */
> +void pinmux_unregister(struct pinmux_dev *pmxdev)
> +{
> + Â Â Â if (pmxdev == NULL)
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â mutex_lock(&pinmuxdev_list_mutex);
> + Â Â Â list_del(&pmxdev->node);
> + Â Â Â device_unregister(&pmxdev->dev);
> + Â Â Â mutex_unlock(&pinmuxdev_list_mutex);
> + Â Â Â pinmux_unclaim_pindescs(pmxdev->desc->base,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pmxdev->desc->base + pmxdev->desc->npins);
> +}
> +EXPORT_SYMBOL_GPL(pinmux_unregister);
> +
> +#ifdef CONFIG_DEBUG_FS
> +
> +static int pins_show(struct seq_file *s, void *what)
> +{
> + Â Â Â unsigned pin;
> +
> + Â Â Â seq_puts(s, "Pins:\n");
> + Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â for (pin = 0; pin < num_pins; pin++) {
> + Â Â Â Â Â Â Â struct pin_desc *desc;
> +
> + Â Â Â Â Â Â Â desc = radix_tree_lookup(&pin_desc_tree, pin);
> +
> + Â Â Â Â Â Â Â seq_printf(s, "pin %d (%s)\n", pin,
> + Â Â Â Â Â Â Â Â Â Â Â Â Âdesc->name ? desc->name : "(unnamed)");
> + Â Â Â }
> + Â Â Â spin_unlock(&pin_desc_tree_lock);
> +
> + Â Â Â return 0;
> +}
> +
> +static int pinmux_devices_show(struct seq_file *s, void *what)
> +{
> + Â Â Â struct pinmux_dev *pmxdev;
> +
> + Â Â Â seq_puts(s, "Available pinmux settings per pinmux device:\n");
> + Â Â Â list_for_each_entry(pmxdev, &pinmuxdev_list, node) {
> + Â Â Â Â Â Â Â const struct pinmux_ops *ops = pmxdev->desc->ops;
> + Â Â Â Â Â Â Â unsigned selector = 0;
> +
> + Â Â Â Â Â Â Â seq_printf(s, "Device %s:\n", pmxdev->desc->name);
> + Â Â Â Â Â Â Â while (ops->list_functions(pmxdev, selector) >= 0) {
> + Â Â Â Â Â Â Â Â Â Â Â unsigned *pins;
> + Â Â Â Â Â Â Â Â Â Â Â unsigned num_pins;
> + Â Â Â Â Â Â Â Â Â Â Â const char *func = ops->get_function_name(pmxdev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â selector);
> + Â Â Â Â Â Â Â Â Â Â Â int ret;
> + Â Â Â Â Â Â Â Â Â Â Â int i;
> +
> + Â Â Â Â Â Â Â Â Â Â Â ret = ops->get_function_pins(pmxdev, selector,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â&pins, &num_pins);
> +
> + Â Â Â Â Â Â Â Â Â Â Â if (ret)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â seq_printf(s, "%s [ERROR GETTING PINS]\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âfunc);
> +
> + Â Â Â Â Â Â Â Â Â Â Â else {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â seq_printf(s, "function: %s, pins = [ ", func);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â for (i = 0; i < num_pins; i++)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â seq_printf(s, "%d ", pins[i]);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â seq_puts(s, "]\n");
> + Â Â Â Â Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â Â Â Â Â selector++;
> +
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â return 0;
> +}
> +
> +static int pinmux_maps_show(struct seq_file *s, void *what)
> +{
> + Â Â Â struct pinmux *pmx;
> + Â Â Â const struct pinmux_map *map;
> +
> + Â Â Â seq_puts(s, "Pinmux maps:\n");
> + Â Â Â list_for_each_entry(pmx, &pinmux_list, node) {
> + Â Â Â Â Â Â Â map = pmx->map;
> +
> + Â Â Â Â Â Â Â seq_printf(s, "map: %s -> %s\n", map->function,
> + Â Â Â Â Â Â Â Â Â Â Â Â Âpmx->dev ? dev_name(pmx->dev) : "(unassigned)");
> + Â Â Â }
> +
> + Â Â Â return 0;
> +}
> +
> +static int pinmux_pins_show(struct seq_file *s, void *what)
> +{
> + Â Â Â unsigned pin;
> +
> + Â Â Â seq_puts(s, "Pinmux settings per pin\n");
> + Â Â Â seq_puts(s, "Format: pin (name): pinmuxfunction pinmuxdriver\n");
> + Â Â Â spin_lock(&pin_desc_tree_lock);
> + Â Â Â for (pin = 0; pin < num_pins; pin++) {
> + Â Â Â Â Â Â Â struct pin_desc *desc;
> + Â Â Â Â Â Â Â struct pinmux_dev *pmxdev;
> +
> + Â Â Â Â Â Â Â desc = radix_tree_lookup(&pin_desc_tree, pin);
> + Â Â Â Â Â Â Â pmxdev = desc->pmxdev;
> +
> + Â Â Â Â Â Â Â seq_printf(s, "pin %d (%s): %s", pin,
> + Â Â Â Â Â Â Â Â Â Â Â Â Âdesc->name ? desc->name : "(unnamed)",
> + Â Â Â Â Â Â Â Â Â Â Â Â Âdesc->requested ? desc->function : "(unclaimed)");
> +
> + Â Â Â Â Â Â Â if (pmxdev && pmxdev->desc->ops->dbg_show)
> + Â Â Â Â Â Â Â Â Â Â Â pmxdev->desc->ops->dbg_show(pmxdev, s,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pin - pmxdev->desc->base);
> +
> + Â Â Â Â Â Â Â seq_puts(s, "\n");
> + Â Â Â }
> + Â Â Â spin_unlock(&pin_desc_tree_lock);
> +
> + Â Â Â return 0;
> +}
> +
> +static int pins_open(struct inode *inode, struct file *file)
> +{
> + Â Â Â return single_open(file, pins_show, NULL);
> +}
> +
> +static int pinmux_devices_open(struct inode *inode, struct file *file)
> +{
> + Â Â Â return single_open(file, pinmux_devices_show, NULL);
> +}
> +
> +static int pinmux_maps_open(struct inode *inode, struct file *file)
> +{
> + Â Â Â return single_open(file, pinmux_maps_show, NULL);
> +}
> +
> +static int pinmux_pins_open(struct inode *inode, struct file *file)
> +{
> + Â Â Â return single_open(file, pinmux_pins_show, NULL);
> +}
> +
> +static const struct file_operations pins_ops = {
> +    .open      = pins_open,
> +    .read      = seq_read,
> +    .llseek     = seq_lseek,
> +    .release    Â= single_release,
> +};
> +
> +static const struct file_operations pinmux_devices_ops = {
> +    .open      = pinmux_devices_open,
> +    .read      = seq_read,
> +    .llseek     = seq_lseek,
> +    .release    Â= single_release,
> +};
> +
> +static const struct file_operations pinmux_maps_ops = {
> +    .open      = pinmux_maps_open,
> +    .read      = seq_read,
> +    .llseek     = seq_lseek,
> +    .release    Â= single_release,
> +};
> +
> +static const struct file_operations pinmux_pins_ops = {
> +    .open      = pinmux_pins_open,
> +    .read      = seq_read,
> +    .llseek     = seq_lseek,
> +    .release    Â= single_release,
> +};
> +
> +static struct dentry *debugfs_root;
> +
> +static void pinctrl_init_debugfs(void)
> +{
> + Â Â Â debugfs_root = debugfs_create_dir("pinctrl", NULL);
> + Â Â Â if (IS_ERR(debugfs_root) || !debugfs_root) {
> + Â Â Â Â Â Â Â pr_warn("failed to create debugfs directory\n");
> + Â Â Â Â Â Â Â debugfs_root = NULL;
> + Â Â Â Â Â Â Â return;
> + Â Â Â }
> +
> + Â Â Â debugfs_create_file("pins", S_IFREG | S_IRUGO,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â debugfs_root, NULL, &pins_ops);
> + Â Â Â debugfs_create_file("pinmux-devices", S_IFREG | S_IRUGO,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â debugfs_root, NULL, &pinmux_devices_ops);
> + Â Â Â debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â debugfs_root, NULL, &pinmux_maps_ops);
> + Â Â Â debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â debugfs_root, NULL, &pinmux_pins_ops);
> +}
> +
> +#else /* CONFIG_DEBUG_FS */
> +
> +static void pinctrl_init_debugfs(void)
> +{
> +}
> +
> +#endif
> +
> +static int __init pinctrl_init(void)
> +{
> + Â Â Â int ret;
> +
> + Â Â Â ret = class_register(&pinmux_class);
> + Â Â Â pr_info("initialized pinctrl subsystem\n");
> +
> + Â Â Â pinctrl_init_debugfs();
> + Â Â Â return ret;
> +}
> +
> +/* init early since many drivers really need to initialized pinmux early */
> +core_initcall(pinctrl_init);
> diff --git a/include/linux/pinctrl/machine.h b/include/linux/pinctrl/machine.h
> new file mode 100644
> index 0000000..1bc29f7
> --- /dev/null
> +++ b/include/linux/pinctrl/machine.h
> @@ -0,0 +1,57 @@
> +/*
> + * Machine interface for the pinctrl subsystem.
> + *
> + * Copyright (C) 2011 ST-Ericsson SA
> + * Written on behalf of Linaro for ST-Ericsson
> + * Based on bits of regulator core, gpio core and clk core
> + *
> + * Author: Linus Walleij <linus.walleij@xxxxxxxxxx>
> + *
> + * License terms: GNU General Public License (GPL) version 2
> + */
> +#ifndef __LINUX_PINMUX_MACHINE_H
> +#define __LINUX_PINMUX_MACHINE_H
> +
> +/**
> + * struct pinmux_map - boards/machines shall provide this map for devices
> + * @function: a functional name for this mapping so it can be passed down
> + * Â Â to the driver to invoke that function and be referenced by this ID
> + * Â Â in e.g. pinmux_get()
> + * @dev: the device using this specific mapping, may be NULL if you provide
> + * Â Â .dev_name instead (this is more common)
> + * @dev_name: the name of the device using this specific mapping, the name
> + * Â Â must be the same that will return your struct device*
> + */
> +struct pinmux_map {
> + Â Â Â const char *function;
> + Â Â Â struct device *dev;
> + Â Â Â const char *dev_name;
> +};
> +
> +/* Convenience macro to set a simple map from a function to a named device */
> +#define PINMUX_MAP(a, b) { .function = a, .dev_name = b }
> +
> +/**
> + * struct pinctrl_pin_desc - boards/machines provide information on their
> + * pins, pads or other muxable units in this struct
> + * @number: unique pin number from the global pin number space
> + * @name: a name for this pin
> + */
> +struct pinctrl_pin_desc {
> + Â Â Â unsigned number;
> + Â Â Â const char *name;
> +};
> +
> +/* Convenience macro to define a single named or anonymous pin descriptor */
> +#define PINCTRL_PIN(a, b) { .number = a, .name = b }
> +#define PINCTRL_PIN_ANON(a) { .number = a }
> +
> +extern int pinctrl_register_pins_sparse(struct pinctrl_pin_desc const *pins,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned num_descs);
> +extern int pinctrl_register_pins_dense(struct pinctrl_pin_desc const *pins,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned num_descs, unsigned num_pins);
> +extern int pinctrl_register_anon_pins(unsigned first, unsigned last);
> +extern int pinmux_register_mappings(struct pinmux_map const *map,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned num_maps);
> +
> +#endif
> diff --git a/include/linux/pinctrl/pinmux.h b/include/linux/pinctrl/pinmux.h
> new file mode 100644
> index 0000000..7b40893
> --- /dev/null
> +++ b/include/linux/pinctrl/pinmux.h
> @@ -0,0 +1,180 @@
> +/*
> + * Interface the pinmux subsystem
> + *
> + * Copyright (C) 2011 ST-Ericsson SA
> + * Written on behalf of Linaro for ST-Ericsson
> + * Based on bits of regulator core, gpio core and clk core
> + *
> + * Author: Linus Walleij <linus.walleij@xxxxxxxxxx>
> + *
> + * License terms: GNU General Public License (GPL) version 2
> + */
> +#ifndef __LINUX_PINCTRL_PINMUX_H
> +#define __LINUX_PINCTRL_PINMUX_H
> +
> +#include <linux/list.h>
> +#include <linux/seq_file.h>
> +
> +struct pinmux;
> +
> +#ifdef CONFIG_PINCTRL
> +
> +struct pinmux_dev;
> +
> +/**
> + * struct pinmux_ops - pinmux operations, to be implemented by drivers
> + * @request: called by the core to see if a certain pin can be muxed in
> + * Â Â and made available in a certain mux setting The driver is allowed
> + * Â Â to answer "no" by returning a negative error code
> + * @list_functions: list the number of selectable named functions available
> + * Â Â in this pinmux driver, the core will begin on 0 and call this
> + * Â Â repeatedly as long as it returns >= 0 to enumerate mux settings
> + * @get_function_name: return the function name of the muxing selector,
> + * Â Â called by the core to figure out which mux setting it shall map a
> + * Â Â certain device to
> + * @get_function_pins: return an array of pins corresponding to a certain
> + * Â Â function selector in @pins, and the size of the array in @num_pins
> + * @enable: enable a certain muxing enumerator. The driver does not need to
> + * Â Â figure out whether enabling this function conflicts some other use
> + * Â Â of the pins, such collisions are handled by the pinmux subsystem
> + * @disable: disable a certain muxing enumerator
> + * @config: custom configuration function for a certain muxing enumerator -
> + * Â Â this works a bit like an ioctl() and can pass in and return arbitrary
> + * Â Â configuration data to the pinmux
> + * @gpio_request_enable: requests and enables GPIO on a certain pin.
> + * Â Â Implement this only if you can mux every pin individually as GPIO. If
> + * Â Â your gpio assignments are grouped, so you cannot control the GPIO
> + * Â Â muxing of every indvidual pin.
> + * @dbg_show: optional debugfs display hook that will provide per-device
> + * Â Â info for a certain pin in debugfs
> + */
> +struct pinmux_ops {
> + Â Â Â int (*request) (struct pinmux_dev *pmxdev, unsigned offset);
> + Â Â Â int (*free) (struct pinmux_dev *pmxdev, unsigned offset);
> + Â Â Â int (*list_functions) (struct pinmux_dev *pmxdev, unsigned selector);
> + Â Â Â const char *(*get_function_name) (struct pinmux_dev *pmxdev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned selector);
> + Â Â Â int (*get_function_pins) (struct pinmux_dev *pmxdev, unsigned selector,
> + Â Â Â Â Â Â Â Â Â Â Â unsigned ** const pins, unsigned * const num_pins);
> + Â Â Â int (*enable) (struct pinmux_dev *pmxdev, unsigned selector);
> + Â Â Â void (*disable) (struct pinmux_dev *pmxdev, unsigned selector);
> + Â Â Â int (*config) (struct pinmux_dev *pmxdev, unsigned selector,
> + Â Â Â Â Â Â Â Â Â Â Âu16 param, unsigned long *data);
> + Â Â Â int (*gpio_request_enable) (struct pinmux_dev *pmxdev, unsigned offset);
> + Â Â Â void (*dbg_show) (struct pinmux_dev *pmxdev, struct seq_file *s,
> + Â Â Â Â Â Â Â Â Â Â Â Â unsigned offset);
> +};
> +
> +/**
> + * struct pinmux_desc - pinmux descriptor, register this to pinmux subsystem
> + * @name: name for the pinmux
> + * @ops: pinmux operation table
> + * @owner: module providing the pinmux, used for refcounting
> + * @base: the number of the first pin handled by this pinmux, in the global
> + * Â Â pin space, subtracted from a given pin to get the offset into the range
> + * Â Â of a certain pinmux
> + * @npins: the number of pins handled by this pinmux - note that
> + * Â Â this is the number of possible pin settings, if your driver handles
> + * Â Â 8 pins that each can be muxed in 3 different ways, you reserve 24
> + * Â Â pins in the global pin space and set this to 24
> + */
> +struct pinmux_desc {
> + Â Â Â const char *name;
> + Â Â Â struct pinmux_ops *ops;
> + Â Â Â struct module *owner;
> + Â Â Â int base;
> + Â Â Â int npins;
> +};
> +
> +/**
> + * struct pinmux_dev - pinmux class device
> + * @desc: the descriptor supplied when initializing this pinmux
> + * @node: node to include this pinmux in the global pinmux list
> + * @dev: the device entry for this pinmux
> + * @owner: module providing the pinmux, used for refcounting
> + * @driver_data: driver data for drivers registering to the subsystem
> + *
> + * This should be dereferenced and used by the pinmux core ONLY
> + */
> +struct pinmux_dev {
> + Â Â Â struct pinmux_desc *desc;
> + Â Â Â struct list_head node;
> + Â Â Â struct device dev;
> + Â Â Â struct module *owner;
> + Â Â Â void *driver_data;
> +};
> +
> +/* These should only be used from drives */
> +static inline const char *pmxdev_get_name(struct pinmux_dev *pmxdev)
> +{
> + Â Â Â /* We're not allowed to register devices without name */
> + Â Â Â return pmxdev->desc->name;
> +}
> +
> +static inline void *pmxdev_get_drvdata(struct pinmux_dev *pmxdev)
> +{
> + Â Â Â return pmxdev->driver_data;
> +}
> +
> +/* External interface to pinmux */
> +extern int pin_is_valid(int pin);
> +extern int pinmux_request_gpio(int pin, unsigned gpio);
> +extern void pinmux_free_gpio(int pin);
> +extern struct pinmux *pinmux_get(struct device *dev, const char *func);
> +extern void pinmux_put(struct pinmux *pmx);
> +extern int pinmux_enable(struct pinmux *pmx);
> +extern void pinmux_disable(struct pinmux *pmx);
> +extern int pinmux_config(struct pinmux *pmx, u16 param, unsigned long *data);
> +extern struct pinmux_dev *pinmux_register(struct pinmux_desc *pmxdesc,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct device *dev, void *driver_data);
> +extern void pinmux_unregister(struct pinmux_dev *pmxdev);
> +
> +#else /* !CONFIG_PINMUX */
> +
> +static inline int pin_is_valid(int pin)
> +{
> + Â Â Â return pin >= 0;
> +}
> +
> +static inline int pinmux_request_gpio(int pin, unsigned gpio)
> +{
> + Â Â Â return 0;
> +}
> +
> +static inline void pinmux_free_gpio(int pin)
> +{
> +}
> +
> +static inline int pinmux_register_mappings(struct pinmux_map const *map,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âunsigned num_maps)
> +{
> + Â Â Â return 0;
> +}
> +
> +static inline struct pinmux *pinmux_get(struct device *dev, const char *func)
> +{
> + Â Â Â return NULL;
> +}
> +
> +static inline void pinmux_put(struct pinmux *pmx)
> +{
> +}
> +
> +static inline int pinmux_enable(struct pinmux *pmx)
> +{
> + Â Â Â return 0;
> +}
> +
> +static inline void pinmux_disable(struct pinmux *pmx)
> +{
> +}
> +
> +static inline int pinmux_config(struct pinmux *pmx, u16 param,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned long *data)
> +{
> + Â Â Â return 0;
> +}
> +
> +#endif /* CONFIG_PINMUX */
> +
> +#endif /* __LINUX_PINCTRL_PINMUX_H */
> --
> 1.7.3.2
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/