blob: 077106732550f3bfdcdf711ec78a4be81d387bc1 [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
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
12#include <linux/clk-private.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/spinlock.h>
16#include <linux/err.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050019#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070020#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053021#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070022#include <linux/sched.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070023
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020024#include "clk.h"
25
Mike Turquetteb24764902012-03-15 23:11:19 -070026static DEFINE_SPINLOCK(enable_lock);
27static DEFINE_MUTEX(prepare_lock);
28
Mike Turquette533ddeb2013-03-28 13:59:02 -070029static struct task_struct *prepare_owner;
30static struct task_struct *enable_owner;
31
32static int prepare_refcnt;
33static int enable_refcnt;
34
Mike Turquetteb24764902012-03-15 23:11:19 -070035static HLIST_HEAD(clk_root_list);
36static HLIST_HEAD(clk_orphan_list);
37static LIST_HEAD(clk_notifier_list);
38
Mike Turquetteeab89f62013-03-28 13:59:01 -070039/*** locking ***/
40static void clk_prepare_lock(void)
41{
Mike Turquette533ddeb2013-03-28 13:59:02 -070042 if (!mutex_trylock(&prepare_lock)) {
43 if (prepare_owner == current) {
44 prepare_refcnt++;
45 return;
46 }
47 mutex_lock(&prepare_lock);
48 }
49 WARN_ON_ONCE(prepare_owner != NULL);
50 WARN_ON_ONCE(prepare_refcnt != 0);
51 prepare_owner = current;
52 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070053}
54
55static void clk_prepare_unlock(void)
56{
Mike Turquette533ddeb2013-03-28 13:59:02 -070057 WARN_ON_ONCE(prepare_owner != current);
58 WARN_ON_ONCE(prepare_refcnt == 0);
59
60 if (--prepare_refcnt)
61 return;
62 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -070063 mutex_unlock(&prepare_lock);
64}
65
66static unsigned long clk_enable_lock(void)
67{
68 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -070069
70 if (!spin_trylock_irqsave(&enable_lock, flags)) {
71 if (enable_owner == current) {
72 enable_refcnt++;
73 return flags;
74 }
75 spin_lock_irqsave(&enable_lock, flags);
76 }
77 WARN_ON_ONCE(enable_owner != NULL);
78 WARN_ON_ONCE(enable_refcnt != 0);
79 enable_owner = current;
80 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070081 return flags;
82}
83
84static void clk_enable_unlock(unsigned long flags)
85{
Mike Turquette533ddeb2013-03-28 13:59:02 -070086 WARN_ON_ONCE(enable_owner != current);
87 WARN_ON_ONCE(enable_refcnt == 0);
88
89 if (--enable_refcnt)
90 return;
91 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -070092 spin_unlock_irqrestore(&enable_lock, flags);
93}
94
Mike Turquetteb24764902012-03-15 23:11:19 -070095/*** debugfs support ***/
96
Mike Turquetteea72dc22013-12-18 21:38:52 -080097#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -070098#include <linux/debugfs.h>
99
100static struct dentry *rootdir;
101static struct dentry *orphandir;
102static int inited = 0;
103
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530104static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
105{
106 if (!c)
107 return;
108
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100109 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu %-11lu",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530110 level * 3 + 1, "",
111 30 - level * 3, c->name,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100112 c->enable_count, c->prepare_count, clk_get_rate(c),
113 clk_get_accuracy(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530114 seq_printf(s, "\n");
115}
116
117static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
118 int level)
119{
120 struct clk *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530121
122 if (!c)
123 return;
124
125 clk_summary_show_one(s, c, level);
126
Sasha Levinb67bfe02013-02-27 17:06:00 -0800127 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530128 clk_summary_show_subtree(s, child, level + 1);
129}
130
131static int clk_summary_show(struct seq_file *s, void *data)
132{
133 struct clk *c;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530134
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100135 seq_printf(s, " clock enable_cnt prepare_cnt rate accuracy\n");
136 seq_printf(s, "---------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530137
Mike Turquetteeab89f62013-03-28 13:59:01 -0700138 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530139
Sasha Levinb67bfe02013-02-27 17:06:00 -0800140 hlist_for_each_entry(c, &clk_root_list, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530141 clk_summary_show_subtree(s, c, 0);
142
Sasha Levinb67bfe02013-02-27 17:06:00 -0800143 hlist_for_each_entry(c, &clk_orphan_list, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530144 clk_summary_show_subtree(s, c, 0);
145
Mike Turquetteeab89f62013-03-28 13:59:01 -0700146 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530147
148 return 0;
149}
150
151
152static int clk_summary_open(struct inode *inode, struct file *file)
153{
154 return single_open(file, clk_summary_show, inode->i_private);
155}
156
157static const struct file_operations clk_summary_fops = {
158 .open = clk_summary_open,
159 .read = seq_read,
160 .llseek = seq_lseek,
161 .release = single_release,
162};
163
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530164static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
165{
166 if (!c)
167 return;
168
169 seq_printf(s, "\"%s\": { ", c->name);
170 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
171 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Peter De Schrijver670decd2013-06-05 18:06:35 +0300172 seq_printf(s, "\"rate\": %lu", clk_get_rate(c));
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100173 seq_printf(s, "\"accuracy\": %lu", clk_get_accuracy(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530174}
175
176static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
177{
178 struct clk *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530179
180 if (!c)
181 return;
182
183 clk_dump_one(s, c, level);
184
Sasha Levinb67bfe02013-02-27 17:06:00 -0800185 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530186 seq_printf(s, ",");
187 clk_dump_subtree(s, child, level + 1);
188 }
189
190 seq_printf(s, "}");
191}
192
193static int clk_dump(struct seq_file *s, void *data)
194{
195 struct clk *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530196 bool first_node = true;
197
198 seq_printf(s, "{");
199
Mike Turquetteeab89f62013-03-28 13:59:01 -0700200 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530201
Sasha Levinb67bfe02013-02-27 17:06:00 -0800202 hlist_for_each_entry(c, &clk_root_list, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530203 if (!first_node)
204 seq_printf(s, ",");
205 first_node = false;
206 clk_dump_subtree(s, c, 0);
207 }
208
Sasha Levinb67bfe02013-02-27 17:06:00 -0800209 hlist_for_each_entry(c, &clk_orphan_list, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530210 seq_printf(s, ",");
211 clk_dump_subtree(s, c, 0);
212 }
213
Mike Turquetteeab89f62013-03-28 13:59:01 -0700214 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530215
216 seq_printf(s, "}");
217 return 0;
218}
219
220
221static int clk_dump_open(struct inode *inode, struct file *file)
222{
223 return single_open(file, clk_dump, inode->i_private);
224}
225
226static const struct file_operations clk_dump_fops = {
227 .open = clk_dump_open,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = single_release,
231};
232
Mike Turquetteb24764902012-03-15 23:11:19 -0700233/* caller must hold prepare_lock */
234static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
235{
236 struct dentry *d;
237 int ret = -ENOMEM;
238
239 if (!clk || !pdentry) {
240 ret = -EINVAL;
241 goto out;
242 }
243
244 d = debugfs_create_dir(clk->name, pdentry);
245 if (!d)
246 goto out;
247
248 clk->dentry = d;
249
250 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
251 (u32 *)&clk->rate);
252 if (!d)
253 goto err_out;
254
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100255 d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
256 (u32 *)&clk->accuracy);
257 if (!d)
258 goto err_out;
259
Mike Turquetteb24764902012-03-15 23:11:19 -0700260 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
261 (u32 *)&clk->flags);
262 if (!d)
263 goto err_out;
264
265 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
266 (u32 *)&clk->prepare_count);
267 if (!d)
268 goto err_out;
269
270 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
271 (u32 *)&clk->enable_count);
272 if (!d)
273 goto err_out;
274
275 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
276 (u32 *)&clk->notifier_count);
277 if (!d)
278 goto err_out;
279
280 ret = 0;
281 goto out;
282
283err_out:
Alex Elderb5f98e62013-11-27 09:39:49 -0600284 debugfs_remove_recursive(clk->dentry);
285 clk->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700286out:
287 return ret;
288}
289
290/* caller must hold prepare_lock */
291static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
292{
293 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700294 int ret = -EINVAL;;
295
296 if (!clk || !pdentry)
297 goto out;
298
299 ret = clk_debug_create_one(clk, pdentry);
300
301 if (ret)
302 goto out;
303
Sasha Levinb67bfe02013-02-27 17:06:00 -0800304 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700305 clk_debug_create_subtree(child, clk->dentry);
306
307 ret = 0;
308out:
309 return ret;
310}
311
312/**
313 * clk_debug_register - add a clk node to the debugfs clk tree
314 * @clk: the clk being added to the debugfs clk tree
315 *
316 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
317 * initialized. Otherwise it bails out early since the debugfs clk tree
318 * will be created lazily by clk_debug_init as part of a late_initcall.
319 *
320 * Caller must hold prepare_lock. Only clk_init calls this function (so
321 * far) so this is taken care.
322 */
323static int clk_debug_register(struct clk *clk)
324{
325 struct clk *parent;
326 struct dentry *pdentry;
327 int ret = 0;
328
329 if (!inited)
330 goto out;
331
332 parent = clk->parent;
333
334 /*
335 * Check to see if a clk is a root clk. Also check that it is
336 * safe to add this clk to debugfs
337 */
338 if (!parent)
339 if (clk->flags & CLK_IS_ROOT)
340 pdentry = rootdir;
341 else
342 pdentry = orphandir;
343 else
344 if (parent->dentry)
345 pdentry = parent->dentry;
346 else
347 goto out;
348
349 ret = clk_debug_create_subtree(clk, pdentry);
350
351out:
352 return ret;
353}
354
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200355 /**
356 * clk_debug_unregister - remove a clk node from the debugfs clk tree
357 * @clk: the clk being removed from the debugfs clk tree
358 *
359 * Dynamically removes a clk and all it's children clk nodes from the
360 * debugfs clk tree if clk->dentry points to debugfs created by
361 * clk_debug_register in __clk_init.
362 *
363 * Caller must hold prepare_lock.
364 */
365static void clk_debug_unregister(struct clk *clk)
366{
367 debugfs_remove_recursive(clk->dentry);
368}
369
Mike Turquetteb24764902012-03-15 23:11:19 -0700370/**
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200371 * clk_debug_reparent - reparent clk node in the debugfs clk tree
372 * @clk: the clk being reparented
373 * @new_parent: the new clk parent, may be NULL
374 *
375 * Rename clk entry in the debugfs clk tree if debugfs has been
376 * initialized. Otherwise it bails out early since the debugfs clk tree
377 * will be created lazily by clk_debug_init as part of a late_initcall.
378 *
379 * Caller must hold prepare_lock.
380 */
381static void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
382{
383 struct dentry *d;
384 struct dentry *new_parent_d;
385
386 if (!inited)
387 return;
388
389 if (new_parent)
390 new_parent_d = new_parent->dentry;
391 else
392 new_parent_d = orphandir;
393
394 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
395 new_parent_d, clk->name);
396 if (d)
397 clk->dentry = d;
398 else
399 pr_debug("%s: failed to rename debugfs entry for %s\n",
400 __func__, clk->name);
401}
402
403/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700404 * clk_debug_init - lazily create the debugfs clk tree visualization
405 *
406 * clks are often initialized very early during boot before memory can
407 * be dynamically allocated and well before debugfs is setup.
408 * clk_debug_init walks the clk tree hierarchy while holding
409 * prepare_lock and creates the topology as part of a late_initcall,
410 * thus insuring that clks initialized very early will still be
411 * represented in the debugfs clk tree. This function should only be
412 * called once at boot-time, and all other clks added dynamically will
413 * be done so with clk_debug_register.
414 */
415static int __init clk_debug_init(void)
416{
417 struct clk *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530418 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700419
420 rootdir = debugfs_create_dir("clk", NULL);
421
422 if (!rootdir)
423 return -ENOMEM;
424
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530425 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
426 &clk_summary_fops);
427 if (!d)
428 return -ENOMEM;
429
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530430 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
431 &clk_dump_fops);
432 if (!d)
433 return -ENOMEM;
434
Mike Turquetteb24764902012-03-15 23:11:19 -0700435 orphandir = debugfs_create_dir("orphans", rootdir);
436
437 if (!orphandir)
438 return -ENOMEM;
439
Mike Turquetteeab89f62013-03-28 13:59:01 -0700440 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700441
Sasha Levinb67bfe02013-02-27 17:06:00 -0800442 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700443 clk_debug_create_subtree(clk, rootdir);
444
Sasha Levinb67bfe02013-02-27 17:06:00 -0800445 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700446 clk_debug_create_subtree(clk, orphandir);
447
448 inited = 1;
449
Mike Turquetteeab89f62013-03-28 13:59:01 -0700450 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700451
452 return 0;
453}
454late_initcall(clk_debug_init);
455#else
456static inline int clk_debug_register(struct clk *clk) { return 0; }
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200457static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
458{
459}
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200460static inline void clk_debug_unregister(struct clk *clk)
461{
462}
Mike Turquette70d347e2012-03-26 11:53:47 -0700463#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700464
Mike Turquetteb24764902012-03-15 23:11:19 -0700465/* caller must hold prepare_lock */
Ulf Hansson1c155b32013-03-12 20:26:03 +0100466static void clk_unprepare_unused_subtree(struct clk *clk)
467{
468 struct clk *child;
469
470 if (!clk)
471 return;
472
473 hlist_for_each_entry(child, &clk->children, child_node)
474 clk_unprepare_unused_subtree(child);
475
476 if (clk->prepare_count)
477 return;
478
479 if (clk->flags & CLK_IGNORE_UNUSED)
480 return;
481
Ulf Hansson3cc82472013-03-12 20:26:04 +0100482 if (__clk_is_prepared(clk)) {
483 if (clk->ops->unprepare_unused)
484 clk->ops->unprepare_unused(clk->hw);
485 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100486 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100487 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100488}
489
490/* caller must hold prepare_lock */
Mike Turquetteb24764902012-03-15 23:11:19 -0700491static void clk_disable_unused_subtree(struct clk *clk)
492{
493 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700494 unsigned long flags;
495
496 if (!clk)
497 goto out;
498
Sasha Levinb67bfe02013-02-27 17:06:00 -0800499 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700500 clk_disable_unused_subtree(child);
501
Mike Turquetteeab89f62013-03-28 13:59:01 -0700502 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700503
504 if (clk->enable_count)
505 goto unlock_out;
506
507 if (clk->flags & CLK_IGNORE_UNUSED)
508 goto unlock_out;
509
Mike Turquette7c045a52012-12-04 11:00:35 -0800510 /*
511 * some gate clocks have special needs during the disable-unused
512 * sequence. call .disable_unused if available, otherwise fall
513 * back to .disable
514 */
515 if (__clk_is_enabled(clk)) {
516 if (clk->ops->disable_unused)
517 clk->ops->disable_unused(clk->hw);
518 else if (clk->ops->disable)
519 clk->ops->disable(clk->hw);
520 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700521
522unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700523 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700524
525out:
526 return;
527}
528
Olof Johansson1e435252013-04-27 14:10:18 -0700529static bool clk_ignore_unused;
530static int __init clk_ignore_unused_setup(char *__unused)
531{
532 clk_ignore_unused = true;
533 return 1;
534}
535__setup("clk_ignore_unused", clk_ignore_unused_setup);
536
Mike Turquetteb24764902012-03-15 23:11:19 -0700537static int clk_disable_unused(void)
538{
539 struct clk *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700540
Olof Johansson1e435252013-04-27 14:10:18 -0700541 if (clk_ignore_unused) {
542 pr_warn("clk: Not disabling unused clocks\n");
543 return 0;
544 }
545
Mike Turquetteeab89f62013-03-28 13:59:01 -0700546 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700547
Sasha Levinb67bfe02013-02-27 17:06:00 -0800548 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700549 clk_disable_unused_subtree(clk);
550
Sasha Levinb67bfe02013-02-27 17:06:00 -0800551 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700552 clk_disable_unused_subtree(clk);
553
Ulf Hansson1c155b32013-03-12 20:26:03 +0100554 hlist_for_each_entry(clk, &clk_root_list, child_node)
555 clk_unprepare_unused_subtree(clk);
556
557 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
558 clk_unprepare_unused_subtree(clk);
559
Mike Turquetteeab89f62013-03-28 13:59:01 -0700560 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700561
562 return 0;
563}
Saravana Kannand41d5802013-05-09 11:35:01 -0700564late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700565
566/*** helper functions ***/
567
Russ Dill65800b22012-11-26 11:20:09 -0800568const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700569{
570 return !clk ? NULL : clk->name;
571}
Niels de Vos48950842012-12-13 13:12:25 +0100572EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700573
Russ Dill65800b22012-11-26 11:20:09 -0800574struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700575{
576 return !clk ? NULL : clk->hw;
577}
578
Russ Dill65800b22012-11-26 11:20:09 -0800579u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700580{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700581 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700582}
583
Russ Dill65800b22012-11-26 11:20:09 -0800584struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700585{
586 return !clk ? NULL : clk->parent;
587}
588
James Hogan7ef3dcc2013-07-29 12:24:58 +0100589struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
590{
591 if (!clk || index >= clk->num_parents)
592 return NULL;
593 else if (!clk->parents)
594 return __clk_lookup(clk->parent_names[index]);
595 else if (!clk->parents[index])
596 return clk->parents[index] =
597 __clk_lookup(clk->parent_names[index]);
598 else
599 return clk->parents[index];
600}
601
Russ Dill65800b22012-11-26 11:20:09 -0800602unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700603{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700604 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700605}
606
Russ Dill65800b22012-11-26 11:20:09 -0800607unsigned int __clk_get_prepare_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700608{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700609 return !clk ? 0 : clk->prepare_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700610}
611
612unsigned long __clk_get_rate(struct clk *clk)
613{
614 unsigned long ret;
615
616 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530617 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700618 goto out;
619 }
620
621 ret = clk->rate;
622
623 if (clk->flags & CLK_IS_ROOT)
624 goto out;
625
626 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530627 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700628
629out:
630 return ret;
631}
632
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100633unsigned long __clk_get_accuracy(struct clk *clk)
634{
635 if (!clk)
636 return 0;
637
638 return clk->accuracy;
639}
640
Russ Dill65800b22012-11-26 11:20:09 -0800641unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700642{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700643 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700644}
Thierry Redingb05c6832013-09-03 09:43:51 +0200645EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700646
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100647bool __clk_is_prepared(struct clk *clk)
648{
649 int ret;
650
651 if (!clk)
652 return false;
653
654 /*
655 * .is_prepared is optional for clocks that can prepare
656 * fall back to software usage counter if it is missing
657 */
658 if (!clk->ops->is_prepared) {
659 ret = clk->prepare_count ? 1 : 0;
660 goto out;
661 }
662
663 ret = clk->ops->is_prepared(clk->hw);
664out:
665 return !!ret;
666}
667
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700668bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700669{
670 int ret;
671
672 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700673 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700674
675 /*
676 * .is_enabled is only mandatory for clocks that gate
677 * fall back to software usage counter if .is_enabled is missing
678 */
679 if (!clk->ops->is_enabled) {
680 ret = clk->enable_count ? 1 : 0;
681 goto out;
682 }
683
684 ret = clk->ops->is_enabled(clk->hw);
685out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700686 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700687}
688
689static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
690{
691 struct clk *child;
692 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700693
694 if (!strcmp(clk->name, name))
695 return clk;
696
Sasha Levinb67bfe02013-02-27 17:06:00 -0800697 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700698 ret = __clk_lookup_subtree(name, child);
699 if (ret)
700 return ret;
701 }
702
703 return NULL;
704}
705
706struct clk *__clk_lookup(const char *name)
707{
708 struct clk *root_clk;
709 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700710
711 if (!name)
712 return NULL;
713
714 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800715 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700716 ret = __clk_lookup_subtree(name, root_clk);
717 if (ret)
718 return ret;
719 }
720
721 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800722 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700723 ret = __clk_lookup_subtree(name, root_clk);
724 if (ret)
725 return ret;
726 }
727
728 return NULL;
729}
730
James Hogane366fdd2013-07-29 12:25:02 +0100731/*
732 * Helper for finding best parent to provide a given frequency. This can be used
733 * directly as a determine_rate callback (e.g. for a mux), or from a more
734 * complex clock that may combine a mux with other operations.
735 */
736long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
737 unsigned long *best_parent_rate,
738 struct clk **best_parent_p)
739{
740 struct clk *clk = hw->clk, *parent, *best_parent = NULL;
741 int i, num_parents;
742 unsigned long parent_rate, best = 0;
743
744 /* if NO_REPARENT flag set, pass through to current parent */
745 if (clk->flags & CLK_SET_RATE_NO_REPARENT) {
746 parent = clk->parent;
747 if (clk->flags & CLK_SET_RATE_PARENT)
748 best = __clk_round_rate(parent, rate);
749 else if (parent)
750 best = __clk_get_rate(parent);
751 else
752 best = __clk_get_rate(clk);
753 goto out;
754 }
755
756 /* find the parent that can provide the fastest rate <= rate */
757 num_parents = clk->num_parents;
758 for (i = 0; i < num_parents; i++) {
759 parent = clk_get_parent_by_index(clk, i);
760 if (!parent)
761 continue;
762 if (clk->flags & CLK_SET_RATE_PARENT)
763 parent_rate = __clk_round_rate(parent, rate);
764 else
765 parent_rate = __clk_get_rate(parent);
766 if (parent_rate <= rate && parent_rate > best) {
767 best_parent = parent;
768 best = parent_rate;
769 }
770 }
771
772out:
773 if (best_parent)
774 *best_parent_p = best_parent;
775 *best_parent_rate = best;
776
777 return best;
778}
779
Mike Turquetteb24764902012-03-15 23:11:19 -0700780/*** clk api ***/
781
782void __clk_unprepare(struct clk *clk)
783{
784 if (!clk)
785 return;
786
787 if (WARN_ON(clk->prepare_count == 0))
788 return;
789
790 if (--clk->prepare_count > 0)
791 return;
792
793 WARN_ON(clk->enable_count > 0);
794
795 if (clk->ops->unprepare)
796 clk->ops->unprepare(clk->hw);
797
798 __clk_unprepare(clk->parent);
799}
800
801/**
802 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200803 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700804 *
805 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
806 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
807 * if the operation may sleep. One example is a clk which is accessed over
808 * I2c. In the complex case a clk gate operation may require a fast and a slow
809 * part. It is this reason that clk_unprepare and clk_disable are not mutually
810 * exclusive. In fact clk_disable must be called before clk_unprepare.
811 */
812void clk_unprepare(struct clk *clk)
813{
Mike Turquetteeab89f62013-03-28 13:59:01 -0700814 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700815 __clk_unprepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700816 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700817}
818EXPORT_SYMBOL_GPL(clk_unprepare);
819
820int __clk_prepare(struct clk *clk)
821{
822 int ret = 0;
823
824 if (!clk)
825 return 0;
826
827 if (clk->prepare_count == 0) {
828 ret = __clk_prepare(clk->parent);
829 if (ret)
830 return ret;
831
832 if (clk->ops->prepare) {
833 ret = clk->ops->prepare(clk->hw);
834 if (ret) {
835 __clk_unprepare(clk->parent);
836 return ret;
837 }
838 }
839 }
840
841 clk->prepare_count++;
842
843 return 0;
844}
845
846/**
847 * clk_prepare - prepare a clock source
848 * @clk: the clk being prepared
849 *
850 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
851 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
852 * operation may sleep. One example is a clk which is accessed over I2c. In
853 * the complex case a clk ungate operation may require a fast and a slow part.
854 * It is this reason that clk_prepare and clk_enable are not mutually
855 * exclusive. In fact clk_prepare must be called before clk_enable.
856 * Returns 0 on success, -EERROR otherwise.
857 */
858int clk_prepare(struct clk *clk)
859{
860 int ret;
861
Mike Turquetteeab89f62013-03-28 13:59:01 -0700862 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700863 ret = __clk_prepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700864 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700865
866 return ret;
867}
868EXPORT_SYMBOL_GPL(clk_prepare);
869
870static void __clk_disable(struct clk *clk)
871{
872 if (!clk)
873 return;
874
Fengguang Wue47c6a32012-07-30 14:39:54 -0700875 if (WARN_ON(IS_ERR(clk)))
876 return;
877
Mike Turquetteb24764902012-03-15 23:11:19 -0700878 if (WARN_ON(clk->enable_count == 0))
879 return;
880
881 if (--clk->enable_count > 0)
882 return;
883
884 if (clk->ops->disable)
885 clk->ops->disable(clk->hw);
886
887 __clk_disable(clk->parent);
888}
889
890/**
891 * clk_disable - gate a clock
892 * @clk: the clk being gated
893 *
894 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
895 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
896 * clk if the operation is fast and will never sleep. One example is a
897 * SoC-internal clk which is controlled via simple register writes. In the
898 * complex case a clk gate operation may require a fast and a slow part. It is
899 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
900 * In fact clk_disable must be called before clk_unprepare.
901 */
902void clk_disable(struct clk *clk)
903{
904 unsigned long flags;
905
Mike Turquetteeab89f62013-03-28 13:59:01 -0700906 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700907 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700908 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700909}
910EXPORT_SYMBOL_GPL(clk_disable);
911
912static int __clk_enable(struct clk *clk)
913{
914 int ret = 0;
915
916 if (!clk)
917 return 0;
918
919 if (WARN_ON(clk->prepare_count == 0))
920 return -ESHUTDOWN;
921
922 if (clk->enable_count == 0) {
923 ret = __clk_enable(clk->parent);
924
925 if (ret)
926 return ret;
927
928 if (clk->ops->enable) {
929 ret = clk->ops->enable(clk->hw);
930 if (ret) {
931 __clk_disable(clk->parent);
932 return ret;
933 }
934 }
935 }
936
937 clk->enable_count++;
938 return 0;
939}
940
941/**
942 * clk_enable - ungate a clock
943 * @clk: the clk being ungated
944 *
945 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
946 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
947 * if the operation will never sleep. One example is a SoC-internal clk which
948 * is controlled via simple register writes. In the complex case a clk ungate
949 * operation may require a fast and a slow part. It is this reason that
950 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
951 * must be called before clk_enable. Returns 0 on success, -EERROR
952 * otherwise.
953 */
954int clk_enable(struct clk *clk)
955{
956 unsigned long flags;
957 int ret;
958
Mike Turquetteeab89f62013-03-28 13:59:01 -0700959 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700960 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700961 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700962
963 return ret;
964}
965EXPORT_SYMBOL_GPL(clk_enable);
966
967/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700968 * __clk_round_rate - round the given rate for a clk
969 * @clk: round the rate of this clock
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200970 * @rate: the rate which is to be rounded
Mike Turquetteb24764902012-03-15 23:11:19 -0700971 *
972 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
973 */
974unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
975{
Shawn Guo81536e02012-04-12 20:50:17 +0800976 unsigned long parent_rate = 0;
James Hogan71472c02013-07-29 12:25:00 +0100977 struct clk *parent;
Mike Turquetteb24764902012-03-15 23:11:19 -0700978
979 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700980 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700981
James Hogan71472c02013-07-29 12:25:00 +0100982 parent = clk->parent;
983 if (parent)
984 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700985
James Hogan71472c02013-07-29 12:25:00 +0100986 if (clk->ops->determine_rate)
987 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
988 &parent);
989 else if (clk->ops->round_rate)
990 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
991 else if (clk->flags & CLK_SET_RATE_PARENT)
992 return __clk_round_rate(clk->parent, rate);
993 else
994 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700995}
996
997/**
998 * clk_round_rate - round the given rate for a clk
999 * @clk: the clk for which we are rounding a rate
1000 * @rate: the rate which is to be rounded
1001 *
1002 * Takes in a rate as input and rounds it to a rate that the clk can actually
1003 * use which is then returned. If clk doesn't support round_rate operation
1004 * then the parent rate is returned.
1005 */
1006long clk_round_rate(struct clk *clk, unsigned long rate)
1007{
1008 unsigned long ret;
1009
Mike Turquetteeab89f62013-03-28 13:59:01 -07001010 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001011 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001012 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001013
1014 return ret;
1015}
1016EXPORT_SYMBOL_GPL(clk_round_rate);
1017
1018/**
1019 * __clk_notify - call clk notifier chain
1020 * @clk: struct clk * that is changing rate
1021 * @msg: clk notifier type (see include/linux/clk.h)
1022 * @old_rate: old clk rate
1023 * @new_rate: new clk rate
1024 *
1025 * Triggers a notifier call chain on the clk rate-change notification
1026 * for 'clk'. Passes a pointer to the struct clk and the previous
1027 * and current rates to the notifier callback. Intended to be called by
1028 * internal clock code only. Returns NOTIFY_DONE from the last driver
1029 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1030 * a driver returns that.
1031 */
1032static int __clk_notify(struct clk *clk, unsigned long msg,
1033 unsigned long old_rate, unsigned long new_rate)
1034{
1035 struct clk_notifier *cn;
1036 struct clk_notifier_data cnd;
1037 int ret = NOTIFY_DONE;
1038
1039 cnd.clk = clk;
1040 cnd.old_rate = old_rate;
1041 cnd.new_rate = new_rate;
1042
1043 list_for_each_entry(cn, &clk_notifier_list, node) {
1044 if (cn->clk == clk) {
1045 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1046 &cnd);
1047 break;
1048 }
1049 }
1050
1051 return ret;
1052}
1053
1054/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001055 * __clk_recalc_accuracies
1056 * @clk: first clk in the subtree
1057 *
1058 * Walks the subtree of clks starting with clk and recalculates accuracies as
1059 * it goes. Note that if a clk does not implement the .recalc_accuracy
1060 * callback then it is assumed that the clock will take on the accuracy of it's
1061 * parent.
1062 *
1063 * Caller must hold prepare_lock.
1064 */
1065static void __clk_recalc_accuracies(struct clk *clk)
1066{
1067 unsigned long parent_accuracy = 0;
1068 struct clk *child;
1069
1070 if (clk->parent)
1071 parent_accuracy = clk->parent->accuracy;
1072
1073 if (clk->ops->recalc_accuracy)
1074 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1075 parent_accuracy);
1076 else
1077 clk->accuracy = parent_accuracy;
1078
1079 hlist_for_each_entry(child, &clk->children, child_node)
1080 __clk_recalc_accuracies(child);
1081}
1082
1083/**
1084 * clk_get_accuracy - return the accuracy of clk
1085 * @clk: the clk whose accuracy is being returned
1086 *
1087 * Simply returns the cached accuracy of the clk, unless
1088 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1089 * issued.
1090 * If clk is NULL then returns 0.
1091 */
1092long clk_get_accuracy(struct clk *clk)
1093{
1094 unsigned long accuracy;
1095
1096 clk_prepare_lock();
1097 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1098 __clk_recalc_accuracies(clk);
1099
1100 accuracy = __clk_get_accuracy(clk);
1101 clk_prepare_unlock();
1102
1103 return accuracy;
1104}
1105EXPORT_SYMBOL_GPL(clk_get_accuracy);
1106
1107/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001108 * __clk_recalc_rates
1109 * @clk: first clk in the subtree
1110 * @msg: notification type (see include/linux/clk.h)
1111 *
1112 * Walks the subtree of clks starting with clk and recalculates rates as it
1113 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001114 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001115 *
1116 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1117 * if necessary.
1118 *
1119 * Caller must hold prepare_lock.
1120 */
1121static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
1122{
1123 unsigned long old_rate;
1124 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001125 struct clk *child;
1126
1127 old_rate = clk->rate;
1128
1129 if (clk->parent)
1130 parent_rate = clk->parent->rate;
1131
1132 if (clk->ops->recalc_rate)
1133 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1134 else
1135 clk->rate = parent_rate;
1136
1137 /*
1138 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1139 * & ABORT_RATE_CHANGE notifiers
1140 */
1141 if (clk->notifier_count && msg)
1142 __clk_notify(clk, msg, old_rate, clk->rate);
1143
Sasha Levinb67bfe02013-02-27 17:06:00 -08001144 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001145 __clk_recalc_rates(child, msg);
1146}
1147
1148/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001149 * clk_get_rate - return the rate of clk
1150 * @clk: the clk whose rate is being returned
1151 *
1152 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1153 * is set, which means a recalc_rate will be issued.
1154 * If clk is NULL then returns 0.
1155 */
1156unsigned long clk_get_rate(struct clk *clk)
1157{
1158 unsigned long rate;
1159
Mike Turquetteeab89f62013-03-28 13:59:01 -07001160 clk_prepare_lock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001161
1162 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1163 __clk_recalc_rates(clk, 0);
1164
1165 rate = __clk_get_rate(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001166 clk_prepare_unlock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001167
1168 return rate;
1169}
1170EXPORT_SYMBOL_GPL(clk_get_rate);
1171
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001172static int clk_fetch_parent_index(struct clk *clk, struct clk *parent)
James Hogan4935b222013-07-29 12:24:59 +01001173{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001174 int i;
James Hogan4935b222013-07-29 12:24:59 +01001175
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001176 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001177 clk->parents = kcalloc(clk->num_parents,
1178 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001179 if (!clk->parents)
1180 return -ENOMEM;
1181 }
James Hogan4935b222013-07-29 12:24:59 +01001182
1183 /*
1184 * find index of new parent clock using cached parent ptrs,
1185 * or if not yet cached, use string name comparison and cache
1186 * them now to avoid future calls to __clk_lookup.
1187 */
1188 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001189 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001190 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001191
1192 if (clk->parents[i])
1193 continue;
1194
1195 if (!strcmp(clk->parent_names[i], parent->name)) {
1196 clk->parents[i] = __clk_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001197 return i;
James Hogan4935b222013-07-29 12:24:59 +01001198 }
1199 }
1200
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001201 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001202}
1203
1204static void clk_reparent(struct clk *clk, struct clk *new_parent)
1205{
1206 hlist_del(&clk->child_node);
1207
James Hogan903efc52013-08-29 12:10:51 +01001208 if (new_parent) {
1209 /* avoid duplicate POST_RATE_CHANGE notifications */
1210 if (new_parent->new_child == clk)
1211 new_parent->new_child = NULL;
1212
James Hogan4935b222013-07-29 12:24:59 +01001213 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001214 } else {
James Hogan4935b222013-07-29 12:24:59 +01001215 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001216 }
James Hogan4935b222013-07-29 12:24:59 +01001217
1218 clk->parent = new_parent;
1219}
1220
Stephen Boyd3fa22522014-01-15 10:47:22 -08001221static struct clk *__clk_set_parent_before(struct clk *clk, struct clk *parent)
James Hogan4935b222013-07-29 12:24:59 +01001222{
1223 unsigned long flags;
James Hogan4935b222013-07-29 12:24:59 +01001224 struct clk *old_parent = clk->parent;
1225
1226 /*
1227 * Migrate prepare state between parents and prevent race with
1228 * clk_enable().
1229 *
1230 * If the clock is not prepared, then a race with
1231 * clk_enable/disable() is impossible since we already have the
1232 * prepare lock (future calls to clk_enable() need to be preceded by
1233 * a clk_prepare()).
1234 *
1235 * If the clock is prepared, migrate the prepared state to the new
1236 * parent and also protect against a race with clk_enable() by
1237 * forcing the clock and the new parent on. This ensures that all
1238 * future calls to clk_enable() are practically NOPs with respect to
1239 * hardware and software states.
1240 *
1241 * See also: Comment for clk_set_parent() below.
1242 */
1243 if (clk->prepare_count) {
1244 __clk_prepare(parent);
1245 clk_enable(parent);
1246 clk_enable(clk);
1247 }
1248
1249 /* update the clk tree topology */
1250 flags = clk_enable_lock();
1251 clk_reparent(clk, parent);
1252 clk_enable_unlock(flags);
1253
Stephen Boyd3fa22522014-01-15 10:47:22 -08001254 return old_parent;
1255}
1256
1257static void __clk_set_parent_after(struct clk *clk, struct clk *parent,
1258 struct clk *old_parent)
1259{
1260 /*
1261 * Finish the migration of prepare state and undo the changes done
1262 * for preventing a race with clk_enable().
1263 */
1264 if (clk->prepare_count) {
1265 clk_disable(clk);
1266 clk_disable(old_parent);
1267 __clk_unprepare(old_parent);
1268 }
1269
1270 /* update debugfs with new clk tree topology */
1271 clk_debug_reparent(clk, parent);
1272}
1273
1274static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
1275{
1276 unsigned long flags;
1277 int ret = 0;
1278 struct clk *old_parent;
1279
1280 old_parent = __clk_set_parent_before(clk, parent);
1281
James Hogan4935b222013-07-29 12:24:59 +01001282 /* change clock input source */
1283 if (parent && clk->ops->set_parent)
1284 ret = clk->ops->set_parent(clk->hw, p_index);
1285
1286 if (ret) {
1287 flags = clk_enable_lock();
1288 clk_reparent(clk, old_parent);
1289 clk_enable_unlock(flags);
1290
1291 if (clk->prepare_count) {
1292 clk_disable(clk);
1293 clk_disable(parent);
1294 __clk_unprepare(parent);
1295 }
1296 return ret;
1297 }
1298
Stephen Boyd3fa22522014-01-15 10:47:22 -08001299 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001300
James Hogan4935b222013-07-29 12:24:59 +01001301 return 0;
1302}
1303
Ulf Hanssona093bde2012-08-31 14:21:28 +02001304/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001305 * __clk_speculate_rates
1306 * @clk: first clk in the subtree
1307 * @parent_rate: the "future" rate of clk's parent
1308 *
1309 * Walks the subtree of clks starting with clk, speculating rates as it
1310 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1311 *
1312 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1313 * pre-rate change notifications and returns early if no clks in the
1314 * subtree have subscribed to the notifications. Note that if a clk does not
1315 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001316 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001317 *
1318 * Caller must hold prepare_lock.
1319 */
1320static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
1321{
Mike Turquetteb24764902012-03-15 23:11:19 -07001322 struct clk *child;
1323 unsigned long new_rate;
1324 int ret = NOTIFY_DONE;
1325
1326 if (clk->ops->recalc_rate)
1327 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1328 else
1329 new_rate = parent_rate;
1330
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001331 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001332 if (clk->notifier_count)
1333 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1334
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001335 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001336 goto out;
1337
Sasha Levinb67bfe02013-02-27 17:06:00 -08001338 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001339 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001340 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001341 break;
1342 }
1343
1344out:
1345 return ret;
1346}
1347
James Hogan71472c02013-07-29 12:25:00 +01001348static void clk_calc_subtree(struct clk *clk, unsigned long new_rate,
1349 struct clk *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001350{
1351 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001352
1353 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001354 clk->new_parent = new_parent;
1355 clk->new_parent_index = p_index;
1356 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1357 clk->new_child = NULL;
1358 if (new_parent && new_parent != clk->parent)
1359 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001360
Sasha Levinb67bfe02013-02-27 17:06:00 -08001361 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001362 if (child->ops->recalc_rate)
1363 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
1364 else
1365 child->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001366 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001367 }
1368}
1369
1370/*
1371 * calculate the new rates returning the topmost clock that has to be
1372 * changed.
1373 */
1374static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
1375{
1376 struct clk *top = clk;
James Hogan71472c02013-07-29 12:25:00 +01001377 struct clk *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001378 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001379 unsigned long new_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001380 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001381
Mike Turquette7452b212012-03-26 14:45:36 -07001382 /* sanity */
1383 if (IS_ERR_OR_NULL(clk))
1384 return NULL;
1385
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001386 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001387 parent = old_parent = clk->parent;
1388 if (parent)
1389 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001390
James Hogan71472c02013-07-29 12:25:00 +01001391 /* find the closest rate and parent clk/rate */
1392 if (clk->ops->determine_rate) {
1393 new_rate = clk->ops->determine_rate(clk->hw, rate,
1394 &best_parent_rate,
1395 &parent);
1396 } else if (clk->ops->round_rate) {
1397 new_rate = clk->ops->round_rate(clk->hw, rate,
1398 &best_parent_rate);
1399 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1400 /* pass-through clock without adjustable parent */
1401 clk->new_rate = clk->rate;
1402 return NULL;
1403 } else {
1404 /* pass-through clock with adjustable parent */
1405 top = clk_calc_new_rates(parent, rate);
1406 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001407 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001408 }
1409
James Hogan71472c02013-07-29 12:25:00 +01001410 /* some clocks must be gated to change parent */
1411 if (parent != old_parent &&
1412 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1413 pr_debug("%s: %s not gated but wants to reparent\n",
1414 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001415 return NULL;
1416 }
1417
James Hogan71472c02013-07-29 12:25:00 +01001418 /* try finding the new parent index */
1419 if (parent) {
1420 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001421 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001422 pr_debug("%s: clk %s can not be parent of clk %s\n",
1423 __func__, parent->name, clk->name);
1424 return NULL;
1425 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001426 }
1427
James Hogan71472c02013-07-29 12:25:00 +01001428 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1429 best_parent_rate != parent->rate)
1430 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001431
1432out:
James Hogan71472c02013-07-29 12:25:00 +01001433 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001434
1435 return top;
1436}
1437
1438/*
1439 * Notify about rate changes in a subtree. Always walk down the whole tree
1440 * so that in case of an error we can walk down the whole tree again and
1441 * abort the change.
1442 */
1443static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
1444{
James Hogan71472c02013-07-29 12:25:00 +01001445 struct clk *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001446 int ret = NOTIFY_DONE;
1447
1448 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301449 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001450
1451 if (clk->notifier_count) {
1452 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001453 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001454 fail_clk = clk;
1455 }
1456
Sasha Levinb67bfe02013-02-27 17:06:00 -08001457 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001458 /* Skip children who will be reparented to another clock */
1459 if (child->new_parent && child->new_parent != clk)
1460 continue;
1461 tmp_clk = clk_propagate_rate_change(child, event);
1462 if (tmp_clk)
1463 fail_clk = tmp_clk;
1464 }
1465
1466 /* handle the new child who might not be in clk->children yet */
1467 if (clk->new_child) {
1468 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1469 if (tmp_clk)
1470 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001471 }
1472
1473 return fail_clk;
1474}
1475
1476/*
1477 * walk down a subtree and set the new rates notifying the rate
1478 * change on the way
1479 */
1480static void clk_change_rate(struct clk *clk)
1481{
1482 struct clk *child;
1483 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001484 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001485 bool skip_set_rate = false;
1486 struct clk *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001487
1488 old_rate = clk->rate;
1489
Stephen Boyd3fa22522014-01-15 10:47:22 -08001490 if (clk->new_parent)
1491 best_parent_rate = clk->new_parent->rate;
1492 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001493 best_parent_rate = clk->parent->rate;
1494
Stephen Boyd3fa22522014-01-15 10:47:22 -08001495 if (clk->new_parent && clk->new_parent != clk->parent) {
1496 old_parent = __clk_set_parent_before(clk, clk->new_parent);
1497
1498 if (clk->ops->set_rate_and_parent) {
1499 skip_set_rate = true;
1500 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1501 best_parent_rate,
1502 clk->new_parent_index);
1503 } else if (clk->ops->set_parent) {
1504 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1505 }
1506
1507 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1508 }
1509
1510 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001511 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001512
1513 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001514 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001515 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001516 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001517
1518 if (clk->notifier_count && old_rate != clk->rate)
1519 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1520
James Hogan71472c02013-07-29 12:25:00 +01001521 hlist_for_each_entry(child, &clk->children, child_node) {
1522 /* Skip children who will be reparented to another clock */
1523 if (child->new_parent && child->new_parent != clk)
1524 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001525 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001526 }
1527
1528 /* handle the new child who might not be in clk->children yet */
1529 if (clk->new_child)
1530 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001531}
1532
1533/**
1534 * clk_set_rate - specify a new rate for clk
1535 * @clk: the clk whose rate is being changed
1536 * @rate: the new rate for clk
1537 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001538 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001539 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001540 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1541 * propagate up to clk's parent; whether or not this happens depends on the
1542 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1543 * after calling .round_rate then upstream parent propagation is ignored. If
1544 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001545 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001546 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1547 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001548 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001549 * Rate changes are accomplished via tree traversal that also recalculates the
1550 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001551 *
1552 * Returns 0 on success, -EERROR otherwise.
1553 */
1554int clk_set_rate(struct clk *clk, unsigned long rate)
1555{
1556 struct clk *top, *fail_clk;
1557 int ret = 0;
1558
Mike Turquette89ac8d72013-08-21 23:58:09 -07001559 if (!clk)
1560 return 0;
1561
Mike Turquetteb24764902012-03-15 23:11:19 -07001562 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001563 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001564
1565 /* bail early if nothing to do */
Peter De Schrijver34e452a2013-06-05 18:06:36 +03001566 if (rate == clk_get_rate(clk))
Mike Turquetteb24764902012-03-15 23:11:19 -07001567 goto out;
1568
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001569 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301570 ret = -EBUSY;
1571 goto out;
1572 }
1573
Mike Turquetteb24764902012-03-15 23:11:19 -07001574 /* calculate new rates and get the topmost changed clock */
1575 top = clk_calc_new_rates(clk, rate);
1576 if (!top) {
1577 ret = -EINVAL;
1578 goto out;
1579 }
1580
1581 /* notify that we are about to change rates */
1582 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1583 if (fail_clk) {
1584 pr_warn("%s: failed to set %s rate\n", __func__,
1585 fail_clk->name);
1586 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1587 ret = -EBUSY;
1588 goto out;
1589 }
1590
1591 /* change the rates */
1592 clk_change_rate(top);
1593
Mike Turquetteb24764902012-03-15 23:11:19 -07001594out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001595 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001596
1597 return ret;
1598}
1599EXPORT_SYMBOL_GPL(clk_set_rate);
1600
1601/**
1602 * clk_get_parent - return the parent of a clk
1603 * @clk: the clk whose parent gets returned
1604 *
1605 * Simply returns clk->parent. Returns NULL if clk is NULL.
1606 */
1607struct clk *clk_get_parent(struct clk *clk)
1608{
1609 struct clk *parent;
1610
Mike Turquetteeab89f62013-03-28 13:59:01 -07001611 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001612 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001613 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001614
1615 return parent;
1616}
1617EXPORT_SYMBOL_GPL(clk_get_parent);
1618
1619/*
1620 * .get_parent is mandatory for clocks with multiple possible parents. It is
1621 * optional for single-parent clocks. Always call .get_parent if it is
1622 * available and WARN if it is missing for multi-parent clocks.
1623 *
1624 * For single-parent clocks without .get_parent, first check to see if the
1625 * .parents array exists, and if so use it to avoid an expensive tree
1626 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1627 */
1628static struct clk *__clk_init_parent(struct clk *clk)
1629{
1630 struct clk *ret = NULL;
1631 u8 index;
1632
1633 /* handle the trivial cases */
1634
1635 if (!clk->num_parents)
1636 goto out;
1637
1638 if (clk->num_parents == 1) {
1639 if (IS_ERR_OR_NULL(clk->parent))
1640 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1641 ret = clk->parent;
1642 goto out;
1643 }
1644
1645 if (!clk->ops->get_parent) {
1646 WARN(!clk->ops->get_parent,
1647 "%s: multi-parent clocks must implement .get_parent\n",
1648 __func__);
1649 goto out;
1650 };
1651
1652 /*
1653 * Do our best to cache parent clocks in clk->parents. This prevents
1654 * unnecessary and expensive calls to __clk_lookup. We don't set
1655 * clk->parent here; that is done by the calling function
1656 */
1657
1658 index = clk->ops->get_parent(clk->hw);
1659
1660 if (!clk->parents)
1661 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001662 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001663 GFP_KERNEL);
1664
James Hogan7ef3dcc2013-07-29 12:24:58 +01001665 ret = clk_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001666
1667out:
1668 return ret;
1669}
1670
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001671void __clk_reparent(struct clk *clk, struct clk *new_parent)
1672{
1673 clk_reparent(clk, new_parent);
1674 clk_debug_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001675 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001676 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1677}
1678
Mike Turquetteb24764902012-03-15 23:11:19 -07001679/**
1680 * clk_set_parent - switch the parent of a mux clk
1681 * @clk: the mux clk whose input we are switching
1682 * @parent: the new input to clk
1683 *
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001684 * Re-parent clk to use parent as its new input source. If clk is in
1685 * prepared state, the clk will get enabled for the duration of this call. If
1686 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1687 * that, the reparenting is glitchy in hardware, etc), use the
1688 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1689 *
1690 * After successfully changing clk's parent clk_set_parent will update the
1691 * clk topology, sysfs topology and propagate rate recalculation via
1692 * __clk_recalc_rates.
1693 *
1694 * Returns 0 on success, -EERROR otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001695 */
1696int clk_set_parent(struct clk *clk, struct clk *parent)
1697{
1698 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001699 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001700 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001701
Mike Turquette89ac8d72013-08-21 23:58:09 -07001702 if (!clk)
1703 return 0;
1704
1705 if (!clk->ops)
Mike Turquetteb24764902012-03-15 23:11:19 -07001706 return -EINVAL;
1707
Ulf Hansson031dcc92013-04-02 23:09:38 +02001708 /* verify ops for for multi-parent clks */
1709 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07001710 return -ENOSYS;
1711
1712 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001713 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001714
1715 if (clk->parent == parent)
1716 goto out;
1717
Ulf Hansson031dcc92013-04-02 23:09:38 +02001718 /* check that we are allowed to re-parent if the clock is in use */
1719 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1720 ret = -EBUSY;
1721 goto out;
1722 }
1723
1724 /* try finding the new parent index */
1725 if (parent) {
1726 p_index = clk_fetch_parent_index(clk, parent);
1727 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001728 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001729 pr_debug("%s: clk %s can not be parent of clk %s\n",
1730 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001731 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001732 goto out;
1733 }
1734 }
1735
Mike Turquetteb24764902012-03-15 23:11:19 -07001736 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07001737 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001738
1739 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001740 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001741 goto out;
1742
Ulf Hansson031dcc92013-04-02 23:09:38 +02001743 /* do the re-parent */
1744 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001745
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001746 /* propagate rate an accuracy recalculation accordingly */
1747 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001748 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001749 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001750 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001751 __clk_recalc_accuracies(clk);
1752 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001753
1754out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001755 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001756
1757 return ret;
1758}
1759EXPORT_SYMBOL_GPL(clk_set_parent);
1760
1761/**
1762 * __clk_init - initialize the data structures in a struct clk
1763 * @dev: device initializing this clk, placeholder for now
1764 * @clk: clk being initialized
1765 *
1766 * Initializes the lists in struct clk, queries the hardware for the
1767 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001768 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001769int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001770{
Mike Turquetted1302a32012-03-29 14:30:40 -07001771 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001772 struct clk *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001773 struct hlist_node *tmp2;
Mike Turquetteb24764902012-03-15 23:11:19 -07001774
1775 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001776 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001777
Mike Turquetteeab89f62013-03-28 13:59:01 -07001778 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001779
1780 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001781 if (__clk_lookup(clk->name)) {
1782 pr_debug("%s: clk %s already initialized\n",
1783 __func__, clk->name);
1784 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001785 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001786 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001787
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001788 /* check that clk_ops are sane. See Documentation/clk.txt */
1789 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01001790 !((clk->ops->round_rate || clk->ops->determine_rate) &&
1791 clk->ops->recalc_rate)) {
1792 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001793 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001794 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001795 goto out;
1796 }
1797
1798 if (clk->ops->set_parent && !clk->ops->get_parent) {
1799 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1800 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001801 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001802 goto out;
1803 }
1804
Stephen Boyd3fa22522014-01-15 10:47:22 -08001805 if (clk->ops->set_rate_and_parent &&
1806 !(clk->ops->set_parent && clk->ops->set_rate)) {
1807 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
1808 __func__, clk->name);
1809 ret = -EINVAL;
1810 goto out;
1811 }
1812
Mike Turquetteb24764902012-03-15 23:11:19 -07001813 /* throw a WARN if any entries in parent_names are NULL */
1814 for (i = 0; i < clk->num_parents; i++)
1815 WARN(!clk->parent_names[i],
1816 "%s: invalid NULL in %s's .parent_names\n",
1817 __func__, clk->name);
1818
1819 /*
1820 * Allocate an array of struct clk *'s to avoid unnecessary string
1821 * look-ups of clk's possible parents. This can fail for clocks passed
1822 * in to clk_init during early boot; thus any access to clk->parents[]
1823 * must always check for a NULL pointer and try to populate it if
1824 * necessary.
1825 *
1826 * If clk->parents is not NULL we skip this entire block. This allows
1827 * for clock drivers to statically initialize clk->parents.
1828 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301829 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001830 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
1831 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001832 /*
1833 * __clk_lookup returns NULL for parents that have not been
1834 * clk_init'd; thus any access to clk->parents[] must check
1835 * for a NULL pointer. We can always perform lazy lookups for
1836 * missing parents later on.
1837 */
1838 if (clk->parents)
1839 for (i = 0; i < clk->num_parents; i++)
1840 clk->parents[i] =
1841 __clk_lookup(clk->parent_names[i]);
1842 }
1843
1844 clk->parent = __clk_init_parent(clk);
1845
1846 /*
1847 * Populate clk->parent if parent has already been __clk_init'd. If
1848 * parent has not yet been __clk_init'd then place clk in the orphan
1849 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1850 * clk list.
1851 *
1852 * Every time a new clk is clk_init'd then we walk the list of orphan
1853 * clocks and re-parent any that are children of the clock currently
1854 * being clk_init'd.
1855 */
1856 if (clk->parent)
1857 hlist_add_head(&clk->child_node,
1858 &clk->parent->children);
1859 else if (clk->flags & CLK_IS_ROOT)
1860 hlist_add_head(&clk->child_node, &clk_root_list);
1861 else
1862 hlist_add_head(&clk->child_node, &clk_orphan_list);
1863
1864 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001865 * Set clk's accuracy. The preferred method is to use
1866 * .recalc_accuracy. For simple clocks and lazy developers the default
1867 * fallback is to use the parent's accuracy. If a clock doesn't have a
1868 * parent (or is orphaned) then accuracy is set to zero (perfect
1869 * clock).
1870 */
1871 if (clk->ops->recalc_accuracy)
1872 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1873 __clk_get_accuracy(clk->parent));
1874 else if (clk->parent)
1875 clk->accuracy = clk->parent->accuracy;
1876 else
1877 clk->accuracy = 0;
1878
1879 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07001880 * Set clk's rate. The preferred method is to use .recalc_rate. For
1881 * simple clocks and lazy developers the default fallback is to use the
1882 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1883 * then rate is set to zero.
1884 */
1885 if (clk->ops->recalc_rate)
1886 clk->rate = clk->ops->recalc_rate(clk->hw,
1887 __clk_get_rate(clk->parent));
1888 else if (clk->parent)
1889 clk->rate = clk->parent->rate;
1890 else
1891 clk->rate = 0;
1892
Stephen Boyd3a5aec22013-10-16 00:40:03 -07001893 clk_debug_register(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001894 /*
1895 * walk the list of orphan clocks and reparent any that are children of
1896 * this clock
1897 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001898 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05001899 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001900 i = orphan->ops->get_parent(orphan->hw);
1901 if (!strcmp(clk->name, orphan->parent_names[i]))
1902 __clk_reparent(orphan, clk);
1903 continue;
1904 }
1905
Mike Turquetteb24764902012-03-15 23:11:19 -07001906 for (i = 0; i < orphan->num_parents; i++)
1907 if (!strcmp(clk->name, orphan->parent_names[i])) {
1908 __clk_reparent(orphan, clk);
1909 break;
1910 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001911 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001912
1913 /*
1914 * optional platform-specific magic
1915 *
1916 * The .init callback is not used by any of the basic clock types, but
1917 * exists for weird hardware that must perform initialization magic.
1918 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001919 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07001920 */
1921 if (clk->ops->init)
1922 clk->ops->init(clk->hw);
1923
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02001924 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07001925out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001926 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001927
Mike Turquetted1302a32012-03-29 14:30:40 -07001928 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001929}
1930
1931/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001932 * __clk_register - register a clock and return a cookie.
1933 *
1934 * Same as clk_register, except that the .clk field inside hw shall point to a
1935 * preallocated (generally statically allocated) struct clk. None of the fields
1936 * of the struct clk need to be initialized.
1937 *
1938 * The data pointed to by .init and .clk field shall NOT be marked as init
1939 * data.
1940 *
1941 * __clk_register is only exposed via clk-private.h and is intended for use with
1942 * very large numbers of clocks that need to be statically initialized. It is
1943 * a layering violation to include clk-private.h from any code which implements
1944 * a clock's .ops; as such any statically initialized clock data MUST be in a
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001945 * separate C file from the logic that implements its operations. Returns 0
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001946 * on success, otherwise an error code.
1947 */
1948struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1949{
1950 int ret;
1951 struct clk *clk;
1952
1953 clk = hw->clk;
1954 clk->name = hw->init->name;
1955 clk->ops = hw->init->ops;
1956 clk->hw = hw;
1957 clk->flags = hw->init->flags;
1958 clk->parent_names = hw->init->parent_names;
1959 clk->num_parents = hw->init->num_parents;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001960 if (dev && dev->driver)
1961 clk->owner = dev->driver->owner;
1962 else
1963 clk->owner = NULL;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001964
1965 ret = __clk_init(dev, clk);
1966 if (ret)
1967 return ERR_PTR(ret);
1968
1969 return clk;
1970}
1971EXPORT_SYMBOL_GPL(__clk_register);
1972
Stephen Boyd46c87732012-09-24 13:38:04 -07001973static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001974{
Mike Turquetted1302a32012-03-29 14:30:40 -07001975 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001976
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001977 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1978 if (!clk->name) {
1979 pr_err("%s: could not allocate clk->name\n", __func__);
1980 ret = -ENOMEM;
1981 goto fail_name;
1982 }
1983 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001984 if (dev && dev->driver)
1985 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07001986 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001987 clk->flags = hw->init->flags;
1988 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001989 hw->clk = clk;
1990
Mike Turquetted1302a32012-03-29 14:30:40 -07001991 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001992 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
1993 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001994
Mike Turquetted1302a32012-03-29 14:30:40 -07001995 if (!clk->parent_names) {
1996 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1997 ret = -ENOMEM;
1998 goto fail_parent_names;
1999 }
2000
2001
2002 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002003 for (i = 0; i < clk->num_parents; i++) {
2004 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
2005 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002006 if (!clk->parent_names[i]) {
2007 pr_err("%s: could not copy parent_names\n", __func__);
2008 ret = -ENOMEM;
2009 goto fail_parent_names_copy;
2010 }
2011 }
2012
2013 ret = __clk_init(dev, clk);
2014 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07002015 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07002016
2017fail_parent_names_copy:
2018 while (--i >= 0)
2019 kfree(clk->parent_names[i]);
2020 kfree(clk->parent_names);
2021fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002022 kfree(clk->name);
2023fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07002024 return ret;
2025}
2026
2027/**
2028 * clk_register - allocate a new clock, register it and return an opaque cookie
2029 * @dev: device that is registering this clock
2030 * @hw: link to hardware-specific clock data
2031 *
2032 * clk_register is the primary interface for populating the clock tree with new
2033 * clock nodes. It returns a pointer to the newly allocated struct clk which
2034 * cannot be dereferenced by driver code but may be used in conjuction with the
2035 * rest of the clock API. In the event of an error clk_register will return an
2036 * error code; drivers must test for an error code after calling clk_register.
2037 */
2038struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2039{
2040 int ret;
2041 struct clk *clk;
2042
2043 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2044 if (!clk) {
2045 pr_err("%s: could not allocate clk\n", __func__);
2046 ret = -ENOMEM;
2047 goto fail_out;
2048 }
2049
2050 ret = _clk_register(dev, hw, clk);
2051 if (!ret)
2052 return clk;
2053
Mike Turquetted1302a32012-03-29 14:30:40 -07002054 kfree(clk);
2055fail_out:
2056 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002057}
2058EXPORT_SYMBOL_GPL(clk_register);
2059
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002060/*
2061 * Free memory allocated for a clock.
2062 * Caller must hold prepare_lock.
2063 */
2064static void __clk_release(struct kref *ref)
2065{
2066 struct clk *clk = container_of(ref, struct clk, ref);
2067 int i = clk->num_parents;
2068
2069 kfree(clk->parents);
2070 while (--i >= 0)
2071 kfree(clk->parent_names[i]);
2072
2073 kfree(clk->parent_names);
2074 kfree(clk->name);
2075 kfree(clk);
2076}
2077
2078/*
2079 * Empty clk_ops for unregistered clocks. These are used temporarily
2080 * after clk_unregister() was called on a clock and until last clock
2081 * consumer calls clk_put() and the struct clk object is freed.
2082 */
2083static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2084{
2085 return -ENXIO;
2086}
2087
2088static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2089{
2090 WARN_ON_ONCE(1);
2091}
2092
2093static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2094 unsigned long parent_rate)
2095{
2096 return -ENXIO;
2097}
2098
2099static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2100{
2101 return -ENXIO;
2102}
2103
2104static const struct clk_ops clk_nodrv_ops = {
2105 .enable = clk_nodrv_prepare_enable,
2106 .disable = clk_nodrv_disable_unprepare,
2107 .prepare = clk_nodrv_prepare_enable,
2108 .unprepare = clk_nodrv_disable_unprepare,
2109 .set_rate = clk_nodrv_set_rate,
2110 .set_parent = clk_nodrv_set_parent,
2111};
2112
Mark Brown1df5c932012-04-18 09:07:12 +01002113/**
2114 * clk_unregister - unregister a currently registered clock
2115 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002116 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002117void clk_unregister(struct clk *clk)
2118{
2119 unsigned long flags;
2120
2121 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2122 return;
2123
2124 clk_prepare_lock();
2125
2126 if (clk->ops == &clk_nodrv_ops) {
2127 pr_err("%s: unregistered clock: %s\n", __func__, clk->name);
2128 goto out;
2129 }
2130 /*
2131 * Assign empty clock ops for consumers that might still hold
2132 * a reference to this clock.
2133 */
2134 flags = clk_enable_lock();
2135 clk->ops = &clk_nodrv_ops;
2136 clk_enable_unlock(flags);
2137
2138 if (!hlist_empty(&clk->children)) {
2139 struct clk *child;
2140
2141 /* Reparent all children to the orphan list. */
2142 hlist_for_each_entry(child, &clk->children, child_node)
2143 clk_set_parent(child, NULL);
2144 }
2145
2146 clk_debug_unregister(clk);
2147
2148 hlist_del_init(&clk->child_node);
2149
2150 if (clk->prepare_count)
2151 pr_warn("%s: unregistering prepared clock: %s\n",
2152 __func__, clk->name);
2153
2154 kref_put(&clk->ref, __clk_release);
2155out:
2156 clk_prepare_unlock();
2157}
Mark Brown1df5c932012-04-18 09:07:12 +01002158EXPORT_SYMBOL_GPL(clk_unregister);
2159
Stephen Boyd46c87732012-09-24 13:38:04 -07002160static void devm_clk_release(struct device *dev, void *res)
2161{
2162 clk_unregister(res);
2163}
2164
2165/**
2166 * devm_clk_register - resource managed clk_register()
2167 * @dev: device that is registering this clock
2168 * @hw: link to hardware-specific clock data
2169 *
2170 * Managed clk_register(). Clocks returned from this function are
2171 * automatically clk_unregister()ed on driver detach. See clk_register() for
2172 * more information.
2173 */
2174struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2175{
2176 struct clk *clk;
2177 int ret;
2178
2179 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
2180 if (!clk)
2181 return ERR_PTR(-ENOMEM);
2182
2183 ret = _clk_register(dev, hw, clk);
2184 if (!ret) {
2185 devres_add(dev, clk);
2186 } else {
2187 devres_free(clk);
2188 clk = ERR_PTR(ret);
2189 }
2190
2191 return clk;
2192}
2193EXPORT_SYMBOL_GPL(devm_clk_register);
2194
2195static int devm_clk_match(struct device *dev, void *res, void *data)
2196{
2197 struct clk *c = res;
2198 if (WARN_ON(!c))
2199 return 0;
2200 return c == data;
2201}
2202
2203/**
2204 * devm_clk_unregister - resource managed clk_unregister()
2205 * @clk: clock to unregister
2206 *
2207 * Deallocate a clock allocated with devm_clk_register(). Normally
2208 * this function will not need to be called and the resource management
2209 * code will ensure that the resource is freed.
2210 */
2211void devm_clk_unregister(struct device *dev, struct clk *clk)
2212{
2213 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2214}
2215EXPORT_SYMBOL_GPL(devm_clk_unregister);
2216
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002217/*
2218 * clkdev helpers
2219 */
2220int __clk_get(struct clk *clk)
2221{
2222 if (clk && !try_module_get(clk->owner))
2223 return 0;
2224
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002225 kref_get(&clk->ref);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002226 return 1;
2227}
2228
2229void __clk_put(struct clk *clk)
2230{
2231 if (WARN_ON_ONCE(IS_ERR(clk)))
2232 return;
2233
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002234 clk_prepare_lock();
2235 kref_put(&clk->ref, __clk_release);
2236 clk_prepare_unlock();
2237
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002238 if (clk)
2239 module_put(clk->owner);
2240}
2241
Mike Turquetteb24764902012-03-15 23:11:19 -07002242/*** clk rate change notifiers ***/
2243
2244/**
2245 * clk_notifier_register - add a clk rate change notifier
2246 * @clk: struct clk * to watch
2247 * @nb: struct notifier_block * with callback info
2248 *
2249 * Request notification when clk's rate changes. This uses an SRCU
2250 * notifier because we want it to block and notifier unregistrations are
2251 * uncommon. The callbacks associated with the notifier must not
2252 * re-enter into the clk framework by calling any top-level clk APIs;
2253 * this will cause a nested prepare_lock mutex.
2254 *
2255 * Pre-change notifier callbacks will be passed the current, pre-change
2256 * rate of the clk via struct clk_notifier_data.old_rate. The new,
2257 * post-change rate of the clk is passed via struct
2258 * clk_notifier_data.new_rate.
2259 *
2260 * Post-change notifiers will pass the now-current, post-change rate of
2261 * the clk in both struct clk_notifier_data.old_rate and struct
2262 * clk_notifier_data.new_rate.
2263 *
2264 * Abort-change notifiers are effectively the opposite of pre-change
2265 * notifiers: the original pre-change clk rate is passed in via struct
2266 * clk_notifier_data.new_rate and the failed post-change rate is passed
2267 * in via struct clk_notifier_data.old_rate.
2268 *
2269 * clk_notifier_register() must be called from non-atomic context.
2270 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2271 * allocation failure; otherwise, passes along the return value of
2272 * srcu_notifier_chain_register().
2273 */
2274int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2275{
2276 struct clk_notifier *cn;
2277 int ret = -ENOMEM;
2278
2279 if (!clk || !nb)
2280 return -EINVAL;
2281
Mike Turquetteeab89f62013-03-28 13:59:01 -07002282 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002283
2284 /* search the list of notifiers for this clk */
2285 list_for_each_entry(cn, &clk_notifier_list, node)
2286 if (cn->clk == clk)
2287 break;
2288
2289 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2290 if (cn->clk != clk) {
2291 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2292 if (!cn)
2293 goto out;
2294
2295 cn->clk = clk;
2296 srcu_init_notifier_head(&cn->notifier_head);
2297
2298 list_add(&cn->node, &clk_notifier_list);
2299 }
2300
2301 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2302
2303 clk->notifier_count++;
2304
2305out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002306 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002307
2308 return ret;
2309}
2310EXPORT_SYMBOL_GPL(clk_notifier_register);
2311
2312/**
2313 * clk_notifier_unregister - remove a clk rate change notifier
2314 * @clk: struct clk *
2315 * @nb: struct notifier_block * with callback info
2316 *
2317 * Request no further notification for changes to 'clk' and frees memory
2318 * allocated in clk_notifier_register.
2319 *
2320 * Returns -EINVAL if called with null arguments; otherwise, passes
2321 * along the return value of srcu_notifier_chain_unregister().
2322 */
2323int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2324{
2325 struct clk_notifier *cn = NULL;
2326 int ret = -EINVAL;
2327
2328 if (!clk || !nb)
2329 return -EINVAL;
2330
Mike Turquetteeab89f62013-03-28 13:59:01 -07002331 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002332
2333 list_for_each_entry(cn, &clk_notifier_list, node)
2334 if (cn->clk == clk)
2335 break;
2336
2337 if (cn->clk == clk) {
2338 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2339
2340 clk->notifier_count--;
2341
2342 /* XXX the notifier code should handle this better */
2343 if (!cn->notifier_head.head) {
2344 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002345 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002346 kfree(cn);
2347 }
2348
2349 } else {
2350 ret = -ENOENT;
2351 }
2352
Mike Turquetteeab89f62013-03-28 13:59:01 -07002353 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002354
2355 return ret;
2356}
2357EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002358
2359#ifdef CONFIG_OF
2360/**
2361 * struct of_clk_provider - Clock provider registration structure
2362 * @link: Entry in global list of clock providers
2363 * @node: Pointer to device tree node of clock provider
2364 * @get: Get clock callback. Returns NULL or a struct clk for the
2365 * given clock specifier
2366 * @data: context pointer to be passed into @get callback
2367 */
2368struct of_clk_provider {
2369 struct list_head link;
2370
2371 struct device_node *node;
2372 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2373 void *data;
2374};
2375
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302376static const struct of_device_id __clk_of_table_sentinel
2377 __used __section(__clk_of_table_end);
2378
Grant Likely766e6a42012-04-09 14:50:06 -05002379static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002380static DEFINE_MUTEX(of_clk_mutex);
2381
2382/* of_clk_provider list locking helpers */
2383void of_clk_lock(void)
2384{
2385 mutex_lock(&of_clk_mutex);
2386}
2387
2388void of_clk_unlock(void)
2389{
2390 mutex_unlock(&of_clk_mutex);
2391}
Grant Likely766e6a42012-04-09 14:50:06 -05002392
2393struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2394 void *data)
2395{
2396 return data;
2397}
2398EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2399
Shawn Guo494bfec2012-08-22 21:36:27 +08002400struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2401{
2402 struct clk_onecell_data *clk_data = data;
2403 unsigned int idx = clkspec->args[0];
2404
2405 if (idx >= clk_data->clk_num) {
2406 pr_err("%s: invalid clock index %d\n", __func__, idx);
2407 return ERR_PTR(-EINVAL);
2408 }
2409
2410 return clk_data->clks[idx];
2411}
2412EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2413
Grant Likely766e6a42012-04-09 14:50:06 -05002414/**
2415 * of_clk_add_provider() - Register a clock provider for a node
2416 * @np: Device node pointer associated with clock provider
2417 * @clk_src_get: callback for decoding clock
2418 * @data: context pointer for @clk_src_get callback.
2419 */
2420int of_clk_add_provider(struct device_node *np,
2421 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2422 void *data),
2423 void *data)
2424{
2425 struct of_clk_provider *cp;
2426
2427 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2428 if (!cp)
2429 return -ENOMEM;
2430
2431 cp->node = of_node_get(np);
2432 cp->data = data;
2433 cp->get = clk_src_get;
2434
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002435 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002436 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002437 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002438 pr_debug("Added clock from %s\n", np->full_name);
2439
2440 return 0;
2441}
2442EXPORT_SYMBOL_GPL(of_clk_add_provider);
2443
2444/**
2445 * of_clk_del_provider() - Remove a previously registered clock provider
2446 * @np: Device node pointer associated with clock provider
2447 */
2448void of_clk_del_provider(struct device_node *np)
2449{
2450 struct of_clk_provider *cp;
2451
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002452 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002453 list_for_each_entry(cp, &of_clk_providers, link) {
2454 if (cp->node == np) {
2455 list_del(&cp->link);
2456 of_node_put(cp->node);
2457 kfree(cp);
2458 break;
2459 }
2460 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002461 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002462}
2463EXPORT_SYMBOL_GPL(of_clk_del_provider);
2464
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002465struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05002466{
2467 struct of_clk_provider *provider;
2468 struct clk *clk = ERR_PTR(-ENOENT);
2469
2470 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002471 list_for_each_entry(provider, &of_clk_providers, link) {
2472 if (provider->node == clkspec->np)
2473 clk = provider->get(clkspec, provider->data);
2474 if (!IS_ERR(clk))
2475 break;
2476 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002477
2478 return clk;
2479}
2480
2481struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2482{
2483 struct clk *clk;
2484
2485 mutex_lock(&of_clk_mutex);
2486 clk = __of_clk_get_from_provider(clkspec);
2487 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002488
2489 return clk;
2490}
2491
Mike Turquettef6102742013-10-07 23:12:13 -07002492int of_clk_get_parent_count(struct device_node *np)
2493{
2494 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2495}
2496EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2497
Grant Likely766e6a42012-04-09 14:50:06 -05002498const char *of_clk_get_parent_name(struct device_node *np, int index)
2499{
2500 struct of_phandle_args clkspec;
2501 const char *clk_name;
2502 int rc;
2503
2504 if (index < 0)
2505 return NULL;
2506
2507 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2508 &clkspec);
2509 if (rc)
2510 return NULL;
2511
2512 if (of_property_read_string_index(clkspec.np, "clock-output-names",
2513 clkspec.args_count ? clkspec.args[0] : 0,
2514 &clk_name) < 0)
2515 clk_name = clkspec.np->name;
2516
2517 of_node_put(clkspec.np);
2518 return clk_name;
2519}
2520EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2521
2522/**
2523 * of_clk_init() - Scan and init clock providers from the DT
2524 * @matches: array of compatible values and init functions for providers.
2525 *
2526 * This function scans the device tree for matching clock providers and
2527 * calls their initialization functions
2528 */
2529void __init of_clk_init(const struct of_device_id *matches)
2530{
Alex Elder7f7ed582013-08-22 11:31:31 -05002531 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05002532 struct device_node *np;
2533
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302534 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03002535 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302536
Alex Elder7f7ed582013-08-22 11:31:31 -05002537 for_each_matching_node_and_match(np, matches, &match) {
Grant Likely766e6a42012-04-09 14:50:06 -05002538 of_clk_init_cb_t clk_init_cb = match->data;
2539 clk_init_cb(np);
2540 }
2541}
2542#endif