]>
Commit | Line | Data |
---|---|---|
7cc560de YSB |
1 | /* |
2 | * clk-s2mps11.c - Clock driver for S2MPS11. | |
3 | * | |
4 | * Copyright (C) 2013 Samsung Electornics | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/err.h> | |
24 | #include <linux/of.h> | |
25 | #include <linux/clkdev.h> | |
26 | #include <linux/regmap.h> | |
27 | #include <linux/clk-provider.h> | |
28 | #include <linux/platform_device.h> | |
29 | #include <linux/mfd/samsung/s2mps11.h> | |
30 | #include <linux/mfd/samsung/core.h> | |
31 | ||
32 | #define s2mps11_name(a) (a->hw.init->name) | |
33 | ||
34 | static struct clk **clk_table; | |
35 | static struct clk_onecell_data clk_data; | |
36 | ||
37 | enum { | |
38 | S2MPS11_CLK_AP = 0, | |
39 | S2MPS11_CLK_CP, | |
40 | S2MPS11_CLK_BT, | |
41 | S2MPS11_CLKS_NUM, | |
42 | }; | |
43 | ||
44 | struct s2mps11_clk { | |
45 | struct sec_pmic_dev *iodev; | |
46 | struct clk_hw hw; | |
47 | struct clk *clk; | |
48 | struct clk_lookup *lookup; | |
49 | u32 mask; | |
50 | bool enabled; | |
64d64c35 | 51 | unsigned int reg; |
7cc560de YSB |
52 | }; |
53 | ||
54 | static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw) | |
55 | { | |
56 | return container_of(hw, struct s2mps11_clk, hw); | |
57 | } | |
58 | ||
59 | static int s2mps11_clk_prepare(struct clk_hw *hw) | |
60 | { | |
61 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
62 | int ret; | |
63 | ||
1b1ccee1 | 64 | ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, |
64d64c35 | 65 | s2mps11->reg, |
7cc560de YSB |
66 | s2mps11->mask, s2mps11->mask); |
67 | if (!ret) | |
68 | s2mps11->enabled = true; | |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | static void s2mps11_clk_unprepare(struct clk_hw *hw) | |
74 | { | |
75 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
76 | int ret; | |
77 | ||
64d64c35 | 78 | ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg, |
7cc560de YSB |
79 | s2mps11->mask, ~s2mps11->mask); |
80 | ||
81 | if (!ret) | |
82 | s2mps11->enabled = false; | |
83 | } | |
84 | ||
85 | static int s2mps11_clk_is_enabled(struct clk_hw *hw) | |
86 | { | |
87 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
88 | ||
89 | return s2mps11->enabled; | |
90 | } | |
91 | ||
92 | static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw, | |
93 | unsigned long parent_rate) | |
94 | { | |
95 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
96 | if (s2mps11->enabled) | |
97 | return 32768; | |
98 | else | |
99 | return 0; | |
100 | } | |
101 | ||
102 | static struct clk_ops s2mps11_clk_ops = { | |
103 | .prepare = s2mps11_clk_prepare, | |
104 | .unprepare = s2mps11_clk_unprepare, | |
105 | .is_enabled = s2mps11_clk_is_enabled, | |
106 | .recalc_rate = s2mps11_clk_recalc_rate, | |
107 | }; | |
108 | ||
109 | static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = { | |
110 | [S2MPS11_CLK_AP] = { | |
111 | .name = "s2mps11_ap", | |
112 | .ops = &s2mps11_clk_ops, | |
113 | .flags = CLK_IS_ROOT, | |
114 | }, | |
115 | [S2MPS11_CLK_CP] = { | |
116 | .name = "s2mps11_cp", | |
117 | .ops = &s2mps11_clk_ops, | |
118 | .flags = CLK_IS_ROOT, | |
119 | }, | |
120 | [S2MPS11_CLK_BT] = { | |
121 | .name = "s2mps11_bt", | |
122 | .ops = &s2mps11_clk_ops, | |
123 | .flags = CLK_IS_ROOT, | |
124 | }, | |
125 | }; | |
126 | ||
127 | static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev) | |
128 | { | |
129 | struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent); | |
130 | struct device_node *clk_np; | |
131 | int i; | |
132 | ||
133 | if (!iodev->dev->of_node) | |
134 | return NULL; | |
135 | ||
136 | clk_np = of_find_node_by_name(iodev->dev->of_node, "clocks"); | |
137 | if (!clk_np) { | |
138 | dev_err(&pdev->dev, "could not find clock sub-node\n"); | |
139 | return ERR_PTR(-EINVAL); | |
140 | } | |
141 | ||
142 | clk_table = devm_kzalloc(&pdev->dev, sizeof(struct clk *) * | |
143 | S2MPS11_CLKS_NUM, GFP_KERNEL); | |
144 | if (!clk_table) | |
145 | return ERR_PTR(-ENOMEM); | |
146 | ||
147 | for (i = 0; i < S2MPS11_CLKS_NUM; i++) | |
148 | of_property_read_string_index(clk_np, "clock-output-names", i, | |
149 | &s2mps11_clks_init[i].name); | |
150 | ||
151 | return clk_np; | |
152 | } | |
153 | ||
154 | static int s2mps11_clk_probe(struct platform_device *pdev) | |
155 | { | |
156 | struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent); | |
157 | struct s2mps11_clk *s2mps11_clks, *s2mps11_clk; | |
158 | struct device_node *clk_np = NULL; | |
64d64c35 | 159 | unsigned int s2mps11_reg; |
7cc560de YSB |
160 | int i, ret = 0; |
161 | u32 val; | |
162 | ||
163 | s2mps11_clks = devm_kzalloc(&pdev->dev, sizeof(*s2mps11_clk) * | |
164 | S2MPS11_CLKS_NUM, GFP_KERNEL); | |
165 | if (!s2mps11_clks) | |
166 | return -ENOMEM; | |
167 | ||
168 | s2mps11_clk = s2mps11_clks; | |
169 | ||
170 | clk_np = s2mps11_clk_parse_dt(pdev); | |
171 | if (IS_ERR(clk_np)) | |
172 | return PTR_ERR(clk_np); | |
173 | ||
64d64c35 TB |
174 | switch(platform_get_device_id(pdev)->driver_data) { |
175 | case S2MPS11X: | |
176 | s2mps11_reg = S2MPS11_REG_RTC_CTRL; | |
177 | break; | |
178 | default: | |
179 | dev_err(&pdev->dev, "Invalid device type\n"); | |
180 | return -EINVAL; | |
181 | }; | |
182 | ||
7cc560de YSB |
183 | for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) { |
184 | s2mps11_clk->iodev = iodev; | |
185 | s2mps11_clk->hw.init = &s2mps11_clks_init[i]; | |
186 | s2mps11_clk->mask = 1 << i; | |
64d64c35 | 187 | s2mps11_clk->reg = s2mps11_reg; |
7cc560de | 188 | |
1b1ccee1 | 189 | ret = regmap_read(s2mps11_clk->iodev->regmap_pmic, |
64d64c35 | 190 | s2mps11_clk->reg, &val); |
7cc560de YSB |
191 | if (ret < 0) |
192 | goto err_reg; | |
193 | ||
194 | s2mps11_clk->enabled = val & s2mps11_clk->mask; | |
195 | ||
196 | s2mps11_clk->clk = devm_clk_register(&pdev->dev, | |
197 | &s2mps11_clk->hw); | |
198 | if (IS_ERR(s2mps11_clk->clk)) { | |
199 | dev_err(&pdev->dev, "Fail to register : %s\n", | |
200 | s2mps11_name(s2mps11_clk)); | |
201 | ret = PTR_ERR(s2mps11_clk->clk); | |
202 | goto err_reg; | |
203 | } | |
204 | ||
205 | s2mps11_clk->lookup = devm_kzalloc(&pdev->dev, | |
206 | sizeof(struct clk_lookup), GFP_KERNEL); | |
207 | if (!s2mps11_clk->lookup) { | |
208 | ret = -ENOMEM; | |
209 | goto err_lup; | |
210 | } | |
211 | ||
212 | s2mps11_clk->lookup->con_id = s2mps11_name(s2mps11_clk); | |
213 | s2mps11_clk->lookup->clk = s2mps11_clk->clk; | |
214 | ||
215 | clkdev_add(s2mps11_clk->lookup); | |
216 | } | |
217 | ||
218 | if (clk_table) { | |
219 | for (i = 0; i < S2MPS11_CLKS_NUM; i++) | |
220 | clk_table[i] = s2mps11_clks[i].clk; | |
221 | ||
222 | clk_data.clks = clk_table; | |
223 | clk_data.clk_num = S2MPS11_CLKS_NUM; | |
224 | of_clk_add_provider(clk_np, of_clk_src_onecell_get, &clk_data); | |
225 | } | |
226 | ||
227 | platform_set_drvdata(pdev, s2mps11_clks); | |
228 | ||
229 | return ret; | |
230 | err_lup: | |
231 | devm_clk_unregister(&pdev->dev, s2mps11_clk->clk); | |
232 | err_reg: | |
233 | while (s2mps11_clk > s2mps11_clks) { | |
234 | if (s2mps11_clk->lookup) { | |
235 | clkdev_drop(s2mps11_clk->lookup); | |
236 | devm_clk_unregister(&pdev->dev, s2mps11_clk->clk); | |
237 | } | |
238 | s2mps11_clk--; | |
239 | } | |
240 | ||
241 | return ret; | |
242 | } | |
243 | ||
244 | static int s2mps11_clk_remove(struct platform_device *pdev) | |
245 | { | |
246 | struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev); | |
247 | int i; | |
248 | ||
249 | for (i = 0; i < S2MPS11_CLKS_NUM; i++) | |
250 | clkdev_drop(s2mps11_clks[i].lookup); | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
255 | static const struct platform_device_id s2mps11_clk_id[] = { | |
64d64c35 | 256 | { "s2mps11-clk", S2MPS11X}, |
7cc560de YSB |
257 | { }, |
258 | }; | |
259 | MODULE_DEVICE_TABLE(platform, s2mps11_clk_id); | |
260 | ||
261 | static struct platform_driver s2mps11_clk_driver = { | |
262 | .driver = { | |
263 | .name = "s2mps11-clk", | |
264 | .owner = THIS_MODULE, | |
265 | }, | |
266 | .probe = s2mps11_clk_probe, | |
267 | .remove = s2mps11_clk_remove, | |
268 | .id_table = s2mps11_clk_id, | |
269 | }; | |
270 | ||
271 | static int __init s2mps11_clk_init(void) | |
272 | { | |
273 | return platform_driver_register(&s2mps11_clk_driver); | |
274 | } | |
275 | subsys_initcall(s2mps11_clk_init); | |
276 | ||
277 | static void __init s2mps11_clk_cleanup(void) | |
278 | { | |
279 | platform_driver_unregister(&s2mps11_clk_driver); | |
280 | } | |
281 | module_exit(s2mps11_clk_cleanup); | |
282 | ||
283 | MODULE_DESCRIPTION("S2MPS11 Clock Driver"); | |
284 | MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>"); | |
285 | MODULE_LICENSE("GPL"); |