gpiolib-acpi.c 29 KB

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