2 * clk-s2mps11.c - Clock driver for S2MPS11.
4 * Copyright (C) 2013,2014 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.
18 #include <linux/module.h>
19 #include <linux/err.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>
39 struct sec_pmic_dev
*iodev
;
40 struct device_node
*clk_np
;
43 struct clk_lookup
*lookup
;
48 static struct s2mps11_clk
*to_s2mps11_clk(struct clk_hw
*hw
)
50 return container_of(hw
, struct s2mps11_clk
, hw
);
53 static int s2mps11_clk_prepare(struct clk_hw
*hw
)
55 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
57 return regmap_update_bits(s2mps11
->iodev
->regmap_pmic
,
59 s2mps11
->mask
, s2mps11
->mask
);
62 static void s2mps11_clk_unprepare(struct clk_hw
*hw
)
64 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
66 regmap_update_bits(s2mps11
->iodev
->regmap_pmic
, s2mps11
->reg
,
67 s2mps11
->mask
, ~s2mps11
->mask
);
70 static int s2mps11_clk_is_prepared(struct clk_hw
*hw
)
74 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
76 ret
= regmap_read(s2mps11
->iodev
->regmap_pmic
,
81 return val
& s2mps11
->mask
;
84 static unsigned long s2mps11_clk_recalc_rate(struct clk_hw
*hw
,
85 unsigned long parent_rate
)
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
,
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
] = {
100 .name
= "s2mps11_ap",
101 .ops
= &s2mps11_clk_ops
,
102 .flags
= CLK_IS_ROOT
,
105 .name
= "s2mps11_cp",
106 .ops
= &s2mps11_clk_ops
,
107 .flags
= CLK_IS_ROOT
,
110 .name
= "s2mps11_bt",
111 .ops
= &s2mps11_clk_ops
,
112 .flags
= CLK_IS_ROOT
,
116 static struct device_node
*s2mps11_clk_parse_dt(struct platform_device
*pdev
,
117 struct clk_init_data
*clks_init
)
119 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
120 struct device_node
*clk_np
;
123 if (!iodev
->dev
->of_node
)
124 return ERR_PTR(-EINVAL
);
126 clk_np
= of_get_child_by_name(iodev
->dev
->of_node
, "clocks");
128 dev_err(&pdev
->dev
, "could not find clock sub-node\n");
129 return ERR_PTR(-EINVAL
);
132 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++)
133 of_property_read_string_index(clk_np
, "clock-output-names", i
,
139 static int s2mps11_clk_probe(struct platform_device
*pdev
)
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
;
146 enum sec_device_type hwid
= platform_get_device_id(pdev
)->driver_data
;
148 s2mps11_clks
= devm_kcalloc(&pdev
->dev
, S2MPS11_CLKS_NUM
,
149 sizeof(*s2mps11_clks
), GFP_KERNEL
);
153 clk_data
= devm_kzalloc(&pdev
->dev
, sizeof(*clk_data
), GFP_KERNEL
);
157 clk_data
->clks
= devm_kcalloc(&pdev
->dev
, S2MPS11_CLKS_NUM
,
158 sizeof(struct clk
*), GFP_KERNEL
);
164 s2mps11_reg
= S2MPS11_REG_RTC_CTRL
;
167 s2mps11_reg
= S2MPS13_REG_RTCCTRL
;
170 s2mps11_reg
= S2MPS14_REG_RTCCTRL
;
173 s2mps11_reg
= S5M8767_REG_CTRL1
;
176 dev_err(&pdev
->dev
, "Invalid device type\n");
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
);
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
;
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
);
202 s2mps11_clks
[i
].lookup
= clkdev_create(s2mps11_clks
[i
].clk
,
203 s2mps11_clks_init
[i
].name
, NULL
);
204 if (!s2mps11_clks
[i
].lookup
) {
208 clk_data
->clks
[i
] = s2mps11_clks
[i
].clk
;
211 clk_data
->clk_num
= S2MPS11_CLKS_NUM
;
212 of_clk_add_provider(s2mps11_clks
->clk_np
, of_clk_src_onecell_get
,
215 platform_set_drvdata(pdev
, s2mps11_clks
);
221 clkdev_drop(s2mps11_clks
[i
].lookup
);
226 static int s2mps11_clk_remove(struct platform_device
*pdev
)
228 struct s2mps11_clk
*s2mps11_clks
= platform_get_drvdata(pdev
);
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
);
235 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++) {
236 /* Skip clocks not present on S2MPS14 */
237 if (!s2mps11_clks
[i
].lookup
)
239 clkdev_drop(s2mps11_clks
[i
].lookup
);
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
},
252 MODULE_DEVICE_TABLE(platform
, s2mps11_clk_id
);
254 static struct platform_driver s2mps11_clk_driver
= {
256 .name
= "s2mps11-clk",
258 .probe
= s2mps11_clk_probe
,
259 .remove
= s2mps11_clk_remove
,
260 .id_table
= s2mps11_clk_id
,
263 static int __init
s2mps11_clk_init(void)
265 return platform_driver_register(&s2mps11_clk_driver
);
267 subsys_initcall(s2mps11_clk_init
);
269 static void __exit
s2mps11_clk_cleanup(void)
271 platform_driver_unregister(&s2mps11_clk_driver
);
273 module_exit(s2mps11_clk_cleanup
);
275 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
276 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
277 MODULE_LICENSE("GPL");