gpiolib-acpi.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include <linux/errno.h>
  13. #include <linux/gpio.h>
  14. #include <linux/export.h>
  15. #include <linux/acpi_gpio.h>
  16. #include <linux/acpi.h>
  17. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  18. {
  19. if (!gc->dev)
  20. return false;
  21. return ACPI_HANDLE(gc->dev) == data;
  22. }
  23. /**
  24. * acpi_get_gpio() - Translate ACPI GPIO pin to GPIO number usable with GPIO API
  25. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  26. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  27. *
  28. * Returns GPIO number to use with Linux generic GPIO API, or errno error value
  29. */
  30. int acpi_get_gpio(char *path, int pin)
  31. {
  32. struct gpio_chip *chip;
  33. acpi_handle handle;
  34. acpi_status status;
  35. status = acpi_get_handle(NULL, path, &handle);
  36. if (ACPI_FAILURE(status))
  37. return -ENODEV;
  38. chip = gpiochip_find(handle, acpi_gpiochip_find);
  39. if (!chip)
  40. return -ENODEV;
  41. if (!gpio_is_valid(chip->base + pin))
  42. return -EINVAL;
  43. return chip->base + pin;
  44. }
  45. EXPORT_SYMBOL_GPL(acpi_get_gpio);