1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 #include <dm/device-internal.h>
14 #include <dm/device_compat.h>
17 #include <linux/err.h>
21 * This API requires the given device has already been bound to the syscon
22 * driver. For example,
24 * compatible = "syscon", "simple-mfd";
28 * compatible = "simple-mfd", "syscon";
30 * does not. The behavior is different from Linux.
32 struct regmap *syscon_get_regmap(struct udevice *dev)
34 struct syscon_uc_info *priv;
36 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
37 return ERR_PTR(-ENOEXEC);
38 priv = dev_get_uclass_priv(dev);
42 static int syscon_pre_probe(struct udevice *dev)
44 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
46 /* Special case for PCI devices, which don't have a regmap */
47 if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
51 * With OF_PLATDATA we really have no way of knowing the format of
52 * the device-specific platform data. So we assume that it starts with
53 * a 'reg' member, and this holds a single address and size. Drivers
54 * using OF_PLATDATA will need to ensure that this is true.
56 #if CONFIG_IS_ENABLED(OF_PLATDATA)
57 struct syscon_base_platdata *plat = dev_get_platdata(dev);
59 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
62 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
66 static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
68 struct udevice *dev, *parent;
71 /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
72 if (!ofnode_device_is_compatible(node, "syscon")) {
73 dev_dbg(dev, "invalid compatible for syscon device\n");
77 /* bound to driver with same ofnode or to root if not found */
78 if (device_find_global_by_ofnode(node, &parent))
81 /* force bound to syscon class */
82 ret = device_bind_driver_to_node(parent, "syscon",
83 ofnode_get_name(node),
86 dev_dbg(dev, "unable to bound syscon device\n");
89 ret = device_probe(dev);
91 dev_dbg(dev, "unable to probe syscon device\n");
99 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
102 struct udevice *syscon;
108 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
111 /* found node with "syscon" compatible, not bounded to SYSCON */
112 err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
116 node = ofnode_get_by_phandle(phandle);
117 if (!ofnode_valid(node)) {
118 dev_dbg(dev, "unable to find syscon device\n");
119 return ERR_PTR(-EINVAL);
121 err = syscon_probe_by_ofnode(node, &syscon);
123 return ERR_PTR(-ENODEV);
126 r = syscon_get_regmap(syscon);
128 dev_dbg(dev, "unable to find regmap\n");
129 return ERR_PTR(-ENODEV);
135 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
141 ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
143 return log_msg_ret("find", ret);
148 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
150 struct syscon_uc_info *priv;
154 ret = syscon_get_by_driver_data(driver_data, &dev);
157 priv = dev_get_uclass_priv(dev);
162 void *syscon_get_first_range(ulong driver_data)
166 map = syscon_get_regmap_by_driver_data(driver_data);
169 return regmap_get_range(map, 0);
172 UCLASS_DRIVER(syscon) = {
175 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
176 .pre_probe = syscon_pre_probe,
179 static const struct udevice_id generic_syscon_ids[] = {
180 { .compatible = "syscon" },
184 U_BOOT_DRIVER(generic_syscon) = {
187 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
188 .bind = dm_scan_fdt_dev,
190 .of_match = generic_syscon_ids,
194 * Linux-compatible syscon-to-regmap
195 * The syscon node can be bound to another driver, but still works
196 * as a syscon provider.
198 struct regmap *syscon_node_to_regmap(ofnode node)
203 if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
204 if (syscon_probe_by_ofnode(node, &dev))
205 return ERR_PTR(-ENODEV);
207 r = syscon_get_regmap(dev);
209 dev_dbg(dev, "unable to find regmap\n");
210 return ERR_PTR(-ENODEV);