gpiolib-acpi.c 44 KB

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