blob: 27db1f92bb262891d59effede70e149e131c4d37 [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>
18#include <linux/of.h>
19#include <linux/of_address.h>
20#include <linux/of_platform.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
Fabio Estevam75177de2013-02-11 18:48:00 -020023#include <linux/mfd/syscon.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080024
25static struct platform_driver syscon_driver;
26
27struct syscon {
Dong Aisheng87d68732012-09-05 10:57:13 +080028 struct regmap *regmap;
29};
30
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040031static int syscon_match_node(struct device *dev, void *data)
Dong Aisheng87d68732012-09-05 10:57:13 +080032{
Dong Aisheng87d68732012-09-05 10:57:13 +080033 struct device_node *dn = data;
34
Alexander Shiyaned214652013-03-13 21:34:19 +040035 return (dev->of_node == dn) ? 1 : 0;
Dong Aisheng87d68732012-09-05 10:57:13 +080036}
37
38struct regmap *syscon_node_to_regmap(struct device_node *np)
39{
40 struct syscon *syscon;
41 struct device *dev;
42
43 dev = driver_find_device(&syscon_driver.driver, NULL, np,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040044 syscon_match_node);
Dong Aisheng87d68732012-09-05 10:57:13 +080045 if (!dev)
46 return ERR_PTR(-EPROBE_DEFER);
47
48 syscon = dev_get_drvdata(dev);
49
50 return syscon->regmap;
51}
52EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
53
54struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
55{
56 struct device_node *syscon_np;
57 struct regmap *regmap;
58
59 syscon_np = of_find_compatible_node(NULL, NULL, s);
60 if (!syscon_np)
61 return ERR_PTR(-ENODEV);
62
63 regmap = syscon_node_to_regmap(syscon_np);
64 of_node_put(syscon_np);
65
66 return regmap;
67}
68EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
69
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040070static int syscon_match_pdevname(struct device *dev, void *data)
71{
72 struct platform_device *pdev = to_platform_device(dev);
73 const struct platform_device_id *id = platform_get_device_id(pdev);
74
75 if (id)
76 if (!strcmp(id->name, (const char *)data))
77 return 1;
78
79 return !strcmp(dev_name(dev), (const char *)data);
80}
81
82struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
83{
84 struct device *dev;
85 struct syscon *syscon;
86
87 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
88 syscon_match_pdevname);
89 if (!dev)
90 return ERR_PTR(-EPROBE_DEFER);
91
92 syscon = dev_get_drvdata(dev);
93
94 return syscon->regmap;
95}
96EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
97
Dong Aisheng87d68732012-09-05 10:57:13 +080098struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
99 const char *property)
100{
101 struct device_node *syscon_np;
102 struct regmap *regmap;
103
104 syscon_np = of_parse_phandle(np, property, 0);
105 if (!syscon_np)
106 return ERR_PTR(-ENODEV);
107
108 regmap = syscon_node_to_regmap(syscon_np);
109 of_node_put(syscon_np);
110
111 return regmap;
112}
113EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
114
115static const struct of_device_id of_syscon_match[] = {
116 { .compatible = "syscon", },
117 { },
118};
119
120static struct regmap_config syscon_regmap_config = {
121 .reg_bits = 32,
122 .val_bits = 32,
123 .reg_stride = 4,
124};
125
Bill Pembertonf791be42012-11-19 13:23:04 -0500126static int syscon_probe(struct platform_device *pdev)
Dong Aisheng87d68732012-09-05 10:57:13 +0800127{
128 struct device *dev = &pdev->dev;
Dong Aisheng87d68732012-09-05 10:57:13 +0800129 struct syscon *syscon;
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400130 struct resource *res;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400131 void __iomem *base;
Dong Aisheng87d68732012-09-05 10:57:13 +0800132
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400133 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
Dong Aisheng87d68732012-09-05 10:57:13 +0800134 if (!syscon)
135 return -ENOMEM;
136
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400137 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 if (!res)
139 return -ENOENT;
140
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400141 base = devm_ioremap(dev, res->start, resource_size(res));
142 if (!base)
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400143 return -ENOMEM;
Dong Aisheng87d68732012-09-05 10:57:13 +0800144
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400145 syscon_regmap_config.max_register = res->end - res->start - 3;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400146 syscon->regmap = devm_regmap_init_mmio(dev, base,
Dong Aisheng87d68732012-09-05 10:57:13 +0800147 &syscon_regmap_config);
148 if (IS_ERR(syscon->regmap)) {
149 dev_err(dev, "regmap init failed\n");
150 return PTR_ERR(syscon->regmap);
151 }
152
Dong Aisheng87d68732012-09-05 10:57:13 +0800153 platform_set_drvdata(pdev, syscon);
154
Alexander Shiyandaba96d2013-04-09 21:50:02 +0400155 dev_info(dev, "regmap %pR registered\n", res);
Dong Aisheng87d68732012-09-05 10:57:13 +0800156
157 return 0;
158}
159
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400160static const struct platform_device_id syscon_ids[] = {
161 { "syscon", },
Alexander Shiyan65976192013-05-13 21:07:36 +0400162#ifdef CONFIG_ARCH_CLPS711X
163 { "clps711x-syscon", },
164#endif
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400165 { }
166};
Dong Aisheng87d68732012-09-05 10:57:13 +0800167
168static struct platform_driver syscon_driver = {
169 .driver = {
170 .name = "syscon",
171 .owner = THIS_MODULE,
172 .of_match_table = of_syscon_match,
173 },
174 .probe = syscon_probe,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400175 .id_table = syscon_ids,
Dong Aisheng87d68732012-09-05 10:57:13 +0800176};
177
178static int __init syscon_init(void)
179{
180 return platform_driver_register(&syscon_driver);
181}
182postcore_initcall(syscon_init);
183
184static void __exit syscon_exit(void)
185{
186 platform_driver_unregister(&syscon_driver);
187}
188module_exit(syscon_exit);
189
190MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
191MODULE_DESCRIPTION("System Control driver");
192MODULE_LICENSE("GPL v2");