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> | |
e8e6b840 | 30 | #include <linux/mfd/samsung/s5m8767.h> |
7cc560de YSB |
31 | #include <linux/mfd/samsung/core.h> |
32 | ||
33 | #define s2mps11_name(a) (a->hw.init->name) | |
34 | ||
35 | static struct clk **clk_table; | |
36 | static struct clk_onecell_data clk_data; | |
37 | ||
38 | enum { | |
39 | S2MPS11_CLK_AP = 0, | |
40 | S2MPS11_CLK_CP, | |
41 | S2MPS11_CLK_BT, | |
42 | S2MPS11_CLKS_NUM, | |
43 | }; | |
44 | ||
45 | struct s2mps11_clk { | |
46 | struct sec_pmic_dev *iodev; | |
bf416bd4 | 47 | struct device_node *clk_np; |
7cc560de YSB |
48 | struct clk_hw hw; |
49 | struct clk *clk; | |
50 | struct clk_lookup *lookup; | |
51 | u32 mask; | |
52 | bool enabled; | |
64d64c35 | 53 | unsigned int reg; |
7cc560de YSB |
54 | }; |
55 | ||
56 | static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw) | |
57 | { | |
58 | return container_of(hw, struct s2mps11_clk, hw); | |
59 | } | |
60 | ||
61 | static int s2mps11_clk_prepare(struct clk_hw *hw) | |
62 | { | |
63 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
64 | int ret; | |
65 | ||
1b1ccee1 | 66 | ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, |
64d64c35 | 67 | s2mps11->reg, |
7cc560de YSB |
68 | s2mps11->mask, s2mps11->mask); |
69 | if (!ret) | |
70 | s2mps11->enabled = true; | |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
75 | static void s2mps11_clk_unprepare(struct clk_hw *hw) | |
76 | { | |
77 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
78 | int ret; | |
79 | ||
64d64c35 | 80 | ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg, |
7cc560de YSB |
81 | s2mps11->mask, ~s2mps11->mask); |
82 | ||
83 | if (!ret) | |
84 | s2mps11->enabled = false; | |
85 | } | |
86 | ||
87 | static int s2mps11_clk_is_enabled(struct clk_hw *hw) | |
88 | { | |
89 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
90 | ||
91 | return s2mps11->enabled; | |
92 | } | |
93 | ||
94 | static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw, | |
95 | unsigned long parent_rate) | |
96 | { | |
97 | struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw); | |
98 | if (s2mps11->enabled) | |
99 | return 32768; | |
100 | else | |
101 | return 0; | |
102 | } | |
103 | ||
104 | static struct clk_ops s2mps11_clk_ops = { | |
105 | .prepare = s2mps11_clk_prepare, | |
106 | .unprepare = s2mps11_clk_unprepare, | |
107 | .is_enabled = s2mps11_clk_is_enabled, | |
108 | .recalc_rate = s2mps11_clk_recalc_rate, | |
109 | }; | |
110 | ||
111 | static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = { | |
112 | [S2MPS11_CLK_AP] = { | |
113 | .name = "s2mps11_ap", | |
114 | .ops = &s2mps11_clk_ops, | |
115 | .flags = CLK_IS_ROOT, | |
116 | }, | |
117 | [S2MPS11_CLK_CP] = { | |
118 | .name = "s2mps11_cp", | |
119 | .ops = &s2mps11_clk_ops, | |
120 | .flags = CLK_IS_ROOT, | |
121 | }, | |
122 | [S2MPS11_CLK_BT] = { | |
123 | .name = "s2mps11_bt", | |
124 | .ops = &s2mps11_clk_ops, | |
125 | .flags = CLK_IS_ROOT, | |
126 | }, | |
127 | }; | |
128 | ||
129 | static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev) | |
130 | { | |
131 | struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent); | |
132 | struct device_node *clk_np; | |
133 | int i; | |
134 | ||
135 | if (!iodev->dev->of_node) | |
238e1405 | 136 | return ERR_PTR(-EINVAL); |
7cc560de | 137 | |
afbd1a0c | 138 | clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks"); |
7cc560de YSB |
139 | if (!clk_np) { |
140 | dev_err(&pdev->dev, "could not find clock sub-node\n"); | |
141 | return ERR_PTR(-EINVAL); | |
142 | } | |
143 | ||
144 | clk_table = devm_kzalloc(&pdev->dev, sizeof(struct clk *) * | |
145 | S2MPS11_CLKS_NUM, GFP_KERNEL); | |
146 | if (!clk_table) | |
147 | return ERR_PTR(-ENOMEM); | |
148 | ||
149 | for (i = 0; i < S2MPS11_CLKS_NUM; i++) | |
150 | of_property_read_string_index(clk_np, "clock-output-names", i, | |
151 | &s2mps11_clks_init[i].name); | |
152 | ||
153 | return clk_np; | |
154 | } | |
155 | ||
156 | static int s2mps11_clk_probe(struct platform_device *pdev) | |
157 | { | |
158 | struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent); | |
159 | struct s2mps11_clk *s2mps11_clks, *s2mps11_clk; | |
64d64c35 | 160 | unsigned int s2mps11_reg; |
7cc560de YSB |
161 | int i, ret = 0; |
162 | u32 val; | |
163 | ||
164 | s2mps11_clks = devm_kzalloc(&pdev->dev, sizeof(*s2mps11_clk) * | |
165 | S2MPS11_CLKS_NUM, GFP_KERNEL); | |
166 | if (!s2mps11_clks) | |
167 | return -ENOMEM; | |
168 | ||
169 | s2mps11_clk = s2mps11_clks; | |
170 | ||
bf416bd4 KK |
171 | /* Store clocks of_node in first element of s2mps11_clks array */ |
172 | s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev); | |
173 | if (IS_ERR(s2mps11_clks->clk_np)) | |
174 | return PTR_ERR(s2mps11_clks->clk_np); | |
7cc560de | 175 | |
64d64c35 TB |
176 | switch(platform_get_device_id(pdev)->driver_data) { |
177 | case S2MPS11X: | |
178 | s2mps11_reg = S2MPS11_REG_RTC_CTRL; | |
179 | break; | |
e8e6b840 TB |
180 | case S5M8767X: |
181 | s2mps11_reg = S5M8767_REG_CTRL1; | |
182 | break; | |
64d64c35 TB |
183 | default: |
184 | dev_err(&pdev->dev, "Invalid device type\n"); | |
185 | return -EINVAL; | |
186 | }; | |
187 | ||
7cc560de YSB |
188 | for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) { |
189 | s2mps11_clk->iodev = iodev; | |
190 | s2mps11_clk->hw.init = &s2mps11_clks_init[i]; | |
191 | s2mps11_clk->mask = 1 << i; | |
64d64c35 | 192 | s2mps11_clk->reg = s2mps11_reg; |
7cc560de | 193 | |
1b1ccee1 | 194 | ret = regmap_read(s2mps11_clk->iodev->regmap_pmic, |
64d64c35 | 195 | s2mps11_clk->reg, &val); |
7cc560de YSB |
196 | if (ret < 0) |
197 | goto err_reg; | |
198 | ||
199 | s2mps11_clk->enabled = val & s2mps11_clk->mask; | |
200 | ||
201 | s2mps11_clk->clk = devm_clk_register(&pdev->dev, | |
202 | &s2mps11_clk->hw); | |
203 | if (IS_ERR(s2mps11_clk->clk)) { | |
204 | dev_err(&pdev->dev, "Fail to register : %s\n", | |
205 | s2mps11_name(s2mps11_clk)); | |
206 | ret = PTR_ERR(s2mps11_clk->clk); | |
207 | goto err_reg; | |
208 | } | |
209 | ||
210 | s2mps11_clk->lookup = devm_kzalloc(&pdev->dev, | |
211 | sizeof(struct clk_lookup), GFP_KERNEL); | |
212 | if (!s2mps11_clk->lookup) { | |
213 | ret = -ENOMEM; | |
214 | goto err_lup; | |
215 | } | |
216 | ||
217 | s2mps11_clk->lookup->con_id = s2mps11_name(s2mps11_clk); | |
218 | s2mps11_clk->lookup->clk = s2mps11_clk->clk; | |
219 | ||
220 | clkdev_add(s2mps11_clk->lookup); | |
221 | } | |
222 | ||
223 | if (clk_table) { | |
224 | for (i = 0; i < S2MPS11_CLKS_NUM; i++) | |
225 | clk_table[i] = s2mps11_clks[i].clk; | |
226 | ||
227 | clk_data.clks = clk_table; | |
228 | clk_data.clk_num = S2MPS11_CLKS_NUM; | |
bf416bd4 KK |
229 | of_clk_add_provider(s2mps11_clks->clk_np, |
230 | of_clk_src_onecell_get, &clk_data); | |
7cc560de YSB |
231 | } |
232 | ||
233 | platform_set_drvdata(pdev, s2mps11_clks); | |
234 | ||
235 | return ret; | |
236 | err_lup: | |
237 | devm_clk_unregister(&pdev->dev, s2mps11_clk->clk); | |
238 | err_reg: | |
239 | while (s2mps11_clk > s2mps11_clks) { | |
240 | if (s2mps11_clk->lookup) { | |
241 | clkdev_drop(s2mps11_clk->lookup); | |
242 | devm_clk_unregister(&pdev->dev, s2mps11_clk->clk); | |
243 | } | |
244 | s2mps11_clk--; | |
245 | } | |
246 | ||
247 | return ret; | |
248 | } | |
249 | ||
250 | static int s2mps11_clk_remove(struct platform_device *pdev) | |
251 | { | |
252 | struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev); | |
253 | int i; | |
254 | ||
bf416bd4 KK |
255 | of_clk_del_provider(s2mps11_clks[0].clk_np); |
256 | /* Drop the reference obtained in s2mps11_clk_parse_dt */ | |
257 | of_node_put(s2mps11_clks[0].clk_np); | |
258 | ||
7cc560de YSB |
259 | for (i = 0; i < S2MPS11_CLKS_NUM; i++) |
260 | clkdev_drop(s2mps11_clks[i].lookup); | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static const struct platform_device_id s2mps11_clk_id[] = { | |
64d64c35 | 266 | { "s2mps11-clk", S2MPS11X}, |
e8e6b840 | 267 | { "s5m8767-clk", S5M8767X}, |
7cc560de YSB |
268 | { }, |
269 | }; | |
270 | MODULE_DEVICE_TABLE(platform, s2mps11_clk_id); | |
271 | ||
272 | static struct platform_driver s2mps11_clk_driver = { | |
273 | .driver = { | |
274 | .name = "s2mps11-clk", | |
275 | .owner = THIS_MODULE, | |
276 | }, | |
277 | .probe = s2mps11_clk_probe, | |
278 | .remove = s2mps11_clk_remove, | |
279 | .id_table = s2mps11_clk_id, | |
280 | }; | |
281 | ||
282 | static int __init s2mps11_clk_init(void) | |
283 | { | |
284 | return platform_driver_register(&s2mps11_clk_driver); | |
285 | } | |
286 | subsys_initcall(s2mps11_clk_init); | |
287 | ||
288 | static void __init s2mps11_clk_cleanup(void) | |
289 | { | |
290 | platform_driver_unregister(&s2mps11_clk_driver); | |
291 | } | |
292 | module_exit(s2mps11_clk_cleanup); | |
293 | ||
294 | MODULE_DESCRIPTION("S2MPS11 Clock Driver"); | |
295 | MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>"); | |
296 | MODULE_LICENSE("GPL"); |