gpiolib-acpi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
  173. if (IS_ERR(desc)) {
  174. dev_err(chip->dev, "Failed to request GPIO\n");
  175. return AE_ERROR;
  176. }
  177. gpiod_direction_input(desc);
  178. ret = gpiochip_lock_as_irq(chip, pin);
  179. if (ret) {
  180. dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
  181. goto fail_free_desc;
  182. }
  183. irq = gpiod_to_irq(desc);
  184. if (irq < 0) {
  185. dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
  186. goto fail_unlock_irq;
  187. }
  188. irqflags = IRQF_ONESHOT;
  189. if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
  190. if (agpio->polarity == ACPI_ACTIVE_HIGH)
  191. irqflags |= IRQF_TRIGGER_HIGH;
  192. else
  193. irqflags |= IRQF_TRIGGER_LOW;
  194. } else {
  195. switch (agpio->polarity) {
  196. case ACPI_ACTIVE_HIGH:
  197. irqflags |= IRQF_TRIGGER_RISING;
  198. break;
  199. case ACPI_ACTIVE_LOW:
  200. irqflags |= IRQF_TRIGGER_FALLING;
  201. break;
  202. default:
  203. irqflags |= IRQF_TRIGGER_RISING |
  204. IRQF_TRIGGER_FALLING;
  205. break;
  206. }
  207. }
  208. event = kzalloc(sizeof(*event), GFP_KERNEL);
  209. if (!event)
  210. goto fail_unlock_irq;
  211. event->handle = evt_handle;
  212. event->irq = irq;
  213. event->pin = pin;
  214. event->desc = desc;
  215. ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
  216. "ACPI:Event", event);
  217. if (ret) {
  218. dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
  219. event->irq);
  220. goto fail_free_event;
  221. }
  222. list_add_tail(&event->node, &acpi_gpio->events);
  223. return AE_OK;
  224. fail_free_event:
  225. kfree(event);
  226. fail_unlock_irq:
  227. gpiochip_unlock_as_irq(chip, pin);
  228. fail_free_desc:
  229. gpiochip_free_own_desc(desc);
  230. return AE_ERROR;
  231. }
  232. /**
  233. * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  234. * @chip: GPIO chip
  235. *
  236. * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  237. * handled by ACPI event methods which need to be called from the GPIO
  238. * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  239. * gpio pins have acpi event methods and assigns interrupt handlers that calls
  240. * the acpi event methods for those pins.
  241. */
  242. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
  243. {
  244. struct acpi_gpio_chip *acpi_gpio;
  245. acpi_handle handle;
  246. acpi_status status;
  247. if (!chip->dev || !chip->to_irq)
  248. return;
  249. handle = ACPI_HANDLE(chip->dev);
  250. if (!handle)
  251. return;
  252. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  253. if (ACPI_FAILURE(status))
  254. return;
  255. INIT_LIST_HEAD(&acpi_gpio->events);
  256. acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
  257. acpi_gpiochip_request_interrupt, acpi_gpio);
  258. }
  259. /**
  260. * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
  261. * @chip: GPIO chip
  262. *
  263. * Free interrupts associated with GPIO ACPI event method for the given
  264. * GPIO chip.
  265. */
  266. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
  267. {
  268. struct acpi_gpio_chip *acpi_gpio;
  269. struct acpi_gpio_event *event, *ep;
  270. acpi_handle handle;
  271. acpi_status status;
  272. if (!chip->dev || !chip->to_irq)
  273. return;
  274. handle = ACPI_HANDLE(chip->dev);
  275. if (!handle)
  276. return;
  277. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  278. if (ACPI_FAILURE(status))
  279. return;
  280. list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
  281. struct gpio_desc *desc;
  282. free_irq(event->irq, event);
  283. desc = event->desc;
  284. if (WARN_ON(IS_ERR(desc)))
  285. continue;
  286. gpiochip_unlock_as_irq(chip, event->pin);
  287. gpiochip_free_own_desc(desc);
  288. list_del(&event->node);
  289. kfree(event);
  290. }
  291. }
  292. int acpi_dev_add_driver_gpios(struct acpi_device *adev,
  293. const struct acpi_gpio_mapping *gpios)
  294. {
  295. if (adev && gpios) {
  296. adev->driver_gpios = gpios;
  297. return 0;
  298. }
  299. return -EINVAL;
  300. }
  301. EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
  302. static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
  303. const char *name, int index,
  304. struct acpi_reference_args *args)
  305. {
  306. const struct acpi_gpio_mapping *gm;
  307. if (!adev->driver_gpios)
  308. return false;
  309. for (gm = adev->driver_gpios; gm->name; gm++)
  310. if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
  311. const struct acpi_gpio_params *par = gm->data + index;
  312. args->adev = adev;
  313. args->args[0] = par->crs_entry_index;
  314. args->args[1] = par->line_index;
  315. args->args[2] = par->active_low;
  316. args->nargs = 3;
  317. return true;
  318. }
  319. return false;
  320. }
  321. struct acpi_gpio_lookup {
  322. struct acpi_gpio_info info;
  323. int index;
  324. int pin_index;
  325. struct gpio_desc *desc;
  326. int n;
  327. };
  328. static int acpi_find_gpio(struct acpi_resource *ares, void *data)
  329. {
  330. struct acpi_gpio_lookup *lookup = data;
  331. if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
  332. return 1;
  333. if (lookup->n++ == lookup->index && !lookup->desc) {
  334. const struct acpi_resource_gpio *agpio = &ares->data.gpio;
  335. int pin_index = lookup->pin_index;
  336. if (pin_index >= agpio->pin_table_length)
  337. return 1;
  338. lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
  339. agpio->pin_table[pin_index]);
  340. lookup->info.gpioint =
  341. agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
  342. /*
  343. * ActiveLow is only specified for GpioInt resource. If
  344. * GpioIo is used then the only way to set the flag is
  345. * to use _DSD "gpios" property.
  346. */
  347. if (lookup->info.gpioint)
  348. lookup->info.active_low =
  349. agpio->polarity == ACPI_ACTIVE_LOW;
  350. }
  351. return 1;
  352. }
  353. /**
  354. * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  355. * @adev: pointer to a ACPI device to get GPIO from
  356. * @propname: Property name of the GPIO (optional)
  357. * @index: index of GpioIo/GpioInt resource (starting from %0)
  358. * @info: info pointer to fill in (optional)
  359. *
  360. * Function goes through ACPI resources for @adev and based on @index looks
  361. * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
  362. * and returns it. @index matches GpioIo/GpioInt resources only so if there
  363. * are total %3 GPIO resources, the index goes from %0 to %2.
  364. *
  365. * If @propname is specified the GPIO is looked using device property. In
  366. * that case @index is used to select the GPIO entry in the property value
  367. * (in case of multiple).
  368. *
  369. * If the GPIO cannot be translated or there is an error an ERR_PTR is
  370. * returned.
  371. *
  372. * Note: if the GPIO resource has multiple entries in the pin list, this
  373. * function only returns the first.
  374. */
  375. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  376. const char *propname, int index,
  377. struct acpi_gpio_info *info)
  378. {
  379. struct acpi_gpio_lookup lookup;
  380. struct list_head resource_list;
  381. bool active_low = false;
  382. int ret;
  383. if (!adev)
  384. return ERR_PTR(-ENODEV);
  385. memset(&lookup, 0, sizeof(lookup));
  386. lookup.index = index;
  387. if (propname) {
  388. struct acpi_reference_args args;
  389. dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
  390. memset(&args, 0, sizeof(args));
  391. ret = acpi_dev_get_property_reference(adev, propname,
  392. index, &args);
  393. if (ret) {
  394. bool found = acpi_get_driver_gpio_data(adev, propname,
  395. index, &args);
  396. if (!found)
  397. return ERR_PTR(ret);
  398. }
  399. /*
  400. * The property was found and resolved so need to
  401. * lookup the GPIO based on returned args instead.
  402. */
  403. adev = args.adev;
  404. if (args.nargs >= 2) {
  405. lookup.index = args.args[0];
  406. lookup.pin_index = args.args[1];
  407. /*
  408. * 3rd argument, if present is used to
  409. * specify active_low.
  410. */
  411. if (args.nargs >= 3)
  412. active_low = !!args.args[2];
  413. }
  414. dev_dbg(&adev->dev, "GPIO: _DSD returned %s %zd %llu %llu %llu\n",
  415. dev_name(&adev->dev), args.nargs,
  416. args.args[0], args.args[1], args.args[2]);
  417. } else {
  418. dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
  419. }
  420. INIT_LIST_HEAD(&resource_list);
  421. ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
  422. &lookup);
  423. if (ret < 0)
  424. return ERR_PTR(ret);
  425. acpi_dev_free_resource_list(&resource_list);
  426. if (lookup.desc && info) {
  427. *info = lookup.info;
  428. if (active_low)
  429. info->active_low = active_low;
  430. }
  431. return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
  432. }
  433. static acpi_status
  434. acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
  435. u32 bits, u64 *value, void *handler_context,
  436. void *region_context)
  437. {
  438. struct acpi_gpio_chip *achip = region_context;
  439. struct gpio_chip *chip = achip->chip;
  440. struct acpi_resource_gpio *agpio;
  441. struct acpi_resource *ares;
  442. int pin_index = (int)address;
  443. acpi_status status;
  444. bool pull_up;
  445. int length;
  446. int i;
  447. status = acpi_buffer_to_resource(achip->conn_info.connection,
  448. achip->conn_info.length, &ares);
  449. if (ACPI_FAILURE(status))
  450. return status;
  451. if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
  452. ACPI_FREE(ares);
  453. return AE_BAD_PARAMETER;
  454. }
  455. agpio = &ares->data.gpio;
  456. pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
  457. if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
  458. function == ACPI_WRITE)) {
  459. ACPI_FREE(ares);
  460. return AE_BAD_PARAMETER;
  461. }
  462. length = min(agpio->pin_table_length, (u16)(pin_index + bits));
  463. for (i = pin_index; i < length; ++i) {
  464. unsigned pin = agpio->pin_table[i];
  465. struct acpi_gpio_connection *conn;
  466. struct gpio_desc *desc;
  467. bool found;
  468. mutex_lock(&achip->conn_lock);
  469. found = false;
  470. list_for_each_entry(conn, &achip->conns, node) {
  471. if (conn->pin == pin) {
  472. found = true;
  473. desc = conn->desc;
  474. break;
  475. }
  476. }
  477. if (!found) {
  478. desc = gpiochip_request_own_desc(chip, pin,
  479. "ACPI:OpRegion");
  480. if (IS_ERR(desc)) {
  481. status = AE_ERROR;
  482. mutex_unlock(&achip->conn_lock);
  483. goto out;
  484. }
  485. switch (agpio->io_restriction) {
  486. case ACPI_IO_RESTRICT_INPUT:
  487. gpiod_direction_input(desc);
  488. break;
  489. case ACPI_IO_RESTRICT_OUTPUT:
  490. /*
  491. * ACPI GPIO resources don't contain an
  492. * initial value for the GPIO. Therefore we
  493. * deduce that value from the pull field
  494. * instead. If the pin is pulled up we
  495. * assume default to be high, otherwise
  496. * low.
  497. */
  498. gpiod_direction_output(desc, pull_up);
  499. break;
  500. default:
  501. /*
  502. * Assume that the BIOS has configured the
  503. * direction and pull accordingly.
  504. */
  505. break;
  506. }
  507. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  508. if (!conn) {
  509. status = AE_NO_MEMORY;
  510. gpiochip_free_own_desc(desc);
  511. mutex_unlock(&achip->conn_lock);
  512. goto out;
  513. }
  514. conn->pin = pin;
  515. conn->desc = desc;
  516. list_add_tail(&conn->node, &achip->conns);
  517. }
  518. mutex_unlock(&achip->conn_lock);
  519. if (function == ACPI_WRITE)
  520. gpiod_set_raw_value_cansleep(desc,
  521. !!((1 << i) & *value));
  522. else
  523. *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
  524. }
  525. out:
  526. ACPI_FREE(ares);
  527. return status;
  528. }
  529. static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
  530. {
  531. struct gpio_chip *chip = achip->chip;
  532. acpi_handle handle = ACPI_HANDLE(chip->dev);
  533. acpi_status status;
  534. INIT_LIST_HEAD(&achip->conns);
  535. mutex_init(&achip->conn_lock);
  536. status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  537. acpi_gpio_adr_space_handler,
  538. NULL, achip);
  539. if (ACPI_FAILURE(status))
  540. dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
  541. }
  542. static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
  543. {
  544. struct gpio_chip *chip = achip->chip;
  545. acpi_handle handle = ACPI_HANDLE(chip->dev);
  546. struct acpi_gpio_connection *conn, *tmp;
  547. acpi_status status;
  548. status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
  549. acpi_gpio_adr_space_handler);
  550. if (ACPI_FAILURE(status)) {
  551. dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
  552. return;
  553. }
  554. list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
  555. gpiochip_free_own_desc(conn->desc);
  556. list_del(&conn->node);
  557. kfree(conn);
  558. }
  559. }
  560. void acpi_gpiochip_add(struct gpio_chip *chip)
  561. {
  562. struct acpi_gpio_chip *acpi_gpio;
  563. acpi_handle handle;
  564. acpi_status status;
  565. if (!chip || !chip->dev)
  566. return;
  567. handle = ACPI_HANDLE(chip->dev);
  568. if (!handle)
  569. return;
  570. acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
  571. if (!acpi_gpio) {
  572. dev_err(chip->dev,
  573. "Failed to allocate memory for ACPI GPIO chip\n");
  574. return;
  575. }
  576. acpi_gpio->chip = chip;
  577. status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
  578. if (ACPI_FAILURE(status)) {
  579. dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
  580. kfree(acpi_gpio);
  581. return;
  582. }
  583. acpi_gpiochip_request_regions(acpi_gpio);
  584. }
  585. void acpi_gpiochip_remove(struct gpio_chip *chip)
  586. {
  587. struct acpi_gpio_chip *acpi_gpio;
  588. acpi_handle handle;
  589. acpi_status status;
  590. if (!chip || !chip->dev)
  591. return;
  592. handle = ACPI_HANDLE(chip->dev);
  593. if (!handle)
  594. return;
  595. status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
  596. if (ACPI_FAILURE(status)) {
  597. dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
  598. return;
  599. }
  600. acpi_gpiochip_free_regions(acpi_gpio);
  601. acpi_detach_data(handle, acpi_gpio_chip_dh);
  602. kfree(acpi_gpio);
  603. }