xref: /openbmc/linux/drivers/mfd/syscon.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
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/cleanup.h>
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/hwspinlock.h>
15 #include <linux/io.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_data/syscon.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/reset.h>
26 #include <linux/mfd/syscon.h>
27 #include <linux/slab.h>
28 
29 static struct platform_driver syscon_driver;
30 
31 static DEFINE_MUTEX(syscon_list_lock);
32 static LIST_HEAD(syscon_list);
33 
34 struct syscon {
35 	struct device_node *np;
36 	struct regmap *regmap;
37 	struct reset_control *reset;
38 	struct list_head list;
39 };
40 
41 static const struct regmap_config syscon_regmap_config = {
42 	.reg_bits = 32,
43 	.val_bits = 32,
44 	.reg_stride = 4,
45 };
46 
of_syscon_register(struct device_node * np,bool check_res)47 static struct syscon *of_syscon_register(struct device_node *np, bool check_res)
48 {
49 	struct clk *clk;
50 	struct regmap *regmap;
51 	void __iomem *base;
52 	u32 reg_io_width;
53 	int ret;
54 	struct regmap_config syscon_config = syscon_regmap_config;
55 	struct resource res;
56 	struct reset_control *reset;
57 
58 	WARN_ON(!mutex_is_locked(&syscon_list_lock));
59 
60 	struct syscon *syscon __free(kfree) = kzalloc(sizeof(*syscon), GFP_KERNEL);
61 	if (!syscon)
62 		return ERR_PTR(-ENOMEM);
63 
64 	if (of_address_to_resource(np, 0, &res))
65 		return ERR_PTR(-ENOMEM);
66 
67 	base = of_iomap(np, 0);
68 	if (!base)
69 		return ERR_PTR(-ENOMEM);
70 
71 	/* Parse the device's DT node for an endianness specification */
72 	if (of_property_read_bool(np, "big-endian"))
73 		syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
74 	else if (of_property_read_bool(np, "little-endian"))
75 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
76 	else if (of_property_read_bool(np, "native-endian"))
77 		syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
78 
79 	/*
80 	 * search for reg-io-width property in DT. If it is not provided,
81 	 * default to 4 bytes. regmap_init_mmio will return an error if values
82 	 * are invalid so there is no need to check them here.
83 	 */
84 	ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
85 	if (ret)
86 		reg_io_width = 4;
87 
88 	ret = of_hwspin_lock_get_id(np, 0);
89 	if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
90 		syscon_config.use_hwlock = true;
91 		syscon_config.hwlock_id = ret;
92 		syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
93 	} else if (ret < 0) {
94 		switch (ret) {
95 		case -ENOENT:
96 			/* Ignore missing hwlock, it's optional. */
97 			break;
98 		default:
99 			pr_err("Failed to retrieve valid hwlock: %d\n", ret);
100 			fallthrough;
101 		case -EPROBE_DEFER:
102 			goto err_regmap;
103 		}
104 	}
105 
106 	syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &res.start);
107 	if (!syscon_config.name) {
108 		ret = -ENOMEM;
109 		goto err_regmap;
110 	}
111 	syscon_config.reg_stride = reg_io_width;
112 	syscon_config.val_bits = reg_io_width * 8;
113 	syscon_config.max_register = resource_size(&res) - reg_io_width;
114 
115 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
116 	kfree(syscon_config.name);
117 	if (IS_ERR(regmap)) {
118 		pr_err("regmap init failed\n");
119 		ret = PTR_ERR(regmap);
120 		goto err_regmap;
121 	}
122 
123 	if (check_res) {
124 		clk = of_clk_get(np, 0);
125 		if (IS_ERR(clk)) {
126 			ret = PTR_ERR(clk);
127 			/* clock is optional */
128 			if (ret != -ENOENT)
129 				goto err_clk;
130 		} else {
131 			ret = regmap_mmio_attach_clk(regmap, clk);
132 			if (ret)
133 				goto err_attach_clk;
134 		}
135 
136 		reset = of_reset_control_get_optional_exclusive(np, NULL);
137 		if (IS_ERR(reset)) {
138 			ret = PTR_ERR(reset);
139 			goto err_attach_clk;
140 		}
141 
142 		ret = reset_control_deassert(reset);
143 		if (ret)
144 			goto err_reset;
145 	}
146 
147 	syscon->regmap = regmap;
148 	syscon->np = np;
149 
150 	list_add_tail(&syscon->list, &syscon_list);
151 
152 	return_ptr(syscon);
153 
154 err_reset:
155 	reset_control_put(reset);
156 err_attach_clk:
157 	if (!IS_ERR(clk))
158 		clk_put(clk);
159 err_clk:
160 	regmap_exit(regmap);
161 err_regmap:
162 	iounmap(base);
163 	return ERR_PTR(ret);
164 }
165 
device_node_get_regmap(struct device_node * np,bool check_res)166 static struct regmap *device_node_get_regmap(struct device_node *np,
167 					     bool check_res)
168 {
169 	struct syscon *entry, *syscon = NULL;
170 
171 	mutex_lock(&syscon_list_lock);
172 
173 	list_for_each_entry(entry, &syscon_list, list)
174 		if (entry->np == np) {
175 			syscon = entry;
176 			break;
177 		}
178 
179 	if (!syscon)
180 		syscon = of_syscon_register(np, check_res);
181 
182 	mutex_unlock(&syscon_list_lock);
183 
184 	if (IS_ERR(syscon))
185 		return ERR_CAST(syscon);
186 
187 	return syscon->regmap;
188 }
189 
190 /**
191  * of_syscon_register_regmap() - Register regmap for specified device node
192  * @np: Device tree node
193  * @regmap: Pointer to regmap object
194  *
195  * Register an externally created regmap object with syscon for the specified
196  * device tree node. This regmap will then be returned to client drivers using
197  * the syscon_regmap_lookup_by_phandle() API.
198  *
199  * Return: 0 on success, negative error code on failure.
200  */
of_syscon_register_regmap(struct device_node * np,struct regmap * regmap)201 int of_syscon_register_regmap(struct device_node *np, struct regmap *regmap)
202 {
203 	struct syscon *entry, *syscon = NULL;
204 	int ret;
205 
206 	if (!np || !regmap)
207 		return -EINVAL;
208 
209 	syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
210 	if (!syscon)
211 		return -ENOMEM;
212 
213 	/* check if syscon entry already exists */
214 	mutex_lock(&syscon_list_lock);
215 
216 	list_for_each_entry(entry, &syscon_list, list)
217 		if (entry->np == np) {
218 			ret = -EEXIST;
219 			goto err_unlock;
220 		}
221 
222 	syscon->regmap = regmap;
223 	syscon->np = np;
224 
225 	/* register the regmap in syscon list */
226 	list_add_tail(&syscon->list, &syscon_list);
227 	mutex_unlock(&syscon_list_lock);
228 
229 	return 0;
230 
231 err_unlock:
232 	mutex_unlock(&syscon_list_lock);
233 	kfree(syscon);
234 	return ret;
235 }
236 EXPORT_SYMBOL_GPL(of_syscon_register_regmap);
237 
device_node_to_regmap(struct device_node * np)238 struct regmap *device_node_to_regmap(struct device_node *np)
239 {
240 	return device_node_get_regmap(np, false);
241 }
242 EXPORT_SYMBOL_GPL(device_node_to_regmap);
243 
syscon_node_to_regmap(struct device_node * np)244 struct regmap *syscon_node_to_regmap(struct device_node *np)
245 {
246 	if (!of_device_is_compatible(np, "syscon"))
247 		return ERR_PTR(-EINVAL);
248 
249 	return device_node_get_regmap(np, true);
250 }
251 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
252 
syscon_regmap_lookup_by_compatible(const char * s)253 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
254 {
255 	struct device_node *syscon_np;
256 	struct regmap *regmap;
257 
258 	syscon_np = of_find_compatible_node(NULL, NULL, s);
259 	if (!syscon_np)
260 		return ERR_PTR(-ENODEV);
261 
262 	regmap = syscon_node_to_regmap(syscon_np);
263 	of_node_put(syscon_np);
264 
265 	return regmap;
266 }
267 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
268 
syscon_regmap_lookup_by_phandle(struct device_node * np,const char * property)269 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
270 					const char *property)
271 {
272 	struct device_node *syscon_np;
273 	struct regmap *regmap;
274 
275 	if (property)
276 		syscon_np = of_parse_phandle(np, property, 0);
277 	else
278 		syscon_np = np;
279 
280 	if (!syscon_np)
281 		return ERR_PTR(-ENODEV);
282 
283 	regmap = syscon_node_to_regmap(syscon_np);
284 
285 	if (property)
286 		of_node_put(syscon_np);
287 
288 	return regmap;
289 }
290 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
291 
syscon_regmap_lookup_by_phandle_args(struct device_node * np,const char * property,int arg_count,unsigned int * out_args)292 struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
293 					const char *property,
294 					int arg_count,
295 					unsigned int *out_args)
296 {
297 	struct device_node *syscon_np;
298 	struct of_phandle_args args;
299 	struct regmap *regmap;
300 	unsigned int index;
301 	int rc;
302 
303 	rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
304 			0, &args);
305 	if (rc)
306 		return ERR_PTR(rc);
307 
308 	syscon_np = args.np;
309 	if (!syscon_np)
310 		return ERR_PTR(-ENODEV);
311 
312 	regmap = syscon_node_to_regmap(syscon_np);
313 	for (index = 0; index < arg_count; index++)
314 		out_args[index] = args.args[index];
315 	of_node_put(syscon_np);
316 
317 	return regmap;
318 }
319 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
320 
321 /*
322  * It behaves the same as syscon_regmap_lookup_by_phandle() except where
323  * there is no regmap phandle. In this case, instead of returning -ENODEV,
324  * the function returns NULL.
325  */
syscon_regmap_lookup_by_phandle_optional(struct device_node * np,const char * property)326 struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np,
327 					const char *property)
328 {
329 	struct regmap *regmap;
330 
331 	regmap = syscon_regmap_lookup_by_phandle(np, property);
332 	if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
333 		return NULL;
334 
335 	return regmap;
336 }
337 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional);
338 
syscon_probe(struct platform_device * pdev)339 static int syscon_probe(struct platform_device *pdev)
340 {
341 	struct device *dev = &pdev->dev;
342 	struct syscon_platform_data *pdata = dev_get_platdata(dev);
343 	struct syscon *syscon;
344 	struct regmap_config syscon_config = syscon_regmap_config;
345 	struct resource *res;
346 	void __iomem *base;
347 
348 	syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
349 	if (!syscon)
350 		return -ENOMEM;
351 
352 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
353 	if (!res)
354 		return -ENOENT;
355 
356 	base = devm_ioremap(dev, res->start, resource_size(res));
357 	if (!base)
358 		return -ENOMEM;
359 
360 	syscon_config.max_register = resource_size(res) - 4;
361 	if (pdata)
362 		syscon_config.name = pdata->label;
363 	syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
364 	if (IS_ERR(syscon->regmap)) {
365 		dev_err(dev, "regmap init failed\n");
366 		return PTR_ERR(syscon->regmap);
367 	}
368 
369 	platform_set_drvdata(pdev, syscon);
370 
371 	dev_dbg(dev, "regmap %pR registered\n", res);
372 
373 	return 0;
374 }
375 
376 static const struct platform_device_id syscon_ids[] = {
377 	{ "syscon", },
378 	{ }
379 };
380 
381 static struct platform_driver syscon_driver = {
382 	.driver = {
383 		.name = "syscon",
384 	},
385 	.probe		= syscon_probe,
386 	.id_table	= syscon_ids,
387 };
388 
syscon_init(void)389 static int __init syscon_init(void)
390 {
391 	return platform_driver_register(&syscon_driver);
392 }
393 postcore_initcall(syscon_init);
394