1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
12 #include <dm/device-internal.h>
13 #include <dm/device_compat.h>
16 #include <linux/err.h>
20 * This API requires the given device has alerady been bound to syscon driver.
22 * compatible = "syscon", "simple-mfd";
24 * compatible = "simple-mfd", "syscon";
25 * does not. The behavior is different from Linux.
27 struct regmap *syscon_get_regmap(struct udevice *dev)
29 struct syscon_uc_info *priv;
31 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
32 return ERR_PTR(-ENOEXEC);
33 priv = dev_get_uclass_priv(dev);
37 static int syscon_pre_probe(struct udevice *dev)
39 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
41 /* Special case for PCI devices, which don't have a regmap */
42 if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
46 * With OF_PLATDATA we really have no way of knowing the format of
47 * the device-specific platform data. So we assume that it starts with
48 * a 'reg' member, and this holds a single address and size. Drivers
49 * using OF_PLATDATA will need to ensure that this is true.
51 #if CONFIG_IS_ENABLED(OF_PLATDATA)
52 struct syscon_base_platdata *plat = dev_get_platdata(dev);
54 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
57 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
61 static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
63 struct udevice *dev, *parent;
66 /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
67 if (!ofnode_device_is_compatible(node, "syscon")) {
68 dev_dbg(dev, "invalid compatible for syscon device\n");
72 /* bound to driver with same ofnode or to root if not found */
73 if (device_find_global_by_ofnode(node, &parent))
76 /* force bound to syscon class */
77 ret = device_bind_driver_to_node(parent, "syscon",
78 ofnode_get_name(node),
81 dev_dbg(dev, "unable to bound syscon device\n");
84 ret = device_probe(dev);
86 dev_dbg(dev, "unable to probe syscon device\n");
94 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
97 struct udevice *syscon;
103 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
106 /* found node with "syscon" compatible, not bounded to SYSCON */
107 err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
111 node = ofnode_get_by_phandle(phandle);
112 if (!ofnode_valid(node)) {
113 dev_dbg(dev, "unable to find syscon device\n");
114 return ERR_PTR(-EINVAL);
116 err = syscon_probe_by_ofnode(node, &syscon);
118 return ERR_PTR(-ENODEV);
121 r = syscon_get_regmap(syscon);
123 dev_dbg(dev, "unable to find regmap\n");
124 return ERR_PTR(-ENODEV);
130 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
137 ret = uclass_get(UCLASS_SYSCON, &uc);
140 uclass_foreach_dev(dev, uc) {
141 if (dev->driver_data == driver_data) {
143 return device_probe(dev);
150 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
152 struct syscon_uc_info *priv;
156 ret = syscon_get_by_driver_data(driver_data, &dev);
159 priv = dev_get_uclass_priv(dev);
164 void *syscon_get_first_range(ulong driver_data)
168 map = syscon_get_regmap_by_driver_data(driver_data);
171 return regmap_get_range(map, 0);
174 UCLASS_DRIVER(syscon) = {
177 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
178 .pre_probe = syscon_pre_probe,
181 static const struct udevice_id generic_syscon_ids[] = {
182 { .compatible = "syscon" },
186 U_BOOT_DRIVER(generic_syscon) = {
189 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
190 .bind = dm_scan_fdt_dev,
192 .of_match = generic_syscon_ids,
196 * Linux-compatible syscon-to-regmap
197 * The syscon node can be bound to another driver, but still works
198 * as a syscon provider.
200 struct regmap *syscon_node_to_regmap(ofnode node)
205 if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
206 if (syscon_probe_by_ofnode(node, &dev))
207 return ERR_PTR(-ENODEV);
209 r = syscon_get_regmap(dev);
211 dev_dbg(dev, "unable to find regmap\n");
212 return ERR_PTR(-ENODEV);