]> nv-tegra.nvidia Code Review - linux-3.10.git/blobdiff - drivers/pinctrl/core.c
Merge linux-3.10.67 into dev-kernel-3.10
[linux-3.10.git] / drivers / pinctrl / core.c
index 8a91eb24eb52252710dd7738534a9f425cc45721..fef8102d62cb5ec05e23ea962ebb2cf95ce95216 100644 (file)
@@ -7,52 +7,70 @@
  *
  * Author: Linus Walleij <linus.walleij@linaro.org>
  *
+ * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
+ *
  * License terms: GNU General Public License (GPL) version 2
  */
 #define pr_fmt(fmt) "pinctrl core: " fmt
 
 #include <linux/kernel.h>
+#include <linux/kref.h>
 #include <linux/export.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/consumer.h>
 #include <linux/pinctrl/pinctrl.h>
 #include <linux/pinctrl/machine.h>
+
+#ifdef CONFIG_GPIOLIB
+#include <asm-generic/gpio.h>
+#endif
+
 #include "core.h"
+#include "devicetree.h"
 #include "pinmux.h"
 #include "pinconf.h"
 
-/**
- * struct pinctrl_hog - a list item to stash control hogs
- * @node: pin control hog list node
- * @map: map entry responsible for this hogging
- * @pmx: the pin control hogged by this item
- */
-struct pinctrl_hog {
-       struct list_head node;
-       struct pinctrl_map const *map;
-       struct pinctrl *p;
-};
 
-/* Global list of pin control devices */
-static DEFINE_MUTEX(pinctrldev_list_mutex);
+static bool pinctrl_dummy_state;
+
+/* Mutex taken to protect pinctrl_list */
+DEFINE_MUTEX(pinctrl_list_mutex);
+
+/* Mutex taken to protect pinctrl_maps */
+DEFINE_MUTEX(pinctrl_maps_mutex);
+
+/* Mutex taken to protect pinctrldev_list */
+DEFINE_MUTEX(pinctrldev_list_mutex);
+
+/* Global list of pin control devices (struct pinctrl_dev) */
 static LIST_HEAD(pinctrldev_list);
 
-/* List of pin controller handles */
-static DEFINE_MUTEX(pinctrl_list_mutex);
+/* List of pin controller handles (struct pinctrl) */
 static LIST_HEAD(pinctrl_list);
 
-/* Global pinctrl maps */
-static struct pinctrl_map *pinctrl_maps;
-static unsigned pinctrl_maps_num;
+/* List of pinctrl maps (struct pinctrl_maps) */
+LIST_HEAD(pinctrl_maps);
+
+
+/**
+ * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
+ *
+ * Usually this function is called by platforms without pinctrl driver support
+ * but run with some shared drivers using pinctrl APIs.
+ * After calling this function, the pinctrl core will return successfully
+ * with creating a dummy state for the driver to keep going smoothly.
+ */
+void pinctrl_provide_dummies(void)
+{
+       pinctrl_dummy_state = true;
+}
 
 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
 {
@@ -61,6 +79,12 @@ const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
 }
 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
 
+const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
+{
+       return dev_name(pctldev->dev);
+}
+EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
+
 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
 {
        return pctldev->driver_data;
@@ -82,7 +106,6 @@ struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
        if (!devname)
                return NULL;
 
-       mutex_lock(&pinctrldev_list_mutex);
        list_for_each_entry(pctldev, &pinctrldev_list, node) {
                if (!strcmp(dev_name(pctldev->dev), devname)) {
                        /* Matched on device name */
@@ -90,22 +113,28 @@ struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
                        break;
                }
        }
-       mutex_unlock(&pinctrldev_list_mutex);
 
        return found ? pctldev : NULL;
 }
+EXPORT_SYMBOL_GPL(get_pinctrl_dev_from_devname);
 
-struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
+struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
 {
-       struct pin_desc *pindesc;
-       unsigned long flags;
+       struct pinctrl_dev *pctldev;
+
+       mutex_lock(&pinctrldev_list_mutex);
+
+       list_for_each_entry(pctldev, &pinctrldev_list, node)
+               if (pctldev->dev->of_node == np) {
+                       mutex_unlock(&pinctrldev_list_mutex);
+                       return pctldev;
+               }
 
-       spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
-       pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
-       spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
+       mutex_unlock(&pinctrldev_list_mutex);
 
-       return pindesc;
+       return NULL;
 }
+EXPORT_SYMBOL_GPL(get_pinctrl_dev_from_of_node);
 
 /**
  * pin_get_from_name() - look up a pin number from a name
@@ -132,6 +161,25 @@ int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
        return -EINVAL;
 }
 
+/**
+ * pin_get_name_from_id() - look up a pin name from a pin id
+ * @pctldev: the pin control device to lookup the pin on
+ * @name: the name of the pin to look up
+ */
+const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
+{
+       const struct pin_desc *desc;
+
+       desc = pin_desc_get(pctldev, pin);
+       if (desc == NULL) {
+               dev_err(pctldev->dev, "failed to get pin(%d) name\n",
+                       pin);
+               return NULL;
+       }
+
+       return desc->name;
+}
+
 /**
  * pin_is_valid() - check if pin exists on controller
  * @pctldev: the pin control device to check the pin on
@@ -147,11 +195,11 @@ bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
        if (pin < 0)
                return false;
 
+       mutex_lock(&pctldev->mutex);
        pindesc = pin_desc_get(pctldev, pin);
-       if (pindesc == NULL)
-               return false;
+       mutex_unlock(&pctldev->mutex);
 
-       return true;
+       return pindesc != NULL;
 }
 EXPORT_SYMBOL_GPL(pin_is_valid);
 
@@ -162,7 +210,6 @@ static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
 {
        int i;
 
-       spin_lock(&pctldev->pin_desc_tree_lock);
        for (i = 0; i < num_pins; i++) {
                struct pin_desc *pindesc;
 
@@ -176,7 +223,6 @@ static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
                }
                kfree(pindesc);
        }
-       spin_unlock(&pctldev->pin_desc_tree_lock);
 }
 
 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
@@ -192,10 +238,10 @@ static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
        }
 
        pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
-       if (pindesc == NULL)
+       if (pindesc == NULL) {
+               dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
                return -ENOMEM;
-
-       spin_lock_init(&pindesc->lock);
+       }
 
        /* Set owner */
        pindesc->pctldev = pctldev;
@@ -205,14 +251,14 @@ static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
                pindesc->name = name;
        } else {
                pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
-               if (pindesc->name == NULL)
+               if (pindesc->name == NULL) {
+                       kfree(pindesc);
                        return -ENOMEM;
+               }
                pindesc->dynamic_name = true;
        }
 
-       spin_lock(&pctldev->pin_desc_tree_lock);
        radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
-       spin_unlock(&pctldev->pin_desc_tree_lock);
        pr_debug("registered pin %d (%s) on %s\n",
                 number, pindesc->name, pctldev->desc->name);
        return 0;
@@ -248,21 +294,57 @@ pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
 {
        struct pinctrl_gpio_range *range = NULL;
 
+       mutex_lock(&pctldev->mutex);
        /* Loop over the ranges */
-       mutex_lock(&pctldev->gpio_ranges_lock);
        list_for_each_entry(range, &pctldev->gpio_ranges, node) {
                /* Check if we're in the valid range */
                if (gpio >= range->base &&
                    gpio < range->base + range->npins) {
-                       mutex_unlock(&pctldev->gpio_ranges_lock);
+                       mutex_unlock(&pctldev->mutex);
                        return range;
                }
        }
-       mutex_unlock(&pctldev->gpio_ranges_lock);
-
+       mutex_unlock(&pctldev->mutex);
        return NULL;
 }
 
