gpiolib-acpi.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include <linux/interrupt.h>
  18. struct acpi_gpio_evt_pin {
  19. struct list_head node;
  20. acpi_handle *evt_handle;
  21. unsigned int pin;
  22. unsigned int irq;
  23. };
  24. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  25. {
  26. if (!gc->dev)
  27. return false;
  28. return ACPI_HANDLE(gc->dev) == data;
  29. }
  30. /**
  31. * acpi_get_gpio() - Translate ACPI GPIO pin to GPIO number usable with GPIO API
  32. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  33. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  34. *
  35. * Returns GPIO number to use with Linux generic GPIO API, or errno error value
  36. */
  37. int acpi_get_gpio(char *path, int pin)
  38. {
  39. struct gpio_chip *chip;
  40. acpi_handle handle;
  41. acpi_status status;
  42. status = acpi_get_handle(NULL, path, &handle);
  43. if (ACPI_FAILURE(status))
  44. return -ENODEV;
  45. chip = gpiochip_find(handle, acpi_gpiochip_find);
  46. if (!chip)
  47. return -ENODEV;
  48. if (!gpio_is_valid(chip->base + pin))
  49. return -EINVAL;
  50. return chip->base + pin;
  51. }
  52. EXPORT_SYMBOL_GPL(acpi_get_gpio);
  53. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  54. {
  55. acpi_handle handle = data;
  56. acpi_evaluate_object(handle, NULL, NULL, NULL);
  57. return IRQ_HANDLED;
  58. }
  59. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  60. {
  61. struct acpi_gpio_evt_pin *evt_pin = data;
  62. acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin);
  63. return IRQ_HANDLED;
  64. }
  65. static void acpi_gpio_evt_dh(acpi_handle handle, void *data)
  66. {
  67. /* The address of this function is used as a key. */
  68. }
  69. /**
  70. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  71. * @chip: gpio chip
  72. *
  73. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  74. * handled by ACPI event methods which need to be called from the GPIO
  75. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  76. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  77. * the acpi event methods for those pins.
  78. */
  79. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  80. {
  81. struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
  82. struct acpi_resource *res;
  83. acpi_handle handle, evt_handle;
  84. struct list_head *evt_pins = NULL;
  85. acpi_status status;
  86. unsigned int pin;
  87. int irq, ret;
  88. char ev_name[5];
  89. if (!chip->dev || !chip->to_irq)
  90. return;
  91. handle = ACPI_HANDLE(chip->dev);
  92. if (!handle)
  93. return;
  94. status = acpi_get_event_resources(handle, &buf);
  95. if (ACPI_FAILURE(status))
  96. return;
  97. status = acpi_get_handle(handle, "_EVT", &evt_handle);
  98. if (ACPI_SUCCESS(status)) {
  99. evt_pins = kzalloc(sizeof(*evt_pins), GFP_KERNEL);
  100. if (evt_pins) {
  101. INIT_LIST_HEAD(evt_pins);
  102. status = acpi_attach_data(handle, acpi_gpio_evt_dh,
  103. evt_pins);
  104. if (ACPI_FAILURE(status)) {
  105. kfree(evt_pins);
  106. evt_pins = NULL;
  107. }
  108. }
  109. }
  110. /*
  111. * If a GPIO interrupt has an ACPI event handler method, or _EVT is
  112. * present, set up an interrupt handler that calls the ACPI event
  113. * handler.
  114. */
  115. for (res = buf.pointer;
  116. res && (res->type != ACPI_RESOURCE_TYPE_END_TAG);
  117. res = ACPI_NEXT_RESOURCE(res)) {
  118. irq_handler_t handler = NULL;
  119. void *data;
  120. if (res->type != ACPI_RESOURCE_TYPE_GPIO ||
  121. res->data.gpio.connection_type !=
  122. ACPI_RESOURCE_GPIO_TYPE_INT)
  123. continue;
  124. pin = res->data.gpio.pin_table[0];
  125. if (pin > chip->ngpio)
  126. continue;
  127. irq = chip->to_irq(chip, pin);
  128. if (irq < 0)
  129. continue;
  130. if (pin <= 255) {
  131. acpi_handle ev_handle;
  132. sprintf(ev_name, "_%c%02X",
  133. res->data.gpio.triggering ? 'E' : 'L', pin);
  134. status = acpi_get_handle(handle, ev_name, &ev_handle);
  135. if (ACPI_SUCCESS(status)) {
  136. handler = acpi_gpio_irq_handler;
  137. data = ev_handle;
  138. }
  139. }
  140. if (!handler && evt_pins) {
  141. struct acpi_gpio_evt_pin *evt_pin;
  142. evt_pin = kzalloc(sizeof(*evt_pin), GFP_KERNEL);
  143. if (!evt_pin)
  144. continue;
  145. list_add_tail(&evt_pin->node, evt_pins);
  146. evt_pin->evt_handle = evt_handle;
  147. evt_pin->pin = pin;
  148. evt_pin->irq = irq;
  149. handler = acpi_gpio_irq_handler_evt;
  150. data = evt_pin;
  151. }
  152. if (!handler)
  153. continue;
  154. /* Assume BIOS sets the triggering, so no flags */
  155. ret = devm_request_threaded_irq(chip->dev, irq, NULL, handler,
  156. 0, "GPIO-signaled-ACPI-event",
  157. data);
  158. if (ret)
  159. dev_err(chip->dev,
  160. "Failed to request IRQ %d ACPI event handler\n",
  161. irq);
  162. }
  163. }
  164. EXPORT_SYMBOL(acpi_gpiochip_request_interrupts);
  165. /**
  166. * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
  167. * @chip: gpio chip
  168. *
  169. * Free interrupts associated with the _EVT method for the given GPIO chip.
  170. *
  171. * The remaining ACPI event interrupts associated with the chip are freed
  172. * automatically.
  173. */
  174. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  175. {
  176. acpi_handle handle;
  177. acpi_status status;
  178. struct list_head *evt_pins;
  179. struct acpi_gpio_evt_pin *evt_pin, *ep;
  180. if (!chip->dev || !chip->to_irq)
  181. return;
  182. handle = ACPI_HANDLE(chip->dev);
  183. if (!handle)
  184. return;
  185. status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
  186. if (ACPI_FAILURE(status))
  187. return;
  188. list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
  189. devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
  190. list_del(&evt_pin->node);
  191. kfree(evt_pin);
  192. }
  193. acpi_detach_data(handle, acpi_gpio_evt_dh);
  194. kfree(evt_pins);
  195. }
  196. EXPORT_SYMBOL(acpi_gpiochip_free_interrupts);
  197. struct acpi_gpio_lookup {
  198. struct acpi_gpio_info info;
  199. int index;
  200. int gpio;
  201. int n;
  202. };
  203. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  204. {
  205. struct acpi_gpio_lookup *lookup = data;
  206. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  207. return 1;
  208. if (lookup->n++ == lookup->index && lookup->gpio < 0) {
  209. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  210. lookup->gpio = acpi_get_gpio(agpio->resource_source.string_ptr,
  211. agpio->pin_table[0]);
  212. lookup->info.gpioint =
  213. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  214. }
  215. return 1;
  216. }
  217. /**
  218. * acpi_get_gpio_by_index() - get a GPIO number from device resources
  219. * @dev: pointer to a device to get GPIO from
  220. * @index: index of GpioIo/GpioInt resource (starting from %0)
  221. * @info: info pointer to fill in (optional)
  222. *
  223. * Function goes through ACPI resources for @dev and based on @index looks
  224. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO number,
  225. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  226. * are total %3 GPIO resources, the index goes from %0 to %2.
  227. *
  228. * If the GPIO cannot be translated or there is an error, negative errno is
  229. * returned.
  230. *
  231. * Note: if the GPIO resource has multiple entries in the pin list, this
  232. * function only returns the first.
  233. */
  234. int acpi_get_gpio_by_index(struct device *dev, int index,
  235. struct acpi_gpio_info *info)
  236. {
  237. struct acpi_gpio_lookup lookup;
  238. struct list_head resource_list;
  239. struct acpi_device *adev;
  240. acpi_handle handle;
  241. int ret;
  242. if (!dev)
  243. return -EINVAL;
  244. handle = ACPI_HANDLE(dev);
  245. if (!handle || acpi_bus_get_device(handle, &adev))
  246. return -ENODEV;
  247. memset(&lookup, 0, sizeof(lookup));
  248. lookup.index = index;
  249. lookup.gpio = -ENODEV;
  250. INIT_LIST_HEAD(&resource_list);
  251. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  252. &lookup);
  253. if (ret < 0)
  254. return ret;
  255. acpi_dev_free_resource_list(&resource_list);
  256. if (lookup.gpio >= 0 && info)
  257. *info = lookup.info;
  258. return lookup.gpio;
  259. }
  260. EXPORT_SYMBOL_GPL(acpi_get_gpio_by_index);