]>
Commit | Line | Data |
---|---|---|
b2476490 MT |
1 | /* |
2 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> | |
3 | * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Standard functionality for the common clock API. See Documentation/clk.txt | |
10 | */ | |
11 | ||
3c373117 | 12 | #include <linux/clk.h> |
b09d6d99 | 13 | #include <linux/clk-provider.h> |
86be408b | 14 | #include <linux/clk/clk-conf.h> |
b2476490 MT |
15 | #include <linux/module.h> |
16 | #include <linux/mutex.h> | |
17 | #include <linux/spinlock.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/list.h> | |
20 | #include <linux/slab.h> | |
766e6a4e | 21 | #include <linux/of.h> |
46c8773a | 22 | #include <linux/device.h> |
f2f6c255 | 23 | #include <linux/init.h> |
533ddeb1 | 24 | #include <linux/sched.h> |
562ef0b0 | 25 | #include <linux/clkdev.h> |
b2476490 | 26 | |
d6782c26 SN |
27 | #include "clk.h" |
28 | ||
b2476490 MT |
29 | static DEFINE_SPINLOCK(enable_lock); |
30 | static DEFINE_MUTEX(prepare_lock); | |
31 | ||
533ddeb1 MT |
32 | static struct task_struct *prepare_owner; |
33 | static struct task_struct *enable_owner; | |
34 | ||
35 | static int prepare_refcnt; | |
36 | static int enable_refcnt; | |
37 | ||
b2476490 MT |
38 | static HLIST_HEAD(clk_root_list); |
39 | static HLIST_HEAD(clk_orphan_list); | |
40 | static LIST_HEAD(clk_notifier_list); | |
41 | ||
b09d6d99 MT |
42 | /*** private data structures ***/ |
43 | ||
44 | struct clk_core { | |
45 | const char *name; | |
46 | const struct clk_ops *ops; | |
47 | struct clk_hw *hw; | |
48 | struct module *owner; | |
49 | struct clk_core *parent; | |
50 | const char **parent_names; | |
51 | struct clk_core **parents; | |
52 | u8 num_parents; | |
53 | u8 new_parent_index; | |
54 | unsigned long rate; | |
1c8e6004 | 55 | unsigned long req_rate; |
b09d6d99 MT |
56 | unsigned long new_rate; |
57 | struct clk_core *new_parent; | |
58 | struct clk_core *new_child; | |
59 | unsigned long flags; | |
e6500344 | 60 | bool orphan; |
b09d6d99 MT |
61 | unsigned int enable_count; |
62 | unsigned int prepare_count; | |
9783c0d9 SB |
63 | unsigned long min_rate; |
64 | unsigned long max_rate; | |
b09d6d99 MT |
65 | unsigned long accuracy; |
66 | int phase; | |
67 | struct hlist_head children; | |
68 | struct hlist_node child_node; | |
1c8e6004 | 69 | struct hlist_head clks; |
b09d6d99 MT |
70 | unsigned int notifier_count; |
71 | #ifdef CONFIG_DEBUG_FS | |
72 | struct dentry *dentry; | |
8c9a8a8f | 73 | struct hlist_node debug_node; |
b09d6d99 MT |
74 | #endif |
75 | struct kref ref; | |
76 | }; | |
77 | ||
dfc202ea SB |
78 | #define CREATE_TRACE_POINTS |
79 | #include <trace/events/clk.h> | |
80 | ||
b09d6d99 MT |
81 | struct clk { |
82 | struct clk_core *core; | |
83 | const char *dev_id; | |
84 | const char *con_id; | |
1c8e6004 TV |
85 | unsigned long min_rate; |
86 | unsigned long max_rate; | |
50595f8b | 87 | struct hlist_node clks_node; |
b09d6d99 MT |
88 | }; |
89 | ||
eab89f69 MT |
90 | /*** locking ***/ |
91 | static void clk_prepare_lock(void) | |
92 | { | |
533ddeb1 MT |
93 | if (!mutex_trylock(&prepare_lock)) { |
94 | if (prepare_owner == current) { | |
95 | prepare_refcnt++; | |
96 | return; | |
97 | } | |
98 | mutex_lock(&prepare_lock); | |
99 | } | |
100 | WARN_ON_ONCE(prepare_owner != NULL); | |
101 | WARN_ON_ONCE(prepare_refcnt != 0); | |
102 | prepare_owner = current; | |
103 | prepare_refcnt = 1; | |
eab89f69 MT |
104 | } |
105 | ||
106 | static void clk_prepare_unlock(void) | |
107 | { | |
533ddeb1 MT |
108 | WARN_ON_ONCE(prepare_owner != current); |
109 | WARN_ON_ONCE(prepare_refcnt == 0); | |
110 | ||
111 | if (--prepare_refcnt) | |
112 | return; | |
113 | prepare_owner = NULL; | |
eab89f69 MT |
114 | mutex_unlock(&prepare_lock); |
115 | } | |
116 | ||
117 | static unsigned long clk_enable_lock(void) | |
a57aa185 | 118 | __acquires(enable_lock) |
eab89f69 MT |
119 | { |
120 | unsigned long flags; | |
533ddeb1 MT |
121 | |
122 | if (!spin_trylock_irqsave(&enable_lock, flags)) { | |
123 | if (enable_owner == current) { | |
124 | enable_refcnt++; | |
a57aa185 | 125 | __acquire(enable_lock); |
533ddeb1 MT |
126 | return flags; |
127 | } | |
128 | spin_lock_irqsave(&enable_lock, flags); | |
129 | } | |
130 | WARN_ON_ONCE(enable_owner != NULL); | |
131 | WARN_ON_ONCE(enable_refcnt != 0); | |
132 | enable_owner = current; | |
133 | enable_refcnt = 1; | |
eab89f69 MT |
134 | return flags; |
135 | } | |
136 | ||
137 | static void clk_enable_unlock(unsigned long flags) | |
a57aa185 | 138 | __releases(enable_lock) |
eab89f69 | 139 | { |
533ddeb1 MT |
140 | WARN_ON_ONCE(enable_owner != current); |
141 | WARN_ON_ONCE(enable_refcnt == 0); | |
142 | ||
a57aa185 SB |
143 | if (--enable_refcnt) { |
144 | __release(enable_lock); | |
533ddeb1 | 145 | return; |
a57aa185 | 146 | } |
533ddeb1 | 147 | enable_owner = NULL; |
eab89f69 MT |
148 | spin_unlock_irqrestore(&enable_lock, flags); |
149 | } | |
150 | ||
4dff95dc SB |
151 | static bool clk_core_is_prepared(struct clk_core *core) |
152 | { | |
153 | /* | |
154 | * .is_prepared is optional for clocks that can prepare | |
155 | * fall back to software usage counter if it is missing | |
156 | */ | |
157 | if (!core->ops->is_prepared) | |
158 | return core->prepare_count; | |
b2476490 | 159 | |
4dff95dc SB |
160 | return core->ops->is_prepared(core->hw); |
161 | } | |
b2476490 | 162 | |
4dff95dc SB |
163 | static bool clk_core_is_enabled(struct clk_core *core) |
164 | { | |
165 | /* | |
166 | * .is_enabled is only mandatory for clocks that gate | |
167 | * fall back to software usage counter if .is_enabled is missing | |
168 | */ | |
169 | if (!core->ops->is_enabled) | |
170 | return core->enable_count; | |
6b44c854 | 171 | |
4dff95dc SB |
172 | return core->ops->is_enabled(core->hw); |
173 | } | |
6b44c854 | 174 | |
4dff95dc | 175 | /*** helper functions ***/ |
1af599df | 176 | |
b76281cb | 177 | const char *__clk_get_name(const struct clk *clk) |
1af599df | 178 | { |
4dff95dc | 179 | return !clk ? NULL : clk->core->name; |
1af599df | 180 | } |
4dff95dc | 181 | EXPORT_SYMBOL_GPL(__clk_get_name); |
1af599df | 182 | |
e7df6f6e | 183 | const char *clk_hw_get_name(const struct clk_hw *hw) |
1a9c069c SB |
184 | { |
185 | return hw->core->name; | |
186 | } | |
187 | EXPORT_SYMBOL_GPL(clk_hw_get_name); | |
188 | ||
4dff95dc SB |
189 | struct clk_hw *__clk_get_hw(struct clk *clk) |
190 | { | |
191 | return !clk ? NULL : clk->core->hw; | |
192 | } | |
193 | EXPORT_SYMBOL_GPL(__clk_get_hw); | |
1af599df | 194 | |
e7df6f6e | 195 | unsigned int clk_hw_get_num_parents(const struct clk_hw *hw) |
1a9c069c SB |
196 | { |
197 | return hw->core->num_parents; | |
198 | } | |
199 | EXPORT_SYMBOL_GPL(clk_hw_get_num_parents); | |
200 | ||
e7df6f6e | 201 | struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw) |
1a9c069c SB |
202 | { |
203 | return hw->core->parent ? hw->core->parent->hw : NULL; | |
204 | } | |
205 | EXPORT_SYMBOL_GPL(clk_hw_get_parent); | |
206 | ||
4dff95dc SB |
207 | static struct clk_core *__clk_lookup_subtree(const char *name, |
208 | struct clk_core *core) | |
bddca894 | 209 | { |
035a61c3 | 210 | struct clk_core *child; |
4dff95dc | 211 | struct clk_core *ret; |
bddca894 | 212 | |
4dff95dc SB |
213 | if (!strcmp(core->name, name)) |
214 | return core; | |
bddca894 | 215 | |
4dff95dc SB |
216 | hlist_for_each_entry(child, &core->children, child_node) { |
217 | ret = __clk_lookup_subtree(name, child); | |
218 | if (ret) | |
219 | return ret; | |
bddca894 PG |
220 | } |
221 | ||
4dff95dc | 222 | return NULL; |
bddca894 PG |
223 | } |
224 | ||
4dff95dc | 225 | static struct clk_core *clk_core_lookup(const char *name) |
bddca894 | 226 | { |
4dff95dc SB |
227 | struct clk_core *root_clk; |
228 | struct clk_core *ret; | |
bddca894 | 229 | |
4dff95dc SB |
230 | if (!name) |
231 | return NULL; | |
bddca894 | 232 | |
4dff95dc SB |
233 | /* search the 'proper' clk tree first */ |
234 | hlist_for_each_entry(root_clk, &clk_root_list, child_node) { | |
235 | ret = __clk_lookup_subtree(name, root_clk); | |
236 | if (ret) | |
237 | return ret; | |
bddca894 PG |
238 | } |
239 | ||
4dff95dc SB |
240 | /* if not found, then search the orphan tree */ |
241 | hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { | |
242 | ret = __clk_lookup_subtree(name, root_clk); | |
243 | if (ret) | |
244 | return ret; | |
245 | } | |
bddca894 | 246 | |
4dff95dc | 247 | return NULL; |
bddca894 PG |
248 | } |
249 | ||
4dff95dc SB |
250 | static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core, |
251 | u8 index) | |
bddca894 | 252 | { |
4dff95dc SB |
253 | if (!core || index >= core->num_parents) |
254 | return NULL; | |
88cfbef2 MY |
255 | |
256 | if (!core->parents[index]) | |
257 | core->parents[index] = | |
258 | clk_core_lookup(core->parent_names[index]); | |
259 | ||
260 | return core->parents[index]; | |
bddca894 PG |
261 | } |
262 | ||
e7df6f6e SB |
263 | struct clk_hw * |
264 | clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index) | |
1a9c069c SB |
265 | { |
266 | struct clk_core *parent; | |
267 | ||
268 | parent = clk_core_get_parent_by_index(hw->core, index); | |
269 | ||
270 | return !parent ? NULL : parent->hw; | |
271 | } | |
272 | EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index); | |
273 | ||
4dff95dc SB |
274 | unsigned int __clk_get_enable_count(struct clk *clk) |
275 | { | |
276 | return !clk ? 0 : clk->core->enable_count; | |
277 | } | |
b2476490 | 278 | |
4dff95dc SB |
279 | static unsigned long clk_core_get_rate_nolock(struct clk_core *core) |
280 | { | |
281 | unsigned long ret; | |
b2476490 | 282 | |
4dff95dc SB |
283 | if (!core) { |
284 | ret = 0; | |
285 | goto out; | |
286 | } | |
b2476490 | 287 | |
4dff95dc | 288 | ret = core->rate; |
b2476490 | 289 | |
47b0eeb3 | 290 | if (!core->num_parents) |
4dff95dc | 291 | goto out; |
c646cbf1 | 292 | |
4dff95dc SB |
293 | if (!core->parent) |
294 | ret = 0; | |
b2476490 | 295 | |
b2476490 MT |
296 | out: |
297 | return ret; | |
298 | } | |
299 | ||
e7df6f6e | 300 | unsigned long clk_hw_get_rate(const struct clk_hw *hw) |
1a9c069c SB |
301 | { |
302 | return clk_core_get_rate_nolock(hw->core); | |
303 | } | |
304 | EXPORT_SYMBOL_GPL(clk_hw_get_rate); | |
305 | ||
4dff95dc SB |
306 | static unsigned long __clk_get_accuracy(struct clk_core *core) |
307 | { | |
308 | if (!core) | |
309 | return 0; | |
b2476490 | 310 | |
4dff95dc | 311 | return core->accuracy; |
b2476490 MT |
312 | } |
313 | ||
4dff95dc | 314 | unsigned long __clk_get_flags(struct clk *clk) |
fcb0ee6a | 315 | { |
4dff95dc | 316 | return !clk ? 0 : clk->core->flags; |
fcb0ee6a | 317 | } |
4dff95dc | 318 | EXPORT_SYMBOL_GPL(__clk_get_flags); |
fcb0ee6a | 319 | |
e7df6f6e | 320 | unsigned long clk_hw_get_flags(const struct clk_hw *hw) |
1a9c069c SB |
321 | { |
322 | return hw->core->flags; | |
323 | } | |
324 | EXPORT_SYMBOL_GPL(clk_hw_get_flags); | |
325 | ||
e7df6f6e | 326 | bool clk_hw_is_prepared(const struct clk_hw *hw) |
1a9c069c SB |
327 | { |
328 | return clk_core_is_prepared(hw->core); | |
329 | } | |
330 | ||
be68bf88 JE |
331 | bool clk_hw_is_enabled(const struct clk_hw *hw) |
332 | { | |
333 | return clk_core_is_enabled(hw->core); | |
334 | } | |
335 | ||
4dff95dc | 336 | bool __clk_is_enabled(struct clk *clk) |
b2476490 | 337 | { |
4dff95dc SB |
338 | if (!clk) |
339 | return false; | |
b2476490 | 340 | |
4dff95dc SB |
341 | return clk_core_is_enabled(clk->core); |
342 | } | |
343 | EXPORT_SYMBOL_GPL(__clk_is_enabled); | |
b2476490 | 344 | |
4dff95dc SB |
345 | static bool mux_is_better_rate(unsigned long rate, unsigned long now, |
346 | unsigned long best, unsigned long flags) | |
347 | { | |
348 | if (flags & CLK_MUX_ROUND_CLOSEST) | |
349 | return abs(now - rate) < abs(best - rate); | |
1af599df | 350 | |
4dff95dc SB |
351 | return now <= rate && now > best; |
352 | } | |
bddca894 | 353 | |
0817b62c BB |
354 | static int |
355 | clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req, | |
4dff95dc SB |
356 | unsigned long flags) |
357 | { | |
358 | struct clk_core *core = hw->core, *parent, *best_parent = NULL; | |
0817b62c BB |
359 | int i, num_parents, ret; |
360 | unsigned long best = 0; | |
361 | struct clk_rate_request parent_req = *req; | |
b2476490 | 362 | |
4dff95dc SB |
363 | /* if NO_REPARENT flag set, pass through to current parent */ |
364 | if (core->flags & CLK_SET_RATE_NO_REPARENT) { | |
365 | parent = core->parent; | |
0817b62c BB |
366 | if (core->flags & CLK_SET_RATE_PARENT) { |
367 | ret = __clk_determine_rate(parent ? parent->hw : NULL, | |
368 | &parent_req); | |
369 | if (ret) | |
370 | return ret; | |
371 | ||
372 | best = parent_req.rate; | |
373 | } else if (parent) { | |
4dff95dc | 374 | best = clk_core_get_rate_nolock(parent); |
0817b62c | 375 | } else { |
4dff95dc | 376 | best = clk_core_get_rate_nolock(core); |
0817b62c BB |
377 | } |
378 | ||
4dff95dc SB |
379 | goto out; |
380 | } | |
b2476490 | 381 | |
4dff95dc SB |
382 | /* find the parent that can provide the fastest rate <= rate */ |
383 | num_parents = core->num_parents; | |
384 | for (i = 0; i < num_parents; i++) { | |
385 | parent = clk_core_get_parent_by_index(core, i); | |
386 | if (!parent) | |
387 | continue; | |
0817b62c BB |
388 | |
389 | if (core->flags & CLK_SET_RATE_PARENT) { | |
390 | parent_req = *req; | |
391 | ret = __clk_determine_rate(parent->hw, &parent_req); | |
392 | if (ret) | |
393 | continue; | |
394 | } else { | |
395 | parent_req.rate = clk_core_get_rate_nolock(parent); | |
396 | } | |
397 | ||
398 | if (mux_is_better_rate(req->rate, parent_req.rate, | |
399 | best, flags)) { | |
4dff95dc | 400 | best_parent = parent; |
0817b62c | 401 | best = parent_req.rate; |
4dff95dc SB |
402 | } |
403 | } | |
b2476490 | 404 | |
57d866e6 BB |
405 | if (!best_parent) |
406 | return -EINVAL; | |
407 | ||
4dff95dc SB |
408 | out: |
409 | if (best_parent) | |
0817b62c BB |
410 | req->best_parent_hw = best_parent->hw; |
411 | req->best_parent_rate = best; | |
412 | req->rate = best; | |
b2476490 | 413 | |
0817b62c | 414 | return 0; |
b33d212f | 415 | } |
4dff95dc SB |
416 | |
417 | struct clk *__clk_lookup(const char *name) | |
fcb0ee6a | 418 | { |
4dff95dc SB |
419 | struct clk_core *core = clk_core_lookup(name); |
420 | ||
421 | return !core ? NULL : core->hw->clk; | |
fcb0ee6a | 422 | } |
b2476490 | 423 | |
4dff95dc SB |
424 | static void clk_core_get_boundaries(struct clk_core *core, |
425 | unsigned long *min_rate, | |
426 | unsigned long *max_rate) | |
1c155b3d | 427 | { |
4dff95dc | 428 | struct clk *clk_user; |
1c155b3d | 429 | |
9783c0d9 SB |
430 | *min_rate = core->min_rate; |
431 | *max_rate = core->max_rate; | |
496eadf8 | 432 | |
4dff95dc SB |
433 | hlist_for_each_entry(clk_user, &core->clks, clks_node) |
434 | *min_rate = max(*min_rate, clk_user->min_rate); | |
1c155b3d | 435 | |
4dff95dc SB |
436 | hlist_for_each_entry(clk_user, &core->clks, clks_node) |
437 | *max_rate = min(*max_rate, clk_user->max_rate); | |
438 | } | |
1c155b3d | 439 | |
9783c0d9 SB |
440 | void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate, |
441 | unsigned long max_rate) | |
442 | { | |
443 | hw->core->min_rate = min_rate; | |
444 | hw->core->max_rate = max_rate; | |
445 | } | |
446 | EXPORT_SYMBOL_GPL(clk_hw_set_rate_range); | |
447 | ||
4dff95dc SB |
448 | /* |
449 | * Helper for finding best parent to provide a given frequency. This can be used | |
450 | * directly as a determine_rate callback (e.g. for a mux), or from a more | |
451 | * complex clock that may combine a mux with other operations. | |
452 | */ | |
0817b62c BB |
453 | int __clk_mux_determine_rate(struct clk_hw *hw, |
454 | struct clk_rate_request *req) | |
4dff95dc | 455 | { |
0817b62c | 456 | return clk_mux_determine_rate_flags(hw, req, 0); |
1c155b3d | 457 | } |
4dff95dc | 458 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate); |
1c155b3d | 459 | |
0817b62c BB |
460 | int __clk_mux_determine_rate_closest(struct clk_hw *hw, |
461 | struct clk_rate_request *req) | |
b2476490 | 462 | { |
0817b62c | 463 | return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST); |
4dff95dc SB |
464 | } |
465 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest); | |
b2476490 | 466 | |
4dff95dc | 467 | /*** clk api ***/ |
496eadf8 | 468 | |
4dff95dc SB |
469 | static void clk_core_unprepare(struct clk_core *core) |
470 | { | |
a6334725 SB |
471 | lockdep_assert_held(&prepare_lock); |
472 | ||
4dff95dc SB |
473 | if (!core) |
474 | return; | |
b2476490 | 475 | |
4dff95dc SB |
476 | if (WARN_ON(core->prepare_count == 0)) |
477 | return; | |
b2476490 | 478 | |
2e20fbf5 LJ |
479 | if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL)) |
480 | return; | |
481 | ||
4dff95dc SB |
482 | if (--core->prepare_count > 0) |
483 | return; | |
b2476490 | 484 | |
4dff95dc | 485 | WARN_ON(core->enable_count > 0); |
b2476490 | 486 | |
4dff95dc | 487 | trace_clk_unprepare(core); |
b2476490 | 488 | |
4dff95dc SB |
489 | if (core->ops->unprepare) |
490 | core->ops->unprepare(core->hw); | |
491 | ||
492 | trace_clk_unprepare_complete(core); | |
493 | clk_core_unprepare(core->parent); | |
b2476490 MT |
494 | } |
495 | ||
a6adc30b DA |
496 | static void clk_core_unprepare_lock(struct clk_core *core) |
497 | { | |
498 | clk_prepare_lock(); | |
499 | clk_core_unprepare(core); | |
500 | clk_prepare_unlock(); | |
501 | } | |
502 | ||
4dff95dc SB |
503 | /** |
504 | * clk_unprepare - undo preparation of a clock source | |
505 | * @clk: the clk being unprepared | |
506 | * | |
507 | * clk_unprepare may sleep, which differentiates it from clk_disable. In a | |
508 | * simple case, clk_unprepare can be used instead of clk_disable to gate a clk | |
509 | * if the operation may sleep. One example is a clk which is accessed over | |
510 | * I2c. In the complex case a clk gate operation may require a fast and a slow | |
511 | * part. It is this reason that clk_unprepare and clk_disable are not mutually | |
512 | * exclusive. In fact clk_disable must be called before clk_unprepare. | |
513 | */ | |
514 | void clk_unprepare(struct clk *clk) | |
1e435256 | 515 | { |
4dff95dc SB |
516 | if (IS_ERR_OR_NULL(clk)) |
517 | return; | |
518 | ||
a6adc30b | 519 | clk_core_unprepare_lock(clk->core); |
1e435256 | 520 | } |
4dff95dc | 521 | EXPORT_SYMBOL_GPL(clk_unprepare); |
1e435256 | 522 | |
4dff95dc | 523 | static int clk_core_prepare(struct clk_core *core) |
b2476490 | 524 | { |
4dff95dc | 525 | int ret = 0; |
b2476490 | 526 | |
a6334725 SB |
527 | lockdep_assert_held(&prepare_lock); |
528 | ||
4dff95dc | 529 | if (!core) |
1e435256 | 530 | return 0; |
1e435256 | 531 | |
4dff95dc SB |
532 | if (core->prepare_count == 0) { |
533 | ret = clk_core_prepare(core->parent); | |
534 | if (ret) | |
535 | return ret; | |
b2476490 | 536 | |
4dff95dc | 537 | trace_clk_prepare(core); |
b2476490 | 538 | |
4dff95dc SB |
539 | if (core->ops->prepare) |
540 | ret = core->ops->prepare(core->hw); | |
b2476490 | 541 | |
4dff95dc | 542 | trace_clk_prepare_complete(core); |
1c155b3d | 543 | |
4dff95dc SB |
544 | if (ret) { |
545 | clk_core_unprepare(core->parent); | |
546 | return ret; | |
547 | } | |
548 | } | |
1c155b3d | 549 | |
4dff95dc | 550 | core->prepare_count++; |
b2476490 MT |
551 | |
552 | return 0; | |
553 | } | |
b2476490 | 554 | |
a6adc30b DA |
555 | static int clk_core_prepare_lock(struct clk_core *core) |
556 | { | |
557 | int ret; | |
558 | ||
559 | clk_prepare_lock(); | |
560 | ret = clk_core_prepare(core); | |
561 | clk_prepare_unlock(); | |
562 | ||
563 | return ret; | |
564 | } | |
565 | ||
4dff95dc SB |
566 | /** |
567 | * clk_prepare - prepare a clock source | |
568 | * @clk: the clk being prepared | |
569 | * | |
570 | * clk_prepare may sleep, which differentiates it from clk_enable. In a simple | |
571 | * case, clk_prepare can be used instead of clk_enable to ungate a clk if the | |
572 | * operation may sleep. One example is a clk which is accessed over I2c. In | |
573 | * the complex case a clk ungate operation may require a fast and a slow part. | |
574 | * It is this reason that clk_prepare and clk_enable are not mutually | |
575 | * exclusive. In fact clk_prepare must be called before clk_enable. | |
576 | * Returns 0 on success, -EERROR otherwise. | |
577 | */ | |
578 | int clk_prepare(struct clk *clk) | |
b2476490 | 579 | { |
4dff95dc SB |
580 | if (!clk) |
581 | return 0; | |
b2476490 | 582 | |
a6adc30b | 583 | return clk_core_prepare_lock(clk->core); |
b2476490 | 584 | } |
4dff95dc | 585 | EXPORT_SYMBOL_GPL(clk_prepare); |
b2476490 | 586 | |
4dff95dc | 587 | static void clk_core_disable(struct clk_core *core) |
b2476490 | 588 | { |
a6334725 SB |
589 | lockdep_assert_held(&enable_lock); |
590 | ||
4dff95dc SB |
591 | if (!core) |
592 | return; | |
035a61c3 | 593 | |
4dff95dc SB |
594 | if (WARN_ON(core->enable_count == 0)) |
595 | return; | |
b2476490 | 596 | |
2e20fbf5 LJ |
597 | if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL)) |
598 | return; | |
599 | ||
4dff95dc SB |
600 | if (--core->enable_count > 0) |
601 | return; | |
035a61c3 | 602 | |
2f87a6ea | 603 | trace_clk_disable_rcuidle(core); |
035a61c3 | 604 | |
4dff95dc SB |
605 | if (core->ops->disable) |
606 | core->ops->disable(core->hw); | |
035a61c3 | 607 | |
2f87a6ea | 608 | trace_clk_disable_complete_rcuidle(core); |
035a61c3 | 609 | |
4dff95dc | 610 | clk_core_disable(core->parent); |
035a61c3 | 611 | } |
7ef3dcc8 | 612 | |
a6adc30b DA |
613 | static void clk_core_disable_lock(struct clk_core *core) |
614 | { | |
615 | unsigned long flags; | |
616 | ||
617 | flags = clk_enable_lock(); | |
618 | clk_core_disable(core); | |
619 | clk_enable_unlock(flags); | |
620 | } | |
621 | ||
4dff95dc SB |
622 | /** |
623 | * clk_disable - gate a clock | |
624 | * @clk: the clk being gated | |
625 | * | |
626 | * clk_disable must not sleep, which differentiates it from clk_unprepare. In | |
627 | * a simple case, clk_disable can be used instead of clk_unprepare to gate a | |
628 | * clk if the operation is fast and will never sleep. One example is a | |
629 | * SoC-internal clk which is controlled via simple register writes. In the | |
630 | * complex case a clk gate operation may require a fast and a slow part. It is | |
631 | * this reason that clk_unprepare and clk_disable are not mutually exclusive. | |
632 | * In fact clk_disable must be called before clk_unprepare. | |
633 | */ | |
634 | void clk_disable(struct clk *clk) | |
b2476490 | 635 | { |
4dff95dc SB |
636 | if (IS_ERR_OR_NULL(clk)) |
637 | return; | |
638 | ||
a6adc30b | 639 | clk_core_disable_lock(clk->core); |
b2476490 | 640 | } |
4dff95dc | 641 | EXPORT_SYMBOL_GPL(clk_disable); |
b2476490 | 642 | |
4dff95dc | 643 | static int clk_core_enable(struct clk_core *core) |
b2476490 | 644 | { |
4dff95dc | 645 | int ret = 0; |
b2476490 | 646 | |
a6334725 SB |
647 | lockdep_assert_held(&enable_lock); |
648 | ||
4dff95dc SB |
649 | if (!core) |
650 | return 0; | |
b2476490 | 651 | |
4dff95dc SB |
652 | if (WARN_ON(core->prepare_count == 0)) |
653 | return -ESHUTDOWN; | |
b2476490 | 654 | |
4dff95dc SB |
655 | if (core->enable_count == 0) { |
656 | ret = clk_core_enable(core->parent); | |
b2476490 | 657 | |
4dff95dc SB |
658 | if (ret) |
659 | return ret; | |
b2476490 | 660 | |
f17a0dd1 | 661 | trace_clk_enable_rcuidle(core); |
035a61c3 | 662 | |
4dff95dc SB |
663 | if (core->ops->enable) |
664 | ret = core->ops->enable(core->hw); | |
035a61c3 | 665 | |
f17a0dd1 | 666 | trace_clk_enable_complete_rcuidle(core); |
4dff95dc SB |
667 | |
668 | if (ret) { | |
669 | clk_core_disable(core->parent); | |
670 | return ret; | |
671 | } | |
672 | } | |
673 | ||
674 | core->enable_count++; | |
675 | return 0; | |
035a61c3 | 676 | } |
b2476490 | 677 | |
a6adc30b DA |
678 | static int clk_core_enable_lock(struct clk_core *core) |
679 | { | |
680 | unsigned long flags; | |
681 | int ret; | |
682 | ||
683 | flags = clk_enable_lock(); | |
684 | ret = clk_core_enable(core); | |
685 | clk_enable_unlock(flags); | |
686 | ||
687 | return ret; | |
688 | } | |
689 | ||
4dff95dc SB |
690 | /** |
691 | * clk_enable - ungate a clock | |
692 | * @clk: the clk being ungated | |
693 | * | |
694 | * clk_enable must not sleep, which differentiates it from clk_prepare. In a | |
695 | * simple case, clk_enable can be used instead of clk_prepare to ungate a clk | |
696 | * if the operation will never sleep. One example is a SoC-internal clk which | |
697 | * is controlled via simple register writes. In the complex case a clk ungate | |
698 | * operation may require a fast and a slow part. It is this reason that | |
699 | * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare | |
700 | * must be called before clk_enable. Returns 0 on success, -EERROR | |
701 | * otherwise. | |
702 | */ | |
703 | int clk_enable(struct clk *clk) | |
5279fc40 | 704 | { |
4dff95dc | 705 | if (!clk) |
5279fc40 BB |
706 | return 0; |
707 | ||
a6adc30b DA |
708 | return clk_core_enable_lock(clk->core); |
709 | } | |
710 | EXPORT_SYMBOL_GPL(clk_enable); | |
711 | ||
712 | static int clk_core_prepare_enable(struct clk_core *core) | |
713 | { | |
714 | int ret; | |
715 | ||
716 | ret = clk_core_prepare_lock(core); | |
717 | if (ret) | |
718 | return ret; | |
719 | ||
720 | ret = clk_core_enable_lock(core); | |
721 | if (ret) | |
722 | clk_core_unprepare_lock(core); | |
5279fc40 | 723 | |
4dff95dc | 724 | return ret; |
b2476490 | 725 | } |
a6adc30b DA |
726 | |
727 | static void clk_core_disable_unprepare(struct clk_core *core) | |
728 | { | |
729 | clk_core_disable_lock(core); | |
730 | clk_core_unprepare_lock(core); | |
731 | } | |
b2476490 | 732 | |
7ec986ef DA |
733 | static void clk_unprepare_unused_subtree(struct clk_core *core) |
734 | { | |
735 | struct clk_core *child; | |
736 | ||
737 | lockdep_assert_held(&prepare_lock); | |
738 | ||
739 | hlist_for_each_entry(child, &core->children, child_node) | |
740 | clk_unprepare_unused_subtree(child); | |
741 | ||
742 | if (core->prepare_count) | |
743 | return; | |
744 | ||
745 | if (core->flags & CLK_IGNORE_UNUSED) | |
746 | return; | |
747 | ||
748 | if (clk_core_is_prepared(core)) { | |
749 | trace_clk_unprepare(core); | |
750 | if (core->ops->unprepare_unused) | |
751 | core->ops->unprepare_unused(core->hw); | |
752 | else if (core->ops->unprepare) | |
753 | core->ops->unprepare(core->hw); | |
754 | trace_clk_unprepare_complete(core); | |
755 | } | |
756 | } | |
757 | ||
758 | static void clk_disable_unused_subtree(struct clk_core *core) | |
759 | { | |
760 | struct clk_core *child; | |
761 | unsigned long flags; | |
762 | ||
763 | lockdep_assert_held(&prepare_lock); | |
764 | ||
765 | hlist_for_each_entry(child, &core->children, child_node) | |
766 | clk_disable_unused_subtree(child); | |
767 | ||
a4b3518d DA |
768 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
769 | clk_core_prepare_enable(core->parent); | |
770 | ||
7ec986ef DA |
771 | flags = clk_enable_lock(); |
772 | ||
773 | if (core->enable_count) | |
774 | goto unlock_out; | |
775 | ||
776 | if (core->flags & CLK_IGNORE_UNUSED) | |
777 | goto unlock_out; | |
778 | ||
779 | /* | |
780 | * some gate clocks have special needs during the disable-unused | |
781 | * sequence. call .disable_unused if available, otherwise fall | |
782 | * back to .disable | |
783 | */ | |
784 | if (clk_core_is_enabled(core)) { | |
785 | trace_clk_disable(core); | |
786 | if (core->ops->disable_unused) | |
787 | core->ops->disable_unused(core->hw); | |
788 | else if (core->ops->disable) | |
789 | core->ops->disable(core->hw); | |
790 | trace_clk_disable_complete(core); | |
791 | } | |
792 | ||
793 | unlock_out: | |
794 | clk_enable_unlock(flags); | |
a4b3518d DA |
795 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
796 | clk_core_disable_unprepare(core->parent); | |
7ec986ef DA |
797 | } |
798 | ||
799 | static bool clk_ignore_unused; | |
800 | static int __init clk_ignore_unused_setup(char *__unused) | |
801 | { | |
802 | clk_ignore_unused = true; | |
803 | return 1; | |
804 | } | |
805 | __setup("clk_ignore_unused", clk_ignore_unused_setup); | |
806 | ||
807 | static int clk_disable_unused(void) | |
808 | { | |
809 | struct clk_core *core; | |
810 | ||
811 | if (clk_ignore_unused) { | |
812 | pr_warn("clk: Not disabling unused clocks\n"); | |
813 | return 0; | |
814 | } | |
815 | ||
816 | clk_prepare_lock(); | |
817 | ||
818 | hlist_for_each_entry(core, &clk_root_list, child_node) | |
819 | clk_disable_unused_subtree(core); | |
820 | ||
821 | hlist_for_each_entry(core, &clk_orphan_list, child_node) | |
822 | clk_disable_unused_subtree(core); | |
823 | ||
824 | hlist_for_each_entry(core, &clk_root_list, child_node) | |
825 | clk_unprepare_unused_subtree(core); | |
826 | ||
827 | hlist_for_each_entry(core, &clk_orphan_list, child_node) | |
828 | clk_unprepare_unused_subtree(core); | |
829 | ||
830 | clk_prepare_unlock(); | |
831 | ||
832 | return 0; | |
833 | } | |
834 | late_initcall_sync(clk_disable_unused); | |
835 | ||
0817b62c BB |
836 | static int clk_core_round_rate_nolock(struct clk_core *core, |
837 | struct clk_rate_request *req) | |
3d6ee287 | 838 | { |
4dff95dc | 839 | struct clk_core *parent; |
0817b62c | 840 | long rate; |
4dff95dc SB |
841 | |
842 | lockdep_assert_held(&prepare_lock); | |
3d6ee287 | 843 | |
d6968fca | 844 | if (!core) |
4dff95dc | 845 | return 0; |
3d6ee287 | 846 | |
4dff95dc | 847 | parent = core->parent; |
0817b62c BB |
848 | if (parent) { |
849 | req->best_parent_hw = parent->hw; | |
850 | req->best_parent_rate = parent->rate; | |
851 | } else { | |
852 | req->best_parent_hw = NULL; | |
853 | req->best_parent_rate = 0; | |
854 | } | |
3d6ee287 | 855 | |
4dff95dc | 856 | if (core->ops->determine_rate) { |
0817b62c BB |
857 | return core->ops->determine_rate(core->hw, req); |
858 | } else if (core->ops->round_rate) { | |
859 | rate = core->ops->round_rate(core->hw, req->rate, | |
860 | &req->best_parent_rate); | |
861 | if (rate < 0) | |
862 | return rate; | |
863 | ||
864 | req->rate = rate; | |
865 | } else if (core->flags & CLK_SET_RATE_PARENT) { | |
866 | return clk_core_round_rate_nolock(parent, req); | |
867 | } else { | |
868 | req->rate = core->rate; | |
869 | } | |
870 | ||
871 | return 0; | |
3d6ee287 UH |
872 | } |
873 | ||
4dff95dc SB |
874 | /** |
875 | * __clk_determine_rate - get the closest rate actually supported by a clock | |
876 | * @hw: determine the rate of this clock | |
2d5b520c | 877 | * @req: target rate request |
4dff95dc | 878 | * |
6e5ab41b | 879 | * Useful for clk_ops such as .set_rate and .determine_rate. |
4dff95dc | 880 | */ |
0817b62c | 881 | int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req) |
035a61c3 | 882 | { |
0817b62c BB |
883 | if (!hw) { |
884 | req->rate = 0; | |
4dff95dc | 885 | return 0; |
0817b62c | 886 | } |
035a61c3 | 887 | |
0817b62c | 888 | return clk_core_round_rate_nolock(hw->core, req); |
035a61c3 | 889 | } |
4dff95dc | 890 | EXPORT_SYMBOL_GPL(__clk_determine_rate); |
035a61c3 | 891 | |
1a9c069c SB |
892 | unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate) |
893 | { | |
894 | int ret; | |
895 | struct clk_rate_request req; | |
896 | ||
897 | clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); | |
898 | req.rate = rate; | |
899 | ||
900 | ret = clk_core_round_rate_nolock(hw->core, &req); | |
901 | if (ret) | |
902 | return 0; | |
903 | ||
904 | return req.rate; | |
905 | } | |
906 | EXPORT_SYMBOL_GPL(clk_hw_round_rate); | |
907 | ||
4dff95dc SB |
908 | /** |
909 | * clk_round_rate - round the given rate for a clk | |
910 | * @clk: the clk for which we are rounding a rate | |
911 | * @rate: the rate which is to be rounded | |
912 | * | |
913 | * Takes in a rate as input and rounds it to a rate that the clk can actually | |
914 | * use which is then returned. If clk doesn't support round_rate operation | |
915 | * then the parent rate is returned. | |
916 | */ | |
917 | long clk_round_rate(struct clk *clk, unsigned long rate) | |
035a61c3 | 918 | { |
fc4a05d4 SB |
919 | struct clk_rate_request req; |
920 | int ret; | |
4dff95dc | 921 | |
035a61c3 | 922 | if (!clk) |
4dff95dc | 923 | return 0; |
035a61c3 | 924 | |
4dff95dc | 925 | clk_prepare_lock(); |
fc4a05d4 SB |
926 | |
927 | clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); | |
928 | req.rate = rate; | |
929 | ||
930 | ret = clk_core_round_rate_nolock(clk->core, &req); | |
4dff95dc SB |
931 | clk_prepare_unlock(); |
932 | ||
fc4a05d4 SB |
933 | if (ret) |
934 | return ret; | |
935 | ||
936 | return req.rate; | |
035a61c3 | 937 | } |
4dff95dc | 938 | EXPORT_SYMBOL_GPL(clk_round_rate); |
b2476490 | 939 | |
4dff95dc SB |
940 | /** |
941 | * __clk_notify - call clk notifier chain | |
942 | * @core: clk that is changing rate | |
943 | * @msg: clk notifier type (see include/linux/clk.h) | |
944 | * @old_rate: old clk rate | |
945 | * @new_rate: new clk rate | |
946 | * | |
947 | * Triggers a notifier call chain on the clk rate-change notification | |
948 | * for 'clk'. Passes a pointer to the struct clk and the previous | |
949 | * and current rates to the notifier callback. Intended to be called by | |
950 | * internal clock code only. Returns NOTIFY_DONE from the last driver | |
951 | * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if | |
952 | * a driver returns that. | |
953 | */ | |
954 | static int __clk_notify(struct clk_core *core, unsigned long msg, | |
955 | unsigned long old_rate, unsigned long new_rate) | |
b2476490 | 956 | { |
4dff95dc SB |
957 | struct clk_notifier *cn; |
958 | struct clk_notifier_data cnd; | |
959 | int ret = NOTIFY_DONE; | |
b2476490 | 960 | |
4dff95dc SB |
961 | cnd.old_rate = old_rate; |
962 | cnd.new_rate = new_rate; | |
b2476490 | 963 | |
4dff95dc SB |
964 | list_for_each_entry(cn, &clk_notifier_list, node) { |
965 | if (cn->clk->core == core) { | |
966 | cnd.clk = cn->clk; | |
967 | ret = srcu_notifier_call_chain(&cn->notifier_head, msg, | |
968 | &cnd); | |
17c34c56 PDS |
969 | if (ret & NOTIFY_STOP_MASK) |
970 | return ret; | |
4dff95dc | 971 | } |
b2476490 MT |
972 | } |
973 | ||
4dff95dc | 974 | return ret; |
b2476490 MT |
975 | } |
976 | ||
4dff95dc SB |
977 | /** |
978 | * __clk_recalc_accuracies | |
979 | * @core: first clk in the subtree | |
980 | * | |
981 | * Walks the subtree of clks starting with clk and recalculates accuracies as | |
982 | * it goes. Note that if a clk does not implement the .recalc_accuracy | |
6e5ab41b | 983 | * callback then it is assumed that the clock will take on the accuracy of its |
4dff95dc | 984 | * parent. |
4dff95dc SB |
985 | */ |
986 | static void __clk_recalc_accuracies(struct clk_core *core) | |
b2476490 | 987 | { |
4dff95dc SB |
988 | unsigned long parent_accuracy = 0; |
989 | struct clk_core *child; | |
b2476490 | 990 | |
4dff95dc | 991 | lockdep_assert_held(&prepare_lock); |
b2476490 | 992 | |
4dff95dc SB |
993 | if (core->parent) |
994 | parent_accuracy = core->parent->accuracy; | |
b2476490 | 995 | |
4dff95dc SB |
996 | if (core->ops->recalc_accuracy) |
997 | core->accuracy = core->ops->recalc_accuracy(core->hw, | |
998 | parent_accuracy); | |
999 | else | |
1000 | core->accuracy = parent_accuracy; | |
b2476490 | 1001 | |
4dff95dc SB |
1002 | hlist_for_each_entry(child, &core->children, child_node) |
1003 | __clk_recalc_accuracies(child); | |
b2476490 MT |
1004 | } |
1005 | ||
4dff95dc | 1006 | static long clk_core_get_accuracy(struct clk_core *core) |
e366fdd7 | 1007 | { |
4dff95dc | 1008 | unsigned long accuracy; |
15a02c1f | 1009 | |
4dff95dc SB |
1010 | clk_prepare_lock(); |
1011 | if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) | |
1012 | __clk_recalc_accuracies(core); | |
15a02c1f | 1013 | |
4dff95dc SB |
1014 | accuracy = __clk_get_accuracy(core); |
1015 | clk_prepare_unlock(); | |
e366fdd7 | 1016 | |
4dff95dc | 1017 | return accuracy; |
e366fdd7 | 1018 | } |
15a02c1f | 1019 | |
4dff95dc SB |
1020 | /** |
1021 | * clk_get_accuracy - return the accuracy of clk | |
1022 | * @clk: the clk whose accuracy is being returned | |
1023 | * | |
1024 | * Simply returns the cached accuracy of the clk, unless | |
1025 | * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be | |
1026 | * issued. | |
1027 | * If clk is NULL then returns 0. | |
1028 | */ | |
1029 | long clk_get_accuracy(struct clk *clk) | |
035a61c3 | 1030 | { |
4dff95dc SB |
1031 | if (!clk) |
1032 | return 0; | |
035a61c3 | 1033 | |
4dff95dc | 1034 | return clk_core_get_accuracy(clk->core); |
035a61c3 | 1035 | } |
4dff95dc | 1036 | EXPORT_SYMBOL_GPL(clk_get_accuracy); |
035a61c3 | 1037 | |
4dff95dc SB |
1038 | static unsigned long clk_recalc(struct clk_core *core, |
1039 | unsigned long parent_rate) | |
1c8e6004 | 1040 | { |
4dff95dc SB |
1041 | if (core->ops->recalc_rate) |
1042 | return core->ops->recalc_rate(core->hw, parent_rate); | |
1043 | return parent_rate; | |
1c8e6004 TV |
1044 | } |
1045 | ||
4dff95dc SB |
1046 | /** |
1047 | * __clk_recalc_rates | |
1048 | * @core: first clk in the subtree | |
1049 | * @msg: notification type (see include/linux/clk.h) | |
1050 | * | |
1051 | * Walks the subtree of clks starting with clk and recalculates rates as it | |
1052 | * goes. Note that if a clk does not implement the .recalc_rate callback then | |
1053 | * it is assumed that the clock will take on the rate of its parent. | |
1054 | * | |
1055 | * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, | |
1056 | * if necessary. | |
15a02c1f | 1057 | */ |
4dff95dc | 1058 | static void __clk_recalc_rates(struct clk_core *core, unsigned long msg) |
15a02c1f | 1059 | { |
4dff95dc SB |
1060 | unsigned long old_rate; |
1061 | unsigned long parent_rate = 0; | |
1062 | struct clk_core *child; | |
e366fdd7 | 1063 | |
4dff95dc | 1064 | lockdep_assert_held(&prepare_lock); |
15a02c1f | 1065 | |
4dff95dc | 1066 | old_rate = core->rate; |
b2476490 | 1067 | |
4dff95dc SB |
1068 | if (core->parent) |
1069 | parent_rate = core->parent->rate; | |
b2476490 | 1070 | |
4dff95dc | 1071 | core->rate = clk_recalc(core, parent_rate); |
b2476490 | 1072 | |
4dff95dc SB |
1073 | /* |
1074 | * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE | |
1075 | * & ABORT_RATE_CHANGE notifiers | |
1076 | */ | |
1077 | if (core->notifier_count && msg) | |
1078 | __clk_notify(core, msg, old_rate, core->rate); | |
b2476490 | 1079 | |
4dff95dc SB |
1080 | hlist_for_each_entry(child, &core->children, child_node) |
1081 | __clk_recalc_rates(child, msg); | |
1082 | } | |
b2476490 | 1083 | |
4dff95dc SB |
1084 | static unsigned long clk_core_get_rate(struct clk_core *core) |
1085 | { | |
1086 | unsigned long rate; | |
dfc202ea | 1087 | |
4dff95dc | 1088 | clk_prepare_lock(); |
b2476490 | 1089 | |
4dff95dc SB |
1090 | if (core && (core->flags & CLK_GET_RATE_NOCACHE)) |
1091 | __clk_recalc_rates(core, 0); | |
1092 | ||
1093 | rate = clk_core_get_rate_nolock(core); | |
1094 | clk_prepare_unlock(); | |
1095 | ||
1096 | return rate; | |
b2476490 MT |
1097 | } |
1098 | ||
1099 | /** | |
4dff95dc SB |
1100 | * clk_get_rate - return the rate of clk |
1101 | * @clk: the clk whose rate is being returned | |
b2476490 | 1102 | * |
4dff95dc SB |
1103 | * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag |
1104 | * is set, which means a recalc_rate will be issued. | |
1105 | * If clk is NULL then returns 0. | |
b2476490 | 1106 | */ |
4dff95dc | 1107 | unsigned long clk_get_rate(struct clk *clk) |
b2476490 | 1108 | { |
4dff95dc SB |
1109 | if (!clk) |
1110 | return 0; | |
63589e92 | 1111 | |
4dff95dc | 1112 | return clk_core_get_rate(clk->core); |
b2476490 | 1113 | } |
4dff95dc | 1114 | EXPORT_SYMBOL_GPL(clk_get_rate); |
b2476490 | 1115 | |
4dff95dc SB |
1116 | static int clk_fetch_parent_index(struct clk_core *core, |
1117 | struct clk_core *parent) | |
b2476490 | 1118 | { |
4dff95dc | 1119 | int i; |
b2476490 | 1120 | |
508f884a MY |
1121 | if (!parent) |
1122 | return -EINVAL; | |
1123 | ||
470b5e2f MY |
1124 | for (i = 0; i < core->num_parents; i++) |
1125 | if (clk_core_get_parent_by_index(core, i) == parent) | |
4dff95dc | 1126 | return i; |
b2476490 | 1127 | |
4dff95dc | 1128 | return -EINVAL; |
b2476490 MT |
1129 | } |
1130 | ||
e6500344 HS |
1131 | /* |
1132 | * Update the orphan status of @core and all its children. | |
1133 | */ | |
1134 | static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan) | |
1135 | { | |
1136 | struct clk_core *child; | |
1137 | ||
1138 | core->orphan = is_orphan; | |
1139 | ||
1140 | hlist_for_each_entry(child, &core->children, child_node) | |
1141 | clk_core_update_orphan_status(child, is_orphan); | |
1142 | } | |
1143 | ||
4dff95dc | 1144 | static void clk_reparent(struct clk_core *core, struct clk_core *new_parent) |
b2476490 | 1145 | { |
e6500344 HS |
1146 | bool was_orphan = core->orphan; |
1147 | ||
4dff95dc | 1148 | hlist_del(&core->child_node); |
035a61c3 | 1149 | |
4dff95dc | 1150 | if (new_parent) { |
e6500344 HS |
1151 | bool becomes_orphan = new_parent->orphan; |
1152 | ||
4dff95dc SB |
1153 | /* avoid duplicate POST_RATE_CHANGE notifications */ |
1154 | if (new_parent->new_child == core) | |
1155 | new_parent->new_child = NULL; | |
b2476490 | 1156 | |
4dff95dc | 1157 | hlist_add_head(&core->child_node, &new_parent->children); |
e6500344 HS |
1158 | |
1159 | if (was_orphan != becomes_orphan) | |
1160 | clk_core_update_orphan_status(core, becomes_orphan); | |
4dff95dc SB |
1161 | } else { |
1162 | hlist_add_head(&core->child_node, &clk_orphan_list); | |
e6500344 HS |
1163 | if (!was_orphan) |
1164 | clk_core_update_orphan_status(core, true); | |
4dff95dc | 1165 | } |
dfc202ea | 1166 | |
4dff95dc | 1167 | core->parent = new_parent; |
035a61c3 TV |
1168 | } |
1169 | ||
4dff95dc SB |
1170 | static struct clk_core *__clk_set_parent_before(struct clk_core *core, |
1171 | struct clk_core *parent) | |
b2476490 MT |
1172 | { |
1173 | unsigned long flags; | |
4dff95dc | 1174 | struct clk_core *old_parent = core->parent; |
b2476490 | 1175 | |
4dff95dc | 1176 | /* |
fc8726a2 DA |
1177 | * 1. enable parents for CLK_OPS_PARENT_ENABLE clock |
1178 | * | |
1179 | * 2. Migrate prepare state between parents and prevent race with | |
4dff95dc SB |
1180 | * clk_enable(). |
1181 | * | |
1182 | * If the clock is not prepared, then a race with | |
1183 | * clk_enable/disable() is impossible since we already have the | |
1184 | * prepare lock (future calls to clk_enable() need to be preceded by | |
1185 | * a clk_prepare()). | |
1186 | * | |
1187 | * If the clock is prepared, migrate the prepared state to the new | |
1188 | * parent and also protect against a race with clk_enable() by | |
1189 | * forcing the clock and the new parent on. This ensures that all | |
1190 | * future calls to clk_enable() are practically NOPs with respect to | |
1191 | * hardware and software states. | |
1192 | * | |
1193 | * See also: Comment for clk_set_parent() below. | |
1194 | */ | |
fc8726a2 DA |
1195 | |
1196 | /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */ | |
1197 | if (core->flags & CLK_OPS_PARENT_ENABLE) { | |
1198 | clk_core_prepare_enable(old_parent); | |
1199 | clk_core_prepare_enable(parent); | |
1200 | } | |
1201 | ||
1202 | /* migrate prepare count if > 0 */ | |
4dff95dc | 1203 | if (core->prepare_count) { |
fc8726a2 DA |
1204 | clk_core_prepare_enable(parent); |
1205 | clk_core_enable_lock(core); | |
4dff95dc | 1206 | } |
63589e92 | 1207 | |
4dff95dc | 1208 | /* update the clk tree topology */ |
eab89f69 | 1209 | flags = clk_enable_lock(); |
4dff95dc | 1210 | clk_reparent(core, parent); |
eab89f69 | 1211 | clk_enable_unlock(flags); |
4dff95dc SB |
1212 | |
1213 | return old_parent; | |
b2476490 | 1214 | } |
b2476490 | 1215 | |
4dff95dc SB |
1216 | static void __clk_set_parent_after(struct clk_core *core, |
1217 | struct clk_core *parent, | |
1218 | struct clk_core *old_parent) | |
b2476490 | 1219 | { |
4dff95dc SB |
1220 | /* |
1221 | * Finish the migration of prepare state and undo the changes done | |
1222 | * for preventing a race with clk_enable(). | |
1223 | */ | |
1224 | if (core->prepare_count) { | |
fc8726a2 DA |
1225 | clk_core_disable_lock(core); |
1226 | clk_core_disable_unprepare(old_parent); | |
1227 | } | |
1228 | ||
1229 | /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ | |
1230 | if (core->flags & CLK_OPS_PARENT_ENABLE) { | |
1231 | clk_core_disable_unprepare(parent); | |
1232 | clk_core_disable_unprepare(old_parent); | |
4dff95dc SB |
1233 | } |
1234 | } | |
b2476490 | 1235 | |
4dff95dc SB |
1236 | static int __clk_set_parent(struct clk_core *core, struct clk_core *parent, |
1237 | u8 p_index) | |
1238 | { | |
1239 | unsigned long flags; | |
1240 | int ret = 0; | |
1241 | struct clk_core *old_parent; | |
b2476490 | 1242 | |
4dff95dc | 1243 | old_parent = __clk_set_parent_before(core, parent); |
b2476490 | 1244 | |
4dff95dc | 1245 | trace_clk_set_parent(core, parent); |
b2476490 | 1246 | |
4dff95dc SB |
1247 | /* change clock input source */ |
1248 | if (parent && core->ops->set_parent) | |
1249 | ret = core->ops->set_parent(core->hw, p_index); | |
dfc202ea | 1250 | |
4dff95dc | 1251 | trace_clk_set_parent_complete(core, parent); |
dfc202ea | 1252 | |
4dff95dc SB |
1253 | if (ret) { |
1254 | flags = clk_enable_lock(); | |
1255 | clk_reparent(core, old_parent); | |
1256 | clk_enable_unlock(flags); | |
c660b2eb | 1257 | __clk_set_parent_after(core, old_parent, parent); |
dfc202ea | 1258 | |
4dff95dc | 1259 | return ret; |
b2476490 MT |
1260 | } |
1261 | ||
4dff95dc SB |
1262 | __clk_set_parent_after(core, parent, old_parent); |
1263 | ||
b2476490 MT |
1264 | return 0; |
1265 | } | |
1266 | ||
1267 | /** | |
4dff95dc SB |
1268 | * __clk_speculate_rates |
1269 | * @core: first clk in the subtree | |
1270 | * @parent_rate: the "future" rate of clk's parent | |
b2476490 | 1271 | * |
4dff95dc SB |
1272 | * Walks the subtree of clks starting with clk, speculating rates as it |
1273 | * goes and firing off PRE_RATE_CHANGE notifications as necessary. | |
1274 | * | |
1275 | * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending | |
1276 | * pre-rate change notifications and returns early if no clks in the | |
1277 | * subtree have subscribed to the notifications. Note that if a clk does not | |
1278 | * implement the .recalc_rate callback then it is assumed that the clock will | |
1279 | * take on the rate of its parent. | |
b2476490 | 1280 | */ |
4dff95dc SB |
1281 | static int __clk_speculate_rates(struct clk_core *core, |
1282 | unsigned long parent_rate) | |
b2476490 | 1283 | { |
4dff95dc SB |
1284 | struct clk_core *child; |
1285 | unsigned long new_rate; | |
1286 | int ret = NOTIFY_DONE; | |
b2476490 | 1287 | |
4dff95dc | 1288 | lockdep_assert_held(&prepare_lock); |
864e160a | 1289 | |
4dff95dc SB |
1290 | new_rate = clk_recalc(core, parent_rate); |
1291 | ||
1292 | /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ | |
1293 | if (core->notifier_count) | |
1294 | ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); | |
1295 | ||
1296 | if (ret & NOTIFY_STOP_MASK) { | |
1297 | pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", | |
1298 | __func__, core->name, ret); | |
1299 | goto out; | |
1300 | } | |
1301 | ||
1302 | hlist_for_each_entry(child, &core->children, child_node) { | |
1303 | ret = __clk_speculate_rates(child, new_rate); | |
1304 | if (ret & NOTIFY_STOP_MASK) | |
1305 | break; | |
1306 | } | |
b2476490 | 1307 | |
4dff95dc | 1308 | out: |
b2476490 MT |
1309 | return ret; |
1310 | } | |
b2476490 | 1311 | |
4dff95dc SB |
1312 | static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate, |
1313 | struct clk_core *new_parent, u8 p_index) | |
b2476490 | 1314 | { |
4dff95dc | 1315 | struct clk_core *child; |
b2476490 | 1316 | |
4dff95dc SB |
1317 | core->new_rate = new_rate; |
1318 | core->new_parent = new_parent; | |
1319 | core->new_parent_index = p_index; | |
1320 | /* include clk in new parent's PRE_RATE_CHANGE notifications */ | |
1321 | core->new_child = NULL; | |
1322 | if (new_parent && new_parent != core->parent) | |
1323 | new_parent->new_child = core; | |
496eadf8 | 1324 | |
4dff95dc SB |
1325 | hlist_for_each_entry(child, &core->children, child_node) { |
1326 | child->new_rate = clk_recalc(child, new_rate); | |
1327 | clk_calc_subtree(child, child->new_rate, NULL, 0); | |
1328 | } | |
1329 | } | |
b2476490 | 1330 | |
4dff95dc SB |
1331 | /* |
1332 | * calculate the new rates returning the topmost clock that has to be | |
1333 | * changed. | |
1334 | */ | |
1335 | static struct clk_core *clk_calc_new_rates(struct clk_core *core, | |
1336 | unsigned long rate) | |
1337 | { | |
1338 | struct clk_core *top = core; | |
1339 | struct clk_core *old_parent, *parent; | |
4dff95dc SB |
1340 | unsigned long best_parent_rate = 0; |
1341 | unsigned long new_rate; | |
1342 | unsigned long min_rate; | |
1343 | unsigned long max_rate; | |
1344 | int p_index = 0; | |
1345 | long ret; | |
1346 | ||
1347 | /* sanity */ | |
1348 | if (IS_ERR_OR_NULL(core)) | |
1349 | return NULL; | |
1350 | ||
1351 | /* save parent rate, if it exists */ | |
1352 | parent = old_parent = core->parent; | |
71472c0c | 1353 | if (parent) |
4dff95dc | 1354 | best_parent_rate = parent->rate; |
71472c0c | 1355 | |
4dff95dc SB |
1356 | clk_core_get_boundaries(core, &min_rate, &max_rate); |
1357 | ||
1358 | /* find the closest rate and parent clk/rate */ | |
d6968fca | 1359 | if (core->ops->determine_rate) { |
0817b62c BB |
1360 | struct clk_rate_request req; |
1361 | ||
1362 | req.rate = rate; | |
1363 | req.min_rate = min_rate; | |
1364 | req.max_rate = max_rate; | |
1365 | if (parent) { | |
1366 | req.best_parent_hw = parent->hw; | |
1367 | req.best_parent_rate = parent->rate; | |
1368 | } else { | |
1369 | req.best_parent_hw = NULL; | |
1370 | req.best_parent_rate = 0; | |
1371 | } | |
1372 | ||
1373 | ret = core->ops->determine_rate(core->hw, &req); | |
4dff95dc SB |
1374 | if (ret < 0) |
1375 | return NULL; | |
1c8e6004 | 1376 | |
0817b62c BB |
1377 | best_parent_rate = req.best_parent_rate; |
1378 | new_rate = req.rate; | |
1379 | parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; | |
4dff95dc SB |
1380 | } else if (core->ops->round_rate) { |
1381 | ret = core->ops->round_rate(core->hw, rate, | |
0817b62c | 1382 | &best_parent_rate); |
4dff95dc SB |
1383 | if (ret < 0) |
1384 | return NULL; | |
035a61c3 | 1385 | |
4dff95dc SB |
1386 | new_rate = ret; |
1387 | if (new_rate < min_rate || new_rate > max_rate) | |
1388 | return NULL; | |
1389 | } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { | |
1390 | /* pass-through clock without adjustable parent */ | |
1391 | core->new_rate = core->rate; | |
1392 | return NULL; | |
1393 | } else { | |
1394 | /* pass-through clock with adjustable parent */ | |
1395 | top = clk_calc_new_rates(parent, rate); | |
1396 | new_rate = parent->new_rate; | |
1397 | goto out; | |
1398 | } | |
1c8e6004 | 1399 | |
4dff95dc SB |
1400 | /* some clocks must be gated to change parent */ |
1401 | if (parent != old_parent && | |
1402 | (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { | |
1403 | pr_debug("%s: %s not gated but wants to reparent\n", | |
1404 | __func__, core->name); | |
1405 | return NULL; | |
1406 | } | |
b2476490 | 1407 | |
4dff95dc SB |
1408 | /* try finding the new parent index */ |
1409 | if (parent && core->num_parents > 1) { | |
1410 | p_index = clk_fetch_parent_index(core, parent); | |
1411 | if (p_index < 0) { | |
1412 | pr_debug("%s: clk %s can not be parent of clk %s\n", | |
1413 | __func__, parent->name, core->name); | |
1414 | return NULL; | |
1415 | } | |
1416 | } | |
b2476490 | 1417 | |
4dff95dc SB |
1418 | if ((core->flags & CLK_SET_RATE_PARENT) && parent && |
1419 | best_parent_rate != parent->rate) | |
1420 | top = clk_calc_new_rates(parent, best_parent_rate); | |
035a61c3 | 1421 | |
4dff95dc SB |
1422 | out: |
1423 | clk_calc_subtree(core, new_rate, parent, p_index); | |
b2476490 | 1424 | |
4dff95dc | 1425 | return top; |
b2476490 | 1426 | } |
b2476490 | 1427 | |
4dff95dc SB |
1428 | /* |
1429 | * Notify about rate changes in a subtree. Always walk down the whole tree | |
1430 | * so that in case of an error we can walk down the whole tree again and | |
1431 | * abort the change. | |
b2476490 | 1432 | */ |
4dff95dc SB |
1433 | static struct clk_core *clk_propagate_rate_change(struct clk_core *core, |
1434 | unsigned long event) | |
b2476490 | 1435 | { |
4dff95dc | 1436 | struct clk_core *child, *tmp_clk, *fail_clk = NULL; |
b2476490 MT |
1437 | int ret = NOTIFY_DONE; |
1438 | ||
4dff95dc SB |
1439 | if (core->rate == core->new_rate) |
1440 | return NULL; | |
b2476490 | 1441 | |
4dff95dc SB |
1442 | if (core->notifier_count) { |
1443 | ret = __clk_notify(core, event, core->rate, core->new_rate); | |
1444 | if (ret & NOTIFY_STOP_MASK) | |
1445 | fail_clk = core; | |
b2476490 MT |
1446 | } |
1447 | ||
4dff95dc SB |
1448 | hlist_for_each_entry(child, &core->children, child_node) { |
1449 | /* Skip children who will be reparented to another clock */ | |
1450 | if (child->new_parent && child->new_parent != core) | |
1451 | continue; | |
1452 | tmp_clk = clk_propagate_rate_change(child, event); | |
1453 | if (tmp_clk) | |
1454 | fail_clk = tmp_clk; | |
1455 | } | |
5279fc40 | 1456 | |
4dff95dc SB |
1457 | /* handle the new child who might not be in core->children yet */ |
1458 | if (core->new_child) { | |
1459 | tmp_clk = clk_propagate_rate_change(core->new_child, event); | |
1460 | if (tmp_clk) | |
1461 | fail_clk = tmp_clk; | |
1462 | } | |
5279fc40 | 1463 | |
4dff95dc | 1464 | return fail_clk; |
5279fc40 BB |
1465 | } |
1466 | ||
4dff95dc SB |
1467 | /* |
1468 | * walk down a subtree and set the new rates notifying the rate | |
1469 | * change on the way | |
1470 | */ | |
1471 | static void clk_change_rate(struct clk_core *core) | |
035a61c3 | 1472 | { |
4dff95dc SB |
1473 | struct clk_core *child; |
1474 | struct hlist_node *tmp; | |
1475 | unsigned long old_rate; | |
1476 | unsigned long best_parent_rate = 0; | |
1477 | bool skip_set_rate = false; | |
1478 | struct clk_core *old_parent; | |
fc8726a2 | 1479 | struct clk_core *parent = NULL; |
035a61c3 | 1480 | |
4dff95dc | 1481 | old_rate = core->rate; |
035a61c3 | 1482 | |
fc8726a2 DA |
1483 | if (core->new_parent) { |
1484 | parent = core->new_parent; | |
4dff95dc | 1485 | best_parent_rate = core->new_parent->rate; |
fc8726a2 DA |
1486 | } else if (core->parent) { |
1487 | parent = core->parent; | |
4dff95dc | 1488 | best_parent_rate = core->parent->rate; |
fc8726a2 | 1489 | } |
035a61c3 | 1490 | |
2eb8c710 HS |
1491 | if (core->flags & CLK_SET_RATE_UNGATE) { |
1492 | unsigned long flags; | |
1493 | ||
1494 | clk_core_prepare(core); | |
1495 | flags = clk_enable_lock(); | |
1496 | clk_core_enable(core); | |
1497 | clk_enable_unlock(flags); | |
1498 | } | |
1499 | ||
4dff95dc SB |
1500 | if (core->new_parent && core->new_parent != core->parent) { |
1501 | old_parent = __clk_set_parent_before(core, core->new_parent); | |
1502 | trace_clk_set_parent(core, core->new_parent); | |
5279fc40 | 1503 | |
4dff95dc SB |
1504 | if (core->ops->set_rate_and_parent) { |
1505 | skip_set_rate = true; | |
1506 | core->ops->set_rate_and_parent(core->hw, core->new_rate, | |
1507 | best_parent_rate, | |
1508 | core->new_parent_index); | |
1509 | } else if (core->ops->set_parent) { | |
1510 | core->ops->set_parent(core->hw, core->new_parent_index); | |
1511 | } | |
5279fc40 | 1512 | |
4dff95dc SB |
1513 | trace_clk_set_parent_complete(core, core->new_parent); |
1514 | __clk_set_parent_after(core, core->new_parent, old_parent); | |
1515 | } | |
8f2c2db1 | 1516 | |
fc8726a2 DA |
1517 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
1518 | clk_core_prepare_enable(parent); | |
1519 | ||
4dff95dc | 1520 | trace_clk_set_rate(core, core->new_rate); |
b2476490 | 1521 | |
4dff95dc SB |
1522 | if (!skip_set_rate && core->ops->set_rate) |
1523 | core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); | |
496eadf8 | 1524 | |
4dff95dc | 1525 | trace_clk_set_rate_complete(core, core->new_rate); |
b2476490 | 1526 | |
4dff95dc | 1527 | core->rate = clk_recalc(core, best_parent_rate); |
b2476490 | 1528 | |
2eb8c710 HS |
1529 | if (core->flags & CLK_SET_RATE_UNGATE) { |
1530 | unsigned long flags; | |
1531 | ||
1532 | flags = clk_enable_lock(); | |
1533 | clk_core_disable(core); | |
1534 | clk_enable_unlock(flags); | |
1535 | clk_core_unprepare(core); | |
1536 | } | |
1537 | ||
fc8726a2 DA |
1538 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
1539 | clk_core_disable_unprepare(parent); | |
1540 | ||
4dff95dc SB |
1541 | if (core->notifier_count && old_rate != core->rate) |
1542 | __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); | |
b2476490 | 1543 | |
85e88fab MT |
1544 | if (core->flags & CLK_RECALC_NEW_RATES) |
1545 | (void)clk_calc_new_rates(core, core->new_rate); | |
d8d91987 | 1546 | |
b2476490 | 1547 | /* |
4dff95dc SB |
1548 | * Use safe iteration, as change_rate can actually swap parents |
1549 | * for certain clock types. | |
b2476490 | 1550 | */ |
4dff95dc SB |
1551 | hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { |
1552 | /* Skip children who will be reparented to another clock */ | |
1553 | if (child->new_parent && child->new_parent != core) | |
1554 | continue; | |
1555 | clk_change_rate(child); | |
1556 | } | |
b2476490 | 1557 | |
4dff95dc SB |
1558 | /* handle the new child who might not be in core->children yet */ |
1559 | if (core->new_child) | |
1560 | clk_change_rate(core->new_child); | |
b2476490 MT |
1561 | } |
1562 | ||
4dff95dc SB |
1563 | static int clk_core_set_rate_nolock(struct clk_core *core, |
1564 | unsigned long req_rate) | |
a093bde2 | 1565 | { |
4dff95dc SB |
1566 | struct clk_core *top, *fail_clk; |
1567 | unsigned long rate = req_rate; | |
a093bde2 | 1568 | |
4dff95dc SB |
1569 | if (!core) |
1570 | return 0; | |
a093bde2 | 1571 | |
4dff95dc SB |
1572 | /* bail early if nothing to do */ |
1573 | if (rate == clk_core_get_rate_nolock(core)) | |
1574 | return 0; | |
a093bde2 | 1575 | |
4dff95dc SB |
1576 | if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count) |
1577 | return -EBUSY; | |
a093bde2 | 1578 | |
4dff95dc SB |
1579 | /* calculate new rates and get the topmost changed clock */ |
1580 | top = clk_calc_new_rates(core, rate); | |
1581 | if (!top) | |
1582 | return -EINVAL; | |
1583 | ||
1584 | /* notify that we are about to change rates */ | |
1585 | fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); | |
1586 | if (fail_clk) { | |
1587 | pr_debug("%s: failed to set %s rate\n", __func__, | |
1588 | fail_clk->name); | |
1589 | clk_propagate_rate_change(top, ABORT_RATE_CHANGE); | |
1590 | return -EBUSY; | |
1591 | } | |
1592 | ||
1593 | /* change the rates */ | |
1594 | clk_change_rate(top); | |
1595 | ||
1596 | core->req_rate = req_rate; | |
1597 | ||
06b37e4a | 1598 | return 0; |
a093bde2 | 1599 | } |
035a61c3 TV |
1600 | |
1601 | /** | |
4dff95dc SB |
1602 | * clk_set_rate - specify a new rate for clk |
1603 | * @clk: the clk whose rate is being changed | |
1604 | * @rate: the new rate for clk | |
035a61c3 | 1605 | * |
4dff95dc SB |
1606 | * In the simplest case clk_set_rate will only adjust the rate of clk. |
1607 | * | |
1608 | * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to | |
1609 | * propagate up to clk's parent; whether or not this happens depends on the | |
1610 | * outcome of clk's .round_rate implementation. If *parent_rate is unchanged | |
1611 | * after calling .round_rate then upstream parent propagation is ignored. If | |
1612 | * *parent_rate comes back with a new rate for clk's parent then we propagate | |
1613 | * up to clk's parent and set its rate. Upward propagation will continue | |
1614 | * until either a clk does not support the CLK_SET_RATE_PARENT flag or | |
1615 | * .round_rate stops requesting changes to clk's parent_rate. | |
1616 | * | |
1617 | * Rate changes are accomplished via tree traversal that also recalculates the | |
1618 | * rates for the clocks and fires off POST_RATE_CHANGE notifiers. | |
1619 | * | |
1620 | * Returns 0 on success, -EERROR otherwise. | |
035a61c3 | 1621 | */ |
4dff95dc | 1622 | int clk_set_rate(struct clk *clk, unsigned long rate) |
035a61c3 | 1623 | { |
4dff95dc SB |
1624 | int ret; |
1625 | ||
035a61c3 TV |
1626 | if (!clk) |
1627 | return 0; | |
1628 | ||
4dff95dc SB |
1629 | /* prevent racing with updates to the clock topology */ |
1630 | clk_prepare_lock(); | |
da0f0b2c | 1631 | |
4dff95dc | 1632 | ret = clk_core_set_rate_nolock(clk->core, rate); |
da0f0b2c | 1633 | |
4dff95dc | 1634 | clk_prepare_unlock(); |
4935b22c | 1635 | |
4dff95dc | 1636 | return ret; |
4935b22c | 1637 | } |
4dff95dc | 1638 | EXPORT_SYMBOL_GPL(clk_set_rate); |
4935b22c | 1639 | |
4dff95dc SB |
1640 | /** |
1641 | * clk_set_rate_range - set a rate range for a clock source | |
1642 | * @clk: clock source | |
1643 | * @min: desired minimum clock rate in Hz, inclusive | |
1644 | * @max: desired maximum clock rate in Hz, inclusive | |
1645 | * | |
1646 | * Returns success (0) or negative errno. | |
1647 | */ | |
1648 | int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) | |
4935b22c | 1649 | { |
4dff95dc | 1650 | int ret = 0; |
4935b22c | 1651 | |
4dff95dc SB |
1652 | if (!clk) |
1653 | return 0; | |
903efc55 | 1654 | |
4dff95dc SB |
1655 | if (min > max) { |
1656 | pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", | |
1657 | __func__, clk->core->name, clk->dev_id, clk->con_id, | |
1658 | min, max); | |
1659 | return -EINVAL; | |
903efc55 | 1660 | } |
4935b22c | 1661 | |
4dff95dc | 1662 | clk_prepare_lock(); |
4935b22c | 1663 | |
4dff95dc SB |
1664 | if (min != clk->min_rate || max != clk->max_rate) { |
1665 | clk->min_rate = min; | |
1666 | clk->max_rate = max; | |
1667 | ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate); | |
4935b22c JH |
1668 | } |
1669 | ||
4dff95dc | 1670 | clk_prepare_unlock(); |
4935b22c | 1671 | |
4dff95dc | 1672 | return ret; |
3fa2252b | 1673 | } |
4dff95dc | 1674 | EXPORT_SYMBOL_GPL(clk_set_rate_range); |
3fa2252b | 1675 | |
4dff95dc SB |
1676 | /** |
1677 | * clk_set_min_rate - set a minimum clock rate for a clock source | |
1678 | * @clk: clock source | |
1679 | * @rate: desired minimum clock rate in Hz, inclusive | |
1680 | * | |
1681 | * Returns success (0) or negative errno. | |
1682 | */ | |
1683 | int clk_set_min_rate(struct clk *clk, unsigned long rate) | |
3fa2252b | 1684 | { |
4dff95dc SB |
1685 | if (!clk) |
1686 | return 0; | |
1687 | ||
1688 | return clk_set_rate_range(clk, rate, clk->max_rate); | |
3fa2252b | 1689 | } |
4dff95dc | 1690 | EXPORT_SYMBOL_GPL(clk_set_min_rate); |
3fa2252b | 1691 | |
4dff95dc SB |
1692 | /** |
1693 | * clk_set_max_rate - set a maximum clock rate for a clock source | |
1694 | * @clk: clock source | |
1695 | * @rate: desired maximum clock rate in Hz, inclusive | |
1696 | * | |
1697 | * Returns success (0) or negative errno. | |
1698 | */ | |
1699 | int clk_set_max_rate(struct clk *clk, unsigned long rate) | |
3fa2252b | 1700 | { |
4dff95dc SB |
1701 | if (!clk) |
1702 | return 0; | |
4935b22c | 1703 | |
4dff95dc | 1704 | return clk_set_rate_range(clk, clk->min_rate, rate); |
4935b22c | 1705 | } |
4dff95dc | 1706 | EXPORT_SYMBOL_GPL(clk_set_max_rate); |
4935b22c | 1707 | |
b2476490 | 1708 | /** |
4dff95dc SB |
1709 | * clk_get_parent - return the parent of a clk |
1710 | * @clk: the clk whose parent gets returned | |
b2476490 | 1711 | * |
4dff95dc | 1712 | * Simply returns clk->parent. Returns NULL if clk is NULL. |
b2476490 | 1713 | */ |
4dff95dc | 1714 | struct clk *clk_get_parent(struct clk *clk) |
b2476490 | 1715 | { |
4dff95dc | 1716 | struct clk *parent; |
b2476490 | 1717 | |
fc4a05d4 SB |
1718 | if (!clk) |
1719 | return NULL; | |
1720 | ||
4dff95dc | 1721 | clk_prepare_lock(); |
fc4a05d4 SB |
1722 | /* TODO: Create a per-user clk and change callers to call clk_put */ |
1723 | parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; | |
4dff95dc | 1724 | clk_prepare_unlock(); |
496eadf8 | 1725 | |
4dff95dc SB |
1726 | return parent; |
1727 | } | |
1728 | EXPORT_SYMBOL_GPL(clk_get_parent); | |
b2476490 | 1729 | |
4dff95dc SB |
1730 | static struct clk_core *__clk_init_parent(struct clk_core *core) |
1731 | { | |
5146e0b0 | 1732 | u8 index = 0; |
4dff95dc | 1733 | |
2430a94d | 1734 | if (core->num_parents > 1 && core->ops->get_parent) |
5146e0b0 | 1735 | index = core->ops->get_parent(core->hw); |
b2476490 | 1736 | |
5146e0b0 | 1737 | return clk_core_get_parent_by_index(core, index); |
b2476490 MT |
1738 | } |
1739 | ||
4dff95dc SB |
1740 | static void clk_core_reparent(struct clk_core *core, |
1741 | struct clk_core *new_parent) | |
b2476490 | 1742 | { |
4dff95dc SB |
1743 | clk_reparent(core, new_parent); |
1744 | __clk_recalc_accuracies(core); | |
1745 | __clk_recalc_rates(core, POST_RATE_CHANGE); | |
b2476490 MT |
1746 | } |
1747 | ||
42c86547 TV |
1748 | void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent) |
1749 | { | |
1750 | if (!hw) | |
1751 | return; | |
1752 | ||
1753 | clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); | |
1754 | } | |
1755 | ||
4dff95dc SB |
1756 | /** |
1757 | * clk_has_parent - check if a clock is a possible parent for another | |
1758 | * @clk: clock source | |
1759 | * @parent: parent clock source | |
1760 | * | |
1761 | * This function can be used in drivers that need to check that a clock can be | |
1762 | * the parent of another without actually changing the parent. | |
1763 | * | |
1764 | * Returns true if @parent is a possible parent for @clk, false otherwise. | |
b2476490 | 1765 | */ |
4dff95dc | 1766 | bool clk_has_parent(struct clk *clk, struct clk *parent) |
b2476490 | 1767 | { |
4dff95dc SB |
1768 | struct clk_core *core, *parent_core; |
1769 | unsigned int i; | |
b2476490 | 1770 | |
4dff95dc SB |
1771 | /* NULL clocks should be nops, so return success if either is NULL. */ |
1772 | if (!clk || !parent) | |
1773 | return true; | |
7452b219 | 1774 | |
4dff95dc SB |
1775 | core = clk->core; |
1776 | parent_core = parent->core; | |
71472c0c | 1777 | |
4dff95dc SB |
1778 | /* Optimize for the case where the parent is already the parent. */ |
1779 | if (core->parent == parent_core) | |
1780 | return true; | |
1c8e6004 | 1781 | |
4dff95dc SB |
1782 | for (i = 0; i < core->num_parents; i++) |
1783 | if (strcmp(core->parent_names[i], parent_core->name) == 0) | |
1784 | return true; | |
03bc10ab | 1785 | |
4dff95dc SB |
1786 | return false; |
1787 | } | |
1788 | EXPORT_SYMBOL_GPL(clk_has_parent); | |
03bc10ab | 1789 | |
4dff95dc SB |
1790 | static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent) |
1791 | { | |
1792 | int ret = 0; | |
1793 | int p_index = 0; | |
1794 | unsigned long p_rate = 0; | |
1795 | ||
1796 | if (!core) | |
1797 | return 0; | |
1798 | ||
1799 | /* prevent racing with updates to the clock topology */ | |
1800 | clk_prepare_lock(); | |
1801 | ||
1802 | if (core->parent == parent) | |
1803 | goto out; | |
1804 | ||
1805 | /* verify ops for for multi-parent clks */ | |
1806 | if ((core->num_parents > 1) && (!core->ops->set_parent)) { | |
1807 | ret = -ENOSYS; | |
63f5c3b2 | 1808 | goto out; |
7452b219 MT |
1809 | } |
1810 | ||
4dff95dc SB |
1811 | /* check that we are allowed to re-parent if the clock is in use */ |
1812 | if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { | |
1813 | ret = -EBUSY; | |
1814 | goto out; | |
b2476490 MT |
1815 | } |
1816 | ||
71472c0c | 1817 | /* try finding the new parent index */ |
4dff95dc | 1818 | if (parent) { |
d6968fca | 1819 | p_index = clk_fetch_parent_index(core, parent); |
f1c8b2ed | 1820 | if (p_index < 0) { |
71472c0c | 1821 | pr_debug("%s: clk %s can not be parent of clk %s\n", |
4dff95dc SB |
1822 | __func__, parent->name, core->name); |
1823 | ret = p_index; | |
1824 | goto out; | |
71472c0c | 1825 | } |
e8f0e68e | 1826 | p_rate = parent->rate; |
b2476490 MT |
1827 | } |
1828 | ||
4dff95dc SB |
1829 | /* propagate PRE_RATE_CHANGE notifications */ |
1830 | ret = __clk_speculate_rates(core, p_rate); | |
b2476490 | 1831 | |
4dff95dc SB |
1832 | /* abort if a driver objects */ |
1833 | if (ret & NOTIFY_STOP_MASK) | |
1834 | goto out; | |
b2476490 | 1835 | |
4dff95dc SB |
1836 | /* do the re-parent */ |
1837 | ret = __clk_set_parent(core, parent, p_index); | |
b2476490 | 1838 | |
4dff95dc SB |
1839 | /* propagate rate an accuracy recalculation accordingly */ |
1840 | if (ret) { | |
1841 | __clk_recalc_rates(core, ABORT_RATE_CHANGE); | |
1842 | } else { | |
1843 | __clk_recalc_rates(core, POST_RATE_CHANGE); | |
1844 | __clk_recalc_accuracies(core); | |
b2476490 MT |
1845 | } |
1846 | ||
4dff95dc SB |
1847 | out: |
1848 | clk_prepare_unlock(); | |
71472c0c | 1849 | |
4dff95dc SB |
1850 | return ret; |
1851 | } | |
b2476490 | 1852 | |
4dff95dc SB |
1853 | /** |
1854 | * clk_set_parent - switch the parent of a mux clk | |
1855 | * @clk: the mux clk whose input we are switching | |
1856 | * @parent: the new input to clk | |
1857 | * | |
1858 | * Re-parent clk to use parent as its new input source. If clk is in | |
1859 | * prepared state, the clk will get enabled for the duration of this call. If | |
1860 | * that's not acceptable for a specific clk (Eg: the consumer can't handle | |
1861 | * that, the reparenting is glitchy in hardware, etc), use the | |
1862 | * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared. | |
1863 | * | |
1864 | * After successfully changing clk's parent clk_set_parent will update the | |
1865 | * clk topology, sysfs topology and propagate rate recalculation via | |
1866 | * __clk_recalc_rates. | |
1867 | * | |
1868 | * Returns 0 on success, -EERROR otherwise. | |
1869 | */ | |
1870 | int clk_set_parent(struct clk *clk, struct clk *parent) | |
1871 | { | |
1872 | if (!clk) | |
1873 | return 0; | |
1874 | ||
1875 | return clk_core_set_parent(clk->core, parent ? parent->core : NULL); | |
b2476490 | 1876 | } |
4dff95dc | 1877 | EXPORT_SYMBOL_GPL(clk_set_parent); |
b2476490 | 1878 | |
4dff95dc SB |
1879 | /** |
1880 | * clk_set_phase - adjust the phase shift of a clock signal | |
1881 | * @clk: clock signal source | |
1882 | * @degrees: number of degrees the signal is shifted | |
1883 | * | |
1884 | * Shifts the phase of a clock signal by the specified | |
1885 | * degrees. Returns 0 on success, -EERROR otherwise. | |
1886 | * | |
1887 | * This function makes no distinction about the input or reference | |
1888 | * signal that we adjust the clock signal phase against. For example | |
1889 | * phase locked-loop clock signal generators we may shift phase with | |
1890 | * respect to feedback clock signal input, but for other cases the | |
1891 | * clock phase may be shifted with respect to some other, unspecified | |
1892 | * signal. | |
1893 | * | |
1894 | * Additionally the concept of phase shift does not propagate through | |
1895 | * the clock tree hierarchy, which sets it apart from clock rates and | |
1896 | * clock accuracy. A parent clock phase attribute does not have an | |
1897 | * impact on the phase attribute of a child clock. | |
b2476490 | 1898 | */ |
4dff95dc | 1899 | int clk_set_phase(struct clk *clk, int degrees) |
b2476490 | 1900 | { |
4dff95dc | 1901 | int ret = -EINVAL; |
b2476490 | 1902 | |
4dff95dc SB |
1903 | if (!clk) |
1904 | return 0; | |
b2476490 | 1905 | |
4dff95dc SB |
1906 | /* sanity check degrees */ |
1907 | degrees %= 360; | |
1908 | if (degrees < 0) | |
1909 | degrees += 360; | |
bf47b4fd | 1910 | |
4dff95dc | 1911 | clk_prepare_lock(); |
3fa2252b | 1912 | |
4dff95dc | 1913 | trace_clk_set_phase(clk->core, degrees); |
3fa2252b | 1914 | |
4dff95dc SB |
1915 | if (clk->core->ops->set_phase) |
1916 | ret = clk->core->ops->set_phase(clk->core->hw, degrees); | |
3fa2252b | 1917 | |
4dff95dc | 1918 | trace_clk_set_phase_complete(clk->core, degrees); |
dfc202ea | 1919 | |
4dff95dc SB |
1920 | if (!ret) |
1921 | clk->core->phase = degrees; | |
b2476490 | 1922 | |
4dff95dc | 1923 | clk_prepare_unlock(); |
dfc202ea | 1924 | |
4dff95dc SB |
1925 | return ret; |
1926 | } | |
1927 | EXPORT_SYMBOL_GPL(clk_set_phase); | |
b2476490 | 1928 | |
4dff95dc SB |
1929 | static int clk_core_get_phase(struct clk_core *core) |
1930 | { | |
1931 | int ret; | |
b2476490 | 1932 | |
4dff95dc SB |
1933 | clk_prepare_lock(); |
1934 | ret = core->phase; | |
1935 | clk_prepare_unlock(); | |
71472c0c | 1936 | |
4dff95dc | 1937 | return ret; |
b2476490 MT |
1938 | } |
1939 | ||
4dff95dc SB |
1940 | /** |
1941 | * clk_get_phase - return the phase shift of a clock signal | |
1942 | * @clk: clock signal source | |
1943 | * | |
1944 | * Returns the phase shift of a clock node in degrees, otherwise returns | |
1945 | * -EERROR. | |
1946 | */ | |
1947 | int clk_get_phase(struct clk *clk) | |
1c8e6004 | 1948 | { |
4dff95dc | 1949 | if (!clk) |
1c8e6004 TV |
1950 | return 0; |
1951 | ||
4dff95dc SB |
1952 | return clk_core_get_phase(clk->core); |
1953 | } | |
1954 | EXPORT_SYMBOL_GPL(clk_get_phase); | |
1c8e6004 | 1955 | |
4dff95dc SB |
1956 | /** |
1957 | * clk_is_match - check if two clk's point to the same hardware clock | |
1958 | * @p: clk compared against q | |
1959 | * @q: clk compared against p | |
1960 | * | |
1961 | * Returns true if the two struct clk pointers both point to the same hardware | |
1962 | * clock node. Put differently, returns true if struct clk *p and struct clk *q | |
1963 | * share the same struct clk_core object. | |
1964 | * | |
1965 | * Returns false otherwise. Note that two NULL clks are treated as matching. | |
1966 | */ | |
1967 | bool clk_is_match(const struct clk *p, const struct clk *q) | |
1968 | { | |
1969 | /* trivial case: identical struct clk's or both NULL */ | |
1970 | if (p == q) | |
1971 | return true; | |
1c8e6004 | 1972 | |
3fe003f9 | 1973 | /* true if clk->core pointers match. Avoid dereferencing garbage */ |
4dff95dc SB |
1974 | if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q)) |
1975 | if (p->core == q->core) | |
1976 | return true; | |
1c8e6004 | 1977 | |
4dff95dc SB |
1978 | return false; |
1979 | } | |
1980 | EXPORT_SYMBOL_GPL(clk_is_match); | |
1c8e6004 | 1981 | |
4dff95dc | 1982 | /*** debugfs support ***/ |
1c8e6004 | 1983 | |
4dff95dc SB |
1984 | #ifdef CONFIG_DEBUG_FS |
1985 | #include <linux/debugfs.h> | |
1c8e6004 | 1986 | |
4dff95dc SB |
1987 | static struct dentry *rootdir; |
1988 | static int inited = 0; | |
1989 | static DEFINE_MUTEX(clk_debug_lock); | |
1990 | static HLIST_HEAD(clk_debug_list); | |
1c8e6004 | 1991 | |
4dff95dc SB |
1992 | static struct hlist_head *all_lists[] = { |
1993 | &clk_root_list, | |
1994 | &clk_orphan_list, | |
1995 | NULL, | |
1996 | }; | |
1997 | ||
1998 | static struct hlist_head *orphan_list[] = { | |
1999 | &clk_orphan_list, | |
2000 | NULL, | |
2001 | }; | |
2002 | ||
2003 | static void clk_summary_show_one(struct seq_file *s, struct clk_core *c, | |
2004 | int level) | |
b2476490 | 2005 | { |
4dff95dc SB |
2006 | if (!c) |
2007 | return; | |
b2476490 | 2008 | |
4dff95dc SB |
2009 | seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n", |
2010 | level * 3 + 1, "", | |
2011 | 30 - level * 3, c->name, | |
2012 | c->enable_count, c->prepare_count, clk_core_get_rate(c), | |
2013 | clk_core_get_accuracy(c), clk_core_get_phase(c)); | |
2014 | } | |
89ac8d7a | 2015 | |
4dff95dc SB |
2016 | static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c, |
2017 | int level) | |
2018 | { | |
2019 | struct clk_core *child; | |
b2476490 | 2020 | |
4dff95dc SB |
2021 | if (!c) |
2022 | return; | |
b2476490 | 2023 | |
4dff95dc | 2024 | clk_summary_show_one(s, c, level); |
0e1c0301 | 2025 | |
4dff95dc SB |
2026 | hlist_for_each_entry(child, &c->children, child_node) |
2027 | clk_summary_show_subtree(s, child, level + 1); | |
1c8e6004 | 2028 | } |
b2476490 | 2029 | |
4dff95dc | 2030 | static int clk_summary_show(struct seq_file *s, void *data) |
1c8e6004 | 2031 | { |
4dff95dc SB |
2032 | struct clk_core *c; |
2033 | struct hlist_head **lists = (struct hlist_head **)s->private; | |
1c8e6004 | 2034 | |
4dff95dc SB |
2035 | seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n"); |
2036 | seq_puts(s, "----------------------------------------------------------------------------------------\n"); | |
b2476490 | 2037 | |
1c8e6004 TV |
2038 | clk_prepare_lock(); |
2039 | ||
4dff95dc SB |
2040 | for (; *lists; lists++) |
2041 | hlist_for_each_entry(c, *lists, child_node) | |
2042 | clk_summary_show_subtree(s, c, 0); | |
b2476490 | 2043 | |
eab89f69 | 2044 | clk_prepare_unlock(); |
b2476490 | 2045 | |
4dff95dc | 2046 | return 0; |
b2476490 | 2047 | } |
1c8e6004 | 2048 | |
1c8e6004 | 2049 | |
4dff95dc | 2050 | static int clk_summary_open(struct inode *inode, struct file *file) |
1c8e6004 | 2051 | { |
4dff95dc | 2052 | return single_open(file, clk_summary_show, inode->i_private); |
1c8e6004 | 2053 | } |
b2476490 | 2054 | |
4dff95dc SB |
2055 | static const struct file_operations clk_summary_fops = { |
2056 | .open = clk_summary_open, | |
2057 | .read = seq_read, | |
2058 | .llseek = seq_lseek, | |
2059 | .release = single_release, | |
2060 | }; | |
b2476490 | 2061 | |
4dff95dc SB |
2062 | static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level) |
2063 | { | |
2064 | if (!c) | |
2065 | return; | |
b2476490 | 2066 | |
7cb81136 | 2067 | /* This should be JSON format, i.e. elements separated with a comma */ |
4dff95dc SB |
2068 | seq_printf(s, "\"%s\": { ", c->name); |
2069 | seq_printf(s, "\"enable_count\": %d,", c->enable_count); | |
2070 | seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); | |
7cb81136 SW |
2071 | seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c)); |
2072 | seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c)); | |
4dff95dc | 2073 | seq_printf(s, "\"phase\": %d", clk_core_get_phase(c)); |
b2476490 | 2074 | } |
b2476490 | 2075 | |
4dff95dc | 2076 | static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level) |
b2476490 | 2077 | { |
4dff95dc | 2078 | struct clk_core *child; |
b2476490 | 2079 | |
4dff95dc SB |
2080 | if (!c) |
2081 | return; | |
b2476490 | 2082 | |
4dff95dc | 2083 | clk_dump_one(s, c, level); |
b2476490 | 2084 | |
4dff95dc SB |
2085 | hlist_for_each_entry(child, &c->children, child_node) { |
2086 | seq_printf(s, ","); | |
2087 | clk_dump_subtree(s, child, level + 1); | |
b2476490 MT |
2088 | } |
2089 | ||
4dff95dc | 2090 | seq_printf(s, "}"); |
b2476490 MT |
2091 | } |
2092 | ||
4dff95dc | 2093 | static int clk_dump(struct seq_file *s, void *data) |
4e88f3de | 2094 | { |
4dff95dc SB |
2095 | struct clk_core *c; |
2096 | bool first_node = true; | |
2097 | struct hlist_head **lists = (struct hlist_head **)s->private; | |
4e88f3de | 2098 | |
4dff95dc | 2099 | seq_printf(s, "{"); |
4e88f3de | 2100 | |
4dff95dc | 2101 | clk_prepare_lock(); |
035a61c3 | 2102 | |
4dff95dc SB |
2103 | for (; *lists; lists++) { |
2104 | hlist_for_each_entry(c, *lists, child_node) { | |
2105 | if (!first_node) | |
2106 | seq_puts(s, ","); | |
2107 | first_node = false; | |
2108 | clk_dump_subtree(s, c, 0); | |
2109 | } | |
2110 | } | |
4e88f3de | 2111 | |
4dff95dc | 2112 | clk_prepare_unlock(); |
4e88f3de | 2113 | |
70e9f4dd | 2114 | seq_puts(s, "}\n"); |
4dff95dc | 2115 | return 0; |
4e88f3de | 2116 | } |
4e88f3de | 2117 | |
4dff95dc SB |
2118 | |
2119 | static int clk_dump_open(struct inode *inode, struct file *file) | |
b2476490 | 2120 | { |
4dff95dc SB |
2121 | return single_open(file, clk_dump, inode->i_private); |
2122 | } | |
b2476490 | 2123 | |
4dff95dc SB |
2124 | static const struct file_operations clk_dump_fops = { |
2125 | .open = clk_dump_open, | |
2126 | .read = seq_read, | |
2127 | .llseek = seq_lseek, | |
2128 | .release = single_release, | |
2129 | }; | |
89ac8d7a | 2130 | |
92031575 PDS |
2131 | static int possible_parents_dump(struct seq_file *s, void *data) |
2132 | { | |
2133 | struct clk_core *core = s->private; | |
2134 | int i; | |
2135 | ||
2136 | for (i = 0; i < core->num_parents - 1; i++) | |
2137 | seq_printf(s, "%s ", core->parent_names[i]); | |
2138 | ||
2139 | seq_printf(s, "%s\n", core->parent_names[i]); | |
2140 | ||
2141 | return 0; | |
2142 | } | |
2143 | ||
2144 | static int possible_parents_open(struct inode *inode, struct file *file) | |
2145 | { | |
2146 | return single_open(file, possible_parents_dump, inode->i_private); | |
2147 | } | |
2148 | ||
2149 | static const struct file_operations possible_parents_fops = { | |
2150 | .open = possible_parents_open, | |
2151 | .read = seq_read, | |
2152 | .llseek = seq_lseek, | |
2153 | .release = single_release, | |
2154 | }; | |
2155 | ||
4dff95dc SB |
2156 | static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry) |
2157 | { | |
2158 | struct dentry *d; | |
2159 | int ret = -ENOMEM; | |
b2476490 | 2160 | |
4dff95dc SB |
2161 | if (!core || !pdentry) { |
2162 | ret = -EINVAL; | |
b2476490 | 2163 | goto out; |
4dff95dc | 2164 | } |
b2476490 | 2165 | |
4dff95dc SB |
2166 | d = debugfs_create_dir(core->name, pdentry); |
2167 | if (!d) | |
b61c43c0 | 2168 | goto out; |
b61c43c0 | 2169 | |
4dff95dc SB |
2170 | core->dentry = d; |
2171 | ||
2172 | d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry, | |
2173 | (u32 *)&core->rate); | |
2174 | if (!d) | |
2175 | goto err_out; | |
2176 | ||
2177 | d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry, | |
2178 | (u32 *)&core->accuracy); | |
2179 | if (!d) | |
2180 | goto err_out; | |
2181 | ||
2182 | d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry, | |
2183 | (u32 *)&core->phase); | |
2184 | if (!d) | |
2185 | goto err_out; | |
031dcc9b | 2186 | |
4dff95dc SB |
2187 | d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry, |
2188 | (u32 *)&core->flags); | |
2189 | if (!d) | |
2190 | goto err_out; | |
031dcc9b | 2191 | |
4dff95dc SB |
2192 | d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry, |
2193 | (u32 *)&core->prepare_count); | |
2194 | if (!d) | |
2195 | goto err_out; | |
b2476490 | 2196 | |
4dff95dc SB |
2197 | d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry, |
2198 | (u32 *)&core->enable_count); | |
2199 | if (!d) | |
2200 | goto err_out; | |
b2476490 | 2201 | |
4dff95dc SB |
2202 | d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry, |
2203 | (u32 *)&core->notifier_count); | |
2204 | if (!d) | |
2205 | goto err_out; | |
b2476490 | 2206 | |
92031575 PDS |
2207 | if (core->num_parents > 1) { |
2208 | d = debugfs_create_file("clk_possible_parents", S_IRUGO, | |
2209 | core->dentry, core, &possible_parents_fops); | |
2210 | if (!d) | |
2211 | goto err_out; | |
2212 | } | |
2213 | ||
4dff95dc SB |
2214 | if (core->ops->debug_init) { |
2215 | ret = core->ops->debug_init(core->hw, core->dentry); | |
2216 | if (ret) | |
2217 | goto err_out; | |
5279fc40 | 2218 | } |
b2476490 | 2219 | |
4dff95dc SB |
2220 | ret = 0; |
2221 | goto out; | |
b2476490 | 2222 | |
4dff95dc SB |
2223 | err_out: |
2224 | debugfs_remove_recursive(core->dentry); | |
2225 | core->dentry = NULL; | |
2226 | out: | |
b2476490 MT |
2227 | return ret; |
2228 | } | |
035a61c3 TV |
2229 | |
2230 | /** | |
6e5ab41b SB |
2231 | * clk_debug_register - add a clk node to the debugfs clk directory |
2232 | * @core: the clk being added to the debugfs clk directory | |
035a61c3 | 2233 | * |
6e5ab41b SB |
2234 | * Dynamically adds a clk to the debugfs clk directory if debugfs has been |
2235 | * initialized. Otherwise it bails out early since the debugfs clk directory | |
4dff95dc | 2236 | * will be created lazily by clk_debug_init as part of a late_initcall. |
035a61c3 | 2237 | */ |
4dff95dc | 2238 | static int clk_debug_register(struct clk_core *core) |
035a61c3 | 2239 | { |
4dff95dc | 2240 | int ret = 0; |
035a61c3 | 2241 | |
4dff95dc SB |
2242 | mutex_lock(&clk_debug_lock); |
2243 | hlist_add_head(&core->debug_node, &clk_debug_list); | |
2244 | ||
2245 | if (!inited) | |
2246 | goto unlock; | |
2247 | ||
2248 | ret = clk_debug_create_one(core, rootdir); | |
2249 | unlock: | |
2250 | mutex_unlock(&clk_debug_lock); | |
2251 | ||
2252 | return ret; | |
035a61c3 | 2253 | } |
b2476490 | 2254 | |
4dff95dc | 2255 | /** |
6e5ab41b SB |
2256 | * clk_debug_unregister - remove a clk node from the debugfs clk directory |
2257 | * @core: the clk being removed from the debugfs clk directory | |
e59c5371 | 2258 | * |
6e5ab41b SB |
2259 | * Dynamically removes a clk and all its child nodes from the |
2260 | * debugfs clk directory if clk->dentry points to debugfs created by | |
706d5c73 | 2261 | * clk_debug_register in __clk_core_init. |
e59c5371 | 2262 | */ |
4dff95dc | 2263 | static void clk_debug_unregister(struct clk_core *core) |
e59c5371 | 2264 | { |
4dff95dc SB |
2265 | mutex_lock(&clk_debug_lock); |
2266 | hlist_del_init(&core->debug_node); | |
2267 | debugfs_remove_recursive(core->dentry); | |
2268 | core->dentry = NULL; | |
2269 | mutex_unlock(&clk_debug_lock); | |
2270 | } | |
e59c5371 | 2271 | |
4dff95dc SB |
2272 | struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode, |
2273 | void *data, const struct file_operations *fops) | |
2274 | { | |
2275 | struct dentry *d = NULL; | |
e59c5371 | 2276 | |
4dff95dc SB |
2277 | if (hw->core->dentry) |
2278 | d = debugfs_create_file(name, mode, hw->core->dentry, data, | |
2279 | fops); | |
e59c5371 | 2280 | |
4dff95dc SB |
2281 | return d; |
2282 | } | |
2283 | EXPORT_SYMBOL_GPL(clk_debugfs_add_file); | |
e59c5371 | 2284 | |
4dff95dc | 2285 | /** |
6e5ab41b | 2286 | * clk_debug_init - lazily populate the debugfs clk directory |
4dff95dc | 2287 | * |
6e5ab41b SB |
2288 | * clks are often initialized very early during boot before memory can be |
2289 | * dynamically allocated and well before debugfs is setup. This function | |
2290 | * populates the debugfs clk directory once at boot-time when we know that | |
2291 | * debugfs is setup. It should only be called once at boot-time, all other clks | |
2292 | * added dynamically will be done so with clk_debug_register. | |
4dff95dc SB |
2293 | */ |
2294 | static int __init clk_debug_init(void) | |
2295 | { | |
2296 | struct clk_core *core; | |
2297 | struct dentry *d; | |
dfc202ea | 2298 | |
4dff95dc | 2299 | rootdir = debugfs_create_dir("clk", NULL); |
e59c5371 | 2300 | |
4dff95dc SB |
2301 | if (!rootdir) |
2302 | return -ENOMEM; | |
dfc202ea | 2303 | |
4dff95dc SB |
2304 | d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists, |
2305 | &clk_summary_fops); | |
2306 | if (!d) | |
2307 | return -ENOMEM; | |
e59c5371 | 2308 | |
4dff95dc SB |
2309 | d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists, |
2310 | &clk_dump_fops); | |
2311 | if (!d) | |
2312 | return -ENOMEM; | |
e59c5371 | 2313 | |
4dff95dc SB |
2314 | d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir, |
2315 | &orphan_list, &clk_summary_fops); | |
2316 | if (!d) | |
2317 | return -ENOMEM; | |
e59c5371 | 2318 | |
4dff95dc SB |
2319 | d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir, |
2320 | &orphan_list, &clk_dump_fops); | |
2321 | if (!d) | |
2322 | return -ENOMEM; | |
e59c5371 | 2323 | |
4dff95dc SB |
2324 | mutex_lock(&clk_debug_lock); |
2325 | hlist_for_each_entry(core, &clk_debug_list, debug_node) | |
2326 | clk_debug_create_one(core, rootdir); | |
e59c5371 | 2327 | |
4dff95dc SB |
2328 | inited = 1; |
2329 | mutex_unlock(&clk_debug_lock); | |
e59c5371 | 2330 | |
4dff95dc SB |
2331 | return 0; |
2332 | } | |
2333 | late_initcall(clk_debug_init); | |
2334 | #else | |
2335 | static inline int clk_debug_register(struct clk_core *core) { return 0; } | |
2336 | static inline void clk_debug_reparent(struct clk_core *core, | |
2337 | struct clk_core *new_parent) | |
035a61c3 | 2338 | { |
035a61c3 | 2339 | } |
4dff95dc | 2340 | static inline void clk_debug_unregister(struct clk_core *core) |
3d3801ef | 2341 | { |
3d3801ef | 2342 | } |
4dff95dc | 2343 | #endif |
3d3801ef | 2344 | |
b2476490 | 2345 | /** |
be45ebf2 | 2346 | * __clk_core_init - initialize the data structures in a struct clk_core |
d35c80c2 | 2347 | * @core: clk_core being initialized |
b2476490 | 2348 | * |
035a61c3 | 2349 | * Initializes the lists in struct clk_core, queries the hardware for the |
b2476490 | 2350 | * parent and rate and sets them both. |
b2476490 | 2351 | */ |
be45ebf2 | 2352 | static int __clk_core_init(struct clk_core *core) |
b2476490 | 2353 | { |
d1302a36 | 2354 | int i, ret = 0; |
035a61c3 | 2355 | struct clk_core *orphan; |
b67bfe0d | 2356 | struct hlist_node *tmp2; |
1c8e6004 | 2357 | unsigned long rate; |
b2476490 | 2358 | |
d35c80c2 | 2359 | if (!core) |
d1302a36 | 2360 | return -EINVAL; |
b2476490 | 2361 | |
eab89f69 | 2362 | clk_prepare_lock(); |
b2476490 MT |
2363 | |
2364 | /* check to see if a clock with this name is already registered */ | |
d6968fca | 2365 | if (clk_core_lookup(core->name)) { |
d1302a36 | 2366 | pr_debug("%s: clk %s already initialized\n", |
d6968fca | 2367 | __func__, core->name); |
d1302a36 | 2368 | ret = -EEXIST; |
b2476490 | 2369 | goto out; |
d1302a36 | 2370 | } |
b2476490 | 2371 | |
d4d7e3dd | 2372 | /* check that clk_ops are sane. See Documentation/clk.txt */ |
d6968fca SB |
2373 | if (core->ops->set_rate && |
2374 | !((core->ops->round_rate || core->ops->determine_rate) && | |
2375 | core->ops->recalc_rate)) { | |
c44fccb5 MY |
2376 | pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n", |
2377 | __func__, core->name); | |
d1302a36 | 2378 | ret = -EINVAL; |
d4d7e3dd MT |
2379 | goto out; |
2380 | } | |
2381 | ||
d6968fca | 2382 | if (core->ops->set_parent && !core->ops->get_parent) { |
c44fccb5 MY |
2383 | pr_err("%s: %s must implement .get_parent & .set_parent\n", |
2384 | __func__, core->name); | |
d1302a36 | 2385 | ret = -EINVAL; |
d4d7e3dd MT |
2386 | goto out; |
2387 | } | |
2388 | ||
3c8e77dd MY |
2389 | if (core->num_parents > 1 && !core->ops->get_parent) { |
2390 | pr_err("%s: %s must implement .get_parent as it has multi parents\n", | |
2391 | __func__, core->name); | |
2392 | ret = -EINVAL; | |
2393 | goto out; | |
2394 | } | |
2395 | ||
d6968fca SB |
2396 | if (core->ops->set_rate_and_parent && |
2397 | !(core->ops->set_parent && core->ops->set_rate)) { | |
c44fccb5 | 2398 | pr_err("%s: %s must implement .set_parent & .set_rate\n", |
d6968fca | 2399 | __func__, core->name); |
3fa2252b SB |
2400 | ret = -EINVAL; |
2401 | goto out; | |
2402 | } | |
2403 | ||
b2476490 | 2404 | /* throw a WARN if any entries in parent_names are NULL */ |
d6968fca SB |
2405 | for (i = 0; i < core->num_parents; i++) |
2406 | WARN(!core->parent_names[i], | |
b2476490 | 2407 | "%s: invalid NULL in %s's .parent_names\n", |
d6968fca | 2408 | __func__, core->name); |
b2476490 | 2409 | |
d6968fca | 2410 | core->parent = __clk_init_parent(core); |
b2476490 MT |
2411 | |
2412 | /* | |
706d5c73 SB |
2413 | * Populate core->parent if parent has already been clk_core_init'd. If |
2414 | * parent has not yet been clk_core_init'd then place clk in the orphan | |
47b0eeb3 | 2415 | * list. If clk doesn't have any parents then place it in the root |
b2476490 MT |
2416 | * clk list. |
2417 | * | |
2418 | * Every time a new clk is clk_init'd then we walk the list of orphan | |
2419 | * clocks and re-parent any that are children of the clock currently | |
2420 | * being clk_init'd. | |
2421 | */ | |
e6500344 | 2422 | if (core->parent) { |
d6968fca SB |
2423 | hlist_add_head(&core->child_node, |
2424 | &core->parent->children); | |
e6500344 | 2425 | core->orphan = core->parent->orphan; |
47b0eeb3 | 2426 | } else if (!core->num_parents) { |
d6968fca | 2427 | hlist_add_head(&core->child_node, &clk_root_list); |
e6500344 HS |
2428 | core->orphan = false; |
2429 | } else { | |
d6968fca | 2430 | hlist_add_head(&core->child_node, &clk_orphan_list); |
e6500344 HS |
2431 | core->orphan = true; |
2432 | } | |
b2476490 | 2433 | |
5279fc40 BB |
2434 | /* |
2435 | * Set clk's accuracy. The preferred method is to use | |
2436 | * .recalc_accuracy. For simple clocks and lazy developers the default | |
2437 | * fallback is to use the parent's accuracy. If a clock doesn't have a | |
2438 | * parent (or is orphaned) then accuracy is set to zero (perfect | |
2439 | * clock). | |
2440 | */ | |
d6968fca SB |
2441 | if (core->ops->recalc_accuracy) |
2442 | core->accuracy = core->ops->recalc_accuracy(core->hw, | |
2443 | __clk_get_accuracy(core->parent)); | |
2444 | else if (core->parent) | |
2445 | core->accuracy = core->parent->accuracy; | |
5279fc40 | 2446 | else |
d6968fca | 2447 | core->accuracy = 0; |
5279fc40 | 2448 | |
9824cf73 MR |
2449 | /* |
2450 | * Set clk's phase. | |
2451 | * Since a phase is by definition relative to its parent, just | |
2452 | * query the current clock phase, or just assume it's in phase. | |
2453 | */ | |
d6968fca SB |
2454 | if (core->ops->get_phase) |
2455 | core->phase = core->ops->get_phase(core->hw); | |
9824cf73 | 2456 | else |
d6968fca | 2457 | core->phase = 0; |
9824cf73 | 2458 | |
b2476490 MT |
2459 | /* |
2460 | * Set clk's rate. The preferred method is to use .recalc_rate. For | |
2461 | * simple clocks and lazy developers the default fallback is to use the | |
2462 | * parent's rate. If a clock doesn't have a parent (or is orphaned) | |
2463 | * then rate is set to zero. | |
2464 | */ | |
d6968fca SB |
2465 | if (core->ops->recalc_rate) |
2466 | rate = core->ops->recalc_rate(core->hw, | |
2467 | clk_core_get_rate_nolock(core->parent)); | |
2468 | else if (core->parent) | |
2469 | rate = core->parent->rate; | |
b2476490 | 2470 | else |
1c8e6004 | 2471 | rate = 0; |
d6968fca | 2472 | core->rate = core->req_rate = rate; |
b2476490 MT |
2473 | |
2474 | /* | |
0e8f6e49 MY |
2475 | * walk the list of orphan clocks and reparent any that newly finds a |
2476 | * parent. | |
b2476490 | 2477 | */ |
b67bfe0d | 2478 | hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { |
0e8f6e49 | 2479 | struct clk_core *parent = __clk_init_parent(orphan); |
1f61e5f1 | 2480 | |
904e6ead MT |
2481 | /* |
2482 | * we could call __clk_set_parent, but that would result in a | |
2483 | * redundant call to the .set_rate op, if it exists | |
2484 | */ | |
2485 | if (parent) { | |
2486 | __clk_set_parent_before(orphan, parent); | |
2487 | __clk_set_parent_after(orphan, parent, NULL); | |
2488 | __clk_recalc_accuracies(orphan); | |
2489 | __clk_recalc_rates(orphan, 0); | |
2490 | } | |
0e8f6e49 | 2491 | } |
b2476490 MT |
2492 | |
2493 | /* | |
2494 | * optional platform-specific magic | |
2495 | * | |
2496 | * The .init callback is not used by any of the basic clock types, but | |
2497 | * exists for weird hardware that must perform initialization magic. | |
2498 | * Please consider other ways of solving initialization problems before | |
24ee1a08 | 2499 | * using this callback, as its use is discouraged. |
b2476490 | 2500 | */ |
d6968fca SB |
2501 | if (core->ops->init) |
2502 | core->ops->init(core->hw); | |
b2476490 | 2503 | |
32b9b109 | 2504 | if (core->flags & CLK_IS_CRITICAL) { |
ef56b79b MR |
2505 | unsigned long flags; |
2506 | ||
32b9b109 | 2507 | clk_core_prepare(core); |
ef56b79b MR |
2508 | |
2509 | flags = clk_enable_lock(); | |
32b9b109 | 2510 | clk_core_enable(core); |
ef56b79b | 2511 | clk_enable_unlock(flags); |
32b9b109 LJ |
2512 | } |
2513 | ||
d6968fca | 2514 | kref_init(&core->ref); |
b2476490 | 2515 | out: |
eab89f69 | 2516 | clk_prepare_unlock(); |
b2476490 | 2517 | |
89f7e9de | 2518 | if (!ret) |
d6968fca | 2519 | clk_debug_register(core); |
89f7e9de | 2520 | |
d1302a36 | 2521 | return ret; |
b2476490 MT |
2522 | } |
2523 | ||
035a61c3 TV |
2524 | struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id, |
2525 | const char *con_id) | |
0197b3ea | 2526 | { |
0197b3ea SK |
2527 | struct clk *clk; |
2528 | ||
035a61c3 | 2529 | /* This is to allow this function to be chained to others */ |
c1de1357 | 2530 | if (IS_ERR_OR_NULL(hw)) |
8a23133c | 2531 | return ERR_CAST(hw); |
0197b3ea | 2532 | |
035a61c3 TV |
2533 | clk = kzalloc(sizeof(*clk), GFP_KERNEL); |
2534 | if (!clk) | |
2535 | return ERR_PTR(-ENOMEM); | |
2536 | ||
2537 | clk->core = hw->core; | |
2538 | clk->dev_id = dev_id; | |
253160a8 | 2539 | clk->con_id = kstrdup_const(con_id, GFP_KERNEL); |
1c8e6004 TV |
2540 | clk->max_rate = ULONG_MAX; |
2541 | ||
2542 | clk_prepare_lock(); | |
50595f8b | 2543 | hlist_add_head(&clk->clks_node, &hw->core->clks); |
1c8e6004 | 2544 | clk_prepare_unlock(); |
0197b3ea SK |
2545 | |
2546 | return clk; | |
2547 | } | |
035a61c3 | 2548 | |
73e0e496 | 2549 | void __clk_free_clk(struct clk *clk) |
1c8e6004 TV |
2550 | { |
2551 | clk_prepare_lock(); | |
50595f8b | 2552 | hlist_del(&clk->clks_node); |
1c8e6004 TV |
2553 | clk_prepare_unlock(); |
2554 | ||
253160a8 | 2555 | kfree_const(clk->con_id); |
1c8e6004 TV |
2556 | kfree(clk); |
2557 | } | |
0197b3ea | 2558 | |
293ba3b4 SB |
2559 | /** |
2560 | * clk_register - allocate a new clock, register it and return an opaque cookie | |
2561 | * @dev: device that is registering this clock | |
2562 | * @hw: link to hardware-specific clock data | |
2563 | * | |
2564 | * clk_register is the primary interface for populating the clock tree with new | |
2565 | * clock nodes. It returns a pointer to the newly allocated struct clk which | |
a59a5163 | 2566 | * cannot be dereferenced by driver code but may be used in conjunction with the |
293ba3b4 SB |
2567 | * rest of the clock API. In the event of an error clk_register will return an |
2568 | * error code; drivers must test for an error code after calling clk_register. | |
2569 | */ | |
2570 | struct clk *clk_register(struct device *dev, struct clk_hw *hw) | |
b2476490 | 2571 | { |
d1302a36 | 2572 | int i, ret; |
d6968fca | 2573 | struct clk_core *core; |
293ba3b4 | 2574 | |
d6968fca SB |
2575 | core = kzalloc(sizeof(*core), GFP_KERNEL); |
2576 | if (!core) { | |
293ba3b4 SB |
2577 | ret = -ENOMEM; |
2578 | goto fail_out; | |
2579 | } | |
b2476490 | 2580 | |
d6968fca SB |
2581 | core->name = kstrdup_const(hw->init->name, GFP_KERNEL); |
2582 | if (!core->name) { | |
0197b3ea SK |
2583 | ret = -ENOMEM; |
2584 | goto fail_name; | |
2585 | } | |
d6968fca | 2586 | core->ops = hw->init->ops; |
ac2df527 | 2587 | if (dev && dev->driver) |
d6968fca SB |
2588 | core->owner = dev->driver->owner; |
2589 | core->hw = hw; | |
2590 | core->flags = hw->init->flags; | |
2591 | core->num_parents = hw->init->num_parents; | |
9783c0d9 SB |
2592 | core->min_rate = 0; |
2593 | core->max_rate = ULONG_MAX; | |
d6968fca | 2594 | hw->core = core; |
b2476490 | 2595 | |
d1302a36 | 2596 | /* allocate local copy in case parent_names is __initdata */ |
d6968fca | 2597 | core->parent_names = kcalloc(core->num_parents, sizeof(char *), |
96a7ed90 | 2598 | GFP_KERNEL); |
d1302a36 | 2599 | |
d6968fca | 2600 | if (!core->parent_names) { |
d1302a36 MT |
2601 | ret = -ENOMEM; |
2602 | goto fail_parent_names; | |
2603 | } | |
2604 | ||
2605 | ||
2606 | /* copy each string name in case parent_names is __initdata */ | |
d6968fca SB |
2607 | for (i = 0; i < core->num_parents; i++) { |
2608 | core->parent_names[i] = kstrdup_const(hw->init->parent_names[i], | |
0197b3ea | 2609 | GFP_KERNEL); |
d6968fca | 2610 | if (!core->parent_names[i]) { |
d1302a36 MT |
2611 | ret = -ENOMEM; |
2612 | goto fail_parent_names_copy; | |
2613 | } | |
2614 | } | |
2615 | ||
176d1169 MY |
2616 | /* avoid unnecessary string look-ups of clk_core's possible parents. */ |
2617 | core->parents = kcalloc(core->num_parents, sizeof(*core->parents), | |
2618 | GFP_KERNEL); | |
2619 | if (!core->parents) { | |
2620 | ret = -ENOMEM; | |
2621 | goto fail_parents; | |
2622 | }; | |
2623 | ||
d6968fca | 2624 | INIT_HLIST_HEAD(&core->clks); |
1c8e6004 | 2625 | |
035a61c3 TV |
2626 | hw->clk = __clk_create_clk(hw, NULL, NULL); |
2627 | if (IS_ERR(hw->clk)) { | |
035a61c3 | 2628 | ret = PTR_ERR(hw->clk); |
176d1169 | 2629 | goto fail_parents; |
035a61c3 TV |
2630 | } |
2631 | ||
be45ebf2 | 2632 | ret = __clk_core_init(core); |
d1302a36 | 2633 | if (!ret) |
035a61c3 | 2634 | return hw->clk; |
b2476490 | 2635 | |
1c8e6004 | 2636 | __clk_free_clk(hw->clk); |
035a61c3 | 2637 | hw->clk = NULL; |
b2476490 | 2638 | |
176d1169 MY |
2639 | fail_parents: |
2640 | kfree(core->parents); | |
d1302a36 MT |
2641 | fail_parent_names_copy: |
2642 | while (--i >= 0) | |
d6968fca SB |
2643 | kfree_const(core->parent_names[i]); |
2644 | kfree(core->parent_names); | |
d1302a36 | 2645 | fail_parent_names: |
d6968fca | 2646 | kfree_const(core->name); |
0197b3ea | 2647 | fail_name: |
d6968fca | 2648 | kfree(core); |
d1302a36 MT |
2649 | fail_out: |
2650 | return ERR_PTR(ret); | |
b2476490 MT |
2651 | } |
2652 | EXPORT_SYMBOL_GPL(clk_register); | |
2653 | ||
4143804c SB |
2654 | /** |
2655 | * clk_hw_register - register a clk_hw and return an error code | |
2656 | * @dev: device that is registering this clock | |
2657 | * @hw: link to hardware-specific clock data | |
2658 | * | |
2659 | * clk_hw_register is the primary interface for populating the clock tree with | |
2660 | * new clock nodes. It returns an integer equal to zero indicating success or | |
2661 | * less than zero indicating failure. Drivers must test for an error code after | |
2662 | * calling clk_hw_register(). | |
2663 | */ | |
2664 | int clk_hw_register(struct device *dev, struct clk_hw *hw) | |
2665 | { | |
2666 | return PTR_ERR_OR_ZERO(clk_register(dev, hw)); | |
2667 | } | |
2668 | EXPORT_SYMBOL_GPL(clk_hw_register); | |
2669 | ||
6e5ab41b | 2670 | /* Free memory allocated for a clock. */ |
fcb0ee6a SN |
2671 | static void __clk_release(struct kref *ref) |
2672 | { | |
d6968fca SB |
2673 | struct clk_core *core = container_of(ref, struct clk_core, ref); |
2674 | int i = core->num_parents; | |
fcb0ee6a | 2675 | |
496eadf8 KK |
2676 | lockdep_assert_held(&prepare_lock); |
2677 | ||
d6968fca | 2678 | kfree(core->parents); |
fcb0ee6a | 2679 | while (--i >= 0) |
d6968fca | 2680 | kfree_const(core->parent_names[i]); |
fcb0ee6a | 2681 | |
d6968fca SB |
2682 | kfree(core->parent_names); |
2683 | kfree_const(core->name); | |
2684 | kfree(core); | |
fcb0ee6a SN |
2685 | } |
2686 | ||
2687 | /* | |
2688 | * Empty clk_ops for unregistered clocks. These are used temporarily | |
2689 | * after clk_unregister() was called on a clock and until last clock | |
2690 | * consumer calls clk_put() and the struct clk object is freed. | |
2691 | */ | |
2692 | static int clk_nodrv_prepare_enable(struct clk_hw *hw) | |
2693 | { | |
2694 | return -ENXIO; | |
2695 | } | |
2696 | ||
2697 | static void clk_nodrv_disable_unprepare(struct clk_hw *hw) | |
2698 | { | |
2699 | WARN_ON_ONCE(1); | |
2700 | } | |
2701 | ||
2702 | static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate, | |
2703 | unsigned long parent_rate) | |
2704 | { | |
2705 | return -ENXIO; | |
2706 | } | |
2707 | ||
2708 | static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index) | |
2709 | { | |
2710 | return -ENXIO; | |
2711 | } | |
2712 | ||
2713 | static const struct clk_ops clk_nodrv_ops = { | |
2714 | .enable = clk_nodrv_prepare_enable, | |
2715 | .disable = clk_nodrv_disable_unprepare, | |
2716 | .prepare = clk_nodrv_prepare_enable, | |
2717 | .unprepare = clk_nodrv_disable_unprepare, | |
2718 | .set_rate = clk_nodrv_set_rate, | |
2719 | .set_parent = clk_nodrv_set_parent, | |
2720 | }; | |
2721 | ||
1df5c939 MB |
2722 | /** |
2723 | * clk_unregister - unregister a currently registered clock | |
2724 | * @clk: clock to unregister | |
1df5c939 | 2725 | */ |
fcb0ee6a SN |
2726 | void clk_unregister(struct clk *clk) |
2727 | { | |
2728 | unsigned long flags; | |
2729 | ||
6314b679 SB |
2730 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
2731 | return; | |
2732 | ||
035a61c3 | 2733 | clk_debug_unregister(clk->core); |
fcb0ee6a SN |
2734 | |
2735 | clk_prepare_lock(); | |
2736 | ||
035a61c3 TV |
2737 | if (clk->core->ops == &clk_nodrv_ops) { |
2738 | pr_err("%s: unregistered clock: %s\n", __func__, | |
2739 | clk->core->name); | |
4106a3d9 | 2740 | goto unlock; |
fcb0ee6a SN |
2741 | } |
2742 | /* | |
2743 | * Assign empty clock ops for consumers that might still hold | |
2744 | * a reference to this clock. | |
2745 | */ | |
2746 | flags = clk_enable_lock(); | |
035a61c3 | 2747 | clk->core->ops = &clk_nodrv_ops; |
fcb0ee6a SN |
2748 | clk_enable_unlock(flags); |
2749 | ||
035a61c3 TV |
2750 | if (!hlist_empty(&clk->core->children)) { |
2751 | struct clk_core *child; | |
874f224c | 2752 | struct hlist_node *t; |
fcb0ee6a SN |
2753 | |
2754 | /* Reparent all children to the orphan list. */ | |
035a61c3 TV |
2755 | hlist_for_each_entry_safe(child, t, &clk->core->children, |
2756 | child_node) | |
2757 | clk_core_set_parent(child, NULL); | |
fcb0ee6a SN |
2758 | } |
2759 | ||
035a61c3 | 2760 | hlist_del_init(&clk->core->child_node); |
fcb0ee6a | 2761 | |
035a61c3 | 2762 | if (clk->core->prepare_count) |
fcb0ee6a | 2763 | pr_warn("%s: unregistering prepared clock: %s\n", |
035a61c3 TV |
2764 | __func__, clk->core->name); |
2765 | kref_put(&clk->core->ref, __clk_release); | |
4106a3d9 | 2766 | unlock: |
fcb0ee6a SN |
2767 | clk_prepare_unlock(); |
2768 | } | |
1df5c939 MB |
2769 | EXPORT_SYMBOL_GPL(clk_unregister); |
2770 | ||
4143804c SB |
2771 | /** |
2772 | * clk_hw_unregister - unregister a currently registered clk_hw | |
2773 | * @hw: hardware-specific clock data to unregister | |
2774 | */ | |
2775 | void clk_hw_unregister(struct clk_hw *hw) | |
2776 | { | |
2777 | clk_unregister(hw->clk); | |
2778 | } | |
2779 | EXPORT_SYMBOL_GPL(clk_hw_unregister); | |
2780 | ||
46c8773a SB |
2781 | static void devm_clk_release(struct device *dev, void *res) |
2782 | { | |
293ba3b4 | 2783 | clk_unregister(*(struct clk **)res); |
46c8773a SB |
2784 | } |
2785 | ||
4143804c SB |
2786 | static void devm_clk_hw_release(struct device *dev, void *res) |
2787 | { | |
2788 | clk_hw_unregister(*(struct clk_hw **)res); | |
2789 | } | |
2790 | ||
46c8773a SB |
2791 | /** |
2792 | * devm_clk_register - resource managed clk_register() | |
2793 | * @dev: device that is registering this clock | |
2794 | * @hw: link to hardware-specific clock data | |
2795 | * | |
2796 | * Managed clk_register(). Clocks returned from this function are | |
2797 | * automatically clk_unregister()ed on driver detach. See clk_register() for | |
2798 | * more information. | |
2799 | */ | |
2800 | struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) | |
2801 | { | |
2802 | struct clk *clk; | |
293ba3b4 | 2803 | struct clk **clkp; |
46c8773a | 2804 | |
293ba3b4 SB |
2805 | clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL); |
2806 | if (!clkp) | |
46c8773a SB |
2807 | return ERR_PTR(-ENOMEM); |
2808 | ||
293ba3b4 SB |
2809 | clk = clk_register(dev, hw); |
2810 | if (!IS_ERR(clk)) { | |
2811 | *clkp = clk; | |
2812 | devres_add(dev, clkp); | |
46c8773a | 2813 | } else { |
293ba3b4 | 2814 | devres_free(clkp); |
46c8773a SB |
2815 | } |
2816 | ||
2817 | return clk; | |
2818 | } | |
2819 | EXPORT_SYMBOL_GPL(devm_clk_register); | |
2820 | ||
4143804c SB |
2821 | /** |
2822 | * devm_clk_hw_register - resource managed clk_hw_register() | |
2823 | * @dev: device that is registering this clock | |
2824 | * @hw: link to hardware-specific clock data | |
2825 | * | |
c47265ad | 2826 | * Managed clk_hw_register(). Clocks registered by this function are |
4143804c SB |
2827 | * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register() |
2828 | * for more information. | |
2829 | */ | |
2830 | int devm_clk_hw_register(struct device *dev, struct clk_hw *hw) | |
2831 | { | |
2832 | struct clk_hw **hwp; | |
2833 | int ret; | |
2834 | ||
2835 | hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL); | |
2836 | if (!hwp) | |
2837 | return -ENOMEM; | |
2838 | ||
2839 | ret = clk_hw_register(dev, hw); | |
2840 | if (!ret) { | |
2841 | *hwp = hw; | |
2842 | devres_add(dev, hwp); | |
2843 | } else { | |
2844 | devres_free(hwp); | |
2845 | } | |
2846 | ||
2847 | return ret; | |
2848 | } | |
2849 | EXPORT_SYMBOL_GPL(devm_clk_hw_register); | |
2850 | ||
46c8773a SB |
2851 | static int devm_clk_match(struct device *dev, void *res, void *data) |
2852 | { | |
2853 | struct clk *c = res; | |
2854 | if (WARN_ON(!c)) | |
2855 | return 0; | |
2856 | return c == data; | |
2857 | } | |
2858 | ||
4143804c SB |
2859 | static int devm_clk_hw_match(struct device *dev, void *res, void *data) |
2860 | { | |
2861 | struct clk_hw *hw = res; | |
2862 | ||
2863 | if (WARN_ON(!hw)) | |
2864 | return 0; | |
2865 | return hw == data; | |
2866 | } | |
2867 | ||
46c8773a SB |
2868 | /** |
2869 | * devm_clk_unregister - resource managed clk_unregister() | |
2870 | * @clk: clock to unregister | |
2871 | * | |
2872 | * Deallocate a clock allocated with devm_clk_register(). Normally | |
2873 | * this function will not need to be called and the resource management | |
2874 | * code will ensure that the resource is freed. | |
2875 | */ | |
2876 | void devm_clk_unregister(struct device *dev, struct clk *clk) | |
2877 | { | |
2878 | WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); | |
2879 | } | |
2880 | EXPORT_SYMBOL_GPL(devm_clk_unregister); | |
2881 | ||
4143804c SB |
2882 | /** |
2883 | * devm_clk_hw_unregister - resource managed clk_hw_unregister() | |
2884 | * @dev: device that is unregistering the hardware-specific clock data | |
2885 | * @hw: link to hardware-specific clock data | |
2886 | * | |
2887 | * Unregister a clk_hw registered with devm_clk_hw_register(). Normally | |
2888 | * this function will not need to be called and the resource management | |
2889 | * code will ensure that the resource is freed. | |
2890 | */ | |
2891 | void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw) | |
2892 | { | |
2893 | WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match, | |
2894 | hw)); | |
2895 | } | |
2896 | EXPORT_SYMBOL_GPL(devm_clk_hw_unregister); | |
2897 | ||
ac2df527 SN |
2898 | /* |
2899 | * clkdev helpers | |
2900 | */ | |
2901 | int __clk_get(struct clk *clk) | |
2902 | { | |
035a61c3 TV |
2903 | struct clk_core *core = !clk ? NULL : clk->core; |
2904 | ||
2905 | if (core) { | |
2906 | if (!try_module_get(core->owner)) | |
00efcb1c | 2907 | return 0; |
ac2df527 | 2908 | |
035a61c3 | 2909 | kref_get(&core->ref); |
00efcb1c | 2910 | } |
ac2df527 SN |
2911 | return 1; |
2912 | } | |
2913 | ||
2914 | void __clk_put(struct clk *clk) | |
2915 | { | |
10cdfe54 TV |
2916 | struct module *owner; |
2917 | ||
00efcb1c | 2918 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
ac2df527 SN |
2919 | return; |
2920 | ||
fcb0ee6a | 2921 | clk_prepare_lock(); |
1c8e6004 | 2922 | |
50595f8b | 2923 | hlist_del(&clk->clks_node); |
ec02ace8 TV |
2924 | if (clk->min_rate > clk->core->req_rate || |
2925 | clk->max_rate < clk->core->req_rate) | |
2926 | clk_core_set_rate_nolock(clk->core, clk->core->req_rate); | |
2927 | ||
1c8e6004 TV |
2928 | owner = clk->core->owner; |
2929 | kref_put(&clk->core->ref, __clk_release); | |
2930 | ||
fcb0ee6a SN |
2931 | clk_prepare_unlock(); |
2932 | ||
10cdfe54 | 2933 | module_put(owner); |
035a61c3 | 2934 | |
035a61c3 | 2935 | kfree(clk); |
ac2df527 SN |
2936 | } |
2937 | ||
b2476490 MT |
2938 | /*** clk rate change notifiers ***/ |
2939 | ||
2940 | /** | |
2941 | * clk_notifier_register - add a clk rate change notifier | |
2942 | * @clk: struct clk * to watch | |
2943 | * @nb: struct notifier_block * with callback info | |
2944 | * | |
2945 | * Request notification when clk's rate changes. This uses an SRCU | |
2946 | * notifier because we want it to block and notifier unregistrations are | |
2947 | * uncommon. The callbacks associated with the notifier must not | |
2948 | * re-enter into the clk framework by calling any top-level clk APIs; | |
2949 | * this will cause a nested prepare_lock mutex. | |
2950 | * | |
198bb594 MY |
2951 | * In all notification cases (pre, post and abort rate change) the original |
2952 | * clock rate is passed to the callback via struct clk_notifier_data.old_rate | |
2953 | * and the new frequency is passed via struct clk_notifier_data.new_rate. | |
b2476490 | 2954 | * |
b2476490 MT |
2955 | * clk_notifier_register() must be called from non-atomic context. |
2956 | * Returns -EINVAL if called with null arguments, -ENOMEM upon | |
2957 | * allocation failure; otherwise, passes along the return value of | |
2958 | * srcu_notifier_chain_register(). | |
2959 | */ | |
2960 | int clk_notifier_register(struct clk *clk, struct notifier_block *nb) | |
2961 | { | |
2962 | struct clk_notifier *cn; | |
2963 | int ret = -ENOMEM; | |
2964 | ||
2965 | if (!clk || !nb) | |
2966 | return -EINVAL; | |
2967 | ||
eab89f69 | 2968 | clk_prepare_lock(); |
b2476490 MT |
2969 | |
2970 | /* search the list of notifiers for this clk */ | |
2971 | list_for_each_entry(cn, &clk_notifier_list, node) | |
2972 | if (cn->clk == clk) | |
2973 | break; | |
2974 | ||
2975 | /* if clk wasn't in the notifier list, allocate new clk_notifier */ | |
2976 | if (cn->clk != clk) { | |
2977 | cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL); | |
2978 | if (!cn) | |
2979 | goto out; | |
2980 | ||
2981 | cn->clk = clk; | |
2982 | srcu_init_notifier_head(&cn->notifier_head); | |
2983 | ||
2984 | list_add(&cn->node, &clk_notifier_list); | |
2985 | } | |
2986 | ||
2987 | ret = srcu_notifier_chain_register(&cn->notifier_head, nb); | |
2988 | ||
035a61c3 | 2989 | clk->core->notifier_count++; |
b2476490 MT |
2990 | |
2991 | out: | |
eab89f69 | 2992 | clk_prepare_unlock(); |
b2476490 MT |
2993 | |
2994 | return ret; | |
2995 | } | |
2996 | EXPORT_SYMBOL_GPL(clk_notifier_register); | |
2997 | ||
2998 | /** | |
2999 | * clk_notifier_unregister - remove a clk rate change notifier | |
3000 | * @clk: struct clk * | |
3001 | * @nb: struct notifier_block * with callback info | |
3002 | * | |
3003 | * Request no further notification for changes to 'clk' and frees memory | |
3004 | * allocated in clk_notifier_register. | |
3005 | * | |
3006 | * Returns -EINVAL if called with null arguments; otherwise, passes | |
3007 | * along the return value of srcu_notifier_chain_unregister(). | |
3008 | */ | |
3009 | int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) | |
3010 | { | |
3011 | struct clk_notifier *cn = NULL; | |
3012 | int ret = -EINVAL; | |
3013 | ||
3014 | if (!clk || !nb) | |
3015 | return -EINVAL; | |
3016 | ||
eab89f69 | 3017 | clk_prepare_lock(); |
b2476490 MT |
3018 | |
3019 | list_for_each_entry(cn, &clk_notifier_list, node) | |
3020 | if (cn->clk == clk) | |
3021 | break; | |
3022 | ||
3023 | if (cn->clk == clk) { | |
3024 | ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); | |
3025 | ||
035a61c3 | 3026 | clk->core->notifier_count--; |
b2476490 MT |
3027 | |
3028 | /* XXX the notifier code should handle this better */ | |
3029 | if (!cn->notifier_head.head) { | |
3030 | srcu_cleanup_notifier_head(&cn->notifier_head); | |
72b5322f | 3031 | list_del(&cn->node); |
b2476490 MT |
3032 | kfree(cn); |
3033 | } | |
3034 | ||
3035 | } else { | |
3036 | ret = -ENOENT; | |
3037 | } | |
3038 | ||
eab89f69 | 3039 | clk_prepare_unlock(); |
b2476490 MT |
3040 | |
3041 | return ret; | |
3042 | } | |
3043 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); | |
766e6a4e GL |
3044 | |
3045 | #ifdef CONFIG_OF | |
3046 | /** | |
3047 | * struct of_clk_provider - Clock provider registration structure | |
3048 | * @link: Entry in global list of clock providers | |
3049 | * @node: Pointer to device tree node of clock provider | |
3050 | * @get: Get clock callback. Returns NULL or a struct clk for the | |
3051 | * given clock specifier | |
3052 | * @data: context pointer to be passed into @get callback | |
3053 | */ | |
3054 | struct of_clk_provider { | |
3055 | struct list_head link; | |
3056 | ||
3057 | struct device_node *node; | |
3058 | struct clk *(*get)(struct of_phandle_args *clkspec, void *data); | |
0861e5b8 | 3059 | struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data); |
766e6a4e GL |
3060 | void *data; |
3061 | }; | |
3062 | ||
f2f6c255 PG |
3063 | static const struct of_device_id __clk_of_table_sentinel |
3064 | __used __section(__clk_of_table_end); | |
3065 | ||
766e6a4e | 3066 | static LIST_HEAD(of_clk_providers); |
d6782c26 SN |
3067 | static DEFINE_MUTEX(of_clk_mutex); |
3068 | ||
766e6a4e GL |
3069 | struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, |
3070 | void *data) | |
3071 | { | |
3072 | return data; | |
3073 | } | |
3074 | EXPORT_SYMBOL_GPL(of_clk_src_simple_get); | |
3075 | ||
0861e5b8 SB |
3076 | struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data) |
3077 | { | |
3078 | return data; | |
3079 | } | |
3080 | EXPORT_SYMBOL_GPL(of_clk_hw_simple_get); | |
3081 | ||
494bfec9 SG |
3082 | struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) |
3083 | { | |
3084 | struct clk_onecell_data *clk_data = data; | |
3085 | unsigned int idx = clkspec->args[0]; | |
3086 | ||
3087 | if (idx >= clk_data->clk_num) { | |
7e96353c | 3088 | pr_err("%s: invalid clock index %u\n", __func__, idx); |
494bfec9 SG |
3089 | return ERR_PTR(-EINVAL); |
3090 | } | |
3091 | ||
3092 | return clk_data->clks[idx]; | |
3093 | } | |
3094 | EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); | |
3095 | ||
0861e5b8 SB |
3096 | struct clk_hw * |
3097 | of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data) | |
3098 | { | |
3099 | struct clk_hw_onecell_data *hw_data = data; | |
3100 | unsigned int idx = clkspec->args[0]; | |
3101 | ||
3102 | if (idx >= hw_data->num) { | |
3103 | pr_err("%s: invalid index %u\n", __func__, idx); | |
3104 | return ERR_PTR(-EINVAL); | |
3105 | } | |
3106 | ||
3107 | return hw_data->hws[idx]; | |
3108 | } | |
3109 | EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get); | |
3110 | ||
766e6a4e GL |
3111 | /** |
3112 | * of_clk_add_provider() - Register a clock provider for a node | |
3113 | * @np: Device node pointer associated with clock provider | |
3114 | * @clk_src_get: callback for decoding clock | |
3115 | * @data: context pointer for @clk_src_get callback. | |
3116 | */ | |
3117 | int of_clk_add_provider(struct device_node *np, | |
3118 | struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, | |
3119 | void *data), | |
3120 | void *data) | |
3121 | { | |
3122 | struct of_clk_provider *cp; | |
86be408b | 3123 | int ret; |
766e6a4e GL |
3124 | |
3125 | cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); | |
3126 | if (!cp) | |
3127 | return -ENOMEM; | |
3128 | ||
3129 | cp->node = of_node_get(np); | |
3130 | cp->data = data; | |
3131 | cp->get = clk_src_get; | |
3132 | ||
d6782c26 | 3133 | mutex_lock(&of_clk_mutex); |
766e6a4e | 3134 | list_add(&cp->link, &of_clk_providers); |
d6782c26 | 3135 | mutex_unlock(&of_clk_mutex); |
766e6a4e GL |
3136 | pr_debug("Added clock from %s\n", np->full_name); |
3137 | ||
86be408b SN |
3138 | ret = of_clk_set_defaults(np, true); |
3139 | if (ret < 0) | |
3140 | of_clk_del_provider(np); | |
3141 | ||
3142 | return ret; | |
766e6a4e GL |
3143 | } |
3144 | EXPORT_SYMBOL_GPL(of_clk_add_provider); | |
3145 | ||
0861e5b8 SB |
3146 | /** |
3147 | * of_clk_add_hw_provider() - Register a clock provider for a node | |
3148 | * @np: Device node pointer associated with clock provider | |
3149 | * @get: callback for decoding clk_hw | |
3150 | * @data: context pointer for @get callback. | |
3151 | */ | |
3152 | int of_clk_add_hw_provider(struct device_node *np, | |
3153 | struct clk_hw *(*get)(struct of_phandle_args *clkspec, | |
3154 | void *data), | |
3155 | void *data) | |
3156 | { | |
3157 | struct of_clk_provider *cp; | |
3158 | int ret; | |
3159 | ||
3160 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); | |
3161 | if (!cp) | |
3162 | return -ENOMEM; | |
3163 | ||
3164 | cp->node = of_node_get(np); | |
3165 | cp->data = data; | |
3166 | cp->get_hw = get; | |
3167 | ||
3168 | mutex_lock(&of_clk_mutex); | |
3169 | list_add(&cp->link, &of_clk_providers); | |
3170 | mutex_unlock(&of_clk_mutex); | |
3171 | pr_debug("Added clk_hw provider from %s\n", np->full_name); | |
3172 | ||
3173 | ret = of_clk_set_defaults(np, true); | |
3174 | if (ret < 0) | |
3175 | of_clk_del_provider(np); | |
3176 | ||
3177 | return ret; | |
3178 | } | |
3179 | EXPORT_SYMBOL_GPL(of_clk_add_hw_provider); | |
3180 | ||
766e6a4e GL |
3181 | /** |
3182 | * of_clk_del_provider() - Remove a previously registered clock provider | |
3183 | * @np: Device node pointer associated with clock provider | |
3184 | */ | |
3185 | void of_clk_del_provider(struct device_node *np) | |
3186 | { | |
3187 | struct of_clk_provider *cp; | |
3188 | ||
d6782c26 | 3189 | mutex_lock(&of_clk_mutex); |
766e6a4e GL |
3190 | list_for_each_entry(cp, &of_clk_providers, link) { |
3191 | if (cp->node == np) { | |
3192 | list_del(&cp->link); | |
3193 | of_node_put(cp->node); | |
3194 | kfree(cp); | |
3195 | break; | |
3196 | } | |
3197 | } | |
d6782c26 | 3198 | mutex_unlock(&of_clk_mutex); |
766e6a4e GL |
3199 | } |
3200 | EXPORT_SYMBOL_GPL(of_clk_del_provider); | |
3201 | ||
0861e5b8 SB |
3202 | static struct clk_hw * |
3203 | __of_clk_get_hw_from_provider(struct of_clk_provider *provider, | |
3204 | struct of_phandle_args *clkspec) | |
3205 | { | |
3206 | struct clk *clk; | |
0861e5b8 | 3207 | |
74002fcd SB |
3208 | if (provider->get_hw) |
3209 | return provider->get_hw(clkspec, provider->data); | |
0861e5b8 | 3210 | |
74002fcd SB |
3211 | clk = provider->get(clkspec, provider->data); |
3212 | if (IS_ERR(clk)) | |
3213 | return ERR_CAST(clk); | |
3214 | return __clk_get_hw(clk); | |
0861e5b8 SB |
3215 | } |
3216 | ||
73e0e496 SB |
3217 | struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec, |
3218 | const char *dev_id, const char *con_id) | |
766e6a4e GL |
3219 | { |
3220 | struct of_clk_provider *provider; | |
a34cd466 | 3221 | struct clk *clk = ERR_PTR(-EPROBE_DEFER); |
f155d15b | 3222 | struct clk_hw *hw; |
766e6a4e | 3223 | |
306c342f SB |
3224 | if (!clkspec) |
3225 | return ERR_PTR(-EINVAL); | |
3226 | ||
766e6a4e | 3227 | /* Check if we have such a provider in our array */ |
306c342f | 3228 | mutex_lock(&of_clk_mutex); |
766e6a4e | 3229 | list_for_each_entry(provider, &of_clk_providers, link) { |
f155d15b | 3230 | if (provider->node == clkspec->np) { |
0861e5b8 | 3231 | hw = __of_clk_get_hw_from_provider(provider, clkspec); |
0861e5b8 | 3232 | clk = __clk_create_clk(hw, dev_id, con_id); |
f155d15b | 3233 | } |
73e0e496 | 3234 | |
f155d15b SB |
3235 | if (!IS_ERR(clk)) { |
3236 | if (!__clk_get(clk)) { | |
73e0e496 SB |
3237 | __clk_free_clk(clk); |
3238 | clk = ERR_PTR(-ENOENT); | |
3239 | } | |
3240 | ||
766e6a4e | 3241 | break; |
73e0e496 | 3242 | } |
766e6a4e | 3243 | } |
306c342f | 3244 | mutex_unlock(&of_clk_mutex); |
d6782c26 SN |
3245 | |
3246 | return clk; | |
3247 | } | |
3248 | ||
306c342f SB |
3249 | /** |
3250 | * of_clk_get_from_provider() - Lookup a clock from a clock provider | |
3251 | * @clkspec: pointer to a clock specifier data structure | |
3252 | * | |
3253 | * This function looks up a struct clk from the registered list of clock | |
3254 | * providers, an input is a clock specifier data structure as returned | |
3255 | * from the of_parse_phandle_with_args() function call. | |
3256 | */ | |
d6782c26 SN |
3257 | struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) |
3258 | { | |
306c342f | 3259 | return __of_clk_get_from_provider(clkspec, NULL, __func__); |
766e6a4e | 3260 | } |
fb4dd222 | 3261 | EXPORT_SYMBOL_GPL(of_clk_get_from_provider); |
766e6a4e | 3262 | |
929e7f3b SB |
3263 | /** |
3264 | * of_clk_get_parent_count() - Count the number of clocks a device node has | |
3265 | * @np: device node to count | |
3266 | * | |
3267 | * Returns: The number of clocks that are possible parents of this node | |
3268 | */ | |
3269 | unsigned int of_clk_get_parent_count(struct device_node *np) | |
f6102742 | 3270 | { |
929e7f3b SB |
3271 | int count; |
3272 | ||
3273 | count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); | |
3274 | if (count < 0) | |
3275 | return 0; | |
3276 | ||
3277 | return count; | |
f6102742 MT |
3278 | } |
3279 | EXPORT_SYMBOL_GPL(of_clk_get_parent_count); | |
3280 | ||
766e6a4e GL |
3281 | const char *of_clk_get_parent_name(struct device_node *np, int index) |
3282 | { | |
3283 | struct of_phandle_args clkspec; | |
7a0fc1a3 | 3284 | struct property *prop; |
766e6a4e | 3285 | const char *clk_name; |
7a0fc1a3 BD |
3286 | const __be32 *vp; |
3287 | u32 pv; | |
766e6a4e | 3288 | int rc; |
7a0fc1a3 | 3289 | int count; |
0a4807c2 | 3290 | struct clk *clk; |
766e6a4e | 3291 | |
766e6a4e GL |
3292 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, |
3293 | &clkspec); | |
3294 | if (rc) | |
3295 | return NULL; | |
3296 | ||
7a0fc1a3 BD |
3297 | index = clkspec.args_count ? clkspec.args[0] : 0; |
3298 | count = 0; | |
3299 | ||
3300 | /* if there is an indices property, use it to transfer the index | |
3301 | * specified into an array offset for the clock-output-names property. | |
3302 | */ | |
3303 | of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { | |
3304 | if (index == pv) { | |
3305 | index = count; | |
3306 | break; | |
3307 | } | |
3308 | count++; | |
3309 | } | |
8da411cc MY |
3310 | /* We went off the end of 'clock-indices' without finding it */ |
3311 | if (prop && !vp) | |
3312 | return NULL; | |
7a0fc1a3 | 3313 | |
766e6a4e | 3314 | if (of_property_read_string_index(clkspec.np, "clock-output-names", |
7a0fc1a3 | 3315 | index, |
0a4807c2 SB |
3316 | &clk_name) < 0) { |
3317 | /* | |
3318 | * Best effort to get the name if the clock has been | |
3319 | * registered with the framework. If the clock isn't | |
3320 | * registered, we return the node name as the name of | |
3321 | * the clock as long as #clock-cells = 0. | |
3322 | */ | |
3323 | clk = of_clk_get_from_provider(&clkspec); | |
3324 | if (IS_ERR(clk)) { | |
3325 | if (clkspec.args_count == 0) | |
3326 | clk_name = clkspec.np->name; | |
3327 | else | |
3328 | clk_name = NULL; | |
3329 | } else { | |
3330 | clk_name = __clk_get_name(clk); | |
3331 | clk_put(clk); | |
3332 | } | |
3333 | } | |
3334 | ||
766e6a4e GL |
3335 | |
3336 | of_node_put(clkspec.np); | |
3337 | return clk_name; | |
3338 | } | |
3339 | EXPORT_SYMBOL_GPL(of_clk_get_parent_name); | |
3340 | ||
2e61dfb3 DN |
3341 | /** |
3342 | * of_clk_parent_fill() - Fill @parents with names of @np's parents and return | |
3343 | * number of parents | |
3344 | * @np: Device node pointer associated with clock provider | |
3345 | * @parents: pointer to char array that hold the parents' names | |
3346 | * @size: size of the @parents array | |
3347 | * | |
3348 | * Return: number of parents for the clock node. | |
3349 | */ | |
3350 | int of_clk_parent_fill(struct device_node *np, const char **parents, | |
3351 | unsigned int size) | |
3352 | { | |
3353 | unsigned int i = 0; | |
3354 | ||
3355 | while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL) | |
3356 | i++; | |
3357 | ||
3358 | return i; | |
3359 | } | |
3360 | EXPORT_SYMBOL_GPL(of_clk_parent_fill); | |
3361 | ||
1771b10d GC |
3362 | struct clock_provider { |
3363 | of_clk_init_cb_t clk_init_cb; | |
3364 | struct device_node *np; | |
3365 | struct list_head node; | |
3366 | }; | |
3367 | ||
1771b10d GC |
3368 | /* |
3369 | * This function looks for a parent clock. If there is one, then it | |
3370 | * checks that the provider for this parent clock was initialized, in | |
3371 | * this case the parent clock will be ready. | |
3372 | */ | |
3373 | static int parent_ready(struct device_node *np) | |
3374 | { | |
3375 | int i = 0; | |
3376 | ||
3377 | while (true) { | |
3378 | struct clk *clk = of_clk_get(np, i); | |
3379 | ||
3380 | /* this parent is ready we can check the next one */ | |
3381 | if (!IS_ERR(clk)) { | |
3382 | clk_put(clk); | |
3383 | i++; | |
3384 | continue; | |
3385 | } | |
3386 | ||
3387 | /* at least one parent is not ready, we exit now */ | |
3388 | if (PTR_ERR(clk) == -EPROBE_DEFER) | |
3389 | return 0; | |
3390 | ||
3391 | /* | |
3392 | * Here we make assumption that the device tree is | |
3393 | * written correctly. So an error means that there is | |
3394 | * no more parent. As we didn't exit yet, then the | |
3395 | * previous parent are ready. If there is no clock | |
3396 | * parent, no need to wait for them, then we can | |
3397 | * consider their absence as being ready | |
3398 | */ | |
3399 | return 1; | |
3400 | } | |
3401 | } | |
3402 | ||
d56f8994 LJ |
3403 | /** |
3404 | * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree | |
3405 | * @np: Device node pointer associated with clock provider | |
3406 | * @index: clock index | |
3407 | * @flags: pointer to clk_core->flags | |
3408 | * | |
3409 | * Detects if the clock-critical property exists and, if so, sets the | |
3410 | * corresponding CLK_IS_CRITICAL flag. | |
3411 | * | |
3412 | * Do not use this function. It exists only for legacy Device Tree | |
3413 | * bindings, such as the one-clock-per-node style that are outdated. | |
3414 | * Those bindings typically put all clock data into .dts and the Linux | |
3415 | * driver has no clock data, thus making it impossible to set this flag | |
3416 | * correctly from the driver. Only those drivers may call | |
3417 | * of_clk_detect_critical from their setup functions. | |
3418 | * | |
3419 | * Return: error code or zero on success | |
3420 | */ | |
3421 | int of_clk_detect_critical(struct device_node *np, | |
3422 | int index, unsigned long *flags) | |
3423 | { | |
3424 | struct property *prop; | |
3425 | const __be32 *cur; | |
3426 | uint32_t idx; | |
3427 | ||
3428 | if (!np || !flags) | |
3429 | return -EINVAL; | |
3430 | ||
3431 | of_property_for_each_u32(np, "clock-critical", prop, cur, idx) | |
3432 | if (index == idx) | |
3433 | *flags |= CLK_IS_CRITICAL; | |
3434 | ||
3435 | return 0; | |
3436 | } | |
3437 | ||
766e6a4e GL |
3438 | /** |
3439 | * of_clk_init() - Scan and init clock providers from the DT | |
3440 | * @matches: array of compatible values and init functions for providers. | |
3441 | * | |
1771b10d | 3442 | * This function scans the device tree for matching clock providers |
e5ca8fb4 | 3443 | * and calls their initialization functions. It also does it by trying |
1771b10d | 3444 | * to follow the dependencies. |
766e6a4e GL |
3445 | */ |
3446 | void __init of_clk_init(const struct of_device_id *matches) | |
3447 | { | |
7f7ed584 | 3448 | const struct of_device_id *match; |
766e6a4e | 3449 | struct device_node *np; |
1771b10d GC |
3450 | struct clock_provider *clk_provider, *next; |
3451 | bool is_init_done; | |
3452 | bool force = false; | |
2573a02a | 3453 | LIST_HEAD(clk_provider_list); |
766e6a4e | 3454 | |
f2f6c255 | 3455 | if (!matches) |
819b4861 | 3456 | matches = &__clk_of_table; |
f2f6c255 | 3457 | |
1771b10d | 3458 | /* First prepare the list of the clocks providers */ |
7f7ed584 | 3459 | for_each_matching_node_and_match(np, matches, &match) { |
2e3b19f1 SB |
3460 | struct clock_provider *parent; |
3461 | ||
3e5dd6f6 GU |
3462 | if (!of_device_is_available(np)) |
3463 | continue; | |
3464 | ||
2e3b19f1 SB |
3465 | parent = kzalloc(sizeof(*parent), GFP_KERNEL); |
3466 | if (!parent) { | |
3467 | list_for_each_entry_safe(clk_provider, next, | |
3468 | &clk_provider_list, node) { | |
3469 | list_del(&clk_provider->node); | |
6bc9d9d6 | 3470 | of_node_put(clk_provider->np); |
2e3b19f1 SB |
3471 | kfree(clk_provider); |
3472 | } | |
6bc9d9d6 | 3473 | of_node_put(np); |
2e3b19f1 SB |
3474 | return; |
3475 | } | |
1771b10d GC |
3476 | |
3477 | parent->clk_init_cb = match->data; | |
6bc9d9d6 | 3478 | parent->np = of_node_get(np); |
3f6d439f | 3479 | list_add_tail(&parent->node, &clk_provider_list); |
1771b10d GC |
3480 | } |
3481 | ||
3482 | while (!list_empty(&clk_provider_list)) { | |
3483 | is_init_done = false; | |
3484 | list_for_each_entry_safe(clk_provider, next, | |
3485 | &clk_provider_list, node) { | |
3486 | if (force || parent_ready(clk_provider->np)) { | |
86be408b | 3487 | |
989eafd0 RRD |
3488 | /* Don't populate platform devices */ |
3489 | of_node_set_flag(clk_provider->np, | |
3490 | OF_POPULATED); | |
3491 | ||
1771b10d | 3492 | clk_provider->clk_init_cb(clk_provider->np); |
86be408b SN |
3493 | of_clk_set_defaults(clk_provider->np, true); |
3494 | ||
1771b10d | 3495 | list_del(&clk_provider->node); |
6bc9d9d6 | 3496 | of_node_put(clk_provider->np); |
1771b10d GC |
3497 | kfree(clk_provider); |
3498 | is_init_done = true; | |
3499 | } | |
3500 | } | |
3501 | ||
3502 | /* | |
e5ca8fb4 | 3503 | * We didn't manage to initialize any of the |
1771b10d GC |
3504 | * remaining providers during the last loop, so now we |
3505 | * initialize all the remaining ones unconditionally | |
3506 | * in case the clock parent was not mandatory | |
3507 | */ | |
3508 | if (!is_init_done) | |
3509 | force = true; | |
766e6a4e GL |
3510 | } |
3511 | } | |
3512 | #endif |