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 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 22 | struct acpi_gpio_evt_pin { |
| 23 | struct list_head node; |
| 24 | acpi_handle *evt_handle; |
| 25 | unsigned int pin; |
| 26 | unsigned int irq; |
| 27 | }; |
| 28 | |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 29 | static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) |
| 30 | { |
| 31 | if (!gc->dev) |
| 32 | return false; |
| 33 | |
| 34 | return ACPI_HANDLE(gc->dev) == data; |
| 35 | } |
| 36 | |
| 37 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 38 | * 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] | 39 | * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") |
| 40 | * @pin: ACPI GPIO pin number (0-based, controller-relative) |
| 41 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 42 | * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR |
| 43 | * error value |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 44 | */ |
| 45 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 46 | static struct gpio_desc *acpi_get_gpiod(char *path, int pin) |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 47 | { |
| 48 | struct gpio_chip *chip; |
| 49 | acpi_handle handle; |
| 50 | acpi_status status; |
| 51 | |
| 52 | status = acpi_get_handle(NULL, path, &handle); |
| 53 | if (ACPI_FAILURE(status)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 54 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 55 | |
| 56 | chip = gpiochip_find(handle, acpi_gpiochip_find); |
| 57 | if (!chip) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 58 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 59 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 60 | if (pin < 0 || pin > chip->ngpio) |
| 61 | return ERR_PTR(-EINVAL); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 62 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 63 | return gpio_to_desc(chip->base + pin); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 64 | } |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 65 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 66 | static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) |
| 67 | { |
| 68 | acpi_handle handle = data; |
| 69 | |
| 70 | acpi_evaluate_object(handle, NULL, NULL, NULL); |
| 71 | |
| 72 | return IRQ_HANDLED; |
| 73 | } |
| 74 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 75 | static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) |
| 76 | { |
| 77 | struct acpi_gpio_evt_pin *evt_pin = data; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 78 | |
Zhang Rui | 524c108 | 2013-09-03 08:31:50 +0800 | [diff] [blame] | 79 | acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin); |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 80 | |
| 81 | return IRQ_HANDLED; |
| 82 | } |
| 83 | |
| 84 | static void acpi_gpio_evt_dh(acpi_handle handle, void *data) |
| 85 | { |
| 86 | /* The address of this function is used as a key. */ |
| 87 | } |
| 88 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 89 | /** |
| 90 | * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events |
| 91 | * @chip: gpio chip |
| 92 | * |
| 93 | * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are |
| 94 | * handled by ACPI event methods which need to be called from the GPIO |
| 95 | * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which |
| 96 | * gpio pins have acpi event methods and assigns interrupt handlers that calls |
| 97 | * the acpi event methods for those pins. |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 98 | */ |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 99 | static void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 100 | { |
| 101 | struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 102 | struct acpi_resource *res; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 103 | acpi_handle handle, evt_handle; |
| 104 | struct list_head *evt_pins = NULL; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 105 | acpi_status status; |
Mathias Nyman | 1107ca1 | 2013-02-04 11:32:22 +0200 | [diff] [blame] | 106 | unsigned int pin; |
| 107 | int irq, ret; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 108 | char ev_name[5]; |
| 109 | |
| 110 | if (!chip->dev || !chip->to_irq) |
| 111 | return; |
| 112 | |
| 113 | handle = ACPI_HANDLE(chip->dev); |
| 114 | if (!handle) |
| 115 | return; |
| 116 | |
| 117 | status = acpi_get_event_resources(handle, &buf); |
| 118 | if (ACPI_FAILURE(status)) |
| 119 | return; |
| 120 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 121 | status = acpi_get_handle(handle, "_EVT", &evt_handle); |
| 122 | if (ACPI_SUCCESS(status)) { |
| 123 | evt_pins = kzalloc(sizeof(*evt_pins), GFP_KERNEL); |
| 124 | if (evt_pins) { |
| 125 | INIT_LIST_HEAD(evt_pins); |
| 126 | status = acpi_attach_data(handle, acpi_gpio_evt_dh, |
| 127 | evt_pins); |
| 128 | if (ACPI_FAILURE(status)) { |
| 129 | kfree(evt_pins); |
| 130 | evt_pins = NULL; |
| 131 | } |
| 132 | } |
| 133 | } |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 134 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 135 | /* |
| 136 | * If a GPIO interrupt has an ACPI event handler method, or _EVT is |
| 137 | * present, set up an interrupt handler that calls the ACPI event |
| 138 | * handler. |
| 139 | */ |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 140 | for (res = buf.pointer; |
| 141 | res && (res->type != ACPI_RESOURCE_TYPE_END_TAG); |
| 142 | res = ACPI_NEXT_RESOURCE(res)) { |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 143 | irq_handler_t handler = NULL; |
| 144 | void *data; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 145 | |
| 146 | if (res->type != ACPI_RESOURCE_TYPE_GPIO || |
| 147 | res->data.gpio.connection_type != |
| 148 | ACPI_RESOURCE_GPIO_TYPE_INT) |
| 149 | continue; |
| 150 | |
| 151 | pin = res->data.gpio.pin_table[0]; |
| 152 | if (pin > chip->ngpio) |
| 153 | continue; |
| 154 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 155 | irq = chip->to_irq(chip, pin); |
| 156 | if (irq < 0) |
| 157 | continue; |
| 158 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 159 | if (pin <= 255) { |
| 160 | acpi_handle ev_handle; |
| 161 | |
| 162 | sprintf(ev_name, "_%c%02X", |
| 163 | res->data.gpio.triggering ? 'E' : 'L', pin); |
| 164 | status = acpi_get_handle(handle, ev_name, &ev_handle); |
| 165 | if (ACPI_SUCCESS(status)) { |
| 166 | handler = acpi_gpio_irq_handler; |
| 167 | data = ev_handle; |
| 168 | } |
| 169 | } |
| 170 | if (!handler && evt_pins) { |
| 171 | struct acpi_gpio_evt_pin *evt_pin; |
| 172 | |
| 173 | evt_pin = kzalloc(sizeof(*evt_pin), GFP_KERNEL); |
| 174 | if (!evt_pin) |
| 175 | continue; |
| 176 | |
| 177 | list_add_tail(&evt_pin->node, evt_pins); |
| 178 | evt_pin->evt_handle = evt_handle; |
| 179 | evt_pin->pin = pin; |
| 180 | evt_pin->irq = irq; |
| 181 | handler = acpi_gpio_irq_handler_evt; |
| 182 | data = evt_pin; |
| 183 | } |
| 184 | if (!handler) |
| 185 | continue; |
| 186 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 187 | /* Assume BIOS sets the triggering, so no flags */ |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 188 | ret = devm_request_threaded_irq(chip->dev, irq, NULL, handler, |
| 189 | 0, "GPIO-signaled-ACPI-event", |
| 190 | data); |
Mathias Nyman | 1107ca1 | 2013-02-04 11:32:22 +0200 | [diff] [blame] | 191 | if (ret) |
| 192 | dev_err(chip->dev, |
| 193 | "Failed to request IRQ %d ACPI event handler\n", |
| 194 | irq); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 195 | } |
| 196 | } |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 197 | |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 198 | /** |
| 199 | * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts. |
| 200 | * @chip: gpio chip |
| 201 | * |
| 202 | * Free interrupts associated with the _EVT method for the given GPIO chip. |
| 203 | * |
| 204 | * The remaining ACPI event interrupts associated with the chip are freed |
| 205 | * automatically. |
| 206 | */ |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 207 | static void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 208 | { |
| 209 | acpi_handle handle; |
| 210 | acpi_status status; |
| 211 | struct list_head *evt_pins; |
| 212 | struct acpi_gpio_evt_pin *evt_pin, *ep; |
| 213 | |
| 214 | if (!chip->dev || !chip->to_irq) |
| 215 | return; |
| 216 | |
| 217 | handle = ACPI_HANDLE(chip->dev); |
| 218 | if (!handle) |
| 219 | return; |
| 220 | |
| 221 | status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins); |
| 222 | if (ACPI_FAILURE(status)) |
| 223 | return; |
| 224 | |
| 225 | list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) { |
| 226 | devm_free_irq(chip->dev, evt_pin->irq, evt_pin); |
| 227 | list_del(&evt_pin->node); |
| 228 | kfree(evt_pin); |
| 229 | } |
| 230 | |
| 231 | acpi_detach_data(handle, acpi_gpio_evt_dh); |
| 232 | kfree(evt_pins); |
| 233 | } |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 234 | |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 235 | struct acpi_gpio_lookup { |
| 236 | struct acpi_gpio_info info; |
| 237 | int index; |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 238 | struct gpio_desc *desc; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 239 | int n; |
| 240 | }; |
| 241 | |
| 242 | static int acpi_find_gpio(struct acpi_resource *ares, void *data) |
| 243 | { |
| 244 | struct acpi_gpio_lookup *lookup = data; |
| 245 | |
| 246 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 247 | return 1; |
| 248 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 249 | if (lookup->n++ == lookup->index && !lookup->desc) { |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 250 | const struct acpi_resource_gpio *agpio = &ares->data.gpio; |
| 251 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 252 | lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, |
| 253 | agpio->pin_table[0]); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 254 | lookup->info.gpioint = |
| 255 | agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 256 | lookup->info.active_low = |
| 257 | agpio->polarity == ACPI_ACTIVE_LOW; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 264 | * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 265 | * @dev: pointer to a device to get GPIO from |
| 266 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
| 267 | * @info: info pointer to fill in (optional) |
| 268 | * |
| 269 | * Function goes through ACPI resources for @dev and based on @index looks |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 270 | * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 271 | * and returns it. @index matches GpioIo/GpioInt resources only so if there |
| 272 | * are total %3 GPIO resources, the index goes from %0 to %2. |
| 273 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 274 | * 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] | 275 | * returned. |
| 276 | * |
| 277 | * Note: if the GPIO resource has multiple entries in the pin list, this |
| 278 | * function only returns the first. |
| 279 | */ |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 280 | struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index, |
| 281 | struct acpi_gpio_info *info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 282 | { |
| 283 | struct acpi_gpio_lookup lookup; |
| 284 | struct list_head resource_list; |
| 285 | struct acpi_device *adev; |
| 286 | acpi_handle handle; |
| 287 | int ret; |
| 288 | |
| 289 | if (!dev) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 290 | return ERR_PTR(-EINVAL); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 291 | |
| 292 | handle = ACPI_HANDLE(dev); |
| 293 | if (!handle || acpi_bus_get_device(handle, &adev)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 294 | return ERR_PTR(-ENODEV); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 295 | |
| 296 | memset(&lookup, 0, sizeof(lookup)); |
| 297 | lookup.index = index; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 298 | |
| 299 | INIT_LIST_HEAD(&resource_list); |
| 300 | ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio, |
| 301 | &lookup); |
| 302 | if (ret < 0) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 303 | return ERR_PTR(ret); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 304 | |
| 305 | acpi_dev_free_resource_list(&resource_list); |
| 306 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 307 | if (lookup.desc && info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 308 | *info = lookup.info; |
| 309 | |
Mika Westerberg | a00580c | 2013-12-10 12:00:27 +0200 | [diff] [blame] | 310 | return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 311 | } |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 312 | |
| 313 | void acpi_gpiochip_add(struct gpio_chip *chip) |
| 314 | { |
| 315 | acpi_gpiochip_request_interrupts(chip); |
| 316 | } |
| 317 | |
| 318 | void acpi_gpiochip_remove(struct gpio_chip *chip) |
| 319 | { |
| 320 | acpi_gpiochip_free_interrupts(chip); |
| 321 | } |