Merge tag 'pinctrl-v4.15-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[sfrench/cifs-2.6.git] / drivers / gpio / gpiolib.c
index 236a9f55a265615509cf3fbccf5a9d6bf9e17317..aad84a6306c4e5ddbc3364d58add85fa1b1e2583 100644 (file)
@@ -72,6 +72,8 @@ static LIST_HEAD(gpio_lookup_list);
 LIST_HEAD(gpio_devices);
 
 static void gpiochip_free_hogs(struct gpio_chip *chip);
+static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
+                               struct lock_class_key *key);
 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
@@ -1097,30 +1099,8 @@ static void gpiochip_setup_devs(void)
        }
 }
 
-/**
- * gpiochip_add_data() - register a gpio_chip
- * @chip: the chip to register, with chip->base initialized
- * @data: driver-private data associated with this chip
- *
- * Context: potentially before irqs will work
- *
- * When gpiochip_add_data() is called very early during boot, so that GPIOs
- * can be freely used, the chip->parent device must be registered before
- * the gpio framework's arch_initcall().  Otherwise sysfs initialization
- * for GPIOs will fail rudely.
- *
- * gpiochip_add_data() must only be called after gpiolib initialization,
- * ie after core_initcall().
- *
- * If chip->base is negative, this requests dynamic assignment of
- * a range of valid GPIOs.
- *
- * 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.
- */
-int gpiochip_add_data(struct gpio_chip *chip, void *data)
+int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
+                              struct lock_class_key *key)
 {
        unsigned long   flags;
        int             status = 0;
@@ -1266,6 +1246,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
        if (status)
                goto err_remove_from_list;
 
+       status = gpiochip_add_irqchip(chip, key);
+       if (status)
+               goto err_remove_chip;
+
        status = of_gpiochip_add(chip);
        if (status)
                goto err_remove_chip;
@@ -1309,7 +1293,7 @@ err_free_gdev:
        kfree(gdev);
        return status;
 }
-EXPORT_SYMBOL_GPL(gpiochip_add_data);
+EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
 
 /**
  * gpiochip_get_data() - get per-subdriver data for the chip
@@ -1504,33 +1488,33 @@ static struct gpio_chip *find_chip_by_name(const char *name)
 
 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
 {
-       if (!gpiochip->irq_need_valid_mask)
+       if (!gpiochip->irq.need_valid_mask)
                return 0;
 
-       gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
+       gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
                                           sizeof(long), GFP_KERNEL);
-       if (!gpiochip->irq_valid_mask)
+       if (!gpiochip->irq.valid_mask)
                return -ENOMEM;
 
        /* Assume by default all GPIOs are valid */
-       bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio);
+       bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
 
        return 0;
 }
 
 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
 {
-       kfree(gpiochip->irq_valid_mask);
-       gpiochip->irq_valid_mask = NULL;
+       kfree(gpiochip->irq.valid_mask);
+       gpiochip->irq.valid_mask = NULL;
 }
 
 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
                                       unsigned int offset)
 {
        /* No mask means all valid */
-       if (likely(!gpiochip->irq_valid_mask))
+       if (likely(!gpiochip->irq.valid_mask))
                return true;
-       return test_bit(offset, gpiochip->irq_valid_mask);
+       return test_bit(offset, gpiochip->irq.valid_mask);
 }
 
 /**
@@ -1598,6 +1582,11 @@ void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
                                  unsigned int parent_irq,
                                  irq_flow_handler_t parent_handler)
 {
+       if (gpiochip->irq.threaded) {
+               chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
+               return;
+       }
+
        gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
                                      parent_handler);
 }
@@ -1614,10 +1603,6 @@ void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
                                 struct irq_chip *irqchip,
                                 unsigned int parent_irq)
 {
-       if (!gpiochip->irq.nested) {
-               chip_err(gpiochip, "tried to nest a chained gpiochip\n");
-               return;
-       }
        gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
                                      NULL);
 }
@@ -1633,10 +1618,11 @@ EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
  * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  * stored inside the gpiochip.
  */
