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