KVM: arm: Add initial dirty page locking support
[linux/fpc-iii.git] / drivers / mfd / syscon.c
blob176bf0fa2685e70749f42bad44931b8489e0d681
1 /*
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/err.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_data/syscon.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/mfd/syscon.h>
26 #include <linux/slab.h>
28 static struct platform_driver syscon_driver;
30 static DEFINE_SPINLOCK(syscon_list_slock);
31 static LIST_HEAD(syscon_list);
33 struct syscon {
34 struct device_node *np;
35 struct regmap *regmap;
36 struct list_head list;
39 static struct regmap_config syscon_regmap_config = {
40 .reg_bits = 32,
41 .val_bits = 32,
42 .reg_stride = 4,
45 static struct syscon *of_syscon_register(struct device_node *np)
47 struct syscon *syscon;
48 struct regmap *regmap;
49 void __iomem *base;
50 int ret;
51 struct regmap_config syscon_config = syscon_regmap_config;
53 if (!of_device_is_compatible(np, "syscon"))
54 return ERR_PTR(-EINVAL);
56 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
57 if (!syscon)
58 return ERR_PTR(-ENOMEM);
60 base = of_iomap(np, 0);
61 if (!base) {
62 ret = -ENOMEM;
63 goto err_map;
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;
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;
79 syscon->regmap = regmap;
80 syscon->np = np;
82 spin_lock(&syscon_list_slock);
83 list_add_tail(&syscon->list, &syscon_list);
84 spin_unlock(&syscon_list_slock);
86 return syscon;
88 err_regmap:
89 iounmap(base);
90 err_map:
91 kfree(syscon);
92 return ERR_PTR(ret);
95 struct regmap *syscon_node_to_regmap(struct device_node *np)
97 struct syscon *entry, *syscon = NULL;
99 spin_lock(&syscon_list_slock);
101 list_for_each_entry(entry, &syscon_list, list)
102 if (entry->np == np) {
103 syscon = entry;
104 break;
107 spin_unlock(&syscon_list_slock);
109 if (!syscon)
110 syscon = of_syscon_register(np);
112 if (IS_ERR(syscon))
113 return ERR_CAST(syscon);
115 return syscon->regmap;
117 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
119 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
121 struct device_node *syscon_np;
122 struct regmap *regmap;
124 syscon_np = of_find_compatible_node(NULL, NULL, s);
125 if (!syscon_np)
126 return ERR_PTR(-ENODEV);
128 regmap = syscon_node_to_regmap(syscon_np);
129 of_node_put(syscon_np);
131 return regmap;
133 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
135 static int syscon_match_pdevname(struct device *dev, void *data)
137 return !strcmp(dev_name(dev), (const char *)data);
140 struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
142 struct device *dev;
143 struct syscon *syscon;
145 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
146 syscon_match_pdevname);
147 if (!dev)
148 return ERR_PTR(-EPROBE_DEFER);
150 syscon = dev_get_drvdata(dev);
152 return syscon->regmap;
154 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
156 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
157 const char *property)
159 struct device_node *syscon_np;
160 struct regmap *regmap;
162 if (property)
163 syscon_np = of_parse_phandle(np, property, 0);
164 else
165 syscon_np = np;
167 if (!syscon_np)
168 return ERR_PTR(-ENODEV);
170 regmap = syscon_node_to_regmap(syscon_np);
171 of_node_put(syscon_np);
173 return regmap;
175 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
177 static int syscon_probe(struct platform_device *pdev)
179 struct device *dev = &pdev->dev;
180 struct syscon_platform_data *pdata = dev_get_platdata(dev);
181 struct syscon *syscon;
182 struct resource *res;
183 void __iomem *base;
185 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
186 if (!syscon)
187 return -ENOMEM;
189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190 if (!res)
191 return -ENOENT;
193 base = devm_ioremap(dev, res->start, resource_size(res));
194 if (!base)
195 return -ENOMEM;
197 syscon_regmap_config.max_register = res->end - res->start - 3;
198 if (pdata)
199 syscon_regmap_config.name = pdata->label;
200 syscon->regmap = devm_regmap_init_mmio(dev, base,
201 &syscon_regmap_config);
202 if (IS_ERR(syscon->regmap)) {
203 dev_err(dev, "regmap init failed\n");
204 return PTR_ERR(syscon->regmap);
207 platform_set_drvdata(pdev, syscon);
209 dev_dbg(dev, "regmap %pR registered\n", res);
211 return 0;
214 static const struct platform_device_id syscon_ids[] = {
215 { "syscon", },
219 static struct platform_driver syscon_driver = {
220 .driver = {
221 .name = "syscon",
223 .probe = syscon_probe,
224 .id_table = syscon_ids,
227 static int __init syscon_init(void)
229 return platform_driver_register(&syscon_driver);
231 postcore_initcall(syscon_init);
233 static void __exit syscon_exit(void)
235 platform_driver_unregister(&syscon_driver);
237 module_exit(syscon_exit);
239 MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
240 MODULE_DESCRIPTION("System Control driver");
241 MODULE_LICENSE("GPL v2");