gpio / ACPI: add ACPI support
[firefly-linux-kernel-4.4.55.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
19 static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
20 {
21         if (!gc->dev)
22                 return false;
23
24         return ACPI_HANDLE(gc->dev) == data;
25 }
26
27 /**
28  * acpi_get_gpio() - Translate ACPI GPIO pin to GPIO number usable with GPIO API
29  * @path:       ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
30  * @pin:        ACPI GPIO pin number (0-based, controller-relative)
31  *
32  * Returns GPIO number to use with Linux generic GPIO API, or errno error value
33  */
34
35 int acpi_get_gpio(char *path, int pin)
36 {
37         struct gpio_chip *chip;
38         acpi_handle handle;
39         acpi_status status;
40
41         status = acpi_get_handle(NULL, path, &handle);
42         if (ACPI_FAILURE(status))
43                 return -ENODEV;
44
45         chip = gpiochip_find(handle, acpi_gpiochip_find);
46         if (!chip)
47                 return -ENODEV;
48
49         if (!gpio_is_valid(chip->base + pin))
50                 return -EINVAL;
51
52         return chip->base + pin;
53 }
54 EXPORT_SYMBOL_GPL(acpi_get_gpio);