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