xref: /openbmc/linux/drivers/gpio/gpiolib-acpi.c (revision f8e17c17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ACPI helpers for GPIO API
4  *
5  * Copyright (C) 2012, Intel Corporation
6  * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7  *          Mika Westerberg <mika.westerberg@linux.intel.com>
8  */
9 
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>
20 
21 #include "gpiolib.h"
22 #include "gpiolib-acpi.h"
23 
24 #define QUIRK_NO_EDGE_EVENTS_ON_BOOT		0x01l
25 #define QUIRK_NO_WAKEUP				0x02l
26 
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");
31 
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");
36 
37 /**
38  * struct acpi_gpio_event - ACPI GPIO event handler data
39  *
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
49  */
50 struct acpi_gpio_event {
51 	struct list_head node;
52 	acpi_handle handle;
53 	irq_handler_t handler;
54 	unsigned int pin;
55 	unsigned int irq;
56 	unsigned long irqflags;
57 	bool irq_is_wake;
58 	bool irq_requested;
59 	struct gpio_desc *desc;
60 };
61 
62 struct acpi_gpio_connection {
63 	struct list_head node;
64 	unsigned int pin;
65 	struct gpio_desc *desc;
66 };
67 
68 struct acpi_gpio_chip {
69 	/*
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.
73 	 */
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;
80 };
81 
82 /*
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.
88  */
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;
92 
93 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
94 {
95 	if (!gc->parent)
96 		return false;
97 
98 	return ACPI_HANDLE(gc->parent) == data;
99 }
100 
101 /**
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)
105  *
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.
110  */
111 static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
112 {
113 	struct gpio_chip *chip;
114 	acpi_handle handle;
115 	acpi_status status;
116 
117 	status = acpi_get_handle(NULL, path, &handle);
118 	if (ACPI_FAILURE(status))
119 		return ERR_PTR(-ENODEV);
120 
121 	chip = gpiochip_find(handle, acpi_gpiochip_find);
122 	if (!chip)
123 		return ERR_PTR(-EPROBE_DEFER);
124 
125 	return gpiochip_get_desc(chip, pin);
126 }
127 
128 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
129 {
130 	struct acpi_gpio_event *event = data;
131 
132 	acpi_evaluate_object(event->handle, NULL, NULL, NULL);
133 
134 	return IRQ_HANDLED;
135 }
136 
137 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
138 {
139 	struct acpi_gpio_event *event = data;
140 
141 	acpi_execute_simple_method(event->handle, NULL, event->pin);
142 
143 	return IRQ_HANDLED;
144 }
145 
146 static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
147 {
148 	/* The address of this function is used as a key. */
149 }
150 
151 bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
152 				struct acpi_resource_gpio **agpio)
153 {
154 	struct acpi_resource_gpio *gpio;
155 
156 	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
157 		return false;
158 
159 	gpio = &ares->data.gpio;
160 	if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
161 		return false;
162 
163 	*agpio = gpio;
164 	return true;
165 }
166 EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
167 
168 static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
169 				      struct acpi_gpio_event *event)
170 {
171 	int ret, value;
172 
173 	ret = request_threaded_irq(event->irq, NULL, event->handler,
174 				   event->irqflags, "ACPI:Event", event);
175 	if (ret) {
176 		dev_err(acpi_gpio->chip->parent,
177 			"Failed to setup interrupt handler for %d\n",
178 			event->irq);
179 		return;
180 	}
181 
182 	if (event->irq_is_wake)
183 		enable_irq_wake(event->irq);
184 
185 	event->irq_requested = true;
186 
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);
194 	}
195 }
196 
197 static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
198 {
199 	struct acpi_gpio_event *event;
200 
201 	list_for_each_entry(event, &acpi_gpio->events, node)
202 		acpi_gpiochip_request_irq(acpi_gpio, event);
203 }
204 
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,
207 					     void *context)
208 {
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;
216 	int ret, pin, irq;
217 
218 	if (!acpi_gpio_get_irq_resource(ares, &agpio))
219 		return AE_OK;
220 
221 	handle = ACPI_HANDLE(chip->parent);
222 	pin = agpio->pin_table[0];
223 
224 	if (pin <= 255) {
225 		char ev_name[5];
226 		sprintf(ev_name, "_%c%02hhX",
227 			agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
228 			pin);
229 		if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
230 			handler = acpi_gpio_irq_handler;
231 	}
232 	if (!handler) {
233 		if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
234 			handler = acpi_gpio_irq_handler_evt;
235 	}
236 	if (!handler)
237 		return AE_OK;
238 
239 	desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event",
240 					 GPIO_ACTIVE_HIGH, GPIOD_IN);
241 	if (IS_ERR(desc)) {
242 		dev_err(chip->parent,
243 			"Failed to request GPIO for pin 0x%04X, err %ld\n",
244 			pin, PTR_ERR(desc));
245 		return AE_OK;
246 	}
247 
248 	ret = gpiochip_lock_as_irq(chip, pin);
249 	if (ret) {
250 		dev_err(chip->parent,
251 			"Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
252 			pin, ret);
253 		goto fail_free_desc;
254 	}
255 
256 	irq = gpiod_to_irq(desc);
257 	if (irq < 0) {
258 		dev_err(chip->parent,
259 			"Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
260 			pin, irq);
261 		goto fail_unlock_irq;
262 	}
263 
264 	event = kzalloc(sizeof(*event), GFP_KERNEL);
265 	if (!event)
266 		goto fail_unlock_irq;
267 
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;
272 		else
273 			event->irqflags |= IRQF_TRIGGER_LOW;
274 	} else {
275 		switch (agpio->polarity) {
276 		case ACPI_ACTIVE_HIGH:
277 			event->irqflags |= IRQF_TRIGGER_RISING;
278 			break;
279 		case ACPI_ACTIVE_LOW:
280 			event->irqflags |= IRQF_TRIGGER_FALLING;
281 			break;
282 		default:
283 			event->irqflags |= IRQF_TRIGGER_RISING |
284 					   IRQF_TRIGGER_FALLING;
285 			break;
286 		}
287 	}
288 
289 	event->handle = evt_handle;
290 	event->handler = handler;
291 	event->irq = irq;
292 	event->irq_is_wake = honor_wakeup && agpio->wake_capable == ACPI_WAKE_CAPABLE;
293 	event->pin = pin;
294 	event->desc = desc;
295 
296 	list_add_tail(&event->node, &acpi_gpio->events);
297 
298 	return AE_OK;
299 
300 fail_unlock_irq:
301 	gpiochip_unlock_as_irq(chip, pin);
302 fail_free_desc:
303 	gpiochip_free_own_desc(desc);
304 
305 	return AE_OK;
306 }
307 
308 /**
309  * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
310  * @chip:      GPIO chip
311  *
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.
317  */
318 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
319 {
320 	struct acpi_gpio_chip *acpi_gpio;
321 	acpi_handle handle;
322 	acpi_status status;
323 	bool defer;
324 
325 	if (!chip->parent || !chip->to_irq)
326 		return;
327 
328 	handle = ACPI_HANDLE(chip->parent);
329 	if (!handle)
330 		return;
331 
332 	status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
333 	if (ACPI_FAILURE(status))
334 		return;
335 
336 	acpi_walk_resources(handle, "_AEI",
337 			    acpi_gpiochip_alloc_event, acpi_gpio);
338 
339 	mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
340 	defer = !acpi_gpio_deferred_req_irqs_done;
341 	if (defer)
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);
345 
346 	if (defer)
347 		return;
348 
349 	acpi_gpiochip_request_irqs(acpi_gpio);
350 }
351 EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
352 
353 /**
354  * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
355  * @chip:      GPIO chip
356  *
357  * Free interrupts associated with GPIO ACPI event method for the given
358  * GPIO chip.
359  */
360 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
361 {
362 	struct acpi_gpio_chip *acpi_gpio;
363 	struct acpi_gpio_event *event, *ep;
364 	acpi_handle handle;
365 	acpi_status status;
366 
367 	if (!chip->parent || !chip->to_irq)
368 		return;
369 
370 	handle = ACPI_HANDLE(chip->parent);
371 	if (!handle)
372 		return;
373 
374 	status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
375 	if (ACPI_FAILURE(status))
376 		return;
377 
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);
382 
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);
387 
388 			free_irq(event->irq, event);
389 		}
390 
391 		gpiochip_unlock_as_irq(chip, event->pin);
392 		gpiochip_free_own_desc(event->desc);
393 		list_del(&event->node);
394 		kfree(event);
395 	}
396 }
397 EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
398 
399 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
400 			      const struct acpi_gpio_mapping *gpios)
401 {
402 	if (adev && gpios) {
403 		adev->driver_gpios = gpios;
404 		return 0;
405 	}
406 	return -EINVAL;
407 }
408 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
409 
410 void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
411 {
412 	if (adev)
413 		adev->driver_gpios = NULL;
414 }
415 EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
416 
417 static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
418 {
419 	acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
420 }
421 
422 int devm_acpi_dev_add_driver_gpios(struct device *dev,
423 				   const struct acpi_gpio_mapping *gpios)
424 {
425 	void *res;
426 	int ret;
427 
428 	res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
429 	if (!res)
430 		return -ENOMEM;
431 
432 	ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
433 	if (ret) {
434 		devres_free(res);
435 		return ret;
436 	}
437 	devres_add(dev, res);
438 	return 0;
439 }
440 EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
441 
442 void devm_acpi_dev_remove_driver_gpios(struct device *dev)
443 {
444 	WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
445 }
446 EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
447 
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)
452 {
453 	const struct acpi_gpio_mapping *gm;
454 
455 	if (!adev->driver_gpios)
456 		return false;
457 
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;
461 
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;
466 			args->nargs = 3;
467 
468 			*quirks = gm->quirks;
469 			return true;
470 		}
471 
472 	return false;
473 }
474 
475 static enum gpiod_flags
476 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
477 {
478 	switch (agpio->io_restriction) {
479 	case ACPI_IO_RESTRICT_INPUT:
480 		return GPIOD_IN;
481 	case ACPI_IO_RESTRICT_OUTPUT:
482 		/*
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.
488 		 */
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;
494 		default:
495 			break;
496 		}
497 	default:
498 		break;
499 	}
500 
501 	/*
502 	 * Assume that the BIOS has configured the direction and pull
503 	 * accordingly.
504 	 */
505 	return GPIOD_ASIS;
506 }
507 
508 static int
509 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
510 {
511 	const enum gpiod_flags mask =
512 		GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
513 		GPIOD_FLAGS_BIT_DIR_VAL;
514 	int ret = 0;
515 
516 	/*
517 	 * Check if the BIOS has IoRestriction with explicitly set direction
518 	 * and update @flags accordingly. Otherwise use whatever caller asked
519 	 * for.
520 	 */
521 	if (update & GPIOD_FLAGS_BIT_DIR_SET) {
522 		enum gpiod_flags diff = *flags ^ update;
523 
524 		/*
525 		 * Check if caller supplied incompatible GPIO initialization
526 		 * flags.
527 		 *
528 		 * Return %-EINVAL to notify that firmware has different
529 		 * settings and we are going to use them.
530 		 */
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)))
533 			ret = -EINVAL;
534 		*flags = (*flags & ~mask) | (update & mask);
535 	}
536 	return ret;
537 }
538 
539 int
540 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
541 {
542 	struct device *dev = &info->adev->dev;
543 	enum gpiod_flags old = *flags;
544 	int ret;
545 
546 	ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
547 	if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
548 		if (ret)
549 			dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
550 	} else {
551 		if (ret)
552 			dev_dbg(dev, "Override GPIO initialization flags\n");
553 		*flags = old;
554 	}
555 
556 	return ret;
557 }
558 
559 int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
560 					struct acpi_gpio_info *info)
561 {
562 	switch (info->pin_config) {
563 	case ACPI_PIN_CONFIG_PULLUP:
564 		*lookupflags |= GPIO_PULL_UP;
565 		break;
566 	case ACPI_PIN_CONFIG_PULLDOWN:
567 		*lookupflags |= GPIO_PULL_DOWN;
568 		break;
569 	default:
570 		break;
571 	}
572 
573 	if (info->polarity == GPIO_ACTIVE_LOW)
574 		*lookupflags |= GPIO_ACTIVE_LOW;
575 
576 	return 0;
577 }
578 
579 struct acpi_gpio_lookup {
580 	struct acpi_gpio_info info;
581 	int index;
582 	int pin_index;
583 	bool active_low;
584 	struct gpio_desc *desc;
585 	int n;
586 };
587 
588 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
589 {
590 	struct acpi_gpio_lookup *lookup = data;
591 
592 	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
593 		return 1;
594 
595 	if (!lookup->desc) {
596 		const struct acpi_resource_gpio *agpio = &ares->data.gpio;
597 		bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
598 		int pin_index;
599 
600 		if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
601 			lookup->index++;
602 
603 		if (lookup->n++ != lookup->index)
604 			return 1;
605 
606 		pin_index = lookup->pin_index;
607 		if (pin_index >= agpio->pin_table_length)
608 			return 1;
609 
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;
614 
615 		/*
616 		 * Polarity and triggering are only specified for GpioInt
617 		 * resource.
618 		 * Note: we expect here:
619 		 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
620 		 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
621 		 */
622 		if (lookup->info.gpioint) {
623 			lookup->info.flags = GPIOD_IN;
624 			lookup->info.polarity = agpio->polarity;
625 			lookup->info.triggering = agpio->triggering;
626 		} else {
627 			lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
628 			lookup->info.polarity = lookup->active_low;
629 		}
630 	}
631 
632 	return 1;
633 }
634 
635 static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
636 				     struct acpi_gpio_info *info)
637 {
638 	struct acpi_device *adev = lookup->info.adev;
639 	struct list_head res_list;
640 	int ret;
641 
642 	INIT_LIST_HEAD(&res_list);
643 
644 	ret = acpi_dev_get_resources(adev, &res_list,
645 				     acpi_populate_gpio_lookup,
646 				     lookup);
647 	if (ret < 0)
648 		return ret;
649 
650 	acpi_dev_free_resource_list(&res_list);
651 
652 	if (!lookup->desc)
653 		return -ENOENT;
654 
655 	if (info)
656 		*info = lookup->info;
657 	return 0;
658 }
659 
660 static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
661 				     const char *propname, int index,
662 				     struct acpi_gpio_lookup *lookup)
663 {
664 	struct fwnode_reference_args args;
665 	unsigned int quirks = 0;
666 	int ret;
667 
668 	memset(&args, 0, sizeof(args));
669 	ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
670 						 &args);
671 	if (ret) {
672 		struct acpi_device *adev = to_acpi_device_node(fwnode);
673 
674 		if (!adev)
675 			return ret;
676 
677 		if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
678 					       &quirks))
679 			return ret;
680 	}
681 	/*
682 	 * The property was found and resolved, so need to lookup the GPIO based
683 	 * on returned args.
684 	 */
685 	if (!to_acpi_device_node(args.fwnode))
686 		return -EINVAL;
687 	if (args.nargs != 3)
688 		return -EPROTO;
689 
690 	lookup->index = args.args[0];
691 	lookup->pin_index = args.args[1];
692 	lookup->active_low = !!args.args[2];
693 
694 	lookup->info.adev = to_acpi_device_node(args.fwnode);
695 	lookup->info.quirks = quirks;
696 
697 	return 0;
698 }
699 
700 /**
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)
706  *
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.
711  *
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).
715  *
716  * If the GPIO cannot be translated or there is an error, an ERR_PTR is
717  * returned.
718  *
719  * Note: if the GPIO resource has multiple entries in the pin list, this
720  * function only returns the first.
721  */
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)
725 {
726 	struct acpi_gpio_lookup lookup;
727 	int ret;
728 
729 	if (!adev)
730 		return ERR_PTR(-ENODEV);
731 
732 	memset(&lookup, 0, sizeof(lookup));
733 	lookup.index = index;
734 
735 	if (propname) {
736 		dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
737 
738 		ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
739 						propname, index, &lookup);
740 		if (ret)
741 			return ERR_PTR(ret);
742 
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);
746 	} else {
747 		dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
748 		lookup.info.adev = adev;
749 	}
750 
751 	ret = acpi_gpio_resource_lookup(&lookup, info);
752 	return ret ? ERR_PTR(ret) : lookup.desc;
753 }
754 
755 static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
756 				     const char *con_id)
757 {
758 	/* Never allow fallback if the device has properties */
759 	if (acpi_dev_has_props(adev) || adev->driver_gpios)
760 		return false;
761 
762 	return con_id == NULL;
763 }
764 
765 struct gpio_desc *acpi_find_gpio(struct device *dev,
766 				 const char *con_id,
767 				 unsigned int idx,
768 				 enum gpiod_flags *dflags,
769 				 unsigned long *lookupflags)
770 {
771 	struct acpi_device *adev = ACPI_COMPANION(dev);
772 	struct acpi_gpio_info info;
773 	struct gpio_desc *desc;
774 	char propname[32];
775 	int i;
776 
777 	/* Try first from _DSD */
778 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
779 		if (con_id) {
780 			snprintf(propname, sizeof(propname), "%s-%s",
781 				 con_id, gpio_suffixes[i]);
782 		} else {
783 			snprintf(propname, sizeof(propname), "%s",
784 				 gpio_suffixes[i]);
785 		}
786 
787 		desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
788 		if (!IS_ERR(desc))
789 			break;
790 		if (PTR_ERR(desc) == -EPROBE_DEFER)
791 			return ERR_CAST(desc);
792 	}
793 
794 	/* Then from plain _CRS GPIOs */
795 	if (IS_ERR(desc)) {
796 		if (!acpi_can_fallback_to_crs(adev, con_id))
797 			return ERR_PTR(-ENOENT);
798 
799 		desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
800 		if (IS_ERR(desc))
801 			return desc;
802 	}
803 
804 	if (info.gpioint &&
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);
808 	}
809 
810 	acpi_gpio_update_gpiod_flags(dflags, &info);
811 	acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
812 	return desc;
813 }
814 
815 /**
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)
821  *
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.
826  *
827  * If the GPIO cannot be translated or there is an error an ERR_PTR is
828  * returned.
829  */
830 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
831 				      const char *propname, int index,
832 				      struct acpi_gpio_info *info)
833 {
834 	struct acpi_gpio_lookup lookup;
835 	struct acpi_device *adev;
836 	int ret;
837 
838 	adev = to_acpi_device_node(fwnode);
839 	if (adev)
840 		return acpi_get_gpiod_by_index(adev, propname, index, info);
841 
842 	if (!is_acpi_data_node(fwnode))
843 		return ERR_PTR(-ENODEV);
844 
845 	if (!propname)
846 		return ERR_PTR(-EINVAL);
847 
848 	memset(&lookup, 0, sizeof(lookup));
849 	lookup.index = index;
850 
851 	ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
852 	if (ret)
853 		return ERR_PTR(ret);
854 
855 	ret = acpi_gpio_resource_lookup(&lookup, info);
856 	return ret ? ERR_PTR(ret) : lookup.desc;
857 }
858 
859 /**
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)
863  *
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
866  * number.
867  *
868  * The function is idempotent, though each time it runs it will configure GPIO
869  * pin direction according to the flags in GpioInt resource.
870  *
871  * Return: Linux IRQ number (> %0) on success, negative errno on failure.
872  */
873 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
874 {
875 	int idx, i;
876 	unsigned int irq_flags;
877 	int ret;
878 
879 	for (i = 0, idx = 0; idx <= index; i++) {
880 		struct acpi_gpio_info info;
881 		struct gpio_desc *desc;
882 
883 		desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
884 
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);
888 
889 		if (info.gpioint && idx++ == index) {
890 			unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
891 			char label[32];
892 			int irq;
893 
894 			if (IS_ERR(desc))
895 				return PTR_ERR(desc);
896 
897 			irq = gpiod_to_irq(desc);
898 			if (irq < 0)
899 				return irq;
900 
901 			snprintf(label, sizeof(label), "GpioInt() %d", index);
902 			ret = gpiod_configure_flags(desc, label, lflags, info.flags);
903 			if (ret < 0)
904 				return ret;
905 
906 			irq_flags = acpi_dev_get_irq_type(info.triggering,
907 							  info.polarity);
908 
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);
913 
914 			return irq;
915 		}
916 
917 	}
918 	return -ENOENT;
919 }
920 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
921 
922 static acpi_status
923 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
924 			    u32 bits, u64 *value, void *handler_context,
925 			    void *region_context)
926 {
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;
932 	acpi_status status;
933 	int length;
934 	int i;
935 
936 	status = acpi_buffer_to_resource(achip->conn_info.connection,
937 					 achip->conn_info.length, &ares);
938 	if (ACPI_FAILURE(status))
939 		return status;
940 
941 	if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
942 		ACPI_FREE(ares);
943 		return AE_BAD_PARAMETER;
944 	}
945 
946 	agpio = &ares->data.gpio;
947 
948 	if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
949 	    function == ACPI_WRITE)) {
950 		ACPI_FREE(ares);
951 		return AE_BAD_PARAMETER;
952 	}
953 
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;
959 		bool found;
960 
961 		mutex_lock(&achip->conn_lock);
962 
963 		found = false;
964 		list_for_each_entry(conn, &achip->conns, node) {
965 			if (conn->pin == pin) {
966 				found = true;
967 				desc = conn->desc;
968 				break;
969 			}
970 		}
971 
972 		/*
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.
976 		 */
977 		if (!found && agpio->shareable == ACPI_SHARED &&
978 		     function == ACPI_READ) {
979 			struct acpi_gpio_event *event;
980 
981 			list_for_each_entry(event, &achip->events, node) {
982 				if (event->pin == pin) {
983 					desc = event->desc;
984 					found = true;
985 					break;
986 				}
987 			}
988 		}
989 
990 		if (!found) {
991 			enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
992 			const char *label = "ACPI:OpRegion";
993 
994 			desc = gpiochip_request_own_desc(chip, pin, label,
995 							 GPIO_ACTIVE_HIGH,
996 							 flags);
997 			if (IS_ERR(desc)) {
998 				status = AE_ERROR;
999 				mutex_unlock(&achip->conn_lock);
1000 				goto out;
1001 			}
1002 
1003 			conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1004 			if (!conn) {
1005 				status = AE_NO_MEMORY;
1006 				gpiochip_free_own_desc(desc);
1007 				mutex_unlock(&achip->conn_lock);
1008 				goto out;
1009 			}
1010 
1011 			conn->pin = pin;
1012 			conn->desc = desc;
1013 			list_add_tail(&conn->node, &achip->conns);
1014 		}
1015 
1016 		mutex_unlock(&achip->conn_lock);
1017 
1018 		if (function == ACPI_WRITE)
1019 			gpiod_set_raw_value_cansleep(desc,
1020 						     !!((1 << i) & *value));
1021 		else
1022 			*value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
1023 	}
1024 
1025 out:
1026 	ACPI_FREE(ares);
1027 	return status;
1028 }
1029 
1030 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
1031 {
1032 	struct gpio_chip *chip = achip->chip;
1033 	acpi_handle handle = ACPI_HANDLE(chip->parent);
1034 	acpi_status status;
1035 
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,
1040 						    NULL, achip);
1041 	if (ACPI_FAILURE(status))
1042 		dev_err(chip->parent,
1043 		        "Failed to install GPIO OpRegion handler\n");
1044 }
1045 
1046 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
1047 {
1048 	struct gpio_chip *chip = achip->chip;
1049 	acpi_handle handle = ACPI_HANDLE(chip->parent);
1050 	struct acpi_gpio_connection *conn, *tmp;
1051 	acpi_status status;
1052 
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");
1058 		return;
1059 	}
1060 
1061 	list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
1062 		gpiochip_free_own_desc(conn->desc);
1063 		list_del(&conn->node);
1064 		kfree(conn);
1065 	}
1066 }
1067 
1068 static struct gpio_desc *
1069 acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
1070 			     struct fwnode_handle *fwnode,
1071 			     const char **name,
1072 			     unsigned long *lflags,
1073 			     enum gpiod_flags *dflags)
1074 {
1075 	struct gpio_chip *chip = achip->chip;
1076 	struct gpio_desc *desc;
1077 	u32 gpios[2];
1078 	int ret;
1079 
1080 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
1081 	*dflags = 0;
1082 	*name = NULL;
1083 
1084 	ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
1085 					     ARRAY_SIZE(gpios));
1086 	if (ret < 0)
1087 		return ERR_PTR(ret);
1088 
1089 	desc = gpiochip_get_desc(chip, gpios[0]);
1090 	if (IS_ERR(desc))
1091 		return desc;
1092 
1093 	if (gpios[1])
1094 		*lflags |= GPIO_ACTIVE_LOW;
1095 
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;
1102 	else
1103 		return ERR_PTR(-EINVAL);
1104 
1105 	fwnode_property_read_string(fwnode, "line-name", name);
1106 
1107 	return desc;
1108 }
1109 
1110 static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1111 {
1112 	struct gpio_chip *chip = achip->chip;
1113 	struct fwnode_handle *fwnode;
1114 
1115 	device_for_each_child_node(chip->parent, fwnode) {
1116 		unsigned long lflags;
1117 		enum gpiod_flags dflags;
1118 		struct gpio_desc *desc;
1119 		const char *name;
1120 		int ret;
1121 
1122 		if (!fwnode_property_present(fwnode, "gpio-hog"))
1123 			continue;
1124 
1125 		desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1126 						    &lflags, &dflags);
1127 		if (IS_ERR(desc))
1128 			continue;
1129 
1130 		ret = gpiod_hog(desc, name, lflags, dflags);
1131 		if (ret) {
1132 			dev_err(chip->parent, "Failed to hog GPIO\n");
1133 			fwnode_handle_put(fwnode);
1134 			return;
1135 		}
1136 	}
1137 }
1138 
1139 void acpi_gpiochip_add(struct gpio_chip *chip)
1140 {
1141 	struct acpi_gpio_chip *acpi_gpio;
1142 	acpi_handle handle;
1143 	acpi_status status;
1144 
1145 	if (!chip || !chip->parent)
1146 		return;
1147 
1148 	handle = ACPI_HANDLE(chip->parent);
1149 	if (!handle)
1150 		return;
1151 
1152 	acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1153 	if (!acpi_gpio) {
1154 		dev_err(chip->parent,
1155 			"Failed to allocate memory for ACPI GPIO chip\n");
1156 		return;
1157 	}
1158 
1159 	acpi_gpio->chip = chip;
1160 	INIT_LIST_HEAD(&acpi_gpio->events);
1161 	INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
1162 
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");
1166 		kfree(acpi_gpio);
1167 		return;
1168 	}
1169 
1170 	if (!chip->names)
1171 		devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
1172 
1173 	acpi_gpiochip_request_regions(acpi_gpio);
1174 	acpi_gpiochip_scan_gpios(acpi_gpio);
1175 	acpi_walk_dep_device_list(handle);
1176 }
1177 
1178 void acpi_gpiochip_remove(struct gpio_chip *chip)
1179 {
1180 	struct acpi_gpio_chip *acpi_gpio;
1181 	acpi_handle handle;
1182 	acpi_status status;
1183 
1184 	if (!chip || !chip->parent)
1185 		return;
1186 
1187 	handle = ACPI_HANDLE(chip->parent);
1188 	if (!handle)
1189 		return;
1190 
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");
1194 		return;
1195 	}
1196 
1197 	acpi_gpiochip_free_regions(acpi_gpio);
1198 
1199 	acpi_detach_data(handle, acpi_gpio_chip_dh);
1200 	kfree(acpi_gpio);
1201 }
1202 
1203 static int acpi_gpio_package_count(const union acpi_object *obj)
1204 {
1205 	const union acpi_object *element = obj->package.elements;
1206 	const union acpi_object *end = element + obj->package.count;
1207 	unsigned int count = 0;
1208 
1209 	while (element < end) {
1210 		switch (element->type) {
1211 		case ACPI_TYPE_LOCAL_REFERENCE:
1212 			element += 3;
1213 			/* Fallthrough */
1214 		case ACPI_TYPE_INTEGER:
1215 			element++;
1216 			count++;
1217 			break;
1218 
1219 		default:
1220 			return -EPROTO;
1221 		}
1222 	}
1223 
1224 	return count;
1225 }
1226 
1227 static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1228 {
1229 	unsigned int *count = data;
1230 
1231 	if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1232 		*count += ares->data.gpio.pin_table_length;
1233 
1234 	return 1;
1235 }
1236 
1237 /**
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
1241  *
1242  * Return:
1243  * The number of GPIOs associated with a device / function or %-ENOENT,
1244  * if no GPIO has been assigned to the requested function.
1245  */
1246 int acpi_gpio_count(struct device *dev, const char *con_id)
1247 {
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;
1252 	int ret;
1253 	char propname[32];
1254 	unsigned int i;
1255 
1256 	/* Try first from _DSD */
1257 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1258 		if (con_id)
1259 			snprintf(propname, sizeof(propname), "%s-%s",
1260 				 con_id, gpio_suffixes[i]);
1261 		else
1262 			snprintf(propname, sizeof(propname), "%s",
1263 				 gpio_suffixes[i]);
1264 
1265 		ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1266 					    &obj);
1267 		if (ret == 0) {
1268 			if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1269 				count = 1;
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) {
1275 					count = gm->size;
1276 					break;
1277 				}
1278 		}
1279 		if (count > 0)
1280 			break;
1281 	}
1282 
1283 	/* Then from plain _CRS GPIOs */
1284 	if (count < 0) {
1285 		struct list_head resource_list;
1286 		unsigned int crs_count = 0;
1287 
1288 		if (!acpi_can_fallback_to_crs(adev, con_id))
1289 			return count;
1290 
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);
1295 		if (crs_count > 0)
1296 			count = crs_count;
1297 	}
1298 	return count ? count : -ENOENT;
1299 }
1300 
1301 /* Run deferred acpi_gpiochip_request_irqs() */
1302 static int acpi_gpio_handle_deferred_request_irqs(void)
1303 {
1304 	struct acpi_gpio_chip *acpi_gpio, *tmp;
1305 
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);
1311 
1312 	acpi_gpio_deferred_req_irqs_done = true;
1313 	mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
1314 
1315 	return 0;
1316 }
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);
1319 
1320 static const struct dmi_system_id gpiolib_acpi_quirks[] = {
1321 	{
1322 		/*
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.
1326 		 */
1327 		.matches = {
1328 			DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
1329 			DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
1330 		},
1331 		.driver_data = (void *)QUIRK_NO_EDGE_EVENTS_ON_BOOT,
1332 	},
1333 	{
1334 		/*
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.
1339 		 */
1340 		.matches = {
1341 			DMI_MATCH(DMI_SYS_VENDOR, "Wortmann_AG"),
1342 			DMI_MATCH(DMI_PRODUCT_NAME, "TERRA_PAD_1061"),
1343 		},
1344 		.driver_data = (void *)QUIRK_NO_EDGE_EVENTS_ON_BOOT,
1345 	},
1346 	{
1347 		/*
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.
1356 		 */
1357 		.matches = {
1358 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1359 			DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
1360 		},
1361 		.driver_data = (void *)QUIRK_NO_WAKEUP,
1362 	},
1363 	{} /* Terminating entry */
1364 };
1365 
1366 static int acpi_gpio_setup_params(void)
1367 {
1368 	const struct dmi_system_id *id;
1369 	long quirks = 0;
1370 
1371 	id = dmi_first_match(gpiolib_acpi_quirks);
1372 	if (id)
1373 		quirks = (long)id->driver_data;
1374 
1375 	if (run_edge_events_on_boot < 0) {
1376 		if (quirks & QUIRK_NO_EDGE_EVENTS_ON_BOOT)
1377 			run_edge_events_on_boot = 0;
1378 		else
1379 			run_edge_events_on_boot = 1;
1380 	}
1381 
1382 	if (honor_wakeup < 0) {
1383 		if (quirks & QUIRK_NO_WAKEUP)
1384 			honor_wakeup = 0;
1385 		else
1386 			honor_wakeup = 1;
1387 	}
1388 
1389 	return 0;
1390 }
1391 
1392 /* Directly after dmi_setup() which runs as core_initcall() */
1393 postcore_initcall(acpi_gpio_setup_params);
1394