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