blob: f6922a0f8058af3f563a4a720e7cfec2d98f55af [file] [log] [blame]
Dong Aisheng87d68732012-09-05 10:57:13 +08001/*
2 * System Control Driver
3 *
4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
5 * Copyright (C) 2012 Linaro Ltd.
6 *
7 * Author: Dong Aisheng <dong.aisheng@linaro.org>
8 *
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.
13 */
14
Fabrice Gasniera00406b2018-12-12 09:48:14 +010015#include <linux/clk.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080016#include <linux/err.h>
Baolin Wang3bafc092017-12-25 14:37:10 +080017#include <linux/hwspinlock.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080018#include <linux/io.h>
Paul Gortmaker1345da72019-01-13 13:36:40 -050019#include <linux/init.h>
Pankaj Dubeybdb00662014-09-30 14:05:27 +053020#include <linux/list.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080021#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/of_platform.h>
Pawel Moll29f9b6c2014-02-12 10:47:10 +000024#include <linux/platform_data/syscon.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080025#include <linux/platform_device.h>
26#include <linux/regmap.h>
Fabio Estevam75177de2013-02-11 18:48:00 -020027#include <linux/mfd/syscon.h>
Pankaj Dubeybdb00662014-09-30 14:05:27 +053028#include <linux/slab.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080029
30static struct platform_driver syscon_driver;
31
Pankaj Dubeybdb00662014-09-30 14:05:27 +053032static DEFINE_SPINLOCK(syscon_list_slock);
33static LIST_HEAD(syscon_list);
34
Dong Aisheng87d68732012-09-05 10:57:13 +080035struct syscon {
Pankaj Dubeybdb00662014-09-30 14:05:27 +053036 struct device_node *np;
Dong Aisheng87d68732012-09-05 10:57:13 +080037 struct regmap *regmap;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053038 struct list_head list;
Dong Aisheng87d68732012-09-05 10:57:13 +080039};
40
Philipp Zabelc1310452016-01-29 10:30:16 +010041static const struct regmap_config syscon_regmap_config = {
Pankaj Dubeybdb00662014-09-30 14:05:27 +053042 .reg_bits = 32,
43 .val_bits = 32,
44 .reg_stride = 4,
45};
Dong Aisheng87d68732012-09-05 10:57:13 +080046
Pankaj Dubeybdb00662014-09-30 14:05:27 +053047static struct syscon *of_syscon_register(struct device_node *np)
48{
Fabrice Gasniera00406b2018-12-12 09:48:14 +010049 struct clk *clk;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053050 struct syscon *syscon;
51 struct regmap *regmap;
52 void __iomem *base;
Damien Riegeldb2fb602015-11-30 10:59:47 -050053 u32 reg_io_width;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053054 int ret;
55 struct regmap_config syscon_config = syscon_regmap_config;
Philipp Zabelca668f02016-01-29 10:35:51 +010056 struct resource res;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053057
58 if (!of_device_is_compatible(np, "syscon"))
59 return ERR_PTR(-EINVAL);
60
61 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
62 if (!syscon)
63 return ERR_PTR(-ENOMEM);
64
Philipp Zabelca668f02016-01-29 10:35:51 +010065 if (of_address_to_resource(np, 0, &res)) {
66 ret = -ENOMEM;
67 goto err_map;
68 }
69
70 base = ioremap(res.start, resource_size(&res));
Pankaj Dubeybdb00662014-09-30 14:05:27 +053071 if (!base) {
72 ret = -ENOMEM;
73 goto err_map;
74 }
75
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;
Paul Burtond29ccdb2016-10-14 10:17:31 +010079 else if (of_property_read_bool(np, "little-endian"))
Pankaj Dubeybdb00662014-09-30 14:05:27 +053080 syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
Paul Burtond29ccdb2016-10-14 10:17:31 +010081 else if (of_property_read_bool(np, "native-endian"))
82 syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053083
Damien Riegeldb2fb602015-11-30 10:59:47 -050084 /*
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.
88 */
89 ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
90 if (ret)
91 reg_io_width = 4;
92
Baolin Wang3bafc092017-12-25 14:37:10 +080093 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;
98 } else if (ret < 0) {
99 switch (ret) {
100 case -ENOENT:
101 /* Ignore missing hwlock, it's optional. */
102 break;
103 default:
104 pr_err("Failed to retrieve valid hwlock: %d\n", ret);
105 /* fall-through */
106 case -EPROBE_DEFER:
107 goto err_regmap;
108 }
109 }
110
David Lechner408d1d52018-01-23 16:57:34 -0600111 syscon_config.name = of_node_full_name(np);
Damien Riegeldb2fb602015-11-30 10:59:47 -0500112 syscon_config.reg_stride = reg_io_width;
113 syscon_config.val_bits = reg_io_width * 8;
Philipp Zabelca668f02016-01-29 10:35:51 +0100114 syscon_config.max_register = resource_size(&res) - reg_io_width;
Jeffy Chen500f9ff2018-03-12 17:51:39 +0800115 syscon_config.name = of_node_full_name(np);
Damien Riegeldb2fb602015-11-30 10:59:47 -0500116
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530117 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);
121 goto err_regmap;
122 }
123
Fabrice Gasniera00406b2018-12-12 09:48:14 +0100124 clk = of_clk_get(np, 0);
125 if (IS_ERR(clk)) {
126 ret = PTR_ERR(clk);
127 /* clock is optional */
128 if (ret != -ENOENT)
129 goto err_clk;
130 } else {
131 ret = regmap_mmio_attach_clk(regmap, clk);
132 if (ret)
133 goto err_attach;
134 }
135
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530136 syscon->regmap = regmap;
137 syscon->np = np;
138
139 spin_lock(&syscon_list_slock);
140 list_add_tail(&syscon->list, &syscon_list);
141 spin_unlock(&syscon_list_slock);
142
143 return syscon;
144
Fabrice Gasniera00406b2018-12-12 09:48:14 +0100145err_attach:
146 if (!IS_ERR(clk))
147 clk_put(clk);
148err_clk:
149 regmap_exit(regmap);
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530150err_regmap:
151 iounmap(base);
152err_map:
153 kfree(syscon);
154 return ERR_PTR(ret);
Dong Aisheng87d68732012-09-05 10:57:13 +0800155}
156
157struct regmap *syscon_node_to_regmap(struct device_node *np)
158{
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530159 struct syscon *entry, *syscon = NULL;
Dong Aisheng87d68732012-09-05 10:57:13 +0800160
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530161 spin_lock(&syscon_list_slock);
Dong Aisheng87d68732012-09-05 10:57:13 +0800162
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530163 list_for_each_entry(entry, &syscon_list, list)
164 if (entry->np == np) {
165 syscon = entry;
166 break;
167 }
168
169 spin_unlock(&syscon_list_slock);
170
171 if (!syscon)
172 syscon = of_syscon_register(np);
173
174 if (IS_ERR(syscon))
175 return ERR_CAST(syscon);
Dong Aisheng87d68732012-09-05 10:57:13 +0800176
177 return syscon->regmap;
178}
179EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
180
181struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
182{
183 struct device_node *syscon_np;
184 struct regmap *regmap;
185
186 syscon_np = of_find_compatible_node(NULL, NULL, s);
187 if (!syscon_np)
188 return ERR_PTR(-ENODEV);
189
190 regmap = syscon_node_to_regmap(syscon_np);
191 of_node_put(syscon_np);
192
193 return regmap;
194}
195EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
196
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400197static int syscon_match_pdevname(struct device *dev, void *data)
198{
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400199 return !strcmp(dev_name(dev), (const char *)data);
200}
201
202struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
203{
204 struct device *dev;
205 struct syscon *syscon;
206
207 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
208 syscon_match_pdevname);
209 if (!dev)
210 return ERR_PTR(-EPROBE_DEFER);
211
212 syscon = dev_get_drvdata(dev);
213
214 return syscon->regmap;
215}
216EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
217
Dong Aisheng87d68732012-09-05 10:57:13 +0800218struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
219 const char *property)
220{
221 struct device_node *syscon_np;
222 struct regmap *regmap;
223
Pankaj Dubey45330bb2014-04-30 11:14:26 +0900224 if (property)
225 syscon_np = of_parse_phandle(np, property, 0);
226 else
227 syscon_np = np;
228
Dong Aisheng87d68732012-09-05 10:57:13 +0800229 if (!syscon_np)
230 return ERR_PTR(-ENODEV);
231
232 regmap = syscon_node_to_regmap(syscon_np);
233 of_node_put(syscon_np);
234
235 return regmap;
236}
237EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
238
Bill Pembertonf791be42012-11-19 13:23:04 -0500239static int syscon_probe(struct platform_device *pdev)
Dong Aisheng87d68732012-09-05 10:57:13 +0800240{
241 struct device *dev = &pdev->dev;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000242 struct syscon_platform_data *pdata = dev_get_platdata(dev);
Dong Aisheng87d68732012-09-05 10:57:13 +0800243 struct syscon *syscon;
Philipp Zabelc1310452016-01-29 10:30:16 +0100244 struct regmap_config syscon_config = syscon_regmap_config;
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400245 struct resource *res;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400246 void __iomem *base;
Dong Aisheng87d68732012-09-05 10:57:13 +0800247
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400248 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
Dong Aisheng87d68732012-09-05 10:57:13 +0800249 if (!syscon)
250 return -ENOMEM;
251
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
253 if (!res)
254 return -ENOENT;
255
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400256 base = devm_ioremap(dev, res->start, resource_size(res));
257 if (!base)
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400258 return -ENOMEM;
Dong Aisheng87d68732012-09-05 10:57:13 +0800259
Philipp Zabelc1310452016-01-29 10:30:16 +0100260 syscon_config.max_register = res->end - res->start - 3;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000261 if (pdata)
Philipp Zabelc1310452016-01-29 10:30:16 +0100262 syscon_config.name = pdata->label;
263 syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
Dong Aisheng87d68732012-09-05 10:57:13 +0800264 if (IS_ERR(syscon->regmap)) {
265 dev_err(dev, "regmap init failed\n");
266 return PTR_ERR(syscon->regmap);
267 }
268
Dong Aisheng87d68732012-09-05 10:57:13 +0800269 platform_set_drvdata(pdev, syscon);
270
Alexander Shiyan38d89742014-02-22 10:02:25 +0400271 dev_dbg(dev, "regmap %pR registered\n", res);
Dong Aisheng87d68732012-09-05 10:57:13 +0800272
273 return 0;
274}
275
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400276static const struct platform_device_id syscon_ids[] = {
277 { "syscon", },
278 { }
279};
Dong Aisheng87d68732012-09-05 10:57:13 +0800280
281static struct platform_driver syscon_driver = {
282 .driver = {
283 .name = "syscon",
Dong Aisheng87d68732012-09-05 10:57:13 +0800284 },
285 .probe = syscon_probe,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400286 .id_table = syscon_ids,
Dong Aisheng87d68732012-09-05 10:57:13 +0800287};
288
289static int __init syscon_init(void)
290{
291 return platform_driver_register(&syscon_driver);
292}
293postcore_initcall(syscon_init);