+/**
+ * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
+ * the same GPIO chip are in range
+ * @gpio: gpio pin to check taken from the global GPIO pin space
+ *
+ * This function is complement of pinctrl_match_gpio_range(). If the return
+ * value of pinctrl_match_gpio_range() is NULL, this function could be used
+ * to check whether pinctrl device is ready or not. Maybe some GPIO pins
+ * of the same GPIO chip don't have back-end pinctrl interface.
+ * If the return value is true, it means that pinctrl device is ready & the
+ * certain GPIO pin doesn't have back-end pinctrl device. If the return value
+ * is false, it means that pinctrl device may not be ready.
+ */
+#ifdef CONFIG_GPIOLIB
+static bool pinctrl_ready_for_gpio_range(unsigned gpio)
+{
+       struct pinctrl_dev *pctldev;
+       struct pinctrl_gpio_range *range = NULL;
+       struct gpio_chip *chip = gpio_to_chip(gpio);
+
+       /* Loop over the pin controllers */
+       list_for_each_entry(pctldev, &pinctrldev_list, node) {
+               /* Loop over the ranges */
+               list_for_each_entry(range, &pctldev->gpio_ranges, node) {
+                       /* Check if any gpio range overlapped with gpio chip */
+                       if (range->base + range->npins - 1 < chip->base ||
+                           range->base > chip->base + chip->ngpio - 1)
+                               continue;
+                       return true;
+               }
+       }
+       return false;
+}
+#else
+static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
+#endif
+
 /**
  * pinctrl_get_device_gpio_range() - find device for GPIO range
  * @gpio: the pin to locate the pin controller for
@@ -271,16 +353,16 @@ pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  *
  * Find the pin controller handling a certain GPIO pin from the pinspace of
  * the GPIO subsystem, return the device and the matching GPIO range. Returns
- * negative if the GPIO range could not be found in any device.
+ * -EPROBE_DEFER if the GPIO range could not be found in any device since it
+ * may still have not been registered.
  */
-int pinctrl_get_device_gpio_range(unsigned gpio,
-                               struct pinctrl_dev **outdev,
-                               struct pinctrl_gpio_range **outrange)
+static int pinctrl_get_device_gpio_range(unsigned gpio,
+                                        struct pinctrl_dev **outdev,
+                                        struct pinctrl_gpio_range **outrange)
 {
        struct pinctrl_dev *pctldev = NULL;
 
        /* Loop over the pin controllers */
-       mutex_lock(&pinctrldev_list_mutex);
        list_for_each_entry(pctldev, &pinctrldev_list, node) {
                struct pinctrl_gpio_range *range;
 
@@ -288,13 +370,11 @@ int pinctrl_get_device_gpio_range(unsigned gpio,
                if (range != NULL) {
                        *outdev = pctldev;
                        *outrange = range;
-                       mutex_unlock(&pinctrldev_list_mutex);
                        return 0;
                }
        }
-       mutex_unlock(&pinctrldev_list_mutex);
 
-       return -EINVAL;
+       return -EPROBE_DEFER;
 }
 
 /**
@@ -308,9 +388,124 @@ int pinctrl_get_device_gpio_range(unsigned gpio,
 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
                            struct pinctrl_gpio_range *range)
 {
-       mutex_lock(&pctldev->gpio_ranges_lock);
-       list_add(&range->node, &pctldev->gpio_ranges);
-       mutex_unlock(&pctldev->gpio_ranges_lock);
+       mutex_lock(&pctldev->mutex);
+       list_add_tail(&range->node, &pctldev->gpio_ranges);
+       mutex_unlock(&pctldev->mutex);
+}
+EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
+
+void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
+                            struct pinctrl_gpio_range *ranges,
+                            unsigned nranges)
+{
+       int i;
+
+       for (i = 0; i < nranges; i++)
+               pinctrl_add_gpio_range(pctldev, &ranges[i]);
+}
+EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
+
+struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
+               struct pinctrl_gpio_range *range)
+{
+       struct pinctrl_dev *pctldev;
+
+       mutex_lock(&pinctrldev_list_mutex);
+
+       pctldev = get_pinctrl_dev_from_devname(devname);
+
+       /*
+        * If we can't find this device, let's assume that is because
+        * it has not probed yet, so the driver trying to register this
+        * range need to defer probing.
+        */
+       if (!pctldev) {
+               mutex_unlock(&pinctrldev_list_mutex);
+               return ERR_PTR(-EPROBE_DEFER);
+       }
+       pinctrl_add_gpio_range(pctldev, range);
+
+       mutex_unlock(&pinctrldev_list_mutex);
+
+       return pctldev;
+}
+EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
+
+/**
+ * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
+ * @pctldev: the pin controller device to look in
+ * @pin: a controller-local number to find the range for
+ */
+struct pinctrl_gpio_range *
+pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
+                                unsigned int pin)
+{
+       struct pinctrl_gpio_range *range = NULL;
+
+       mutex_lock(&pctldev->mutex);
+       /* Loop over the ranges */
+       list_for_each_entry(range, &pctldev->gpio_ranges, node) {
+               /* Check if we're in the valid range */
+               if (pin >= range->pin_base &&
+                   pin < range->pin_base + range->npins) {
+                       mutex_unlock(&pctldev->mutex);
+                       return range;
+               }
+       }
+       mutex_unlock(&pctldev->mutex);
+
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
+
+/**
+ * pinctrl_get_pin_id_from_gpio() - Get pinID from the gpio
+ * @pctldev: the pin controller device to look in
+ * @gpio: GPIO number whose pinid need to be find.
+ */
+int pinctrl_get_pin_id_from_gpio(struct pinctrl_dev *pctldev,
+               unsigned gpio)
+{
+       struct pinctrl_gpio_range *range;
+       int ret;
+
+       range = pinctrl_match_gpio_range(pctldev, gpio);
+       if (!range) {
+               pr_err("Pincontrol does not have gpio %d\n", gpio);
+               return -EINVAL;
+       }
+
+       /* Convert to the pin controllers number space */
+       ret = gpio - range->base + range->pin_base;
+       return ret;
+}
+
+/**
+ * pinctrl_get_pinctrl_dev_pin_id_from_gpio() - Get pinID and pincontrol dev
+ * from the gpio. It returns the pinctrl device handle and pin id for the gpio
+ * which it belongs to.
+ * @gpio: GPIO number whose pinid need to be find.
+ */
+int pinctrl_get_pinctrl_dev_pin_id_from_gpio(unsigned gpio,
+       struct pinctrl_dev **pctl_dev, unsigned *pin_id)
+{
+       struct pinctrl_dev *pctldev;
+       struct pinctrl_gpio_range *range;
+       int ret;
+
+       mutex_lock(&pinctrldev_list_mutex);
+
+       ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
+       if (ret < 0) {
+               pr_err("Pincontrol not found for gpio %d\n", gpio);
+               goto unlock;
+       }
+
+       *pctl_dev = pctldev;
+       *pin_id = gpio - range->base + range->pin_base;
+unlock:
+       mutex_unlock(&pinctrldev_list_mutex);
+       return ret;
 }
 
 /**
@@ -321,10 +516,11 @@ void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
                               struct pinctrl_gpio_range *range)
 {
-       mutex_lock(&pctldev->gpio_ranges_lock);
+       mutex_lock(&pctldev->mutex);
        list_del(&range->node);
-       mutex_unlock(&pctldev->gpio_ranges_lock);
+       mutex_unlock(&pctldev->mutex);
 }
+EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
 
 /**
  * pinctrl_get_group_selector() - returns the group selector for a group
@@ -335,9 +531,10 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
                               const char *pin_group)
 {
        const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
+       unsigned ngroups = pctlops->get_groups_count(pctldev);
        unsigned group_selector = 0;
 
-       while (pctlops->list_groups(pctldev, group_selector) >= 0) {
+       while (group_selector < ngroups) {
                const char *gname = pctlops->get_group_name(pctldev,
                                                            group_selector);
                if (!strcmp(gname, pin_group)) {
@@ -357,6 +554,48 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
        return -EINVAL;
 }
 
+/**
+ * pinctrl_get_group_selector_from_pin() - returns the group selector for a pin
+ * @pctldev: the pin controller handling the group
+ * @pin: the pin id on the look up
+ */
+int pinctrl_get_group_selector_from_pin(struct pinctrl_dev *pctldev,
+                       unsigned int pin)
+{
+       const struct pinctrl_ops *ops = pctldev->desc->pctlops;
+       unsigned ngroups, selector;
+       bool found = false;
+
+       mutex_lock(&pctldev->mutex);
+
+       ngroups = ops->get_groups_count(pctldev);
+       for (selector = 0; selector < ngroups; ++selector) {
+               const unsigned *pins;
+               unsigned num_pins;
+               int ret;
+
+               ret = ops->get_group_pins(pctldev, selector, &pins, &num_pins);
+               if (ret < 0) {
+                       dev_err(pctldev->dev,
+                               "Not getting group pins for selector %u\n",
+                               selector);
+                       continue;
+               }
+               if (num_pins != 1) {
+                       dev_dbg(pctldev->dev,
+                               "Multiple pins for the selector %u\n",
+                               selector);
+                       continue;
+               }
+               if (pins[0] == pin) {
+                       found = true;
+                       break;
+               }
+       }
+       mutex_unlock(&pctldev->mutex);
+       return found ? selector : -EINVAL;
+}
+
 /**
  * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  * @gpio: the GPIO pin number from the GPIO subsystem number space
@@ -372,14 +611,23 @@ int pinctrl_request_gpio(unsigned gpio)
        int ret;
        int pin;
 
+       mutex_lock(&pinctrldev_list_mutex);
+
        ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
-       if (ret)
-               return -EINVAL;
+       if (ret) {
+               if (pinctrl_ready_for_gpio_range(gpio))
+                       ret = 0;
+               mutex_unlock(&pinctrldev_list_mutex);
+               return ret;
+       }
 
        /* Convert to the pin controllers number space */
        pin = gpio - range->base + range->pin_base;
 
-       return pinmux_request_gpio(pctldev, range, pin, gpio);
+       ret = pinmux_request_gpio(pctldev, range, pin, gpio);
+
+       mutex_unlock(&pinctrldev_list_mutex);
+       return ret;
 }
 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
 
