perf probe: Fix memory leak when synthesizing SDT probes
[linux-2.6-microblaze.git] / drivers / gpio / gpiolib-acpi.c
index 834a12f..e37a57d 100644 (file)
@@ -205,6 +205,68 @@ static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
                acpi_gpiochip_request_irq(acpi_gpio, event);
 }
 
+static enum gpiod_flags
+acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio, int polarity)
+{
+       /* GpioInt() implies input configuration */
+       if (agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
+               return GPIOD_IN;
+
+       switch (agpio->io_restriction) {
+       case ACPI_IO_RESTRICT_INPUT:
+               return GPIOD_IN;
+       case ACPI_IO_RESTRICT_OUTPUT:
+               /*
+                * ACPI GPIO resources don't contain an initial value for the
+                * GPIO. Therefore we deduce that value from the pull field
+                * and the polarity instead. If the pin is pulled up we assume
+                * default to be high, if it is pulled down we assume default
+                * to be low, otherwise we leave pin untouched. For active low
+                * polarity values will be switched. See also
+                * Documentation/firmware-guide/acpi/gpio-properties.rst.
+                */
+               switch (agpio->pin_config) {
+               case ACPI_PIN_CONFIG_PULLUP:
+                       return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH;
+               case ACPI_PIN_CONFIG_PULLDOWN:
+                       return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
+               default:
+                       break;
+               }
+               break;
+       default:
+               break;
+       }
+
+       /*
+        * Assume that the BIOS has configured the direction and pull
+        * accordingly.
+        */
+       return GPIOD_ASIS;
+}
+
+static struct gpio_desc *acpi_request_own_gpiod(struct gpio_chip *chip,
+                                               struct acpi_resource_gpio *agpio,
+                                               unsigned int index,
+                                               const char *label)
+{
+       int polarity = GPIO_ACTIVE_HIGH;
+       enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio, polarity);
+       unsigned int pin = agpio->pin_table[index];
+       struct gpio_desc *desc;
+       int ret;
+
+       desc = gpiochip_request_own_desc(chip, pin, label, polarity, flags);
+       if (IS_ERR(desc))
+               return desc;
+
+       ret = gpio_set_debounce_timeout(desc, agpio->debounce_timeout);
+       if (ret)
+               gpiochip_free_own_desc(desc);
+
+       return ret ? ERR_PTR(ret) : desc;
+}
+
 static bool acpi_gpio_in_ignore_list(const char *controller_in, int pin_in)
 {
        const char *controller, *pin_str;
@@ -290,8 +352,7 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
        if (!handler)
                return AE_OK;
 
-       desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event",
-                                        GPIO_ACTIVE_HIGH, GPIOD_IN);
+       desc = acpi_request_own_gpiod(chip, agpio, 0, "ACPI:Event");
        if (IS_ERR(desc)) {
                dev_err(chip->parent,
                        "Failed to request GPIO for pin 0x%04X, err %ld\n",
@@ -526,39 +587,6 @@ static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
        return false;
 }
 
-static enum gpiod_flags
-acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
-{
-       switch (agpio->io_restriction) {
-       case ACPI_IO_RESTRICT_INPUT:
-               return GPIOD_IN;
-       case ACPI_IO_RESTRICT_OUTPUT:
-               /*
-                * ACPI GPIO resources don't contain an initial value for the
-                * GPIO. Therefore we deduce that value from the pull field
-                * instead. If the pin is pulled up we assume default to be
-                * high, if it is pulled down we assume default to be low,
-                * otherwise we leave pin untouched.
-                */
-               switch (agpio->pin_config) {
-               case ACPI_PIN_CONFIG_PULLUP:
-                       return GPIOD_OUT_HIGH;
-               case ACPI_PIN_CONFIG_PULLDOWN:
-                       return GPIOD_OUT_LOW;
-               default:
-                       break;
-               }
-       default:
-               break;
-       }
-
-       /*
-        * Assume that the BIOS has configured the direction and pull
-        * accordingly.
-        */
-       return GPIOD_ASIS;
-}
-
 static int
 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
 {
@@ -633,7 +661,7 @@ int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
 struct acpi_gpio_lookup {
        struct acpi_gpio_info info;
        int index;
-       int pin_index;
+       u16 pin_index;
        bool active_low;
        struct gpio_desc *desc;
        int n;
@@ -649,7 +677,7 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
        if (!lookup->desc) {
                const struct acpi_resource_gpio *agpio = &ares->data.gpio;
                bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
-               int pin_index;
+               u16 pin_index;
 
                if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
                        lookup->index++;
@@ -664,6 +692,7 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
                lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
                                              agpio->pin_table[pin_index]);
                lookup->info.pin_config = agpio->pin_config;
+               lookup->info.debounce = agpio->debounce_timeout;
                lookup->info.gpioint = gpioint;
 
                /*
@@ -674,13 +703,13 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
                 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
                 */
                if (lookup->info.gpioint) {
-                       lookup->info.flags = GPIOD_IN;
                        lookup->info.polarity = agpio->polarity;
                        lookup->info.triggering = agpio->triggering;
                } else {
-                       lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
                        lookup->info.polarity = lookup->active_low;
                }
+
+               lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio, lookup->info.polarity);
        }
 
        return 1;
@@ -794,7 +823,7 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
                if (ret)
                        return ERR_PTR(ret);
 
-               dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
+               dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %u %u\n",
                        dev_name(&lookup.info.adev->dev), lookup.index,
                        lookup.pin_index, lookup.active_low);
        } else {
@@ -942,6 +971,7 @@ int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
 
                if (info.gpioint && idx++ == index) {
                        unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
+                       enum gpiod_flags dflags = GPIOD_ASIS;
                        char label[32];
                        int irq;
 
@@ -952,11 +982,18 @@ int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
                        if (irq < 0)
                                return irq;
 
+                       acpi_gpio_update_gpiod_flags(&dflags, &info);
+                       acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
+
                        snprintf(label, sizeof(label), "GpioInt() %d", index);
-                       ret = gpiod_configure_flags(desc, label, lflags, info.flags);
+                       ret = gpiod_configure_flags(desc, label, lflags, dflags);
                        if (ret < 0)
                                return ret;
 
+                       ret = gpio_set_debounce_timeout(desc, info.debounce);
+                       if (ret)
+                               return ret;
+
                        irq_flags = acpi_dev_get_irq_type(info.triggering,
                                                          info.polarity);
 
@@ -982,7 +1019,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
        struct gpio_chip *chip = achip->chip;
        struct acpi_resource_gpio *agpio;
        struct acpi_resource *ares;
-       int pin_index = (int)address;
+       u16 pin_index = address;
        acpi_status status;
        int length;
        int i;
@@ -1005,7 +1042,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
                return AE_BAD_PARAMETER;
        }
 
-       length = min(agpio->pin_table_length, (u16)(pin_index + bits));
+       length = min_t(u16, agpio->pin_table_length, pin_index + bits);
        for (i = pin_index; i < length; ++i) {
                int pin = agpio->pin_table[i];
                struct acpi_gpio_connection *conn;
@@ -1042,23 +1079,18 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
                }
 
                if (!found) {
-                       enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
-                       const char *label = "ACPI:OpRegion";
-
-                       desc = gpiochip_request_own_desc(chip, pin, label,
-                                                        GPIO_ACTIVE_HIGH,
-                                                        flags);
+                       desc = acpi_request_own_gpiod(chip, agpio, i, "ACPI:OpRegion");
                        if (IS_ERR(desc)) {
-                               status = AE_ERROR;
                                mutex_unlock(&achip->conn_lock);
+                               status = AE_ERROR;
                                goto out;
                        }
 
                        conn = kzalloc(sizeof(*conn), GFP_KERNEL);
                        if (!conn) {
-                               status = AE_NO_MEMORY;
                                gpiochip_free_own_desc(desc);
                                mutex_unlock(&achip->conn_lock);
+                               status = AE_NO_MEMORY;
                                goto out;
                        }
 
@@ -1070,8 +1102,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
                mutex_unlock(&achip->conn_lock);
 
                if (function == ACPI_WRITE)
-                       gpiod_set_raw_value_cansleep(desc,
-                                                    !!((1 << i) & *value));
+                       gpiod_set_raw_value_cansleep(desc, !!(*value & BIT(i)));
                else
                        *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
        }
@@ -1132,7 +1163,7 @@ acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
        int ret;
 
        *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
-       *dflags = 0;
+       *dflags = GPIOD_ASIS;
        *name = NULL;
 
        ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,