gpiolib-acpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 contigous 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. INIT_LIST_HEAD(&acpi_gpio->events);
  261. acpi_walk_resources(handle, "_AEI",
  262. acpi_gpiochip_request_interrupt, acpi_gpio);
  263. }
  264. EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
  265. /**
  266. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  267. * @chip: GPIO chip
  268. *
  269. * Free interrupts associated with GPIO ACPI event method for the given
  270. * GPIO chip.
  271. */
  272. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  273. {
  274. struct acpi_gpio_chip *acpi_gpio;
  275. struct acpi_gpio_event *event, *ep;
  276. acpi_handle handle;
  277. acpi_status status;
  278. if (!chip->dev || !chip->to_irq)
  279. return;
  280. handle = ACPI_HANDLE(chip->dev);
  281. if (!handle)
  282. return;
  283. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  284. if (ACPI_FAILURE(status))
  285. return;
  286. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  287. struct gpio_desc *desc;
  288. free_irq(event->irq, event);
  289. desc = event->desc;
  290. if (WARN_ON(IS_ERR(desc)))
  291. continue;
  292. gpiochip_unlock_as_irq(chip, event->pin);
  293. gpiochip_free_own_desc(desc);
  294. list_del(&event->node);
  295. kfree(event);
  296. }
  297. }
  298. EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
  299. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  300. const struct acpi_gpio_mapping *gpios)
  301. {
  302. if (adev && gpios) {
  303. adev->driver_gpios = gpios;
  304. return 0;
  305. }
  306. return -EINVAL;
  307. }
  308. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  309. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  310. const char *name, int index,
  311. struct acpi_reference_args *args)
  312. {
  313. const struct acpi_gpio_mapping *gm;
  314. if (!adev->driver_gpios)
  315. return false;
  316. for (gm = adev->driver_gpios; gm->name; gm++)
  317. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  318. const struct acpi_gpio_params *par = gm->data + index;
  319. args->adev = adev;
  320. args->args[0] = par->crs_entry_index;
  321. args->args[1] = par->line_index;
  322. args->args[2] = par->active_low;
  323. args->nargs = 3;
  324. return true;
  325. }
  326. return false;
  327. }
  328. struct acpi_gpio_lookup {
  329. struct acpi_gpio_info info;
  330. int index;
  331. int pin_index;
  332. struct gpio_desc *desc;
  333. int n;
  334. };
  335. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  336. {
  337. struct acpi_gpio_lookup *lookup = data;
  338. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  339. return 1;
  340. if (lookup->n++ == lookup->index && !lookup->desc) {
  341. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  342. int pin_index = lookup->pin_index;
  343. if (pin_index >= agpio->pin_table_length)
  344. return 1;
  345. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  346. agpio->pin_table[pin_index]);
  347. lookup->info.gpioint =
  348. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  349. /*
  350. * ActiveLow is only specified for GpioInt resource. If
  351. * GpioIo is used then the only way to set the flag is
  352. * to use _DSD "gpios" property.
  353. */
  354. if (lookup->info.gpioint)
  355. lookup->info.active_low =
  356. agpio->polarity == ACPI_ACTIVE_LOW;
  357. }
  358. return 1;
  359. }
  360. /**
  361. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  362. * @adev: pointer to a ACPI device to get GPIO from
  363. * @propname: Property name of the GPIO (optional)
  364. * @index: index of GpioIo/GpioInt resource (starting from %0)
  365. * @info: info pointer to fill in (optional)
  366. *
  367. * Function goes through ACPI resources for @adev and based on @index looks
  368. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  369. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  370. * are total %3 GPIO resources, the index goes from %0 to %2.
  371. *
  372. * If @propname is specified the GPIO is looked using device property. In
  373. * that case @index is used to select the GPIO entry in the property value
  374. * (in case of multiple).
  375. *
  376. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  377. * returned.
  378. *
  379. * Note: if the GPIO resource has multiple entries in the pin list, this
  380. * function only returns the first.
  381. */
  382. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  383. const char *propname, int index,
  384. struct acpi_gpio_info *info)
  385. {
  386. struct acpi_gpio_lookup lookup;
  387. struct list_head resource_list;
  388. bool active_low = false;
  389. int ret;
  390. if (!adev)
  391. return ERR_PTR(-ENODEV);
  392. memset(&lookup, 0, sizeof(lookup));
  393. lookup.index = index;
  394. if (propname) {
  395. struct acpi_reference_args args;
  396. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  397. memset(&args, 0, sizeof(args));
  398. ret = acpi_dev_get_property_reference(adev, propname,
  399. index, &args);
  400. if (ret) {
  401. bool found = acpi_get_driver_gpio_data(adev, propname,
  402. index, &args);
  403. if (!found)
  404. return ERR_PTR(ret);
  405. }
  406. /*
  407. * The property was found and resolved so need to
  408. * lookup the GPIO based on returned args instead.
  409. */
  410. adev = args.adev;
  411. if (args.nargs >= 2) {
  412. lookup.index = args.args[0];
  413. lookup.pin_index = args.args[1];
  414. /*
  415. * 3rd argument, if present is used to
  416. * specify active_low.
  417. */
  418. if (args.nargs >= 3)
  419. active_low = !!args.args[2];
  420. }
  421. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %zd %llu %llu %llu\n",
  422. dev_name(&adev->dev), args.nargs,
  423. args.args[0], args.args[1], args.args[2]);
  424. } else {
  425. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  426. }
  427. INIT_LIST_HEAD(&resource_list);
  428. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  429. &lookup);
  430. if (ret < 0)
  431. return ERR_PTR(ret);
  432. acpi_dev_free_resource_list(&resource_list);
  433. if (lookup.desc && info) {
  434. *info = lookup.info;
  435. if (active_low)
  436. info->active_low = active_low;
  437. }
  438. return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
  439. }
  440. /**
  441. * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
  442. * @adev: pointer to a ACPI device to get IRQ from
  443. * @index: index of GpioInt resource (starting from %0)
  444. *
  445. * If the device has one or more GpioInt resources, this function can be
  446. * used to translate from the GPIO offset in the resource to the Linux IRQ
  447. * number.
  448. *
  449. * Return: Linux IRQ number (>%0) on success, negative errno on failure.
  450. */
  451. int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
  452. {
  453. int idx, i;
  454. for (i = 0, idx = 0; idx <= index; i++) {
  455. struct acpi_gpio_info info;
  456. struct gpio_desc *desc;
  457. desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
  458. if (IS_ERR(desc))
  459. break;
  460. if (info.gpioint && idx++ == index)
  461. return gpiod_to_irq(desc);
  462. }
  463. return -ENOENT;
  464. }
  465. EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
  466. static acpi_status
  467. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  468. u32 bits, u64 *value, void *handler_context,
  469. void *region_context)
  470. {
  471. struct acpi_gpio_chip *achip = region_context;
  472. struct gpio_chip *chip = achip->chip;
  473. struct acpi_resource_gpio *agpio;
  474. struct acpi_resource *ares;
  475. int pin_index = (int)address;
  476. acpi_status status;
  477. bool pull_up;
  478. int length;
  479. int i;
  480. status = acpi_buffer_to_resource(achip->conn_info.connection,
  481. achip->conn_info.length, &ares);
  482. if (ACPI_FAILURE(status))
  483. return status;
  484. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  485. ACPI_FREE(ares);
  486. return AE_BAD_PARAMETER;
  487. }
  488. agpio = &ares->data.gpio;
  489. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  490. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  491. function == ACPI_WRITE)) {
  492. ACPI_FREE(ares);
  493. return AE_BAD_PARAMETER;
  494. }
  495. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  496. for (i = pin_index; i < length; ++i) {
  497. int pin = agpio->pin_table[i];
  498. struct acpi_gpio_connection *conn;
  499. struct gpio_desc *desc;
  500. bool found;
  501. pin = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  502. if (pin < 0) {
  503. status = AE_BAD_PARAMETER;
  504. goto out;
  505. }
  506. mutex_lock(&achip->conn_lock);
  507. found = false;
  508. list_for_each_entry(conn, &achip->conns, node) {
  509. if (conn->pin == pin) {
  510. found = true;
  511. desc = conn->desc;
  512. break;
  513. }
  514. }
  515. if (!found) {
  516. desc = gpiochip_request_own_desc(chip, pin,
  517. "ACPI:OpRegion");
  518. if (IS_ERR(desc)) {
  519. status = AE_ERROR;
  520. mutex_unlock(&achip->conn_lock);
  521. goto out;
  522. }
  523. switch (agpio->io_restriction) {
  524. case ACPI_IO_RESTRICT_INPUT:
  525. gpiod_direction_input(desc);
  526. break;
  527. case ACPI_IO_RESTRICT_OUTPUT:
  528. /*
  529. * ACPI GPIO resources don't contain an
  530. * initial value for the GPIO. Therefore we
  531. * deduce that value from the pull field
  532. * instead. If the pin is pulled up we
  533. * assume default to be high, otherwise
  534. * low.
  535. */
  536. gpiod_direction_output(desc, pull_up);
  537. break;
  538. default:
  539. /*
  540. * Assume that the BIOS has configured the
  541. * direction and pull accordingly.
  542. */
  543. break;
  544. }
  545. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  546. if (!conn) {
  547. status = AE_NO_MEMORY;
  548. gpiochip_free_own_desc(desc);
  549. mutex_unlock(&achip->conn_lock);
  550. goto out;
  551. }
  552. conn->pin = pin;
  553. conn->desc = desc;
  554. list_add_tail(&conn->node, &achip->conns);
  555. }
  556. mutex_unlock(&achip->conn_lock);
  557. if (function == ACPI_WRITE)
  558. gpiod_set_raw_value_cansleep(desc,
  559. !!((1 << i) & *value));
  560. else
  561. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  562. }
  563. out:
  564. ACPI_FREE(ares);
  565. return status;
  566. }
  567. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  568. {
  569. struct gpio_chip *chip = achip->chip;
  570. acpi_handle handle = ACPI_HANDLE(chip->dev);
  571. acpi_status status;
  572. INIT_LIST_HEAD(&achip->conns);
  573. mutex_init(&achip->conn_lock);
  574. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  575. acpi_gpio_adr_space_handler,
  576. NULL, achip);
  577. if (ACPI_FAILURE(status))
  578. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  579. }
  580. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  581. {
  582. struct gpio_chip *chip = achip->chip;
  583. acpi_handle handle = ACPI_HANDLE(chip->dev);
  584. struct acpi_gpio_connection *conn, *tmp;
  585. acpi_status status;
  586. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  587. acpi_gpio_adr_space_handler);
  588. if (ACPI_FAILURE(status)) {
  589. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  590. return;
  591. }
  592. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  593. gpiochip_free_own_desc(conn->desc);
  594. list_del(&conn->node);
  595. kfree(conn);
  596. }
  597. }
  598. void acpi_gpiochip_add(struct gpio_chip *chip)
  599. {
  600. struct acpi_gpio_chip *acpi_gpio;
  601. acpi_handle handle;
  602. acpi_status status;
  603. if (!chip || !chip->dev)
  604. return;
  605. handle = ACPI_HANDLE(chip->dev);
  606. if (!handle)
  607. return;
  608. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  609. if (!acpi_gpio) {
  610. dev_err(chip->dev,
  611. "Failed to allocate memory for ACPI GPIO chip\n");
  612. return;
  613. }
  614. acpi_gpio->chip = chip;
  615. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  616. if (ACPI_FAILURE(status)) {
  617. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  618. kfree(acpi_gpio);
  619. return;
  620. }
  621. acpi_gpiochip_request_regions(acpi_gpio);
  622. }
  623. void acpi_gpiochip_remove(struct gpio_chip *chip)
  624. {
  625. struct acpi_gpio_chip *acpi_gpio;
  626. acpi_handle handle;
  627. acpi_status status;
  628. if (!chip || !chip->dev)
  629. return;
  630. handle = ACPI_HANDLE(chip->dev);
  631. if (!handle)
  632. return;
  633. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  634. if (ACPI_FAILURE(status)) {
  635. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  636. return;
  637. }
  638. acpi_gpiochip_free_regions(acpi_gpio);
  639. acpi_detach_data(handle, acpi_gpio_chip_dh);
  640. kfree(acpi_gpio);
  641. }
  642. static unsigned int acpi_gpio_package_count(const union acpi_object *obj)
  643. {
  644. const union acpi_object *element = obj->package.elements;
  645. const union acpi_object *end = element + obj->package.count;
  646. unsigned int count = 0;
  647. while (element < end) {
  648. if (element->type == ACPI_TYPE_LOCAL_REFERENCE)
  649. count++;
  650. element++;
  651. }
  652. return count;
  653. }
  654. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  655. {
  656. unsigned int *count = data;
  657. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  658. *count += ares->data.gpio.pin_table_length;
  659. return 1;
  660. }
  661. /**
  662. * acpi_gpio_count - return the number of GPIOs associated with a
  663. * device / function or -ENOENT if no GPIO has been
  664. * assigned to the requested function.
  665. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  666. * @con_id: function within the GPIO consumer
  667. */
  668. int acpi_gpio_count(struct device *dev, const char *con_id)
  669. {
  670. struct acpi_device *adev = ACPI_COMPANION(dev);
  671. const union acpi_object *obj;
  672. const struct acpi_gpio_mapping *gm;
  673. int count = -ENOENT;
  674. int ret;
  675. char propname[32];
  676. unsigned int i;
  677. /* Try first from _DSD */
  678. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  679. if (con_id && strcmp(con_id, "gpios"))
  680. snprintf(propname, sizeof(propname), "%s-%s",
  681. con_id, gpio_suffixes[i]);
  682. else
  683. snprintf(propname, sizeof(propname), "%s",
  684. gpio_suffixes[i]);
  685. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  686. &obj);
  687. if (ret == 0) {
  688. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  689. count = 1;
  690. else if (obj->type == ACPI_TYPE_PACKAGE)
  691. count = acpi_gpio_package_count(obj);
  692. } else if (adev->driver_gpios) {
  693. for (gm = adev->driver_gpios; gm->name; gm++)
  694. if (strcmp(propname, gm->name) == 0) {
  695. count = gm->size;
  696. break;
  697. }
  698. }
  699. if (count >= 0)
  700. break;
  701. }
  702. /* Then from plain _CRS GPIOs */
  703. if (count < 0) {
  704. struct list_head resource_list;
  705. unsigned int crs_count = 0;
  706. INIT_LIST_HEAD(&resource_list);
  707. acpi_dev_get_resources(adev, &resource_list,
  708. acpi_find_gpio_count, &crs_count);
  709. acpi_dev_free_resource_list(&resource_list);
  710. if (crs_count > 0)
  711. count = crs_count;
  712. }
  713. return count;
  714. }