Merge branch 'wire-accept4' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl...
[cascardo/linux.git] / drivers / pinctrl / pinmux.c
index 92aa13e..a76a348 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/list.h>
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
+#include <linux/string.h>
 #include <linux/sysfs.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
@@ -32,8 +33,8 @@
 static DEFINE_MUTEX(pinmux_list_mutex);
 static LIST_HEAD(pinmux_list);
 
-/* Global pinmux maps, we allow one set only */
-static struct pinmux_map const *pinmux_maps;
+/* Global pinmux maps */
+static struct pinmux_map *pinmux_maps;
 static unsigned pinmux_maps_num;
 
 /**
@@ -105,24 +106,24 @@ static int pin_request(struct pinctrl_dev *pctldev,
        const struct pinmux_ops *ops = pctldev->desc->pmxops;
        int status = -EINVAL;
 
-       dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function);
+       dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
 
        desc = pin_desc_get(pctldev, pin);
        if (desc == NULL) {
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "pin is not registered so it cannot be requested\n");
                goto out;
        }
 
        if (!function) {
-               dev_err(&pctldev->dev, "no function name given\n");
+               dev_err(pctldev->dev, "no function name given\n");
                return -EINVAL;
        }
 
        spin_lock(&desc->lock);
        if (desc->mux_function) {
                spin_unlock(&desc->lock);
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "pin already requested\n");
                goto out;
        }
@@ -131,7 +132,7 @@ static int pin_request(struct pinctrl_dev *pctldev,
 
        /* Let each pin increase references to this module */
        if (!try_module_get(pctldev->owner)) {
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "could not increase module refcount for pin %d\n",
                        pin);
                status = -EINVAL;
@@ -151,7 +152,7 @@ static int pin_request(struct pinctrl_dev *pctldev,
                status = 0;
 
        if (status)
-               dev_err(&pctldev->dev, "->request on device %s failed "
+               dev_err(pctldev->dev, "->request on device %s failed "
                       "for pin %d\n",
                       pctldev->desc->name, pin);
 out_free_pin:
@@ -162,7 +163,7 @@ out_free_pin:
        }
 out:
        if (status)
-               dev_err(&pctldev->dev, "pin-%d (%s) status %d\n",
+               dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
                       pin, function ? : "?", status);
 
        return status;
@@ -188,7 +189,7 @@ static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
 
        desc = pin_desc_get(pctldev, pin);
        if (desc == NULL) {
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "pin is not registered so it cannot be freed\n");
                return NULL;
        }
