1 // SPDX-License-Identifier: GPL-2.0
3 * ACPI helpers for GPIO API
5 * Copyright (C) 2012, Intel Corporation
10 #include <linux/dmi.h>
11 #include <linux/errno.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/gpio/machine.h>
15 #include <linux/export.h>
16 #include <linux/acpi.h>
17 #include <linux/interrupt.h>
18 #include <linux/mutex.h>
19 #include <linux/pinctrl/pinctrl.h>
22 #include "gpiolib-acpi.h"
24 #define QUIRK_NO_EDGE_EVENTS_ON_BOOT 0x01l
25 #define QUIRK_NO_WAKEUP 0x02l
27 static int run_edge_events_on_boot = -1;
28 module_param(run_edge_events_on_boot, int, 0444);
29 MODULE_PARM_DESC(run_edge_events_on_boot,
30 "Run edge _AEI event-handlers at boot: 0=no, 1=yes, -1=auto");
32 static int honor_wakeup = -1;
33 module_param(honor_wakeup, int, 0444);
34 MODULE_PARM_DESC(honor_wakeup,
35 "Honor the ACPI wake-capable flag: 0=no, 1=yes, -1=auto");
38 * struct acpi_gpio_event - ACPI GPIO event handler data
40 * @node: list-entry of the events list of the struct acpi_gpio_chip
41 * @handle: handle of ACPI method to execute when the IRQ triggers
42 * @handler: handler function to pass to request_irq() when requesting the IRQ
43 * @pin: GPIO pin number on the struct gpio_chip
44 * @irq: Linux IRQ number for the event, for request_irq() / free_irq()
45 * @irqflags: flags to pass to request_irq() when requesting the IRQ
46 * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source
47 * @irq_requested:True if request_irq() has been done
48 * @desc: struct gpio_desc for the GPIO pin for this event
50 struct acpi_gpio_event {
51 struct list_head node;
53 irq_handler_t handler;
56 unsigned long irqflags;
59 struct gpio_desc *desc;
62 struct acpi_gpio_connection {
63 struct list_head node;
65 struct gpio_desc *desc;
68 struct acpi_gpio_chip {
70 * ACPICA requires that the first field of the context parameter
71 * passed to acpi_install_address_space_handler() is large enough
72 * to hold struct acpi_connection_info.
74 struct acpi_connection_info conn_info;
75 struct list_head conns;
76 struct mutex conn_lock;
77 struct gpio_chip *chip;
78 struct list_head events;
79 struct list_head deferred_req_irqs_list_entry;
83 * For GPIO chips which call acpi_gpiochip_request_interrupts() before late_init
84 * (so builtin drivers) we register the ACPI GpioInt IRQ handlers from a
85 * late_initcall_sync() handler, so that other builtin drivers can register their
86 * OpRegions before the event handlers can run. This list contains GPIO chips
87 * for which the acpi_gpiochip_request_irqs() call has been deferred.
89 static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
90 static LIST_HEAD(acpi_gpio_deferred_req_irqs_list);
91 static bool acpi_gpio_deferred_req_irqs_done;
93 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
98 return ACPI_HANDLE(gc->parent) == data;
102 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
103 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
104 * @pin: ACPI GPIO pin number (0-based, controller-relative)
106 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
107 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
108 * controller does not have GPIO chip registered at the moment. This is to
109 * support probe deferral.
111 static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
113 struct gpio_chip *chip;
117 status = acpi_get_handle(NULL, path, &handle);
118 if (ACPI_FAILURE(status))
119 return ERR_PTR(-ENODEV);
121 chip = gpiochip_find(handle, acpi_gpiochip_find);
123 return ERR_PTR(-EPROBE_DEFER);
125 return gpiochip_get_desc(chip, pin);
128 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
130 struct acpi_gpio_event *event = data;
132 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
137 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
139 struct acpi_gpio_event *event = data;
141 acpi_execute_simple_method(event->handle, NULL, event->pin);
146 static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
148 /* The address of this function is used as a key. */
151 bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
152 struct acpi_resource_gpio **agpio)
154 struct acpi_resource_gpio *gpio;
156 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
159 gpio = &ares->data.gpio;
160 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
166 EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
168 static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
169 struct acpi_gpio_event *event)
173 ret = request_threaded_irq(event->irq, NULL, event->handler,
174 event->irqflags, "ACPI:Event", event);
176 dev_err(acpi_gpio->chip->parent,
177 "Failed to setup interrupt handler for %d\n",
182 if (event->irq_is_wake)
183 enable_irq_wake(event->irq);
185 event->irq_requested = true;
187 /* Make sure we trigger the initial state of edge-triggered IRQs */
188 if (run_edge_events_on_boot &&
189 (event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) {
190 value = gpiod_get_raw_value_cansleep(event->desc);
191 if (((event->irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
192 ((event->irqflags & IRQF_TRIGGER_FALLING) && value == 0))
193 event->handler(event->irq, event);
197 static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
199 struct acpi_gpio_event *event;
201 list_for_each_entry(event, &acpi_gpio->events, node)
202 acpi_gpiochip_request_irq(acpi_gpio, event);
205 /* Always returns AE_OK so that we keep looping over the resources */
206 static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
209 struct acpi_gpio_chip *acpi_gpio = context;
210 struct gpio_chip *chip = acpi_gpio->chip;
211 struct acpi_resource_gpio *agpio;
212 acpi_handle handle, evt_handle;
213 struct acpi_gpio_event *event;
214 irq_handler_t handler = NULL;
215 struct gpio_desc *desc;
218 if (!acpi_gpio_get_irq_resource(ares, &agpio))
221 handle = ACPI_HANDLE(chip->parent);
222 pin = agpio->pin_table[0];
226 sprintf(ev_name, "_%c%02hhX",
227 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
229 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
230 handler = acpi_gpio_irq_handler;
233 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
234 handler = acpi_gpio_irq_handler_evt;
239 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event",
240 GPIO_ACTIVE_HIGH, GPIOD_IN);
242 dev_err(chip->parent,
243 "Failed to request GPIO for pin 0x%04X, err %ld\n",
248 ret = gpiochip_lock_as_irq(chip, pin);
250 dev_err(chip->parent,
251 "Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
256 irq = gpiod_to_irq(desc);
258 dev_err(chip->parent,
259 "Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
261 goto fail_unlock_irq;
264 event = kzalloc(sizeof(*event), GFP_KERNEL);
266 goto fail_unlock_irq;
268 event->irqflags = IRQF_ONESHOT;
269 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
270 if (agpio->polarity == ACPI_ACTIVE_HIGH)
271 event->irqflags |= IRQF_TRIGGER_HIGH;
273 event->irqflags |= IRQF_TRIGGER_LOW;
275 switch (agpio->polarity) {
276 case ACPI_ACTIVE_HIGH:
277 event->irqflags |= IRQF_TRIGGER_RISING;
279 case ACPI_ACTIVE_LOW:
280 event->irqflags |= IRQF_TRIGGER_FALLING;
283 event->irqflags |= IRQF_TRIGGER_RISING |
284 IRQF_TRIGGER_FALLING;
289 event->handle = evt_handle;
290 event->handler = handler;
292 event->irq_is_wake = honor_wakeup && agpio->wake_capable == ACPI_WAKE_CAPABLE;
296 list_add_tail(&event->node, &acpi_gpio->events);
301 gpiochip_unlock_as_irq(chip, pin);
303 gpiochip_free_own_desc(desc);
309 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
312 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
313 * handled by ACPI event methods which need to be called from the GPIO
314 * chip's interrupt handler. acpi_gpiochip_request_interrupts() finds out which
315 * GPIO pins have ACPI event methods and assigns interrupt handlers that calls
316 * the ACPI event methods for those pins.
318 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
320 struct acpi_gpio_chip *acpi_gpio;
325 if (!chip->parent || !chip->to_irq)
328 handle = ACPI_HANDLE(chip->parent);
332 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
333 if (ACPI_FAILURE(status))
336 acpi_walk_resources(handle, "_AEI",
337 acpi_gpiochip_alloc_event, acpi_gpio);
339 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
340 defer = !acpi_gpio_deferred_req_irqs_done;
342 list_add(&acpi_gpio->deferred_req_irqs_list_entry,
343 &acpi_gpio_deferred_req_irqs_list);
344 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
349 acpi_gpiochip_request_irqs(acpi_gpio);
351 EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
354 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
357 * Free interrupts associated with GPIO ACPI event method for the given
360 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
362 struct acpi_gpio_chip *acpi_gpio;
363 struct acpi_gpio_event *event, *ep;
367 if (!chip->parent || !chip->to_irq)
370 handle = ACPI_HANDLE(chip->parent);
374 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
375 if (ACPI_FAILURE(status))
378 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
379 if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry))
380 list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
381 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
383 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
384 if (event->irq_requested) {
385 if (event->irq_is_wake)
386 disable_irq_wake(event->irq);
388 free_irq(event->irq, event);
391 gpiochip_unlock_as_irq(chip, event->pin);
392 gpiochip_free_own_desc(event->desc);
393 list_del(&event->node);
397 EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
399 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
400 const struct acpi_gpio_mapping *gpios)
403 adev->driver_gpios = gpios;
408 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
410 void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
413 adev->driver_gpios = NULL;
415 EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
417 static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
419 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
422 int devm_acpi_dev_add_driver_gpios(struct device *dev,
423 const struct acpi_gpio_mapping *gpios)
428 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
432 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
437 devres_add(dev, res);
440 EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
442 void devm_acpi_dev_remove_driver_gpios(struct device *dev)
444 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
446 EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
448 static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
449 const char *name, int index,
450 struct fwnode_reference_args *args,
451 unsigned int *quirks)
453 const struct acpi_gpio_mapping *gm;
455 if (!adev->driver_gpios)
458 for (gm = adev->driver_gpios; gm->name; gm++)
459 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
460 const struct acpi_gpio_params *par = gm->data + index;
462 args->fwnode = acpi_fwnode_handle(adev);
463 args->args[0] = par->crs_entry_index;
464 args->args[1] = par->line_index;
465 args->args[2] = par->active_low;
468 *quirks = gm->quirks;
475 static enum gpiod_flags
476 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
478 switch (agpio->io_restriction) {
479 case ACPI_IO_RESTRICT_INPUT:
481 case ACPI_IO_RESTRICT_OUTPUT:
483 * ACPI GPIO resources don't contain an initial value for the
484 * GPIO. Therefore we deduce that value from the pull field
485 * instead. If the pin is pulled up we assume default to be
486 * high, if it is pulled down we assume default to be low,
487 * otherwise we leave pin untouched.
489 switch (agpio->pin_config) {
490 case ACPI_PIN_CONFIG_PULLUP:
491 return GPIOD_OUT_HIGH;
492 case ACPI_PIN_CONFIG_PULLDOWN:
493 return GPIOD_OUT_LOW;
502 * Assume that the BIOS has configured the direction and pull
509 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
511 const enum gpiod_flags mask =
512 GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
513 GPIOD_FLAGS_BIT_DIR_VAL;
517 * Check if the BIOS has IoRestriction with explicitly set direction
518 * and update @flags accordingly. Otherwise use whatever caller asked
521 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
522 enum gpiod_flags diff = *flags ^ update;
525 * Check if caller supplied incompatible GPIO initialization
528 * Return %-EINVAL to notify that firmware has different
529 * settings and we are going to use them.
531 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
532 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
534 *flags = (*flags & ~mask) | (update & mask);
540 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
542 struct device *dev = &info->adev->dev;
543 enum gpiod_flags old = *flags;
546 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
547 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
549 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
552 dev_dbg(dev, "Override GPIO initialization flags\n");
559 int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
560 struct acpi_gpio_info *info)
562 switch (info->pin_config) {
563 case ACPI_PIN_CONFIG_PULLUP:
564 *lookupflags |= GPIO_PULL_UP;
566 case ACPI_PIN_CONFIG_PULLDOWN:
567 *lookupflags |= GPIO_PULL_DOWN;
573 if (info->polarity == GPIO_ACTIVE_LOW)
574 *lookupflags |= GPIO_ACTIVE_LOW;
579 struct acpi_gpio_lookup {
580 struct acpi_gpio_info info;
584 struct gpio_desc *desc;
588 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
590 struct acpi_gpio_lookup *lookup = data;
592 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
596 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
597 bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
600 if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
603 if (lookup->n++ != lookup->index)
606 pin_index = lookup->pin_index;
607 if (pin_index >= agpio->pin_table_length)
610 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
611 agpio->pin_table[pin_index]);
612 lookup->info.pin_config = agpio->pin_config;
613 lookup->info.gpioint = gpioint;
616 * Polarity and triggering are only specified for GpioInt
618 * Note: we expect here:
619 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
620 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
622 if (lookup->info.gpioint) {
623 lookup->info.flags = GPIOD_IN;
624 lookup->info.polarity = agpio->polarity;
625 lookup->info.triggering = agpio->triggering;
627 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
628 lookup->info.polarity = lookup->active_low;
635 static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
636 struct acpi_gpio_info *info)
638 struct acpi_device *adev = lookup->info.adev;
639 struct list_head res_list;
642 INIT_LIST_HEAD(&res_list);
644 ret = acpi_dev_get_resources(adev, &res_list,
645 acpi_populate_gpio_lookup,
650 acpi_dev_free_resource_list(&res_list);
656 *info = lookup->info;
660 static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
661 const char *propname, int index,
662 struct acpi_gpio_lookup *lookup)
664 struct fwnode_reference_args args;
665 unsigned int quirks = 0;
668 memset(&args, 0, sizeof(args));
669 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
672 struct acpi_device *adev = to_acpi_device_node(fwnode);
677 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
682 * The property was found and resolved, so need to lookup the GPIO based
685 if (!to_acpi_device_node(args.fwnode))
690 lookup->index = args.args[0];
691 lookup->pin_index = args.args[1];
692 lookup->active_low = !!args.args[2];
694 lookup->info.adev = to_acpi_device_node(args.fwnode);
695 lookup->info.quirks = quirks;
701 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
702 * @adev: pointer to a ACPI device to get GPIO from
703 * @propname: Property name of the GPIO (optional)
704 * @index: index of GpioIo/GpioInt resource (starting from %0)
705 * @info: info pointer to fill in (optional)
707 * Function goes through ACPI resources for @adev and based on @index looks
708 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
709 * and returns it. @index matches GpioIo/GpioInt resources only so if there
710 * are total %3 GPIO resources, the index goes from %0 to %2.
712 * If @propname is specified the GPIO is looked using device property. In
713 * that case @index is used to select the GPIO entry in the property value
714 * (in case of multiple).
716 * If the GPIO cannot be translated or there is an error, an ERR_PTR is
719 * Note: if the GPIO resource has multiple entries in the pin list, this
720 * function only returns the first.
722 static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
723 const char *propname, int index,
724 struct acpi_gpio_info *info)
726 struct acpi_gpio_lookup lookup;
730 return ERR_PTR(-ENODEV);
732 memset(&lookup, 0, sizeof(lookup));
733 lookup.index = index;
736 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
738 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
739 propname, index, &lookup);
743 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
744 dev_name(&lookup.info.adev->dev), lookup.index,
745 lookup.pin_index, lookup.active_low);
747 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
748 lookup.info.adev = adev;
751 ret = acpi_gpio_resource_lookup(&lookup, info);
752 return ret ? ERR_PTR(ret) : lookup.desc;
755 static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
758 /* Never allow fallback if the device has properties */
759 if (acpi_dev_has_props(adev) || adev->driver_gpios)
762 return con_id == NULL;
765 struct gpio_desc *acpi_find_gpio(struct device *dev,
768 enum gpiod_flags *dflags,
769 unsigned long *lookupflags)
771 struct acpi_device *adev = ACPI_COMPANION(dev);
772 struct acpi_gpio_info info;
773 struct gpio_desc *desc;
777 /* Try first from _DSD */
778 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
780 snprintf(propname, sizeof(propname), "%s-%s",
781 con_id, gpio_suffixes[i]);
783 snprintf(propname, sizeof(propname), "%s",
787 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
790 if (PTR_ERR(desc) == -EPROBE_DEFER)
791 return ERR_CAST(desc);
794 /* Then from plain _CRS GPIOs */
796 if (!acpi_can_fallback_to_crs(adev, con_id))
797 return ERR_PTR(-ENOENT);
799 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
805 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
806 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
807 return ERR_PTR(-ENOENT);
810 acpi_gpio_update_gpiod_flags(dflags, &info);
811 acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
816 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
817 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
818 * @propname: Property name of the GPIO
819 * @index: index of GpioIo/GpioInt resource (starting from %0)
820 * @info: info pointer to fill in (optional)
822 * If @fwnode is an ACPI device object, call acpi_get_gpiod_by_index() for it.
823 * Otherwise (i.e. it is a data-only non-device object), use the property-based
824 * GPIO lookup to get to the GPIO resource with the relevant information and use
825 * that to obtain the GPIO descriptor to return.
827 * If the GPIO cannot be translated or there is an error an ERR_PTR is
830 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
831 const char *propname, int index,
832 struct acpi_gpio_info *info)
834 struct acpi_gpio_lookup lookup;
835 struct acpi_device *adev;
838 adev = to_acpi_device_node(fwnode);
840 return acpi_get_gpiod_by_index(adev, propname, index, info);
842 if (!is_acpi_data_node(fwnode))
843 return ERR_PTR(-ENODEV);
846 return ERR_PTR(-EINVAL);
848 memset(&lookup, 0, sizeof(lookup));
849 lookup.index = index;
851 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
855 ret = acpi_gpio_resource_lookup(&lookup, info);
856 return ret ? ERR_PTR(ret) : lookup.desc;
860 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
861 * @adev: pointer to a ACPI device to get IRQ from
862 * @index: index of GpioInt resource (starting from %0)
864 * If the device has one or more GpioInt resources, this function can be
865 * used to translate from the GPIO offset in the resource to the Linux IRQ
868 * The function is idempotent, though each time it runs it will configure GPIO
869 * pin direction according to the flags in GpioInt resource.
871 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
873 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
876 unsigned int irq_flags;
879 for (i = 0, idx = 0; idx <= index; i++) {
880 struct acpi_gpio_info info;
881 struct gpio_desc *desc;
883 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
885 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
886 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
887 return PTR_ERR(desc);
889 if (info.gpioint && idx++ == index) {
890 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
895 return PTR_ERR(desc);
897 irq = gpiod_to_irq(desc);
901 snprintf(label, sizeof(label), "GpioInt() %d", index);
902 ret = gpiod_configure_flags(desc, label, lflags, info.flags);
906 irq_flags = acpi_dev_get_irq_type(info.triggering,
909 /* Set type if specified and different than the current one */
910 if (irq_flags != IRQ_TYPE_NONE &&
911 irq_flags != irq_get_trigger_type(irq))
912 irq_set_irq_type(irq, irq_flags);
920 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
923 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
924 u32 bits, u64 *value, void *handler_context,
925 void *region_context)
927 struct acpi_gpio_chip *achip = region_context;
928 struct gpio_chip *chip = achip->chip;
929 struct acpi_resource_gpio *agpio;
930 struct acpi_resource *ares;
931 int pin_index = (int)address;
936 status = acpi_buffer_to_resource(achip->conn_info.connection,
937 achip->conn_info.length, &ares);
938 if (ACPI_FAILURE(status))
941 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
943 return AE_BAD_PARAMETER;
946 agpio = &ares->data.gpio;
948 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
949 function == ACPI_WRITE)) {
951 return AE_BAD_PARAMETER;
954 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
955 for (i = pin_index; i < length; ++i) {
956 int pin = agpio->pin_table[i];
957 struct acpi_gpio_connection *conn;
958 struct gpio_desc *desc;
961 mutex_lock(&achip->conn_lock);
964 list_for_each_entry(conn, &achip->conns, node) {
965 if (conn->pin == pin) {
973 * The same GPIO can be shared between operation region and
974 * event but only if the access here is ACPI_READ. In that
975 * case we "borrow" the event GPIO instead.
977 if (!found && agpio->shareable == ACPI_SHARED &&
978 function == ACPI_READ) {
979 struct acpi_gpio_event *event;
981 list_for_each_entry(event, &achip->events, node) {
982 if (event->pin == pin) {
991 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
992 const char *label = "ACPI:OpRegion";
994 desc = gpiochip_request_own_desc(chip, pin, label,
999 mutex_unlock(&achip->conn_lock);
1003 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1005 status = AE_NO_MEMORY;
1006 gpiochip_free_own_desc(desc);
1007 mutex_unlock(&achip->conn_lock);
1013 list_add_tail(&conn->node, &achip->conns);
1016 mutex_unlock(&achip->conn_lock);
1018 if (function == ACPI_WRITE)
1019 gpiod_set_raw_value_cansleep(desc,
1020 !!((1 << i) & *value));
1022 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
1030 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
1032 struct gpio_chip *chip = achip->chip;
1033 acpi_handle handle = ACPI_HANDLE(chip->parent);
1036 INIT_LIST_HEAD(&achip->conns);
1037 mutex_init(&achip->conn_lock);
1038 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1039 acpi_gpio_adr_space_handler,
1041 if (ACPI_FAILURE(status))
1042 dev_err(chip->parent,
1043 "Failed to install GPIO OpRegion handler\n");
1046 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
1048 struct gpio_chip *chip = achip->chip;
1049 acpi_handle handle = ACPI_HANDLE(chip->parent);
1050 struct acpi_gpio_connection *conn, *tmp;
1053 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1054 acpi_gpio_adr_space_handler);
1055 if (ACPI_FAILURE(status)) {
1056 dev_err(chip->parent,
1057 "Failed to remove GPIO OpRegion handler\n");
1061 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
1062 gpiochip_free_own_desc(conn->desc);
1063 list_del(&conn->node);
1068 static struct gpio_desc *
1069 acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
1070 struct fwnode_handle *fwnode,
1072 unsigned long *lflags,
1073 enum gpiod_flags *dflags)
1075 struct gpio_chip *chip = achip->chip;
1076 struct gpio_desc *desc;
1080 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
1084 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
1087 return ERR_PTR(ret);
1089 desc = gpiochip_get_desc(chip, gpios[0]);
1094 *lflags |= GPIO_ACTIVE_LOW;
1096 if (fwnode_property_present(fwnode, "input"))
1097 *dflags |= GPIOD_IN;
1098 else if (fwnode_property_present(fwnode, "output-low"))
1099 *dflags |= GPIOD_OUT_LOW;
1100 else if (fwnode_property_present(fwnode, "output-high"))
1101 *dflags |= GPIOD_OUT_HIGH;
1103 return ERR_PTR(-EINVAL);
1105 fwnode_property_read_string(fwnode, "line-name", name);
1110 static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1112 struct gpio_chip *chip = achip->chip;
1113 struct fwnode_handle *fwnode;
1115 device_for_each_child_node(chip->parent, fwnode) {
1116 unsigned long lflags;
1117 enum gpiod_flags dflags;
1118 struct gpio_desc *desc;
1122 if (!fwnode_property_present(fwnode, "gpio-hog"))
1125 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1130 ret = gpiod_hog(desc, name, lflags, dflags);
1132 dev_err(chip->parent, "Failed to hog GPIO\n");
1133 fwnode_handle_put(fwnode);
1139 void acpi_gpiochip_add(struct gpio_chip *chip)
1141 struct acpi_gpio_chip *acpi_gpio;
1145 if (!chip || !chip->parent)
1148 handle = ACPI_HANDLE(chip->parent);
1152 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1154 dev_err(chip->parent,
1155 "Failed to allocate memory for ACPI GPIO chip\n");
1159 acpi_gpio->chip = chip;
1160 INIT_LIST_HEAD(&acpi_gpio->events);
1161 INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
1163 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1164 if (ACPI_FAILURE(status)) {
1165 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
1171 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
1173 acpi_gpiochip_request_regions(acpi_gpio);
1174 acpi_gpiochip_scan_gpios(acpi_gpio);
1175 acpi_walk_dep_device_list(handle);
1178 void acpi_gpiochip_remove(struct gpio_chip *chip)
1180 struct acpi_gpio_chip *acpi_gpio;
1184 if (!chip || !chip->parent)
1187 handle = ACPI_HANDLE(chip->parent);
1191 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1192 if (ACPI_FAILURE(status)) {
1193 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
1197 acpi_gpiochip_free_regions(acpi_gpio);
1199 acpi_detach_data(handle, acpi_gpio_chip_dh);
1203 static int acpi_gpio_package_count(const union acpi_object *obj)
1205 const union acpi_object *element = obj->package.elements;
1206 const union acpi_object *end = element + obj->package.count;
1207 unsigned int count = 0;
1209 while (element < end) {
1210 switch (element->type) {
1211 case ACPI_TYPE_LOCAL_REFERENCE:
1214 case ACPI_TYPE_INTEGER:
1227 static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1229 unsigned int *count = data;
1231 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1232 *count += ares->data.gpio.pin_table_length;
1238 * acpi_gpio_count - count the GPIOs associated with a device / function
1239 * @dev: GPIO consumer, can be %NULL for system-global GPIOs
1240 * @con_id: function within the GPIO consumer
1243 * The number of GPIOs associated with a device / function or %-ENOENT,
1244 * if no GPIO has been assigned to the requested function.
1246 int acpi_gpio_count(struct device *dev, const char *con_id)
1248 struct acpi_device *adev = ACPI_COMPANION(dev);
1249 const union acpi_object *obj;
1250 const struct acpi_gpio_mapping *gm;
1251 int count = -ENOENT;
1256 /* Try first from _DSD */
1257 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1259 snprintf(propname, sizeof(propname), "%s-%s",
1260 con_id, gpio_suffixes[i]);
1262 snprintf(propname, sizeof(propname), "%s",
1265 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1268 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1270 else if (obj->type == ACPI_TYPE_PACKAGE)
1271 count = acpi_gpio_package_count(obj);
1272 } else if (adev->driver_gpios) {
1273 for (gm = adev->driver_gpios; gm->name; gm++)
1274 if (strcmp(propname, gm->name) == 0) {
1283 /* Then from plain _CRS GPIOs */
1285 struct list_head resource_list;
1286 unsigned int crs_count = 0;
1288 if (!acpi_can_fallback_to_crs(adev, con_id))
1291 INIT_LIST_HEAD(&resource_list);
1292 acpi_dev_get_resources(adev, &resource_list,
1293 acpi_find_gpio_count, &crs_count);
1294 acpi_dev_free_resource_list(&resource_list);
1298 return count ? count : -ENOENT;
1301 /* Run deferred acpi_gpiochip_request_irqs() */
1302 static int acpi_gpio_handle_deferred_request_irqs(void)
1304 struct acpi_gpio_chip *acpi_gpio, *tmp;
1306 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
1307 list_for_each_entry_safe(acpi_gpio, tmp,
1308 &acpi_gpio_deferred_req_irqs_list,
1309 deferred_req_irqs_list_entry)
1310 acpi_gpiochip_request_irqs(acpi_gpio);
1312 acpi_gpio_deferred_req_irqs_done = true;
1313 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
1317 /* We must use _sync so that this runs after the first deferred_probe run */
1318 late_initcall_sync(acpi_gpio_handle_deferred_request_irqs);
1320 static const struct dmi_system_id gpiolib_acpi_quirks[] = {
1323 * The Minix Neo Z83-4 has a micro-USB-B id-pin handler for
1324 * a non existing micro-USB-B connector which puts the HDMI
1325 * DDC pins in GPIO mode, breaking HDMI support.
1328 DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
1329 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
1331 .driver_data = (void *)QUIRK_NO_EDGE_EVENTS_ON_BOOT,
1335 * The Terra Pad 1061 has a micro-USB-B id-pin handler, which
1336 * instead of controlling the actual micro-USB-B turns the 5V
1337 * boost for its USB-A connector off. The actual micro-USB-B
1338 * connector is wired for charging only.
1341 DMI_MATCH(DMI_SYS_VENDOR, "Wortmann_AG"),
1342 DMI_MATCH(DMI_PRODUCT_NAME, "TERRA_PAD_1061"),
1344 .driver_data = (void *)QUIRK_NO_EDGE_EVENTS_ON_BOOT,
1348 * Various HP X2 10 Cherry Trail models use an external
1349 * embedded-controller connected via I2C + an ACPI GPIO
1350 * event handler. The embedded controller generates various
1351 * spurious wakeup events when suspended. So disable wakeup
1352 * for its handler (it uses the only ACPI GPIO event handler).
1353 * This breaks wakeup when opening the lid, the user needs
1354 * to press the power-button to wakeup the system. The
1355 * alternative is suspend simply not working, which is worse.
1358 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1359 DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
1361 .driver_data = (void *)QUIRK_NO_WAKEUP,
1363 {} /* Terminating entry */
1366 static int acpi_gpio_setup_params(void)
1368 const struct dmi_system_id *id;
1371 id = dmi_first_match(gpiolib_acpi_quirks);
1373 quirks = (long)id->driver_data;
1375 if (run_edge_events_on_boot < 0) {
1376 if (quirks & QUIRK_NO_EDGE_EVENTS_ON_BOOT)
1377 run_edge_events_on_boot = 0;
1379 run_edge_events_on_boot = 1;
1382 if (honor_wakeup < 0) {
1383 if (quirks & QUIRK_NO_WAKEUP)
1392 /* Directly after dmi_setup() which runs as core_initcall() */
1393 postcore_initcall(acpi_gpio_setup_params);