xref: /openbmc/linux/drivers/mfd/syscon.c (revision a4e1d0b7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * System Control Driver
4  *
5  * Copyright (C) 2012 Freescale Semiconductor, Inc.
6  * Copyright (C) 2012 Linaro Ltd.
7  *
8  * Author: Dong Aisheng <dong.aisheng@linaro.org>
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/err.h>
13 #include <linux/hwspinlock.h>
14 #include <linux/io.h>
15 #include <linux/init.h>
16 #include <linux/list.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_data/syscon.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/slab.h>
25 
26 static struct platform_driver syscon_driver;
27 
28 static DEFINE_SPINLOCK(syscon_list_slock);
29 static LIST_HEAD(syscon_list);
30 
31 struct syscon {
32 	struct device_node *np;
33 	struct regmap *regmap;
34 	struct list_head list;
35 };
36 
37 static const struct regmap_config syscon_regmap_config = {
38 	.reg_bits = 32,
39 	.val_bits = 32,
40 	.reg_stride = 4,
41 };
42 
43 static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
44 {
45 	struct clk *clk;
46 	struct syscon *syscon;
47 	struct regmap *regmap;
48 	void __iomem *base;
49 	u32 reg_io_width;
50 	int ret;
51 	struct regmap_config syscon_config = syscon_regmap_config;
52 	struct resource res;
53 
54 	syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
55 	if (!syscon)
56 		return ERR_PTR(-ENOMEM);
57 
58 	if (of_address_to_resource(np, 0, &res)) {
59 		ret = -ENOMEM;
60 		goto err_map;
61 	}
62 
63 	base = of_iomap(np, 0);
64 	if (!base) {
65 		ret = -ENOMEM;
66 		goto err_map;
67 	}
68 
69 	/* Parse the device's DT node for an endianness specification */
70 	if (of_property_read_bool(np, "big-endian"))
71 		syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
72 	else if (of_property_read_bool(np, "little-endian"))
73 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
74 	else if (of_property_read_bool(np, "native-endian"))
75 		syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
76 
77 	/*
78 	 * search for reg-io-width property in DT. If it is not provided,
79 	 * default to 4 bytes. regmap_init_mmio will return an error if values
80 	 * are invalid so there is no need to check them here.
81 	 */
82 	ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
83 	if (ret)
84 		reg_io_width = 4;
85 
86 	ret = of_hwspin_lock_get_id(np, 0);
87 	if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
88 		syscon_config.use_hwlock = true;
89 		syscon_config.hwlock_id = ret;
90 		syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
91 	} else if (ret < 0) {
92 		switch (ret) {
93 		case -ENOENT:
94 			/* Ignore missing hwlock, it's optional. */
95 			break;
96 		default:
97 			pr_err("Failed to retrieve valid hwlock: %d\n", ret);
98 			fallthrough;
99 		case -EPROBE_DEFER:
100 			goto err_regmap;
101 		}
102 	}
103 
104 	syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &res.start);
105 	syscon_config.reg_stride = reg_io_width;
106 	syscon_config.val_bits = reg_io_width * 8;
107 	syscon_config.max_register = resource_size(&res) - reg_io_width;
108 
109 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
110 	kfree(syscon_config.name);
111 	if (IS_ERR(regmap)) {
112 		pr_err("regmap init failed\n");
113 		ret = PTR_ERR(regmap);
114 		goto err_regmap;
115 	}
116 
117 	if (check_clk) {
118 		clk = of_clk_get(np, 0);
119 		if (IS_ERR(clk)) {
120 			ret = PTR_ERR(clk);
121 			/* clock is optional */
122 			if (ret != -ENOENT)
123 				goto err_clk;
124 		} else {
125 			ret = regmap_mmio_attach_clk(regmap, clk);
126 			if (ret)
127 				goto err_attach;
128 		}
129 	}
130 
131 	syscon->regmap = regmap;
132 	syscon->np = np;
133 
134 	spin_lock(&syscon_list_slock);
135 	list_add_tail(&syscon->list, &syscon_list);
136 	spin_unlock(&syscon_list_slock);
137 
138 	return syscon;
139 
140 err_attach:
141 	if (!IS_ERR(clk))
142 		clk_put(clk);
143 err_clk:
144 	regmap_exit(regmap);
145 err_regmap:
146 	iounmap(base);
147 err_map:
148 	kfree(syscon);
149 	return ERR_PTR(ret);
150 }
151 
152 static struct regmap *device_node_get_regmap(struct device_node *np,
153 					     bool check_clk)
154 {
155 	struct syscon *entry, *syscon = NULL;
156 
157 	spin_lock(&syscon_list_slock);
158 
159 	list_for_each_entry(entry, &syscon_list, list)
160 		if (entry->np == np) {
161 			syscon = entry;
162 			break;
163 		}
164 
165 	spin_unlock(&syscon_list_slock);
166 
167 	if (!syscon)
168 		syscon = of_syscon_register(np, check_clk);
169 
170 	if (IS_ERR(syscon))
171 		return ERR_CAST(syscon);
172 
173 	return syscon->regmap;
174 }
175 
176 struct regmap *device_node_to_regmap(struct device_node *np)
177 {
178 	return device_node_get_regmap(np, false);
179 }
180 EXPORT_SYMBOL_GPL(device_node_to_regmap);
181 
182 struct regmap *syscon_node_to_regmap(struct device_node *np)
183 {
184 	if (!of_device_is_compatible(np, "syscon"))
185 		return ERR_PTR(-EINVAL);
186 
187 	return device_node_get_regmap(np, true);
188 }
189 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
190 
191 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
192 {
193 	struct device_node *syscon_np;
194 	struct regmap *regmap;
195 
196 	syscon_np = of_find_compatible_node(NULL, NULL, s);
197 	if (!syscon_np)
198 		return ERR_PTR(-ENODEV);
199 
200 	regmap = syscon_node_to_regmap(syscon_np);
201 	of_node_put(syscon_np);
202 
203 	return regmap;
204 }
205 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
206 
207 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
208 					const char *property)
209 {
210 	struct device_node *syscon_np;
211 	struct regmap *regmap;
212 
213 	if (property)
214 		syscon_np = of_parse_phandle(np, property, 0);
215 	else
216 		syscon_np = np;
217 
218 	if (!syscon_np)
219 		return ERR_PTR(-ENODEV);
220 
221 	regmap = syscon_node_to_regmap(syscon_np);
222 	of_node_put(syscon_np);
223 
224 	return regmap;
225 }
226 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
227 
228 struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
229 					const char *property,
230 					int arg_count,
231 					unsigned int *out_args)
232 {
233 	struct device_node *syscon_np;
234 	struct of_phandle_args args;
235 	struct regmap *regmap;
236 	unsigned int index;
237 	int rc;
238 
239 	rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
240 			0, &args);
241 	if (rc)
242 		return ERR_PTR(rc);
243 
244 	syscon_np = args.np;
245 	if (!syscon_np)
246 		return ERR_PTR(-ENODEV);
247 
248 	regmap = syscon_node_to_regmap(syscon_np);
249 	for (index = 0; index < arg_count; index++)
250 		out_args[index] = args.args[index];
251 	of_node_put(syscon_np);
252 
253 	return regmap;
254 }
255 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
256 
257 /*
258  * It behaves the same as syscon_regmap_lookup_by_phandle() except where
259  * there is no regmap phandle. In this case, instead of returning -ENODEV,
260  * the function returns NULL.
261  */
262 struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np,
263 					const char *property)
264 {
265 	struct regmap *regmap;
266 
267 	regmap = syscon_regmap_lookup_by_phandle(np, property);
268 	if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
269 		return NULL;
270 
271 	return regmap;
272 }
273 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional);
274 
275 static int syscon_probe(struct platform_device *pdev)
276 {
277 	struct device *dev = &pdev->dev;
278 	struct syscon_platform_data *pdata = dev_get_platdata(dev);
279 	struct syscon *syscon;
280 	struct regmap_config syscon_config = syscon_regmap_config;
281 	struct resource *res;
282 	void __iomem *base;
283 
284 	syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
285 	if (!syscon)
286 		return -ENOMEM;
287 
288 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 	if (!res)
290 		return -ENOENT;
291 
292 	base = devm_ioremap(dev, res->start, resource_size(res));
293 	if (!base)
294 		return -ENOMEM;
295 
296 	syscon_config.max_register = resource_size(res) - 4;
297 	if (pdata)
298 		syscon_config.name = pdata->label;
299 	syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
300 	if (IS_ERR(syscon->regmap)) {
301 		dev_err(dev, "regmap init failed\n");
302 		return PTR_ERR(syscon->regmap);
303 	}
304 
305 	platform_set_drvdata(pdev, syscon);
306 
307 	dev_dbg(dev, "regmap %pR registered\n", res);
308 
309 	return 0;
310 }
311 
312 static const struct platform_device_id syscon_ids[] = {
313 	{ "syscon", },
314 	{ }
315 };
316 
317 static struct platform_driver syscon_driver = {
318 	.driver = {
319 		.name = "syscon",
320 	},
321 	.probe		= syscon_probe,
322 	.id_table	= syscon_ids,
323 };
324 
325 static int __init syscon_init(void)
326 {
327 	return platform_driver_register(&syscon_driver);
328 }
329 postcore_initcall(syscon_init);
330