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