2 * ACPI helpers for GPIO API
4 * Copyright (C) 2012, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
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.
13 #include <linux/errno.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/export.h>
19 #include <linux/acpi.h>
20 #include <linux/interrupt.h>
21 #include <linux/mutex.h>
22 #include <linux/pinctrl/pinctrl.h>
26 struct acpi_gpio_event {
27 struct list_head node;
28 struct list_head initial_sync_list;
32 struct gpio_desc *desc;
35 struct acpi_gpio_connection {
36 struct list_head node;
38 struct gpio_desc *desc;
41 struct acpi_gpio_chip {
43 * ACPICA requires that the first field of the context parameter
44 * passed to acpi_install_address_space_handler() is large enough
45 * to hold struct acpi_connection_info.
47 struct acpi_connection_info conn_info;
48 struct list_head conns;
49 struct mutex conn_lock;
50 struct gpio_chip *chip;
51 struct list_head events;
54 static LIST_HEAD(acpi_gpio_initial_sync_list);
55 static DEFINE_MUTEX(acpi_gpio_initial_sync_list_lock);
57 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
62 return ACPI_HANDLE(gc->parent) == data;
66 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
67 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
68 * @pin: ACPI GPIO pin number (0-based, controller-relative)
70 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
71 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
72 * controller does not have gpiochip registered at the moment. This is to
73 * support probe deferral.
75 static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
77 struct gpio_chip *chip;
81 status = acpi_get_handle(NULL, path, &handle);
82 if (ACPI_FAILURE(status))
83 return ERR_PTR(-ENODEV);
85 chip = gpiochip_find(handle, acpi_gpiochip_find);
87 return ERR_PTR(-EPROBE_DEFER);
89 return gpiochip_get_desc(chip, pin);
92 static void acpi_gpio_add_to_initial_sync_list(struct acpi_gpio_event *event)
94 mutex_lock(&acpi_gpio_initial_sync_list_lock);
95 list_add(&event->initial_sync_list, &acpi_gpio_initial_sync_list);
96 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
99 static void acpi_gpio_del_from_initial_sync_list(struct acpi_gpio_event *event)
101 mutex_lock(&acpi_gpio_initial_sync_list_lock);
102 if (!list_empty(&event->initial_sync_list))
103 list_del_init(&event->initial_sync_list);
104 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
107 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
109 struct acpi_gpio_event *event = data;
111 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
116 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
118 struct acpi_gpio_event *event = data;
120 acpi_execute_simple_method(event->handle, NULL, event->pin);
125 static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
127 /* The address of this function is used as a key. */
130 bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
131 struct acpi_resource_gpio **agpio)
133 struct acpi_resource_gpio *gpio;
135 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
138 gpio = &ares->data.gpio;
139 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
145 EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
147 static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
150 struct acpi_gpio_chip *acpi_gpio = context;
151 struct gpio_chip *chip = acpi_gpio->chip;
152 struct acpi_resource_gpio *agpio;
153 acpi_handle handle, evt_handle;
154 struct acpi_gpio_event *event;
155 irq_handler_t handler = NULL;
156 struct gpio_desc *desc;
157 unsigned long irqflags;
158 int ret, pin, irq, value;
160 if (!acpi_gpio_get_irq_resource(ares, &agpio))
163 handle = ACPI_HANDLE(chip->parent);
164 pin = agpio->pin_table[0];
168 sprintf(ev_name, "_%c%02hhX",
169 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
171 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
172 handler = acpi_gpio_irq_handler;
175 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
176 handler = acpi_gpio_irq_handler_evt;
181 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
183 dev_err(chip->parent, "Failed to request GPIO\n");
187 gpiod_direction_input(desc);
189 value = gpiod_get_value(desc);
191 ret = gpiochip_lock_as_irq(chip, pin);
193 dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
197 irq = gpiod_to_irq(desc);
199 dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
200 goto fail_unlock_irq;
203 irqflags = IRQF_ONESHOT;
204 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
205 if (agpio->polarity == ACPI_ACTIVE_HIGH)
206 irqflags |= IRQF_TRIGGER_HIGH;
208 irqflags |= IRQF_TRIGGER_LOW;
210 switch (agpio->polarity) {
211 case ACPI_ACTIVE_HIGH:
212 irqflags |= IRQF_TRIGGER_RISING;
214 case ACPI_ACTIVE_LOW:
215 irqflags |= IRQF_TRIGGER_FALLING;
218 irqflags |= IRQF_TRIGGER_RISING |
219 IRQF_TRIGGER_FALLING;
224 event = kzalloc(sizeof(*event), GFP_KERNEL);
226 goto fail_unlock_irq;
228 event->handle = evt_handle;
232 INIT_LIST_HEAD(&event->initial_sync_list);
234 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
235 "ACPI:Event", event);
237 dev_err(chip->parent,
238 "Failed to setup interrupt handler for %d\n",
240 goto fail_free_event;
243 if (agpio->wake_capable == ACPI_WAKE_CAPABLE)
244 enable_irq_wake(irq);
246 list_add_tail(&event->node, &acpi_gpio->events);
249 * Make sure we trigger the initial state of the IRQ when using RISING
250 * or FALLING. Note we run the handlers on late_init, the AML code
251 * may refer to OperationRegions from other (builtin) drivers which
252 * may be probed after us.
254 if (handler == acpi_gpio_irq_handler &&
255 (((irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
256 ((irqflags & IRQF_TRIGGER_FALLING) && value == 0)))
257 acpi_gpio_add_to_initial_sync_list(event);
264 gpiochip_unlock_as_irq(chip, pin);
266 gpiochip_free_own_desc(desc);
272 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
275 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
276 * handled by ACPI event methods which need to be called from the GPIO
277 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
278 * gpio pins have acpi event methods and assigns interrupt handlers that calls
279 * the acpi event methods for those pins.
281 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
283 struct acpi_gpio_chip *acpi_gpio;
287 if (!chip->parent || !chip->to_irq)
290 handle = ACPI_HANDLE(chip->parent);
294 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
295 if (ACPI_FAILURE(status))
298 acpi_walk_resources(handle, "_AEI",
299 acpi_gpiochip_request_interrupt, acpi_gpio);
301 EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
304 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
307 * Free interrupts associated with GPIO ACPI event method for the given
310 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
312 struct acpi_gpio_chip *acpi_gpio;
313 struct acpi_gpio_event *event, *ep;
317 if (!chip->parent || !chip->to_irq)
320 handle = ACPI_HANDLE(chip->parent);
324 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
325 if (ACPI_FAILURE(status))
328 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
329 struct gpio_desc *desc;
331 acpi_gpio_del_from_initial_sync_list(event);
333 if (irqd_is_wakeup_set(irq_get_irq_data(event->irq)))
334 disable_irq_wake(event->irq);
336 free_irq(event->irq, event);
338 if (WARN_ON(IS_ERR(desc)))
340 gpiochip_unlock_as_irq(chip, event->pin);
341 gpiochip_free_own_desc(desc);
342 list_del(&event->node);
346 EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
348 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
349 const struct acpi_gpio_mapping *gpios)
352 adev->driver_gpios = gpios;
357 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
359 static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
361 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
364 int devm_acpi_dev_add_driver_gpios(struct device *dev,
365 const struct acpi_gpio_mapping *gpios)
370 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
374 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
379 devres_add(dev, res);
382 EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
384 void devm_acpi_dev_remove_driver_gpios(struct device *dev)
386 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
388 EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
390 static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
391 const char *name, int index,
392 struct acpi_reference_args *args,
393 unsigned int *quirks)
395 const struct acpi_gpio_mapping *gm;
397 if (!adev->driver_gpios)
400 for (gm = adev->driver_gpios; gm->name; gm++)
401 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
402 const struct acpi_gpio_params *par = gm->data + index;
405 args->args[0] = par->crs_entry_index;
406 args->args[1] = par->line_index;
407 args->args[2] = par->active_low;
410 *quirks = gm->quirks;
417 static enum gpiod_flags
418 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
420 bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
422 switch (agpio->io_restriction) {
423 case ACPI_IO_RESTRICT_INPUT:
425 case ACPI_IO_RESTRICT_OUTPUT:
427 * ACPI GPIO resources don't contain an initial value for the
428 * GPIO. Therefore we deduce that value from the pull field
429 * instead. If the pin is pulled up we assume default to be
430 * high, otherwise low.
432 return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
435 * Assume that the BIOS has configured the direction and pull
443 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
448 * Check if the BIOS has IoRestriction with explicitly set direction
449 * and update @flags accordingly. Otherwise use whatever caller asked
452 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
453 enum gpiod_flags diff = *flags ^ update;
456 * Check if caller supplied incompatible GPIO initialization
459 * Return %-EINVAL to notify that firmware has different
460 * settings and we are going to use them.
462 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
463 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
471 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
473 struct device *dev = &info->adev->dev;
474 enum gpiod_flags old = *flags;
477 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
478 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
480 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
483 dev_dbg(dev, "Override GPIO initialization flags\n");
490 struct acpi_gpio_lookup {
491 struct acpi_gpio_info info;
495 struct gpio_desc *desc;
499 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
501 struct acpi_gpio_lookup *lookup = data;
503 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
506 if (lookup->n++ == lookup->index && !lookup->desc) {
507 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
508 int pin_index = lookup->pin_index;
510 if (pin_index >= agpio->pin_table_length)
513 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
514 agpio->pin_table[pin_index]);
515 lookup->info.gpioint =
516 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
519 * Polarity and triggering are only specified for GpioInt
521 * Note: we expect here:
522 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
523 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
525 if (lookup->info.gpioint) {
526 lookup->info.flags = GPIOD_IN;
527 lookup->info.polarity = agpio->polarity;
528 lookup->info.triggering = agpio->triggering;
530 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
531 lookup->info.polarity = lookup->active_low;
538 static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
539 struct acpi_gpio_info *info)
541 struct acpi_device *adev = lookup->info.adev;
542 struct list_head res_list;
545 INIT_LIST_HEAD(&res_list);
547 ret = acpi_dev_get_resources(adev, &res_list,
548 acpi_populate_gpio_lookup,
553 acpi_dev_free_resource_list(&res_list);
559 *info = lookup->info;
563 static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
564 const char *propname, int index,
565 struct acpi_gpio_lookup *lookup)
567 struct acpi_reference_args args;
568 unsigned int quirks = 0;
571 memset(&args, 0, sizeof(args));
572 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
575 struct acpi_device *adev = to_acpi_device_node(fwnode);
580 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
585 * The property was found and resolved, so need to lookup the GPIO based
591 lookup->index = args.args[0];
592 lookup->pin_index = args.args[1];
593 lookup->active_low = !!args.args[2];
595 lookup->info.adev = args.adev;
596 lookup->info.quirks = quirks;
601 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
602 * @adev: pointer to a ACPI device to get GPIO from
603 * @propname: Property name of the GPIO (optional)
604 * @index: index of GpioIo/GpioInt resource (starting from %0)
605 * @info: info pointer to fill in (optional)
607 * Function goes through ACPI resources for @adev and based on @index looks
608 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
609 * and returns it. @index matches GpioIo/GpioInt resources only so if there
610 * are total %3 GPIO resources, the index goes from %0 to %2.
612 * If @propname is specified the GPIO is looked using device property. In
613 * that case @index is used to select the GPIO entry in the property value
614 * (in case of multiple).
616 * If the GPIO cannot be translated or there is an error an ERR_PTR is
619 * Note: if the GPIO resource has multiple entries in the pin list, this
620 * function only returns the first.
622 static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
623 const char *propname, int index,
624 struct acpi_gpio_info *info)
626 struct acpi_gpio_lookup lookup;
630 return ERR_PTR(-ENODEV);
632 memset(&lookup, 0, sizeof(lookup));
633 lookup.index = index;
636 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
638 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
639 propname, index, &lookup);
643 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
644 dev_name(&lookup.info.adev->dev), lookup.index,
645 lookup.pin_index, lookup.active_low);
647 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
648 lookup.info.adev = adev;
651 ret = acpi_gpio_resource_lookup(&lookup, info);
652 return ret ? ERR_PTR(ret) : lookup.desc;
655 struct gpio_desc *acpi_find_gpio(struct device *dev,
658 enum gpiod_flags *dflags,
659 enum gpio_lookup_flags *lookupflags)
661 struct acpi_device *adev = ACPI_COMPANION(dev);
662 struct acpi_gpio_info info;
663 struct gpio_desc *desc;
667 /* Try first from _DSD */
668 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
670 snprintf(propname, sizeof(propname), "%s-%s",
671 con_id, gpio_suffixes[i]);
673 snprintf(propname, sizeof(propname), "%s",
677 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
680 if (PTR_ERR(desc) == -EPROBE_DEFER)
681 return ERR_CAST(desc);
684 /* Then from plain _CRS GPIOs */
686 if (!acpi_can_fallback_to_crs(adev, con_id))
687 return ERR_PTR(-ENOENT);
689 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
695 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
696 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
697 return ERR_PTR(-ENOENT);
700 if (info.polarity == GPIO_ACTIVE_LOW)
701 *lookupflags |= GPIO_ACTIVE_LOW;
703 acpi_gpio_update_gpiod_flags(dflags, &info);
708 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
709 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
710 * @propname: Property name of the GPIO
711 * @index: index of GpioIo/GpioInt resource (starting from %0)
712 * @info: info pointer to fill in (optional)
714 * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
715 * Otherwise (ie. it is a data-only non-device object), use the property-based
716 * GPIO lookup to get to the GPIO resource with the relevant information and use
717 * that to obtain the GPIO descriptor to return.
719 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
720 const char *propname, int index,
721 struct acpi_gpio_info *info)
723 struct acpi_gpio_lookup lookup;
724 struct acpi_device *adev;
727 adev = to_acpi_device_node(fwnode);
729 return acpi_get_gpiod_by_index(adev, propname, index, info);
731 if (!is_acpi_data_node(fwnode))
732 return ERR_PTR(-ENODEV);
735 return ERR_PTR(-EINVAL);
737 memset(&lookup, 0, sizeof(lookup));
738 lookup.index = index;
740 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
744 ret = acpi_gpio_resource_lookup(&lookup, info);
745 return ret ? ERR_PTR(ret) : lookup.desc;
749 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
750 * @adev: pointer to a ACPI device to get IRQ from
751 * @index: index of GpioInt resource (starting from %0)
753 * If the device has one or more GpioInt resources, this function can be
754 * used to translate from the GPIO offset in the resource to the Linux IRQ
757 * The function is idempotent, though each time it runs it will configure GPIO
758 * pin direction according to the flags in GpioInt resource.
760 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
762 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
765 unsigned int irq_flags;
768 for (i = 0, idx = 0; idx <= index; i++) {
769 struct acpi_gpio_info info;
770 struct gpio_desc *desc;
772 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
774 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
775 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
776 return PTR_ERR(desc);
778 if (info.gpioint && idx++ == index) {
783 return PTR_ERR(desc);
785 irq = gpiod_to_irq(desc);
789 snprintf(label, sizeof(label), "GpioInt() %d", index);
790 ret = gpiod_configure_flags(desc, label, 0, info.flags);
794 irq_flags = acpi_dev_get_irq_type(info.triggering,
797 /* Set type if specified and different than the current one */
798 if (irq_flags != IRQ_TYPE_NONE &&
799 irq_flags != irq_get_trigger_type(irq))
800 irq_set_irq_type(irq, irq_flags);
808 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
811 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
812 u32 bits, u64 *value, void *handler_context,
813 void *region_context)
815 struct acpi_gpio_chip *achip = region_context;
816 struct gpio_chip *chip = achip->chip;
817 struct acpi_resource_gpio *agpio;
818 struct acpi_resource *ares;
819 int pin_index = (int)address;
824 status = acpi_buffer_to_resource(achip->conn_info.connection,
825 achip->conn_info.length, &ares);
826 if (ACPI_FAILURE(status))
829 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
831 return AE_BAD_PARAMETER;
834 agpio = &ares->data.gpio;
836 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
837 function == ACPI_WRITE)) {
839 return AE_BAD_PARAMETER;
842 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
843 for (i = pin_index; i < length; ++i) {
844 int pin = agpio->pin_table[i];
845 struct acpi_gpio_connection *conn;
846 struct gpio_desc *desc;
849 mutex_lock(&achip->conn_lock);
852 list_for_each_entry(conn, &achip->conns, node) {
853 if (conn->pin == pin) {
861 * The same GPIO can be shared between operation region and
862 * event but only if the access here is ACPI_READ. In that
863 * case we "borrow" the event GPIO instead.
865 if (!found && agpio->sharable == ACPI_SHARED &&
866 function == ACPI_READ) {
867 struct acpi_gpio_event *event;
869 list_for_each_entry(event, &achip->events, node) {
870 if (event->pin == pin) {
879 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
880 const char *label = "ACPI:OpRegion";
883 desc = gpiochip_request_own_desc(chip, pin, label);
886 mutex_unlock(&achip->conn_lock);
890 err = gpiod_configure_flags(desc, label, 0, flags);
892 status = AE_NOT_CONFIGURED;
893 gpiochip_free_own_desc(desc);
894 mutex_unlock(&achip->conn_lock);
898 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
900 status = AE_NO_MEMORY;
901 gpiochip_free_own_desc(desc);
902 mutex_unlock(&achip->conn_lock);
908 list_add_tail(&conn->node, &achip->conns);
911 mutex_unlock(&achip->conn_lock);
913 if (function == ACPI_WRITE)
914 gpiod_set_raw_value_cansleep(desc,
915 !!((1 << i) & *value));
917 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
925 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
927 struct gpio_chip *chip = achip->chip;
928 acpi_handle handle = ACPI_HANDLE(chip->parent);
931 INIT_LIST_HEAD(&achip->conns);
932 mutex_init(&achip->conn_lock);
933 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
934 acpi_gpio_adr_space_handler,
936 if (ACPI_FAILURE(status))
937 dev_err(chip->parent,
938 "Failed to install GPIO OpRegion handler\n");
941 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
943 struct gpio_chip *chip = achip->chip;
944 acpi_handle handle = ACPI_HANDLE(chip->parent);
945 struct acpi_gpio_connection *conn, *tmp;
948 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
949 acpi_gpio_adr_space_handler);
950 if (ACPI_FAILURE(status)) {
951 dev_err(chip->parent,
952 "Failed to remove GPIO OpRegion handler\n");
956 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
957 gpiochip_free_own_desc(conn->desc);
958 list_del(&conn->node);
963 static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
964 struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
965 const char **name, unsigned int *lflags, unsigned int *dflags)
967 struct gpio_chip *chip = achip->chip;
968 struct gpio_desc *desc;
976 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
981 desc = gpiochip_get_desc(chip, gpios[0]);
986 *lflags |= GPIO_ACTIVE_LOW;
988 if (fwnode_property_present(fwnode, "input"))
990 else if (fwnode_property_present(fwnode, "output-low"))
991 *dflags |= GPIOD_OUT_LOW;
992 else if (fwnode_property_present(fwnode, "output-high"))
993 *dflags |= GPIOD_OUT_HIGH;
995 return ERR_PTR(-EINVAL);
997 fwnode_property_read_string(fwnode, "line-name", name);
1002 static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1004 struct gpio_chip *chip = achip->chip;
1005 struct fwnode_handle *fwnode;
1007 device_for_each_child_node(chip->parent, fwnode) {
1008 unsigned int lflags, dflags;
1009 struct gpio_desc *desc;
1013 if (!fwnode_property_present(fwnode, "gpio-hog"))
1016 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1021 ret = gpiod_hog(desc, name, lflags, dflags);
1023 dev_err(chip->parent, "Failed to hog GPIO\n");
1024 fwnode_handle_put(fwnode);
1030 void acpi_gpiochip_add(struct gpio_chip *chip)
1032 struct acpi_gpio_chip *acpi_gpio;
1036 if (!chip || !chip->parent)
1039 handle = ACPI_HANDLE(chip->parent);
1043 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1045 dev_err(chip->parent,
1046 "Failed to allocate memory for ACPI GPIO chip\n");
1050 acpi_gpio->chip = chip;
1051 INIT_LIST_HEAD(&acpi_gpio->events);
1053 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1054 if (ACPI_FAILURE(status)) {
1055 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
1061 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
1063 acpi_gpiochip_request_regions(acpi_gpio);
1064 acpi_gpiochip_scan_gpios(acpi_gpio);
1065 acpi_walk_dep_device_list(handle);
1068 void acpi_gpiochip_remove(struct gpio_chip *chip)
1070 struct acpi_gpio_chip *acpi_gpio;
1074 if (!chip || !chip->parent)
1077 handle = ACPI_HANDLE(chip->parent);
1081 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1082 if (ACPI_FAILURE(status)) {
1083 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
1087 acpi_gpiochip_free_regions(acpi_gpio);
1089 acpi_detach_data(handle, acpi_gpio_chip_dh);
1093 static int acpi_gpio_package_count(const union acpi_object *obj)
1095 const union acpi_object *element = obj->package.elements;
1096 const union acpi_object *end = element + obj->package.count;
1097 unsigned int count = 0;
1099 while (element < end) {
1100 switch (element->type) {
1101 case ACPI_TYPE_LOCAL_REFERENCE:
1104 case ACPI_TYPE_INTEGER:
1117 static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1119 unsigned int *count = data;
1121 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1122 *count += ares->data.gpio.pin_table_length;
1128 * acpi_gpio_count - return the number of GPIOs associated with a
1129 * device / function or -ENOENT if no GPIO has been
1130 * assigned to the requested function.
1131 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1132 * @con_id: function within the GPIO consumer
1134 int acpi_gpio_count(struct device *dev, const char *con_id)
1136 struct acpi_device *adev = ACPI_COMPANION(dev);
1137 const union acpi_object *obj;
1138 const struct acpi_gpio_mapping *gm;
1139 int count = -ENOENT;
1144 /* Try first from _DSD */
1145 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1147 snprintf(propname, sizeof(propname), "%s-%s",
1148 con_id, gpio_suffixes[i]);
1150 snprintf(propname, sizeof(propname), "%s",
1153 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1156 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1158 else if (obj->type == ACPI_TYPE_PACKAGE)
1159 count = acpi_gpio_package_count(obj);
1160 } else if (adev->driver_gpios) {
1161 for (gm = adev->driver_gpios; gm->name; gm++)
1162 if (strcmp(propname, gm->name) == 0) {
1171 /* Then from plain _CRS GPIOs */
1173 struct list_head resource_list;
1174 unsigned int crs_count = 0;
1176 if (!acpi_can_fallback_to_crs(adev, con_id))
1179 INIT_LIST_HEAD(&resource_list);
1180 acpi_dev_get_resources(adev, &resource_list,
1181 acpi_find_gpio_count, &crs_count);
1182 acpi_dev_free_resource_list(&resource_list);
1186 return count ? count : -ENOENT;
1189 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
1191 /* Never allow fallback if the device has properties */
1192 if (adev->data.properties || adev->driver_gpios)
1195 return con_id == NULL;
1198 /* Sync the initial state of handlers after all builtin drivers have probed */
1199 static int acpi_gpio_initial_sync(void)
1201 struct acpi_gpio_event *event, *ep;
1203 mutex_lock(&acpi_gpio_initial_sync_list_lock);
1204 list_for_each_entry_safe(event, ep, &acpi_gpio_initial_sync_list,
1205 initial_sync_list) {
1206 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
1207 list_del_init(&event->initial_sync_list);
1209 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
1213 /* We must use _sync so that this runs after the first deferred_probe run */
1214 late_initcall_sync(acpi_gpio_initial_sync);