gpiolib-acpi.c 28 KB

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