@@ -398,14 +646,22 @@ void pinctrl_free_gpio(unsigned gpio)
        int ret;
        int pin;
 
+       mutex_lock(&pinctrldev_list_mutex);
+
        ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
-       if (ret)
+       if (ret) {
+               mutex_unlock(&pinctrldev_list_mutex);
                return;
+       }
+       mutex_lock(&pctldev->mutex);
 
        /* Convert to the pin controllers number space */
        pin = gpio - range->base + range->pin_base;
 
-       return pinmux_free_gpio(pctldev, pin, range);
+       pinmux_free_gpio(pctldev, pin, range);
+
+       mutex_unlock(&pctldev->mutex);
+       mutex_unlock(&pinctrldev_list_mutex);
 }
 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
 
@@ -416,14 +672,24 @@ static int pinctrl_gpio_direction(unsigned gpio, bool input)
        int ret;
        int pin;
 
+       mutex_lock(&pinctrldev_list_mutex);
+
        ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
-       if (ret)
+       if (ret) {
+               mutex_unlock(&pinctrldev_list_mutex);
                return ret;
+       }
+
+       mutex_lock(&pctldev->mutex);
 
        /* Convert to the pin controllers number space */
        pin = gpio - range->base + range->pin_base;
+       ret = pinmux_gpio_direction(pctldev, range, pin, input);
+
+       mutex_unlock(&pctldev->mutex);
+       mutex_unlock(&pinctrldev_list_mutex);
 
-       return pinmux_gpio_direction(pctldev, range, pin, input);
+       return ret;
 }
 
 /**
@@ -454,364 +720,645 @@ int pinctrl_gpio_direction_output(unsigned gpio)
 }
 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
 
-/**
- * pinctrl_get() - retrieves the pin controller handle for a certain device
- * @dev: the device to get the pin controller handle for
- * @name: an optional specific control mapping name or NULL, the name is only
- *     needed if you want to have more than one mapping per device, or if you
- *     need an anonymous pin control (not tied to any specific device)
- */
-struct pinctrl *pinctrl_get(struct device *dev, const char *name)
+static struct pinctrl_state *find_state(struct pinctrl *p,
+                                       const char *name)
+{
+       struct pinctrl_state *state;
+
+       list_for_each_entry(state, &p->states, node)
+               if (!strcmp(state->name, name))
+                       return state;
+
+       return NULL;
+}
+
+static struct pinctrl_state *create_state(struct pinctrl *p,
+                                         const char *name)
+{
+       struct pinctrl_state *state;
+
+       state = kzalloc(sizeof(*state), GFP_KERNEL);
+       if (state == NULL) {
+               dev_err(p->dev,
+                       "failed to alloc struct pinctrl_state\n");
+               return ERR_PTR(-ENOMEM);
+       }
+
+       state->name = name;
+       INIT_LIST_HEAD(&state->settings);
+
+       list_add_tail(&state->node, &p->states);
+
+       return state;
+}
+
+static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
+{
+       struct pinctrl_state *state;
+       struct pinctrl_setting *setting;
+       int ret;
+
+       state = find_state(p, map->name);
+       if (!state)
+               state = create_state(p, map->name);
+       if (IS_ERR(state))
+               return PTR_ERR(state);
+
+       if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
+               return 0;
+
+       setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+       if (setting == NULL) {
+               dev_err(p->dev,
+                       "failed to alloc struct pinctrl_setting\n");
+               return -ENOMEM;
+       }
+
+       setting->type = map->type;
+
+       setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
+       if (setting->pctldev == NULL) {
+               kfree(setting);
+               /* Do not defer probing of hogs (circular loop) */
+               if (!strcmp(map->ctrl_dev_name, map->dev_name))
+                       return -ENODEV;
+               /*
+                * OK let us guess that the driver is not there yet, and
+                * let's defer obtaining this pinctrl handle to later...
+                */
+               dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
+                       map->ctrl_dev_name);
+               return -EPROBE_DEFER;
+       }
+
+       setting->dev_name = map->dev_name;
+
+       switch (map->type) {
+       case PIN_MAP_TYPE_MUX_GROUP:
+               ret = pinmux_map_to_setting(map, setting);
+               break;
+       case PIN_MAP_TYPE_CONFIGS_PIN:
+       case PIN_MAP_TYPE_CONFIGS_GROUP:
+               ret = pinconf_map_to_setting(map, setting);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+       if (ret < 0) {
+               kfree(setting);
+               return ret;
+       }
+
+       list_add_tail(&setting->node, &state->settings);
+
+       return 0;
+}
+
+static struct pinctrl *find_pinctrl(struct device *dev)
 {
-       struct pinctrl_map const *map = NULL;
-       struct pinctrl_dev *pctldev = NULL;
-       const char *devname = NULL;
        struct pinctrl *p;
-       bool found_map;
-       unsigned num_maps = 0;
-       int ret = -ENODEV;
-       int i;
 
-       /* We must have dev or ID or both */
-       if (!dev && !name)
-               return ERR_PTR(-EINVAL);
+       mutex_lock(&pinctrl_list_mutex);
+       list_for_each_entry(p, &pinctrl_list, node)
+               if (p->dev == dev) {
+                       mutex_unlock(&pinctrl_list_mutex);
+                       return p;
+               }
 
-       if (dev)
-               devname = dev_name(dev);
+       mutex_unlock(&pinctrl_list_mutex);
+       return NULL;
+}
+
+static void pinctrl_free(struct pinctrl *p, bool inlist);
 
