1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <dm/device-internal.h>
15 #include <linux/err.h>
19 * This API requires the given device has alerady been bound to syscon driver.
21 * compatible = "syscon", "simple-mfd";
23 * compatible = "simple-mfd", "syscon";
24 * does not. The behavior is different from Linux.
26 struct regmap *syscon_get_regmap(struct udevice *dev)
28 struct syscon_uc_info *priv;
30 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
31 return ERR_PTR(-ENOEXEC);
32 priv = dev_get_uclass_priv(dev);
36 static int syscon_pre_probe(struct udevice *dev)
38 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
41 * With OF_PLATDATA we really have no way of knowing the format of
42 * the device-specific platform data. So we assume that it starts with
43 * a 'reg' member, and this holds a single address and size. Drivers
44 * using OF_PLATDATA will need to ensure that this is true.
46 #if CONFIG_IS_ENABLED(OF_PLATDATA)
47 struct syscon_base_platdata *plat = dev_get_platdata(dev);
49 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
52 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
56 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
63 ret = uclass_get(UCLASS_SYSCON, &uc);
66 uclass_foreach_dev(dev, uc) {
67 if (dev->driver_data == driver_data) {
69 return device_probe(dev);
76 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
78 struct syscon_uc_info *priv;
82 ret = syscon_get_by_driver_data(driver_data, &dev);
85 priv = dev_get_uclass_priv(dev);
90 void *syscon_get_first_range(ulong driver_data)
94 map = syscon_get_regmap_by_driver_data(driver_data);
97 return regmap_get_range(map, 0);
100 UCLASS_DRIVER(syscon) = {
103 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
104 .pre_probe = syscon_pre_probe,
107 static const struct udevice_id generic_syscon_ids[] = {
108 { .compatible = "syscon" },
112 U_BOOT_DRIVER(generic_syscon) = {
115 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
116 .bind = dm_scan_fdt_dev,
118 .of_match = generic_syscon_ids,
122 * Linux-compatible syscon-to-regmap
123 * The syscon node can be bound to another driver, but still works
124 * as a syscon provider.
126 static LIST_HEAD(syscon_list);
130 struct regmap *regmap;
131 struct list_head list;
134 static struct syscon *of_syscon_register(ofnode node)
136 struct syscon *syscon;
139 if (!ofnode_device_is_compatible(node, "syscon"))
140 return ERR_PTR(-EINVAL);
142 syscon = malloc(sizeof(*syscon));
144 return ERR_PTR(-ENOMEM);
146 ret = regmap_init_mem(node, &syscon->regmap);
152 list_add_tail(&syscon->list, &syscon_list);
157 struct regmap *syscon_node_to_regmap(ofnode node)
159 struct syscon *entry, *syscon = NULL;
161 list_for_each_entry(entry, &syscon_list, list)
162 if (ofnode_equal(entry->node, node)) {
168 syscon = of_syscon_register(node);
171 return ERR_CAST(syscon);
173 return syscon->regmap;