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);
40 /* Special case for PCI devices, which don't have a regmap */
41 if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
45 * With OF_PLATDATA we really have no way of knowing the format of
46 * the device-specific platform data. So we assume that it starts with
47 * a 'reg' member, and this holds a single address and size. Drivers
48 * using OF_PLATDATA will need to ensure that this is true.
50 #if CONFIG_IS_ENABLED(OF_PLATDATA)
51 struct syscon_base_platdata *plat = dev_get_platdata(dev);
53 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
56 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
60 static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
62 struct udevice *dev, *parent;
65 /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
66 if (!ofnode_device_is_compatible(node, "syscon")) {
67 dev_dbg(dev, "invalid compatible for syscon device\n");
71 /* bound to driver with same ofnode or to root if not found */
72 if (device_find_global_by_ofnode(node, &parent))
75 /* force bound to syscon class */
76 ret = device_bind_driver_to_node(parent, "syscon",
77 ofnode_get_name(node),
80 dev_dbg(dev, "unable to bound syscon device\n");
83 ret = device_probe(dev);
85 dev_dbg(dev, "unable to probe syscon device\n");
93 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
96 struct udevice *syscon;
102 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
105 /* found node with "syscon" compatible, not bounded to SYSCON */
106 err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
110 node = ofnode_get_by_phandle(phandle);
111 if (!ofnode_valid(node)) {
112 dev_dbg(dev, "unable to find syscon device\n");
113 return ERR_PTR(-EINVAL);
115 err = syscon_probe_by_ofnode(node, &syscon);
117 return ERR_PTR(-ENODEV);
120 r = syscon_get_regmap(syscon);
122 dev_dbg(dev, "unable to find regmap\n");
123 return ERR_PTR(-ENODEV);
129 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
135 ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
137 return log_msg_ret("find", ret);
142 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
144 struct syscon_uc_info *priv;
148 ret = syscon_get_by_driver_data(driver_data, &dev);
151 priv = dev_get_uclass_priv(dev);
156 void *syscon_get_first_range(ulong driver_data)
160 map = syscon_get_regmap_by_driver_data(driver_data);
163 return regmap_get_range(map, 0);
166 UCLASS_DRIVER(syscon) = {
169 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
170 .pre_probe = syscon_pre_probe,
173 static const struct udevice_id generic_syscon_ids[] = {
174 { .compatible = "syscon" },
178 U_BOOT_DRIVER(generic_syscon) = {
181 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
182 .bind = dm_scan_fdt_dev,
184 .of_match = generic_syscon_ids,
188 * Linux-compatible syscon-to-regmap
189 * The syscon node can be bound to another driver, but still works
190 * as a syscon provider.
192 struct regmap *syscon_node_to_regmap(ofnode node)
197 if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
198 if (syscon_probe_by_ofnode(node, &dev))
199 return ERR_PTR(-ENODEV);
201 r = syscon_get_regmap(dev);
203 dev_dbg(dev, "unable to find regmap\n");
204 return ERR_PTR(-ENODEV);