gpiolib-acpi.c 7.8 KB

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