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/cleanup.h>
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/hwspinlock.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
19 #include <linux/of_address.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_data/syscon.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/reset.h>
25 #include <linux/mfd/syscon.h>
26 #include <linux/slab.h>
28 static struct platform_driver syscon_driver
;
30 static DEFINE_SPINLOCK(syscon_list_slock
);
31 static LIST_HEAD(syscon_list
);
34 struct device_node
*np
;
35 struct regmap
*regmap
;
36 struct reset_control
*reset
;
37 struct list_head list
;
40 static const struct regmap_config syscon_regmap_config
= {
46 static struct syscon
*of_syscon_register(struct device_node
*np
, bool check_res
)
49 struct regmap
*regmap
;
53 struct regmap_config syscon_config
= syscon_regmap_config
;
55 struct reset_control
*reset
;
57 struct syscon
*syscon
__free(kfree
) = kzalloc(sizeof(*syscon
), GFP_KERNEL
);
59 return ERR_PTR(-ENOMEM
);
61 if (of_address_to_resource(np
, 0, &res
))
62 return ERR_PTR(-ENOMEM
);
64 base
= of_iomap(np
, 0);
66 return ERR_PTR(-ENOMEM
);
68 /* Parse the device's DT node for an endianness specification */
69 if (of_property_read_bool(np
, "big-endian"))
70 syscon_config
.val_format_endian
= REGMAP_ENDIAN_BIG
;
71 else if (of_property_read_bool(np
, "little-endian"))
72 syscon_config
.val_format_endian
= REGMAP_ENDIAN_LITTLE
;
73 else if (of_property_read_bool(np
, "native-endian"))
74 syscon_config
.val_format_endian
= REGMAP_ENDIAN_NATIVE
;
77 * search for reg-io-width property in DT. If it is not provided,
78 * default to 4 bytes. regmap_init_mmio will return an error if values
79 * are invalid so there is no need to check them here.
81 ret
= of_property_read_u32(np
, "reg-io-width", ®_io_width
);
85 ret
= of_hwspin_lock_get_id(np
, 0);
86 if (ret
> 0 || (IS_ENABLED(CONFIG_HWSPINLOCK
) && ret
== 0)) {
87 syscon_config
.use_hwlock
= true;
88 syscon_config
.hwlock_id
= ret
;
89 syscon_config
.hwlock_mode
= HWLOCK_IRQSTATE
;
93 /* Ignore missing hwlock, it's optional. */
96 pr_err("Failed to retrieve valid hwlock: %d\n", ret
);
103 syscon_config
.name
= kasprintf(GFP_KERNEL
, "%pOFn@%pa", np
, &res
.start
);
104 if (!syscon_config
.name
) {
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
;
111 if (!syscon_config
.max_register
)
112 syscon_config
.max_register_is_0
= true;
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
);
123 clk
= of_clk_get(np
, 0);
126 /* clock is optional */
130 ret
= regmap_mmio_attach_clk(regmap
, clk
);
135 reset
= of_reset_control_get_optional_exclusive(np
, NULL
);
137 ret
= PTR_ERR(reset
);
141 ret
= reset_control_deassert(reset
);
146 syscon
->regmap
= regmap
;
149 spin_lock(&syscon_list_slock
);
150 list_add_tail(&syscon
->list
, &syscon_list
);
151 spin_unlock(&syscon_list_slock
);
156 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
;
192 * of_syscon_register_regmap() - Register regmap for specified device node
193 * @np: Device tree node
194 * @regmap: Pointer to regmap object
196 * Register an externally created regmap object with syscon for the specified
197 * device tree node. This regmap will then be returned to client drivers using
198 * the syscon_regmap_lookup_by_phandle() API.
200 * Return: 0 on success, negative error code on failure.
202 int of_syscon_register_regmap(struct device_node
*np
, struct regmap
*regmap
)
204 struct syscon
*entry
, *syscon
= NULL
;
210 syscon
= kzalloc(sizeof(*syscon
), GFP_KERNEL
);
214 /* check if syscon entry already exists */
215 spin_lock(&syscon_list_slock
);
217 list_for_each_entry(entry
, &syscon_list
, list
)
218 if (entry
->np
== np
) {
223 syscon
->regmap
= regmap
;
226 /* register the regmap in syscon list */
227 list_add_tail(&syscon
->list
, &syscon_list
);
228 spin_unlock(&syscon_list_slock
);
233 spin_unlock(&syscon_list_slock
);
237 EXPORT_SYMBOL_GPL(of_syscon_register_regmap
);
239 struct regmap
*device_node_to_regmap(struct device_node
*np
)
241 return device_node_get_regmap(np
, false);
243 EXPORT_SYMBOL_GPL(device_node_to_regmap
);
245 struct regmap
*syscon_node_to_regmap(struct device_node
*np
)
247 if (!of_device_is_compatible(np
, "syscon"))
248 return ERR_PTR(-EINVAL
);
250 return device_node_get_regmap(np
, true);
252 EXPORT_SYMBOL_GPL(syscon_node_to_regmap
);
254 struct regmap
*syscon_regmap_lookup_by_compatible(const char *s
)
256 struct device_node
*syscon_np
;
257 struct regmap
*regmap
;
259 syscon_np
= of_find_compatible_node(NULL
, NULL
, s
);
261 return ERR_PTR(-ENODEV
);
263 regmap
= syscon_node_to_regmap(syscon_np
);
264 of_node_put(syscon_np
);
268 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible
);
270 struct regmap
*syscon_regmap_lookup_by_phandle(struct device_node
*np
,
271 const char *property
)
273 struct device_node
*syscon_np
;
274 struct regmap
*regmap
;
277 syscon_np
= of_parse_phandle(np
, property
, 0);
282 return ERR_PTR(-ENODEV
);
284 regmap
= syscon_node_to_regmap(syscon_np
);
287 of_node_put(syscon_np
);
291 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle
);
293 struct regmap
*syscon_regmap_lookup_by_phandle_args(struct device_node
*np
,
294 const char *property
,
296 unsigned int *out_args
)
298 struct device_node
*syscon_np
;
299 struct of_phandle_args args
;
300 struct regmap
*regmap
;
304 rc
= of_parse_phandle_with_fixed_args(np
, property
, arg_count
,
311 return ERR_PTR(-ENODEV
);
313 regmap
= syscon_node_to_regmap(syscon_np
);
314 for (index
= 0; index
< arg_count
; index
++)
315 out_args
[index
] = args
.args
[index
];
316 of_node_put(syscon_np
);
320 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args
);
323 * It behaves the same as syscon_regmap_lookup_by_phandle() except where
324 * there is no regmap phandle. In this case, instead of returning -ENODEV,
325 * the function returns NULL.
327 struct regmap
*syscon_regmap_lookup_by_phandle_optional(struct device_node
*np
,
328 const char *property
)
330 struct regmap
*regmap
;
332 regmap
= syscon_regmap_lookup_by_phandle(np
, property
);
333 if (IS_ERR(regmap
) && PTR_ERR(regmap
) == -ENODEV
)
338 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional
);
340 static int syscon_probe(struct platform_device
*pdev
)
342 struct device
*dev
= &pdev
->dev
;
343 struct syscon_platform_data
*pdata
= dev_get_platdata(dev
);
344 struct syscon
*syscon
;
345 struct regmap_config syscon_config
= syscon_regmap_config
;
346 struct resource
*res
;
349 syscon
= devm_kzalloc(dev
, sizeof(*syscon
), GFP_KERNEL
);
353 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
357 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
361 syscon_config
.max_register
= resource_size(res
) - 4;
362 if (!syscon_config
.max_register
)
363 syscon_config
.max_register_is_0
= true;
366 syscon_config
.name
= pdata
->label
;
367 syscon
->regmap
= devm_regmap_init_mmio(dev
, base
, &syscon_config
);
368 if (IS_ERR(syscon
->regmap
)) {
369 dev_err(dev
, "regmap init failed\n");
370 return PTR_ERR(syscon
->regmap
);
373 platform_set_drvdata(pdev
, syscon
);
375 dev_dbg(dev
, "regmap %pR registered\n", res
);
380 static const struct platform_device_id syscon_ids
[] = {
385 static struct platform_driver syscon_driver
= {
389 .probe
= syscon_probe
,
390 .id_table
= syscon_ids
,
393 static int __init
syscon_init(void)
395 return platform_driver_register(&syscon_driver
);
397 postcore_initcall(syscon_init
);