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