gpio / ACPI: Allocate ACPI specific data directly in acpi_gpiochip_add()
[linux-2.6-block.git] / drivers / gpio / gpiolib-acpi.c
CommitLineData
e29482e8
MN
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
13#include <linux/errno.h>
936e15dd 14#include <linux/gpio/consumer.h>
5ccff852 15#include <linux/gpio/driver.h>
e29482e8 16#include <linux/export.h>
e29482e8 17#include <linux/acpi.h>
0d1c28a4 18#include <linux/interrupt.h>
e29482e8 19
5ccff852
MW
20#include "gpiolib.h"
21
7fc7acb9
RW
22struct acpi_gpio_evt_pin {
23 struct list_head node;
24 acpi_handle *evt_handle;
25 unsigned int pin;
26 unsigned int irq;
27};
28
aa92b6f6
MW
29struct acpi_gpio_chip {
30 struct gpio_chip *chip;
31 struct list_head evt_pins;
32};
33
e29482e8
MN
34static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
35{
36 if (!gc->dev)
37 return false;
38
39 return ACPI_HANDLE(gc->dev) == data;
40}
41
42/**
936e15dd 43 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
e29482e8
MN
44 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
45 * @pin: ACPI GPIO pin number (0-based, controller-relative)
46 *
936e15dd
MW
47 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
48 * error value
e29482e8
MN
49 */
50
936e15dd 51static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
e29482e8
MN
52{
53 struct gpio_chip *chip;
54 acpi_handle handle;
55 acpi_status status;
56
57 status = acpi_get_handle(NULL, path, &handle);
58 if (ACPI_FAILURE(status))
936e15dd 59 return ERR_PTR(-ENODEV);
e29482e8
MN
60
61 chip = gpiochip_find(handle, acpi_gpiochip_find);
62 if (!chip)
936e15dd 63 return ERR_PTR(-ENODEV);
e29482e8 64
936e15dd
MW
65 if (pin < 0 || pin > chip->ngpio)
66 return ERR_PTR(-EINVAL);
e29482e8 67
390d82e3 68 return gpiochip_get_desc(chip, pin);
e29482e8 69}
0d1c28a4 70
0d1c28a4
MN
71static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
72{
73 acpi_handle handle = data;
74
75 acpi_evaluate_object(handle, NULL, NULL, NULL);
76
77 return IRQ_HANDLED;
78}
79
7fc7acb9
RW
80static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
81{
82 struct acpi_gpio_evt_pin *evt_pin = data;
7fc7acb9 83
524c1081 84 acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin);
7fc7acb9
RW
85
86 return IRQ_HANDLED;
87}
88
aa92b6f6 89static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
7fc7acb9
RW
90{
91 /* The address of this function is used as a key. */
92}
93
0d1c28a4
MN
94/**
95 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
aa92b6f6 96 * @acpi_gpio: ACPI GPIO chip
0d1c28a4
MN
97 *
98 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
99 * handled by ACPI event methods which need to be called from the GPIO
100 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
101 * gpio pins have acpi event methods and assigns interrupt handlers that calls
102 * the acpi event methods for those pins.
0d1c28a4 103 */
aa92b6f6 104static void acpi_gpiochip_request_interrupts(struct acpi_gpio_chip *acpi_gpio)
0d1c28a4
MN
105{
106 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
aa92b6f6 107 struct gpio_chip *chip = acpi_gpio->chip;
0d1c28a4 108 struct acpi_resource *res;
7fc7acb9 109 acpi_handle handle, evt_handle;
0d1c28a4 110 acpi_status status;
1107ca10
MN
111 unsigned int pin;
112 int irq, ret;
0d1c28a4
MN
113 char ev_name[5];
114
115 if (!chip->dev || !chip->to_irq)
116 return;
117
118 handle = ACPI_HANDLE(chip->dev);
119 if (!handle)
120 return;
121
aa92b6f6 122 INIT_LIST_HEAD(&acpi_gpio->evt_pins);
0d1c28a4 123
7fc7acb9
RW
124 /*
125 * If a GPIO interrupt has an ACPI event handler method, or _EVT is
126 * present, set up an interrupt handler that calls the ACPI event
127 * handler.
128 */
0d1c28a4
MN
129 for (res = buf.pointer;
130 res && (res->type != ACPI_RESOURCE_TYPE_END_TAG);
131 res = ACPI_NEXT_RESOURCE(res)) {
7fc7acb9
RW
132 irq_handler_t handler = NULL;
133 void *data;
0d1c28a4
MN
134
135 if (res->type != ACPI_RESOURCE_TYPE_GPIO ||
136 res->data.gpio.connection_type !=
137 ACPI_RESOURCE_GPIO_TYPE_INT)
138 continue;
139
140 pin = res->data.gpio.pin_table[0];
141 if (pin > chip->ngpio)
142 continue;
143
0d1c28a4
MN
144 irq = chip->to_irq(chip, pin);
145 if (irq < 0)
146 continue;
147
7fc7acb9
RW
148 if (pin <= 255) {
149 acpi_handle ev_handle;
150
151 sprintf(ev_name, "_%c%02X",
152 res->data.gpio.triggering ? 'E' : 'L', pin);
153 status = acpi_get_handle(handle, ev_name, &ev_handle);
154 if (ACPI_SUCCESS(status)) {
155 handler = acpi_gpio_irq_handler;
156 data = ev_handle;
157 }
158 }
aa92b6f6 159 if (!handler) {
7fc7acb9
RW
160 struct acpi_gpio_evt_pin *evt_pin;
161
aa92b6f6
MW
162 status = acpi_get_handle(handle, "_EVT", &evt_handle);
163 if (ACPI_FAILURE(status))
164 continue
165
7fc7acb9
RW
166 evt_pin = kzalloc(sizeof(*evt_pin), GFP_KERNEL);
167 if (!evt_pin)
168 continue;
169
aa92b6f6 170 list_add_tail(&evt_pin->node, &acpi_gpio->evt_pins);
7fc7acb9
RW
171 evt_pin->evt_handle = evt_handle;
172 evt_pin->pin = pin;
173 evt_pin->irq = irq;
174 handler = acpi_gpio_irq_handler_evt;
175 data = evt_pin;
176 }
177 if (!handler)
178 continue;
179
0d1c28a4 180 /* Assume BIOS sets the triggering, so no flags */
7fc7acb9
RW
181 ret = devm_request_threaded_irq(chip->dev, irq, NULL, handler,
182 0, "GPIO-signaled-ACPI-event",
183 data);
1107ca10
MN
184 if (ret)
185 dev_err(chip->dev,
186 "Failed to request IRQ %d ACPI event handler\n",
187 irq);
0d1c28a4
MN
188 }
189}
7fc7acb9 190
70b53411
MW
191/**
192 * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
aa92b6f6 193 * @acpi_gpio: ACPI GPIO chip
70b53411
MW
194 *
195 * Free interrupts associated with the _EVT method for the given GPIO chip.
196 *
197 * The remaining ACPI event interrupts associated with the chip are freed
198 * automatically.
199 */
aa92b6f6 200static void acpi_gpiochip_free_interrupts(struct acpi_gpio_chip *acpi_gpio)
70b53411 201{
70b53411 202 struct acpi_gpio_evt_pin *evt_pin, *ep;
aa92b6f6 203 struct gpio_chip *chip = acpi_gpio->chip;
70b53411
MW
204
205 if (!chip->dev || !chip->to_irq)
206 return;
207
aa92b6f6
MW
208 list_for_each_entry_safe_reverse(evt_pin, ep, &acpi_gpio->evt_pins,
209 node) {
70b53411
MW
210 devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
211 list_del(&evt_pin->node);
212 kfree(evt_pin);
213 }
70b53411 214}
70b53411 215
12028d2d
MW
216struct acpi_gpio_lookup {
217 struct acpi_gpio_info info;
218 int index;
936e15dd 219 struct gpio_desc *desc;
12028d2d
MW
220 int n;
221};
222
223static int acpi_find_gpio(struct acpi_resource *ares, void *data)
224{
225 struct acpi_gpio_lookup *lookup = data;
226
227 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
228 return 1;
229
936e15dd 230 if (lookup->n++ == lookup->index && !lookup->desc) {
12028d2d
MW
231 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
232
936e15dd
MW
233 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
234 agpio->pin_table[0]);
12028d2d
MW
235 lookup->info.gpioint =
236 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
e01f440a
MW
237 lookup->info.active_low =
238 agpio->polarity == ACPI_ACTIVE_LOW;
12028d2d
MW
239 }
240
241 return 1;
242}
243
244/**
936e15dd 245 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
12028d2d
MW
246 * @dev: pointer to a device to get GPIO from
247 * @index: index of GpioIo/GpioInt resource (starting from %0)
248 * @info: info pointer to fill in (optional)
249 *
250 * Function goes through ACPI resources for @dev and based on @index looks
936e15dd 251 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
12028d2d
MW
252 * and returns it. @index matches GpioIo/GpioInt resources only so if there
253 * are total %3 GPIO resources, the index goes from %0 to %2.
254 *
936e15dd 255 * If the GPIO cannot be translated or there is an error an ERR_PTR is
12028d2d
MW
256 * returned.
257 *
258 * Note: if the GPIO resource has multiple entries in the pin list, this
259 * function only returns the first.
260 */
936e15dd
MW
261struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
262 struct acpi_gpio_info *info)
12028d2d
MW
263{
264 struct acpi_gpio_lookup lookup;
265 struct list_head resource_list;
266 struct acpi_device *adev;
267 acpi_handle handle;
268 int ret;
269
270 if (!dev)
936e15dd 271 return ERR_PTR(-EINVAL);
12028d2d
MW
272
273 handle = ACPI_HANDLE(dev);
274 if (!handle || acpi_bus_get_device(handle, &adev))
936e15dd 275 return ERR_PTR(-ENODEV);
12028d2d
MW
276
277 memset(&lookup, 0, sizeof(lookup));
278 lookup.index = index;
12028d2d
MW
279
280 INIT_LIST_HEAD(&resource_list);
281 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
282 &lookup);
283 if (ret < 0)
936e15dd 284 return ERR_PTR(ret);
12028d2d
MW
285
286 acpi_dev_free_resource_list(&resource_list);
287
936e15dd 288 if (lookup.desc && info)
12028d2d
MW
289 *info = lookup.info;
290
a00580c2 291 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
12028d2d 292}
664e3e5a
MW
293
294void acpi_gpiochip_add(struct gpio_chip *chip)
295{
aa92b6f6
MW
296 struct acpi_gpio_chip *acpi_gpio;
297 acpi_handle handle;
298 acpi_status status;
299
300 handle = ACPI_HANDLE(chip->dev);
301 if (!handle)
302 return;
303
304 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
305 if (!acpi_gpio) {
306 dev_err(chip->dev,
307 "Failed to allocate memory for ACPI GPIO chip\n");
308 return;
309 }
310
311 acpi_gpio->chip = chip;
312
313 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
314 if (ACPI_FAILURE(status)) {
315 dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
316 kfree(acpi_gpio);
317 return;
318 }
319
320 acpi_gpiochip_request_interrupts(acpi_gpio);
664e3e5a
MW
321}
322
323void acpi_gpiochip_remove(struct gpio_chip *chip)
324{
aa92b6f6
MW
325 struct acpi_gpio_chip *acpi_gpio;
326 acpi_handle handle;
327 acpi_status status;
328
329 handle = ACPI_HANDLE(chip->dev);
330 if (!handle)
331 return;
332
333 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
334 if (ACPI_FAILURE(status)) {
335 dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
336 return;
337 }
338
339 acpi_gpiochip_free_interrupts(acpi_gpio);
340
341 acpi_detach_data(handle, acpi_gpio_chip_dh);
342 kfree(acpi_gpio);
664e3e5a 343}