gpiolib-acpi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  104. * error value
  105. */
  106. static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  107. {
  108. struct gpio_chip *chip;
  109. acpi_handle handle;
  110. acpi_status status;
  111. int offset;
  112. status = acpi_get_handle(NULL, path, &handle);
  113. if (ACPI_FAILURE(status))
  114. return ERR_PTR(-ENODEV);
  115. chip = gpiochip_find(handle, acpi_gpiochip_find);
  116. if (!chip)
  117. return ERR_PTR(-ENODEV);
  118. offset = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  119. if (offset < 0)
  120. return ERR_PTR(offset);
  121. return gpiochip_get_desc(chip, offset);
  122. }
  123. static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  124. {
  125. struct acpi_gpio_event *event = data;
  126. acpi_evaluate_object(event->handle, NULL, NULL, NULL);
  127. return IRQ_HANDLED;
  128. }
  129. static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  130. {
  131. struct acpi_gpio_event *event = data;
  132. acpi_execute_simple_method(event->handle, NULL, event->pin);
  133. return IRQ_HANDLED;
  134. }
  135. static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
  136. {
  137. /* The address of this function is used as a key. */
  138. }
  139. static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
  140. void *context)
  141. {
  142. struct acpi_gpio_chip *acpi_gpio = context;
  143. struct gpio_chip *chip = acpi_gpio->chip;
  144. struct acpi_resource_gpio *agpio;
  145. acpi_handle handle, evt_handle;
  146. struct acpi_gpio_event *event;
  147. irq_handler_t handler = NULL;
  148. struct gpio_desc *desc;
  149. unsigned long irqflags;
  150. int ret, pin, irq;
  151. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  152. return AE_OK;
  153. agpio = &ares->data.gpio;
  154. if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
  155. return AE_OK;
  156. handle = ACPI_HANDLE(chip->dev);
  157. pin = agpio->pin_table[0];
  158. if (pin <= 255) {
  159. char ev_name[5];
  160. sprintf(ev_name, "_%c%02X",
  161. agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
  162. pin);
  163. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  164. handler = acpi_gpio_irq_handler;
  165. }
  166. if (!handler) {
  167. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  168. handler = acpi_gpio_irq_handler_evt;
  169. }
  170. if (!handler)
  171. return AE_BAD_PARAMETER;
  172. pin = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  173. if (pin < 0)
  174. return AE_BAD_PARAMETER;
  175. desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
  176. if (IS_ERR(desc)) {
  177. dev_err(chip->dev, "Failed to request GPIO\n");
  178. return AE_ERROR;
  179. }
  180. gpiod_direction_input(desc);
  181. ret = gpiochip_lock_as_irq(chip, pin);
  182. if (ret) {
  183. dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
  184. goto fail_free_desc;
  185. }
  186. irq = gpiod_to_irq(desc);
  187. if (irq < 0) {
  188. dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
  189. goto fail_unlock_irq;
  190. }
  191. irqflags = IRQF_ONESHOT;
  192. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  193. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  194. irqflags |= IRQF_TRIGGER_HIGH;
  195. else
  196. irqflags |= IRQF_TRIGGER_LOW;
  197. } else {
  198. switch (agpio->polarity) {
  199. case ACPI_ACTIVE_HIGH:
  200. irqflags |= IRQF_TRIGGER_RISING;
  201. break;
  202. case ACPI_ACTIVE_LOW:
  203. irqflags |= IRQF_TRIGGER_FALLING;
  204. break;
  205. default:
  206. irqflags |= IRQF_TRIGGER_RISING |
  207. IRQF_TRIGGER_FALLING;
  208. break;
  209. }
  210. }
  211. event = kzalloc(sizeof(*event), GFP_KERNEL);
  212. if (!event)
  213. goto fail_unlock_irq;
  214. event->handle = evt_handle;
  215. event->irq = irq;
  216. event->pin = pin;
  217. event->desc = desc;
  218. ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
  219. "ACPI:Event", event);
  220. if (ret) {
  221. dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
  222. event->irq);
  223. goto fail_free_event;
  224. }
  225. list_add_tail(&event->node, &acpi_gpio->events);
  226. return AE_OK;
  227. fail_free_event:
  228. kfree(event);
  229. fail_unlock_irq:
  230. gpiochip_unlock_as_irq(chip, pin);
  231. fail_free_desc:
  232. gpiochip_free_own_desc(desc);
  233. return AE_ERROR;
  234. }
  235. /**
  236. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  237. * @chip: GPIO chip
  238. *
  239. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  240. * handled by ACPI event methods which need to be called from the GPIO
  241. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  242. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  243. * the acpi event methods for those pins.
  244. */
  245. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  246. {
  247. struct acpi_gpio_chip *acpi_gpio;
  248. acpi_handle handle;
  249. acpi_status status;
  250. if (!chip->dev || !chip->to_irq)
  251. return;
  252. handle = ACPI_HANDLE(chip->dev);
  253. if (!handle)
  254. return;
  255. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  256. if (ACPI_FAILURE(status))
  257. return;
  258. INIT_LIST_HEAD(&acpi_gpio->events);
  259. acpi_walk_resources(handle, "_AEI",
  260. acpi_gpiochip_request_interrupt, acpi_gpio);
  261. }
  262. /**
  263. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  264. * @chip: GPIO chip
  265. *
  266. * Free interrupts associated with GPIO ACPI event method for the given
  267. * GPIO chip.
  268. */
  269. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  270. {
  271. struct acpi_gpio_chip *acpi_gpio;
  272. struct acpi_gpio_event *event, *ep;
  273. acpi_handle handle;
  274. acpi_status status;
  275. if (!chip->dev || !chip->to_irq)
  276. return;
  277. handle = ACPI_HANDLE(chip->dev);
  278. if (!handle)
  279. return;
  280. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  281. if (ACPI_FAILURE(status))
  282. return;
  283. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  284. struct gpio_desc *desc;
  285. free_irq(event->irq, event);
  286. desc = event->desc;
  287. if (WARN_ON(IS_ERR(desc)))
  288. continue;
  289. gpiochip_unlock_as_irq(chip, event->pin);
  290. gpiochip_free_own_desc(desc);
  291. list_del(&event->node);
  292. kfree(event);
  293. }
  294. }
  295. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  296. const struct acpi_gpio_mapping *gpios)
  297. {
  298. if (adev && gpios) {
  299. adev->driver_gpios = gpios;
  300. return 0;
  301. }
  302. return -EINVAL;
  303. }
  304. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  305. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  306. const char *name, int index,
  307. struct acpi_reference_args *args)
  308. {
  309. const struct acpi_gpio_mapping *gm;
  310. if (!adev->driver_gpios)
  311. return false;
  312. for (gm = adev->driver_gpios; gm->name; gm++)
  313. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  314. const struct acpi_gpio_params *par = gm->data + index;
  315. args->adev = adev;
  316. args->args[0] = par->crs_entry_index;
  317. args->args[1] = par->line_index;
  318. args->args[2] = par->active_low;
  319. args->nargs = 3;
  320. return true;
  321. }
  322. return false;
  323. }
  324. struct acpi_gpio_lookup {
  325. struct acpi_gpio_info info;
  326. int index;
  327. int pin_index;
  328. struct gpio_desc *desc;
  329. int n;
  330. };
  331. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  332. {
  333. struct acpi_gpio_lookup *lookup = data;
  334. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  335. return 1;
  336. if (lookup->n++ == lookup->index && !lookup->desc) {
  337. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  338. int pin_index = lookup->pin_index;
  339. if (pin_index >= agpio->pin_table_length)
  340. return 1;
  341. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  342. agpio->pin_table[pin_index]);
  343. lookup->info.gpioint =
  344. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  345. /*
  346. * ActiveLow is only specified for GpioInt resource. If
  347. * GpioIo is used then the only way to set the flag is
  348. * to use _DSD "gpios" property.
  349. */
  350. if (lookup->info.gpioint)
  351. lookup->info.active_low =
  352. agpio->polarity == ACPI_ACTIVE_LOW;
  353. }
  354. return 1;
  355. }
  356. /**
  357. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  358. * @adev: pointer to a ACPI device to get GPIO from
  359. * @propname: Property name of the GPIO (optional)
  360. * @index: index of GpioIo/GpioInt resource (starting from %0)
  361. * @info: info pointer to fill in (optional)
  362. *
  363. * Function goes through ACPI resources for @adev and based on @index looks
  364. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  365. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  366. * are total %3 GPIO resources, the index goes from %0 to %2.
  367. *
  368. * If @propname is specified the GPIO is looked using device property. In
  369. * that case @index is used to select the GPIO entry in the property value
  370. * (in case of multiple).
  371. *
  372. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  373. * returned.
  374. *
  375. * Note: if the GPIO resource has multiple entries in the pin list, this
  376. * function only returns the first.
  377. */
  378. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  379. const char *propname, int index,
  380. struct acpi_gpio_info *info)
  381. {
  382. struct acpi_gpio_lookup lookup;
  383. struct list_head resource_list;
  384. bool active_low = false;
  385. int ret;
  386. if (!adev)
  387. return ERR_PTR(-ENODEV);
  388. memset(&lookup, 0, sizeof(lookup));
  389. lookup.index = index;
  390. if (propname) {
  391. struct acpi_reference_args args;
  392. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  393. memset(&args, 0, sizeof(args));
  394. ret = acpi_dev_get_property_reference(adev, propname,
  395. index, &args);
  396. if (ret) {
  397. bool found = acpi_get_driver_gpio_data(adev, propname,
  398. index, &args);
  399. if (!found)
  400. return ERR_PTR(ret);
  401. }
  402. /*
  403. * The property was found and resolved so need to
  404. * lookup the GPIO based on returned args instead.
  405. */
  406. adev = args.adev;
  407. if (args.nargs >= 2) {
  408. lookup.index = args.args[0];
  409. lookup.pin_index = args.args[1];
  410. /*
  411. * 3rd argument, if present is used to
  412. * specify active_low.
  413. */
  414. if (args.nargs >= 3)
  415. active_low = !!args.args[2];
  416. }
  417. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %zd %llu %llu %llu\n",
  418. dev_name(&adev->dev), args.nargs,
  419. args.args[0], args.args[1], args.args[2]);
  420. } else {
  421. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  422. }
  423. INIT_LIST_HEAD(&resource_list);
  424. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  425. &lookup);
  426. if (ret < 0)
  427. return ERR_PTR(ret);
  428. acpi_dev_free_resource_list(&resource_list);
  429. if (lookup.desc && info) {
  430. *info = lookup.info;
  431. if (active_low)
  432. info->active_low = active_low;
  433. }
  434. return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
  435. }
  436. static acpi_status
  437. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  438. u32 bits, u64 *value, void *handler_context,
  439. void *region_context)
  440. {
  441. struct acpi_gpio_chip *achip = region_context;
  442. struct gpio_chip *chip = achip->chip;
  443. struct acpi_resource_gpio *agpio;
  444. struct acpi_resource *ares;
  445. int pin_index = (int)address;
  446. acpi_status status;
  447. bool pull_up;
  448. int length;
  449. int i;
  450. status = acpi_buffer_to_resource(achip->conn_info.connection,
  451. achip->conn_info.length, &ares);
  452. if (ACPI_FAILURE(status))
  453. return status;
  454. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  455. ACPI_FREE(ares);
  456. return AE_BAD_PARAMETER;
  457. }
  458. agpio = &ares->data.gpio;
  459. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  460. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  461. function == ACPI_WRITE)) {
  462. ACPI_FREE(ares);
  463. return AE_BAD_PARAMETER;
  464. }
  465. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  466. for (i = pin_index; i < length; ++i) {
  467. int pin = agpio->pin_table[i];
  468. struct acpi_gpio_connection *conn;
  469. struct gpio_desc *desc;
  470. bool found;
  471. pin = acpi_gpiochip_pin_to_gpio_offset(chip, pin);
  472. if (pin < 0) {
  473. status = AE_BAD_PARAMETER;
  474. goto out;
  475. }
  476. mutex_lock(&achip->conn_lock);
  477. found = false;
  478. list_for_each_entry(conn, &achip->conns, node) {
  479. if (conn->pin == pin) {
  480. found = true;
  481. desc = conn->desc;
  482. break;
  483. }
  484. }
  485. if (!found) {
  486. desc = gpiochip_request_own_desc(chip, pin,
  487. "ACPI:OpRegion");
  488. if (IS_ERR(desc)) {
  489. status = AE_ERROR;
  490. mutex_unlock(&achip->conn_lock);
  491. goto out;
  492. }
  493. switch (agpio->io_restriction) {
  494. case ACPI_IO_RESTRICT_INPUT:
  495. gpiod_direction_input(desc);
  496. break;
  497. case ACPI_IO_RESTRICT_OUTPUT:
  498. /*
  499. * ACPI GPIO resources don't contain an
  500. * initial value for the GPIO. Therefore we
  501. * deduce that value from the pull field
  502. * instead. If the pin is pulled up we
  503. * assume default to be high, otherwise
  504. * low.
  505. */
  506. gpiod_direction_output(desc, pull_up);
  507. break;
  508. default:
  509. /*
  510. * Assume that the BIOS has configured the
  511. * direction and pull accordingly.
  512. */
  513. break;
  514. }
  515. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  516. if (!conn) {
  517. status = AE_NO_MEMORY;
  518. gpiochip_free_own_desc(desc);
  519. mutex_unlock(&achip->conn_lock);
  520. goto out;
  521. }
  522. conn->pin = pin;
  523. conn->desc = desc;
  524. list_add_tail(&conn->node, &achip->conns);
  525. }
  526. mutex_unlock(&achip->conn_lock);
  527. if (function == ACPI_WRITE)
  528. gpiod_set_raw_value_cansleep(desc,
  529. !!((1 << i) & *value));
  530. else
  531. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  532. }
  533. out:
  534. ACPI_FREE(ares);
  535. return status;
  536. }
  537. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  538. {
  539. struct gpio_chip *chip = achip->chip;
  540. acpi_handle handle = ACPI_HANDLE(chip->dev);
  541. acpi_status status;
  542. INIT_LIST_HEAD(&achip->conns);
  543. mutex_init(&achip->conn_lock);
  544. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  545. acpi_gpio_adr_space_handler,
  546. NULL, achip);
  547. if (ACPI_FAILURE(status))
  548. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  549. }
  550. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  551. {
  552. struct gpio_chip *chip = achip->chip;
  553. acpi_handle handle = ACPI_HANDLE(chip->dev);
  554. struct acpi_gpio_connection *conn, *tmp;
  555. acpi_status status;
  556. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  557. acpi_gpio_adr_space_handler);
  558. if (ACPI_FAILURE(status)) {
  559. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  560. return;
  561. }
  562. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  563. gpiochip_free_own_desc(conn->desc);
  564. list_del(&conn->node);
  565. kfree(conn);
  566. }
  567. }
  568. void acpi_gpiochip_add(struct gpio_chip *chip)
  569. {
  570. struct acpi_gpio_chip *acpi_gpio;
  571. acpi_handle handle;
  572. acpi_status status;
  573. if (!chip || !chip->dev)
  574. return;
  575. handle = ACPI_HANDLE(chip->dev);
  576. if (!handle)
  577. return;
  578. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  579. if (!acpi_gpio) {
  580. dev_err(chip->dev,
  581. "Failed to allocate memory for ACPI GPIO chip\n");
  582. return;
  583. }
  584. acpi_gpio->chip = chip;
  585. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  586. if (ACPI_FAILURE(status)) {
  587. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  588. kfree(acpi_gpio);
  589. return;
  590. }
  591. acpi_gpiochip_request_regions(acpi_gpio);
  592. }
  593. void acpi_gpiochip_remove(struct gpio_chip *chip)
  594. {
  595. struct acpi_gpio_chip *acpi_gpio;
  596. acpi_handle handle;
  597. acpi_status status;
  598. if (!chip || !chip->dev)
  599. return;
  600. handle = ACPI_HANDLE(chip->dev);
  601. if (!handle)
  602. return;
  603. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  604. if (ACPI_FAILURE(status)) {
  605. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  606. return;
  607. }
  608. acpi_gpiochip_free_regions(acpi_gpio);
  609. acpi_detach_data(handle, acpi_gpio_chip_dh);
  610. kfree(acpi_gpio);
  611. }
  612. static unsigned int acpi_gpio_package_count(const union acpi_object *obj)
  613. {
  614. const union acpi_object *element = obj->package.elements;
  615. const union acpi_object *end = element + obj->package.count;
  616. unsigned int count = 0;
  617. while (element < end) {
  618. if (element->type == ACPI_TYPE_LOCAL_REFERENCE)
  619. count++;
  620. element++;
  621. }
  622. return count;
  623. }
  624. static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
  625. {
  626. unsigned int *count = data;
  627. if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
  628. *count += ares->data.gpio.pin_table_length;
  629. return 1;
  630. }
  631. /**
  632. * acpi_gpio_count - return the number of GPIOs associated with a
  633. * device / function or -ENOENT if no GPIO has been
  634. * assigned to the requested function.
  635. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  636. * @con_id: function within the GPIO consumer
  637. */
  638. int acpi_gpio_count(struct device *dev, const char *con_id)
  639. {
  640. struct acpi_device *adev = ACPI_COMPANION(dev);
  641. const union acpi_object *obj;
  642. const struct acpi_gpio_mapping *gm;
  643. int count = -ENOENT;
  644. int ret;
  645. char propname[32];
  646. unsigned int i;
  647. /* Try first from _DSD */
  648. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  649. if (con_id && strcmp(con_id, "gpios"))
  650. snprintf(propname, sizeof(propname), "%s-%s",
  651. con_id, gpio_suffixes[i]);
  652. else
  653. snprintf(propname, sizeof(propname), "%s",
  654. gpio_suffixes[i]);
  655. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  656. &obj);
  657. if (ret == 0) {
  658. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
  659. count = 1;
  660. else if (obj->type == ACPI_TYPE_PACKAGE)
  661. count = acpi_gpio_package_count(obj);
  662. } else if (adev->driver_gpios) {
  663. for (gm = adev->driver_gpios; gm->name; gm++)
  664. if (strcmp(propname, gm->name) == 0) {
  665. count = gm->size;
  666. break;
  667. }
  668. }
  669. if (count >= 0)
  670. break;
  671. }
  672. /* Then from plain _CRS GPIOs */
  673. if (count < 0) {
  674. struct list_head resource_list;
  675. unsigned int crs_count = 0;
  676. INIT_LIST_HEAD(&resource_list);
  677. acpi_dev_get_resources(adev, &resource_list,
  678. acpi_find_gpio_count, &crs_count);
  679. acpi_dev_free_resource_list(&resource_list);
  680. if (crs_count > 0)
  681. count = crs_count;
  682. }
  683. return count;
  684. }