2012-02-15 23:51:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 The Chromium OS Authors.
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-02-15 23:51:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2014-02-26 22:59:25 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <malloc.h>
|
2012-02-15 23:51:13 +00:00
|
|
|
#include <asm/gpio.h>
|
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
|
|
|
#include <dt-bindings/gpio/gpio.h>
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2012-02-15 23:51:13 +00:00
|
|
|
/* Flags for each GPIO */
|
|
|
|
#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
|
|
|
|
#define GPIOF_HIGH (1 << 1) /* Currently set high */
|
|
|
|
|
|
|
|
struct gpio_state {
|
|
|
|
const char *label; /* label given by requester */
|
|
|
|
u8 flags; /* flags (GPIOF_...) */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Access routines for GPIO state */
|
2014-05-22 10:43:05 +00:00
|
|
|
static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2014-02-26 22:59:25 +00:00
|
|
|
struct gpio_state *state = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (offset >= uc_priv->gpio_count) {
|
2012-02-15 23:51:13 +00:00
|
|
|
static u8 invalid_flags;
|
2014-02-26 22:59:25 +00:00
|
|
|
printf("sandbox_gpio: error: invalid gpio %u\n", offset);
|
2012-02-15 23:51:13 +00:00
|
|
|
return &invalid_flags;
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
return &state[offset].flags;
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
return (*get_gpio_flags(dev, offset) & flag) != 0;
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
|
2014-02-26 22:59:25 +00:00
|
|
|
int value)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
u8 *gpio = get_gpio_flags(dev, offset);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
*gpio |= flag;
|
|
|
|
else
|
|
|
|
*gpio &= ~flag;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Back-channel sandbox-internal-only access to GPIO state
|
|
|
|
*/
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
|
|
|
|
debug("sandbox_gpio: get_value on output gpio %u\n", offset);
|
|
|
|
return get_gpio_flag(dev, offset, GPIOF_HIGH);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These functions implement the public interface within U-Boot
|
|
|
|
*/
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
/* set GPIO port 'offset' as an input */
|
2014-05-22 10:43:05 +00:00
|
|
|
static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
debug("%s: offset:%u\n", __func__, offset);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
return sandbox_gpio_set_direction(dev, offset, 0);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
/* set GPIO port 'offset' as an output, with polarity 'value' */
|
2014-05-22 10:43:05 +00:00
|
|
|
static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
|
2014-02-26 22:59:25 +00:00
|
|
|
int value)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
debug("%s: offset:%u, value = %d\n", __func__, offset, value);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
return sandbox_gpio_set_direction(dev, offset, 1) |
|
|
|
|
sandbox_gpio_set_value(dev, offset, value);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
/* read GPIO IN value of port 'offset' */
|
2014-05-22 10:43:05 +00:00
|
|
|
static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
debug("%s: offset:%u\n", __func__, offset);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
return sandbox_gpio_get_value(dev, offset);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
/* write GPIO OUT value to port 'offset' */
|
2014-05-22 10:43:05 +00:00
|
|
|
static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
|
2012-02-15 23:51:13 +00:00
|
|
|
{
|
2014-02-26 22:59:25 +00:00
|
|
|
debug("%s: offset:%u, value = %d\n", __func__, offset, value);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
if (!sandbox_gpio_get_direction(dev, offset)) {
|
|
|
|
printf("sandbox_gpio: error: set_value on input gpio %u\n",
|
|
|
|
offset);
|
2012-02-15 23:51:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
return sandbox_gpio_set_value(dev, offset, value);
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 17:29:45 +00:00
|
|
|
static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
|
|
|
if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
|
|
|
|
return GPIOF_OUTPUT;
|
|
|
|
return GPIOF_INPUT;
|
|
|
|
}
|
|
|
|
|
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 sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
|
|
|
|
struct fdtdec_phandle_args *args)
|
|
|
|
{
|
|
|
|
desc->offset = args->args[0];
|
|
|
|
if (args->args_count < 2)
|
|
|
|
return 0;
|
|
|
|
if (args->args[1] & GPIO_ACTIVE_LOW)
|
|
|
|
desc->flags |= GPIOD_ACTIVE_LOW;
|
|
|
|
if (args->args[1] & 2)
|
|
|
|
desc->flags |= GPIOD_IS_IN;
|
|
|
|
if (args->args[1] & 4)
|
|
|
|
desc->flags |= GPIOD_IS_OUT;
|
|
|
|
if (args->args[1] & 8)
|
|
|
|
desc->flags |= GPIOD_IS_OUT_ACTIVE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
static const struct dm_gpio_ops gpio_sandbox_ops = {
|
|
|
|
.direction_input = sb_gpio_direction_input,
|
|
|
|
.direction_output = sb_gpio_direction_output,
|
|
|
|
.get_value = sb_gpio_get_value,
|
|
|
|
.set_value = sb_gpio_set_value,
|
2014-10-04 17:29:45 +00:00
|
|
|
.get_function = sb_gpio_get_function,
|
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
|
|
|
.xlate = sb_gpio_xlate,
|
2014-02-26 22:59:25 +00:00
|
|
|
};
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
|
2014-02-26 22:59:25 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
|
|
|
|
"num-gpios", 0);
|
|
|
|
uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
|
|
|
|
"gpio-bank-name", NULL);
|
2012-02-15 23:51:13 +00:00
|
|
|
|
2014-02-26 22:59:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
static int gpio_sandbox_probe(struct udevice *dev)
|
2014-02-26 22:59:25 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2014-02-26 22:59:25 +00:00
|
|
|
|
|
|
|
if (dev->of_offset == -1) {
|
|
|
|
/* Tell the uclass how many GPIOs we have */
|
|
|
|
uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
2014-02-26 22:59:25 +00:00
|
|
|
|
|
|
|
dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
|
|
|
|
|
|
|
|
return 0;
|
2012-02-15 23:51:13 +00:00
|
|
|
}
|
2014-02-26 22:59:25 +00:00
|
|
|
|
2014-10-04 17:29:46 +00:00
|
|
|
static int gpio_sandbox_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
free(dev->priv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-12 05:29:45 +00:00
|
|
|
static const struct udevice_id sandbox_gpio_ids[] = {
|
2014-02-26 22:59:25 +00:00
|
|
|
{ .compatible = "sandbox,gpio" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(gpio_sandbox) = {
|
|
|
|
.name = "gpio_sandbox",
|
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.of_match = sandbox_gpio_ids,
|
|
|
|
.ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
|
|
|
|
.probe = gpio_sandbox_probe,
|
2014-10-04 17:29:46 +00:00
|
|
|
.remove = gpio_sandbox_remove,
|
2014-02-26 22:59:25 +00:00
|
|
|
.ops = &gpio_sandbox_ops,
|
|
|
|
};
|