blob: d5be56fe689ee43a01cfde01ca0040d0b9b1afae [file] [log] [blame]
Mathias Nymane29482e2012-11-30 12:37:36 +01001/*
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
13#include <linux/errno.h>
Mika Westerberg936e15d2013-10-10 11:01:08 +030014#include <linux/gpio/consumer.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020015#include <linux/gpio/driver.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010016#include <linux/export.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010017#include <linux/acpi.h>
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020018#include <linux/interrupt.h>
Mika Westerberg473ed7b2014-03-14 17:58:07 +020019#include <linux/mutex.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010020
Mika Westerberg5ccff852014-01-08 12:40:56 +020021#include "gpiolib.h"
22
Mika Westerberg4b01a142014-03-10 14:54:52 +020023struct acpi_gpio_event {
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020024 struct list_head node;
Mika Westerberg4b01a142014-03-10 14:54:52 +020025 acpi_handle handle;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020026 unsigned int pin;
27 unsigned int irq;
28};
29
Mika Westerberg473ed7b2014-03-14 17:58:07 +020030struct acpi_gpio_connection {
31 struct list_head node;
32 struct gpio_desc *desc;
33};
34
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020035struct acpi_gpio_chip {
Mika Westerberg473ed7b2014-03-14 17:58:07 +020036 /*
37 * ACPICA requires that the first field of the context parameter
38 * passed to acpi_install_address_space_handler() is large enough
39 * to hold struct acpi_connection_info.
40 */
41 struct acpi_connection_info conn_info;
42 struct list_head conns;
43 struct mutex conn_lock;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020044 struct gpio_chip *chip;
Mika Westerberg4b01a142014-03-10 14:54:52 +020045 struct list_head events;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020046};
47
Mathias Nymane29482e2012-11-30 12:37:36 +010048static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
49{
50 if (!gc->dev)
51 return false;
52
53 return ACPI_HANDLE(gc->dev) == data;
54}
55
56/**
Mika Westerberg936e15d2013-10-10 11:01:08 +030057 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +010058 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
59 * @pin: ACPI GPIO pin number (0-based, controller-relative)
60 *
Mika Westerberg936e15d2013-10-10 11:01:08 +030061 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
62 * error value
Mathias Nymane29482e2012-11-30 12:37:36 +010063 */
64
Mika Westerberg936e15d2013-10-10 11:01:08 +030065static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +010066{
67 struct gpio_chip *chip;
68 acpi_handle handle;
69 acpi_status status;
70
71 status = acpi_get_handle(NULL, path, &handle);
72 if (ACPI_FAILURE(status))
Mika Westerberg936e15d2013-10-10 11:01:08 +030073 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010074
75 chip = gpiochip_find(handle, acpi_gpiochip_find);
76 if (!chip)
Mika Westerberg936e15d2013-10-10 11:01:08 +030077 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010078
Mika Westerberg936e15d2013-10-10 11:01:08 +030079 if (pin < 0 || pin > chip->ngpio)
80 return ERR_PTR(-EINVAL);
Mathias Nymane29482e2012-11-30 12:37:36 +010081
Alexandre Courbot390d82e2014-02-09 17:43:55 +090082 return gpiochip_get_desc(chip, pin);
Mathias Nymane29482e2012-11-30 12:37:36 +010083}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020084
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020085static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
86{
Mika Westerberg6072b9d2014-03-10 14:54:53 +020087 struct acpi_gpio_event *event = data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020088
Mika Westerberg6072b9d2014-03-10 14:54:53 +020089 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020090
91 return IRQ_HANDLED;
92}
93
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020094static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
95{
Mika Westerberg4b01a142014-03-10 14:54:52 +020096 struct acpi_gpio_event *event = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020097
Mika Westerberg4b01a142014-03-10 14:54:52 +020098 acpi_execute_simple_method(event->handle, NULL, event->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020099
100 return IRQ_HANDLED;
101}
102
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200103static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200104{
105 /* The address of this function is used as a key. */
106}
107
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200108static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
109 void *context)
110{
111 struct acpi_gpio_chip *acpi_gpio = context;
112 struct gpio_chip *chip = acpi_gpio->chip;
113 struct acpi_resource_gpio *agpio;
114 acpi_handle handle, evt_handle;
115 struct acpi_gpio_event *event;
116 irq_handler_t handler = NULL;
117 struct gpio_desc *desc;
118 unsigned long irqflags;
119 int ret, pin, irq;
120
121 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
122 return AE_OK;
123
124 agpio = &ares->data.gpio;
125 if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
126 return AE_OK;
127
128 handle = ACPI_HANDLE(chip->dev);
129 pin = agpio->pin_table[0];
130
131 if (pin <= 255) {
132 char ev_name[5];
133 sprintf(ev_name, "_%c%02X",
134 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
135 pin);
136 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
137 handler = acpi_gpio_irq_handler;
138 }
139 if (!handler) {
140 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
141 handler = acpi_gpio_irq_handler_evt;
142 }
143 if (!handler)
144 return AE_BAD_PARAMETER;
145
146 desc = gpiochip_get_desc(chip, pin);
147 if (IS_ERR(desc)) {
148 dev_err(chip->dev, "Failed to get GPIO descriptor\n");
149 return AE_ERROR;
150 }
151
152 ret = gpiochip_request_own_desc(desc, "ACPI:Event");
153 if (ret) {
154 dev_err(chip->dev, "Failed to request GPIO\n");
155 return AE_ERROR;
156 }
157
158 gpiod_direction_input(desc);
159
160 ret = gpiod_lock_as_irq(desc);
161 if (ret) {
162 dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
163 goto fail_free_desc;
164 }
165
166 irq = gpiod_to_irq(desc);
167 if (irq < 0) {
168 dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
169 goto fail_unlock_irq;
170 }
171
172 irqflags = IRQF_ONESHOT;
173 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
174 if (agpio->polarity == ACPI_ACTIVE_HIGH)
175 irqflags |= IRQF_TRIGGER_HIGH;
176 else
177 irqflags |= IRQF_TRIGGER_LOW;
178 } else {
179 switch (agpio->polarity) {
180 case ACPI_ACTIVE_HIGH:
181 irqflags |= IRQF_TRIGGER_RISING;
182 break;
183 case ACPI_ACTIVE_LOW:
184 irqflags |= IRQF_TRIGGER_FALLING;
185 break;
186 default:
187 irqflags |= IRQF_TRIGGER_RISING |
188 IRQF_TRIGGER_FALLING;
189 break;
190 }
191 }
192
193 event = kzalloc(sizeof(*event), GFP_KERNEL);
194 if (!event)
195 goto fail_unlock_irq;
196
197 event->handle = evt_handle;
198 event->irq = irq;
199 event->pin = pin;
200
201 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
202 "ACPI:Event", event);
203 if (ret) {
204 dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
205 event->irq);
206 goto fail_free_event;
207 }
208
209 list_add_tail(&event->node, &acpi_gpio->events);
210 return AE_OK;
211
212fail_free_event:
213 kfree(event);
214fail_unlock_irq:
215 gpiod_unlock_as_irq(desc);
216fail_free_desc:
217 gpiochip_free_own_desc(desc);
218
219 return AE_ERROR;
220}
221
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200222/**
223 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200224 * @acpi_gpio: ACPI GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200225 *
226 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
227 * handled by ACPI event methods which need to be called from the GPIO
228 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
229 * gpio pins have acpi event methods and assigns interrupt handlers that calls
230 * the acpi event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200231 */
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200232static void acpi_gpiochip_request_interrupts(struct acpi_gpio_chip *acpi_gpio)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200233{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200234 struct gpio_chip *chip = acpi_gpio->chip;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200235
Mika Westerberge9595f82014-03-31 15:16:49 +0300236 if (!chip->to_irq)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200237 return;
238
Mika Westerberg4b01a142014-03-10 14:54:52 +0200239 INIT_LIST_HEAD(&acpi_gpio->events);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200240 acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
241 acpi_gpiochip_request_interrupt, acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200242}
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200243
Mika Westerberg70b53412013-10-10 11:01:07 +0300244/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200245 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200246 * @acpi_gpio: ACPI GPIO chip
Mika Westerberg70b53412013-10-10 11:01:07 +0300247 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200248 * Free interrupts associated with GPIO ACPI event method for the given
249 * GPIO chip.
Mika Westerberg70b53412013-10-10 11:01:07 +0300250 */
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200251static void acpi_gpiochip_free_interrupts(struct acpi_gpio_chip *acpi_gpio)
Mika Westerberg70b53412013-10-10 11:01:07 +0300252{
Mika Westerberg4b01a142014-03-10 14:54:52 +0200253 struct acpi_gpio_event *event, *ep;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200254 struct gpio_chip *chip = acpi_gpio->chip;
Mika Westerberg70b53412013-10-10 11:01:07 +0300255
Mika Westerberge9595f82014-03-31 15:16:49 +0300256 if (!chip->to_irq)
Mika Westerberg70b53412013-10-10 11:01:07 +0300257 return;
258
Mika Westerberg4b01a142014-03-10 14:54:52 +0200259 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200260 struct gpio_desc *desc;
261
262 free_irq(event->irq, event);
263 desc = gpiochip_get_desc(chip, event->pin);
264 if (WARN_ON(IS_ERR(desc)))
265 continue;
266 gpiod_unlock_as_irq(desc);
267 gpiochip_free_own_desc(desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200268 list_del(&event->node);
269 kfree(event);
Mika Westerberg70b53412013-10-10 11:01:07 +0300270 }
Mika Westerberg70b53412013-10-10 11:01:07 +0300271}
Mika Westerberg70b53412013-10-10 11:01:07 +0300272
Mika Westerberg12028d22013-04-03 13:56:54 +0300273struct acpi_gpio_lookup {
274 struct acpi_gpio_info info;
275 int index;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300276 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300277 int n;
278};
279
280static int acpi_find_gpio(struct acpi_resource *ares, void *data)
281{
282 struct acpi_gpio_lookup *lookup = data;
283
284 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
285 return 1;
286
Mika Westerberg936e15d2013-10-10 11:01:08 +0300287 if (lookup->n++ == lookup->index && !lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300288 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
289
Mika Westerberg936e15d2013-10-10 11:01:08 +0300290 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
291 agpio->pin_table[0]);
Mika Westerberg12028d22013-04-03 13:56:54 +0300292 lookup->info.gpioint =
293 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Mika Westerberge01f4402013-10-10 11:01:10 +0300294 lookup->info.active_low =
295 agpio->polarity == ACPI_ACTIVE_LOW;
Mika Westerberg12028d22013-04-03 13:56:54 +0300296 }
297
298 return 1;
299}
300
301/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300302 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg12028d22013-04-03 13:56:54 +0300303 * @dev: pointer to a device to get GPIO from
304 * @index: index of GpioIo/GpioInt resource (starting from %0)
305 * @info: info pointer to fill in (optional)
306 *
307 * Function goes through ACPI resources for @dev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300308 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300309 * and returns it. @index matches GpioIo/GpioInt resources only so if there
310 * are total %3 GPIO resources, the index goes from %0 to %2.
311 *
Mika Westerberg936e15d2013-10-10 11:01:08 +0300312 * If the GPIO cannot be translated or there is an error an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300313 * returned.
314 *
315 * Note: if the GPIO resource has multiple entries in the pin list, this
316 * function only returns the first.
317 */
Mika Westerberg936e15d2013-10-10 11:01:08 +0300318struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
319 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300320{
321 struct acpi_gpio_lookup lookup;
322 struct list_head resource_list;
323 struct acpi_device *adev;
324 acpi_handle handle;
325 int ret;
326
327 if (!dev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300328 return ERR_PTR(-EINVAL);
Mika Westerberg12028d22013-04-03 13:56:54 +0300329
330 handle = ACPI_HANDLE(dev);
331 if (!handle || acpi_bus_get_device(handle, &adev))
Mika Westerberg936e15d2013-10-10 11:01:08 +0300332 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300333
334 memset(&lookup, 0, sizeof(lookup));
335 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300336
337 INIT_LIST_HEAD(&resource_list);
338 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
339 &lookup);
340 if (ret < 0)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300341 return ERR_PTR(ret);
Mika Westerberg12028d22013-04-03 13:56:54 +0300342
343 acpi_dev_free_resource_list(&resource_list);
344
Mika Westerberg936e15d2013-10-10 11:01:08 +0300345 if (lookup.desc && info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300346 *info = lookup.info;
347
Mika Westerberga00580c2013-12-10 12:00:27 +0200348 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
Mika Westerberg12028d22013-04-03 13:56:54 +0300349}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200350
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200351static acpi_status
352acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
353 u32 bits, u64 *value, void *handler_context,
354 void *region_context)
355{
356 struct acpi_gpio_chip *achip = region_context;
357 struct gpio_chip *chip = achip->chip;
358 struct acpi_resource_gpio *agpio;
359 struct acpi_resource *ares;
360 acpi_status status;
361 bool pull_up;
362 int i;
363
364 status = acpi_buffer_to_resource(achip->conn_info.connection,
365 achip->conn_info.length, &ares);
366 if (ACPI_FAILURE(status))
367 return status;
368
369 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
370 ACPI_FREE(ares);
371 return AE_BAD_PARAMETER;
372 }
373
374 agpio = &ares->data.gpio;
375 pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
376
377 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
378 function == ACPI_WRITE)) {
379 ACPI_FREE(ares);
380 return AE_BAD_PARAMETER;
381 }
382
383 for (i = 0; i < agpio->pin_table_length; i++) {
384 unsigned pin = agpio->pin_table[i];
385 struct acpi_gpio_connection *conn;
386 struct gpio_desc *desc;
387 bool found;
388
389 desc = gpiochip_get_desc(chip, pin);
390 if (IS_ERR(desc)) {
391 status = AE_ERROR;
392 goto out;
393 }
394
395 mutex_lock(&achip->conn_lock);
396
397 found = false;
398 list_for_each_entry(conn, &achip->conns, node) {
399 if (conn->desc == desc) {
400 found = true;
401 break;
402 }
403 }
404 if (!found) {
405 int ret;
406
407 ret = gpiochip_request_own_desc(desc, "ACPI:OpRegion");
408 if (ret) {
409 status = AE_ERROR;
410 mutex_unlock(&achip->conn_lock);
411 goto out;
412 }
413
414 switch (agpio->io_restriction) {
415 case ACPI_IO_RESTRICT_INPUT:
416 gpiod_direction_input(desc);
417 break;
418 case ACPI_IO_RESTRICT_OUTPUT:
419 /*
420 * ACPI GPIO resources don't contain an
421 * initial value for the GPIO. Therefore we
422 * deduce that value from the pull field
423 * instead. If the pin is pulled up we
424 * assume default to be high, otherwise
425 * low.
426 */
427 gpiod_direction_output(desc, pull_up);
428 break;
429 default:
430 /*
431 * Assume that the BIOS has configured the
432 * direction and pull accordingly.
433 */
434 break;
435 }
436
437 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
438 if (!conn) {
439 status = AE_NO_MEMORY;
440 gpiochip_free_own_desc(desc);
441 mutex_unlock(&achip->conn_lock);
442 goto out;
443 }
444
445 conn->desc = desc;
446 list_add_tail(&conn->node, &achip->conns);
447 }
448
449 mutex_unlock(&achip->conn_lock);
450
451 if (function == ACPI_WRITE)
452 gpiod_set_raw_value(desc, !!((1 << i) & *value));
453 else
454 *value |= gpiod_get_raw_value(desc) << i;
455 }
456
457out:
458 ACPI_FREE(ares);
459 return status;
460}
461
462static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
463{
464 struct gpio_chip *chip = achip->chip;
465 acpi_handle handle = ACPI_HANDLE(chip->dev);
466 acpi_status status;
467
468 INIT_LIST_HEAD(&achip->conns);
469 mutex_init(&achip->conn_lock);
470 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
471 acpi_gpio_adr_space_handler,
472 NULL, achip);
473 if (ACPI_FAILURE(status))
474 dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
475}
476
477static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
478{
479 struct gpio_chip *chip = achip->chip;
480 acpi_handle handle = ACPI_HANDLE(chip->dev);
481 struct acpi_gpio_connection *conn, *tmp;
482 acpi_status status;
483
484 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
485 acpi_gpio_adr_space_handler);
486 if (ACPI_FAILURE(status)) {
487 dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
488 return;
489 }
490
491 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
492 gpiochip_free_own_desc(conn->desc);
493 list_del(&conn->node);
494 kfree(conn);
495 }
496}
497
Mika Westerberg664e3e52014-01-08 12:40:54 +0200498void acpi_gpiochip_add(struct gpio_chip *chip)
499{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200500 struct acpi_gpio_chip *acpi_gpio;
501 acpi_handle handle;
502 acpi_status status;
503
Mika Westerberge9595f82014-03-31 15:16:49 +0300504 if (!chip || !chip->dev)
505 return;
506
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200507 handle = ACPI_HANDLE(chip->dev);
508 if (!handle)
509 return;
510
511 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
512 if (!acpi_gpio) {
513 dev_err(chip->dev,
514 "Failed to allocate memory for ACPI GPIO chip\n");
515 return;
516 }
517
518 acpi_gpio->chip = chip;
519
520 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
521 if (ACPI_FAILURE(status)) {
522 dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
523 kfree(acpi_gpio);
524 return;
525 }
526
527 acpi_gpiochip_request_interrupts(acpi_gpio);
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200528 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200529}
530
531void acpi_gpiochip_remove(struct gpio_chip *chip)
532{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200533 struct acpi_gpio_chip *acpi_gpio;
534 acpi_handle handle;
535 acpi_status status;
536
Mika Westerberge9595f82014-03-31 15:16:49 +0300537 if (!chip || !chip->dev)
538 return;
539
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200540 handle = ACPI_HANDLE(chip->dev);
541 if (!handle)
542 return;
543
544 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
545 if (ACPI_FAILURE(status)) {
546 dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
547 return;
548 }
549
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200550 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200551 acpi_gpiochip_free_interrupts(acpi_gpio);
552
553 acpi_detach_data(handle, acpi_gpio_chip_dh);
554 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200555}