1 // SPDX-License-Identifier: GPL-2.0
3 * ACPI helpers for GPIO API
5 * Copyright (C) 2012, Intel Corporation
6 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/errno.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/gpio/machine.h>
14 #include <linux/export.h>
15 #include <linux/acpi.h>
16 #include <linux/interrupt.h>
17 #include <linux/mutex.h>
18 #include <linux/pinctrl/pinctrl.h>
22 struct acpi_gpio_event
{
23 struct list_head node
;
27 struct gpio_desc
*desc
;
30 struct acpi_gpio_connection
{
31 struct list_head node
;
33 struct gpio_desc
*desc
;
36 struct acpi_gpio_chip
{
38 * ACPICA requires that the first field of the context parameter
39 * passed to acpi_install_address_space_handler() is large enough
40 * to hold struct acpi_connection_info.
42 struct acpi_connection_info conn_info
;
43 struct list_head conns
;
44 struct mutex conn_lock
;
45 struct gpio_chip
*chip
;
46 struct list_head events
;
47 struct list_head deferred_req_irqs_list_entry
;
51 * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init
52 * (so builtin drivers) we register the ACPI GpioInt event handlers from a
53 * late_initcall_sync handler, so that other builtin drivers can register their
54 * OpRegions before the event handlers can run. This list contains gpiochips
55 * for which the acpi_gpiochip_request_interrupts() has been deferred.
57 static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock
);
58 static LIST_HEAD(acpi_gpio_deferred_req_irqs_list
);
59 static bool acpi_gpio_deferred_req_irqs_done
;
61 static int acpi_gpiochip_find(struct gpio_chip
*gc
, void *data
)
66 return ACPI_HANDLE(gc
->parent
) == data
;
70 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
71 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
72 * @pin: ACPI GPIO pin number (0-based, controller-relative)
74 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
75 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
76 * controller does not have gpiochip registered at the moment. This is to
77 * support probe deferral.
79 static struct gpio_desc
*acpi_get_gpiod(char *path
, int pin
)
81 struct gpio_chip
*chip
;
85 status
= acpi_get_handle(NULL
, path
, &handle
);
86 if (ACPI_FAILURE(status
))
87 return ERR_PTR(-ENODEV
);
89 chip
= gpiochip_find(handle
, acpi_gpiochip_find
);
91 return ERR_PTR(-EPROBE_DEFER
);
93 return gpiochip_get_desc(chip
, pin
);
96 static irqreturn_t
acpi_gpio_irq_handler(int irq
, void *data
)
98 struct acpi_gpio_event
*event
= data
;
100 acpi_evaluate_object(event
->handle
, NULL
, NULL
, NULL
);
105 static irqreturn_t
acpi_gpio_irq_handler_evt(int irq
, void *data
)
107 struct acpi_gpio_event
*event
= data
;
109 acpi_execute_simple_method(event
->handle
, NULL
, event
->pin
);
114 static void acpi_gpio_chip_dh(acpi_handle handle
, void *data
)
116 /* The address of this function is used as a key. */
119 bool acpi_gpio_get_irq_resource(struct acpi_resource
*ares
,
120 struct acpi_resource_gpio
**agpio
)
122 struct acpi_resource_gpio
*gpio
;
124 if (ares
->type
!= ACPI_RESOURCE_TYPE_GPIO
)
127 gpio
= &ares
->data
.gpio
;
128 if (gpio
->connection_type
!= ACPI_RESOURCE_GPIO_TYPE_INT
)
134 EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource
);
136 static acpi_status
acpi_gpiochip_request_interrupt(struct acpi_resource
*ares
,
139 struct acpi_gpio_chip
*acpi_gpio
= context
;
140 struct gpio_chip
*chip
= acpi_gpio
->chip
;
141 struct acpi_resource_gpio
*agpio
;
142 acpi_handle handle
, evt_handle
;
143 struct acpi_gpio_event
*event
;
144 irq_handler_t handler
= NULL
;
145 struct gpio_desc
*desc
;
146 unsigned long irqflags
;
147 int ret
, pin
, irq
, value
;
149 if (!acpi_gpio_get_irq_resource(ares
, &agpio
))
152 handle
= ACPI_HANDLE(chip
->parent
);
153 pin
= agpio
->pin_table
[0];
157 sprintf(ev_name
, "_%c%02hhX",
158 agpio
->triggering
== ACPI_EDGE_SENSITIVE
? 'E' : 'L',
160 if (ACPI_SUCCESS(acpi_get_handle(handle
, ev_name
, &evt_handle
)))
161 handler
= acpi_gpio_irq_handler
;
164 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_EVT", &evt_handle
)))
165 handler
= acpi_gpio_irq_handler_evt
;
170 desc
= gpiochip_request_own_desc(chip
, pin
, "ACPI:Event");
172 dev_err(chip
->parent
, "Failed to request GPIO\n");
176 gpiod_direction_input(desc
);
178 value
= gpiod_get_value_cansleep(desc
);
180 ret
= gpiochip_lock_as_irq(chip
, pin
);
182 dev_err(chip
->parent
, "Failed to lock GPIO as interrupt\n");
186 irq
= gpiod_to_irq(desc
);
188 dev_err(chip
->parent
, "Failed to translate GPIO to IRQ\n");
189 goto fail_unlock_irq
;
192 irqflags
= IRQF_ONESHOT
;
193 if (agpio
->triggering
== ACPI_LEVEL_SENSITIVE
) {
194 if (agpio
->polarity
== ACPI_ACTIVE_HIGH
)
195 irqflags
|= IRQF_TRIGGER_HIGH
;
197 irqflags
|= IRQF_TRIGGER_LOW
;
199 switch (agpio
->polarity
) {
200 case ACPI_ACTIVE_HIGH
:
201 irqflags
|= IRQF_TRIGGER_RISING
;
203 case ACPI_ACTIVE_LOW
:
204 irqflags
|= IRQF_TRIGGER_FALLING
;
207 irqflags
|= IRQF_TRIGGER_RISING
|
208 IRQF_TRIGGER_FALLING
;
213 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
215 goto fail_unlock_irq
;
217 event
->handle
= evt_handle
;
222 ret
= request_threaded_irq(event
->irq
, NULL
, handler
, irqflags
,
223 "ACPI:Event", event
);
225 dev_err(chip
->parent
,
226 "Failed to setup interrupt handler for %d\n",
228 goto fail_free_event
;
231 if (agpio
->wake_capable
== ACPI_WAKE_CAPABLE
)
232 enable_irq_wake(irq
);
234 list_add_tail(&event
->node
, &acpi_gpio
->events
);
237 * Make sure we trigger the initial state of the IRQ when using RISING
238 * or FALLING. Note we run the handlers on late_init, the AML code
239 * may refer to OperationRegions from other (builtin) drivers which
240 * may be probed after us.
242 if (((irqflags
& IRQF_TRIGGER_RISING
) && value
== 1) ||
243 ((irqflags
& IRQF_TRIGGER_FALLING
) && value
== 0))
244 handler(event
->irq
, event
);
251 gpiochip_unlock_as_irq(chip
, pin
);
253 gpiochip_free_own_desc(desc
);
259 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
262 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
263 * handled by ACPI event methods which need to be called from the GPIO
264 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
265 * gpio pins have acpi event methods and assigns interrupt handlers that calls
266 * the acpi event methods for those pins.
268 void acpi_gpiochip_request_interrupts(struct gpio_chip
*chip
)
270 struct acpi_gpio_chip
*acpi_gpio
;
275 if (!chip
->parent
|| !chip
->to_irq
)
278 handle
= ACPI_HANDLE(chip
->parent
);
282 status
= acpi_get_data(handle
, acpi_gpio_chip_dh
, (void **)&acpi_gpio
);
283 if (ACPI_FAILURE(status
))
286 mutex_lock(&acpi_gpio_deferred_req_irqs_lock
);
287 defer
= !acpi_gpio_deferred_req_irqs_done
;
289 list_add(&acpi_gpio
->deferred_req_irqs_list_entry
,
290 &acpi_gpio_deferred_req_irqs_list
);
291 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock
);
296 acpi_walk_resources(handle
, "_AEI",
297 acpi_gpiochip_request_interrupt
, acpi_gpio
);
299 EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts
);
302 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
305 * Free interrupts associated with GPIO ACPI event method for the given
308 void acpi_gpiochip_free_interrupts(struct gpio_chip
*chip
)
310 struct acpi_gpio_chip
*acpi_gpio
;
311 struct acpi_gpio_event
*event
, *ep
;
315 if (!chip
->parent
|| !chip
->to_irq
)
318 handle
= ACPI_HANDLE(chip
->parent
);
322 status
= acpi_get_data(handle
, acpi_gpio_chip_dh
, (void **)&acpi_gpio
);
323 if (ACPI_FAILURE(status
))
326 mutex_lock(&acpi_gpio_deferred_req_irqs_lock
);
327 if (!list_empty(&acpi_gpio
->deferred_req_irqs_list_entry
))
328 list_del_init(&acpi_gpio
->deferred_req_irqs_list_entry
);
329 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock
);
331 list_for_each_entry_safe_reverse(event
, ep
, &acpi_gpio
->events
, node
) {
332 struct gpio_desc
*desc
;
334 if (irqd_is_wakeup_set(irq_get_irq_data(event
->irq
)))
335 disable_irq_wake(event
->irq
);
337 free_irq(event
->irq
, event
);
339 if (WARN_ON(IS_ERR(desc
)))
341 gpiochip_unlock_as_irq(chip
, event
->pin
);
342 gpiochip_free_own_desc(desc
);
343 list_del(&event
->node
);
347 EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts
);
349 int acpi_dev_add_driver_gpios(struct acpi_device
*adev
,
350 const struct acpi_gpio_mapping
*gpios
)
353 adev
->driver_gpios
= gpios
;
358 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios
);
360 static void devm_acpi_dev_release_driver_gpios(struct device
*dev
, void *res
)
362 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev
));
365 int devm_acpi_dev_add_driver_gpios(struct device
*dev
,
366 const struct acpi_gpio_mapping
*gpios
)
371 res
= devres_alloc(devm_acpi_dev_release_driver_gpios
, 0, GFP_KERNEL
);
375 ret
= acpi_dev_add_driver_gpios(ACPI_COMPANION(dev
), gpios
);
380 devres_add(dev
, res
);
383 EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios
);
385 void devm_acpi_dev_remove_driver_gpios(struct device
*dev
)
387 WARN_ON(devres_release(dev
, devm_acpi_dev_release_driver_gpios
, NULL
, NULL
));
389 EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios
);
391 static bool acpi_get_driver_gpio_data(struct acpi_device
*adev
,
392 const char *name
, int index
,
393 struct fwnode_reference_args
*args
,
394 unsigned int *quirks
)
396 const struct acpi_gpio_mapping
*gm
;
398 if (!adev
->driver_gpios
)
401 for (gm
= adev
->driver_gpios
; gm
->name
; gm
++)
402 if (!strcmp(name
, gm
->name
) && gm
->data
&& index
< gm
->size
) {
403 const struct acpi_gpio_params
*par
= gm
->data
+ index
;
405 args
->fwnode
= acpi_fwnode_handle(adev
);
406 args
->args
[0] = par
->crs_entry_index
;
407 args
->args
[1] = par
->line_index
;
408 args
->args
[2] = par
->active_low
;
411 *quirks
= gm
->quirks
;
418 static enum gpiod_flags
419 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio
*agpio
)
421 bool pull_up
= agpio
->pin_config
== ACPI_PIN_CONFIG_PULLUP
;
423 switch (agpio
->io_restriction
) {
424 case ACPI_IO_RESTRICT_INPUT
:
426 case ACPI_IO_RESTRICT_OUTPUT
:
428 * ACPI GPIO resources don't contain an initial value for the
429 * GPIO. Therefore we deduce that value from the pull field
430 * instead. If the pin is pulled up we assume default to be
431 * high, otherwise low.
433 return pull_up
? GPIOD_OUT_HIGH
: GPIOD_OUT_LOW
;
436 * Assume that the BIOS has configured the direction and pull
444 __acpi_gpio_update_gpiod_flags(enum gpiod_flags
*flags
, enum gpiod_flags update
)
449 * Check if the BIOS has IoRestriction with explicitly set direction
450 * and update @flags accordingly. Otherwise use whatever caller asked
453 if (update
& GPIOD_FLAGS_BIT_DIR_SET
) {
454 enum gpiod_flags diff
= *flags
^ update
;
457 * Check if caller supplied incompatible GPIO initialization
460 * Return %-EINVAL to notify that firmware has different
461 * settings and we are going to use them.
463 if (((*flags
& GPIOD_FLAGS_BIT_DIR_SET
) && (diff
& GPIOD_FLAGS_BIT_DIR_OUT
)) ||
464 ((*flags
& GPIOD_FLAGS_BIT_DIR_OUT
) && (diff
& GPIOD_FLAGS_BIT_DIR_VAL
)))
472 acpi_gpio_update_gpiod_flags(enum gpiod_flags
*flags
, struct acpi_gpio_info
*info
)
474 struct device
*dev
= &info
->adev
->dev
;
475 enum gpiod_flags old
= *flags
;
478 ret
= __acpi_gpio_update_gpiod_flags(&old
, info
->flags
);
479 if (info
->quirks
& ACPI_GPIO_QUIRK_NO_IO_RESTRICTION
) {
481 dev_warn(dev
, FW_BUG
"GPIO not in correct mode, fixing\n");
484 dev_dbg(dev
, "Override GPIO initialization flags\n");
491 struct acpi_gpio_lookup
{
492 struct acpi_gpio_info info
;
496 struct gpio_desc
*desc
;
500 static int acpi_populate_gpio_lookup(struct acpi_resource
*ares
, void *data
)
502 struct acpi_gpio_lookup
*lookup
= data
;
504 if (ares
->type
!= ACPI_RESOURCE_TYPE_GPIO
)
507 if (lookup
->n
++ == lookup
->index
&& !lookup
->desc
) {
508 const struct acpi_resource_gpio
*agpio
= &ares
->data
.gpio
;
509 int pin_index
= lookup
->pin_index
;
511 if (pin_index
>= agpio
->pin_table_length
)
514 lookup
->desc
= acpi_get_gpiod(agpio
->resource_source
.string_ptr
,
515 agpio
->pin_table
[pin_index
]);
516 lookup
->info
.gpioint
=
517 agpio
->connection_type
== ACPI_RESOURCE_GPIO_TYPE_INT
;
520 * Polarity and triggering are only specified for GpioInt
522 * Note: we expect here:
523 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
524 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
526 if (lookup
->info
.gpioint
) {
527 lookup
->info
.flags
= GPIOD_IN
;
528 lookup
->info
.polarity
= agpio
->polarity
;
529 lookup
->info
.triggering
= agpio
->triggering
;
531 lookup
->info
.flags
= acpi_gpio_to_gpiod_flags(agpio
);
532 lookup
->info
.polarity
= lookup
->active_low
;
539 static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup
*lookup
,
540 struct acpi_gpio_info
*info
)
542 struct acpi_device
*adev
= lookup
->info
.adev
;
543 struct list_head res_list
;
546 INIT_LIST_HEAD(&res_list
);
548 ret
= acpi_dev_get_resources(adev
, &res_list
,
549 acpi_populate_gpio_lookup
,
554 acpi_dev_free_resource_list(&res_list
);
560 *info
= lookup
->info
;
564 static int acpi_gpio_property_lookup(struct fwnode_handle
*fwnode
,
565 const char *propname
, int index
,
566 struct acpi_gpio_lookup
*lookup
)
568 struct fwnode_reference_args args
;
569 unsigned int quirks
= 0;
572 memset(&args
, 0, sizeof(args
));
573 ret
= __acpi_node_get_property_reference(fwnode
, propname
, index
, 3,
576 struct acpi_device
*adev
= to_acpi_device_node(fwnode
);
581 if (!acpi_get_driver_gpio_data(adev
, propname
, index
, &args
,
586 * The property was found and resolved, so need to lookup the GPIO based
589 if (!to_acpi_device_node(args
.fwnode
))
594 lookup
->index
= args
.args
[0];
595 lookup
->pin_index
= args
.args
[1];
596 lookup
->active_low
= !!args
.args
[2];
598 lookup
->info
.adev
= to_acpi_device_node(args
.fwnode
);
599 lookup
->info
.quirks
= quirks
;
605 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
606 * @adev: pointer to a ACPI device to get GPIO from
607 * @propname: Property name of the GPIO (optional)
608 * @index: index of GpioIo/GpioInt resource (starting from %0)
609 * @info: info pointer to fill in (optional)
611 * Function goes through ACPI resources for @adev and based on @index looks
612 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
613 * and returns it. @index matches GpioIo/GpioInt resources only so if there
614 * are total %3 GPIO resources, the index goes from %0 to %2.
616 * If @propname is specified the GPIO is looked using device property. In
617 * that case @index is used to select the GPIO entry in the property value
618 * (in case of multiple).
620 * If the GPIO cannot be translated or there is an error an ERR_PTR is
623 * Note: if the GPIO resource has multiple entries in the pin list, this
624 * function only returns the first.
626 static struct gpio_desc
*acpi_get_gpiod_by_index(struct acpi_device
*adev
,
627 const char *propname
, int index
,
628 struct acpi_gpio_info
*info
)
630 struct acpi_gpio_lookup lookup
;
634 return ERR_PTR(-ENODEV
);
636 memset(&lookup
, 0, sizeof(lookup
));
637 lookup
.index
= index
;
640 dev_dbg(&adev
->dev
, "GPIO: looking up %s\n", propname
);
642 ret
= acpi_gpio_property_lookup(acpi_fwnode_handle(adev
),
643 propname
, index
, &lookup
);
647 dev_dbg(&adev
->dev
, "GPIO: _DSD returned %s %d %d %u\n",
648 dev_name(&lookup
.info
.adev
->dev
), lookup
.index
,
649 lookup
.pin_index
, lookup
.active_low
);
651 dev_dbg(&adev
->dev
, "GPIO: looking up %d in _CRS\n", index
);
652 lookup
.info
.adev
= adev
;
655 ret
= acpi_gpio_resource_lookup(&lookup
, info
);
656 return ret
? ERR_PTR(ret
) : lookup
.desc
;
659 struct gpio_desc
*acpi_find_gpio(struct device
*dev
,
662 enum gpiod_flags
*dflags
,
663 enum gpio_lookup_flags
*lookupflags
)
665 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
666 struct acpi_gpio_info info
;
667 struct gpio_desc
*desc
;
671 /* Try first from _DSD */
672 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
674 snprintf(propname
, sizeof(propname
), "%s-%s",
675 con_id
, gpio_suffixes
[i
]);
677 snprintf(propname
, sizeof(propname
), "%s",
681 desc
= acpi_get_gpiod_by_index(adev
, propname
, idx
, &info
);
684 if (PTR_ERR(desc
) == -EPROBE_DEFER
)
685 return ERR_CAST(desc
);
688 /* Then from plain _CRS GPIOs */
690 if (!acpi_can_fallback_to_crs(adev
, con_id
))
691 return ERR_PTR(-ENOENT
);
693 desc
= acpi_get_gpiod_by_index(adev
, NULL
, idx
, &info
);
699 (*dflags
== GPIOD_OUT_LOW
|| *dflags
== GPIOD_OUT_HIGH
)) {
700 dev_dbg(dev
, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
701 return ERR_PTR(-ENOENT
);
704 if (info
.polarity
== GPIO_ACTIVE_LOW
)
705 *lookupflags
|= GPIO_ACTIVE_LOW
;
707 acpi_gpio_update_gpiod_flags(dflags
, &info
);
712 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
713 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
714 * @propname: Property name of the GPIO
715 * @index: index of GpioIo/GpioInt resource (starting from %0)
716 * @info: info pointer to fill in (optional)
718 * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
719 * Otherwise (ie. it is a data-only non-device object), use the property-based
720 * GPIO lookup to get to the GPIO resource with the relevant information and use
721 * that to obtain the GPIO descriptor to return.
723 struct gpio_desc
*acpi_node_get_gpiod(struct fwnode_handle
*fwnode
,
724 const char *propname
, int index
,
725 struct acpi_gpio_info
*info
)
727 struct acpi_gpio_lookup lookup
;
728 struct acpi_device
*adev
;
731 adev
= to_acpi_device_node(fwnode
);
733 return acpi_get_gpiod_by_index(adev
, propname
, index
, info
);
735 if (!is_acpi_data_node(fwnode
))
736 return ERR_PTR(-ENODEV
);
739 return ERR_PTR(-EINVAL
);
741 memset(&lookup
, 0, sizeof(lookup
));
742 lookup
.index
= index
;
744 ret
= acpi_gpio_property_lookup(fwnode
, propname
, index
, &lookup
);
748 ret
= acpi_gpio_resource_lookup(&lookup
, info
);
749 return ret
? ERR_PTR(ret
) : lookup
.desc
;
753 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
754 * @adev: pointer to a ACPI device to get IRQ from
755 * @index: index of GpioInt resource (starting from %0)
757 * If the device has one or more GpioInt resources, this function can be
758 * used to translate from the GPIO offset in the resource to the Linux IRQ
761 * The function is idempotent, though each time it runs it will configure GPIO
762 * pin direction according to the flags in GpioInt resource.
764 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
766 int acpi_dev_gpio_irq_get(struct acpi_device
*adev
, int index
)
769 unsigned int irq_flags
;
772 for (i
= 0, idx
= 0; idx
<= index
; i
++) {
773 struct acpi_gpio_info info
;
774 struct gpio_desc
*desc
;
776 desc
= acpi_get_gpiod_by_index(adev
, NULL
, i
, &info
);
778 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
779 if (IS_ERR(desc
) && PTR_ERR(desc
) != -EPROBE_DEFER
)
780 return PTR_ERR(desc
);
782 if (info
.gpioint
&& idx
++ == index
) {
787 return PTR_ERR(desc
);
789 irq
= gpiod_to_irq(desc
);
793 snprintf(label
, sizeof(label
), "GpioInt() %d", index
);
794 ret
= gpiod_configure_flags(desc
, label
, 0, info
.flags
);
798 irq_flags
= acpi_dev_get_irq_type(info
.triggering
,
801 /* Set type if specified and different than the current one */
802 if (irq_flags
!= IRQ_TYPE_NONE
&&
803 irq_flags
!= irq_get_trigger_type(irq
))
804 irq_set_irq_type(irq
, irq_flags
);
812 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get
);
815 acpi_gpio_adr_space_handler(u32 function
, acpi_physical_address address
,
816 u32 bits
, u64
*value
, void *handler_context
,
817 void *region_context
)
819 struct acpi_gpio_chip
*achip
= region_context
;
820 struct gpio_chip
*chip
= achip
->chip
;
821 struct acpi_resource_gpio
*agpio
;
822 struct acpi_resource
*ares
;
823 int pin_index
= (int)address
;
828 status
= acpi_buffer_to_resource(achip
->conn_info
.connection
,
829 achip
->conn_info
.length
, &ares
);
830 if (ACPI_FAILURE(status
))
833 if (WARN_ON(ares
->type
!= ACPI_RESOURCE_TYPE_GPIO
)) {
835 return AE_BAD_PARAMETER
;
838 agpio
= &ares
->data
.gpio
;
840 if (WARN_ON(agpio
->io_restriction
== ACPI_IO_RESTRICT_INPUT
&&
841 function
== ACPI_WRITE
)) {
843 return AE_BAD_PARAMETER
;
846 length
= min(agpio
->pin_table_length
, (u16
)(pin_index
+ bits
));
847 for (i
= pin_index
; i
< length
; ++i
) {
848 int pin
= agpio
->pin_table
[i
];
849 struct acpi_gpio_connection
*conn
;
850 struct gpio_desc
*desc
;
853 mutex_lock(&achip
->conn_lock
);
856 list_for_each_entry(conn
, &achip
->conns
, node
) {
857 if (conn
->pin
== pin
) {
865 * The same GPIO can be shared between operation region and
866 * event but only if the access here is ACPI_READ. In that
867 * case we "borrow" the event GPIO instead.
869 if (!found
&& agpio
->sharable
== ACPI_SHARED
&&
870 function
== ACPI_READ
) {
871 struct acpi_gpio_event
*event
;
873 list_for_each_entry(event
, &achip
->events
, node
) {
874 if (event
->pin
== pin
) {
883 enum gpiod_flags flags
= acpi_gpio_to_gpiod_flags(agpio
);
884 const char *label
= "ACPI:OpRegion";
887 desc
= gpiochip_request_own_desc(chip
, pin
, label
);
890 mutex_unlock(&achip
->conn_lock
);
894 err
= gpiod_configure_flags(desc
, label
, 0, flags
);
896 status
= AE_NOT_CONFIGURED
;
897 gpiochip_free_own_desc(desc
);
898 mutex_unlock(&achip
->conn_lock
);
902 conn
= kzalloc(sizeof(*conn
), GFP_KERNEL
);
904 status
= AE_NO_MEMORY
;
905 gpiochip_free_own_desc(desc
);
906 mutex_unlock(&achip
->conn_lock
);
912 list_add_tail(&conn
->node
, &achip
->conns
);
915 mutex_unlock(&achip
->conn_lock
);
917 if (function
== ACPI_WRITE
)
918 gpiod_set_raw_value_cansleep(desc
,
919 !!((1 << i
) & *value
));
921 *value
|= (u64
)gpiod_get_raw_value_cansleep(desc
) << i
;
929 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip
*achip
)
931 struct gpio_chip
*chip
= achip
->chip
;
932 acpi_handle handle
= ACPI_HANDLE(chip
->parent
);
935 INIT_LIST_HEAD(&achip
->conns
);
936 mutex_init(&achip
->conn_lock
);
937 status
= acpi_install_address_space_handler(handle
, ACPI_ADR_SPACE_GPIO
,
938 acpi_gpio_adr_space_handler
,
940 if (ACPI_FAILURE(status
))
941 dev_err(chip
->parent
,
942 "Failed to install GPIO OpRegion handler\n");
945 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip
*achip
)
947 struct gpio_chip
*chip
= achip
->chip
;
948 acpi_handle handle
= ACPI_HANDLE(chip
->parent
);
949 struct acpi_gpio_connection
*conn
, *tmp
;
952 status
= acpi_remove_address_space_handler(handle
, ACPI_ADR_SPACE_GPIO
,
953 acpi_gpio_adr_space_handler
);
954 if (ACPI_FAILURE(status
)) {
955 dev_err(chip
->parent
,
956 "Failed to remove GPIO OpRegion handler\n");
960 list_for_each_entry_safe_reverse(conn
, tmp
, &achip
->conns
, node
) {
961 gpiochip_free_own_desc(conn
->desc
);
962 list_del(&conn
->node
);
967 static struct gpio_desc
*acpi_gpiochip_parse_own_gpio(
968 struct acpi_gpio_chip
*achip
, struct fwnode_handle
*fwnode
,
969 const char **name
, unsigned int *lflags
, unsigned int *dflags
)
971 struct gpio_chip
*chip
= achip
->chip
;
972 struct gpio_desc
*desc
;
980 ret
= fwnode_property_read_u32_array(fwnode
, "gpios", gpios
,
985 desc
= gpiochip_get_desc(chip
, gpios
[0]);
990 *lflags
|= GPIO_ACTIVE_LOW
;
992 if (fwnode_property_present(fwnode
, "input"))
994 else if (fwnode_property_present(fwnode
, "output-low"))
995 *dflags
|= GPIOD_OUT_LOW
;
996 else if (fwnode_property_present(fwnode
, "output-high"))
997 *dflags
|= GPIOD_OUT_HIGH
;
999 return ERR_PTR(-EINVAL
);
1001 fwnode_property_read_string(fwnode
, "line-name", name
);
1006 static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip
*achip
)
1008 struct gpio_chip
*chip
= achip
->chip
;
1009 struct fwnode_handle
*fwnode
;
1011 device_for_each_child_node(chip
->parent
, fwnode
) {
1012 unsigned int lflags
, dflags
;
1013 struct gpio_desc
*desc
;
1017 if (!fwnode_property_present(fwnode
, "gpio-hog"))
1020 desc
= acpi_gpiochip_parse_own_gpio(achip
, fwnode
, &name
,
1025 ret
= gpiod_hog(desc
, name
, lflags
, dflags
);
1027 dev_err(chip
->parent
, "Failed to hog GPIO\n");
1028 fwnode_handle_put(fwnode
);
1034 void acpi_gpiochip_add(struct gpio_chip
*chip
)
1036 struct acpi_gpio_chip
*acpi_gpio
;
1040 if (!chip
|| !chip
->parent
)
1043 handle
= ACPI_HANDLE(chip
->parent
);
1047 acpi_gpio
= kzalloc(sizeof(*acpi_gpio
), GFP_KERNEL
);
1049 dev_err(chip
->parent
,
1050 "Failed to allocate memory for ACPI GPIO chip\n");
1054 acpi_gpio
->chip
= chip
;
1055 INIT_LIST_HEAD(&acpi_gpio
->events
);
1056 INIT_LIST_HEAD(&acpi_gpio
->deferred_req_irqs_list_entry
);
1058 status
= acpi_attach_data(handle
, acpi_gpio_chip_dh
, acpi_gpio
);
1059 if (ACPI_FAILURE(status
)) {
1060 dev_err(chip
->parent
, "Failed to attach ACPI GPIO chip\n");
1066 devprop_gpiochip_set_names(chip
, dev_fwnode(chip
->parent
));
1068 acpi_gpiochip_request_regions(acpi_gpio
);
1069 acpi_gpiochip_scan_gpios(acpi_gpio
);
1070 acpi_walk_dep_device_list(handle
);
1073 void acpi_gpiochip_remove(struct gpio_chip
*chip
)
1075 struct acpi_gpio_chip
*acpi_gpio
;
1079 if (!chip
|| !chip
->parent
)
1082 handle
= ACPI_HANDLE(chip
->parent
);
1086 status
= acpi_get_data(handle
, acpi_gpio_chip_dh
, (void **)&acpi_gpio
);
1087 if (ACPI_FAILURE(status
)) {
1088 dev_warn(chip
->parent
, "Failed to retrieve ACPI GPIO chip\n");
1092 acpi_gpiochip_free_regions(acpi_gpio
);
1094 acpi_detach_data(handle
, acpi_gpio_chip_dh
);
1098 static int acpi_gpio_package_count(const union acpi_object
*obj
)
1100 const union acpi_object
*element
= obj
->package
.elements
;
1101 const union acpi_object
*end
= element
+ obj
->package
.count
;
1102 unsigned int count
= 0;
1104 while (element
< end
) {
1105 switch (element
->type
) {
1106 case ACPI_TYPE_LOCAL_REFERENCE
:
1109 case ACPI_TYPE_INTEGER
:
1122 static int acpi_find_gpio_count(struct acpi_resource
*ares
, void *data
)
1124 unsigned int *count
= data
;
1126 if (ares
->type
== ACPI_RESOURCE_TYPE_GPIO
)
1127 *count
+= ares
->data
.gpio
.pin_table_length
;
1133 * acpi_gpio_count - return the number of GPIOs associated with a
1134 * device / function or -ENOENT if no GPIO has been
1135 * assigned to the requested function.
1136 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1137 * @con_id: function within the GPIO consumer
1139 int acpi_gpio_count(struct device
*dev
, const char *con_id
)
1141 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
1142 const union acpi_object
*obj
;
1143 const struct acpi_gpio_mapping
*gm
;
1144 int count
= -ENOENT
;
1149 /* Try first from _DSD */
1150 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
1152 snprintf(propname
, sizeof(propname
), "%s-%s",
1153 con_id
, gpio_suffixes
[i
]);
1155 snprintf(propname
, sizeof(propname
), "%s",
1158 ret
= acpi_dev_get_property(adev
, propname
, ACPI_TYPE_ANY
,
1161 if (obj
->type
== ACPI_TYPE_LOCAL_REFERENCE
)
1163 else if (obj
->type
== ACPI_TYPE_PACKAGE
)
1164 count
= acpi_gpio_package_count(obj
);
1165 } else if (adev
->driver_gpios
) {
1166 for (gm
= adev
->driver_gpios
; gm
->name
; gm
++)
1167 if (strcmp(propname
, gm
->name
) == 0) {
1176 /* Then from plain _CRS GPIOs */
1178 struct list_head resource_list
;
1179 unsigned int crs_count
= 0;
1181 if (!acpi_can_fallback_to_crs(adev
, con_id
))
1184 INIT_LIST_HEAD(&resource_list
);
1185 acpi_dev_get_resources(adev
, &resource_list
,
1186 acpi_find_gpio_count
, &crs_count
);
1187 acpi_dev_free_resource_list(&resource_list
);
1191 return count
? count
: -ENOENT
;
1194 bool acpi_can_fallback_to_crs(struct acpi_device
*adev
, const char *con_id
)
1196 /* Never allow fallback if the device has properties */
1197 if (acpi_dev_has_props(adev
) || adev
->driver_gpios
)
1200 return con_id
== NULL
;
1203 /* Run deferred acpi_gpiochip_request_interrupts() */
1204 static int acpi_gpio_handle_deferred_request_interrupts(void)
1206 struct acpi_gpio_chip
*acpi_gpio
, *tmp
;
1208 mutex_lock(&acpi_gpio_deferred_req_irqs_lock
);
1209 list_for_each_entry_safe(acpi_gpio
, tmp
,
1210 &acpi_gpio_deferred_req_irqs_list
,
1211 deferred_req_irqs_list_entry
) {
1214 handle
= ACPI_HANDLE(acpi_gpio
->chip
->parent
);
1215 acpi_walk_resources(handle
, "_AEI",
1216 acpi_gpiochip_request_interrupt
, acpi_gpio
);
1218 list_del_init(&acpi_gpio
->deferred_req_irqs_list_entry
);
1221 acpi_gpio_deferred_req_irqs_done
= true;
1222 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock
);
1226 /* We must use _sync so that this runs after the first deferred_probe run */
1227 late_initcall_sync(acpi_gpio_handle_deferred_request_interrupts
);