-static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
-                           irq_hw_number_t hwirq)
+int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
+                    irq_hw_number_t hwirq)
 {
        struct gpio_chip *chip = d->host_data;
+       int err = 0;
 
        if (!gpiochip_irqchip_irq_valid(chip, hwirq))
                return -ENXIO;
@@ -1646,13 +1632,21 @@ static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
         * This lock class tells lockdep that GPIO irqs are in a different
         * category than their parents, so it won't report false recursion.
         */
-       irq_set_lockdep_class(irq, chip->lock_key);
+       irq_set_lockdep_class(irq, chip->irq.lock_key);
        irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
        /* Chips that use nested thread handlers have them marked */
-       if (chip->irq.nested)
+       if (chip->irq.threaded)
                irq_set_nested_thread(irq, 1);
        irq_set_noprobe(irq);
 
+       if (chip->irq.num_parents == 1)
+               err = irq_set_parent(irq, chip->irq.parents[0]);
+       else if (chip->irq.map)
+               err = irq_set_parent(irq, chip->irq.map[hwirq]);
+
+       if (err < 0)
+               return err;
+
        /*
         * No set-up of the hardware will happen if IRQ_TYPE_NONE
         * is passed as default type.
@@ -1662,16 +1656,18 @@ static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(gpiochip_irq_map);
 
-static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
+void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
 {
        struct gpio_chip *chip = d->host_data;
 
-       if (chip->irq.nested)
+       if (chip->irq.threaded)
                irq_set_nested_thread(irq, 0);
        irq_set_chip_and_handler(irq, NULL, NULL);
        irq_set_chip_data(irq, NULL);
 }
+EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
 
 static const struct irq_domain_ops gpiochip_domain_ops = {
        .map    = gpiochip_irq_map,
@@ -1709,9 +1705,96 @@ static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
 {
        if (!gpiochip_irqchip_irq_valid(chip, offset))
                return -ENXIO;
+
        return irq_create_mapping(chip->irq.domain, offset);
 }
 
+/**
+ * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
+ * @gpiochip: the GPIO chip to add the IRQ chip to
+ * @lock_key: lockdep class
+ */
+static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
+                               struct lock_class_key *lock_key)
+{
+       struct irq_chip *irqchip = gpiochip->irq.chip;
+       const struct irq_domain_ops *ops;
+       struct device_node *np;
+       unsigned int type;
+       unsigned int i;
+
+       if (!irqchip)
+               return 0;
+
+       if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
+               chip_err(gpiochip, "you cannot have chained interrupts on a "
+                        "chip that may sleep\n");
+               return -EINVAL;
+       }
+
+       np = gpiochip->gpiodev->dev.of_node;
+       type = gpiochip->irq.default_type;
+
+       /*
+        * Specifying a default trigger is a terrible idea if DT or ACPI is
+        * used to configure the interrupts, as you may end up with
+        * conflicting triggers. Tell the user, and reset to NONE.
+        */
+       if (WARN(np && type != IRQ_TYPE_NONE,
+                "%s: Ignoring %u default trigger\n", np->full_name, type))
+               type = IRQ_TYPE_NONE;
+
+       if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
+               acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
+                                "Ignoring %u default trigger\n", type);
+               type = IRQ_TYPE_NONE;
+       }
+
+       gpiochip->to_irq = gpiochip_to_irq;
+       gpiochip->irq.default_type = type;
+       gpiochip->irq.lock_key = lock_key;
+
+       if (gpiochip->irq.domain_ops)
+               ops = gpiochip->irq.domain_ops;
+       else
+               ops = &gpiochip_domain_ops;
+
+       gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
+                                                    gpiochip->irq.first,
+                                                    ops, gpiochip);
+       if (!gpiochip->irq.domain)
+               return -EINVAL;
+
+       /*
+        * It is possible for a driver to override this, but only if the
+        * alternative functions are both implemented.
+        */
+       if (!irqchip->irq_request_resources &&
+           !irqchip->irq_release_resources) {
+               irqchip->irq_request_resources = gpiochip_irq_reqres;
+               irqchip->irq_release_resources = gpiochip_irq_relres;
+       }
+
+       if (gpiochip->irq.parent_handler) {
+               void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
+
+               for (i = 0; i < gpiochip->irq.num_parents; i++) {
+                       /*
+                        * The parent IRQ chip is already using the chip_data
+                        * for this IRQ chip, so our callbacks simply use the
+                        * handler_data.
+                        */
+                       irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
+                                                        gpiochip->irq.parent_handler,
+                                                        data);
+               }
+       }
+
+       acpi_gpiochip_request_interrupts(gpiochip);
+
+       return 0;
+}
+
 /**
  * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  * @gpiochip: the gpiochip to remove the irqchip from
@@ -1724,7 +1807,7 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
 
        acpi_gpiochip_free_interrupts(gpiochip);
 
-       if (gpiochip->irq.num_parents > 0) {
+       if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
                struct gpio_irq_chip *irq = &gpiochip->irq;
                unsigned int i;
 
@@ -1766,8 +1849,7 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  * @handler: the irq handler to use (often a predefined irq core function)
  * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  * to have the core avoid setting up any default type in the hardware.
- * @nested: whether this is a nested irqchip calling handle_nested_irq()
- * in its IRQ handler
+ * @threaded: whether this irqchip uses a nested thread handler
  * @lock_key: lockdep class
  *
  * This function closely associates a certain irqchip with a certain
@@ -1789,7 +1871,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
                             unsigned int first_irq,
                             irq_flow_handler_t handler,
                             unsigned int type,
-                            bool nested,
+                            bool threaded,
                             struct lock_class_key *lock_key)
 {
        struct device_node *of_node;
@@ -1801,7 +1883,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
                pr_err("missing gpiochip .dev parent pointer\n");
                return -EINVAL;
        }
-       gpiochip->irq.nested = nested;
+       gpiochip->irq.threaded = threaded;
        of_node = gpiochip->parent->of_node;
 #ifdef CONFIG_OF_GPIO
        /*
@@ -1830,7 +1912,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
        gpiochip->irq.handler = handler;
        gpiochip->irq.default_type = type;
        gpiochip->to_irq = gpiochip_to_irq;
-       gpiochip->lock_key = lock_key;
+       gpiochip->irq.lock_key = lock_key;
        gpiochip->irq.domain = irq_domain_add_simple(of_node,
                                        gpiochip->ngpio, first_irq,
                                        &gpiochip_domain_ops, gpiochip);
@@ -1857,6 +1939,12 @@ EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
 
 #else /* CONFIG_GPIOLIB_IRQCHIP */
 
