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 syscon_config.reg_stride = reg_io_width;
109 syscon_config.val_bits = reg_io_width * 8;
110 syscon_config.max_register = resource_size(&res) - reg_io_width;
112 regmap = regmap_init_mmio(NULL, base, &syscon_config);
113 kfree(syscon_config.name);
114 if (IS_ERR(regmap)) {
115 pr_err("regmap init failed\n");
116 ret = PTR_ERR(regmap);
121 clk = of_clk_get(np, 0);
124 /* clock is optional */
128 ret = regmap_mmio_attach_clk(regmap, clk);
133 reset = of_reset_control_get_optional_exclusive(np, NULL);
135 ret = PTR_ERR(reset);
139 ret = reset_control_deassert(reset);
144 syscon->regmap = regmap;
147 spin_lock(&syscon_list_slock);
148 list_add_tail(&syscon->list, &syscon_list);
149 spin_unlock(&syscon_list_slock);
154 reset_control_put(reset);
167 static struct regmap *device_node_get_regmap(struct device_node *np,
170 struct syscon *entry, *syscon = NULL;
172 spin_lock(&syscon_list_slock);
174 list_for_each_entry(entry, &syscon_list, list)
175 if (entry->np == np) {
180 spin_unlock(&syscon_list_slock);
183 syscon = of_syscon_register(np, check_res);
186 return ERR_CAST(syscon);
188 return syscon->regmap;
191 struct regmap *device_node_to_regmap(struct device_node *np)
193 return device_node_get_regmap(np, false);
195 EXPORT_SYMBOL_GPL(device_node_to_regmap);
197 struct regmap *syscon_node_to_regmap(struct device_node *np)
199 if (!of_device_is_compatible(np, "syscon"))
200 return ERR_PTR(-EINVAL);
202 return device_node_get_regmap(np, true);
204 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
206 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
208 struct device_node *syscon_np;
209 struct regmap *regmap;
211 syscon_np = of_find_compatible_node(NULL, NULL, s);
213 return ERR_PTR(-ENODEV);
215 regmap = syscon_node_to_regmap(syscon_np);
216 of_node_put(syscon_np);
220 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
222 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
223 const char *property)
225 struct device_node *syscon_np;
226 struct regmap *regmap;
229 syscon_np = of_parse_phandle(np, property, 0);
234 return ERR_PTR(-ENODEV);
236 regmap = syscon_node_to_regmap(syscon_np);
237 of_node_put(syscon_np);
241 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
243 struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
244 const char *property,
246 unsigned int *out_args)
248 struct device_node *syscon_np;
249 struct of_phandle_args args;
250 struct regmap *regmap;
254 rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
261 return ERR_PTR(-ENODEV);
263 regmap = syscon_node_to_regmap(syscon_np);
264 for (index = 0; index < arg_count; index++)
265 out_args[index] = args.args[index];
266 of_node_put(syscon_np);
270 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
273 * It behaves the same as syscon_regmap_lookup_by_phandle() except where
274 * there is no regmap phandle. In this case, instead of returning -ENODEV,
275 * the function returns NULL.
277 struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np,
278 const char *property)
280 struct regmap *regmap;
282 regmap = syscon_regmap_lookup_by_phandle(np, property);
283 if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
288 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional);
290 static int syscon_probe(struct platform_device *pdev)
292 struct device *dev = &pdev->dev;
293 struct syscon_platform_data *pdata = dev_get_platdata(dev);
294 struct syscon *syscon;
295 struct regmap_config syscon_config = syscon_regmap_config;
296 struct resource *res;
299 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
303 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
307 base = devm_ioremap(dev, res->start, resource_size(res));
311 syscon_config.max_register = resource_size(res) - 4;
313 syscon_config.name = pdata->label;
314 syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
315 if (IS_ERR(syscon->regmap)) {
316 dev_err(dev, "regmap init failed\n");
317 return PTR_ERR(syscon->regmap);
320 platform_set_drvdata(pdev, syscon);
322 dev_dbg(dev, "regmap %pR registered\n", res);
327 static const struct platform_device_id syscon_ids[] = {
332 static struct platform_driver syscon_driver = {
336 .probe = syscon_probe,
337 .id_table = syscon_ids,
340 static int __init syscon_init(void)
342 return platform_driver_register(&syscon_driver);
344 postcore_initcall(syscon_init);