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