gpiolib-acpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 <linux/mutex.h>
  19. #include "gpiolib.h"
  20. struct acpi_gpio_event {
  21. struct list_head node;
  22. acpi_handle handle;
  23. unsigned int pin;
  24. unsigned int irq;
  25. };
  26. struct acpi_gpio_connection {
  27. struct list_head node;
  28. struct gpio_desc *desc;
  29. };
  30. struct acpi_gpio_chip {
  31. /*
  32. * ACPICA requires that the first field of the context parameter
  33. * passed to acpi_install_address_space_handler() is large enough
  34. * to hold struct acpi_connection_info.
  35. */
  36. struct acpi_connection_info conn_info;
  37. struct list_head conns;
  38. struct mutex conn_lock;
  39. struct gpio_chip *chip;
  40. struct list_head events;
  41. };
  42. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  43. {
  44. if (!gc->dev)
  45. return false;
  46. return ACPI_HANDLE(gc->dev) == data;
  47. }
  48. /**
  49. * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
  50. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  51. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  52. *
  53. * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  54. * error value
  55. */
  56. static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  57. {
  58. struct gpio_chip *chip;
  59. acpi_handle handle;
  60. acpi_status status;
  61. status = acpi_get_handle(NULL, path, &handle);
  62. if (ACPI_FAILURE(status))
  63. return ERR_PTR(-ENODEV);
  64. chip = gpiochip_find(handle, acpi_gpiochip_find);
  65. if (!chip)
  66. return ERR_PTR(-ENODEV);
  67. if (pin < 0 || pin > chip->ngpio)
  68. return ERR_PTR(-EINVAL);
  69. return gpiochip_get_desc(chip, pin);
  70. }
  71. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  72. {
  73. struct acpi_gpio_event *event = data;
  74. acpi_evaluate_object(event->handle, NULL, NULL, NULL);
  75. return IRQ_HANDLED;
  76. }
  77. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  78. {
  79. struct acpi_gpio_event *event = data;
  80. acpi_execute_simple_method(event->handle, NULL, event->pin);
  81. return IRQ_HANDLED;
  82. }
  83. static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
  84. {
  85. /* The address of this function is used as a key. */
  86. }
  87. static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
  88. void *context)
  89. {
  90. struct acpi_gpio_chip *acpi_gpio = context;
  91. struct gpio_chip *chip = acpi_gpio->chip;
  92. struct acpi_resource_gpio *agpio;
  93. acpi_handle handle, evt_handle;
  94. struct acpi_gpio_event *event;
  95. irq_handler_t handler = NULL;
  96. struct gpio_desc *desc;
  97. unsigned long irqflags;
  98. int ret, pin, irq;
  99. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  100. return AE_OK;
  101. agpio = &ares->data.gpio;
  102. if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
  103. return AE_OK;
  104. handle = ACPI_HANDLE(chip->dev);
  105. pin = agpio->pin_table[0];
  106. if (pin <= 255) {
  107. char ev_name[5];
  108. sprintf(ev_name, "_%c%02X",
  109. agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
  110. pin);
  111. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  112. handler = acpi_gpio_irq_handler;
  113. }
  114. if (!handler) {
  115. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  116. handler = acpi_gpio_irq_handler_evt;
  117. }
  118. if (!handler)
  119. return AE_BAD_PARAMETER;
  120. desc = gpiochip_get_desc(chip, pin);
  121. if (IS_ERR(desc)) {
  122. dev_err(chip->dev, "Failed to get GPIO descriptor\n");
  123. return AE_ERROR;
  124. }
  125. ret = gpiochip_request_own_desc(desc, "ACPI:Event");
  126. if (ret) {
  127. dev_err(chip->dev, "Failed to request GPIO\n");
  128. return AE_ERROR;
  129. }
  130. gpiod_direction_input(desc);
  131. ret = gpiod_lock_as_irq(desc);
  132. if (ret) {
  133. dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
  134. goto fail_free_desc;
  135. }
  136. irq = gpiod_to_irq(desc);
  137. if (irq < 0) {
  138. dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
  139. goto fail_unlock_irq;
  140. }
  141. irqflags = IRQF_ONESHOT;
  142. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  143. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  144. irqflags |= IRQF_TRIGGER_HIGH;
  145. else
  146. irqflags |= IRQF_TRIGGER_LOW;
  147. } else {
  148. switch (agpio->polarity) {
  149. case ACPI_ACTIVE_HIGH:
  150. irqflags |= IRQF_TRIGGER_RISING;
  151. break;
  152. case ACPI_ACTIVE_LOW:
  153. irqflags |= IRQF_TRIGGER_FALLING;
  154. break;
  155. default:
  156. irqflags |= IRQF_TRIGGER_RISING |
  157. IRQF_TRIGGER_FALLING;
  158. break;
  159. }
  160. }
  161. event = kzalloc(sizeof(*event), GFP_KERNEL);
  162. if (!event)
  163. goto fail_unlock_irq;
  164. event->handle = evt_handle;
  165. event->irq = irq;
  166. event->pin = pin;
  167. ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
  168. "ACPI:Event", event);
  169. if (ret) {
  170. dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
  171. event->irq);
  172. goto fail_free_event;
  173. }
  174. list_add_tail(&event->node, &acpi_gpio->events);
  175. return AE_OK;
  176. fail_free_event:
  177. kfree(event);
  178. fail_unlock_irq:
  179. gpiod_unlock_as_irq(desc);
  180. fail_free_desc:
  181. gpiochip_free_own_desc(desc);
  182. return AE_ERROR;
  183. }
  184. /**
  185. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  186. * @acpi_gpio: ACPI GPIO chip
  187. *
  188. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  189. * handled by ACPI event methods which need to be called from the GPIO
  190. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  191. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  192. * the acpi event methods for those pins.
  193. */
  194. static void acpi_gpiochip_request_interrupts(struct acpi_gpio_chip *acpi_gpio)
  195. {
  196. struct gpio_chip *chip = acpi_gpio->chip;
  197. if (!chip->to_irq)
  198. return;
  199. INIT_LIST_HEAD(&acpi_gpio->events);
  200. acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
  201. acpi_gpiochip_request_interrupt, acpi_gpio);
  202. }
  203. /**
  204. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  205. * @acpi_gpio: ACPI GPIO chip
  206. *
  207. * Free interrupts associated with GPIO ACPI event method for the given
  208. * GPIO chip.
  209. */
  210. static void acpi_gpiochip_free_interrupts(struct acpi_gpio_chip *acpi_gpio)
  211. {
  212. struct acpi_gpio_event *event, *ep;
  213. struct gpio_chip *chip = acpi_gpio->chip;
  214. if (!chip->to_irq)
  215. return;
  216. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  217. struct gpio_desc *desc;
  218. free_irq(event->irq, event);
  219. desc = gpiochip_get_desc(chip, event->pin);
  220. if (WARN_ON(IS_ERR(desc)))
  221. continue;
  222. gpiod_unlock_as_irq(desc);
  223. gpiochip_free_own_desc(desc);
  224. list_del(&event->node);
  225. kfree(event);
  226. }
  227. }
  228. struct acpi_gpio_lookup {
  229. struct acpi_gpio_info info;
  230. int index;
  231. struct gpio_desc *desc;
  232. int n;
  233. };
  234. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  235. {
  236. struct acpi_gpio_lookup *lookup = data;
  237. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  238. return 1;
  239. if (lookup->n++ == lookup->index && !lookup->desc) {
  240. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  241. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  242. agpio->pin_table[0]);
  243. lookup->info.gpioint =
  244. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  245. lookup->info.active_low =
  246. agpio->polarity == ACPI_ACTIVE_LOW;
  247. }
  248. return 1;
  249. }
  250. /**
  251. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  252. * @dev: pointer to a device to get GPIO from
  253. * @index: index of GpioIo/GpioInt resource (starting from %0)
  254. * @info: info pointer to fill in (optional)
  255. *
  256. * Function goes through ACPI resources for @dev and based on @index looks
  257. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  258. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  259. * are total %3 GPIO resources, the index goes from %0 to %2.
  260. *
  261. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  262. * returned.
  263. *
  264. * Note: if the GPIO resource has multiple entries in the pin list, this
  265. * function only returns the first.
  266. */
  267. struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
  268. struct acpi_gpio_info *info)
  269. {
  270. struct acpi_gpio_lookup lookup;
  271. struct list_head resource_list;
  272. struct acpi_device *adev;
  273. acpi_handle handle;
  274. int ret;
  275. if (!dev)
  276. return ERR_PTR(-EINVAL);
  277. handle = ACPI_HANDLE(dev);
  278. if (!handle || acpi_bus_get_device(handle, &adev))
  279. return ERR_PTR(-ENODEV);
  280. memset(&lookup, 0, sizeof(lookup));
  281. lookup.index = index;
  282. INIT_LIST_HEAD(&resource_list);
  283. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  284. &lookup);
  285. if (ret < 0)
  286. return ERR_PTR(ret);
  287. acpi_dev_free_resource_list(&resource_list);
  288. if (lookup.desc && info)
  289. *info = lookup.info;
  290. return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
  291. }
  292. static acpi_status
  293. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  294. u32 bits, u64 *value, void *handler_context,
  295. void *region_context)
  296. {
  297. struct acpi_gpio_chip *achip = region_context;
  298. struct gpio_chip *chip = achip->chip;
  299. struct acpi_resource_gpio *agpio;
  300. struct acpi_resource *ares;
  301. acpi_status status;
  302. bool pull_up;
  303. int i;
  304. status = acpi_buffer_to_resource(achip->conn_info.connection,
  305. achip->conn_info.length, &ares);
  306. if (ACPI_FAILURE(status))
  307. return status;
  308. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  309. ACPI_FREE(ares);
  310. return AE_BAD_PARAMETER;
  311. }
  312. agpio = &ares->data.gpio;
  313. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  314. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  315. function == ACPI_WRITE)) {
  316. ACPI_FREE(ares);
  317. return AE_BAD_PARAMETER;
  318. }
  319. for (i = 0; i < agpio->pin_table_length; i++) {
  320. unsigned pin = agpio->pin_table[i];
  321. struct acpi_gpio_connection *conn;
  322. struct gpio_desc *desc;
  323. bool found;
  324. desc = gpiochip_get_desc(chip, pin);
  325. if (IS_ERR(desc)) {
  326. status = AE_ERROR;
  327. goto out;
  328. }
  329. mutex_lock(&achip->conn_lock);
  330. found = false;
  331. list_for_each_entry(conn, &achip->conns, node) {
  332. if (conn->desc == desc) {
  333. found = true;
  334. break;
  335. }
  336. }
  337. if (!found) {
  338. int ret;
  339. ret = gpiochip_request_own_desc(desc, "ACPI:OpRegion");
  340. if (ret) {
  341. status = AE_ERROR;
  342. mutex_unlock(&achip->conn_lock);
  343. goto out;
  344. }
  345. switch (agpio->io_restriction) {
  346. case ACPI_IO_RESTRICT_INPUT:
  347. gpiod_direction_input(desc);
  348. break;
  349. case ACPI_IO_RESTRICT_OUTPUT:
  350. /*
  351. * ACPI GPIO resources don't contain an
  352. * initial value for the GPIO. Therefore we
  353. * deduce that value from the pull field
  354. * instead. If the pin is pulled up we
  355. * assume default to be high, otherwise
  356. * low.
  357. */
  358. gpiod_direction_output(desc, pull_up);
  359. break;
  360. default:
  361. /*
  362. * Assume that the BIOS has configured the
  363. * direction and pull accordingly.
  364. */
  365. break;
  366. }
  367. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  368. if (!conn) {
  369. status = AE_NO_MEMORY;
  370. gpiochip_free_own_desc(desc);
  371. mutex_unlock(&achip->conn_lock);
  372. goto out;
  373. }
  374. conn->desc = desc;
  375. list_add_tail(&conn->node, &achip->conns);
  376. }
  377. mutex_unlock(&achip->conn_lock);
  378. if (function == ACPI_WRITE)
  379. gpiod_set_raw_value_cansleep(desc,
  380. !!((1 << i) & *value));
  381. else
  382. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  383. }
  384. out:
  385. ACPI_FREE(ares);
  386. return status;
  387. }
  388. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  389. {
  390. struct gpio_chip *chip = achip->chip;
  391. acpi_handle handle = ACPI_HANDLE(chip->dev);
  392. acpi_status status;
  393. INIT_LIST_HEAD(&achip->conns);
  394. mutex_init(&achip->conn_lock);
  395. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  396. acpi_gpio_adr_space_handler,
  397. NULL, achip);
  398. if (ACPI_FAILURE(status))
  399. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  400. }
  401. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  402. {
  403. struct gpio_chip *chip = achip->chip;
  404. acpi_handle handle = ACPI_HANDLE(chip->dev);
  405. struct acpi_gpio_connection *conn, *tmp;
  406. acpi_status status;
  407. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  408. acpi_gpio_adr_space_handler);
  409. if (ACPI_FAILURE(status)) {
  410. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  411. return;
  412. }
  413. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  414. gpiochip_free_own_desc(conn->desc);
  415. list_del(&conn->node);
  416. kfree(conn);
  417. }
  418. }
  419. void acpi_gpiochip_add(struct gpio_chip *chip)
  420. {
  421. struct acpi_gpio_chip *acpi_gpio;
  422. acpi_handle handle;
  423. acpi_status status;
  424. if (!chip || !chip->dev)
  425. return;
  426. handle = ACPI_HANDLE(chip->dev);
  427. if (!handle)
  428. return;
  429. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  430. if (!acpi_gpio) {
  431. dev_err(chip->dev,
  432. "Failed to allocate memory for ACPI GPIO chip\n");
  433. return;
  434. }
  435. acpi_gpio->chip = chip;
  436. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  437. if (ACPI_FAILURE(status)) {
  438. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  439. kfree(acpi_gpio);
  440. return;
  441. }
  442. acpi_gpiochip_request_interrupts(acpi_gpio);
  443. acpi_gpiochip_request_regions(acpi_gpio);
  444. }
  445. void acpi_gpiochip_remove(struct gpio_chip *chip)
  446. {
  447. struct acpi_gpio_chip *acpi_gpio;
  448. acpi_handle handle;
  449. acpi_status status;
  450. if (!chip || !chip->dev)
  451. return;
  452. handle = ACPI_HANDLE(chip->dev);
  453. if (!handle)
  454. return;
  455. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  456. if (ACPI_FAILURE(status)) {
  457. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  458. return;
  459. }
  460. acpi_gpiochip_free_regions(acpi_gpio);
  461. acpi_gpiochip_free_interrupts(acpi_gpio);
  462. acpi_detach_data(handle, acpi_gpio_chip_dh);
  463. kfree(acpi_gpio);
  464. }