+static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
+                                      struct lock_class_key *key)
+{
+       return 0;
+}
+
 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
 {
@@ -1874,7 +1962,7 @@ static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  */
 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
 {
-       return pinctrl_request_gpio(chip->gpiodev->base + offset);
+       return pinctrl_gpio_request(chip->gpiodev->base + offset);
 }
 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
 
@@ -1885,7 +1973,7 @@ EXPORT_SYMBOL_GPL(gpiochip_generic_request);
  */
 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
 {
-       pinctrl_free_gpio(chip->gpiodev->base + offset);
+       pinctrl_gpio_free(chip->gpiodev->base + offset);
 }
 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
 
@@ -3440,8 +3528,21 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
 
        if (lflags & GPIO_ACTIVE_LOW)
                set_bit(FLAG_ACTIVE_LOW, &desc->flags);
+
        if (lflags & GPIO_OPEN_DRAIN)
                set_bit(FLAG_OPEN_DRAIN, &desc->flags);
+       else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
+               /*
+                * This enforces open drain mode from the consumer side.
+                * This is necessary for some busses like I2C, but the lookup
+                * should *REALLY* have specified them as open drain in the
+                * first place, so print a little warning here.
+                */
+               set_bit(FLAG_OPEN_DRAIN, &desc->flags);
+               gpiod_warn(desc,
+                          "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
+       }
+
        if (lflags & GPIO_OPEN_SOURCE)
                set_bit(FLAG_OPEN_SOURCE, &desc->flags);
        if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)