gpiolib-acpi.c 39 KB

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