blob: 0ecdffb3d96728ba89fb7dc34f57483364784fe7 [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
15#include <linux/err.h>
Baolin Wang3bafc092017-12-25 14:37:10 +080016#include <linux/hwspinlock.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080017#include <linux/io.h>
Paul Gortmaker1345da72019-01-13 13:36:40 -050018#include <linux/init.h>
Pankaj Dubeybdb00662014-09-30 14:05:27 +053019#include <linux/list.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080020#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_platform.h>
Pawel Moll29f9b6c2014-02-12 10:47:10 +000023#include <linux/platform_data/syscon.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080024#include <linux/platform_device.h>
25#include <linux/regmap.h>
Fabio Estevam75177de2013-02-11 18:48:00 -020026#include <linux/mfd/syscon.h>
Pankaj Dubeybdb00662014-09-30 14:05:27 +053027#include <linux/slab.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080028
29static struct platform_driver syscon_driver;
30
Pankaj Dubeybdb00662014-09-30 14:05:27 +053031static DEFINE_SPINLOCK(syscon_list_slock);
32static LIST_HEAD(syscon_list);
33
Dong Aisheng87d68732012-09-05 10:57:13 +080034struct syscon {
Pankaj Dubeybdb00662014-09-30 14:05:27 +053035 struct device_node *np;
Dong Aisheng87d68732012-09-05 10:57:13 +080036 struct regmap *regmap;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053037 struct list_head list;
Dong Aisheng87d68732012-09-05 10:57:13 +080038};
39
Philipp Zabelc1310452016-01-29 10:30:16 +010040static const struct regmap_config syscon_regmap_config = {
Pankaj Dubeybdb00662014-09-30 14:05:27 +053041 .reg_bits = 32,
42 .val_bits = 32,
43 .reg_stride = 4,
44};
Dong Aisheng87d68732012-09-05 10:57:13 +080045
Pankaj Dubeybdb00662014-09-30 14:05:27 +053046static struct syscon *of_syscon_register(struct device_node *np)
47{
48 struct syscon *syscon;
49 struct regmap *regmap;
50 void __iomem *base;
Damien Riegeldb2fb602015-11-30 10:59:47 -050051 u32 reg_io_width;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053052 int ret;
53 struct regmap_config syscon_config = syscon_regmap_config;
Philipp Zabelca668f02016-01-29 10:35:51 +010054 struct resource res;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053055
56 if (!of_device_is_compatible(np, "syscon"))
57 return ERR_PTR(-EINVAL);
58
59 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
60 if (!syscon)
61 return ERR_PTR(-ENOMEM);
62
Philipp Zabelca668f02016-01-29 10:35:51 +010063 if (of_address_to_resource(np, 0, &res)) {
64 ret = -ENOMEM;
65 goto err_map;
66 }
67
68 base = ioremap(res.start, resource_size(&res));
Pankaj Dubeybdb00662014-09-30 14:05:27 +053069 if (!base) {
70 ret = -ENOMEM;
71 goto err_map;
72 }
73
74 /* Parse the device's DT node for an endianness specification */
75 if (of_property_read_bool(np, "big-endian"))
76 syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
Paul Burtond29ccdb2016-10-14 10:17:31 +010077 else if (of_property_read_bool(np, "little-endian"))
Pankaj Dubeybdb00662014-09-30 14:05:27 +053078 syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
Paul Burtond29ccdb2016-10-14 10:17:31 +010079 else if (of_property_read_bool(np, "native-endian"))
80 syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053081
Damien Riegeldb2fb602015-11-30 10:59:47 -050082 /*
83 * search for reg-io-width property in DT. If it is not provided,
84 * default to 4 bytes. regmap_init_mmio will return an error if values
85 * are invalid so there is no need to check them here.
86 */
87 ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
88 if (ret)
89 reg_io_width = 4;
90
Baolin Wang3bafc092017-12-25 14:37:10 +080091 ret = of_hwspin_lock_get_id(np, 0);
92 if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
93 syscon_config.use_hwlock = true;
94 syscon_config.hwlock_id = ret;
95 syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
96 } else if (ret < 0) {
97 switch (ret) {
98 case -ENOENT:
99 /* Ignore missing hwlock, it's optional. */
100 break;
101 default:
102 pr_err("Failed to retrieve valid hwlock: %d\n", ret);
103 /* fall-through */
104 case -EPROBE_DEFER:
105 goto err_regmap;
106 }
107 }
108
David Lechner408d1d52018-01-23 16:57:34 -0600109 syscon_config.name = of_node_full_name(np);
Damien Riegeldb2fb602015-11-30 10:59:47 -0500110 syscon_config.reg_stride = reg_io_width;
111 syscon_config.val_bits = reg_io_width * 8;
Philipp Zabelca668f02016-01-29 10:35:51 +0100112 syscon_config.max_register = resource_size(&res) - reg_io_width;
Jeffy Chen500f9ff2018-03-12 17:51:39 +0800113 syscon_config.name = of_node_full_name(np);
Damien Riegeldb2fb602015-11-30 10:59:47 -0500114
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530115 regmap = regmap_init_mmio(NULL, base, &syscon_config);
116 if (IS_ERR(regmap)) {
117 pr_err("regmap init failed\n");
118 ret = PTR_ERR(regmap);
119 goto err_regmap;
120 }
121
122 syscon->regmap = regmap;
123 syscon->np = np;
124
125 spin_lock(&syscon_list_slock);
126 list_add_tail(&syscon->list, &syscon_list);
127 spin_unlock(&syscon_list_slock);
128
129 return syscon;
130
131err_regmap:
132 iounmap(base);
133err_map:
134 kfree(syscon);
135 return ERR_PTR(ret);
Dong Aisheng87d68732012-09-05 10:57:13 +0800136}
137
138struct regmap *syscon_node_to_regmap(struct device_node *np)
139{
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530140 struct syscon *entry, *syscon = NULL;
Dong Aisheng87d68732012-09-05 10:57:13 +0800141
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530142 spin_lock(&syscon_list_slock);
Dong Aisheng87d68732012-09-05 10:57:13 +0800143
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530144 list_for_each_entry(entry, &syscon_list, list)
145 if (entry->np == np) {
146 syscon = entry;
147 break;
148 }
149
150 spin_unlock(&syscon_list_slock);
151
152 if (!syscon)
153 syscon = of_syscon_register(np);
154
155 if (IS_ERR(syscon))
156 return ERR_CAST(syscon);
Dong Aisheng87d68732012-09-05 10:57:13 +0800157
158 return syscon->regmap;
159}
160EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
161
162struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
163{
164 struct device_node *syscon_np;
165 struct regmap *regmap;
166
167 syscon_np = of_find_compatible_node(NULL, NULL, s);
168 if (!syscon_np)
169 return ERR_PTR(-ENODEV);
170
171 regmap = syscon_node_to_regmap(syscon_np);
172 of_node_put(syscon_np);
173
174 return regmap;
175}
176EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
177
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400178static int syscon_match_pdevname(struct device *dev, void *data)
179{
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400180 return !strcmp(dev_name(dev), (const char *)data);
181}
182
183struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
184{
185 struct device *dev;
186 struct syscon *syscon;
187
188 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
189 syscon_match_pdevname);
190 if (!dev)
191 return ERR_PTR(-EPROBE_DEFER);
192
193 syscon = dev_get_drvdata(dev);
194
195 return syscon->regmap;
196}
197EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
198
Dong Aisheng87d68732012-09-05 10:57:13 +0800199struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
200 const char *property)
201{
202 struct device_node *syscon_np;
203 struct regmap *regmap;
204
Pankaj Dubey45330bb2014-04-30 11:14:26 +0900205 if (property)
206 syscon_np = of_parse_phandle(np, property, 0);
207 else
208 syscon_np = np;
209
Dong Aisheng87d68732012-09-05 10:57:13 +0800210 if (!syscon_np)
211 return ERR_PTR(-ENODEV);
212
213 regmap = syscon_node_to_regmap(syscon_np);
214 of_node_put(syscon_np);
215
216 return regmap;
217}
218EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
219
Bill Pembertonf791be42012-11-19 13:23:04 -0500220static int syscon_probe(struct platform_device *pdev)
Dong Aisheng87d68732012-09-05 10:57:13 +0800221{
222 struct device *dev = &pdev->dev;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000223 struct syscon_platform_data *pdata = dev_get_platdata(dev);
Dong Aisheng87d68732012-09-05 10:57:13 +0800224 struct syscon *syscon;
Philipp Zabelc1310452016-01-29 10:30:16 +0100225 struct regmap_config syscon_config = syscon_regmap_config;
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400226 struct resource *res;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400227 void __iomem *base;
Dong Aisheng87d68732012-09-05 10:57:13 +0800228
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400229 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
Dong Aisheng87d68732012-09-05 10:57:13 +0800230 if (!syscon)
231 return -ENOMEM;
232
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400233 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
234 if (!res)
235 return -ENOENT;
236
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400237 base = devm_ioremap(dev, res->start, resource_size(res));
238 if (!base)
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400239 return -ENOMEM;
Dong Aisheng87d68732012-09-05 10:57:13 +0800240
Philipp Zabelc1310452016-01-29 10:30:16 +0100241 syscon_config.max_register = res->end - res->start - 3;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000242 if (pdata)
Philipp Zabelc1310452016-01-29 10:30:16 +0100243 syscon_config.name = pdata->label;
244 syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
Dong Aisheng87d68732012-09-05 10:57:13 +0800245 if (IS_ERR(syscon->regmap)) {
246 dev_err(dev, "regmap init failed\n");
247 return PTR_ERR(syscon->regmap);
248 }
249
Dong Aisheng87d68732012-09-05 10:57:13 +0800250 platform_set_drvdata(pdev, syscon);
251
Alexander Shiyan38d89742014-02-22 10:02:25 +0400252 dev_dbg(dev, "regmap %pR registered\n", res);
Dong Aisheng87d68732012-09-05 10:57:13 +0800253
254 return 0;
255}
256
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400257static const struct platform_device_id syscon_ids[] = {
258 { "syscon", },
259 { }
260};
Dong Aisheng87d68732012-09-05 10:57:13 +0800261
262static struct platform_driver syscon_driver = {
263 .driver = {
264 .name = "syscon",
Dong Aisheng87d68732012-09-05 10:57:13 +0800265 },
266 .probe = syscon_probe,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400267 .id_table = syscon_ids,
Dong Aisheng87d68732012-09-05 10:57:13 +0800268};
269
270static int __init syscon_init(void)
271{
272 return platform_driver_register(&syscon_driver);
273}
274postcore_initcall(syscon_init);