1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
6 * Fixed rate clock implementation
9 #include <linux/clk-provider.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
13 #include <linux/err.h>
15 #include <linux/platform_device.h>
18 * DOC: basic fixed-rate clock that cannot gate
20 * Traits of this clock:
21 * prepare - clk_(un)prepare only ensures parents are prepared
22 * enable - clk_enable only ensures parents are enabled
23 * rate - rate is always a fixed value. No clk_set_rate support
24 * parent - fixed parent. No clk_set_parent support
27 #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
29 static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw
*hw
,
30 unsigned long parent_rate
)
32 return to_clk_fixed_rate(hw
)->fixed_rate
;
35 static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw
*hw
,
36 unsigned long parent_accuracy
)
38 struct clk_fixed_rate
*fixed
= to_clk_fixed_rate(hw
);
40 if (fixed
->flags
& CLK_FIXED_RATE_PARENT_ACCURACY
)
41 return parent_accuracy
;
43 return fixed
->fixed_accuracy
;
46 const struct clk_ops clk_fixed_rate_ops
= {
47 .recalc_rate
= clk_fixed_rate_recalc_rate
,
48 .recalc_accuracy
= clk_fixed_rate_recalc_accuracy
,
50 EXPORT_SYMBOL_GPL(clk_fixed_rate_ops
);
52 static void devm_clk_hw_register_fixed_rate_release(struct device
*dev
, void *res
)
54 struct clk_fixed_rate
*fix
= res
;
57 * We can not use clk_hw_unregister_fixed_rate, since it will kfree()
58 * the hw, resulting in double free. Just unregister the hw and let
59 * devres code kfree() it.
61 clk_hw_unregister(&fix
->hw
);
64 struct clk_hw
*__clk_hw_register_fixed_rate(struct device
*dev
,
65 struct device_node
*np
, const char *name
,
66 const char *parent_name
, const struct clk_hw
*parent_hw
,
67 const struct clk_parent_data
*parent_data
, unsigned long flags
,
68 unsigned long fixed_rate
, unsigned long fixed_accuracy
,
69 unsigned long clk_fixed_flags
, bool devm
)
71 struct clk_fixed_rate
*fixed
;
73 struct clk_init_data init
= {};
76 /* allocate fixed-rate clock */
78 fixed
= devres_alloc(devm_clk_hw_register_fixed_rate_release
,
79 sizeof(*fixed
), GFP_KERNEL
);
81 fixed
= kzalloc(sizeof(*fixed
), GFP_KERNEL
);
83 return ERR_PTR(-ENOMEM
);
86 init
.ops
= &clk_fixed_rate_ops
;
88 init
.parent_names
= parent_name
? &parent_name
: NULL
;
89 init
.parent_hws
= parent_hw
? &parent_hw
: NULL
;
90 init
.parent_data
= parent_data
;
91 if (parent_name
|| parent_hw
|| parent_data
)
96 /* struct clk_fixed_rate assignments */
97 fixed
->flags
= clk_fixed_flags
;
98 fixed
->fixed_rate
= fixed_rate
;
99 fixed
->fixed_accuracy
= fixed_accuracy
;
100 fixed
->hw
.init
= &init
;
102 /* register the clock */
105 ret
= clk_hw_register(dev
, hw
);
107 ret
= of_clk_hw_register(np
, hw
);
115 devres_add(dev
, fixed
);
119 EXPORT_SYMBOL_GPL(__clk_hw_register_fixed_rate
);
121 struct clk
*clk_register_fixed_rate(struct device
*dev
, const char *name
,
122 const char *parent_name
, unsigned long flags
,
123 unsigned long fixed_rate
)
127 hw
= clk_hw_register_fixed_rate_with_accuracy(dev
, name
, parent_name
,
128 flags
, fixed_rate
, 0);
133 EXPORT_SYMBOL_GPL(clk_register_fixed_rate
);
135 void clk_unregister_fixed_rate(struct clk
*clk
)
139 hw
= __clk_get_hw(clk
);
144 kfree(to_clk_fixed_rate(hw
));
146 EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate
);
148 void clk_hw_unregister_fixed_rate(struct clk_hw
*hw
)
150 struct clk_fixed_rate
*fixed
;
152 fixed
= to_clk_fixed_rate(hw
);
154 clk_hw_unregister(hw
);
157 EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate
);
160 static struct clk_hw
*_of_fixed_clk_setup(struct device_node
*node
)
163 const char *clk_name
= node
->name
;
168 if (of_property_read_u32(node
, "clock-frequency", &rate
))
169 return ERR_PTR(-EIO
);
171 of_property_read_u32(node
, "clock-accuracy", &accuracy
);
173 of_property_read_string(node
, "clock-output-names", &clk_name
);
175 hw
= clk_hw_register_fixed_rate_with_accuracy(NULL
, clk_name
, NULL
,
180 ret
= of_clk_add_hw_provider(node
, of_clk_hw_simple_get
, hw
);
182 clk_hw_unregister_fixed_rate(hw
);
190 * of_fixed_clk_setup() - Setup function for simple fixed rate clock
191 * @node: device node for the clock
193 void __init
of_fixed_clk_setup(struct device_node
*node
)
195 _of_fixed_clk_setup(node
);
197 CLK_OF_DECLARE(fixed_clk
, "fixed-clock", of_fixed_clk_setup
);
199 static void of_fixed_clk_remove(struct platform_device
*pdev
)
201 struct clk_hw
*hw
= platform_get_drvdata(pdev
);
203 of_clk_del_provider(pdev
->dev
.of_node
);
204 clk_hw_unregister_fixed_rate(hw
);
207 static int of_fixed_clk_probe(struct platform_device
*pdev
)
212 * This function is not executed when of_fixed_clk_setup
215 hw
= _of_fixed_clk_setup(pdev
->dev
.of_node
);
219 platform_set_drvdata(pdev
, hw
);
224 static const struct of_device_id of_fixed_clk_ids
[] = {
225 { .compatible
= "fixed-clock" },
229 static struct platform_driver of_fixed_clk_driver
= {
231 .name
= "of_fixed_clk",
232 .of_match_table
= of_fixed_clk_ids
,
234 .probe
= of_fixed_clk_probe
,
235 .remove
= of_fixed_clk_remove
,
237 builtin_platform_driver(of_fixed_clk_driver
);