gpiolib-acpi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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/gpio/consumer.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/export.h>
  17. #include <linux/acpi.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include "gpiolib.h"
  22. struct acpi_gpio_event {
  23. struct list_head node;
  24. acpi_handle handle;
  25. unsigned int pin;
  26. unsigned int irq;
  27. struct gpio_desc *desc;
  28. };
  29. struct acpi_gpio_connection {
  30. struct list_head node;
  31. unsigned int pin;
  32. struct gpio_desc *desc;
  33. };
  34. struct acpi_gpio_chip {
  35. /*
  36. * ACPICA requires that the first field of the context parameter
  37. * passed to acpi_install_address_space_handler() is large enough
  38. * to hold struct acpi_connection_info.
  39. */
  40. struct acpi_connection_info conn_info;
  41. struct list_head conns;
  42. struct mutex conn_lock;
  43. struct gpio_chip *chip;
  44. struct list_head events;
  45. };
  46. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  47. {
  48. if (!gc->parent)
  49. return false;
  50. return ACPI_HANDLE(gc->parent) == data;
  51. }
  52. #ifdef CONFIG_PINCTRL
  53. /**
  54. * acpi_gpiochip_pin_to_gpio_offset() - translates ACPI GPIO to Linux GPIO
  55. * @chip: GPIO chip
  56. * @pin: ACPI GPIO pin number from GpioIo/GpioInt resource
  57. *
  58. * Function takes ACPI GpioIo/GpioInt pin number as a parameter and
  59. * translates it to a corresponding offset suitable to be passed to a
  60. * GPIO controller driver.
  61. *
  62. * Typically the returned offset is same as @pin, but if the GPIO
  63. * controller uses pin controller and the mapping is not contiguous the
  64. * offset might be different.
  65. */
  66. static int acpi_gpiochip_pin_to_gpio_offset(struct gpio_device *gdev, int pin)
  67. {
  68. struct gpio_pin_range *pin_range;
  69. /* If there are no ranges in this chip, use 1:1 mapping */
  70. if (list_empty(&gdev->pin_ranges))
  71. return pin;
  72. list_for_each_entry(pin_range, &gdev->pin_ranges, node) {
  73. const struct pinctrl_gpio_range *range = &pin_range->range;
  74. int i;
  75. if (range->pins) {
  76. for (i = 0; i < range->npins; i++) {
  77. if (range->pins[i] == pin)
  78. return range->base + i - gdev->base;
  79. }
  80. } else {
  81. if (pin >= range->pin_base &&
  82. pin < range->pin_base + range->npins) {
  83. unsigned gpio_base;
  84. gpio_base = range->base - gdev->base;
  85. return gpio_base + pin - range->pin_base;
  86. }
  87. }
  88. }
  89. return -EINVAL;
  90. }
  91. #else
  92. static inline int acpi_gpiochip_pin_to_gpio_offset(struct gpio_device *gdev,
  93. int pin)
  94. {
  95. return pin;
  96. }
  97. #endif
  98. /**
  99. * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
  100. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  101. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  102. *
  103. * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  104. * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
  105. * controller does not have gpiochip registered at the moment. This is to
  106. * support probe deferral.
  107. */
  108. static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  109. {
  110. struct gpio_chip *chip;
  111. acpi_handle handle;
  112. acpi_status status;
  113. int offset;
  114. status = acpi_get_handle(NULL, path, &handle);
  115. if (ACPI_FAILURE(status))
  116. return ERR_PTR(-ENODEV);
  117. chip = gpiochip_find(handle, acpi_gpiochip_find);
  118. if (!chip)
  119. return ERR_PTR(-EPROBE_DEFER);
  120. offset = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin);
  121. if (offset < 0)
  122. return ERR_PTR(offset);
  123. return gpiochip_get_desc(chip, offset);
  124. }
  125. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  126. {
  127. struct acpi_gpio_event *event = data;
  128. acpi_evaluate_object(event->handle, NULL, NULL, NULL);
  129. return IRQ_HANDLED;
  130. }
  131. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  132. {
  133. struct acpi_gpio_event *event = data;
  134. acpi_execute_simple_method(event->handle, NULL, event->pin);
  135. return IRQ_HANDLED;
  136. }
  137. static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
  138. {
  139. /* The address of this function is used as a key. */
  140. }
  141. static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
  142. void *context)
  143. {
  144. struct acpi_gpio_chip *acpi_gpio = context;
  145. struct gpio_chip *chip = acpi_gpio->chip;
  146. struct acpi_resource_gpio *agpio;
  147. acpi_handle handle, evt_handle;
  148. struct acpi_gpio_event *event;
  149. irq_handler_t handler = NULL;
  150. struct gpio_desc *desc;
  151. unsigned long irqflags;
  152. int ret, pin, irq;
  153. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  154. return AE_OK;
  155. agpio = &ares->data.gpio;
  156. if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
  157. return AE_OK;
  158. handle = ACPI_HANDLE(chip->parent);
  159. pin = agpio->pin_table[0];
  160. if (pin <= 255) {
  161. char ev_name[5];
  162. sprintf(ev_name, "_%c%02X",
  163. agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
  164. pin);
  165. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  166. handler = acpi_gpio_irq_handler;
  167. }
  168. if (!handler) {
  169. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  170. handler = acpi_gpio_irq_handler_evt;
  171. }
  172. if (!handler)
  173. return AE_BAD_PARAMETER;
  174. pin = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin);
  175. if (pin < 0)
  176. return AE_BAD_PARAMETER;
  177. desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
  178. if (IS_ERR(desc)) {
  179. dev_err(chip->parent, "Failed to request GPIO\n");
  180. return AE_ERROR;
  181. }
  182. gpiod_direction_input(desc);
  183. ret = gpiochip_lock_as_irq(chip, pin);
  184. if (ret) {
  185. dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
  186. goto fail_free_desc;
  187. }
  188. irq = gpiod_to_irq(desc);
  189. if (irq < 0) {
  190. dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
  191. goto fail_unlock_irq;
  192. }
  193. irqflags = IRQF_ONESHOT;
  194. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  195. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  196. irqflags |= IRQF_TRIGGER_HIGH;
  197. else
  198. irqflags |= IRQF_TRIGGER_LOW;
  199. } else {
  200. switch (agpio->polarity) {
  201. case ACPI_ACTIVE_HIGH:
  202. irqflags |= IRQF_TRIGGER_RISING;
  203. break;
  204. case ACPI_ACTIVE_LOW:
  205. irqflags |= IRQF_TRIGGER_FALLING;
  206. break;
  207. default:
  208. irqflags |= IRQF_TRIGGER_RISING |
  209. IRQF_TRIGGER_FALLING;
  210. break;
  211. }
  212. }
  213. event = kzalloc(sizeof(*event), GFP_KERNEL);
  214. if (!event)
  215. goto fail_unlock_irq;
  216. event->handle = evt_handle;
  217. event->irq = irq;
  218. event->pin = pin;
  219. event->desc = desc;
  220. ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
  221. "ACPI:Event", event);
  222. if (ret) {
  223. dev_err(chip->parent,
  224. "Failed to setup interrupt handler for %d\n",
  225. event->irq);
  226. goto fail_free_event;
  227. }
  228. list_add_tail(&event->node, &acpi_gpio->events);
  229. return AE_OK;
  230. fail_free_event:
  231. kfree(event);
  232. fail_unlock_irq:
  233. gpiochip_unlock_as_irq(chip, pin);
  234. fail_free_desc:
  235. gpiochip_free_own_desc(desc);
  236. return AE_ERROR;
  237. }
  238. /**
  239. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  240. * @chip: GPIO chip
  241. *
  242. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  243. * handled by ACPI event methods which need to be called from the GPIO
  244. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  245. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  246. * the acpi event methods for those pins.
  247. */
  248. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  249. {
  250. struct acpi_gpio_chip *acpi_gpio;
  251. acpi_handle handle;
  252. acpi_status status;
  253. if (!chip->parent || !chip->to_irq)
  254. return;
  255. handle = ACPI_HANDLE(chip->parent);
  256. if (!handle)
  257. return;
  258. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  259. if (ACPI_FAILURE(status))
  260. return;
  261. acpi_walk_resources(handle, "_AEI",
  262. acpi_gpiochip_request_interrupt, acpi_gpio);
  263. }
  264. EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
  265. /**
  266. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  267. * @chip: GPIO chip
  268. *
  269. * Free interrupts associated with GPIO ACPI event method for the given
  270. * GPIO chip.
  271. */
  272. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  273. {
  274. struct acpi_gpio_chip *acpi_gpio;
  275. struct acpi_gpio_event *event, *ep;
  276. acpi_handle handle;
  277. acpi_status status;
  278. if (!chip->parent || !chip->to_irq)
  279. return;
  280. handle = ACPI_HANDLE(chip->parent);
  281. if (!handle)
  282. return;
  283. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  284. if (ACPI_FAILURE(status))
  285. return;
  286. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  287. struct gpio_desc *desc;
  288. free_irq(event->irq, event);
  289. desc = event->desc;
  290. if (WARN_ON(IS_ERR(desc)))
  291. continue;
  292. gpiochip_unlock_as_irq(chip, event->pin);
  293. gpiochip_free_own_desc(desc);
  294. list_del(&event->node);
  295. kfree(event);
  296. }
  297. }
  298. EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
  299. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  300. const struct acpi_gpio_mapping *gpios)
  301. {
  302. if (adev && gpios) {
  303. adev->driver_gpios = gpios;
  304. return 0;
  305. }
  306. return -EINVAL;
  307. }
  308. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  309. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  310. const char *name, int index,
  311. struct acpi_reference_args *args)
  312. {
  313. const struct acpi_gpio_mapping *gm;
  314. if (!adev->driver_gpios)
  315. return false;
  316. for (gm = adev->driver_gpios; gm->name; gm++)
  317. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  318. const struct acpi_gpio_params *par = gm->data + index;
  319. args->adev = adev;
  320. args->args[0] = par->crs_entry_index;
  321. args->args[1] = par->line_index;
  322. args->args[2] = par->active_low;
  323. args->nargs = 3;
  324. return true;
  325. }
  326. return false;
  327. }
  328. struct acpi_gpio_lookup {
  329. struct acpi_gpio_info info;
  330. int index;
  331. int pin_index;
  332. bool active_low;
  333. struct acpi_device *adev;
  334. struct gpio_desc *desc;
  335. int n;
  336. };
  337. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  338. {
  339. struct acpi_gpio_lookup *lookup = data;
  340. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  341. return 1;
  342. if (lookup->n++ == lookup->index && !lookup->desc) {
  343. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  344. int pin_index = lookup->pin_index;
  345. if (pin_index >= agpio->pin_table_length)
  346. return 1;
  347. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  348. agpio->pin_table[pin_index]);
  349. lookup->info.gpioint =
  350. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  351. /*
  352. * ActiveLow is only specified for GpioInt resource. If
  353. * GpioIo is used then the only way to set the flag is
  354. * to use _DSD "gpios" property.
  355. * Note: we expect here:
  356. * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
  357. * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
  358. */
  359. if (lookup->info.gpioint) {
  360. lookup->info.polarity = agpio->polarity;
  361. lookup->info.triggering = agpio->triggering;
  362. }
  363. }
  364. return 1;
  365. }
  366. static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
  367. struct acpi_gpio_info *info)
  368. {
  369. struct list_head res_list;
  370. int ret;
  371. INIT_LIST_HEAD(&res_list);
  372. ret = acpi_dev_get_resources(lookup->adev, &res_list, acpi_find_gpio,
  373. lookup);
  374. if (ret < 0)
  375. return ret;
  376. acpi_dev_free_resource_list(&res_list);
  377. if (!lookup->desc)
  378. return -ENOENT;
  379. if (info) {
  380. *info = lookup->info;
  381. if (lookup->active_low)
  382. info->polarity = lookup->active_low;
  383. }
  384. return 0;
  385. }
  386. static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
  387. const char *propname, int index,
  388. struct acpi_gpio_lookup *lookup)
  389. {
  390. struct acpi_reference_args args;
  391. int ret;
  392. memset(&args, 0, sizeof(args));
  393. ret = acpi_node_get_property_reference(fwnode, propname, index, &args);
  394. if (ret) {
  395. struct acpi_device *adev = to_acpi_device_node(fwnode);
  396. if (!adev)
  397. return ret;
  398. if (!acpi_get_driver_gpio_data(adev, propname, index, &args))
  399. return ret;
  400. }
  401. /*
  402. * The property was found and resolved, so need to lookup the GPIO based
  403. * on returned args.
  404. */
  405. lookup->adev = args.adev;
  406. if (args.nargs >= 2) {
  407. lookup->index = args.args[0];
  408. lookup->pin_index = args.args[1];
  409. /* 3rd argument, if present is used to specify active_low. */
  410. if (args.nargs >= 3)
  411. lookup->active_low = !!args.args[2];
  412. }
  413. return 0;
  414. }
  415. /**
  416. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  417. * @adev: pointer to a ACPI device to get GPIO from
  418. * @propname: Property name of the GPIO (optional)
  419. * @index: index of GpioIo/GpioInt resource (starting from %0)
  420. * @info: info pointer to fill in (optional)
  421. *
  422. * Function goes through ACPI resources for @adev and based on @index looks
  423. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  424. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  425. * are total %3 GPIO resources, the index goes from %0 to %2.
  426. *
  427. * If @propname is specified the GPIO is looked using device property. In
  428. * that case @index is used to select the GPIO entry in the property value
  429. * (in case of multiple).
  430. *
  431. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  432. * returned.
  433. *
  434. * Note: if the GPIO resource has multiple entries in the pin list, this
  435. * function only returns the first.
  436. */
  437. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  438. const char *propname, int index,
  439. struct acpi_gpio_info *info)
  440. {
  441. struct acpi_gpio_lookup lookup;
  442. int ret;
  443. if (!adev)
  444. return ERR_PTR(-ENODEV);
  445. memset(&lookup, 0, sizeof(lookup));
  446. lookup.index = index;
  447. if (propname) {
  448. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  449. ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
  450. propname, index, &lookup);
  451. if (ret)
  452. return ERR_PTR(ret);
  453. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
  454. dev_name(&lookup.adev->dev), lookup.index,
  455. lookup.pin_index, lookup.active_low);
  456. } else {
  457. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  458. lookup.adev = adev;
  459. }
  460. ret = acpi_gpio_resource_lookup(&lookup, info);
  461. return ret ? ERR_PTR(ret) : lookup.desc;
  462. }
  463. /**
  464. * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
  465. * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  466. * @propname: Property name of the GPIO
  467. * @index: index of GpioIo/GpioInt resource (starting from %0)
  468. * @info: info pointer to fill in (optional)
  469. *
  470. * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
  471. * Otherwise (ie. it is a data-only non-device object), use the property-based
  472. * GPIO lookup to get to the GPIO resource with the relevant information and use
  473. * that to obtain the GPIO descriptor to return.
  474. */
  475. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  476. const char *propname, int index,
  477. struct acpi_gpio_info *info)
  478. {
  479. struct acpi_gpio_lookup lookup;
  480. struct acpi_device *adev;
  481. int ret;
  482. adev = to_acpi_device_node(fwnode);
  483. if (adev)
  484. return acpi_get_gpiod_by_index(adev, propname, index, info);
  485. if (!is_acpi_data_node(fwnode))
  486. return ERR_PTR(-ENODEV);
  487. if (!propname)
  488. return ERR_PTR(-EINVAL);
  489. memset(&lookup, 0, sizeof(lookup));
  490. lookup.index = index;
  491. ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
  492. if (ret)
  493. return ERR_PTR(ret);
  494. ret = acpi_gpio_resource_lookup(&lookup, info);
  495. return ret ? ERR_PTR(ret) : lookup.desc;
  496. }
  497. /**
  498. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  499. * @adev: pointer to a ACPI device to get IRQ from
  500. * @index: index of GpioInt resource (starting from %0)
  501. *
  502. * If the device has one or more GpioInt resources, this function can be
  503. * used to translate from the GPIO offset in the resource to the Linux IRQ
  504. * number.
  505. *
  506. * Return: Linux IRQ number (>%0) on success, negative errno on failure.
  507. */
  508. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  509. {
  510. int idx, i;
  511. unsigned int irq_flags;
  512. for (i = 0, idx = 0; idx <= index; i++) {
  513. struct acpi_gpio_info info;
  514. struct gpio_desc *desc;
  515. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  516. if (IS_ERR(desc))
  517. break;
  518. if (info.gpioint && idx++ == index) {
  519. int irq = gpiod_to_irq(desc);
  520. if (irq < 0)
  521. return irq;
  522. irq_flags = acpi_dev_get_irq_type(info.triggering,
  523. info.polarity);
  524. /* Set type if specified and different than the current one */
  525. if (irq_flags != IRQ_TYPE_NONE &&
  526. irq_flags != irq_get_trigger_type(irq))
  527. irq_set_irq_type(irq, irq_flags);
  528. return irq;
  529. }
  530. }
  531. return -ENOENT;
  532. }
  533. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  534. static acpi_status
  535. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  536. u32 bits, u64 *value, void *handler_context,
  537. void *region_context)
  538. {
  539. struct acpi_gpio_chip *achip = region_context;
  540. struct gpio_chip *chip = achip->chip;
  541. struct acpi_resource_gpio *agpio;
  542. struct acpi_resource *ares;
  543. int pin_index = (int)address;
  544. acpi_status status;
  545. bool pull_up;
  546. int length;
  547. int i;
  548. status = acpi_buffer_to_resource(achip->conn_info.connection,
  549. achip->conn_info.length, &ares);
  550. if (ACPI_FAILURE(status))
  551. return status;
  552. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  553. ACPI_FREE(ares);
  554. return AE_BAD_PARAMETER;
  555. }
  556. agpio = &ares->data.gpio;
  557. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  558. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  559. function == ACPI_WRITE)) {
  560. ACPI_FREE(ares);
  561. return AE_BAD_PARAMETER;
  562. }
  563. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  564. for (i = pin_index; i < length; ++i) {
  565. int pin = agpio->pin_table[i];
  566. struct acpi_gpio_connection *conn;
  567. struct gpio_desc *desc;
  568. bool found;
  569. pin = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin);
  570. if (pin < 0) {
  571. status = AE_BAD_PARAMETER;
  572. goto out;
  573. }
  574. mutex_lock(&achip->conn_lock);
  575. found = false;
  576. list_for_each_entry(conn, &achip->conns, node) {
  577. if (conn->pin == pin) {
  578. found = true;
  579. desc = conn->desc;
  580. break;
  581. }
  582. }
  583. /*
  584. * The same GPIO can be shared between operation region and
  585. * event but only if the access here is ACPI_READ. In that
  586. * case we "borrow" the event GPIO instead.
  587. */
  588. if (!found && agpio->sharable == ACPI_SHARED &&
  589. function == ACPI_READ) {
  590. struct acpi_gpio_event *event;
  591. list_for_each_entry(event, &achip->events, node) {
  592. if (event->pin == pin) {
  593. desc = event->desc;
  594. found = true;
  595. break;
  596. }
  597. }
  598. }
  599. if (!found) {
  600. desc = gpiochip_request_own_desc(chip, pin,
  601. "ACPI:OpRegion");
  602. if (IS_ERR(desc)) {
  603. status = AE_ERROR;
  604. mutex_unlock(&achip->conn_lock);
  605. goto out;
  606. }
  607. switch (agpio->io_restriction) {
  608. case ACPI_IO_RESTRICT_INPUT:
  609. gpiod_direction_input(desc);
  610. break;
  611. case ACPI_IO_RESTRICT_OUTPUT:
  612. /*
  613. * ACPI GPIO resources don't contain an
  614. * initial value for the GPIO. Therefore we
  615. * deduce that value from the pull field
  616. * instead. If the pin is pulled up we
  617. * assume default to be high, otherwise
  618. * low.
  619. */
  620. gpiod_direction_output(desc, pull_up);
  621. break;
  622. default:
  623. /*
  624. * Assume that the BIOS has configured the
  625. * direction and pull accordingly.
  626. */
  627. break;
  628. }
  629. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  630. if (!conn) {
  631. status = AE_NO_MEMORY;
  632. gpiochip_free_own_desc(desc);
  633. mutex_unlock(&achip->conn_lock);
  634. goto out;
  635. }
  636. conn->pin = pin;
  637. conn->desc = desc;
  638. list_add_tail(&conn->node, &achip->conns);
  639. }
  640. mutex_unlock(&achip->conn_lock);
  641. if (function == ACPI_WRITE)
  642. gpiod_set_raw_value_cansleep(desc,
  643. !!((1 << i) & *value));
  644. else
  645. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  646. }
  647. out:
  648. ACPI_FREE(ares);
  649. return status;
  650. }
  651. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  652. {
  653. struct gpio_chip *chip = achip->chip;
  654. acpi_handle handle = ACPI_HANDLE(chip->parent);
  655. acpi_status status;
  656. INIT_LIST_HEAD(&achip->conns);
  657. mutex_init(&achip->conn_lock);
  658. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  659. acpi_gpio_adr_space_handler,
  660. NULL, achip);
  661. if (ACPI_FAILURE(status))
  662. dev_err(chip->parent,
  663. "Failed to install GPIO OpRegion handler\n");
  664. }
  665. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  666. {
  667. struct gpio_chip *chip = achip->chip;
  668. acpi_handle handle = ACPI_HANDLE(chip->parent);
  669. struct acpi_gpio_connection *conn, *tmp;
  670. acpi_status status;
  671. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  672. acpi_gpio_adr_space_handler);
  673. if (ACPI_FAILURE(status)) {
  674. dev_err(chip->parent,
  675. "Failed to remove GPIO OpRegion handler\n");
  676. return;
  677. }
  678. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  679. gpiochip_free_own_desc(conn->desc);
  680. list_del(&conn->node);
  681. kfree(conn);
  682. }
  683. }
  684. void acpi_gpiochip_add(struct gpio_chip *chip)
  685. {
  686. struct acpi_gpio_chip *acpi_gpio;
  687. acpi_handle handle;
  688. acpi_status status;
  689. if (!chip || !chip->parent)
  690. return;
  691. handle = ACPI_HANDLE(chip->parent);
  692. if (!handle)
  693. return;
  694. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  695. if (!acpi_gpio) {
  696. dev_err(chip->parent,
  697. "Failed to allocate memory for ACPI GPIO chip\n");
  698. return;
  699. }
  700. acpi_gpio->chip = chip;
  701. INIT_LIST_HEAD(&acpi_gpio->events);
  702. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  703. if (ACPI_FAILURE(status)) {
  704. dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
  705. kfree(acpi_gpio);
  706. return;
  707. }
  708. acpi_gpiochip_request_regions(acpi_gpio);
  709. }
  710. void acpi_gpiochip_remove(struct gpio_chip *chip)
  711. {
  712. struct acpi_gpio_chip *acpi_gpio;
  713. acpi_handle handle;
  714. acpi_status status;
  715. if (!chip || !chip->parent)
  716. return;
  717. handle = ACPI_HANDLE(chip->parent);
  718. if (!handle)
  719. return;
  720. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  721. if (ACPI_FAILURE(status)) {
  722. dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
  723. return;
  724. }
  725. acpi_gpiochip_free_regions(acpi_gpio);
  726. acpi_detach_data(handle, acpi_gpio_chip_dh);
  727. kfree(acpi_gpio);
  728. }
  729. static unsigned int acpi_gpio_package_count(const union acpi_object *obj)
  730. {
  731. const union acpi_object *element = obj->package.elements;
  732. const union acpi_object *end = element + obj->package.count;
  733. unsigned int count = 0;
  734. while (element < end) {
  735. if (element->type == ACPI_TYPE_LOCAL_REFERENCE)
  736. count++;
  737. element++;
  738. }
  739. return count;
  740. }
  741. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  742. {
  743. unsigned int *count = data;
  744. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  745. *count += ares->data.gpio.pin_table_length;
  746. return 1;
  747. }
  748. /**
  749. * acpi_gpio_count - return the number of GPIOs associated with a
  750. * device / function or -ENOENT if no GPIO has been
  751. * assigned to the requested function.
  752. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  753. * @con_id: function within the GPIO consumer
  754. */
  755. int acpi_gpio_count(struct device *dev, const char *con_id)
  756. {
  757. struct acpi_device *adev = ACPI_COMPANION(dev);
  758. const union acpi_object *obj;
  759. const struct acpi_gpio_mapping *gm;
  760. int count = -ENOENT;
  761. int ret;
  762. char propname[32];
  763. unsigned int i;
  764. /* Try first from _DSD */
  765. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  766. if (con_id && strcmp(con_id, "gpios"))
  767. snprintf(propname, sizeof(propname), "%s-%s",
  768. con_id, gpio_suffixes[i]);
  769. else
  770. snprintf(propname, sizeof(propname), "%s",
  771. gpio_suffixes[i]);
  772. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  773. &obj);
  774. if (ret == 0) {
  775. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  776. count = 1;
  777. else if (obj->type == ACPI_TYPE_PACKAGE)
  778. count = acpi_gpio_package_count(obj);
  779. } else if (adev->driver_gpios) {
  780. for (gm = adev->driver_gpios; gm->name; gm++)
  781. if (strcmp(propname, gm->name) == 0) {
  782. count = gm->size;
  783. break;
  784. }
  785. }
  786. if (count >= 0)
  787. break;
  788. }
  789. /* Then from plain _CRS GPIOs */
  790. if (count < 0) {
  791. struct list_head resource_list;
  792. unsigned int crs_count = 0;
  793. INIT_LIST_HEAD(&resource_list);
  794. acpi_dev_get_resources(adev, &resource_list,
  795. acpi_find_gpio_count, &crs_count);
  796. acpi_dev_free_resource_list(&resource_list);
  797. if (crs_count > 0)
  798. count = crs_count;
  799. }
  800. return count;
  801. }
  802. struct acpi_crs_lookup {
  803. struct list_head node;
  804. struct acpi_device *adev;
  805. const char *con_id;
  806. };
  807. static DEFINE_MUTEX(acpi_crs_lookup_lock);
  808. static LIST_HEAD(acpi_crs_lookup_list);
  809. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
  810. {
  811. struct acpi_crs_lookup *l, *lookup = NULL;
  812. /* Never allow fallback if the device has properties */
  813. if (adev->data.properties || adev->driver_gpios)
  814. return false;
  815. mutex_lock(&acpi_crs_lookup_lock);
  816. list_for_each_entry(l, &acpi_crs_lookup_list, node) {
  817. if (l->adev == adev) {
  818. lookup = l;
  819. break;
  820. }
  821. }
  822. if (!lookup) {
  823. lookup = kmalloc(sizeof(*lookup), GFP_KERNEL);
  824. if (lookup) {
  825. lookup->adev = adev;
  826. lookup->con_id = con_id;
  827. list_add_tail(&lookup->node, &acpi_crs_lookup_list);
  828. }
  829. }
  830. mutex_unlock(&acpi_crs_lookup_lock);
  831. return lookup &&
  832. ((!lookup->con_id && !con_id) ||
  833. (lookup->con_id && con_id &&
  834. strcmp(lookup->con_id, con_id) == 0));
  835. }