blob: ae0ffdce8bd57d4857b46cdc963f708923839982 [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>
Mathias Nymane29482e2012-11-30 12:37:36 +010015#include <linux/export.h>
16#include <linux/acpi_gpio.h>
17#include <linux/acpi.h>
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020018#include <linux/interrupt.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010019
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020020struct acpi_gpio_evt_pin {
21 struct list_head node;
22 acpi_handle *evt_handle;
23 unsigned int pin;
24 unsigned int irq;
25};
26
Mathias Nymane29482e2012-11-30 12:37:36 +010027static 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/**
Mika Westerberg936e15d2013-10-10 11:01:08 +030036 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +010037 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
38 * @pin: ACPI GPIO pin number (0-based, controller-relative)
39 *
Mika Westerberg936e15d2013-10-10 11:01:08 +030040 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
41 * error value
Mathias Nymane29482e2012-11-30 12:37:36 +010042 */
43
Mika Westerberg936e15d2013-10-10 11:01:08 +030044static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +010045{
46 struct gpio_chip *chip;
47 acpi_handle handle;
48 acpi_status status;
49
50 status = acpi_get_handle(NULL, path, &handle);
51 if (ACPI_FAILURE(status))
Mika Westerberg936e15d2013-10-10 11:01:08 +030052 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010053
54 chip = gpiochip_find(handle, acpi_gpiochip_find);
55 if (!chip)
Mika Westerberg936e15d2013-10-10 11:01:08 +030056 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010057
Mika Westerberg936e15d2013-10-10 11:01:08 +030058 if (pin < 0 || pin > chip->ngpio)
59 return ERR_PTR(-EINVAL);
Mathias Nymane29482e2012-11-30 12:37:36 +010060
Mika Westerberg936e15d2013-10-10 11:01:08 +030061 return gpio_to_desc(chip->base + pin);
Mathias Nymane29482e2012-11-30 12:37:36 +010062}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020063
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020064static 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
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020073static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
74{
75 struct acpi_gpio_evt_pin *evt_pin = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020076
Zhang Rui524c1082013-09-03 08:31:50 +080077 acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020078
79 return IRQ_HANDLED;
80}
81
82static void acpi_gpio_evt_dh(acpi_handle handle, void *data)
83{
84 /* The address of this function is used as a key. */
85}
86
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020087/**
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.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020096 */
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020097void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
98{
99 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
100 struct acpi_resource *res;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200101 acpi_handle handle, evt_handle;
102 struct list_head *evt_pins = NULL;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200103 acpi_status status;
Mathias Nyman1107ca12013-02-04 11:32:22 +0200104 unsigned int pin;
105 int irq, ret;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200106 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
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200119 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 }
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200132
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200133 /*
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 */
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200138 for (res = buf.pointer;
139 res && (res->type != ACPI_RESOURCE_TYPE_END_TAG);
140 res = ACPI_NEXT_RESOURCE(res)) {
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200141 irq_handler_t handler = NULL;
142 void *data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200143
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
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200153 irq = chip->to_irq(chip, pin);
154 if (irq < 0)
155 continue;
156
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200157 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
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200185 /* Assume BIOS sets the triggering, so no flags */
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200186 ret = devm_request_threaded_irq(chip->dev, irq, NULL, handler,
187 0, "GPIO-signaled-ACPI-event",
188 data);
Mathias Nyman1107ca12013-02-04 11:32:22 +0200189 if (ret)
190 dev_err(chip->dev,
191 "Failed to request IRQ %d ACPI event handler\n",
192 irq);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200193 }
194}
195EXPORT_SYMBOL(acpi_gpiochip_request_interrupts);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200196
Mika Westerberg70b53412013-10-10 11:01:07 +0300197/**
198 * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
199 * @chip: gpio chip
200 *
201 * Free interrupts associated with the _EVT method for the given GPIO chip.
202 *
203 * The remaining ACPI event interrupts associated with the chip are freed
204 * automatically.
205 */
206void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
207{
208 acpi_handle handle;
209 acpi_status status;
210 struct list_head *evt_pins;
211 struct acpi_gpio_evt_pin *evt_pin, *ep;
212
213 if (!chip->dev || !chip->to_irq)
214 return;
215
216 handle = ACPI_HANDLE(chip->dev);
217 if (!handle)
218 return;
219
220 status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
221 if (ACPI_FAILURE(status))
222 return;
223
224 list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
225 devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
226 list_del(&evt_pin->node);
227 kfree(evt_pin);
228 }
229
230 acpi_detach_data(handle, acpi_gpio_evt_dh);
231 kfree(evt_pins);
232}
233EXPORT_SYMBOL(acpi_gpiochip_free_interrupts);
234
Mika Westerberg12028d22013-04-03 13:56:54 +0300235struct acpi_gpio_lookup {
236 struct acpi_gpio_info info;
237 int index;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300238 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300239 int n;
240};
241
242static int acpi_find_gpio(struct acpi_resource *ares, void *data)
243{
244 struct acpi_gpio_lookup *lookup = data;
245
246 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
247 return 1;
248
Mika Westerberg936e15d2013-10-10 11:01:08 +0300249 if (lookup->n++ == lookup->index && !lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300250 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
251
Mika Westerberg936e15d2013-10-10 11:01:08 +0300252 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
253 agpio->pin_table[0]);
Mika Westerberg12028d22013-04-03 13:56:54 +0300254 lookup->info.gpioint =
255 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Mika Westerberge01f4402013-10-10 11:01:10 +0300256 lookup->info.active_low =
257 agpio->polarity == ACPI_ACTIVE_LOW;
Mika Westerberg12028d22013-04-03 13:56:54 +0300258 }
259
260 return 1;
261}
262
263/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300264 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg12028d22013-04-03 13:56:54 +0300265 * @dev: pointer to a device to get GPIO from
266 * @index: index of GpioIo/GpioInt resource (starting from %0)
267 * @info: info pointer to fill in (optional)
268 *
269 * Function goes through ACPI resources for @dev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300270 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300271 * and returns it. @index matches GpioIo/GpioInt resources only so if there
272 * are total %3 GPIO resources, the index goes from %0 to %2.
273 *
Mika Westerberg936e15d2013-10-10 11:01:08 +0300274 * If the GPIO cannot be translated or there is an error an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300275 * returned.
276 *
277 * Note: if the GPIO resource has multiple entries in the pin list, this
278 * function only returns the first.
279 */
Mika Westerberg936e15d2013-10-10 11:01:08 +0300280struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
281 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300282{
283 struct acpi_gpio_lookup lookup;
284 struct list_head resource_list;
285 struct acpi_device *adev;
286 acpi_handle handle;
287 int ret;
288
289 if (!dev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300290 return ERR_PTR(-EINVAL);
Mika Westerberg12028d22013-04-03 13:56:54 +0300291
292 handle = ACPI_HANDLE(dev);
293 if (!handle || acpi_bus_get_device(handle, &adev))
Mika Westerberg936e15d2013-10-10 11:01:08 +0300294 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300295
296 memset(&lookup, 0, sizeof(lookup));
297 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300298
299 INIT_LIST_HEAD(&resource_list);
300 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
301 &lookup);
302 if (ret < 0)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300303 return ERR_PTR(ret);
Mika Westerberg12028d22013-04-03 13:56:54 +0300304
305 acpi_dev_free_resource_list(&resource_list);
306
Mika Westerberg936e15d2013-10-10 11:01:08 +0300307 if (lookup.desc && info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300308 *info = lookup.info;
309
Mika Westerberg936e15d2013-10-10 11:01:08 +0300310 return lookup.desc ? lookup.desc : ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300311}
Mika Westerberg936e15d2013-10-10 11:01:08 +0300312EXPORT_SYMBOL_GPL(acpi_get_gpiod_by_index);