-       pr_debug("get pin control handle %s for device %s\n", name,
-                devname ? devname : "(none)");
+static struct pinctrl *create_pinctrl(struct device *dev)
+{
+       struct pinctrl *p;
+       const char *devname;
+       struct pinctrl_maps *maps_node;
+       int i;
+       struct pinctrl_map const *map;
+       int ret;
 
        /*
         * create the state cookie holder struct pinctrl for each
         * mapping, this is what consumers will get when requesting
         * a pin control handle with pinctrl_get()
         */
-       p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
-       if (p == NULL)
+       p = kzalloc(sizeof(*p), GFP_KERNEL);
+       if (p == NULL) {
+               dev_err(dev, "failed to alloc struct pinctrl\n");
                return ERR_PTR(-ENOMEM);
-       mutex_init(&p->mutex);
-       pinmux_init_pinctrl_handle(p);
+       }
+       p->dev = dev;
+       INIT_LIST_HEAD(&p->states);
+       INIT_LIST_HEAD(&p->dt_maps);
 
-       /* Iterate over the pin control maps to locate the right ones */
-       for (i = 0; i < pinctrl_maps_num; i++) {
-               map = &pinctrl_maps[i];
-               found_map = false;
+       ret = pinctrl_dt_to_map(p);
+       if (ret < 0) {
+               kfree(p);
+               return ERR_PTR(ret);
+       }
 
-               /*
-                * First, try to find the pctldev given in the map
-                */
-               pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
-               if (!pctldev) {
-                       pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
-                                  map->function);
-                       pr_warning("given pinctrl device name: %s",
-                                  map->ctrl_dev_name);
+       devname = dev_name(dev);
 
-                       /* Continue to check the other mappings anyway... */
+       mutex_lock(&pinctrl_maps_mutex);
+       /* Iterate over the pin control maps to locate the right ones */
+       for_each_maps(maps_node, i, map) {
+               /* Map must be for this device */
+               if (strcmp(map->dev_name, devname))
                        continue;
-               }
-
-               pr_debug("in map, found pctldev %s to handle function %s",
-                        dev_name(pctldev->dev), map->function);
-
-
-               /*
-                * If we're looking for a specific named map, this must match,
-                * else we loop and look for the next.
-                */
-               if (name != NULL) {
-                       if (map->name == NULL)
-                               continue;
-                       if (strcmp(map->name, name))
-                               continue;
-               }
 
+               ret = add_setting(p, map);
                /*
-                * This is for the case where no device name is given, we
-                * already know that the function name matches from above
-                * code.
+                * At this point the adding of a setting may:
+                *
+                * - Defer, if the pinctrl device is not yet available
+                * - Fail, if the pinctrl device is not yet available,
+                *   AND the setting is a hog. We cannot defer that, since
+                *   the hog will kick in immediately after the device
+                *   is registered.
+                *
+                * If the error returned was not -EPROBE_DEFER then we
+                * accumulate the errors to see if we end up with
+                * an -EPROBE_DEFER later, as that is the worst case.
                 */
-               if (!map->dev_name && (name != NULL))
-                       found_map = true;
-
-               /* If the mapping has a device set up it must match */
-               if (map->dev_name &&
-                   (!devname || !strcmp(map->dev_name, devname)))
-                       /* MATCH! */
-                       found_map = true;
-
-               /* If this map is applicable, then apply it */
-               if (found_map) {
-                       ret = pinmux_apply_muxmap(pctldev, p, dev,
-                                                  devname, map);
-                       if (ret) {
-                               kfree(p);
-                               return ERR_PTR(ret);
-                       }
-                       num_maps++;
+               if (ret == -EPROBE_DEFER) {
+                       pinctrl_free(p, false);
+                       mutex_unlock(&pinctrl_maps_mutex);
+                       return ERR_PTR(ret);
                }
        }
+       mutex_unlock(&pinctrl_maps_mutex);
 
-       /* We should have atleast one map, right */
-       if (!num_maps) {
-               pr_err("could not find any mux maps for device %s, ID %s\n",
-                      devname ? devname : "(anonymous)",
-                      name ? name : "(undefined)");
-               kfree(p);
-               return ERR_PTR(-EINVAL);
+       if (ret < 0) {
+               /* If some other error than deferral occured, return here */
+               pinctrl_free(p, false);
+               return ERR_PTR(ret);
        }
 
-       pr_debug("found %u mux maps for device %s, UD %s\n",
-                num_maps,
-                devname ? devname : "(anonymous)",
-                name ? name : "(undefined)");
+       kref_init(&p->users);
 
-       /* Add the pinmux to the global list */
+       /* Add the pinctrl handle to the global list */
        mutex_lock(&pinctrl_list_mutex);
-       list_add(&p->node, &pinctrl_list);
+       list_add_tail(&p->node, &pinctrl_list);
        mutex_unlock(&pinctrl_list_mutex);
 
        return p;
 }
-EXPORT_SYMBOL_GPL(pinctrl_get);
 
 /**
- * pinctrl_put() - release a previously claimed pin control handle
- * @p: a pin control handle previously claimed by pinctrl_get()
+ * pinctrl_get() - retrieves the pinctrl handle for a device
+ * @dev: the device to obtain the handle for
  */
-void pinctrl_put(struct pinctrl *p)
+struct pinctrl *pinctrl_get(struct device *dev)
 {
-       if (p == NULL)
-               return;
+       struct pinctrl *p;
+
+       if (WARN_ON(!dev))
+               return ERR_PTR(-EINVAL);
+
+       /*
+        * See if somebody else (such as the device core) has already
+        * obtained a handle to the pinctrl for this device. In that case,
+        * return another pointer to it.
+        */
+       p = find_pinctrl(dev);
+       if (p != NULL) {
+               dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
+               kref_get(&p->users);
+               return p;
+       }
+
+       return create_pinctrl(dev);
+}
+EXPORT_SYMBOL_GPL(pinctrl_get);
 
-       mutex_lock(&p->mutex);
-       if (p->usecount)
-               pr_warn("releasing pin control handle with active users!\n");
-       /* Free the groups and all acquired pins */
-       pinmux_put(p);
-       mutex_unlock(&p->mutex);
+static void pinctrl_free_setting(bool disable_setting,
+                                struct pinctrl_setting *setting)
+{
+       switch (setting->type) {
+       case PIN_MAP_TYPE_MUX_GROUP:
+               if (disable_setting)
+                       pinmux_disable_setting(setting);
+               pinmux_free_setting(setting);
+               break;
+       case PIN_MAP_TYPE_CONFIGS_PIN:
+       case PIN_MAP_TYPE_CONFIGS_GROUP:
+               pinconf_free_setting(setting);
+               break;
+       default:
+               break;
+       }
+}
+
+static void pinctrl_free(struct pinctrl *p, bool inlist)
+{
+       struct pinctrl_state *state, *n1;
+       struct pinctrl_setting *setting, *n2;
 
-       /* Remove from list */
        mutex_lock(&pinctrl_list_mutex);
-       list_del(&p->node);
-       mutex_unlock(&pinctrl_list_mutex);
+       list_for_each_entry_safe(state, n1, &p->states, node) {
+               list_for_each_entry_safe(setting, n2, &state->settings, node) {
+                       pinctrl_free_setting(state == p->state, setting);
+                       list_del(&setting->node);
+                       kfree(setting);
+               }
+               list_del(&state->node);
+               kfree(state);
+       }
 
+       pinctrl_dt_free_maps(p);
+
+       if (inlist)
+               list_del(&p->node);
        kfree(p);
+       mutex_unlock(&pinctrl_list_mutex);
+}
+
+/**
+ * pinctrl_release() - release the pinctrl handle
+ * @kref: the kref in the pinctrl being released
+ */
+static void pinctrl_release(struct kref *kref)
+{
+       struct pinctrl *p = container_of(kref, struct pinctrl, users);
+
+       pinctrl_free(p, true);
+}
+
+/**
+ * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
+ * @p: the pinctrl handle to release
+ */
+void pinctrl_put(struct pinctrl *p)
+{
+       kref_put(&p->users, pinctrl_release);
 }
 EXPORT_SYMBOL_GPL(pinctrl_put);
 
 /**
- * pinctrl_enable() - enable a certain pin controller setting
- * @p: the pin control handle to enable, previously claimed by pinctrl_get()
+ * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
+ * @p: the pinctrl handle to retrieve the state from
+ * @name: the state name to retrieve
  */
-int pinctrl_enable(struct pinctrl *p)
+struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
+                                                const char *name)
 {
-       int ret = 0;
+       struct pinctrl_state *state;
+
+       state = find_state(p, name);
+       if (!state) {
+               if (pinctrl_dummy_state) {
+                       /* create dummy state */
+                       dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
+                               name);
+                       state = create_state(p, name);
+               } else
+                       state = ERR_PTR(-ENODEV);
+       }
+
+       return state;
+}
+EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
 
