blob: baa2f66a7d19d58ffbc9966c0cb54de69fd2df33 [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
97#ifdef CONFIG_COMMON_CLK_DEBUG
98#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
109 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
110 level * 3 + 1, "",
111 30 - level * 3, c->name,
Peter De Schrijver670decd2013-06-05 18:06:35 +0300112 c->enable_count, c->prepare_count, clk_get_rate(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530113 seq_printf(s, "\n");
114}
115
116static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
117 int level)
118{
119 struct clk *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530120
121 if (!c)
122 return;
123
124 clk_summary_show_one(s, c, level);
125
Sasha Levinb67bfe02013-02-27 17:06:00 -0800126 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530127 clk_summary_show_subtree(s, child, level + 1);
128}
129
130static int clk_summary_show(struct seq_file *s, void *data)
131{
132 struct clk *c;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530133
134 seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
135 seq_printf(s, "---------------------------------------------------------------------\n");
136
Mike Turquetteeab89f62013-03-28 13:59:01 -0700137 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530138
Sasha Levinb67bfe02013-02-27 17:06:00 -0800139 hlist_for_each_entry(c, &clk_root_list, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530140 clk_summary_show_subtree(s, c, 0);
141
Sasha Levinb67bfe02013-02-27 17:06:00 -0800142 hlist_for_each_entry(c, &clk_orphan_list, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530143 clk_summary_show_subtree(s, c, 0);
144
Mike Turquetteeab89f62013-03-28 13:59:01 -0700145 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530146
147 return 0;
148}
149
150
151static int clk_summary_open(struct inode *inode, struct file *file)
152{
153 return single_open(file, clk_summary_show, inode->i_private);
154}
155
156static const struct file_operations clk_summary_fops = {
157 .open = clk_summary_open,
158 .read = seq_read,
159 .llseek = seq_lseek,
160 .release = single_release,
161};
162
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530163static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
164{
165 if (!c)
166 return;
167
168 seq_printf(s, "\"%s\": { ", c->name);
169 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
170 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Peter De Schrijver670decd2013-06-05 18:06:35 +0300171 seq_printf(s, "\"rate\": %lu", clk_get_rate(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530172}
173
174static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
175{
176 struct clk *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530177
178 if (!c)
179 return;
180
181 clk_dump_one(s, c, level);
182
Sasha Levinb67bfe02013-02-27 17:06:00 -0800183 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530184 seq_printf(s, ",");
185 clk_dump_subtree(s, child, level + 1);
186 }
187
188 seq_printf(s, "}");
189}
190
191static int clk_dump(struct seq_file *s, void *data)
192{
193 struct clk *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530194 bool first_node = true;
195
196 seq_printf(s, "{");
197
Mike Turquetteeab89f62013-03-28 13:59:01 -0700198 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530199
Sasha Levinb67bfe02013-02-27 17:06:00 -0800200 hlist_for_each_entry(c, &clk_root_list, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530201 if (!first_node)
202 seq_printf(s, ",");
203 first_node = false;
204 clk_dump_subtree(s, c, 0);
205 }
206
Sasha Levinb67bfe02013-02-27 17:06:00 -0800207 hlist_for_each_entry(c, &clk_orphan_list, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530208 seq_printf(s, ",");
209 clk_dump_subtree(s, c, 0);
210 }
211
Mike Turquetteeab89f62013-03-28 13:59:01 -0700212 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530213
214 seq_printf(s, "}");
215 return 0;
216}
217
218
219static int clk_dump_open(struct inode *inode, struct file *file)
220{
221 return single_open(file, clk_dump, inode->i_private);
222}
223
224static const struct file_operations clk_dump_fops = {
225 .open = clk_dump_open,
226 .read = seq_read,
227 .llseek = seq_lseek,
228 .release = single_release,
229};
230
Mike Turquetteb24764902012-03-15 23:11:19 -0700231/* caller must hold prepare_lock */
232static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
233{
234 struct dentry *d;
235 int ret = -ENOMEM;
236
237 if (!clk || !pdentry) {
238 ret = -EINVAL;
239 goto out;
240 }
241
242 d = debugfs_create_dir(clk->name, pdentry);
243 if (!d)
244 goto out;
245
246 clk->dentry = d;
247
248 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
249 (u32 *)&clk->rate);
250 if (!d)
251 goto err_out;
252
253 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
254 (u32 *)&clk->flags);
255 if (!d)
256 goto err_out;
257
258 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
259 (u32 *)&clk->prepare_count);
260 if (!d)
261 goto err_out;
262
263 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
264 (u32 *)&clk->enable_count);
265 if (!d)
266 goto err_out;
267
268 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
269 (u32 *)&clk->notifier_count);
270 if (!d)
271 goto err_out;
272
273 ret = 0;
274 goto out;
275
276err_out:
Alex Elderb5f98e62013-11-27 09:39:49 -0600277 debugfs_remove_recursive(clk->dentry);
278 clk->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700279out:
280 return ret;
281}
282
283/* caller must hold prepare_lock */
284static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
285{
286 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700287 int ret = -EINVAL;;
288
289 if (!clk || !pdentry)
290 goto out;
291
292 ret = clk_debug_create_one(clk, pdentry);
293
294 if (ret)
295 goto out;
296
Sasha Levinb67bfe02013-02-27 17:06:00 -0800297 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700298 clk_debug_create_subtree(child, clk->dentry);
299
300 ret = 0;
301out:
302 return ret;
303}
304
305/**
306 * clk_debug_register - add a clk node to the debugfs clk tree
307 * @clk: the clk being added to the debugfs clk tree
308 *
309 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
310 * initialized. Otherwise it bails out early since the debugfs clk tree
311 * will be created lazily by clk_debug_init as part of a late_initcall.
312 *
313 * Caller must hold prepare_lock. Only clk_init calls this function (so
314 * far) so this is taken care.
315 */
316static int clk_debug_register(struct clk *clk)
317{
318 struct clk *parent;
319 struct dentry *pdentry;
320 int ret = 0;
321
322 if (!inited)
323 goto out;
324
325 parent = clk->parent;
326
327 /*
328 * Check to see if a clk is a root clk. Also check that it is
329 * safe to add this clk to debugfs
330 */
331 if (!parent)
332 if (clk->flags & CLK_IS_ROOT)
333 pdentry = rootdir;
334 else
335 pdentry = orphandir;
336 else
337 if (parent->dentry)
338 pdentry = parent->dentry;
339 else
340 goto out;
341
342 ret = clk_debug_create_subtree(clk, pdentry);
343
344out:
345 return ret;
346}
347
348/**
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200349 * clk_debug_reparent - reparent clk node in the debugfs clk tree
350 * @clk: the clk being reparented
351 * @new_parent: the new clk parent, may be NULL
352 *
353 * Rename clk entry in the debugfs clk tree if debugfs has been
354 * initialized. Otherwise it bails out early since the debugfs clk tree
355 * will be created lazily by clk_debug_init as part of a late_initcall.
356 *
357 * Caller must hold prepare_lock.
358 */
359static void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
360{
361 struct dentry *d;
362 struct dentry *new_parent_d;
363
364 if (!inited)
365 return;
366
367 if (new_parent)
368 new_parent_d = new_parent->dentry;
369 else
370 new_parent_d = orphandir;
371
372 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
373 new_parent_d, clk->name);
374 if (d)
375 clk->dentry = d;
376 else
377 pr_debug("%s: failed to rename debugfs entry for %s\n",
378 __func__, clk->name);
379}
380
381/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700382 * clk_debug_init - lazily create the debugfs clk tree visualization
383 *
384 * clks are often initialized very early during boot before memory can
385 * be dynamically allocated and well before debugfs is setup.
386 * clk_debug_init walks the clk tree hierarchy while holding
387 * prepare_lock and creates the topology as part of a late_initcall,
388 * thus insuring that clks initialized very early will still be
389 * represented in the debugfs clk tree. This function should only be
390 * called once at boot-time, and all other clks added dynamically will
391 * be done so with clk_debug_register.
392 */
393static int __init clk_debug_init(void)
394{
395 struct clk *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530396 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700397
398 rootdir = debugfs_create_dir("clk", NULL);
399
400 if (!rootdir)
401 return -ENOMEM;
402
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530403 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
404 &clk_summary_fops);
405 if (!d)
406 return -ENOMEM;
407
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530408 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
409 &clk_dump_fops);
410 if (!d)
411 return -ENOMEM;
412
Mike Turquetteb24764902012-03-15 23:11:19 -0700413 orphandir = debugfs_create_dir("orphans", rootdir);
414
415 if (!orphandir)
416 return -ENOMEM;
417
Mike Turquetteeab89f62013-03-28 13:59:01 -0700418 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700419
Sasha Levinb67bfe02013-02-27 17:06:00 -0800420 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700421 clk_debug_create_subtree(clk, rootdir);
422
Sasha Levinb67bfe02013-02-27 17:06:00 -0800423 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700424 clk_debug_create_subtree(clk, orphandir);
425
426 inited = 1;
427
Mike Turquetteeab89f62013-03-28 13:59:01 -0700428 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700429
430 return 0;
431}
432late_initcall(clk_debug_init);
433#else
434static inline int clk_debug_register(struct clk *clk) { return 0; }
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200435static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
436{
437}
Mike Turquette70d347e2012-03-26 11:53:47 -0700438#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700439
Mike Turquetteb24764902012-03-15 23:11:19 -0700440/* caller must hold prepare_lock */
Ulf Hansson1c155b32013-03-12 20:26:03 +0100441static void clk_unprepare_unused_subtree(struct clk *clk)
442{
443 struct clk *child;
444
445 if (!clk)
446 return;
447
448 hlist_for_each_entry(child, &clk->children, child_node)
449 clk_unprepare_unused_subtree(child);
450
451 if (clk->prepare_count)
452 return;
453
454 if (clk->flags & CLK_IGNORE_UNUSED)
455 return;
456
Ulf Hansson3cc82472013-03-12 20:26:04 +0100457 if (__clk_is_prepared(clk)) {
458 if (clk->ops->unprepare_unused)
459 clk->ops->unprepare_unused(clk->hw);
460 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100461 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100462 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100463}
464
465/* caller must hold prepare_lock */
Mike Turquetteb24764902012-03-15 23:11:19 -0700466static void clk_disable_unused_subtree(struct clk *clk)
467{
468 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700469 unsigned long flags;
470
471 if (!clk)
472 goto out;
473
Sasha Levinb67bfe02013-02-27 17:06:00 -0800474 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700475 clk_disable_unused_subtree(child);
476
Mike Turquetteeab89f62013-03-28 13:59:01 -0700477 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700478
479 if (clk->enable_count)
480 goto unlock_out;
481
482 if (clk->flags & CLK_IGNORE_UNUSED)
483 goto unlock_out;
484
Mike Turquette7c045a52012-12-04 11:00:35 -0800485 /*
486 * some gate clocks have special needs during the disable-unused
487 * sequence. call .disable_unused if available, otherwise fall
488 * back to .disable
489 */
490 if (__clk_is_enabled(clk)) {
491 if (clk->ops->disable_unused)
492 clk->ops->disable_unused(clk->hw);
493 else if (clk->ops->disable)
494 clk->ops->disable(clk->hw);
495 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700496
497unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700498 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700499
500out:
501 return;
502}
503
Olof Johansson1e435252013-04-27 14:10:18 -0700504static bool clk_ignore_unused;
505static int __init clk_ignore_unused_setup(char *__unused)
506{
507 clk_ignore_unused = true;
508 return 1;
509}
510__setup("clk_ignore_unused", clk_ignore_unused_setup);
511
Mike Turquetteb24764902012-03-15 23:11:19 -0700512static int clk_disable_unused(void)
513{
514 struct clk *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700515
Olof Johansson1e435252013-04-27 14:10:18 -0700516 if (clk_ignore_unused) {
517 pr_warn("clk: Not disabling unused clocks\n");
518 return 0;
519 }
520
Mike Turquetteeab89f62013-03-28 13:59:01 -0700521 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700522
Sasha Levinb67bfe02013-02-27 17:06:00 -0800523 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700524 clk_disable_unused_subtree(clk);
525
Sasha Levinb67bfe02013-02-27 17:06:00 -0800526 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700527 clk_disable_unused_subtree(clk);
528
Ulf Hansson1c155b32013-03-12 20:26:03 +0100529 hlist_for_each_entry(clk, &clk_root_list, child_node)
530 clk_unprepare_unused_subtree(clk);
531
532 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
533 clk_unprepare_unused_subtree(clk);
534
Mike Turquetteeab89f62013-03-28 13:59:01 -0700535 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700536
537 return 0;
538}
Saravana Kannand41d5802013-05-09 11:35:01 -0700539late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700540
541/*** helper functions ***/
542
Russ Dill65800b22012-11-26 11:20:09 -0800543const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700544{
545 return !clk ? NULL : clk->name;
546}
Niels de Vos48950842012-12-13 13:12:25 +0100547EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700548
Russ Dill65800b22012-11-26 11:20:09 -0800549struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700550{
551 return !clk ? NULL : clk->hw;
552}
553
Russ Dill65800b22012-11-26 11:20:09 -0800554u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700555{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700556 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700557}
558
Russ Dill65800b22012-11-26 11:20:09 -0800559struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700560{
561 return !clk ? NULL : clk->parent;
562}
563
James Hogan7ef3dcc2013-07-29 12:24:58 +0100564struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
565{
566 if (!clk || index >= clk->num_parents)
567 return NULL;
568 else if (!clk->parents)
569 return __clk_lookup(clk->parent_names[index]);
570 else if (!clk->parents[index])
571 return clk->parents[index] =
572 __clk_lookup(clk->parent_names[index]);
573 else
574 return clk->parents[index];
575}
576
Russ Dill65800b22012-11-26 11:20:09 -0800577unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700578{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700579 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700580}
581
Russ Dill65800b22012-11-26 11:20:09 -0800582unsigned int __clk_get_prepare_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700583{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700584 return !clk ? 0 : clk->prepare_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700585}
586
587unsigned long __clk_get_rate(struct clk *clk)
588{
589 unsigned long ret;
590
591 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530592 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700593 goto out;
594 }
595
596 ret = clk->rate;
597
598 if (clk->flags & CLK_IS_ROOT)
599 goto out;
600
601 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530602 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700603
604out:
605 return ret;
606}
607
Russ Dill65800b22012-11-26 11:20:09 -0800608unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700609{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700610 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700611}
Thierry Redingb05c6832013-09-03 09:43:51 +0200612EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700613
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100614bool __clk_is_prepared(struct clk *clk)
615{
616 int ret;
617
618 if (!clk)
619 return false;
620
621 /*
622 * .is_prepared is optional for clocks that can prepare
623 * fall back to software usage counter if it is missing
624 */
625 if (!clk->ops->is_prepared) {
626 ret = clk->prepare_count ? 1 : 0;
627 goto out;
628 }
629
630 ret = clk->ops->is_prepared(clk->hw);
631out:
632 return !!ret;
633}
634
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700635bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700636{
637 int ret;
638
639 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700640 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700641
642 /*
643 * .is_enabled is only mandatory for clocks that gate
644 * fall back to software usage counter if .is_enabled is missing
645 */
646 if (!clk->ops->is_enabled) {
647 ret = clk->enable_count ? 1 : 0;
648 goto out;
649 }
650
651 ret = clk->ops->is_enabled(clk->hw);
652out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700653 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700654}
655
656static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
657{
658 struct clk *child;
659 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700660
661 if (!strcmp(clk->name, name))
662 return clk;
663
Sasha Levinb67bfe02013-02-27 17:06:00 -0800664 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700665 ret = __clk_lookup_subtree(name, child);
666 if (ret)
667 return ret;
668 }
669
670 return NULL;
671}
672
673struct clk *__clk_lookup(const char *name)
674{
675 struct clk *root_clk;
676 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700677
678 if (!name)
679 return NULL;
680
681 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800682 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700683 ret = __clk_lookup_subtree(name, root_clk);
684 if (ret)
685 return ret;
686 }
687
688 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800689 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700690 ret = __clk_lookup_subtree(name, root_clk);
691 if (ret)
692 return ret;
693 }
694
695 return NULL;
696}
697
James Hogane366fdd2013-07-29 12:25:02 +0100698/*
699 * Helper for finding best parent to provide a given frequency. This can be used
700 * directly as a determine_rate callback (e.g. for a mux), or from a more
701 * complex clock that may combine a mux with other operations.
702 */
703long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
704 unsigned long *best_parent_rate,
705 struct clk **best_parent_p)
706{
707 struct clk *clk = hw->clk, *parent, *best_parent = NULL;
708 int i, num_parents;
709 unsigned long parent_rate, best = 0;
710
711 /* if NO_REPARENT flag set, pass through to current parent */
712 if (clk->flags & CLK_SET_RATE_NO_REPARENT) {
713 parent = clk->parent;
714 if (clk->flags & CLK_SET_RATE_PARENT)
715 best = __clk_round_rate(parent, rate);
716 else if (parent)
717 best = __clk_get_rate(parent);
718 else
719 best = __clk_get_rate(clk);
720 goto out;
721 }
722
723 /* find the parent that can provide the fastest rate <= rate */
724 num_parents = clk->num_parents;
725 for (i = 0; i < num_parents; i++) {
726 parent = clk_get_parent_by_index(clk, i);
727 if (!parent)
728 continue;
729 if (clk->flags & CLK_SET_RATE_PARENT)
730 parent_rate = __clk_round_rate(parent, rate);
731 else
732 parent_rate = __clk_get_rate(parent);
733 if (parent_rate <= rate && parent_rate > best) {
734 best_parent = parent;
735 best = parent_rate;
736 }
737 }
738
739out:
740 if (best_parent)
741 *best_parent_p = best_parent;
742 *best_parent_rate = best;
743
744 return best;
745}
746
Mike Turquetteb24764902012-03-15 23:11:19 -0700747/*** clk api ***/
748
749void __clk_unprepare(struct clk *clk)
750{
751 if (!clk)
752 return;
753
754 if (WARN_ON(clk->prepare_count == 0))
755 return;
756
757 if (--clk->prepare_count > 0)
758 return;
759
760 WARN_ON(clk->enable_count > 0);
761
762 if (clk->ops->unprepare)
763 clk->ops->unprepare(clk->hw);
764
765 __clk_unprepare(clk->parent);
766}
767
768/**
769 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200770 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700771 *
772 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
773 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
774 * if the operation may sleep. One example is a clk which is accessed over
775 * I2c. In the complex case a clk gate operation may require a fast and a slow
776 * part. It is this reason that clk_unprepare and clk_disable are not mutually
777 * exclusive. In fact clk_disable must be called before clk_unprepare.
778 */
779void clk_unprepare(struct clk *clk)
780{
Mike Turquetteeab89f62013-03-28 13:59:01 -0700781 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700782 __clk_unprepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700783 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700784}
785EXPORT_SYMBOL_GPL(clk_unprepare);
786
787int __clk_prepare(struct clk *clk)
788{
789 int ret = 0;
790
791 if (!clk)
792 return 0;
793
794 if (clk->prepare_count == 0) {
795 ret = __clk_prepare(clk->parent);
796 if (ret)
797 return ret;
798
799 if (clk->ops->prepare) {
800 ret = clk->ops->prepare(clk->hw);
801 if (ret) {
802 __clk_unprepare(clk->parent);
803 return ret;
804 }
805 }
806 }
807
808 clk->prepare_count++;
809
810 return 0;
811}
812
813/**
814 * clk_prepare - prepare a clock source
815 * @clk: the clk being prepared
816 *
817 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
818 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
819 * operation may sleep. One example is a clk which is accessed over I2c. In
820 * the complex case a clk ungate operation may require a fast and a slow part.
821 * It is this reason that clk_prepare and clk_enable are not mutually
822 * exclusive. In fact clk_prepare must be called before clk_enable.
823 * Returns 0 on success, -EERROR otherwise.
824 */
825int clk_prepare(struct clk *clk)
826{
827 int ret;
828
Mike Turquetteeab89f62013-03-28 13:59:01 -0700829 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700830 ret = __clk_prepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700831 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700832
833 return ret;
834}
835EXPORT_SYMBOL_GPL(clk_prepare);
836
837static void __clk_disable(struct clk *clk)
838{
839 if (!clk)
840 return;
841
Fengguang Wue47c6a32012-07-30 14:39:54 -0700842 if (WARN_ON(IS_ERR(clk)))
843 return;
844
Mike Turquetteb24764902012-03-15 23:11:19 -0700845 if (WARN_ON(clk->enable_count == 0))
846 return;
847
848 if (--clk->enable_count > 0)
849 return;
850
851 if (clk->ops->disable)
852 clk->ops->disable(clk->hw);
853
854 __clk_disable(clk->parent);
855}
856
857/**
858 * clk_disable - gate a clock
859 * @clk: the clk being gated
860 *
861 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
862 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
863 * clk if the operation is fast and will never sleep. One example is a
864 * SoC-internal clk which is controlled via simple register writes. In the
865 * complex case a clk gate operation may require a fast and a slow part. It is
866 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
867 * In fact clk_disable must be called before clk_unprepare.
868 */
869void clk_disable(struct clk *clk)
870{
871 unsigned long flags;
872
Mike Turquetteeab89f62013-03-28 13:59:01 -0700873 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700874 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700875 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700876}
877EXPORT_SYMBOL_GPL(clk_disable);
878
879static int __clk_enable(struct clk *clk)
880{
881 int ret = 0;
882
883 if (!clk)
884 return 0;
885
886 if (WARN_ON(clk->prepare_count == 0))
887 return -ESHUTDOWN;
888
889 if (clk->enable_count == 0) {
890 ret = __clk_enable(clk->parent);
891
892 if (ret)
893 return ret;
894
895 if (clk->ops->enable) {
896 ret = clk->ops->enable(clk->hw);
897 if (ret) {
898 __clk_disable(clk->parent);
899 return ret;
900 }
901 }
902 }
903
904 clk->enable_count++;
905 return 0;
906}
907
908/**
909 * clk_enable - ungate a clock
910 * @clk: the clk being ungated
911 *
912 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
913 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
914 * if the operation will never sleep. One example is a SoC-internal clk which
915 * is controlled via simple register writes. In the complex case a clk ungate
916 * operation may require a fast and a slow part. It is this reason that
917 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
918 * must be called before clk_enable. Returns 0 on success, -EERROR
919 * otherwise.
920 */
921int clk_enable(struct clk *clk)
922{
923 unsigned long flags;
924 int ret;
925
Mike Turquetteeab89f62013-03-28 13:59:01 -0700926 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700927 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700928 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700929
930 return ret;
931}
932EXPORT_SYMBOL_GPL(clk_enable);
933
934/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700935 * __clk_round_rate - round the given rate for a clk
936 * @clk: round the rate of this clock
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200937 * @rate: the rate which is to be rounded
Mike Turquetteb24764902012-03-15 23:11:19 -0700938 *
939 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
940 */
941unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
942{
Shawn Guo81536e02012-04-12 20:50:17 +0800943 unsigned long parent_rate = 0;
James Hogan71472c02013-07-29 12:25:00 +0100944 struct clk *parent;
Mike Turquetteb24764902012-03-15 23:11:19 -0700945
946 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700947 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700948
James Hogan71472c02013-07-29 12:25:00 +0100949 parent = clk->parent;
950 if (parent)
951 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700952
James Hogan71472c02013-07-29 12:25:00 +0100953 if (clk->ops->determine_rate)
954 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
955 &parent);
956 else if (clk->ops->round_rate)
957 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
958 else if (clk->flags & CLK_SET_RATE_PARENT)
959 return __clk_round_rate(clk->parent, rate);
960 else
961 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700962}
963
964/**
965 * clk_round_rate - round the given rate for a clk
966 * @clk: the clk for which we are rounding a rate
967 * @rate: the rate which is to be rounded
968 *
969 * Takes in a rate as input and rounds it to a rate that the clk can actually
970 * use which is then returned. If clk doesn't support round_rate operation
971 * then the parent rate is returned.
972 */
973long clk_round_rate(struct clk *clk, unsigned long rate)
974{
975 unsigned long ret;
976
Mike Turquetteeab89f62013-03-28 13:59:01 -0700977 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700978 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700979 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700980
981 return ret;
982}
983EXPORT_SYMBOL_GPL(clk_round_rate);
984
985/**
986 * __clk_notify - call clk notifier chain
987 * @clk: struct clk * that is changing rate
988 * @msg: clk notifier type (see include/linux/clk.h)
989 * @old_rate: old clk rate
990 * @new_rate: new clk rate
991 *
992 * Triggers a notifier call chain on the clk rate-change notification
993 * for 'clk'. Passes a pointer to the struct clk and the previous
994 * and current rates to the notifier callback. Intended to be called by
995 * internal clock code only. Returns NOTIFY_DONE from the last driver
996 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
997 * a driver returns that.
998 */
999static int __clk_notify(struct clk *clk, unsigned long msg,
1000 unsigned long old_rate, unsigned long new_rate)
1001{
1002 struct clk_notifier *cn;
1003 struct clk_notifier_data cnd;
1004 int ret = NOTIFY_DONE;
1005
1006 cnd.clk = clk;
1007 cnd.old_rate = old_rate;
1008 cnd.new_rate = new_rate;
1009
1010 list_for_each_entry(cn, &clk_notifier_list, node) {
1011 if (cn->clk == clk) {
1012 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1013 &cnd);
1014 break;
1015 }
1016 }
1017
1018 return ret;
1019}
1020
1021/**
1022 * __clk_recalc_rates
1023 * @clk: first clk in the subtree
1024 * @msg: notification type (see include/linux/clk.h)
1025 *
1026 * Walks the subtree of clks starting with clk and recalculates rates as it
1027 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001028 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001029 *
1030 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1031 * if necessary.
1032 *
1033 * Caller must hold prepare_lock.
1034 */
1035static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
1036{
1037 unsigned long old_rate;
1038 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001039 struct clk *child;
1040
1041 old_rate = clk->rate;
1042
1043 if (clk->parent)
1044 parent_rate = clk->parent->rate;
1045
1046 if (clk->ops->recalc_rate)
1047 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1048 else
1049 clk->rate = parent_rate;
1050
1051 /*
1052 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1053 * & ABORT_RATE_CHANGE notifiers
1054 */
1055 if (clk->notifier_count && msg)
1056 __clk_notify(clk, msg, old_rate, clk->rate);
1057
Sasha Levinb67bfe02013-02-27 17:06:00 -08001058 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001059 __clk_recalc_rates(child, msg);
1060}
1061
1062/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001063 * clk_get_rate - return the rate of clk
1064 * @clk: the clk whose rate is being returned
1065 *
1066 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1067 * is set, which means a recalc_rate will be issued.
1068 * If clk is NULL then returns 0.
1069 */
1070unsigned long clk_get_rate(struct clk *clk)
1071{
1072 unsigned long rate;
1073
Mike Turquetteeab89f62013-03-28 13:59:01 -07001074 clk_prepare_lock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001075
1076 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1077 __clk_recalc_rates(clk, 0);
1078
1079 rate = __clk_get_rate(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001080 clk_prepare_unlock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001081
1082 return rate;
1083}
1084EXPORT_SYMBOL_GPL(clk_get_rate);
1085
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001086static int clk_fetch_parent_index(struct clk *clk, struct clk *parent)
James Hogan4935b222013-07-29 12:24:59 +01001087{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001088 int i;
James Hogan4935b222013-07-29 12:24:59 +01001089
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001090 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001091 clk->parents = kcalloc(clk->num_parents,
1092 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001093 if (!clk->parents)
1094 return -ENOMEM;
1095 }
James Hogan4935b222013-07-29 12:24:59 +01001096
1097 /*
1098 * find index of new parent clock using cached parent ptrs,
1099 * or if not yet cached, use string name comparison and cache
1100 * them now to avoid future calls to __clk_lookup.
1101 */
1102 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001103 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001104 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001105
1106 if (clk->parents[i])
1107 continue;
1108
1109 if (!strcmp(clk->parent_names[i], parent->name)) {
1110 clk->parents[i] = __clk_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001111 return i;
James Hogan4935b222013-07-29 12:24:59 +01001112 }
1113 }
1114
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001115 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001116}
1117
1118static void clk_reparent(struct clk *clk, struct clk *new_parent)
1119{
1120 hlist_del(&clk->child_node);
1121
James Hogan903efc52013-08-29 12:10:51 +01001122 if (new_parent) {
1123 /* avoid duplicate POST_RATE_CHANGE notifications */
1124 if (new_parent->new_child == clk)
1125 new_parent->new_child = NULL;
1126
James Hogan4935b222013-07-29 12:24:59 +01001127 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001128 } else {
James Hogan4935b222013-07-29 12:24:59 +01001129 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001130 }
James Hogan4935b222013-07-29 12:24:59 +01001131
1132 clk->parent = new_parent;
1133}
1134
1135static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
1136{
1137 unsigned long flags;
1138 int ret = 0;
1139 struct clk *old_parent = clk->parent;
1140
1141 /*
1142 * Migrate prepare state between parents and prevent race with
1143 * clk_enable().
1144 *
1145 * If the clock is not prepared, then a race with
1146 * clk_enable/disable() is impossible since we already have the
1147 * prepare lock (future calls to clk_enable() need to be preceded by
1148 * a clk_prepare()).
1149 *
1150 * If the clock is prepared, migrate the prepared state to the new
1151 * parent and also protect against a race with clk_enable() by
1152 * forcing the clock and the new parent on. This ensures that all
1153 * future calls to clk_enable() are practically NOPs with respect to
1154 * hardware and software states.
1155 *
1156 * See also: Comment for clk_set_parent() below.
1157 */
1158 if (clk->prepare_count) {
1159 __clk_prepare(parent);
1160 clk_enable(parent);
1161 clk_enable(clk);
1162 }
1163
1164 /* update the clk tree topology */
1165 flags = clk_enable_lock();
1166 clk_reparent(clk, parent);
1167 clk_enable_unlock(flags);
1168
1169 /* change clock input source */
1170 if (parent && clk->ops->set_parent)
1171 ret = clk->ops->set_parent(clk->hw, p_index);
1172
1173 if (ret) {
1174 flags = clk_enable_lock();
1175 clk_reparent(clk, old_parent);
1176 clk_enable_unlock(flags);
1177
1178 if (clk->prepare_count) {
1179 clk_disable(clk);
1180 clk_disable(parent);
1181 __clk_unprepare(parent);
1182 }
1183 return ret;
1184 }
1185
1186 /*
1187 * Finish the migration of prepare state and undo the changes done
1188 * for preventing a race with clk_enable().
1189 */
1190 if (clk->prepare_count) {
1191 clk_disable(clk);
1192 clk_disable(old_parent);
1193 __clk_unprepare(old_parent);
1194 }
1195
1196 /* update debugfs with new clk tree topology */
1197 clk_debug_reparent(clk, parent);
1198 return 0;
1199}
1200
Ulf Hanssona093bde2012-08-31 14:21:28 +02001201/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001202 * __clk_speculate_rates
1203 * @clk: first clk in the subtree
1204 * @parent_rate: the "future" rate of clk's parent
1205 *
1206 * Walks the subtree of clks starting with clk, speculating rates as it
1207 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1208 *
1209 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1210 * pre-rate change notifications and returns early if no clks in the
1211 * subtree have subscribed to the notifications. Note that if a clk does not
1212 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001213 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001214 *
1215 * Caller must hold prepare_lock.
1216 */
1217static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
1218{
Mike Turquetteb24764902012-03-15 23:11:19 -07001219 struct clk *child;
1220 unsigned long new_rate;
1221 int ret = NOTIFY_DONE;
1222
1223 if (clk->ops->recalc_rate)
1224 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1225 else
1226 new_rate = parent_rate;
1227
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001228 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001229 if (clk->notifier_count)
1230 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1231
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001232 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001233 goto out;
1234
Sasha Levinb67bfe02013-02-27 17:06:00 -08001235 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001236 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001237 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001238 break;
1239 }
1240
1241out:
1242 return ret;
1243}
1244
James Hogan71472c02013-07-29 12:25:00 +01001245static void clk_calc_subtree(struct clk *clk, unsigned long new_rate,
1246 struct clk *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001247{
1248 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001249
1250 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001251 clk->new_parent = new_parent;
1252 clk->new_parent_index = p_index;
1253 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1254 clk->new_child = NULL;
1255 if (new_parent && new_parent != clk->parent)
1256 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001257
Sasha Levinb67bfe02013-02-27 17:06:00 -08001258 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001259 if (child->ops->recalc_rate)
1260 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
1261 else
1262 child->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001263 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001264 }
1265}
1266
1267/*
1268 * calculate the new rates returning the topmost clock that has to be
1269 * changed.
1270 */
1271static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
1272{
1273 struct clk *top = clk;
James Hogan71472c02013-07-29 12:25:00 +01001274 struct clk *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001275 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001276 unsigned long new_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001277 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001278
Mike Turquette7452b212012-03-26 14:45:36 -07001279 /* sanity */
1280 if (IS_ERR_OR_NULL(clk))
1281 return NULL;
1282
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001283 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001284 parent = old_parent = clk->parent;
1285 if (parent)
1286 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001287
James Hogan71472c02013-07-29 12:25:00 +01001288 /* find the closest rate and parent clk/rate */
1289 if (clk->ops->determine_rate) {
1290 new_rate = clk->ops->determine_rate(clk->hw, rate,
1291 &best_parent_rate,
1292 &parent);
1293 } else if (clk->ops->round_rate) {
1294 new_rate = clk->ops->round_rate(clk->hw, rate,
1295 &best_parent_rate);
1296 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1297 /* pass-through clock without adjustable parent */
1298 clk->new_rate = clk->rate;
1299 return NULL;
1300 } else {
1301 /* pass-through clock with adjustable parent */
1302 top = clk_calc_new_rates(parent, rate);
1303 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001304 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001305 }
1306
James Hogan71472c02013-07-29 12:25:00 +01001307 /* some clocks must be gated to change parent */
1308 if (parent != old_parent &&
1309 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1310 pr_debug("%s: %s not gated but wants to reparent\n",
1311 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001312 return NULL;
1313 }
1314
James Hogan71472c02013-07-29 12:25:00 +01001315 /* try finding the new parent index */
1316 if (parent) {
1317 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001318 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001319 pr_debug("%s: clk %s can not be parent of clk %s\n",
1320 __func__, parent->name, clk->name);
1321 return NULL;
1322 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001323 }
1324
James Hogan71472c02013-07-29 12:25:00 +01001325 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1326 best_parent_rate != parent->rate)
1327 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001328
1329out:
James Hogan71472c02013-07-29 12:25:00 +01001330 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001331
1332 return top;
1333}
1334
1335/*
1336 * Notify about rate changes in a subtree. Always walk down the whole tree
1337 * so that in case of an error we can walk down the whole tree again and
1338 * abort the change.
1339 */
1340static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
1341{
James Hogan71472c02013-07-29 12:25:00 +01001342 struct clk *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001343 int ret = NOTIFY_DONE;
1344
1345 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301346 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001347
1348 if (clk->notifier_count) {
1349 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001350 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001351 fail_clk = clk;
1352 }
1353
Sasha Levinb67bfe02013-02-27 17:06:00 -08001354 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001355 /* Skip children who will be reparented to another clock */
1356 if (child->new_parent && child->new_parent != clk)
1357 continue;
1358 tmp_clk = clk_propagate_rate_change(child, event);
1359 if (tmp_clk)
1360 fail_clk = tmp_clk;
1361 }
1362
1363 /* handle the new child who might not be in clk->children yet */
1364 if (clk->new_child) {
1365 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1366 if (tmp_clk)
1367 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001368 }
1369
1370 return fail_clk;
1371}
1372
1373/*
1374 * walk down a subtree and set the new rates notifying the rate
1375 * change on the way
1376 */
1377static void clk_change_rate(struct clk *clk)
1378{
1379 struct clk *child;
1380 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001381 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001382
1383 old_rate = clk->rate;
1384
James Hogan71472c02013-07-29 12:25:00 +01001385 /* set parent */
1386 if (clk->new_parent && clk->new_parent != clk->parent)
1387 __clk_set_parent(clk, clk->new_parent, clk->new_parent_index);
1388
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001389 if (clk->parent)
1390 best_parent_rate = clk->parent->rate;
1391
Mike Turquetteb24764902012-03-15 23:11:19 -07001392 if (clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001393 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001394
1395 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001396 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001397 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001398 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001399
1400 if (clk->notifier_count && old_rate != clk->rate)
1401 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1402
James Hogan71472c02013-07-29 12:25:00 +01001403 hlist_for_each_entry(child, &clk->children, child_node) {
1404 /* Skip children who will be reparented to another clock */
1405 if (child->new_parent && child->new_parent != clk)
1406 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001407 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001408 }
1409
1410 /* handle the new child who might not be in clk->children yet */
1411 if (clk->new_child)
1412 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001413}
1414
1415/**
1416 * clk_set_rate - specify a new rate for clk
1417 * @clk: the clk whose rate is being changed
1418 * @rate: the new rate for clk
1419 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001420 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001421 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001422 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1423 * propagate up to clk's parent; whether or not this happens depends on the
1424 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1425 * after calling .round_rate then upstream parent propagation is ignored. If
1426 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001427 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001428 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1429 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001430 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001431 * Rate changes are accomplished via tree traversal that also recalculates the
1432 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001433 *
1434 * Returns 0 on success, -EERROR otherwise.
1435 */
1436int clk_set_rate(struct clk *clk, unsigned long rate)
1437{
1438 struct clk *top, *fail_clk;
1439 int ret = 0;
1440
Mike Turquette89ac8d72013-08-21 23:58:09 -07001441 if (!clk)
1442 return 0;
1443
Mike Turquetteb24764902012-03-15 23:11:19 -07001444 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001445 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001446
1447 /* bail early if nothing to do */
Peter De Schrijver34e452a2013-06-05 18:06:36 +03001448 if (rate == clk_get_rate(clk))
Mike Turquetteb24764902012-03-15 23:11:19 -07001449 goto out;
1450
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001451 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301452 ret = -EBUSY;
1453 goto out;
1454 }
1455
Mike Turquetteb24764902012-03-15 23:11:19 -07001456 /* calculate new rates and get the topmost changed clock */
1457 top = clk_calc_new_rates(clk, rate);
1458 if (!top) {
1459 ret = -EINVAL;
1460 goto out;
1461 }
1462
1463 /* notify that we are about to change rates */
1464 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1465 if (fail_clk) {
1466 pr_warn("%s: failed to set %s rate\n", __func__,
1467 fail_clk->name);
1468 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1469 ret = -EBUSY;
1470 goto out;
1471 }
1472
1473 /* change the rates */
1474 clk_change_rate(top);
1475
Mike Turquetteb24764902012-03-15 23:11:19 -07001476out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001477 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001478
1479 return ret;
1480}
1481EXPORT_SYMBOL_GPL(clk_set_rate);
1482
1483/**
1484 * clk_get_parent - return the parent of a clk
1485 * @clk: the clk whose parent gets returned
1486 *
1487 * Simply returns clk->parent. Returns NULL if clk is NULL.
1488 */
1489struct clk *clk_get_parent(struct clk *clk)
1490{
1491 struct clk *parent;
1492
Mike Turquetteeab89f62013-03-28 13:59:01 -07001493 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001494 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001495 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001496
1497 return parent;
1498}
1499EXPORT_SYMBOL_GPL(clk_get_parent);
1500
1501/*
1502 * .get_parent is mandatory for clocks with multiple possible parents. It is
1503 * optional for single-parent clocks. Always call .get_parent if it is
1504 * available and WARN if it is missing for multi-parent clocks.
1505 *
1506 * For single-parent clocks without .get_parent, first check to see if the
1507 * .parents array exists, and if so use it to avoid an expensive tree
1508 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1509 */
1510static struct clk *__clk_init_parent(struct clk *clk)
1511{
1512 struct clk *ret = NULL;
1513 u8 index;
1514
1515 /* handle the trivial cases */
1516
1517 if (!clk->num_parents)
1518 goto out;
1519
1520 if (clk->num_parents == 1) {
1521 if (IS_ERR_OR_NULL(clk->parent))
1522 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1523 ret = clk->parent;
1524 goto out;
1525 }
1526
1527 if (!clk->ops->get_parent) {
1528 WARN(!clk->ops->get_parent,
1529 "%s: multi-parent clocks must implement .get_parent\n",
1530 __func__);
1531 goto out;
1532 };
1533
1534 /*
1535 * Do our best to cache parent clocks in clk->parents. This prevents
1536 * unnecessary and expensive calls to __clk_lookup. We don't set
1537 * clk->parent here; that is done by the calling function
1538 */
1539
1540 index = clk->ops->get_parent(clk->hw);
1541
1542 if (!clk->parents)
1543 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001544 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001545 GFP_KERNEL);
1546
James Hogan7ef3dcc2013-07-29 12:24:58 +01001547 ret = clk_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001548
1549out:
1550 return ret;
1551}
1552
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001553void __clk_reparent(struct clk *clk, struct clk *new_parent)
1554{
1555 clk_reparent(clk, new_parent);
1556 clk_debug_reparent(clk, new_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001557 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1558}
1559
Mike Turquetteb24764902012-03-15 23:11:19 -07001560/**
1561 * clk_set_parent - switch the parent of a mux clk
1562 * @clk: the mux clk whose input we are switching
1563 * @parent: the new input to clk
1564 *
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001565 * Re-parent clk to use parent as its new input source. If clk is in
1566 * prepared state, the clk will get enabled for the duration of this call. If
1567 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1568 * that, the reparenting is glitchy in hardware, etc), use the
1569 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1570 *
1571 * After successfully changing clk's parent clk_set_parent will update the
1572 * clk topology, sysfs topology and propagate rate recalculation via
1573 * __clk_recalc_rates.
1574 *
1575 * Returns 0 on success, -EERROR otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001576 */
1577int clk_set_parent(struct clk *clk, struct clk *parent)
1578{
1579 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001580 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001581 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001582
Mike Turquette89ac8d72013-08-21 23:58:09 -07001583 if (!clk)
1584 return 0;
1585
1586 if (!clk->ops)
Mike Turquetteb24764902012-03-15 23:11:19 -07001587 return -EINVAL;
1588
Ulf Hansson031dcc92013-04-02 23:09:38 +02001589 /* verify ops for for multi-parent clks */
1590 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07001591 return -ENOSYS;
1592
1593 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001594 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001595
1596 if (clk->parent == parent)
1597 goto out;
1598
Ulf Hansson031dcc92013-04-02 23:09:38 +02001599 /* check that we are allowed to re-parent if the clock is in use */
1600 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1601 ret = -EBUSY;
1602 goto out;
1603 }
1604
1605 /* try finding the new parent index */
1606 if (parent) {
1607 p_index = clk_fetch_parent_index(clk, parent);
1608 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001609 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001610 pr_debug("%s: clk %s can not be parent of clk %s\n",
1611 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001612 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001613 goto out;
1614 }
1615 }
1616
Mike Turquetteb24764902012-03-15 23:11:19 -07001617 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07001618 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001619
1620 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001621 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001622 goto out;
1623
Ulf Hansson031dcc92013-04-02 23:09:38 +02001624 /* do the re-parent */
1625 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001626
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001627 /* propagate rate recalculation accordingly */
1628 if (ret)
Mike Turquetteb24764902012-03-15 23:11:19 -07001629 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001630 else
1631 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001632
1633out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001634 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001635
1636 return ret;
1637}
1638EXPORT_SYMBOL_GPL(clk_set_parent);
1639
1640/**
1641 * __clk_init - initialize the data structures in a struct clk
1642 * @dev: device initializing this clk, placeholder for now
1643 * @clk: clk being initialized
1644 *
1645 * Initializes the lists in struct clk, queries the hardware for the
1646 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001647 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001648int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001649{
Mike Turquetted1302a32012-03-29 14:30:40 -07001650 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001651 struct clk *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001652 struct hlist_node *tmp2;
Mike Turquetteb24764902012-03-15 23:11:19 -07001653
1654 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001655 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001656
Mike Turquetteeab89f62013-03-28 13:59:01 -07001657 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001658
1659 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001660 if (__clk_lookup(clk->name)) {
1661 pr_debug("%s: clk %s already initialized\n",
1662 __func__, clk->name);
1663 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001664 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001665 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001666
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001667 /* check that clk_ops are sane. See Documentation/clk.txt */
1668 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01001669 !((clk->ops->round_rate || clk->ops->determine_rate) &&
1670 clk->ops->recalc_rate)) {
1671 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001672 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001673 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001674 goto out;
1675 }
1676
1677 if (clk->ops->set_parent && !clk->ops->get_parent) {
1678 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1679 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001680 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001681 goto out;
1682 }
1683
Mike Turquetteb24764902012-03-15 23:11:19 -07001684 /* throw a WARN if any entries in parent_names are NULL */
1685 for (i = 0; i < clk->num_parents; i++)
1686 WARN(!clk->parent_names[i],
1687 "%s: invalid NULL in %s's .parent_names\n",
1688 __func__, clk->name);
1689
1690 /*
1691 * Allocate an array of struct clk *'s to avoid unnecessary string
1692 * look-ups of clk's possible parents. This can fail for clocks passed
1693 * in to clk_init during early boot; thus any access to clk->parents[]
1694 * must always check for a NULL pointer and try to populate it if
1695 * necessary.
1696 *
1697 * If clk->parents is not NULL we skip this entire block. This allows
1698 * for clock drivers to statically initialize clk->parents.
1699 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301700 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001701 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
1702 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001703 /*
1704 * __clk_lookup returns NULL for parents that have not been
1705 * clk_init'd; thus any access to clk->parents[] must check
1706 * for a NULL pointer. We can always perform lazy lookups for
1707 * missing parents later on.
1708 */
1709 if (clk->parents)
1710 for (i = 0; i < clk->num_parents; i++)
1711 clk->parents[i] =
1712 __clk_lookup(clk->parent_names[i]);
1713 }
1714
1715 clk->parent = __clk_init_parent(clk);
1716
1717 /*
1718 * Populate clk->parent if parent has already been __clk_init'd. If
1719 * parent has not yet been __clk_init'd then place clk in the orphan
1720 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1721 * clk list.
1722 *
1723 * Every time a new clk is clk_init'd then we walk the list of orphan
1724 * clocks and re-parent any that are children of the clock currently
1725 * being clk_init'd.
1726 */
1727 if (clk->parent)
1728 hlist_add_head(&clk->child_node,
1729 &clk->parent->children);
1730 else if (clk->flags & CLK_IS_ROOT)
1731 hlist_add_head(&clk->child_node, &clk_root_list);
1732 else
1733 hlist_add_head(&clk->child_node, &clk_orphan_list);
1734
1735 /*
1736 * Set clk's rate. The preferred method is to use .recalc_rate. For
1737 * simple clocks and lazy developers the default fallback is to use the
1738 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1739 * then rate is set to zero.
1740 */
1741 if (clk->ops->recalc_rate)
1742 clk->rate = clk->ops->recalc_rate(clk->hw,
1743 __clk_get_rate(clk->parent));
1744 else if (clk->parent)
1745 clk->rate = clk->parent->rate;
1746 else
1747 clk->rate = 0;
1748
1749 /*
1750 * walk the list of orphan clocks and reparent any that are children of
1751 * this clock
1752 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001753 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05001754 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001755 i = orphan->ops->get_parent(orphan->hw);
1756 if (!strcmp(clk->name, orphan->parent_names[i]))
1757 __clk_reparent(orphan, clk);
1758 continue;
1759 }
1760
Mike Turquetteb24764902012-03-15 23:11:19 -07001761 for (i = 0; i < orphan->num_parents; i++)
1762 if (!strcmp(clk->name, orphan->parent_names[i])) {
1763 __clk_reparent(orphan, clk);
1764 break;
1765 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001766 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001767
1768 /*
1769 * optional platform-specific magic
1770 *
1771 * The .init callback is not used by any of the basic clock types, but
1772 * exists for weird hardware that must perform initialization magic.
1773 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001774 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07001775 */
1776 if (clk->ops->init)
1777 clk->ops->init(clk->hw);
1778
1779 clk_debug_register(clk);
1780
1781out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001782 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001783
Mike Turquetted1302a32012-03-29 14:30:40 -07001784 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001785}
1786
1787/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001788 * __clk_register - register a clock and return a cookie.
1789 *
1790 * Same as clk_register, except that the .clk field inside hw shall point to a
1791 * preallocated (generally statically allocated) struct clk. None of the fields
1792 * of the struct clk need to be initialized.
1793 *
1794 * The data pointed to by .init and .clk field shall NOT be marked as init
1795 * data.
1796 *
1797 * __clk_register is only exposed via clk-private.h and is intended for use with
1798 * very large numbers of clocks that need to be statically initialized. It is
1799 * a layering violation to include clk-private.h from any code which implements
1800 * a clock's .ops; as such any statically initialized clock data MUST be in a
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001801 * separate C file from the logic that implements its operations. Returns 0
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001802 * on success, otherwise an error code.
1803 */
1804struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1805{
1806 int ret;
1807 struct clk *clk;
1808
1809 clk = hw->clk;
1810 clk->name = hw->init->name;
1811 clk->ops = hw->init->ops;
1812 clk->hw = hw;
1813 clk->flags = hw->init->flags;
1814 clk->parent_names = hw->init->parent_names;
1815 clk->num_parents = hw->init->num_parents;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001816 if (dev && dev->driver)
1817 clk->owner = dev->driver->owner;
1818 else
1819 clk->owner = NULL;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001820
1821 ret = __clk_init(dev, clk);
1822 if (ret)
1823 return ERR_PTR(ret);
1824
1825 return clk;
1826}
1827EXPORT_SYMBOL_GPL(__clk_register);
1828
Stephen Boyd46c87732012-09-24 13:38:04 -07001829static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001830{
Mike Turquetted1302a32012-03-29 14:30:40 -07001831 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001832
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001833 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1834 if (!clk->name) {
1835 pr_err("%s: could not allocate clk->name\n", __func__);
1836 ret = -ENOMEM;
1837 goto fail_name;
1838 }
1839 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001840 if (dev && dev->driver)
1841 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07001842 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001843 clk->flags = hw->init->flags;
1844 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001845 hw->clk = clk;
1846
Mike Turquetted1302a32012-03-29 14:30:40 -07001847 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001848 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
1849 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001850
Mike Turquetted1302a32012-03-29 14:30:40 -07001851 if (!clk->parent_names) {
1852 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1853 ret = -ENOMEM;
1854 goto fail_parent_names;
1855 }
1856
1857
1858 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001859 for (i = 0; i < clk->num_parents; i++) {
1860 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1861 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07001862 if (!clk->parent_names[i]) {
1863 pr_err("%s: could not copy parent_names\n", __func__);
1864 ret = -ENOMEM;
1865 goto fail_parent_names_copy;
1866 }
1867 }
1868
1869 ret = __clk_init(dev, clk);
1870 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07001871 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07001872
1873fail_parent_names_copy:
1874 while (--i >= 0)
1875 kfree(clk->parent_names[i]);
1876 kfree(clk->parent_names);
1877fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001878 kfree(clk->name);
1879fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07001880 return ret;
1881}
1882
1883/**
1884 * clk_register - allocate a new clock, register it and return an opaque cookie
1885 * @dev: device that is registering this clock
1886 * @hw: link to hardware-specific clock data
1887 *
1888 * clk_register is the primary interface for populating the clock tree with new
1889 * clock nodes. It returns a pointer to the newly allocated struct clk which
1890 * cannot be dereferenced by driver code but may be used in conjuction with the
1891 * rest of the clock API. In the event of an error clk_register will return an
1892 * error code; drivers must test for an error code after calling clk_register.
1893 */
1894struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1895{
1896 int ret;
1897 struct clk *clk;
1898
1899 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1900 if (!clk) {
1901 pr_err("%s: could not allocate clk\n", __func__);
1902 ret = -ENOMEM;
1903 goto fail_out;
1904 }
1905
1906 ret = _clk_register(dev, hw, clk);
1907 if (!ret)
1908 return clk;
1909
Mike Turquetted1302a32012-03-29 14:30:40 -07001910 kfree(clk);
1911fail_out:
1912 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001913}
1914EXPORT_SYMBOL_GPL(clk_register);
1915
Mark Brown1df5c932012-04-18 09:07:12 +01001916/**
1917 * clk_unregister - unregister a currently registered clock
1918 * @clk: clock to unregister
1919 *
1920 * Currently unimplemented.
1921 */
1922void clk_unregister(struct clk *clk) {}
1923EXPORT_SYMBOL_GPL(clk_unregister);
1924
Stephen Boyd46c87732012-09-24 13:38:04 -07001925static void devm_clk_release(struct device *dev, void *res)
1926{
1927 clk_unregister(res);
1928}
1929
1930/**
1931 * devm_clk_register - resource managed clk_register()
1932 * @dev: device that is registering this clock
1933 * @hw: link to hardware-specific clock data
1934 *
1935 * Managed clk_register(). Clocks returned from this function are
1936 * automatically clk_unregister()ed on driver detach. See clk_register() for
1937 * more information.
1938 */
1939struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1940{
1941 struct clk *clk;
1942 int ret;
1943
1944 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1945 if (!clk)
1946 return ERR_PTR(-ENOMEM);
1947
1948 ret = _clk_register(dev, hw, clk);
1949 if (!ret) {
1950 devres_add(dev, clk);
1951 } else {
1952 devres_free(clk);
1953 clk = ERR_PTR(ret);
1954 }
1955
1956 return clk;
1957}
1958EXPORT_SYMBOL_GPL(devm_clk_register);
1959
1960static int devm_clk_match(struct device *dev, void *res, void *data)
1961{
1962 struct clk *c = res;
1963 if (WARN_ON(!c))
1964 return 0;
1965 return c == data;
1966}
1967
1968/**
1969 * devm_clk_unregister - resource managed clk_unregister()
1970 * @clk: clock to unregister
1971 *
1972 * Deallocate a clock allocated with devm_clk_register(). Normally
1973 * this function will not need to be called and the resource management
1974 * code will ensure that the resource is freed.
1975 */
1976void devm_clk_unregister(struct device *dev, struct clk *clk)
1977{
1978 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1979}
1980EXPORT_SYMBOL_GPL(devm_clk_unregister);
1981
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001982/*
1983 * clkdev helpers
1984 */
1985int __clk_get(struct clk *clk)
1986{
1987 if (clk && !try_module_get(clk->owner))
1988 return 0;
1989
1990 return 1;
1991}
1992
1993void __clk_put(struct clk *clk)
1994{
1995 if (WARN_ON_ONCE(IS_ERR(clk)))
1996 return;
1997
1998 if (clk)
1999 module_put(clk->owner);
2000}
2001
Mike Turquetteb24764902012-03-15 23:11:19 -07002002/*** clk rate change notifiers ***/
2003
2004/**
2005 * clk_notifier_register - add a clk rate change notifier
2006 * @clk: struct clk * to watch
2007 * @nb: struct notifier_block * with callback info
2008 *
2009 * Request notification when clk's rate changes. This uses an SRCU
2010 * notifier because we want it to block and notifier unregistrations are
2011 * uncommon. The callbacks associated with the notifier must not
2012 * re-enter into the clk framework by calling any top-level clk APIs;
2013 * this will cause a nested prepare_lock mutex.
2014 *
2015 * Pre-change notifier callbacks will be passed the current, pre-change
2016 * rate of the clk via struct clk_notifier_data.old_rate. The new,
2017 * post-change rate of the clk is passed via struct
2018 * clk_notifier_data.new_rate.
2019 *
2020 * Post-change notifiers will pass the now-current, post-change rate of
2021 * the clk in both struct clk_notifier_data.old_rate and struct
2022 * clk_notifier_data.new_rate.
2023 *
2024 * Abort-change notifiers are effectively the opposite of pre-change
2025 * notifiers: the original pre-change clk rate is passed in via struct
2026 * clk_notifier_data.new_rate and the failed post-change rate is passed
2027 * in via struct clk_notifier_data.old_rate.
2028 *
2029 * clk_notifier_register() must be called from non-atomic context.
2030 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2031 * allocation failure; otherwise, passes along the return value of
2032 * srcu_notifier_chain_register().
2033 */
2034int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2035{
2036 struct clk_notifier *cn;
2037 int ret = -ENOMEM;
2038
2039 if (!clk || !nb)
2040 return -EINVAL;
2041
Mike Turquetteeab89f62013-03-28 13:59:01 -07002042 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002043
2044 /* search the list of notifiers for this clk */
2045 list_for_each_entry(cn, &clk_notifier_list, node)
2046 if (cn->clk == clk)
2047 break;
2048
2049 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2050 if (cn->clk != clk) {
2051 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2052 if (!cn)
2053 goto out;
2054
2055 cn->clk = clk;
2056 srcu_init_notifier_head(&cn->notifier_head);
2057
2058 list_add(&cn->node, &clk_notifier_list);
2059 }
2060
2061 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2062
2063 clk->notifier_count++;
2064
2065out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002066 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002067
2068 return ret;
2069}
2070EXPORT_SYMBOL_GPL(clk_notifier_register);
2071
2072/**
2073 * clk_notifier_unregister - remove a clk rate change notifier
2074 * @clk: struct clk *
2075 * @nb: struct notifier_block * with callback info
2076 *
2077 * Request no further notification for changes to 'clk' and frees memory
2078 * allocated in clk_notifier_register.
2079 *
2080 * Returns -EINVAL if called with null arguments; otherwise, passes
2081 * along the return value of srcu_notifier_chain_unregister().
2082 */
2083int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2084{
2085 struct clk_notifier *cn = NULL;
2086 int ret = -EINVAL;
2087
2088 if (!clk || !nb)
2089 return -EINVAL;
2090
Mike Turquetteeab89f62013-03-28 13:59:01 -07002091 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002092
2093 list_for_each_entry(cn, &clk_notifier_list, node)
2094 if (cn->clk == clk)
2095 break;
2096
2097 if (cn->clk == clk) {
2098 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2099
2100 clk->notifier_count--;
2101
2102 /* XXX the notifier code should handle this better */
2103 if (!cn->notifier_head.head) {
2104 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002105 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002106 kfree(cn);
2107 }
2108
2109 } else {
2110 ret = -ENOENT;
2111 }
2112
Mike Turquetteeab89f62013-03-28 13:59:01 -07002113 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002114
2115 return ret;
2116}
2117EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002118
2119#ifdef CONFIG_OF
2120/**
2121 * struct of_clk_provider - Clock provider registration structure
2122 * @link: Entry in global list of clock providers
2123 * @node: Pointer to device tree node of clock provider
2124 * @get: Get clock callback. Returns NULL or a struct clk for the
2125 * given clock specifier
2126 * @data: context pointer to be passed into @get callback
2127 */
2128struct of_clk_provider {
2129 struct list_head link;
2130
2131 struct device_node *node;
2132 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2133 void *data;
2134};
2135
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302136extern struct of_device_id __clk_of_table[];
2137
2138static const struct of_device_id __clk_of_table_sentinel
2139 __used __section(__clk_of_table_end);
2140
Grant Likely766e6a42012-04-09 14:50:06 -05002141static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002142static DEFINE_MUTEX(of_clk_mutex);
2143
2144/* of_clk_provider list locking helpers */
2145void of_clk_lock(void)
2146{
2147 mutex_lock(&of_clk_mutex);
2148}
2149
2150void of_clk_unlock(void)
2151{
2152 mutex_unlock(&of_clk_mutex);
2153}
Grant Likely766e6a42012-04-09 14:50:06 -05002154
2155struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2156 void *data)
2157{
2158 return data;
2159}
2160EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2161
Shawn Guo494bfec2012-08-22 21:36:27 +08002162struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2163{
2164 struct clk_onecell_data *clk_data = data;
2165 unsigned int idx = clkspec->args[0];
2166
2167 if (idx >= clk_data->clk_num) {
2168 pr_err("%s: invalid clock index %d\n", __func__, idx);
2169 return ERR_PTR(-EINVAL);
2170 }
2171
2172 return clk_data->clks[idx];
2173}
2174EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2175
Grant Likely766e6a42012-04-09 14:50:06 -05002176/**
2177 * of_clk_add_provider() - Register a clock provider for a node
2178 * @np: Device node pointer associated with clock provider
2179 * @clk_src_get: callback for decoding clock
2180 * @data: context pointer for @clk_src_get callback.
2181 */
2182int of_clk_add_provider(struct device_node *np,
2183 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2184 void *data),
2185 void *data)
2186{
2187 struct of_clk_provider *cp;
2188
2189 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2190 if (!cp)
2191 return -ENOMEM;
2192
2193 cp->node = of_node_get(np);
2194 cp->data = data;
2195 cp->get = clk_src_get;
2196
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002197 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002198 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002199 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002200 pr_debug("Added clock from %s\n", np->full_name);
2201
2202 return 0;
2203}
2204EXPORT_SYMBOL_GPL(of_clk_add_provider);
2205
2206/**
2207 * of_clk_del_provider() - Remove a previously registered clock provider
2208 * @np: Device node pointer associated with clock provider
2209 */
2210void of_clk_del_provider(struct device_node *np)
2211{
2212 struct of_clk_provider *cp;
2213
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002214 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002215 list_for_each_entry(cp, &of_clk_providers, link) {
2216 if (cp->node == np) {
2217 list_del(&cp->link);
2218 of_node_put(cp->node);
2219 kfree(cp);
2220 break;
2221 }
2222 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002223 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002224}
2225EXPORT_SYMBOL_GPL(of_clk_del_provider);
2226
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002227struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05002228{
2229 struct of_clk_provider *provider;
2230 struct clk *clk = ERR_PTR(-ENOENT);
2231
2232 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002233 list_for_each_entry(provider, &of_clk_providers, link) {
2234 if (provider->node == clkspec->np)
2235 clk = provider->get(clkspec, provider->data);
2236 if (!IS_ERR(clk))
2237 break;
2238 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002239
2240 return clk;
2241}
2242
2243struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2244{
2245 struct clk *clk;
2246
2247 mutex_lock(&of_clk_mutex);
2248 clk = __of_clk_get_from_provider(clkspec);
2249 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002250
2251 return clk;
2252}
2253
Mike Turquettef6102742013-10-07 23:12:13 -07002254int of_clk_get_parent_count(struct device_node *np)
2255{
2256 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2257}
2258EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2259
Grant Likely766e6a42012-04-09 14:50:06 -05002260const char *of_clk_get_parent_name(struct device_node *np, int index)
2261{
2262 struct of_phandle_args clkspec;
2263 const char *clk_name;
2264 int rc;
2265
2266 if (index < 0)
2267 return NULL;
2268
2269 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2270 &clkspec);
2271 if (rc)
2272 return NULL;
2273
2274 if (of_property_read_string_index(clkspec.np, "clock-output-names",
2275 clkspec.args_count ? clkspec.args[0] : 0,
2276 &clk_name) < 0)
2277 clk_name = clkspec.np->name;
2278
2279 of_node_put(clkspec.np);
2280 return clk_name;
2281}
2282EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2283
2284/**
2285 * of_clk_init() - Scan and init clock providers from the DT
2286 * @matches: array of compatible values and init functions for providers.
2287 *
2288 * This function scans the device tree for matching clock providers and
2289 * calls their initialization functions
2290 */
2291void __init of_clk_init(const struct of_device_id *matches)
2292{
Alex Elder7f7ed582013-08-22 11:31:31 -05002293 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05002294 struct device_node *np;
2295
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302296 if (!matches)
2297 matches = __clk_of_table;
2298
Alex Elder7f7ed582013-08-22 11:31:31 -05002299 for_each_matching_node_and_match(np, matches, &match) {
Grant Likely766e6a42012-04-09 14:50:06 -05002300 of_clk_init_cb_t clk_init_cb = match->data;
2301 clk_init_cb(np);
2302 }
2303}
2304#endif