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> |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 20 | |
Mika Westerberg | 5ccff85 | 2014-01-08 12:40:56 +0200 | [diff] [blame] | 21 | #include "gpiolib.h" |
| 22 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 23 | struct acpi_gpio_event { |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 24 | struct list_head node; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 25 | acpi_handle handle; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 26 | unsigned int pin; |
| 27 | unsigned int irq; |
| 28 | }; |
| 29 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 30 | struct acpi_gpio_connection { |
| 31 | struct list_head node; |
| 32 | struct gpio_desc *desc; |
| 33 | }; |
| 34 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 35 | struct acpi_gpio_chip { |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 36 | /* |
| 37 | * ACPICA requires that the first field of the context parameter |
| 38 | * passed to acpi_install_address_space_handler() is large enough |
| 39 | * to hold struct acpi_connection_info. |
| 40 | */ |
| 41 | struct acpi_connection_info conn_info; |
| 42 | struct list_head conns; |
| 43 | struct mutex conn_lock; |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 44 | struct gpio_chip *chip; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 45 | struct list_head events; |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 48 | static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) |
| 49 | { |
| 50 | if (!gc->dev) |
| 51 | return false; |
| 52 | |
| 53 | return ACPI_HANDLE(gc->dev) == data; |
| 54 | } |
| 55 | |
| 56 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 57 | * 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] | 58 | * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") |
| 59 | * @pin: ACPI GPIO pin number (0-based, controller-relative) |
| 60 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 61 | * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR |
| 62 | * error value |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 63 | */ |
| 64 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 65 | static struct gpio_desc *acpi_get_gpiod(char *path, int pin) |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 66 | { |
| 67 | struct gpio_chip *chip; |
| 68 | acpi_handle handle; |
| 69 | acpi_status status; |
| 70 | |
| 71 | status = acpi_get_handle(NULL, path, &handle); |
| 72 | if (ACPI_FAILURE(status)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 73 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 74 | |
| 75 | chip = gpiochip_find(handle, acpi_gpiochip_find); |
| 76 | if (!chip) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 77 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 78 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 79 | if (pin < 0 || pin > chip->ngpio) |
| 80 | return ERR_PTR(-EINVAL); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 81 | |
Alexandre Courbot | 390d82e | 2014-02-09 17:43:55 +0900 | [diff] [blame] | 82 | return gpiochip_get_desc(chip, pin); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 83 | } |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 84 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 85 | static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) |
| 86 | { |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 87 | struct acpi_gpio_event *event = data; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 88 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 89 | acpi_evaluate_object(event->handle, NULL, NULL, NULL); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 90 | |
| 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 94 | static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) |
| 95 | { |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 96 | struct acpi_gpio_event *event = data; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 97 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 98 | acpi_execute_simple_method(event->handle, NULL, event->pin); |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 99 | |
| 100 | return IRQ_HANDLED; |
| 101 | } |
| 102 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 103 | static void acpi_gpio_chip_dh(acpi_handle handle, void *data) |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 104 | { |
| 105 | /* The address of this function is used as a key. */ |
| 106 | } |
| 107 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 108 | static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares, |
| 109 | void *context) |
| 110 | { |
| 111 | struct acpi_gpio_chip *acpi_gpio = context; |
| 112 | struct gpio_chip *chip = acpi_gpio->chip; |
| 113 | struct acpi_resource_gpio *agpio; |
| 114 | acpi_handle handle, evt_handle; |
| 115 | struct acpi_gpio_event *event; |
| 116 | irq_handler_t handler = NULL; |
| 117 | struct gpio_desc *desc; |
| 118 | unsigned long irqflags; |
| 119 | int ret, pin, irq; |
| 120 | |
| 121 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 122 | return AE_OK; |
| 123 | |
| 124 | agpio = &ares->data.gpio; |
| 125 | if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT) |
| 126 | return AE_OK; |
| 127 | |
| 128 | handle = ACPI_HANDLE(chip->dev); |
| 129 | pin = agpio->pin_table[0]; |
| 130 | |
| 131 | if (pin <= 255) { |
| 132 | char ev_name[5]; |
| 133 | sprintf(ev_name, "_%c%02X", |
| 134 | agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', |
| 135 | pin); |
| 136 | if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) |
| 137 | handler = acpi_gpio_irq_handler; |
| 138 | } |
| 139 | if (!handler) { |
| 140 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle))) |
| 141 | handler = acpi_gpio_irq_handler_evt; |
| 142 | } |
| 143 | if (!handler) |
| 144 | return AE_BAD_PARAMETER; |
| 145 | |
| 146 | desc = gpiochip_get_desc(chip, pin); |
| 147 | if (IS_ERR(desc)) { |
| 148 | dev_err(chip->dev, "Failed to get GPIO descriptor\n"); |
| 149 | return AE_ERROR; |
| 150 | } |
| 151 | |
| 152 | ret = gpiochip_request_own_desc(desc, "ACPI:Event"); |
| 153 | if (ret) { |
| 154 | dev_err(chip->dev, "Failed to request GPIO\n"); |
| 155 | return AE_ERROR; |
| 156 | } |
| 157 | |
| 158 | gpiod_direction_input(desc); |
| 159 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 160 | ret = gpio_lock_as_irq(chip, pin); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 161 | if (ret) { |
| 162 | dev_err(chip->dev, "Failed to lock GPIO as interrupt\n"); |
| 163 | goto fail_free_desc; |
| 164 | } |
| 165 | |
| 166 | irq = gpiod_to_irq(desc); |
| 167 | if (irq < 0) { |
| 168 | dev_err(chip->dev, "Failed to translate GPIO to IRQ\n"); |
| 169 | goto fail_unlock_irq; |
| 170 | } |
| 171 | |
| 172 | irqflags = IRQF_ONESHOT; |
| 173 | if (agpio->triggering == ACPI_LEVEL_SENSITIVE) { |
| 174 | if (agpio->polarity == ACPI_ACTIVE_HIGH) |
| 175 | irqflags |= IRQF_TRIGGER_HIGH; |
| 176 | else |
| 177 | irqflags |= IRQF_TRIGGER_LOW; |
| 178 | } else { |
| 179 | switch (agpio->polarity) { |
| 180 | case ACPI_ACTIVE_HIGH: |
| 181 | irqflags |= IRQF_TRIGGER_RISING; |
| 182 | break; |
| 183 | case ACPI_ACTIVE_LOW: |
| 184 | irqflags |= IRQF_TRIGGER_FALLING; |
| 185 | break; |
| 186 | default: |
| 187 | irqflags |= IRQF_TRIGGER_RISING | |
| 188 | IRQF_TRIGGER_FALLING; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 194 | if (!event) |
| 195 | goto fail_unlock_irq; |
| 196 | |
| 197 | event->handle = evt_handle; |
| 198 | event->irq = irq; |
| 199 | event->pin = pin; |
| 200 | |
| 201 | ret = request_threaded_irq(event->irq, NULL, handler, irqflags, |
| 202 | "ACPI:Event", event); |
| 203 | if (ret) { |
| 204 | dev_err(chip->dev, "Failed to setup interrupt handler for %d\n", |
| 205 | event->irq); |
| 206 | goto fail_free_event; |
| 207 | } |
| 208 | |
| 209 | list_add_tail(&event->node, &acpi_gpio->events); |
| 210 | return AE_OK; |
| 211 | |
| 212 | fail_free_event: |
| 213 | kfree(event); |
| 214 | fail_unlock_irq: |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 215 | gpio_unlock_as_irq(chip, pin); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 216 | fail_free_desc: |
| 217 | gpiochip_free_own_desc(desc); |
| 218 | |
| 219 | return AE_ERROR; |
| 220 | } |
| 221 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 222 | /** |
| 223 | * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 224 | * @chip: GPIO chip |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 225 | * |
| 226 | * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are |
| 227 | * handled by ACPI event methods which need to be called from the GPIO |
| 228 | * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which |
| 229 | * gpio pins have acpi event methods and assigns interrupt handlers that calls |
| 230 | * the acpi event methods for those pins. |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 231 | */ |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 232 | void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 233 | { |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 234 | struct acpi_gpio_chip *acpi_gpio; |
| 235 | acpi_handle handle; |
| 236 | acpi_status status; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 237 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 238 | if (!chip->dev || !chip->to_irq) |
| 239 | return; |
| 240 | |
| 241 | handle = ACPI_HANDLE(chip->dev); |
| 242 | if (!handle) |
| 243 | return; |
| 244 | |
| 245 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 246 | if (ACPI_FAILURE(status)) |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 247 | return; |
| 248 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 249 | INIT_LIST_HEAD(&acpi_gpio->events); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 250 | acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI", |
| 251 | acpi_gpiochip_request_interrupt, acpi_gpio); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 252 | } |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 253 | |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 254 | /** |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 255 | * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts. |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 256 | * @chip: GPIO chip |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 257 | * |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 258 | * Free interrupts associated with GPIO ACPI event method for the given |
| 259 | * GPIO chip. |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 260 | */ |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 261 | void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 262 | { |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 263 | struct acpi_gpio_chip *acpi_gpio; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 264 | struct acpi_gpio_event *event, *ep; |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 265 | acpi_handle handle; |
| 266 | acpi_status status; |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 267 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 268 | if (!chip->dev || !chip->to_irq) |
| 269 | return; |
| 270 | |
| 271 | handle = ACPI_HANDLE(chip->dev); |
| 272 | if (!handle) |
| 273 | return; |
| 274 | |
| 275 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 276 | if (ACPI_FAILURE(status)) |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 277 | return; |
| 278 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 279 | list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) { |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 280 | struct gpio_desc *desc; |
| 281 | |
| 282 | free_irq(event->irq, event); |
| 283 | desc = gpiochip_get_desc(chip, event->pin); |
| 284 | if (WARN_ON(IS_ERR(desc))) |
| 285 | continue; |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 286 | gpio_unlock_as_irq(chip, event->pin); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 287 | gpiochip_free_own_desc(desc); |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 288 | list_del(&event->node); |
| 289 | kfree(event); |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 290 | } |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 291 | } |
Mika Westerberg | 70b5341 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 292 | |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 293 | struct acpi_gpio_lookup { |
| 294 | struct acpi_gpio_info info; |
| 295 | int index; |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 296 | struct gpio_desc *desc; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 297 | int n; |
| 298 | }; |
| 299 | |
| 300 | static int acpi_find_gpio(struct acpi_resource *ares, void *data) |
| 301 | { |
| 302 | struct acpi_gpio_lookup *lookup = data; |
| 303 | |
| 304 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 305 | return 1; |
| 306 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 307 | if (lookup->n++ == lookup->index && !lookup->desc) { |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 308 | const struct acpi_resource_gpio *agpio = &ares->data.gpio; |
| 309 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 310 | lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, |
| 311 | agpio->pin_table[0]); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 312 | lookup->info.gpioint = |
| 313 | agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 314 | lookup->info.active_low = |
| 315 | agpio->polarity == ACPI_ACTIVE_LOW; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 322 | * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 323 | * @dev: pointer to a device to get GPIO from |
| 324 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
| 325 | * @info: info pointer to fill in (optional) |
| 326 | * |
| 327 | * Function goes through ACPI resources for @dev and based on @index looks |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 328 | * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 329 | * and returns it. @index matches GpioIo/GpioInt resources only so if there |
| 330 | * are total %3 GPIO resources, the index goes from %0 to %2. |
| 331 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 332 | * 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] | 333 | * returned. |
| 334 | * |
| 335 | * Note: if the GPIO resource has multiple entries in the pin list, this |
| 336 | * function only returns the first. |
| 337 | */ |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 338 | struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index, |
| 339 | struct acpi_gpio_info *info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 340 | { |
| 341 | struct acpi_gpio_lookup lookup; |
| 342 | struct list_head resource_list; |
| 343 | struct acpi_device *adev; |
| 344 | acpi_handle handle; |
| 345 | int ret; |
| 346 | |
| 347 | if (!dev) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 348 | return ERR_PTR(-EINVAL); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 349 | |
| 350 | handle = ACPI_HANDLE(dev); |
| 351 | if (!handle || acpi_bus_get_device(handle, &adev)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 352 | return ERR_PTR(-ENODEV); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 353 | |
| 354 | memset(&lookup, 0, sizeof(lookup)); |
| 355 | lookup.index = index; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 356 | |
| 357 | INIT_LIST_HEAD(&resource_list); |
| 358 | ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio, |
| 359 | &lookup); |
| 360 | if (ret < 0) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 361 | return ERR_PTR(ret); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 362 | |
| 363 | acpi_dev_free_resource_list(&resource_list); |
| 364 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 365 | if (lookup.desc && info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 366 | *info = lookup.info; |
| 367 | |
Mika Westerberg | a00580c | 2013-12-10 12:00:27 +0200 | [diff] [blame] | 368 | return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 369 | } |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 370 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 371 | static acpi_status |
| 372 | acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, |
| 373 | u32 bits, u64 *value, void *handler_context, |
| 374 | void *region_context) |
| 375 | { |
| 376 | struct acpi_gpio_chip *achip = region_context; |
| 377 | struct gpio_chip *chip = achip->chip; |
| 378 | struct acpi_resource_gpio *agpio; |
| 379 | struct acpi_resource *ares; |
| 380 | acpi_status status; |
| 381 | bool pull_up; |
| 382 | int i; |
| 383 | |
| 384 | status = acpi_buffer_to_resource(achip->conn_info.connection, |
| 385 | achip->conn_info.length, &ares); |
| 386 | if (ACPI_FAILURE(status)) |
| 387 | return status; |
| 388 | |
| 389 | if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) { |
| 390 | ACPI_FREE(ares); |
| 391 | return AE_BAD_PARAMETER; |
| 392 | } |
| 393 | |
| 394 | agpio = &ares->data.gpio; |
| 395 | pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; |
| 396 | |
| 397 | if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT && |
| 398 | function == ACPI_WRITE)) { |
| 399 | ACPI_FREE(ares); |
| 400 | return AE_BAD_PARAMETER; |
| 401 | } |
| 402 | |
| 403 | for (i = 0; i < agpio->pin_table_length; i++) { |
| 404 | unsigned pin = agpio->pin_table[i]; |
| 405 | struct acpi_gpio_connection *conn; |
| 406 | struct gpio_desc *desc; |
| 407 | bool found; |
| 408 | |
| 409 | desc = gpiochip_get_desc(chip, pin); |
| 410 | if (IS_ERR(desc)) { |
| 411 | status = AE_ERROR; |
| 412 | goto out; |
| 413 | } |
| 414 | |
| 415 | mutex_lock(&achip->conn_lock); |
| 416 | |
| 417 | found = false; |
| 418 | list_for_each_entry(conn, &achip->conns, node) { |
| 419 | if (conn->desc == desc) { |
| 420 | found = true; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | if (!found) { |
| 425 | int ret; |
| 426 | |
| 427 | ret = gpiochip_request_own_desc(desc, "ACPI:OpRegion"); |
| 428 | if (ret) { |
| 429 | status = AE_ERROR; |
| 430 | mutex_unlock(&achip->conn_lock); |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | switch (agpio->io_restriction) { |
| 435 | case ACPI_IO_RESTRICT_INPUT: |
| 436 | gpiod_direction_input(desc); |
| 437 | break; |
| 438 | case ACPI_IO_RESTRICT_OUTPUT: |
| 439 | /* |
| 440 | * ACPI GPIO resources don't contain an |
| 441 | * initial value for the GPIO. Therefore we |
| 442 | * deduce that value from the pull field |
| 443 | * instead. If the pin is pulled up we |
| 444 | * assume default to be high, otherwise |
| 445 | * low. |
| 446 | */ |
| 447 | gpiod_direction_output(desc, pull_up); |
| 448 | break; |
| 449 | default: |
| 450 | /* |
| 451 | * Assume that the BIOS has configured the |
| 452 | * direction and pull accordingly. |
| 453 | */ |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
| 458 | if (!conn) { |
| 459 | status = AE_NO_MEMORY; |
| 460 | gpiochip_free_own_desc(desc); |
| 461 | mutex_unlock(&achip->conn_lock); |
| 462 | goto out; |
| 463 | } |
| 464 | |
| 465 | conn->desc = desc; |
| 466 | list_add_tail(&conn->node, &achip->conns); |
| 467 | } |
| 468 | |
| 469 | mutex_unlock(&achip->conn_lock); |
| 470 | |
| 471 | if (function == ACPI_WRITE) |
Aaron Lu | dc62b56 | 2014-05-20 17:07:38 +0800 | [diff] [blame] | 472 | gpiod_set_raw_value_cansleep(desc, |
| 473 | !!((1 << i) & *value)); |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 474 | else |
Aaron Lu | dc62b56 | 2014-05-20 17:07:38 +0800 | [diff] [blame] | 475 | *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | out: |
| 479 | ACPI_FREE(ares); |
| 480 | return status; |
| 481 | } |
| 482 | |
| 483 | static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip) |
| 484 | { |
| 485 | struct gpio_chip *chip = achip->chip; |
| 486 | acpi_handle handle = ACPI_HANDLE(chip->dev); |
| 487 | acpi_status status; |
| 488 | |
| 489 | INIT_LIST_HEAD(&achip->conns); |
| 490 | mutex_init(&achip->conn_lock); |
| 491 | status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, |
| 492 | acpi_gpio_adr_space_handler, |
| 493 | NULL, achip); |
| 494 | if (ACPI_FAILURE(status)) |
| 495 | dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n"); |
| 496 | } |
| 497 | |
| 498 | static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip) |
| 499 | { |
| 500 | struct gpio_chip *chip = achip->chip; |
| 501 | acpi_handle handle = ACPI_HANDLE(chip->dev); |
| 502 | struct acpi_gpio_connection *conn, *tmp; |
| 503 | acpi_status status; |
| 504 | |
| 505 | status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, |
| 506 | acpi_gpio_adr_space_handler); |
| 507 | if (ACPI_FAILURE(status)) { |
| 508 | dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n"); |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) { |
| 513 | gpiochip_free_own_desc(conn->desc); |
| 514 | list_del(&conn->node); |
| 515 | kfree(conn); |
| 516 | } |
| 517 | } |
| 518 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 519 | void acpi_gpiochip_add(struct gpio_chip *chip) |
| 520 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 521 | struct acpi_gpio_chip *acpi_gpio; |
| 522 | acpi_handle handle; |
| 523 | acpi_status status; |
| 524 | |
Mika Westerberg | e9595f8 | 2014-03-31 15:16:49 +0300 | [diff] [blame] | 525 | if (!chip || !chip->dev) |
| 526 | return; |
| 527 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 528 | handle = ACPI_HANDLE(chip->dev); |
| 529 | if (!handle) |
| 530 | return; |
| 531 | |
| 532 | acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL); |
| 533 | if (!acpi_gpio) { |
| 534 | dev_err(chip->dev, |
| 535 | "Failed to allocate memory for ACPI GPIO chip\n"); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | acpi_gpio->chip = chip; |
| 540 | |
| 541 | status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio); |
| 542 | if (ACPI_FAILURE(status)) { |
| 543 | dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n"); |
| 544 | kfree(acpi_gpio); |
| 545 | return; |
| 546 | } |
| 547 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 548 | acpi_gpiochip_request_regions(acpi_gpio); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | void acpi_gpiochip_remove(struct gpio_chip *chip) |
| 552 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 553 | struct acpi_gpio_chip *acpi_gpio; |
| 554 | acpi_handle handle; |
| 555 | acpi_status status; |
| 556 | |
Mika Westerberg | e9595f8 | 2014-03-31 15:16:49 +0300 | [diff] [blame] | 557 | if (!chip || !chip->dev) |
| 558 | return; |
| 559 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 560 | handle = ACPI_HANDLE(chip->dev); |
| 561 | if (!handle) |
| 562 | return; |
| 563 | |
| 564 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 565 | if (ACPI_FAILURE(status)) { |
| 566 | dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n"); |
| 567 | return; |
| 568 | } |
| 569 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 570 | acpi_gpiochip_free_regions(acpi_gpio); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 571 | |
| 572 | acpi_detach_data(handle, acpi_gpio_chip_dh); |
| 573 | kfree(acpi_gpio); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 574 | } |