1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #define LOG_CATEGORY UCLASS_SYSCON
14 #include <dm/device-internal.h>
15 #include <dm/device_compat.h>
18 #include <linux/err.h>
22 * This API requires the given device has already been bound to the syscon
23 * driver. For example,
25 * compatible = "syscon", "simple-mfd";
29 * compatible = "simple-mfd", "syscon";
31 * does not. The behavior is different from Linux.
33 struct regmap *syscon_get_regmap(struct udevice *dev)
35 struct syscon_uc_info *priv;
37 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
38 return ERR_PTR(-ENOEXEC);
39 priv = dev_get_uclass_priv(dev);
43 static int syscon_pre_probe(struct udevice *dev)
45 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
47 /* Special case for PCI devices, which don't have a regmap */
48 if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
51 #if CONFIG_IS_ENABLED(OF_PLATDATA)
53 * With OF_PLATDATA we really have no way of knowing the format of
54 * the device-specific platform data. So we assume that it starts with
55 * a 'reg' member that holds a single address and size. Drivers
56 * using OF_PLATDATA will need to ensure that this is true. In case of
57 * odd reg structures other then the syscon_base_plat structure
58 * below the regmap must be defined in the individual syscon driver.
60 struct syscon_base_plat {
64 struct syscon_base_plat *plat = dev_get_plat(dev);
67 * Return if the regmap is already defined in the individual
73 return regmap_init_mem_plat(dev, plat->reg, sizeof(plat->reg[0]),
74 ARRAY_SIZE(plat->reg) / 2, &priv->regmap);
76 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
80 static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
82 struct udevice *dev, *parent;
85 /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
86 if (!ofnode_device_is_compatible(node, "syscon")) {
87 log_debug("invalid compatible for syscon device\n");
91 /* bound to driver with same ofnode or to root if not found */
92 if (device_find_global_by_ofnode(node, &parent))
95 /* force bound to syscon class */
96 ret = device_bind_driver_to_node(parent, "syscon",
97 ofnode_get_name(node),
100 dev_dbg(dev, "unable to bound syscon device\n");
103 ret = device_probe(dev);
105 dev_dbg(dev, "unable to probe syscon device\n");
113 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
116 struct udevice *syscon;
122 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
125 /* found node with "syscon" compatible, not bounded to SYSCON */
126 err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
130 node = ofnode_get_by_phandle(phandle);
131 if (!ofnode_valid(node)) {
132 dev_dbg(dev, "unable to find syscon device\n");
133 return ERR_PTR(-EINVAL);
135 err = syscon_probe_by_ofnode(node, &syscon);
137 return ERR_PTR(-ENODEV);
140 r = syscon_get_regmap(syscon);
142 dev_dbg(dev, "unable to find regmap\n");
143 return ERR_PTR(-ENODEV);
149 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
155 ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
162 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
164 struct syscon_uc_info *priv;
168 ret = syscon_get_by_driver_data(driver_data, &dev);
171 priv = dev_get_uclass_priv(dev);
176 void *syscon_get_first_range(ulong driver_data)
180 map = syscon_get_regmap_by_driver_data(driver_data);
183 return regmap_get_range(map, 0);
186 UCLASS_DRIVER(syscon) = {
189 .per_device_auto = sizeof(struct syscon_uc_info),
190 .pre_probe = syscon_pre_probe,
193 static const struct udevice_id generic_syscon_ids[] = {
194 { .compatible = "syscon" },
198 U_BOOT_DRIVER(generic_syscon) = {
201 #if CONFIG_IS_ENABLED(OF_REAL)
202 .bind = dm_scan_fdt_dev,
204 .of_match = generic_syscon_ids,
208 * Linux-compatible syscon-to-regmap
209 * The syscon node can be bound to another driver, but still works
210 * as a syscon provider.
212 struct regmap *syscon_node_to_regmap(ofnode node)
217 if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
218 if (syscon_probe_by_ofnode(node, &dev))
219 return ERR_PTR(-ENODEV);
221 r = syscon_get_regmap(dev);
223 dev_dbg(dev, "unable to find regmap\n");
224 return ERR_PTR(-ENODEV);