1 /* 2 * ACPI helpers for GPIO API 3 * 4 * Copyright (C) 2012, Intel Corporation 5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> 6 * Mika Westerberg <mika.westerberg@linux.intel.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/errno.h> 14 #include <linux/gpio.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/gpio/driver.h> 17 #include <linux/gpio/machine.h> 18 #include <linux/export.h> 19 #include <linux/acpi.h> 20 #include <linux/interrupt.h> 21 #include <linux/mutex.h> 22 #include <linux/pinctrl/pinctrl.h> 23 24 #include "gpiolib.h" 25 26 struct acpi_gpio_event { 27 struct list_head node; 28 struct list_head initial_sync_list; 29 acpi_handle handle; 30 unsigned int pin; 31 unsigned int irq; 32 struct gpio_desc *desc; 33 }; 34 35 struct acpi_gpio_connection { 36 struct list_head node; 37 unsigned int pin; 38 struct gpio_desc *desc; 39 }; 40 41 struct acpi_gpio_chip { 42 /* 43 * ACPICA requires that the first field of the context parameter 44 * passed to acpi_install_address_space_handler() is large enough 45 * to hold struct acpi_connection_info. 46 */ 47 struct acpi_connection_info conn_info; 48 struct list_head conns; 49 struct mutex conn_lock; 50 struct gpio_chip *chip; 51 struct list_head events; 52 }; 53 54 static LIST_HEAD(acpi_gpio_initial_sync_list); 55 static DEFINE_MUTEX(acpi_gpio_initial_sync_list_lock); 56 57 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) 58 { 59 if (!gc->parent) 60 return false; 61 62 return ACPI_HANDLE(gc->parent) == data; 63 } 64 65 /** 66 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API 67 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") 68 * @pin: ACPI GPIO pin number (0-based, controller-relative) 69 * 70 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR 71 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO 72 * controller does not have gpiochip registered at the moment. This is to 73 * support probe deferral. 74 */ 75 static struct gpio_desc *acpi_get_gpiod(char *path, int pin) 76 { 77 struct gpio_chip *chip; 78 acpi_handle handle; 79 acpi_status status; 80 81 status = acpi_get_handle(NULL, path, &handle); 82 if (ACPI_FAILURE(status)) 83 return ERR_PTR(-ENODEV); 84 85 chip = gpiochip_find(handle, acpi_gpiochip_find); 86 if (!chip) 87 return ERR_PTR(-EPROBE_DEFER); 88 89 return gpiochip_get_desc(chip, pin); 90 } 91 92 static void acpi_gpio_add_to_initial_sync_list(struct acpi_gpio_event *event) 93 { 94 mutex_lock(&acpi_gpio_initial_sync_list_lock); 95 list_add(&event->initial_sync_list, &acpi_gpio_initial_sync_list); 96 mutex_unlock(&acpi_gpio_initial_sync_list_lock); 97 } 98 99 static void acpi_gpio_del_from_initial_sync_list(struct acpi_gpio_event *event) 100 { 101 mutex_lock(&acpi_gpio_initial_sync_list_lock); 102 if (!list_empty(&event->initial_sync_list)) 103 list_del_init(&event->initial_sync_list); 104 mutex_unlock(&acpi_gpio_initial_sync_list_lock); 105 } 106 107 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) 108 { 109 struct acpi_gpio_event *event = data; 110 111 acpi_evaluate_object(event->handle, NULL, NULL, NULL); 112 113 return IRQ_HANDLED; 114 } 115 116 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) 117 { 118 struct acpi_gpio_event *event = data; 119 120 acpi_execute_simple_method(event->handle, NULL, event->pin); 121 122 return IRQ_HANDLED; 123 } 124 125 static void acpi_gpio_chip_dh(acpi_handle handle, void *data) 126 { 127 /* The address of this function is used as a key. */ 128 } 129 130 bool acpi_gpio_get_irq_resource(struct acpi_resource *ares, 131 struct acpi_resource_gpio **agpio) 132 { 133 struct acpi_resource_gpio *gpio; 134 135 if (ares->type != ACPI_RESOURCE_TYPE_GPIO) 136 return false; 137 138 gpio = &ares->data.gpio; 139 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT) 140 return false; 141 142 *agpio = gpio; 143 return true; 144 } 145 EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource); 146 147 static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares, 148 void *context) 149 { 150 struct acpi_gpio_chip *acpi_gpio = context; 151 struct gpio_chip *chip = acpi_gpio->chip; 152 struct acpi_resource_gpio *agpio; 153 acpi_handle handle, evt_handle; 154 struct acpi_gpio_event *event; 155 irq_handler_t handler = NULL; 156 struct gpio_desc *desc; 157 unsigned long irqflags; 158 int ret, pin, irq, value; 159 160 if (!acpi_gpio_get_irq_resource(ares, &agpio)) 161 return AE_OK; 162 163 handle = ACPI_HANDLE(chip->parent); 164 pin = agpio->pin_table[0]; 165 166 if (pin <= 255) { 167 char ev_name[5]; 168 sprintf(ev_name, "_%c%02hhX", 169 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', 170 pin); 171 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) 172 handler = acpi_gpio_irq_handler; 173 } 174 if (!handler) { 175 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle))) 176 handler = acpi_gpio_irq_handler_evt; 177 } 178 if (!handler) 179 return AE_OK; 180 181 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event"); 182 if (IS_ERR(desc)) { 183 dev_err(chip->parent, "Failed to request GPIO\n"); 184 return AE_ERROR; 185 } 186 187 gpiod_direction_input(desc); 188 189 value = gpiod_get_value(desc); 190 191 ret = gpiochip_lock_as_irq(chip, pin); 192 if (ret) { 193 dev_err(chip->parent, "Failed to lock GPIO as interrupt\n"); 194 goto fail_free_desc; 195 } 196 197 irq = gpiod_to_irq(desc); 198 if (irq < 0) { 199 dev_err(chip->parent, "Failed to translate GPIO to IRQ\n"); 200 goto fail_unlock_irq; 201 } 202 203 irqflags = IRQF_ONESHOT; 204 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) { 205 if (agpio->polarity == ACPI_ACTIVE_HIGH) 206 irqflags |= IRQF_TRIGGER_HIGH; 207 else 208 irqflags |= IRQF_TRIGGER_LOW; 209 } else { 210 switch (agpio->polarity) { 211 case ACPI_ACTIVE_HIGH: 212 irqflags |= IRQF_TRIGGER_RISING; 213 break; 214 case ACPI_ACTIVE_LOW: 215 irqflags |= IRQF_TRIGGER_FALLING; 216 break; 217 default: 218 irqflags |= IRQF_TRIGGER_RISING | 219 IRQF_TRIGGER_FALLING; 220 break; 221 } 222 } 223 224 event = kzalloc(sizeof(*event), GFP_KERNEL); 225 if (!event) 226 goto fail_unlock_irq; 227 228 event->handle = evt_handle; 229 event->irq = irq; 230 event->pin = pin; 231 event->desc = desc; 232 INIT_LIST_HEAD(&event->initial_sync_list); 233 234 ret = request_threaded_irq(event->irq, NULL, handler, irqflags, 235 "ACPI:Event", event); 236 if (ret) { 237 dev_err(chip->parent, 238 "Failed to setup interrupt handler for %d\n", 239 event->irq); 240 goto fail_free_event; 241 } 242 243 if (agpio->wake_capable == ACPI_WAKE_CAPABLE) 244 enable_irq_wake(irq); 245 246 list_add_tail(&event->node, &acpi_gpio->events); 247 248 /* 249 * Make sure we trigger the initial state of the IRQ when using RISING 250 * or FALLING. Note we run the handlers on late_init, the AML code 251 * may refer to OperationRegions from other (builtin) drivers which 252 * may be probed after us. 253 */ 254 if (handler == acpi_gpio_irq_handler && 255 (((irqflags & IRQF_TRIGGER_RISING) && value == 1) || 256 ((irqflags & IRQF_TRIGGER_FALLING) && value == 0))) 257 acpi_gpio_add_to_initial_sync_list(event); 258 259 return AE_OK; 260 261 fail_free_event: 262 kfree(event); 263 fail_unlock_irq: 264 gpiochip_unlock_as_irq(chip, pin); 265 fail_free_desc: 266 gpiochip_free_own_desc(desc); 267 268 return AE_ERROR; 269 } 270 271 /** 272 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events 273 * @chip: GPIO chip 274 * 275 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are 276 * handled by ACPI event methods which need to be called from the GPIO 277 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which 278 * gpio pins have acpi event methods and assigns interrupt handlers that calls 279 * the acpi event methods for those pins. 280 */ 281 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) 282 { 283 struct acpi_gpio_chip *acpi_gpio; 284 acpi_handle handle; 285 acpi_status status; 286 287 if (!chip->parent || !chip->to_irq) 288 return; 289 290 handle = ACPI_HANDLE(chip->parent); 291 if (!handle) 292 return; 293 294 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); 295 if (ACPI_FAILURE(status)) 296 return; 297 298 acpi_walk_resources(handle, "_AEI", 299 acpi_gpiochip_request_interrupt, acpi_gpio); 300 } 301 EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts); 302 303 /** 304 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts. 305 * @chip: GPIO chip 306 * 307 * Free interrupts associated with GPIO ACPI event method for the given 308 * GPIO chip. 309 */ 310 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) 311 { 312 struct acpi_gpio_chip *acpi_gpio; 313 struct acpi_gpio_event *event, *ep; 314 acpi_handle handle; 315 acpi_status status; 316 317 if (!chip->parent || !chip->to_irq) 318 return; 319 320 handle = ACPI_HANDLE(chip->parent); 321 if (!handle) 322 return; 323 324 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); 325 if (ACPI_FAILURE(status)) 326 return; 327 328 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) { 329 struct gpio_desc *desc; 330 331 acpi_gpio_del_from_initial_sync_list(event); 332 333 if (irqd_is_wakeup_set(irq_get_irq_data(event->irq))) 334 disable_irq_wake(event->irq); 335 336 free_irq(event->irq, event); 337 desc = event->desc; 338 if (WARN_ON(IS_ERR(desc))) 339 continue; 340 gpiochip_unlock_as_irq(chip, event->pin); 341 gpiochip_free_own_desc(desc); 342 list_del(&event->node); 343 kfree(event); 344 } 345 } 346 EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts); 347 348 int acpi_dev_add_driver_gpios(struct acpi_device *adev, 349 const struct acpi_gpio_mapping *gpios) 350 { 351 if (adev && gpios) { 352 adev->driver_gpios = gpios; 353 return 0; 354 } 355 return -EINVAL; 356 } 357 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios); 358 359 static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res) 360 { 361 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev)); 362 } 363 364 int devm_acpi_dev_add_driver_gpios(struct device *dev, 365 const struct acpi_gpio_mapping *gpios) 366 { 367 void *res; 368 int ret; 369 370 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL); 371 if (!res) 372 return -ENOMEM; 373 374 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios); 375 if (ret) { 376 devres_free(res); 377 return ret; 378 } 379 devres_add(dev, res); 380 return 0; 381 } 382 EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios); 383 384 void devm_acpi_dev_remove_driver_gpios(struct device *dev) 385 { 386 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL)); 387 } 388 EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios); 389 390 static bool acpi_get_driver_gpio_data(struct acpi_device *adev, 391 const char *name, int index, 392 struct acpi_reference_args *args, 393 unsigned int *quirks) 394 { 395 const struct acpi_gpio_mapping *gm; 396 397 if (!adev->driver_gpios) 398 return false; 399 400 for (gm = adev->driver_gpios; gm->name; gm++) 401 if (!strcmp(name, gm->name) && gm->data && index < gm->size) { 402 const struct acpi_gpio_params *par = gm->data + index; 403 404 args->adev = adev; 405 args->args[0] = par->crs_entry_index; 406 args->args[1] = par->line_index; 407 args->args[2] = par->active_low; 408 args->nargs = 3; 409 410 *quirks = gm->quirks; 411 return true; 412 } 413 414 return false; 415 } 416 417 static enum gpiod_flags 418 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio) 419 { 420 bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; 421 422 switch (agpio->io_restriction) { 423 case ACPI_IO_RESTRICT_INPUT: 424 return GPIOD_IN; 425 case ACPI_IO_RESTRICT_OUTPUT: 426 /* 427 * ACPI GPIO resources don't contain an initial value for the 428 * GPIO. Therefore we deduce that value from the pull field 429 * instead. If the pin is pulled up we assume default to be 430 * high, otherwise low. 431 */ 432 return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 433 default: 434 /* 435 * Assume that the BIOS has configured the direction and pull 436 * accordingly. 437 */ 438 return GPIOD_ASIS; 439 } 440 } 441 442 static int 443 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update) 444 { 445 int ret = 0; 446 447 /* 448 * Check if the BIOS has IoRestriction with explicitly set direction 449 * and update @flags accordingly. Otherwise use whatever caller asked 450 * for. 451 */ 452 if (update & GPIOD_FLAGS_BIT_DIR_SET) { 453 enum gpiod_flags diff = *flags ^ update; 454 455 /* 456 * Check if caller supplied incompatible GPIO initialization 457 * flags. 458 * 459 * Return %-EINVAL to notify that firmware has different 460 * settings and we are going to use them. 461 */ 462 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) || 463 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL))) 464 ret = -EINVAL; 465 *flags = update; 466 } 467 return ret; 468 } 469 470 int 471 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info) 472 { 473 struct device *dev = &info->adev->dev; 474 enum gpiod_flags old = *flags; 475 int ret; 476 477 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags); 478 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) { 479 if (ret) 480 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n"); 481 } else { 482 if (ret) 483 dev_dbg(dev, "Override GPIO initialization flags\n"); 484 *flags = old; 485 } 486 487 return ret; 488 } 489 490 struct acpi_gpio_lookup { 491 struct acpi_gpio_info info; 492 int index; 493 int pin_index; 494 bool active_low; 495 struct gpio_desc *desc; 496 int n; 497 }; 498 499 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data) 500 { 501 struct acpi_gpio_lookup *lookup = data; 502 503 if (ares->type != ACPI_RESOURCE_TYPE_GPIO) 504 return 1; 505 506 if (lookup->n++ == lookup->index && !lookup->desc) { 507 const struct acpi_resource_gpio *agpio = &ares->data.gpio; 508 int pin_index = lookup->pin_index; 509 510 if (pin_index >= agpio->pin_table_length) 511 return 1; 512 513 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, 514 agpio->pin_table[pin_index]); 515 lookup->info.gpioint = 516 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; 517 518 /* 519 * Polarity and triggering are only specified for GpioInt 520 * resource. 521 * Note: we expect here: 522 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW 523 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH 524 */ 525 if (lookup->info.gpioint) { 526 lookup->info.flags = GPIOD_IN; 527 lookup->info.polarity = agpio->polarity; 528 lookup->info.triggering = agpio->triggering; 529 } else { 530 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio); 531 lookup->info.polarity = lookup->active_low; 532 } 533 } 534 535 return 1; 536 } 537 538 static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup, 539 struct acpi_gpio_info *info) 540 { 541 struct acpi_device *adev = lookup->info.adev; 542 struct list_head res_list; 543 int ret; 544 545 INIT_LIST_HEAD(&res_list); 546 547 ret = acpi_dev_get_resources(adev, &res_list, 548 acpi_populate_gpio_lookup, 549 lookup); 550 if (ret < 0) 551 return ret; 552 553 acpi_dev_free_resource_list(&res_list); 554 555 if (!lookup->desc) 556 return -ENOENT; 557 558 if (info) 559 *info = lookup->info; 560 return 0; 561 } 562 563 static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, 564 const char *propname, int index, 565 struct acpi_gpio_lookup *lookup) 566 { 567 struct acpi_reference_args args; 568 unsigned int quirks = 0; 569 int ret; 570 571 memset(&args, 0, sizeof(args)); 572 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, 573 &args); 574 if (ret) { 575 struct acpi_device *adev = to_acpi_device_node(fwnode); 576 577 if (!adev) 578 return ret; 579 580 if (!acpi_get_driver_gpio_data(adev, propname, index, &args, 581 &quirks)) 582 return ret; 583 } 584 /* 585 * The property was found and resolved, so need to lookup the GPIO based 586 * on returned args. 587 */ 588 if (args.nargs != 3) 589 return -EPROTO; 590 591 lookup->index = args.args[0]; 592 lookup->pin_index = args.args[1]; 593 lookup->active_low = !!args.args[2]; 594 595 lookup->info.adev = args.adev; 596 lookup->info.quirks = quirks; 597 return 0; 598 } 599 600 /** 601 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources 602 * @adev: pointer to a ACPI device to get GPIO from 603 * @propname: Property name of the GPIO (optional) 604 * @index: index of GpioIo/GpioInt resource (starting from %0) 605 * @info: info pointer to fill in (optional) 606 * 607 * Function goes through ACPI resources for @adev and based on @index looks 608 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, 609 * and returns it. @index matches GpioIo/GpioInt resources only so if there 610 * are total %3 GPIO resources, the index goes from %0 to %2. 611 * 612 * If @propname is specified the GPIO is looked using device property. In 613 * that case @index is used to select the GPIO entry in the property value 614 * (in case of multiple). 615 * 616 * If the GPIO cannot be translated or there is an error an ERR_PTR is 617 * returned. 618 * 619 * Note: if the GPIO resource has multiple entries in the pin list, this 620 * function only returns the first. 621 */ 622 static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, 623 const char *propname, int index, 624 struct acpi_gpio_info *info) 625 { 626 struct acpi_gpio_lookup lookup; 627 int ret; 628 629 if (!adev) 630 return ERR_PTR(-ENODEV); 631 632 memset(&lookup, 0, sizeof(lookup)); 633 lookup.index = index; 634 635 if (propname) { 636 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname); 637 638 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), 639 propname, index, &lookup); 640 if (ret) 641 return ERR_PTR(ret); 642 643 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n", 644 dev_name(&lookup.info.adev->dev), lookup.index, 645 lookup.pin_index, lookup.active_low); 646 } else { 647 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index); 648 lookup.info.adev = adev; 649 } 650 651 ret = acpi_gpio_resource_lookup(&lookup, info); 652 return ret ? ERR_PTR(ret) : lookup.desc; 653 } 654 655 struct gpio_desc *acpi_find_gpio(struct device *dev, 656 const char *con_id, 657 unsigned int idx, 658 enum gpiod_flags *dflags, 659 enum gpio_lookup_flags *lookupflags) 660 { 661 struct acpi_device *adev = ACPI_COMPANION(dev); 662 struct acpi_gpio_info info; 663 struct gpio_desc *desc; 664 char propname[32]; 665 int i; 666 667 /* Try first from _DSD */ 668 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 669 if (con_id) { 670 snprintf(propname, sizeof(propname), "%s-%s", 671 con_id, gpio_suffixes[i]); 672 } else { 673 snprintf(propname, sizeof(propname), "%s", 674 gpio_suffixes[i]); 675 } 676 677 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); 678 if (!IS_ERR(desc)) 679 break; 680 if (PTR_ERR(desc) == -EPROBE_DEFER) 681 return ERR_CAST(desc); 682 } 683 684 /* Then from plain _CRS GPIOs */ 685 if (IS_ERR(desc)) { 686 if (!acpi_can_fallback_to_crs(adev, con_id)) 687 return ERR_PTR(-ENOENT); 688 689 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); 690 if (IS_ERR(desc)) 691 return desc; 692 } 693 694 if (info.gpioint && 695 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) { 696 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n"); 697 return ERR_PTR(-ENOENT); 698 } 699 700 if (info.polarity == GPIO_ACTIVE_LOW) 701 *lookupflags |= GPIO_ACTIVE_LOW; 702 703 acpi_gpio_update_gpiod_flags(dflags, &info); 704 return desc; 705 } 706 707 /** 708 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources 709 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from 710 * @propname: Property name of the GPIO 711 * @index: index of GpioIo/GpioInt resource (starting from %0) 712 * @info: info pointer to fill in (optional) 713 * 714 * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it. 715 * Otherwise (ie. it is a data-only non-device object), use the property-based 716 * GPIO lookup to get to the GPIO resource with the relevant information and use 717 * that to obtain the GPIO descriptor to return. 718 */ 719 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, 720 const char *propname, int index, 721 struct acpi_gpio_info *info) 722 { 723 struct acpi_gpio_lookup lookup; 724 struct acpi_device *adev; 725 int ret; 726 727 adev = to_acpi_device_node(fwnode); 728 if (adev) 729 return acpi_get_gpiod_by_index(adev, propname, index, info); 730 731 if (!is_acpi_data_node(fwnode)) 732 return ERR_PTR(-ENODEV); 733 734 if (!propname) 735 return ERR_PTR(-EINVAL); 736 737 memset(&lookup, 0, sizeof(lookup)); 738 lookup.index = index; 739 740 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup); 741 if (ret) 742 return ERR_PTR(ret); 743 744 ret = acpi_gpio_resource_lookup(&lookup, info); 745 return ret ? ERR_PTR(ret) : lookup.desc; 746 } 747 748 /** 749 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number 750 * @adev: pointer to a ACPI device to get IRQ from 751 * @index: index of GpioInt resource (starting from %0) 752 * 753 * If the device has one or more GpioInt resources, this function can be 754 * used to translate from the GPIO offset in the resource to the Linux IRQ 755 * number. 756 * 757 * The function is idempotent, though each time it runs it will configure GPIO 758 * pin direction according to the flags in GpioInt resource. 759 * 760 * Return: Linux IRQ number (> %0) on success, negative errno on failure. 761 */ 762 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index) 763 { 764 int idx, i; 765 unsigned int irq_flags; 766 int ret; 767 768 for (i = 0, idx = 0; idx <= index; i++) { 769 struct acpi_gpio_info info; 770 struct gpio_desc *desc; 771 772 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info); 773 774 /* Ignore -EPROBE_DEFER, it only matters if idx matches */ 775 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) 776 return PTR_ERR(desc); 777 778 if (info.gpioint && idx++ == index) { 779 char label[32]; 780 int irq; 781 782 if (IS_ERR(desc)) 783 return PTR_ERR(desc); 784 785 irq = gpiod_to_irq(desc); 786 if (irq < 0) 787 return irq; 788 789 snprintf(label, sizeof(label), "GpioInt() %d", index); 790 ret = gpiod_configure_flags(desc, label, 0, info.flags); 791 if (ret < 0) 792 return ret; 793 794 irq_flags = acpi_dev_get_irq_type(info.triggering, 795 info.polarity); 796 797 /* Set type if specified and different than the current one */ 798 if (irq_flags != IRQ_TYPE_NONE && 799 irq_flags != irq_get_trigger_type(irq)) 800 irq_set_irq_type(irq, irq_flags); 801 802 return irq; 803 } 804 805 } 806 return -ENOENT; 807 } 808 EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get); 809 810 static acpi_status 811 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, 812 u32 bits, u64 *value, void *handler_context, 813 void *region_context) 814 { 815 struct acpi_gpio_chip *achip = region_context; 816 struct gpio_chip *chip = achip->chip; 817 struct acpi_resource_gpio *agpio; 818 struct acpi_resource *ares; 819 int pin_index = (int)address; 820 acpi_status status; 821 int length; 822 int i; 823 824 status = acpi_buffer_to_resource(achip->conn_info.connection, 825 achip->conn_info.length, &ares); 826 if (ACPI_FAILURE(status)) 827 return status; 828 829 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) { 830 ACPI_FREE(ares); 831 return AE_BAD_PARAMETER; 832 } 833 834 agpio = &ares->data.gpio; 835 836 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT && 837 function == ACPI_WRITE)) { 838 ACPI_FREE(ares); 839 return AE_BAD_PARAMETER; 840 } 841 842 length = min(agpio->pin_table_length, (u16)(pin_index + bits)); 843 for (i = pin_index; i < length; ++i) { 844 int pin = agpio->pin_table[i]; 845 struct acpi_gpio_connection *conn; 846 struct gpio_desc *desc; 847 bool found; 848 849 mutex_lock(&achip->conn_lock); 850 851 found = false; 852 list_for_each_entry(conn, &achip->conns, node) { 853 if (conn->pin == pin) { 854 found = true; 855 desc = conn->desc; 856 break; 857 } 858 } 859 860 /* 861 * The same GPIO can be shared between operation region and 862 * event but only if the access here is ACPI_READ. In that 863 * case we "borrow" the event GPIO instead. 864 */ 865 if (!found && agpio->sharable == ACPI_SHARED && 866 function == ACPI_READ) { 867 struct acpi_gpio_event *event; 868 869 list_for_each_entry(event, &achip->events, node) { 870 if (event->pin == pin) { 871 desc = event->desc; 872 found = true; 873 break; 874 } 875 } 876 } 877 878 if (!found) { 879 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio); 880 const char *label = "ACPI:OpRegion"; 881 int err; 882 883 desc = gpiochip_request_own_desc(chip, pin, label); 884 if (IS_ERR(desc)) { 885 status = AE_ERROR; 886 mutex_unlock(&achip->conn_lock); 887 goto out; 888 } 889 890 err = gpiod_configure_flags(desc, label, 0, flags); 891 if (err < 0) { 892 status = AE_NOT_CONFIGURED; 893 gpiochip_free_own_desc(desc); 894 mutex_unlock(&achip->conn_lock); 895 goto out; 896 } 897 898 conn = kzalloc(sizeof(*conn), GFP_KERNEL); 899 if (!conn) { 900 status = AE_NO_MEMORY; 901 gpiochip_free_own_desc(desc); 902 mutex_unlock(&achip->conn_lock); 903 goto out; 904 } 905 906 conn->pin = pin; 907 conn->desc = desc; 908 list_add_tail(&conn->node, &achip->conns); 909 } 910 911 mutex_unlock(&achip->conn_lock); 912 913 if (function == ACPI_WRITE) 914 gpiod_set_raw_value_cansleep(desc, 915 !!((1 << i) & *value)); 916 else 917 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i; 918 } 919 920 out: 921 ACPI_FREE(ares); 922 return status; 923 } 924 925 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip) 926 { 927 struct gpio_chip *chip = achip->chip; 928 acpi_handle handle = ACPI_HANDLE(chip->parent); 929 acpi_status status; 930 931 INIT_LIST_HEAD(&achip->conns); 932 mutex_init(&achip->conn_lock); 933 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, 934 acpi_gpio_adr_space_handler, 935 NULL, achip); 936 if (ACPI_FAILURE(status)) 937 dev_err(chip->parent, 938 "Failed to install GPIO OpRegion handler\n"); 939 } 940 941 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip) 942 { 943 struct gpio_chip *chip = achip->chip; 944 acpi_handle handle = ACPI_HANDLE(chip->parent); 945 struct acpi_gpio_connection *conn, *tmp; 946 acpi_status status; 947 948 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, 949 acpi_gpio_adr_space_handler); 950 if (ACPI_FAILURE(status)) { 951 dev_err(chip->parent, 952 "Failed to remove GPIO OpRegion handler\n"); 953 return; 954 } 955 956 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) { 957 gpiochip_free_own_desc(conn->desc); 958 list_del(&conn->node); 959 kfree(conn); 960 } 961 } 962 963 static struct gpio_desc *acpi_gpiochip_parse_own_gpio( 964 struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode, 965 const char **name, unsigned int *lflags, unsigned int *dflags) 966 { 967 struct gpio_chip *chip = achip->chip; 968 struct gpio_desc *desc; 969 u32 gpios[2]; 970 int ret; 971 972 *lflags = 0; 973 *dflags = 0; 974 *name = NULL; 975 976 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, 977 ARRAY_SIZE(gpios)); 978 if (ret < 0) 979 return ERR_PTR(ret); 980 981 desc = gpiochip_get_desc(chip, gpios[0]); 982 if (IS_ERR(desc)) 983 return desc; 984 985 if (gpios[1]) 986 *lflags |= GPIO_ACTIVE_LOW; 987 988 if (fwnode_property_present(fwnode, "input")) 989 *dflags |= GPIOD_IN; 990 else if (fwnode_property_present(fwnode, "output-low")) 991 *dflags |= GPIOD_OUT_LOW; 992 else if (fwnode_property_present(fwnode, "output-high")) 993 *dflags |= GPIOD_OUT_HIGH; 994 else 995 return ERR_PTR(-EINVAL); 996 997 fwnode_property_read_string(fwnode, "line-name", name); 998 999 return desc; 1000 } 1001 1002 static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip) 1003 { 1004 struct gpio_chip *chip = achip->chip; 1005 struct fwnode_handle *fwnode; 1006 1007 device_for_each_child_node(chip->parent, fwnode) { 1008 unsigned int lflags, dflags; 1009 struct gpio_desc *desc; 1010 const char *name; 1011 int ret; 1012 1013 if (!fwnode_property_present(fwnode, "gpio-hog")) 1014 continue; 1015 1016 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name, 1017 &lflags, &dflags); 1018 if (IS_ERR(desc)) 1019 continue; 1020 1021 ret = gpiod_hog(desc, name, lflags, dflags); 1022 if (ret) { 1023 dev_err(chip->parent, "Failed to hog GPIO\n"); 1024 fwnode_handle_put(fwnode); 1025 return; 1026 } 1027 } 1028 } 1029 1030 void acpi_gpiochip_add(struct gpio_chip *chip) 1031 { 1032 struct acpi_gpio_chip *acpi_gpio; 1033 acpi_handle handle; 1034 acpi_status status; 1035 1036 if (!chip || !chip->parent) 1037 return; 1038 1039 handle = ACPI_HANDLE(chip->parent); 1040 if (!handle) 1041 return; 1042 1043 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL); 1044 if (!acpi_gpio) { 1045 dev_err(chip->parent, 1046 "Failed to allocate memory for ACPI GPIO chip\n"); 1047 return; 1048 } 1049 1050 acpi_gpio->chip = chip; 1051 INIT_LIST_HEAD(&acpi_gpio->events); 1052 1053 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio); 1054 if (ACPI_FAILURE(status)) { 1055 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n"); 1056 kfree(acpi_gpio); 1057 return; 1058 } 1059 1060 if (!chip->names) 1061 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent)); 1062 1063 acpi_gpiochip_request_regions(acpi_gpio); 1064 acpi_gpiochip_scan_gpios(acpi_gpio); 1065 acpi_walk_dep_device_list(handle); 1066 } 1067 1068 void acpi_gpiochip_remove(struct gpio_chip *chip) 1069 { 1070 struct acpi_gpio_chip *acpi_gpio; 1071 acpi_handle handle; 1072 acpi_status status; 1073 1074 if (!chip || !chip->parent) 1075 return; 1076 1077 handle = ACPI_HANDLE(chip->parent); 1078 if (!handle) 1079 return; 1080 1081 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); 1082 if (ACPI_FAILURE(status)) { 1083 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n"); 1084 return; 1085 } 1086 1087 acpi_gpiochip_free_regions(acpi_gpio); 1088 1089 acpi_detach_data(handle, acpi_gpio_chip_dh); 1090 kfree(acpi_gpio); 1091 } 1092 1093 static int acpi_gpio_package_count(const union acpi_object *obj) 1094 { 1095 const union acpi_object *element = obj->package.elements; 1096 const union acpi_object *end = element + obj->package.count; 1097 unsigned int count = 0; 1098 1099 while (element < end) { 1100 switch (element->type) { 1101 case ACPI_TYPE_LOCAL_REFERENCE: 1102 element += 3; 1103 /* Fallthrough */ 1104 case ACPI_TYPE_INTEGER: 1105 element++; 1106 count++; 1107 break; 1108 1109 default: 1110 return -EPROTO; 1111 } 1112 } 1113 1114 return count; 1115 } 1116 1117 static int acpi_find_gpio_count(struct acpi_resource *ares, void *data) 1118 { 1119 unsigned int *count = data; 1120 1121 if (ares->type == ACPI_RESOURCE_TYPE_GPIO) 1122 *count += ares->data.gpio.pin_table_length; 1123 1124 return 1; 1125 } 1126 1127 /** 1128 * acpi_gpio_count - return the number of GPIOs associated with a 1129 * device / function or -ENOENT if no GPIO has been 1130 * assigned to the requested function. 1131 * @dev: GPIO consumer, can be NULL for system-global GPIOs 1132 * @con_id: function within the GPIO consumer 1133 */ 1134 int acpi_gpio_count(struct device *dev, const char *con_id) 1135 { 1136 struct acpi_device *adev = ACPI_COMPANION(dev); 1137 const union acpi_object *obj; 1138 const struct acpi_gpio_mapping *gm; 1139 int count = -ENOENT; 1140 int ret; 1141 char propname[32]; 1142 unsigned int i; 1143 1144 /* Try first from _DSD */ 1145 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 1146 if (con_id) 1147 snprintf(propname, sizeof(propname), "%s-%s", 1148 con_id, gpio_suffixes[i]); 1149 else 1150 snprintf(propname, sizeof(propname), "%s", 1151 gpio_suffixes[i]); 1152 1153 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY, 1154 &obj); 1155 if (ret == 0) { 1156 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) 1157 count = 1; 1158 else if (obj->type == ACPI_TYPE_PACKAGE) 1159 count = acpi_gpio_package_count(obj); 1160 } else if (adev->driver_gpios) { 1161 for (gm = adev->driver_gpios; gm->name; gm++) 1162 if (strcmp(propname, gm->name) == 0) { 1163 count = gm->size; 1164 break; 1165 } 1166 } 1167 if (count > 0) 1168 break; 1169 } 1170 1171 /* Then from plain _CRS GPIOs */ 1172 if (count < 0) { 1173 struct list_head resource_list; 1174 unsigned int crs_count = 0; 1175 1176 if (!acpi_can_fallback_to_crs(adev, con_id)) 1177 return count; 1178 1179 INIT_LIST_HEAD(&resource_list); 1180 acpi_dev_get_resources(adev, &resource_list, 1181 acpi_find_gpio_count, &crs_count); 1182 acpi_dev_free_resource_list(&resource_list); 1183 if (crs_count > 0) 1184 count = crs_count; 1185 } 1186 return count ? count : -ENOENT; 1187 } 1188 1189 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id) 1190 { 1191 /* Never allow fallback if the device has properties */ 1192 if (adev->data.properties || adev->driver_gpios) 1193 return false; 1194 1195 return con_id == NULL; 1196 } 1197 1198 /* Sync the initial state of handlers after all builtin drivers have probed */ 1199 static int acpi_gpio_initial_sync(void) 1200 { 1201 struct acpi_gpio_event *event, *ep; 1202 1203 mutex_lock(&acpi_gpio_initial_sync_list_lock); 1204 list_for_each_entry_safe(event, ep, &acpi_gpio_initial_sync_list, 1205 initial_sync_list) { 1206 acpi_evaluate_object(event->handle, NULL, NULL, NULL); 1207 list_del_init(&event->initial_sync_list); 1208 } 1209 mutex_unlock(&acpi_gpio_initial_sync_list_lock); 1210 1211 return 0; 1212 } 1213 /* We must use _sync so that this runs after the first deferred_probe run */ 1214 late_initcall_sync(acpi_gpio_initial_sync); 1215