gpiolib-acpi.c 14 KB

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