gpiolib: make names array and its values const
[safe/jmp/linux-2.6] / drivers / gpio / gpiolib.c
index 29a7e6b..ce267ee 100644 (file)
@@ -1,5 +1,6 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/spinlock.h>
 #include <linux/device.h>
@@ -7,6 +8,8 @@
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
 #include <linux/gpio.h>
+#include <linux/idr.h>
+#include <linux/slab.h>
 
 
 /* Optional implementation infrastructure for GPIO interfaces.
@@ -49,6 +52,14 @@ struct gpio_desc {
 #define FLAG_RESERVED  2
 #define FLAG_EXPORT    3       /* protected by sysfs_lock */
 #define FLAG_SYSFS     4       /* exported via /sys/class/gpio/control */
+#define FLAG_TRIG_FALL 5       /* trigger on falling edge */
+#define FLAG_TRIG_RISE 6       /* trigger on rising edge */
+#define FLAG_ACTIVE_LOW        7       /* sysfs value has active low */
+
+#define PDESC_ID_SHIFT 16      /* add new flags before this one */
+
+#define GPIO_FLAGS_MASK                ((1 << PDESC_ID_SHIFT) - 1)
+#define GPIO_TRIGGER_MASK      (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
 
 #ifdef CONFIG_DEBUG_FS
        const char              *label;
@@ -56,6 +67,15 @@ struct gpio_desc {
 };
 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
 
+#ifdef CONFIG_GPIO_SYSFS
+struct poll_desc {
+       struct work_struct      work;
+       struct sysfs_dirent     *value_sd;
+};
+
+static struct idr pdesc_idr;
+#endif
+
 static inline void desc_set_label(struct gpio_desc *d, const char *label)
 {
 #ifdef CONFIG_DEBUG_FS
@@ -67,17 +87,32 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
  * when setting direction, and otherwise illegal.  Until board setup code
  * and drivers use explicit requests everywhere (which won't happen when
  * those calls have no teeth) we can't avoid autorequesting.  This nag
- * message should motivate switching to explicit requests...
+ * message should motivate switching to explicit requests... so should
+ * the weaker cleanup after faults, compared to gpio_request().
+ *
+ * NOTE: the autorequest mechanism is going away; at this point it's
+ * only "legal" in the sense that (old) code using it won't break yet,
+ * but instead only triggers a WARN() stack dump.
  */
-static void gpio_ensure_requested(struct gpio_desc *desc)
+static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
 {
-       if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
-               pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc));
+       const struct gpio_chip *chip = desc->chip;
+       const int gpio = chip->base + offset;
+
+       if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
+                       "autorequest GPIO-%d\n", gpio)) {
+               if (!try_module_get(chip->owner)) {
+                       pr_err("GPIO-%d: module can't be gotten \n", gpio);
+                       clear_bit(FLAG_REQUESTED, &desc->flags);
+                       /* lose */
+                       return -EIO;
+               }
                desc_set_label(desc, "[auto]");
-               if (!try_module_get(desc->chip->owner))
-                       pr_err("GPIO-%d: module can't be gotten \n",
-                                       (int)(desc - gpio_desc));
+               /* caller must chip->request() w/o spinlock */
+               if (chip->request)
+                       return 1;
        }
+       return 0;
 }
 
 /* caller holds gpio_lock *OR* gpio is marked as requested */
@@ -173,10 +208,15 @@ static DEFINE_MUTEX(sysfs_lock);
  *   /value
  *      * always readable, subject to hardware behavior
  *      * may be writable, as zero/nonzero
