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/gpio/driver.h>
16 #include <linux/export.h>
17 #include <linux/acpi.h>
18 #include <linux/interrupt.h>
19 #include <linux/mutex.h>
23 struct acpi_gpio_event {
24 struct list_head node;
28 struct gpio_desc *desc;
31 struct acpi_gpio_connection {
32 struct list_head node;
34 struct gpio_desc *desc;
37 struct acpi_gpio_chip {
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.
43 struct acpi_connection_info conn_info;
44 struct list_head conns;
45 struct mutex conn_lock;
46 struct gpio_chip *chip;
47 struct list_head events;
50 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
55 return ACPI_HANDLE(gc->dev) == data;
59 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
60 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
61 * @pin: ACPI GPIO pin number (0-based, controller-relative)
63 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
67 static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
69 struct gpio_chip *chip;
73 status = acpi_get_handle(NULL, path, &handle);
74 if (ACPI_FAILURE(status))
75 return ERR_PTR(-ENODEV);
77 chip = gpiochip_find(handle, acpi_gpiochip_find);
79 return ERR_PTR(-ENODEV);
81 if (pin < 0 || pin > chip->ngpio)
82 return ERR_PTR(-EINVAL);
84 return gpiochip_get_desc(chip, pin);
87 static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
89 struct acpi_gpio_event *event = data;
91 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
96 static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
98 struct acpi_gpio_event *event = data;
100 acpi_execute_simple_method(event->handle, NULL, event->pin);
105 static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
107 /* The address of this function is used as a key. */
110 static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
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;
123 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
126 agpio = &ares->data.gpio;
127 if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
130 handle = ACPI_HANDLE(chip->dev);
131 pin = agpio->pin_table[0];
135 sprintf(ev_name, "_%c%02X",
136 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
138 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
139 handler = acpi_gpio_irq_handler;
142 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
143 handler = acpi_gpio_irq_handler_evt;
146 return AE_BAD_PARAMETER;
148 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
150 dev_err(chip->dev, "Failed to request GPIO\n");
154 gpiod_direction_input(desc);
156 ret = gpio_lock_as_irq(chip, pin);
158 dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
162 irq = gpiod_to_irq(desc);
164 dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
165 goto fail_unlock_irq;
168 irqflags = IRQF_ONESHOT;
169 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
170 if (agpio->polarity == ACPI_ACTIVE_HIGH)
171 irqflags |= IRQF_TRIGGER_HIGH;
173 irqflags |= IRQF_TRIGGER_LOW;
175 switch (agpio->polarity) {
176 case ACPI_ACTIVE_HIGH:
177 irqflags |= IRQF_TRIGGER_RISING;
179 case ACPI_ACTIVE_LOW:
180 irqflags |= IRQF_TRIGGER_FALLING;
183 irqflags |= IRQF_TRIGGER_RISING |
184 IRQF_TRIGGER_FALLING;
189 event = kzalloc(sizeof(*event), GFP_KERNEL);
191 goto fail_unlock_irq;
193 event->handle = evt_handle;
198 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
199 "ACPI:Event", event);
201 dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
203 goto fail_free_event;
206 list_add_tail(&event->node, &acpi_gpio->events);
212 gpio_unlock_as_irq(chip, pin);
214 gpiochip_free_own_desc(desc);
220 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
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.
229 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
231 struct acpi_gpio_chip *acpi_gpio;
235 if (!chip->dev || !chip->to_irq)
238 handle = ACPI_HANDLE(chip->dev);
242 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
243 if (ACPI_FAILURE(status))
246 INIT_LIST_HEAD(&acpi_gpio->events);
247 acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
248 acpi_gpiochip_request_interrupt, acpi_gpio);
252 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
255 * Free interrupts associated with GPIO ACPI event method for the given
258 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
260 struct acpi_gpio_chip *acpi_gpio;
261 struct acpi_gpio_event *event, *ep;
265 if (!chip->dev || !chip->to_irq)
268 handle = ACPI_HANDLE(chip->dev);
272 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
273 if (ACPI_FAILURE(status))
276 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
277 struct gpio_desc *desc;
279 free_irq(event->irq, event);
281 if (WARN_ON(IS_ERR(desc)))
283 gpio_unlock_as_irq(chip, event->pin);
284 gpiochip_free_own_desc(desc);
285 list_del(&event->node);
290 struct acpi_gpio_lookup {
291 struct acpi_gpio_info info;
293 struct gpio_desc *desc;
297 static int acpi_find_gpio(struct acpi_resource *ares, void *data)
299 struct acpi_gpio_lookup *lookup = data;
301 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
304 if (lookup->n++ == lookup->index && !lookup->desc) {
305 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
307 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
308 agpio->pin_table[0]);
309 lookup->info.gpioint =
310 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
311 lookup->info.active_low =
312 agpio->polarity == ACPI_ACTIVE_LOW;
319 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
320 * @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)
324 * Function goes through ACPI resources for @dev and based on @index looks
325 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
326 * 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.
329 * If the GPIO cannot be translated or there is an error an ERR_PTR is
332 * Note: if the GPIO resource has multiple entries in the pin list, this
333 * function only returns the first.
335 struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
336 struct acpi_gpio_info *info)
338 struct acpi_gpio_lookup lookup;
339 struct list_head resource_list;
340 struct acpi_device *adev;
345 return ERR_PTR(-EINVAL);
347 handle = ACPI_HANDLE(dev);
348 if (!handle || acpi_bus_get_device(handle, &adev))
349 return ERR_PTR(-ENODEV);
351 memset(&lookup, 0, sizeof(lookup));
352 lookup.index = index;
354 INIT_LIST_HEAD(&resource_list);
355 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
360 acpi_dev_free_resource_list(&resource_list);
362 if (lookup.desc && info)
365 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
369 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
370 u32 bits, u64 *value, void *handler_context,
371 void *region_context)
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 int pin_index = (int)address;
383 status = acpi_buffer_to_resource(achip->conn_info.connection,
384 achip->conn_info.length, &ares);
385 if (ACPI_FAILURE(status))
388 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
390 return AE_BAD_PARAMETER;
393 agpio = &ares->data.gpio;
394 pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
396 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
397 function == ACPI_WRITE)) {
399 return AE_BAD_PARAMETER;
402 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
403 for (i = pin_index; i < length; ++i) {
404 unsigned pin = agpio->pin_table[i];
405 struct acpi_gpio_connection *conn;
406 struct gpio_desc *desc;
409 mutex_lock(&achip->conn_lock);
412 list_for_each_entry(conn, &achip->conns, node) {
413 if (conn->pin == pin) {
420 desc = gpiochip_request_own_desc(chip, pin,
424 mutex_unlock(&achip->conn_lock);
428 switch (agpio->io_restriction) {
429 case ACPI_IO_RESTRICT_INPUT:
430 gpiod_direction_input(desc);
432 case ACPI_IO_RESTRICT_OUTPUT:
434 * ACPI GPIO resources don't contain an
435 * initial value for the GPIO. Therefore we
436 * deduce that value from the pull field
437 * instead. If the pin is pulled up we
438 * assume default to be high, otherwise
441 gpiod_direction_output(desc, pull_up);
445 * Assume that the BIOS has configured the
446 * direction and pull accordingly.
451 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
453 status = AE_NO_MEMORY;
454 gpiochip_free_own_desc(desc);
455 mutex_unlock(&achip->conn_lock);
461 list_add_tail(&conn->node, &achip->conns);
464 mutex_unlock(&achip->conn_lock);
466 if (function == ACPI_WRITE)
467 gpiod_set_raw_value_cansleep(desc,
468 !!((1 << i) & *value));
470 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
478 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
480 struct gpio_chip *chip = achip->chip;
481 acpi_handle handle = ACPI_HANDLE(chip->dev);
484 INIT_LIST_HEAD(&achip->conns);
485 mutex_init(&achip->conn_lock);
486 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
487 acpi_gpio_adr_space_handler,
489 if (ACPI_FAILURE(status))
490 dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
493 static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
495 struct gpio_chip *chip = achip->chip;
496 acpi_handle handle = ACPI_HANDLE(chip->dev);
497 struct acpi_gpio_connection *conn, *tmp;
500 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
501 acpi_gpio_adr_space_handler);
502 if (ACPI_FAILURE(status)) {
503 dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
507 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
508 gpiochip_free_own_desc(conn->desc);
509 list_del(&conn->node);
514 void acpi_gpiochip_add(struct gpio_chip *chip)
516 struct acpi_gpio_chip *acpi_gpio;
520 if (!chip || !chip->dev)
523 handle = ACPI_HANDLE(chip->dev);
527 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
530 "Failed to allocate memory for ACPI GPIO chip\n");
534 acpi_gpio->chip = chip;
536 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
537 if (ACPI_FAILURE(status)) {
538 dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
543 acpi_gpiochip_request_regions(acpi_gpio);
546 void acpi_gpiochip_remove(struct gpio_chip *chip)
548 struct acpi_gpio_chip *acpi_gpio;
552 if (!chip || !chip->dev)
555 handle = ACPI_HANDLE(chip->dev);
559 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
560 if (ACPI_FAILURE(status)) {
561 dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
565 acpi_gpiochip_free_regions(acpi_gpio);
567 acpi_detach_data(handle, acpi_gpio_chip_dh);