Merge tag 'hsi-for-3.19' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi
[cascardo/linux.git] / drivers / gpio / gpiolib.c
index 12d981a..487afe6 100644 (file)
@@ -47,8 +47,6 @@
  */
 DEFINE_SPINLOCK(gpio_lock);
 
-static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
-
 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
 
 static DEFINE_MUTEX(gpio_lookup_lock);
@@ -65,10 +63,24 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
  */
 struct gpio_desc *gpio_to_desc(unsigned gpio)
 {
-       if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
-               return NULL;
-       else
-               return &gpio_desc[gpio];
+       struct gpio_chip *chip;
+       unsigned long flags;
+
+       spin_lock_irqsave(&gpio_lock, flags);
+
+       list_for_each_entry(chip, &gpio_chips, list) {
+               if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
+                       spin_unlock_irqrestore(&gpio_lock, flags);
+                       return &chip->desc[gpio - chip->base];
+               }
+       }
+
+       spin_unlock_irqrestore(&gpio_lock, flags);
+
+       if (!gpio_is_valid(gpio))
+               WARN(1, "invalid GPIO %d\n", gpio);
+
+       return NULL;
 }
 EXPORT_SYMBOL_GPL(gpio_to_desc);
 
@@ -91,7 +103,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
  */
 int desc_to_gpio(const struct gpio_desc *desc)
 {
-       return desc - &gpio_desc[0];
+       return desc->chip->base + (desc - &desc->chip->desc[0]);
 }
 EXPORT_SYMBOL_GPL(desc_to_gpio);
 
@@ -138,7 +150,7 @@ static int gpiochip_find_base(int ngpio)
  *
  * This function may sleep if gpiod_cansleep() is true.
  */
-int gpiod_get_direction(const struct gpio_desc *desc)
+int gpiod_get_direction(struct gpio_desc *desc)
 {
        struct gpio_chip        *chip;
        unsigned                offset;
@@ -154,13 +166,11 @@ int gpiod_get_direction(const struct gpio_desc *desc)
        if (status > 0) {
                /* GPIOF_DIR_IN, or other positive */
                status = 1;
-               /* FLAG_IS_OUT is just a cache of the result of get_direction(),
-                * so it does not affect constness per se */
-               clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
+               clear_bit(FLAG_IS_OUT, &desc->flags);
        }
        if (status == 0) {
                /* GPIOF_DIR_OUT */
-               set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
+               set_bit(FLAG_IS_OUT, &desc->flags);
        }
        return status;
 }
@@ -206,7 +216,7 @@ static int gpiochip_add_to_list(struct gpio_chip *chip)
 /**
  * gpiochip_add() - register a gpio_chip
  * @chip: the chip to register, with chip->base initialized
- * Context: potentially before irqs or kmalloc will work
+ * Context: potentially before irqs will work
  *
  * Returns a negative errno if the chip can't be registered, such as
  * because the chip->base is invalid or already associated with a
@@ -226,12 +236,11 @@ int gpiochip_add(struct gpio_chip *chip)
        int             status = 0;
        unsigned        id;
        int             base = chip->base;
+       struct gpio_desc *descs;
 
-       if (base >= 0 &&
-           (!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))) {
-               status = -EINVAL;
-               goto fail;
-       }
+       descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
+       if (!descs)
+               return -ENOMEM;
 
        spin_lock_irqsave(&gpio_lock, flags);
 
@@ -247,10 +256,8 @@ int gpiochip_add(struct gpio_chip *chip)
        status = gpiochip_add_to_list(chip);
 
        if (status == 0) {
-               chip->desc = &gpio_desc[chip->base];
-
                for (id = 0; id < chip->ngpio; id++) {
-                       struct gpio_desc *desc = &chip->desc[id];
+                       struct gpio_desc *desc = &descs[id];
                        desc->chip = chip;
 
                        /* REVISIT:  most hardware initializes GPIOs as
@@ -266,6 +273,8 @@ int gpiochip_add(struct gpio_chip *chip)
                }
        }
 
+       chip->desc = descs;
+
        spin_unlock_irqrestore(&gpio_lock, flags);
 
 #ifdef CONFIG_PINCTRL
@@ -291,6 +300,9 @@ int gpiochip_add(struct gpio_chip *chip)
 unlock:
        spin_unlock_irqrestore(&gpio_lock, flags);
 fail:
+       kfree(descs);
+       chip->desc = NULL;
+
        /* failures here can mean systems won't boot... */
        pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
                chip->base, chip->base + chip->ngpio - 1,
