2 * clk-s2mps11.c - Clock driver for S2MPS11.
4 * Copyright (C) 2013 Samsung Electornics
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/err.h>
25 #include <linux/clkdev.h>
26 #include <linux/regmap.h>
27 #include <linux/clk-provider.h>
28 #include <linux/platform_device.h>
29 #include <linux/mfd/samsung/s2mps11.h>
30 #include <linux/mfd/samsung/core.h>
32 #define s2mps11_name(a) (a->hw.init->name)
34 static struct clk **clk_table;
35 static struct clk_onecell_data clk_data;
45 struct sec_pmic_dev *iodev;
48 struct clk_lookup *lookup;
53 static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
55 return container_of(hw, struct s2mps11_clk, hw);
58 static int s2mps11_clk_prepare(struct clk_hw *hw)
60 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
63 ret = regmap_update_bits(s2mps11->iodev->regmap_pmic,
65 s2mps11->mask, s2mps11->mask);
67 s2mps11->enabled = true;
72 static void s2mps11_clk_unprepare(struct clk_hw *hw)
74 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
77 ret = regmap_update_bits(s2mps11->iodev->regmap_pmic, S2MPS11_REG_RTC_CTRL,
78 s2mps11->mask, ~s2mps11->mask);
81 s2mps11->enabled = false;
84 static int s2mps11_clk_is_enabled(struct clk_hw *hw)
86 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
88 return s2mps11->enabled;
91 static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
92 unsigned long parent_rate)
94 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
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,
108 static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
110 .name = "s2mps11_ap",
111 .ops = &s2mps11_clk_ops,
112 .flags = CLK_IS_ROOT,
115 .name = "s2mps11_cp",
116 .ops = &s2mps11_clk_ops,
117 .flags = CLK_IS_ROOT,
120 .name = "s2mps11_bt",
121 .ops = &s2mps11_clk_ops,
122 .flags = CLK_IS_ROOT,
126 static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev)
128 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
129 struct device_node *clk_np;
132 if (!iodev->dev->of_node)
135 clk_np = of_find_node_by_name(iodev->dev->of_node, "clocks");
137 dev_err(&pdev->dev, "could not find clock sub-node\n");
138 return ERR_PTR(-EINVAL);
141 clk_table = devm_kzalloc(&pdev->dev, sizeof(struct clk *) *
142 S2MPS11_CLKS_NUM, GFP_KERNEL);
144 return ERR_PTR(-ENOMEM);
146 for (i = 0; i < S2MPS11_CLKS_NUM; i++)
147 of_property_read_string_index(clk_np, "clock-output-names", i,
148 &s2mps11_clks_init[i].name);
153 static int s2mps11_clk_probe(struct platform_device *pdev)
155 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
156 struct s2mps11_clk *s2mps11_clks, *s2mps11_clk;
157 struct device_node *clk_np = NULL;
161 s2mps11_clks = devm_kzalloc(&pdev->dev, sizeof(*s2mps11_clk) *
162 S2MPS11_CLKS_NUM, GFP_KERNEL);
166 s2mps11_clk = s2mps11_clks;
168 clk_np = s2mps11_clk_parse_dt(pdev);
170 return PTR_ERR(clk_np);
172 for (i = 0; i < S2MPS11_CLKS_NUM; i++, s2mps11_clk++) {
173 s2mps11_clk->iodev = iodev;
174 s2mps11_clk->hw.init = &s2mps11_clks_init[i];
175 s2mps11_clk->mask = 1 << i;
177 ret = regmap_read(s2mps11_clk->iodev->regmap_pmic,
178 S2MPS11_REG_RTC_CTRL, &val);
182 s2mps11_clk->enabled = val & s2mps11_clk->mask;
184 s2mps11_clk->clk = devm_clk_register(&pdev->dev,
186 if (IS_ERR(s2mps11_clk->clk)) {
187 dev_err(&pdev->dev, "Fail to register : %s\n",
188 s2mps11_name(s2mps11_clk));
189 ret = PTR_ERR(s2mps11_clk->clk);
193 s2mps11_clk->lookup = devm_kzalloc(&pdev->dev,
194 sizeof(struct clk_lookup), GFP_KERNEL);
195 if (!s2mps11_clk->lookup) {
200 s2mps11_clk->lookup->con_id = s2mps11_name(s2mps11_clk);
201 s2mps11_clk->lookup->clk = s2mps11_clk->clk;
203 clkdev_add(s2mps11_clk->lookup);
207 for (i = 0; i < S2MPS11_CLKS_NUM; i++)
208 clk_table[i] = s2mps11_clks[i].clk;
210 clk_data.clks = clk_table;
211 clk_data.clk_num = S2MPS11_CLKS_NUM;
212 of_clk_add_provider(clk_np, of_clk_src_onecell_get, &clk_data);
215 platform_set_drvdata(pdev, s2mps11_clks);
219 devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
221 while (s2mps11_clk > s2mps11_clks) {
222 if (s2mps11_clk->lookup) {
223 clkdev_drop(s2mps11_clk->lookup);
224 devm_clk_unregister(&pdev->dev, s2mps11_clk->clk);
232 static int s2mps11_clk_remove(struct platform_device *pdev)
234 struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
237 for (i = 0; i < S2MPS11_CLKS_NUM; i++)
238 clkdev_drop(s2mps11_clks[i].lookup);
243 static const struct platform_device_id s2mps11_clk_id[] = {
247 MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
249 static struct platform_driver s2mps11_clk_driver = {
251 .name = "s2mps11-clk",
252 .owner = THIS_MODULE,
254 .probe = s2mps11_clk_probe,
255 .remove = s2mps11_clk_remove,
256 .id_table = s2mps11_clk_id,
259 static int __init s2mps11_clk_init(void)
261 return platform_driver_register(&s2mps11_clk_driver);
263 subsys_initcall(s2mps11_clk_init);
265 static void __init s2mps11_clk_cleanup(void)
267 platform_driver_unregister(&s2mps11_clk_driver);
269 module_exit(s2mps11_clk_cleanup);
271 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
272 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
273 MODULE_LICENSE("GPL");