gpiolib-acpi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  243. const struct acpi_gpio_mapping *gpios)
  244. {
  245. if (adev && gpios) {
  246. adev->driver_gpios = gpios;
  247. return 0;
  248. }
  249. return -EINVAL;
  250. }
  251. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  252. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  253. const char *name, int index,
  254. struct acpi_reference_args *args)
  255. {
  256. const struct acpi_gpio_mapping *gm;
  257. if (!adev->driver_gpios)
  258. return false;
  259. for (gm = adev->driver_gpios; gm->name; gm++)
  260. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  261. const struct acpi_gpio_params *par = gm->data + index;
  262. args->adev = adev;
  263. args->args[0] = par->crs_entry_index;
  264. args->args[1] = par->line_index;
  265. args->args[2] = par->active_low;
  266. args->nargs = 3;
  267. return true;
  268. }
  269. return false;
  270. }
  271. struct acpi_gpio_lookup {
  272. struct acpi_gpio_info info;
  273. int index;
  274. int pin_index;
  275. struct gpio_desc *desc;
  276. int n;
  277. };
  278. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  279. {
  280. struct acpi_gpio_lookup *lookup = data;
  281. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  282. return 1;
  283. if (lookup->n++ == lookup->index && !lookup->desc) {
  284. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  285. int pin_index = lookup->pin_index;
  286. if (pin_index >= agpio->pin_table_length)
  287. return 1;
  288. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  289. agpio->pin_table[pin_index]);
  290. lookup->info.gpioint =
  291. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  292. /*
  293. * ActiveLow is only specified for GpioInt resource. If
  294. * GpioIo is used then the only way to set the flag is
  295. * to use _DSD "gpios" property.
  296. */
  297. if (lookup->info.gpioint)
  298. lookup->info.active_low =
  299. agpio->polarity == ACPI_ACTIVE_LOW;
  300. }
  301. return 1;
  302. }
  303. /**
  304. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  305. * @adev: pointer to a ACPI device to get GPIO from
  306. * @propname: Property name of the GPIO (optional)
  307. * @index: index of GpioIo/GpioInt resource (starting from %0)
  308. * @info: info pointer to fill in (optional)
  309. *
  310. * Function goes through ACPI resources for @adev and based on @index looks
  311. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  312. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  313. * are total %3 GPIO resources, the index goes from %0 to %2.
  314. *
  315. * If @propname is specified the GPIO is looked using device property. In
  316. * that case @index is used to select the GPIO entry in the property value
  317. * (in case of multiple).
  318. *
  319. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  320. * returned.
  321. *
  322. * Note: if the GPIO resource has multiple entries in the pin list, this
  323. * function only returns the first.
  324. */
  325. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  326. const char *propname, int index,
  327. struct acpi_gpio_info *info)
  328. {
  329. struct acpi_gpio_lookup lookup;
  330. struct list_head resource_list;
  331. bool active_low = false;
  332. int ret;
  333. if (!adev)
  334. return ERR_PTR(-ENODEV);
  335. memset(&lookup, 0, sizeof(lookup));
  336. lookup.index = index;
  337. if (propname) {
  338. struct acpi_reference_args args;
  339. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  340. memset(&args, 0, sizeof(args));
  341. ret = acpi_dev_get_property_reference(adev, propname,
  342. index, &args);
  343. if (ret) {
  344. bool found = acpi_get_driver_gpio_data(adev, propname,
  345. index, &args);
  346. if (!found)
  347. return ERR_PTR(ret);
  348. }
  349. /*
  350. * The property was found and resolved so need to
  351. * lookup the GPIO based on returned args instead.
  352. */
  353. adev = args.adev;
  354. if (args.nargs >= 2) {
  355. lookup.index = args.args[0];
  356. lookup.pin_index = args.args[1];
  357. /*
  358. * 3rd argument, if present is used to
  359. * specify active_low.
  360. */
  361. if (args.nargs >= 3)
  362. active_low = !!args.args[2];
  363. }
  364. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %zd %llu %llu %llu\n",
  365. dev_name(&adev->dev), args.nargs,
  366. args.args[0], args.args[1], args.args[2]);
  367. } else {
  368. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  369. }
  370. INIT_LIST_HEAD(&resource_list);
  371. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  372. &lookup);
  373. if (ret < 0)
  374. return ERR_PTR(ret);
  375. acpi_dev_free_resource_list(&resource_list);
  376. if (lookup.desc && info) {
  377. *info = lookup.info;
  378. if (active_low)
  379. info->active_low = active_low;
  380. }
  381. return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
  382. }
  383. static acpi_status
  384. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  385. u32 bits, u64 *value, void *handler_context,
  386. void *region_context)
  387. {
  388. struct acpi_gpio_chip *achip = region_context;
  389. struct gpio_chip *chip = achip->chip;
  390. struct acpi_resource_gpio *agpio;
  391. struct acpi_resource *ares;
  392. int pin_index = (int)address;
  393. acpi_status status;
  394. bool pull_up;
  395. int length;
  396. int i;
  397. status = acpi_buffer_to_resource(achip->conn_info.connection,
  398. achip->conn_info.length, &ares);
  399. if (ACPI_FAILURE(status))
  400. return status;
  401. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  402. ACPI_FREE(ares);
  403. return AE_BAD_PARAMETER;
  404. }
  405. agpio = &ares->data.gpio;
  406. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  407. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  408. function == ACPI_WRITE)) {
  409. ACPI_FREE(ares);
  410. return AE_BAD_PARAMETER;
  411. }
  412. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  413. for (i = pin_index; i < length; ++i) {
  414. unsigned pin = agpio->pin_table[i];
  415. struct acpi_gpio_connection *conn;
  416. struct gpio_desc *desc;
  417. bool found;
  418. mutex_lock(&achip->conn_lock);
  419. found = false;
  420. list_for_each_entry(conn, &achip->conns, node) {
  421. if (conn->pin == pin) {
  422. found = true;
  423. desc = conn->desc;
  424. break;
  425. }
  426. }
  427. if (!found) {
  428. desc = gpiochip_request_own_desc(chip, pin,
  429. "ACPI:OpRegion");
  430. if (IS_ERR(desc)) {
  431. status = AE_ERROR;
  432. mutex_unlock(&achip->conn_lock);
  433. goto out;
  434. }
  435. switch (agpio->io_restriction) {
  436. case ACPI_IO_RESTRICT_INPUT:
  437. gpiod_direction_input(desc);
  438. break;
  439. case ACPI_IO_RESTRICT_OUTPUT:
  440. /*
  441. * ACPI GPIO resources don't contain an
  442. * initial value for the GPIO. Therefore we
  443. * deduce that value from the pull field
  444. * instead. If the pin is pulled up we
  445. * assume default to be high, otherwise
  446. * low.
  447. */
  448. gpiod_direction_output(desc, pull_up);
  449. break;
  450. default:
  451. /*
  452. * Assume that the BIOS has configured the
  453. * direction and pull accordingly.
  454. */
  455. break;
  456. }
  457. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  458. if (!conn) {
  459. status = AE_NO_MEMORY;
  460. gpiochip_free_own_desc(desc);
  461. mutex_unlock(&achip->conn_lock);
  462. goto out;
  463. }
  464. conn->pin = pin;
  465. conn->desc = desc;
  466. list_add_tail(&conn->node, &achip->conns);
  467. }
  468. mutex_unlock(&achip->conn_lock);
  469. if (function == ACPI_WRITE)
  470. gpiod_set_raw_value_cansleep(desc,
  471. !!((1 << i) & *value));
  472. else
  473. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  474. }
  475. out:
  476. ACPI_FREE(ares);
  477. return status;
  478. }
  479. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  480. {
  481. struct gpio_chip *chip = achip->chip;
  482. acpi_handle handle = ACPI_HANDLE(chip->dev);
  483. acpi_status status;
  484. INIT_LIST_HEAD(&achip->conns);
  485. mutex_init(&achip->conn_lock);
  486. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  487. acpi_gpio_adr_space_handler,
  488. NULL, achip);
  489. if (ACPI_FAILURE(status))
  490. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  491. }
  492. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  493. {
  494. struct gpio_chip *chip = achip->chip;
  495. acpi_handle handle = ACPI_HANDLE(chip->dev);
  496. struct acpi_gpio_connection *conn, *tmp;
  497. acpi_status status;
  498. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  499. acpi_gpio_adr_space_handler);
  500. if (ACPI_FAILURE(status)) {
  501. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  502. return;
  503. }
  504. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  505. gpiochip_free_own_desc(conn->desc);
  506. list_del(&conn->node);
  507. kfree(conn);
  508. }
  509. }
  510. void acpi_gpiochip_add(struct gpio_chip *chip)
  511. {
  512. struct acpi_gpio_chip *acpi_gpio;
  513. acpi_handle handle;
  514. acpi_status status;
  515. if (!chip || !chip->dev)
  516. return;
  517. handle = ACPI_HANDLE(chip->dev);
  518. if (!handle)
  519. return;
  520. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  521. if (!acpi_gpio) {
  522. dev_err(chip->dev,
  523. "Failed to allocate memory for ACPI GPIO chip\n");
  524. return;
  525. }
  526. acpi_gpio->chip = chip;
  527. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  528. if (ACPI_FAILURE(status)) {
  529. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  530. kfree(acpi_gpio);
  531. return;
  532. }
  533. acpi_gpiochip_request_regions(acpi_gpio);
  534. }
  535. void acpi_gpiochip_remove(struct gpio_chip *chip)
  536. {
  537. struct acpi_gpio_chip *acpi_gpio;
  538. acpi_handle handle;
  539. acpi_status status;
  540. if (!chip || !chip->dev)
  541. return;
  542. handle = ACPI_HANDLE(chip->dev);
  543. if (!handle)
  544. return;
  545. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  546. if (ACPI_FAILURE(status)) {
  547. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  548. return;
  549. }
  550. acpi_gpiochip_free_regions(acpi_gpio);
  551. acpi_detach_data(handle, acpi_gpio_chip_dh);
  552. kfree(acpi_gpio);
  553. }