Commit 12028d2d authored by Mika Westerberg's avatar Mika Westerberg Committed by Linus Walleij

gpiolib-acpi: introduce acpi_get_gpio_by_index() helper

Instead of open-coding ACPI GPIO resource lookup in each driver, we provide
a helper function analogous to Device Tree version that allows drivers to
specify which GPIO resource they are interested (using an index to the GPIO
resources). The function then finds out the correct resource, translates
the ACPI GPIO number to the corresponding Linux GPIO number and returns
that.
Signed-off-by: default avatarMika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 2ce7c62d
...@@ -199,6 +199,8 @@ the device to the driver. For example: ...@@ -199,6 +199,8 @@ the device to the driver. For example:
{ {
Name (SBUF, ResourceTemplate() Name (SBUF, ResourceTemplate()
{ {
...
// Used to power on/off the device
GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
IoRestrictionOutputOnly, "\\_SB.PCI0.GPI0", IoRestrictionOutputOnly, "\\_SB.PCI0.GPI0",
0x00, ResourceConsumer,,) 0x00, ResourceConsumer,,)
...@@ -206,10 +208,20 @@ the device to the driver. For example: ...@@ -206,10 +208,20 @@ the device to the driver. For example:
// Pin List // Pin List
0x0055 0x0055
} }
// Interrupt for the device
GpioInt (Edge, ActiveHigh, ExclusiveAndWake, PullNone,
0x0000, "\\_SB.PCI0.GPI0", 0x00, ResourceConsumer,,)
{
// Pin list
0x0058
}
... ...
Return (SBUF)
} }
Return (SBUF)
} }
These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0" These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
...@@ -220,6 +232,24 @@ The driver can do this by including <linux/acpi_gpio.h> and then calling ...@@ -220,6 +232,24 @@ The driver can do this by including <linux/acpi_gpio.h> and then calling
acpi_get_gpio(path, gpio). This will return the Linux GPIO number or acpi_get_gpio(path, gpio). This will return the Linux GPIO number or
negative errno if there was no translation found. negative errno if there was no translation found.
In a simple case of just getting the Linux GPIO number from device
resources one can use acpi_get_gpio_by_index() helper function. It takes
pointer to the device and index of the GpioIo/GpioInt descriptor in the
device resources list. For example:
int gpio_irq, gpio_power;
int ret;
gpio_irq = acpi_get_gpio_by_index(dev, 1, NULL);
if (gpio_irq < 0)
/* handle error */
gpio_power = acpi_get_gpio_by_index(dev, 0, NULL);
if (gpio_power < 0)
/* handle error */
/* Now we can use the GPIO numbers */
Other GpioIo parameters must be converted first by the driver to be Other GpioIo parameters must be converted first by the driver to be
suitable to the gpiolib before passing them. suitable to the gpiolib before passing them.
......
...@@ -201,6 +201,83 @@ void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) ...@@ -201,6 +201,83 @@ void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
} }
EXPORT_SYMBOL(acpi_gpiochip_request_interrupts); EXPORT_SYMBOL(acpi_gpiochip_request_interrupts);
struct acpi_gpio_lookup {
struct acpi_gpio_info info;
int index;
int gpio;
int n;
};
static int acpi_find_gpio(struct acpi_resource *ares, void *data)
{
struct acpi_gpio_lookup *lookup = data;
if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
return 1;
if (lookup->n++ == lookup->index && lookup->gpio < 0) {
const struct acpi_resource_gpio *agpio = &ares->data.gpio;
lookup->gpio = acpi_get_gpio(agpio->resource_source.string_ptr,
agpio->pin_table[0]);
lookup->info.gpioint =
agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
}
return 1;
}
/**
* acpi_get_gpio_by_index() - get a GPIO number from device resources
* @dev: pointer to a device to get GPIO from
* @index: index of GpioIo/GpioInt resource (starting from %0)
* @info: info pointer to fill in (optional)
*
* Function goes through ACPI resources for @dev and based on @index looks
* up a GpioIo/GpioInt resource, translates it to the Linux GPIO number,
* and returns it. @index matches GpioIo/GpioInt resources only so if there
* are total %3 GPIO resources, the index goes from %0 to %2.
*
* If the GPIO cannot be translated or there is an error, negative errno is
* returned.
*
* Note: if the GPIO resource has multiple entries in the pin list, this
* function only returns the first.
*/
int acpi_get_gpio_by_index(struct device *dev, int index,
struct acpi_gpio_info *info)
{
struct acpi_gpio_lookup lookup;
struct list_head resource_list;
struct acpi_device *adev;
acpi_handle handle;
int ret;
if (!dev)
return -EINVAL;
handle = ACPI_HANDLE(dev);
if (!handle || acpi_bus_get_device(handle, &adev))
return -ENODEV;
memset(&lookup, 0, sizeof(lookup));
lookup.index = index;
lookup.gpio = -ENODEV;
INIT_LIST_HEAD(&resource_list);
ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
&lookup);
if (ret < 0)
return ret;
acpi_dev_free_resource_list(&resource_list);
if (lookup.gpio >= 0 && info)
*info = lookup.info;
return lookup.gpio;
}
EXPORT_SYMBOL_GPL(acpi_get_gpio_by_index);
/** /**
* acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts. * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
......
#ifndef _LINUX_ACPI_GPIO_H_ #ifndef _LINUX_ACPI_GPIO_H_
#define _LINUX_ACPI_GPIO_H_ #define _LINUX_ACPI_GPIO_H_
#include <linux/device.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/gpio.h> #include <linux/gpio.h>
/**
* struct acpi_gpio_info - ACPI GPIO specific information
* @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
*/
struct acpi_gpio_info {
bool gpioint;
};
#ifdef CONFIG_GPIO_ACPI #ifdef CONFIG_GPIO_ACPI
int acpi_get_gpio(char *path, int pin); int acpi_get_gpio(char *path, int pin);
int acpi_get_gpio_by_index(struct device *dev, int index,
struct acpi_gpio_info *info);
void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
...@@ -17,6 +28,12 @@ static inline int acpi_get_gpio(char *path, int pin) ...@@ -17,6 +28,12 @@ static inline int acpi_get_gpio(char *path, int pin)
return -ENODEV; return -ENODEV;
} }
static inline int acpi_get_gpio_by_index(struct device *dev, int index,
struct acpi_gpio_info *info)
{
return -ENODEV;
}
static inline void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } static inline void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
static inline void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } static inline void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment