1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * System Control Driver
5 * Copyright (C) 2012 Freescale Semiconductor, Inc.
6 * Copyright (C) 2012 Linaro Ltd.
8 * Author: Dong Aisheng <dong.aisheng@linaro.org>
11 #include <linux/clk.h>
12 #include <linux/err.h>
13 #include <linux/hwspinlock.h>
15 #include <linux/init.h>
16 #include <linux/list.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/reset.h>
24 #include <linux/mfd/syscon.h>
25 #include <linux/slab.h>
27 static struct platform_driver syscon_driver;
29 static DEFINE_SPINLOCK(syscon_list_slock);
30 static LIST_HEAD(syscon_list);
33 struct device_node *np;
34 struct regmap *regmap;
35 struct reset_control *reset;
36 struct list_head list;
39 static const struct regmap_config syscon_regmap_config = {
45 static struct syscon *of_syscon_register(struct device_node *np, bool check_res)
48 struct syscon *syscon;
49 struct regmap *regmap;
53 struct regmap_config syscon_config = syscon_regmap_config;
55 struct reset_control *reset;
57 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
59 return ERR_PTR(-ENOMEM);
61 if (of_address_to_resource(np, 0, &res)) {
66 base = of_iomap(np, 0);
72 /* Parse the device's DT node for an endianness specification */
73 if (of_property_read_bool(np, "big-endian"))
74 syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
75 else if (of_property_read_bool(np, "little-endian"))
76 syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
77 else if (of_property_read_bool(np, "native-endian"))
78 syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
81 * search for reg-io-width property in DT. If it is not provided,
82 * default to 4 bytes. regmap_init_mmio will return an error if values
83 * are invalid so there is no need to check them here.
85 ret = of_property_read_u32(np, "reg-io-width", ®_io_width);
89 ret = of_hwspin_lock_get_id(np, 0);
90 if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
91 syscon_config.use_hwlock = true;
92 syscon_config.hwlock_id = ret;
93 syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
97 /* Ignore missing hwlock, it's optional. */
100 pr_err("Failed to retrieve valid hwlock: %d\n", ret);
107 syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &res.start);
108 if (!syscon_config.name) {
112 syscon_config.reg_stride = reg_io_width;
113 syscon_config.val_bits = reg_io_width * 8;
114 syscon_config.max_register = resource_size(&res) - reg_io_width;
116 regmap = regmap_init_mmio(NULL, base, &syscon_config);
117 kfree(syscon_config.name);
118 if (IS_ERR(regmap)) {
119 pr_err("regmap init failed\n");
120 ret = PTR_ERR(regmap);
125 clk = of_clk_get(np, 0);
128 /* clock is optional */
132 ret = regmap_mmio_attach_clk(regmap, clk);
137 reset = of_reset_control_get_optional_exclusive(np, NULL);
139 ret = PTR_ERR(reset);
143 ret = reset_control_deassert(reset);
148 syscon->regmap = regmap;
151 spin_lock(&syscon_list_slock);
152 list_add_tail(&syscon->list, &syscon_list);
153 spin_unlock(&syscon_list_slock);
158 reset_control_put(reset);
171 static struct regmap *device_node_get_regmap(struct device_node *np,
174 struct syscon *entry, *syscon = NULL;
176 spin_lock(&syscon_list_slock);
178 list_for_each_entry(entry, &syscon_list, list)
179 if (entry->np == np) {
184 spin_unlock(&syscon_list_slock);
187 syscon = of_syscon_register(np, check_res);
190 return ERR_CAST(syscon);
192 return syscon->regmap;
195 struct regmap *device_node_to_regmap(struct device_node *np)
197 return device_node_get_regmap(np, false);
199 EXPORT_SYMBOL_GPL(device_node_to_regmap);
201 struct regmap *syscon_node_to_regmap(struct device_node *np)
203 if (!of_device_is_compatible(np, "syscon"))
204 return ERR_PTR(-EINVAL);
206 return device_node_get_regmap(np, true);
208 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
210 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
212 struct device_node *syscon_np;
213 struct regmap *regmap;
215 syscon_np = of_find_compatible_node(NULL, NULL, s);
217 return ERR_PTR(-ENODEV);
219 regmap = syscon_node_to_regmap(syscon_np);
220 of_node_put(syscon_np);
224 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
226 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
227 const char *property)
229 struct device_node *syscon_np;
230 struct regmap *regmap;
233 syscon_np = of_parse_phandle(np, property, 0);
238 return ERR_PTR(-ENODEV);
240 regmap = syscon_node_to_regmap(syscon_np);
241 of_node_put(syscon_np);
245 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
247 struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
248 const char *property,
250 unsigned int *out_args)
252 struct device_node *syscon_np;
253 struct of_phandle_args args;
254 struct regmap *regmap;
258 rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
265 return ERR_PTR(-ENODEV);
267 regmap = syscon_node_to_regmap(syscon_np);
268 for (index = 0; index < arg_count; index++)
269 out_args[index] = args.args[index];
270 of_node_put(syscon_np);
274 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
277 * It behaves the same as syscon_regmap_lookup_by_phandle() except where
278 * there is no regmap phandle. In this case, instead of returning -ENODEV,
279 * the function returns NULL.
281 struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np,
282 const char *property)
284 struct regmap *regmap;
286 regmap = syscon_regmap_lookup_by_phandle(np, property);
287 if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
292 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional);
294 static int syscon_probe(struct platform_device *pdev)
296 struct device *dev = &pdev->dev;
297 struct syscon_platform_data *pdata = dev_get_platdata(dev);
298 struct syscon *syscon;
299 struct regmap_config syscon_config = syscon_regmap_config;
300 struct resource *res;
303 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
307 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
311 base = devm_ioremap(dev, res->start, resource_size(res));
315 syscon_config.max_register = resource_size(res) - 4;
317 syscon_config.name = pdata->label;
318 syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
319 if (IS_ERR(syscon->regmap)) {
320 dev_err(dev, "regmap init failed\n");
321 return PTR_ERR(syscon->regmap);
324 platform_set_drvdata(pdev, syscon);
326 dev_dbg(dev, "regmap %pR registered\n", res);
331 static const struct platform_device_id syscon_ids[] = {
336 static struct platform_driver syscon_driver = {
340 .probe = syscon_probe,
341 .id_table = syscon_ids,
344 static int __init syscon_init(void)
346 return platform_driver_register(&syscon_driver);
348 postcore_initcall(syscon_init);