gpiolib-acpi.c 30 KB

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