regulator: da9211: Remove unnecessary devm_regulator_unregister() calls
[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/s2mps14.h>
27 #include <linux/mfd/samsung/s5m8767.h>
28 #include <linux/mfd/samsung/core.h>
29
30 #define s2mps11_name(a) (a->hw.init->name)
31
32 static struct clk **clk_table;
33 static struct clk_onecell_data clk_data;
34
35 enum {
36         S2MPS11_CLK_AP = 0,
37         S2MPS11_CLK_CP,
38         S2MPS11_CLK_BT,
39         S2MPS11_CLKS_NUM,
40 };
41
42 struct s2mps11_clk {
43         struct sec_pmic_dev *iodev;
44         struct device_node *clk_np;
45         struct clk_hw hw;
46         struct clk *clk;
47         struct clk_lookup *lookup;
48         u32 mask;
49         bool enabled;
50         unsigned int reg;
51 };
52
53 static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
54 {
55         return container_of(hw, struct s2mps11_clk, hw);
56 }
57
58 static int s2mps11_clk_prepare(struct clk_hw *hw)
59 {
60         struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
61         int ret;
62
63         ret = regmap_update_bits(s2mps11->iodev->regmap_pmic,
64                                  s2mps11->reg,
65                                  s2mps11->mask, s2mps11->mask);
66         if (!ret)
67                 s2mps11->enabled = true;
68
69         return ret;
70 }
71
72 static void s2mps11_clk_unprepare(struct clk_hw *hw)
73 {
74         struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
75         int ret;
76
77         ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
78                            s2mps11->mask, ~s2mps11->mask);
79
80         if (!ret)
81                 s2mps11->enabled = false;
82 }
83
84 static 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
91 static 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
101 static 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
108 static 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
126 static 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
139 static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
140                 struct clk_init_data *clks_init)
141 {
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)
147                 return ERR_PTR(-EINVAL);
148
149         clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
150         if (!clk_np) {
151                 dev_err(&pdev->dev, "could not find clock sub-node\n");
152                 return ERR_PTR(-EINVAL);
153         }
154
155         for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
156                 if (!clks_init[i].name)
157                         continue; /* Skip clocks not present in some devices */
158                 of_property_read_string_index(clk_np, "clock-output-names", i,
159                                 &clks_init[i].name);
160         }
161
162         return clk_np;
163 }
164
165 static 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;
169         unsigned int s2mps11_reg;
170         struct clk_init_data *clks_init;
171         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
181         clk_table = devm_kzalloc(&pdev->dev, sizeof(struct clk *) *
182                                  S2MPS11_CLKS_NUM, GFP_KERNEL);
183         if (!clk_table)
184                 return -ENOMEM;
185
186         switch(platform_get_device_id(pdev)->driver_data) {
187         case S2MPS11X:
188                 s2mps11_reg = S2MPS11_REG_RTC_CTRL;
189                 clks_init = s2mps11_clks_init;
190                 break;
191         case S2MPS14X:
192                 s2mps11_reg = S2MPS14_REG_RTCCTRL;
193                 clks_init = s2mps14_clks_init;
194                 break;
195         case S5M8767X:
196                 s2mps11_reg = S5M8767_REG_CTRL1;
197                 clks_init = s2mps11_clks_init;
198                 break;
199         default:
200                 dev_err(&pdev->dev, "Invalid device type\n");
201                 return -EINVAL;
202         };
203
204         /* 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
209         for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) {
210                 if (!clks_init[i].name)
211                         continue; /* Skip clocks not present in some devices */
212                 s2mps11_clk->iodev = iodev;
213                 s2mps11_clk->hw.init = &clks_init[i];
214                 s2mps11_clk->mask = 1 << i;
215                 s2mps11_clk->reg = s2mps11_reg;
216
217                 ret = regmap_read(s2mps11_clk->iodev->regmap_pmic,
218                                   s2mps11_clk->reg, &val);
219                 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
246         for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
247                 /* Skip clocks not present on S2MPS14 */
248                 if (!clks_init[i].name)
249                         continue;
250                 clk_table[i] = s2mps11_clks[i].clk;
251         }
252
253         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);
257
258         platform_set_drvdata(pdev, s2mps11_clks);
259
260         return ret;
261 err_lup:
262         devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
263 err_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
275 static int s2mps11_clk_remove(struct platform_device *pdev)
276 {
277         struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
278         int i;
279
280         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
284         for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
285                 /* Skip clocks not present on S2MPS14 */
286                 if (!s2mps11_clks[i].lookup)
287                         continue;
288                 clkdev_drop(s2mps11_clks[i].lookup);
289         }
290
291         return 0;
292 }
293
294 static const struct platform_device_id s2mps11_clk_id[] = {
295         { "s2mps11-clk", S2MPS11X},
296         { "s2mps14-clk", S2MPS14X},
297         { "s5m8767-clk", S5M8767X},
298         { },
299 };
300 MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
301
302 static 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
312 static int __init s2mps11_clk_init(void)
313 {
314         return platform_driver_register(&s2mps11_clk_driver);
315 }
316 subsys_initcall(s2mps11_clk_init);
317
318 static void __init s2mps11_clk_cleanup(void)
319 {
320         platform_driver_unregister(&s2mps11_clk_driver);
321 }
322 module_exit(s2mps11_clk_cleanup);
323
324 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
325 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
326 MODULE_LICENSE("GPL");