blob: b7aabeefab07a2b44ab96b3e371de3d731c0b9cf [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>
16#include <linux/io.h>
17#include <linux/module.h>
Pankaj Dubeybdb00662014-09-30 14:05:27 +053018#include <linux/list.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080019#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_platform.h>
Pawel Moll29f9b6c2014-02-12 10:47:10 +000022#include <linux/platform_data/syscon.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080023#include <linux/platform_device.h>
24#include <linux/regmap.h>
Fabio Estevam75177de2013-02-11 18:48:00 -020025#include <linux/mfd/syscon.h>
Pankaj Dubeybdb00662014-09-30 14:05:27 +053026#include <linux/slab.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080027
28static struct platform_driver syscon_driver;
29
Pankaj Dubeybdb00662014-09-30 14:05:27 +053030static DEFINE_SPINLOCK(syscon_list_slock);
31static LIST_HEAD(syscon_list);
32
Dong Aisheng87d68732012-09-05 10:57:13 +080033struct syscon {
Pankaj Dubeybdb00662014-09-30 14:05:27 +053034 struct device_node *np;
Dong Aisheng87d68732012-09-05 10:57:13 +080035 struct regmap *regmap;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053036 struct list_head list;
Dong Aisheng87d68732012-09-05 10:57:13 +080037};
38
Pankaj Dubeybdb00662014-09-30 14:05:27 +053039static struct regmap_config syscon_regmap_config = {
40 .reg_bits = 32,
41 .val_bits = 32,
42 .reg_stride = 4,
43};
Dong Aisheng87d68732012-09-05 10:57:13 +080044
Pankaj Dubeybdb00662014-09-30 14:05:27 +053045static struct syscon *of_syscon_register(struct device_node *np)
46{
47 struct syscon *syscon;
48 struct regmap *regmap;
49 void __iomem *base;
Damien Riegeldb2fb602015-11-30 10:59:47 -050050 u32 reg_io_width;
Pankaj Dubeybdb00662014-09-30 14:05:27 +053051 int ret;
52 struct regmap_config syscon_config = syscon_regmap_config;
53
54 if (!of_device_is_compatible(np, "syscon"))
55 return ERR_PTR(-EINVAL);
56
57 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
58 if (!syscon)
59 return ERR_PTR(-ENOMEM);
60
61 base = of_iomap(np, 0);
62 if (!base) {
63 ret = -ENOMEM;
64 goto err_map;
65 }
66
67 /* Parse the device's DT node for an endianness specification */
68 if (of_property_read_bool(np, "big-endian"))
69 syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
70 else if (of_property_read_bool(np, "little-endian"))
71 syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
72
Damien Riegeldb2fb602015-11-30 10:59:47 -050073 /*
74 * search for reg-io-width property in DT. If it is not provided,
75 * default to 4 bytes. regmap_init_mmio will return an error if values
76 * are invalid so there is no need to check them here.
77 */
78 ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
79 if (ret)
80 reg_io_width = 4;
81
82 syscon_config.reg_stride = reg_io_width;
83 syscon_config.val_bits = reg_io_width * 8;
84
Pankaj Dubeybdb00662014-09-30 14:05:27 +053085 regmap = regmap_init_mmio(NULL, base, &syscon_config);
86 if (IS_ERR(regmap)) {
87 pr_err("regmap init failed\n");
88 ret = PTR_ERR(regmap);
89 goto err_regmap;
90 }
91
92 syscon->regmap = regmap;
93 syscon->np = np;
94
95 spin_lock(&syscon_list_slock);
96 list_add_tail(&syscon->list, &syscon_list);
97 spin_unlock(&syscon_list_slock);
98
99 return syscon;
100
101err_regmap:
102 iounmap(base);
103err_map:
104 kfree(syscon);
105 return ERR_PTR(ret);
Dong Aisheng87d68732012-09-05 10:57:13 +0800106}
107
108struct regmap *syscon_node_to_regmap(struct device_node *np)
109{
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530110 struct syscon *entry, *syscon = NULL;
Dong Aisheng87d68732012-09-05 10:57:13 +0800111
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530112 spin_lock(&syscon_list_slock);
Dong Aisheng87d68732012-09-05 10:57:13 +0800113
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530114 list_for_each_entry(entry, &syscon_list, list)
115 if (entry->np == np) {
116 syscon = entry;
117 break;
118 }
119
120 spin_unlock(&syscon_list_slock);
121
122 if (!syscon)
123 syscon = of_syscon_register(np);
124
125 if (IS_ERR(syscon))
126 return ERR_CAST(syscon);
Dong Aisheng87d68732012-09-05 10:57:13 +0800127
128 return syscon->regmap;
129}
130EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
131
132struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
133{
134 struct device_node *syscon_np;
135 struct regmap *regmap;
136
137 syscon_np = of_find_compatible_node(NULL, NULL, s);
138 if (!syscon_np)
139 return ERR_PTR(-ENODEV);
140
141 regmap = syscon_node_to_regmap(syscon_np);
142 of_node_put(syscon_np);
143
144 return regmap;
145}
146EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
147
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400148static int syscon_match_pdevname(struct device *dev, void *data)
149{
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400150 return !strcmp(dev_name(dev), (const char *)data);
151}
152
153struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
154{
155 struct device *dev;
156 struct syscon *syscon;
157
158 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
159 syscon_match_pdevname);
160 if (!dev)
161 return ERR_PTR(-EPROBE_DEFER);
162
163 syscon = dev_get_drvdata(dev);
164
165 return syscon->regmap;
166}
167EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
168
Dong Aisheng87d68732012-09-05 10:57:13 +0800169struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
170 const char *property)
171{
172 struct device_node *syscon_np;
173 struct regmap *regmap;
174
Pankaj Dubey45330bb2014-04-30 11:14:26 +0900175 if (property)
176 syscon_np = of_parse_phandle(np, property, 0);
177 else
178 syscon_np = np;
179
Dong Aisheng87d68732012-09-05 10:57:13 +0800180 if (!syscon_np)
181 return ERR_PTR(-ENODEV);
182
183 regmap = syscon_node_to_regmap(syscon_np);
184 of_node_put(syscon_np);
185
186 return regmap;
187}
188EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
189
Bill Pembertonf791be42012-11-19 13:23:04 -0500190static int syscon_probe(struct platform_device *pdev)
Dong Aisheng87d68732012-09-05 10:57:13 +0800191{
192 struct device *dev = &pdev->dev;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000193 struct syscon_platform_data *pdata = dev_get_platdata(dev);
Dong Aisheng87d68732012-09-05 10:57:13 +0800194 struct syscon *syscon;
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400195 struct resource *res;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400196 void __iomem *base;
Dong Aisheng87d68732012-09-05 10:57:13 +0800197
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400198 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
Dong Aisheng87d68732012-09-05 10:57:13 +0800199 if (!syscon)
200 return -ENOMEM;
201
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400202 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 if (!res)
204 return -ENOENT;
205
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400206 base = devm_ioremap(dev, res->start, resource_size(res));
207 if (!base)
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400208 return -ENOMEM;
Dong Aisheng87d68732012-09-05 10:57:13 +0800209
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400210 syscon_regmap_config.max_register = res->end - res->start - 3;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000211 if (pdata)
212 syscon_regmap_config.name = pdata->label;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400213 syscon->regmap = devm_regmap_init_mmio(dev, base,
Dong Aisheng87d68732012-09-05 10:57:13 +0800214 &syscon_regmap_config);
215 if (IS_ERR(syscon->regmap)) {
216 dev_err(dev, "regmap init failed\n");
217 return PTR_ERR(syscon->regmap);
218 }
219
Dong Aisheng87d68732012-09-05 10:57:13 +0800220 platform_set_drvdata(pdev, syscon);
221
Alexander Shiyan38d89742014-02-22 10:02:25 +0400222 dev_dbg(dev, "regmap %pR registered\n", res);
Dong Aisheng87d68732012-09-05 10:57:13 +0800223
224 return 0;
225}
226
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400227static const struct platform_device_id syscon_ids[] = {
228 { "syscon", },
229 { }
230};
Dong Aisheng87d68732012-09-05 10:57:13 +0800231
232static struct platform_driver syscon_driver = {
233 .driver = {
234 .name = "syscon",
Dong Aisheng87d68732012-09-05 10:57:13 +0800235 },
236 .probe = syscon_probe,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400237 .id_table = syscon_ids,
Dong Aisheng87d68732012-09-05 10:57:13 +0800238};
239
240static int __init syscon_init(void)
241{
242 return platform_driver_register(&syscon_driver);
243}
244postcore_initcall(syscon_init);
245
246static void __exit syscon_exit(void)
247{
248 platform_driver_unregister(&syscon_driver);
249}
250module_exit(syscon_exit);
251
252MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
253MODULE_DESCRIPTION("System Control driver");
254MODULE_LICENSE("GPL v2");