]> git.sur5r.net Git - u-boot/blobdiff - drivers/core/syscon-uclass.c
adc: add Amlogic Meson SAR ADC driver
[u-boot] / drivers / core / syscon-uclass.c
index a69937e63c15d6db1a96baa6b28715c7f3ddb51a..303e166a69c1b769f5006712c06dc3d855e1c2c4 100644 (file)
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2015 Google, Inc
  * Written by Simon Glass <sjg@chromium.org>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <dm/root.h>
 #include <linux/err.h>
 
+/*
+ * Caution:
+ * This API requires the given device has alerady been bound to syscon driver.
+ * For example,
+ *    compatible = "syscon", "simple-mfd";
+ * works, but
+ *    compatible = "simple-mfd", "syscon";
+ * does not.  The behavior is different from Linux.
+ */
 struct regmap *syscon_get_regmap(struct udevice *dev)
 {
        struct syscon_uc_info *priv;
@@ -41,7 +49,7 @@ static int syscon_pre_probe(struct udevice *dev)
        return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
                                        &priv->regmap);
 #else
-       return regmap_init_mem(dev, &priv->regmap);
+       return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
 #endif
 }
 
@@ -109,3 +117,58 @@ U_BOOT_DRIVER(generic_syscon) = {
 #endif
        .of_match = generic_syscon_ids,
 };
+
+/*
+ * Linux-compatible syscon-to-regmap
+ * The syscon node can be bound to another driver, but still works
+ * as a syscon provider.
+ */
+static LIST_HEAD(syscon_list);
+
+struct syscon {
+       ofnode node;
+       struct regmap *regmap;
+       struct list_head list;
+};
+
+static struct syscon *of_syscon_register(ofnode node)
+{
+       struct syscon *syscon;
+       int ret;
+
+       if (!ofnode_device_is_compatible(node, "syscon"))
+               return ERR_PTR(-EINVAL);
+
+       syscon = malloc(sizeof(*syscon));
+       if (!syscon)
+               return ERR_PTR(-ENOMEM);
+
+       ret = regmap_init_mem(node, &syscon->regmap);
+       if (ret) {
+               free(syscon);
+               return ERR_PTR(ret);
+       }
+
+       list_add_tail(&syscon->list, &syscon_list);
+
+       return syscon;
+}
+
+struct regmap *syscon_node_to_regmap(ofnode node)
+{
+       struct syscon *entry, *syscon = NULL;
+
+       list_for_each_entry(entry, &syscon_list, list)
+               if (ofnode_equal(entry->node, node)) {
+                       syscon = entry;
+                       break;
+               }
+
+       if (!syscon)
+               syscon = of_syscon_register(node);
+
+       if (IS_ERR(syscon))
+               return ERR_CAST(syscon);
+
+       return syscon->regmap;
+}