blob: c7a0e56593e77974a79e21d006f137c3e7af98b6 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Mathias Nymane29482e2012-11-30 12:37:36 +01002/*
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 Nymane29482e2012-11-30 12:37:36 +01008 */
9
Hans de Goede61f7f7c2019-08-27 22:28:35 +020010#include <linux/dmi.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010011#include <linux/errno.h>
Mika Westerberg936e15d2013-10-10 11:01:08 +030012#include <linux/gpio/consumer.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020013#include <linux/gpio/driver.h>
Linus Walleij031ba282016-10-03 10:40:03 +020014#include <linux/gpio/machine.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010015#include <linux/export.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010016#include <linux/acpi.h>
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020017#include <linux/interrupt.h>
Mika Westerberg473ed7b2014-03-14 17:58:07 +020018#include <linux/mutex.h>
Mika Westerberg354567e2014-11-03 13:01:32 +020019#include <linux/pinctrl/pinctrl.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010020
Mika Westerberg5ccff852014-01-08 12:40:56 +020021#include "gpiolib.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030022#include "gpiolib-acpi.h"
Mika Westerberg5ccff852014-01-08 12:40:56 +020023
Hans de Goede61f7f7c2019-08-27 22:28:35 +020024static int run_edge_events_on_boot = -1;
25module_param(run_edge_events_on_boot, int, 0444);
26MODULE_PARM_DESC(run_edge_events_on_boot,
27 "Run edge _AEI event-handlers at boot: 0=no, 1=yes, -1=auto");
28
Hans de Goede2ccb21f2020-03-02 12:12:23 +010029static char *ignore_wake;
30module_param(ignore_wake, charp, 0444);
31MODULE_PARM_DESC(ignore_wake,
32 "controller@pin combos on which to ignore the ACPI wake flag "
33 "ignore_wake=controller@pin[,controller@pin[,...]]");
34
35struct acpi_gpiolib_dmi_quirk {
36 bool no_edge_events_on_boot;
37 char *ignore_wake;
38};
Hans de Goedeaa23ca32020-01-05 17:03:57 +010039
Hans de Goedee59f5e02018-11-28 17:57:55 +010040/**
41 * struct acpi_gpio_event - ACPI GPIO event handler data
42 *
43 * @node: list-entry of the events list of the struct acpi_gpio_chip
44 * @handle: handle of ACPI method to execute when the IRQ triggers
Andy Shevchenko85edcd02019-03-30 00:33:45 +030045 * @handler: handler function to pass to request_irq() when requesting the IRQ
46 * @pin: GPIO pin number on the struct gpio_chip
47 * @irq: Linux IRQ number for the event, for request_irq() / free_irq()
48 * @irqflags: flags to pass to request_irq() when requesting the IRQ
Hans de Goedee59f5e02018-11-28 17:57:55 +010049 * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source
Andy Shevchenko85edcd02019-03-30 00:33:45 +030050 * @irq_requested:True if request_irq() has been done
51 * @desc: struct gpio_desc for the GPIO pin for this event
Hans de Goedee59f5e02018-11-28 17:57:55 +010052 */
Mika Westerberg4b01a142014-03-10 14:54:52 +020053struct acpi_gpio_event {
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020054 struct list_head node;
Mika Westerberg4b01a142014-03-10 14:54:52 +020055 acpi_handle handle;
Hans de Goedee59f5e02018-11-28 17:57:55 +010056 irq_handler_t handler;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020057 unsigned int pin;
58 unsigned int irq;
Hans de Goedee59f5e02018-11-28 17:57:55 +010059 unsigned long irqflags;
60 bool irq_is_wake;
61 bool irq_requested;
Alexandre Courbote46cf322014-08-19 10:06:08 -070062 struct gpio_desc *desc;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020063};
64
Mika Westerberg473ed7b2014-03-14 17:58:07 +020065struct acpi_gpio_connection {
66 struct list_head node;
Alexandre Courbote46cf322014-08-19 10:06:08 -070067 unsigned int pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +020068 struct gpio_desc *desc;
69};
70
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020071struct acpi_gpio_chip {
Mika Westerberg473ed7b2014-03-14 17:58:07 +020072 /*
73 * ACPICA requires that the first field of the context parameter
74 * passed to acpi_install_address_space_handler() is large enough
75 * to hold struct acpi_connection_info.
76 */
77 struct acpi_connection_info conn_info;
78 struct list_head conns;
79 struct mutex conn_lock;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020080 struct gpio_chip *chip;
Mika Westerberg4b01a142014-03-10 14:54:52 +020081 struct list_head events;
Hans de Goede78d3a922018-08-14 16:07:03 +020082 struct list_head deferred_req_irqs_list_entry;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020083};
84
Hans de Goede78d3a922018-08-14 16:07:03 +020085/*
Andy Shevchenko85edcd02019-03-30 00:33:45 +030086 * For GPIO chips which call acpi_gpiochip_request_interrupts() before late_init
Hans de Goedee59f5e02018-11-28 17:57:55 +010087 * (so builtin drivers) we register the ACPI GpioInt IRQ handlers from a
Andy Shevchenko85edcd02019-03-30 00:33:45 +030088 * late_initcall_sync() handler, so that other builtin drivers can register their
89 * OpRegions before the event handlers can run. This list contains GPIO chips
Hans de Goedee59f5e02018-11-28 17:57:55 +010090 * for which the acpi_gpiochip_request_irqs() call has been deferred.
Hans de Goede78d3a922018-08-14 16:07:03 +020091 */
92static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
93static LIST_HEAD(acpi_gpio_deferred_req_irqs_list);
94static bool acpi_gpio_deferred_req_irqs_done;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +020095
Mathias Nymane29482e2012-11-30 12:37:36 +010096static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
97{
Andy Shevchenkoadb51512021-10-14 16:47:56 +030098 return gc->parent && device_match_acpi_handle(gc->parent, data);
Mathias Nymane29482e2012-11-30 12:37:36 +010099}
100
101/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300102 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +0100103 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
104 * @pin: ACPI GPIO pin number (0-based, controller-relative)
105 *
Mika Westerbergf35bbf62015-06-10 16:05:05 +0300106 * 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 Shevchenko85edcd02019-03-30 00:33:45 +0300108 * controller does not have GPIO chip registered at the moment. This is to
Mika Westerbergf35bbf62015-06-10 16:05:05 +0300109 * support probe deferral.
Mathias Nymane29482e2012-11-30 12:37:36 +0100110 */
Mika Westerberg936e15d2013-10-10 11:01:08 +0300111static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +0100112{
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 Westerberg936e15d2013-10-10 11:01:08 +0300119 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +0100120
121 chip = gpiochip_find(handle, acpi_gpiochip_find);
122 if (!chip)
Mika Westerbergf35bbf62015-06-10 16:05:05 +0300123 return ERR_PTR(-EPROBE_DEFER);
Mathias Nymane29482e2012-11-30 12:37:36 +0100124
Mika Westerberg03c47492017-11-27 16:54:42 +0300125 return gpiochip_get_desc(chip, pin);
Mathias Nymane29482e2012-11-30 12:37:36 +0100126}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200127
Daniel Scally43582f22021-06-03 23:40:04 +0100128/**
129 * acpi_get_and_request_gpiod - Translate ACPI GPIO pin to GPIO descriptor and
130 * hold a refcount to the GPIO device.
131 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
132 * @pin: ACPI GPIO pin number (0-based, controller-relative)
133 * @label: Label to pass to gpiod_request()
134 *
135 * This function is a simple pass-through to acpi_get_gpiod(), except that
136 * as it is intended for use outside of the GPIO layer (in a similar fashion to
137 * gpiod_get_index() for example) it also holds a reference to the GPIO device.
138 */
139struct gpio_desc *acpi_get_and_request_gpiod(char *path, int pin, char *label)
140{
141 struct gpio_desc *gpio;
142 int ret;
143
144 gpio = acpi_get_gpiod(path, pin);
145 if (IS_ERR(gpio))
146 return gpio;
147
148 ret = gpiod_request(gpio, label);
149 if (ret)
150 return ERR_PTR(ret);
151
152 return gpio;
153}
154EXPORT_SYMBOL_GPL(acpi_get_and_request_gpiod);
155
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200156static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
157{
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200158 struct acpi_gpio_event *event = data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200159
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200160 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200161
162 return IRQ_HANDLED;
163}
164
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200165static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
166{
Mika Westerberg4b01a142014-03-10 14:54:52 +0200167 struct acpi_gpio_event *event = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200168
Mika Westerberg4b01a142014-03-10 14:54:52 +0200169 acpi_execute_simple_method(event->handle, NULL, event->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200170
171 return IRQ_HANDLED;
172}
173
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200174static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200175{
176 /* The address of this function is used as a key. */
177}
178
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300179bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
180 struct acpi_resource_gpio **agpio)
181{
182 struct acpi_resource_gpio *gpio;
183
184 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
185 return false;
186
187 gpio = &ares->data.gpio;
188 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
189 return false;
190
191 *agpio = gpio;
192 return true;
193}
194EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
195
Daniel Scally043d7f02021-06-03 23:40:05 +0100196/**
197 * acpi_gpio_get_io_resource - Fetch details of an ACPI resource if it is a GPIO
198 * I/O resource or return False if not.
199 * @ares: Pointer to the ACPI resource to fetch
200 * @agpio: Pointer to a &struct acpi_resource_gpio to store the output pointer
201 */
202bool acpi_gpio_get_io_resource(struct acpi_resource *ares,
203 struct acpi_resource_gpio **agpio)
204{
205 struct acpi_resource_gpio *gpio;
206
207 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
208 return false;
209
210 gpio = &ares->data.gpio;
211 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_IO)
212 return false;
213
214 *agpio = gpio;
215 return true;
216}
217EXPORT_SYMBOL_GPL(acpi_gpio_get_io_resource);
218
Hans de Goedee59f5e02018-11-28 17:57:55 +0100219static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
220 struct acpi_gpio_event *event)
221{
Andy Shevchenkobe3dc152021-11-23 12:18:37 +0200222 struct device *parent = acpi_gpio->chip->parent;
Hans de Goedee59f5e02018-11-28 17:57:55 +0100223 int ret, value;
224
225 ret = request_threaded_irq(event->irq, NULL, event->handler,
Yang Li6e5d5792021-02-23 16:35:58 +0800226 event->irqflags | IRQF_ONESHOT, "ACPI:Event", event);
Hans de Goedee59f5e02018-11-28 17:57:55 +0100227 if (ret) {
Andy Shevchenkobe3dc152021-11-23 12:18:37 +0200228 dev_err(parent, "Failed to setup interrupt handler for %d\n", event->irq);
Hans de Goedee59f5e02018-11-28 17:57:55 +0100229 return;
230 }
231
232 if (event->irq_is_wake)
233 enable_irq_wake(event->irq);
234
235 event->irq_requested = true;
236
237 /* Make sure we trigger the initial state of edge-triggered IRQs */
Hans de Goede61f7f7c2019-08-27 22:28:35 +0200238 if (run_edge_events_on_boot &&
239 (event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) {
240 value = gpiod_get_raw_value_cansleep(event->desc);
241 if (((event->irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
242 ((event->irqflags & IRQF_TRIGGER_FALLING) && value == 0))
243 event->handler(event->irq, event);
244 }
Hans de Goedee59f5e02018-11-28 17:57:55 +0100245}
246
247static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
248{
249 struct acpi_gpio_event *event;
250
251 list_for_each_entry(event, &acpi_gpio->events, node)
252 acpi_gpiochip_request_irq(acpi_gpio, event);
253}
254
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200255static enum gpiod_flags
Vasile-Laurentiu Stanimir56f70582020-10-01 20:12:12 +0300256acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio, int polarity)
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200257{
Andy Shevchenkobca40482020-11-09 22:53:28 +0200258 /* GpioInt() implies input configuration */
259 if (agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
260 return GPIOD_IN;
261
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200262 switch (agpio->io_restriction) {
263 case ACPI_IO_RESTRICT_INPUT:
264 return GPIOD_IN;
265 case ACPI_IO_RESTRICT_OUTPUT:
266 /*
267 * ACPI GPIO resources don't contain an initial value for the
268 * GPIO. Therefore we deduce that value from the pull field
Vasile-Laurentiu Stanimir56f70582020-10-01 20:12:12 +0300269 * and the polarity instead. If the pin is pulled up we assume
270 * default to be high, if it is pulled down we assume default
271 * to be low, otherwise we leave pin untouched. For active low
272 * polarity values will be switched. See also
273 * Documentation/firmware-guide/acpi/gpio-properties.rst.
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200274 */
275 switch (agpio->pin_config) {
276 case ACPI_PIN_CONFIG_PULLUP:
Vasile-Laurentiu Stanimir56f70582020-10-01 20:12:12 +0300277 return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH;
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200278 case ACPI_PIN_CONFIG_PULLDOWN:
Vasile-Laurentiu Stanimir56f70582020-10-01 20:12:12 +0300279 return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200280 default:
281 break;
282 }
Linus Walleij40b37002020-12-09 15:17:24 +0100283 break;
Andy Shevchenko1a81f192020-11-09 22:53:27 +0200284 default:
285 break;
286 }
287
288 /*
289 * Assume that the BIOS has configured the direction and pull
290 * accordingly.
291 */
292 return GPIOD_ASIS;
293}
294
Andy Shevchenko2e2b4962020-11-11 23:35:33 +0200295static struct gpio_desc *acpi_request_own_gpiod(struct gpio_chip *chip,
296 struct acpi_resource_gpio *agpio,
297 unsigned int index,
298 const char *label)
299{
300 int polarity = GPIO_ACTIVE_HIGH;
301 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio, polarity);
302 unsigned int pin = agpio->pin_table[index];
303 struct gpio_desc *desc;
304 int ret;
305
306 desc = gpiochip_request_own_desc(chip, pin, label, polarity, flags);
307 if (IS_ERR(desc))
308 return desc;
309
310 ret = gpio_set_debounce_timeout(desc, agpio->debounce_timeout);
311 if (ret)
Hans de Goedecef0d022021-08-16 12:41:19 +0200312 dev_warn(chip->parent,
313 "Failed to set debounce-timeout for pin 0x%04X, err %d\n",
314 pin, ret);
Andy Shevchenko2e2b4962020-11-11 23:35:33 +0200315
Hans de Goedecef0d022021-08-16 12:41:19 +0200316 return desc;
Andy Shevchenko2e2b4962020-11-11 23:35:33 +0200317}
318
Hans de Goede2ccb21f2020-03-02 12:12:23 +0100319static bool acpi_gpio_in_ignore_list(const char *controller_in, int pin_in)
320{
321 const char *controller, *pin_str;
322 int len, pin;
323 char *endp;
324
325 controller = ignore_wake;
326 while (controller) {
327 pin_str = strchr(controller, '@');
328 if (!pin_str)
329 goto err;
330
331 len = pin_str - controller;
332 if (len == strlen(controller_in) &&
333 strncmp(controller, controller_in, len) == 0) {
334 pin = simple_strtoul(pin_str + 1, &endp, 10);
335 if (*endp != 0 && *endp != ',')
336 goto err;
337
338 if (pin == pin_in)
339 return true;
340 }
341
342 controller = strchr(controller, ',');
343 if (controller)
344 controller++;
345 }
346
347 return false;
348err:
Andy Shevchenkobe3dc152021-11-23 12:18:37 +0200349 pr_err_once("Error: Invalid value for gpiolib_acpi.ignore_wake: %s\n", ignore_wake);
Hans de Goede2ccb21f2020-03-02 12:12:23 +0100350 return false;
351}
352
353static bool acpi_gpio_irq_is_wake(struct device *parent,
354 struct acpi_resource_gpio *agpio)
355{
356 int pin = agpio->pin_table[0];
357
358 if (agpio->wake_capable != ACPI_WAKE_CAPABLE)
359 return false;
360
361 if (acpi_gpio_in_ignore_list(dev_name(parent), pin)) {
362 dev_info(parent, "Ignoring wakeup on pin %d\n", pin);
363 return false;
364 }
365
366 return true;
367}
368
Hans de Goeded4fc46f2019-11-14 11:26:00 +0100369/* Always returns AE_OK so that we keep looping over the resources */
Hans de Goedee59f5e02018-11-28 17:57:55 +0100370static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
371 void *context)
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200372{
373 struct acpi_gpio_chip *acpi_gpio = context;
374 struct gpio_chip *chip = acpi_gpio->chip;
375 struct acpi_resource_gpio *agpio;
376 acpi_handle handle, evt_handle;
377 struct acpi_gpio_event *event;
378 irq_handler_t handler = NULL;
379 struct gpio_desc *desc;
Hans de Goedee59f5e02018-11-28 17:57:55 +0100380 int ret, pin, irq;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200381
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300382 if (!acpi_gpio_get_irq_resource(ares, &agpio))
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200383 return AE_OK;
384
Linus Walleij58383c782015-11-04 09:56:26 +0100385 handle = ACPI_HANDLE(chip->parent);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200386 pin = agpio->pin_table[0];
387
388 if (pin <= 255) {
389 char ev_name[5];
Arnd Bergmanne40a3ae2017-09-06 17:47:45 +0200390 sprintf(ev_name, "_%c%02hhX",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200391 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
392 pin);
393 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
394 handler = acpi_gpio_irq_handler;
395 }
396 if (!handler) {
397 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
398 handler = acpi_gpio_irq_handler_evt;
399 }
400 if (!handler)
Hans de Goedec06632e2017-06-23 09:26:13 +0200401 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200402
Andy Shevchenko2e2b4962020-11-11 23:35:33 +0200403 desc = acpi_request_own_gpiod(chip, agpio, 0, "ACPI:Event");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200404 if (IS_ERR(desc)) {
Hans de Goede3f86a7e2019-11-14 11:25:59 +0100405 dev_err(chip->parent,
406 "Failed to request GPIO for pin 0x%04X, err %ld\n",
407 pin, PTR_ERR(desc));
Hans de Goeded4fc46f2019-11-14 11:26:00 +0100408 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200409 }
410
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900411 ret = gpiochip_lock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200412 if (ret) {
Hans de Goede3f86a7e2019-11-14 11:25:59 +0100413 dev_err(chip->parent,
414 "Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
415 pin, ret);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200416 goto fail_free_desc;
417 }
418
419 irq = gpiod_to_irq(desc);
420 if (irq < 0) {
Hans de Goede3f86a7e2019-11-14 11:25:59 +0100421 dev_err(chip->parent,
422 "Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
423 pin, irq);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200424 goto fail_unlock_irq;
425 }
426
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200427 event = kzalloc(sizeof(*event), GFP_KERNEL);
428 if (!event)
429 goto fail_unlock_irq;
430
Hans de Goedee59f5e02018-11-28 17:57:55 +0100431 event->irqflags = IRQF_ONESHOT;
432 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
433 if (agpio->polarity == ACPI_ACTIVE_HIGH)
434 event->irqflags |= IRQF_TRIGGER_HIGH;
435 else
436 event->irqflags |= IRQF_TRIGGER_LOW;
437 } else {
438 switch (agpio->polarity) {
439 case ACPI_ACTIVE_HIGH:
440 event->irqflags |= IRQF_TRIGGER_RISING;
441 break;
442 case ACPI_ACTIVE_LOW:
443 event->irqflags |= IRQF_TRIGGER_FALLING;
444 break;
445 default:
446 event->irqflags |= IRQF_TRIGGER_RISING |
447 IRQF_TRIGGER_FALLING;
448 break;
449 }
450 }
451
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200452 event->handle = evt_handle;
Hans de Goedee59f5e02018-11-28 17:57:55 +0100453 event->handler = handler;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200454 event->irq = irq;
Hans de Goede2ccb21f2020-03-02 12:12:23 +0100455 event->irq_is_wake = acpi_gpio_irq_is_wake(chip->parent, agpio);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200456 event->pin = pin;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700457 event->desc = desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200458
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200459 list_add_tail(&event->node, &acpi_gpio->events);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200460
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200461 return AE_OK;
462
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200463fail_unlock_irq:
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900464 gpiochip_unlock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200465fail_free_desc:
466 gpiochip_free_own_desc(desc);
467
Hans de Goeded4fc46f2019-11-14 11:26:00 +0100468 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200469}
470
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200471/**
472 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300473 * @chip: GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200474 *
475 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
476 * handled by ACPI event methods which need to be called from the GPIO
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300477 * chip's interrupt handler. acpi_gpiochip_request_interrupts() finds out which
478 * GPIO pins have ACPI event methods and assigns interrupt handlers that calls
479 * the ACPI event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200480 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300481void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200482{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300483 struct acpi_gpio_chip *acpi_gpio;
484 acpi_handle handle;
485 acpi_status status;
Hans de Goede78d3a922018-08-14 16:07:03 +0200486 bool defer;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200487
Linus Walleij58383c782015-11-04 09:56:26 +0100488 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300489 return;
490
Linus Walleij58383c782015-11-04 09:56:26 +0100491 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300492 if (!handle)
493 return;
494
495 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
496 if (ACPI_FAILURE(status))
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200497 return;
498
Hans de Goedee59f5e02018-11-28 17:57:55 +0100499 acpi_walk_resources(handle, "_AEI",
500 acpi_gpiochip_alloc_event, acpi_gpio);
501
Hans de Goede78d3a922018-08-14 16:07:03 +0200502 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
503 defer = !acpi_gpio_deferred_req_irqs_done;
504 if (defer)
505 list_add(&acpi_gpio->deferred_req_irqs_list_entry,
506 &acpi_gpio_deferred_req_irqs_list);
507 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
508
509 if (defer)
510 return;
511
Hans de Goedee59f5e02018-11-28 17:57:55 +0100512 acpi_gpiochip_request_irqs(acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200513}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800514EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200515
Mika Westerberg70b534112013-10-10 11:01:07 +0300516/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200517 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300518 * @chip: GPIO chip
Mika Westerberg70b534112013-10-10 11:01:07 +0300519 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200520 * Free interrupts associated with GPIO ACPI event method for the given
521 * GPIO chip.
Mika Westerberg70b534112013-10-10 11:01:07 +0300522 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300523void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
Mika Westerberg70b534112013-10-10 11:01:07 +0300524{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300525 struct acpi_gpio_chip *acpi_gpio;
Mika Westerberg4b01a142014-03-10 14:54:52 +0200526 struct acpi_gpio_event *event, *ep;
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300527 acpi_handle handle;
528 acpi_status status;
Mika Westerberg70b534112013-10-10 11:01:07 +0300529
Linus Walleij58383c782015-11-04 09:56:26 +0100530 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300531 return;
532
Linus Walleij58383c782015-11-04 09:56:26 +0100533 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300534 if (!handle)
535 return;
536
537 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
538 if (ACPI_FAILURE(status))
Mika Westerberg70b534112013-10-10 11:01:07 +0300539 return;
540
Hans de Goede78d3a922018-08-14 16:07:03 +0200541 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
542 if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry))
543 list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
544 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
545
Mika Westerberg4b01a142014-03-10 14:54:52 +0200546 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Hans de Goedee59f5e02018-11-28 17:57:55 +0100547 if (event->irq_requested) {
548 if (event->irq_is_wake)
549 disable_irq_wake(event->irq);
Hans de Goede8a146fb2017-03-24 11:08:47 +0100550
Hans de Goedee59f5e02018-11-28 17:57:55 +0100551 free_irq(event->irq, event);
552 }
553
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900554 gpiochip_unlock_as_irq(chip, event->pin);
Hans de Goede86252322018-11-28 17:57:56 +0100555 gpiochip_free_own_desc(event->desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200556 list_del(&event->node);
557 kfree(event);
Mika Westerberg70b534112013-10-10 11:01:07 +0300558 }
Mika Westerberg70b534112013-10-10 11:01:07 +0300559}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800560EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
Mika Westerberg70b534112013-10-10 11:01:07 +0300561
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100562int acpi_dev_add_driver_gpios(struct acpi_device *adev,
563 const struct acpi_gpio_mapping *gpios)
564{
565 if (adev && gpios) {
566 adev->driver_gpios = gpios;
567 return 0;
568 }
569 return -EINVAL;
570}
571EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
572
Andy Shevchenko2838bf92019-07-30 13:43:37 +0300573void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
574{
575 if (adev)
576 adev->driver_gpios = NULL;
577}
578EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
579
Andy Shevchenko2ff64a82021-11-10 15:47:43 +0200580static void acpi_dev_release_driver_gpios(void *adev)
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200581{
Andy Shevchenko2ff64a82021-11-10 15:47:43 +0200582 acpi_dev_remove_driver_gpios(adev);
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200583}
584
585int devm_acpi_dev_add_driver_gpios(struct device *dev,
586 const struct acpi_gpio_mapping *gpios)
587{
Andy Shevchenko2ff64a82021-11-10 15:47:43 +0200588 struct acpi_device *adev = ACPI_COMPANION(dev);
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200589 int ret;
590
Andy Shevchenko2ff64a82021-11-10 15:47:43 +0200591 ret = acpi_dev_add_driver_gpios(adev, gpios);
592 if (ret)
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200593 return ret;
Andy Shevchenko2ff64a82021-11-10 15:47:43 +0200594
595 return devm_add_action_or_reset(dev, acpi_dev_release_driver_gpios, adev);
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200596}
597EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
598
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100599static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
600 const char *name, int index,
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300601 struct fwnode_reference_args *args,
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200602 unsigned int *quirks)
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100603{
604 const struct acpi_gpio_mapping *gm;
605
606 if (!adev->driver_gpios)
607 return false;
608
609 for (gm = adev->driver_gpios; gm->name; gm++)
610 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
611 const struct acpi_gpio_params *par = gm->data + index;
612
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300613 args->fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100614 args->args[0] = par->crs_entry_index;
615 args->args[1] = par->line_index;
616 args->args[2] = par->active_low;
617 args->nargs = 3;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200618
619 *quirks = gm->quirks;
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100620 return true;
621 }
622
623 return false;
624}
625
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200626static int
627__acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300628{
Hans de Goede72893f02018-12-31 21:55:21 +0100629 const enum gpiod_flags mask =
630 GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
631 GPIOD_FLAGS_BIT_DIR_VAL;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300632 int ret = 0;
633
634 /*
635 * Check if the BIOS has IoRestriction with explicitly set direction
636 * and update @flags accordingly. Otherwise use whatever caller asked
637 * for.
638 */
639 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
640 enum gpiod_flags diff = *flags ^ update;
641
642 /*
643 * Check if caller supplied incompatible GPIO initialization
644 * flags.
645 *
646 * Return %-EINVAL to notify that firmware has different
647 * settings and we are going to use them.
648 */
649 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
650 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
651 ret = -EINVAL;
Hans de Goede72893f02018-12-31 21:55:21 +0100652 *flags = (*flags & ~mask) | (update & mask);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300653 }
654 return ret;
655}
656
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200657int
658acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
659{
660 struct device *dev = &info->adev->dev;
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200661 enum gpiod_flags old = *flags;
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200662 int ret;
663
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200664 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
665 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
666 if (ret)
667 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
668 } else {
669 if (ret)
670 dev_dbg(dev, "Override GPIO initialization flags\n");
671 *flags = old;
672 }
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200673
674 return ret;
675}
676
Andy Shevchenko606be342019-04-10 18:39:20 +0300677int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
678 struct acpi_gpio_info *info)
679{
Andy Shevchenko2d3b6db2019-04-10 18:39:21 +0300680 switch (info->pin_config) {
681 case ACPI_PIN_CONFIG_PULLUP:
682 *lookupflags |= GPIO_PULL_UP;
683 break;
684 case ACPI_PIN_CONFIG_PULLDOWN:
685 *lookupflags |= GPIO_PULL_DOWN;
686 break;
687 default:
688 break;
689 }
690
Andy Shevchenko606be342019-04-10 18:39:20 +0300691 if (info->polarity == GPIO_ACTIVE_LOW)
692 *lookupflags |= GPIO_ACTIVE_LOW;
693
694 return 0;
695}
696
Mika Westerberg12028d22013-04-03 13:56:54 +0300697struct acpi_gpio_lookup {
698 struct acpi_gpio_info info;
699 int index;
Andy Shevchenko74301f22020-11-09 22:53:30 +0200700 u16 pin_index;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200701 bool active_low;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300702 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300703 int n;
704};
705
Linus Walleij031ba282016-10-03 10:40:03 +0200706static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
Mika Westerberg12028d22013-04-03 13:56:54 +0300707{
708 struct acpi_gpio_lookup *lookup = data;
709
710 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
711 return 1;
712
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200713 if (!lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300714 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200715 bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Andy Shevchenko62d52472021-02-25 18:33:18 +0200716 struct gpio_desc *desc;
Andy Shevchenko74301f22020-11-09 22:53:30 +0200717 u16 pin_index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100718
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200719 if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
720 lookup->index++;
721
722 if (lookup->n++ != lookup->index)
723 return 1;
724
725 pin_index = lookup->pin_index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100726 if (pin_index >= agpio->pin_table_length)
727 return 1;
Mika Westerberg12028d22013-04-03 13:56:54 +0300728
Andy Shevchenko62d52472021-02-25 18:33:18 +0200729 if (lookup->info.quirks & ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER)
730 desc = gpio_to_desc(agpio->pin_table[pin_index]);
731 else
732 desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100733 agpio->pin_table[pin_index]);
Andy Shevchenko62d52472021-02-25 18:33:18 +0200734 lookup->desc = desc;
Andy Shevchenko2d3b6db2019-04-10 18:39:21 +0300735 lookup->info.pin_config = agpio->pin_config;
Andy Shevchenko8dcb7a12020-11-09 22:53:26 +0200736 lookup->info.debounce = agpio->debounce_timeout;
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200737 lookup->info.gpioint = gpioint;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100738
739 /*
Andy Shevchenkoe567c352017-01-03 19:01:18 +0200740 * Polarity and triggering are only specified for GpioInt
741 * resource.
Christophe RICARD52044722015-12-23 23:25:34 +0100742 * Note: we expect here:
743 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
744 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100745 */
Christophe RICARD52044722015-12-23 23:25:34 +0100746 if (lookup->info.gpioint) {
747 lookup->info.polarity = agpio->polarity;
748 lookup->info.triggering = agpio->triggering;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300749 } else {
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200750 lookup->info.polarity = lookup->active_low;
Christophe RICARD52044722015-12-23 23:25:34 +0100751 }
Andy Shevchenkobca40482020-11-09 22:53:28 +0200752
753 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio, lookup->info.polarity);
Mika Westerberg12028d22013-04-03 13:56:54 +0300754 }
755
756 return 1;
757}
758
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200759static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
760 struct acpi_gpio_info *info)
761{
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200762 struct acpi_device *adev = lookup->info.adev;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200763 struct list_head res_list;
764 int ret;
765
766 INIT_LIST_HEAD(&res_list);
767
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200768 ret = acpi_dev_get_resources(adev, &res_list,
Linus Walleij031ba282016-10-03 10:40:03 +0200769 acpi_populate_gpio_lookup,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200770 lookup);
771 if (ret < 0)
772 return ret;
773
774 acpi_dev_free_resource_list(&res_list);
775
776 if (!lookup->desc)
777 return -ENOENT;
778
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200779 if (info)
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200780 *info = lookup->info;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200781 return 0;
782}
783
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200784static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200785 const char *propname, int index,
786 struct acpi_gpio_lookup *lookup)
787{
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300788 struct fwnode_reference_args args;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200789 unsigned int quirks = 0;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200790 int ret;
791
792 memset(&args, 0, sizeof(args));
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300793 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
794 &args);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200795 if (ret) {
796 struct acpi_device *adev = to_acpi_device_node(fwnode);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200797
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200798 if (!adev)
799 return ret;
800
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200801 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
802 &quirks))
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200803 return ret;
804 }
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200805 /*
806 * The property was found and resolved, so need to lookup the GPIO based
807 * on returned args.
808 */
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300809 if (!to_acpi_device_node(args.fwnode))
810 return -EINVAL;
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300811 if (args.nargs != 3)
812 return -EPROTO;
813
814 lookup->index = args.args[0];
815 lookup->pin_index = args.args[1];
816 lookup->active_low = !!args.args[2];
817
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300818 lookup->info.adev = to_acpi_device_node(args.fwnode);
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200819 lookup->info.quirks = quirks;
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300820
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200821 return 0;
822}
823
Mika Westerberg12028d22013-04-03 13:56:54 +0300824/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300825 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100826 * @adev: pointer to a ACPI device to get GPIO from
827 * @propname: Property name of the GPIO (optional)
Mika Westerberg12028d22013-04-03 13:56:54 +0300828 * @index: index of GpioIo/GpioInt resource (starting from %0)
829 * @info: info pointer to fill in (optional)
830 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100831 * Function goes through ACPI resources for @adev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300832 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300833 * and returns it. @index matches GpioIo/GpioInt resources only so if there
834 * are total %3 GPIO resources, the index goes from %0 to %2.
835 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100836 * If @propname is specified the GPIO is looked using device property. In
837 * that case @index is used to select the GPIO entry in the property value
838 * (in case of multiple).
839 *
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300840 * If the GPIO cannot be translated or there is an error, an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300841 * returned.
842 *
843 * Note: if the GPIO resource has multiple entries in the pin list, this
844 * function only returns the first.
845 */
Linus Walleij031ba282016-10-03 10:40:03 +0200846static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100847 const char *propname, int index,
Mika Westerberg936e15d2013-10-10 11:01:08 +0300848 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300849{
850 struct acpi_gpio_lookup lookup;
Mika Westerberg12028d22013-04-03 13:56:54 +0300851 int ret;
852
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100853 if (!adev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300854 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300855
856 memset(&lookup, 0, sizeof(lookup));
857 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300858
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100859 if (propname) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100860 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
861
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200862 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
863 propname, index, &lookup);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200864 if (ret)
865 return ERR_PTR(ret);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100866
Andy Shevchenko74301f22020-11-09 22:53:30 +0200867 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %u %u\n",
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200868 dev_name(&lookup.info.adev->dev), lookup.index,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200869 lookup.pin_index, lookup.active_low);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100870 } else {
871 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200872 lookup.info.adev = adev;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100873 }
874
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200875 ret = acpi_gpio_resource_lookup(&lookup, info);
876 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300877}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200878
Dmitry Torokhov4f78d912019-09-04 10:26:24 -0700879static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
880 const char *con_id)
881{
882 /* Never allow fallback if the device has properties */
883 if (acpi_dev_has_props(adev) || adev->driver_gpios)
884 return false;
885
886 return con_id == NULL;
887}
888
Linus Walleij031ba282016-10-03 10:40:03 +0200889struct gpio_desc *acpi_find_gpio(struct device *dev,
890 const char *con_id,
891 unsigned int idx,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300892 enum gpiod_flags *dflags,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300893 unsigned long *lookupflags)
Linus Walleij031ba282016-10-03 10:40:03 +0200894{
895 struct acpi_device *adev = ACPI_COMPANION(dev);
896 struct acpi_gpio_info info;
897 struct gpio_desc *desc;
898 char propname[32];
899 int i;
900
901 /* Try first from _DSD */
902 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +0300903 if (con_id) {
Linus Walleij031ba282016-10-03 10:40:03 +0200904 snprintf(propname, sizeof(propname), "%s-%s",
905 con_id, gpio_suffixes[i]);
906 } else {
907 snprintf(propname, sizeof(propname), "%s",
908 gpio_suffixes[i]);
909 }
910
911 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700912 if (!IS_ERR(desc))
Linus Walleij031ba282016-10-03 10:40:03 +0200913 break;
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700914 if (PTR_ERR(desc) == -EPROBE_DEFER)
915 return ERR_CAST(desc);
Linus Walleij031ba282016-10-03 10:40:03 +0200916 }
917
918 /* Then from plain _CRS GPIOs */
919 if (IS_ERR(desc)) {
920 if (!acpi_can_fallback_to_crs(adev, con_id))
921 return ERR_PTR(-ENOENT);
922
923 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
924 if (IS_ERR(desc))
925 return desc;
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300926 }
Linus Walleij031ba282016-10-03 10:40:03 +0200927
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300928 if (info.gpioint &&
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300929 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
Andy Shevchenkobe3dc152021-11-23 12:18:37 +0200930 dev_dbg(&adev->dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300931 return ERR_PTR(-ENOENT);
Linus Walleij031ba282016-10-03 10:40:03 +0200932 }
933
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200934 acpi_gpio_update_gpiod_flags(dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +0300935 acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
Linus Walleij031ba282016-10-03 10:40:03 +0200936 return desc;
937}
938
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300939/**
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200940 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
941 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
942 * @propname: Property name of the GPIO
943 * @index: index of GpioIo/GpioInt resource (starting from %0)
944 * @info: info pointer to fill in (optional)
945 *
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300946 * If @fwnode is an ACPI device object, call acpi_get_gpiod_by_index() for it.
947 * Otherwise (i.e. it is a data-only non-device object), use the property-based
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200948 * GPIO lookup to get to the GPIO resource with the relevant information and use
949 * that to obtain the GPIO descriptor to return.
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300950 *
951 * If the GPIO cannot be translated or there is an error an ERR_PTR is
952 * returned.
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200953 */
954struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
955 const char *propname, int index,
956 struct acpi_gpio_info *info)
957{
958 struct acpi_gpio_lookup lookup;
959 struct acpi_device *adev;
960 int ret;
961
962 adev = to_acpi_device_node(fwnode);
963 if (adev)
964 return acpi_get_gpiod_by_index(adev, propname, index, info);
965
966 if (!is_acpi_data_node(fwnode))
967 return ERR_PTR(-ENODEV);
968
969 if (!propname)
970 return ERR_PTR(-EINVAL);
971
972 memset(&lookup, 0, sizeof(lookup));
973 lookup.index = index;
974
975 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
976 if (ret)
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200977 return ERR_PTR(ret);
978
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200979 ret = acpi_gpio_resource_lookup(&lookup, info);
980 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg664e3e52014-01-08 12:40:54 +0200981}
982
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300983/**
Andy Shevchenko80939022021-02-25 18:33:19 +0200984 * acpi_dev_gpio_irq_get_by() - Find GpioInt and translate it to Linux IRQ number
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300985 * @adev: pointer to a ACPI device to get IRQ from
Andy Shevchenko80939022021-02-25 18:33:19 +0200986 * @name: optional name of GpioInt resource
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300987 * @index: index of GpioInt resource (starting from %0)
988 *
989 * If the device has one or more GpioInt resources, this function can be
990 * used to translate from the GPIO offset in the resource to the Linux IRQ
991 * number.
992 *
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300993 * The function is idempotent, though each time it runs it will configure GPIO
994 * pin direction according to the flags in GpioInt resource.
995 *
Andy Shevchenko80939022021-02-25 18:33:19 +0200996 * The function takes optional @name parameter. If the resource has a property
997 * name, then only those will be taken into account.
998 *
Thierry Redingdb05c7e2017-07-24 16:57:25 +0200999 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
Mika Westerbergc884fbd2015-05-06 13:29:06 +03001000 */
Andy Shevchenko80939022021-02-25 18:33:19 +02001001int acpi_dev_gpio_irq_get_by(struct acpi_device *adev, const char *name, int index)
Mika Westerbergc884fbd2015-05-06 13:29:06 +03001002{
1003 int idx, i;
Christophe RICARD52044722015-12-23 23:25:34 +01001004 unsigned int irq_flags;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03001005 int ret;
Mika Westerbergc884fbd2015-05-06 13:29:06 +03001006
1007 for (i = 0, idx = 0; idx <= index; i++) {
1008 struct acpi_gpio_info info;
1009 struct gpio_desc *desc;
1010
Andy Shevchenko80939022021-02-25 18:33:19 +02001011 desc = acpi_get_gpiod_by_index(adev, name, i, &info);
Christophe RICARD52044722015-12-23 23:25:34 +01001012
Hans de Goede6798d722017-03-13 23:04:21 +01001013 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
1014 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
1015 return PTR_ERR(desc);
1016
1017 if (info.gpioint && idx++ == index) {
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03001018 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Andy Shevchenkoe7b73132020-11-09 22:53:24 +02001019 enum gpiod_flags dflags = GPIOD_ASIS;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03001020 char label[32];
Hans de Goede6798d722017-03-13 23:04:21 +01001021 int irq;
1022
1023 if (IS_ERR(desc))
1024 return PTR_ERR(desc);
1025
1026 irq = gpiod_to_irq(desc);
Christophe RICARD52044722015-12-23 23:25:34 +01001027 if (irq < 0)
1028 return irq;
1029
Andy Shevchenkoe7b73132020-11-09 22:53:24 +02001030 acpi_gpio_update_gpiod_flags(&dflags, &info);
1031 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
1032
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03001033 snprintf(label, sizeof(label), "GpioInt() %d", index);
Andy Shevchenkoe7b73132020-11-09 22:53:24 +02001034 ret = gpiod_configure_flags(desc, label, lflags, dflags);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03001035 if (ret < 0)
1036 return ret;
1037
Andy Shevchenko8dcb7a12020-11-09 22:53:26 +02001038 ret = gpio_set_debounce_timeout(desc, info.debounce);
1039 if (ret)
1040 return ret;
1041
Christophe RICARD52044722015-12-23 23:25:34 +01001042 irq_flags = acpi_dev_get_irq_type(info.triggering,
1043 info.polarity);
1044
Hans de Goedebdfd6ab2021-11-25 21:30:10 +01001045 /*
1046 * If the IRQ is not already in use then set type
1047 * if specified and different than the current one.
1048 */
1049 if (can_request_irq(irq, irq_flags)) {
1050 if (irq_flags != IRQ_TYPE_NONE &&
1051 irq_flags != irq_get_trigger_type(irq))
1052 irq_set_irq_type(irq, irq_flags);
1053 } else {
1054 dev_dbg(&adev->dev, "IRQ %d already in use\n", irq);
1055 }
Christophe RICARD52044722015-12-23 23:25:34 +01001056
1057 return irq;
1058 }
1059
Mika Westerbergc884fbd2015-05-06 13:29:06 +03001060 }
Hans de Goede6798d722017-03-13 23:04:21 +01001061 return -ENOENT;
Mika Westerbergc884fbd2015-05-06 13:29:06 +03001062}
Andy Shevchenko80939022021-02-25 18:33:19 +02001063EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get_by);
Mika Westerbergc884fbd2015-05-06 13:29:06 +03001064
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001065static acpi_status
1066acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
1067 u32 bits, u64 *value, void *handler_context,
1068 void *region_context)
1069{
1070 struct acpi_gpio_chip *achip = region_context;
1071 struct gpio_chip *chip = achip->chip;
1072 struct acpi_resource_gpio *agpio;
1073 struct acpi_resource *ares;
Andy Shevchenko74301f22020-11-09 22:53:30 +02001074 u16 pin_index = address;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001075 acpi_status status;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001076 int length;
1077 int i;
1078
1079 status = acpi_buffer_to_resource(achip->conn_info.connection,
1080 achip->conn_info.length, &ares);
1081 if (ACPI_FAILURE(status))
1082 return status;
1083
1084 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
1085 ACPI_FREE(ares);
1086 return AE_BAD_PARAMETER;
1087 }
1088
1089 agpio = &ares->data.gpio;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001090
1091 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
1092 function == ACPI_WRITE)) {
1093 ACPI_FREE(ares);
1094 return AE_BAD_PARAMETER;
1095 }
1096
Andy Shevchenko74301f22020-11-09 22:53:30 +02001097 length = min_t(u16, agpio->pin_table_length, pin_index + bits);
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001098 for (i = pin_index; i < length; ++i) {
Qipeng Zhaa4811622015-04-10 06:25:10 +08001099 int pin = agpio->pin_table[i];
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001100 struct acpi_gpio_connection *conn;
1101 struct gpio_desc *desc;
1102 bool found;
1103
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001104 mutex_lock(&achip->conn_lock);
1105
1106 found = false;
1107 list_for_each_entry(conn, &achip->conns, node) {
Alexandre Courbote46cf322014-08-19 10:06:08 -07001108 if (conn->pin == pin) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001109 found = true;
Alexandre Courbote46cf322014-08-19 10:06:08 -07001110 desc = conn->desc;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001111 break;
1112 }
1113 }
Mika Westerbergc103a102015-10-30 12:02:05 +02001114
1115 /*
1116 * The same GPIO can be shared between operation region and
1117 * event but only if the access here is ACPI_READ. In that
1118 * case we "borrow" the event GPIO instead.
1119 */
Erik Schmaussc163f90c2019-02-15 13:36:19 -08001120 if (!found && agpio->shareable == ACPI_SHARED &&
Mika Westerbergc103a102015-10-30 12:02:05 +02001121 function == ACPI_READ) {
1122 struct acpi_gpio_event *event;
1123
1124 list_for_each_entry(event, &achip->events, node) {
1125 if (event->pin == pin) {
1126 desc = event->desc;
1127 found = true;
1128 break;
1129 }
1130 }
1131 }
1132
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001133 if (!found) {
Andy Shevchenko2e2b4962020-11-11 23:35:33 +02001134 desc = acpi_request_own_gpiod(chip, agpio, i, "ACPI:OpRegion");
Alexandre Courbote46cf322014-08-19 10:06:08 -07001135 if (IS_ERR(desc)) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001136 mutex_unlock(&achip->conn_lock);
Andy Shevchenkoce698f42020-11-11 20:01:52 +02001137 status = AE_ERROR;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001138 goto out;
1139 }
1140
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001141 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1142 if (!conn) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001143 gpiochip_free_own_desc(desc);
1144 mutex_unlock(&achip->conn_lock);
Andy Shevchenkoce698f42020-11-11 20:01:52 +02001145 status = AE_NO_MEMORY;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001146 goto out;
1147 }
1148
Alexandre Courbote46cf322014-08-19 10:06:08 -07001149 conn->pin = pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001150 conn->desc = desc;
1151 list_add_tail(&conn->node, &achip->conns);
1152 }
1153
1154 mutex_unlock(&achip->conn_lock);
1155
1156 if (function == ACPI_WRITE)
Andy Shevchenko2c4d00c2020-11-09 22:53:31 +02001157 gpiod_set_raw_value_cansleep(desc, !!(*value & BIT(i)));
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001158 else
Aaron Ludc62b562014-05-20 17:07:38 +08001159 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001160 }
1161
1162out:
1163 ACPI_FREE(ares);
1164 return status;
1165}
1166
1167static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
1168{
1169 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +01001170 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001171 acpi_status status;
1172
1173 INIT_LIST_HEAD(&achip->conns);
1174 mutex_init(&achip->conn_lock);
1175 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1176 acpi_gpio_adr_space_handler,
1177 NULL, achip);
1178 if (ACPI_FAILURE(status))
Linus Walleij58383c782015-11-04 09:56:26 +01001179 dev_err(chip->parent,
1180 "Failed to install GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001181}
1182
1183static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
1184{
1185 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +01001186 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001187 struct acpi_gpio_connection *conn, *tmp;
1188 acpi_status status;
1189
1190 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1191 acpi_gpio_adr_space_handler);
1192 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001193 dev_err(chip->parent,
1194 "Failed to remove GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001195 return;
1196 }
1197
1198 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
1199 gpiochip_free_own_desc(conn->desc);
1200 list_del(&conn->node);
1201 kfree(conn);
1202 }
1203}
1204
Andy Shevchenkofed70262019-04-10 18:39:16 +03001205static struct gpio_desc *
1206acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
1207 struct fwnode_handle *fwnode,
1208 const char **name,
1209 unsigned long *lflags,
Andy Shevchenko80c8d922019-04-10 18:39:18 +03001210 enum gpiod_flags *dflags)
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001211{
1212 struct gpio_chip *chip = achip->chip;
1213 struct gpio_desc *desc;
1214 u32 gpios[2];
1215 int ret;
1216
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03001217 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Andy Shevchenko32fa6552020-11-09 22:53:25 +02001218 *dflags = GPIOD_ASIS;
Arnd Bergmannc82064f2016-11-08 14:40:06 +01001219 *name = NULL;
1220
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001221 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
1222 ARRAY_SIZE(gpios));
1223 if (ret < 0)
1224 return ERR_PTR(ret);
1225
Mika Westerberg03c47492017-11-27 16:54:42 +03001226 desc = gpiochip_get_desc(chip, gpios[0]);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001227 if (IS_ERR(desc))
1228 return desc;
1229
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001230 if (gpios[1])
1231 *lflags |= GPIO_ACTIVE_LOW;
1232
1233 if (fwnode_property_present(fwnode, "input"))
1234 *dflags |= GPIOD_IN;
1235 else if (fwnode_property_present(fwnode, "output-low"))
1236 *dflags |= GPIOD_OUT_LOW;
1237 else if (fwnode_property_present(fwnode, "output-high"))
1238 *dflags |= GPIOD_OUT_HIGH;
1239 else
1240 return ERR_PTR(-EINVAL);
1241
1242 fwnode_property_read_string(fwnode, "line-name", name);
1243
1244 return desc;
1245}
1246
1247static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1248{
1249 struct gpio_chip *chip = achip->chip;
1250 struct fwnode_handle *fwnode;
1251
1252 device_for_each_child_node(chip->parent, fwnode) {
Andy Shevchenkofed70262019-04-10 18:39:16 +03001253 unsigned long lflags;
Andy Shevchenko80c8d922019-04-10 18:39:18 +03001254 enum gpiod_flags dflags;
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001255 struct gpio_desc *desc;
1256 const char *name;
1257 int ret;
1258
1259 if (!fwnode_property_present(fwnode, "gpio-hog"))
1260 continue;
1261
1262 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1263 &lflags, &dflags);
1264 if (IS_ERR(desc))
1265 continue;
1266
1267 ret = gpiod_hog(desc, name, lflags, dflags);
1268 if (ret) {
1269 dev_err(chip->parent, "Failed to hog GPIO\n");
Wei Yongjun1b6998c2016-10-29 16:11:57 +00001270 fwnode_handle_put(fwnode);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001271 return;
1272 }
1273 }
1274}
1275
Mika Westerberg664e3e52014-01-08 12:40:54 +02001276void acpi_gpiochip_add(struct gpio_chip *chip)
1277{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001278 struct acpi_gpio_chip *acpi_gpio;
Daniel Scallya9e10e52021-06-03 23:40:02 +01001279 struct acpi_device *adev;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001280 acpi_status status;
1281
Linus Walleij58383c782015-11-04 09:56:26 +01001282 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001283 return;
1284
Daniel Scallya9e10e52021-06-03 23:40:02 +01001285 adev = ACPI_COMPANION(chip->parent);
1286 if (!adev)
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001287 return;
1288
1289 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1290 if (!acpi_gpio) {
Linus Walleij58383c782015-11-04 09:56:26 +01001291 dev_err(chip->parent,
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001292 "Failed to allocate memory for ACPI GPIO chip\n");
1293 return;
1294 }
1295
1296 acpi_gpio->chip = chip;
Mika Westerbergc103a102015-10-30 12:02:05 +02001297 INIT_LIST_HEAD(&acpi_gpio->events);
Hans de Goede78d3a922018-08-14 16:07:03 +02001298 INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001299
Daniel Scallya9e10e52021-06-03 23:40:02 +01001300 status = acpi_attach_data(adev->handle, acpi_gpio_chip_dh, acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001301 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001302 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001303 kfree(acpi_gpio);
1304 return;
1305 }
1306
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001307 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001308 acpi_gpiochip_scan_gpios(acpi_gpio);
Daniel Scallya9e10e52021-06-03 23:40:02 +01001309 acpi_dev_clear_dependencies(adev);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001310}
1311
1312void acpi_gpiochip_remove(struct gpio_chip *chip)
1313{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001314 struct acpi_gpio_chip *acpi_gpio;
1315 acpi_handle handle;
1316 acpi_status status;
1317
Linus Walleij58383c782015-11-04 09:56:26 +01001318 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001319 return;
1320
Linus Walleij58383c782015-11-04 09:56:26 +01001321 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001322 if (!handle)
1323 return;
1324
1325 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1326 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001327 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001328 return;
1329 }
1330
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001331 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001332
1333 acpi_detach_data(handle, acpi_gpio_chip_dh);
1334 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001335}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001336
Andy Shevchenko515321a2021-03-09 11:37:34 +02001337void acpi_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
1338{
1339 /* Set default fwnode to parent's one if present */
1340 if (gc->parent)
1341 ACPI_COMPANION_SET(&gdev->dev, ACPI_COMPANION(gc->parent));
1342}
1343
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001344static int acpi_gpio_package_count(const union acpi_object *obj)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001345{
1346 const union acpi_object *element = obj->package.elements;
1347 const union acpi_object *end = element + obj->package.count;
1348 unsigned int count = 0;
1349
1350 while (element < end) {
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001351 switch (element->type) {
1352 case ACPI_TYPE_LOCAL_REFERENCE:
1353 element += 3;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001354 fallthrough;
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001355 case ACPI_TYPE_INTEGER:
1356 element++;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001357 count++;
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001358 break;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001359
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001360 default:
1361 return -EPROTO;
1362 }
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001363 }
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001364
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001365 return count;
1366}
1367
1368static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1369{
1370 unsigned int *count = data;
1371
1372 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1373 *count += ares->data.gpio.pin_table_length;
1374
1375 return 1;
1376}
1377
1378/**
Andy Shevchenko85edcd02019-03-30 00:33:45 +03001379 * acpi_gpio_count - count the GPIOs associated with a device / function
1380 * @dev: GPIO consumer, can be %NULL for system-global GPIOs
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001381 * @con_id: function within the GPIO consumer
Andy Shevchenko85edcd02019-03-30 00:33:45 +03001382 *
1383 * Return:
1384 * The number of GPIOs associated with a device / function or %-ENOENT,
1385 * if no GPIO has been assigned to the requested function.
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001386 */
1387int acpi_gpio_count(struct device *dev, const char *con_id)
1388{
1389 struct acpi_device *adev = ACPI_COMPANION(dev);
1390 const union acpi_object *obj;
1391 const struct acpi_gpio_mapping *gm;
1392 int count = -ENOENT;
1393 int ret;
1394 char propname[32];
1395 unsigned int i;
1396
1397 /* Try first from _DSD */
1398 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +03001399 if (con_id)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001400 snprintf(propname, sizeof(propname), "%s-%s",
1401 con_id, gpio_suffixes[i]);
1402 else
1403 snprintf(propname, sizeof(propname), "%s",
1404 gpio_suffixes[i]);
1405
1406 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1407 &obj);
1408 if (ret == 0) {
1409 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1410 count = 1;
1411 else if (obj->type == ACPI_TYPE_PACKAGE)
1412 count = acpi_gpio_package_count(obj);
1413 } else if (adev->driver_gpios) {
1414 for (gm = adev->driver_gpios; gm->name; gm++)
1415 if (strcmp(propname, gm->name) == 0) {
1416 count = gm->size;
1417 break;
1418 }
1419 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001420 if (count > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001421 break;
1422 }
1423
1424 /* Then from plain _CRS GPIOs */
1425 if (count < 0) {
1426 struct list_head resource_list;
1427 unsigned int crs_count = 0;
1428
Andy Shevchenko6fe9da42017-05-23 20:03:20 +03001429 if (!acpi_can_fallback_to_crs(adev, con_id))
1430 return count;
1431
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001432 INIT_LIST_HEAD(&resource_list);
1433 acpi_dev_get_resources(adev, &resource_list,
1434 acpi_find_gpio_count, &crs_count);
1435 acpi_dev_free_resource_list(&resource_list);
1436 if (crs_count > 0)
1437 count = crs_count;
1438 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001439 return count ? count : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001440}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001441
Hans de Goedee59f5e02018-11-28 17:57:55 +01001442/* Run deferred acpi_gpiochip_request_irqs() */
Hans de Goede04fd1ca2020-03-25 11:39:56 +01001443static int __init acpi_gpio_handle_deferred_request_irqs(void)
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001444{
Hans de Goede78d3a922018-08-14 16:07:03 +02001445 struct acpi_gpio_chip *acpi_gpio, *tmp;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001446
Hans de Goede78d3a922018-08-14 16:07:03 +02001447 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
1448 list_for_each_entry_safe(acpi_gpio, tmp,
1449 &acpi_gpio_deferred_req_irqs_list,
Hans de Goedee59f5e02018-11-28 17:57:55 +01001450 deferred_req_irqs_list_entry)
1451 acpi_gpiochip_request_irqs(acpi_gpio);
Hans de Goede78d3a922018-08-14 16:07:03 +02001452
1453 acpi_gpio_deferred_req_irqs_done = true;
1454 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001455
1456 return 0;
1457}
1458/* We must use _sync so that this runs after the first deferred_probe run */
Hans de Goedee59f5e02018-11-28 17:57:55 +01001459late_initcall_sync(acpi_gpio_handle_deferred_request_irqs);
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001460
Hans de Goede04fd1ca2020-03-25 11:39:56 +01001461static const struct dmi_system_id gpiolib_acpi_quirks[] __initconst = {
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001462 {
Hans de Goede27273152019-11-06 12:51:09 +01001463 /*
1464 * The Minix Neo Z83-4 has a micro-USB-B id-pin handler for
1465 * a non existing micro-USB-B connector which puts the HDMI
1466 * DDC pins in GPIO mode, breaking HDMI support.
1467 */
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001468 .matches = {
1469 DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
1470 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
Hans de Goede1ad1b542020-01-05 17:03:56 +01001471 },
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001472 .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
1473 .no_edge_events_on_boot = true,
1474 },
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001475 },
Hans de Goede27273152019-11-06 12:51:09 +01001476 {
1477 /*
1478 * The Terra Pad 1061 has a micro-USB-B id-pin handler, which
1479 * instead of controlling the actual micro-USB-B turns the 5V
1480 * boost for its USB-A connector off. The actual micro-USB-B
1481 * connector is wired for charging only.
1482 */
1483 .matches = {
1484 DMI_MATCH(DMI_SYS_VENDOR, "Wortmann_AG"),
1485 DMI_MATCH(DMI_PRODUCT_NAME, "TERRA_PAD_1061"),
Hans de Goede1ad1b542020-01-05 17:03:56 +01001486 },
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001487 .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
1488 .no_edge_events_on_boot = true,
1489 },
Hans de Goede27273152019-11-06 12:51:09 +01001490 },
Hans de Goedeaa23ca32020-01-05 17:03:57 +01001491 {
1492 /*
Hans de Goededa91ece2021-04-01 18:27:40 +02001493 * The Dell Venue 10 Pro 5055, with Bay Trail SoC + TI PMIC uses an
1494 * external embedded-controller connected via I2C + an ACPI GPIO
1495 * event handler on INT33FFC:02 pin 12, causing spurious wakeups.
1496 */
1497 .matches = {
1498 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1499 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 10 Pro 5055"),
1500 },
1501 .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
1502 .ignore_wake = "INT33FC:02@12",
1503 },
1504 },
1505 {
1506 /*
Hans de Goedeefaa87f2020-03-02 12:12:22 +01001507 * HP X2 10 models with Cherry Trail SoC + TI PMIC use an
1508 * external embedded-controller connected via I2C + an ACPI GPIO
1509 * event handler on INT33FF:01 pin 0, causing spurious wakeups.
1510 * When suspending by closing the LID, the power to the USB
1511 * keyboard is turned off, causing INT0002 ACPI events to
1512 * trigger once the XHCI controller notices the keyboard is
1513 * gone. So INT0002 events cause spurious wakeups too. Ignoring
1514 * EC wakes breaks wakeup when opening the lid, the user needs
Hans de Goedeaa23ca32020-01-05 17:03:57 +01001515 * to press the power-button to wakeup the system. The
1516 * alternative is suspend simply not working, which is worse.
1517 */
1518 .matches = {
1519 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1520 DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
1521 },
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001522 .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
1523 .ignore_wake = "INT33FF:01@0,INT0002:00@2",
1524 },
Hans de Goedeaa23ca32020-01-05 17:03:57 +01001525 },
Hans de Goede0e915062020-03-02 12:12:24 +01001526 {
1527 /*
1528 * HP X2 10 models with Bay Trail SoC + AXP288 PMIC use an
1529 * external embedded-controller connected via I2C + an ACPI GPIO
1530 * event handler on INT33FC:02 pin 28, causing spurious wakeups.
1531 */
1532 .matches = {
1533 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1534 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
1535 DMI_MATCH(DMI_BOARD_NAME, "815D"),
1536 },
1537 .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
1538 .ignore_wake = "INT33FC:02@28",
1539 },
1540 },
Hans de Goede0c625cc2020-03-02 12:12:25 +01001541 {
1542 /*
1543 * HP X2 10 models with Cherry Trail SoC + AXP288 PMIC use an
1544 * external embedded-controller connected via I2C + an ACPI GPIO
1545 * event handler on INT33FF:01 pin 0, causing spurious wakeups.
1546 */
1547 .matches = {
1548 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1549 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
1550 DMI_MATCH(DMI_BOARD_NAME, "813E"),
1551 },
1552 .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
1553 .ignore_wake = "INT33FF:01@0",
1554 },
1555 },
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001556 {} /* Terminating entry */
1557};
1558
Hans de Goede04fd1ca2020-03-25 11:39:56 +01001559static int __init acpi_gpio_setup_params(void)
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001560{
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001561 const struct acpi_gpiolib_dmi_quirk *quirk = NULL;
Hans de Goede1ad1b542020-01-05 17:03:56 +01001562 const struct dmi_system_id *id;
Hans de Goede1ad1b542020-01-05 17:03:56 +01001563
1564 id = dmi_first_match(gpiolib_acpi_quirks);
1565 if (id)
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001566 quirk = id->driver_data;
Hans de Goede1ad1b542020-01-05 17:03:56 +01001567
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001568 if (run_edge_events_on_boot < 0) {
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001569 if (quirk && quirk->no_edge_events_on_boot)
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001570 run_edge_events_on_boot = 0;
1571 else
1572 run_edge_events_on_boot = 1;
1573 }
1574
Hans de Goede2ccb21f2020-03-02 12:12:23 +01001575 if (ignore_wake == NULL && quirk && quirk->ignore_wake)
1576 ignore_wake = quirk->ignore_wake;
Hans de Goedeaa23ca32020-01-05 17:03:57 +01001577
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001578 return 0;
1579}
1580
1581/* Directly after dmi_setup() which runs as core_initcall() */
1582postcore_initcall(acpi_gpio_setup_params);