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