blob: e2a04bb8bc1e7fb7b0948bf186ec2df8aa13b632 [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>
Pawel Moll29f9b6c2014-02-12 10:47:10 +000021#include <linux/platform_data/syscon.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080022#include <linux/platform_device.h>
23#include <linux/regmap.h>
Fabio Estevam75177de2013-02-11 18:48:00 -020024#include <linux/mfd/syscon.h>
Dong Aisheng87d68732012-09-05 10:57:13 +080025
26static struct platform_driver syscon_driver;
27
28struct syscon {
Dong Aisheng87d68732012-09-05 10:57:13 +080029 struct regmap *regmap;
30};
31
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040032static int syscon_match_node(struct device *dev, void *data)
Dong Aisheng87d68732012-09-05 10:57:13 +080033{
Dong Aisheng87d68732012-09-05 10:57:13 +080034 struct device_node *dn = data;
35
Alexander Shiyaned214652013-03-13 21:34:19 +040036 return (dev->of_node == dn) ? 1 : 0;
Dong Aisheng87d68732012-09-05 10:57:13 +080037}
38
39struct regmap *syscon_node_to_regmap(struct device_node *np)
40{
41 struct syscon *syscon;
42 struct device *dev;
43
44 dev = driver_find_device(&syscon_driver.driver, NULL, np,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040045 syscon_match_node);
Dong Aisheng87d68732012-09-05 10:57:13 +080046 if (!dev)
47 return ERR_PTR(-EPROBE_DEFER);
48
49 syscon = dev_get_drvdata(dev);
50
51 return syscon->regmap;
52}
53EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
54
55struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
56{
57 struct device_node *syscon_np;
58 struct regmap *regmap;
59
60 syscon_np = of_find_compatible_node(NULL, NULL, s);
61 if (!syscon_np)
62 return ERR_PTR(-ENODEV);
63
64 regmap = syscon_node_to_regmap(syscon_np);
65 of_node_put(syscon_np);
66
67 return regmap;
68}
69EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
70
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040071static int syscon_match_pdevname(struct device *dev, void *data)
72{
Alexander Shiyan5ab3a892013-03-13 21:34:20 +040073 return !strcmp(dev_name(dev), (const char *)data);
74}
75
76struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
77{
78 struct device *dev;
79 struct syscon *syscon;
80
81 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
82 syscon_match_pdevname);
83 if (!dev)
84 return ERR_PTR(-EPROBE_DEFER);
85
86 syscon = dev_get_drvdata(dev);
87
88 return syscon->regmap;
89}
90EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
91
Dong Aisheng87d68732012-09-05 10:57:13 +080092struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
93 const char *property)
94{
95 struct device_node *syscon_np;
96 struct regmap *regmap;
97
98 syscon_np = of_parse_phandle(np, property, 0);
99 if (!syscon_np)
100 return ERR_PTR(-ENODEV);
101
102 regmap = syscon_node_to_regmap(syscon_np);
103 of_node_put(syscon_np);
104
105 return regmap;
106}
107EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
108
109static const struct of_device_id of_syscon_match[] = {
110 { .compatible = "syscon", },
111 { },
112};
113
114static struct regmap_config syscon_regmap_config = {
115 .reg_bits = 32,
116 .val_bits = 32,
117 .reg_stride = 4,
118};
119
Bill Pembertonf791be42012-11-19 13:23:04 -0500120static int syscon_probe(struct platform_device *pdev)
Dong Aisheng87d68732012-09-05 10:57:13 +0800121{
122 struct device *dev = &pdev->dev;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000123 struct syscon_platform_data *pdata = dev_get_platdata(dev);
Dong Aisheng87d68732012-09-05 10:57:13 +0800124 struct syscon *syscon;
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400125 struct resource *res;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400126 void __iomem *base;
Dong Aisheng87d68732012-09-05 10:57:13 +0800127
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400128 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
Dong Aisheng87d68732012-09-05 10:57:13 +0800129 if (!syscon)
130 return -ENOMEM;
131
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 if (!res)
134 return -ENOENT;
135
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400136 base = devm_ioremap(dev, res->start, resource_size(res));
137 if (!base)
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400138 return -ENOMEM;
Dong Aisheng87d68732012-09-05 10:57:13 +0800139
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400140 syscon_regmap_config.max_register = res->end - res->start - 3;
Pawel Moll29f9b6c2014-02-12 10:47:10 +0000141 if (pdata)
142 syscon_regmap_config.name = pdata->label;
Alexander Shiyanf10111cc2013-07-14 10:39:42 +0400143 syscon->regmap = devm_regmap_init_mmio(dev, base,
Dong Aisheng87d68732012-09-05 10:57:13 +0800144 &syscon_regmap_config);
145 if (IS_ERR(syscon->regmap)) {
146 dev_err(dev, "regmap init failed\n");
147 return PTR_ERR(syscon->regmap);
148 }
149
Dong Aisheng87d68732012-09-05 10:57:13 +0800150 platform_set_drvdata(pdev, syscon);
151
Alexander Shiyan38d89742014-02-22 10:02:25 +0400152 dev_dbg(dev, "regmap %pR registered\n", res);
Dong Aisheng87d68732012-09-05 10:57:13 +0800153
154 return 0;
155}
156
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400157static const struct platform_device_id syscon_ids[] = {
158 { "syscon", },
159 { }
160};
Dong Aisheng87d68732012-09-05 10:57:13 +0800161
162static struct platform_driver syscon_driver = {
163 .driver = {
164 .name = "syscon",
165 .owner = THIS_MODULE,
166 .of_match_table = of_syscon_match,
167 },
168 .probe = syscon_probe,
Alexander Shiyan5ab3a892013-03-13 21:34:20 +0400169 .id_table = syscon_ids,
Dong Aisheng87d68732012-09-05 10:57:13 +0800170};
171
172static int __init syscon_init(void)
173{
174 return platform_driver_register(&syscon_driver);
175}
176postcore_initcall(syscon_init);
177
178static void __exit syscon_exit(void)
179{
180 platform_driver_unregister(&syscon_driver);
181}
182module_exit(syscon_exit);
183
184MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
185MODULE_DESCRIPTION("System Control driver");
186MODULE_LICENSE("GPL v2");