1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #define LOG_CATEGORY UCLASS_SYSCON
8 
9 #include <common.h>
10 #include <log.h>
11 #include <syscon.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <regmap.h>
15 #include <dm/device-internal.h>
16 #include <dm/device_compat.h>
17 #include <dm/lists.h>
18 #include <dm/root.h>
19 #include <linux/err.h>
20 
21 /*
22  * Caution:
23  * This API requires the given device has already been bound to the syscon
24  * driver. For example,
25  *
26  *    compatible = "syscon", "simple-mfd";
27  *
28  * works, but
29  *
30  *    compatible = "simple-mfd", "syscon";
31  *
32  * does not. The behavior is different from Linux.
33  */
syscon_get_regmap(struct udevice * dev)34 struct regmap *syscon_get_regmap(struct udevice *dev)
35 {
36 	struct syscon_uc_info *priv;
37 
38 	if (device_get_uclass_id(dev) != UCLASS_SYSCON)
39 		return ERR_PTR(-ENOEXEC);
40 	priv = dev_get_uclass_priv(dev);
41 	return priv->regmap;
42 }
43 
syscon_pre_probe(struct udevice * dev)44 static int syscon_pre_probe(struct udevice *dev)
45 {
46 	struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
47 
48 	/* Special case for PCI devices, which don't have a regmap */
49 	if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
50 		return 0;
51 
52 	/*
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, and this holds a single address and size. Drivers
56 	 * using OF_PLATDATA will need to ensure that this is true.
57 	 */
58 #if CONFIG_IS_ENABLED(OF_PLATDATA)
59 	struct syscon_base_plat *plat = dev_get_plat(dev);
60 
61 	return regmap_init_mem_plat(dev, plat->reg, ARRAY_SIZE(plat->reg),
62 					&priv->regmap);
63 #else
64 	return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
65 #endif
66 }
67 
syscon_probe_by_ofnode(ofnode node,struct udevice ** devp)68 static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
69 {
70 	struct udevice *dev, *parent;
71 	int ret;
72 
73 	/* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
74 	if (!ofnode_device_is_compatible(node, "syscon")) {
75 		log_debug("invalid compatible for syscon device\n");
76 		return -EINVAL;
77 	}
78 
79 	/* bound to driver with same ofnode or to root if not found */
80 	if (device_find_global_by_ofnode(node, &parent))
81 		parent = dm_root();
82 
83 	/* force bound to syscon class */
84 	ret = device_bind_driver_to_node(parent, "syscon",
85 					 ofnode_get_name(node),
86 					 node, &dev);
87 	if (ret) {
88 		dev_dbg(dev, "unable to bound syscon device\n");
89 		return ret;
90 	}
91 	ret = device_probe(dev);
92 	if (ret) {
93 		dev_dbg(dev, "unable to probe syscon device\n");
94 		return ret;
95 	}
96 
97 	*devp = dev;
98 	return 0;
99 }
100 
syscon_regmap_lookup_by_phandle(struct udevice * dev,const char * name)101 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
102 					       const char *name)
103 {
104 	struct udevice *syscon;
105 	struct regmap *r;
106 	u32 phandle;
107 	ofnode node;
108 	int err;
109 
110 	err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
111 					   name, &syscon);
112 	if (err) {
113 		/* found node with "syscon" compatible, not bounded to SYSCON */
114 		err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
115 		if (err)
116 			return ERR_PTR(err);
117 
118 		node = ofnode_get_by_phandle(phandle);
119 		if (!ofnode_valid(node)) {
120 			dev_dbg(dev, "unable to find syscon device\n");
121 			return ERR_PTR(-EINVAL);
122 		}
123 		err = syscon_probe_by_ofnode(node, &syscon);
124 		if (err)
125 			return ERR_PTR(-ENODEV);
126 	}
127 
128 	r = syscon_get_regmap(syscon);
129 	if (!r) {
130 		dev_dbg(dev, "unable to find regmap\n");
131 		return ERR_PTR(-ENODEV);
132 	}
133 
134 	return r;
135 }
136 
syscon_get_by_driver_data(ulong driver_data,struct udevice ** devp)137 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
138 {
139 	int ret;
140 
141 	*devp = NULL;
142 
143 	ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
144 	if (ret)
145 		return ret;
146 
147 	return 0;
148 }
149 
syscon_get_regmap_by_driver_data(ulong driver_data)150 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
151 {
152 	struct syscon_uc_info *priv;
153 	struct udevice *dev;
154 	int ret;
155 
156 	ret = syscon_get_by_driver_data(driver_data, &dev);
157 	if (ret)
158 		return ERR_PTR(ret);
159 	priv = dev_get_uclass_priv(dev);
160 
161 	return priv->regmap;
162 }
163 
syscon_get_first_range(ulong driver_data)164 void *syscon_get_first_range(ulong driver_data)
165 {
166 	struct regmap *map;
167 
168 	map = syscon_get_regmap_by_driver_data(driver_data);
169 	if (IS_ERR(map))
170 		return map;
171 	return regmap_get_range(map, 0);
172 }
173 
174 UCLASS_DRIVER(syscon) = {
175 	.id		= UCLASS_SYSCON,
176 	.name		= "syscon",
177 	.per_device_auto	= sizeof(struct syscon_uc_info),
178 	.pre_probe = syscon_pre_probe,
179 };
180 
181 static const struct udevice_id generic_syscon_ids[] = {
182 	{ .compatible = "syscon" },
183 	{ }
184 };
185 
186 U_BOOT_DRIVER(generic_syscon) = {
187 	.name	= "syscon",
188 	.id	= UCLASS_SYSCON,
189 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
190 	.bind           = dm_scan_fdt_dev,
191 #endif
192 	.of_match = generic_syscon_ids,
193 };
194 
195 /*
196  * Linux-compatible syscon-to-regmap
197  * The syscon node can be bound to another driver, but still works
198  * as a syscon provider.
199  */
syscon_node_to_regmap(ofnode node)200 struct regmap *syscon_node_to_regmap(ofnode node)
201 {
202 	struct udevice *dev;
203 	struct regmap *r;
204 
205 	if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
206 		if (syscon_probe_by_ofnode(node, &dev))
207 			return ERR_PTR(-ENODEV);
208 
209 	r = syscon_get_regmap(dev);
210 	if (!r) {
211 		dev_dbg(dev, "unable to find regmap\n");
212 		return ERR_PTR(-ENODEV);
213 	}
214 
215 	return r;
216 }
217