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/s2mps14.h>
27 #include <linux/mfd/samsung/s5m8767.h>
28 #include <linux/mfd/samsung/core.h>
30 #define s2mps11_name(a) (a->hw.init->name)
32 static struct clk
**clk_table
;
33 static struct clk_onecell_data clk_data
;
43 struct sec_pmic_dev
*iodev
;
44 struct device_node
*clk_np
;
47 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
,
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 clk_init_data s2mps14_clks_init
[S2MPS11_CLKS_NUM
] = {
128 .name
= "s2mps14_ap",
129 .ops
= &s2mps11_clk_ops
,
130 .flags
= CLK_IS_ROOT
,
133 .name
= "s2mps14_bt",
134 .ops
= &s2mps11_clk_ops
,
135 .flags
= CLK_IS_ROOT
,
139 static struct device_node
*s2mps11_clk_parse_dt(struct platform_device
*pdev
,
140 struct clk_init_data
*clks_init
)
142 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
143 struct device_node
*clk_np
;
146 if (!iodev
->dev
->of_node
)
147 return ERR_PTR(-EINVAL
);
149 clk_np
= of_get_child_by_name(iodev
->dev
->of_node
, "clocks");
151 dev_err(&pdev
->dev
, "could not find clock sub-node\n");
152 return ERR_PTR(-EINVAL
);
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
,
165 static int s2mps11_clk_probe(struct platform_device
*pdev
)
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
;
174 s2mps11_clks
= devm_kzalloc(&pdev
->dev
, sizeof(*s2mps11_clk
) *
175 S2MPS11_CLKS_NUM
, GFP_KERNEL
);
179 s2mps11_clk
= s2mps11_clks
;
181 clk_table
= devm_kzalloc(&pdev
->dev
, sizeof(struct clk
*) *
182 S2MPS11_CLKS_NUM
, GFP_KERNEL
);
186 switch(platform_get_device_id(pdev
)->driver_data
) {
188 s2mps11_reg
= S2MPS11_REG_RTC_CTRL
;
189 clks_init
= s2mps11_clks_init
;
192 s2mps11_reg
= S2MPS14_REG_RTCCTRL
;
193 clks_init
= s2mps14_clks_init
;
196 s2mps11_reg
= S5M8767_REG_CTRL1
;
197 clks_init
= s2mps11_clks_init
;
200 dev_err(&pdev
->dev
, "Invalid device type\n");
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
);
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
;
217 ret
= regmap_read(s2mps11_clk
->iodev
->regmap_pmic
,
218 s2mps11_clk
->reg
, &val
);
222 s2mps11_clk
->enabled
= val
& s2mps11_clk
->mask
;
224 s2mps11_clk
->clk
= devm_clk_register(&pdev
->dev
,
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
);
233 s2mps11_clk
->lookup
= devm_kzalloc(&pdev
->dev
,
234 sizeof(struct clk_lookup
), GFP_KERNEL
);
235 if (!s2mps11_clk
->lookup
) {
240 s2mps11_clk
->lookup
->con_id
= s2mps11_name(s2mps11_clk
);
241 s2mps11_clk
->lookup
->clk
= s2mps11_clk
->clk
;
243 clkdev_add(s2mps11_clk
->lookup
);
246 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++) {
247 /* Skip clocks not present on S2MPS14 */
248 if (!clks_init
[i
].name
)
250 clk_table
[i
] = s2mps11_clks
[i
].clk
;
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
,
258 platform_set_drvdata(pdev
, s2mps11_clks
);
262 devm_clk_unregister(&pdev
->dev
, s2mps11_clk
->clk
);
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
);
275 static int s2mps11_clk_remove(struct platform_device
*pdev
)
277 struct s2mps11_clk
*s2mps11_clks
= platform_get_drvdata(pdev
);
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
);
284 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++) {
285 /* Skip clocks not present on S2MPS14 */
286 if (!s2mps11_clks
[i
].lookup
)
288 clkdev_drop(s2mps11_clks
[i
].lookup
);
294 static const struct platform_device_id s2mps11_clk_id
[] = {
295 { "s2mps11-clk", S2MPS11X
},
296 { "s2mps14-clk", S2MPS14X
},
297 { "s5m8767-clk", S5M8767X
},
300 MODULE_DEVICE_TABLE(platform
, s2mps11_clk_id
);
302 static struct platform_driver s2mps11_clk_driver
= {
304 .name
= "s2mps11-clk",
305 .owner
= THIS_MODULE
,
307 .probe
= s2mps11_clk_probe
,
308 .remove
= s2mps11_clk_remove
,
309 .id_table
= s2mps11_clk_id
,
312 static int __init
s2mps11_clk_init(void)
314 return platform_driver_register(&s2mps11_clk_driver
);
316 subsys_initcall(s2mps11_clk_init
);
318 static void __init
s2mps11_clk_cleanup(void)
320 platform_driver_unregister(&s2mps11_clk_driver
);
322 module_exit(s2mps11_clk_cleanup
);
324 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
325 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
326 MODULE_LICENSE("GPL");