Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ACPI helpers for GPIO API |
| 3 | * |
| 4 | * Copyright (C) 2012, Intel Corporation |
| 5 | * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> |
| 6 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/errno.h> |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 14 | #include <linux/gpio/consumer.h> |
Mika Westerberg | 5ccff85 | 2014-01-08 12:40:56 +0200 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 16 | #include <linux/export.h> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 17 | #include <linux/acpi.h> |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 19 | |
Mika Westerberg | 5ccff85 | 2014-01-08 12:40:56 +0200 | [diff] [blame] | 20 | #include "gpiolib.h" |
| 21 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 22 | struct acpi_gpio_event { |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 23 | struct list_head node; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 24 | acpi_handle handle; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 25 | unsigned int pin; |
| 26 | unsigned int irq; |
| 27 | }; |
| 28 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 29 | struct acpi_gpio_chip { |
| 30 | struct gpio_chip *chip; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 31 | struct list_head events; |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 34 | static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) |
| 35 | { |
| 36 | if (!gc->dev) |
| 37 | return false; |
| 38 | |
| 39 | return ACPI_HANDLE(gc->dev) == data; |
| 40 | } |
| 41 | |
| 42 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 43 | * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 44 | * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") |
| 45 | * @pin: ACPI GPIO pin number (0-based, controller-relative) |
| 46 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 47 | * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR |
| 48 | * error value |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 49 | */ |
| 50 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 51 | static struct gpio_desc *acpi_get_gpiod(char *path, int pin) |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 52 | { |
| 53 | struct gpio_chip *chip; |
| 54 | acpi_handle handle; |
| 55 | acpi_status status; |
| 56 | |
| 57 | status = acpi_get_handle(NULL, path, &handle); |
| 58 | if (ACPI_FAILURE(status)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 59 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 60 | |
| 61 | chip = gpiochip_find(handle, acpi_gpiochip_find); |
| 62 | if (!chip) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 63 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 64 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 65 | if (pin < 0 || pin > chip->ngpio) |
| 66 | return ERR_PTR(-EINVAL); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 67 | |
Alexandre Courbot | 390d82e | 2014-02-09 17:43:55 +0900 | [diff] [blame] | 68 | return gpiochip_get_desc(chip, pin); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 69 | } |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 70 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 71 | static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) |
| 72 | { |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 73 | struct acpi_gpio_event *event = data; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 74 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 75 | acpi_evaluate_object(event->handle, NULL, NULL, NULL); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 76 | |
| 77 | return IRQ_HANDLED; |
| 78 | } |
| 79 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 80 | static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) |
| 81 | { |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 82 | struct acpi_gpio_event *event = data; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 83 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 84 | acpi_execute_simple_method(event->handle, NULL, event->pin); |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 85 | |
| 86 | return IRQ_HANDLED; |
| 87 | } |
| 88 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 89 | static void acpi_gpio_chip_dh(acpi_handle handle, void *data) |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 90 | { |
| 91 | /* The address of this function is used as a key. */ |
| 92 | } |
| 93 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 94 | static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares, |
| 95 | void *context) |
| 96 | { |
| 97 | struct acpi_gpio_chip *acpi_gpio = context; |
| 98 | struct gpio_chip *chip = acpi_gpio->chip; |
| 99 | struct acpi_resource_gpio *agpio; |
| 100 | acpi_handle handle, evt_handle; |
| 101 | struct acpi_gpio_event *event; |
| 102 | irq_handler_t handler = NULL; |
| 103 | struct gpio_desc *desc; |
| 104 | unsigned long irqflags; |
| 105 | int ret, pin, irq; |
| 106 | |
| 107 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 108 | return AE_OK; |
| 109 | |
| 110 | agpio = &ares->data.gpio; |
| 111 | if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT) |
| 112 | return AE_OK; |
| 113 | |
| 114 | handle = ACPI_HANDLE(chip->dev); |
| 115 | pin = agpio->pin_table[0]; |
| 116 | |
| 117 | if (pin <= 255) { |
| 118 | char ev_name[5]; |
| 119 | sprintf(ev_name, "_%c%02X", |
| 120 | agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', |
| 121 | pin); |
| 122 | if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) |
| 123 | handler = acpi_gpio_irq_handler; |
| 124 | } |
| 125 | if (!handler) { |
| 126 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle))) |
| 127 | handler = acpi_gpio_irq_handler_evt; |
| 128 | } |
| 129 | if (!handler) |
| 130 | return AE_BAD_PARAMETER; |
| 131 | |
| 132 | desc = gpiochip_get_desc(chip, pin); |
| 133 | if (IS_ERR(desc)) { |
| 134 | dev_err(chip->dev, "Failed to get GPIO descriptor\n"); |
| 135 | return AE_ERROR; |
| 136 | } |
| 137 | |
| 138 | ret = gpiochip_request_own_desc(desc, "ACPI:Event"); |
| 139 | if (ret) { |
| 140 | dev_err(chip->dev, "Failed to request GPIO\n"); |
| 141 | return AE_ERROR; |
| 142 | } |
| 143 | |
| 144 | gpiod_direction_input(desc); |
| 145 | |
| 146 | ret = gpiod_lock_as_irq(desc); |
| 147 | if (ret) { |
| 148 | dev_err(chip->dev, "Failed to lock GPIO as interrupt\n"); |
| 149 | goto fail_free_desc; |
| 150 | } |
| 151 | |
| 152 | irq = gpiod_to_irq(desc); |
| 153 | if (irq < 0) { |
| 154 | dev_err(chip->dev, "Failed to translate GPIO to IRQ\n"); |
| 155 | goto fail_unlock_irq; |
| 156 | } |
| 157 | |
| 158 | irqflags = IRQF_ONESHOT; |
| 159 | if (agpio->triggering == ACPI_LEVEL_SENSITIVE) { |
| 160 | if (agpio->polarity == ACPI_ACTIVE_HIGH) |
| 161 | irqflags |= IRQF_TRIGGER_HIGH; |
| 162 | else |
| 163 | irqflags |= IRQF_TRIGGER_LOW; |
| 164 | } else { |
| 165 | switch (agpio->polarity) { |
| 166 | case ACPI_ACTIVE_HIGH: |
| 167 | irqflags |= IRQF_TRIGGER_RISING; |
| 168 | break; |
| 169 | case ACPI_ACTIVE_LOW: |
| 170 | irqflags |= IRQF_TRIGGER_FALLING; |
| 171 | break; |
| 172 | default: |
| 173 | irqflags |= IRQF_TRIGGER_RISING | |
| 174 | IRQF_TRIGGER_FALLING; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 180 | if (!event) |
| 181 | goto fail_unlock_irq; |
| 182 | |
| 183 | event->handle = evt_handle; |
| 184 | event->irq = irq; |
| 185 | event->pin = pin; |
| 186 | |
| 187 | ret = request_threaded_irq(event->irq, NULL, handler, irqflags, |
| 188 | "ACPI:Event", event); |
| 189 | if (ret) { |
| 190 | dev_err(chip->dev, "Failed to setup interrupt handler for %d\n", |
| 191 | event->irq); |
| 192 | goto fail_free_event; |
| 193 | } |
| 194 | |
| 195 | list_add_tail(&event->node, &acpi_gpio->events); |
| 196 | return AE_OK; |
| 197 | |
| 198 | fail_free_event: |
| 199 | kfree(event); |
| 200 | fail_unlock_irq: |
| 201 | gpiod_unlock_as_irq(desc); |
| 202 | fail_free_desc: |
| 203 | gpiochip_free_own_desc(desc); |
| 204 | |
| 205 | return AE_ERROR; |
| 206 | } |
| 207 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 208 | /** |
| 209 | * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 210 | * @acpi_gpio: ACPI GPIO chip |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 211 | * |
| 212 | * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are |
| 213 | * handled by ACPI event methods which need to be called from the GPIO |
| 214 | * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which |
| 215 | * gpio pins have acpi event methods and assigns interrupt handlers that calls |
| 216 | * the acpi event methods for those pins. |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 217 | */ |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 218 | static void acpi_gpiochip_request_interrupts(struct acpi_gpio_chip *acpi_gpio) |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 219 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 220 | struct gpio_chip *chip = acpi_gpio->chip; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 221 | |
| 222 | if (!chip->dev || !chip->to_irq) |
| 223 | return; |
| 224 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 225 | INIT_LIST_HEAD(&acpi_gpio->events); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 226 | acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI", |
| 227 | acpi_gpiochip_request_interrupt, acpi_gpio); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 228 | } |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 229 | |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 230 | /** |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 231 | * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts. |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 232 | * @acpi_gpio: ACPI GPIO chip |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 233 | * |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 234 | * Free interrupts associated with GPIO ACPI event method for the given |
| 235 | * GPIO chip. |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 236 | */ |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 237 | static void acpi_gpiochip_free_interrupts(struct acpi_gpio_chip *acpi_gpio) |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 238 | { |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 239 | struct acpi_gpio_event *event, *ep; |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 240 | struct gpio_chip *chip = acpi_gpio->chip; |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 241 | |
| 242 | if (!chip->dev || !chip->to_irq) |
| 243 | return; |
| 244 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 245 | list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) { |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame^] | 246 | struct gpio_desc *desc; |
| 247 | |
| 248 | free_irq(event->irq, event); |
| 249 | desc = gpiochip_get_desc(chip, event->pin); |
| 250 | if (WARN_ON(IS_ERR(desc))) |
| 251 | continue; |
| 252 | gpiod_unlock_as_irq(desc); |
| 253 | gpiochip_free_own_desc(desc); |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 254 | list_del(&event->node); |
| 255 | kfree(event); |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 256 | } |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 257 | } |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 258 | |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 259 | struct acpi_gpio_lookup { |
| 260 | struct acpi_gpio_info info; |
| 261 | int index; |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 262 | struct gpio_desc *desc; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 263 | int n; |
| 264 | }; |
| 265 | |
| 266 | static int acpi_find_gpio(struct acpi_resource *ares, void *data) |
| 267 | { |
| 268 | struct acpi_gpio_lookup *lookup = data; |
| 269 | |
| 270 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 271 | return 1; |
| 272 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 273 | if (lookup->n++ == lookup->index && !lookup->desc) { |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 274 | const struct acpi_resource_gpio *agpio = &ares->data.gpio; |
| 275 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 276 | lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, |
| 277 | agpio->pin_table[0]); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 278 | lookup->info.gpioint = |
| 279 | agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 280 | lookup->info.active_low = |
| 281 | agpio->polarity == ACPI_ACTIVE_LOW; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return 1; |
| 285 | } |
| 286 | |
| 287 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 288 | * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 289 | * @dev: pointer to a device to get GPIO from |
| 290 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
| 291 | * @info: info pointer to fill in (optional) |
| 292 | * |
| 293 | * Function goes through ACPI resources for @dev and based on @index looks |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 294 | * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 295 | * and returns it. @index matches GpioIo/GpioInt resources only so if there |
| 296 | * are total %3 GPIO resources, the index goes from %0 to %2. |
| 297 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 298 | * If the GPIO cannot be translated or there is an error an ERR_PTR is |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 299 | * returned. |
| 300 | * |
| 301 | * Note: if the GPIO resource has multiple entries in the pin list, this |
| 302 | * function only returns the first. |
| 303 | */ |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 304 | struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index, |
| 305 | struct acpi_gpio_info *info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 306 | { |
| 307 | struct acpi_gpio_lookup lookup; |
| 308 | struct list_head resource_list; |
| 309 | struct acpi_device *adev; |
| 310 | acpi_handle handle; |
| 311 | int ret; |
| 312 | |
| 313 | if (!dev) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 314 | return ERR_PTR(-EINVAL); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 315 | |
| 316 | handle = ACPI_HANDLE(dev); |
| 317 | if (!handle || acpi_bus_get_device(handle, &adev)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 318 | return ERR_PTR(-ENODEV); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 319 | |
| 320 | memset(&lookup, 0, sizeof(lookup)); |
| 321 | lookup.index = index; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 322 | |
| 323 | INIT_LIST_HEAD(&resource_list); |
| 324 | ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio, |
| 325 | &lookup); |
| 326 | if (ret < 0) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 327 | return ERR_PTR(ret); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 328 | |
| 329 | acpi_dev_free_resource_list(&resource_list); |
| 330 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 331 | if (lookup.desc && info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 332 | *info = lookup.info; |
| 333 | |
Mika Westerberg | a00580c | 2013-12-10 12:00:27 +0200 | [diff] [blame] | 334 | return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 335 | } |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 336 | |
| 337 | void acpi_gpiochip_add(struct gpio_chip *chip) |
| 338 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 339 | struct acpi_gpio_chip *acpi_gpio; |
| 340 | acpi_handle handle; |
| 341 | acpi_status status; |
| 342 | |
| 343 | handle = ACPI_HANDLE(chip->dev); |
| 344 | if (!handle) |
| 345 | return; |
| 346 | |
| 347 | acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL); |
| 348 | if (!acpi_gpio) { |
| 349 | dev_err(chip->dev, |
| 350 | "Failed to allocate memory for ACPI GPIO chip\n"); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | acpi_gpio->chip = chip; |
| 355 | |
| 356 | status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio); |
| 357 | if (ACPI_FAILURE(status)) { |
| 358 | dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n"); |
| 359 | kfree(acpi_gpio); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | acpi_gpiochip_request_interrupts(acpi_gpio); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void acpi_gpiochip_remove(struct gpio_chip *chip) |
| 367 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 368 | struct acpi_gpio_chip *acpi_gpio; |
| 369 | acpi_handle handle; |
| 370 | acpi_status status; |
| 371 | |
| 372 | handle = ACPI_HANDLE(chip->dev); |
| 373 | if (!handle) |
| 374 | return; |
| 375 | |
| 376 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 377 | if (ACPI_FAILURE(status)) { |
| 378 | dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n"); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | acpi_gpiochip_free_interrupts(acpi_gpio); |
| 383 | |
| 384 | acpi_detach_data(handle, acpi_gpio_chip_dh); |
| 385 | kfree(acpi_gpio); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 386 | } |