File Index
Files and directories
Sign in
FILE

gpiolib-acpi-core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpio/gpiolib-acpi-core.c C
   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/acpi.h>
  11#include <linux/dmi.h>
  12#include <linux/errno.h>
  13#include <linux/export.h>
  14#include <linux/interrupt.h>
  15#include <linux/irq.h>
  16#include <linux/mutex.h>
  17#include <linux/pinctrl/pinctrl.h>
  18
  19#include <linux/gpio/consumer.h>
  20#include <linux/gpio/driver.h>
  21#include <linux/gpio/machine.h>
  22
  23#include "gpiolib.h"
  24#include "gpiolib-acpi.h"
  25
  26/**
  27 * struct acpi_gpio_event - ACPI GPIO event handler data
  28 *
  29 * @node:	  list-entry of the events list of the struct acpi_gpio_chip
  30 * @handle:	  handle of ACPI method to execute when the IRQ triggers
  31 * @handler:	  handler function to pass to request_irq() when requesting the IRQ
  32 * @pin:	  GPIO pin number on the struct gpio_chip
  33 * @irq:	  Linux IRQ number for the event, for request_irq() / free_irq()
  34 * @irqflags:	  flags to pass to request_irq() when requesting the IRQ
  35 * @irq_is_wake:  If the ACPI flags indicate the IRQ is a wakeup source
  36 * @irq_requested:True if request_irq() has been done
  37 * @desc:	  struct gpio_desc for the GPIO pin for this event
  38 */
  39struct acpi_gpio_event {
  40	struct list_head node;
  41	acpi_handle handle;
  42	irq_handler_t handler;
  43	unsigned int pin;
  44	unsigned int irq;
  45	unsigned long irqflags;
  46	bool irq_is_wake;
  47	bool irq_requested;
  48	struct gpio_desc *desc;
  49};
  50
  51struct acpi_gpio_connection {
  52	struct list_head node;
  53	unsigned int pin;
  54	struct gpio_desc *desc;
  55};
  56
  57struct acpi_gpio_chip {
  58	/*
  59	 * ACPICA requires that the first field of the context parameter
  60	 * passed to acpi_install_address_space_handler() is large enough
  61	 * to hold struct acpi_connection_info.
  62	 */
  63	struct acpi_connection_info conn_info;
  64	struct list_head conns;
  65	struct mutex conn_lock;
  66	struct gpio_chip *chip;
  67	struct list_head events;
  68	struct list_head deferred_req_irqs_list_entry;
  69};
  70
  71/**
  72 * struct acpi_gpio_info - ACPI GPIO specific information
  73 * @adev: reference to ACPI device which consumes GPIO resource
  74 * @flags: GPIO initialization flags
  75 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
  76 * @wake_capable: wake capability as provided by ACPI
  77 * @pin_config: pin bias as provided by ACPI
  78 * @polarity: interrupt polarity as provided by ACPI
  79 * @triggering: triggering type as provided by ACPI
  80 * @debounce: debounce timeout as provided by ACPI
  81 * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
  82 */
  83struct acpi_gpio_info {
  84	struct acpi_device *adev;
  85	enum gpiod_flags flags;
  86	bool gpioint;
  87	bool wake_capable;
  88	int pin_config;
  89	int polarity;
  90	int triggering;
  91	unsigned int debounce;
  92	unsigned int quirks;
  93};
  94
  95static int acpi_gpiochip_find(struct gpio_chip *gc, const void *data)
  96{
  97	/* First check the actual GPIO device */
  98	if (device_match_acpi_handle(&gc->gpiodev->dev, data))
  99		return true;
 100
 101	/*
 102	 * When the ACPI device is artificially split to the banks of GPIOs,
 103	 * where each of them is represented by a separate GPIO device,
 104	 * the firmware node of the physical device may not be shared among
 105	 * the banks as they may require different values for the same property,
 106	 * e.g., number of GPIOs in a certain bank. In such case the ACPI handle
 107	 * of a GPIO device is NULL and can not be used. Hence we have to check
 108	 * the parent device to be sure that there is no match before bailing
 109	 * out.
 110	 */
 111	if (gc->parent)
 112		return device_match_acpi_handle(gc->parent, data);
 113
 114	return false;
 115}
 116
 117/**
 118 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
 119 * @path:	ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
 120 * @pin:	ACPI GPIO pin number (0-based, controller-relative)
 121 *
 122 * Returns:
 123 * GPIO descriptor to use with Linux generic GPIO API.
 124 * If the GPIO cannot be translated or there is an error an ERR_PTR is
 125 * returned.
 126 *
 127 * Specifically returns %-EPROBE_DEFER if the referenced GPIO
 128 * controller does not have GPIO chip registered at the moment. This is to
 129 * support probe deferral.
 130 */
 131static struct gpio_desc *acpi_get_gpiod(char *path, unsigned int pin)
 132{
 133	acpi_handle handle;
 134	acpi_status status;
 135
 136	status = acpi_get_handle(NULL, path, &handle);
 137	if (ACPI_FAILURE(status))
 138		return ERR_PTR(-ENODEV);
 139
 140	struct gpio_device *gdev __free(gpio_device_put) =
 141				gpio_device_find(handle, acpi_gpiochip_find);
 142	if (!gdev)
 143		return ERR_PTR(-EPROBE_DEFER);
 144
 145	/*
 146	 * FIXME: keep track of the reference to the GPIO device somehow
 147	 * instead of putting it here.
 148	 */
 149	return gpio_device_get_desc(gdev, pin);
 150}
 151
 152static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
 153{
 154	struct acpi_gpio_event *event = data;
 155
 156	acpi_evaluate_object(event->handle, NULL, NULL, NULL);
 157
 158	return IRQ_HANDLED;
 159}
 160
 161static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
 162{
 163	struct acpi_gpio_event *event = data;
 164
 165	acpi_execute_simple_method(event->handle, NULL, event->pin);
 166
 167	return IRQ_HANDLED;
 168}
 169
 170static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
 171{
 172	/* The address of this function is used as a key. */
 173}
 174
 175bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
 176				struct acpi_resource_gpio **agpio)
 177{
 178	struct acpi_resource_gpio *gpio;
 179
 180	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
 181		return false;
 182
 183	gpio = &ares->data.gpio;
 184	if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
 185		return false;
 186
 187	*agpio = gpio;
 188	return true;
 189}
 190EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
 191
 192/**
 193 * acpi_gpio_get_io_resource - Fetch details of an ACPI resource if it is a GPIO
 194 *			       I/O resource or return False if not.
 195 * @ares:	Pointer to the ACPI resource to fetch
 196 * @agpio:	Pointer to a &struct acpi_resource_gpio to store the output pointer
 197 *
 198 * Returns:
 199 * %true if GpioIo resource is found, %false otherwise.
 200 */
 201bool acpi_gpio_get_io_resource(struct acpi_resource *ares,
 202			       struct acpi_resource_gpio **agpio)
 203{
 204	struct acpi_resource_gpio *gpio;
 205
 206	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
 207		return false;
 208
 209	gpio = &ares->data.gpio;
 210	if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_IO)
 211		return false;
 212
 213	*agpio = gpio;
 214	return true;
 215}
 216EXPORT_SYMBOL_GPL(acpi_gpio_get_io_resource);
 217
 218static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
 219				      struct acpi_gpio_event *event)
 220{
 221	struct device *parent = acpi_gpio->chip->parent;
 222	int ret, value;
 223
 224	ret = request_threaded_irq(event->irq, NULL, event->handler,
 225				   event->irqflags | IRQF_ONESHOT, "ACPI:Event", event);
 226	if (ret) {
 227		dev_err(parent, "Failed to setup interrupt handler for %d\n", event->irq);
 228		return;
 229	}
 230
 231	if (event->irq_is_wake)
 232		enable_irq_wake(event->irq);
 233
 234	event->irq_requested = true;
 235
 236	/* Make sure we trigger the initial state of edge-triggered IRQs */
 237	if (acpi_gpio_need_run_edge_events_on_boot() &&
 238	    (event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) {
 239		value = gpiod_get_raw_value_cansleep(event->desc);
 240		if (((event->irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
 241		    ((event->irqflags & IRQF_TRIGGER_FALLING) && value == 0))
 242			event->handler(event->irq, event);
 243	}
 244}
 245
 246static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
 247{
 248	struct acpi_gpio_event *event;
 249
 250	list_for_each_entry(event, &acpi_gpio->events, node)
 251		acpi_gpiochip_request_irq(acpi_gpio, event);
 252}
 253
 254static enum gpiod_flags
 255acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio, int polarity)
 256{
 257	/* GpioInt() implies input configuration */
 258	if (agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
 259		return GPIOD_IN;
 260
 261	switch (agpio->io_restriction) {
 262	case ACPI_IO_RESTRICT_INPUT:
 263		return GPIOD_IN;
 264	case ACPI_IO_RESTRICT_OUTPUT:
 265		/*
 266		 * ACPI GPIO resources don't contain an initial value for the
 267		 * GPIO. Therefore we deduce that value from the pull field
 268		 * and the polarity instead. If the pin is pulled up we assume
 269		 * default to be high, if it is pulled down we assume default
 270		 * to be low, otherwise we leave pin untouched. For active low
 271		 * polarity values will be switched. See also
 272		 * Documentation/firmware-guide/acpi/gpio-properties.rst.
 273		 */
 274		switch (agpio->pin_config) {
 275		case ACPI_PIN_CONFIG_PULLUP:
 276			return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH;
 277		case ACPI_PIN_CONFIG_PULLDOWN:
 278			return polarity == GPIO_ACTIVE_LOW ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
 279		default:
 280			break;
 281		}
 282		break;
 283	default:
 284		break;
 285	}
 286
 287	/*
 288	 * Assume that the BIOS has configured the direction and pull
 289	 * accordingly.
 290	 */
 291	return GPIOD_ASIS;
 292}
 293
 294static void acpi_gpio_set_debounce_timeout(struct gpio_desc *desc,
 295					   unsigned int acpi_debounce)
 296{
 297	int ret;
 298
 299	/* ACPI uses hundredths of milliseconds units */
 300	acpi_debounce *= 10;
 301	ret = gpio_set_debounce_timeout(desc, acpi_debounce);
 302	if (ret)
 303		gpiod_warn(desc, "Failed to set debounce-timeout %u: %d\n",
 304			   acpi_debounce, ret);
 305}
 306
 307static struct gpio_desc *acpi_request_own_gpiod(struct gpio_chip *chip,
 308						struct acpi_resource_gpio *agpio,
 309						unsigned int index,
 310						const char *label)
 311{
 312	int polarity = GPIO_ACTIVE_HIGH;
 313	enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio, polarity);
 314	unsigned int pin = agpio->pin_table[index];
 315	struct gpio_desc *desc;
 316
 317	desc = gpiochip_request_own_desc(chip, pin, label, polarity, flags);
 318	if (IS_ERR(desc))
 319		return desc;
 320
 321	acpi_gpio_set_debounce_timeout(desc, agpio->debounce_timeout);
 322
 323	return desc;
 324}
 325
 326static bool acpi_gpio_irq_is_wake(struct device *parent,
 327				  const struct acpi_resource_gpio *agpio)
 328{
 329	unsigned int pin = agpio->pin_table[0];
 330
 331	if (agpio->wake_capable != ACPI_WAKE_CAPABLE)
 332		return false;
 333
 334	if (acpi_gpio_in_ignore_list(ACPI_GPIO_IGNORE_WAKE, dev_name(parent), pin)) {
 335		dev_info(parent, "Ignoring wakeup on pin %u\n", pin);
 336		return false;
 337	}
 338
 339	return true;
 340}
 341
 342/* Always returns AE_OK so that we keep looping over the resources */
 343static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
 344					     void *context)
 345{
 346	struct acpi_gpio_chip *acpi_gpio = context;
 347	struct gpio_chip *chip = acpi_gpio->chip;
 348	struct acpi_resource_gpio *agpio;
 349	acpi_handle handle, evt_handle;
 350	struct acpi_gpio_event *event;
 351	irq_handler_t handler = NULL;
 352	struct gpio_desc *desc;
 353	unsigned int pin;
 354	int ret, irq;
 355
 356	if (!acpi_gpio_get_irq_resource(ares, &agpio))
 357		return AE_OK;
 358
 359	handle = ACPI_HANDLE(chip->parent);
 360	pin = agpio->pin_table[0];
 361
 362	if (pin <= 255) {
 363		char ev_name[8];
 364		sprintf(ev_name, "_%c%02X",
 365			agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
 366			pin);
 367		if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
 368			handler = acpi_gpio_irq_handler;
 369	}
 370	if (!handler) {
 371		if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
 372			handler = acpi_gpio_irq_handler_evt;
 373	}
 374	if (!handler)
 375		return AE_OK;
 376
 377	if (acpi_gpio_in_ignore_list(ACPI_GPIO_IGNORE_INTERRUPT, dev_name(chip->parent), pin)) {
 378		dev_info(chip->parent, "Ignoring interrupt on pin %u\n", pin);
 379		return AE_OK;
 380	}
 381
 382	desc = acpi_request_own_gpiod(chip, agpio, 0, "ACPI:Event");
 383	if (IS_ERR(desc)) {
 384		dev_err(chip->parent,
 385			"Failed to request GPIO for pin 0x%04X, err %pe\n",
 386			pin, desc);
 387		return AE_OK;
 388	}
 389
 390	ret = gpiochip_lock_as_irq(chip, pin);
 391	if (ret) {
 392		dev_err(chip->parent,
 393			"Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
 394			pin, ret);
 395		goto fail_free_desc;
 396	}
 397
 398	irq = gpiod_to_irq(desc);
 399	if (irq < 0) {
 400		dev_err(chip->parent,
 401			"Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
 402			pin, irq);
 403		goto fail_unlock_irq;
 404	}
 405
 406	event = kzalloc(sizeof(*event), GFP_KERNEL);
 407	if (!event)
 408		goto fail_unlock_irq;
 409
 410	event->irqflags = IRQF_ONESHOT;
 411	if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
 412		if (agpio->polarity == ACPI_ACTIVE_HIGH)
 413			event->irqflags |= IRQF_TRIGGER_HIGH;
 414		else
 415			event->irqflags |= IRQF_TRIGGER_LOW;
 416	} else {
 417		switch (agpio->polarity) {
 418		case ACPI_ACTIVE_HIGH:
 419			event->irqflags |= IRQF_TRIGGER_RISING;
 420			break;
 421		case ACPI_ACTIVE_LOW:
 422			event->irqflags |= IRQF_TRIGGER_FALLING;
 423			break;
 424		default:
 425			event->irqflags |= IRQF_TRIGGER_RISING |
 426					   IRQF_TRIGGER_FALLING;
 427			break;
 428		}
 429	}
 430
 431	event->handle = evt_handle;
 432	event->handler = handler;
 433	event->irq = irq;
 434	event->irq_is_wake = acpi_gpio_irq_is_wake(chip->parent, agpio);
 435	event->pin = pin;
 436	event->desc = desc;
 437
 438	list_add_tail(&event->node, &acpi_gpio->events);
 439
 440	return AE_OK;
 441
 442fail_unlock_irq:
 443	gpiochip_unlock_as_irq(chip, pin);
 444fail_free_desc:
 445	gpiochip_free_own_desc(desc);
 446
 447	return AE_OK;
 448}
 449
 450/**
 451 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
 452 * @chip:      GPIO chip
 453 *
 454 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
 455 * handled by ACPI event methods which need to be called from the GPIO
 456 * chip's interrupt handler. acpi_gpiochip_request_interrupts() finds out which
 457 * GPIO pins have ACPI event methods and assigns interrupt handlers that calls
 458 * the ACPI event methods for those pins.
 459 */
 460void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
 461{
 462	struct acpi_gpio_chip *acpi_gpio;
 463	acpi_handle handle;
 464	acpi_status status;
 465
 466	if (!chip->parent || !chip->to_irq)
 467		return;
 468
 469	handle = ACPI_HANDLE(chip->parent);
 470	if (!handle)
 471		return;
 472
 473	status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
 474	if (ACPI_FAILURE(status))
 475		return;
 476
 477	if (acpi_quirk_skip_gpio_event_handlers())
 478		return;
 479
 480	acpi_walk_resources(handle, METHOD_NAME__AEI,
 481			    acpi_gpiochip_alloc_event, acpi_gpio);
 482
 483	if (acpi_gpio_add_to_deferred_list(&acpi_gpio->deferred_req_irqs_list_entry))
 484		return;
 485
 486	acpi_gpiochip_request_irqs(acpi_gpio);
 487}
 488EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
 489
 490/**
 491 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
 492 * @chip:      GPIO chip
 493 *
 494 * Free interrupts associated with GPIO ACPI event method for the given
 495 * GPIO chip.
 496 */
 497void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
 498{
 499	struct acpi_gpio_chip *acpi_gpio;
 500	struct acpi_gpio_event *event, *ep;
 501	acpi_handle handle;
 502	acpi_status status;
 503
 504	if (!chip->parent || !chip->to_irq)
 505		return;
 506
 507	handle = ACPI_HANDLE(chip->parent);
 508	if (!handle)
 509		return;
 510
 511	status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
 512	if (ACPI_FAILURE(status))
 513		return;
 514
 515	acpi_gpio_remove_from_deferred_list(&acpi_gpio->deferred_req_irqs_list_entry);
 516
 517	list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
 518		if (event->irq_requested) {
 519			if (event->irq_is_wake)
 520				disable_irq_wake(event->irq);
 521
 522			free_irq(event->irq, event);
 523		}
 524
 525		gpiochip_unlock_as_irq(chip, event->pin);
 526		gpiochip_free_own_desc(event->desc);
 527		list_del(&event->node);
 528		kfree(event);
 529	}
 530}
 531EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
 532
 533void __init acpi_gpio_process_deferred_list(struct list_head *list)
 534{
 535	struct acpi_gpio_chip *acpi_gpio, *tmp;
 536
 537	list_for_each_entry_safe(acpi_gpio, tmp, list, deferred_req_irqs_list_entry)
 538		acpi_gpiochip_request_irqs(acpi_gpio);
 539}
 540
 541int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 542			      const struct acpi_gpio_mapping *gpios)
 543{
 544	if (adev && gpios) {
 545		adev->driver_gpios = gpios;
 546		return 0;
 547	}
 548	return -EINVAL;
 549}
 550EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
 551
 552void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
 553{
 554	if (adev)
 555		adev->driver_gpios = NULL;
 556}
 557EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
 558
 559static void acpi_dev_release_driver_gpios(void *adev)
 560{
 561	acpi_dev_remove_driver_gpios(adev);
 562}
 563
 564int devm_acpi_dev_add_driver_gpios(struct device *dev,
 565				   const struct acpi_gpio_mapping *gpios)
 566{
 567	struct acpi_device *adev = ACPI_COMPANION(dev);
 568	int ret;
 569
 570	ret = acpi_dev_add_driver_gpios(adev, gpios);
 571	if (ret)
 572		return ret;
 573
 574	return devm_add_action_or_reset(dev, acpi_dev_release_driver_gpios, adev);
 575}
 576EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
 577
 578static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
 579				      const char *name, int index,
 580				      struct fwnode_reference_args *args,
 581				      unsigned int *quirks)
 582{
 583	const struct acpi_gpio_mapping *gm;
 584
 585	if (!adev || !adev->driver_gpios)
 586		return false;
 587
 588	for (gm = adev->driver_gpios; gm->name; gm++)
 589		if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
 590			const struct acpi_gpio_params *params = gm->data + index;
 591
 592			args->fwnode = acpi_fwnode_handle(adev);
 593			args->args[0] = params->crs_entry_index;
 594			args->args[1] = params->line_index;
 595			args->args[2] = params->active_low;
 596			args->nargs = 3;
 597
 598			*quirks = gm->quirks;
 599			return true;
 600		}
 601
 602	return false;
 603}
 604
 605static int
 606__acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
 607{
 608	const enum gpiod_flags mask =
 609		GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
 610		GPIOD_FLAGS_BIT_DIR_VAL;
 611	int ret = 0;
 612
 613	/*
 614	 * Check if the BIOS has IoRestriction with explicitly set direction
 615	 * and update @flags accordingly. Otherwise use whatever caller asked
 616	 * for.
 617	 */
 618	if (update & GPIOD_FLAGS_BIT_DIR_SET) {
 619		enum gpiod_flags diff = *flags ^ update;
 620
 621		/*
 622		 * Check if caller supplied incompatible GPIO initialization
 623		 * flags.
 624		 *
 625		 * Return %-EINVAL to notify that firmware has different
 626		 * settings and we are going to use them.
 627		 */
 628		if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
 629		    ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
 630			ret = -EINVAL;
 631		*flags = (*flags & ~mask) | (update & mask);
 632	}
 633	return ret;
 634}
 635
 636static int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
 637				        struct acpi_gpio_info *info)
 638{
 639	struct device *dev = &info->adev->dev;
 640	enum gpiod_flags old = *flags;
 641	int ret;
 642
 643	ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
 644	if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
 645		if (ret)
 646			dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
 647	} else {
 648		if (ret)
 649			dev_dbg(dev, "Override GPIO initialization flags\n");
 650		*flags = old;
 651	}
 652
 653	return ret;
 654}
 655
 656static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
 657					       struct acpi_gpio_info *info)
 658{
 659	switch (info->pin_config) {
 660	case ACPI_PIN_CONFIG_PULLUP:
 661		*lookupflags |= GPIO_PULL_UP;
 662		break;
 663	case ACPI_PIN_CONFIG_PULLDOWN:
 664		*lookupflags |= GPIO_PULL_DOWN;
 665		break;
 666	case ACPI_PIN_CONFIG_NOPULL:
 667		*lookupflags |= GPIO_PULL_DISABLE;
 668		break;
 669	default:
 670		break;
 671	}
 672
 673	if (info->polarity == GPIO_ACTIVE_LOW)
 674		*lookupflags |= GPIO_ACTIVE_LOW;
 675
 676	return 0;
 677}
 678
 679struct acpi_gpio_lookup {
 680	struct acpi_gpio_params params;
 681	struct acpi_gpio_info *info;
 682	struct gpio_desc *desc;
 683	int n;
 684};
 685
 686static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 687{
 688	struct acpi_gpio_lookup *lookup = data;
 689	struct acpi_gpio_params *params = &lookup->params;
 690	struct acpi_gpio_info *info = lookup->info;
 691
 692	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
 693		return 1;
 694
 695	if (!lookup->desc) {
 696		const struct acpi_resource_gpio *agpio = &ares->data.gpio;
 697		bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
 698		struct gpio_desc *desc;
 699		u16 pin_index;
 700
 701		if (info->quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
 702			params->crs_entry_index++;
 703
 704		if (lookup->n++ != params->crs_entry_index)
 705			return 1;
 706
 707		pin_index = params->line_index;
 708		if (pin_index >= agpio->pin_table_length)
 709			return 1;
 710
 711		if (info->quirks & ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER)
 712			desc = gpio_to_desc(agpio->pin_table[pin_index]);
 713		else
 714			desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
 715					      agpio->pin_table[pin_index]);
 716		lookup->desc = desc;
 717		info->pin_config = agpio->pin_config;
 718		info->debounce = agpio->debounce_timeout;
 719		info->gpioint = gpioint;
 720		info->wake_capable = acpi_gpio_irq_is_wake(&info->adev->dev, agpio);
 721
 722		/*
 723		 * Polarity and triggering are only specified for GpioInt
 724		 * resource.
 725		 * Note: we expect here:
 726		 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
 727		 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
 728		 */
 729		if (info->gpioint) {
 730			info->polarity = agpio->polarity;
 731			info->triggering = agpio->triggering;
 732		} else {
 733			info->polarity = params->active_low;
 734		}
 735
 736		info->flags = acpi_gpio_to_gpiod_flags(agpio, info->polarity);
 737	}
 738
 739	return 1;
 740}
 741
 742static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup)
 743{
 744	struct acpi_gpio_info *info = lookup->info;
 745	struct acpi_device *adev = info->adev;
 746	struct list_head res_list;
 747	int ret;
 748
 749	INIT_LIST_HEAD(&res_list);
 750
 751	ret = acpi_dev_get_resources(adev, &res_list,
 752				     acpi_populate_gpio_lookup,
 753				     lookup);
 754	if (ret < 0)
 755		return ret;
 756
 757	acpi_dev_free_resource_list(&res_list);
 758
 759	if (!lookup->desc)
 760		return -ENOENT;
 761
 762	return 0;
 763}
 764
 765static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *propname,
 766				     struct acpi_gpio_lookup *lookup)
 767{
 768	struct fwnode_reference_args args;
 769	struct acpi_gpio_params *params = &lookup->params;
 770	struct acpi_gpio_info *info = lookup->info;
 771	unsigned int index = params->crs_entry_index;
 772	unsigned int quirks = 0;
 773	int ret;
 774
 775	memset(&args, 0, sizeof(args));
 776
 777	ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, &args);
 778	if (ret) {
 779		struct acpi_device *adev;
 780
 781		adev = to_acpi_device_node(fwnode);
 782		if (!acpi_get_driver_gpio_data(adev, propname, index, &args, &quirks))
 783			return ret;
 784	}
 785	/*
 786	 * The property was found and resolved, so need to lookup the GPIO based
 787	 * on returned args.
 788	 */
 789	if (!to_acpi_device_node(args.fwnode))
 790		return -EINVAL;
 791	if (args.nargs != 3)
 792		return -EPROTO;
 793
 794	params->crs_entry_index = args.args[0];
 795	params->line_index = args.args[1];
 796	params->active_low = !!args.args[2];
 797
 798	info->adev = to_acpi_device_node(args.fwnode);
 799	info->quirks = quirks;
 800
 801	return 0;
 802}
 803
 804/**
 805 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
 806 * @adev: pointer to a ACPI device to get GPIO from
 807 * @propname: Property name of the GPIO (optional)
 808 * @lookup: pointer to struct acpi_gpio_lookup to fill in
 809 *
 810 * Function goes through ACPI resources for @adev and based on @lookup.index looks
 811 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
 812 * and returns it. @lookup.index matches GpioIo/GpioInt resources only so if there
 813 * are total 3 GPIO resources, the index goes from 0 to 2.
 814 *
 815 * If @propname is specified the GPIO is looked using device property. In
 816 * that case @index is used to select the GPIO entry in the property value
 817 * (in case of multiple).
 818 *
 819 * Returns:
 820 * 0 on success, negative errno on failure.
 821 *
 822 * The @lookup is filled with GPIO descriptor to use with Linux generic GPIO API.
 823 * If the GPIO cannot be translated an error will be returned.
 824 *
 825 * Note: if the GPIO resource has multiple entries in the pin list, this
 826 * function only returns the first.
 827 */
 828static int acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
 829				   struct acpi_gpio_lookup *lookup)
 830{
 831	struct acpi_gpio_params *params = &lookup->params;
 832	struct acpi_gpio_info *info = lookup->info;
 833	int ret;
 834
 835	if (propname) {
 836		dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
 837
 838		ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), propname, lookup);
 839		if (ret)
 840			return ret;
 841
 842		dev_dbg(&adev->dev, "GPIO: _DSD returned %s %u %u %u\n",
 843			dev_name(&info->adev->dev),
 844			params->crs_entry_index, params->line_index, params->active_low);
 845	} else {
 846		dev_dbg(&adev->dev, "GPIO: looking up %u in _CRS\n", params->crs_entry_index);
 847		info->adev = adev;
 848	}
 849
 850	return acpi_gpio_resource_lookup(lookup);
 851}
 852
 853/**
 854 * acpi_get_gpiod_from_data() - get a GPIO descriptor from ACPI data node
 855 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
 856 * @propname: Property name of the GPIO
 857 * @lookup: pointer to struct acpi_gpio_lookup to fill in
 858 *
 859 * This function uses the property-based GPIO lookup to get to the GPIO
 860 * resource with the relevant information from a data-only ACPI firmware node
 861 * and uses that to obtain the GPIO descriptor to return.
 862 *
 863 * Returns:
 864 * 0 on success, negative errno on failure.
 865 *
 866 * The @lookup is filled with GPIO descriptor to use with Linux generic GPIO API.
 867 * If the GPIO cannot be translated an error will be returned.
 868 */
 869static int acpi_get_gpiod_from_data(struct fwnode_handle *fwnode, const char *propname,
 870				    struct acpi_gpio_lookup *lookup)
 871{
 872	int ret;
 873
 874	if (!is_acpi_data_node(fwnode))
 875		return -ENODEV;
 876
 877	if (!propname)
 878		return -EINVAL;
 879
 880	ret = acpi_gpio_property_lookup(fwnode, propname, lookup);
 881	if (ret)
 882		return ret;
 883
 884	return acpi_gpio_resource_lookup(lookup);
 885}
 886
 887static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
 888				     const char *con_id)
 889{
 890	/* If there is no ACPI device, there is no _CRS to fall back to */
 891	if (!adev)
 892		return false;
 893
 894	/* Never allow fallback if the device has properties */
 895	if (acpi_dev_has_props(adev) || adev->driver_gpios)
 896		return false;
 897
 898	return con_id == NULL;
 899}
 900
 901static struct gpio_desc *
 902__acpi_find_gpio(struct fwnode_handle *fwnode, const char *con_id, unsigned int idx,
 903		 bool can_fallback, struct acpi_gpio_info *info)
 904{
 905	struct acpi_device *adev = to_acpi_device_node(fwnode);
 906	struct acpi_gpio_lookup lookup;
 907	struct gpio_desc *desc;
 908	char propname[32];
 909	int ret;
 910
 911	memset(&lookup, 0, sizeof(lookup));
 912	lookup.params.crs_entry_index = idx;
 913	lookup.info = info;
 914
 915	/* Try first from _DSD */
 916	for_each_gpio_property_name(propname, con_id) {
 917		if (adev)
 918			ret = acpi_get_gpiod_by_index(adev, propname, &lookup);
 919		else
 920			ret = acpi_get_gpiod_from_data(fwnode, propname, &lookup);
 921		if (ret)
 922			continue;
 923
 924		desc = lookup.desc;
 925		if (PTR_ERR(desc) == -EPROBE_DEFER)
 926			return desc;
 927
 928		if (!IS_ERR(desc))
 929			return desc;
 930	}
 931
 932	/* Then from plain _CRS GPIOs */
 933	if (can_fallback) {
 934		ret = acpi_get_gpiod_by_index(adev, NULL, &lookup);
 935		if (ret)
 936			return ERR_PTR(ret);
 937
 938		return lookup.desc;
 939	}
 940
 941	return ERR_PTR(-ENOENT);
 942}
 943
 944struct gpio_desc *acpi_find_gpio(struct fwnode_handle *fwnode,
 945				 const char *con_id,
 946				 unsigned int idx,
 947				 enum gpiod_flags *dflags,
 948				 unsigned long *lookupflags)
 949{
 950	struct acpi_device *adev = to_acpi_device_node(fwnode);
 951	bool can_fallback = acpi_can_fallback_to_crs(adev, con_id);
 952	struct acpi_gpio_info info = {};
 953	struct gpio_desc *desc;
 954
 955	desc = __acpi_find_gpio(fwnode, con_id, idx, can_fallback, &info);
 956	if (IS_ERR(desc))
 957		return desc;
 958
 959	if (info.gpioint &&
 960	    (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
 961		dev_dbg(&adev->dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
 962		return ERR_PTR(-ENOENT);
 963	}
 964
 965	acpi_gpio_update_gpiod_flags(dflags, &info);
 966	acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
 967
 968	acpi_gpio_set_debounce_timeout(desc, info.debounce);
 969
 970	return desc;
 971}
 972
 973/**
 974 * acpi_dev_gpio_irq_wake_get_by() - Find GpioInt and translate it to Linux IRQ number
 975 * @adev: pointer to a ACPI device to get IRQ from
 976 * @con_id: optional name of GpioInt resource
 977 * @index: index of GpioInt resource (starting from %0)
 978 * @wake_capable: Set to true if the IRQ is wake capable
 979 *
 980 * If the device has one or more GpioInt resources, this function can be
 981 * used to translate from the GPIO offset in the resource to the Linux IRQ
 982 * number.
 983 *
 984 * The function is idempotent, though each time it runs it will configure GPIO
 985 * pin direction according to the flags in GpioInt resource.
 986 *
 987 * The function takes optional @con_id parameter. If the resource has
 988 * a @con_id in a property, then only those will be taken into account.
 989 *
 990 * The GPIO is considered wake capable if the GpioInt resource specifies
 991 * SharedAndWake or ExclusiveAndWake.
 992 *
 993 * Returns:
 994 * Linux IRQ number (> 0) on success, negative errno on failure.
 995 */
 996int acpi_dev_gpio_irq_wake_get_by(struct acpi_device *adev, const char *con_id, int index,
 997				  bool *wake_capable)
 998{
 999	struct fwnode_handle *fwnode = acpi_fwnode_handle(adev);
1000	int idx, i;
1001	unsigned int irq_flags;
1002	int ret;
1003
1004	for (i = 0, idx = 0; idx <= index; i++) {
1005		struct acpi_gpio_info info = {};
1006		struct gpio_desc *desc;
1007
1008		/* Ignore -EPROBE_DEFER, it only matters if idx matches */
1009		desc = __acpi_find_gpio(fwnode, con_id, i, true, &info);
1010		if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
1011			return PTR_ERR(desc);
1012
1013		if (info.gpioint && idx++ == index) {
1014			unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
1015			enum gpiod_flags dflags = GPIOD_ASIS;
1016			char label[32];
1017			int irq;
1018
1019			if (IS_ERR(desc))
1020				return PTR_ERR(desc);
1021
1022			irq = gpiod_to_irq(desc);
1023			if (irq < 0)
1024				return irq;
1025
1026			acpi_gpio_update_gpiod_flags(&dflags, &info);
1027			acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
1028
1029			snprintf(label, sizeof(label), "%pfwP GpioInt(%d)", fwnode, index);
1030			ret = gpiod_set_consumer_name(desc, con_id ?: label);
1031			if (ret)
1032				return ret;
1033
1034			ret = gpiod_configure_flags(desc, label, lflags, dflags);
1035			if (ret < 0)
1036				return ret;
1037
1038			/* ACPI uses hundredths of milliseconds units */
1039			ret = gpio_set_debounce_timeout(desc, info.debounce * 10);
1040			if (ret)
1041				return ret;
1042
1043			irq_flags = acpi_dev_get_irq_type(info.triggering,
1044							  info.polarity);
1045
1046			/*
1047			 * If the IRQ is not already in use then set type
1048			 * if specified and different than the current one.
1049			 */
1050			if (can_request_irq(irq, irq_flags)) {
1051				if (irq_flags != IRQ_TYPE_NONE &&
1052				    irq_flags != irq_get_trigger_type(irq))
1053					irq_set_irq_type(irq, irq_flags);
1054			} else {
1055				dev_dbg(&adev->dev, "IRQ %d already in use\n", irq);
1056			}
1057
1058			/* avoid suspend issues with GPIOs when systems are using S3 */
1059			if (wake_capable && acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
1060				*wake_capable = info.wake_capable;
1061
1062			return irq;
1063		}
1064
1065	}
1066	return -ENOENT;
1067}
1068EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_wake_get_by);
1069
1070static acpi_status
1071acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
1072			    u32 bits, u64 *value, void *handler_context,
1073			    void *region_context)
1074{
1075	struct acpi_gpio_chip *achip = region_context;
1076	struct gpio_chip *chip = achip->chip;
1077	struct acpi_resource_gpio *agpio;
1078	struct acpi_resource *ares;
1079	u16 pin_index = address;
1080	acpi_status status;
1081	int length;
1082	int i;
1083
1084	status = acpi_buffer_to_resource(achip->conn_info.connection,
1085					 achip->conn_info.length, &ares);
1086	if (ACPI_FAILURE(status))
1087		return status;
1088
1089	if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
1090		ACPI_FREE(ares);
1091		return AE_BAD_PARAMETER;
1092	}
1093
1094	agpio = &ares->data.gpio;
1095
1096	if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
1097	    function == ACPI_WRITE)) {
1098		ACPI_FREE(ares);
1099		return AE_BAD_PARAMETER;
1100	}
1101
1102	length = min(agpio->pin_table_length, pin_index + bits);
1103	for (i = pin_index; i < length; ++i) {
1104		unsigned int pin = agpio->pin_table[i];
1105		struct acpi_gpio_connection *conn;
1106		struct gpio_desc *desc;
1107		u16 word, shift;
1108		bool found;
1109
1110		mutex_lock(&achip->conn_lock);
1111
1112		found = false;
1113		list_for_each_entry(conn, &achip->conns, node) {
1114			if (conn->pin == pin) {
1115				found = true;
1116				desc = conn->desc;
1117				break;
1118			}
1119		}
1120
1121		/*
1122		 * The same GPIO can be shared between operation region and
1123		 * event but only if the access here is ACPI_READ. In that
1124		 * case we "borrow" the event GPIO instead.
1125		 */
1126		if (!found && agpio->shareable == ACPI_SHARED &&
1127		     function == ACPI_READ) {
1128			struct acpi_gpio_event *event;
1129
1130			list_for_each_entry(event, &achip->events, node) {
1131				if (event->pin == pin) {
1132					desc = event->desc;
1133					found = true;
1134					break;
1135				}
1136			}
1137		}
1138
1139		if (!found) {
1140			desc = acpi_request_own_gpiod(chip, agpio, i, "ACPI:OpRegion");
1141			if (IS_ERR(desc)) {
1142				mutex_unlock(&achip->conn_lock);
1143				status = AE_ERROR;
1144				goto out;
1145			}
1146
1147			conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1148			if (!conn) {
1149				gpiochip_free_own_desc(desc);
1150				mutex_unlock(&achip->conn_lock);
1151				status = AE_NO_MEMORY;
1152				goto out;
1153			}
1154
1155			conn->pin = pin;
1156			conn->desc = desc;
1157			list_add_tail(&conn->node, &achip->conns);
1158		}
1159
1160		mutex_unlock(&achip->conn_lock);
1161
1162		/*
1163		 * For the cases when OperationRegion() consists of more than
1164		 * 64 bits calculate the word and bit shift to use that one to
1165		 * access the value.
1166		 */
1167		word = i / 64;
1168		shift = i % 64;
1169
1170		if (function == ACPI_WRITE) {
1171			gpiod_set_raw_value_cansleep(desc, value[word] & BIT_ULL(shift));
1172		} else {
1173			if (gpiod_get_raw_value_cansleep(desc))
1174				value[word] |= BIT_ULL(shift);
1175			else
1176				value[word] &= ~BIT_ULL(shift);
1177		}
1178	}
1179
1180out:
1181	ACPI_FREE(ares);
1182	return status;
1183}
1184
1185static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
1186{
1187	struct gpio_chip *chip = achip->chip;
1188	acpi_handle handle = ACPI_HANDLE(chip->parent);
1189	acpi_status status;
1190
1191	INIT_LIST_HEAD(&achip->conns);
1192	mutex_init(&achip->conn_lock);
1193	status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1194						    acpi_gpio_adr_space_handler,
1195						    NULL, achip);
1196	if (ACPI_FAILURE(status))
1197		dev_err(chip->parent,
1198		        "Failed to install GPIO OpRegion handler\n");
1199}
1200
1201static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
1202{
1203	struct gpio_chip *chip = achip->chip;
1204	acpi_handle handle = ACPI_HANDLE(chip->parent);
1205	struct acpi_gpio_connection *conn, *tmp;
1206	acpi_status status;
1207
1208	status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1209						   acpi_gpio_adr_space_handler);
1210	if (ACPI_FAILURE(status)) {
1211		dev_err(chip->parent,
1212			"Failed to remove GPIO OpRegion handler\n");
1213		return;
1214	}
1215
1216	list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
1217		gpiochip_free_own_desc(conn->desc);
1218		list_del(&conn->node);
1219		kfree(conn);
1220	}
1221}
1222
1223static struct gpio_desc *
1224acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
1225			     struct fwnode_handle *fwnode,
1226			     const char **name,
1227			     unsigned long *lflags,
1228			     enum gpiod_flags *dflags)
1229{
1230	struct gpio_chip *chip = achip->chip;
1231	struct gpio_desc *desc;
1232	u32 gpios[2];
1233	int ret;
1234
1235	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
1236	*dflags = GPIOD_ASIS;
1237	*name = NULL;
1238
1239	ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
1240					     ARRAY_SIZE(gpios));
1241	if (ret < 0)
1242		return ERR_PTR(ret);
1243
1244	desc = gpiochip_get_desc(chip, gpios[0]);
1245	if (IS_ERR(desc))
1246		return desc;
1247
1248	if (gpios[1])
1249		*lflags |= GPIO_ACTIVE_LOW;
1250
1251	if (fwnode_property_present(fwnode, "input"))
1252		*dflags |= GPIOD_IN;
1253	else if (fwnode_property_present(fwnode, "output-low"))
1254		*dflags |= GPIOD_OUT_LOW;
1255	else if (fwnode_property_present(fwnode, "output-high"))
1256		*dflags |= GPIOD_OUT_HIGH;
1257	else
1258		return ERR_PTR(-EINVAL);
1259
1260	fwnode_property_read_string(fwnode, "line-name", name);
1261
1262	return desc;
1263}
1264
1265static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1266{
1267	struct gpio_chip *chip = achip->chip;
1268
1269	device_for_each_child_node_scoped(chip->parent, fwnode) {
1270		unsigned long lflags;
1271		enum gpiod_flags dflags;
1272		struct gpio_desc *desc;
1273		const char *name;
1274		int ret;
1275
1276		if (!fwnode_property_present(fwnode, "gpio-hog"))
1277			continue;
1278
1279		desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1280						    &lflags, &dflags);
1281		if (IS_ERR(desc))
1282			continue;
1283
1284		ret = gpiod_hog(desc, name, lflags, dflags);
1285		if (ret) {
1286			dev_err(chip->parent, "Failed to hog GPIO\n");
1287			return;
1288		}
1289	}
1290}
1291
1292void acpi_gpiochip_add(struct gpio_chip *chip)
1293{
1294	struct acpi_gpio_chip *acpi_gpio;
1295	struct acpi_device *adev;
1296	acpi_status status;
1297
1298	if (!chip || !chip->parent)
1299		return;
1300
1301	adev = ACPI_COMPANION(chip->parent);
1302	if (!adev)
1303		return;
1304
1305	acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1306	if (!acpi_gpio) {
1307		dev_err(chip->parent,
1308			"Failed to allocate memory for ACPI GPIO chip\n");
1309		return;
1310	}
1311
1312	acpi_gpio->chip = chip;
1313	INIT_LIST_HEAD(&acpi_gpio->events);
1314	INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
1315
1316	status = acpi_attach_data(adev->handle, acpi_gpio_chip_dh, acpi_gpio);
1317	if (ACPI_FAILURE(status)) {
1318		dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
1319		kfree(acpi_gpio);
1320		return;
1321	}
1322
1323	acpi_gpiochip_request_regions(acpi_gpio);
1324	acpi_gpiochip_scan_gpios(acpi_gpio);
1325	acpi_dev_clear_dependencies(adev);
1326}
1327
1328void acpi_gpiochip_remove(struct gpio_chip *chip)
1329{
1330	struct acpi_gpio_chip *acpi_gpio;
1331	acpi_handle handle;
1332	acpi_status status;
1333
1334	if (!chip || !chip->parent)
1335		return;
1336
1337	handle = ACPI_HANDLE(chip->parent);
1338	if (!handle)
1339		return;
1340
1341	status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1342	if (ACPI_FAILURE(status)) {
1343		dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
1344		return;
1345	}
1346
1347	acpi_gpiochip_free_regions(acpi_gpio);
1348
1349	acpi_detach_data(handle, acpi_gpio_chip_dh);
1350	kfree(acpi_gpio);
1351}
1352
1353static int acpi_gpio_package_count(const union acpi_object *obj)
1354{
1355	const union acpi_object *element = obj->package.elements;
1356	const union acpi_object *end = element + obj->package.count;
1357	unsigned int count = 0;
1358
1359	while (element < end) {
1360		switch (element->type) {
1361		case ACPI_TYPE_LOCAL_REFERENCE:
1362		case ACPI_TYPE_STRING:
1363			element += 3;
1364			fallthrough;
1365		case ACPI_TYPE_INTEGER:
1366			element++;
1367			count++;
1368			break;
1369
1370		default:
1371			return -EPROTO;
1372		}
1373	}
1374
1375	return count;
1376}
1377
1378static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1379{
1380	unsigned int *count = data;
1381
1382	if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1383		*count += ares->data.gpio.pin_table_length;
1384
1385	return 1;
1386}
1387
1388/**
1389 * acpi_gpio_count - count the GPIOs associated with a firmware node / function
1390 * @fwnode:	firmware node of the GPIO consumer
1391 * @con_id:	function within the GPIO consumer
1392 *
1393 * Returns:
1394 * The number of GPIOs associated with a firmware node / function or %-ENOENT,
1395 * if no GPIO has been assigned to the requested function.
1396 */
1397int acpi_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
1398{
1399	struct acpi_device *adev = to_acpi_device_node(fwnode);
1400	const union acpi_object *obj;
1401	const struct acpi_gpio_mapping *gm;
1402	int count = -ENOENT;
1403	int ret;
1404	char propname[32];
1405
1406	/* Try first from _DSD */
1407	for_each_gpio_property_name(propname, con_id) {
1408		ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY, &obj);
1409		if (ret == 0) {
1410			if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1411				count = 1;
1412			else if (obj->type == ACPI_TYPE_PACKAGE)
1413				count = acpi_gpio_package_count(obj);
1414		} else if (adev->driver_gpios) {
1415			for (gm = adev->driver_gpios; gm->name; gm++)
1416				if (strcmp(propname, gm->name) == 0) {
1417					count = gm->size;
1418					break;
1419				}
1420		}
1421		if (count > 0)
1422			break;
1423	}
1424
1425	/* Then from plain _CRS GPIOs */
1426	if (count < 0) {
1427		struct list_head resource_list;
1428		unsigned int crs_count = 0;
1429
1430		if (!acpi_can_fallback_to_crs(adev, con_id))
1431			return count;
1432
1433		INIT_LIST_HEAD(&resource_list);
1434		acpi_dev_get_resources(adev, &resource_list,
1435				       acpi_find_gpio_count, &crs_count);
1436		acpi_dev_free_resource_list(&resource_list);
1437		if (crs_count > 0)
1438			count = crs_count;
1439	}
1440	return count ? count : -ENOENT;
1441}
~/ Read-only file preview.