blob: 176bf0fa2685e70749f42bad44931b8489e0d681 [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;
50 int ret;
51 struct regmap_config syscon_config = syscon_regmap_config;
52
53 if (!of_device_is_compatible(np, "syscon"))
54 return ERR_PTR(-EINVAL);
55
56 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
57 if (!syscon)
58 return ERR_PTR(-ENOMEM);
59
60 base = of_iomap(np, 0);
61 if (!base) {
62 ret = -ENOMEM;
63 goto err_map;
64 }
65
66 /* Parse the device's DT node for an endianness specification */
67 if (of_property_read_bool(np, "big-endian"))
68 syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
69 else if (of_property_read_bool(np, "little-endian"))
70 syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
71
72 regmap = regmap_init_mmio(NULL, base, &syscon_config);
73 if (IS_ERR(regmap)) {
74 pr_err("regmap init failed\n");
75 ret = PTR_ERR(regmap);
76 goto err_regmap;
77 }
78
79 syscon->regmap = regmap;
80 syscon->np = np;
81
82 spin_lock(&syscon_list_slock);
83 list_add_tail(&syscon->list, &syscon_list);
84 spin_unlock(&syscon_list_slock);
85
86 return syscon;
87
88err_regmap:
89 iounmap(base);
90err_map:
91 kfree(syscon);
92 return ERR_PTR(ret);
Dong Aisheng87d68732012-09-05 10:57:13 +080093}
94
95struct regmap *syscon_node_to_regmap(struct device_node *np)
96{
Pankaj Dubeybdb00662014-09-30 14:05:27 +053097 struct syscon *entry, *syscon = NULL;
Dong Aisheng87d68732012-09-05 10:57:13 +080098
Pankaj Dubeybdb00662014-09-30 14:05:27 +053099 spin_lock(&syscon_list_slock);
Dong Aisheng87d68732012-09-05 10:57:13 +0800100
Pankaj Dubeybdb00662014-09-30 14:05:27 +0530101 list_for_each_entry(entry, &syscon_list, list)
102 if (entry->np == np) {
103 syscon = entry;
104 break;
105 }
106
107 spin_unlock(&syscon_list_slock);
108
109 if (!syscon)
110 syscon = of_syscon_register(np);
111
112 if (IS_ERR(syscon))
113 return ERR_CAST(syscon);
Dong Aisheng87d68732012-09-05 10:57:13 +0800114
115 return syscon->regmap;
116}
117EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
118
119struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
120{
121 struct device_node *syscon_np;
122 struct regmap *regmap;
123
124 syscon_np = of_find_compatible_node(NULL, NULL, s);
125 if (!syscon_np)
126 return ERR_PTR(-ENODEV);
127
128 regmap = syscon_node_to_regmap(syscon_np);
129 of_node_put(syscon_np);
130
131 return regmap;
132}
133EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
134
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400135static int syscon_match_pdevname(struct device *dev, void *data)
136{
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400137 return !strcmp(dev_name(dev), (const char *)data);
138}
139
140struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
141{
142 struct device *dev;
143 struct syscon *syscon;
144
145 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
146 syscon_match_pdevname);
147 if (!dev)
148 return ERR_PTR(-EPROBE_DEFER);
149
150 syscon = dev_get_drvdata(dev);
151
152 return syscon->regmap;
153}
154EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
155
Dong Aisheng87d68732012-09-05 10:57:13 +0800156struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
157 const char *property)
158{
159 struct device_node *syscon_np;
160 struct regmap *regmap;
161
Pankaj Dubey45330bb2014-04-30 11:14:26 +0900162 if (property)
163 syscon_np = of_parse_phandle(np, property, 0);
164 else
165 syscon_np = np;
166
Dong Aisheng87d68732012-09-05 10:57:13 +0800167 if (!syscon_np)
168 return ERR_PTR(-ENODEV);
169
170 regmap = syscon_node_to_regmap(syscon_np);
171 of_node_put(syscon_np);
172
173 return regmap;
174}
175EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
176
Bill Pembertonf791be42012-11-19 13:23:04 -0500177static int syscon_probe(struct platform_device *pdev)
Dong Aisheng87d68732012-09-05 10:57:13 +0800178{
179 struct device *dev = &pdev->dev;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000180 struct syscon_platform_data *pdata = dev_get_platdata(dev);
Dong Aisheng87d68732012-09-05 10:57:13 +0800181 struct syscon *syscon;
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400182 struct resource *res;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400183 void __iomem *base;
Dong Aisheng87d68732012-09-05 10:57:13 +0800184
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400185 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
Dong Aisheng87d68732012-09-05 10:57:13 +0800186 if (!syscon)
187 return -ENOMEM;
188
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190 if (!res)
191 return -ENOENT;
192
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400193 base = devm_ioremap(dev, res->start, resource_size(res));
194 if (!base)
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400195 return -ENOMEM;
Dong Aisheng87d68732012-09-05 10:57:13 +0800196
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400197 syscon_regmap_config.max_register = res->end - res->start - 3;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000198 if (pdata)
199 syscon_regmap_config.name = pdata->label;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400200 syscon->regmap = devm_regmap_init_mmio(dev, base,
Dong Aisheng87d68732012-09-05 10:57:13 +0800201 &syscon_regmap_config);
202 if (IS_ERR(syscon->regmap)) {
203 dev_err(dev, "regmap init failed\n");
204 return PTR_ERR(syscon->regmap);
205 }
206
Dong Aisheng87d68732012-09-05 10:57:13 +0800207 platform_set_drvdata(pdev, syscon);
208
Alexander Shiyan38d89742014-02-22 10:02:25 +0400209 dev_dbg(dev, "regmap %pR registered\n", res);
Dong Aisheng87d68732012-09-05 10:57:13 +0800210
211 return 0;
212}
213
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400214static const struct platform_device_id syscon_ids[] = {
215 { "syscon", },
216 { }
217};
Dong Aisheng87d68732012-09-05 10:57:13 +0800218
219static struct platform_driver syscon_driver = {
220 .driver = {
221 .name = "syscon",
Dong Aisheng87d68732012-09-05 10:57:13 +0800222 },
223 .probe = syscon_probe,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400224 .id_table = syscon_ids,
Dong Aisheng87d68732012-09-05 10:57:13 +0800225};
226
227static int __init syscon_init(void)
228{
229 return platform_driver_register(&syscon_driver);
230}
231postcore_initcall(syscon_init);
232
233static void __exit syscon_exit(void)
234{
235 platform_driver_unregister(&syscon_driver);
236}
237module_exit(syscon_exit);
238
239MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
240MODULE_DESCRIPTION("System Control driver");
241MODULE_LICENSE("GPL v2");