blob: 84540025aa08a3f22b36b78859a442a66b542f0d [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;
Alexandre Courbote46cf322014-08-19 10:06:08 -070028 struct gpio_desc *desc;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020029};
30
Mika Westerberg473ed7b2014-03-14 17:58:07 +020031struct acpi_gpio_connection {
32 struct list_head node;
Alexandre Courbote46cf322014-08-19 10:06:08 -070033 unsigned int pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +020034 struct gpio_desc *desc;
35};
36
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020037struct acpi_gpio_chip {
Mika Westerberg473ed7b2014-03-14 17:58:07 +020038 /*
39 * ACPICA requires that the first field of the context parameter
40 * passed to acpi_install_address_space_handler() is large enough
41 * to hold struct acpi_connection_info.
42 */
43 struct acpi_connection_info conn_info;
44 struct list_head conns;
45 struct mutex conn_lock;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020046 struct gpio_chip *chip;
Mika Westerberg4b01a142014-03-10 14:54:52 +020047 struct list_head events;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020048};
49
Mathias Nymane29482e2012-11-30 12:37:36 +010050static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
51{
52 if (!gc->dev)
53 return false;
54
55 return ACPI_HANDLE(gc->dev) == data;
56}
57
58/**
Mika Westerberg936e15d2013-10-10 11:01:08 +030059 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +010060 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
61 * @pin: ACPI GPIO pin number (0-based, controller-relative)
62 *
Mika Westerberg936e15d2013-10-10 11:01:08 +030063 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
64 * error value
Mathias Nymane29482e2012-11-30 12:37:36 +010065 */
66
Mika Westerberg936e15d2013-10-10 11:01:08 +030067static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +010068{
69 struct gpio_chip *chip;
70 acpi_handle handle;
71 acpi_status status;
72
73 status = acpi_get_handle(NULL, path, &handle);
74 if (ACPI_FAILURE(status))
Mika Westerberg936e15d2013-10-10 11:01:08 +030075 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010076
77 chip = gpiochip_find(handle, acpi_gpiochip_find);
78 if (!chip)
Mika Westerberg936e15d2013-10-10 11:01:08 +030079 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010080
Mika Westerberg936e15d2013-10-10 11:01:08 +030081 if (pin < 0 || pin > chip->ngpio)
82 return ERR_PTR(-EINVAL);
Mathias Nymane29482e2012-11-30 12:37:36 +010083
Alexandre Courbot390d82e2014-02-09 17:43:55 +090084 return gpiochip_get_desc(chip, pin);
Mathias Nymane29482e2012-11-30 12:37:36 +010085}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020086
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020087static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
88{
Mika Westerberg6072b9d2014-03-10 14:54:53 +020089 struct acpi_gpio_event *event = data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020090
Mika Westerberg6072b9d2014-03-10 14:54:53 +020091 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020092
93 return IRQ_HANDLED;
94}
95
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020096static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
97{
Mika Westerberg4b01a142014-03-10 14:54:52 +020098 struct acpi_gpio_event *event = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020099
Mika Westerberg4b01a142014-03-10 14:54:52 +0200100 acpi_execute_simple_method(event->handle, NULL, event->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200101
102 return IRQ_HANDLED;
103}
104
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200105static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200106{
107 /* The address of this function is used as a key. */
108}
109
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200110static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
111 void *context)
112{
113 struct acpi_gpio_chip *acpi_gpio = context;
114 struct gpio_chip *chip = acpi_gpio->chip;
115 struct acpi_resource_gpio *agpio;
116 acpi_handle handle, evt_handle;
117 struct acpi_gpio_event *event;
118 irq_handler_t handler = NULL;
119 struct gpio_desc *desc;
120 unsigned long irqflags;
121 int ret, pin, irq;
122
123 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
124 return AE_OK;
125
126 agpio = &ares->data.gpio;
127 if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
128 return AE_OK;
129
130 handle = ACPI_HANDLE(chip->dev);
131 pin = agpio->pin_table[0];
132
133 if (pin <= 255) {
134 char ev_name[5];
135 sprintf(ev_name, "_%c%02X",
136 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
137 pin);
138 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
139 handler = acpi_gpio_irq_handler;
140 }
141 if (!handler) {
142 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
143 handler = acpi_gpio_irq_handler_evt;
144 }
145 if (!handler)
146 return AE_BAD_PARAMETER;
147
148 desc = gpiochip_get_desc(chip, pin);
149 if (IS_ERR(desc)) {
150 dev_err(chip->dev, "Failed to get GPIO descriptor\n");
151 return AE_ERROR;
152 }
153
154 ret = gpiochip_request_own_desc(desc, "ACPI:Event");
155 if (ret) {
156 dev_err(chip->dev, "Failed to request GPIO\n");
157 return AE_ERROR;
158 }
159
160 gpiod_direction_input(desc);
161
Alexandre Courbotd74be6d2014-07-22 16:17:42 +0900162 ret = gpio_lock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200163 if (ret) {
164 dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
165 goto fail_free_desc;
166 }
167
168 irq = gpiod_to_irq(desc);
169 if (irq < 0) {
170 dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
171 goto fail_unlock_irq;
172 }
173
174 irqflags = IRQF_ONESHOT;
175 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
176 if (agpio->polarity == ACPI_ACTIVE_HIGH)
177 irqflags |= IRQF_TRIGGER_HIGH;
178 else
179 irqflags |= IRQF_TRIGGER_LOW;
180 } else {
181 switch (agpio->polarity) {
182 case ACPI_ACTIVE_HIGH:
183 irqflags |= IRQF_TRIGGER_RISING;
184 break;
185 case ACPI_ACTIVE_LOW:
186 irqflags |= IRQF_TRIGGER_FALLING;
187 break;
188 default:
189 irqflags |= IRQF_TRIGGER_RISING |
190 IRQF_TRIGGER_FALLING;
191 break;
192 }
193 }
194
195 event = kzalloc(sizeof(*event), GFP_KERNEL);
196 if (!event)
197 goto fail_unlock_irq;
198
199 event->handle = evt_handle;
200 event->irq = irq;
201 event->pin = pin;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700202 event->desc = desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200203
204 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
205 "ACPI:Event", event);
206 if (ret) {
207 dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
208 event->irq);
209 goto fail_free_event;
210 }
211
212 list_add_tail(&event->node, &acpi_gpio->events);
213 return AE_OK;
214
215fail_free_event:
216 kfree(event);
217fail_unlock_irq:
Alexandre Courbotd74be6d2014-07-22 16:17:42 +0900218 gpio_unlock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200219fail_free_desc:
220 gpiochip_free_own_desc(desc);
221
222 return AE_ERROR;
223}
224
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200225/**
226 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300227 * @chip: GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200228 *
229 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
230 * handled by ACPI event methods which need to be called from the GPIO
231 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
232 * gpio pins have acpi event methods and assigns interrupt handlers that calls
233 * the acpi event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200234 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300235void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200236{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300237 struct acpi_gpio_chip *acpi_gpio;
238 acpi_handle handle;
239 acpi_status status;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200240
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300241 if (!chip->dev || !chip->to_irq)
242 return;
243
244 handle = ACPI_HANDLE(chip->dev);
245 if (!handle)
246 return;
247
248 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
249 if (ACPI_FAILURE(status))
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200250 return;
251
Mika Westerberg4b01a142014-03-10 14:54:52 +0200252 INIT_LIST_HEAD(&acpi_gpio->events);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200253 acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
254 acpi_gpiochip_request_interrupt, acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200255}
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200256
Mika Westerberg70b53412013-10-10 11:01:07 +0300257/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200258 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300259 * @chip: GPIO chip
Mika Westerberg70b53412013-10-10 11:01:07 +0300260 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200261 * Free interrupts associated with GPIO ACPI event method for the given
262 * GPIO chip.
Mika Westerberg70b53412013-10-10 11:01:07 +0300263 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300264void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
Mika Westerberg70b53412013-10-10 11:01:07 +0300265{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300266 struct acpi_gpio_chip *acpi_gpio;
Mika Westerberg4b01a142014-03-10 14:54:52 +0200267 struct acpi_gpio_event *event, *ep;
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300268 acpi_handle handle;
269 acpi_status status;
Mika Westerberg70b53412013-10-10 11:01:07 +0300270
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300271 if (!chip->dev || !chip->to_irq)
272 return;
273
274 handle = ACPI_HANDLE(chip->dev);
275 if (!handle)
276 return;
277
278 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
279 if (ACPI_FAILURE(status))
Mika Westerberg70b53412013-10-10 11:01:07 +0300280 return;
281
Mika Westerberg4b01a142014-03-10 14:54:52 +0200282 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200283 struct gpio_desc *desc;
284
285 free_irq(event->irq, event);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700286 desc = event->desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200287 if (WARN_ON(IS_ERR(desc)))
288 continue;
Alexandre Courbotd74be6d2014-07-22 16:17:42 +0900289 gpio_unlock_as_irq(chip, event->pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200290 gpiochip_free_own_desc(desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200291 list_del(&event->node);
292 kfree(event);
Mika Westerberg70b53412013-10-10 11:01:07 +0300293 }
Mika Westerberg70b53412013-10-10 11:01:07 +0300294}
Mika Westerberg70b53412013-10-10 11:01:07 +0300295
Mika Westerberg12028d22013-04-03 13:56:54 +0300296struct acpi_gpio_lookup {
297 struct acpi_gpio_info info;
298 int index;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300299 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300300 int n;
301};
302
303static int acpi_find_gpio(struct acpi_resource *ares, void *data)
304{
305 struct acpi_gpio_lookup *lookup = data;
306
307 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
308 return 1;
309
Mika Westerberg936e15d2013-10-10 11:01:08 +0300310 if (lookup->n++ == lookup->index && !lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300311 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
312
Mika Westerberg936e15d2013-10-10 11:01:08 +0300313 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
314 agpio->pin_table[0]);
Mika Westerberg12028d22013-04-03 13:56:54 +0300315 lookup->info.gpioint =
316 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Mika Westerberge01f4402013-10-10 11:01:10 +0300317 lookup->info.active_low =
318 agpio->polarity == ACPI_ACTIVE_LOW;
Mika Westerberg12028d22013-04-03 13:56:54 +0300319 }
320
321 return 1;
322}
323
324/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300325 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg12028d22013-04-03 13:56:54 +0300326 * @dev: pointer to a device to get GPIO from
327 * @index: index of GpioIo/GpioInt resource (starting from %0)
328 * @info: info pointer to fill in (optional)
329 *
330 * Function goes through ACPI resources for @dev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300331 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300332 * and returns it. @index matches GpioIo/GpioInt resources only so if there
333 * are total %3 GPIO resources, the index goes from %0 to %2.
334 *
Mika Westerberg936e15d2013-10-10 11:01:08 +0300335 * If the GPIO cannot be translated or there is an error an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300336 * returned.
337 *
338 * Note: if the GPIO resource has multiple entries in the pin list, this
339 * function only returns the first.
340 */
Mika Westerberg936e15d2013-10-10 11:01:08 +0300341struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
342 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300343{
344 struct acpi_gpio_lookup lookup;
345 struct list_head resource_list;
346 struct acpi_device *adev;
347 acpi_handle handle;
348 int ret;
349
350 if (!dev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300351 return ERR_PTR(-EINVAL);
Mika Westerberg12028d22013-04-03 13:56:54 +0300352
353 handle = ACPI_HANDLE(dev);
354 if (!handle || acpi_bus_get_device(handle, &adev))
Mika Westerberg936e15d2013-10-10 11:01:08 +0300355 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300356
357 memset(&lookup, 0, sizeof(lookup));
358 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300359
360 INIT_LIST_HEAD(&resource_list);
361 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
362 &lookup);
363 if (ret < 0)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300364 return ERR_PTR(ret);
Mika Westerberg12028d22013-04-03 13:56:54 +0300365
366 acpi_dev_free_resource_list(&resource_list);
367
Mika Westerberg936e15d2013-10-10 11:01:08 +0300368 if (lookup.desc && info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300369 *info = lookup.info;
370
Mika Westerberga00580c2013-12-10 12:00:27 +0200371 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
Mika Westerberg12028d22013-04-03 13:56:54 +0300372}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200373
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200374static acpi_status
375acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
376 u32 bits, u64 *value, void *handler_context,
377 void *region_context)
378{
379 struct acpi_gpio_chip *achip = region_context;
380 struct gpio_chip *chip = achip->chip;
381 struct acpi_resource_gpio *agpio;
382 struct acpi_resource *ares;
383 acpi_status status;
384 bool pull_up;
385 int i;
386
387 status = acpi_buffer_to_resource(achip->conn_info.connection,
388 achip->conn_info.length, &ares);
389 if (ACPI_FAILURE(status))
390 return status;
391
392 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
393 ACPI_FREE(ares);
394 return AE_BAD_PARAMETER;
395 }
396
397 agpio = &ares->data.gpio;
398 pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
399
400 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
401 function == ACPI_WRITE)) {
402 ACPI_FREE(ares);
403 return AE_BAD_PARAMETER;
404 }
405
406 for (i = 0; i < agpio->pin_table_length; i++) {
407 unsigned pin = agpio->pin_table[i];
408 struct acpi_gpio_connection *conn;
409 struct gpio_desc *desc;
410 bool found;
411
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200412 mutex_lock(&achip->conn_lock);
413
414 found = false;
415 list_for_each_entry(conn, &achip->conns, node) {
Alexandre Courbote46cf322014-08-19 10:06:08 -0700416 if (conn->pin == pin) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200417 found = true;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700418 desc = conn->desc;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200419 break;
420 }
421 }
422 if (!found) {
423 int ret;
424
Alexandre Courbote46cf322014-08-19 10:06:08 -0700425 desc = gpiochip_get_desc(chip, pin);
426 if (IS_ERR(desc)) {
427 status = AE_ERROR;
428 mutex_unlock(&achip->conn_lock);
429 goto out;
430 }
431
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200432 ret = gpiochip_request_own_desc(desc, "ACPI:OpRegion");
433 if (ret) {
434 status = AE_ERROR;
435 mutex_unlock(&achip->conn_lock);
436 goto out;
437 }
438
439 switch (agpio->io_restriction) {
440 case ACPI_IO_RESTRICT_INPUT:
441 gpiod_direction_input(desc);
442 break;
443 case ACPI_IO_RESTRICT_OUTPUT:
444 /*
445 * ACPI GPIO resources don't contain an
446 * initial value for the GPIO. Therefore we
447 * deduce that value from the pull field
448 * instead. If the pin is pulled up we
449 * assume default to be high, otherwise
450 * low.
451 */
452 gpiod_direction_output(desc, pull_up);
453 break;
454 default:
455 /*
456 * Assume that the BIOS has configured the
457 * direction and pull accordingly.
458 */
459 break;
460 }
461
462 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
463 if (!conn) {
464 status = AE_NO_MEMORY;
465 gpiochip_free_own_desc(desc);
466 mutex_unlock(&achip->conn_lock);
467 goto out;
468 }
469
Alexandre Courbote46cf322014-08-19 10:06:08 -0700470 conn->pin = pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200471 conn->desc = desc;
472 list_add_tail(&conn->node, &achip->conns);
473 }
474
475 mutex_unlock(&achip->conn_lock);
476
477 if (function == ACPI_WRITE)
Aaron Ludc62b562014-05-20 17:07:38 +0800478 gpiod_set_raw_value_cansleep(desc,
479 !!((1 << i) & *value));
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200480 else
Aaron Ludc62b562014-05-20 17:07:38 +0800481 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200482 }
483
484out:
485 ACPI_FREE(ares);
486 return status;
487}
488
489static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
490{
491 struct gpio_chip *chip = achip->chip;
492 acpi_handle handle = ACPI_HANDLE(chip->dev);
493 acpi_status status;
494
495 INIT_LIST_HEAD(&achip->conns);
496 mutex_init(&achip->conn_lock);
497 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
498 acpi_gpio_adr_space_handler,
499 NULL, achip);
500 if (ACPI_FAILURE(status))
501 dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
502}
503
504static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
505{
506 struct gpio_chip *chip = achip->chip;
507 acpi_handle handle = ACPI_HANDLE(chip->dev);
508 struct acpi_gpio_connection *conn, *tmp;
509 acpi_status status;
510
511 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
512 acpi_gpio_adr_space_handler);
513 if (ACPI_FAILURE(status)) {
514 dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
515 return;
516 }
517
518 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
519 gpiochip_free_own_desc(conn->desc);
520 list_del(&conn->node);
521 kfree(conn);
522 }
523}
524
Mika Westerberg664e3e52014-01-08 12:40:54 +0200525void acpi_gpiochip_add(struct gpio_chip *chip)
526{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200527 struct acpi_gpio_chip *acpi_gpio;
528 acpi_handle handle;
529 acpi_status status;
530
Mika Westerberge9595f82014-03-31 15:16:49 +0300531 if (!chip || !chip->dev)
532 return;
533
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200534 handle = ACPI_HANDLE(chip->dev);
535 if (!handle)
536 return;
537
538 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
539 if (!acpi_gpio) {
540 dev_err(chip->dev,
541 "Failed to allocate memory for ACPI GPIO chip\n");
542 return;
543 }
544
545 acpi_gpio->chip = chip;
546
547 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
548 if (ACPI_FAILURE(status)) {
549 dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
550 kfree(acpi_gpio);
551 return;
552 }
553
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200554 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200555}
556
557void acpi_gpiochip_remove(struct gpio_chip *chip)
558{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200559 struct acpi_gpio_chip *acpi_gpio;
560 acpi_handle handle;
561 acpi_status status;
562
Mika Westerberge9595f82014-03-31 15:16:49 +0300563 if (!chip || !chip->dev)
564 return;
565
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200566 handle = ACPI_HANDLE(chip->dev);
567 if (!handle)
568 return;
569
570 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
571 if (ACPI_FAILURE(status)) {
572 dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
573 return;
574 }
575
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200576 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200577
578 acpi_detach_data(handle, acpi_gpio_chip_dh);
579 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200580}