@@ -214,6 +215,10 @@ static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
 /**
  * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
  * @gpio: the GPIO pin number from the GPIO subsystem number space
+ *
+ * This function should *ONLY* be used from gpiolib-based GPIO drivers,
+ * as part of their gpio_request() semantics, platforms and individual drivers
+ * shall *NOT* request GPIO pins to be muxed in.
  */
 int pinmux_request_gpio(unsigned gpio)
 {
@@ -249,6 +254,10 @@ EXPORT_SYMBOL_GPL(pinmux_request_gpio);
 /**
  * pinmux_free_gpio() - free a single pin, currently used as GPIO
  * @gpio: the GPIO pin number from the GPIO subsystem number space
+ *
+ * This function should *ONLY* be used from gpiolib-based GPIO drivers,
+ * as part of their gpio_free() semantics, platforms and individual drivers
+ * shall *NOT* request GPIO pins to be muxed out.
  */
 void pinmux_free_gpio(unsigned gpio)
 {
@@ -270,41 +279,94 @@ void pinmux_free_gpio(unsigned gpio)
 }
 EXPORT_SYMBOL_GPL(pinmux_free_gpio);
 
+static int pinmux_gpio_direction(unsigned gpio, bool input)
+{
+       struct pinctrl_dev *pctldev;
+       struct pinctrl_gpio_range *range;
+       const struct pinmux_ops *ops;
+       int ret;
+       int pin;
+
+       ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
+       if (ret)
+               return ret;
+
+       ops = pctldev->desc->pmxops;
+
+       /* Convert to the pin controllers number space */
+       pin = gpio - range->base + range->pin_base;
+
+       if (ops->gpio_set_direction)
+               ret = ops->gpio_set_direction(pctldev, range, pin, input);
+       else
+               ret = 0;
+
+       return ret;
+}
+
+/**
+ * pinmux_gpio_direction_input() - request a GPIO pin to go into input mode
+ * @gpio: the GPIO pin number from the GPIO subsystem number space
+ *
+ * This function should *ONLY* be used from gpiolib-based GPIO drivers,
+ * as part of their gpio_direction_input() semantics, platforms and individual
+ * drivers shall *NOT* touch pinmux GPIO calls.
+ */
+int pinmux_gpio_direction_input(unsigned gpio)
+{
+       return pinmux_gpio_direction(gpio, true);
+}
+EXPORT_SYMBOL_GPL(pinmux_gpio_direction_input);
+
+/**
+ * pinmux_gpio_direction_output() - request a GPIO pin to go into output mode
+ * @gpio: the GPIO pin number from the GPIO subsystem number space
+ *
+ * This function should *ONLY* be used from gpiolib-based GPIO drivers,
+ * as part of their gpio_direction_output() semantics, platforms and individual
+ * drivers shall *NOT* touch pinmux GPIO calls.
+ */
+int pinmux_gpio_direction_output(unsigned gpio)
+{
+       return pinmux_gpio_direction(gpio, false);
+}
+EXPORT_SYMBOL_GPL(pinmux_gpio_direction_output);
+
 /**
  * pinmux_register_mappings() - register a set of pinmux mappings
- * @maps: the pinmux mappings table to register
+ * @maps: the pinmux mappings table to register, this should be marked with
+ *     __initdata so it can be discarded after boot, this function will
+ *     perform a shallow copy for the mapping entries.
  * @num_maps: the number of maps in the mapping table
  *
  * Only call this once during initialization of your machine, the function is
  * tagged as __init and won't be callable after init has completed. The map
  * passed into this function will be owned by the pinmux core and cannot be
- * free:d.
+ * freed.
  */
 int __init pinmux_register_mappings(struct pinmux_map const *maps,
                                    unsigned num_maps)
 {
+       void *tmp_maps;
        int i;
 
-       if (pinmux_maps != NULL) {
-               pr_err("pinmux mappings already registered, you can only "
-                      "register one set of maps\n");
-               return -EINVAL;
-       }
-
        pr_debug("add %d pinmux maps\n", num_maps);
+
+       /* First sanity check the new mapping */
        for (i = 0; i < num_maps; i++) {
-               /* Sanity check the mapping */
                if (!maps[i].name) {
                        pr_err("failed to register map %d: "
                               "no map name given\n", i);
                        return -EINVAL;
                }
+
                if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
                        pr_err("failed to register map %s (%d): "
                               "no pin control device given\n",
                               maps[i].name, i);
                        return -EINVAL;
                }
+
                if (!maps[i].function) {
                        pr_err("failed to register map %s (%d): "
                               "no function ID given\n", maps[i].name, i);
@@ -321,9 +383,30 @@ int __init pinmux_register_mappings(struct pinmux_map const *maps,
                                 maps[i].function);
        }
 
-       pinmux_maps = maps;
-       pinmux_maps_num = num_maps;
+       /*
+        * Make a copy of the map array - string pointers will end up in the
+        * kernel const section anyway so these do not need to be deep copied.
+        */
+       if (!pinmux_maps_num) {
+               /* On first call, just copy them */
+               tmp_maps = kmemdup(maps,
+                                  sizeof(struct pinmux_map) * num_maps,
+                                  GFP_KERNEL);
+               if (!tmp_maps)
+                       return -ENOMEM;
+       } else {
+               /* Subsequent calls, reallocate array to new size */
+               size_t oldsize = sizeof(struct pinmux_map) * pinmux_maps_num;
+               size_t newsize = sizeof(struct pinmux_map) * num_maps;
+
+               tmp_maps = krealloc(pinmux_maps, oldsize + newsize, GFP_KERNEL);
+               if (!tmp_maps)
+                       return -ENOMEM;
+               memcpy((tmp_maps + oldsize), maps, newsize);
+       }
 
+       pinmux_maps = tmp_maps;
+       pinmux_maps_num += num_maps;
        return 0;
 }
 
@@ -351,14 +434,14 @@ static int acquire_pins(struct pinctrl_dev *pctldev,
        if (ret)
                return ret;
 
-       dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n",
+       dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
                num_pins, group_selector);
 
        /* Try to allocate all pins in this group, one by one */
        for (i = 0; i < num_pins; i++) {
                ret = pin_request(pctldev, pins[i], func, NULL);
                if (ret) {
-                       dev_err(&pctldev->dev,
+                       dev_err(pctldev->dev,
                                "could not get pin %d for function %s "
                                "on device %s - conflicting mux mappings?\n",
                                pins[i], func ? : "(undefined)",
@@ -390,7 +473,7 @@ static void release_pins(struct pinctrl_dev *pctldev,
        ret = pctlops->get_group_pins(pctldev, group_selector,
                                      &pins, &num_pins);
        if (ret) {
-               dev_err(&pctldev->dev, "could not get pins to release for "
+               dev_err(pctldev->dev, "could not get pins to release for "
                        "group selector %d\n",
                        group_selector);
                return;
@@ -442,7 +525,7 @@ static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
                        return -EINVAL;
                ret = pinctrl_get_group_selector(pctldev, groups[0]);
                if (ret < 0) {
-                       dev_err(&pctldev->dev,
+                       dev_err(pctldev->dev,
                                "function %s wants group %s but the pin "
                                "controller does not seem to have that group\n",
                                pmxops->get_function_name(pctldev, func_selector),
@@ -451,7 +534,7 @@ static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
                }
 
                if (num_groups > 1)
-                       dev_dbg(&pctldev->dev,
+                       dev_dbg(pctldev->dev,
                                "function %s support more than one group, "
                                "default-selecting first group %s (%d)\n",
                                pmxops->get_function_name(pctldev, func_selector),
@@ -461,13 +544,13 @@ static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
                return ret;
        }
 
-       dev_dbg(&pctldev->dev,
+       dev_dbg(pctldev->dev,
                "check if we have pin group %s on controller %s\n",
                pin_group, pinctrl_dev_get_name(pctldev));
 
        ret = pinctrl_get_group_selector(pctldev, pin_group);
        if (ret < 0) {
-               dev_dbg(&pctldev->dev,
+               dev_dbg(pctldev->dev,
                        "%s does not support pin group %s with function %s\n",
                        pinctrl_dev_get_name(pctldev),
                        pin_group,
@@ -544,7 +627,7 @@ static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
         */
 
        if (pmx->pctldev && pmx->pctldev != pctldev) {
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "different pin control devices given for device %s, "
                        "function %s\n",
                        devname,
@@ -567,7 +650,7 @@ static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
         */
        if (pmx->func_selector != UINT_MAX &&
            pmx->func_selector != func_selector) {
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "dual function defines in the map for device %s\n",
                       devname);
                return -EINVAL;
@@ -673,7 +756,7 @@ struct pinmux *pinmux_get(struct device *dev, const char *name)
                }
 
                pr_debug("in map, found pctldev %s to handle function %s",
-                        dev_name(&pctldev->dev), map->function);
+                        dev_name(pctldev->dev), map->function);
 
 
                /*
@@ -849,7 +932,7 @@ static int pinmux_hog_map(struct pinctrl_dev *pctldev,
                 * without any problems, so then we can hog pinmuxes for
                 * all devices that just want a static pin mux at this point.
                 */
-               dev_err(&pctldev->dev, "map %s wants to hog a non-system "
+               dev_err(pctldev->dev, "map %s wants to hog a non-system "
                        "pinmux, this is not going to work\n", map->name);
                return -EINVAL;
        }
@@ -861,7 +944,7 @@ static int pinmux_hog_map(struct pinctrl_dev *pctldev,
        pmx = pinmux_get(NULL, map->name);
        if (IS_ERR(pmx)) {
                kfree(hog);
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "could not get the %s pinmux mapping for hogging\n",
                        map->name);
                return PTR_ERR(pmx);
@@ -871,7 +954,7 @@ static int pinmux_hog_map(struct pinctrl_dev *pctldev,
        if (ret) {
                pinmux_put(pmx);
                kfree(hog);
-               dev_err(&pctldev->dev,
+               dev_err(pctldev->dev,
                        "could not enable the %s pinmux mapping for hogging\n",
                        map->name);
                return ret;
@@ -880,7 +963,7 @@ static int pinmux_hog_map(struct pinctrl_dev *pctldev,
        hog->map = map;
        hog->pmx = pmx;
 
-       dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
+       dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
                 map->function);
        mutex_lock(&pctldev->pinmux_hogs_lock);
        list_add(&hog->node, &pctldev->pinmux_hogs);
@@ -899,7 +982,7 @@ static int pinmux_hog_map(struct pinctrl_dev *pctldev,
  */
 int pinmux_hog_maps(struct pinctrl_dev *pctldev)
 {
-       struct device *dev = &pctldev->dev;
+       struct device *dev = pctldev->dev;
        const char *devname = dev_name(dev);
        int ret;
        int i;
@@ -980,18 +1063,19 @@ static int pinmux_functions_show(struct seq_file *s, void *what)
 static int pinmux_pins_show(struct seq_file *s, void *what)
 {
        struct pinctrl_dev *pctldev = s->private;
-       unsigned pin;
+       unsigned i, pin;
 
        seq_puts(s, "Pinmux settings per pin\n");
        seq_puts(s, "Format: pin (name): pinmuxfunction\n");
 
-       /* The highest pin number need to be included in the loop, thus <= */
-       for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
+       /* The pin number can be retrived from the pin controller descriptor */
+       for (i = 0; i < pctldev->desc->npins; i++) {
 
                struct pin_desc *desc;
 
+               pin = pctldev->desc->pins[i].number;
                desc = pin_desc_get(pctldev, pin);
-               /* Pin space may be sparse */
+               /* Skip if we cannot search the pin */
                if (desc == NULL)
                        continue;