@@ -331,6 +343,9 @@ void gpiochip_remove(struct gpio_chip *chip)
        list_del(&chip->list);
        spin_unlock_irqrestore(&gpio_lock, flags);
        gpiochip_unexport(chip);
+
+       kfree(chip->desc);
+       chip->desc = NULL;
 }
 EXPORT_SYMBOL_GPL(gpiochip_remove);
 
@@ -1673,14 +1688,36 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
                                        unsigned int idx,
                                        enum gpio_lookup_flags *flags)
 {
+       static const char * const suffixes[] = { "gpios", "gpio" };
+       struct acpi_device *adev = ACPI_COMPANION(dev);
        struct acpi_gpio_info info;
        struct gpio_desc *desc;
+       char propname[32];
+       int i;
 
-       desc = acpi_get_gpiod_by_index(dev, idx, &info);
-       if (IS_ERR(desc))
-               return desc;
+       /* Try first from _DSD */
+       for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
+               if (con_id && strcmp(con_id, "gpios")) {
+                       snprintf(propname, sizeof(propname), "%s-%s",
+                                con_id, suffixes[i]);
+               } else {
+                       snprintf(propname, sizeof(propname), "%s",
+                                suffixes[i]);
+               }
 
-       if (info.gpioint && info.active_low)
+               desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
+               if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
+                       break;
+       }
+
+       /* Then from plain _CRS GPIOs */
+       if (IS_ERR(desc)) {
+               desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
+               if (IS_ERR(desc))
+                       return desc;
+       }
+
+       if (info.active_low)
                *flags |= GPIO_ACTIVE_LOW;
 
        return desc;
@@ -1880,6 +1917,61 @@ struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(__gpiod_get_index);
 
+/**
+ * fwnode_get_named_gpiod - obtain a GPIO from firmware node
+ * @fwnode:    handle of the firmware node
+ * @propname:  name of the firmware property representing the GPIO
+ *
+ * This function can be used for drivers that get their configuration
+ * from firmware.
+ *
+ * Function properly finds the corresponding GPIO using whatever is the
+ * underlying firmware interface and then makes sure that the GPIO
+ * descriptor is requested before it is returned to the caller.
+ *
+ * In case of error an ERR_PTR() is returned.
+ */
+struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
+                                        const char *propname)
+{
+       struct gpio_desc *desc = ERR_PTR(-ENODEV);
+       bool active_low = false;
+       int ret;
+
+       if (!fwnode)
+               return ERR_PTR(-EINVAL);
+
+       if (is_of_node(fwnode)) {
+               enum of_gpio_flags flags;
+
+               desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
+                                               &flags);
+               if (!IS_ERR(desc))
+                       active_low = flags & OF_GPIO_ACTIVE_LOW;
+       } else if (is_acpi_node(fwnode)) {
+               struct acpi_gpio_info info;
+
+               desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
+                                              &info);
+               if (!IS_ERR(desc))
+                       active_low = info.active_low;
+       }
+
+       if (IS_ERR(desc))
+               return desc;
+
+       ret = gpiod_request(desc, NULL);
+       if (ret)
+               return ERR_PTR(ret);
+
+       /* Only value flag can be set from both DT and ACPI is active_low */
+       if (active_low)
+               set_bit(FLAG_ACTIVE_LOW, &desc->flags);
+
+       return desc;
+}
+EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
+
 /**
  * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  *                            function