-       if (p == NULL)
+/**
+ * pinctrl_select_state() - select/activate/program a pinctrl state to HW
+ * @p: the pinctrl handle for the device that requests configuration
+ * @state: the state handle to select/activate/program
+ */
+int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
+{
+       struct pinctrl_setting *setting, *setting2;
+       struct pinctrl_state *old_state = p->state;
+       int ret;
+
+       if (p->state == state)
+               return 0;
+
+       if (p->state) {
+               /*
+                * The set of groups with a mux configuration in the old state
+                * may not be identical to the set of groups with a mux setting
+                * in the new state. While this might be unusual, it's entirely
+                * possible for the "user"-supplied mapping table to be written
+                * that way. For each group that was configured in the old state
+                * but not in the new state, this code puts that group into a
+                * safe/disabled state.
+                */
+               list_for_each_entry(setting, &p->state->settings, node) {
+                       bool found = false;
+                       if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
+                               continue;
+                       list_for_each_entry(setting2, &state->settings, node) {
+                               if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
+                                       continue;
+                               if (setting2->data.mux.group ==
+                                               setting->data.mux.group) {
+                                       found = true;
+                                       break;
+                               }
+                       }
+                       if (!found)
+                               pinmux_disable_setting(setting);
+               }
+       }
+
+       p->state = NULL;
+
+       /* Apply all the settings for the new state */
+       list_for_each_entry(setting, &state->settings, node) {
+               switch (setting->type) {
+               case PIN_MAP_TYPE_MUX_GROUP:
+                       ret = pinmux_enable_setting(setting);
+                       break;
+               case PIN_MAP_TYPE_CONFIGS_PIN:
+               case PIN_MAP_TYPE_CONFIGS_GROUP:
+                       ret = pinconf_apply_setting(setting);
+                       break;
+               default:
+                       ret = -EINVAL;
+                       break;
+               }
+
+               if (ret < 0) {
+                       goto unapply_new_state;
+               }
+       }
+
+       p->state = state;
+
+       return 0;
+
+unapply_new_state:
+       dev_err(p->dev, "Error applying setting, reverse things back\n");
+
+       list_for_each_entry(setting2, &state->settings, node) {
+               if (&setting2->node == &setting->node)
+                       break;
+               /*
+                * All we can do here is pinmux_disable_setting.
+                * That means that some pins are muxed differently now
+                * than they were before applying the setting (We can't
+                * "unmux a pin"!), but it's not a big deal since the pins
+                * are free to be muxed by another apply_setting.
+                */
+               if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
+                       pinmux_disable_setting(setting2);
+       }
+
+       /* There's no infinite recursive loop here because p->state is NULL */
+       if (old_state)
+               pinctrl_select_state(p, old_state);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(pinctrl_select_state);
+
+int pinctrl_configure_user_state(struct pinctrl_dev *pctldev,
+               const char *user_state_name)
+{
+       struct pinctrl_setting *setting, *setting2;
+       struct pinctrl_state *user_state;
+       int ret;
+
+       if (!pctldev || IS_ERR(pctldev->p)) {
+               pr_err("The pincontrol driver is not valid\n");
                return -EINVAL;
-       mutex_lock(&p->mutex);
-       if (p->usecount++ == 0) {
-               ret = pinmux_enable(p);
-               if (ret)
-                       p->usecount--;
        }
-       mutex_unlock(&p->mutex);
+
+       user_state = pinctrl_lookup_state(pctldev->p, user_state_name);
+       if (IS_ERR(user_state)) {
+               ret = PTR_ERR(user_state);
+               dev_info(pctldev->dev, "lookup the user state %s not found\n",
+                       user_state_name);
+               return ret;
+       }
+
+       list_for_each_entry(setting, &user_state->settings, node) {
+               switch (setting->type) {
+               case PIN_MAP_TYPE_MUX_GROUP:
+                       ret = pinmux_enable_setting(setting);
+                       break;
+               case PIN_MAP_TYPE_CONFIGS_PIN:
+               case PIN_MAP_TYPE_CONFIGS_GROUP:
+                       ret = pinconf_apply_setting(setting);
+                       break;
+               default:
+                       ret = -EINVAL;
+                       break;
+               }
+
+               if (ret < 0)
+                       goto unapply_new_state;
+       }
+       return 0;
+
+unapply_new_state:
+       dev_err(pctldev->dev, "Error applying setting, reverse things back\n");
+
+       list_for_each_entry(setting2, &user_state->settings, node) {
+               if (&setting2->node == &setting->node)
+                       break;
+               /*
+                * All we can do here is pinmux_disable_setting.
+                * That means that some pins are muxed differently now
+                * than they were before applying the setting (We can't
+                * "unmux a pin"!), but it's not a big deal since the pins
+                * are free to be muxed by another apply_setting.
+                */
+               if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
+                       pinmux_disable_setting(setting2);
+       }
        return ret;
 }
-EXPORT_SYMBOL_GPL(pinctrl_enable);
+EXPORT_SYMBOL_GPL(pinctrl_configure_user_state);
+
+static void devm_pinctrl_release(struct device *dev, void *res)
+{
+       pinctrl_put(*(struct pinctrl **)res);
+}
 
 /**
- * pinctrl_disable() - disable a certain pin control setting
- * @p: the pin control handle to disable, previously claimed by pinctrl_get()
+ * struct devm_pinctrl_get() - Resource managed pinctrl_get()
+ * @dev: the device to obtain the handle for
+ *
+ * If there is a need to explicitly destroy the returned struct pinctrl,
+ * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  */
-void pinctrl_disable(struct pinctrl *p)
+struct pinctrl *devm_pinctrl_get(struct device *dev)
 {
-       if (p == NULL)
-               return;
+       struct pinctrl **ptr, *p;
 
-       mutex_lock(&p->mutex);
-       if (--p->usecount == 0) {
-               pinmux_disable(p);
+       ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
+       if (!ptr)
+               return ERR_PTR(-ENOMEM);
+
+       p = pinctrl_get(dev);
+       if (!IS_ERR(p)) {
+               *ptr = p;
+               devres_add(dev, ptr);
+       } else {
+               devres_free(ptr);
        }
-       mutex_unlock(&p->mutex);
+
+       return p;
+}
+EXPORT_SYMBOL_GPL(devm_pinctrl_get);
+
+static int devm_pinctrl_match(struct device *dev, void *res, void *data)
+{
+       struct pinctrl **p = res;
+
+       return *p == data;
 }
-EXPORT_SYMBOL_GPL(pinctrl_disable);
 
 /**
- * pinctrl_register_mappings() - register a set of pin controller mappings
- * @maps: the pincontrol 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
+ * devm_pinctrl_put() - Resource managed pinctrl_put()
+ * @p: the pinctrl handle to release
  *
- * 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
- * freed.
+ * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
+ * this function will not need to be called and the resource management
+ * code will ensure that the resource is freed.
  */
-int __init pinctrl_register_mappings(struct pinctrl_map const *maps,
-                                    unsigned num_maps)
+void devm_pinctrl_put(struct pinctrl *p)
 {
-       void *tmp_maps;
-       int i;
+       WARN_ON(devres_release(p->dev, devm_pinctrl_release,
+                              devm_pinctrl_match, p));
+}
+EXPORT_SYMBOL_GPL(devm_pinctrl_put);
+
+int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
+                        bool dup, bool locked)
+{
+       int i, ret;
+       struct pinctrl_maps *maps_node;
 
        pr_debug("add %d pinmux maps\n", num_maps);
 
        /* First sanity check the new mapping */
        for (i = 0; i < num_maps; i++) {
+               if (!maps[i].dev_name) {
+                       pr_err("failed to register map %s (%d): no device given\n",
+                              maps[i].name, i);
+                       return -EINVAL;
+               }
+
                if (!maps[i].name) {
                        pr_err("failed to register map %d: no map name given\n",
-                                       i);
+                              i);
                        return -EINVAL;
                }
 
-               if (!maps[i].ctrl_dev_name) {
+               if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
+                               !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);
+               switch (maps[i].type) {
+               case PIN_MAP_TYPE_DUMMY_STATE:
+                       break;
+               case PIN_MAP_TYPE_MUX_GROUP:
+                       ret = pinmux_validate_map(&maps[i], i);
+                       if (ret < 0)
+                               return ret;
+                       break;
+               case PIN_MAP_TYPE_CONFIGS_PIN:
+               case PIN_MAP_TYPE_CONFIGS_GROUP:
+                       ret = pinconf_validate_map(&maps[i], i);
+                       if (ret < 0)
+                               return ret;
+                       break;
+               default:
+                       pr_err("failed to register map %s (%d): invalid type given\n",
+                              maps[i].name, i);
                        return -EINVAL;
                }
+       }
 
-               if (!maps[i].dev_name)
-                       pr_debug("add system map %s function %s with no device\n",
-                                maps[i].name,
-                                maps[i].function);
-               else
-                       pr_debug("register map %s, function %s\n",
-                                maps[i].name,
-                                maps[i].function);
+       maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
+       if (!maps_node) {
+               pr_err("failed to alloc struct pinctrl_maps\n");
+               return -ENOMEM;
        }
 
-       /*
-        * 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 (!pinctrl_maps_num) {
-               /* On first call, just copy them */
-               tmp_maps = kmemdup(maps,
-                                  sizeof(struct pinctrl_map) * num_maps,
-                                  GFP_KERNEL);
-               if (!tmp_maps)
+       maps_node->num_maps = num_maps;
+       if (dup) {
+               maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
+                                         GFP_KERNEL);
+               if (!maps_node->maps) {
+                       pr_err("failed to duplicate mapping table\n");
+                       kfree(maps_node);
                        return -ENOMEM;
+               }
        } else {
-               /* Subsequent calls, reallocate array to new size */
-               size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num;
-               size_t newsize = sizeof(struct pinctrl_map) * num_maps;
-
-               tmp_maps = krealloc(pinctrl_maps,
-                                   oldsize + newsize, GFP_KERNEL);
-               if (!tmp_maps)
-                       return -ENOMEM;
-               memcpy((tmp_maps + oldsize), maps, newsize);
+               maps_node->maps = maps;
        }
 
-       pinctrl_maps = tmp_maps;
-       pinctrl_maps_num += num_maps;
+       if (!locked)
+               mutex_lock(&pinctrl_maps_mutex);
+       list_add_tail(&maps_node->node, &pinctrl_maps);
+       if (!locked)
+               mutex_unlock(&pinctrl_maps_mutex);
+
        return 0;
 }
 
