gpiolib-acpi.c 31 KB

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