gpiolib-acpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 = gpio_lock_as_irq(chip, pin);
  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. gpio_unlock_as_irq(chip, pin);
  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. * @chip: 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. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  195. {
  196. struct acpi_gpio_chip *acpi_gpio;
  197. acpi_handle handle;
  198. acpi_status status;
  199. if (!chip->dev || !chip->to_irq)
  200. return;
  201. handle = ACPI_HANDLE(chip->dev);
  202. if (!handle)
  203. return;
  204. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  205. if (ACPI_FAILURE(status))
  206. return;
  207. INIT_LIST_HEAD(&acpi_gpio->events);
  208. acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
  209. acpi_gpiochip_request_interrupt, acpi_gpio);
  210. }
  211. /**
  212. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  213. * @chip: GPIO chip
  214. *
  215. * Free interrupts associated with GPIO ACPI event method for the given
  216. * GPIO chip.
  217. */
  218. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  219. {
  220. struct acpi_gpio_chip *acpi_gpio;
  221. struct acpi_gpio_event *event, *ep;
  222. acpi_handle handle;
  223. acpi_status status;
  224. if (!chip->dev || !chip->to_irq)
  225. return;
  226. handle = ACPI_HANDLE(chip->dev);
  227. if (!handle)
  228. return;
  229. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  230. if (ACPI_FAILURE(status))
  231. return;
  232. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  233. struct gpio_desc *desc;
  234. free_irq(event->irq, event);
  235. desc = gpiochip_get_desc(chip, event->pin);
  236. if (WARN_ON(IS_ERR(desc)))
  237. continue;
  238. gpio_unlock_as_irq(chip, event->pin);
  239. gpiochip_free_own_desc(desc);
  240. list_del(&event->node);
  241. kfree(event);
  242. }
  243. }
  244. struct acpi_gpio_lookup {
  245. struct acpi_gpio_info info;
  246. int index;
  247. struct gpio_desc *desc;
  248. int n;
  249. };
  250. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  251. {
  252. struct acpi_gpio_lookup *lookup = data;
  253. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  254. return 1;
  255. if (lookup->n++ == lookup->index && !lookup->desc) {
  256. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  257. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  258. agpio->pin_table[0]);
  259. lookup->info.gpioint =
  260. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  261. lookup->info.active_low =
  262. agpio->polarity == ACPI_ACTIVE_LOW;
  263. }
  264. return 1;
  265. }
  266. /**
  267. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  268. * @dev: pointer to a device to get GPIO from
  269. * @index: index of GpioIo/GpioInt resource (starting from %0)
  270. * @info: info pointer to fill in (optional)
  271. *
  272. * Function goes through ACPI resources for @dev and based on @index looks
  273. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  274. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  275. * are total %3 GPIO resources, the index goes from %0 to %2.
  276. *
  277. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  278. * returned.
  279. *
  280. * Note: if the GPIO resource has multiple entries in the pin list, this
  281. * function only returns the first.
  282. */
  283. struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
  284. struct acpi_gpio_info *info)
  285. {
  286. struct acpi_gpio_lookup lookup;
  287. struct list_head resource_list;
  288. struct acpi_device *adev;
  289. acpi_handle handle;
  290. int ret;
  291. if (!dev)
  292. return ERR_PTR(-EINVAL);
  293. handle = ACPI_HANDLE(dev);
  294. if (!handle || acpi_bus_get_device(handle, &adev))
  295. return ERR_PTR(-ENODEV);
  296. memset(&lookup, 0, sizeof(lookup));
  297. lookup.index = index;
  298. INIT_LIST_HEAD(&resource_list);
  299. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  300. &lookup);
  301. if (ret < 0)
  302. return ERR_PTR(ret);
  303. acpi_dev_free_resource_list(&resource_list);
  304. if (lookup.desc && info)
  305. *info = lookup.info;
  306. return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
  307. }
  308. static acpi_status
  309. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  310. u32 bits, u64 *value, void *handler_context,
  311. void *region_context)
  312. {
  313. struct acpi_gpio_chip *achip = region_context;
  314. struct gpio_chip *chip = achip->chip;
  315. struct acpi_resource_gpio *agpio;
  316. struct acpi_resource *ares;
  317. acpi_status status;
  318. bool pull_up;
  319. int i;
  320. status = acpi_buffer_to_resource(achip->conn_info.connection,
  321. achip->conn_info.length, &ares);
  322. if (ACPI_FAILURE(status))
  323. return status;
  324. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  325. ACPI_FREE(ares);
  326. return AE_BAD_PARAMETER;
  327. }
  328. agpio = &ares->data.gpio;
  329. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  330. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  331. function == ACPI_WRITE)) {
  332. ACPI_FREE(ares);
  333. return AE_BAD_PARAMETER;
  334. }
  335. for (i = 0; i < agpio->pin_table_length; i++) {
  336. unsigned pin = agpio->pin_table[i];
  337. struct acpi_gpio_connection *conn;
  338. struct gpio_desc *desc;
  339. bool found;
  340. desc = gpiochip_get_desc(chip, pin);
  341. if (IS_ERR(desc)) {
  342. status = AE_ERROR;
  343. goto out;
  344. }
  345. mutex_lock(&achip->conn_lock);
  346. found = false;
  347. list_for_each_entry(conn, &achip->conns, node) {
  348. if (conn->desc == desc) {
  349. found = true;
  350. break;
  351. }
  352. }
  353. if (!found) {
  354. int ret;
  355. ret = gpiochip_request_own_desc(desc, "ACPI:OpRegion");
  356. if (ret) {
  357. status = AE_ERROR;
  358. mutex_unlock(&achip->conn_lock);
  359. goto out;
  360. }
  361. switch (agpio->io_restriction) {
  362. case ACPI_IO_RESTRICT_INPUT:
  363. gpiod_direction_input(desc);
  364. break;
  365. case ACPI_IO_RESTRICT_OUTPUT:
  366. /*
  367. * ACPI GPIO resources don't contain an
  368. * initial value for the GPIO. Therefore we
  369. * deduce that value from the pull field
  370. * instead. If the pin is pulled up we
  371. * assume default to be high, otherwise
  372. * low.
  373. */
  374. gpiod_direction_output(desc, pull_up);
  375. break;
  376. default:
  377. /*
  378. * Assume that the BIOS has configured the
  379. * direction and pull accordingly.
  380. */
  381. break;
  382. }
  383. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  384. if (!conn) {
  385. status = AE_NO_MEMORY;
  386. gpiochip_free_own_desc(desc);
  387. mutex_unlock(&achip->conn_lock);
  388. goto out;
  389. }
  390. conn->desc = desc;
  391. list_add_tail(&conn->node, &achip->conns);
  392. }
  393. mutex_unlock(&achip->conn_lock);
  394. if (function == ACPI_WRITE)
  395. gpiod_set_raw_value_cansleep(desc,
  396. !!((1 << i) & *value));
  397. else
  398. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  399. }
  400. out:
  401. ACPI_FREE(ares);
  402. return status;
  403. }
  404. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  405. {
  406. struct gpio_chip *chip = achip->chip;
  407. acpi_handle handle = ACPI_HANDLE(chip->dev);
  408. acpi_status status;
  409. INIT_LIST_HEAD(&achip->conns);
  410. mutex_init(&achip->conn_lock);
  411. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  412. acpi_gpio_adr_space_handler,
  413. NULL, achip);
  414. if (ACPI_FAILURE(status))
  415. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  416. }
  417. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  418. {
  419. struct gpio_chip *chip = achip->chip;
  420. acpi_handle handle = ACPI_HANDLE(chip->dev);
  421. struct acpi_gpio_connection *conn, *tmp;
  422. acpi_status status;
  423. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  424. acpi_gpio_adr_space_handler);
  425. if (ACPI_FAILURE(status)) {
  426. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  427. return;
  428. }
  429. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  430. gpiochip_free_own_desc(conn->desc);
  431. list_del(&conn->node);
  432. kfree(conn);
  433. }
  434. }
  435. void acpi_gpiochip_add(struct gpio_chip *chip)
  436. {
  437. struct acpi_gpio_chip *acpi_gpio;
  438. acpi_handle handle;
  439. acpi_status status;
  440. if (!chip || !chip->dev)
  441. return;
  442. handle = ACPI_HANDLE(chip->dev);
  443. if (!handle)
  444. return;
  445. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  446. if (!acpi_gpio) {
  447. dev_err(chip->dev,
  448. "Failed to allocate memory for ACPI GPIO chip\n");
  449. return;
  450. }
  451. acpi_gpio->chip = chip;
  452. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  453. if (ACPI_FAILURE(status)) {
  454. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  455. kfree(acpi_gpio);
  456. return;
  457. }
  458. acpi_gpiochip_request_regions(acpi_gpio);
  459. }
  460. void acpi_gpiochip_remove(struct gpio_chip *chip)
  461. {
  462. struct acpi_gpio_chip *acpi_gpio;
  463. acpi_handle handle;
  464. acpi_status status;
  465. if (!chip || !chip->dev)
  466. return;
  467. handle = ACPI_HANDLE(chip->dev);
  468. if (!handle)
  469. return;
  470. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  471. if (ACPI_FAILURE(status)) {
  472. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  473. return;
  474. }
  475. acpi_gpiochip_free_regions(acpi_gpio);
  476. acpi_detach_data(handle, acpi_gpio_chip_dh);
  477. kfree(acpi_gpio);
  478. }