Linus Walleij | dae5f0a | 2018-09-25 09:08:48 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 2 | /* |
| 3 | * ACPI helpers for GPIO API |
| 4 | * |
| 5 | * Copyright (C) 2012, Intel Corporation |
| 6 | * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> |
| 7 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/errno.h> |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 11 | #include <linux/gpio/consumer.h> |
Mika Westerberg | 5ccff85 | 2014-01-08 12:40:56 +0200 | [diff] [blame] | 12 | #include <linux/gpio/driver.h> |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 13 | #include <linux/gpio/machine.h> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 14 | #include <linux/export.h> |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 15 | #include <linux/acpi.h> |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Mika Westerberg | 354567e | 2014-11-03 13:01:32 +0200 | [diff] [blame] | 18 | #include <linux/pinctrl/pinctrl.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; |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 27 | struct gpio_desc *desc; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 30 | struct acpi_gpio_connection { |
| 31 | struct list_head node; |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 32 | unsigned int pin; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 33 | struct gpio_desc *desc; |
| 34 | }; |
| 35 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 36 | struct acpi_gpio_chip { |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 37 | /* |
| 38 | * ACPICA requires that the first field of the context parameter |
| 39 | * passed to acpi_install_address_space_handler() is large enough |
| 40 | * to hold struct acpi_connection_info. |
| 41 | */ |
| 42 | struct acpi_connection_info conn_info; |
| 43 | struct list_head conns; |
| 44 | struct mutex conn_lock; |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 45 | struct gpio_chip *chip; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 46 | struct list_head events; |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 47 | struct list_head deferred_req_irqs_list_entry; |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 50 | /* |
| 51 | * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init |
| 52 | * (so builtin drivers) we register the ACPI GpioInt event handlers from a |
| 53 | * late_initcall_sync handler, so that other builtin drivers can register their |
| 54 | * OpRegions before the event handlers can run. This list contains gpiochips |
| 55 | * for which the acpi_gpiochip_request_interrupts() has been deferred. |
| 56 | */ |
| 57 | static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock); |
| 58 | static LIST_HEAD(acpi_gpio_deferred_req_irqs_list); |
| 59 | static bool acpi_gpio_deferred_req_irqs_done; |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 60 | |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 61 | static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) |
| 62 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 63 | if (!gc->parent) |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 64 | return false; |
| 65 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 66 | return ACPI_HANDLE(gc->parent) == data; |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 70 | * 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] | 71 | * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") |
| 72 | * @pin: ACPI GPIO pin number (0-based, controller-relative) |
| 73 | * |
Mika Westerberg | f35bbf6 | 2015-06-10 16:05:05 +0300 | [diff] [blame] | 74 | * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR |
| 75 | * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO |
| 76 | * controller does not have gpiochip registered at the moment. This is to |
| 77 | * support probe deferral. |
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 | static struct gpio_desc *acpi_get_gpiod(char *path, int pin) |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 80 | { |
| 81 | struct gpio_chip *chip; |
| 82 | acpi_handle handle; |
| 83 | acpi_status status; |
| 84 | |
| 85 | status = acpi_get_handle(NULL, path, &handle); |
| 86 | if (ACPI_FAILURE(status)) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 87 | return ERR_PTR(-ENODEV); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 88 | |
| 89 | chip = gpiochip_find(handle, acpi_gpiochip_find); |
| 90 | if (!chip) |
Mika Westerberg | f35bbf6 | 2015-06-10 16:05:05 +0300 | [diff] [blame] | 91 | return ERR_PTR(-EPROBE_DEFER); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 92 | |
Mika Westerberg | 03c4749 | 2017-11-27 16:54:42 +0300 | [diff] [blame] | 93 | return gpiochip_get_desc(chip, pin); |
Mathias Nyman | e29482e | 2012-11-30 12:37:36 +0100 | [diff] [blame] | 94 | } |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 95 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 96 | static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) |
| 97 | { |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 98 | struct acpi_gpio_event *event = data; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 99 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 100 | acpi_evaluate_object(event->handle, NULL, NULL, NULL); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 101 | |
| 102 | return IRQ_HANDLED; |
| 103 | } |
| 104 | |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 105 | static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) |
| 106 | { |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 107 | struct acpi_gpio_event *event = data; |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 108 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 109 | acpi_execute_simple_method(event->handle, NULL, event->pin); |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 110 | |
| 111 | return IRQ_HANDLED; |
| 112 | } |
| 113 | |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 114 | static void acpi_gpio_chip_dh(acpi_handle handle, void *data) |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 115 | { |
| 116 | /* The address of this function is used as a key. */ |
| 117 | } |
| 118 | |
Andy Shevchenko | 25e3ef8 | 2017-05-23 20:03:24 +0300 | [diff] [blame] | 119 | bool acpi_gpio_get_irq_resource(struct acpi_resource *ares, |
| 120 | struct acpi_resource_gpio **agpio) |
| 121 | { |
| 122 | struct acpi_resource_gpio *gpio; |
| 123 | |
| 124 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 125 | return false; |
| 126 | |
| 127 | gpio = &ares->data.gpio; |
| 128 | if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT) |
| 129 | return false; |
| 130 | |
| 131 | *agpio = gpio; |
| 132 | return true; |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource); |
| 135 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 136 | static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares, |
| 137 | void *context) |
| 138 | { |
| 139 | struct acpi_gpio_chip *acpi_gpio = context; |
| 140 | struct gpio_chip *chip = acpi_gpio->chip; |
| 141 | struct acpi_resource_gpio *agpio; |
| 142 | acpi_handle handle, evt_handle; |
| 143 | struct acpi_gpio_event *event; |
| 144 | irq_handler_t handler = NULL; |
| 145 | struct gpio_desc *desc; |
| 146 | unsigned long irqflags; |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 147 | int ret, pin, irq, value; |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 148 | |
Andy Shevchenko | 25e3ef8 | 2017-05-23 20:03:24 +0300 | [diff] [blame] | 149 | if (!acpi_gpio_get_irq_resource(ares, &agpio)) |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 150 | return AE_OK; |
| 151 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 152 | handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 153 | pin = agpio->pin_table[0]; |
| 154 | |
| 155 | if (pin <= 255) { |
| 156 | char ev_name[5]; |
Arnd Bergmann | e40a3ae | 2017-09-06 17:47:45 +0200 | [diff] [blame] | 157 | sprintf(ev_name, "_%c%02hhX", |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 158 | agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', |
| 159 | pin); |
| 160 | if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) |
| 161 | handler = acpi_gpio_irq_handler; |
| 162 | } |
| 163 | if (!handler) { |
| 164 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle))) |
| 165 | handler = acpi_gpio_irq_handler_evt; |
| 166 | } |
| 167 | if (!handler) |
Hans de Goede | c06632e | 2017-06-23 09:26:13 +0200 | [diff] [blame] | 168 | return AE_OK; |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 169 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 170 | desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event"); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 171 | if (IS_ERR(desc)) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 172 | dev_err(chip->parent, "Failed to request GPIO\n"); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 173 | return AE_ERROR; |
| 174 | } |
| 175 | |
| 176 | gpiod_direction_input(desc); |
| 177 | |
Andy Shevchenko | 993b9bc | 2018-08-13 19:00:27 +0300 | [diff] [blame] | 178 | value = gpiod_get_value_cansleep(desc); |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 179 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 180 | ret = gpiochip_lock_as_irq(chip, pin); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 181 | if (ret) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 182 | dev_err(chip->parent, "Failed to lock GPIO as interrupt\n"); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 183 | goto fail_free_desc; |
| 184 | } |
| 185 | |
| 186 | irq = gpiod_to_irq(desc); |
| 187 | if (irq < 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 188 | dev_err(chip->parent, "Failed to translate GPIO to IRQ\n"); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 189 | goto fail_unlock_irq; |
| 190 | } |
| 191 | |
| 192 | irqflags = IRQF_ONESHOT; |
| 193 | if (agpio->triggering == ACPI_LEVEL_SENSITIVE) { |
| 194 | if (agpio->polarity == ACPI_ACTIVE_HIGH) |
| 195 | irqflags |= IRQF_TRIGGER_HIGH; |
| 196 | else |
| 197 | irqflags |= IRQF_TRIGGER_LOW; |
| 198 | } else { |
| 199 | switch (agpio->polarity) { |
| 200 | case ACPI_ACTIVE_HIGH: |
| 201 | irqflags |= IRQF_TRIGGER_RISING; |
| 202 | break; |
| 203 | case ACPI_ACTIVE_LOW: |
| 204 | irqflags |= IRQF_TRIGGER_FALLING; |
| 205 | break; |
| 206 | default: |
| 207 | irqflags |= IRQF_TRIGGER_RISING | |
| 208 | IRQF_TRIGGER_FALLING; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 214 | if (!event) |
| 215 | goto fail_unlock_irq; |
| 216 | |
| 217 | event->handle = evt_handle; |
| 218 | event->irq = irq; |
| 219 | event->pin = pin; |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 220 | event->desc = desc; |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 221 | |
| 222 | ret = request_threaded_irq(event->irq, NULL, handler, irqflags, |
| 223 | "ACPI:Event", event); |
| 224 | if (ret) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 225 | dev_err(chip->parent, |
| 226 | "Failed to setup interrupt handler for %d\n", |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 227 | event->irq); |
| 228 | goto fail_free_event; |
| 229 | } |
| 230 | |
Hans de Goede | 8a146fb | 2017-03-24 11:08:47 +0100 | [diff] [blame] | 231 | if (agpio->wake_capable == ACPI_WAKE_CAPABLE) |
| 232 | enable_irq_wake(irq); |
| 233 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 234 | list_add_tail(&event->node, &acpi_gpio->events); |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * Make sure we trigger the initial state of the IRQ when using RISING |
| 238 | * or FALLING. Note we run the handlers on late_init, the AML code |
| 239 | * may refer to OperationRegions from other (builtin) drivers which |
| 240 | * may be probed after us. |
| 241 | */ |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 242 | if (((irqflags & IRQF_TRIGGER_RISING) && value == 1) || |
| 243 | ((irqflags & IRQF_TRIGGER_FALLING) && value == 0)) |
| 244 | handler(event->irq, event); |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 245 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 246 | return AE_OK; |
| 247 | |
| 248 | fail_free_event: |
| 249 | kfree(event); |
| 250 | fail_unlock_irq: |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 251 | gpiochip_unlock_as_irq(chip, pin); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 252 | fail_free_desc: |
| 253 | gpiochip_free_own_desc(desc); |
| 254 | |
| 255 | return AE_ERROR; |
| 256 | } |
| 257 | |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 258 | /** |
| 259 | * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 260 | * @chip: GPIO chip |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 261 | * |
| 262 | * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are |
| 263 | * handled by ACPI event methods which need to be called from the GPIO |
| 264 | * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which |
| 265 | * gpio pins have acpi event methods and assigns interrupt handlers that calls |
| 266 | * the acpi event methods for those pins. |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 267 | */ |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 268 | void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 269 | { |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 270 | struct acpi_gpio_chip *acpi_gpio; |
| 271 | acpi_handle handle; |
| 272 | acpi_status status; |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 273 | bool defer; |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 274 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 275 | if (!chip->parent || !chip->to_irq) |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 276 | return; |
| 277 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 278 | handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 279 | if (!handle) |
| 280 | return; |
| 281 | |
| 282 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 283 | if (ACPI_FAILURE(status)) |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 284 | return; |
| 285 | |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 286 | mutex_lock(&acpi_gpio_deferred_req_irqs_lock); |
| 287 | defer = !acpi_gpio_deferred_req_irqs_done; |
| 288 | if (defer) |
| 289 | list_add(&acpi_gpio->deferred_req_irqs_list_entry, |
| 290 | &acpi_gpio_deferred_req_irqs_list); |
| 291 | mutex_unlock(&acpi_gpio_deferred_req_irqs_lock); |
| 292 | |
| 293 | if (defer) |
| 294 | return; |
| 295 | |
Rafael J. Wysocki | 1ecb016 | 2015-03-10 23:10:01 +0100 | [diff] [blame] | 296 | acpi_walk_resources(handle, "_AEI", |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 297 | acpi_gpiochip_request_interrupt, acpi_gpio); |
Mathias Nyman | 0d1c28a | 2013-01-28 16:23:10 +0200 | [diff] [blame] | 298 | } |
Hanjun Guo | 2b528ff | 2015-06-10 17:12:07 +0800 | [diff] [blame] | 299 | EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts); |
Rafael J. Wysocki | 7fc7acb | 2013-04-09 15:57:25 +0200 | [diff] [blame] | 300 | |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 301 | /** |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 302 | * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts. |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 303 | * @chip: GPIO chip |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 304 | * |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 305 | * Free interrupts associated with GPIO ACPI event method for the given |
| 306 | * GPIO chip. |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 307 | */ |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 308 | void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 309 | { |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 310 | struct acpi_gpio_chip *acpi_gpio; |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 311 | struct acpi_gpio_event *event, *ep; |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 312 | acpi_handle handle; |
| 313 | acpi_status status; |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 314 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 315 | if (!chip->parent || !chip->to_irq) |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 316 | return; |
| 317 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 318 | handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 319 | if (!handle) |
| 320 | return; |
| 321 | |
| 322 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 323 | if (ACPI_FAILURE(status)) |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 324 | return; |
| 325 | |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 326 | mutex_lock(&acpi_gpio_deferred_req_irqs_lock); |
| 327 | if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry)) |
| 328 | list_del_init(&acpi_gpio->deferred_req_irqs_list_entry); |
| 329 | mutex_unlock(&acpi_gpio_deferred_req_irqs_lock); |
| 330 | |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 331 | list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) { |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 332 | struct gpio_desc *desc; |
| 333 | |
Hans de Goede | 8a146fb | 2017-03-24 11:08:47 +0100 | [diff] [blame] | 334 | if (irqd_is_wakeup_set(irq_get_irq_data(event->irq))) |
| 335 | disable_irq_wake(event->irq); |
| 336 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 337 | free_irq(event->irq, event); |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 338 | desc = event->desc; |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 339 | if (WARN_ON(IS_ERR(desc))) |
| 340 | continue; |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 341 | gpiochip_unlock_as_irq(chip, event->pin); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 342 | gpiochip_free_own_desc(desc); |
Mika Westerberg | 4b01a14 | 2014-03-10 14:54:52 +0200 | [diff] [blame] | 343 | list_del(&event->node); |
| 344 | kfree(event); |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 345 | } |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 346 | } |
Hanjun Guo | 2b528ff | 2015-06-10 17:12:07 +0800 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts); |
Mika Westerberg | 70b53411 | 2013-10-10 11:01:07 +0300 | [diff] [blame] | 348 | |
Rafael J. Wysocki | f028d52 | 2014-11-03 23:39:41 +0100 | [diff] [blame] | 349 | int acpi_dev_add_driver_gpios(struct acpi_device *adev, |
| 350 | const struct acpi_gpio_mapping *gpios) |
| 351 | { |
| 352 | if (adev && gpios) { |
| 353 | adev->driver_gpios = gpios; |
| 354 | return 0; |
| 355 | } |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios); |
| 359 | |
Andy Shevchenko | 85c73d5 | 2017-03-02 15:48:05 +0200 | [diff] [blame] | 360 | static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res) |
| 361 | { |
| 362 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev)); |
| 363 | } |
| 364 | |
| 365 | int devm_acpi_dev_add_driver_gpios(struct device *dev, |
| 366 | const struct acpi_gpio_mapping *gpios) |
| 367 | { |
| 368 | void *res; |
| 369 | int ret; |
| 370 | |
| 371 | res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL); |
| 372 | if (!res) |
| 373 | return -ENOMEM; |
| 374 | |
| 375 | ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios); |
| 376 | if (ret) { |
| 377 | devres_free(res); |
| 378 | return ret; |
| 379 | } |
| 380 | devres_add(dev, res); |
| 381 | return 0; |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios); |
| 384 | |
| 385 | void devm_acpi_dev_remove_driver_gpios(struct device *dev) |
| 386 | { |
| 387 | WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL)); |
| 388 | } |
| 389 | EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios); |
| 390 | |
Rafael J. Wysocki | f028d52 | 2014-11-03 23:39:41 +0100 | [diff] [blame] | 391 | static bool acpi_get_driver_gpio_data(struct acpi_device *adev, |
| 392 | const char *name, int index, |
Sakari Ailus | 977d5ad | 2018-07-17 17:19:11 +0300 | [diff] [blame] | 393 | struct fwnode_reference_args *args, |
Andy Shevchenko | ce0929d | 2017-11-10 15:40:32 +0200 | [diff] [blame] | 394 | unsigned int *quirks) |
Rafael J. Wysocki | f028d52 | 2014-11-03 23:39:41 +0100 | [diff] [blame] | 395 | { |
| 396 | const struct acpi_gpio_mapping *gm; |
| 397 | |
| 398 | if (!adev->driver_gpios) |
| 399 | return false; |
| 400 | |
| 401 | for (gm = adev->driver_gpios; gm->name; gm++) |
| 402 | if (!strcmp(name, gm->name) && gm->data && index < gm->size) { |
| 403 | const struct acpi_gpio_params *par = gm->data + index; |
| 404 | |
Sakari Ailus | 977d5ad | 2018-07-17 17:19:11 +0300 | [diff] [blame] | 405 | args->fwnode = acpi_fwnode_handle(adev); |
Rafael J. Wysocki | f028d52 | 2014-11-03 23:39:41 +0100 | [diff] [blame] | 406 | args->args[0] = par->crs_entry_index; |
| 407 | args->args[1] = par->line_index; |
| 408 | args->args[2] = par->active_low; |
| 409 | args->nargs = 3; |
Andy Shevchenko | ce0929d | 2017-11-10 15:40:32 +0200 | [diff] [blame] | 410 | |
| 411 | *quirks = gm->quirks; |
Rafael J. Wysocki | f028d52 | 2014-11-03 23:39:41 +0100 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
| 415 | return false; |
| 416 | } |
| 417 | |
Andy Shevchenko | 2eca25a | 2017-05-23 20:03:22 +0300 | [diff] [blame] | 418 | static enum gpiod_flags |
| 419 | acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio) |
| 420 | { |
| 421 | bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; |
| 422 | |
| 423 | switch (agpio->io_restriction) { |
| 424 | case ACPI_IO_RESTRICT_INPUT: |
| 425 | return GPIOD_IN; |
| 426 | case ACPI_IO_RESTRICT_OUTPUT: |
| 427 | /* |
| 428 | * ACPI GPIO resources don't contain an initial value for the |
| 429 | * GPIO. Therefore we deduce that value from the pull field |
| 430 | * instead. If the pin is pulled up we assume default to be |
| 431 | * high, otherwise low. |
| 432 | */ |
| 433 | return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
| 434 | default: |
| 435 | /* |
| 436 | * Assume that the BIOS has configured the direction and pull |
| 437 | * accordingly. |
| 438 | */ |
| 439 | return GPIOD_ASIS; |
| 440 | } |
| 441 | } |
| 442 | |
Andy Shevchenko | 5c34b6c | 2017-11-10 15:40:31 +0200 | [diff] [blame] | 443 | static int |
| 444 | __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update) |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 445 | { |
| 446 | int ret = 0; |
| 447 | |
| 448 | /* |
| 449 | * Check if the BIOS has IoRestriction with explicitly set direction |
| 450 | * and update @flags accordingly. Otherwise use whatever caller asked |
| 451 | * for. |
| 452 | */ |
| 453 | if (update & GPIOD_FLAGS_BIT_DIR_SET) { |
| 454 | enum gpiod_flags diff = *flags ^ update; |
| 455 | |
| 456 | /* |
| 457 | * Check if caller supplied incompatible GPIO initialization |
| 458 | * flags. |
| 459 | * |
| 460 | * Return %-EINVAL to notify that firmware has different |
| 461 | * settings and we are going to use them. |
| 462 | */ |
| 463 | if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) || |
| 464 | ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL))) |
| 465 | ret = -EINVAL; |
| 466 | *flags = update; |
| 467 | } |
| 468 | return ret; |
| 469 | } |
| 470 | |
Andy Shevchenko | 5c34b6c | 2017-11-10 15:40:31 +0200 | [diff] [blame] | 471 | int |
| 472 | acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info) |
| 473 | { |
| 474 | struct device *dev = &info->adev->dev; |
Andy Shevchenko | 1b2ca32 | 2017-11-10 15:40:33 +0200 | [diff] [blame] | 475 | enum gpiod_flags old = *flags; |
Andy Shevchenko | 5c34b6c | 2017-11-10 15:40:31 +0200 | [diff] [blame] | 476 | int ret; |
| 477 | |
Andy Shevchenko | 1b2ca32 | 2017-11-10 15:40:33 +0200 | [diff] [blame] | 478 | ret = __acpi_gpio_update_gpiod_flags(&old, info->flags); |
| 479 | if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) { |
| 480 | if (ret) |
| 481 | dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n"); |
| 482 | } else { |
| 483 | if (ret) |
| 484 | dev_dbg(dev, "Override GPIO initialization flags\n"); |
| 485 | *flags = old; |
| 486 | } |
Andy Shevchenko | 5c34b6c | 2017-11-10 15:40:31 +0200 | [diff] [blame] | 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 491 | struct acpi_gpio_lookup { |
| 492 | struct acpi_gpio_info info; |
| 493 | int index; |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 494 | int pin_index; |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 495 | bool active_low; |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 496 | struct gpio_desc *desc; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 497 | int n; |
| 498 | }; |
| 499 | |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 500 | static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 501 | { |
| 502 | struct acpi_gpio_lookup *lookup = data; |
| 503 | |
| 504 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) |
| 505 | return 1; |
| 506 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 507 | if (lookup->n++ == lookup->index && !lookup->desc) { |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 508 | const struct acpi_resource_gpio *agpio = &ares->data.gpio; |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 509 | int pin_index = lookup->pin_index; |
| 510 | |
| 511 | if (pin_index >= agpio->pin_table_length) |
| 512 | return 1; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 513 | |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 514 | lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 515 | agpio->pin_table[pin_index]); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 516 | lookup->info.gpioint = |
| 517 | agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 518 | |
| 519 | /* |
Andy Shevchenko | e567c35 | 2017-01-03 19:01:18 +0200 | [diff] [blame] | 520 | * Polarity and triggering are only specified for GpioInt |
| 521 | * resource. |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 522 | * Note: we expect here: |
| 523 | * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW |
| 524 | * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 525 | */ |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 526 | if (lookup->info.gpioint) { |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 527 | lookup->info.flags = GPIOD_IN; |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 528 | lookup->info.polarity = agpio->polarity; |
| 529 | lookup->info.triggering = agpio->triggering; |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 530 | } else { |
| 531 | lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio); |
Andy Shevchenko | f67a6c1 | 2017-11-10 15:40:28 +0200 | [diff] [blame] | 532 | lookup->info.polarity = lookup->active_low; |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 533 | } |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return 1; |
| 537 | } |
| 538 | |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 539 | static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup, |
| 540 | struct acpi_gpio_info *info) |
| 541 | { |
Andy Shevchenko | 5870cff47 | 2017-11-10 15:40:30 +0200 | [diff] [blame] | 542 | struct acpi_device *adev = lookup->info.adev; |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 543 | struct list_head res_list; |
| 544 | int ret; |
| 545 | |
| 546 | INIT_LIST_HEAD(&res_list); |
| 547 | |
Andy Shevchenko | 5870cff47 | 2017-11-10 15:40:30 +0200 | [diff] [blame] | 548 | ret = acpi_dev_get_resources(adev, &res_list, |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 549 | acpi_populate_gpio_lookup, |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 550 | lookup); |
| 551 | if (ret < 0) |
| 552 | return ret; |
| 553 | |
| 554 | acpi_dev_free_resource_list(&res_list); |
| 555 | |
| 556 | if (!lookup->desc) |
| 557 | return -ENOENT; |
| 558 | |
Andy Shevchenko | f67a6c1 | 2017-11-10 15:40:28 +0200 | [diff] [blame] | 559 | if (info) |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 560 | *info = lookup->info; |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 564 | static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 565 | const char *propname, int index, |
| 566 | struct acpi_gpio_lookup *lookup) |
| 567 | { |
Sakari Ailus | 977d5ad | 2018-07-17 17:19:11 +0300 | [diff] [blame] | 568 | struct fwnode_reference_args args; |
Andy Shevchenko | ce0929d | 2017-11-10 15:40:32 +0200 | [diff] [blame] | 569 | unsigned int quirks = 0; |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 570 | int ret; |
| 571 | |
| 572 | memset(&args, 0, sizeof(args)); |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 573 | ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, |
| 574 | &args); |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 575 | if (ret) { |
| 576 | struct acpi_device *adev = to_acpi_device_node(fwnode); |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 577 | |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 578 | if (!adev) |
| 579 | return ret; |
| 580 | |
Andy Shevchenko | ce0929d | 2017-11-10 15:40:32 +0200 | [diff] [blame] | 581 | if (!acpi_get_driver_gpio_data(adev, propname, index, &args, |
| 582 | &quirks)) |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 583 | return ret; |
| 584 | } |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 585 | /* |
| 586 | * The property was found and resolved, so need to lookup the GPIO based |
| 587 | * on returned args. |
| 588 | */ |
Sakari Ailus | 977d5ad | 2018-07-17 17:19:11 +0300 | [diff] [blame] | 589 | if (!to_acpi_device_node(args.fwnode)) |
| 590 | return -EINVAL; |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 591 | if (args.nargs != 3) |
| 592 | return -EPROTO; |
| 593 | |
| 594 | lookup->index = args.args[0]; |
| 595 | lookup->pin_index = args.args[1]; |
| 596 | lookup->active_low = !!args.args[2]; |
| 597 | |
Sakari Ailus | 977d5ad | 2018-07-17 17:19:11 +0300 | [diff] [blame] | 598 | lookup->info.adev = to_acpi_device_node(args.fwnode); |
Andy Shevchenko | ce0929d | 2017-11-10 15:40:32 +0200 | [diff] [blame] | 599 | lookup->info.quirks = quirks; |
Sakari Ailus | 977d5ad | 2018-07-17 17:19:11 +0300 | [diff] [blame] | 600 | |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 604 | /** |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 605 | * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 606 | * @adev: pointer to a ACPI device to get GPIO from |
| 607 | * @propname: Property name of the GPIO (optional) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 608 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
| 609 | * @info: info pointer to fill in (optional) |
| 610 | * |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 611 | * Function goes through ACPI resources for @adev and based on @index looks |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 612 | * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 613 | * and returns it. @index matches GpioIo/GpioInt resources only so if there |
| 614 | * are total %3 GPIO resources, the index goes from %0 to %2. |
| 615 | * |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 616 | * If @propname is specified the GPIO is looked using device property. In |
| 617 | * that case @index is used to select the GPIO entry in the property value |
| 618 | * (in case of multiple). |
| 619 | * |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 620 | * 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] | 621 | * returned. |
| 622 | * |
| 623 | * Note: if the GPIO resource has multiple entries in the pin list, this |
| 624 | * function only returns the first. |
| 625 | */ |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 626 | static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 627 | const char *propname, int index, |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 628 | struct acpi_gpio_info *info) |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 629 | { |
| 630 | struct acpi_gpio_lookup lookup; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 631 | int ret; |
| 632 | |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 633 | if (!adev) |
Mika Westerberg | 936e15d | 2013-10-10 11:01:08 +0300 | [diff] [blame] | 634 | return ERR_PTR(-ENODEV); |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 635 | |
| 636 | memset(&lookup, 0, sizeof(lookup)); |
| 637 | lookup.index = index; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 638 | |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 639 | if (propname) { |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 640 | dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname); |
| 641 | |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 642 | ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), |
| 643 | propname, index, &lookup); |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 644 | if (ret) |
| 645 | return ERR_PTR(ret); |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 646 | |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 647 | dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n", |
Andy Shevchenko | 5870cff47 | 2017-11-10 15:40:30 +0200 | [diff] [blame] | 648 | dev_name(&lookup.info.adev->dev), lookup.index, |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 649 | lookup.pin_index, lookup.active_low); |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 650 | } else { |
| 651 | dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index); |
Andy Shevchenko | 5870cff47 | 2017-11-10 15:40:30 +0200 | [diff] [blame] | 652 | lookup.info.adev = adev; |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Rafael J. Wysocki | d079524 | 2015-08-27 04:41:33 +0200 | [diff] [blame] | 655 | ret = acpi_gpio_resource_lookup(&lookup, info); |
| 656 | return ret ? ERR_PTR(ret) : lookup.desc; |
Mika Westerberg | 12028d2 | 2013-04-03 13:56:54 +0300 | [diff] [blame] | 657 | } |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 658 | |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 659 | struct gpio_desc *acpi_find_gpio(struct device *dev, |
| 660 | const char *con_id, |
| 661 | unsigned int idx, |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 662 | enum gpiod_flags *dflags, |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 663 | enum gpio_lookup_flags *lookupflags) |
| 664 | { |
| 665 | struct acpi_device *adev = ACPI_COMPANION(dev); |
| 666 | struct acpi_gpio_info info; |
| 667 | struct gpio_desc *desc; |
| 668 | char propname[32]; |
| 669 | int i; |
| 670 | |
| 671 | /* Try first from _DSD */ |
| 672 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
Andy Shevchenko | 9e66504 | 2017-05-23 20:03:17 +0300 | [diff] [blame] | 673 | if (con_id) { |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 674 | snprintf(propname, sizeof(propname), "%s-%s", |
| 675 | con_id, gpio_suffixes[i]); |
| 676 | } else { |
| 677 | snprintf(propname, sizeof(propname), "%s", |
| 678 | gpio_suffixes[i]); |
| 679 | } |
| 680 | |
| 681 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); |
Dmitry Torokhov | 693bdaa | 2017-03-23 13:21:38 -0700 | [diff] [blame] | 682 | if (!IS_ERR(desc)) |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 683 | break; |
Dmitry Torokhov | 693bdaa | 2017-03-23 13:21:38 -0700 | [diff] [blame] | 684 | if (PTR_ERR(desc) == -EPROBE_DEFER) |
| 685 | return ERR_CAST(desc); |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* Then from plain _CRS GPIOs */ |
| 689 | if (IS_ERR(desc)) { |
| 690 | if (!acpi_can_fallback_to_crs(adev, con_id)) |
| 691 | return ERR_PTR(-ENOENT); |
| 692 | |
| 693 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); |
| 694 | if (IS_ERR(desc)) |
| 695 | return desc; |
Andy Shevchenko | fe06b56 | 2017-05-23 20:03:18 +0300 | [diff] [blame] | 696 | } |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 697 | |
Andy Shevchenko | fe06b56 | 2017-05-23 20:03:18 +0300 | [diff] [blame] | 698 | if (info.gpioint && |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 699 | (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) { |
Andy Shevchenko | fe06b56 | 2017-05-23 20:03:18 +0300 | [diff] [blame] | 700 | dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n"); |
| 701 | return ERR_PTR(-ENOENT); |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | if (info.polarity == GPIO_ACTIVE_LOW) |
| 705 | *lookupflags |= GPIO_ACTIVE_LOW; |
| 706 | |
Andy Shevchenko | 5c34b6c | 2017-11-10 15:40:31 +0200 | [diff] [blame] | 707 | acpi_gpio_update_gpiod_flags(dflags, &info); |
Linus Walleij | 031ba28 | 2016-10-03 10:40:03 +0200 | [diff] [blame] | 708 | return desc; |
| 709 | } |
| 710 | |
Mika Westerberg | c884fbd | 2015-05-06 13:29:06 +0300 | [diff] [blame] | 711 | /** |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 712 | * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources |
| 713 | * @fwnode: pointer to an ACPI firmware node to get the GPIO information from |
| 714 | * @propname: Property name of the GPIO |
| 715 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
| 716 | * @info: info pointer to fill in (optional) |
| 717 | * |
| 718 | * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it. |
| 719 | * Otherwise (ie. it is a data-only non-device object), use the property-based |
| 720 | * GPIO lookup to get to the GPIO resource with the relevant information and use |
| 721 | * that to obtain the GPIO descriptor to return. |
| 722 | */ |
| 723 | struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, |
| 724 | const char *propname, int index, |
| 725 | struct acpi_gpio_info *info) |
| 726 | { |
| 727 | struct acpi_gpio_lookup lookup; |
| 728 | struct acpi_device *adev; |
| 729 | int ret; |
| 730 | |
| 731 | adev = to_acpi_device_node(fwnode); |
| 732 | if (adev) |
| 733 | return acpi_get_gpiod_by_index(adev, propname, index, info); |
| 734 | |
| 735 | if (!is_acpi_data_node(fwnode)) |
| 736 | return ERR_PTR(-ENODEV); |
| 737 | |
| 738 | if (!propname) |
| 739 | return ERR_PTR(-EINVAL); |
| 740 | |
| 741 | memset(&lookup, 0, sizeof(lookup)); |
| 742 | lookup.index = index; |
| 743 | |
| 744 | ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup); |
| 745 | if (ret) |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 746 | return ERR_PTR(ret); |
| 747 | |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 748 | ret = acpi_gpio_resource_lookup(&lookup, info); |
| 749 | return ret ? ERR_PTR(ret) : lookup.desc; |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Mika Westerberg | c884fbd | 2015-05-06 13:29:06 +0300 | [diff] [blame] | 752 | /** |
| 753 | * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number |
| 754 | * @adev: pointer to a ACPI device to get IRQ from |
| 755 | * @index: index of GpioInt resource (starting from %0) |
| 756 | * |
| 757 | * If the device has one or more GpioInt resources, this function can be |
| 758 | * used to translate from the GPIO offset in the resource to the Linux IRQ |
| 759 | * number. |
| 760 | * |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 761 | * The function is idempotent, though each time it runs it will configure GPIO |
| 762 | * pin direction according to the flags in GpioInt resource. |
| 763 | * |
Thierry Reding | db05c7e | 2017-07-24 16:57:25 +0200 | [diff] [blame] | 764 | * Return: Linux IRQ number (> %0) on success, negative errno on failure. |
Mika Westerberg | c884fbd | 2015-05-06 13:29:06 +0300 | [diff] [blame] | 765 | */ |
| 766 | int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index) |
| 767 | { |
| 768 | int idx, i; |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 769 | unsigned int irq_flags; |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 770 | int ret; |
Mika Westerberg | c884fbd | 2015-05-06 13:29:06 +0300 | [diff] [blame] | 771 | |
| 772 | for (i = 0, idx = 0; idx <= index; i++) { |
| 773 | struct acpi_gpio_info info; |
| 774 | struct gpio_desc *desc; |
| 775 | |
| 776 | desc = acpi_get_gpiod_by_index(adev, NULL, i, &info); |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 777 | |
Hans de Goede | 6798d72 | 2017-03-13 23:04:21 +0100 | [diff] [blame] | 778 | /* Ignore -EPROBE_DEFER, it only matters if idx matches */ |
| 779 | if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) |
| 780 | return PTR_ERR(desc); |
| 781 | |
| 782 | if (info.gpioint && idx++ == index) { |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 783 | char label[32]; |
Hans de Goede | 6798d72 | 2017-03-13 23:04:21 +0100 | [diff] [blame] | 784 | int irq; |
| 785 | |
| 786 | if (IS_ERR(desc)) |
| 787 | return PTR_ERR(desc); |
| 788 | |
| 789 | irq = gpiod_to_irq(desc); |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 790 | if (irq < 0) |
| 791 | return irq; |
| 792 | |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 793 | snprintf(label, sizeof(label), "GpioInt() %d", index); |
| 794 | ret = gpiod_configure_flags(desc, label, 0, info.flags); |
| 795 | if (ret < 0) |
| 796 | return ret; |
| 797 | |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 798 | irq_flags = acpi_dev_get_irq_type(info.triggering, |
| 799 | info.polarity); |
| 800 | |
| 801 | /* Set type if specified and different than the current one */ |
| 802 | if (irq_flags != IRQ_TYPE_NONE && |
| 803 | irq_flags != irq_get_trigger_type(irq)) |
| 804 | irq_set_irq_type(irq, irq_flags); |
| 805 | |
| 806 | return irq; |
| 807 | } |
| 808 | |
Mika Westerberg | c884fbd | 2015-05-06 13:29:06 +0300 | [diff] [blame] | 809 | } |
Hans de Goede | 6798d72 | 2017-03-13 23:04:21 +0100 | [diff] [blame] | 810 | return -ENOENT; |
Mika Westerberg | c884fbd | 2015-05-06 13:29:06 +0300 | [diff] [blame] | 811 | } |
| 812 | EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get); |
| 813 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 814 | static acpi_status |
| 815 | acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, |
| 816 | u32 bits, u64 *value, void *handler_context, |
| 817 | void *region_context) |
| 818 | { |
| 819 | struct acpi_gpio_chip *achip = region_context; |
| 820 | struct gpio_chip *chip = achip->chip; |
| 821 | struct acpi_resource_gpio *agpio; |
| 822 | struct acpi_resource *ares; |
| 823 | int pin_index = (int)address; |
| 824 | acpi_status status; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 825 | int length; |
| 826 | int i; |
| 827 | |
| 828 | status = acpi_buffer_to_resource(achip->conn_info.connection, |
| 829 | achip->conn_info.length, &ares); |
| 830 | if (ACPI_FAILURE(status)) |
| 831 | return status; |
| 832 | |
| 833 | if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) { |
| 834 | ACPI_FREE(ares); |
| 835 | return AE_BAD_PARAMETER; |
| 836 | } |
| 837 | |
| 838 | agpio = &ares->data.gpio; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 839 | |
| 840 | if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT && |
| 841 | function == ACPI_WRITE)) { |
| 842 | ACPI_FREE(ares); |
| 843 | return AE_BAD_PARAMETER; |
| 844 | } |
| 845 | |
| 846 | length = min(agpio->pin_table_length, (u16)(pin_index + bits)); |
| 847 | for (i = pin_index; i < length; ++i) { |
Qipeng Zha | a481162 | 2015-04-10 06:25:10 +0800 | [diff] [blame] | 848 | int pin = agpio->pin_table[i]; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 849 | struct acpi_gpio_connection *conn; |
| 850 | struct gpio_desc *desc; |
| 851 | bool found; |
| 852 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 853 | mutex_lock(&achip->conn_lock); |
| 854 | |
| 855 | found = false; |
| 856 | list_for_each_entry(conn, &achip->conns, node) { |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 857 | if (conn->pin == pin) { |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 858 | found = true; |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 859 | desc = conn->desc; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | } |
Mika Westerberg | c103a10 | 2015-10-30 12:02:05 +0200 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * The same GPIO can be shared between operation region and |
| 866 | * event but only if the access here is ACPI_READ. In that |
| 867 | * case we "borrow" the event GPIO instead. |
| 868 | */ |
| 869 | if (!found && agpio->sharable == ACPI_SHARED && |
| 870 | function == ACPI_READ) { |
| 871 | struct acpi_gpio_event *event; |
| 872 | |
| 873 | list_for_each_entry(event, &achip->events, node) { |
| 874 | if (event->pin == pin) { |
| 875 | desc = event->desc; |
| 876 | found = true; |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 882 | if (!found) { |
Andy Shevchenko | 2eca25a | 2017-05-23 20:03:22 +0300 | [diff] [blame] | 883 | enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio); |
| 884 | const char *label = "ACPI:OpRegion"; |
| 885 | int err; |
| 886 | |
| 887 | desc = gpiochip_request_own_desc(chip, pin, label); |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 888 | if (IS_ERR(desc)) { |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 889 | status = AE_ERROR; |
| 890 | mutex_unlock(&achip->conn_lock); |
| 891 | goto out; |
| 892 | } |
| 893 | |
Andy Shevchenko | 2eca25a | 2017-05-23 20:03:22 +0300 | [diff] [blame] | 894 | err = gpiod_configure_flags(desc, label, 0, flags); |
| 895 | if (err < 0) { |
| 896 | status = AE_NOT_CONFIGURED; |
| 897 | gpiochip_free_own_desc(desc); |
| 898 | mutex_unlock(&achip->conn_lock); |
| 899 | goto out; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
| 903 | if (!conn) { |
| 904 | status = AE_NO_MEMORY; |
| 905 | gpiochip_free_own_desc(desc); |
| 906 | mutex_unlock(&achip->conn_lock); |
| 907 | goto out; |
| 908 | } |
| 909 | |
Alexandre Courbot | e46cf32 | 2014-08-19 10:06:08 -0700 | [diff] [blame] | 910 | conn->pin = pin; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 911 | conn->desc = desc; |
| 912 | list_add_tail(&conn->node, &achip->conns); |
| 913 | } |
| 914 | |
| 915 | mutex_unlock(&achip->conn_lock); |
| 916 | |
| 917 | if (function == ACPI_WRITE) |
Aaron Lu | dc62b56 | 2014-05-20 17:07:38 +0800 | [diff] [blame] | 918 | gpiod_set_raw_value_cansleep(desc, |
| 919 | !!((1 << i) & *value)); |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 920 | else |
Aaron Lu | dc62b56 | 2014-05-20 17:07:38 +0800 | [diff] [blame] | 921 | *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i; |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | out: |
| 925 | ACPI_FREE(ares); |
| 926 | return status; |
| 927 | } |
| 928 | |
| 929 | static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip) |
| 930 | { |
| 931 | struct gpio_chip *chip = achip->chip; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 932 | acpi_handle handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 933 | acpi_status status; |
| 934 | |
| 935 | INIT_LIST_HEAD(&achip->conns); |
| 936 | mutex_init(&achip->conn_lock); |
| 937 | status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, |
| 938 | acpi_gpio_adr_space_handler, |
| 939 | NULL, achip); |
| 940 | if (ACPI_FAILURE(status)) |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 941 | dev_err(chip->parent, |
| 942 | "Failed to install GPIO OpRegion handler\n"); |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip) |
| 946 | { |
| 947 | struct gpio_chip *chip = achip->chip; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 948 | acpi_handle handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 949 | struct acpi_gpio_connection *conn, *tmp; |
| 950 | acpi_status status; |
| 951 | |
| 952 | status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, |
| 953 | acpi_gpio_adr_space_handler); |
| 954 | if (ACPI_FAILURE(status)) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 955 | dev_err(chip->parent, |
| 956 | "Failed to remove GPIO OpRegion handler\n"); |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 957 | return; |
| 958 | } |
| 959 | |
| 960 | list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) { |
| 961 | gpiochip_free_own_desc(conn->desc); |
| 962 | list_del(&conn->node); |
| 963 | kfree(conn); |
| 964 | } |
| 965 | } |
| 966 | |
Wei Yongjun | 550a953 | 2016-10-29 16:13:12 +0000 | [diff] [blame] | 967 | static struct gpio_desc *acpi_gpiochip_parse_own_gpio( |
| 968 | struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode, |
| 969 | const char **name, unsigned int *lflags, unsigned int *dflags) |
Mika Westerberg | c80f1ba | 2016-10-21 17:21:30 +0300 | [diff] [blame] | 970 | { |
| 971 | struct gpio_chip *chip = achip->chip; |
| 972 | struct gpio_desc *desc; |
| 973 | u32 gpios[2]; |
| 974 | int ret; |
| 975 | |
Arnd Bergmann | c82064f | 2016-11-08 14:40:06 +0100 | [diff] [blame] | 976 | *lflags = 0; |
| 977 | *dflags = 0; |
| 978 | *name = NULL; |
| 979 | |
Mika Westerberg | c80f1ba | 2016-10-21 17:21:30 +0300 | [diff] [blame] | 980 | ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, |
| 981 | ARRAY_SIZE(gpios)); |
| 982 | if (ret < 0) |
| 983 | return ERR_PTR(ret); |
| 984 | |
Mika Westerberg | 03c4749 | 2017-11-27 16:54:42 +0300 | [diff] [blame] | 985 | desc = gpiochip_get_desc(chip, gpios[0]); |
Mika Westerberg | c80f1ba | 2016-10-21 17:21:30 +0300 | [diff] [blame] | 986 | if (IS_ERR(desc)) |
| 987 | return desc; |
| 988 | |
Mika Westerberg | c80f1ba | 2016-10-21 17:21:30 +0300 | [diff] [blame] | 989 | if (gpios[1]) |
| 990 | *lflags |= GPIO_ACTIVE_LOW; |
| 991 | |
| 992 | if (fwnode_property_present(fwnode, "input")) |
| 993 | *dflags |= GPIOD_IN; |
| 994 | else if (fwnode_property_present(fwnode, "output-low")) |
| 995 | *dflags |= GPIOD_OUT_LOW; |
| 996 | else if (fwnode_property_present(fwnode, "output-high")) |
| 997 | *dflags |= GPIOD_OUT_HIGH; |
| 998 | else |
| 999 | return ERR_PTR(-EINVAL); |
| 1000 | |
| 1001 | fwnode_property_read_string(fwnode, "line-name", name); |
| 1002 | |
| 1003 | return desc; |
| 1004 | } |
| 1005 | |
| 1006 | static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip) |
| 1007 | { |
| 1008 | struct gpio_chip *chip = achip->chip; |
| 1009 | struct fwnode_handle *fwnode; |
| 1010 | |
| 1011 | device_for_each_child_node(chip->parent, fwnode) { |
| 1012 | unsigned int lflags, dflags; |
| 1013 | struct gpio_desc *desc; |
| 1014 | const char *name; |
| 1015 | int ret; |
| 1016 | |
| 1017 | if (!fwnode_property_present(fwnode, "gpio-hog")) |
| 1018 | continue; |
| 1019 | |
| 1020 | desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name, |
| 1021 | &lflags, &dflags); |
| 1022 | if (IS_ERR(desc)) |
| 1023 | continue; |
| 1024 | |
| 1025 | ret = gpiod_hog(desc, name, lflags, dflags); |
| 1026 | if (ret) { |
| 1027 | dev_err(chip->parent, "Failed to hog GPIO\n"); |
Wei Yongjun | 1b6998c | 2016-10-29 16:11:57 +0000 | [diff] [blame] | 1028 | fwnode_handle_put(fwnode); |
Mika Westerberg | c80f1ba | 2016-10-21 17:21:30 +0300 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 1034 | void acpi_gpiochip_add(struct gpio_chip *chip) |
| 1035 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1036 | struct acpi_gpio_chip *acpi_gpio; |
| 1037 | acpi_handle handle; |
| 1038 | acpi_status status; |
| 1039 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1040 | if (!chip || !chip->parent) |
Mika Westerberg | e9595f8 | 2014-03-31 15:16:49 +0300 | [diff] [blame] | 1041 | return; |
| 1042 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1043 | handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1044 | if (!handle) |
| 1045 | return; |
| 1046 | |
| 1047 | acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL); |
| 1048 | if (!acpi_gpio) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1049 | dev_err(chip->parent, |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1050 | "Failed to allocate memory for ACPI GPIO chip\n"); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | acpi_gpio->chip = chip; |
Mika Westerberg | c103a10 | 2015-10-30 12:02:05 +0200 | [diff] [blame] | 1055 | INIT_LIST_HEAD(&acpi_gpio->events); |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 1056 | INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1057 | |
| 1058 | status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio); |
| 1059 | if (ACPI_FAILURE(status)) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1060 | dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n"); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1061 | kfree(acpi_gpio); |
| 1062 | return; |
| 1063 | } |
| 1064 | |
Mika Westerberg | 4035cc1 | 2016-10-21 17:21:32 +0300 | [diff] [blame] | 1065 | if (!chip->names) |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 1066 | devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent)); |
Mika Westerberg | 4035cc1 | 2016-10-21 17:21:32 +0300 | [diff] [blame] | 1067 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 1068 | acpi_gpiochip_request_regions(acpi_gpio); |
Mika Westerberg | c80f1ba | 2016-10-21 17:21:30 +0300 | [diff] [blame] | 1069 | acpi_gpiochip_scan_gpios(acpi_gpio); |
Rui Zhang | 3f86a63 | 2016-06-15 08:47:51 +0200 | [diff] [blame] | 1070 | acpi_walk_dep_device_list(handle); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | void acpi_gpiochip_remove(struct gpio_chip *chip) |
| 1074 | { |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1075 | struct acpi_gpio_chip *acpi_gpio; |
| 1076 | acpi_handle handle; |
| 1077 | acpi_status status; |
| 1078 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1079 | if (!chip || !chip->parent) |
Mika Westerberg | e9595f8 | 2014-03-31 15:16:49 +0300 | [diff] [blame] | 1080 | return; |
| 1081 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1082 | handle = ACPI_HANDLE(chip->parent); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1083 | if (!handle) |
| 1084 | return; |
| 1085 | |
| 1086 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); |
| 1087 | if (ACPI_FAILURE(status)) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1088 | dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n"); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1089 | return; |
| 1090 | } |
| 1091 | |
Mika Westerberg | 473ed7b | 2014-03-14 17:58:07 +0200 | [diff] [blame] | 1092 | acpi_gpiochip_free_regions(acpi_gpio); |
Mika Westerberg | aa92b6f | 2014-03-10 14:54:51 +0200 | [diff] [blame] | 1093 | |
| 1094 | acpi_detach_data(handle, acpi_gpio_chip_dh); |
| 1095 | kfree(acpi_gpio); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 1096 | } |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1097 | |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 1098 | static int acpi_gpio_package_count(const union acpi_object *obj) |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1099 | { |
| 1100 | const union acpi_object *element = obj->package.elements; |
| 1101 | const union acpi_object *end = element + obj->package.count; |
| 1102 | unsigned int count = 0; |
| 1103 | |
| 1104 | while (element < end) { |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 1105 | switch (element->type) { |
| 1106 | case ACPI_TYPE_LOCAL_REFERENCE: |
| 1107 | element += 3; |
| 1108 | /* Fallthrough */ |
| 1109 | case ACPI_TYPE_INTEGER: |
| 1110 | element++; |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1111 | count++; |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 1112 | break; |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1113 | |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 1114 | default: |
| 1115 | return -EPROTO; |
| 1116 | } |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1117 | } |
Mika Westerberg | 6f7194a | 2016-10-21 17:21:29 +0300 | [diff] [blame] | 1118 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1119 | return count; |
| 1120 | } |
| 1121 | |
| 1122 | static int acpi_find_gpio_count(struct acpi_resource *ares, void *data) |
| 1123 | { |
| 1124 | unsigned int *count = data; |
| 1125 | |
| 1126 | if (ares->type == ACPI_RESOURCE_TYPE_GPIO) |
| 1127 | *count += ares->data.gpio.pin_table_length; |
| 1128 | |
| 1129 | return 1; |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * acpi_gpio_count - return the number of GPIOs associated with a |
| 1134 | * device / function or -ENOENT if no GPIO has been |
| 1135 | * assigned to the requested function. |
| 1136 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 1137 | * @con_id: function within the GPIO consumer |
| 1138 | */ |
| 1139 | int acpi_gpio_count(struct device *dev, const char *con_id) |
| 1140 | { |
| 1141 | struct acpi_device *adev = ACPI_COMPANION(dev); |
| 1142 | const union acpi_object *obj; |
| 1143 | const struct acpi_gpio_mapping *gm; |
| 1144 | int count = -ENOENT; |
| 1145 | int ret; |
| 1146 | char propname[32]; |
| 1147 | unsigned int i; |
| 1148 | |
| 1149 | /* Try first from _DSD */ |
| 1150 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
Andy Shevchenko | 9e66504 | 2017-05-23 20:03:17 +0300 | [diff] [blame] | 1151 | if (con_id) |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1152 | snprintf(propname, sizeof(propname), "%s-%s", |
| 1153 | con_id, gpio_suffixes[i]); |
| 1154 | else |
| 1155 | snprintf(propname, sizeof(propname), "%s", |
| 1156 | gpio_suffixes[i]); |
| 1157 | |
| 1158 | ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY, |
| 1159 | &obj); |
| 1160 | if (ret == 0) { |
| 1161 | if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) |
| 1162 | count = 1; |
| 1163 | else if (obj->type == ACPI_TYPE_PACKAGE) |
| 1164 | count = acpi_gpio_package_count(obj); |
| 1165 | } else if (adev->driver_gpios) { |
| 1166 | for (gm = adev->driver_gpios; gm->name; gm++) |
| 1167 | if (strcmp(propname, gm->name) == 0) { |
| 1168 | count = gm->size; |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
Andy Shevchenko | 4ed5501 | 2017-02-20 18:15:46 +0200 | [diff] [blame] | 1172 | if (count > 0) |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | /* Then from plain _CRS GPIOs */ |
| 1177 | if (count < 0) { |
| 1178 | struct list_head resource_list; |
| 1179 | unsigned int crs_count = 0; |
| 1180 | |
Andy Shevchenko | 6fe9da4 | 2017-05-23 20:03:20 +0300 | [diff] [blame] | 1181 | if (!acpi_can_fallback_to_crs(adev, con_id)) |
| 1182 | return count; |
| 1183 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1184 | INIT_LIST_HEAD(&resource_list); |
| 1185 | acpi_dev_get_resources(adev, &resource_list, |
| 1186 | acpi_find_gpio_count, &crs_count); |
| 1187 | acpi_dev_free_resource_list(&resource_list); |
| 1188 | if (crs_count > 0) |
| 1189 | count = crs_count; |
| 1190 | } |
Andy Shevchenko | 4ed5501 | 2017-02-20 18:15:46 +0200 | [diff] [blame] | 1191 | return count ? count : -ENOENT; |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1192 | } |
Dmitry Torokhov | 10cf489 | 2015-11-11 11:45:30 -0800 | [diff] [blame] | 1193 | |
Dmitry Torokhov | 10cf489 | 2015-11-11 11:45:30 -0800 | [diff] [blame] | 1194 | bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id) |
| 1195 | { |
Dmitry Torokhov | 10cf489 | 2015-11-11 11:45:30 -0800 | [diff] [blame] | 1196 | /* Never allow fallback if the device has properties */ |
Mika Westerberg | 5f5e489 | 2018-09-27 16:57:05 -0500 | [diff] [blame] | 1197 | if (acpi_dev_has_props(adev) || adev->driver_gpios) |
Dmitry Torokhov | 10cf489 | 2015-11-11 11:45:30 -0800 | [diff] [blame] | 1198 | return false; |
| 1199 | |
Andy Shevchenko | f10e4bf | 2017-05-23 20:03:19 +0300 | [diff] [blame] | 1200 | return con_id == NULL; |
Dmitry Torokhov | 10cf489 | 2015-11-11 11:45:30 -0800 | [diff] [blame] | 1201 | } |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 1202 | |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 1203 | /* Run deferred acpi_gpiochip_request_interrupts() */ |
| 1204 | static int acpi_gpio_handle_deferred_request_interrupts(void) |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 1205 | { |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 1206 | struct acpi_gpio_chip *acpi_gpio, *tmp; |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 1207 | |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 1208 | mutex_lock(&acpi_gpio_deferred_req_irqs_lock); |
| 1209 | list_for_each_entry_safe(acpi_gpio, tmp, |
| 1210 | &acpi_gpio_deferred_req_irqs_list, |
| 1211 | deferred_req_irqs_list_entry) { |
| 1212 | acpi_handle handle; |
| 1213 | |
| 1214 | handle = ACPI_HANDLE(acpi_gpio->chip->parent); |
| 1215 | acpi_walk_resources(handle, "_AEI", |
| 1216 | acpi_gpiochip_request_interrupt, acpi_gpio); |
| 1217 | |
| 1218 | list_del_init(&acpi_gpio->deferred_req_irqs_list_entry); |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 1219 | } |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 1220 | |
| 1221 | acpi_gpio_deferred_req_irqs_done = true; |
| 1222 | mutex_unlock(&acpi_gpio_deferred_req_irqs_lock); |
Benjamin Tissoires | ca876c7 | 2018-07-12 17:25:06 +0200 | [diff] [blame] | 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | /* We must use _sync so that this runs after the first deferred_probe run */ |
Hans de Goede | 78d3a92 | 2018-08-14 16:07:03 +0200 | [diff] [blame] | 1227 | late_initcall_sync(acpi_gpio_handle_deferred_request_interrupts); |