Linux-libre 3.16.85-gnu
[librecmc/linux-libre.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 = clkdev_alloc(s2mps11_clk->clk,
234                                         s2mps11_name(s2mps11_clk), NULL);
235                 if (!s2mps11_clk->lookup) {
236                         ret = -ENOMEM;
237                         goto err_lup;
238                 }
239
240                 clkdev_add(s2mps11_clk->lookup);
241         }
242
243         for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
244                 /* Skip clocks not present on S2MPS14 */
245                 if (!clks_init[i].name)
246                         continue;
247                 clk_table[i] = s2mps11_clks[i].clk;
248         }
249
250         clk_data.clks = clk_table;
251         clk_data.clk_num = S2MPS11_CLKS_NUM;
252         of_clk_add_provider(s2mps11_clks->clk_np, of_clk_src_onecell_get,
253                         &clk_data);
254
255         platform_set_drvdata(pdev, s2mps11_clks);
256
257         return ret;
258 err_lup:
259         devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
260 err_reg:
261         while (s2mps11_clk > s2mps11_clks) {
262                 if (s2mps11_clk->lookup) {
263                         clkdev_drop(s2mps11_clk->lookup);
264                         devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
265                 }
266                 s2mps11_clk--;
267         }
268
269         return ret;
270 }
271
272 static int s2mps11_clk_remove(struct platform_device *pdev)
273 {
274         struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
275         int i;
276
277         of_clk_del_provider(s2mps11_clks[0].clk_np);
278         /* Drop the reference obtained in s2mps11_clk_parse_dt */
279         of_node_put(s2mps11_clks[0].clk_np);
280
281         for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
282                 /* Skip clocks not present on S2MPS14 */
283                 if (!s2mps11_clks[i].lookup)
284                         continue;
285                 clkdev_drop(s2mps11_clks[i].lookup);
286         }
287
288         return 0;
289 }
290
291 static const struct platform_device_id s2mps11_clk_id[] = {
292         { "s2mps11-clk", S2MPS11X},
293         { "s2mps14-clk", S2MPS14X},
294         { "s5m8767-clk", S5M8767X},
295         { },
296 };
297 MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
298
299 #ifdef CONFIG_OF
300 /*
301  * Device is instantiated through parent MFD device and device matching is done
302  * through platform_device_id.
303  *
304  * However if device's DT node contains proper clock compatible and driver is
305  * built as a module, then the *module* matching will be done trough DT aliases.
306  * This requires of_device_id table.  In the same time this will not change the
307  * actual *device* matching so do not add .of_match_table.
308  */
309 static const struct of_device_id s2mps11_dt_match[] __used = {
310         {
311                 .compatible = "samsung,s2mps11-clk",
312                 .data = (void *)S2MPS11X,
313         }, {
314                 .compatible = "samsung,s2mps14-clk",
315                 .data = (void *)S2MPS14X,
316         }, {
317                 .compatible = "samsung,s5m8767-clk",
318                 .data = (void *)S5M8767X,
319         }, {
320                 /* Sentinel */
321         },
322 };
323 MODULE_DEVICE_TABLE(of, s2mps11_dt_match);
324 #endif
325
326 static struct platform_driver s2mps11_clk_driver = {
327         .driver = {
328                 .name  = "s2mps11-clk",
329                 .owner = THIS_MODULE,
330         },
331         .probe = s2mps11_clk_probe,
332         .remove = s2mps11_clk_remove,
333         .id_table = s2mps11_clk_id,
334 };
335
336 static int __init s2mps11_clk_init(void)
337 {
338         return platform_driver_register(&s2mps11_clk_driver);
339 }
340 subsys_initcall(s2mps11_clk_init);
341
342 static void __init s2mps11_clk_cleanup(void)
343 {
344         platform_driver_unregister(&s2mps11_clk_driver);
345 }
346 module_exit(s2mps11_clk_cleanup);
347
348 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
349 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
350 MODULE_LICENSE("GPL");