misc: at25: Make use of device property API
[deliverable/linux.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>
473ed7be 19#include <linux/mutex.h>
e29482e8 20
5ccff852
MW
21#include "gpiolib.h"
22
4b01a14b 23struct acpi_gpio_event {
7fc7acb9 24 struct list_head node;
4b01a14b 25 acpi_handle handle;
7fc7acb9
RW
26 unsigned int pin;
27 unsigned int irq;
e46cf32c 28 struct gpio_desc *desc;
7fc7acb9
RW
29};
30
473ed7be
MW
31struct acpi_gpio_connection {
32 struct list_head node;
e46cf32c 33 unsigned int pin;
473ed7be
MW
34 struct gpio_desc *desc;
35};
36
aa92b6f6 37struct acpi_gpio_chip {
473ed7be
MW
38 /*
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.
42 */
43 struct acpi_connection_info conn_info;
44 struct list_head conns;
45 struct mutex conn_lock;
aa92b6f6 46 struct gpio_chip *chip;
4b01a14b 47 struct list_head events;
aa92b6f6
MW
48};
49
e29482e8
MN
50static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
51{
52 if (!gc->dev)
53 return false;
54
55 return ACPI_HANDLE(gc->dev) == data;
56}
57
58/**
936e15dd 59 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
e29482e8
MN
60 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
61 * @pin: ACPI GPIO pin number (0-based, controller-relative)
62 *
936e15dd
MW
63 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
64 * error value
e29482e8
MN
65 */
66
936e15dd 67static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
e29482e8
MN
68{
69 struct gpio_chip *chip;
70 acpi_handle handle;
71 acpi_status status;
72
73 status = acpi_get_handle(NULL, path, &handle);
74 if (ACPI_FAILURE(status))
936e15dd 75 return ERR_PTR(-ENODEV);
e29482e8
MN
76
77 chip = gpiochip_find(handle, acpi_gpiochip_find);
78 if (!chip)
936e15dd 79 return ERR_PTR(-ENODEV);
e29482e8 80
936e15dd
MW
81 if (pin < 0 || pin > chip->ngpio)
82 return ERR_PTR(-EINVAL);
e29482e8 83
390d82e3 84 return gpiochip_get_desc(chip, pin);
e29482e8 85}
0d1c28a4 86
0d1c28a4
MN
87static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
88{
6072b9dc 89 struct acpi_gpio_event *event = data;
0d1c28a4 90
6072b9dc 91 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
0d1c28a4
MN
92
93 return IRQ_HANDLED;
94}
95
7fc7acb9
RW
96static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
97{
4b01a14b 98 struct acpi_gpio_event *event = data;
7fc7acb9 99
4b01a14b 100 acpi_execute_simple_method(event->handle, NULL, event->pin);
7fc7acb9
RW
101
102 return IRQ_HANDLED;
103}
104
aa92b6f6 105static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
7fc7acb9
RW
106{
107 /* The address of this function is used as a key. */
108}
109
6072b9dc
MW
110static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
111 void *context)
0d1c28a4 112{
6072b9dc 113 struct acpi_gpio_chip *acpi_gpio = context;
aa92b6f6 114 struct gpio_chip *chip = acpi_gpio->chip;
6072b9dc 115 struct acpi_resource_gpio *agpio;
7fc7acb9 116 acpi_handle handle, evt_handle;
6072b9dc
MW
117 struct acpi_gpio_event *event;
118 irq_handler_t handler = NULL;
119 struct gpio_desc *desc;
120 unsigned long irqflags;
121 int ret, pin, irq;
0d1c28a4 122
6072b9dc
MW
123 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
124 return AE_OK;
125
126 agpio = &ares->data.gpio;
127 if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
128 return AE_OK;
0d1c28a4
MN
129
130 handle = ACPI_HANDLE(chip->dev);
6072b9dc
MW
131 pin = agpio->pin_table[0];
132
133 if (pin <= 255) {
134 char ev_name[5];
135 sprintf(ev_name, "_%c%02X",
136 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
137 pin);
138 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
139 handler = acpi_gpio_irq_handler;
140 }
141 if (!handler) {
142 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
143 handler = acpi_gpio_irq_handler_evt;
144 }
145 if (!handler)
146 return AE_BAD_PARAMETER;
0d1c28a4 147
abdc08a3 148 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
6072b9dc 149 if (IS_ERR(desc)) {
6072b9dc
MW
150 dev_err(chip->dev, "Failed to request GPIO\n");
151 return AE_ERROR;
152 }
0d1c28a4 153
6072b9dc 154 gpiod_direction_input(desc);
0d1c28a4 155
d74be6df 156 ret = gpio_lock_as_irq(chip, pin);
6072b9dc
MW
157 if (ret) {
158 dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
159 goto fail_free_desc;
160 }
0d1c28a4 161
6072b9dc
MW
162 irq = gpiod_to_irq(desc);
163 if (irq < 0) {
164 dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
165 goto fail_unlock_irq;
166 }
7fc7acb9 167
6072b9dc
MW
168 irqflags = IRQF_ONESHOT;
169 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
170 if (agpio->polarity == ACPI_ACTIVE_HIGH)
171 irqflags |= IRQF_TRIGGER_HIGH;
172 else
173 irqflags |= IRQF_TRIGGER_LOW;
174 } else {
175 switch (agpio->polarity) {
176 case ACPI_ACTIVE_HIGH:
177 irqflags |= IRQF_TRIGGER_RISING;
178 break;
179 case ACPI_ACTIVE_LOW:
180 irqflags |= IRQF_TRIGGER_FALLING;
181 break;
182 default:
183 irqflags |= IRQF_TRIGGER_RISING |
184 IRQF_TRIGGER_FALLING;
185 break;
7fc7acb9 186 }
6072b9dc 187 }
aa92b6f6 188
6072b9dc
MW
189 event = kzalloc(sizeof(*event), GFP_KERNEL);
190 if (!event)
191 goto fail_unlock_irq;
7fc7acb9 192
6072b9dc
MW
193 event->handle = evt_handle;
194 event->irq = irq;
195 event->pin = pin;
e46cf32c 196 event->desc = desc;
7fc7acb9 197
6072b9dc
MW
198 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
199 "ACPI:Event", event);
200 if (ret) {
201 dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
202 event->irq);
203 goto fail_free_event;
0d1c28a4 204 }
6072b9dc
MW
205
206 list_add_tail(&event->node, &acpi_gpio->events);
207 return AE_OK;
208
209fail_free_event:
210 kfree(event);
211fail_unlock_irq:
d74be6df 212 gpio_unlock_as_irq(chip, pin);
6072b9dc
MW
213fail_free_desc:
214 gpiochip_free_own_desc(desc);
215
216 return AE_ERROR;
0d1c28a4 217}
7fc7acb9 218
70b53411 219/**
6072b9dc 220 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
afa82fab 221 * @chip: GPIO chip
70b53411 222 *
6072b9dc
MW
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.
228 */
afa82fab 229void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
6072b9dc 230{
afa82fab
MW
231 struct acpi_gpio_chip *acpi_gpio;
232 acpi_handle handle;
233 acpi_status status;
234
235 if (!chip->dev || !chip->to_irq)
236 return;
6072b9dc 237
afa82fab
MW
238 handle = ACPI_HANDLE(chip->dev);
239 if (!handle)
240 return;
241
242 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
243 if (ACPI_FAILURE(status))
6072b9dc
MW
244 return;
245
246 INIT_LIST_HEAD(&acpi_gpio->events);
247 acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
248 acpi_gpiochip_request_interrupt, acpi_gpio);
249}
250
251/**
252 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
afa82fab 253 * @chip: GPIO chip
70b53411 254 *
6072b9dc
MW
255 * Free interrupts associated with GPIO ACPI event method for the given
256 * GPIO chip.
70b53411 257 */
afa82fab 258void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
70b53411 259{
afa82fab 260 struct acpi_gpio_chip *acpi_gpio;
4b01a14b 261 struct acpi_gpio_event *event, *ep;
afa82fab
MW
262 acpi_handle handle;
263 acpi_status status;
264
265 if (!chip->dev || !chip->to_irq)
266 return;
70b53411 267
afa82fab
MW
268 handle = ACPI_HANDLE(chip->dev);
269 if (!handle)
270 return;
271
272 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
273 if (ACPI_FAILURE(status))
70b53411
MW
274 return;
275
4b01a14b 276 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
6072b9dc
MW
277 struct gpio_desc *desc;
278
279 free_irq(event->irq, event);
e46cf32c 280 desc = event->desc;
6072b9dc
MW
281 if (WARN_ON(IS_ERR(desc)))
282 continue;
d74be6df 283 gpio_unlock_as_irq(chip, event->pin);
6072b9dc 284 gpiochip_free_own_desc(desc);
4b01a14b
MW
285 list_del(&event->node);
286 kfree(event);
70b53411 287 }
70b53411 288}
70b53411 289
12028d2d
MW
290struct acpi_gpio_lookup {
291 struct acpi_gpio_info info;
292 int index;
936e15dd 293 struct gpio_desc *desc;
12028d2d
MW
294 int n;
295};
296
297static int acpi_find_gpio(struct acpi_resource *ares, void *data)
298{
299 struct acpi_gpio_lookup *lookup = data;
300
301 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
302 return 1;
303
936e15dd 304 if (lookup->n++ == lookup->index && !lookup->desc) {
12028d2d
MW
305 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
306
936e15dd
MW
307 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
308 agpio->pin_table[0]);
12028d2d
MW
309 lookup->info.gpioint =
310 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
e01f440a
MW
311 lookup->info.active_low =
312 agpio->polarity == ACPI_ACTIVE_LOW;
12028d2d
MW
313 }
314
315 return 1;
316}
317
318/**
936e15dd 319 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
12028d2d
MW
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)
323 *
324 * Function goes through ACPI resources for @dev and based on @index looks
936e15dd 325 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
12028d2d
MW
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.
328 *
936e15dd 329 * If the GPIO cannot be translated or there is an error an ERR_PTR is
12028d2d
MW
330 * returned.
331 *
332 * Note: if the GPIO resource has multiple entries in the pin list, this
333 * function only returns the first.
334 */
936e15dd
MW
335struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
336 struct acpi_gpio_info *info)
12028d2d
MW
337{
338 struct acpi_gpio_lookup lookup;
339 struct list_head resource_list;
340 struct acpi_device *adev;
341 acpi_handle handle;
342 int ret;
343
344 if (!dev)
936e15dd 345 return ERR_PTR(-EINVAL);
12028d2d
MW
346
347 handle = ACPI_HANDLE(dev);
348 if (!handle || acpi_bus_get_device(handle, &adev))
936e15dd 349 return ERR_PTR(-ENODEV);
12028d2d
MW
350
351 memset(&lookup, 0, sizeof(lookup));
352 lookup.index = index;
12028d2d
MW
353
354 INIT_LIST_HEAD(&resource_list);
355 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
356 &lookup);
357 if (ret < 0)
936e15dd 358 return ERR_PTR(ret);
12028d2d
MW
359
360 acpi_dev_free_resource_list(&resource_list);
361
936e15dd 362 if (lookup.desc && info)
12028d2d
MW
363 *info = lookup.info;
364
a00580c2 365 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
12028d2d 366}
664e3e5a 367
473ed7be
MW
368static acpi_status
369acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
370 u32 bits, u64 *value, void *handler_context,
371 void *region_context)
372{
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;
c15d821d 377 int pin_index = (int)address;
473ed7be
MW
378 acpi_status status;
379 bool pull_up;
c15d821d 380 int length;
473ed7be
MW
381 int i;
382
383 status = acpi_buffer_to_resource(achip->conn_info.connection,
384 achip->conn_info.length, &ares);
385 if (ACPI_FAILURE(status))
386 return status;
387
388 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
389 ACPI_FREE(ares);
390 return AE_BAD_PARAMETER;
391 }
392
393 agpio = &ares->data.gpio;
394 pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
395
396 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
397 function == ACPI_WRITE)) {
398 ACPI_FREE(ares);
399 return AE_BAD_PARAMETER;
400 }
401
c15d821d
SP
402 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
403 for (i = pin_index; i < length; ++i) {
473ed7be
MW
404 unsigned pin = agpio->pin_table[i];
405 struct acpi_gpio_connection *conn;
406 struct gpio_desc *desc;
407 bool found;
408
473ed7be
MW
409 mutex_lock(&achip->conn_lock);
410
411 found = false;
412 list_for_each_entry(conn, &achip->conns, node) {
e46cf32c 413 if (conn->pin == pin) {
473ed7be 414 found = true;
e46cf32c 415 desc = conn->desc;
473ed7be
MW
416 break;
417 }
418 }
419 if (!found) {
abdc08a3
AC
420 desc = gpiochip_request_own_desc(chip, pin,
421 "ACPI:OpRegion");
e46cf32c 422 if (IS_ERR(desc)) {
473ed7be
MW
423 status = AE_ERROR;
424 mutex_unlock(&achip->conn_lock);
425 goto out;
426 }
427
428 switch (agpio->io_restriction) {
429 case ACPI_IO_RESTRICT_INPUT:
430 gpiod_direction_input(desc);
431 break;
432 case ACPI_IO_RESTRICT_OUTPUT:
433 /*
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
439 * low.
440 */
441 gpiod_direction_output(desc, pull_up);
442 break;
443 default:
444 /*
445 * Assume that the BIOS has configured the
446 * direction and pull accordingly.
447 */
448 break;
449 }
450
451 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
452 if (!conn) {
453 status = AE_NO_MEMORY;
454 gpiochip_free_own_desc(desc);
455 mutex_unlock(&achip->conn_lock);
456 goto out;
457 }
458
e46cf32c 459 conn->pin = pin;
473ed7be
MW
460 conn->desc = desc;
461 list_add_tail(&conn->node, &achip->conns);
462 }
463
464 mutex_unlock(&achip->conn_lock);
465
466 if (function == ACPI_WRITE)
dc62b56a
AL
467 gpiod_set_raw_value_cansleep(desc,
468 !!((1 << i) & *value));
473ed7be 469 else
dc62b56a 470 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
473ed7be
MW
471 }
472
473out:
474 ACPI_FREE(ares);
475 return status;
476}
477
478static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
479{
480 struct gpio_chip *chip = achip->chip;
481 acpi_handle handle = ACPI_HANDLE(chip->dev);
482 acpi_status status;
483
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,
488 NULL, achip);
489 if (ACPI_FAILURE(status))
490 dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
491}
492
493static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
494{
495 struct gpio_chip *chip = achip->chip;
496 acpi_handle handle = ACPI_HANDLE(chip->dev);
497 struct acpi_gpio_connection *conn, *tmp;
498 acpi_status status;
499
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");
504 return;
505 }
506
507 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
508 gpiochip_free_own_desc(conn->desc);
509 list_del(&conn->node);
510 kfree(conn);
511 }
512}
513
664e3e5a
MW
514void acpi_gpiochip_add(struct gpio_chip *chip)
515{
aa92b6f6
MW
516 struct acpi_gpio_chip *acpi_gpio;
517 acpi_handle handle;
518 acpi_status status;
519
e9595f84
MW
520 if (!chip || !chip->dev)
521 return;
522
aa92b6f6
MW
523 handle = ACPI_HANDLE(chip->dev);
524 if (!handle)
525 return;
526
527 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
528 if (!acpi_gpio) {
529 dev_err(chip->dev,
530 "Failed to allocate memory for ACPI GPIO chip\n");
531 return;
532 }
533
534 acpi_gpio->chip = chip;
535
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");
539 kfree(acpi_gpio);
540 return;
541 }
542
473ed7be 543 acpi_gpiochip_request_regions(acpi_gpio);
664e3e5a
MW
544}
545
546void acpi_gpiochip_remove(struct gpio_chip *chip)
547{
aa92b6f6
MW
548 struct acpi_gpio_chip *acpi_gpio;
549 acpi_handle handle;
550 acpi_status status;
551
e9595f84
MW
552 if (!chip || !chip->dev)
553 return;
554
aa92b6f6
MW
555 handle = ACPI_HANDLE(chip->dev);
556 if (!handle)
557 return;
558
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");
562 return;
563 }
564
473ed7be 565 acpi_gpiochip_free_regions(acpi_gpio);
aa92b6f6
MW
566
567 acpi_detach_data(handle, acpi_gpio_chip_dh);
568 kfree(acpi_gpio);
664e3e5a 569}
This page took 0.166379 seconds and 5 git commands to generate.