gpiolib-acpi.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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/gpio/machine.h>
  17. #include <linux/export.h>
  18. #include <linux/acpi.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mutex.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include "gpiolib.h"
  23. struct acpi_gpio_event {
  24. struct list_head node;
  25. acpi_handle handle;
  26. unsigned int pin;
  27. unsigned int irq;
  28. struct gpio_desc *desc;
  29. };
  30. struct acpi_gpio_connection {
  31. struct list_head node;
  32. unsigned int pin;
  33. struct gpio_desc *desc;
  34. };
  35. struct acpi_gpio_chip {
  36. /*
  37. * ACPICA requires that the first field of the context parameter
  38. * passed to acpi_install_address_space_handler() is large enough
  39. * to hold struct acpi_connection_info.
  40. */
  41. struct acpi_connection_info conn_info;
  42. struct list_head conns;
  43. struct mutex conn_lock;
  44. struct gpio_chip *chip;
  45. struct list_head events;
  46. struct list_head deferred_req_irqs_list_entry;
  47. };
  48. /*
  49. * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init
  50. * (so builtin drivers) we register the ACPI GpioInt event handlers from a
  51. * late_initcall_sync handler, so that other builtin drivers can register their
  52. * OpRegions before the event handlers can run. This list contains gpiochips
  53. * for which the acpi_gpiochip_request_interrupts() has been deferred.
  54. */
  55. static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
  56. static LIST_HEAD(acpi_gpio_deferred_req_irqs_list);
  57. static bool acpi_gpio_deferred_req_irqs_done;
  58. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  59. {
  60. if (!gc->parent)
  61. return false;
  62. return ACPI_HANDLE(gc->parent) == data;
  63. }
  64. /**
  65. * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
  66. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  67. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  68. *
  69. * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  70. * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
  71. * controller does not have gpiochip registered at the moment. This is to
  72. * support probe deferral.
  73. */
  74. static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  75. {
  76. struct gpio_chip *chip;
  77. acpi_handle handle;
  78. acpi_status status;
  79. status = acpi_get_handle(NULL, path, &handle);
  80. if (ACPI_FAILURE(status))
  81. return ERR_PTR(-ENODEV);
  82. chip = gpiochip_find(handle, acpi_gpiochip_find);
  83. if (!chip)
  84. return ERR_PTR(-EPROBE_DEFER);
  85. return gpiochip_get_desc(chip, pin);
  86. }
  87. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  88. {
  89. struct acpi_gpio_event *event = data;
  90. acpi_evaluate_object(event->handle, NULL, NULL, NULL);
  91. return IRQ_HANDLED;
  92. }
  93. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  94. {
  95. struct acpi_gpio_event *event = data;
  96. acpi_execute_simple_method(event->handle, NULL, event->pin);
  97. return IRQ_HANDLED;
  98. }
  99. static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
  100. {
  101. /* The address of this function is used as a key. */
  102. }
  103. bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
  104. struct acpi_resource_gpio **agpio)
  105. {
  106. struct acpi_resource_gpio *gpio;
  107. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  108. return false;
  109. gpio = &ares->data.gpio;
  110. if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
  111. return false;
  112. *agpio = gpio;
  113. return true;
  114. }
  115. EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
  116. static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
  117. void *context)
  118. {
  119. struct acpi_gpio_chip *acpi_gpio = context;
  120. struct gpio_chip *chip = acpi_gpio->chip;
  121. struct acpi_resource_gpio *agpio;
  122. acpi_handle handle, evt_handle;
  123. struct acpi_gpio_event *event;
  124. irq_handler_t handler = NULL;
  125. struct gpio_desc *desc;
  126. unsigned long irqflags;
  127. int ret, pin, irq, value;
  128. if (!acpi_gpio_get_irq_resource(ares, &agpio))
  129. return AE_OK;
  130. handle = ACPI_HANDLE(chip->parent);
  131. pin = agpio->pin_table[0];
  132. if (pin <= 255) {
  133. char ev_name[5];
  134. sprintf(ev_name, "_%c%02hhX",
  135. agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
  136. pin);
  137. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  138. handler = acpi_gpio_irq_handler;
  139. }
  140. if (!handler) {
  141. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  142. handler = acpi_gpio_irq_handler_evt;
  143. }
  144. if (!handler)
  145. return AE_OK;
  146. desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
  147. if (IS_ERR(desc)) {
  148. dev_err(chip->parent, "Failed to request GPIO\n");
  149. return AE_ERROR;
  150. }
  151. gpiod_direction_input(desc);
  152. value = gpiod_get_value_cansleep(desc);
  153. ret = gpiochip_lock_as_irq(chip, pin);
  154. if (ret) {
  155. dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
  156. goto fail_free_desc;
  157. }
  158. irq = gpiod_to_irq(desc);
  159. if (irq < 0) {
  160. dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
  161. goto fail_unlock_irq;
  162. }
  163. irqflags = IRQF_ONESHOT;
  164. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  165. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  166. irqflags |= IRQF_TRIGGER_HIGH;
  167. else
  168. irqflags |= IRQF_TRIGGER_LOW;
  169. } else {
  170. switch (agpio->polarity) {
  171. case ACPI_ACTIVE_HIGH:
  172. irqflags |= IRQF_TRIGGER_RISING;
  173. break;
  174. case ACPI_ACTIVE_LOW:
  175. irqflags |= IRQF_TRIGGER_FALLING;
  176. break;
  177. default:
  178. irqflags |= IRQF_TRIGGER_RISING |
  179. IRQF_TRIGGER_FALLING;
  180. break;
  181. }
  182. }
  183. event = kzalloc(sizeof(*event), GFP_KERNEL);
  184. if (!event)
  185. goto fail_unlock_irq;
  186. event->handle = evt_handle;
  187. event->irq = irq;
  188. event->pin = pin;
  189. event->desc = desc;
  190. ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
  191. "ACPI:Event", event);
  192. if (ret) {
  193. dev_err(chip->parent,
  194. "Failed to setup interrupt handler for %d\n",
  195. event->irq);
  196. goto fail_free_event;
  197. }
  198. if (agpio->wake_capable == ACPI_WAKE_CAPABLE)
  199. enable_irq_wake(irq);
  200. list_add_tail(&event->node, &acpi_gpio->events);
  201. /*
  202. * Make sure we trigger the initial state of the IRQ when using RISING
  203. * or FALLING. Note we run the handlers on late_init, the AML code
  204. * may refer to OperationRegions from other (builtin) drivers which
  205. * may be probed after us.
  206. */
  207. if (((irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
  208. ((irqflags & IRQF_TRIGGER_FALLING) && value == 0))
  209. handler(event->irq, event);
  210. return AE_OK;
  211. fail_free_event:
  212. kfree(event);
  213. fail_unlock_irq:
  214. gpiochip_unlock_as_irq(chip, pin);
  215. fail_free_desc:
  216. gpiochip_free_own_desc(desc);
  217. return AE_ERROR;
  218. }
  219. /**
  220. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  221. * @chip: GPIO chip
  222. *
  223. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  224. * handled by ACPI event methods which need to be called from the GPIO
  225. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  226. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  227. * the acpi event methods for those pins.
  228. */
  229. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  230. {
  231. struct acpi_gpio_chip *acpi_gpio;
  232. acpi_handle handle;
  233. acpi_status status;
  234. bool defer;
  235. if (!chip->parent || !chip->to_irq)
  236. return;
  237. handle = ACPI_HANDLE(chip->parent);
  238. if (!handle)
  239. return;
  240. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  241. if (ACPI_FAILURE(status))
  242. return;
  243. mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
  244. defer = !acpi_gpio_deferred_req_irqs_done;
  245. if (defer)
  246. list_add(&acpi_gpio->deferred_req_irqs_list_entry,
  247. &acpi_gpio_deferred_req_irqs_list);
  248. mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
  249. if (defer)
  250. return;
  251. acpi_walk_resources(handle, "_AEI",
  252. acpi_gpiochip_request_interrupt, acpi_gpio);
  253. }
  254. EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
  255. /**
  256. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  257. * @chip: GPIO chip
  258. *
  259. * Free interrupts associated with GPIO ACPI event method for the given
  260. * GPIO chip.
  261. */
  262. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  263. {
  264. struct acpi_gpio_chip *acpi_gpio;
  265. struct acpi_gpio_event *event, *ep;
  266. acpi_handle handle;
  267. acpi_status status;
  268. if (!chip->parent || !chip->to_irq)
  269. return;
  270. handle = ACPI_HANDLE(chip->parent);
  271. if (!handle)
  272. return;
  273. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  274. if (ACPI_FAILURE(status))
  275. return;
  276. mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
  277. if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry))
  278. list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
  279. mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
  280. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  281. struct gpio_desc *desc;
  282. if (irqd_is_wakeup_set(irq_get_irq_data(event->irq)))
  283. disable_irq_wake(event->irq);
  284. free_irq(event->irq, event);
  285. desc = event->desc;
  286. if (WARN_ON(IS_ERR(desc)))
  287. continue;
  288. gpiochip_unlock_as_irq(chip, event->pin);
  289. gpiochip_free_own_desc(desc);
  290. list_del(&event->node);
  291. kfree(event);
  292. }
  293. }
  294. EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
  295. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  296. const struct acpi_gpio_mapping *gpios)
  297. {
  298. if (adev && gpios) {
  299. adev->driver_gpios = gpios;
  300. return 0;
  301. }
  302. return -EINVAL;
  303. }
  304. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  305. static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
  306. {
  307. acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
  308. }
  309. int devm_acpi_dev_add_driver_gpios(struct device *dev,
  310. const struct acpi_gpio_mapping *gpios)
  311. {
  312. void *res;
  313. int ret;
  314. res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
  315. if (!res)
  316. return -ENOMEM;
  317. ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
  318. if (ret) {
  319. devres_free(res);
  320. return ret;
  321. }
  322. devres_add(dev, res);
  323. return 0;
  324. }
  325. EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
  326. void devm_acpi_dev_remove_driver_gpios(struct device *dev)
  327. {
  328. WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
  329. }
  330. EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
  331. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  332. const char *name, int index,
  333. struct fwnode_reference_args *args,
  334. unsigned int *quirks)
  335. {
  336. const struct acpi_gpio_mapping *gm;
  337. if (!adev->driver_gpios)
  338. return false;
  339. for (gm = adev->driver_gpios; gm->name; gm++)
  340. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  341. const struct acpi_gpio_params *par = gm->data + index;
  342. args->fwnode = acpi_fwnode_handle(adev);
  343. args->args[0] = par->crs_entry_index;
  344. args->args[1] = par->line_index;
  345. args->args[2] = par->active_low;
  346. args->nargs = 3;
  347. *quirks = gm->quirks;
  348. return true;
  349. }
  350. return false;
  351. }
  352. static enum gpiod_flags
  353. acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
  354. {
  355. bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  356. switch (agpio->io_restriction) {
  357. case ACPI_IO_RESTRICT_INPUT:
  358. return GPIOD_IN;
  359. case ACPI_IO_RESTRICT_OUTPUT:
  360. /*
  361. * ACPI GPIO resources don't contain an initial value for the
  362. * GPIO. Therefore we deduce that value from the pull field
  363. * instead. If the pin is pulled up we assume default to be
  364. * high, otherwise low.
  365. */
  366. return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  367. default:
  368. /*
  369. * Assume that the BIOS has configured the direction and pull
  370. * accordingly.
  371. */
  372. return GPIOD_ASIS;
  373. }
  374. }
  375. static int
  376. __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
  377. {
  378. int ret = 0;
  379. /*
  380. * Check if the BIOS has IoRestriction with explicitly set direction
  381. * and update @flags accordingly. Otherwise use whatever caller asked
  382. * for.
  383. */
  384. if (update & GPIOD_FLAGS_BIT_DIR_SET) {
  385. enum gpiod_flags diff = *flags ^ update;
  386. /*
  387. * Check if caller supplied incompatible GPIO initialization
  388. * flags.
  389. *
  390. * Return %-EINVAL to notify that firmware has different
  391. * settings and we are going to use them.
  392. */
  393. if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
  394. ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
  395. ret = -EINVAL;
  396. *flags = update;
  397. }
  398. return ret;
  399. }
  400. int
  401. acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
  402. {
  403. struct device *dev = &info->adev->dev;
  404. enum gpiod_flags old = *flags;
  405. int ret;
  406. ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
  407. if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
  408. if (ret)
  409. dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
  410. } else {
  411. if (ret)
  412. dev_dbg(dev, "Override GPIO initialization flags\n");
  413. *flags = old;
  414. }
  415. return ret;
  416. }
  417. struct acpi_gpio_lookup {
  418. struct acpi_gpio_info info;
  419. int index;
  420. int pin_index;
  421. bool active_low;
  422. struct gpio_desc *desc;
  423. int n;
  424. };
  425. static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
  426. {
  427. struct acpi_gpio_lookup *lookup = data;
  428. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  429. return 1;
  430. if (lookup->n++ == lookup->index && !lookup->desc) {
  431. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  432. int pin_index = lookup->pin_index;
  433. if (pin_index >= agpio->pin_table_length)
  434. return 1;
  435. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  436. agpio->pin_table[pin_index]);
  437. lookup->info.gpioint =
  438. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  439. /*
  440. * Polarity and triggering are only specified for GpioInt
  441. * resource.
  442. * Note: we expect here:
  443. * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
  444. * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
  445. */
  446. if (lookup->info.gpioint) {
  447. lookup->info.flags = GPIOD_IN;
  448. lookup->info.polarity = agpio->polarity;
  449. lookup->info.triggering = agpio->triggering;
  450. } else {
  451. lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
  452. lookup->info.polarity = lookup->active_low;
  453. }
  454. }
  455. return 1;
  456. }
  457. static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
  458. struct acpi_gpio_info *info)
  459. {
  460. struct acpi_device *adev = lookup->info.adev;
  461. struct list_head res_list;
  462. int ret;
  463. INIT_LIST_HEAD(&res_list);
  464. ret = acpi_dev_get_resources(adev, &res_list,
  465. acpi_populate_gpio_lookup,
  466. lookup);
  467. if (ret < 0)
  468. return ret;
  469. acpi_dev_free_resource_list(&res_list);
  470. if (!lookup->desc)
  471. return -ENOENT;
  472. if (info)
  473. *info = lookup->info;
  474. return 0;
  475. }
  476. static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
  477. const char *propname, int index,
  478. struct acpi_gpio_lookup *lookup)
  479. {
  480. struct fwnode_reference_args args;
  481. unsigned int quirks = 0;
  482. int ret;
  483. memset(&args, 0, sizeof(args));
  484. ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
  485. &args);
  486. if (ret) {
  487. struct acpi_device *adev = to_acpi_device_node(fwnode);
  488. if (!adev)
  489. return ret;
  490. if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
  491. &quirks))
  492. return ret;
  493. }
  494. /*
  495. * The property was found and resolved, so need to lookup the GPIO based
  496. * on returned args.
  497. */
  498. if (!to_acpi_device_node(args.fwnode))
  499. return -EINVAL;
  500. if (args.nargs != 3)
  501. return -EPROTO;
  502. lookup->index = args.args[0];
  503. lookup->pin_index = args.args[1];
  504. lookup->active_low = !!args.args[2];
  505. lookup->info.adev = to_acpi_device_node(args.fwnode);
  506. lookup->info.quirks = quirks;
  507. return 0;
  508. }
  509. /**
  510. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  511. * @adev: pointer to a ACPI device to get GPIO from
  512. * @propname: Property name of the GPIO (optional)
  513. * @index: index of GpioIo/GpioInt resource (starting from %0)
  514. * @info: info pointer to fill in (optional)
  515. *
  516. * Function goes through ACPI resources for @adev and based on @index looks
  517. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  518. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  519. * are total %3 GPIO resources, the index goes from %0 to %2.
  520. *
  521. * If @propname is specified the GPIO is looked using device property. In
  522. * that case @index is used to select the GPIO entry in the property value
  523. * (in case of multiple).
  524. *
  525. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  526. * returned.
  527. *
  528. * Note: if the GPIO resource has multiple entries in the pin list, this
  529. * function only returns the first.
  530. */
  531. static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  532. const char *propname, int index,
  533. struct acpi_gpio_info *info)
  534. {
  535. struct acpi_gpio_lookup lookup;
  536. int ret;
  537. if (!adev)
  538. return ERR_PTR(-ENODEV);
  539. memset(&lookup, 0, sizeof(lookup));
  540. lookup.index = index;
  541. if (propname) {
  542. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  543. ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
  544. propname, index, &lookup);
  545. if (ret)
  546. return ERR_PTR(ret);
  547. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
  548. dev_name(&lookup.info.adev->dev), lookup.index,
  549. lookup.pin_index, lookup.active_low);
  550. } else {
  551. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  552. lookup.info.adev = adev;
  553. }
  554. ret = acpi_gpio_resource_lookup(&lookup, info);
  555. return ret ? ERR_PTR(ret) : lookup.desc;
  556. }
  557. struct gpio_desc *acpi_find_gpio(struct device *dev,
  558. const char *con_id,
  559. unsigned int idx,
  560. enum gpiod_flags *dflags,
  561. enum gpio_lookup_flags *lookupflags)
  562. {
  563. struct acpi_device *adev = ACPI_COMPANION(dev);
  564. struct acpi_gpio_info info;
  565. struct gpio_desc *desc;
  566. char propname[32];
  567. int i;
  568. /* Try first from _DSD */
  569. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  570. if (con_id) {
  571. snprintf(propname, sizeof(propname), "%s-%s",
  572. con_id, gpio_suffixes[i]);
  573. } else {
  574. snprintf(propname, sizeof(propname), "%s",
  575. gpio_suffixes[i]);
  576. }
  577. desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
  578. if (!IS_ERR(desc))
  579. break;
  580. if (PTR_ERR(desc) == -EPROBE_DEFER)
  581. return ERR_CAST(desc);
  582. }
  583. /* Then from plain _CRS GPIOs */
  584. if (IS_ERR(desc)) {
  585. if (!acpi_can_fallback_to_crs(adev, con_id))
  586. return ERR_PTR(-ENOENT);
  587. desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
  588. if (IS_ERR(desc))
  589. return desc;
  590. }
  591. if (info.gpioint &&
  592. (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
  593. dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
  594. return ERR_PTR(-ENOENT);
  595. }
  596. if (info.polarity == GPIO_ACTIVE_LOW)
  597. *lookupflags |= GPIO_ACTIVE_LOW;
  598. acpi_gpio_update_gpiod_flags(dflags, &info);
  599. return desc;
  600. }
  601. /**
  602. * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
  603. * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  604. * @propname: Property name of the GPIO
  605. * @index: index of GpioIo/GpioInt resource (starting from %0)
  606. * @info: info pointer to fill in (optional)
  607. *
  608. * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
  609. * Otherwise (ie. it is a data-only non-device object), use the property-based
  610. * GPIO lookup to get to the GPIO resource with the relevant information and use
  611. * that to obtain the GPIO descriptor to return.
  612. */
  613. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  614. const char *propname, int index,
  615. struct acpi_gpio_info *info)
  616. {
  617. struct acpi_gpio_lookup lookup;
  618. struct acpi_device *adev;
  619. int ret;
  620. adev = to_acpi_device_node(fwnode);
  621. if (adev)
  622. return acpi_get_gpiod_by_index(adev, propname, index, info);
  623. if (!is_acpi_data_node(fwnode))
  624. return ERR_PTR(-ENODEV);
  625. if (!propname)
  626. return ERR_PTR(-EINVAL);
  627. memset(&lookup, 0, sizeof(lookup));
  628. lookup.index = index;
  629. ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
  630. if (ret)
  631. return ERR_PTR(ret);
  632. ret = acpi_gpio_resource_lookup(&lookup, info);
  633. return ret ? ERR_PTR(ret) : lookup.desc;
  634. }
  635. /**
  636. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  637. * @adev: pointer to a ACPI device to get IRQ from
  638. * @index: index of GpioInt resource (starting from %0)
  639. *
  640. * If the device has one or more GpioInt resources, this function can be
  641. * used to translate from the GPIO offset in the resource to the Linux IRQ
  642. * number.
  643. *
  644. * The function is idempotent, though each time it runs it will configure GPIO
  645. * pin direction according to the flags in GpioInt resource.
  646. *
  647. * Return: Linux IRQ number (> %0) on success, negative errno on failure.
  648. */
  649. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  650. {
  651. int idx, i;
  652. unsigned int irq_flags;
  653. int ret;
  654. for (i = 0, idx = 0; idx <= index; i++) {
  655. struct acpi_gpio_info info;
  656. struct gpio_desc *desc;
  657. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  658. /* Ignore -EPROBE_DEFER, it only matters if idx matches */
  659. if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
  660. return PTR_ERR(desc);
  661. if (info.gpioint && idx++ == index) {
  662. char label[32];
  663. int irq;
  664. if (IS_ERR(desc))
  665. return PTR_ERR(desc);
  666. irq = gpiod_to_irq(desc);
  667. if (irq < 0)
  668. return irq;
  669. snprintf(label, sizeof(label), "GpioInt() %d", index);
  670. ret = gpiod_configure_flags(desc, label, 0, info.flags);
  671. if (ret < 0)
  672. return ret;
  673. irq_flags = acpi_dev_get_irq_type(info.triggering,
  674. info.polarity);
  675. /* Set type if specified and different than the current one */
  676. if (irq_flags != IRQ_TYPE_NONE &&
  677. irq_flags != irq_get_trigger_type(irq))
  678. irq_set_irq_type(irq, irq_flags);
  679. return irq;
  680. }
  681. }
  682. return -ENOENT;
  683. }
  684. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  685. static acpi_status
  686. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  687. u32 bits, u64 *value, void *handler_context,
  688. void *region_context)
  689. {
  690. struct acpi_gpio_chip *achip = region_context;
  691. struct gpio_chip *chip = achip->chip;
  692. struct acpi_resource_gpio *agpio;
  693. struct acpi_resource *ares;
  694. int pin_index = (int)address;
  695. acpi_status status;
  696. int length;
  697. int i;
  698. status = acpi_buffer_to_resource(achip->conn_info.connection,
  699. achip->conn_info.length, &ares);
  700. if (ACPI_FAILURE(status))
  701. return status;
  702. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  703. ACPI_FREE(ares);
  704. return AE_BAD_PARAMETER;
  705. }
  706. agpio = &ares->data.gpio;
  707. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  708. function == ACPI_WRITE)) {
  709. ACPI_FREE(ares);
  710. return AE_BAD_PARAMETER;
  711. }
  712. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  713. for (i = pin_index; i < length; ++i) {
  714. int pin = agpio->pin_table[i];
  715. struct acpi_gpio_connection *conn;
  716. struct gpio_desc *desc;
  717. bool found;
  718. mutex_lock(&achip->conn_lock);
  719. found = false;
  720. list_for_each_entry(conn, &achip->conns, node) {
  721. if (conn->pin == pin) {
  722. found = true;
  723. desc = conn->desc;
  724. break;
  725. }
  726. }
  727. /*
  728. * The same GPIO can be shared between operation region and
  729. * event but only if the access here is ACPI_READ. In that
  730. * case we "borrow" the event GPIO instead.
  731. */
  732. if (!found && agpio->sharable == ACPI_SHARED &&
  733. function == ACPI_READ) {
  734. struct acpi_gpio_event *event;
  735. list_for_each_entry(event, &achip->events, node) {
  736. if (event->pin == pin) {
  737. desc = event->desc;
  738. found = true;
  739. break;
  740. }
  741. }
  742. }
  743. if (!found) {
  744. enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
  745. const char *label = "ACPI:OpRegion";
  746. int err;
  747. desc = gpiochip_request_own_desc(chip, pin, label);
  748. if (IS_ERR(desc)) {
  749. status = AE_ERROR;
  750. mutex_unlock(&achip->conn_lock);
  751. goto out;
  752. }
  753. err = gpiod_configure_flags(desc, label, 0, flags);
  754. if (err < 0) {
  755. status = AE_NOT_CONFIGURED;
  756. gpiochip_free_own_desc(desc);
  757. mutex_unlock(&achip->conn_lock);
  758. goto out;
  759. }
  760. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  761. if (!conn) {
  762. status = AE_NO_MEMORY;
  763. gpiochip_free_own_desc(desc);
  764. mutex_unlock(&achip->conn_lock);
  765. goto out;
  766. }
  767. conn->pin = pin;
  768. conn->desc = desc;
  769. list_add_tail(&conn->node, &achip->conns);
  770. }
  771. mutex_unlock(&achip->conn_lock);
  772. if (function == ACPI_WRITE)
  773. gpiod_set_raw_value_cansleep(desc,
  774. !!((1 << i) & *value));
  775. else
  776. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  777. }
  778. out:
  779. ACPI_FREE(ares);
  780. return status;
  781. }
  782. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  783. {
  784. struct gpio_chip *chip = achip->chip;
  785. acpi_handle handle = ACPI_HANDLE(chip->parent);
  786. acpi_status status;
  787. INIT_LIST_HEAD(&achip->conns);
  788. mutex_init(&achip->conn_lock);
  789. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  790. acpi_gpio_adr_space_handler,
  791. NULL, achip);
  792. if (ACPI_FAILURE(status))
  793. dev_err(chip->parent,
  794. "Failed to install GPIO OpRegion handler\n");
  795. }
  796. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  797. {
  798. struct gpio_chip *chip = achip->chip;
  799. acpi_handle handle = ACPI_HANDLE(chip->parent);
  800. struct acpi_gpio_connection *conn, *tmp;
  801. acpi_status status;
  802. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  803. acpi_gpio_adr_space_handler);
  804. if (ACPI_FAILURE(status)) {
  805. dev_err(chip->parent,
  806. "Failed to remove GPIO OpRegion handler\n");
  807. return;
  808. }
  809. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  810. gpiochip_free_own_desc(conn->desc);
  811. list_del(&conn->node);
  812. kfree(conn);
  813. }
  814. }
  815. static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
  816. struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
  817. const char **name, unsigned int *lflags, unsigned int *dflags)
  818. {
  819. struct gpio_chip *chip = achip->chip;
  820. struct gpio_desc *desc;
  821. u32 gpios[2];
  822. int ret;
  823. *lflags = 0;
  824. *dflags = 0;
  825. *name = NULL;
  826. ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
  827. ARRAY_SIZE(gpios));
  828. if (ret < 0)
  829. return ERR_PTR(ret);
  830. desc = gpiochip_get_desc(chip, gpios[0]);
  831. if (IS_ERR(desc))
  832. return desc;
  833. if (gpios[1])
  834. *lflags |= GPIO_ACTIVE_LOW;
  835. if (fwnode_property_present(fwnode, "input"))
  836. *dflags |= GPIOD_IN;
  837. else if (fwnode_property_present(fwnode, "output-low"))
  838. *dflags |= GPIOD_OUT_LOW;
  839. else if (fwnode_property_present(fwnode, "output-high"))
  840. *dflags |= GPIOD_OUT_HIGH;
  841. else
  842. return ERR_PTR(-EINVAL);
  843. fwnode_property_read_string(fwnode, "line-name", name);
  844. return desc;
  845. }
  846. static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
  847. {
  848. struct gpio_chip *chip = achip->chip;
  849. struct fwnode_handle *fwnode;
  850. device_for_each_child_node(chip->parent, fwnode) {
  851. unsigned int lflags, dflags;
  852. struct gpio_desc *desc;
  853. const char *name;
  854. int ret;
  855. if (!fwnode_property_present(fwnode, "gpio-hog"))
  856. continue;
  857. desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
  858. &lflags, &dflags);
  859. if (IS_ERR(desc))
  860. continue;
  861. ret = gpiod_hog(desc, name, lflags, dflags);
  862. if (ret) {
  863. dev_err(chip->parent, "Failed to hog GPIO\n");
  864. fwnode_handle_put(fwnode);
  865. return;
  866. }
  867. }
  868. }
  869. void acpi_gpiochip_add(struct gpio_chip *chip)
  870. {
  871. struct acpi_gpio_chip *acpi_gpio;
  872. acpi_handle handle;
  873. acpi_status status;
  874. if (!chip || !chip->parent)
  875. return;
  876. handle = ACPI_HANDLE(chip->parent);
  877. if (!handle)
  878. return;
  879. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  880. if (!acpi_gpio) {
  881. dev_err(chip->parent,
  882. "Failed to allocate memory for ACPI GPIO chip\n");
  883. return;
  884. }
  885. acpi_gpio->chip = chip;
  886. INIT_LIST_HEAD(&acpi_gpio->events);
  887. INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
  888. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  889. if (ACPI_FAILURE(status)) {
  890. dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
  891. kfree(acpi_gpio);
  892. return;
  893. }
  894. if (!chip->names)
  895. devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
  896. acpi_gpiochip_request_regions(acpi_gpio);
  897. acpi_gpiochip_scan_gpios(acpi_gpio);
  898. acpi_walk_dep_device_list(handle);
  899. }
  900. void acpi_gpiochip_remove(struct gpio_chip *chip)
  901. {
  902. struct acpi_gpio_chip *acpi_gpio;
  903. acpi_handle handle;
  904. acpi_status status;
  905. if (!chip || !chip->parent)
  906. return;
  907. handle = ACPI_HANDLE(chip->parent);
  908. if (!handle)
  909. return;
  910. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  911. if (ACPI_FAILURE(status)) {
  912. dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
  913. return;
  914. }
  915. acpi_gpiochip_free_regions(acpi_gpio);
  916. acpi_detach_data(handle, acpi_gpio_chip_dh);
  917. kfree(acpi_gpio);
  918. }
  919. static int acpi_gpio_package_count(const union acpi_object *obj)
  920. {
  921. const union acpi_object *element = obj->package.elements;
  922. const union acpi_object *end = element + obj->package.count;
  923. unsigned int count = 0;
  924. while (element < end) {
  925. switch (element->type) {
  926. case ACPI_TYPE_LOCAL_REFERENCE:
  927. element += 3;
  928. /* Fallthrough */
  929. case ACPI_TYPE_INTEGER:
  930. element++;
  931. count++;
  932. break;
  933. default:
  934. return -EPROTO;
  935. }
  936. }
  937. return count;
  938. }
  939. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  940. {
  941. unsigned int *count = data;
  942. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  943. *count += ares->data.gpio.pin_table_length;
  944. return 1;
  945. }
  946. /**
  947. * acpi_gpio_count - return the number of GPIOs associated with a
  948. * device / function or -ENOENT if no GPIO has been
  949. * assigned to the requested function.
  950. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  951. * @con_id: function within the GPIO consumer
  952. */
  953. int acpi_gpio_count(struct device *dev, const char *con_id)
  954. {
  955. struct acpi_device *adev = ACPI_COMPANION(dev);
  956. const union acpi_object *obj;
  957. const struct acpi_gpio_mapping *gm;
  958. int count = -ENOENT;
  959. int ret;
  960. char propname[32];
  961. unsigned int i;
  962. /* Try first from _DSD */
  963. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  964. if (con_id)
  965. snprintf(propname, sizeof(propname), "%s-%s",
  966. con_id, gpio_suffixes[i]);
  967. else
  968. snprintf(propname, sizeof(propname), "%s",
  969. gpio_suffixes[i]);
  970. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  971. &obj);
  972. if (ret == 0) {
  973. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  974. count = 1;
  975. else if (obj->type == ACPI_TYPE_PACKAGE)
  976. count = acpi_gpio_package_count(obj);
  977. } else if (adev->driver_gpios) {
  978. for (gm = adev->driver_gpios; gm->name; gm++)
  979. if (strcmp(propname, gm->name) == 0) {
  980. count = gm->size;
  981. break;
  982. }
  983. }
  984. if (count > 0)
  985. break;
  986. }
  987. /* Then from plain _CRS GPIOs */
  988. if (count < 0) {
  989. struct list_head resource_list;
  990. unsigned int crs_count = 0;
  991. if (!acpi_can_fallback_to_crs(adev, con_id))
  992. return count;
  993. INIT_LIST_HEAD(&resource_list);
  994. acpi_dev_get_resources(adev, &resource_list,
  995. acpi_find_gpio_count, &crs_count);
  996. acpi_dev_free_resource_list(&resource_list);
  997. if (crs_count > 0)
  998. count = crs_count;
  999. }
  1000. return count ? count : -ENOENT;
  1001. }
  1002. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
  1003. {
  1004. /* Never allow fallback if the device has properties */
  1005. if (adev->data.properties || adev->driver_gpios)
  1006. return false;
  1007. return con_id == NULL;
  1008. }
  1009. /* Run deferred acpi_gpiochip_request_interrupts() */
  1010. static int acpi_gpio_handle_deferred_request_interrupts(void)
  1011. {
  1012. struct acpi_gpio_chip *acpi_gpio, *tmp;
  1013. mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
  1014. list_for_each_entry_safe(acpi_gpio, tmp,
  1015. &acpi_gpio_deferred_req_irqs_list,
  1016. deferred_req_irqs_list_entry) {
  1017. acpi_handle handle;
  1018. handle = ACPI_HANDLE(acpi_gpio->chip->parent);
  1019. acpi_walk_resources(handle, "_AEI",
  1020. acpi_gpiochip_request_interrupt, acpi_gpio);
  1021. list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
  1022. }
  1023. acpi_gpio_deferred_req_irqs_done = true;
  1024. mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
  1025. return 0;
  1026. }
  1027. /* We must use _sync so that this runs after the first deferred_probe run */
  1028. late_initcall_sync(acpi_gpio_handle_deferred_request_interrupts);