2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-02-26 22:59:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2016-04-24 23:32:40 +00:00
|
|
|
#include <dt-bindings/gpio/gpio.h>
|
2014-02-26 22:59:24 +00:00
|
|
|
#include <errno.h>
|
2015-01-06 03:05:28 +00:00
|
|
|
#include <fdtdec.h>
|
2014-10-04 17:29:42 +00:00
|
|
|
#include <malloc.h>
|
2014-02-26 22:59:24 +00:00
|
|
|
#include <asm/gpio.h>
|
2016-01-24 14:27:48 +00:00
|
|
|
#include <linux/bug.h>
|
2014-10-23 03:37:01 +00:00
|
|
|
#include <linux/ctype.h>
|
2014-02-26 22:59:24 +00:00
|
|
|
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2014-02-26 22:59:24 +00:00
|
|
|
/**
|
|
|
|
* gpio_to_device() - Convert global GPIO number to device, number
|
|
|
|
*
|
|
|
|
* 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.
|
2015-01-06 03:05:27 +00:00
|
|
|
*
|
|
|
|
* @gpio: The numeric representation of the GPIO
|
|
|
|
* @desc: Returns description (desc->flags will always be 0)
|
|
|
|
* @return 0 if found, -ENOENT if not found
|
2014-02-26 22:59:24 +00:00
|
|
|
*/
|
2015-01-06 03:05:27 +00:00
|
|
|
static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
|
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
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (ret = uclass_first_device(UCLASS_GPIO, &dev);
|
|
|
|
dev;
|
|
|
|
ret = uclass_next_device(&dev)) {
|
2015-03-05 19:25:20 +00:00
|
|
|
uc_priv = dev_get_uclass_priv(dev);
|
2014-02-26 22:59:24 +00:00
|
|
|
if (gpio >= uc_priv->gpio_base &&
|
|
|
|
gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
|
2015-01-06 03:05:27 +00:00
|
|
|
desc->dev = dev;
|
|
|
|
desc->offset = gpio - uc_priv->gpio_base;
|
|
|
|
desc->flags = 0;
|
2014-02-26 22:59:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No such GPIO */
|
2015-01-06 03:05:27 +00:00
|
|
|
return ret ? ret : -ENOENT;
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:40 +00:00
|
|
|
int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
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;
|
|
|
|
|
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;
|
|
|
|
|
2015-03-05 19:25:20 +00:00
|
|
|
uc_priv = dev_get_uclass_priv(dev);
|
2014-10-23 03:37:01 +00:00
|
|
|
if (numeric != -1) {
|
|
|
|
offset = numeric - uc_priv->gpio_base;
|
|
|
|
/* Allow GPIOs to be numbered from 0 */
|
2017-05-10 19:20:15 +00:00
|
|
|
if (offset < uc_priv->gpio_count)
|
2014-10-23 03:37:01 +00:00
|
|
|
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;
|
|
|
|
|
2015-06-23 21:38:40 +00:00
|
|
|
desc->dev = dev;
|
|
|
|
desc->offset = offset;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_lookup_name(const char *name, struct udevice **devp,
|
|
|
|
unsigned int *offsetp, unsigned int *gpiop)
|
|
|
|
{
|
|
|
|
struct gpio_desc desc;
|
|
|
|
int ret;
|
|
|
|
|
2014-10-23 03:37:01 +00:00
|
|
|
if (devp)
|
2015-06-23 21:38:40 +00:00
|
|
|
*devp = NULL;
|
|
|
|
ret = dm_gpio_lookup_name(name, &desc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (devp)
|
|
|
|
*devp = desc.dev;
|
2014-10-23 03:37:01 +00:00
|
|
|
if (offsetp)
|
2015-06-23 21:38:40 +00:00
|
|
|
*offsetp = desc.offset;
|
|
|
|
if (gpiop) {
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
|
|
|
|
|
|
|
|
*gpiop = uc_priv->gpio_base + desc.offset;
|
|
|
|
}
|
2014-10-23 03:37:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:18 +00:00
|
|
|
int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
|
|
|
|
struct ofnode_phandle_args *args)
|
2016-04-24 23:32:40 +00:00
|
|
|
{
|
|
|
|
if (args->args_count < 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
desc->offset = args->args[0];
|
|
|
|
|
|
|
|
if (args->args_count < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (args->args[1] & GPIO_ACTIVE_LOW)
|
|
|
|
desc->flags = GPIOD_ACTIVE_LOW;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
static int gpio_find_and_xlate(struct gpio_desc *desc,
|
2017-05-19 02:09:18 +00:00
|
|
|
struct ofnode_phandle_args *args)
|
2015-01-06 03:05:28 +00:00
|
|
|
{
|
|
|
|
struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
|
|
|
|
|
2016-04-24 23:32:40 +00:00
|
|
|
if (ops->xlate)
|
|
|
|
return ops->xlate(desc->dev, desc, args);
|
2015-01-06 03:05:28 +00:00
|
|
|
else
|
2016-04-24 23:32:40 +00:00
|
|
|
return gpio_xlate_offs_flags(desc->dev, desc, args);
|
2015-01-06 03:05:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:41 +00:00
|
|
|
int dm_gpio_request(struct gpio_desc *desc, const char *label)
|
2015-01-06 03:05:27 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev = desc->dev;
|
|
|
|
struct gpio_dev_priv *uc_priv;
|
|
|
|
char *str;
|
|
|
|
int ret;
|
|
|
|
|
2015-03-05 19:25:20 +00:00
|
|
|
uc_priv = dev_get_uclass_priv(dev);
|
2015-01-06 03:05:27 +00:00
|
|
|
if (uc_priv->name[desc->offset])
|
|
|
|
return -EBUSY;
|
|
|
|
str = strdup(label);
|
|
|
|
if (!str)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (gpio_get_ops(dev)->request) {
|
|
|
|
ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
|
|
|
|
if (ret) {
|
|
|
|
free(str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uc_priv->name[desc->offset] = str;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
|
|
|
|
{
|
2015-12-29 12:22:48 +00:00
|
|
|
#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
va_list args;
|
|
|
|
char buf[40];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vscnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
return dm_gpio_request(desc, buf);
|
2015-12-29 12:22:48 +00:00
|
|
|
#else
|
|
|
|
return dm_gpio_request(desc, fmt);
|
|
|
|
#endif
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-01-06 03:05:27 +00:00
|
|
|
struct gpio_desc desc;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
ret = gpio_to_device(gpio, &desc);
|
2014-02-26 22:59:24 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
return dm_gpio_request(&desc, label);
|
2014-02-26 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 17:29:49 +00:00
|
|
|
/**
|
|
|
|
* gpio_requestf() - [COMPAT] Request GPIO
|
|
|
|
* @gpio: GPIO number
|
|
|
|
* @fmt: Format string for the requested GPIO
|
|
|
|
* @...: Arguments for the printf() format string
|
|
|
|
*
|
|
|
|
* 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_requestf(unsigned gpio, const char *fmt, ...)
|
|
|
|
{
|
2015-12-29 12:22:48 +00:00
|
|
|
#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
|
2014-10-04 17:29:49 +00:00
|
|
|
va_list args;
|
|
|
|
char buf[40];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vscnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
return gpio_request(gpio, buf);
|
2015-12-29 12:22:48 +00:00
|
|
|
#else
|
|
|
|
return gpio_request(gpio, fmt);
|
|
|
|
#endif
|
2014-10-04 17:29:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
int _dm_gpio_free(struct udevice *dev, uint offset)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
2014-10-04 17:29:42 +00:00
|
|
|
struct gpio_dev_priv *uc_priv;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-03-05 19:25:20 +00:00
|
|
|
uc_priv = dev_get_uclass_priv(dev);
|
2014-10-04 17:29:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-06 03:05:27 +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
|
|
|
{
|
2015-01-06 03:05:27 +00:00
|
|
|
struct gpio_desc desc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &desc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return _dm_gpio_free(desc.dev, desc.offset);
|
|
|
|
}
|
2014-10-04 17:29:42 +00:00
|
|
|
|
2016-03-07 02:27:51 +00:00
|
|
|
static int check_reserved(const struct gpio_desc *desc, const char *func)
|
2015-01-06 03:05:27 +00:00
|
|
|
{
|
2015-07-03 00:16:16 +00:00
|
|
|
struct gpio_dev_priv *uc_priv;
|
|
|
|
|
|
|
|
if (!dm_gpio_is_valid(desc))
|
|
|
|
return -ENOENT;
|
2015-01-06 03:05:27 +00:00
|
|
|
|
2015-07-03 00:16:16 +00:00
|
|
|
uc_priv = dev_get_uclass_priv(desc->dev);
|
2015-01-06 03:05:27 +00:00
|
|
|
if (!uc_priv->name[desc->offset]) {
|
2014-10-04 17:29:42 +00:00
|
|
|
printf("%s: %s: error: gpio %s%d not reserved\n",
|
2015-01-06 03:05:27 +00:00
|
|
|
desc->dev->name, func,
|
|
|
|
uc_priv->bank_name ? uc_priv->bank_name : "",
|
|
|
|
desc->offset);
|
2014-10-04 17:29:42 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-01-06 03:05:27 +00:00
|
|
|
struct gpio_desc desc;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
ret = gpio_to_device(gpio, &desc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = check_reserved(&desc, "dir_input");
|
2014-02-26 22:59:24 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.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)
|
|
|
|
{
|
2015-01-06 03:05:27 +00:00
|
|
|
struct gpio_desc desc;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
ret = gpio_to_device(gpio, &desc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = check_reserved(&desc, "dir_output");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return gpio_get_ops(desc.dev)->direction_output(desc.dev,
|
|
|
|
desc.offset, value);
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:27:51 +00:00
|
|
|
int dm_gpio_get_value(const struct gpio_desc *desc)
|
2015-01-06 03:05:27 +00:00
|
|
|
{
|
|
|
|
int value;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = check_reserved(desc, "get_value");
|
2014-02-26 22:59:24 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
|
|
|
|
|
|
|
|
return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:27:51 +00:00
|
|
|
int dm_gpio_set_value(const struct gpio_desc *desc, int value)
|
2015-01-06 03:05:27 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = check_reserved(desc, "set_value");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (desc->flags & GPIOD_ACTIVE_LOW)
|
|
|
|
value = !value;
|
|
|
|
gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-25 13:15:21 +00:00
|
|
|
int dm_gpio_get_open_drain(struct gpio_desc *desc)
|
|
|
|
{
|
|
|
|
struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = check_reserved(desc, "get_open_drain");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (ops->set_open_drain)
|
|
|
|
return ops->get_open_drain(desc->dev, desc->offset);
|
|
|
|
else
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
|
|
|
|
{
|
|
|
|
struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = check_reserved(desc, "set_open_drain");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (ops->set_open_drain)
|
|
|
|
ret = ops->set_open_drain(desc->dev, desc->offset, value);
|
|
|
|
else
|
|
|
|
return 0; /* feature not supported -> ignore setting */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
|
|
|
|
{
|
|
|
|
struct udevice *dev = desc->dev;
|
|
|
|
struct dm_gpio_ops *ops = gpio_get_ops(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = check_reserved(desc, "set_dir");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (flags & GPIOD_IS_OUT) {
|
|
|
|
int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
|
|
|
|
|
|
|
|
if (flags & GPIOD_ACTIVE_LOW)
|
|
|
|
value = !value;
|
|
|
|
ret = ops->direction_output(dev, desc->offset, value);
|
|
|
|
} else if (flags & GPIOD_IS_IN) {
|
|
|
|
ret = ops->direction_input(dev, desc->offset);
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/*
|
|
|
|
* Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
|
|
|
|
* futures
|
|
|
|
*/
|
|
|
|
desc->flags = flags;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_gpio_set_dir(struct gpio_desc *desc)
|
|
|
|
{
|
|
|
|
return dm_gpio_set_dir_flags(desc, desc->flags);
|
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)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
struct gpio_desc desc;
|
|
|
|
|
|
|
|
ret = gpio_to_device(gpio, &desc);
|
2014-02-26 22:59:24 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-01-06 03:05:27 +00:00
|
|
|
return dm_gpio_get_value(&desc);
|
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)
|
|
|
|
{
|
2015-01-06 03:05:27 +00:00
|
|
|
struct gpio_desc desc;
|
2014-02-26 22:59:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-01-06 03:05:27 +00:00
|
|
|
ret = gpio_to_device(gpio, &desc);
|
2014-02-26 22:59:24 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-01-06 03:05:27 +00:00
|
|
|
return dm_gpio_set_value(&desc, 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 */
|
2015-03-05 19:25:20 +00:00
|
|
|
priv = dev_get_uclass_priv(dev);
|
2014-02-26 22:59:24 +00:00
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
2017-06-22 07:50:25 +00:00
|
|
|
static int get_function(struct udevice *dev, int offset, bool skip_unused,
|
|
|
|
const char **namep)
|
2014-10-04 17:29:43 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2014-10-04 17:29:43 +00:00
|
|
|
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-10-04 17:29:44 +00:00
|
|
|
int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
|
|
|
|
{
|
|
|
|
struct dm_gpio_ops *ops = gpio_get_ops(dev);
|
|
|
|
struct gpio_dev_priv *priv;
|
|
|
|
char *str = buf;
|
|
|
|
int func;
|
|
|
|
int ret;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
|
|
|
|
|
|
|
|
*buf = 0;
|
2015-03-05 19:25:20 +00:00
|
|
|
priv = dev_get_uclass_priv(dev);
|
2014-10-04 17:29:44 +00:00
|
|
|
ret = gpio_get_raw_function(dev, offset, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
func = ret;
|
|
|
|
len = snprintf(str, buffsize, "%s%d: %s",
|
|
|
|
priv->bank_name ? priv->bank_name : "",
|
|
|
|
offset, gpio_function[func]);
|
|
|
|
if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
|
|
|
|
func == GPIOF_UNUSED) {
|
|
|
|
const char *label;
|
|
|
|
bool used;
|
|
|
|
|
|
|
|
ret = ops->get_value(dev, offset);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
|
|
|
|
snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
|
|
|
|
ret,
|
|
|
|
used ? 'x' : ' ',
|
|
|
|
used ? " " : "",
|
|
|
|
label ? label : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-15 03:03:20 +00:00
|
|
|
int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
int gpio;
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
gpio = gpio_num_array[i];
|
|
|
|
if (gpio == -1)
|
|
|
|
break;
|
|
|
|
ret = gpio_requestf(gpio, fmt, i);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
ret = gpio_direction_input(gpio);
|
|
|
|
if (ret) {
|
|
|
|
gpio_free(gpio);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
for (i--; i >= 0; i--)
|
|
|
|
gpio_free(gpio_num_array[i]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-11 01:00:21 +00:00
|
|
|
/*
|
|
|
|
* get a number comprised of multiple GPIO values. gpio_num_array points to
|
|
|
|
* the array of gpio pin numbers to scan, terminated by -1.
|
|
|
|
*/
|
2015-04-15 03:03:20 +00:00
|
|
|
int gpio_get_values_as_int(const int *gpio_list)
|
2014-11-11 01:00:21 +00:00
|
|
|
{
|
|
|
|
int gpio;
|
|
|
|
unsigned bitmask = 1;
|
|
|
|
unsigned vector = 0;
|
2015-04-15 03:03:20 +00:00
|
|
|
int ret;
|
2014-11-11 01:00:21 +00:00
|
|
|
|
|
|
|
while (bitmask &&
|
2015-04-15 03:03:20 +00:00
|
|
|
((gpio = *gpio_list++) != -1)) {
|
|
|
|
ret = gpio_get_value(gpio);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret)
|
2014-11-11 01:00:21 +00:00
|
|
|
vector |= bitmask;
|
|
|
|
bitmask <<= 1;
|
|
|
|
}
|
2015-04-15 03:03:20 +00:00
|
|
|
|
2014-11-11 01:00:21 +00:00
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:27:51 +00:00
|
|
|
int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
|
2016-03-07 02:27:50 +00:00
|
|
|
{
|
|
|
|
unsigned bitmask = 1;
|
|
|
|
unsigned vector = 0;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
ret = dm_gpio_get_value(&desc_list[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret)
|
|
|
|
vector |= bitmask;
|
|
|
|
bitmask <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:18 +00:00
|
|
|
static int gpio_request_tail(int ret, ofnode node,
|
|
|
|
struct ofnode_phandle_args *args,
|
|
|
|
const char *list_name, int index,
|
|
|
|
struct gpio_desc *desc, int flags, bool add_index)
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
{
|
|
|
|
desc->dev = NULL;
|
|
|
|
desc->offset = 0;
|
2016-04-24 23:32:40 +00:00
|
|
|
desc->flags = 0;
|
2017-05-19 02:09:18 +00:00
|
|
|
if (ret)
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
goto err;
|
|
|
|
|
2017-05-19 02:09:18 +00:00
|
|
|
ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
|
|
|
|
&desc->dev);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
if (ret) {
|
2018-03-28 12:39:01 +00:00
|
|
|
debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2017-05-19 02:09:18 +00:00
|
|
|
ret = gpio_find_and_xlate(desc, args);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("%s: gpio_find_and_xlate failed\n", __func__);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
|
2017-05-19 02:09:18 +00:00
|
|
|
ofnode_get_name(node),
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
list_name, index);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: dm_gpio_requestf failed\n", __func__);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: dm_gpio_set_dir failed\n", __func__);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
|
2017-05-19 02:09:18 +00:00
|
|
|
__func__, ofnode_get_name(node), list_name, index, ret);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:47:09 +00:00
|
|
|
static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
|
|
|
|
int index, struct gpio_desc *desc,
|
|
|
|
int flags, bool add_index)
|
2017-05-19 02:09:18 +00:00
|
|
|
{
|
|
|
|
struct ofnode_phandle_args args;
|
|
|
|
int ret;
|
|
|
|
|
2017-05-31 03:47:09 +00:00
|
|
|
ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
|
|
|
|
index, &args);
|
2017-05-19 02:09:18 +00:00
|
|
|
|
2017-05-31 03:47:09 +00:00
|
|
|
return gpio_request_tail(ret, node, &args, list_name, index, desc,
|
|
|
|
flags, add_index);
|
2017-05-19 02:09:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 03:47:09 +00:00
|
|
|
int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
struct gpio_desc *desc, int flags)
|
|
|
|
{
|
2017-05-31 03:47:09 +00:00
|
|
|
return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
|
|
|
|
index > 0);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 03:47:09 +00:00
|
|
|
int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
struct gpio_desc *desc, int flags)
|
|
|
|
{
|
2017-05-31 03:47:09 +00:00
|
|
|
struct ofnode_phandle_args args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
|
|
|
|
index, &args);
|
|
|
|
|
|
|
|
return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
|
|
|
|
index, desc, flags, index > 0);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 03:47:09 +00:00
|
|
|
int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
struct gpio_desc *desc, int max_count,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
int ret;
|
|
|
|
|
2015-03-31 16:57:16 +00:00
|
|
|
for (count = 0; count < max_count; count++) {
|
2017-05-31 03:47:09 +00:00
|
|
|
ret = _gpio_request_by_name_nodev(node, list_name, count,
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
&desc[count], flags, true);
|
|
|
|
if (ret == -ENOENT)
|
|
|
|
break;
|
|
|
|
else if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We ran out of GPIOs in the list */
|
|
|
|
return count;
|
|
|
|
|
|
|
|
err:
|
|
|
|
gpio_free_list_nodev(desc, count - 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
|
|
|
|
struct gpio_desc *desc, int max_count,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This isn't ideal since we don't use dev->name in the debug()
|
|
|
|
* calls in gpio_request_by_name(), but we can do this until
|
|
|
|
* gpio_request_list_by_name_nodev() can be dropped.
|
|
|
|
*/
|
2017-05-31 03:47:09 +00:00
|
|
|
return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
|
|
|
|
max_count, flags);
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_get_list_count(struct udevice *dev, const char *list_name)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-01-17 23:52:55 +00:00
|
|
|
ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
|
dm: gpio: Add better functions to request GPIOs
At present U-Boot sort-of supports the standard way of reading GPIOs from
device tree nodes, but the support is incomplete, a bit clunky and only
works for GPIO bindings where #gpio-cells is 2.
Add new functions to request GPIOs, taking full account of the device
tree binding. These permit requesting a GPIO with a simple call like:
gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
This will request the GPIO, looking at the device's node which might be
this, for example:
cd-gpios = <&gpio TEGRA_GPIO(B, 3) GPIO_ACTIVE_LOW>;
The GPIO will be set to input mode in this case and polarity will be
honoured by the GPIO calls.
It is also possible to request and free a list of GPIOs.
Signed-off-by: Simon Glass <sjg@chromium.org>
2015-01-06 03:05:29 +00:00
|
|
|
list_name, "#gpio-cells", 0, -1,
|
|
|
|
NULL);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
|
|
|
|
__func__, dev->name, list_name, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
|
|
|
|
{
|
|
|
|
/* For now, we don't do any checking of dev */
|
|
|
|
return _dm_gpio_free(desc->dev, desc->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* For now, we don't do any checking of dev */
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
dm_gpio_free(dev, &desc[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_free_list_nodev(struct gpio_desc *desc, int count)
|
|
|
|
{
|
|
|
|
return gpio_free_list(NULL, desc, count);
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-03-05 19:25:20 +00:00
|
|
|
uc_priv = dev_get_uclass_priv(dev);
|
2014-02-26 22:59:24 +00:00
|
|
|
uc_priv->gpio_base = base;
|
|
|
|
base += uc_priv->gpio_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:27:51 +00:00
|
|
|
int gpio_get_number(const struct gpio_desc *desc)
|
2015-03-25 18:21:58 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev = desc->dev;
|
|
|
|
struct gpio_dev_priv *uc_priv;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return -1;
|
|
|
|
uc_priv = dev->uclass_priv;
|
|
|
|
|
|
|
|
return uc_priv->gpio_base + desc->offset;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int gpio_post_probe(struct udevice *dev)
|
2014-02-26 22:59:24 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2014-10-04 17:29:42 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2014-10-04 17:29:42 +00:00
|
|
|
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",
|
2015-06-01 13:07:15 +00:00
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
2014-02-26 22:59:24 +00:00
|
|
|
.post_probe = gpio_post_probe,
|
|
|
|
.pre_remove = gpio_pre_remove,
|
|
|
|
.per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
|
|
|
|
};
|