gpiolib-acpi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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/export.h>
  17. #include <linux/acpi.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include "gpiolib.h"
  22. struct acpi_gpio_event {
  23. struct list_head node;
  24. acpi_handle handle;
  25. unsigned int pin;
  26. unsigned int irq;
  27. struct gpio_desc *desc;
  28. };
  29. struct acpi_gpio_connection {
  30. struct list_head node;
  31. unsigned int pin;
  32. struct gpio_desc *desc;
  33. };
  34. struct acpi_gpio_chip {
  35. /*
  36. * ACPICA requires that the first field of the context parameter
  37. * passed to acpi_install_address_space_handler() is large enough
  38. * to hold struct acpi_connection_info.
  39. */
  40. struct acpi_connection_info conn_info;
  41. struct list_head conns;
  42. struct mutex conn_lock;
  43. struct gpio_chip *chip;
  44. struct list_head events;
  45. };
  46. static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  47. {
  48. if (!gc->dev)
  49. return false;
  50. return ACPI_HANDLE(gc->dev) == data;
  51. }
  52. #ifdef CONFIG_PINCTRL
  53. /**
  54. * acpi_gpiochip_pin_to_gpio_offset() - translates ACPI GPIO to Linux GPIO
  55. * @chip: GPIO chip
  56. * @pin: ACPI GPIO pin number from GpioIo/GpioInt resource
  57. *
  58. * Function takes ACPI GpioIo/GpioInt pin number as a parameter and
  59. * translates it to a corresponding offset suitable to be passed to a
  60. * GPIO controller driver.
  61. *
  62. * Typically the returned offset is same as @pin, but if the GPIO
  63. * controller uses pin controller and the mapping is not contiguous the
  64. * offset might be different.
  65. */
  66. static int acpi_gpiochip_pin_to_gpio_offset(struct gpio_chip *chip, int pin)
  67. {
  68. struct gpio_pin_range *pin_range;
  69. /* If there are no ranges in this chip, use 1:1 mapping */
  70. if (list_empty(&chip->pin_ranges))
  71. return pin;
  72. list_for_each_entry(pin_range, &chip->pin_ranges, node) {
  73. const struct pinctrl_gpio_range *range = &pin_range->range;
  74. int i;
  75. if (range->pins) {
  76. for (i = 0; i < range->npins; i++) {
  77. if (range->pins[i] == pin)
  78. return range->base + i - chip->base;
  79. }
  80. } else {
  81. if (pin >= range->pin_base &&
  82. pin < range->pin_base + range->npins) {
  83. unsigned gpio_base;
  84. gpio_base = range->base - chip->base;
  85. return gpio_base + pin - range->pin_base;
  86. }
  87. }
  88. }
  89. return -EINVAL;
  90. }
  91. #else
  92. static inline int acpi_gpiochip_pin_to_gpio_offset(struct gpio_chip *chip,
  93. int pin)
  94. {
  95. return pin;
  96. }
  97. #endif
  98. /**
  99. * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
  100. * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  101. * @pin: ACPI GPIO pin number (0-based, controller-relative)
  102. *
  103. * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  104. * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
  105. * controller does not have gpiochip registered at the moment. This is to
  106. * support probe deferral.
  107. */
  108. static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  109. {
  110. struct gpio_chip *chip;
  111. acpi_handle handle;
  112. acpi_status status;
  113. int offset;
  114. status = acpi_get_handle(NULL, path, &handle);
  115. if (ACPI_FAILURE(status))
  116. return ERR_PTR(-ENODEV);
  117. chip = gpiochip_find(handle, acpi_gpiochip_find);
  118. if (!chip)
  119. return ERR_PTR(-EPROBE_DEFER);
  120. offset = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  121. if (offset < 0)
  122. return ERR_PTR(offset);
  123. return gpiochip_get_desc(chip, offset);
  124. }
  125. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  126. {
  127. struct acpi_gpio_event *event = data;
  128. acpi_evaluate_object(event->handle, NULL, NULL, NULL);
  129. return IRQ_HANDLED;
  130. }
  131. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  132. {
  133. struct acpi_gpio_event *event = data;
  134. acpi_execute_simple_method(event->handle, NULL, event->pin);
  135. return IRQ_HANDLED;
  136. }
  137. static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
  138. {
  139. /* The address of this function is used as a key. */
  140. }
  141. static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
  142. void *context)
  143. {
  144. struct acpi_gpio_chip *acpi_gpio = context;
  145. struct gpio_chip *chip = acpi_gpio->chip;
  146. struct acpi_resource_gpio *agpio;
  147. acpi_handle handle, evt_handle;
  148. struct acpi_gpio_event *event;
  149. irq_handler_t handler = NULL;
  150. struct gpio_desc *desc;
  151. unsigned long irqflags;
  152. int ret, pin, irq;
  153. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  154. return AE_OK;
  155. agpio = &ares->data.gpio;
  156. if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
  157. return AE_OK;
  158. handle = ACPI_HANDLE(chip->dev);
  159. pin = agpio->pin_table[0];
  160. if (pin <= 255) {
  161. char ev_name[5];
  162. sprintf(ev_name, "_%c%02X",
  163. agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
  164. pin);
  165. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  166. handler = acpi_gpio_irq_handler;
  167. }
  168. if (!handler) {
  169. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  170. handler = acpi_gpio_irq_handler_evt;
  171. }
  172. if (!handler)
  173. return AE_BAD_PARAMETER;
  174. pin = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  175. if (pin < 0)
  176. return AE_BAD_PARAMETER;
  177. desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
  178. if (IS_ERR(desc)) {
  179. dev_err(chip->dev, "Failed to request GPIO\n");
  180. return AE_ERROR;
  181. }
  182. gpiod_direction_input(desc);
  183. ret = gpiochip_lock_as_irq(chip, pin);
  184. if (ret) {
  185. dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
  186. goto fail_free_desc;
  187. }
  188. irq = gpiod_to_irq(desc);
  189. if (irq < 0) {
  190. dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
  191. goto fail_unlock_irq;
  192. }
  193. irqflags = IRQF_ONESHOT;
  194. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  195. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  196. irqflags |= IRQF_TRIGGER_HIGH;
  197. else
  198. irqflags |= IRQF_TRIGGER_LOW;
  199. } else {
  200. switch (agpio->polarity) {
  201. case ACPI_ACTIVE_HIGH:
  202. irqflags |= IRQF_TRIGGER_RISING;
  203. break;
  204. case ACPI_ACTIVE_LOW:
  205. irqflags |= IRQF_TRIGGER_FALLING;
  206. break;
  207. default:
  208. irqflags |= IRQF_TRIGGER_RISING |
  209. IRQF_TRIGGER_FALLING;
  210. break;
  211. }
  212. }
  213. event = kzalloc(sizeof(*event), GFP_KERNEL);
  214. if (!event)
  215. goto fail_unlock_irq;
  216. event->handle = evt_handle;
  217. event->irq = irq;
  218. event->pin = pin;
  219. event->desc = desc;
  220. ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
  221. "ACPI:Event", event);
  222. if (ret) {
  223. dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
  224. event->irq);
  225. goto fail_free_event;
  226. }
  227. list_add_tail(&event->node, &acpi_gpio->events);
  228. return AE_OK;
  229. fail_free_event:
  230. kfree(event);
  231. fail_unlock_irq:
  232. gpiochip_unlock_as_irq(chip, pin);
  233. fail_free_desc:
  234. gpiochip_free_own_desc(desc);
  235. return AE_ERROR;
  236. }
  237. /**
  238. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  239. * @chip: GPIO chip
  240. *
  241. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  242. * handled by ACPI event methods which need to be called from the GPIO
  243. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  244. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  245. * the acpi event methods for those pins.
  246. */
  247. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  248. {
  249. struct acpi_gpio_chip *acpi_gpio;
  250. acpi_handle handle;
  251. acpi_status status;
  252. if (!chip->dev || !chip->to_irq)
  253. return;
  254. handle = ACPI_HANDLE(chip->dev);
  255. if (!handle)
  256. return;
  257. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  258. if (ACPI_FAILURE(status))
  259. return;
  260. acpi_walk_resources(handle, "_AEI",
  261. acpi_gpiochip_request_interrupt, acpi_gpio);
  262. }
  263. EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
  264. /**
  265. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  266. * @chip: GPIO chip
  267. *
  268. * Free interrupts associated with GPIO ACPI event method for the given
  269. * GPIO chip.
  270. */
  271. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  272. {
  273. struct acpi_gpio_chip *acpi_gpio;
  274. struct acpi_gpio_event *event, *ep;
  275. acpi_handle handle;
  276. acpi_status status;
  277. if (!chip->dev || !chip->to_irq)
  278. return;
  279. handle = ACPI_HANDLE(chip->dev);
  280. if (!handle)
  281. return;
  282. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  283. if (ACPI_FAILURE(status))
  284. return;
  285. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  286. struct gpio_desc *desc;
  287. free_irq(event->irq, event);
  288. desc = event->desc;
  289. if (WARN_ON(IS_ERR(desc)))
  290. continue;
  291. gpiochip_unlock_as_irq(chip, event->pin);
  292. gpiochip_free_own_desc(desc);
  293. list_del(&event->node);
  294. kfree(event);
  295. }
  296. }
  297. EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
  298. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  299. const struct acpi_gpio_mapping *gpios)
  300. {
  301. if (adev && gpios) {
  302. adev->driver_gpios = gpios;
  303. return 0;
  304. }
  305. return -EINVAL;
  306. }
  307. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  308. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  309. const char *name, int index,
  310. struct acpi_reference_args *args)
  311. {
  312. const struct acpi_gpio_mapping *gm;
  313. if (!adev->driver_gpios)
  314. return false;
  315. for (gm = adev->driver_gpios; gm->name; gm++)
  316. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  317. const struct acpi_gpio_params *par = gm->data + index;
  318. args->adev = adev;
  319. args->args[0] = par->crs_entry_index;
  320. args->args[1] = par->line_index;
  321. args->args[2] = par->active_low;
  322. args->nargs = 3;
  323. return true;
  324. }
  325. return false;
  326. }
  327. struct acpi_gpio_lookup {
  328. struct acpi_gpio_info info;
  329. int index;
  330. int pin_index;
  331. bool active_low;
  332. struct acpi_device *adev;
  333. struct gpio_desc *desc;
  334. int n;
  335. };
  336. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  337. {
  338. struct acpi_gpio_lookup *lookup = data;
  339. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  340. return 1;
  341. if (lookup->n++ == lookup->index && !lookup->desc) {
  342. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  343. int pin_index = lookup->pin_index;
  344. if (pin_index >= agpio->pin_table_length)
  345. return 1;
  346. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  347. agpio->pin_table[pin_index]);
  348. lookup->info.gpioint =
  349. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  350. /*
  351. * ActiveLow is only specified for GpioInt resource. If
  352. * GpioIo is used then the only way to set the flag is
  353. * to use _DSD "gpios" property.
  354. */
  355. if (lookup->info.gpioint)
  356. lookup->info.active_low =
  357. agpio->polarity == ACPI_ACTIVE_LOW;
  358. }
  359. return 1;
  360. }
  361. static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
  362. struct acpi_gpio_info *info)
  363. {
  364. struct list_head res_list;
  365. int ret;
  366. INIT_LIST_HEAD(&res_list);
  367. ret = acpi_dev_get_resources(lookup->adev, &res_list, acpi_find_gpio,
  368. lookup);
  369. if (ret < 0)
  370. return ret;
  371. acpi_dev_free_resource_list(&res_list);
  372. if (!lookup->desc)
  373. return -ENOENT;
  374. if (info) {
  375. *info = lookup->info;
  376. if (lookup->active_low)
  377. info->active_low = lookup->active_low;
  378. }
  379. return 0;
  380. }
  381. static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
  382. const char *propname, int index,
  383. struct acpi_gpio_lookup *lookup)
  384. {
  385. struct acpi_reference_args args;
  386. int ret;
  387. memset(&args, 0, sizeof(args));
  388. ret = acpi_node_get_property_reference(fwnode, propname, index, &args);
  389. if (ret) {
  390. struct acpi_device *adev = to_acpi_device_node(fwnode);
  391. if (!adev)
  392. return ret;
  393. if (!acpi_get_driver_gpio_data(adev, propname, index, &args))
  394. return ret;
  395. }
  396. /*
  397. * The property was found and resolved, so need to lookup the GPIO based
  398. * on returned args.
  399. */
  400. lookup->adev = args.adev;
  401. if (args.nargs >= 2) {
  402. lookup->index = args.args[0];
  403. lookup->pin_index = args.args[1];
  404. /* 3rd argument, if present is used to specify active_low. */
  405. if (args.nargs >= 3)
  406. lookup->active_low = !!args.args[2];
  407. }
  408. return 0;
  409. }
  410. /**
  411. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  412. * @adev: pointer to a ACPI device to get GPIO from
  413. * @propname: Property name of the GPIO (optional)
  414. * @index: index of GpioIo/GpioInt resource (starting from %0)
  415. * @info: info pointer to fill in (optional)
  416. *
  417. * Function goes through ACPI resources for @adev and based on @index looks
  418. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  419. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  420. * are total %3 GPIO resources, the index goes from %0 to %2.
  421. *
  422. * If @propname is specified the GPIO is looked using device property. In
  423. * that case @index is used to select the GPIO entry in the property value
  424. * (in case of multiple).
  425. *
  426. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  427. * returned.
  428. *
  429. * Note: if the GPIO resource has multiple entries in the pin list, this
  430. * function only returns the first.
  431. */
  432. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  433. const char *propname, int index,
  434. struct acpi_gpio_info *info)
  435. {
  436. struct acpi_gpio_lookup lookup;
  437. int ret;
  438. if (!adev)
  439. return ERR_PTR(-ENODEV);
  440. memset(&lookup, 0, sizeof(lookup));
  441. lookup.index = index;
  442. if (propname) {
  443. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  444. ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
  445. propname, index, &lookup);
  446. if (ret)
  447. return ERR_PTR(ret);
  448. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
  449. dev_name(&lookup.adev->dev), lookup.index,
  450. lookup.pin_index, lookup.active_low);
  451. } else {
  452. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  453. lookup.adev = adev;
  454. }
  455. ret = acpi_gpio_resource_lookup(&lookup, info);
  456. return ret ? ERR_PTR(ret) : lookup.desc;
  457. }
  458. /**
  459. * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
  460. * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  461. * @propname: Property name of the GPIO
  462. * @index: index of GpioIo/GpioInt resource (starting from %0)
  463. * @info: info pointer to fill in (optional)
  464. *
  465. * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
  466. * Otherwise (ie. it is a data-only non-device object), use the property-based
  467. * GPIO lookup to get to the GPIO resource with the relevant information and use
  468. * that to obtain the GPIO descriptor to return.
  469. */
  470. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  471. const char *propname, int index,
  472. struct acpi_gpio_info *info)
  473. {
  474. struct acpi_gpio_lookup lookup;
  475. struct acpi_device *adev;
  476. int ret;
  477. adev = to_acpi_device_node(fwnode);
  478. if (adev)
  479. return acpi_get_gpiod_by_index(adev, propname, index, info);
  480. if (!is_acpi_data_node(fwnode))
  481. return ERR_PTR(-ENODEV);
  482. if (!propname)
  483. return ERR_PTR(-EINVAL);
  484. memset(&lookup, 0, sizeof(lookup));
  485. lookup.index = index;
  486. ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
  487. if (ret)
  488. return ERR_PTR(ret);
  489. ret = acpi_gpio_resource_lookup(&lookup, info);
  490. return ret ? ERR_PTR(ret) : lookup.desc;
  491. }
  492. /**
  493. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  494. * @adev: pointer to a ACPI device to get IRQ from
  495. * @index: index of GpioInt resource (starting from %0)
  496. *
  497. * If the device has one or more GpioInt resources, this function can be
  498. * used to translate from the GPIO offset in the resource to the Linux IRQ
  499. * number.
  500. *
  501. * Return: Linux IRQ number (>%0) on success, negative errno on failure.
  502. */
  503. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  504. {
  505. int idx, i;
  506. for (i = 0, idx = 0; idx <= index; i++) {
  507. struct acpi_gpio_info info;
  508. struct gpio_desc *desc;
  509. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  510. if (IS_ERR(desc))
  511. break;
  512. if (info.gpioint && idx++ == index)
  513. return gpiod_to_irq(desc);
  514. }
  515. return -ENOENT;
  516. }
  517. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  518. static acpi_status
  519. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  520. u32 bits, u64 *value, void *handler_context,
  521. void *region_context)
  522. {
  523. struct acpi_gpio_chip *achip = region_context;
  524. struct gpio_chip *chip = achip->chip;
  525. struct acpi_resource_gpio *agpio;
  526. struct acpi_resource *ares;
  527. int pin_index = (int)address;
  528. acpi_status status;
  529. bool pull_up;
  530. int length;
  531. int i;
  532. status = acpi_buffer_to_resource(achip->conn_info.connection,
  533. achip->conn_info.length, &ares);
  534. if (ACPI_FAILURE(status))
  535. return status;
  536. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  537. ACPI_FREE(ares);
  538. return AE_BAD_PARAMETER;
  539. }
  540. agpio = &ares->data.gpio;
  541. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  542. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  543. function == ACPI_WRITE)) {
  544. ACPI_FREE(ares);
  545. return AE_BAD_PARAMETER;
  546. }
  547. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  548. for (i = pin_index; i < length; ++i) {
  549. int pin = agpio->pin_table[i];
  550. struct acpi_gpio_connection *conn;
  551. struct gpio_desc *desc;
  552. bool found;
  553. pin = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  554. if (pin < 0) {
  555. status = AE_BAD_PARAMETER;
  556. goto out;
  557. }
  558. mutex_lock(&achip->conn_lock);
  559. found = false;
  560. list_for_each_entry(conn, &achip->conns, node) {
  561. if (conn->pin == pin) {
  562. found = true;
  563. desc = conn->desc;
  564. break;
  565. }
  566. }
  567. /*
  568. * The same GPIO can be shared between operation region and
  569. * event but only if the access here is ACPI_READ. In that
  570. * case we "borrow" the event GPIO instead.
  571. */
  572. if (!found && agpio->sharable == ACPI_SHARED &&
  573. function == ACPI_READ) {
  574. struct acpi_gpio_event *event;
  575. list_for_each_entry(event, &achip->events, node) {
  576. if (event->pin == pin) {
  577. desc = event->desc;
  578. found = true;
  579. break;
  580. }
  581. }
  582. }
  583. if (!found) {
  584. desc = gpiochip_request_own_desc(chip, pin,
  585. "ACPI:OpRegion");
  586. if (IS_ERR(desc)) {
  587. status = AE_ERROR;
  588. mutex_unlock(&achip->conn_lock);
  589. goto out;
  590. }
  591. switch (agpio->io_restriction) {
  592. case ACPI_IO_RESTRICT_INPUT:
  593. gpiod_direction_input(desc);
  594. break;
  595. case ACPI_IO_RESTRICT_OUTPUT:
  596. /*
  597. * ACPI GPIO resources don't contain an
  598. * initial value for the GPIO. Therefore we
  599. * deduce that value from the pull field
  600. * instead. If the pin is pulled up we
  601. * assume default to be high, otherwise
  602. * low.
  603. */
  604. gpiod_direction_output(desc, pull_up);
  605. break;
  606. default:
  607. /*
  608. * Assume that the BIOS has configured the
  609. * direction and pull accordingly.
  610. */
  611. break;
  612. }
  613. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  614. if (!conn) {
  615. status = AE_NO_MEMORY;
  616. gpiochip_free_own_desc(desc);
  617. mutex_unlock(&achip->conn_lock);
  618. goto out;
  619. }
  620. conn->pin = pin;
  621. conn->desc = desc;
  622. list_add_tail(&conn->node, &achip->conns);
  623. }
  624. mutex_unlock(&achip->conn_lock);
  625. if (function == ACPI_WRITE)
  626. gpiod_set_raw_value_cansleep(desc,
  627. !!((1 << i) & *value));
  628. else
  629. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  630. }
  631. out:
  632. ACPI_FREE(ares);
  633. return status;
  634. }
  635. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  636. {
  637. struct gpio_chip *chip = achip->chip;
  638. acpi_handle handle = ACPI_HANDLE(chip->dev);
  639. acpi_status status;
  640. INIT_LIST_HEAD(&achip->conns);
  641. mutex_init(&achip->conn_lock);
  642. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  643. acpi_gpio_adr_space_handler,
  644. NULL, achip);
  645. if (ACPI_FAILURE(status))
  646. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  647. }
  648. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  649. {
  650. struct gpio_chip *chip = achip->chip;
  651. acpi_handle handle = ACPI_HANDLE(chip->dev);
  652. struct acpi_gpio_connection *conn, *tmp;
  653. acpi_status status;
  654. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  655. acpi_gpio_adr_space_handler);
  656. if (ACPI_FAILURE(status)) {
  657. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  658. return;
  659. }
  660. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  661. gpiochip_free_own_desc(conn->desc);
  662. list_del(&conn->node);
  663. kfree(conn);
  664. }
  665. }
  666. void acpi_gpiochip_add(struct gpio_chip *chip)
  667. {
  668. struct acpi_gpio_chip *acpi_gpio;
  669. acpi_handle handle;
  670. acpi_status status;
  671. if (!chip || !chip->dev)
  672. return;
  673. handle = ACPI_HANDLE(chip->dev);
  674. if (!handle)
  675. return;
  676. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  677. if (!acpi_gpio) {
  678. dev_err(chip->dev,
  679. "Failed to allocate memory for ACPI GPIO chip\n");
  680. return;
  681. }
  682. acpi_gpio->chip = chip;
  683. INIT_LIST_HEAD(&acpi_gpio->events);
  684. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  685. if (ACPI_FAILURE(status)) {
  686. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  687. kfree(acpi_gpio);
  688. return;
  689. }
  690. acpi_gpiochip_request_regions(acpi_gpio);
  691. }
  692. void acpi_gpiochip_remove(struct gpio_chip *chip)
  693. {
  694. struct acpi_gpio_chip *acpi_gpio;
  695. acpi_handle handle;
  696. acpi_status status;
  697. if (!chip || !chip->dev)
  698. return;
  699. handle = ACPI_HANDLE(chip->dev);
  700. if (!handle)
  701. return;
  702. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  703. if (ACPI_FAILURE(status)) {
  704. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  705. return;
  706. }
  707. acpi_gpiochip_free_regions(acpi_gpio);
  708. acpi_detach_data(handle, acpi_gpio_chip_dh);
  709. kfree(acpi_gpio);
  710. }
  711. static unsigned int acpi_gpio_package_count(const union acpi_object *obj)
  712. {
  713. const union acpi_object *element = obj->package.elements;
  714. const union acpi_object *end = element + obj->package.count;
  715. unsigned int count = 0;
  716. while (element < end) {
  717. if (element->type == ACPI_TYPE_LOCAL_REFERENCE)
  718. count++;
  719. element++;
  720. }
  721. return count;
  722. }
  723. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  724. {
  725. unsigned int *count = data;
  726. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  727. *count += ares->data.gpio.pin_table_length;
  728. return 1;
  729. }
  730. /**
  731. * acpi_gpio_count - return the number of GPIOs associated with a
  732. * device / function or -ENOENT if no GPIO has been
  733. * assigned to the requested function.
  734. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  735. * @con_id: function within the GPIO consumer
  736. */
  737. int acpi_gpio_count(struct device *dev, const char *con_id)
  738. {
  739. struct acpi_device *adev = ACPI_COMPANION(dev);
  740. const union acpi_object *obj;
  741. const struct acpi_gpio_mapping *gm;
  742. int count = -ENOENT;
  743. int ret;
  744. char propname[32];
  745. unsigned int i;
  746. /* Try first from _DSD */
  747. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  748. if (con_id && strcmp(con_id, "gpios"))
  749. snprintf(propname, sizeof(propname), "%s-%s",
  750. con_id, gpio_suffixes[i]);
  751. else
  752. snprintf(propname, sizeof(propname), "%s",
  753. gpio_suffixes[i]);
  754. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  755. &obj);
  756. if (ret == 0) {
  757. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  758. count = 1;
  759. else if (obj->type == ACPI_TYPE_PACKAGE)
  760. count = acpi_gpio_package_count(obj);
  761. } else if (adev->driver_gpios) {
  762. for (gm = adev->driver_gpios; gm->name; gm++)
  763. if (strcmp(propname, gm->name) == 0) {
  764. count = gm->size;
  765. break;
  766. }
  767. }
  768. if (count >= 0)
  769. break;
  770. }
  771. /* Then from plain _CRS GPIOs */
  772. if (count < 0) {
  773. struct list_head resource_list;
  774. unsigned int crs_count = 0;
  775. INIT_LIST_HEAD(&resource_list);
  776. acpi_dev_get_resources(adev, &resource_list,
  777. acpi_find_gpio_count, &crs_count);
  778. acpi_dev_free_resource_list(&resource_list);
  779. if (crs_count > 0)
  780. count = crs_count;
  781. }
  782. return count;
  783. }