Commit | Line | Data |
---|---|---|
87d68730 DA |
1 | /* |
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> | |
75177dee | 23 | #include <linux/mfd/syscon.h> |
87d68730 DA |
24 | |
25 | static struct platform_driver syscon_driver; | |
26 | ||
27 | struct syscon { | |
87d68730 DA |
28 | void __iomem *base; |
29 | struct regmap *regmap; | |
30 | }; | |
31 | ||
32 | static int syscon_match(struct device *dev, void *data) | |
33 | { | |
87d68730 DA |
34 | struct device_node *dn = data; |
35 | ||
ed21465a | 36 | return (dev->of_node == dn) ? 1 : 0; |
87d68730 DA |
37 | } |
38 | ||
39 | struct 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, | |
45 | syscon_match); | |
46 | if (!dev) | |
47 | return ERR_PTR(-EPROBE_DEFER); | |
48 | ||
49 | syscon = dev_get_drvdata(dev); | |
50 | ||
51 | return syscon->regmap; | |
52 | } | |
53 | EXPORT_SYMBOL_GPL(syscon_node_to_regmap); | |
54 | ||
55 | struct 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 | } | |
69 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); | |
70 | ||
71 | struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, | |
72 | const char *property) | |
73 | { | |
74 | struct device_node *syscon_np; | |
75 | struct regmap *regmap; | |
76 | ||
77 | syscon_np = of_parse_phandle(np, property, 0); | |
78 | if (!syscon_np) | |
79 | return ERR_PTR(-ENODEV); | |
80 | ||
81 | regmap = syscon_node_to_regmap(syscon_np); | |
82 | of_node_put(syscon_np); | |
83 | ||
84 | return regmap; | |
85 | } | |
86 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); | |
87 | ||
88 | static const struct of_device_id of_syscon_match[] = { | |
89 | { .compatible = "syscon", }, | |
90 | { }, | |
91 | }; | |
92 | ||
93 | static struct regmap_config syscon_regmap_config = { | |
94 | .reg_bits = 32, | |
95 | .val_bits = 32, | |
96 | .reg_stride = 4, | |
97 | }; | |
98 | ||
f791be49 | 99 | static int syscon_probe(struct platform_device *pdev) |
87d68730 DA |
100 | { |
101 | struct device *dev = &pdev->dev; | |
102 | struct device_node *np = dev->of_node; | |
103 | struct syscon *syscon; | |
104 | struct resource res; | |
105 | int ret; | |
106 | ||
107 | if (!np) | |
108 | return -ENOENT; | |
109 | ||
110 | syscon = devm_kzalloc(dev, sizeof(struct syscon), | |
111 | GFP_KERNEL); | |
112 | if (!syscon) | |
113 | return -ENOMEM; | |
114 | ||
115 | syscon->base = of_iomap(np, 0); | |
116 | if (!syscon->base) | |
117 | return -EADDRNOTAVAIL; | |
118 | ||
119 | ret = of_address_to_resource(np, 0, &res); | |
120 | if (ret) | |
121 | return ret; | |
122 | ||
123 | syscon_regmap_config.max_register = res.end - res.start - 3; | |
124 | syscon->regmap = devm_regmap_init_mmio(dev, syscon->base, | |
125 | &syscon_regmap_config); | |
126 | if (IS_ERR(syscon->regmap)) { | |
127 | dev_err(dev, "regmap init failed\n"); | |
128 | return PTR_ERR(syscon->regmap); | |
129 | } | |
130 | ||
87d68730 DA |
131 | platform_set_drvdata(pdev, syscon); |
132 | ||
133 | dev_info(dev, "syscon regmap start 0x%x end 0x%x registered\n", | |
134 | res.start, res.end); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
4740f73f | 139 | static int syscon_remove(struct platform_device *pdev) |
87d68730 DA |
140 | { |
141 | struct syscon *syscon; | |
142 | ||
143 | syscon = platform_get_drvdata(pdev); | |
144 | iounmap(syscon->base); | |
145 | platform_set_drvdata(pdev, NULL); | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static struct platform_driver syscon_driver = { | |
151 | .driver = { | |
152 | .name = "syscon", | |
153 | .owner = THIS_MODULE, | |
154 | .of_match_table = of_syscon_match, | |
155 | }, | |
156 | .probe = syscon_probe, | |
84449216 | 157 | .remove = syscon_remove, |
87d68730 DA |
158 | }; |
159 | ||
160 | static int __init syscon_init(void) | |
161 | { | |
162 | return platform_driver_register(&syscon_driver); | |
163 | } | |
164 | postcore_initcall(syscon_init); | |
165 | ||
166 | static void __exit syscon_exit(void) | |
167 | { | |
168 | platform_driver_unregister(&syscon_driver); | |
169 | } | |
170 | module_exit(syscon_exit); | |
171 | ||
172 | MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>"); | |
173 | MODULE_DESCRIPTION("System Control driver"); | |
174 | MODULE_LICENSE("GPL v2"); |