Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: acpi: Factor out acpi_gpio_to_gpiod_flags() helper

The helper function acpi_gpio_to_gpiod_flags() will be used later to configure
pin properly whenever it's requested.

While here, introduce a checking error code returned by gpiod_configure_flags()
and bail out if it's not okay.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Andy Shevchenko and committed by
Linus Walleij
2eca25af ed7fcf1e

+36 -25
+36 -25
drivers/gpio/gpiolib-acpi.c
··· 423 423 return false; 424 424 } 425 425 426 + static enum gpiod_flags 427 + acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio) 428 + { 429 + bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; 430 + 431 + switch (agpio->io_restriction) { 432 + case ACPI_IO_RESTRICT_INPUT: 433 + return GPIOD_IN; 434 + case ACPI_IO_RESTRICT_OUTPUT: 435 + /* 436 + * ACPI GPIO resources don't contain an initial value for the 437 + * GPIO. Therefore we deduce that value from the pull field 438 + * instead. If the pin is pulled up we assume default to be 439 + * high, otherwise low. 440 + */ 441 + return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 442 + default: 443 + /* 444 + * Assume that the BIOS has configured the direction and pull 445 + * accordingly. 446 + */ 447 + return GPIOD_ASIS; 448 + } 449 + } 450 + 426 451 struct acpi_gpio_lookup { 427 452 struct acpi_gpio_info info; 428 453 int index; ··· 765 740 struct acpi_resource *ares; 766 741 int pin_index = (int)address; 767 742 acpi_status status; 768 - bool pull_up; 769 743 int length; 770 744 int i; 771 745 ··· 779 755 } 780 756 781 757 agpio = &ares->data.gpio; 782 - pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; 783 758 784 759 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT && 785 760 function == ACPI_WRITE)) { ··· 829 806 } 830 807 831 808 if (!found) { 832 - desc = gpiochip_request_own_desc(chip, pin, 833 - "ACPI:OpRegion"); 809 + enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio); 810 + const char *label = "ACPI:OpRegion"; 811 + int err; 812 + 813 + desc = gpiochip_request_own_desc(chip, pin, label); 834 814 if (IS_ERR(desc)) { 835 815 status = AE_ERROR; 836 816 mutex_unlock(&achip->conn_lock); 837 817 goto out; 838 818 } 839 819 840 - switch (agpio->io_restriction) { 841 - case ACPI_IO_RESTRICT_INPUT: 842 - gpiod_direction_input(desc); 843 - break; 844 - case ACPI_IO_RESTRICT_OUTPUT: 845 - /* 846 - * ACPI GPIO resources don't contain an 847 - * initial value for the GPIO. Therefore we 848 - * deduce that value from the pull field 849 - * instead. If the pin is pulled up we 850 - * assume default to be high, otherwise 851 - * low. 852 - */ 853 - gpiod_direction_output(desc, pull_up); 854 - break; 855 - default: 856 - /* 857 - * Assume that the BIOS has configured the 858 - * direction and pull accordingly. 859 - */ 860 - break; 820 + err = gpiod_configure_flags(desc, label, 0, flags); 821 + if (err < 0) { 822 + status = AE_NOT_CONFIGURED; 823 + gpiochip_free_own_desc(desc); 824 + mutex_unlock(&achip->conn_lock); 825 + goto out; 861 826 } 862 827 863 828 conn = kzalloc(sizeof(*conn), GFP_KERNEL);