]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/gpio/gpiolib.c
x86: section mismatch fix
[linux-2.6-omap-h63xx.git] / drivers / gpio / gpiolib.c
index 623fcd9b547a4e5a47c430172e253dd1492d9bf8..beaf6b3a37dcefb0b2c1d8c8f79b7db2713dba45 100644 (file)
@@ -43,6 +43,7 @@ struct gpio_desc {
 /* flag symbols are bit numbers */
 #define FLAG_REQUESTED 0
 #define FLAG_IS_OUT    1
+#define FLAG_RESERVED  2
 
 #ifdef CONFIG_DEBUG_FS
        const char              *label;
@@ -80,6 +81,76 @@ static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
        return gpio_desc[gpio].chip;
 }
 
+/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
+static int gpiochip_find_base(int ngpio)
+{
+       int i;
+       int spare = 0;
+       int base = -ENOSPC;
+
+       for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
+               struct gpio_desc *desc = &gpio_desc[i];
+               struct gpio_chip *chip = desc->chip;
+
+               if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
+                       spare++;
+                       if (spare == ngpio) {
+                               base = i;
+                               break;
+                       }
+               } else {
+                       spare = 0;
+                       if (chip)
+                               i -= chip->ngpio - 1;
+               }
+       }
+
+       if (gpio_is_valid(base))
+               pr_debug("%s: found new base at %d\n", __func__, base);
+       return base;
+}
+
+/**
+ * gpiochip_reserve() - reserve range of gpios to use with platform code only
+ * @start: starting gpio number
+ * @ngpio: number of gpios to reserve
+ * Context: platform init, potentially before irqs or kmalloc will work
+ *
+ * Returns a negative errno if any gpio within the range is already reserved
+ * or registered, else returns zero as a success code.  Use this function
+ * to mark a range of gpios as unavailable for dynamic gpio number allocation,
+ * for example because its driver support is not yet loaded.
+ */
+int __init gpiochip_reserve(int start, int ngpio)
+{
+       int ret = 0;
+       unsigned long flags;
+       int i;
+
+       if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
+               return -EINVAL;
+
+       spin_lock_irqsave(&gpio_lock, flags);
+
+       for (i = start; i < start + ngpio; i++) {
+               struct gpio_desc *desc = &gpio_desc[i];
+
+               if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
+                       ret = -EBUSY;
+                       goto err;
+               }
+
+               set_bit(FLAG_RESERVED, &desc->flags);
+       }
+
+       pr_debug("%s: reserved gpios from %d to %d\n",
+                __func__, start, start + ngpio - 1);
+err:
+       spin_unlock_irqrestore(&gpio_lock, flags);
+
+       return ret;
+}
+
 /**
  * gpiochip_add() - register a gpio_chip
  * @chip: the chip to register, with chip->base initialized
@@ -88,44 +159,55 @@ static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  * Returns a negative errno if the chip can't be registered, such as
  * because the chip->base is invalid or already associated with a
  * different chip.  Otherwise it returns zero as a success code.
+ *
+ * If chip->base is negative, this requests dynamic assignment of
+ * a range of valid GPIOs.
  */
 int gpiochip_add(struct gpio_chip *chip)
 {
        unsigned long   flags;
        int             status = 0;
        unsigned        id;
+       int             base = chip->base;
 
-       /* NOTE chip->base negative is reserved to mean a request for
-        * dynamic allocation.  We don't currently support that.
-        */
-
-       if (chip->base < 0 || !gpio_is_valid(chip->base  + chip->ngpio)) {
+       if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
+                       && base >= 0) {
                status = -EINVAL;
                goto fail;
        }
 
        spin_lock_irqsave(&gpio_lock, flags);
 
+       if (base < 0) {
+               base = gpiochip_find_base(chip->ngpio);
+               if (base < 0) {
+                       status = base;
+                       goto fail_unlock;
+               }
+               chip->base = base;
+       }
+
        /* these GPIO numbers must not be managed by another gpio_chip */
-       for (id = chip->base; id < chip->base + chip->ngpio; id++) {
+       for (id = base; id < base + chip->ngpio; id++) {
                if (gpio_desc[id].chip != NULL) {
                        status = -EBUSY;
                        break;
                }
        }
        if (status == 0) {
-               for (id = chip->base; id < chip->base + chip->ngpio; id++) {
+               for (id = base; id < base + chip->ngpio; id++) {
                        gpio_desc[id].chip = chip;
                        gpio_desc[id].flags = 0;
                }
        }
 
+fail_unlock:
        spin_unlock_irqrestore(&gpio_lock, flags);
 fail:
        /* failures here can mean systems won't boot... */
        if (status)
                pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
-                       chip->base, chip->base + chip->ngpio,
+                       chip->base, chip->base + chip->ngpio - 1,
                        chip->label ? : "generic");
        return status;
 }
@@ -300,7 +382,7 @@ fail:
        spin_unlock_irqrestore(&gpio_lock, flags);
        if (status)
                pr_debug("%s: gpio-%d status %d\n",
-                       __FUNCTION__, gpio, status);
+                       __func__, gpio, status);
        return status;
 }
 EXPORT_SYMBOL_GPL(gpio_direction_input);
@@ -338,7 +420,7 @@ fail:
        spin_unlock_irqrestore(&gpio_lock, flags);
        if (status)
                pr_debug("%s: gpio-%d status %d\n",
-                       __FUNCTION__, gpio, status);
+                       __func__, gpio, status);
        return status;
 }
 EXPORT_SYMBOL_GPL(gpio_direction_output);