gpiolib-acpi.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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/dmi.h>
  13. #include <linux/errno.h>
  14. #include <linux/gpio.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/gpio/machine.h>
  18. #include <linux/export.h>
  19. #include <linux/acpi.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pinctrl/pinctrl.h>
  23. #include "gpiolib.h"
  24. static int run_edge_events_on_boot = -1;
  25. module_param(run_edge_events_on_boot, int, 0444);
  26. MODULE_PARM_DESC(run_edge_events_on_boot,
  27. "Run edge _AEI event-handlers at boot: 0=no, 1=yes, -1=auto");
  28. static char *ignore_wake;
  29. module_param(ignore_wake, charp, 0444);
  30. MODULE_PARM_DESC(ignore_wake,
  31. "controller@pin combos on which to ignore the ACPI wake flag "
  32. "ignore_wake=controller@pin[,controller@pin[,...]]");
  33. struct acpi_gpiolib_dmi_quirk {
  34. bool no_edge_events_on_boot;
  35. char *ignore_wake;
  36. };
  37. /**
  38. * struct acpi_gpio_event - ACPI GPIO event handler data
  39. *
  40. * @node: list-entry of the events list of the struct acpi_gpio_chip
  41. * @handle: handle of ACPI method to execute when the IRQ triggers
  42. * @handler: irq_handler to pass to request_irq when requesting the IRQ
  43. * @pin: GPIO pin number on the gpio_chip
  44. * @irq: Linux IRQ number for the event, for request_ / free_irq
  45. * @irqflags: flags to pass to request_irq when requesting the IRQ
  46. * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source
  47. * @is_requested: True if request_irq has been done
  48. * @desc: gpio_desc for the GPIO pin for this event
  49. */
  50. struct acpi_gpio_event {
  51. struct list_head node;
  52. acpi_handle handle;
  53. irq_handler_t handler;
  54. unsigned int pin;
  55. unsigned int irq;
  56. unsigned long irqflags;
  57. bool irq_is_wake;
  58. bool irq_requested;
  59. struct gpio_desc *desc;
  60. };
  61. struct acpi_gpio_connection {
  62. struct list_head node;
  63. unsigned int pin;
  64. struct gpio_desc *desc;
  65. };
  66. struct acpi_gpio_chip {
  67. /*
  68. * ACPICA requires that the first field of the context parameter
  69. * passed to acpi_install_address_space_handler() is large enough
  70. * to hold struct acpi_connection_info.
  71. */
  72. struct acpi_connection_info conn_info;
  73. struct list_head conns;
  74. struct mutex conn_lock;
  75. struct gpio_chip *chip;
  76. struct list_head events;
  77. struct list_head deferred_req_irqs_list_entry;
  78. };
  79. /*
  80. * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init
  81. * (so builtin drivers) we register the ACPI GpioInt IRQ handlers from a
  82. * late_initcall_sync handler, so that other builtin drivers can register their
  83. * OpRegions before the event handlers can run. This list contains gpiochips
  84. * for which the acpi_gpiochip_request_irqs() call has been deferred.
  85. */
  86. static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
  87. static LIST_HEAD(acpi_gpio_deferred_req_irqs_list);
  88. static bool acpi_gpio_deferred_req_irqs_done;
  89. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  90. {
  91. if (!gc->parent)
  92. return false;
  93. return ACPI_HANDLE(gc->parent) == data;
  94. }
  95. /**
  96. * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
  97. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  98. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  99. *
  100. * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  101. * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
  102. * controller does not have gpiochip registered at the moment. This is to
  103. * support probe deferral.
  104. */
  105. static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  106. {
  107. struct gpio_chip *chip;
  108. acpi_handle handle;
  109. acpi_status status;
  110. status = acpi_get_handle(NULL, path, &handle);
  111. if (ACPI_FAILURE(status))
  112. return ERR_PTR(-ENODEV);
  113. chip = gpiochip_find(handle, acpi_gpiochip_find);
  114. if (!chip)
  115. return ERR_PTR(-EPROBE_DEFER);
  116. return gpiochip_get_desc(chip, pin);
  117. }
  118. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  119. {
  120. struct acpi_gpio_event *event = data;
  121. acpi_evaluate_object(event->handle, NULL, NULL, NULL);
  122. return IRQ_HANDLED;
  123. }
  124. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  125. {
  126. struct acpi_gpio_event *event = data;
  127. acpi_execute_simple_method(event->handle, NULL, event->pin);
  128. return IRQ_HANDLED;
  129. }
  130. static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
  131. {
  132. /* The address of this function is used as a key. */
  133. }
  134. bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
  135. struct acpi_resource_gpio **agpio)
  136. {
  137. struct acpi_resource_gpio *gpio;
  138. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  139. return false;
  140. gpio = &ares->data.gpio;
  141. if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
  142. return false;
  143. *agpio = gpio;
  144. return true;
  145. }
  146. EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
  147. static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
  148. struct acpi_gpio_event *event)
  149. {
  150. int ret, value;
  151. ret = request_threaded_irq(event->irq, NULL, event->handler,
  152. event->irqflags, "ACPI:Event", event);
  153. if (ret) {
  154. dev_err(acpi_gpio->chip->parent,
  155. "Failed to setup interrupt handler for %d\n",
  156. event->irq);
  157. return;
  158. }
  159. if (event->irq_is_wake)
  160. enable_irq_wake(event->irq);
  161. event->irq_requested = true;
  162. /* Make sure we trigger the initial state of edge-triggered IRQs */
  163. if (run_edge_events_on_boot &&
  164. (event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) {
  165. value = gpiod_get_raw_value_cansleep(event->desc);
  166. if (((event->irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
  167. ((event->irqflags & IRQF_TRIGGER_FALLING) && value == 0))
  168. event->handler(event->irq, event);
  169. }
  170. }
  171. static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
  172. {
  173. struct acpi_gpio_event *event;
  174. list_for_each_entry(event, &acpi_gpio->events, node)
  175. acpi_gpiochip_request_irq(acpi_gpio, event);
  176. }
  177. static bool acpi_gpio_in_ignore_list(const char *controller_in, int pin_in)
  178. {
  179. const char *controller, *pin_str;
  180. int len, pin;
  181. char *endp;
  182. controller = ignore_wake;
  183. while (controller) {
  184. pin_str = strchr(controller, '@');
  185. if (!pin_str)
  186. goto err;
  187. len = pin_str - controller;
  188. if (len == strlen(controller_in) &&
  189. strncmp(controller, controller_in, len) == 0) {
  190. pin = simple_strtoul(pin_str + 1, &endp, 10);
  191. if (*endp != 0 && *endp != ',')
  192. goto err;
  193. if (pin == pin_in)
  194. return true;
  195. }
  196. controller = strchr(controller, ',');
  197. if (controller)
  198. controller++;
  199. }
  200. return false;
  201. err:
  202. pr_err_once("Error invalid value for gpiolib_acpi.ignore_wake: %s\n",
  203. ignore_wake);
  204. return false;
  205. }
  206. static bool acpi_gpio_irq_is_wake(struct device *parent,
  207. struct acpi_resource_gpio *agpio)
  208. {
  209. int pin = agpio->pin_table[0];
  210. if (agpio->wake_capable != ACPI_WAKE_CAPABLE)
  211. return false;
  212. if (acpi_gpio_in_ignore_list(dev_name(parent), pin)) {
  213. dev_info(parent, "Ignoring wakeup on pin %d\n", pin);
  214. return false;
  215. }
  216. return true;
  217. }
  218. static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
  219. void *context)
  220. {
  221. struct acpi_gpio_chip *acpi_gpio = context;
  222. struct gpio_chip *chip = acpi_gpio->chip;
  223. struct acpi_resource_gpio *agpio;
  224. acpi_handle handle, evt_handle;
  225. struct acpi_gpio_event *event;
  226. irq_handler_t handler = NULL;
  227. struct gpio_desc *desc;
  228. int ret, pin, irq;
  229. if (!acpi_gpio_get_irq_resource(ares, &agpio))
  230. return AE_OK;
  231. handle = ACPI_HANDLE(chip->parent);
  232. pin = agpio->pin_table[0];
  233. if (pin <= 255) {
  234. char ev_name[5];
  235. sprintf(ev_name, "_%c%02hhX",
  236. agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
  237. pin);
  238. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  239. handler = acpi_gpio_irq_handler;
  240. }
  241. if (!handler) {
  242. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  243. handler = acpi_gpio_irq_handler_evt;
  244. }
  245. if (!handler)
  246. return AE_OK;
  247. desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
  248. if (IS_ERR(desc)) {
  249. dev_err(chip->parent, "Failed to request GPIO\n");
  250. return AE_ERROR;
  251. }
  252. gpiod_direction_input(desc);
  253. ret = gpiochip_lock_as_irq(chip, pin);
  254. if (ret) {
  255. dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
  256. goto fail_free_desc;
  257. }
  258. irq = gpiod_to_irq(desc);
  259. if (irq < 0) {
  260. dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
  261. goto fail_unlock_irq;
  262. }
  263. event = kzalloc(sizeof(*event), GFP_KERNEL);
  264. if (!event)
  265. goto fail_unlock_irq;
  266. event->irqflags = IRQF_ONESHOT;
  267. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  268. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  269. event->irqflags |= IRQF_TRIGGER_HIGH;
  270. else
  271. event->irqflags |= IRQF_TRIGGER_LOW;
  272. } else {
  273. switch (agpio->polarity) {
  274. case ACPI_ACTIVE_HIGH:
  275. event->irqflags |= IRQF_TRIGGER_RISING;
  276. break;
  277. case ACPI_ACTIVE_LOW:
  278. event->irqflags |= IRQF_TRIGGER_FALLING;
  279. break;
  280. default:
  281. event->irqflags |= IRQF_TRIGGER_RISING |
  282. IRQF_TRIGGER_FALLING;
  283. break;
  284. }
  285. }
  286. event->handle = evt_handle;
  287. event->handler = handler;
  288. event->irq = irq;
  289. event->irq_is_wake = acpi_gpio_irq_is_wake(chip->parent, agpio);
  290. event->pin = pin;
  291. event->desc = desc;
  292. list_add_tail(&event->node, &acpi_gpio->events);
  293. return AE_OK;
  294. fail_unlock_irq:
  295. gpiochip_unlock_as_irq(chip, pin);
  296. fail_free_desc:
  297. gpiochip_free_own_desc(desc);
  298. return AE_ERROR;
  299. }
  300. /**
  301. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  302. * @chip: GPIO chip
  303. *
  304. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  305. * handled by ACPI event methods which need to be called from the GPIO
  306. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  307. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  308. * the acpi event methods for those pins.
  309. */
  310. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  311. {
  312. struct acpi_gpio_chip *acpi_gpio;
  313. acpi_handle handle;
  314. acpi_status status;
  315. bool defer;
  316. if (!chip->parent || !chip->to_irq)
  317. return;
  318. handle = ACPI_HANDLE(chip->parent);
  319. if (!handle)
  320. return;
  321. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  322. if (ACPI_FAILURE(status))
  323. return;
  324. acpi_walk_resources(handle, "_AEI",
  325. acpi_gpiochip_alloc_event, acpi_gpio);
  326. mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
  327. defer = !acpi_gpio_deferred_req_irqs_done;
  328. if (defer)
  329. list_add(&acpi_gpio->deferred_req_irqs_list_entry,
  330. &acpi_gpio_deferred_req_irqs_list);
  331. mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
  332. if (defer)
  333. return;
  334. acpi_gpiochip_request_irqs(acpi_gpio);
  335. }
  336. EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
  337. /**
  338. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  339. * @chip: GPIO chip
  340. *
  341. * Free interrupts associated with GPIO ACPI event method for the given
  342. * GPIO chip.
  343. */
  344. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  345. {
  346. struct acpi_gpio_chip *acpi_gpio;
  347. struct acpi_gpio_event *event, *ep;
  348. acpi_handle handle;
  349. acpi_status status;
  350. if (!chip->parent || !chip->to_irq)
  351. return;
  352. handle = ACPI_HANDLE(chip->parent);
  353. if (!handle)
  354. return;
  355. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  356. if (ACPI_FAILURE(status))
  357. return;
  358. mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
  359. if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry))
  360. list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
  361. mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
  362. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  363. struct gpio_desc *desc;
  364. if (event->irq_requested) {
  365. if (event->irq_is_wake)
  366. disable_irq_wake(event->irq);
  367. free_irq(event->irq, event);
  368. }
  369. desc = event->desc;
  370. if (WARN_ON(IS_ERR(desc)))
  371. continue;
  372. gpiochip_unlock_as_irq(chip, event->pin);
  373. gpiochip_free_own_desc(desc);
  374. list_del(&event->node);
  375. kfree(event);
  376. }
  377. }
  378. EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
  379. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  380. const struct acpi_gpio_mapping *gpios)
  381. {
  382. if (adev && gpios) {
  383. adev->driver_gpios = gpios;
  384. return 0;
  385. }
  386. return -EINVAL;
  387. }
  388. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  389. static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
  390. {
  391. acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
  392. }
  393. int devm_acpi_dev_add_driver_gpios(struct device *dev,
  394. const struct acpi_gpio_mapping *gpios)
  395. {
  396. void *res;
  397. int ret;
  398. res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
  399. if (!res)
  400. return -ENOMEM;
  401. ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
  402. if (ret) {
  403. devres_free(res);
  404. return ret;
  405. }
  406. devres_add(dev, res);
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
  410. void devm_acpi_dev_remove_driver_gpios(struct device *dev)
  411. {
  412. WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
  413. }
  414. EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
  415. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  416. const char *name, int index,
  417. struct fwnode_reference_args *args,
  418. unsigned int *quirks)
  419. {
  420. const struct acpi_gpio_mapping *gm;
  421. if (!adev->driver_gpios)
  422. return false;
  423. for (gm = adev->driver_gpios; gm->name; gm++)
  424. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  425. const struct acpi_gpio_params *par = gm->data + index;
  426. args->fwnode = acpi_fwnode_handle(adev);
  427. args->args[0] = par->crs_entry_index;
  428. args->args[1] = par->line_index;
  429. args->args[2] = par->active_low;
  430. args->nargs = 3;
  431. *quirks = gm->quirks;
  432. return true;
  433. }
  434. return false;
  435. }
  436. static enum gpiod_flags
  437. acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
  438. {
  439. bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  440. switch (agpio->io_restriction) {
  441. case ACPI_IO_RESTRICT_INPUT:
  442. return GPIOD_IN;
  443. case ACPI_IO_RESTRICT_OUTPUT:
  444. /*
  445. * ACPI GPIO resources don't contain an initial value for the
  446. * GPIO. Therefore we deduce that value from the pull field
  447. * instead. If the pin is pulled up we assume default to be
  448. * high, otherwise low.
  449. */
  450. return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  451. default:
  452. /*
  453. * Assume that the BIOS has configured the direction and pull
  454. * accordingly.
  455. */
  456. return GPIOD_ASIS;
  457. }
  458. }
  459. static int
  460. __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
  461. {
  462. int ret = 0;
  463. /*
  464. * Check if the BIOS has IoRestriction with explicitly set direction
  465. * and update @flags accordingly. Otherwise use whatever caller asked
  466. * for.
  467. */
  468. if (update & GPIOD_FLAGS_BIT_DIR_SET) {
  469. enum gpiod_flags diff = *flags ^ update;
  470. /*
  471. * Check if caller supplied incompatible GPIO initialization
  472. * flags.
  473. *
  474. * Return %-EINVAL to notify that firmware has different
  475. * settings and we are going to use them.
  476. */
  477. if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
  478. ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
  479. ret = -EINVAL;
  480. *flags = update;
  481. }
  482. return ret;
  483. }
  484. int
  485. acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
  486. {
  487. struct device *dev = &info->adev->dev;
  488. enum gpiod_flags old = *flags;
  489. int ret;
  490. ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
  491. if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
  492. if (ret)
  493. dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
  494. } else {
  495. if (ret)
  496. dev_dbg(dev, "Override GPIO initialization flags\n");
  497. *flags = old;
  498. }
  499. return ret;
  500. }
  501. struct acpi_gpio_lookup {
  502. struct acpi_gpio_info info;
  503. int index;
  504. int pin_index;
  505. bool active_low;
  506. struct gpio_desc *desc;
  507. int n;
  508. };
  509. static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
  510. {
  511. struct acpi_gpio_lookup *lookup = data;
  512. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  513. return 1;
  514. if (lookup->n++ == lookup->index && !lookup->desc) {
  515. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  516. int pin_index = lookup->pin_index;
  517. if (pin_index >= agpio->pin_table_length)
  518. return 1;
  519. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  520. agpio->pin_table[pin_index]);
  521. lookup->info.gpioint =
  522. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  523. /*
  524. * Polarity and triggering are only specified for GpioInt
  525. * resource.
  526. * Note: we expect here:
  527. * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
  528. * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
  529. */
  530. if (lookup->info.gpioint) {
  531. lookup->info.flags = GPIOD_IN;
  532. lookup->info.polarity = agpio->polarity;
  533. lookup->info.triggering = agpio->triggering;
  534. } else {
  535. lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
  536. lookup->info.polarity = lookup->active_low;
  537. }
  538. }
  539. return 1;
  540. }
  541. static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
  542. struct acpi_gpio_info *info)
  543. {
  544. struct acpi_device *adev = lookup->info.adev;
  545. struct list_head res_list;
  546. int ret;
  547. INIT_LIST_HEAD(&res_list);
  548. ret = acpi_dev_get_resources(adev, &res_list,
  549. acpi_populate_gpio_lookup,
  550. lookup);
  551. if (ret < 0)
  552. return ret;
  553. acpi_dev_free_resource_list(&res_list);
  554. if (!lookup->desc)
  555. return -ENOENT;
  556. if (info)
  557. *info = lookup->info;
  558. return 0;
  559. }
  560. static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
  561. const char *propname, int index,
  562. struct acpi_gpio_lookup *lookup)
  563. {
  564. struct fwnode_reference_args args;
  565. unsigned int quirks = 0;
  566. int ret;
  567. memset(&args, 0, sizeof(args));
  568. ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
  569. &args);
  570. if (ret) {
  571. struct acpi_device *adev = to_acpi_device_node(fwnode);
  572. if (!adev)
  573. return ret;
  574. if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
  575. &quirks))
  576. return ret;
  577. }
  578. /*
  579. * The property was found and resolved, so need to lookup the GPIO based
  580. * on returned args.
  581. */
  582. if (!to_acpi_device_node(args.fwnode))
  583. return -EINVAL;
  584. if (args.nargs != 3)
  585. return -EPROTO;
  586. lookup->index = args.args[0];
  587. lookup->pin_index = args.args[1];
  588. lookup->active_low = !!args.args[2];
  589. lookup->info.adev = to_acpi_device_node(args.fwnode);
  590. lookup->info.quirks = quirks;
  591. return 0;
  592. }
  593. /**
  594. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  595. * @adev: pointer to a ACPI device to get GPIO from
  596. * @propname: Property name of the GPIO (optional)
  597. * @index: index of GpioIo/GpioInt resource (starting from %0)
  598. * @info: info pointer to fill in (optional)
  599. *
  600. * Function goes through ACPI resources for @adev and based on @index looks
  601. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  602. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  603. * are total %3 GPIO resources, the index goes from %0 to %2.
  604. *
  605. * If @propname is specified the GPIO is looked using device property. In
  606. * that case @index is used to select the GPIO entry in the property value
  607. * (in case of multiple).
  608. *
  609. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  610. * returned.
  611. *
  612. * Note: if the GPIO resource has multiple entries in the pin list, this
  613. * function only returns the first.
  614. */
  615. static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  616. const char *propname, int index,
  617. struct acpi_gpio_info *info)
  618. {
  619. struct acpi_gpio_lookup lookup;
  620. int ret;
  621. if (!adev)
  622. return ERR_PTR(-ENODEV);
  623. memset(&lookup, 0, sizeof(lookup));
  624. lookup.index = index;
  625. if (propname) {
  626. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  627. ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
  628. propname, index, &lookup);
  629. if (ret)
  630. return ERR_PTR(ret);
  631. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
  632. dev_name(&lookup.info.adev->dev), lookup.index,
  633. lookup.pin_index, lookup.active_low);
  634. } else {
  635. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  636. lookup.info.adev = adev;
  637. }
  638. ret = acpi_gpio_resource_lookup(&lookup, info);
  639. return ret ? ERR_PTR(ret) : lookup.desc;
  640. }
  641. struct gpio_desc *acpi_find_gpio(struct device *dev,
  642. const char *con_id,
  643. unsigned int idx,
  644. enum gpiod_flags *dflags,
  645. enum gpio_lookup_flags *lookupflags)
  646. {
  647. struct acpi_device *adev = ACPI_COMPANION(dev);
  648. struct acpi_gpio_info info;
  649. struct gpio_desc *desc;
  650. char propname[32];
  651. int i;
  652. /* Try first from _DSD */
  653. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  654. if (con_id) {
  655. snprintf(propname, sizeof(propname), "%s-%s",
  656. con_id, gpio_suffixes[i]);
  657. } else {
  658. snprintf(propname, sizeof(propname), "%s",
  659. gpio_suffixes[i]);
  660. }
  661. desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
  662. if (!IS_ERR(desc))
  663. break;
  664. if (PTR_ERR(desc) == -EPROBE_DEFER)
  665. return ERR_CAST(desc);
  666. }
  667. /* Then from plain _CRS GPIOs */
  668. if (IS_ERR(desc)) {
  669. if (!acpi_can_fallback_to_crs(adev, con_id))
  670. return ERR_PTR(-ENOENT);
  671. desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
  672. if (IS_ERR(desc))
  673. return desc;
  674. }
  675. if (info.gpioint &&
  676. (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
  677. dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
  678. return ERR_PTR(-ENOENT);
  679. }
  680. if (info.polarity == GPIO_ACTIVE_LOW)
  681. *lookupflags |= GPIO_ACTIVE_LOW;
  682. acpi_gpio_update_gpiod_flags(dflags, &info);
  683. return desc;
  684. }
  685. /**
  686. * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
  687. * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  688. * @propname: Property name of the GPIO
  689. * @index: index of GpioIo/GpioInt resource (starting from %0)
  690. * @info: info pointer to fill in (optional)
  691. *
  692. * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
  693. * Otherwise (ie. it is a data-only non-device object), use the property-based
  694. * GPIO lookup to get to the GPIO resource with the relevant information and use
  695. * that to obtain the GPIO descriptor to return.
  696. */
  697. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  698. const char *propname, int index,
  699. struct acpi_gpio_info *info)
  700. {
  701. struct acpi_gpio_lookup lookup;
  702. struct acpi_device *adev;
  703. int ret;
  704. adev = to_acpi_device_node(fwnode);
  705. if (adev)
  706. return acpi_get_gpiod_by_index(adev, propname, index, info);
  707. if (!is_acpi_data_node(fwnode))
  708. return ERR_PTR(-ENODEV);
  709. if (!propname)
  710. return ERR_PTR(-EINVAL);
  711. memset(&lookup, 0, sizeof(lookup));
  712. lookup.index = index;
  713. ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
  714. if (ret)
  715. return ERR_PTR(ret);
  716. ret = acpi_gpio_resource_lookup(&lookup, info);
  717. return ret ? ERR_PTR(ret) : lookup.desc;
  718. }
  719. /**
  720. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  721. * @adev: pointer to a ACPI device to get IRQ from
  722. * @index: index of GpioInt resource (starting from %0)
  723. *
  724. * If the device has one or more GpioInt resources, this function can be
  725. * used to translate from the GPIO offset in the resource to the Linux IRQ
  726. * number.
  727. *
  728. * The function is idempotent, though each time it runs it will configure GPIO
  729. * pin direction according to the flags in GpioInt resource.
  730. *
  731. * Return: Linux IRQ number (> %0) on success, negative errno on failure.
  732. */
  733. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  734. {
  735. int idx, i;
  736. unsigned int irq_flags;
  737. int ret;
  738. for (i = 0, idx = 0; idx <= index; i++) {
  739. struct acpi_gpio_info info;
  740. struct gpio_desc *desc;
  741. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  742. /* Ignore -EPROBE_DEFER, it only matters if idx matches */
  743. if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
  744. return PTR_ERR(desc);
  745. if (info.gpioint && idx++ == index) {
  746. char label[32];
  747. int irq;
  748. if (IS_ERR(desc))
  749. return PTR_ERR(desc);
  750. irq = gpiod_to_irq(desc);
  751. if (irq < 0)
  752. return irq;
  753. snprintf(label, sizeof(label), "GpioInt() %d", index);
  754. ret = gpiod_configure_flags(desc, label, 0, info.flags);
  755. if (ret < 0)
  756. return ret;
  757. irq_flags = acpi_dev_get_irq_type(info.triggering,
  758. info.polarity);
  759. /* Set type if specified and different than the current one */
  760. if (irq_flags != IRQ_TYPE_NONE &&
  761. irq_flags != irq_get_trigger_type(irq))
  762. irq_set_irq_type(irq, irq_flags);
  763. return irq;
  764. }
  765. }
  766. return -ENOENT;
  767. }
  768. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  769. static acpi_status
  770. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  771. u32 bits, u64 *value, void *handler_context,
  772. void *region_context)
  773. {
  774. struct acpi_gpio_chip *achip = region_context;
  775. struct gpio_chip *chip = achip->chip;
  776. struct acpi_resource_gpio *agpio;
  777. struct acpi_resource *ares;
  778. int pin_index = (int)address;
  779. acpi_status status;
  780. int length;
  781. int i;
  782. status = acpi_buffer_to_resource(achip->conn_info.connection,
  783. achip->conn_info.length, &ares);
  784. if (ACPI_FAILURE(status))
  785. return status;
  786. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  787. ACPI_FREE(ares);
  788. return AE_BAD_PARAMETER;
  789. }
  790. agpio = &ares->data.gpio;
  791. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  792. function == ACPI_WRITE)) {
  793. ACPI_FREE(ares);
  794. return AE_BAD_PARAMETER;
  795. }
  796. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  797. for (i = pin_index; i < length; ++i) {
  798. int pin = agpio->pin_table[i];
  799. struct acpi_gpio_connection *conn;
  800. struct gpio_desc *desc;
  801. bool found;
  802. mutex_lock(&achip->conn_lock);
  803. found = false;
  804. list_for_each_entry(conn, &achip->conns, node) {
  805. if (conn->pin == pin) {
  806. found = true;
  807. desc = conn->desc;
  808. break;
  809. }
  810. }
  811. /*
  812. * The same GPIO can be shared between operation region and
  813. * event but only if the access here is ACPI_READ. In that
  814. * case we "borrow" the event GPIO instead.
  815. */
  816. if (!found && agpio->sharable == ACPI_SHARED &&
  817. function == ACPI_READ) {
  818. struct acpi_gpio_event *event;
  819. list_for_each_entry(event, &achip->events, node) {
  820. if (event->pin == pin) {
  821. desc = event->desc;
  822. found = true;
  823. break;
  824. }
  825. }
  826. }
  827. if (!found) {
  828. enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
  829. const char *label = "ACPI:OpRegion";
  830. int err;
  831. desc = gpiochip_request_own_desc(chip, pin, label);
  832. if (IS_ERR(desc)) {
  833. status = AE_ERROR;
  834. mutex_unlock(&achip->conn_lock);
  835. goto out;
  836. }
  837. err = gpiod_configure_flags(desc, label, 0, flags);
  838. if (err < 0) {
  839. status = AE_NOT_CONFIGURED;
  840. gpiochip_free_own_desc(desc);
  841. mutex_unlock(&achip->conn_lock);
  842. goto out;
  843. }
  844. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  845. if (!conn) {
  846. status = AE_NO_MEMORY;
  847. gpiochip_free_own_desc(desc);
  848. mutex_unlock(&achip->conn_lock);
  849. goto out;
  850. }
  851. conn->pin = pin;
  852. conn->desc = desc;
  853. list_add_tail(&conn->node, &achip->conns);
  854. }
  855. mutex_unlock(&achip->conn_lock);
  856. if (function == ACPI_WRITE)
  857. gpiod_set_raw_value_cansleep(desc,
  858. !!((1 << i) & *value));
  859. else
  860. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  861. }
  862. out:
  863. ACPI_FREE(ares);
  864. return status;
  865. }
  866. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  867. {
  868. struct gpio_chip *chip = achip->chip;
  869. acpi_handle handle = ACPI_HANDLE(chip->parent);
  870. acpi_status status;
  871. INIT_LIST_HEAD(&achip->conns);
  872. mutex_init(&achip->conn_lock);
  873. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  874. acpi_gpio_adr_space_handler,
  875. NULL, achip);
  876. if (ACPI_FAILURE(status))
  877. dev_err(chip->parent,
  878. "Failed to install GPIO OpRegion handler\n");
  879. }
  880. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  881. {
  882. struct gpio_chip *chip = achip->chip;
  883. acpi_handle handle = ACPI_HANDLE(chip->parent);
  884. struct acpi_gpio_connection *conn, *tmp;
  885. acpi_status status;
  886. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  887. acpi_gpio_adr_space_handler);
  888. if (ACPI_FAILURE(status)) {
  889. dev_err(chip->parent,
  890. "Failed to remove GPIO OpRegion handler\n");
  891. return;
  892. }
  893. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  894. gpiochip_free_own_desc(conn->desc);
  895. list_del(&conn->node);
  896. kfree(conn);
  897. }
  898. }
  899. static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
  900. struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
  901. const char **name, unsigned int *lflags, unsigned int *dflags)
  902. {
  903. struct gpio_chip *chip = achip->chip;
  904. struct gpio_desc *desc;
  905. u32 gpios[2];
  906. int ret;
  907. *lflags = 0;
  908. *dflags = 0;
  909. *name = NULL;
  910. ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
  911. ARRAY_SIZE(gpios));
  912. if (ret < 0)
  913. return ERR_PTR(ret);
  914. desc = gpiochip_get_desc(chip, gpios[0]);
  915. if (IS_ERR(desc))
  916. return desc;
  917. if (gpios[1])
  918. *lflags |= GPIO_ACTIVE_LOW;
  919. if (fwnode_property_present(fwnode, "input"))
  920. *dflags |= GPIOD_IN;
  921. else if (fwnode_property_present(fwnode, "output-low"))
  922. *dflags |= GPIOD_OUT_LOW;
  923. else if (fwnode_property_present(fwnode, "output-high"))
  924. *dflags |= GPIOD_OUT_HIGH;
  925. else
  926. return ERR_PTR(-EINVAL);
  927. fwnode_property_read_string(fwnode, "line-name", name);
  928. return desc;
  929. }
  930. static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
  931. {
  932. struct gpio_chip *chip = achip->chip;
  933. struct fwnode_handle *fwnode;
  934. device_for_each_child_node(chip->parent, fwnode) {
  935. unsigned int lflags, dflags;
  936. struct gpio_desc *desc;
  937. const char *name;
  938. int ret;
  939. if (!fwnode_property_present(fwnode, "gpio-hog"))
  940. continue;
  941. desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
  942. &lflags, &dflags);
  943. if (IS_ERR(desc))
  944. continue;
  945. ret = gpiod_hog(desc, name, lflags, dflags);
  946. if (ret) {
  947. dev_err(chip->parent, "Failed to hog GPIO\n");
  948. fwnode_handle_put(fwnode);
  949. return;
  950. }
  951. }
  952. }
  953. void acpi_gpiochip_add(struct gpio_chip *chip)
  954. {
  955. struct acpi_gpio_chip *acpi_gpio;
  956. acpi_handle handle;
  957. acpi_status status;
  958. if (!chip || !chip->parent)
  959. return;
  960. handle = ACPI_HANDLE(chip->parent);
  961. if (!handle)
  962. return;
  963. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  964. if (!acpi_gpio) {
  965. dev_err(chip->parent,
  966. "Failed to allocate memory for ACPI GPIO chip\n");
  967. return;
  968. }
  969. acpi_gpio->chip = chip;
  970. INIT_LIST_HEAD(&acpi_gpio->events);
  971. INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
  972. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  973. if (ACPI_FAILURE(status)) {
  974. dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
  975. kfree(acpi_gpio);
  976. return;
  977. }
  978. if (!chip->names)
  979. devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
  980. acpi_gpiochip_request_regions(acpi_gpio);
  981. acpi_gpiochip_scan_gpios(acpi_gpio);
  982. acpi_walk_dep_device_list(handle);
  983. }
  984. void acpi_gpiochip_remove(struct gpio_chip *chip)
  985. {
  986. struct acpi_gpio_chip *acpi_gpio;
  987. acpi_handle handle;
  988. acpi_status status;
  989. if (!chip || !chip->parent)
  990. return;
  991. handle = ACPI_HANDLE(chip->parent);
  992. if (!handle)
  993. return;
  994. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  995. if (ACPI_FAILURE(status)) {
  996. dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
  997. return;
  998. }
  999. acpi_gpiochip_free_regions(acpi_gpio);
  1000. acpi_detach_data(handle, acpi_gpio_chip_dh);
  1001. kfree(acpi_gpio);
  1002. }
  1003. static int acpi_gpio_package_count(const union acpi_object *obj)
  1004. {
  1005. const union acpi_object *element = obj->package.elements;
  1006. const union acpi_object *end = element + obj->package.count;
  1007. unsigned int count = 0;
  1008. while (element < end) {
  1009. switch (element->type) {
  1010. case ACPI_TYPE_LOCAL_REFERENCE:
  1011. element += 3;
  1012. /* Fallthrough */
  1013. case ACPI_TYPE_INTEGER:
  1014. element++;
  1015. count++;
  1016. break;
  1017. default:
  1018. return -EPROTO;
  1019. }
  1020. }
  1021. return count;
  1022. }
  1023. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  1024. {
  1025. unsigned int *count = data;
  1026. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  1027. *count += ares->data.gpio.pin_table_length;
  1028. return 1;
  1029. }
  1030. /**
  1031. * acpi_gpio_count - return the number of GPIOs associated with a
  1032. * device / function or -ENOENT if no GPIO has been
  1033. * assigned to the requested function.
  1034. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1035. * @con_id: function within the GPIO consumer
  1036. */
  1037. int acpi_gpio_count(struct device *dev, const char *con_id)
  1038. {
  1039. struct acpi_device *adev = ACPI_COMPANION(dev);
  1040. const union acpi_object *obj;
  1041. const struct acpi_gpio_mapping *gm;
  1042. int count = -ENOENT;
  1043. int ret;
  1044. char propname[32];
  1045. unsigned int i;
  1046. /* Try first from _DSD */
  1047. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  1048. if (con_id)
  1049. snprintf(propname, sizeof(propname), "%s-%s",
  1050. con_id, gpio_suffixes[i]);
  1051. else
  1052. snprintf(propname, sizeof(propname), "%s",
  1053. gpio_suffixes[i]);
  1054. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  1055. &obj);
  1056. if (ret == 0) {
  1057. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  1058. count = 1;
  1059. else if (obj->type == ACPI_TYPE_PACKAGE)
  1060. count = acpi_gpio_package_count(obj);
  1061. } else if (adev->driver_gpios) {
  1062. for (gm = adev->driver_gpios; gm->name; gm++)
  1063. if (strcmp(propname, gm->name) == 0) {
  1064. count = gm->size;
  1065. break;
  1066. }
  1067. }
  1068. if (count > 0)
  1069. break;
  1070. }
  1071. /* Then from plain _CRS GPIOs */
  1072. if (count < 0) {
  1073. struct list_head resource_list;
  1074. unsigned int crs_count = 0;
  1075. if (!acpi_can_fallback_to_crs(adev, con_id))
  1076. return count;
  1077. INIT_LIST_HEAD(&resource_list);
  1078. acpi_dev_get_resources(adev, &resource_list,
  1079. acpi_find_gpio_count, &crs_count);
  1080. acpi_dev_free_resource_list(&resource_list);
  1081. if (crs_count > 0)
  1082. count = crs_count;
  1083. }
  1084. return count ? count : -ENOENT;
  1085. }
  1086. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
  1087. {
  1088. /* Never allow fallback if the device has properties */
  1089. if (adev->data.properties || adev->driver_gpios)
  1090. return false;
  1091. return con_id == NULL;
  1092. }
  1093. /* Run deferred acpi_gpiochip_request_irqs() */
  1094. static int acpi_gpio_handle_deferred_request_irqs(void)
  1095. {
  1096. struct acpi_gpio_chip *acpi_gpio, *tmp;
  1097. mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
  1098. list_for_each_entry_safe(acpi_gpio, tmp,
  1099. &acpi_gpio_deferred_req_irqs_list,
  1100. deferred_req_irqs_list_entry)
  1101. acpi_gpiochip_request_irqs(acpi_gpio);
  1102. acpi_gpio_deferred_req_irqs_done = true;
  1103. mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
  1104. return 0;
  1105. }
  1106. /* We must use _sync so that this runs after the first deferred_probe run */
  1107. late_initcall_sync(acpi_gpio_handle_deferred_request_irqs);
  1108. static const struct dmi_system_id gpiolib_acpi_quirks[] = {
  1109. {
  1110. /*
  1111. * The Minix Neo Z83-4 has a micro-USB-B id-pin handler for
  1112. * a non existing micro-USB-B connector which puts the HDMI
  1113. * DDC pins in GPIO mode, breaking HDMI support.
  1114. */
  1115. .matches = {
  1116. DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
  1117. DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
  1118. },
  1119. .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
  1120. .no_edge_events_on_boot = true,
  1121. },
  1122. },
  1123. {
  1124. /*
  1125. * The Terra Pad 1061 has a micro-USB-B id-pin handler, which
  1126. * instead of controlling the actual micro-USB-B turns the 5V
  1127. * boost for its USB-A connector off. The actual micro-USB-B
  1128. * connector is wired for charging only.
  1129. */
  1130. .matches = {
  1131. DMI_MATCH(DMI_SYS_VENDOR, "Wortmann_AG"),
  1132. DMI_MATCH(DMI_PRODUCT_NAME, "TERRA_PAD_1061"),
  1133. },
  1134. .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
  1135. .no_edge_events_on_boot = true,
  1136. },
  1137. },
  1138. {
  1139. /*
  1140. * HP X2 10 models with Cherry Trail SoC + TI PMIC use an
  1141. * external embedded-controller connected via I2C + an ACPI GPIO
  1142. * event handler on INT33FF:01 pin 0, causing spurious wakeups.
  1143. * When suspending by closing the LID, the power to the USB
  1144. * keyboard is turned off, causing INT0002 ACPI events to
  1145. * trigger once the XHCI controller notices the keyboard is
  1146. * gone. So INT0002 events cause spurious wakeups too. Ignoring
  1147. * EC wakes breaks wakeup when opening the lid, the user needs
  1148. * to press the power-button to wakeup the system. The
  1149. * alternative is suspend simply not working, which is worse.
  1150. */
  1151. .matches = {
  1152. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  1153. DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
  1154. },
  1155. .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
  1156. .ignore_wake = "INT33FF:01@0,INT0002:00@2",
  1157. },
  1158. },
  1159. {
  1160. /*
  1161. * HP X2 10 models with Bay Trail SoC + AXP288 PMIC use an
  1162. * external embedded-controller connected via I2C + an ACPI GPIO
  1163. * event handler on INT33FC:02 pin 28, causing spurious wakeups.
  1164. */
  1165. .matches = {
  1166. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  1167. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
  1168. DMI_MATCH(DMI_BOARD_NAME, "815D"),
  1169. },
  1170. .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
  1171. .ignore_wake = "INT33FC:02@28",
  1172. },
  1173. },
  1174. {
  1175. /*
  1176. * HP X2 10 models with Cherry Trail SoC + AXP288 PMIC use an
  1177. * external embedded-controller connected via I2C + an ACPI GPIO
  1178. * event handler on INT33FF:01 pin 0, causing spurious wakeups.
  1179. */
  1180. .matches = {
  1181. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  1182. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
  1183. DMI_MATCH(DMI_BOARD_NAME, "813E"),
  1184. },
  1185. .driver_data = &(struct acpi_gpiolib_dmi_quirk) {
  1186. .ignore_wake = "INT33FF:01@0",
  1187. },
  1188. },
  1189. {} /* Terminating entry */
  1190. };
  1191. static int acpi_gpio_setup_params(void)
  1192. {
  1193. const struct acpi_gpiolib_dmi_quirk *quirk = NULL;
  1194. const struct dmi_system_id *id;
  1195. id = dmi_first_match(gpiolib_acpi_quirks);
  1196. if (id)
  1197. quirk = id->driver_data;
  1198. if (run_edge_events_on_boot < 0) {
  1199. if (quirk && quirk->no_edge_events_on_boot)
  1200. run_edge_events_on_boot = 0;
  1201. else
  1202. run_edge_events_on_boot = 1;
  1203. }
  1204. if (ignore_wake == NULL && quirk && quirk->ignore_wake)
  1205. ignore_wake = quirk->ignore_wake;
  1206. return 0;
  1207. }
  1208. /* Directly after dmi_setup() which runs as core_initcall() */
  1209. postcore_initcall(acpi_gpio_setup_params);