-/* Hog a single map entry and add to the hoglist */
-static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
-                          struct pinctrl_map const *map)
+/**
+ * pinctrl_register_mappings() - register a set of pin controller mappings
+ * @maps: the pincontrol mappings table to register. This should probably 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
+ */
+int pinctrl_register_mappings(struct pinctrl_map const *maps,
+                             unsigned num_maps)
 {
-       struct pinctrl_hog *hog;
-       struct pinctrl *p;
-       int ret;
-
-       hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
-       if (!hog)
-               return -ENOMEM;
-
-       p = pinctrl_get(pctldev->dev, map->name);
-       if (IS_ERR(p)) {
-               kfree(hog);
-               dev_err(pctldev->dev,
-                       "could not get the %s pin control mapping for hogging\n",
-                       map->name);
-               return PTR_ERR(p);
-       }
+       return pinctrl_register_map(maps, num_maps, true, false);
+}
 
-       ret = pinctrl_enable(p);
-       if (ret) {
-               pinctrl_put(p);
-               kfree(hog);
-               dev_err(pctldev->dev,
-                       "could not enable the %s pin control mapping for hogging\n",
-                       map->name);
-               return ret;
+void pinctrl_unregister_map(struct pinctrl_map const *map)
+{
+       struct pinctrl_maps *maps_node;
+
+       mutex_lock(&pinctrl_maps_mutex);
+       list_for_each_entry(maps_node, &pinctrl_maps, node) {
+               if (maps_node->maps == map) {
+                       list_del(&maps_node->node);
+                       kfree(maps_node);
+                       mutex_unlock(&pinctrl_maps_mutex);
+                       return;
+               }
        }
-
-       hog->map = map;
-       hog->p = p;
-
-       dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
-                map->function);
-       mutex_lock(&pctldev->pinctrl_hogs_lock);
-       list_add(&hog->node, &pctldev->pinctrl_hogs);
-       mutex_unlock(&pctldev->pinctrl_hogs_lock);
-
-       return 0;
+       mutex_unlock(&pinctrl_maps_mutex);
 }
 
 /**
- * pinctrl_hog_maps() - hog specific map entries on controller device
- * @pctldev: the pin control device to hog entries on
- *
- * When the pin controllers are registered, there may be some specific pinmux
- * map entries that need to be hogged, i.e. get+enabled until the system shuts
- * down.
+ * pinctrl_force_sleep() - turn a given controller device into sleep state
+ * @pctldev: pin controller device
  */
-int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
+int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
 {
-       struct device *dev = pctldev->dev;
-       const char *devname = dev_name(dev);
-       int ret;
-       int i;
-
-       INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
-       mutex_init(&pctldev->pinctrl_hogs_lock);
-
-       for (i = 0; i < pinctrl_maps_num; i++) {
-               struct pinctrl_map const *map = &pinctrl_maps[i];
-
-               if (map->ctrl_dev_name &&
-                   !strcmp(map->ctrl_dev_name, devname) &&
-                   !strcmp(map->dev_name, devname)) {
-                       /* OK time to hog! */
-                       ret = pinctrl_hog_map(pctldev, map);
-                       if (ret)
-                               return ret;
-               }
-       }
+       if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
+               return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
        return 0;
 }
+EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
 
 /**
- * pinctrl_unhog_maps() - unhog specific map entries on controller device
- * @pctldev: the pin control device to unhog entries on
+ * pinctrl_force_default() - turn a given controller device into default state
+ * @pctldev: pin controller device
  */
-void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
-{
-       struct list_head *node, *tmp;
-
-       mutex_lock(&pctldev->pinctrl_hogs_lock);
-       list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
-               struct pinctrl_hog *hog =
-                       list_entry(node, struct pinctrl_hog, node);
-               pinctrl_disable(hog->p);
-               pinctrl_put(hog->p);
-               list_del(node);
-               kfree(hog);
-       }
-       mutex_unlock(&pctldev->pinctrl_hogs_lock);
+int pinctrl_force_default(struct pinctrl_dev *pctldev)
+{
+       if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
+               return pinctrl_select_state(pctldev->p, pctldev->hog_default);
+       return 0;
 }
+EXPORT_SYMBOL_GPL(pinctrl_force_default);
 
 #ifdef CONFIG_DEBUG_FS
 
@@ -823,6 +1370,8 @@ static int pinctrl_pins_show(struct seq_file *s, void *what)
 
        seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
 
+       mutex_lock(&pctldev->mutex);
+
        /* The pin number can be retrived from the pin controller descriptor */
        for (i = 0; i < pctldev->desc->npins; i++) {
                struct pin_desc *desc;
@@ -843,6 +1392,8 @@ static int pinctrl_pins_show(struct seq_file *s, void *what)
                seq_puts(s, "\n");
        }
 
+       mutex_unlock(&pctldev->mutex);
+
        return 0;
 }
 
