1 // SPDX-License-Identifier: GPL-2.0+
3 // clk-s2mps11.c - Clock driver for S2MPS11.
5 // Copyright (C) 2013,2014 Samsung Electornics
7 #include <linux/module.h>
10 #include <linux/clkdev.h>
11 #include <linux/regmap.h>
12 #include <linux/clk-provider.h>
13 #include <linux/platform_device.h>
14 #include <linux/mfd/samsung/s2mps11.h>
15 #include <linux/mfd/samsung/s2mps13.h>
16 #include <linux/mfd/samsung/s2mps14.h>
17 #include <linux/mfd/samsung/s5m8767.h>
18 #include <linux/mfd/samsung/core.h>
20 #include <dt-bindings/clock/samsung,s2mps11.h>
23 struct sec_pmic_dev *iodev;
24 struct device_node *clk_np;
27 struct clk_lookup *lookup;
32 static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
34 return container_of(hw, struct s2mps11_clk, hw);
37 static int s2mps11_clk_prepare(struct clk_hw *hw)
39 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
41 return regmap_update_bits(s2mps11->iodev->regmap_pmic,
43 s2mps11->mask, s2mps11->mask);
46 static void s2mps11_clk_unprepare(struct clk_hw *hw)
48 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
50 regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
51 s2mps11->mask, ~s2mps11->mask);
54 static int s2mps11_clk_is_prepared(struct clk_hw *hw)
58 struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
60 ret = regmap_read(s2mps11->iodev->regmap_pmic,
65 return val & s2mps11->mask;
68 static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
69 unsigned long parent_rate)
74 static const struct clk_ops s2mps11_clk_ops = {
75 .prepare = s2mps11_clk_prepare,
76 .unprepare = s2mps11_clk_unprepare,
77 .is_prepared = s2mps11_clk_is_prepared,
78 .recalc_rate = s2mps11_clk_recalc_rate,
81 /* This s2mps11_clks_init tructure is common to s2mps11, s2mps13 and s2mps14 */
82 static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
85 .ops = &s2mps11_clk_ops,
89 .ops = &s2mps11_clk_ops,
93 .ops = &s2mps11_clk_ops,
97 static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
98 struct clk_init_data *clks_init)
100 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
101 struct device_node *clk_np;
104 if (!iodev->dev->of_node)
105 return ERR_PTR(-EINVAL);
107 clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
109 dev_err(&pdev->dev, "could not find clock sub-node\n");
110 return ERR_PTR(-EINVAL);
113 for (i = 0; i < S2MPS11_CLKS_NUM; i++)
114 of_property_read_string_index(clk_np, "clock-output-names", i,
120 static int s2mps11_clk_probe(struct platform_device *pdev)
122 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
123 struct s2mps11_clk *s2mps11_clks;
124 struct clk_hw_onecell_data *clk_data;
125 unsigned int s2mps11_reg;
127 enum sec_device_type hwid = platform_get_device_id(pdev)->driver_data;
129 s2mps11_clks = devm_kcalloc(&pdev->dev, S2MPS11_CLKS_NUM,
130 sizeof(*s2mps11_clks), GFP_KERNEL);
134 clk_data = devm_kzalloc(&pdev->dev,
135 struct_size(clk_data, hws, S2MPS11_CLKS_NUM),
142 s2mps11_reg = S2MPS11_REG_RTC_CTRL;
145 s2mps11_reg = S2MPS13_REG_RTCCTRL;
148 s2mps11_reg = S2MPS14_REG_RTCCTRL;
151 s2mps11_reg = S5M8767_REG_CTRL1;
154 dev_err(&pdev->dev, "Invalid device type\n");
158 /* Store clocks of_node in first element of s2mps11_clks array */
159 s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev, s2mps11_clks_init);
160 if (IS_ERR(s2mps11_clks->clk_np))
161 return PTR_ERR(s2mps11_clks->clk_np);
163 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
164 if (i == S2MPS11_CLK_CP && hwid == S2MPS14X)
165 continue; /* Skip clocks not present in some devices */
166 s2mps11_clks[i].iodev = iodev;
167 s2mps11_clks[i].hw.init = &s2mps11_clks_init[i];
168 s2mps11_clks[i].mask = 1 << i;
169 s2mps11_clks[i].reg = s2mps11_reg;
171 s2mps11_clks[i].clk = devm_clk_register(&pdev->dev,
172 &s2mps11_clks[i].hw);
173 if (IS_ERR(s2mps11_clks[i].clk)) {
174 dev_err(&pdev->dev, "Fail to register : %s\n",
175 s2mps11_clks_init[i].name);
176 ret = PTR_ERR(s2mps11_clks[i].clk);
180 s2mps11_clks[i].lookup = clkdev_hw_create(&s2mps11_clks[i].hw,
181 s2mps11_clks_init[i].name, NULL);
182 if (!s2mps11_clks[i].lookup) {
186 clk_data->hws[i] = &s2mps11_clks[i].hw;
189 clk_data->num = S2MPS11_CLKS_NUM;
190 of_clk_add_hw_provider(s2mps11_clks->clk_np, of_clk_hw_onecell_get,
193 platform_set_drvdata(pdev, s2mps11_clks);
199 clkdev_drop(s2mps11_clks[i].lookup);
204 static int s2mps11_clk_remove(struct platform_device *pdev)
206 struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
209 of_clk_del_provider(s2mps11_clks[0].clk_np);
210 /* Drop the reference obtained in s2mps11_clk_parse_dt */
211 of_node_put(s2mps11_clks[0].clk_np);
213 for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
214 /* Skip clocks not present on S2MPS14 */
215 if (!s2mps11_clks[i].lookup)
217 clkdev_drop(s2mps11_clks[i].lookup);
223 static const struct platform_device_id s2mps11_clk_id[] = {
224 { "s2mps11-clk", S2MPS11X},
225 { "s2mps13-clk", S2MPS13X},
226 { "s2mps14-clk", S2MPS14X},
227 { "s5m8767-clk", S5M8767X},
230 MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
234 * Device is instantiated through parent MFD device and device matching is done
235 * through platform_device_id.
237 * However if device's DT node contains proper clock compatible and driver is
238 * built as a module, then the *module* matching will be done trough DT aliases.
239 * This requires of_device_id table. In the same time this will not change the
240 * actual *device* matching so do not add .of_match_table.
242 static const struct of_device_id s2mps11_dt_match[] __used = {
244 .compatible = "samsung,s2mps11-clk",
245 .data = (void *)S2MPS11X,
247 .compatible = "samsung,s2mps13-clk",
248 .data = (void *)S2MPS13X,
250 .compatible = "samsung,s2mps14-clk",
251 .data = (void *)S2MPS14X,
253 .compatible = "samsung,s5m8767-clk",
254 .data = (void *)S5M8767X,
259 MODULE_DEVICE_TABLE(of, s2mps11_dt_match);
262 static struct platform_driver s2mps11_clk_driver = {
264 .name = "s2mps11-clk",
266 .probe = s2mps11_clk_probe,
267 .remove = s2mps11_clk_remove,
268 .id_table = s2mps11_clk_id,
271 static int __init s2mps11_clk_init(void)
273 return platform_driver_register(&s2mps11_clk_driver);
275 subsys_initcall(s2mps11_clk_init);
277 static void __exit s2mps11_clk_cleanup(void)
279 platform_driver_unregister(&s2mps11_clk_driver);
281 module_exit(s2mps11_clk_cleanup);
283 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
284 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
285 MODULE_LICENSE("GPL");