- *
- * REVISIT there will likely be an attribute for configuring async
- * notifications, e.g. to specify polling interval or IRQ trigger type
- * that would for example trigger a poll() on the "value".
+ *   /edge
+ *      * configures behavior of poll(2) on /value
+ *      * available only if pin can generate IRQs on input
+ *      * is read/write as "none", "falling", "rising", or "both"
+ *   /active_low
+ *      * configures polarity of /value
+ *      * is read/write as zero/nonzero
+ *      * also affects existing and subsequent "falling" and "rising"
+ *        /edge configuration
  */
 
 static ssize_t gpio_direction_show(struct device *dev,
@@ -222,7 +262,7 @@ static ssize_t gpio_direction_store(struct device *dev,
        return status ? : size;
 }
 
-static const DEVICE_ATTR(direction, 0644,
+static /* const */ DEVICE_ATTR(direction, 0644,
                gpio_direction_show, gpio_direction_store);
 
 static ssize_t gpio_value_show(struct device *dev,
@@ -234,10 +274,17 @@ static ssize_t gpio_value_show(struct device *dev,
 
        mutex_lock(&sysfs_lock);
 
-       if (!test_bit(FLAG_EXPORT, &desc->flags))
+       if (!test_bit(FLAG_EXPORT, &desc->flags)) {
                status = -EIO;
-       else
-               status = sprintf(buf, "%d\n", gpio_get_value_cansleep(gpio));
+       } else {
+               int value;
+
+               value = !!gpio_get_value_cansleep(gpio);
+               if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
+                       value = !value;
+
+               status = sprintf(buf, "%d\n", value);
+       }
 
        mutex_unlock(&sysfs_lock);
        return status;
@@ -261,6 +308,8 @@ static ssize_t gpio_value_store(struct device *dev,
 
                status = strict_strtol(buf, 0, &value);
                if (status == 0) {
+                       if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
+                               value = !value;
                        gpio_set_value_cansleep(gpio, value != 0);
                        status = size;
                }
@@ -270,12 +319,254 @@ static ssize_t gpio_value_store(struct device *dev,
        return status;
 }
 
-static /*const*/ DEVICE_ATTR(value, 0644,
+static const DEVICE_ATTR(value, 0644,
                gpio_value_show, gpio_value_store);
 
+static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
+{
+       struct work_struct      *work = priv;
+
+       schedule_work(work);
+       return IRQ_HANDLED;
+}
+
+static void gpio_notify_sysfs(struct work_struct *work)
+{
+       struct poll_desc        *pdesc;
+
+       pdesc = container_of(work, struct poll_desc, work);
+       sysfs_notify_dirent(pdesc->value_sd);
+}
+
+static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
+               unsigned long gpio_flags)
+{
+       struct poll_desc        *pdesc;
+       unsigned long           irq_flags;
+       int                     ret, irq, id;
+
+       if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
+               return 0;
+
+       irq = gpio_to_irq(desc - gpio_desc);
+       if (irq < 0)
+               return -EIO;
+
+       id = desc->flags >> PDESC_ID_SHIFT;
+       pdesc = idr_find(&pdesc_idr, id);
+       if (pdesc) {
+               free_irq(irq, &pdesc->work);
+               cancel_work_sync(&pdesc->work);
+       }
+
+       desc->flags &= ~GPIO_TRIGGER_MASK;
+
+       if (!gpio_flags) {
+               ret = 0;
+               goto free_sd;
+       }
+
+       irq_flags = IRQF_SHARED;
+       if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
+               irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
+                       IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
+       if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
+               irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
+                       IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
+
+       if (!pdesc) {
+               pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
+               if (!pdesc) {
+                       ret = -ENOMEM;
+                       goto err_out;
+               }
+
+               do {
+                       ret = -ENOMEM;
+                       if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
+                               ret = idr_get_new_above(&pdesc_idr,
+                                               pdesc, 1, &id);
+               } while (ret == -EAGAIN);
+
+               if (ret)
+                       goto free_mem;
+
+               desc->flags &= GPIO_FLAGS_MASK;
+               desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
+
+               if (desc->flags >> PDESC_ID_SHIFT != id) {
+                       ret = -ERANGE;
+                       goto free_id;
+               }
+
+               pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
+               if (!pdesc->value_sd) {
+                       ret = -ENODEV;
+                       goto free_id;
+               }
+               INIT_WORK(&pdesc->work, gpio_notify_sysfs);
+       }
+
+       ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
+                       "gpiolib", &pdesc->work);
+       if (ret)
+               goto free_sd;
+
+       desc->flags |= gpio_flags;
+       return 0;
+
+free_sd:
+       if (pdesc)
+               sysfs_put(pdesc->value_sd);
+free_id:
+       idr_remove(&pdesc_idr, id);
+       desc->flags &= GPIO_FLAGS_MASK;
+free_mem:
+       kfree(pdesc);
+err_out:
+       return ret;
+}
+
+static const struct {
+       const char *name;
+       unsigned long flags;
+} trigger_types[] = {
+       { "none",    0 },
+       { "falling", BIT(FLAG_TRIG_FALL) },
+       { "rising",  BIT(FLAG_TRIG_RISE) },
+       { "both",    BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
+};
+
+static ssize_t gpio_edge_show(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       const struct gpio_desc  *desc = dev_get_drvdata(dev);
+       ssize_t                 status;
+
+       mutex_lock(&sysfs_lock);
+
+       if (!test_bit(FLAG_EXPORT, &desc->flags))
+               status = -EIO;
+       else {
+               int i;
+
+               status = 0;
+               for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
+                       if ((desc->flags & GPIO_TRIGGER_MASK)
+                                       == trigger_types[i].flags) {
+                               status = sprintf(buf, "%s\n",
+                                                trigger_types[i].name);
+                               break;
+                       }
+       }
+
+       mutex_unlock(&sysfs_lock);
+       return status;
+}
+
+static ssize_t gpio_edge_store(struct device *dev,
+               struct device_attribute *attr, const char *buf, size_t size)
+{
+       struct gpio_desc        *desc = dev_get_drvdata(dev);
+       ssize_t                 status;
+       int                     i;
+
+       for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
+               if (sysfs_streq(trigger_types[i].name, buf))
+                       goto found;
+       return -EINVAL;
+
+found:
+       mutex_lock(&sysfs_lock);
+
+       if (!test_bit(FLAG_EXPORT, &desc->flags))
+               status = -EIO;
+       else {
+               status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
+               if (!status)
+                       status = size;
+       }
+
+       mutex_unlock(&sysfs_lock);
+
+       return status;
+}
+
+static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
+
+static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
+                               int value)
+{
+       int                     status = 0;
+
+       if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
+               return 0;
+
+       if (value)
+               set_bit(FLAG_ACTIVE_LOW, &desc->flags);
+       else
+               clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
+
+       /* reconfigure poll(2) support if enabled on one edge only */
+       if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
+                               !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
+               unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
+
+               gpio_setup_irq(desc, dev, 0);
+               status = gpio_setup_irq(desc, dev, trigger_flags);
+       }
+
+       return status;
+}
+
+static ssize_t gpio_active_low_show(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       const struct gpio_desc  *desc = dev_get_drvdata(dev);
+       ssize_t                 status;
+
+       mutex_lock(&sysfs_lock);
+
+       if (!test_bit(FLAG_EXPORT, &desc->flags))
+               status = -EIO;
+       else
+               status = sprintf(buf, "%d\n",
+                               !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
+
+       mutex_unlock(&sysfs_lock);
+
+       return status;
+}
+
+static ssize_t gpio_active_low_store(struct device *dev,
+               struct device_attribute *attr, const char *buf, size_t size)
+{
+       struct gpio_desc        *desc = dev_get_drvdata(dev);
+       ssize_t                 status;
+
+       mutex_lock(&sysfs_lock);
+
+       if (!test_bit(FLAG_EXPORT, &desc->flags)) {
+               status = -EIO;
+       } else {
+               long            value;
+
+               status = strict_strtol(buf, 0, &value);
+               if (status == 0)
+                       status = sysfs_set_active_low(desc, dev, value != 0);
+       }
+
+       mutex_unlock(&sysfs_lock);
+
+       return status ? : size;
+}
+
+static const DEVICE_ATTR(active_low, 0644,
+               gpio_active_low_show, gpio_active_low_store);
+
 static const struct attribute *gpio_attrs[] = {
-       &dev_attr_direction.attr,
        &dev_attr_value.attr,
+       &dev_attr_active_low.attr,
        NULL,
 };
 
@@ -334,7 +625,9 @@ static const struct attribute_group gpiochip_attr_group = {
  * /sys/class/gpio/unexport ... write-only
  *     integer N ... number of GPIO to unexport
  */
-static ssize_t export_store(struct class *class, const char *buf, size_t len)
+static ssize_t export_store(struct class *class,
+                               struct class_attribute *attr,
+                               const char *buf, size_t len)
 {
        long    gpio;
        int     status;
@@ -364,7 +657,9 @@ done:
        return status ? : len;
 }
 
-static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
+static ssize_t unexport_store(struct class *class,
+                               struct class_attribute *attr,
+                               const char *buf, size_t len)
 {
        long    gpio;
        int     status;
@@ -427,6 +722,7 @@ int gpio_export(unsigned gpio, bool direction_may_change)
        unsigned long           flags;
        struct gpio_desc        *desc;
        int                     status = -EINVAL;
+       const char              *ioname = NULL;
 
        /* can't export until sysfs is available ... */
        if (!gpio_class.p) {
@@ -450,22 +746,33 @@ int gpio_export(unsigned gpio, bool direction_may_change)
        }
        spin_unlock_irqrestore(&gpio_lock, flags);
 
+       if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
+               ioname = desc->chip->names[gpio - desc->chip->base];
+
        if (status == 0) {
                struct device   *dev;
 
                dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
-                                       desc, "gpio%d", gpio);
-               if (dev) {
-                       if (direction_may_change)
-                               status = sysfs_create_group(&dev->kobj,
+                               desc, ioname ? ioname : "gpio%d", gpio);
+               if (!IS_ERR(dev)) {
+                       status = sysfs_create_group(&dev->kobj,
                                                &gpio_attr_group);
-                       else
+
+                       if (!status && direction_may_change)
+                               status = device_create_file(dev,
+                                               &dev_attr_direction);
+
+                       if (!status && gpio_to_irq(gpio) >= 0
+                                       && (direction_may_change
+                                               || !test_bit(FLAG_IS_OUT,
+                                                       &desc->flags)))
                                status = device_create_file(dev,
-                                               &dev_attr_value);
+                                               &dev_attr_edge);
+
                        if (status != 0)
                                device_unregister(dev);
                } else
-                       status = -ENODEV;
+                       status = PTR_ERR(dev);
                if (status == 0)
                        set_bit(FLAG_EXPORT, &desc->flags);
        }
@@ -486,6 +793,98 @@ static int match_export(struct device *dev, void *data)
 }
 
 /**
+ * gpio_export_link - create a sysfs link to an exported GPIO node
+ * @dev: device under which to create symlink
+ * @name: name of the symlink
+ * @gpio: gpio to create symlink to, already exported
+ *
+ * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
+ * node. Caller is responsible for unlinking.
+ *
+ * Returns zero on success, else an error.
+ */
+int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
+{
+       struct gpio_desc        *desc;
+       int                     status = -EINVAL;
+
+       if (!gpio_is_valid(gpio))
+               goto done;
+
+       mutex_lock(&sysfs_lock);
+
+       desc = &gpio_desc[gpio];
+
+       if (test_bit(FLAG_EXPORT, &desc->flags)) {
+               struct device *tdev;
+
+               tdev = class_find_device(&gpio_class, NULL, desc, match_export);
+               if (tdev != NULL) {
+                       status = sysfs_create_link(&dev->kobj, &tdev->kobj,
+                                               name);
+               } else {
+                       status = -ENODEV;
+               }
+       }
+
+       mutex_unlock(&sysfs_lock);
+
+done:
+       if (status)
+               pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
+
+       return status;
+}
+EXPORT_SYMBOL_GPL(gpio_export_link);
+
+
+/**
+ * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
+ * @gpio: gpio to change
+ * @value: non-zero to use active low, i.e. inverted values
+ *
+ * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
+ * The GPIO does not have to be exported yet.  If poll(2) support has
+ * been enabled for either rising or falling edge, it will be
+ * reconfigured to follow the new polarity.
+ *
+ * Returns zero on success, else an error.
+ */
+int gpio_sysfs_set_active_low(unsigned gpio, int value)
+{
+       struct gpio_desc        *desc;
+       struct device           *dev = NULL;
+       int                     status = -EINVAL;
+
+       if (!gpio_is_valid(gpio))
+               goto done;
+
+       mutex_lock(&sysfs_lock);
+
+       desc = &gpio_desc[gpio];
+
+       if (test_bit(FLAG_EXPORT, &desc->flags)) {
+               dev = class_find_device(&gpio_class, NULL, desc, match_export);
+               if (dev == NULL) {
+                       status = -ENODEV;
+                       goto unlock;
+               }
+       }
+
+       status = sysfs_set_active_low(desc, dev, value);
+
+unlock:
+       mutex_unlock(&sysfs_lock);
+
+done:
+       if (status)
+               pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
+
+       return status;
+}
+EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
+
+/**
  * gpio_unexport - reverse effect of gpio_export()
  * @gpio: gpio to make unavailable
  *
@@ -502,11 +901,13 @@ void gpio_unexport(unsigned gpio)
        mutex_lock(&sysfs_lock);
 
        desc = &gpio_desc[gpio];
+
        if (test_bit(FLAG_EXPORT, &desc->flags)) {
                struct device   *dev = NULL;
 
                dev = class_find_device(&gpio_class, NULL, desc, match_export);
                if (dev) {
+                       gpio_setup_irq(desc, dev, 0);
                        clear_bit(FLAG_EXPORT, &desc->flags);
                        put_device(dev);
                        device_unregister(dev);
@@ -539,11 +940,11 @@ static int gpiochip_export(struct gpio_chip *chip)
        mutex_lock(&sysfs_lock);
        dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
                                "gpiochip%d", chip->base);
-       if (dev) {
+       if (!IS_ERR(dev)) {
                status = sysfs_create_group(&dev->kobj,
                                &gpiochip_attr_group);
        } else
-               status = -ENODEV;
+               status = PTR_ERR(dev);
        chip->exported = (status == 0);
        mutex_unlock(&sysfs_lock);
 
@@ -591,6 +992,8 @@ static int __init gpiolib_sysfs_init(void)
        unsigned long   flags;
        unsigned        gpio;
 
+       idr_init(&pdesc_idr);
+
        status = class_register(&gpio_class);
        if (status < 0)
                return status;
@@ -752,6 +1155,7 @@ EXPORT_SYMBOL_GPL(gpiochip_remove);
 int gpio_request(unsigned gpio, const char *label)
 {
        struct gpio_desc        *desc;
+       struct gpio_chip        *chip;
        int                     status = -EINVAL;
        unsigned long           flags;
 
@@ -760,14 +1164,15 @@ int gpio_request(unsigned gpio, const char *label)
        if (!gpio_is_valid(gpio))
                goto done;
        desc = &gpio_desc[gpio];
-       if (desc->chip == NULL)
+       chip = desc->chip;
+       if (chip == NULL)
                goto done;
 
-       if (!try_module_get(desc->chip->owner))
+       if (!try_module_get(chip->owner))
                goto done;
 
        /* NOTE:  gpio_request() can be called in early boot,
-        * before IRQs are enabled.
+        * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
         */
 
        if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
@@ -775,7 +1180,21 @@ int gpio_request(unsigned gpio, const char *label)
                status = 0;
        } else {
                status = -EBUSY;
-               module_put(desc->chip->owner);
+               module_put(chip->owner);
+               goto done;
+       }
+
+       if (chip->request) {
+               /* chip->request may sleep */
+               spin_unlock_irqrestore(&gpio_lock, flags);
+               status = chip->request(chip, gpio - chip->base);
+               spin_lock_irqsave(&gpio_lock, flags);
+
+               if (status < 0) {
+                       desc_set_label(desc, NULL);
+                       module_put(chip->owner);
+                       clear_bit(FLAG_REQUESTED, &desc->flags);
+               }
        }
 
 done:
@@ -791,6 +1210,7 @@ void gpio_free(unsigned gpio)
 {
        unsigned long           flags;
        struct gpio_desc        *desc;
+       struct gpio_chip        *chip;
 
        might_sleep();
 
@@ -804,9 +1224,18 @@ void gpio_free(unsigned gpio)
        spin_lock_irqsave(&gpio_lock, flags);
 
        desc = &gpio_desc[gpio];
-       if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) {
+       chip = desc->chip;
+       if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
+               if (chip->free) {
+                       spin_unlock_irqrestore(&gpio_lock, flags);
+                       might_sleep_if(extra_checks && chip->can_sleep);
+                       chip->free(chip, gpio - chip->base);
+                       spin_lock_irqsave(&gpio_lock, flags);
+               }
                desc_set_label(desc, NULL);
                module_put(desc->chip->owner);
+               clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
+               clear_bit(FLAG_REQUESTED, &desc->flags);
        } else
                WARN_ON(extra_checks);
 
@@ -814,6 +1243,64 @@ void gpio_free(unsigned gpio)
 }
 EXPORT_SYMBOL_GPL(gpio_free);
 
+/**
+ * gpio_request_one - request a single GPIO with initial configuration
+ * @gpio:      the GPIO number
+ * @flags:     GPIO configuration as specified by GPIOF_*
+ * @label:     a literal description string of this GPIO
+ */
+int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
+{
+       int err;
+
+       err = gpio_request(gpio, label);
+       if (err)
+               return err;
+
+       if (flags & GPIOF_DIR_IN)
+               err = gpio_direction_input(gpio);
+       else
+               err = gpio_direction_output(gpio,
+                               (flags & GPIOF_INIT_HIGH) ? 1 : 0);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(gpio_request_one);
+
+/**
+ * gpio_request_array - request multiple GPIOs in a single call
+ * @array:     array of the 'struct gpio'
+ * @num:       how many GPIOs in the array
+ */
+int gpio_request_array(struct gpio *array, size_t num)
+{
+       int i, err;
+
+       for (i = 0; i < num; i++, array++) {
+               err = gpio_request_one(array->gpio, array->flags, array->label);
+               if (err)
+                       goto err_free;
+       }
+       return 0;
+
+err_free:
+       while (i--)
+               gpio_free((--array)->gpio);
+       return err;
+}
+EXPORT_SYMBOL_GPL(gpio_request_array);
+
+/**
+ * gpio_free_array - release multiple GPIOs in a single call
+ * @array:     array of the 'struct gpio'
+ * @num:       how many GPIOs in the array
+ */
+void gpio_free_array(struct gpio *array, size_t num)
+{
+       while (num--)
+               gpio_free((array++)->gpio);
+}
+EXPORT_SYMBOL_GPL(gpio_free_array);
 
 /**
  * gpiochip_is_requested - return string iff signal was requested
@@ -871,7 +1358,9 @@ int gpio_direction_input(unsigned gpio)
        gpio -= chip->base;
        if (gpio >= chip->ngpio)
                goto fail;
-       gpio_ensure_requested(desc);
+       status = gpio_ensure_requested(desc, gpio);
+       if (status < 0)
+               goto fail;
 
        /* now we know the gpio is valid and chip won't vanish */
 
@@ -879,9 +1368,22 @@ int gpio_direction_input(unsigned gpio)
 
        might_sleep_if(extra_checks && chip->can_sleep);
 
+       if (status) {
+               status = chip->request(chip, gpio);
+               if (status < 0) {
+                       pr_debug("GPIO-%d: chip request fail, %d\n",
+                               chip->base + gpio, status);
+                       /* and it's not available to anyone else ...
+                        * gpio_request() is the fully clean solution.
+                        */
+                       goto lose;
+               }
+       }
+
        status = chip->direction_input(chip, gpio);
        if (status == 0)
                clear_bit(FLAG_IS_OUT, &desc->flags);
+lose:
        return status;
 fail:
        spin_unlock_irqrestore(&gpio_lock, flags);
@@ -909,7 +1411,9 @@ int gpio_direction_output(unsigned gpio, int value)
        gpio -= chip->base;
        if (gpio >= chip->ngpio)
                goto fail;
-       gpio_ensure_requested(desc);
+       status = gpio_ensure_requested(desc, gpio);
+       if (status < 0)
+               goto fail;
 
        /* now we know the gpio is valid and chip won't vanish */
 
@@ -917,9 +1421,22 @@ int gpio_direction_output(unsigned gpio, int value)
 
        might_sleep_if(extra_checks && chip->can_sleep);
 
+       if (status) {
+               status = chip->request(chip, gpio);
+               if (status < 0) {
+                       pr_debug("GPIO-%d: chip request fail, %d\n",
+                               chip->base + gpio, status);
+                       /* and it's not available to anyone else ...
+                        * gpio_request() is the fully clean solution.
+                        */
+                       goto lose;
+               }
+       }
+
        status = chip->direction_output(chip, gpio, value);
        if (status == 0)
                set_bit(FLAG_IS_OUT, &desc->flags);
+lose:
        return status;
 fail:
        spin_unlock_irqrestore(&gpio_lock, flags);
@@ -1040,7 +1557,7 @@ int gpio_get_value_cansleep(unsigned gpio)
 
        might_sleep_if(extra_checks);
        chip = gpio_to_chip(gpio);
-       return chip->get(chip, gpio - chip->base);
+       return chip->get ? chip->get(chip, gpio - chip->base) : 0;
 }
 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
 
@@ -1069,7 +1586,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
                        continue;
 
                is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
-               seq_printf(s, " gpio-%-3d (%-12s) %s %s",
+               seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
                        gpio, gdesc->label,
                        is_out ? "out" : "in ",
                        chip->get
@@ -1078,7 +1595,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 
                if (!is_out) {
                        int             irq = gpio_to_irq(gpio);
-                       struct irq_desc *desc = irq_desc + irq;
+                       struct irq_desc *desc = irq_to_desc(irq);
 
                        /* This races with request_irq(), set_irq_type(),
                         * and set_irq_wake() ... but those are "rare".
@@ -1148,7 +1665,7 @@ static int gpiolib_show(struct seq_file *s, void *unused)
                if (dev)
                        seq_printf(s, ", %s/%s",
                                dev->bus ? dev->bus->name : "no-bus",
-                               dev->bus_id);
+                               dev_name(dev));
                if (chip->label)
                        seq_printf(s, ", %s", chip->label);
                if (chip->can_sleep)
@@ -1169,7 +1686,7 @@ static int gpiolib_open(struct inode *inode, struct file *file)
        return single_open(file, gpiolib_show, NULL);
 }
 
-static struct file_operations gpiolib_operations = {
+static const struct file_operations gpiolib_operations = {
        .open           = gpiolib_open,
        .read           = seq_read,
        .llseek         = seq_lseek,