gpiolib-acpi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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, &args);
  396. if (ret) {
  397. struct acpi_device *adev = to_acpi_device_node(fwnode);
  398. if (!adev)
  399. return ret;
  400. if (!acpi_get_driver_gpio_data(adev, propname, index, &args))
  401. return ret;
  402. }
  403. /*
  404. * The property was found and resolved, so need to lookup the GPIO based
  405. * on returned args.
  406. */
  407. lookup->adev = args.adev;
  408. if (args.nargs >= 2) {
  409. lookup->index = args.args[0];
  410. lookup->pin_index = args.args[1];
  411. /* 3rd argument, if present is used to specify active_low. */
  412. if (args.nargs >= 3)
  413. lookup->active_low = !!args.args[2];
  414. }
  415. return 0;
  416. }
  417. /**
  418. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  419. * @adev: pointer to a ACPI device to get GPIO from
  420. * @propname: Property name of the GPIO (optional)
  421. * @index: index of GpioIo/GpioInt resource (starting from %0)
  422. * @info: info pointer to fill in (optional)
  423. *
  424. * Function goes through ACPI resources for @adev and based on @index looks
  425. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  426. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  427. * are total %3 GPIO resources, the index goes from %0 to %2.
  428. *
  429. * If @propname is specified the GPIO is looked using device property. In
  430. * that case @index is used to select the GPIO entry in the property value
  431. * (in case of multiple).
  432. *
  433. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  434. * returned.
  435. *
  436. * Note: if the GPIO resource has multiple entries in the pin list, this
  437. * function only returns the first.
  438. */
  439. static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  440. const char *propname, int index,
  441. struct acpi_gpio_info *info)
  442. {
  443. struct acpi_gpio_lookup lookup;
  444. int ret;
  445. if (!adev)
  446. return ERR_PTR(-ENODEV);
  447. memset(&lookup, 0, sizeof(lookup));
  448. lookup.index = index;
  449. if (propname) {
  450. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  451. ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
  452. propname, index, &lookup);
  453. if (ret)
  454. return ERR_PTR(ret);
  455. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
  456. dev_name(&lookup.adev->dev), lookup.index,
  457. lookup.pin_index, lookup.active_low);
  458. } else {
  459. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  460. lookup.adev = adev;
  461. }
  462. ret = acpi_gpio_resource_lookup(&lookup, info);
  463. return ret ? ERR_PTR(ret) : lookup.desc;
  464. }
  465. struct gpio_desc *acpi_find_gpio(struct device *dev,
  466. const char *con_id,
  467. unsigned int idx,
  468. enum gpiod_flags flags,
  469. enum gpio_lookup_flags *lookupflags)
  470. {
  471. struct acpi_device *adev = ACPI_COMPANION(dev);
  472. struct acpi_gpio_info info;
  473. struct gpio_desc *desc;
  474. char propname[32];
  475. int i;
  476. /* Try first from _DSD */
  477. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  478. if (con_id && strcmp(con_id, "gpios")) {
  479. snprintf(propname, sizeof(propname), "%s-%s",
  480. con_id, gpio_suffixes[i]);
  481. } else {
  482. snprintf(propname, sizeof(propname), "%s",
  483. gpio_suffixes[i]);
  484. }
  485. desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
  486. if (!IS_ERR(desc))
  487. break;
  488. if (PTR_ERR(desc) == -EPROBE_DEFER)
  489. return ERR_CAST(desc);
  490. }
  491. /* Then from plain _CRS GPIOs */
  492. if (IS_ERR(desc)) {
  493. if (!acpi_can_fallback_to_crs(adev, con_id))
  494. return ERR_PTR(-ENOENT);
  495. desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
  496. if (IS_ERR(desc))
  497. return desc;
  498. if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
  499. info.gpioint) {
  500. dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
  501. return ERR_PTR(-ENOENT);
  502. }
  503. }
  504. if (info.polarity == GPIO_ACTIVE_LOW)
  505. *lookupflags |= GPIO_ACTIVE_LOW;
  506. return desc;
  507. }
  508. /**
  509. * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
  510. * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  511. * @propname: Property name of the GPIO
  512. * @index: index of GpioIo/GpioInt resource (starting from %0)
  513. * @info: info pointer to fill in (optional)
  514. *
  515. * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
  516. * Otherwise (ie. it is a data-only non-device object), use the property-based
  517. * GPIO lookup to get to the GPIO resource with the relevant information and use
  518. * that to obtain the GPIO descriptor to return.
  519. */
  520. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  521. const char *propname, int index,
  522. struct acpi_gpio_info *info)
  523. {
  524. struct acpi_gpio_lookup lookup;
  525. struct acpi_device *adev;
  526. int ret;
  527. adev = to_acpi_device_node(fwnode);
  528. if (adev)
  529. return acpi_get_gpiod_by_index(adev, propname, index, info);
  530. if (!is_acpi_data_node(fwnode))
  531. return ERR_PTR(-ENODEV);
  532. if (!propname)
  533. return ERR_PTR(-EINVAL);
  534. memset(&lookup, 0, sizeof(lookup));
  535. lookup.index = index;
  536. ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
  537. if (ret)
  538. return ERR_PTR(ret);
  539. ret = acpi_gpio_resource_lookup(&lookup, info);
  540. return ret ? ERR_PTR(ret) : lookup.desc;
  541. }
  542. /**
  543. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  544. * @adev: pointer to a ACPI device to get IRQ from
  545. * @index: index of GpioInt resource (starting from %0)
  546. *
  547. * If the device has one or more GpioInt resources, this function can be
  548. * used to translate from the GPIO offset in the resource to the Linux IRQ
  549. * number.
  550. *
  551. * Return: Linux IRQ number (>%0) on success, negative errno on failure.
  552. */
  553. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  554. {
  555. int idx, i;
  556. unsigned int irq_flags;
  557. int ret = -ENOENT;
  558. for (i = 0, idx = 0; idx <= index; i++) {
  559. struct acpi_gpio_info info;
  560. struct gpio_desc *desc;
  561. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  562. if (IS_ERR(desc)) {
  563. ret = PTR_ERR(desc);
  564. break;
  565. }
  566. if (info.gpioint && idx++ == index) {
  567. int irq = gpiod_to_irq(desc);
  568. if (irq < 0)
  569. return irq;
  570. irq_flags = acpi_dev_get_irq_type(info.triggering,
  571. info.polarity);
  572. /* Set type if specified and different than the current one */
  573. if (irq_flags != IRQ_TYPE_NONE &&
  574. irq_flags != irq_get_trigger_type(irq))
  575. irq_set_irq_type(irq, irq_flags);
  576. return irq;
  577. }
  578. }
  579. return ret;
  580. }
  581. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  582. static acpi_status
  583. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  584. u32 bits, u64 *value, void *handler_context,
  585. void *region_context)
  586. {
  587. struct acpi_gpio_chip *achip = region_context;
  588. struct gpio_chip *chip = achip->chip;
  589. struct acpi_resource_gpio *agpio;
  590. struct acpi_resource *ares;
  591. int pin_index = (int)address;
  592. acpi_status status;
  593. bool pull_up;
  594. int length;
  595. int i;
  596. status = acpi_buffer_to_resource(achip->conn_info.connection,
  597. achip->conn_info.length, &ares);
  598. if (ACPI_FAILURE(status))
  599. return status;
  600. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  601. ACPI_FREE(ares);
  602. return AE_BAD_PARAMETER;
  603. }
  604. agpio = &ares->data.gpio;
  605. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  606. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  607. function == ACPI_WRITE)) {
  608. ACPI_FREE(ares);
  609. return AE_BAD_PARAMETER;
  610. }
  611. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  612. for (i = pin_index; i < length; ++i) {
  613. int pin = agpio->pin_table[i];
  614. struct acpi_gpio_connection *conn;
  615. struct gpio_desc *desc;
  616. bool found;
  617. pin = acpi_gpiochip_pin_to_gpio_offset(chip->gpiodev, pin);
  618. if (pin < 0) {
  619. status = AE_BAD_PARAMETER;
  620. goto out;
  621. }
  622. mutex_lock(&achip->conn_lock);
  623. found = false;
  624. list_for_each_entry(conn, &achip->conns, node) {
  625. if (conn->pin == pin) {
  626. found = true;
  627. desc = conn->desc;
  628. break;
  629. }
  630. }
  631. /*
  632. * The same GPIO can be shared between operation region and
  633. * event but only if the access here is ACPI_READ. In that
  634. * case we "borrow" the event GPIO instead.
  635. */
  636. if (!found && agpio->sharable == ACPI_SHARED &&
  637. function == ACPI_READ) {
  638. struct acpi_gpio_event *event;
  639. list_for_each_entry(event, &achip->events, node) {
  640. if (event->pin == pin) {
  641. desc = event->desc;
  642. found = true;
  643. break;
  644. }
  645. }
  646. }
  647. if (!found) {
  648. desc = gpiochip_request_own_desc(chip, pin,
  649. "ACPI:OpRegion");
  650. if (IS_ERR(desc)) {
  651. status = AE_ERROR;
  652. mutex_unlock(&achip->conn_lock);
  653. goto out;
  654. }
  655. switch (agpio->io_restriction) {
  656. case ACPI_IO_RESTRICT_INPUT:
  657. gpiod_direction_input(desc);
  658. break;
  659. case ACPI_IO_RESTRICT_OUTPUT:
  660. /*
  661. * ACPI GPIO resources don't contain an
  662. * initial value for the GPIO. Therefore we
  663. * deduce that value from the pull field
  664. * instead. If the pin is pulled up we
  665. * assume default to be high, otherwise
  666. * low.
  667. */
  668. gpiod_direction_output(desc, pull_up);
  669. break;
  670. default:
  671. /*
  672. * Assume that the BIOS has configured the
  673. * direction and pull accordingly.
  674. */
  675. break;
  676. }
  677. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  678. if (!conn) {
  679. status = AE_NO_MEMORY;
  680. gpiochip_free_own_desc(desc);
  681. mutex_unlock(&achip->conn_lock);
  682. goto out;
  683. }
  684. conn->pin = pin;
  685. conn->desc = desc;
  686. list_add_tail(&conn->node, &achip->conns);
  687. }
  688. mutex_unlock(&achip->conn_lock);
  689. if (function == ACPI_WRITE)
  690. gpiod_set_raw_value_cansleep(desc,
  691. !!((1 << i) & *value));
  692. else
  693. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  694. }
  695. out:
  696. ACPI_FREE(ares);
  697. return status;
  698. }
  699. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  700. {
  701. struct gpio_chip *chip = achip->chip;
  702. acpi_handle handle = ACPI_HANDLE(chip->parent);
  703. acpi_status status;
  704. INIT_LIST_HEAD(&achip->conns);
  705. mutex_init(&achip->conn_lock);
  706. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  707. acpi_gpio_adr_space_handler,
  708. NULL, achip);
  709. if (ACPI_FAILURE(status))
  710. dev_err(chip->parent,
  711. "Failed to install GPIO OpRegion handler\n");
  712. }
  713. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  714. {
  715. struct gpio_chip *chip = achip->chip;
  716. acpi_handle handle = ACPI_HANDLE(chip->parent);
  717. struct acpi_gpio_connection *conn, *tmp;
  718. acpi_status status;
  719. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  720. acpi_gpio_adr_space_handler);
  721. if (ACPI_FAILURE(status)) {
  722. dev_err(chip->parent,
  723. "Failed to remove GPIO OpRegion handler\n");
  724. return;
  725. }
  726. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  727. gpiochip_free_own_desc(conn->desc);
  728. list_del(&conn->node);
  729. kfree(conn);
  730. }
  731. }
  732. void acpi_gpiochip_add(struct gpio_chip *chip)
  733. {
  734. struct acpi_gpio_chip *acpi_gpio;
  735. acpi_handle handle;
  736. acpi_status status;
  737. if (!chip || !chip->parent)
  738. return;
  739. handle = ACPI_HANDLE(chip->parent);
  740. if (!handle)
  741. return;
  742. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  743. if (!acpi_gpio) {
  744. dev_err(chip->parent,
  745. "Failed to allocate memory for ACPI GPIO chip\n");
  746. return;
  747. }
  748. acpi_gpio->chip = chip;
  749. INIT_LIST_HEAD(&acpi_gpio->events);
  750. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  751. if (ACPI_FAILURE(status)) {
  752. dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
  753. kfree(acpi_gpio);
  754. return;
  755. }
  756. acpi_gpiochip_request_regions(acpi_gpio);
  757. acpi_walk_dep_device_list(handle);
  758. }
  759. void acpi_gpiochip_remove(struct gpio_chip *chip)
  760. {
  761. struct acpi_gpio_chip *acpi_gpio;
  762. acpi_handle handle;
  763. acpi_status status;
  764. if (!chip || !chip->parent)
  765. return;
  766. handle = ACPI_HANDLE(chip->parent);
  767. if (!handle)
  768. return;
  769. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  770. if (ACPI_FAILURE(status)) {
  771. dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
  772. return;
  773. }
  774. acpi_gpiochip_free_regions(acpi_gpio);
  775. acpi_detach_data(handle, acpi_gpio_chip_dh);
  776. kfree(acpi_gpio);
  777. }
  778. static unsigned int acpi_gpio_package_count(const union acpi_object *obj)
  779. {
  780. const union acpi_object *element = obj->package.elements;
  781. const union acpi_object *end = element + obj->package.count;
  782. unsigned int count = 0;
  783. while (element < end) {
  784. if (element->type == ACPI_TYPE_LOCAL_REFERENCE)
  785. count++;
  786. element++;
  787. }
  788. return count;
  789. }
  790. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  791. {
  792. unsigned int *count = data;
  793. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  794. *count += ares->data.gpio.pin_table_length;
  795. return 1;
  796. }
  797. /**
  798. * acpi_gpio_count - return the number of GPIOs associated with a
  799. * device / function or -ENOENT if no GPIO has been
  800. * assigned to the requested function.
  801. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  802. * @con_id: function within the GPIO consumer
  803. */
  804. int acpi_gpio_count(struct device *dev, const char *con_id)
  805. {
  806. struct acpi_device *adev = ACPI_COMPANION(dev);
  807. const union acpi_object *obj;
  808. const struct acpi_gpio_mapping *gm;
  809. int count = -ENOENT;
  810. int ret;
  811. char propname[32];
  812. unsigned int i;
  813. /* Try first from _DSD */
  814. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  815. if (con_id && strcmp(con_id, "gpios"))
  816. snprintf(propname, sizeof(propname), "%s-%s",
  817. con_id, gpio_suffixes[i]);
  818. else
  819. snprintf(propname, sizeof(propname), "%s",
  820. gpio_suffixes[i]);
  821. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  822. &obj);
  823. if (ret == 0) {
  824. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  825. count = 1;
  826. else if (obj->type == ACPI_TYPE_PACKAGE)
  827. count = acpi_gpio_package_count(obj);
  828. } else if (adev->driver_gpios) {
  829. for (gm = adev->driver_gpios; gm->name; gm++)
  830. if (strcmp(propname, gm->name) == 0) {
  831. count = gm->size;
  832. break;
  833. }
  834. }
  835. if (count >= 0)
  836. break;
  837. }
  838. /* Then from plain _CRS GPIOs */
  839. if (count < 0) {
  840. struct list_head resource_list;
  841. unsigned int crs_count = 0;
  842. INIT_LIST_HEAD(&resource_list);
  843. acpi_dev_get_resources(adev, &resource_list,
  844. acpi_find_gpio_count, &crs_count);
  845. acpi_dev_free_resource_list(&resource_list);
  846. if (crs_count > 0)
  847. count = crs_count;
  848. }
  849. return count;
  850. }
  851. struct acpi_crs_lookup {
  852. struct list_head node;
  853. struct acpi_device *adev;
  854. const char *con_id;
  855. };
  856. static DEFINE_MUTEX(acpi_crs_lookup_lock);
  857. static LIST_HEAD(acpi_crs_lookup_list);
  858. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
  859. {
  860. struct acpi_crs_lookup *l, *lookup = NULL;
  861. /* Never allow fallback if the device has properties */
  862. if (adev->data.properties || adev->driver_gpios)
  863. return false;
  864. mutex_lock(&acpi_crs_lookup_lock);
  865. list_for_each_entry(l, &acpi_crs_lookup_list, node) {
  866. if (l->adev == adev) {
  867. lookup = l;
  868. break;
  869. }
  870. }
  871. if (!lookup) {
  872. lookup = kmalloc(sizeof(*lookup), GFP_KERNEL);
  873. if (lookup) {
  874. lookup->adev = adev;
  875. lookup->con_id = kstrdup(con_id, GFP_KERNEL);
  876. list_add_tail(&lookup->node, &acpi_crs_lookup_list);
  877. }
  878. }
  879. mutex_unlock(&acpi_crs_lookup_lock);
  880. return lookup &&
  881. ((!lookup->con_id && !con_id) ||
  882. (lookup->con_id && con_id &&
  883. strcmp(lookup->con_id, con_id) == 0));
  884. }