blob: fbaa84a33c46c4e5b0160c154c1b3fd9a30b1788 [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>
Chanwoo Choif928b532014-11-18 17:59:41 +090026#include <linux/mfd/samsung/s2mps13.h>
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +020027#include <linux/mfd/samsung/s2mps14.h>
Tushar Beherae8e6b842013-12-26 15:48:59 +053028#include <linux/mfd/samsung/s5m8767.h>
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053029#include <linux/mfd/samsung/core.h>
30
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053031enum {
32 S2MPS11_CLK_AP = 0,
33 S2MPS11_CLK_CP,
34 S2MPS11_CLK_BT,
35 S2MPS11_CLKS_NUM,
36};
37
38struct s2mps11_clk {
39 struct sec_pmic_dev *iodev;
Krzysztof Kozlowskibf416bd2014-05-21 13:22:59 +020040 struct device_node *clk_np;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053041 struct clk_hw hw;
42 struct clk *clk;
43 struct clk_lookup *lookup;
44 u32 mask;
Tushar Behera64d64c32013-12-26 15:48:58 +053045 unsigned int reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053046};
47
48static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
49{
50 return container_of(hw, struct s2mps11_clk, hw);
51}
52
53static int s2mps11_clk_prepare(struct clk_hw *hw)
54{
55 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053056
Vaibhav Hiremath7764d0c2015-07-22 15:04:53 +053057 return regmap_update_bits(s2mps11->iodev->regmap_pmic,
Tushar Behera64d64c32013-12-26 15:48:58 +053058 s2mps11->reg,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053059 s2mps11->mask, s2mps11->mask);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053060}
61
62static void s2mps11_clk_unprepare(struct clk_hw *hw)
63{
64 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053065
Vaibhav Hiremath7764d0c2015-07-22 15:04:53 +053066 regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053067 s2mps11->mask, ~s2mps11->mask);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053068}
69
Karol Wrona89ed7e62014-07-01 19:13:59 +020070static int s2mps11_clk_is_prepared(struct clk_hw *hw)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053071{
Karol Wrona89ed7e62014-07-01 19:13:59 +020072 int ret;
73 u32 val;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053074 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
75
Karol Wrona89ed7e62014-07-01 19:13:59 +020076 ret = regmap_read(s2mps11->iodev->regmap_pmic,
77 s2mps11->reg, &val);
78 if (ret < 0)
79 return -EINVAL;
80
81 return val & s2mps11->mask;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053082}
83
84static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
85 unsigned long parent_rate)
86{
Karol Wrona89ed7e62014-07-01 19:13:59 +020087 return 32768;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053088}
89
90static struct clk_ops s2mps11_clk_ops = {
91 .prepare = s2mps11_clk_prepare,
92 .unprepare = s2mps11_clk_unprepare,
Karol Wrona89ed7e62014-07-01 19:13:59 +020093 .is_prepared = s2mps11_clk_is_prepared,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053094 .recalc_rate = s2mps11_clk_recalc_rate,
95};
96
Andi Shyti31ad0e2a2016-01-20 19:14:22 +090097/* This s2mps11_clks_init tructure is common to s2mps11, s2mps13 and s2mps14 */
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +053098static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
99 [S2MPS11_CLK_AP] = {
100 .name = "s2mps11_ap",
101 .ops = &s2mps11_clk_ops,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530102 },
103 [S2MPS11_CLK_CP] = {
104 .name = "s2mps11_cp",
105 .ops = &s2mps11_clk_ops,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530106 },
107 [S2MPS11_CLK_BT] = {
108 .name = "s2mps11_bt",
109 .ops = &s2mps11_clk_ops,
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530110 },
111};
112
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200113static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
114 struct clk_init_data *clks_init)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530115{
116 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
117 struct device_node *clk_np;
118 int i;
119
120 if (!iodev->dev->of_node)
Krzysztof Kozlowski238e1402014-03-21 13:18:17 +0100121 return ERR_PTR(-EINVAL);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530122
Krzysztof Kozlowskiafbd1a02014-03-21 13:39:20 +0100123 clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530124 if (!clk_np) {
125 dev_err(&pdev->dev, "could not find clock sub-node\n");
126 return ERR_PTR(-EINVAL);
127 }
128
Andi Shyti31ad0e2a2016-01-20 19:14:22 +0900129 for (i = 0; i < S2MPS11_CLKS_NUM; i++)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530130 of_property_read_string_index(clk_np, "clock-output-names", i,
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200131 &clks_init[i].name);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530132
133 return clk_np;
134}
135
136static int s2mps11_clk_probe(struct platform_device *pdev)
137{
138 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Andi Shytidedbb272016-01-20 19:14:24 +0900139 struct s2mps11_clk *s2mps11_clks;
Stephen Boydb228fad2016-06-01 16:15:24 -0700140 struct clk_hw_onecell_data *clk_data;
Tushar Behera64d64c32013-12-26 15:48:58 +0530141 unsigned int s2mps11_reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530142 int i, ret = 0;
Andi Shyti31ad0e2a2016-01-20 19:14:22 +0900143 enum sec_device_type hwid = platform_get_device_id(pdev)->driver_data;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530144
Vaibhav Hiremath7764d0c2015-07-22 15:04:53 +0530145 s2mps11_clks = devm_kcalloc(&pdev->dev, S2MPS11_CLKS_NUM,
Andi Shytidedbb272016-01-20 19:14:24 +0900146 sizeof(*s2mps11_clks), GFP_KERNEL);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530147 if (!s2mps11_clks)
148 return -ENOMEM;
149
Stephen Boydb228fad2016-06-01 16:15:24 -0700150 clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data) +
151 sizeof(*clk_data->hws) * S2MPS11_CLKS_NUM,
152 GFP_KERNEL);
Andi Shyti36da4de2016-01-20 19:14:23 +0900153 if (!clk_data)
154 return -ENOMEM;
155
Andi Shyti31ad0e2a2016-01-20 19:14:22 +0900156 switch (hwid) {
Tushar Behera64d64c32013-12-26 15:48:58 +0530157 case S2MPS11X:
158 s2mps11_reg = S2MPS11_REG_RTC_CTRL;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200159 break;
Chanwoo Choif928b532014-11-18 17:59:41 +0900160 case S2MPS13X:
161 s2mps11_reg = S2MPS13_REG_RTCCTRL;
Chanwoo Choif928b532014-11-18 17:59:41 +0900162 break;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200163 case S2MPS14X:
164 s2mps11_reg = S2MPS14_REG_RTCCTRL;
Tushar Behera64d64c32013-12-26 15:48:58 +0530165 break;
Tushar Beherae8e6b842013-12-26 15:48:59 +0530166 case S5M8767X:
167 s2mps11_reg = S5M8767_REG_CTRL1;
168 break;
Tushar Behera64d64c32013-12-26 15:48:58 +0530169 default:
170 dev_err(&pdev->dev, "Invalid device type\n");
171 return -EINVAL;
kbuild test robot250d07d2014-11-26 07:45:12 +0800172 }
Tushar Behera64d64c32013-12-26 15:48:58 +0530173
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200174 /* Store clocks of_node in first element of s2mps11_clks array */
Andi Shyti31ad0e2a2016-01-20 19:14:22 +0900175 s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev, s2mps11_clks_init);
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200176 if (IS_ERR(s2mps11_clks->clk_np))
177 return PTR_ERR(s2mps11_clks->clk_np);
178
Andi Shytidedbb272016-01-20 19:14:24 +0900179 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
Andi Shyti31ad0e2a2016-01-20 19:14:22 +0900180 if (i == S2MPS11_CLK_CP && hwid == S2MPS14X)
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200181 continue; /* Skip clocks not present in some devices */
Andi Shytidedbb272016-01-20 19:14:24 +0900182 s2mps11_clks[i].iodev = iodev;
183 s2mps11_clks[i].hw.init = &s2mps11_clks_init[i];
184 s2mps11_clks[i].mask = 1 << i;
185 s2mps11_clks[i].reg = s2mps11_reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530186
Andi Shytidedbb272016-01-20 19:14:24 +0900187 s2mps11_clks[i].clk = devm_clk_register(&pdev->dev,
188 &s2mps11_clks[i].hw);
189 if (IS_ERR(s2mps11_clks[i].clk)) {
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530190 dev_err(&pdev->dev, "Fail to register : %s\n",
Andi Shytidedbb272016-01-20 19:14:24 +0900191 s2mps11_clks_init[i].name);
192 ret = PTR_ERR(s2mps11_clks[i].clk);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530193 goto err_reg;
194 }
195
Stephen Boydb228fad2016-06-01 16:15:24 -0700196 s2mps11_clks[i].lookup = clkdev_hw_create(&s2mps11_clks[i].hw,
Andi Shytidedbb272016-01-20 19:14:24 +0900197 s2mps11_clks_init[i].name, NULL);
198 if (!s2mps11_clks[i].lookup) {
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530199 ret = -ENOMEM;
Axel Lin264e3b72015-07-16 21:59:43 +0800200 goto err_reg;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530201 }
Stephen Boydb228fad2016-06-01 16:15:24 -0700202 clk_data->hws[i] = &s2mps11_clks[i].hw;
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200203 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530204
Stephen Boydb228fad2016-06-01 16:15:24 -0700205 clk_data->num = S2MPS11_CLKS_NUM;
206 of_clk_add_hw_provider(s2mps11_clks->clk_np, of_clk_hw_onecell_get,
207 clk_data);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530208
209 platform_set_drvdata(pdev, s2mps11_clks);
210
211 return ret;
Axel Lin264e3b72015-07-16 21:59:43 +0800212
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530213err_reg:
Axel Lin264e3b72015-07-16 21:59:43 +0800214 while (--i >= 0)
215 clkdev_drop(s2mps11_clks[i].lookup);
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530216
217 return ret;
218}
219
220static int s2mps11_clk_remove(struct platform_device *pdev)
221{
222 struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
223 int i;
224
Krzysztof Kozlowskibf416bd2014-05-21 13:22:59 +0200225 of_clk_del_provider(s2mps11_clks[0].clk_np);
226 /* Drop the reference obtained in s2mps11_clk_parse_dt */
227 of_node_put(s2mps11_clks[0].clk_np);
228
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200229 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
230 /* Skip clocks not present on S2MPS14 */
231 if (!s2mps11_clks[i].lookup)
232 continue;
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530233 clkdev_drop(s2mps11_clks[i].lookup);
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200234 }
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530235
236 return 0;
237}
238
239static const struct platform_device_id s2mps11_clk_id[] = {
Tushar Behera64d64c32013-12-26 15:48:58 +0530240 { "s2mps11-clk", S2MPS11X},
Chanwoo Choif928b532014-11-18 17:59:41 +0900241 { "s2mps13-clk", S2MPS13X},
Krzysztof Kozlowskie8b60a42014-05-21 13:23:01 +0200242 { "s2mps14-clk", S2MPS14X},
Tushar Beherae8e6b842013-12-26 15:48:59 +0530243 { "s5m8767-clk", S5M8767X},
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530244 { },
245};
246MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
247
248static struct platform_driver s2mps11_clk_driver = {
249 .driver = {
250 .name = "s2mps11-clk",
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530251 },
252 .probe = s2mps11_clk_probe,
253 .remove = s2mps11_clk_remove,
254 .id_table = s2mps11_clk_id,
255};
256
257static int __init s2mps11_clk_init(void)
258{
259 return platform_driver_register(&s2mps11_clk_driver);
260}
261subsys_initcall(s2mps11_clk_init);
262
Vaibhav Hiremath7764d0c2015-07-22 15:04:53 +0530263static void __exit s2mps11_clk_cleanup(void)
Yadwinder Singh Brar7cc560d2013-07-07 17:14:20 +0530264{
265 platform_driver_unregister(&s2mps11_clk_driver);
266}
267module_exit(s2mps11_clk_cleanup);
268
269MODULE_DESCRIPTION("S2MPS11 Clock Driver");
270MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
271MODULE_LICENSE("GPL");