blob: f9103e72e2a41dcd1584c74aaa367c59fe423975 [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
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700148 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200149 if (IS_ERR(desc)) {
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200150 dev_err(chip->dev, "Failed to request GPIO\n");
151 return AE_ERROR;
152 }
153
154 gpiod_direction_input(desc);
155
Alexandre Courbotd74be6d2014-07-22 16:17:42 +0900156 ret = gpio_lock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200157 if (ret) {
158 dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
159 goto fail_free_desc;
160 }
161
162 irq = gpiod_to_irq(desc);
163 if (irq < 0) {
164 dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
165 goto fail_unlock_irq;
166 }
167
168 irqflags = IRQF_ONESHOT;
169 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
170 if (agpio->polarity == ACPI_ACTIVE_HIGH)
171 irqflags |= IRQF_TRIGGER_HIGH;
172 else
173 irqflags |= IRQF_TRIGGER_LOW;
174 } else {
175 switch (agpio->polarity) {
176 case ACPI_ACTIVE_HIGH:
177 irqflags |= IRQF_TRIGGER_RISING;
178 break;
179 case ACPI_ACTIVE_LOW:
180 irqflags |= IRQF_TRIGGER_FALLING;
181 break;
182 default:
183 irqflags |= IRQF_TRIGGER_RISING |
184 IRQF_TRIGGER_FALLING;
185 break;
186 }
187 }
188
189 event = kzalloc(sizeof(*event), GFP_KERNEL);
190 if (!event)
191 goto fail_unlock_irq;
192
193 event->handle = evt_handle;
194 event->irq = irq;
195 event->pin = pin;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700196 event->desc = desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200197
198 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
199 "ACPI:Event", event);
200 if (ret) {
201 dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
202 event->irq);
203 goto fail_free_event;
204 }
205
206 list_add_tail(&event->node, &acpi_gpio->events);
207 return AE_OK;
208
209fail_free_event:
210 kfree(event);
211fail_unlock_irq:
Alexandre Courbotd74be6d2014-07-22 16:17:42 +0900212 gpio_unlock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200213fail_free_desc:
214 gpiochip_free_own_desc(desc);
215
216 return AE_ERROR;
217}
218
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200219/**
220 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300221 * @chip: GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200222 *
223 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
224 * handled by ACPI event methods which need to be called from the GPIO
225 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
226 * gpio pins have acpi event methods and assigns interrupt handlers that calls
227 * the acpi event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200228 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300229void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200230{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300231 struct acpi_gpio_chip *acpi_gpio;
232 acpi_handle handle;
233 acpi_status status;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200234
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300235 if (!chip->dev || !chip->to_irq)
236 return;
237
238 handle = ACPI_HANDLE(chip->dev);
239 if (!handle)
240 return;
241
242 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
243 if (ACPI_FAILURE(status))
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200244 return;
245
Mika Westerberg4b01a142014-03-10 14:54:52 +0200246 INIT_LIST_HEAD(&acpi_gpio->events);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200247 acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
248 acpi_gpiochip_request_interrupt, acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200249}
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200250
Mika Westerberg70b53412013-10-10 11:01:07 +0300251/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200252 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300253 * @chip: GPIO chip
Mika Westerberg70b53412013-10-10 11:01:07 +0300254 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200255 * Free interrupts associated with GPIO ACPI event method for the given
256 * GPIO chip.
Mika Westerberg70b53412013-10-10 11:01:07 +0300257 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300258void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
Mika Westerberg70b53412013-10-10 11:01:07 +0300259{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300260 struct acpi_gpio_chip *acpi_gpio;
Mika Westerberg4b01a142014-03-10 14:54:52 +0200261 struct acpi_gpio_event *event, *ep;
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300262 acpi_handle handle;
263 acpi_status status;
Mika Westerberg70b53412013-10-10 11:01:07 +0300264
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300265 if (!chip->dev || !chip->to_irq)
266 return;
267
268 handle = ACPI_HANDLE(chip->dev);
269 if (!handle)
270 return;
271
272 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
273 if (ACPI_FAILURE(status))
Mika Westerberg70b53412013-10-10 11:01:07 +0300274 return;
275
Mika Westerberg4b01a142014-03-10 14:54:52 +0200276 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200277 struct gpio_desc *desc;
278
279 free_irq(event->irq, event);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700280 desc = event->desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200281 if (WARN_ON(IS_ERR(desc)))
282 continue;
Alexandre Courbotd74be6d2014-07-22 16:17:42 +0900283 gpio_unlock_as_irq(chip, event->pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200284 gpiochip_free_own_desc(desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200285 list_del(&event->node);
286 kfree(event);
Mika Westerberg70b53412013-10-10 11:01:07 +0300287 }
Mika Westerberg70b53412013-10-10 11:01:07 +0300288}
Mika Westerberg70b53412013-10-10 11:01:07 +0300289
Mika Westerberg12028d22013-04-03 13:56:54 +0300290struct acpi_gpio_lookup {
291 struct acpi_gpio_info info;
292 int index;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300293 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300294 int n;
295};
296
297static int acpi_find_gpio(struct acpi_resource *ares, void *data)
298{
299 struct acpi_gpio_lookup *lookup = data;
300
301 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
302 return 1;
303
Mika Westerberg936e15d2013-10-10 11:01:08 +0300304 if (lookup->n++ == lookup->index && !lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300305 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
306
Mika Westerberg936e15d2013-10-10 11:01:08 +0300307 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
308 agpio->pin_table[0]);
Mika Westerberg12028d22013-04-03 13:56:54 +0300309 lookup->info.gpioint =
310 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Mika Westerberge01f4402013-10-10 11:01:10 +0300311 lookup->info.active_low =
312 agpio->polarity == ACPI_ACTIVE_LOW;
Mika Westerberg12028d22013-04-03 13:56:54 +0300313 }
314
315 return 1;
316}
317
318/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300319 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg12028d22013-04-03 13:56:54 +0300320 * @dev: pointer to a device to get GPIO from
321 * @index: index of GpioIo/GpioInt resource (starting from %0)
322 * @info: info pointer to fill in (optional)
323 *
324 * Function goes through ACPI resources for @dev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300325 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300326 * and returns it. @index matches GpioIo/GpioInt resources only so if there
327 * are total %3 GPIO resources, the index goes from %0 to %2.
328 *
Mika Westerberg936e15d2013-10-10 11:01:08 +0300329 * If the GPIO cannot be translated or there is an error an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300330 * returned.
331 *
332 * Note: if the GPIO resource has multiple entries in the pin list, this
333 * function only returns the first.
334 */
Mika Westerberg936e15d2013-10-10 11:01:08 +0300335struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
336 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300337{
338 struct acpi_gpio_lookup lookup;
339 struct list_head resource_list;
340 struct acpi_device *adev;
341 acpi_handle handle;
342 int ret;
343
344 if (!dev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300345 return ERR_PTR(-EINVAL);
Mika Westerberg12028d22013-04-03 13:56:54 +0300346
347 handle = ACPI_HANDLE(dev);
348 if (!handle || acpi_bus_get_device(handle, &adev))
Mika Westerberg936e15d2013-10-10 11:01:08 +0300349 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300350
351 memset(&lookup, 0, sizeof(lookup));
352 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300353
354 INIT_LIST_HEAD(&resource_list);
355 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
356 &lookup);
357 if (ret < 0)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300358 return ERR_PTR(ret);
Mika Westerberg12028d22013-04-03 13:56:54 +0300359
360 acpi_dev_free_resource_list(&resource_list);
361
Mika Westerberg936e15d2013-10-10 11:01:08 +0300362 if (lookup.desc && info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300363 *info = lookup.info;
364
Mika Westerberga00580c2013-12-10 12:00:27 +0200365 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
Mika Westerberg12028d22013-04-03 13:56:54 +0300366}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200367
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200368static acpi_status
369acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
370 u32 bits, u64 *value, void *handler_context,
371 void *region_context)
372{
373 struct acpi_gpio_chip *achip = region_context;
374 struct gpio_chip *chip = achip->chip;
375 struct acpi_resource_gpio *agpio;
376 struct acpi_resource *ares;
377 acpi_status status;
378 bool pull_up;
379 int i;
380
381 status = acpi_buffer_to_resource(achip->conn_info.connection,
382 achip->conn_info.length, &ares);
383 if (ACPI_FAILURE(status))
384 return status;
385
386 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
387 ACPI_FREE(ares);
388 return AE_BAD_PARAMETER;
389 }
390
391 agpio = &ares->data.gpio;
392 pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
393
394 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
395 function == ACPI_WRITE)) {
396 ACPI_FREE(ares);
397 return AE_BAD_PARAMETER;
398 }
399
400 for (i = 0; i < agpio->pin_table_length; i++) {
401 unsigned pin = agpio->pin_table[i];
402 struct acpi_gpio_connection *conn;
403 struct gpio_desc *desc;
404 bool found;
405
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200406 mutex_lock(&achip->conn_lock);
407
408 found = false;
409 list_for_each_entry(conn, &achip->conns, node) {
Alexandre Courbote46cf322014-08-19 10:06:08 -0700410 if (conn->pin == pin) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200411 found = true;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700412 desc = conn->desc;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200413 break;
414 }
415 }
416 if (!found) {
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700417 desc = gpiochip_request_own_desc(chip, pin,
418 "ACPI:OpRegion");
Alexandre Courbote46cf322014-08-19 10:06:08 -0700419 if (IS_ERR(desc)) {
420 status = AE_ERROR;
421 mutex_unlock(&achip->conn_lock);
422 goto out;
423 }
424
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200425 switch (agpio->io_restriction) {
426 case ACPI_IO_RESTRICT_INPUT:
427 gpiod_direction_input(desc);
428 break;
429 case ACPI_IO_RESTRICT_OUTPUT:
430 /*
431 * ACPI GPIO resources don't contain an
432 * initial value for the GPIO. Therefore we
433 * deduce that value from the pull field
434 * instead. If the pin is pulled up we
435 * assume default to be high, otherwise
436 * low.
437 */
438 gpiod_direction_output(desc, pull_up);
439 break;
440 default:
441 /*
442 * Assume that the BIOS has configured the
443 * direction and pull accordingly.
444 */
445 break;
446 }
447
448 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
449 if (!conn) {
450 status = AE_NO_MEMORY;
451 gpiochip_free_own_desc(desc);
452 mutex_unlock(&achip->conn_lock);
453 goto out;
454 }
455
Alexandre Courbote46cf322014-08-19 10:06:08 -0700456 conn->pin = pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200457 conn->desc = desc;
458 list_add_tail(&conn->node, &achip->conns);
459 }
460
461 mutex_unlock(&achip->conn_lock);
462
463 if (function == ACPI_WRITE)
Aaron Ludc62b562014-05-20 17:07:38 +0800464 gpiod_set_raw_value_cansleep(desc,
465 !!((1 << i) & *value));
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200466 else
Aaron Ludc62b562014-05-20 17:07:38 +0800467 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200468 }
469
470out:
471 ACPI_FREE(ares);
472 return status;
473}
474
475static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
476{
477 struct gpio_chip *chip = achip->chip;
478 acpi_handle handle = ACPI_HANDLE(chip->dev);
479 acpi_status status;
480
481 INIT_LIST_HEAD(&achip->conns);
482 mutex_init(&achip->conn_lock);
483 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
484 acpi_gpio_adr_space_handler,
485 NULL, achip);
486 if (ACPI_FAILURE(status))
487 dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
488}
489
490static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
491{
492 struct gpio_chip *chip = achip->chip;
493 acpi_handle handle = ACPI_HANDLE(chip->dev);
494 struct acpi_gpio_connection *conn, *tmp;
495 acpi_status status;
496
497 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
498 acpi_gpio_adr_space_handler);
499 if (ACPI_FAILURE(status)) {
500 dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
501 return;
502 }
503
504 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
505 gpiochip_free_own_desc(conn->desc);
506 list_del(&conn->node);
507 kfree(conn);
508 }
509}
510
Mika Westerberg664e3e52014-01-08 12:40:54 +0200511void acpi_gpiochip_add(struct gpio_chip *chip)
512{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200513 struct acpi_gpio_chip *acpi_gpio;
514 acpi_handle handle;
515 acpi_status status;
516
Mika Westerberge9595f82014-03-31 15:16:49 +0300517 if (!chip || !chip->dev)
518 return;
519
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200520 handle = ACPI_HANDLE(chip->dev);
521 if (!handle)
522 return;
523
524 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
525 if (!acpi_gpio) {
526 dev_err(chip->dev,
527 "Failed to allocate memory for ACPI GPIO chip\n");
528 return;
529 }
530
531 acpi_gpio->chip = chip;
532
533 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
534 if (ACPI_FAILURE(status)) {
535 dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
536 kfree(acpi_gpio);
537 return;
538 }
539
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200540 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200541}
542
543void acpi_gpiochip_remove(struct gpio_chip *chip)
544{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200545 struct acpi_gpio_chip *acpi_gpio;
546 acpi_handle handle;
547 acpi_status status;
548
Mika Westerberge9595f82014-03-31 15:16:49 +0300549 if (!chip || !chip->dev)
550 return;
551
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200552 handle = ACPI_HANDLE(chip->dev);
553 if (!handle)
554 return;
555
556 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
557 if (ACPI_FAILURE(status)) {
558 dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
559 return;
560 }
561
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200562 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200563
564 acpi_detach_data(handle, acpi_gpio_chip_dh);
565 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200566}