blob: 9b7b5859a4206f3414e67b37f48675f8d94919f6 [file] [log] [blame]
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +05301/*
2 * clk-s2mps11.c - Clock driver for S2MPS11.
3 *
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +02004 * Copyright (C) 2013,2014 Samsung Electornics
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +05305 *
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 *
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053016 */
17
18#include <linux/module.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/clkdev.h>
22#include <linux/regmap.h>
23#include <linux/clk-provider.h>
24#include <linux/platform_device.h>
25#include <linux/mfd/samsung/s2mps11.h>
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +020026#include <linux/mfd/samsung/s2mps14.h>
Tushar Beherae8e6b842013-12-26 15:48:59 +053027#include <linux/mfd/samsung/s5m8767.h>
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053028#include <linux/mfd/samsung/core.h>
29
30#define s2mps11_name(a) (a->hw.init->name)
31
32static struct clk **clk_table;
33static struct clk_onecell_data clk_data;
34
35enum {
36 S2MPS11_CLK_AP = 0,
37 S2MPS11_CLK_CP,
38 S2MPS11_CLK_BT,
39 S2MPS11_CLKS_NUM,
40};
41
42struct s2mps11_clk {
43 struct sec_pmic_dev *iodev;
Krzysztof Kozlowskibf416bd2014-05-21 13:22:59 +020044 struct device_node *clk_np;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053045 struct clk_hw hw;
46 struct clk *clk;
47 struct clk_lookup *lookup;
48 u32 mask;
49 bool enabled;
Tushar Behera64d64c32013-12-26 15:48:58 +053050 unsigned int reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053051};
52
53static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
54{
55 return container_of(hw, struct s2mps11_clk, hw);
56}
57
58static int s2mps11_clk_prepare(struct clk_hw *hw)
59{
60 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
61 int ret;
62
Krzysztof Kozlowski1b1ccee2013-12-11 15:07:43 +010063 ret = regmap_update_bits(s2mps11->iodev->regmap_pmic,
Tushar Behera64d64c32013-12-26 15:48:58 +053064 s2mps11->reg,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053065 s2mps11->mask, s2mps11->mask);
66 if (!ret)
67 s2mps11->enabled = true;
68
69 return ret;
70}
71
72static void s2mps11_clk_unprepare(struct clk_hw *hw)
73{
74 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
75 int ret;
76
Tushar Behera64d64c32013-12-26 15:48:58 +053077 ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053078 s2mps11->mask, ~s2mps11->mask);
79
80 if (!ret)
81 s2mps11->enabled = false;
82}
83
84static int s2mps11_clk_is_enabled(struct clk_hw *hw)
85{
86 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
87
88 return s2mps11->enabled;
89}
90
91static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
92 unsigned long parent_rate)
93{
94 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
95 if (s2mps11->enabled)
96 return 32768;
97 else
98 return 0;
99}
100
101static struct clk_ops s2mps11_clk_ops = {
102 .prepare = s2mps11_clk_prepare,
103 .unprepare = s2mps11_clk_unprepare,
104 .is_enabled = s2mps11_clk_is_enabled,
105 .recalc_rate = s2mps11_clk_recalc_rate,
106};
107
108static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
109 [S2MPS11_CLK_AP] = {
110 .name = "s2mps11_ap",
111 .ops = &s2mps11_clk_ops,
112 .flags = CLK_IS_ROOT,
113 },
114 [S2MPS11_CLK_CP] = {
115 .name = "s2mps11_cp",
116 .ops = &s2mps11_clk_ops,
117 .flags = CLK_IS_ROOT,
118 },
119 [S2MPS11_CLK_BT] = {
120 .name = "s2mps11_bt",
121 .ops = &s2mps11_clk_ops,
122 .flags = CLK_IS_ROOT,
123 },
124};
125
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200126static struct clk_init_data s2mps14_clks_init[S2MPS11_CLKS_NUM] = {
127 [S2MPS11_CLK_AP] = {
128 .name = "s2mps14_ap",
129 .ops = &s2mps11_clk_ops,
130 .flags = CLK_IS_ROOT,
131 },
132 [S2MPS11_CLK_BT] = {
133 .name = "s2mps14_bt",
134 .ops = &s2mps11_clk_ops,
135 .flags = CLK_IS_ROOT,
136 },
137};
138
139static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
140 struct clk_init_data *clks_init)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530141{
142 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
143 struct device_node *clk_np;
144 int i;
145
146 if (!iodev->dev->of_node)
Krzysztof Kozlowski238e1402014-03-21 13:18:17 +0100147 return ERR_PTR(-EINVAL);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530148
Krzysztof Kozlowskiafbd1a02014-03-21 13:39:20 +0100149 clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530150 if (!clk_np) {
151 dev_err(&pdev->dev, "could not find clock sub-node\n");
152 return ERR_PTR(-EINVAL);
153 }
154
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200155 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
156 if (!clks_init[i].name)
157 continue; /* Skip clocks not present in some devices */
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530158 of_property_read_string_index(clk_np, "clock-output-names", i,
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200159 &clks_init[i].name);
160 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530161
162 return clk_np;
163}
164
165static int s2mps11_clk_probe(struct platform_device *pdev)
166{
167 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
168 struct s2mps11_clk *s2mps11_clks, *s2mps11_clk;
Tushar Behera64d64c32013-12-26 15:48:58 +0530169 unsigned int s2mps11_reg;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200170 struct clk_init_data *clks_init;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530171 int i, ret = 0;
172 u32 val;
173
174 s2mps11_clks = devm_kzalloc(&pdev->dev, sizeof(*s2mps11_clk) *
175 S2MPS11_CLKS_NUM, GFP_KERNEL);
176 if (!s2mps11_clks)
177 return -ENOMEM;
178
179 s2mps11_clk = s2mps11_clks;
180
Krzysztof Kozlowski70024832014-05-21 13:23:00 +0200181 clk_table = devm_kzalloc(&pdev->dev, sizeof(struct clk *) *
182 S2MPS11_CLKS_NUM, GFP_KERNEL);
183 if (!clk_table)
184 return -ENOMEM;
185
Tushar Behera64d64c32013-12-26 15:48:58 +0530186 switch(platform_get_device_id(pdev)->driver_data) {
187 case S2MPS11X:
188 s2mps11_reg = S2MPS11_REG_RTC_CTRL;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200189 clks_init = s2mps11_clks_init;
190 break;
191 case S2MPS14X:
192 s2mps11_reg = S2MPS14_REG_RTCCTRL;
193 clks_init = s2mps14_clks_init;
Tushar Behera64d64c32013-12-26 15:48:58 +0530194 break;
Tushar Beherae8e6b842013-12-26 15:48:59 +0530195 case S5M8767X:
196 s2mps11_reg = S5M8767_REG_CTRL1;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200197 clks_init = s2mps11_clks_init;
Tushar Beherae8e6b842013-12-26 15:48:59 +0530198 break;
Tushar Behera64d64c32013-12-26 15:48:58 +0530199 default:
200 dev_err(&pdev->dev, "Invalid device type\n");
201 return -EINVAL;
202 };
203
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200204 /* Store clocks of_node in first element of s2mps11_clks array */
205 s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev, clks_init);
206 if (IS_ERR(s2mps11_clks->clk_np))
207 return PTR_ERR(s2mps11_clks->clk_np);
208
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530209 for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) {
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200210 if (!clks_init[i].name)
211 continue; /* Skip clocks not present in some devices */
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530212 s2mps11_clk->iodev = iodev;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200213 s2mps11_clk->hw.init = &clks_init[i];
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530214 s2mps11_clk->mask = 1 << i;
Tushar Behera64d64c32013-12-26 15:48:58 +0530215 s2mps11_clk->reg = s2mps11_reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530216
Krzysztof Kozlowski1b1ccee2013-12-11 15:07:43 +0100217 ret = regmap_read(s2mps11_clk->iodev->regmap_pmic,
Tushar Behera64d64c32013-12-26 15:48:58 +0530218 s2mps11_clk->reg, &val);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530219 if (ret < 0)
220 goto err_reg;
221
222 s2mps11_clk->enabled = val & s2mps11_clk->mask;
223
224 s2mps11_clk->clk = devm_clk_register(&pdev->dev,
225 &s2mps11_clk->hw);
226 if (IS_ERR(s2mps11_clk->clk)) {
227 dev_err(&pdev->dev, "Fail to register : %s\n",
228 s2mps11_name(s2mps11_clk));
229 ret = PTR_ERR(s2mps11_clk->clk);
230 goto err_reg;
231 }
232
233 s2mps11_clk->lookup = devm_kzalloc(&pdev->dev,
234 sizeof(struct clk_lookup), GFP_KERNEL);
235 if (!s2mps11_clk->lookup) {
236 ret = -ENOMEM;
237 goto err_lup;
238 }
239
240 s2mps11_clk->lookup->con_id = s2mps11_name(s2mps11_clk);
241 s2mps11_clk->lookup->clk = s2mps11_clk->clk;
242
243 clkdev_add(s2mps11_clk->lookup);
244 }
245
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200246 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
247 /* Skip clocks not present on S2MPS14 */
248 if (!clks_init[i].name)
249 continue;
Krzysztof Kozlowski70024832014-05-21 13:23:00 +0200250 clk_table[i] = s2mps11_clks[i].clk;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200251 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530252
Krzysztof Kozlowski70024832014-05-21 13:23:00 +0200253 clk_data.clks = clk_table;
254 clk_data.clk_num = S2MPS11_CLKS_NUM;
255 of_clk_add_provider(s2mps11_clks->clk_np, of_clk_src_onecell_get,
256 &clk_data);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530257
258 platform_set_drvdata(pdev, s2mps11_clks);
259
260 return ret;
261err_lup:
262 devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
263err_reg:
264 while (s2mps11_clk > s2mps11_clks) {
265 if (s2mps11_clk->lookup) {
266 clkdev_drop(s2mps11_clk->lookup);
267 devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
268 }
269 s2mps11_clk--;
270 }
271
272 return ret;
273}
274
275static int s2mps11_clk_remove(struct platform_device *pdev)
276{
277 struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
278 int i;
279
Krzysztof Kozlowskibf416bd2014-05-21 13:22:59 +0200280 of_clk_del_provider(s2mps11_clks[0].clk_np);
281 /* Drop the reference obtained in s2mps11_clk_parse_dt */
282 of_node_put(s2mps11_clks[0].clk_np);
283
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200284 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
285 /* Skip clocks not present on S2MPS14 */
286 if (!s2mps11_clks[i].lookup)
287 continue;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530288 clkdev_drop(s2mps11_clks[i].lookup);
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200289 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530290
291 return 0;
292}
293
294static const struct platform_device_id s2mps11_clk_id[] = {
Tushar Behera64d64c32013-12-26 15:48:58 +0530295 { "s2mps11-clk", S2MPS11X},
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200296 { "s2mps14-clk", S2MPS14X},
Tushar Beherae8e6b842013-12-26 15:48:59 +0530297 { "s5m8767-clk", S5M8767X},
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530298 { },
299};
300MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
301
302static struct platform_driver s2mps11_clk_driver = {
303 .driver = {
304 .name = "s2mps11-clk",
305 .owner = THIS_MODULE,
306 },
307 .probe = s2mps11_clk_probe,
308 .remove = s2mps11_clk_remove,
309 .id_table = s2mps11_clk_id,
310};
311
312static int __init s2mps11_clk_init(void)
313{
314 return platform_driver_register(&s2mps11_clk_driver);
315}
316subsys_initcall(s2mps11_clk_init);
317
318static void __init s2mps11_clk_cleanup(void)
319{
320 platform_driver_unregister(&s2mps11_clk_driver);
321}
322module_exit(s2mps11_clk_cleanup);
323
324MODULE_DESCRIPTION("S2MPS11 Clock Driver");
325MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
326MODULE_LICENSE("GPL");