gpiolib-acpi.c 30 KB

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