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