blob: b7797fb12e1254d5e9e8a2214b11286b21e991eb [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;
Tushar Behera64d64c32013-12-26 15:48:58 +053049 unsigned int reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053050};
51
52static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
53{
54 return container_of(hw, struct s2mps11_clk, hw);
55}
56
57static int s2mps11_clk_prepare(struct clk_hw *hw)
58{
59 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
60 int ret;
61
Krzysztof Kozlowski1b1ccee2013-12-11 15:07:43 +010062 ret = regmap_update_bits(s2mps11->iodev->regmap_pmic,
Tushar Behera64d64c32013-12-26 15:48:58 +053063 s2mps11->reg,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053064 s2mps11->mask, s2mps11->mask);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053065
66 return ret;
67}
68
69static void s2mps11_clk_unprepare(struct clk_hw *hw)
70{
71 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
72 int ret;
73
Tushar Behera64d64c32013-12-26 15:48:58 +053074 ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053075 s2mps11->mask, ~s2mps11->mask);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053076}
77
Karol Wrona89ed7e62014-07-01 19:13:59 +020078static int s2mps11_clk_is_prepared(struct clk_hw *hw)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053079{
Karol Wrona89ed7e62014-07-01 19:13:59 +020080 int ret;
81 u32 val;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053082 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
83
Karol Wrona89ed7e62014-07-01 19:13:59 +020084 ret = regmap_read(s2mps11->iodev->regmap_pmic,
85 s2mps11->reg, &val);
86 if (ret < 0)
87 return -EINVAL;
88
89 return val & s2mps11->mask;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053090}
91
92static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
93 unsigned long parent_rate)
94{
Karol Wrona89ed7e62014-07-01 19:13:59 +020095 return 32768;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053096}
97
98static struct clk_ops s2mps11_clk_ops = {
99 .prepare = s2mps11_clk_prepare,
100 .unprepare = s2mps11_clk_unprepare,
Karol Wrona89ed7e62014-07-01 19:13:59 +0200101 .is_prepared = s2mps11_clk_is_prepared,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530102 .recalc_rate = s2mps11_clk_recalc_rate,
103};
104
105static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
106 [S2MPS11_CLK_AP] = {
107 .name = "s2mps11_ap",
108 .ops = &s2mps11_clk_ops,
109 .flags = CLK_IS_ROOT,
110 },
111 [S2MPS11_CLK_CP] = {
112 .name = "s2mps11_cp",
113 .ops = &s2mps11_clk_ops,
114 .flags = CLK_IS_ROOT,
115 },
116 [S2MPS11_CLK_BT] = {
117 .name = "s2mps11_bt",
118 .ops = &s2mps11_clk_ops,
119 .flags = CLK_IS_ROOT,
120 },
121};
122
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200123static struct clk_init_data s2mps14_clks_init[S2MPS11_CLKS_NUM] = {
124 [S2MPS11_CLK_AP] = {
125 .name = "s2mps14_ap",
126 .ops = &s2mps11_clk_ops,
127 .flags = CLK_IS_ROOT,
128 },
129 [S2MPS11_CLK_BT] = {
130 .name = "s2mps14_bt",
131 .ops = &s2mps11_clk_ops,
132 .flags = CLK_IS_ROOT,
133 },
134};
135
136static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
137 struct clk_init_data *clks_init)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530138{
139 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
140 struct device_node *clk_np;
141 int i;
142
143 if (!iodev->dev->of_node)
Krzysztof Kozlowski238e1402014-03-21 13:18:17 +0100144 return ERR_PTR(-EINVAL);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530145
Krzysztof Kozlowskiafbd1a02014-03-21 13:39:20 +0100146 clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530147 if (!clk_np) {
148 dev_err(&pdev->dev, "could not find clock sub-node\n");
149 return ERR_PTR(-EINVAL);
150 }
151
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200152 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
153 if (!clks_init[i].name)
154 continue; /* Skip clocks not present in some devices */
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530155 of_property_read_string_index(clk_np, "clock-output-names", i,
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200156 &clks_init[i].name);
157 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530158
159 return clk_np;
160}
161
162static int s2mps11_clk_probe(struct platform_device *pdev)
163{
164 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
165 struct s2mps11_clk *s2mps11_clks, *s2mps11_clk;
Tushar Behera64d64c32013-12-26 15:48:58 +0530166 unsigned int s2mps11_reg;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200167 struct clk_init_data *clks_init;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530168 int i, ret = 0;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530169
170 s2mps11_clks = devm_kzalloc(&pdev->dev, sizeof(*s2mps11_clk) *
171 S2MPS11_CLKS_NUM, GFP_KERNEL);
172 if (!s2mps11_clks)
173 return -ENOMEM;
174
175 s2mps11_clk = s2mps11_clks;
176
Krzysztof Kozlowski70024832014-05-21 13:23:00 +0200177 clk_table = devm_kzalloc(&pdev->dev, sizeof(struct clk *) *
178 S2MPS11_CLKS_NUM, GFP_KERNEL);
179 if (!clk_table)
180 return -ENOMEM;
181
Tushar Behera64d64c32013-12-26 15:48:58 +0530182 switch(platform_get_device_id(pdev)->driver_data) {
183 case S2MPS11X:
184 s2mps11_reg = S2MPS11_REG_RTC_CTRL;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200185 clks_init = s2mps11_clks_init;
186 break;
187 case S2MPS14X:
188 s2mps11_reg = S2MPS14_REG_RTCCTRL;
189 clks_init = s2mps14_clks_init;
Tushar Behera64d64c32013-12-26 15:48:58 +0530190 break;
Tushar Beherae8e6b842013-12-26 15:48:59 +0530191 case S5M8767X:
192 s2mps11_reg = S5M8767_REG_CTRL1;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200193 clks_init = s2mps11_clks_init;
Tushar Beherae8e6b842013-12-26 15:48:59 +0530194 break;
Tushar Behera64d64c32013-12-26 15:48:58 +0530195 default:
196 dev_err(&pdev->dev, "Invalid device type\n");
197 return -EINVAL;
198 };
199
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200200 /* Store clocks of_node in first element of s2mps11_clks array */
201 s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev, clks_init);
202 if (IS_ERR(s2mps11_clks->clk_np))
203 return PTR_ERR(s2mps11_clks->clk_np);
204
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530205 for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) {
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200206 if (!clks_init[i].name)
207 continue; /* Skip clocks not present in some devices */
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530208 s2mps11_clk->iodev = iodev;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200209 s2mps11_clk->hw.init = &clks_init[i];
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530210 s2mps11_clk->mask = 1 << i;
Tushar Behera64d64c32013-12-26 15:48:58 +0530211 s2mps11_clk->reg = s2mps11_reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530212
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530213 s2mps11_clk->clk = devm_clk_register(&pdev->dev,
214 &s2mps11_clk->hw);
215 if (IS_ERR(s2mps11_clk->clk)) {
216 dev_err(&pdev->dev, "Fail to register : %s\n",
217 s2mps11_name(s2mps11_clk));
218 ret = PTR_ERR(s2mps11_clk->clk);
219 goto err_reg;
220 }
221
Krzysztof Kozlowski2a96dfa2014-06-27 14:21:10 +0200222 s2mps11_clk->lookup = clkdev_alloc(s2mps11_clk->clk,
223 s2mps11_name(s2mps11_clk), NULL);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530224 if (!s2mps11_clk->lookup) {
225 ret = -ENOMEM;
226 goto err_lup;
227 }
228
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530229 clkdev_add(s2mps11_clk->lookup);
230 }
231
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200232 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
233 /* Skip clocks not present on S2MPS14 */
234 if (!clks_init[i].name)
235 continue;
Krzysztof Kozlowski70024832014-05-21 13:23:00 +0200236 clk_table[i] = s2mps11_clks[i].clk;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200237 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530238
Krzysztof Kozlowski70024832014-05-21 13:23:00 +0200239 clk_data.clks = clk_table;
240 clk_data.clk_num = S2MPS11_CLKS_NUM;
241 of_clk_add_provider(s2mps11_clks->clk_np, of_clk_src_onecell_get,
242 &clk_data);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530243
244 platform_set_drvdata(pdev, s2mps11_clks);
245
246 return ret;
247err_lup:
248 devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
249err_reg:
250 while (s2mps11_clk > s2mps11_clks) {
251 if (s2mps11_clk->lookup) {
252 clkdev_drop(s2mps11_clk->lookup);
253 devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
254 }
255 s2mps11_clk--;
256 }
257
258 return ret;
259}
260
261static int s2mps11_clk_remove(struct platform_device *pdev)
262{
263 struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
264 int i;
265
Krzysztof Kozlowskibf416bd2014-05-21 13:22:59 +0200266 of_clk_del_provider(s2mps11_clks[0].clk_np);
267 /* Drop the reference obtained in s2mps11_clk_parse_dt */
268 of_node_put(s2mps11_clks[0].clk_np);
269
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200270 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
271 /* Skip clocks not present on S2MPS14 */
272 if (!s2mps11_clks[i].lookup)
273 continue;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530274 clkdev_drop(s2mps11_clks[i].lookup);
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200275 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530276
277 return 0;
278}
279
280static const struct platform_device_id s2mps11_clk_id[] = {
Tushar Behera64d64c32013-12-26 15:48:58 +0530281 { "s2mps11-clk", S2MPS11X},
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200282 { "s2mps14-clk", S2MPS14X},
Tushar Beherae8e6b842013-12-26 15:48:59 +0530283 { "s5m8767-clk", S5M8767X},
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530284 { },
285};
286MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
287
288static struct platform_driver s2mps11_clk_driver = {
289 .driver = {
290 .name = "s2mps11-clk",
291 .owner = THIS_MODULE,
292 },
293 .probe = s2mps11_clk_probe,
294 .remove = s2mps11_clk_remove,
295 .id_table = s2mps11_clk_id,
296};
297
298static int __init s2mps11_clk_init(void)
299{
300 return platform_driver_register(&s2mps11_clk_driver);
301}
302subsys_initcall(s2mps11_clk_init);
303
304static void __init s2mps11_clk_cleanup(void)
305{
306 platform_driver_unregister(&s2mps11_clk_driver);
307}
308module_exit(s2mps11_clk_cleanup);
309
310MODULE_DESCRIPTION("S2MPS11 Clock Driver");
311MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
312MODULE_LICENSE("GPL");