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 fwnode_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;
404 args->fwnode = acpi_fwnode_handle(adev);
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 fwnode_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
588 if (!to_acpi_device_node(args.fwnode))
593 lookup->index = args.args[0];
594 lookup->pin_index = args.args[1];
595 lookup->active_low = !!args.args[2];
597 lookup->info.adev = to_acpi_device_node(args.fwnode);
598 lookup->info.quirks = quirks;
604 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
605 * @adev: pointer to a ACPI device to get GPIO from
606 * @propname: Property name of the GPIO (optional)
607 * @index: index of GpioIo/GpioInt resource (starting from %0)
608 * @info: info pointer to fill in (optional)
610 * Function goes through ACPI resources for @adev and based on @index looks
611 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
612 * and returns it. @index matches GpioIo/GpioInt resources only so if there
613 * are total %3 GPIO resources, the index goes from %0 to %2.
615 * If @propname is specified the GPIO is looked using device property. In
616 * that case @index is used to select the GPIO entry in the property value
617 * (in case of multiple).
619 * If the GPIO cannot be translated or there is an error an ERR_PTR is
622 * Note: if the GPIO resource has multiple entries in the pin list, this
623 * function only returns the first.
625 static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
626 const char *propname, int index,
627 struct acpi_gpio_info *info)
629 struct acpi_gpio_lookup lookup;
633 return ERR_PTR(-ENODEV);
635 memset(&lookup, 0, sizeof(lookup));
636 lookup.index = index;
639 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
641 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
642 propname, index, &lookup);
646 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
647 dev_name(&lookup.info.adev->dev), lookup.index,
648 lookup.pin_index, lookup.active_low);
650 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
651 lookup.info.adev = adev;
654 ret = acpi_gpio_resource_lookup(&lookup, info);
655 return ret ? ERR_PTR(ret) : lookup.desc;
658 struct gpio_desc *acpi_find_gpio(struct device *dev,
661 enum gpiod_flags *dflags,
662 enum gpio_lookup_flags *lookupflags)
664 struct acpi_device *adev = ACPI_COMPANION(dev);
665 struct acpi_gpio_info info;
666 struct gpio_desc *desc;
670 /* Try first from _DSD */
671 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
673 snprintf(propname, sizeof(propname), "%s-%s",
674 con_id, gpio_suffixes[i]);
676 snprintf(propname, sizeof(propname), "%s",
680 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
683 if (PTR_ERR(desc) == -EPROBE_DEFER)
684 return ERR_CAST(desc);
687 /* Then from plain _CRS GPIOs */
689 if (!acpi_can_fallback_to_crs(adev, con_id))
690 return ERR_PTR(-ENOENT);
692 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
698 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
699 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
700 return ERR_PTR(-ENOENT);
703 if (info.polarity == GPIO_ACTIVE_LOW)
704 *lookupflags |= GPIO_ACTIVE_LOW;
706 acpi_gpio_update_gpiod_flags(dflags, &info);
711 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
712 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
713 * @propname: Property name of the GPIO
714 * @index: index of GpioIo/GpioInt resource (starting from %0)
715 * @info: info pointer to fill in (optional)
717 * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
718 * Otherwise (ie. it is a data-only non-device object), use the property-based
719 * GPIO lookup to get to the GPIO resource with the relevant information and use
720 * that to obtain the GPIO descriptor to return.
722 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
723 const char *propname, int index,
724 struct acpi_gpio_info *info)
726 struct acpi_gpio_lookup lookup;
727 struct acpi_device *adev;
730 adev = to_acpi_device_node(fwnode);
732 return acpi_get_gpiod_by_index(adev, propname, index, info);
734 if (!is_acpi_data_node(fwnode))
735 return ERR_PTR(-ENODEV);
738 return ERR_PTR(-EINVAL);
740 memset(&lookup, 0, sizeof(lookup));
741 lookup.index = index;
743 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
747 ret = acpi_gpio_resource_lookup(&lookup, info);
748 return ret ? ERR_PTR(ret) : lookup.desc;
752 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
753 * @adev: pointer to a ACPI device to get IRQ from
754 * @index: index of GpioInt resource (starting from %0)
756 * If the device has one or more GpioInt resources, this function can be
757 * used to translate from the GPIO offset in the resource to the Linux IRQ
760 * The function is idempotent, though each time it runs it will configure GPIO
761 * pin direction according to the flags in GpioInt resource.
763 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
765 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
768 unsigned int irq_flags;
771 for (i = 0, idx = 0; idx <= index; i++) {
772 struct acpi_gpio_info info;
773 struct gpio_desc *desc;
775 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
777 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
778 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
779 return PTR_ERR(desc);
781 if (info.gpioint && idx++ == index) {
786 return PTR_ERR(desc);
788 irq = gpiod_to_irq(desc);
792 snprintf(label, sizeof(label), "GpioInt() %d", index);
793 ret = gpiod_configure_flags(desc, label, 0, info.flags);
797 irq_flags = acpi_dev_get_irq_type(info.triggering,
800 /* Set type if specified and different than the current one */
801 if (irq_flags != IRQ_TYPE_NONE &&
802 irq_flags != irq_get_trigger_type(irq))
803 irq_set_irq_type(irq, irq_flags);
811 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
814 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
815 u32 bits, u64 *value, void *handler_context,
816 void *region_context)
818 struct acpi_gpio_chip *achip = region_context;
819 struct gpio_chip *chip = achip->chip;
820 struct acpi_resource_gpio *agpio;
821 struct acpi_resource *ares;
822 int pin_index = (int)address;
827 status = acpi_buffer_to_resource(achip->conn_info.connection,
828 achip->conn_info.length, &ares);
829 if (ACPI_FAILURE(status))
832 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
834 return AE_BAD_PARAMETER;
837 agpio = &ares->data.gpio;
839 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
840 function == ACPI_WRITE)) {
842 return AE_BAD_PARAMETER;
845 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
846 for (i = pin_index; i < length; ++i) {
847 int pin = agpio->pin_table[i];
848 struct acpi_gpio_connection *conn;
849 struct gpio_desc *desc;
852 mutex_lock(&achip->conn_lock);
855 list_for_each_entry(conn, &achip->conns, node) {
856 if (conn->pin == pin) {
864 * The same GPIO can be shared between operation region and
865 * event but only if the access here is ACPI_READ. In that
866 * case we "borrow" the event GPIO instead.
868 if (!found && agpio->sharable == ACPI_SHARED &&
869 function == ACPI_READ) {
870 struct acpi_gpio_event *event;
872 list_for_each_entry(event, &achip->events, node) {
873 if (event->pin == pin) {
882 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
883 const char *label = "ACPI:OpRegion";
886 desc = gpiochip_request_own_desc(chip, pin, label);
889 mutex_unlock(&achip->conn_lock);
893 err = gpiod_configure_flags(desc, label, 0, flags);
895 status = AE_NOT_CONFIGURED;
896 gpiochip_free_own_desc(desc);
897 mutex_unlock(&achip->conn_lock);
901 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
903 status = AE_NO_MEMORY;
904 gpiochip_free_own_desc(desc);
905 mutex_unlock(&achip->conn_lock);
911 list_add_tail(&conn->node, &achip->conns);
914 mutex_unlock(&achip->conn_lock);
916 if (function == ACPI_WRITE)
917 gpiod_set_raw_value_cansleep(desc,
918 !!((1 << i) & *value));
920 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
928 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
930 struct gpio_chip *chip = achip->chip;
931 acpi_handle handle = ACPI_HANDLE(chip->parent);
934 INIT_LIST_HEAD(&achip->conns);
935 mutex_init(&achip->conn_lock);
936 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
937 acpi_gpio_adr_space_handler,
939 if (ACPI_FAILURE(status))
940 dev_err(chip->parent,
941 "Failed to install GPIO OpRegion handler\n");
944 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
946 struct gpio_chip *chip = achip->chip;
947 acpi_handle handle = ACPI_HANDLE(chip->parent);
948 struct acpi_gpio_connection *conn, *tmp;
951 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
952 acpi_gpio_adr_space_handler);
953 if (ACPI_FAILURE(status)) {
954 dev_err(chip->parent,
955 "Failed to remove GPIO OpRegion handler\n");
959 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
960 gpiochip_free_own_desc(conn->desc);
961 list_del(&conn->node);
966 static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
967 struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
968 const char **name, unsigned int *lflags, unsigned int *dflags)
970 struct gpio_chip *chip = achip->chip;
971 struct gpio_desc *desc;
979 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
984 desc = gpiochip_get_desc(chip, gpios[0]);
989 *lflags |= GPIO_ACTIVE_LOW;
991 if (fwnode_property_present(fwnode, "input"))
993 else if (fwnode_property_present(fwnode, "output-low"))
994 *dflags |= GPIOD_OUT_LOW;
995 else if (fwnode_property_present(fwnode, "output-high"))
996 *dflags |= GPIOD_OUT_HIGH;
998 return ERR_PTR(-EINVAL);
1000 fwnode_property_read_string(fwnode, "line-name", name);
1005 static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1007 struct gpio_chip *chip = achip->chip;
1008 struct fwnode_handle *fwnode;
1010 device_for_each_child_node(chip->parent, fwnode) {
1011 unsigned int lflags, dflags;
1012 struct gpio_desc *desc;
1016 if (!fwnode_property_present(fwnode, "gpio-hog"))
1019 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1024 ret = gpiod_hog(desc, name, lflags, dflags);
1026 dev_err(chip->parent, "Failed to hog GPIO\n");
1027 fwnode_handle_put(fwnode);
1033 void acpi_gpiochip_add(struct gpio_chip *chip)
1035 struct acpi_gpio_chip *acpi_gpio;
1039 if (!chip || !chip->parent)
1042 handle = ACPI_HANDLE(chip->parent);
1046 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1048 dev_err(chip->parent,
1049 "Failed to allocate memory for ACPI GPIO chip\n");
1053 acpi_gpio->chip = chip;
1054 INIT_LIST_HEAD(&acpi_gpio->events);
1056 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1057 if (ACPI_FAILURE(status)) {
1058 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
1064 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
1066 acpi_gpiochip_request_regions(acpi_gpio);
1067 acpi_gpiochip_scan_gpios(acpi_gpio);
1068 acpi_walk_dep_device_list(handle);
1071 void acpi_gpiochip_remove(struct gpio_chip *chip)
1073 struct acpi_gpio_chip *acpi_gpio;
1077 if (!chip || !chip->parent)
1080 handle = ACPI_HANDLE(chip->parent);
1084 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1085 if (ACPI_FAILURE(status)) {
1086 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
1090 acpi_gpiochip_free_regions(acpi_gpio);
1092 acpi_detach_data(handle, acpi_gpio_chip_dh);
1096 static int acpi_gpio_package_count(const union acpi_object *obj)
1098 const union acpi_object *element = obj->package.elements;
1099 const union acpi_object *end = element + obj->package.count;
1100 unsigned int count = 0;
1102 while (element < end) {
1103 switch (element->type) {
1104 case ACPI_TYPE_LOCAL_REFERENCE:
1107 case ACPI_TYPE_INTEGER:
1120 static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1122 unsigned int *count = data;
1124 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1125 *count += ares->data.gpio.pin_table_length;
1131 * acpi_gpio_count - return the number of GPIOs associated with a
1132 * device / function or -ENOENT if no GPIO has been
1133 * assigned to the requested function.
1134 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1135 * @con_id: function within the GPIO consumer
1137 int acpi_gpio_count(struct device *dev, const char *con_id)
1139 struct acpi_device *adev = ACPI_COMPANION(dev);
1140 const union acpi_object *obj;
1141 const struct acpi_gpio_mapping *gm;
1142 int count = -ENOENT;
1147 /* Try first from _DSD */
1148 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1150 snprintf(propname, sizeof(propname), "%s-%s",
1151 con_id, gpio_suffixes[i]);
1153 snprintf(propname, sizeof(propname), "%s",
1156 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1159 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1161 else if (obj->type == ACPI_TYPE_PACKAGE)
1162 count = acpi_gpio_package_count(obj);
1163 } else if (adev->driver_gpios) {
1164 for (gm = adev->driver_gpios; gm->name; gm++)
1165 if (strcmp(propname, gm->name) == 0) {
1174 /* Then from plain _CRS GPIOs */
1176 struct list_head resource_list;
1177 unsigned int crs_count = 0;
1179 if (!acpi_can_fallback_to_crs(adev, con_id))
1182 INIT_LIST_HEAD(&resource_list);
1183 acpi_dev_get_resources(adev, &resource_list,
1184 acpi_find_gpio_count, &crs_count);
1185 acpi_dev_free_resource_list(&resource_list);
1189 return count ? count : -ENOENT;
1192 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
1194 /* Never allow fallback if the device has properties */
1195 if (adev->data.properties || adev->driver_gpios)
1198 return con_id == NULL;
1201 /* Sync the initial state of handlers after all builtin drivers have probed */
1202 static int acpi_gpio_initial_sync(void)
1204 struct acpi_gpio_event *event, *ep;
1206 mutex_lock(&acpi_gpio_initial_sync_list_lock);
1207 list_for_each_entry_safe(event, ep, &acpi_gpio_initial_sync_list,
1208 initial_sync_list) {
1209 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
1210 list_del_init(&event->initial_sync_list);
1212 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
1216 /* We must use _sync so that this runs after the first deferred_probe run */
1217 late_initcall_sync(acpi_gpio_initial_sync);