#include <common.h>
#include <dm.h>
+#include <log.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/uclass-internal.h>
#include <fdtdec.h>
#include <malloc.h>
#include <asm/gpio.h>
+#include <dm/device_compat.h>
#include <linux/bug.h>
#include <linux/ctype.h>
if (args->args_count < 2)
return 0;
+ desc->flags = 0;
if (args->args[1] & GPIO_ACTIVE_LOW)
- desc->flags = GPIOD_ACTIVE_LOW;
+ desc->flags |= GPIOD_ACTIVE_LOW;
+
+ /*
+ * need to test 2 bits for gpio output binding:
+ * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
+ * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
+ */
+ if (args->args[1] & GPIO_SINGLE_ENDED) {
+ if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
+ desc->flags |= GPIOD_OPEN_DRAIN;
+ else
+ desc->flags |= GPIOD_OPEN_SOURCE;
+ }
+
+ if (args->args[1] & GPIO_PULL_UP)
+ desc->flags |= GPIOD_PULL_UP;
+
+ if (args->args[1] & GPIO_PULL_DOWN)
+ desc->flags |= GPIOD_PULL_DOWN;
return 0;
}
if (desc->flags & GPIOD_ACTIVE_LOW)
value = !value;
+
+ /*
+ * Emulate open drain by not actively driving the line high or
+ * Emulate open source by not actively driving the line low
+ */
+ if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
+ (desc->flags & GPIOD_OPEN_SOURCE && !value))
+ return gpio_get_ops(desc->dev)->direction_input(desc->dev,
+ desc->offset);
+ else if (desc->flags & GPIOD_OPEN_DRAIN ||
+ desc->flags & GPIOD_OPEN_SOURCE)
+ return gpio_get_ops(desc->dev)->direction_output(desc->dev,
+ desc->offset,
+ value);
+
gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
return 0;
}
-int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
+/* check dir flags invalid configuration */
+static int check_dir_flags(ulong flags)
+{
+ if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
+ log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
+ __func__, flags);
+ return -EINVAL;
+ }
+
+ if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
+ log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
+ __func__, flags);
+ return -EINVAL;
+ }
+
+ if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
+ log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
+ __func__, flags);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static 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);
+ struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+ int ret = 0;
+
+ ret = check_dir_flags(flags);
+ if (ret) {
+ dev_dbg(dev,
+ "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
+ desc->dev->name,
+ uc_priv->bank_name ? uc_priv->bank_name : "",
+ desc->offset, flags);
+
+ return ret;
+ }
+
+ /* GPIOD_ are directly managed by driver in set_dir_flags*/
+ if (ops->set_dir_flags) {
+ ret = ops->set_dir_flags(dev, desc->offset, flags);
+ } else {
+ if (flags & GPIOD_IS_OUT) {
+ ret = ops->direction_output(dev, desc->offset,
+ GPIOD_FLAGS_OUTPUT(flags));
+ } else if (flags & GPIOD_IS_IN) {
+ ret = ops->direction_input(dev, desc->offset);
+ }
+ }
+
+ return ret;
+}
+
+int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
+{
int ret;
- ret = check_reserved(desc, "set_dir");
+ ret = check_reserved(desc, "set_dir_flags");
if (ret)
return ret;
- if (flags & GPIOD_IS_OUT) {
- int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
+ /* combine the requested flags (for IN/OUT) and the descriptor flags */
+ flags |= desc->flags;
+ ret = _dm_gpio_set_dir_flags(desc, flags);
- 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);
- }
+ /* update the descriptor flags */
if (ret)
- return ret;
- /*
- * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
- * futures
- */
- desc->flags = flags;
+ desc->flags = flags;
- return 0;
+ return ret;
}
int dm_gpio_set_dir(struct gpio_desc *desc)
{
- return dm_gpio_set_dir_flags(desc, desc->flags);
+ int ret;
+
+ ret = check_reserved(desc, "set_dir");
+ if (ret)
+ return ret;
+
+ return _dm_gpio_set_dir_flags(desc, desc->flags);
+}
+
+int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
+{
+ struct udevice *dev = desc->dev;
+ int ret, value;
+ struct dm_gpio_ops *ops = gpio_get_ops(dev);
+ ulong dir_flags;
+
+ ret = check_reserved(desc, "get_dir_flags");
+ if (ret)
+ return ret;
+
+ /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
+ if (ops->get_dir_flags) {
+ ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
+ if (ret)
+ return ret;
+
+ /* GPIOD_ACTIVE_LOW is saved in desc->flags */
+ value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
+ if (desc->flags & GPIOD_ACTIVE_LOW)
+ value = !value;
+ dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
+ dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
+ if (value)
+ dir_flags |= GPIOD_IS_OUT_ACTIVE;
+ } else {
+ dir_flags = desc->flags;
+ /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
+ dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
+ if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
+ dir_flags |= GPIOD_IS_OUT_ACTIVE;
+ }
+ *flags = dir_flags;
+
+ return 0;
}
/**
debug("%s: dm_gpio_requestf failed\n", __func__);
goto err;
}
- ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
+ ret = dm_gpio_set_dir_flags(desc, flags);
if (ret) {
debug("%s: dm_gpio_set_dir failed\n", __func__);
goto err;
ops->get_function += gd->reloc_off;
if (ops->xlate)
ops->xlate += gd->reloc_off;
+ if (ops->set_dir_flags)
+ ops->set_dir_flags += gd->reloc_off;
+ if (ops->get_dir_flags)
+ ops->get_dir_flags += gd->reloc_off;
reloc_done++;
}