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
;
52 static struct s2mps11_clk
*to_s2mps11_clk(struct clk_hw
*hw
)
54 return container_of(hw
, struct s2mps11_clk
, hw
);
57 static int s2mps11_clk_prepare(struct clk_hw
*hw
)
59 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
62 ret
= regmap_update_bits(s2mps11
->iodev
->regmap_pmic
,
64 s2mps11
->mask
, s2mps11
->mask
);
69 static void s2mps11_clk_unprepare(struct clk_hw
*hw
)
71 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
74 ret
= regmap_update_bits(s2mps11
->iodev
->regmap_pmic
, s2mps11
->reg
,
75 s2mps11
->mask
, ~s2mps11
->mask
);
78 static int s2mps11_clk_is_prepared(struct clk_hw
*hw
)
82 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
84 ret
= regmap_read(s2mps11
->iodev
->regmap_pmic
,
89 return val
& s2mps11
->mask
;
92 static unsigned long s2mps11_clk_recalc_rate(struct clk_hw
*hw
,
93 unsigned long parent_rate
)
98 static struct clk_ops s2mps11_clk_ops
= {
99 .prepare
= s2mps11_clk_prepare
,
100 .unprepare
= s2mps11_clk_unprepare
,
101 .is_prepared
= s2mps11_clk_is_prepared
,
102 .recalc_rate
= s2mps11_clk_recalc_rate
,
105 static struct clk_init_data s2mps11_clks_init
[S2MPS11_CLKS_NUM
] = {
107 .name
= "s2mps11_ap",
108 .ops
= &s2mps11_clk_ops
,
109 .flags
= CLK_IS_ROOT
,
112 .name
= "s2mps11_cp",
113 .ops
= &s2mps11_clk_ops
,
114 .flags
= CLK_IS_ROOT
,
117 .name
= "s2mps11_bt",
118 .ops
= &s2mps11_clk_ops
,
119 .flags
= CLK_IS_ROOT
,
123 static struct clk_init_data s2mps14_clks_init
[S2MPS11_CLKS_NUM
] = {
125 .name
= "s2mps14_ap",
126 .ops
= &s2mps11_clk_ops
,
127 .flags
= CLK_IS_ROOT
,
130 .name
= "s2mps14_bt",
131 .ops
= &s2mps11_clk_ops
,
132 .flags
= CLK_IS_ROOT
,
136 static struct device_node
*s2mps11_clk_parse_dt(struct platform_device
*pdev
,
137 struct clk_init_data
*clks_init
)
139 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
140 struct device_node
*clk_np
;
143 if (!iodev
->dev
->of_node
)
144 return ERR_PTR(-EINVAL
);
146 clk_np
= of_get_child_by_name(iodev
->dev
->of_node
, "clocks");
148 dev_err(&pdev
->dev
, "could not find clock sub-node\n");
149 return ERR_PTR(-EINVAL
);
152 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++) {
153 if (!clks_init
[i
].name
)
154 continue; /* Skip clocks not present in some devices */
155 of_property_read_string_index(clk_np
, "clock-output-names", i
,
162 static int s2mps11_clk_probe(struct platform_device
*pdev
)
164 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
165 struct s2mps11_clk
*s2mps11_clks
, *s2mps11_clk
;
166 unsigned int s2mps11_reg
;
167 struct clk_init_data
*clks_init
;
170 s2mps11_clks
= devm_kzalloc(&pdev
->dev
, sizeof(*s2mps11_clk
) *
171 S2MPS11_CLKS_NUM
, GFP_KERNEL
);
175 s2mps11_clk
= s2mps11_clks
;
177 clk_table
= devm_kzalloc(&pdev
->dev
, sizeof(struct clk
*) *
178 S2MPS11_CLKS_NUM
, GFP_KERNEL
);
182 switch(platform_get_device_id(pdev
)->driver_data
) {
184 s2mps11_reg
= S2MPS11_REG_RTC_CTRL
;
185 clks_init
= s2mps11_clks_init
;
188 s2mps11_reg
= S2MPS14_REG_RTCCTRL
;
189 clks_init
= s2mps14_clks_init
;
192 s2mps11_reg
= S5M8767_REG_CTRL1
;
193 clks_init
= s2mps11_clks_init
;
196 dev_err(&pdev
->dev
, "Invalid device type\n");
200 /* Store clocks of_node in first element of s2mps11_clks array */
201 s2mps11_clks
->clk_np
= s2mps11_clk_parse_dt(pdev
, clks_init
);
202 if (IS_ERR(s2mps11_clks
->clk_np
))
203 return PTR_ERR(s2mps11_clks
->clk_np
);
205 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++, s2mps11_clk
++) {
206 if (!clks_init
[i
].name
)
207 continue; /* Skip clocks not present in some devices */
208 s2mps11_clk
->iodev
= iodev
;
209 s2mps11_clk
->hw
.init
= &clks_init
[i
];
210 s2mps11_clk
->mask
= 1 << i
;
211 s2mps11_clk
->reg
= s2mps11_reg
;
213 s2mps11_clk
->clk
= devm_clk_register(&pdev
->dev
,
215 if (IS_ERR(s2mps11_clk
->clk
)) {
216 dev_err(&pdev
->dev
, "Fail to register : %s\n",
217 s2mps11_name(s2mps11_clk
));
218 ret
= PTR_ERR(s2mps11_clk
->clk
);
222 s2mps11_clk
->lookup
= clkdev_alloc(s2mps11_clk
->clk
,
223 s2mps11_name(s2mps11_clk
), NULL
);
224 if (!s2mps11_clk
->lookup
) {
229 clkdev_add(s2mps11_clk
->lookup
);
232 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++) {
233 /* Skip clocks not present on S2MPS14 */
234 if (!clks_init
[i
].name
)
236 clk_table
[i
] = s2mps11_clks
[i
].clk
;
239 clk_data
.clks
= clk_table
;
240 clk_data
.clk_num
= S2MPS11_CLKS_NUM
;
241 of_clk_add_provider(s2mps11_clks
->clk_np
, of_clk_src_onecell_get
,
244 platform_set_drvdata(pdev
, s2mps11_clks
);
248 devm_clk_unregister(&pdev
->dev
, s2mps11_clk
->clk
);
250 while (s2mps11_clk
> s2mps11_clks
) {
251 if (s2mps11_clk
->lookup
) {
252 clkdev_drop(s2mps11_clk
->lookup
);
253 devm_clk_unregister(&pdev
->dev
, s2mps11_clk
->clk
);
261 static int s2mps11_clk_remove(struct platform_device
*pdev
)
263 struct s2mps11_clk
*s2mps11_clks
= platform_get_drvdata(pdev
);
266 of_clk_del_provider(s2mps11_clks
[0].clk_np
);
267 /* Drop the reference obtained in s2mps11_clk_parse_dt */
268 of_node_put(s2mps11_clks
[0].clk_np
);
270 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++) {
271 /* Skip clocks not present on S2MPS14 */
272 if (!s2mps11_clks
[i
].lookup
)
274 clkdev_drop(s2mps11_clks
[i
].lookup
);
280 static const struct platform_device_id s2mps11_clk_id
[] = {
281 { "s2mps11-clk", S2MPS11X
},
282 { "s2mps14-clk", S2MPS14X
},
283 { "s5m8767-clk", S5M8767X
},
286 MODULE_DEVICE_TABLE(platform
, s2mps11_clk_id
);
288 static struct platform_driver s2mps11_clk_driver
= {
290 .name
= "s2mps11-clk",
291 .owner
= THIS_MODULE
,
293 .probe
= s2mps11_clk_probe
,
294 .remove
= s2mps11_clk_remove
,
295 .id_table
= s2mps11_clk_id
,
298 static int __init
s2mps11_clk_init(void)
300 return platform_driver_register(&s2mps11_clk_driver
);
302 subsys_initcall(s2mps11_clk_init
);
304 static void __init
s2mps11_clk_cleanup(void)
306 platform_driver_unregister(&s2mps11_clk_driver
);
308 module_exit(s2mps11_clk_cleanup
);
310 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
311 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
312 MODULE_LICENSE("GPL");