2014-02-26 22:59:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Google, Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2014-10-04 17:29:42 +00:00
|
|
|
#include <malloc.h>
|
2014-02-26 22:59:24 +00:00
|
|
|
#include <asm/gpio.h>
|
2014-10-23 03:37:01 +00:00
|
|
|
#include <linux/ctype.h>
|
2014-02-26 22:59:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_to_device() - Convert global GPIO number to device, number
|
|
|
|
* gpio: The numeric representation of the GPIO
|
|
|
|
*
|
|
|
|
* Convert the GPIO number to an entry in the list of GPIOs
|
|
|
|
* or GPIO blocks registered with the GPIO controller. Returns
|
|
|
|
* entry on success, NULL on error.
|
|
|
|
*/
|
2014-05-22 10:43:05 +00:00
|
|
|
static int gpio_to_device(unsigned int gpio, struct udevice **devp,
|
2014-02-26 22:59:24 +00:00
|
|
|
unsigned int *offset)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (ret = uclass_first_device(UCLASS_GPIO, &dev);
|
|
|
|
dev;
|
|
|
|
ret = uclass_next_device(&dev)) {
|
|
|
|
uc_priv = dev->uclass_priv;
|
|
|
|
if (gpio >= uc_priv->gpio_base &&
|
|
|
|
gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
|
|
|
|
*devp = dev;
|
|
|
|
*offset = gpio - uc_priv->gpio_base;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No such GPIO */
|
|
|
|
return ret ? ret : -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int gpio_lookup_name(const char *name, struct udevice **devp,
|
2014-02-26 22:59:24 +00:00
|
|
|
unsigned int *offsetp, unsigned int *gpiop)
|
|
|
|
{
|
2014-10-23 03:37:01 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = NULL;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-10-23 03:37:01 +00:00
|
|
|
ulong offset;
|
|
|
|
int numeric;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (devp)
|
|
|
|
*devp = NULL;
|
2014-10-23 03:37:01 +00:00
|
|
|
numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
|
2014-02-26 22:59:24 +00:00
|
|
|
for (ret = uclass_first_device(UCLASS_GPIO, &dev);
|
|
|
|
dev;
|
|
|
|
ret = uclass_next_device(&dev)) {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
uc_priv = dev->uclass_priv;
|
2014-10-23 03:37:01 +00:00
|
|
|
if (numeric != -1) {
|
|
|
|
offset = numeric - uc_priv->gpio_base;
|
|
|
|
/* Allow GPIOs to be numbered from 0 */
|
|
|
|
if (offset >= 0 && offset < uc_priv->gpio_count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:24 +00:00
|
|
|
len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
|
|
|
|
|
2014-06-12 05:29:47 +00:00
|
|
|
if (!strncasecmp(name, uc_priv->bank_name, len)) {
|
2014-10-23 03:37:01 +00:00
|
|
|
if (!strict_strtoul(name + len, 10, &offset))
|
|
|
|
break;
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 03:37:01 +00:00
|
|
|
if (!dev)
|
|
|
|
return ret ? ret : -EINVAL;
|
|
|
|
|
|
|
|
if (devp)
|
|
|
|
*devp = dev;
|
|
|
|
if (offsetp)
|
|
|
|
*offsetp = offset;
|
|
|
|
if (gpiop)
|
|
|
|
*gpiop = uc_priv->gpio_base + offset;
|
|
|
|
|
|
|
|
return 0;
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_request() - [COMPAT] Request GPIO
|
|
|
|
* gpio: GPIO number
|
|
|
|
* label: Name for the requested GPIO
|
|
|
|
*
|
2014-10-04 17:29:42 +00:00
|
|
|
* The label is copied and allocated so the caller does not need to keep
|
|
|
|
* the pointer around.
|
|
|
|
*
|
2014-02-26 22:59:24 +00:00
|
|
|
* This function implements the API that's compatible with current
|
|
|
|
* GPIO API used in U-Boot. The request is forwarded to particular
|
|
|
|
* GPIO driver. Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int gpio_request(unsigned gpio, const char *label)
|
|
|
|
{
|
2014-10-04 17:29:42 +00:00
|
|
|
struct gpio_dev_priv *uc_priv;
|
2014-02-26 22:59:24 +00:00
|
|
|
unsigned int offset;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-10-04 17:29:42 +00:00
|
|
|
char *str;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &dev, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
uc_priv = dev->uclass_priv;
|
|
|
|
if (uc_priv->name[offset])
|
|
|
|
return -EBUSY;
|
|
|
|
str = strdup(label);
|
|
|
|
if (!str)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (gpio_get_ops(dev)->request) {
|
|
|
|
ret = gpio_get_ops(dev)->request(dev, offset, label);
|
|
|
|
if (ret) {
|
|
|
|
free(str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uc_priv->name[offset] = str;
|
2014-02-26 22:59:24 +00:00
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
return 0;
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_free() - [COMPAT] Relinquish GPIO
|
|
|
|
* gpio: GPIO number
|
|
|
|
*
|
|
|
|
* This function implements the API that's compatible with current
|
|
|
|
* GPIO API used in U-Boot. The request is forwarded to particular
|
|
|
|
* GPIO driver. Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int gpio_free(unsigned gpio)
|
|
|
|
{
|
2014-10-04 17:29:42 +00:00
|
|
|
struct gpio_dev_priv *uc_priv;
|
2014-02-26 22:59:24 +00:00
|
|
|
unsigned int offset;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &dev, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
uc_priv = dev->uclass_priv;
|
|
|
|
if (!uc_priv->name[offset])
|
|
|
|
return -ENXIO;
|
|
|
|
if (gpio_get_ops(dev)->free) {
|
|
|
|
ret = gpio_get_ops(dev)->free(dev, offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(uc_priv->name[offset]);
|
|
|
|
uc_priv->name[offset] = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_reserved(struct udevice *dev, unsigned offset,
|
|
|
|
const char *func)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev->uclass_priv;
|
|
|
|
|
|
|
|
if (!uc_priv->name[offset]) {
|
|
|
|
printf("%s: %s: error: gpio %s%d not reserved\n",
|
|
|
|
dev->name, func,
|
|
|
|
uc_priv->bank_name ? uc_priv->bank_name : "", offset);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_direction_input() - [COMPAT] Set GPIO direction to input
|
|
|
|
* gpio: GPIO number
|
|
|
|
*
|
|
|
|
* This function implements the API that's compatible with current
|
|
|
|
* GPIO API used in U-Boot. The request is forwarded to particular
|
|
|
|
* GPIO driver. Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int gpio_direction_input(unsigned gpio)
|
|
|
|
{
|
|
|
|
unsigned int offset;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &dev, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-04 17:29:42 +00:00
|
|
|
ret = check_reserved(dev, offset, "dir_input");
|
2014-02-26 22:59:24 +00:00
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
|
|
|
|
* gpio: GPIO number
|
|
|
|
* value: Logical value to be set on the GPIO pin
|
|
|
|
*
|
|
|
|
* This function implements the API that's compatible with current
|
|
|
|
* GPIO API used in U-Boot. The request is forwarded to particular
|
|
|
|
* GPIO driver. Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int gpio_direction_output(unsigned gpio, int value)
|
|
|
|
{
|
|
|
|
unsigned int offset;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &dev, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-04 17:29:42 +00:00
|
|
|
ret = check_reserved(dev, offset, "dir_output");
|
2014-02-26 22:59:24 +00:00
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
return ret ? ret :
|
|
|
|
gpio_get_ops(dev)->direction_output(dev, offset, value);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
|
|
|
|
* gpio: GPIO number
|
|
|
|
*
|
|
|
|
* This function implements the API that's compatible with current
|
|
|
|
* GPIO API used in U-Boot. The request is forwarded to particular
|
|
|
|
* GPIO driver. Returns the value of the GPIO pin, or negative value
|
|
|
|
* on error.
|
|
|
|
*/
|
|
|
|
int gpio_get_value(unsigned gpio)
|
|
|
|
{
|
|
|
|
unsigned int offset;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &dev, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-04 17:29:42 +00:00
|
|
|
ret = check_reserved(dev, offset, "get_value");
|
2014-02-26 22:59:24 +00:00
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
|
|
|
|
* gpio: GPIO number
|
|
|
|
* value: Logical value to be set on the GPIO pin.
|
|
|
|
*
|
|
|
|
* This function implements the API that's compatible with current
|
|
|
|
* GPIO API used in U-Boot. The request is forwarded to particular
|
|
|
|
* GPIO driver. Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int gpio_set_value(unsigned gpio, int value)
|
|
|
|
{
|
|
|
|
unsigned int offset;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &dev, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-04 17:29:42 +00:00
|
|
|
ret = check_reserved(dev, offset, "set_value");
|
2014-02-26 22:59:24 +00:00
|
|
|
|
2014-10-04 17:29:42 +00:00
|
|
|
return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
|
|
|
struct gpio_dev_priv *priv;
|
|
|
|
|
|
|
|
/* Must be called on an active device */
|
|
|
|
priv = dev->uclass_priv;
|
|
|
|
assert(priv);
|
|
|
|
|
|
|
|
*bit_count = priv->gpio_count;
|
|
|
|
return priv->bank_name;
|
|
|
|
}
|
|
|
|
|
2014-10-04 17:29:43 +00:00
|
|
|
static const char * const gpio_function[GPIOF_COUNT] = {
|
|
|
|
"input",
|
|
|
|
"output",
|
|
|
|
"unused",
|
|
|
|
"unknown",
|
|
|
|
"func",
|
|
|
|
};
|
|
|
|
|
|
|
|
int get_function(struct udevice *dev, int offset, bool skip_unused,
|
|
|
|
const char **namep)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev->uclass_priv;
|
|
|
|
struct dm_gpio_ops *ops = gpio_get_ops(dev);
|
|
|
|
|
|
|
|
BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
|
|
|
|
if (!device_active(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
if (offset < 0 || offset >= uc_priv->gpio_count)
|
|
|
|
return -EINVAL;
|
|
|
|
if (namep)
|
|
|
|
*namep = uc_priv->name[offset];
|
|
|
|
if (skip_unused && !uc_priv->name[offset])
|
|
|
|
return GPIOF_UNUSED;
|
|
|
|
if (ops->get_function) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ops->get_function(dev, offset);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret >= ARRAY_SIZE(gpio_function))
|
|
|
|
return -ENODATA;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GPIOF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_get_function(struct udevice *dev, int offset, const char **namep)
|
|
|
|
{
|
|
|
|
return get_function(dev, offset, true, namep);
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
|
|
|
|
{
|
|
|
|
return get_function(dev, offset, false, namep);
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:24 +00:00
|
|
|
/* We need to renumber the GPIOs when any driver is probed/removed */
|
2014-10-04 17:29:42 +00:00
|
|
|
static int gpio_renumber(struct udevice *removed_dev)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:24 +00:00
|
|
|
struct uclass *uc;
|
|
|
|
unsigned base;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get(UCLASS_GPIO, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Ensure that we have a base for each bank */
|
|
|
|
base = 0;
|
|
|
|
uclass_foreach_dev(dev, uc) {
|
2014-10-04 17:29:42 +00:00
|
|
|
if (device_active(dev) && dev != removed_dev) {
|
2014-02-26 22:59:24 +00:00
|
|
|
uc_priv = dev->uclass_priv;
|
|
|
|
uc_priv->gpio_base = base;
|
|
|
|
base += uc_priv->gpio_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int gpio_post_probe(struct udevice *dev)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
2014-10-04 17:29:42 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev->uclass_priv;
|
|
|
|
|
|
|
|
uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
|
|
|
|
if (!uc_priv->name)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return gpio_renumber(NULL);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int gpio_pre_remove(struct udevice *dev)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
2014-10-04 17:29:42 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev->uclass_priv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < uc_priv->gpio_count; i++) {
|
|
|
|
if (uc_priv->name[i])
|
|
|
|
free(uc_priv->name[i]);
|
|
|
|
}
|
|
|
|
free(uc_priv->name);
|
|
|
|
|
|
|
|
return gpio_renumber(dev);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UCLASS_DRIVER(gpio) = {
|
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.name = "gpio",
|
|
|
|
.post_probe = gpio_post_probe,
|
|
|
|
.pre_remove = gpio_pre_remove,
|
|
|
|
.per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
|
|
|
|
};
|