2 * System Control Driver
4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
5 * Copyright (C) 2012 Linaro Ltd.
7 * Author: Dong Aisheng <dong.aisheng@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/hwspinlock.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
22 #include <linux/of_address.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_data/syscon.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/slab.h>
30 static struct platform_driver syscon_driver
;
32 static DEFINE_SPINLOCK(syscon_list_slock
);
33 static LIST_HEAD(syscon_list
);
36 struct device_node
*np
;
37 struct regmap
*regmap
;
38 struct list_head list
;
41 static const struct regmap_config syscon_regmap_config
= {
47 static struct syscon
*of_syscon_register(struct device_node
*np
)
50 struct syscon
*syscon
;
51 struct regmap
*regmap
;
55 struct regmap_config syscon_config
= syscon_regmap_config
;
58 if (!of_device_is_compatible(np
, "syscon"))
59 return ERR_PTR(-EINVAL
);
61 syscon
= kzalloc(sizeof(*syscon
), GFP_KERNEL
);
63 return ERR_PTR(-ENOMEM
);
65 if (of_address_to_resource(np
, 0, &res
)) {
70 base
= ioremap(res
.start
, resource_size(&res
));
76 /* Parse the device's DT node for an endianness specification */
77 if (of_property_read_bool(np
, "big-endian"))
78 syscon_config
.val_format_endian
= REGMAP_ENDIAN_BIG
;
79 else if (of_property_read_bool(np
, "little-endian"))
80 syscon_config
.val_format_endian
= REGMAP_ENDIAN_LITTLE
;
81 else if (of_property_read_bool(np
, "native-endian"))
82 syscon_config
.val_format_endian
= REGMAP_ENDIAN_NATIVE
;
85 * search for reg-io-width property in DT. If it is not provided,
86 * default to 4 bytes. regmap_init_mmio will return an error if values
87 * are invalid so there is no need to check them here.
89 ret
= of_property_read_u32(np
, "reg-io-width", ®_io_width
);
93 ret
= of_hwspin_lock_get_id(np
, 0);
94 if (ret
> 0 || (IS_ENABLED(CONFIG_HWSPINLOCK
) && ret
== 0)) {
95 syscon_config
.use_hwlock
= true;
96 syscon_config
.hwlock_id
= ret
;
97 syscon_config
.hwlock_mode
= HWLOCK_IRQSTATE
;
101 /* Ignore missing hwlock, it's optional. */
104 pr_err("Failed to retrieve valid hwlock: %d\n", ret
);
111 syscon_config
.name
= of_node_full_name(np
);
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
;
115 syscon_config
.name
= of_node_full_name(np
);
117 regmap
= regmap_init_mmio(NULL
, base
, &syscon_config
);
118 if (IS_ERR(regmap
)) {
119 pr_err("regmap init failed\n");
120 ret
= PTR_ERR(regmap
);
124 clk
= of_clk_get(np
, 0);
127 /* clock is optional */
131 ret
= regmap_mmio_attach_clk(regmap
, clk
);
136 syscon
->regmap
= regmap
;
139 spin_lock(&syscon_list_slock
);
140 list_add_tail(&syscon
->list
, &syscon_list
);
141 spin_unlock(&syscon_list_slock
);
157 struct regmap
*syscon_node_to_regmap(struct device_node
*np
)
159 struct syscon
*entry
, *syscon
= NULL
;
161 spin_lock(&syscon_list_slock
);
163 list_for_each_entry(entry
, &syscon_list
, list
)
164 if (entry
->np
== np
) {
169 spin_unlock(&syscon_list_slock
);
172 syscon
= of_syscon_register(np
);
175 return ERR_CAST(syscon
);
177 return syscon
->regmap
;
179 EXPORT_SYMBOL_GPL(syscon_node_to_regmap
);
181 struct regmap
*syscon_regmap_lookup_by_compatible(const char *s
)
183 struct device_node
*syscon_np
;
184 struct regmap
*regmap
;
186 syscon_np
= of_find_compatible_node(NULL
, NULL
, s
);
188 return ERR_PTR(-ENODEV
);
190 regmap
= syscon_node_to_regmap(syscon_np
);
191 of_node_put(syscon_np
);
195 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible
);
197 static int syscon_match_pdevname(struct device
*dev
, void *data
)
199 return !strcmp(dev_name(dev
), (const char *)data
);
202 struct regmap
*syscon_regmap_lookup_by_pdevname(const char *s
)
205 struct syscon
*syscon
;
207 dev
= driver_find_device(&syscon_driver
.driver
, NULL
, (void *)s
,
208 syscon_match_pdevname
);
210 return ERR_PTR(-EPROBE_DEFER
);
212 syscon
= dev_get_drvdata(dev
);
214 return syscon
->regmap
;
216 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname
);
218 struct regmap
*syscon_regmap_lookup_by_phandle(struct device_node
*np
,
219 const char *property
)
221 struct device_node
*syscon_np
;
222 struct regmap
*regmap
;
225 syscon_np
= of_parse_phandle(np
, property
, 0);
230 return ERR_PTR(-ENODEV
);
232 regmap
= syscon_node_to_regmap(syscon_np
);
233 of_node_put(syscon_np
);
237 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle
);
239 static int syscon_probe(struct platform_device
*pdev
)
241 struct device
*dev
= &pdev
->dev
;
242 struct syscon_platform_data
*pdata
= dev_get_platdata(dev
);
243 struct syscon
*syscon
;
244 struct regmap_config syscon_config
= syscon_regmap_config
;
245 struct resource
*res
;
248 syscon
= devm_kzalloc(dev
, sizeof(*syscon
), GFP_KERNEL
);
252 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
256 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
260 syscon_config
.max_register
= res
->end
- res
->start
- 3;
262 syscon_config
.name
= pdata
->label
;
263 syscon
->regmap
= devm_regmap_init_mmio(dev
, base
, &syscon_config
);
264 if (IS_ERR(syscon
->regmap
)) {
265 dev_err(dev
, "regmap init failed\n");
266 return PTR_ERR(syscon
->regmap
);
269 platform_set_drvdata(pdev
, syscon
);
271 dev_dbg(dev
, "regmap %pR registered\n", res
);
276 static const struct platform_device_id syscon_ids
[] = {
281 static struct platform_driver syscon_driver
= {
285 .probe
= syscon_probe
,
286 .id_table
= syscon_ids
,
289 static int __init
syscon_init(void)
291 return platform_driver_register(&syscon_driver
);
293 postcore_initcall(syscon_init
);