2 * ACPI helpers for GPIO API
4 * Copyright (C) 2012, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
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.
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/export.h>
16 #include <linux/acpi_gpio.h>
17 #include <linux/acpi.h>
18 #include <linux/interrupt.h>
20 struct acpi_gpio_evt_pin {
21 struct list_head node;
22 acpi_handle *evt_handle;
27 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
32 return ACPI_HANDLE(gc->dev) == data;
36 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor 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)
40 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
44 static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
46 struct gpio_chip *chip;
50 status = acpi_get_handle(NULL, path, &handle);
51 if (ACPI_FAILURE(status))
52 return ERR_PTR(-ENODEV);
54 chip = gpiochip_find(handle, acpi_gpiochip_find);
56 return ERR_PTR(-ENODEV);
58 if (pin < 0 || pin > chip->ngpio)
59 return ERR_PTR(-EINVAL);
61 return gpio_to_desc(chip->base + pin);
64 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
66 acpi_handle handle = data;
68 acpi_evaluate_object(handle, NULL, NULL, NULL);
73 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
75 struct acpi_gpio_evt_pin *evt_pin = data;
77 acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin);
82 static void acpi_gpio_evt_dh(acpi_handle handle, void *data)
84 /* The address of this function is used as a key. */
88 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
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.
97 static void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
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;
108 if (!chip->dev || !chip->to_irq)
111 handle = ACPI_HANDLE(chip->dev);
115 status = acpi_get_event_resources(handle, &buf);
116 if (ACPI_FAILURE(status))
119 status = acpi_get_handle(handle, "_EVT", &evt_handle);
120 if (ACPI_SUCCESS(status)) {
121 evt_pins = kzalloc(sizeof(*evt_pins), GFP_KERNEL);
123 INIT_LIST_HEAD(evt_pins);
124 status = acpi_attach_data(handle, acpi_gpio_evt_dh,
126 if (ACPI_FAILURE(status)) {
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
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;
144 if (res->type != ACPI_RESOURCE_TYPE_GPIO ||
145 res->data.gpio.connection_type !=
146 ACPI_RESOURCE_GPIO_TYPE_INT)
149 pin = res->data.gpio.pin_table[0];
150 if (pin > chip->ngpio)
153 irq = chip->to_irq(chip, pin);
158 acpi_handle ev_handle;
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;
168 if (!handler && evt_pins) {
169 struct acpi_gpio_evt_pin *evt_pin;
171 evt_pin = kzalloc(sizeof(*evt_pin), GFP_KERNEL);
175 list_add_tail(&evt_pin->node, evt_pins);
176 evt_pin->evt_handle = evt_handle;
179 handler = acpi_gpio_irq_handler_evt;
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",
191 "Failed to request IRQ %d ACPI event handler\n",
197 * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
200 * Free interrupts associated with the _EVT method for the given GPIO chip.
202 * The remaining ACPI event interrupts associated with the chip are freed
205 static void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
209 struct list_head *evt_pins;
210 struct acpi_gpio_evt_pin *evt_pin, *ep;
212 if (!chip->dev || !chip->to_irq)
215 handle = ACPI_HANDLE(chip->dev);
219 status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
220 if (ACPI_FAILURE(status))
223 list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
224 devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
225 list_del(&evt_pin->node);
229 acpi_detach_data(handle, acpi_gpio_evt_dh);
233 struct acpi_gpio_lookup {
234 struct acpi_gpio_info info;
236 struct gpio_desc *desc;
240 static int acpi_find_gpio(struct acpi_resource *ares, void *data)
242 struct acpi_gpio_lookup *lookup = data;
244 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
247 if (lookup->n++ == lookup->index && !lookup->desc) {
248 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
250 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
251 agpio->pin_table[0]);
252 lookup->info.gpioint =
253 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
254 lookup->info.active_low =
255 agpio->polarity == ACPI_ACTIVE_LOW;
262 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
263 * @dev: pointer to a device to get GPIO from
264 * @index: index of GpioIo/GpioInt resource (starting from %0)
265 * @info: info pointer to fill in (optional)
267 * Function goes through ACPI resources for @dev and based on @index looks
268 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
269 * and returns it. @index matches GpioIo/GpioInt resources only so if there
270 * are total %3 GPIO resources, the index goes from %0 to %2.
272 * If the GPIO cannot be translated or there is an error an ERR_PTR is
275 * Note: if the GPIO resource has multiple entries in the pin list, this
276 * function only returns the first.
278 struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
279 struct acpi_gpio_info *info)
281 struct acpi_gpio_lookup lookup;
282 struct list_head resource_list;
283 struct acpi_device *adev;
288 return ERR_PTR(-EINVAL);
290 handle = ACPI_HANDLE(dev);
291 if (!handle || acpi_bus_get_device(handle, &adev))
292 return ERR_PTR(-ENODEV);
294 memset(&lookup, 0, sizeof(lookup));
295 lookup.index = index;
297 INIT_LIST_HEAD(&resource_list);
298 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
303 acpi_dev_free_resource_list(&resource_list);
305 if (lookup.desc && info)
308 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
310 EXPORT_SYMBOL_GPL(acpi_get_gpiod_by_index);
312 void acpi_gpiochip_add(struct gpio_chip *chip)
314 acpi_gpiochip_request_interrupts(chip);
317 void acpi_gpiochip_remove(struct gpio_chip *chip)
319 acpi_gpiochip_free_interrupts(chip);