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