gpiolib-acpi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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%02X",
  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. {
  354. const struct acpi_gpio_mapping *gm;
  355. if (!adev->driver_gpios)
  356. return false;
  357. for (gm = adev->driver_gpios; gm->name; gm++)
  358. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  359. const struct acpi_gpio_params *par = gm->data + index;
  360. args->adev = adev;
  361. args->args[0] = par->crs_entry_index;
  362. args->args[1] = par->line_index;
  363. args->args[2] = par->active_low;
  364. args->nargs = 3;
  365. return true;
  366. }
  367. return false;
  368. }
  369. static enum gpiod_flags
  370. acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
  371. {
  372. bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  373. switch (agpio->io_restriction) {
  374. case ACPI_IO_RESTRICT_INPUT:
  375. return GPIOD_IN;
  376. case ACPI_IO_RESTRICT_OUTPUT:
  377. /*
  378. * ACPI GPIO resources don't contain an initial value for the
  379. * GPIO. Therefore we deduce that value from the pull field
  380. * instead. If the pin is pulled up we assume default to be
  381. * high, otherwise low.
  382. */
  383. return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  384. default:
  385. /*
  386. * Assume that the BIOS has configured the direction and pull
  387. * accordingly.
  388. */
  389. return GPIOD_ASIS;
  390. }
  391. }
  392. int
  393. acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
  394. {
  395. int ret = 0;
  396. /*
  397. * Check if the BIOS has IoRestriction with explicitly set direction
  398. * and update @flags accordingly. Otherwise use whatever caller asked
  399. * for.
  400. */
  401. if (update & GPIOD_FLAGS_BIT_DIR_SET) {
  402. enum gpiod_flags diff = *flags ^ update;
  403. /*
  404. * Check if caller supplied incompatible GPIO initialization
  405. * flags.
  406. *
  407. * Return %-EINVAL to notify that firmware has different
  408. * settings and we are going to use them.
  409. */
  410. if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
  411. ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
  412. ret = -EINVAL;
  413. *flags = update;
  414. }
  415. return ret;
  416. }
  417. struct acpi_gpio_lookup {
  418. struct acpi_gpio_info info;
  419. int index;
  420. int pin_index;
  421. bool active_low;
  422. struct acpi_device *adev;
  423. struct gpio_desc *desc;
  424. int n;
  425. };
  426. static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
  427. {
  428. struct acpi_gpio_lookup *lookup = data;
  429. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  430. return 1;
  431. if (lookup->n++ == lookup->index && !lookup->desc) {
  432. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  433. int pin_index = lookup->pin_index;
  434. if (pin_index >= agpio->pin_table_length)
  435. return 1;
  436. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  437. agpio->pin_table[pin_index]);
  438. lookup->info.gpioint =
  439. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  440. /*
  441. * Polarity and triggering are only specified for GpioInt
  442. * resource.
  443. * Note: we expect here:
  444. * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
  445. * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
  446. */
  447. if (lookup->info.gpioint) {
  448. lookup->info.flags = GPIOD_IN;
  449. lookup->info.polarity = agpio->polarity;
  450. lookup->info.triggering = agpio->triggering;
  451. } else {
  452. lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
  453. }
  454. }
  455. return 1;
  456. }
  457. static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
  458. struct acpi_gpio_info *info)
  459. {
  460. struct list_head res_list;
  461. int ret;
  462. INIT_LIST_HEAD(&res_list);
  463. ret = acpi_dev_get_resources(lookup->adev, &res_list,
  464. acpi_populate_gpio_lookup,
  465. lookup);
  466. if (ret < 0)
  467. return ret;
  468. acpi_dev_free_resource_list(&res_list);
  469. if (!lookup->desc)
  470. return -ENOENT;
  471. if (info) {
  472. *info = lookup->info;
  473. if (lookup->active_low)
  474. info->polarity = lookup->active_low;
  475. }
  476. return 0;
  477. }
  478. static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
  479. const char *propname, int index,
  480. struct acpi_gpio_lookup *lookup)
  481. {
  482. struct acpi_reference_args args;
  483. int ret;
  484. memset(&args, 0, sizeof(args));
  485. ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
  486. &args);
  487. if (ret) {
  488. struct acpi_device *adev = to_acpi_device_node(fwnode);
  489. if (!adev)
  490. return ret;
  491. if (!acpi_get_driver_gpio_data(adev, propname, index, &args))
  492. return ret;
  493. }
  494. /*
  495. * The property was found and resolved, so need to lookup the GPIO based
  496. * on returned args.
  497. */
  498. lookup->adev = args.adev;
  499. if (args.nargs != 3)
  500. return -EPROTO;
  501. lookup->index = args.args[0];
  502. lookup->pin_index = args.args[1];
  503. lookup->active_low = !!args.args[2];
  504. return 0;
  505. }
  506. /**
  507. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  508. * @adev: pointer to a ACPI device to get GPIO from
  509. * @propname: Property name of the GPIO (optional)
  510. * @index: index of GpioIo/GpioInt resource (starting from %0)
  511. * @info: info pointer to fill in (optional)
  512. *
  513. * Function goes through ACPI resources for @adev and based on @index looks
  514. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  515. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  516. * are total %3 GPIO resources, the index goes from %0 to %2.
  517. *
  518. * If @propname is specified the GPIO is looked using device property. In
  519. * that case @index is used to select the GPIO entry in the property value
  520. * (in case of multiple).
  521. *
  522. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  523. * returned.
  524. *
  525. * Note: if the GPIO resource has multiple entries in the pin list, this
  526. * function only returns the first.
  527. */
  528. static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  529. const char *propname, int index,
  530. struct acpi_gpio_info *info)
  531. {
  532. struct acpi_gpio_lookup lookup;
  533. int ret;
  534. if (!adev)
  535. return ERR_PTR(-ENODEV);
  536. memset(&lookup, 0, sizeof(lookup));
  537. lookup.index = index;
  538. if (propname) {
  539. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  540. ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
  541. propname, index, &lookup);
  542. if (ret)
  543. return ERR_PTR(ret);
  544. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
  545. dev_name(&lookup.adev->dev), lookup.index,
  546. lookup.pin_index, lookup.active_low);
  547. } else {
  548. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  549. lookup.adev = adev;
  550. }
  551. ret = acpi_gpio_resource_lookup(&lookup, info);
  552. return ret ? ERR_PTR(ret) : lookup.desc;
  553. }
  554. struct gpio_desc *acpi_find_gpio(struct device *dev,
  555. const char *con_id,
  556. unsigned int idx,
  557. enum gpiod_flags *dflags,
  558. enum gpio_lookup_flags *lookupflags)
  559. {
  560. struct acpi_device *adev = ACPI_COMPANION(dev);
  561. struct acpi_gpio_info info;
  562. struct gpio_desc *desc;
  563. char propname[32];
  564. int err;
  565. int i;
  566. /* Try first from _DSD */
  567. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  568. if (con_id) {
  569. snprintf(propname, sizeof(propname), "%s-%s",
  570. con_id, gpio_suffixes[i]);
  571. } else {
  572. snprintf(propname, sizeof(propname), "%s",
  573. gpio_suffixes[i]);
  574. }
  575. desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
  576. if (!IS_ERR(desc))
  577. break;
  578. if (PTR_ERR(desc) == -EPROBE_DEFER)
  579. return ERR_CAST(desc);
  580. }
  581. /* Then from plain _CRS GPIOs */
  582. if (IS_ERR(desc)) {
  583. if (!acpi_can_fallback_to_crs(adev, con_id))
  584. return ERR_PTR(-ENOENT);
  585. desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
  586. if (IS_ERR(desc))
  587. return desc;
  588. }
  589. if (info.gpioint &&
  590. (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
  591. dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
  592. return ERR_PTR(-ENOENT);
  593. }
  594. if (info.polarity == GPIO_ACTIVE_LOW)
  595. *lookupflags |= GPIO_ACTIVE_LOW;
  596. err = acpi_gpio_update_gpiod_flags(dflags, info.flags);
  597. if (err)
  598. dev_dbg(dev, "Override GPIO initialization flags\n");
  599. return desc;
  600. }
  601. /**
  602. * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
  603. * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  604. * @propname: Property name of the GPIO
  605. * @index: index of GpioIo/GpioInt resource (starting from %0)
  606. * @info: info pointer to fill in (optional)
  607. *
  608. * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
  609. * Otherwise (ie. it is a data-only non-device object), use the property-based
  610. * GPIO lookup to get to the GPIO resource with the relevant information and use
  611. * that to obtain the GPIO descriptor to return.
  612. */
  613. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  614. const char *propname, int index,
  615. struct acpi_gpio_info *info)
  616. {
  617. struct acpi_gpio_lookup lookup;
  618. struct acpi_device *adev;
  619. int ret;
  620. adev = to_acpi_device_node(fwnode);
  621. if (adev)
  622. return acpi_get_gpiod_by_index(adev, propname, index, info);
  623. if (!is_acpi_data_node(fwnode))
  624. return ERR_PTR(-ENODEV);
  625. if (!propname)
  626. return ERR_PTR(-EINVAL);
  627. memset(&lookup, 0, sizeof(lookup));
  628. lookup.index = index;
  629. ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
  630. if (ret)
  631. return ERR_PTR(ret);
  632. ret = acpi_gpio_resource_lookup(&lookup, info);
  633. return ret ? ERR_PTR(ret) : lookup.desc;
  634. }
  635. /**
  636. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  637. * @adev: pointer to a ACPI device to get IRQ from
  638. * @index: index of GpioInt resource (starting from %0)
  639. *
  640. * If the device has one or more GpioInt resources, this function can be
  641. * used to translate from the GPIO offset in the resource to the Linux IRQ
  642. * number.
  643. *
  644. * The function is idempotent, though each time it runs it will configure GPIO
  645. * pin direction according to the flags in GpioInt resource.
  646. *
  647. * Return: Linux IRQ number (> %0) on success, negative errno on failure.
  648. */
  649. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  650. {
  651. int idx, i;
  652. unsigned int irq_flags;
  653. int ret;
  654. for (i = 0, idx = 0; idx <= index; i++) {
  655. struct acpi_gpio_info info;
  656. struct gpio_desc *desc;
  657. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  658. /* Ignore -EPROBE_DEFER, it only matters if idx matches */
  659. if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
  660. return PTR_ERR(desc);
  661. if (info.gpioint && idx++ == index) {
  662. char label[32];
  663. int irq;
  664. if (IS_ERR(desc))
  665. return PTR_ERR(desc);
  666. irq = gpiod_to_irq(desc);
  667. if (irq < 0)
  668. return irq;
  669. snprintf(label, sizeof(label), "GpioInt() %d", index);
  670. ret = gpiod_configure_flags(desc, label, 0, info.flags);
  671. if (ret < 0)
  672. return ret;
  673. irq_flags = acpi_dev_get_irq_type(info.triggering,
  674. info.polarity);
  675. /* Set type if specified and different than the current one */
  676. if (irq_flags != IRQ_TYPE_NONE &&
  677. irq_flags != irq_get_trigger_type(irq))
  678. irq_set_irq_type(irq, irq_flags);
  679. return irq;
  680. }
  681. }
  682. return -ENOENT;
  683. }
  684. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  685. static acpi_status
  686. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  687. u32 bits, u64 *value, void *handler_context,
  688. void *region_context)
  689. {
  690. struct acpi_gpio_chip *achip = region_context;
  691. struct gpio_chip *chip = achip->chip;
  692. struct acpi_resource_gpio *agpio;
  693. struct acpi_resource *ares;
  694. int pin_index = (int)address;
  695. acpi_status status;
  696. int length;
  697. int i;
  698. status = acpi_buffer_to_resource(achip->conn_info.connection,
  699. achip->conn_info.length, &ares);
  700. if (ACPI_FAILURE(status))
  701. return status;
  702. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  703. ACPI_FREE(ares);
  704. return AE_BAD_PARAMETER;
  705. }
  706. agpio = &ares->data.gpio;
  707. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  708. function == ACPI_WRITE)) {
  709. ACPI_FREE(ares);
  710. return AE_BAD_PARAMETER;
  711. }
  712. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  713. for (i = pin_index; i < length; ++i) {
  714. int pin = agpio->pin_table[i];
  715. struct acpi_gpio_connection *conn;
  716. struct gpio_desc *desc;
  717. bool found;
  718. pin = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin);
  719. if (pin < 0) {
  720. status = AE_BAD_PARAMETER;
  721. goto out;
  722. }
  723. mutex_lock(&achip->conn_lock);
  724. found = false;
  725. list_for_each_entry(conn, &achip->conns, node) {
  726. if (conn->pin == pin) {
  727. found = true;
  728. desc = conn->desc;
  729. break;
  730. }
  731. }
  732. /*
  733. * The same GPIO can be shared between operation region and
  734. * event but only if the access here is ACPI_READ. In that
  735. * case we "borrow" the event GPIO instead.
  736. */
  737. if (!found && agpio->sharable == ACPI_SHARED &&
  738. function == ACPI_READ) {
  739. struct acpi_gpio_event *event;
  740. list_for_each_entry(event, &achip->events, node) {
  741. if (event->pin == pin) {
  742. desc = event->desc;
  743. found = true;
  744. break;
  745. }
  746. }
  747. }
  748. if (!found) {
  749. enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
  750. const char *label = "ACPI:OpRegion";
  751. int err;
  752. desc = gpiochip_request_own_desc(chip, pin, label);
  753. if (IS_ERR(desc)) {
  754. status = AE_ERROR;
  755. mutex_unlock(&achip->conn_lock);
  756. goto out;
  757. }
  758. err = gpiod_configure_flags(desc, label, 0, flags);
  759. if (err < 0) {
  760. status = AE_NOT_CONFIGURED;
  761. gpiochip_free_own_desc(desc);
  762. mutex_unlock(&achip->conn_lock);
  763. goto out;
  764. }
  765. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  766. if (!conn) {
  767. status = AE_NO_MEMORY;
  768. gpiochip_free_own_desc(desc);
  769. mutex_unlock(&achip->conn_lock);
  770. goto out;
  771. }
  772. conn->pin = pin;
  773. conn->desc = desc;
  774. list_add_tail(&conn->node, &achip->conns);
  775. }
  776. mutex_unlock(&achip->conn_lock);
  777. if (function == ACPI_WRITE)
  778. gpiod_set_raw_value_cansleep(desc,
  779. !!((1 << i) & *value));
  780. else
  781. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  782. }
  783. out:
  784. ACPI_FREE(ares);
  785. return status;
  786. }
  787. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  788. {
  789. struct gpio_chip *chip = achip->chip;
  790. acpi_handle handle = ACPI_HANDLE(chip->parent);
  791. acpi_status status;
  792. INIT_LIST_HEAD(&achip->conns);
  793. mutex_init(&achip->conn_lock);
  794. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  795. acpi_gpio_adr_space_handler,
  796. NULL, achip);
  797. if (ACPI_FAILURE(status))
  798. dev_err(chip->parent,
  799. "Failed to install GPIO OpRegion handler\n");
  800. }
  801. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  802. {
  803. struct gpio_chip *chip = achip->chip;
  804. acpi_handle handle = ACPI_HANDLE(chip->parent);
  805. struct acpi_gpio_connection *conn, *tmp;
  806. acpi_status status;
  807. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  808. acpi_gpio_adr_space_handler);
  809. if (ACPI_FAILURE(status)) {
  810. dev_err(chip->parent,
  811. "Failed to remove GPIO OpRegion handler\n");
  812. return;
  813. }
  814. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  815. gpiochip_free_own_desc(conn->desc);
  816. list_del(&conn->node);
  817. kfree(conn);
  818. }
  819. }
  820. static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
  821. struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
  822. const char **name, unsigned int *lflags, unsigned int *dflags)
  823. {
  824. struct gpio_chip *chip = achip->chip;
  825. struct gpio_desc *desc;
  826. u32 gpios[2];
  827. int ret;
  828. *lflags = 0;
  829. *dflags = 0;
  830. *name = NULL;
  831. ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
  832. ARRAY_SIZE(gpios));
  833. if (ret < 0)
  834. return ERR_PTR(ret);
  835. ret = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, gpios[0]);
  836. if (ret < 0)
  837. return ERR_PTR(ret);
  838. desc = gpiochip_get_desc(chip, ret);
  839. if (IS_ERR(desc))
  840. return desc;
  841. if (gpios[1])
  842. *lflags |= GPIO_ACTIVE_LOW;
  843. if (fwnode_property_present(fwnode, "input"))
  844. *dflags |= GPIOD_IN;
  845. else if (fwnode_property_present(fwnode, "output-low"))
  846. *dflags |= GPIOD_OUT_LOW;
  847. else if (fwnode_property_present(fwnode, "output-high"))
  848. *dflags |= GPIOD_OUT_HIGH;
  849. else
  850. return ERR_PTR(-EINVAL);
  851. fwnode_property_read_string(fwnode, "line-name", name);
  852. return desc;
  853. }
  854. static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
  855. {
  856. struct gpio_chip *chip = achip->chip;
  857. struct fwnode_handle *fwnode;
  858. device_for_each_child_node(chip->parent, fwnode) {
  859. unsigned int lflags, dflags;
  860. struct gpio_desc *desc;
  861. const char *name;
  862. int ret;
  863. if (!fwnode_property_present(fwnode, "gpio-hog"))
  864. continue;
  865. desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
  866. &lflags, &dflags);
  867. if (IS_ERR(desc))
  868. continue;
  869. ret = gpiod_hog(desc, name, lflags, dflags);
  870. if (ret) {
  871. dev_err(chip->parent, "Failed to hog GPIO\n");
  872. fwnode_handle_put(fwnode);
  873. return;
  874. }
  875. }
  876. }
  877. void acpi_gpiochip_add(struct gpio_chip *chip)
  878. {
  879. struct acpi_gpio_chip *acpi_gpio;
  880. acpi_handle handle;
  881. acpi_status status;
  882. if (!chip || !chip->parent)
  883. return;
  884. handle = ACPI_HANDLE(chip->parent);
  885. if (!handle)
  886. return;
  887. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  888. if (!acpi_gpio) {
  889. dev_err(chip->parent,
  890. "Failed to allocate memory for ACPI GPIO chip\n");
  891. return;
  892. }
  893. acpi_gpio->chip = chip;
  894. INIT_LIST_HEAD(&acpi_gpio->events);
  895. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  896. if (ACPI_FAILURE(status)) {
  897. dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
  898. kfree(acpi_gpio);
  899. return;
  900. }
  901. if (!chip->names)
  902. devprop_gpiochip_set_names(chip);
  903. acpi_gpiochip_request_regions(acpi_gpio);
  904. acpi_gpiochip_scan_gpios(acpi_gpio);
  905. acpi_walk_dep_device_list(handle);
  906. }
  907. void acpi_gpiochip_remove(struct gpio_chip *chip)
  908. {
  909. struct acpi_gpio_chip *acpi_gpio;
  910. acpi_handle handle;
  911. acpi_status status;
  912. if (!chip || !chip->parent)
  913. return;
  914. handle = ACPI_HANDLE(chip->parent);
  915. if (!handle)
  916. return;
  917. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  918. if (ACPI_FAILURE(status)) {
  919. dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
  920. return;
  921. }
  922. acpi_gpiochip_free_regions(acpi_gpio);
  923. acpi_detach_data(handle, acpi_gpio_chip_dh);
  924. kfree(acpi_gpio);
  925. }
  926. static int acpi_gpio_package_count(const union acpi_object *obj)
  927. {
  928. const union acpi_object *element = obj->package.elements;
  929. const union acpi_object *end = element + obj->package.count;
  930. unsigned int count = 0;
  931. while (element < end) {
  932. switch (element->type) {
  933. case ACPI_TYPE_LOCAL_REFERENCE:
  934. element += 3;
  935. /* Fallthrough */
  936. case ACPI_TYPE_INTEGER:
  937. element++;
  938. count++;
  939. break;
  940. default:
  941. return -EPROTO;
  942. }
  943. }
  944. return count;
  945. }
  946. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  947. {
  948. unsigned int *count = data;
  949. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  950. *count += ares->data.gpio.pin_table_length;
  951. return 1;
  952. }
  953. /**
  954. * acpi_gpio_count - return the number of GPIOs associated with a
  955. * device / function or -ENOENT if no GPIO has been
  956. * assigned to the requested function.
  957. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  958. * @con_id: function within the GPIO consumer
  959. */
  960. int acpi_gpio_count(struct device *dev, const char *con_id)
  961. {
  962. struct acpi_device *adev = ACPI_COMPANION(dev);
  963. const union acpi_object *obj;
  964. const struct acpi_gpio_mapping *gm;
  965. int count = -ENOENT;
  966. int ret;
  967. char propname[32];
  968. unsigned int i;
  969. /* Try first from _DSD */
  970. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  971. if (con_id)
  972. snprintf(propname, sizeof(propname), "%s-%s",
  973. con_id, gpio_suffixes[i]);
  974. else
  975. snprintf(propname, sizeof(propname), "%s",
  976. gpio_suffixes[i]);
  977. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  978. &obj);
  979. if (ret == 0) {
  980. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  981. count = 1;
  982. else if (obj->type == ACPI_TYPE_PACKAGE)
  983. count = acpi_gpio_package_count(obj);
  984. } else if (adev->driver_gpios) {
  985. for (gm = adev->driver_gpios; gm->name; gm++)
  986. if (strcmp(propname, gm->name) == 0) {
  987. count = gm->size;
  988. break;
  989. }
  990. }
  991. if (count > 0)
  992. break;
  993. }
  994. /* Then from plain _CRS GPIOs */
  995. if (count < 0) {
  996. struct list_head resource_list;
  997. unsigned int crs_count = 0;
  998. if (!acpi_can_fallback_to_crs(adev, con_id))
  999. return count;
  1000. INIT_LIST_HEAD(&resource_list);
  1001. acpi_dev_get_resources(adev, &resource_list,
  1002. acpi_find_gpio_count, &crs_count);
  1003. acpi_dev_free_resource_list(&resource_list);
  1004. if (crs_count > 0)
  1005. count = crs_count;
  1006. }
  1007. return count ? count : -ENOENT;
  1008. }
  1009. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
  1010. {
  1011. /* Never allow fallback if the device has properties */
  1012. if (adev->data.properties || adev->driver_gpios)
  1013. return false;
  1014. return con_id == NULL;
  1015. }