gpiolib-acpi.c 7.9 KB

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