+// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2013 Google, Inc
- *
- * SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
+#include <dm/device-internal.h>
+#include <dm/lists.h>
+#include <dm/uclass-internal.h>
+#include <dt-bindings/gpio/gpio.h>
#include <errno.h>
#include <fdtdec.h>
#include <malloc.h>
#include <asm/gpio.h>
+#include <linux/bug.h>
#include <linux/ctype.h>
DECLARE_GLOBAL_DATA_PTR;
if (numeric != -1) {
offset = numeric - uc_priv->gpio_base;
/* Allow GPIOs to be numbered from 0 */
- if (offset >= 0 && offset < uc_priv->gpio_count)
+ if (offset < uc_priv->gpio_count)
break;
}
return 0;
}
+int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
+ struct ofnode_phandle_args *args)
+{
+ 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;
+}
+
static int gpio_find_and_xlate(struct gpio_desc *desc,
- struct fdtdec_phandle_args *args)
+ struct ofnode_phandle_args *args)
{
struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
- /* Use the first argument as the offset by default */
- if (args->args_count > 0)
- desc->offset = args->args[0];
+ if (ops->xlate)
+ return ops->xlate(desc->dev, desc, args);
else
- desc->offset = -1;
- desc->flags = 0;
+ return gpio_xlate_offs_flags(desc->dev, desc, args);
+}
+
+#if defined(CONFIG_DM_GPIO_HOG)
+
+struct gpio_hog_priv {
+ struct gpio_desc gpiod;
+};
+
+struct gpio_hog_data {
+ int gpiod_flags;
+ int value;
+ u32 val[2];
+};
- return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
+static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
+{
+ struct gpio_hog_data *plat = dev_get_platdata(dev);
+ const char *nodename;
+ int ret;
+
+ plat->value = 0;
+ if (dev_read_bool(dev, "input")) {
+ plat->gpiod_flags = GPIOD_IS_IN;
+ } else if (dev_read_bool(dev, "output-high")) {
+ plat->value = 1;
+ plat->gpiod_flags = GPIOD_IS_OUT;
+ } else if (dev_read_bool(dev, "output-low")) {
+ plat->gpiod_flags = GPIOD_IS_OUT;
+ } else {
+ printf("%s: missing gpio-hog state.\n", __func__);
+ return -EINVAL;
+ }
+ ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
+ if (ret) {
+ printf("%s: wrong gpios property, 2 values needed %d\n",
+ __func__, ret);
+ return ret;
+ }
+ nodename = dev_read_string(dev, "line-name");
+ if (!nodename)
+ nodename = dev_read_name(dev);
+ device_set_name(dev, nodename);
+
+ return 0;
}
+static int gpio_hog_probe(struct udevice *dev)
+{
+ struct gpio_hog_data *plat = dev_get_platdata(dev);
+ struct gpio_hog_priv *priv = dev_get_priv(dev);
+ int ret;
+
+ ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
+ plat->val[0], plat->gpiod_flags,
+ plat->val[1], &priv->gpiod);
+ if (ret < 0) {
+ debug("%s: node %s could not get gpio.\n", __func__,
+ dev->name);
+ return ret;
+ }
+ dm_gpio_set_dir(&priv->gpiod);
+ if (plat->gpiod_flags == GPIOD_IS_OUT)
+ dm_gpio_set_value(&priv->gpiod, plat->value);
+
+ return 0;
+}
+
+int gpio_hog_probe_all(void)
+{
+ struct udevice *dev;
+ int ret;
+
+ for (uclass_first_device(UCLASS_NOP, &dev);
+ dev;
+ uclass_find_next_device(&dev)) {
+ if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
+ ret = device_probe(dev);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+struct gpio_desc *gpio_hog_lookup_name(const char *name)
+{
+ struct udevice *dev;
+
+ gpio_hog_probe_all();
+ if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
+ struct gpio_hog_priv *priv = dev_get_priv(dev);
+
+ return &priv->gpiod;
+ }
+
+ return NULL;
+}
+
+U_BOOT_DRIVER(gpio_hog) = {
+ .name = "gpio_hog",
+ .id = UCLASS_NOP,
+ .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
+ .probe = gpio_hog_probe,
+ .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
+ .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
+};
+#else
+struct gpio_desc *gpio_hog_lookup_name(const char *name)
+{
+ return NULL;
+}
+#endif
+
int dm_gpio_request(struct gpio_desc *desc, const char *label)
{
struct udevice *dev = desc->dev;
return _dm_gpio_free(desc.dev, desc.offset);
}
-static int check_reserved(struct gpio_desc *desc, const char *func)
+static int check_reserved(const struct gpio_desc *desc, const char *func)
{
struct gpio_dev_priv *uc_priv;
desc.offset, value);
}
-int dm_gpio_get_value(struct gpio_desc *desc)
+int dm_gpio_get_value(const struct gpio_desc *desc)
{
int value;
int ret;
return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
}
-int dm_gpio_set_value(struct gpio_desc *desc, int value)
+int dm_gpio_set_value(const struct gpio_desc *desc, int value)
{
int ret;
return 0;
}
+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;
+}
+
int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
{
struct udevice *dev = desc->dev;
"func",
};
-int get_function(struct udevice *dev, int offset, bool skip_unused,
- const char **namep)
+static int get_function(struct udevice *dev, int offset, bool skip_unused,
+ const char **namep)
{
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
struct dm_gpio_ops *ops = gpio_get_ops(dev);
return vector;
}
-static int _gpio_request_by_name_nodev(const void *blob, int node,
- const char *list_name, int index,
- struct gpio_desc *desc, int flags,
- bool add_index)
+int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
{
- struct fdtdec_phandle_args args;
- int ret;
+ unsigned bitmask = 1;
+ unsigned vector = 0;
+ int ret, i;
- desc->dev = NULL;
- desc->offset = 0;
- ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
- "#gpio-cells", 0, index, &args);
- if (ret) {
- debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
- goto err;
+ 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;
}
- ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
- &desc->dev);
- if (ret) {
- debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
+ return vector;
+}
+
+static int gpio_request_tail(int ret, const char *nodename,
+ struct ofnode_phandle_args *args,
+ const char *list_name, int index,
+ struct gpio_desc *desc, int flags,
+ bool add_index, struct udevice *dev)
+{
+ desc->dev = dev;
+ desc->offset = 0;
+ desc->flags = 0;
+ if (ret)
goto err;
+
+ if (!desc->dev) {
+ ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
+ &desc->dev);
+ if (ret) {
+ debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
+ goto err;
+ }
}
- ret = gpio_find_and_xlate(desc, &args);
+ ret = gpio_find_and_xlate(desc, args);
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",
- fdt_get_name(blob, node, NULL),
- list_name, index);
+ nodename, list_name, index);
if (ret) {
debug("%s: dm_gpio_requestf failed\n", __func__);
goto err;
return 0;
err:
debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
- __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
+ __func__, nodename, list_name, index, ret);
return ret;
}
-int gpio_request_by_name_nodev(const void *blob, int node,
- const char *list_name, int index,
+static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
+ int index, struct gpio_desc *desc,
+ int flags, bool add_index)
+{
+ struct ofnode_phandle_args args;
+ int ret;
+
+ ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
+ index, &args);
+
+ return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
+ index, desc, flags, add_index, NULL);
+}
+
+int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
struct gpio_desc *desc, int flags)
{
- return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
- flags, index > 0);
+ return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
+ index > 0);
}
-int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
+int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
struct gpio_desc *desc, 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_by_name_nodev() can be dropped.
- */
- return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
- list_name, index, desc, flags);
+ struct ofnode_phandle_args args;
+ ofnode node;
+ int ret;
+
+ ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
+ index, &args);
+ node = dev_ofnode(dev);
+ return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
+ index, desc, flags, index > 0, NULL);
}
-int gpio_request_list_by_name_nodev(const void *blob, int node,
- const char *list_name,
+int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
struct gpio_desc *desc, int max_count,
int flags)
{
int ret;
for (count = 0; count < max_count; count++) {
- ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
+ ret = _gpio_request_by_name_nodev(node, list_name, count,
&desc[count], flags, true);
if (ret == -ENOENT)
break;
* calls in gpio_request_by_name(), but we can do this until
* gpio_request_list_by_name_nodev() can be dropped.
*/
- return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
- list_name, desc, max_count,
- flags);
+ return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
+ max_count, flags);
}
int gpio_get_list_count(struct udevice *dev, const char *list_name)
{
int ret;
- ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
+ ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
list_name, "#gpio-cells", 0, -1,
NULL);
if (ret) {
return 0;
}
-int gpio_get_number(struct gpio_desc *desc)
+int gpio_get_number(const struct gpio_desc *desc)
{
struct udevice *dev = desc->dev;
struct gpio_dev_priv *uc_priv;
return gpio_renumber(dev);
}
+int gpio_dev_request_index(struct udevice *dev, const char *nodename,
+ char *list_name, int index, int flags,
+ int dtflags, struct gpio_desc *desc)
+{
+ struct ofnode_phandle_args args;
+
+ args.node = ofnode_null();
+ args.args_count = 2;
+ args.args[0] = index;
+ args.args[1] = dtflags;
+
+ return gpio_request_tail(0, nodename, &args, list_name, index, desc,
+ flags, 0, dev);
+}
+
+static int gpio_post_bind(struct udevice *dev)
+{
+#if defined(CONFIG_DM_GPIO_HOG)
+ struct udevice *child;
+ ofnode node;
+#endif
+
+#if defined(CONFIG_NEEDS_MANUAL_RELOC)
+ struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
+ static int reloc_done;
+
+ if (!reloc_done) {
+ if (ops->request)
+ ops->request += gd->reloc_off;
+ if (ops->free)
+ ops->free += gd->reloc_off;
+ if (ops->direction_input)
+ ops->direction_input += gd->reloc_off;
+ if (ops->direction_output)
+ ops->direction_output += gd->reloc_off;
+ if (ops->get_value)
+ ops->get_value += gd->reloc_off;
+ if (ops->set_value)
+ ops->set_value += gd->reloc_off;
+ if (ops->get_open_drain)
+ ops->get_open_drain += gd->reloc_off;
+ if (ops->set_open_drain)
+ ops->set_open_drain += gd->reloc_off;
+ if (ops->get_function)
+ ops->get_function += gd->reloc_off;
+ if (ops->xlate)
+ ops->xlate += gd->reloc_off;
+
+ reloc_done++;
+ }
+#endif
+
+#if defined(CONFIG_DM_GPIO_HOG)
+ dev_for_each_subnode(node, dev) {
+ if (ofnode_read_bool(node, "gpio-hog")) {
+ const char *name = ofnode_get_name(node);
+
+ device_bind_driver_to_node(dev, "gpio_hog", name,
+ node, &child);
+ }
+ }
+#endif
+ return 0;
+}
+
UCLASS_DRIVER(gpio) = {
.id = UCLASS_GPIO,
.name = "gpio",
.flags = DM_UC_FLAG_SEQ_ALIAS,
.post_probe = gpio_post_probe,
+ .post_bind = gpio_post_bind,
.pre_remove = gpio_pre_remove,
.per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
};