1
2
3
4
5
6
7
8
9
10
11
12#include <linux/clk-provider.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/io.h>
16#include <linux/err.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19
20
21
22
23
24
25
26
27
28
29
30static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
31 unsigned long parent_rate)
32{
33 return to_clk_fixed_rate(hw)->fixed_rate;
34}
35
36static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
37 unsigned long parent_accuracy)
38{
39 return to_clk_fixed_rate(hw)->fixed_accuracy;
40}
41
42const struct clk_ops clk_fixed_rate_ops = {
43 .recalc_rate = clk_fixed_rate_recalc_rate,
44 .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
45};
46EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
47
48
49
50
51
52
53
54
55
56
57
58struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
59 const char *name, const char *parent_name, unsigned long flags,
60 unsigned long fixed_rate, unsigned long fixed_accuracy)
61{
62 struct clk_fixed_rate *fixed;
63 struct clk_hw *hw;
64 struct clk_init_data init;
65 int ret;
66
67
68 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
69 if (!fixed)
70 return ERR_PTR(-ENOMEM);
71
72 init.name = name;
73 init.ops = &clk_fixed_rate_ops;
74 init.flags = flags | CLK_IS_BASIC;
75 init.parent_names = (parent_name ? &parent_name: NULL);
76 init.num_parents = (parent_name ? 1 : 0);
77
78
79 fixed->fixed_rate = fixed_rate;
80 fixed->fixed_accuracy = fixed_accuracy;
81 fixed->hw.init = &init;
82
83
84 hw = &fixed->hw;
85 ret = clk_hw_register(dev, hw);
86 if (ret) {
87 kfree(fixed);
88 hw = ERR_PTR(ret);
89 }
90
91 return hw;
92}
93EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate_with_accuracy);
94
95struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
96 const char *name, const char *parent_name, unsigned long flags,
97 unsigned long fixed_rate, unsigned long fixed_accuracy)
98{
99 struct clk_hw *hw;
100
101 hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
102 flags, fixed_rate, fixed_accuracy);
103 if (IS_ERR(hw))
104 return ERR_CAST(hw);
105 return hw->clk;
106}
107EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
108
109
110
111
112
113
114
115
116
117
118struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
119 const char *parent_name, unsigned long flags,
120 unsigned long fixed_rate)
121{
122 return clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
123 flags, fixed_rate, 0);
124}
125EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate);
126
127struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
128 const char *parent_name, unsigned long flags,
129 unsigned long fixed_rate)
130{
131 return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
132 flags, fixed_rate, 0);
133}
134EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
135
136void clk_unregister_fixed_rate(struct clk *clk)
137{
138 struct clk_hw *hw;
139
140 hw = __clk_get_hw(clk);
141 if (!hw)
142 return;
143
144 clk_unregister(clk);
145 kfree(to_clk_fixed_rate(hw));
146}
147EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
148
149void clk_hw_unregister_fixed_rate(struct clk_hw *hw)
150{
151 struct clk_fixed_rate *fixed;
152
153 fixed = to_clk_fixed_rate(hw);
154
155 clk_hw_unregister(hw);
156 kfree(fixed);
157}
158EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate);
159
160#ifdef CONFIG_OF
161static struct clk *_of_fixed_clk_setup(struct device_node *node)
162{
163 struct clk *clk;
164 const char *clk_name = node->name;
165 u32 rate;
166 u32 accuracy = 0;
167 int ret;
168
169 if (of_property_read_u32(node, "clock-frequency", &rate))
170 return ERR_PTR(-EIO);
171
172 of_property_read_u32(node, "clock-accuracy", &accuracy);
173
174 of_property_read_string(node, "clock-output-names", &clk_name);
175
176 clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
177 0, rate, accuracy);
178 if (IS_ERR(clk))
179 return clk;
180
181 ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
182 if (ret) {
183 clk_unregister(clk);
184 return ERR_PTR(ret);
185 }
186
187 return clk;
188}
189
190
191
192
193void __init of_fixed_clk_setup(struct device_node *node)
194{
195 _of_fixed_clk_setup(node);
196}
197CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
198
199static int of_fixed_clk_remove(struct platform_device *pdev)
200{
201 struct clk *clk = platform_get_drvdata(pdev);
202
203 clk_unregister_fixed_rate(clk);
204
205 return 0;
206}
207
208static int of_fixed_clk_probe(struct platform_device *pdev)
209{
210 struct clk *clk;
211
212
213
214
215
216 clk = _of_fixed_clk_setup(pdev->dev.of_node);
217 if (IS_ERR(clk))
218 return PTR_ERR(clk);
219
220 platform_set_drvdata(pdev, clk);
221
222 return 0;
223}
224
225static const struct of_device_id of_fixed_clk_ids[] = {
226 { .compatible = "fixed-clock" },
227 { }
228};
229MODULE_DEVICE_TABLE(of, of_fixed_clk_ids);
230
231static struct platform_driver of_fixed_clk_driver = {
232 .driver = {
233 .name = "of_fixed_clk",
234 .of_match_table = of_fixed_clk_ids,
235 },
236 .probe = of_fixed_clk_probe,
237 .remove = of_fixed_clk_remove,
238};
239builtin_platform_driver(of_fixed_clk_driver);
240#endif
241