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