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/s5m8767.h>
31 #include <linux/mfd/samsung/core.h>
33 #define s2mps11_name(a) (a->hw.init->name)
35 static struct clk
**clk_table
;
36 static struct clk_onecell_data clk_data
;
46 struct sec_pmic_dev
*iodev
;
49 struct clk_lookup
*lookup
;
55 static struct s2mps11_clk
*to_s2mps11_clk(struct clk_hw
*hw
)
57 return container_of(hw
, struct s2mps11_clk
, hw
);
60 static int s2mps11_clk_prepare(struct clk_hw
*hw
)
62 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
65 ret
= regmap_update_bits(s2mps11
->iodev
->regmap_pmic
,
67 s2mps11
->mask
, s2mps11
->mask
);
69 s2mps11
->enabled
= true;
74 static void s2mps11_clk_unprepare(struct clk_hw
*hw
)
76 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
79 ret
= regmap_update_bits(s2mps11
->iodev
->regmap_pmic
, s2mps11
->reg
,
80 s2mps11
->mask
, ~s2mps11
->mask
);
83 s2mps11
->enabled
= false;
86 static int s2mps11_clk_is_enabled(struct clk_hw
*hw
)
88 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
90 return s2mps11
->enabled
;
93 static unsigned long s2mps11_clk_recalc_rate(struct clk_hw
*hw
,
94 unsigned long parent_rate
)
96 struct s2mps11_clk
*s2mps11
= to_s2mps11_clk(hw
);
103 static struct clk_ops s2mps11_clk_ops
= {
104 .prepare
= s2mps11_clk_prepare
,
105 .unprepare
= s2mps11_clk_unprepare
,
106 .is_enabled
= s2mps11_clk_is_enabled
,
107 .recalc_rate
= s2mps11_clk_recalc_rate
,
110 static struct clk_init_data s2mps11_clks_init
[S2MPS11_CLKS_NUM
] = {
112 .name
= "s2mps11_ap",
113 .ops
= &s2mps11_clk_ops
,
114 .flags
= CLK_IS_ROOT
,
117 .name
= "s2mps11_cp",
118 .ops
= &s2mps11_clk_ops
,
119 .flags
= CLK_IS_ROOT
,
122 .name
= "s2mps11_bt",
123 .ops
= &s2mps11_clk_ops
,
124 .flags
= CLK_IS_ROOT
,
128 static struct device_node
*s2mps11_clk_parse_dt(struct platform_device
*pdev
)
130 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
131 struct device_node
*clk_np
;
134 if (!iodev
->dev
->of_node
)
135 return ERR_PTR(-EINVAL
);
137 clk_np
= of_get_child_by_name(iodev
->dev
->of_node
, "clocks");
139 dev_err(&pdev
->dev
, "could not find clock sub-node\n");
140 return ERR_PTR(-EINVAL
);
143 clk_table
= devm_kzalloc(&pdev
->dev
, sizeof(struct clk
*) *
144 S2MPS11_CLKS_NUM
, GFP_KERNEL
);
146 return ERR_PTR(-ENOMEM
);
148 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++)
149 of_property_read_string_index(clk_np
, "clock-output-names", i
,
150 &s2mps11_clks_init
[i
].name
);
155 static int s2mps11_clk_probe(struct platform_device
*pdev
)
157 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
158 struct s2mps11_clk
*s2mps11_clks
, *s2mps11_clk
;
159 struct device_node
*clk_np
= NULL
;
160 unsigned int s2mps11_reg
;
164 s2mps11_clks
= devm_kzalloc(&pdev
->dev
, sizeof(*s2mps11_clk
) *
165 S2MPS11_CLKS_NUM
, GFP_KERNEL
);
169 s2mps11_clk
= s2mps11_clks
;
171 clk_np
= s2mps11_clk_parse_dt(pdev
);
173 return PTR_ERR(clk_np
);
175 switch(platform_get_device_id(pdev
)->driver_data
) {
177 s2mps11_reg
= S2MPS11_REG_RTC_CTRL
;
180 s2mps11_reg
= S5M8767_REG_CTRL1
;
183 dev_err(&pdev
->dev
, "Invalid device type\n");
187 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++, s2mps11_clk
++) {
188 s2mps11_clk
->iodev
= iodev
;
189 s2mps11_clk
->hw
.init
= &s2mps11_clks_init
[i
];
190 s2mps11_clk
->mask
= 1 << i
;
191 s2mps11_clk
->reg
= s2mps11_reg
;
193 ret
= regmap_read(s2mps11_clk
->iodev
->regmap_pmic
,
194 s2mps11_clk
->reg
, &val
);
198 s2mps11_clk
->enabled
= val
& s2mps11_clk
->mask
;
200 s2mps11_clk
->clk
= devm_clk_register(&pdev
->dev
,
202 if (IS_ERR(s2mps11_clk
->clk
)) {
203 dev_err(&pdev
->dev
, "Fail to register : %s\n",
204 s2mps11_name(s2mps11_clk
));
205 ret
= PTR_ERR(s2mps11_clk
->clk
);
209 s2mps11_clk
->lookup
= devm_kzalloc(&pdev
->dev
,
210 sizeof(struct clk_lookup
), GFP_KERNEL
);
211 if (!s2mps11_clk
->lookup
) {
216 s2mps11_clk
->lookup
->con_id
= s2mps11_name(s2mps11_clk
);
217 s2mps11_clk
->lookup
->clk
= s2mps11_clk
->clk
;
219 clkdev_add(s2mps11_clk
->lookup
);
223 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++)
224 clk_table
[i
] = s2mps11_clks
[i
].clk
;
226 clk_data
.clks
= clk_table
;
227 clk_data
.clk_num
= S2MPS11_CLKS_NUM
;
228 of_clk_add_provider(clk_np
, of_clk_src_onecell_get
, &clk_data
);
231 platform_set_drvdata(pdev
, s2mps11_clks
);
235 devm_clk_unregister(&pdev
->dev
, s2mps11_clk
->clk
);
237 while (s2mps11_clk
> s2mps11_clks
) {
238 if (s2mps11_clk
->lookup
) {
239 clkdev_drop(s2mps11_clk
->lookup
);
240 devm_clk_unregister(&pdev
->dev
, s2mps11_clk
->clk
);
248 static int s2mps11_clk_remove(struct platform_device
*pdev
)
250 struct s2mps11_clk
*s2mps11_clks
= platform_get_drvdata(pdev
);
253 for (i
= 0; i
< S2MPS11_CLKS_NUM
; i
++)
254 clkdev_drop(s2mps11_clks
[i
].lookup
);
259 static const struct platform_device_id s2mps11_clk_id
[] = {
260 { "s2mps11-clk", S2MPS11X
},
261 { "s5m8767-clk", S5M8767X
},
264 MODULE_DEVICE_TABLE(platform
, s2mps11_clk_id
);
266 static struct platform_driver s2mps11_clk_driver
= {
268 .name
= "s2mps11-clk",
269 .owner
= THIS_MODULE
,
271 .probe
= s2mps11_clk_probe
,
272 .remove
= s2mps11_clk_remove
,
273 .id_table
= s2mps11_clk_id
,
276 static int __init
s2mps11_clk_init(void)
278 return platform_driver_register(&s2mps11_clk_driver
);
280 subsys_initcall(s2mps11_clk_init
);
282 static void __init
s2mps11_clk_cleanup(void)
284 platform_driver_unregister(&s2mps11_clk_driver
);
286 module_exit(s2mps11_clk_cleanup
);
288 MODULE_DESCRIPTION("S2MPS11 Clock Driver");
289 MODULE_AUTHOR("Yadwinder Singh Brar <yadi.brar@samsung.com>");
290 MODULE_LICENSE("GPL");