gpiolib-acpi.c 28 KB

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