@@ -850,17 +1401,18 @@ static int pinctrl_groups_show(struct seq_file *s, void *what)
 {
        struct pinctrl_dev *pctldev = s->private;
        const struct pinctrl_ops *ops = pctldev->desc->pctlops;
-       unsigned selector = 0;
+       unsigned ngroups, selector = 0;
 
-       /* No grouping */
-       if (!ops)
-               return 0;
+       mutex_lock(&pctldev->mutex);
+
+       ngroups = ops->get_groups_count(pctldev);
 
        seq_puts(s, "registered pin groups:\n");
-       while (ops->list_groups(pctldev, selector) >= 0) {
+       while (selector < ngroups) {
                const unsigned *pins;
                unsigned num_pins;
                const char *gname = ops->get_group_name(pctldev, selector);
+               const char *pname;
                int ret;
                int i;
 
@@ -870,14 +1422,21 @@ static int pinctrl_groups_show(struct seq_file *s, void *what)
                        seq_printf(s, "%s [ERROR GETTING PINS]\n",
                                   gname);
                else {
-                       seq_printf(s, "group: %s, pins = [ ", gname);
-                       for (i = 0; i < num_pins; i++)
-                               seq_printf(s, "%d ", pins[i]);
-                       seq_puts(s, "]\n");
+                       seq_printf(s, "group: %s\n", gname);
+                       for (i = 0; i < num_pins; i++) {
+                               pname = pin_get_name(pctldev, pins[i]);
+                               if (WARN_ON(!pname)) {
+                                       mutex_unlock(&pctldev->mutex);
+                                       return -EINVAL;
+                               }
+                               seq_printf(s, "pin %d (%s)\n", pins[i], pname);
+                       }
+                       seq_puts(s, "\n");
                }
                selector++;
        }
 
+       mutex_unlock(&pctldev->mutex);
 
        return 0;
 }
@@ -889,8 +1448,9 @@ static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
 
        seq_puts(s, "GPIO ranges handled:\n");
 
+       mutex_lock(&pctldev->mutex);
+
        /* Loop over the ranges */
-       mutex_lock(&pctldev->gpio_ranges_lock);
        list_for_each_entry(range, &pctldev->gpio_ranges, node) {
                seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
                           range->id, range->name,
@@ -898,44 +1458,8 @@ static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
                           range->pin_base,
                           (range->pin_base + range->npins - 1));
        }
-       mutex_unlock(&pctldev->gpio_ranges_lock);
-
-       return 0;
-}
-
-static int pinctrl_maps_show(struct seq_file *s, void *what)
-{
-       int i;
-
-       seq_puts(s, "Pinctrl maps:\n");
-
-       for (i = 0; i < pinctrl_maps_num; i++) {
-               struct pinctrl_map const *map = &pinctrl_maps[i];
-
-               seq_printf(s, "%s:\n", map->name);
-               if (map->dev_name)
-                       seq_printf(s, "  device: %s\n",
-                                  map->dev_name);
-               else
-                       seq_printf(s, "  SYSTEM MUX\n");
-               seq_printf(s, "  controlling device %s\n",
-                          map->ctrl_dev_name);
-               seq_printf(s, "  function: %s\n", map->function);
-               seq_printf(s, "  group: %s\n", map->group ? map->group :
-                          "(default)");
-       }
-       return 0;
-}
-
-static int pinmux_hogs_show(struct seq_file *s, void *what)
-{
-       struct pinctrl_dev *pctldev = s->private;
-       struct pinctrl_hog *hog;
-
-       seq_puts(s, "Pin control map hogs held by device\n");
 
-       list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
-               seq_printf(s, "%s\n", hog->map->name);
+       mutex_unlock(&pctldev->mutex);
 
        return 0;
 }
@@ -945,7 +1469,9 @@ static int pinctrl_devices_show(struct seq_file *s, void *what)
        struct pinctrl_dev *pctldev;
 
        seq_puts(s, "name [pinmux] [pinconf]\n");
+
        mutex_lock(&pinctrldev_list_mutex);
+
        list_for_each_entry(pctldev, &pinctrldev_list, node) {
                seq_printf(s, "%s ", pctldev->desc->name);
                if (pctldev->desc->pmxops)
@@ -958,34 +1484,107 @@ static int pinctrl_devices_show(struct seq_file *s, void *what)
                        seq_puts(s, "no");
                seq_puts(s, "\n");
        }
+
        mutex_unlock(&pinctrldev_list_mutex);
 
        return 0;
 }
 
-static int pinctrl_show(struct seq_file *s, void *what)
+static inline const char *map_type(enum pinctrl_map_type type)
 {
-       struct pinctrl *p;
+       static const char * const names[] = {
+               "INVALID",
+               "DUMMY_STATE",
+               "MUX_GROUP",
+               "CONFIGS_PIN",
+               "CONFIGS_GROUP",
+       };
+
+       if (type >= ARRAY_SIZE(names))
+               return "UNKNOWN";
+
+       return names[type];
+}
 
-       seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
-       list_for_each_entry(p, &pinctrl_list, node) {
-               struct pinctrl_dev *pctldev = p->pctldev;
+static int pinctrl_maps_show(struct seq_file *s, void *what)
+{
+       struct pinctrl_maps *maps_node;
+       int i;
+       struct pinctrl_map const *map;
 
-               if (!pctldev) {
-                       seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
-                       continue;
+       seq_puts(s, "Pinctrl maps:\n");
+
+       mutex_lock(&pinctrl_maps_mutex);
+       for_each_maps(maps_node, i, map) {
+               seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
+                          map->dev_name, map->name, map_type(map->type),
+                          map->type);
+
+               if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
+                       seq_printf(s, "controlling device %s\n",
+                                  map->ctrl_dev_name);
+
+               switch (map->type) {
+               case PIN_MAP_TYPE_MUX_GROUP:
+                       pinmux_show_map(s, map);
+                       break;
+               case PIN_MAP_TYPE_CONFIGS_PIN:
+               case PIN_MAP_TYPE_CONFIGS_GROUP:
+                       pinconf_show_map(s, map);
+                       break;
+               default:
+                       break;
                }
 
-               seq_printf(s, "device: %s",
-                          pinctrl_dev_get_name(p->pctldev));
+               seq_printf(s, "\n");
+       }
+       mutex_unlock(&pinctrl_maps_mutex);
+
+       return 0;
+}
 
-               pinmux_dbg_show(s, p);
+static int pinctrl_show(struct seq_file *s, void *what)
+{
+       struct pinctrl *p;
+       struct pinctrl_state *state;
+       struct pinctrl_setting *setting;
+
+       seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
+
+       mutex_lock(&pinctrl_list_mutex);
 
-               seq_printf(s, " users: %u map-> %s\n",
-                          p->usecount,
-                          p->dev ? dev_name(p->dev) : "(system)");
+       list_for_each_entry(p, &pinctrl_list, node) {
+               seq_printf(s, "device: %s current state: %s\n",
+                          dev_name(p->dev),
+                          p->state ? p->state->name : "none");
+
+               list_for_each_entry(state, &p->states, node) {
+                       seq_printf(s, "  state: %s\n", state->name);
+
+                       list_for_each_entry(setting, &state->settings, node) {
+                               struct pinctrl_dev *pctldev = setting->pctldev;
+
+                               seq_printf(s, "    type: %s controller %s ",
+                                          map_type(setting->type),
+                                          pinctrl_dev_get_name(pctldev));
+
+                               switch (setting->type) {
+                               case PIN_MAP_TYPE_MUX_GROUP:
+                                       pinmux_show_setting(s, setting);
+                                       break;
+                               case PIN_MAP_TYPE_CONFIGS_PIN:
+                               case PIN_MAP_TYPE_CONFIGS_GROUP:
+                                       pinconf_show_setting(s, setting);
+                                       break;
+                               default:
+                                       break;
+                               }
+                       }
+               }
        }
 
+       mutex_unlock(&pinctrl_list_mutex);
+
        return 0;
 }
 
@@ -1004,19 +1603,14 @@ static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
        return single_open(file, pinctrl_gpioranges_show, inode->i_private);
 }
 
