gpiolib-acpi.c 37 KB

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