-static int pinctrl_maps_open(struct inode *inode, struct file *file)
-{
-       return single_open(file, pinctrl_maps_show, inode->i_private);
-}
-
-static int pinmux_hogs_open(struct inode *inode, struct file *file)
+static int pinctrl_devices_open(struct inode *inode, struct file *file)
 {
-       return single_open(file, pinmux_hogs_show, inode->i_private);
+       return single_open(file, pinctrl_devices_show, NULL);
 }
 
-static int pinctrl_devices_open(struct inode *inode, struct file *file)
+static int pinctrl_maps_open(struct inode *inode, struct file *file)
 {
-       return single_open(file, pinctrl_devices_show, NULL);
+       return single_open(file, pinctrl_maps_show, NULL);
 }
 
 static int pinctrl_open(struct inode *inode, struct file *file)
@@ -1045,22 +1639,15 @@ static const struct file_operations pinctrl_gpioranges_ops = {
        .release        = single_release,
 };
 
-static const struct file_operations pinctrl_maps_ops = {
-       .open           = pinctrl_maps_open,
-       .read           = seq_read,
-       .llseek         = seq_lseek,
-       .release        = single_release,
-};
-
-static const struct file_operations pinmux_hogs_ops = {
-       .open           = pinmux_hogs_open,
+static const struct file_operations pinctrl_devices_ops = {
+       .open           = pinctrl_devices_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = single_release,
 };
 
-static const struct file_operations pinctrl_devices_ops = {
-       .open           = pinctrl_devices_open,
+static const struct file_operations pinctrl_maps_ops = {
+       .open           = pinctrl_maps_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = single_release,
@@ -1094,10 +1681,6 @@ static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
                            device_root, pctldev, &pinctrl_groups_ops);
        debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
                            device_root, pctldev, &pinctrl_gpioranges_ops);
-       debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
-                           device_root, pctldev, &pinctrl_maps_ops);
-       debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
-                           device_root, pctldev, &pinmux_hogs_ops);
        pinmux_init_device_debugfs(device_root, pctldev);
        pinconf_init_device_debugfs(device_root, pctldev);
 }
@@ -1118,6 +1701,8 @@ static void pinctrl_init_debugfs(void)
 
        debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
                            debugfs_root, NULL, &pinctrl_devices_ops);
+       debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
+                           debugfs_root, NULL, &pinctrl_maps_ops);
        debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
                            debugfs_root, NULL, &pinctrl_ops);
 }
@@ -1138,6 +1723,22 @@ static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
 
 #endif
 
+static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
+{
+       const struct pinctrl_ops *ops = pctldev->desc->pctlops;
+
+       if (!ops ||
+           !ops->get_groups_count ||
+           !ops->get_group_name ||
+           !ops->get_group_pins)
+               return -EINVAL;
+
+       if (ops->dt_node_to_map && !ops->dt_free_map)
+               return -EINVAL;
+
+       return 0;
+}
+
 /**
  * pinctrl_register() - register a pin controller device
  * @pctldesc: descriptor for this pin controller
@@ -1150,64 +1751,85 @@ struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
        struct pinctrl_dev *pctldev;
        int ret;
 
-       if (pctldesc == NULL)
+       if (!pctldesc)
                return NULL;
-       if (pctldesc->name == NULL)
+       if (!pctldesc->name)
                return NULL;
 
-       pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
-       if (pctldev == NULL)
+       pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
+       if (pctldev == NULL) {
+               dev_err(dev, "failed to alloc struct pinctrl_dev\n");
                return NULL;
+       }
 
        /* Initialize pin control device struct */
        pctldev->owner = pctldesc->owner;
        pctldev->desc = pctldesc;
        pctldev->driver_data = driver_data;
        INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
-       spin_lock_init(&pctldev->pin_desc_tree_lock);
        INIT_LIST_HEAD(&pctldev->gpio_ranges);
-       mutex_init(&pctldev->gpio_ranges_lock);
        pctldev->dev = dev;
+       mutex_init(&pctldev->mutex);
+
+       /* check core ops for sanity */
+       if (pinctrl_check_ops(pctldev)) {
+               dev_err(dev, "pinctrl ops lacks necessary functions\n");
+               goto out_err;
+       }
 
        /* If we're implementing pinmuxing, check the ops for sanity */
        if (pctldesc->pmxops) {
-               ret = pinmux_check_ops(pctldev);
-               if (ret) {
-                       pr_err("%s pinmux ops lacks necessary functions\n",
-                              pctldesc->name);
+               if (pinmux_check_ops(pctldev))
                        goto out_err;
-               }
        }
 
        /* If we're implementing pinconfig, check the ops for sanity */
        if (pctldesc->confops) {
-               ret = pinconf_check_ops(pctldev);
-               if (ret) {
-                       pr_err("%s pin config ops lacks necessary functions\n",
-                              pctldesc->name);
+               if (pinconf_check_ops(pctldev))
                        goto out_err;
-               }
        }
 
        /* Register all the pins */
-       pr_debug("try to register %d pins on %s...\n",
-                pctldesc->npins, pctldesc->name);
+       dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
        ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
        if (ret) {
-               pr_err("error during pin registration\n");
+               dev_err(dev, "error during pin registration\n");
                pinctrl_free_pindescs(pctldev, pctldesc->pins,
                                      pctldesc->npins);
                goto out_err;
        }
 
-       pinctrl_init_device_debugfs(pctldev);
        mutex_lock(&pinctrldev_list_mutex);
-       list_add(&pctldev->node, &pinctrldev_list);
+       list_add_tail(&pctldev->node, &pinctrldev_list);
        mutex_unlock(&pinctrldev_list_mutex);
-       pinctrl_hog_maps(pctldev);
+
+       pctldev->p = pinctrl_get(pctldev->dev);
+
+       if (!IS_ERR(pctldev->p)) {
+               pctldev->hog_default =
+                       pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
+               if (IS_ERR(pctldev->hog_default)) {
+                       dev_dbg(dev, "failed to lookup the default state\n");
+               } else {
+                       if (pinctrl_select_state(pctldev->p,
+                                               pctldev->hog_default))
+                               dev_err(dev,
+                                       "failed to select default state\n");
+               }
+
+               pctldev->hog_sleep =
+                       pinctrl_lookup_state(pctldev->p,
+                                                   PINCTRL_STATE_SLEEP);
+               if (IS_ERR(pctldev->hog_sleep))
+                       dev_dbg(dev, "failed to lookup the sleep state\n");
+       }
+
+       pinctrl_init_device_debugfs(pctldev);
+
        return pctldev;
 
 out_err:
+       mutex_destroy(&pctldev->mutex);
        kfree(pctldev);
        return NULL;
 }
@@ -1221,19 +1843,32 @@ EXPORT_SYMBOL_GPL(pinctrl_register);
  */
 void pinctrl_unregister(struct pinctrl_dev *pctldev)
 {
+       struct pinctrl_gpio_range *range, *n;
        if (pctldev == NULL)
                return;
 
+       mutex_lock(&pctldev->mutex);
        pinctrl_remove_device_debugfs(pctldev);
-       pinctrl_unhog_maps(pctldev);
-       /* TODO: check that no pinmuxes are still active? */
+       mutex_unlock(&pctldev->mutex);
+
+       if (!IS_ERR(pctldev->p))
+               pinctrl_put(pctldev->p);
+
        mutex_lock(&pinctrldev_list_mutex);
+       mutex_lock(&pctldev->mutex);
+       /* TODO: check that no pinmuxes are still active? */
        list_del(&pctldev->node);
-       mutex_unlock(&pinctrldev_list_mutex);
        /* Destroy descriptor tree */
        pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
                              pctldev->desc->npins);
+       /* remove gpio ranges map */
+       list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
+               list_del(&range->node);
+
+       mutex_unlock(&pctldev->mutex);
+       mutex_destroy(&pctldev->mutex);
        kfree(pctldev);
+       mutex_unlock(&pinctrldev_list_mutex);
 }
 EXPORT_SYMBOL_GPL(pinctrl_unregister);