blob: da7b33e4c5a25bf6d934dfabdd396bd1bf46b9c6 [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
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200348 /**
349 * clk_debug_unregister - remove a clk node from the debugfs clk tree
350 * @clk: the clk being removed from the debugfs clk tree
351 *
352 * Dynamically removes a clk and all it's children clk nodes from the
353 * debugfs clk tree if clk->dentry points to debugfs created by
354 * clk_debug_register in __clk_init.
355 *
356 * Caller must hold prepare_lock.
357 */
358static void clk_debug_unregister(struct clk *clk)
359{
360 debugfs_remove_recursive(clk->dentry);
361}
362
Mike Turquetteb24764902012-03-15 23:11:19 -0700363/**
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200364 * clk_debug_reparent - reparent clk node in the debugfs clk tree
365 * @clk: the clk being reparented
366 * @new_parent: the new clk parent, may be NULL
367 *
368 * Rename clk entry in the debugfs clk tree if debugfs has been
369 * initialized. Otherwise it bails out early since the debugfs clk tree
370 * will be created lazily by clk_debug_init as part of a late_initcall.
371 *
372 * Caller must hold prepare_lock.
373 */
374static void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
375{
376 struct dentry *d;
377 struct dentry *new_parent_d;
378
379 if (!inited)
380 return;
381
382 if (new_parent)
383 new_parent_d = new_parent->dentry;
384 else
385 new_parent_d = orphandir;
386
387 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
388 new_parent_d, clk->name);
389 if (d)
390 clk->dentry = d;
391 else
392 pr_debug("%s: failed to rename debugfs entry for %s\n",
393 __func__, clk->name);
394}
395
396/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700397 * clk_debug_init - lazily create the debugfs clk tree visualization
398 *
399 * clks are often initialized very early during boot before memory can
400 * be dynamically allocated and well before debugfs is setup.
401 * clk_debug_init walks the clk tree hierarchy while holding
402 * prepare_lock and creates the topology as part of a late_initcall,
403 * thus insuring that clks initialized very early will still be
404 * represented in the debugfs clk tree. This function should only be
405 * called once at boot-time, and all other clks added dynamically will
406 * be done so with clk_debug_register.
407 */
408static int __init clk_debug_init(void)
409{
410 struct clk *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530411 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700412
413 rootdir = debugfs_create_dir("clk", NULL);
414
415 if (!rootdir)
416 return -ENOMEM;
417
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530418 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
419 &clk_summary_fops);
420 if (!d)
421 return -ENOMEM;
422
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530423 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
424 &clk_dump_fops);
425 if (!d)
426 return -ENOMEM;
427
Mike Turquetteb24764902012-03-15 23:11:19 -0700428 orphandir = debugfs_create_dir("orphans", rootdir);
429
430 if (!orphandir)
431 return -ENOMEM;
432
Mike Turquetteeab89f62013-03-28 13:59:01 -0700433 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700434
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700436 clk_debug_create_subtree(clk, rootdir);
437
Sasha Levinb67bfe02013-02-27 17:06:00 -0800438 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700439 clk_debug_create_subtree(clk, orphandir);
440
441 inited = 1;
442
Mike Turquetteeab89f62013-03-28 13:59:01 -0700443 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700444
445 return 0;
446}
447late_initcall(clk_debug_init);
448#else
449static inline int clk_debug_register(struct clk *clk) { return 0; }
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200450static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
451{
452}
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200453static inline void clk_debug_unregister(struct clk *clk)
454{
455}
Mike Turquette70d347e2012-03-26 11:53:47 -0700456#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700457
Mike Turquetteb24764902012-03-15 23:11:19 -0700458/* caller must hold prepare_lock */
Ulf Hansson1c155b32013-03-12 20:26:03 +0100459static void clk_unprepare_unused_subtree(struct clk *clk)
460{
461 struct clk *child;
462
463 if (!clk)
464 return;
465
466 hlist_for_each_entry(child, &clk->children, child_node)
467 clk_unprepare_unused_subtree(child);
468
469 if (clk->prepare_count)
470 return;
471
472 if (clk->flags & CLK_IGNORE_UNUSED)
473 return;
474
Ulf Hansson3cc82472013-03-12 20:26:04 +0100475 if (__clk_is_prepared(clk)) {
476 if (clk->ops->unprepare_unused)
477 clk->ops->unprepare_unused(clk->hw);
478 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100479 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100480 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100481}
482
483/* caller must hold prepare_lock */
Mike Turquetteb24764902012-03-15 23:11:19 -0700484static void clk_disable_unused_subtree(struct clk *clk)
485{
486 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700487 unsigned long flags;
488
489 if (!clk)
490 goto out;
491
Sasha Levinb67bfe02013-02-27 17:06:00 -0800492 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700493 clk_disable_unused_subtree(child);
494
Mike Turquetteeab89f62013-03-28 13:59:01 -0700495 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700496
497 if (clk->enable_count)
498 goto unlock_out;
499
500 if (clk->flags & CLK_IGNORE_UNUSED)
501 goto unlock_out;
502
Mike Turquette7c045a52012-12-04 11:00:35 -0800503 /*
504 * some gate clocks have special needs during the disable-unused
505 * sequence. call .disable_unused if available, otherwise fall
506 * back to .disable
507 */
508 if (__clk_is_enabled(clk)) {
509 if (clk->ops->disable_unused)
510 clk->ops->disable_unused(clk->hw);
511 else if (clk->ops->disable)
512 clk->ops->disable(clk->hw);
513 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700514
515unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700516 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700517
518out:
519 return;
520}
521
Olof Johansson1e435252013-04-27 14:10:18 -0700522static bool clk_ignore_unused;
523static int __init clk_ignore_unused_setup(char *__unused)
524{
525 clk_ignore_unused = true;
526 return 1;
527}
528__setup("clk_ignore_unused", clk_ignore_unused_setup);
529
Mike Turquetteb24764902012-03-15 23:11:19 -0700530static int clk_disable_unused(void)
531{
532 struct clk *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700533
Olof Johansson1e435252013-04-27 14:10:18 -0700534 if (clk_ignore_unused) {
535 pr_warn("clk: Not disabling unused clocks\n");
536 return 0;
537 }
538
Mike Turquetteeab89f62013-03-28 13:59:01 -0700539 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700540
Sasha Levinb67bfe02013-02-27 17:06:00 -0800541 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700542 clk_disable_unused_subtree(clk);
543
Sasha Levinb67bfe02013-02-27 17:06:00 -0800544 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700545 clk_disable_unused_subtree(clk);
546
Ulf Hansson1c155b32013-03-12 20:26:03 +0100547 hlist_for_each_entry(clk, &clk_root_list, child_node)
548 clk_unprepare_unused_subtree(clk);
549
550 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
551 clk_unprepare_unused_subtree(clk);
552
Mike Turquetteeab89f62013-03-28 13:59:01 -0700553 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700554
555 return 0;
556}
Saravana Kannand41d5802013-05-09 11:35:01 -0700557late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700558
559/*** helper functions ***/
560
Russ Dill65800b22012-11-26 11:20:09 -0800561const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700562{
563 return !clk ? NULL : clk->name;
564}
Niels de Vos48950842012-12-13 13:12:25 +0100565EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700566
Russ Dill65800b22012-11-26 11:20:09 -0800567struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700568{
569 return !clk ? NULL : clk->hw;
570}
571
Russ Dill65800b22012-11-26 11:20:09 -0800572u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700573{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700574 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700575}
576
Russ Dill65800b22012-11-26 11:20:09 -0800577struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700578{
579 return !clk ? NULL : clk->parent;
580}
581
James Hogan7ef3dcc2013-07-29 12:24:58 +0100582struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
583{
584 if (!clk || index >= clk->num_parents)
585 return NULL;
586 else if (!clk->parents)
587 return __clk_lookup(clk->parent_names[index]);
588 else if (!clk->parents[index])
589 return clk->parents[index] =
590 __clk_lookup(clk->parent_names[index]);
591 else
592 return clk->parents[index];
593}
594
Russ Dill65800b22012-11-26 11:20:09 -0800595unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700596{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700597 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700598}
599
Russ Dill65800b22012-11-26 11:20:09 -0800600unsigned int __clk_get_prepare_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700601{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700602 return !clk ? 0 : clk->prepare_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700603}
604
605unsigned long __clk_get_rate(struct clk *clk)
606{
607 unsigned long ret;
608
609 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530610 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700611 goto out;
612 }
613
614 ret = clk->rate;
615
616 if (clk->flags & CLK_IS_ROOT)
617 goto out;
618
619 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530620 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700621
622out:
623 return ret;
624}
625
Russ Dill65800b22012-11-26 11:20:09 -0800626unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700627{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700628 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700629}
Thierry Redingb05c6832013-09-03 09:43:51 +0200630EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700631
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100632bool __clk_is_prepared(struct clk *clk)
633{
634 int ret;
635
636 if (!clk)
637 return false;
638
639 /*
640 * .is_prepared is optional for clocks that can prepare
641 * fall back to software usage counter if it is missing
642 */
643 if (!clk->ops->is_prepared) {
644 ret = clk->prepare_count ? 1 : 0;
645 goto out;
646 }
647
648 ret = clk->ops->is_prepared(clk->hw);
649out:
650 return !!ret;
651}
652
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700653bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700654{
655 int ret;
656
657 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700658 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700659
660 /*
661 * .is_enabled is only mandatory for clocks that gate
662 * fall back to software usage counter if .is_enabled is missing
663 */
664 if (!clk->ops->is_enabled) {
665 ret = clk->enable_count ? 1 : 0;
666 goto out;
667 }
668
669 ret = clk->ops->is_enabled(clk->hw);
670out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700671 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700672}
673
674static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
675{
676 struct clk *child;
677 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700678
679 if (!strcmp(clk->name, name))
680 return clk;
681
Sasha Levinb67bfe02013-02-27 17:06:00 -0800682 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700683 ret = __clk_lookup_subtree(name, child);
684 if (ret)
685 return ret;
686 }
687
688 return NULL;
689}
690
691struct clk *__clk_lookup(const char *name)
692{
693 struct clk *root_clk;
694 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700695
696 if (!name)
697 return NULL;
698
699 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800700 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700701 ret = __clk_lookup_subtree(name, root_clk);
702 if (ret)
703 return ret;
704 }
705
706 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800707 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700708 ret = __clk_lookup_subtree(name, root_clk);
709 if (ret)
710 return ret;
711 }
712
713 return NULL;
714}
715
James Hogane366fdd2013-07-29 12:25:02 +0100716/*
717 * Helper for finding best parent to provide a given frequency. This can be used
718 * directly as a determine_rate callback (e.g. for a mux), or from a more
719 * complex clock that may combine a mux with other operations.
720 */
721long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
722 unsigned long *best_parent_rate,
723 struct clk **best_parent_p)
724{
725 struct clk *clk = hw->clk, *parent, *best_parent = NULL;
726 int i, num_parents;
727 unsigned long parent_rate, best = 0;
728
729 /* if NO_REPARENT flag set, pass through to current parent */
730 if (clk->flags & CLK_SET_RATE_NO_REPARENT) {
731 parent = clk->parent;
732 if (clk->flags & CLK_SET_RATE_PARENT)
733 best = __clk_round_rate(parent, rate);
734 else if (parent)
735 best = __clk_get_rate(parent);
736 else
737 best = __clk_get_rate(clk);
738 goto out;
739 }
740
741 /* find the parent that can provide the fastest rate <= rate */
742 num_parents = clk->num_parents;
743 for (i = 0; i < num_parents; i++) {
744 parent = clk_get_parent_by_index(clk, i);
745 if (!parent)
746 continue;
747 if (clk->flags & CLK_SET_RATE_PARENT)
748 parent_rate = __clk_round_rate(parent, rate);
749 else
750 parent_rate = __clk_get_rate(parent);
751 if (parent_rate <= rate && parent_rate > best) {
752 best_parent = parent;
753 best = parent_rate;
754 }
755 }
756
757out:
758 if (best_parent)
759 *best_parent_p = best_parent;
760 *best_parent_rate = best;
761
762 return best;
763}
764
Mike Turquetteb24764902012-03-15 23:11:19 -0700765/*** clk api ***/
766
767void __clk_unprepare(struct clk *clk)
768{
769 if (!clk)
770 return;
771
772 if (WARN_ON(clk->prepare_count == 0))
773 return;
774
775 if (--clk->prepare_count > 0)
776 return;
777
778 WARN_ON(clk->enable_count > 0);
779
780 if (clk->ops->unprepare)
781 clk->ops->unprepare(clk->hw);
782
783 __clk_unprepare(clk->parent);
784}
785
786/**
787 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200788 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700789 *
790 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
791 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
792 * if the operation may sleep. One example is a clk which is accessed over
793 * I2c. In the complex case a clk gate operation may require a fast and a slow
794 * part. It is this reason that clk_unprepare and clk_disable are not mutually
795 * exclusive. In fact clk_disable must be called before clk_unprepare.
796 */
797void clk_unprepare(struct clk *clk)
798{
Mike Turquetteeab89f62013-03-28 13:59:01 -0700799 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700800 __clk_unprepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700801 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700802}
803EXPORT_SYMBOL_GPL(clk_unprepare);
804
805int __clk_prepare(struct clk *clk)
806{
807 int ret = 0;
808
809 if (!clk)
810 return 0;
811
812 if (clk->prepare_count == 0) {
813 ret = __clk_prepare(clk->parent);
814 if (ret)
815 return ret;
816
817 if (clk->ops->prepare) {
818 ret = clk->ops->prepare(clk->hw);
819 if (ret) {
820 __clk_unprepare(clk->parent);
821 return ret;
822 }
823 }
824 }
825
826 clk->prepare_count++;
827
828 return 0;
829}
830
831/**
832 * clk_prepare - prepare a clock source
833 * @clk: the clk being prepared
834 *
835 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
836 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
837 * operation may sleep. One example is a clk which is accessed over I2c. In
838 * the complex case a clk ungate operation may require a fast and a slow part.
839 * It is this reason that clk_prepare and clk_enable are not mutually
840 * exclusive. In fact clk_prepare must be called before clk_enable.
841 * Returns 0 on success, -EERROR otherwise.
842 */
843int clk_prepare(struct clk *clk)
844{
845 int ret;
846
Mike Turquetteeab89f62013-03-28 13:59:01 -0700847 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700848 ret = __clk_prepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700849 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700850
851 return ret;
852}
853EXPORT_SYMBOL_GPL(clk_prepare);
854
855static void __clk_disable(struct clk *clk)
856{
857 if (!clk)
858 return;
859
Fengguang Wue47c6a32012-07-30 14:39:54 -0700860 if (WARN_ON(IS_ERR(clk)))
861 return;
862
Mike Turquetteb24764902012-03-15 23:11:19 -0700863 if (WARN_ON(clk->enable_count == 0))
864 return;
865
866 if (--clk->enable_count > 0)
867 return;
868
869 if (clk->ops->disable)
870 clk->ops->disable(clk->hw);
871
872 __clk_disable(clk->parent);
873}
874
875/**
876 * clk_disable - gate a clock
877 * @clk: the clk being gated
878 *
879 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
880 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
881 * clk if the operation is fast and will never sleep. One example is a
882 * SoC-internal clk which is controlled via simple register writes. In the
883 * complex case a clk gate operation may require a fast and a slow part. It is
884 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
885 * In fact clk_disable must be called before clk_unprepare.
886 */
887void clk_disable(struct clk *clk)
888{
889 unsigned long flags;
890
Mike Turquetteeab89f62013-03-28 13:59:01 -0700891 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700892 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700893 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700894}
895EXPORT_SYMBOL_GPL(clk_disable);
896
897static int __clk_enable(struct clk *clk)
898{
899 int ret = 0;
900
901 if (!clk)
902 return 0;
903
904 if (WARN_ON(clk->prepare_count == 0))
905 return -ESHUTDOWN;
906
907 if (clk->enable_count == 0) {
908 ret = __clk_enable(clk->parent);
909
910 if (ret)
911 return ret;
912
913 if (clk->ops->enable) {
914 ret = clk->ops->enable(clk->hw);
915 if (ret) {
916 __clk_disable(clk->parent);
917 return ret;
918 }
919 }
920 }
921
922 clk->enable_count++;
923 return 0;
924}
925
926/**
927 * clk_enable - ungate a clock
928 * @clk: the clk being ungated
929 *
930 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
931 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
932 * if the operation will never sleep. One example is a SoC-internal clk which
933 * is controlled via simple register writes. In the complex case a clk ungate
934 * operation may require a fast and a slow part. It is this reason that
935 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
936 * must be called before clk_enable. Returns 0 on success, -EERROR
937 * otherwise.
938 */
939int clk_enable(struct clk *clk)
940{
941 unsigned long flags;
942 int ret;
943
Mike Turquetteeab89f62013-03-28 13:59:01 -0700944 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700945 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700946 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700947
948 return ret;
949}
950EXPORT_SYMBOL_GPL(clk_enable);
951
952/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700953 * __clk_round_rate - round the given rate for a clk
954 * @clk: round the rate of this clock
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200955 * @rate: the rate which is to be rounded
Mike Turquetteb24764902012-03-15 23:11:19 -0700956 *
957 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
958 */
959unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
960{
Shawn Guo81536e02012-04-12 20:50:17 +0800961 unsigned long parent_rate = 0;
James Hogan71472c02013-07-29 12:25:00 +0100962 struct clk *parent;
Mike Turquetteb24764902012-03-15 23:11:19 -0700963
964 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700965 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700966
James Hogan71472c02013-07-29 12:25:00 +0100967 parent = clk->parent;
968 if (parent)
969 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700970
James Hogan71472c02013-07-29 12:25:00 +0100971 if (clk->ops->determine_rate)
972 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
973 &parent);
974 else if (clk->ops->round_rate)
975 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
976 else if (clk->flags & CLK_SET_RATE_PARENT)
977 return __clk_round_rate(clk->parent, rate);
978 else
979 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700980}
981
982/**
983 * clk_round_rate - round the given rate for a clk
984 * @clk: the clk for which we are rounding a rate
985 * @rate: the rate which is to be rounded
986 *
987 * Takes in a rate as input and rounds it to a rate that the clk can actually
988 * use which is then returned. If clk doesn't support round_rate operation
989 * then the parent rate is returned.
990 */
991long clk_round_rate(struct clk *clk, unsigned long rate)
992{
993 unsigned long ret;
994
Mike Turquetteeab89f62013-03-28 13:59:01 -0700995 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700996 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700997 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700998
999 return ret;
1000}
1001EXPORT_SYMBOL_GPL(clk_round_rate);
1002
1003/**
1004 * __clk_notify - call clk notifier chain
1005 * @clk: struct clk * that is changing rate
1006 * @msg: clk notifier type (see include/linux/clk.h)
1007 * @old_rate: old clk rate
1008 * @new_rate: new clk rate
1009 *
1010 * Triggers a notifier call chain on the clk rate-change notification
1011 * for 'clk'. Passes a pointer to the struct clk and the previous
1012 * and current rates to the notifier callback. Intended to be called by
1013 * internal clock code only. Returns NOTIFY_DONE from the last driver
1014 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1015 * a driver returns that.
1016 */
1017static int __clk_notify(struct clk *clk, unsigned long msg,
1018 unsigned long old_rate, unsigned long new_rate)
1019{
1020 struct clk_notifier *cn;
1021 struct clk_notifier_data cnd;
1022 int ret = NOTIFY_DONE;
1023
1024 cnd.clk = clk;
1025 cnd.old_rate = old_rate;
1026 cnd.new_rate = new_rate;
1027
1028 list_for_each_entry(cn, &clk_notifier_list, node) {
1029 if (cn->clk == clk) {
1030 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1031 &cnd);
1032 break;
1033 }
1034 }
1035
1036 return ret;
1037}
1038
1039/**
1040 * __clk_recalc_rates
1041 * @clk: first clk in the subtree
1042 * @msg: notification type (see include/linux/clk.h)
1043 *
1044 * Walks the subtree of clks starting with clk and recalculates rates as it
1045 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001046 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001047 *
1048 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1049 * if necessary.
1050 *
1051 * Caller must hold prepare_lock.
1052 */
1053static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
1054{
1055 unsigned long old_rate;
1056 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001057 struct clk *child;
1058
1059 old_rate = clk->rate;
1060
1061 if (clk->parent)
1062 parent_rate = clk->parent->rate;
1063
1064 if (clk->ops->recalc_rate)
1065 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1066 else
1067 clk->rate = parent_rate;
1068
1069 /*
1070 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1071 * & ABORT_RATE_CHANGE notifiers
1072 */
1073 if (clk->notifier_count && msg)
1074 __clk_notify(clk, msg, old_rate, clk->rate);
1075
Sasha Levinb67bfe02013-02-27 17:06:00 -08001076 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001077 __clk_recalc_rates(child, msg);
1078}
1079
1080/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001081 * clk_get_rate - return the rate of clk
1082 * @clk: the clk whose rate is being returned
1083 *
1084 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1085 * is set, which means a recalc_rate will be issued.
1086 * If clk is NULL then returns 0.
1087 */
1088unsigned long clk_get_rate(struct clk *clk)
1089{
1090 unsigned long rate;
1091
Mike Turquetteeab89f62013-03-28 13:59:01 -07001092 clk_prepare_lock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001093
1094 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1095 __clk_recalc_rates(clk, 0);
1096
1097 rate = __clk_get_rate(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001098 clk_prepare_unlock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001099
1100 return rate;
1101}
1102EXPORT_SYMBOL_GPL(clk_get_rate);
1103
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001104static int clk_fetch_parent_index(struct clk *clk, struct clk *parent)
James Hogan4935b222013-07-29 12:24:59 +01001105{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001106 int i;
James Hogan4935b222013-07-29 12:24:59 +01001107
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001108 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001109 clk->parents = kcalloc(clk->num_parents,
1110 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001111 if (!clk->parents)
1112 return -ENOMEM;
1113 }
James Hogan4935b222013-07-29 12:24:59 +01001114
1115 /*
1116 * find index of new parent clock using cached parent ptrs,
1117 * or if not yet cached, use string name comparison and cache
1118 * them now to avoid future calls to __clk_lookup.
1119 */
1120 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001121 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001122 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001123
1124 if (clk->parents[i])
1125 continue;
1126
1127 if (!strcmp(clk->parent_names[i], parent->name)) {
1128 clk->parents[i] = __clk_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001129 return i;
James Hogan4935b222013-07-29 12:24:59 +01001130 }
1131 }
1132
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001133 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001134}
1135
1136static void clk_reparent(struct clk *clk, struct clk *new_parent)
1137{
1138 hlist_del(&clk->child_node);
1139
James Hogan903efc52013-08-29 12:10:51 +01001140 if (new_parent) {
1141 /* avoid duplicate POST_RATE_CHANGE notifications */
1142 if (new_parent->new_child == clk)
1143 new_parent->new_child = NULL;
1144
James Hogan4935b222013-07-29 12:24:59 +01001145 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001146 } else {
James Hogan4935b222013-07-29 12:24:59 +01001147 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001148 }
James Hogan4935b222013-07-29 12:24:59 +01001149
1150 clk->parent = new_parent;
1151}
1152
1153static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
1154{
1155 unsigned long flags;
1156 int ret = 0;
1157 struct clk *old_parent = clk->parent;
1158
1159 /*
1160 * Migrate prepare state between parents and prevent race with
1161 * clk_enable().
1162 *
1163 * If the clock is not prepared, then a race with
1164 * clk_enable/disable() is impossible since we already have the
1165 * prepare lock (future calls to clk_enable() need to be preceded by
1166 * a clk_prepare()).
1167 *
1168 * If the clock is prepared, migrate the prepared state to the new
1169 * parent and also protect against a race with clk_enable() by
1170 * forcing the clock and the new parent on. This ensures that all
1171 * future calls to clk_enable() are practically NOPs with respect to
1172 * hardware and software states.
1173 *
1174 * See also: Comment for clk_set_parent() below.
1175 */
1176 if (clk->prepare_count) {
1177 __clk_prepare(parent);
1178 clk_enable(parent);
1179 clk_enable(clk);
1180 }
1181
1182 /* update the clk tree topology */
1183 flags = clk_enable_lock();
1184 clk_reparent(clk, parent);
1185 clk_enable_unlock(flags);
1186
1187 /* change clock input source */
1188 if (parent && clk->ops->set_parent)
1189 ret = clk->ops->set_parent(clk->hw, p_index);
1190
1191 if (ret) {
1192 flags = clk_enable_lock();
1193 clk_reparent(clk, old_parent);
1194 clk_enable_unlock(flags);
1195
1196 if (clk->prepare_count) {
1197 clk_disable(clk);
1198 clk_disable(parent);
1199 __clk_unprepare(parent);
1200 }
1201 return ret;
1202 }
1203
1204 /*
1205 * Finish the migration of prepare state and undo the changes done
1206 * for preventing a race with clk_enable().
1207 */
1208 if (clk->prepare_count) {
1209 clk_disable(clk);
1210 clk_disable(old_parent);
1211 __clk_unprepare(old_parent);
1212 }
1213
1214 /* update debugfs with new clk tree topology */
1215 clk_debug_reparent(clk, parent);
1216 return 0;
1217}
1218
Ulf Hanssona093bde2012-08-31 14:21:28 +02001219/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001220 * __clk_speculate_rates
1221 * @clk: first clk in the subtree
1222 * @parent_rate: the "future" rate of clk's parent
1223 *
1224 * Walks the subtree of clks starting with clk, speculating rates as it
1225 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1226 *
1227 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1228 * pre-rate change notifications and returns early if no clks in the
1229 * subtree have subscribed to the notifications. Note that if a clk does not
1230 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001231 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001232 *
1233 * Caller must hold prepare_lock.
1234 */
1235static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
1236{
Mike Turquetteb24764902012-03-15 23:11:19 -07001237 struct clk *child;
1238 unsigned long new_rate;
1239 int ret = NOTIFY_DONE;
1240
1241 if (clk->ops->recalc_rate)
1242 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1243 else
1244 new_rate = parent_rate;
1245
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001246 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001247 if (clk->notifier_count)
1248 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1249
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001250 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001251 goto out;
1252
Sasha Levinb67bfe02013-02-27 17:06:00 -08001253 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001254 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001255 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001256 break;
1257 }
1258
1259out:
1260 return ret;
1261}
1262
James Hogan71472c02013-07-29 12:25:00 +01001263static void clk_calc_subtree(struct clk *clk, unsigned long new_rate,
1264 struct clk *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001265{
1266 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001267
1268 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001269 clk->new_parent = new_parent;
1270 clk->new_parent_index = p_index;
1271 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1272 clk->new_child = NULL;
1273 if (new_parent && new_parent != clk->parent)
1274 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001275
Sasha Levinb67bfe02013-02-27 17:06:00 -08001276 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001277 if (child->ops->recalc_rate)
1278 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
1279 else
1280 child->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001281 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001282 }
1283}
1284
1285/*
1286 * calculate the new rates returning the topmost clock that has to be
1287 * changed.
1288 */
1289static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
1290{
1291 struct clk *top = clk;
James Hogan71472c02013-07-29 12:25:00 +01001292 struct clk *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001293 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001294 unsigned long new_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001295 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001296
Mike Turquette7452b212012-03-26 14:45:36 -07001297 /* sanity */
1298 if (IS_ERR_OR_NULL(clk))
1299 return NULL;
1300
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001301 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001302 parent = old_parent = clk->parent;
1303 if (parent)
1304 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001305
James Hogan71472c02013-07-29 12:25:00 +01001306 /* find the closest rate and parent clk/rate */
1307 if (clk->ops->determine_rate) {
1308 new_rate = clk->ops->determine_rate(clk->hw, rate,
1309 &best_parent_rate,
1310 &parent);
1311 } else if (clk->ops->round_rate) {
1312 new_rate = clk->ops->round_rate(clk->hw, rate,
1313 &best_parent_rate);
1314 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1315 /* pass-through clock without adjustable parent */
1316 clk->new_rate = clk->rate;
1317 return NULL;
1318 } else {
1319 /* pass-through clock with adjustable parent */
1320 top = clk_calc_new_rates(parent, rate);
1321 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001322 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001323 }
1324
James Hogan71472c02013-07-29 12:25:00 +01001325 /* some clocks must be gated to change parent */
1326 if (parent != old_parent &&
1327 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1328 pr_debug("%s: %s not gated but wants to reparent\n",
1329 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001330 return NULL;
1331 }
1332
James Hogan71472c02013-07-29 12:25:00 +01001333 /* try finding the new parent index */
1334 if (parent) {
1335 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001336 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001337 pr_debug("%s: clk %s can not be parent of clk %s\n",
1338 __func__, parent->name, clk->name);
1339 return NULL;
1340 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001341 }
1342
James Hogan71472c02013-07-29 12:25:00 +01001343 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1344 best_parent_rate != parent->rate)
1345 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001346
1347out:
James Hogan71472c02013-07-29 12:25:00 +01001348 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001349
1350 return top;
1351}
1352
1353/*
1354 * Notify about rate changes in a subtree. Always walk down the whole tree
1355 * so that in case of an error we can walk down the whole tree again and
1356 * abort the change.
1357 */
1358static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
1359{
James Hogan71472c02013-07-29 12:25:00 +01001360 struct clk *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001361 int ret = NOTIFY_DONE;
1362
1363 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301364 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001365
1366 if (clk->notifier_count) {
1367 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001368 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001369 fail_clk = clk;
1370 }
1371
Sasha Levinb67bfe02013-02-27 17:06:00 -08001372 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001373 /* Skip children who will be reparented to another clock */
1374 if (child->new_parent && child->new_parent != clk)
1375 continue;
1376 tmp_clk = clk_propagate_rate_change(child, event);
1377 if (tmp_clk)
1378 fail_clk = tmp_clk;
1379 }
1380
1381 /* handle the new child who might not be in clk->children yet */
1382 if (clk->new_child) {
1383 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1384 if (tmp_clk)
1385 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001386 }
1387
1388 return fail_clk;
1389}
1390
1391/*
1392 * walk down a subtree and set the new rates notifying the rate
1393 * change on the way
1394 */
1395static void clk_change_rate(struct clk *clk)
1396{
1397 struct clk *child;
1398 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001399 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001400
1401 old_rate = clk->rate;
1402
James Hogan71472c02013-07-29 12:25:00 +01001403 /* set parent */
1404 if (clk->new_parent && clk->new_parent != clk->parent)
1405 __clk_set_parent(clk, clk->new_parent, clk->new_parent_index);
1406
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001407 if (clk->parent)
1408 best_parent_rate = clk->parent->rate;
1409
Mike Turquetteb24764902012-03-15 23:11:19 -07001410 if (clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001411 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001412
1413 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001414 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001415 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001416 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001417
1418 if (clk->notifier_count && old_rate != clk->rate)
1419 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1420
James Hogan71472c02013-07-29 12:25:00 +01001421 hlist_for_each_entry(child, &clk->children, child_node) {
1422 /* Skip children who will be reparented to another clock */
1423 if (child->new_parent && child->new_parent != clk)
1424 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001425 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001426 }
1427
1428 /* handle the new child who might not be in clk->children yet */
1429 if (clk->new_child)
1430 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001431}
1432
1433/**
1434 * clk_set_rate - specify a new rate for clk
1435 * @clk: the clk whose rate is being changed
1436 * @rate: the new rate for clk
1437 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001438 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001439 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001440 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1441 * propagate up to clk's parent; whether or not this happens depends on the
1442 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1443 * after calling .round_rate then upstream parent propagation is ignored. If
1444 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001445 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001446 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1447 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001448 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001449 * Rate changes are accomplished via tree traversal that also recalculates the
1450 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001451 *
1452 * Returns 0 on success, -EERROR otherwise.
1453 */
1454int clk_set_rate(struct clk *clk, unsigned long rate)
1455{
1456 struct clk *top, *fail_clk;
1457 int ret = 0;
1458
Mike Turquette89ac8d72013-08-21 23:58:09 -07001459 if (!clk)
1460 return 0;
1461
Mike Turquetteb24764902012-03-15 23:11:19 -07001462 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001463 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001464
1465 /* bail early if nothing to do */
Peter De Schrijver34e452a2013-06-05 18:06:36 +03001466 if (rate == clk_get_rate(clk))
Mike Turquetteb24764902012-03-15 23:11:19 -07001467 goto out;
1468
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001469 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301470 ret = -EBUSY;
1471 goto out;
1472 }
1473
Mike Turquetteb24764902012-03-15 23:11:19 -07001474 /* calculate new rates and get the topmost changed clock */
1475 top = clk_calc_new_rates(clk, rate);
1476 if (!top) {
1477 ret = -EINVAL;
1478 goto out;
1479 }
1480
1481 /* notify that we are about to change rates */
1482 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1483 if (fail_clk) {
1484 pr_warn("%s: failed to set %s rate\n", __func__,
1485 fail_clk->name);
1486 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1487 ret = -EBUSY;
1488 goto out;
1489 }
1490
1491 /* change the rates */
1492 clk_change_rate(top);
1493
Mike Turquetteb24764902012-03-15 23:11:19 -07001494out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001495 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001496
1497 return ret;
1498}
1499EXPORT_SYMBOL_GPL(clk_set_rate);
1500
1501/**
1502 * clk_get_parent - return the parent of a clk
1503 * @clk: the clk whose parent gets returned
1504 *
1505 * Simply returns clk->parent. Returns NULL if clk is NULL.
1506 */
1507struct clk *clk_get_parent(struct clk *clk)
1508{
1509 struct clk *parent;
1510
Mike Turquetteeab89f62013-03-28 13:59:01 -07001511 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001512 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001513 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001514
1515 return parent;
1516}
1517EXPORT_SYMBOL_GPL(clk_get_parent);
1518
1519/*
1520 * .get_parent is mandatory for clocks with multiple possible parents. It is
1521 * optional for single-parent clocks. Always call .get_parent if it is
1522 * available and WARN if it is missing for multi-parent clocks.
1523 *
1524 * For single-parent clocks without .get_parent, first check to see if the
1525 * .parents array exists, and if so use it to avoid an expensive tree
1526 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1527 */
1528static struct clk *__clk_init_parent(struct clk *clk)
1529{
1530 struct clk *ret = NULL;
1531 u8 index;
1532
1533 /* handle the trivial cases */
1534
1535 if (!clk->num_parents)
1536 goto out;
1537
1538 if (clk->num_parents == 1) {
1539 if (IS_ERR_OR_NULL(clk->parent))
1540 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1541 ret = clk->parent;
1542 goto out;
1543 }
1544
1545 if (!clk->ops->get_parent) {
1546 WARN(!clk->ops->get_parent,
1547 "%s: multi-parent clocks must implement .get_parent\n",
1548 __func__);
1549 goto out;
1550 };
1551
1552 /*
1553 * Do our best to cache parent clocks in clk->parents. This prevents
1554 * unnecessary and expensive calls to __clk_lookup. We don't set
1555 * clk->parent here; that is done by the calling function
1556 */
1557
1558 index = clk->ops->get_parent(clk->hw);
1559
1560 if (!clk->parents)
1561 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001562 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001563 GFP_KERNEL);
1564
James Hogan7ef3dcc2013-07-29 12:24:58 +01001565 ret = clk_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001566
1567out:
1568 return ret;
1569}
1570
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001571void __clk_reparent(struct clk *clk, struct clk *new_parent)
1572{
1573 clk_reparent(clk, new_parent);
1574 clk_debug_reparent(clk, new_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001575 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1576}
1577
Mike Turquetteb24764902012-03-15 23:11:19 -07001578/**
1579 * clk_set_parent - switch the parent of a mux clk
1580 * @clk: the mux clk whose input we are switching
1581 * @parent: the new input to clk
1582 *
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001583 * Re-parent clk to use parent as its new input source. If clk is in
1584 * prepared state, the clk will get enabled for the duration of this call. If
1585 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1586 * that, the reparenting is glitchy in hardware, etc), use the
1587 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1588 *
1589 * After successfully changing clk's parent clk_set_parent will update the
1590 * clk topology, sysfs topology and propagate rate recalculation via
1591 * __clk_recalc_rates.
1592 *
1593 * Returns 0 on success, -EERROR otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001594 */
1595int clk_set_parent(struct clk *clk, struct clk *parent)
1596{
1597 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001598 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001599 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001600
Mike Turquette89ac8d72013-08-21 23:58:09 -07001601 if (!clk)
1602 return 0;
1603
1604 if (!clk->ops)
Mike Turquetteb24764902012-03-15 23:11:19 -07001605 return -EINVAL;
1606
Ulf Hansson031dcc92013-04-02 23:09:38 +02001607 /* verify ops for for multi-parent clks */
1608 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07001609 return -ENOSYS;
1610
1611 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001612 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001613
1614 if (clk->parent == parent)
1615 goto out;
1616
Ulf Hansson031dcc92013-04-02 23:09:38 +02001617 /* check that we are allowed to re-parent if the clock is in use */
1618 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1619 ret = -EBUSY;
1620 goto out;
1621 }
1622
1623 /* try finding the new parent index */
1624 if (parent) {
1625 p_index = clk_fetch_parent_index(clk, parent);
1626 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001627 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001628 pr_debug("%s: clk %s can not be parent of clk %s\n",
1629 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001630 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001631 goto out;
1632 }
1633 }
1634
Mike Turquetteb24764902012-03-15 23:11:19 -07001635 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07001636 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001637
1638 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001639 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001640 goto out;
1641
Ulf Hansson031dcc92013-04-02 23:09:38 +02001642 /* do the re-parent */
1643 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001644
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001645 /* propagate rate recalculation accordingly */
1646 if (ret)
Mike Turquetteb24764902012-03-15 23:11:19 -07001647 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001648 else
1649 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001650
1651out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001652 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001653
1654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(clk_set_parent);
1657
1658/**
1659 * __clk_init - initialize the data structures in a struct clk
1660 * @dev: device initializing this clk, placeholder for now
1661 * @clk: clk being initialized
1662 *
1663 * Initializes the lists in struct clk, queries the hardware for the
1664 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001665 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001666int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001667{
Mike Turquetted1302a32012-03-29 14:30:40 -07001668 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001669 struct clk *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001670 struct hlist_node *tmp2;
Mike Turquetteb24764902012-03-15 23:11:19 -07001671
1672 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001673 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001674
Mike Turquetteeab89f62013-03-28 13:59:01 -07001675 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001676
1677 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001678 if (__clk_lookup(clk->name)) {
1679 pr_debug("%s: clk %s already initialized\n",
1680 __func__, clk->name);
1681 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001682 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001683 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001684
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001685 /* check that clk_ops are sane. See Documentation/clk.txt */
1686 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01001687 !((clk->ops->round_rate || clk->ops->determine_rate) &&
1688 clk->ops->recalc_rate)) {
1689 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001690 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001691 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001692 goto out;
1693 }
1694
1695 if (clk->ops->set_parent && !clk->ops->get_parent) {
1696 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1697 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001698 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001699 goto out;
1700 }
1701
Mike Turquetteb24764902012-03-15 23:11:19 -07001702 /* throw a WARN if any entries in parent_names are NULL */
1703 for (i = 0; i < clk->num_parents; i++)
1704 WARN(!clk->parent_names[i],
1705 "%s: invalid NULL in %s's .parent_names\n",
1706 __func__, clk->name);
1707
1708 /*
1709 * Allocate an array of struct clk *'s to avoid unnecessary string
1710 * look-ups of clk's possible parents. This can fail for clocks passed
1711 * in to clk_init during early boot; thus any access to clk->parents[]
1712 * must always check for a NULL pointer and try to populate it if
1713 * necessary.
1714 *
1715 * If clk->parents is not NULL we skip this entire block. This allows
1716 * for clock drivers to statically initialize clk->parents.
1717 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301718 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001719 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
1720 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001721 /*
1722 * __clk_lookup returns NULL for parents that have not been
1723 * clk_init'd; thus any access to clk->parents[] must check
1724 * for a NULL pointer. We can always perform lazy lookups for
1725 * missing parents later on.
1726 */
1727 if (clk->parents)
1728 for (i = 0; i < clk->num_parents; i++)
1729 clk->parents[i] =
1730 __clk_lookup(clk->parent_names[i]);
1731 }
1732
1733 clk->parent = __clk_init_parent(clk);
1734
1735 /*
1736 * Populate clk->parent if parent has already been __clk_init'd. If
1737 * parent has not yet been __clk_init'd then place clk in the orphan
1738 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1739 * clk list.
1740 *
1741 * Every time a new clk is clk_init'd then we walk the list of orphan
1742 * clocks and re-parent any that are children of the clock currently
1743 * being clk_init'd.
1744 */
1745 if (clk->parent)
1746 hlist_add_head(&clk->child_node,
1747 &clk->parent->children);
1748 else if (clk->flags & CLK_IS_ROOT)
1749 hlist_add_head(&clk->child_node, &clk_root_list);
1750 else
1751 hlist_add_head(&clk->child_node, &clk_orphan_list);
1752
1753 /*
1754 * Set clk's rate. The preferred method is to use .recalc_rate. For
1755 * simple clocks and lazy developers the default fallback is to use the
1756 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1757 * then rate is set to zero.
1758 */
1759 if (clk->ops->recalc_rate)
1760 clk->rate = clk->ops->recalc_rate(clk->hw,
1761 __clk_get_rate(clk->parent));
1762 else if (clk->parent)
1763 clk->rate = clk->parent->rate;
1764 else
1765 clk->rate = 0;
1766
1767 /*
1768 * walk the list of orphan clocks and reparent any that are children of
1769 * this clock
1770 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001771 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05001772 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001773 i = orphan->ops->get_parent(orphan->hw);
1774 if (!strcmp(clk->name, orphan->parent_names[i]))
1775 __clk_reparent(orphan, clk);
1776 continue;
1777 }
1778
Mike Turquetteb24764902012-03-15 23:11:19 -07001779 for (i = 0; i < orphan->num_parents; i++)
1780 if (!strcmp(clk->name, orphan->parent_names[i])) {
1781 __clk_reparent(orphan, clk);
1782 break;
1783 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001784 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001785
1786 /*
1787 * optional platform-specific magic
1788 *
1789 * The .init callback is not used by any of the basic clock types, but
1790 * exists for weird hardware that must perform initialization magic.
1791 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001792 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07001793 */
1794 if (clk->ops->init)
1795 clk->ops->init(clk->hw);
1796
1797 clk_debug_register(clk);
1798
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02001799 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07001800out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001801 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001802
Mike Turquetted1302a32012-03-29 14:30:40 -07001803 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001804}
1805
1806/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001807 * __clk_register - register a clock and return a cookie.
1808 *
1809 * Same as clk_register, except that the .clk field inside hw shall point to a
1810 * preallocated (generally statically allocated) struct clk. None of the fields
1811 * of the struct clk need to be initialized.
1812 *
1813 * The data pointed to by .init and .clk field shall NOT be marked as init
1814 * data.
1815 *
1816 * __clk_register is only exposed via clk-private.h and is intended for use with
1817 * very large numbers of clocks that need to be statically initialized. It is
1818 * a layering violation to include clk-private.h from any code which implements
1819 * a clock's .ops; as such any statically initialized clock data MUST be in a
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001820 * separate C file from the logic that implements its operations. Returns 0
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001821 * on success, otherwise an error code.
1822 */
1823struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1824{
1825 int ret;
1826 struct clk *clk;
1827
1828 clk = hw->clk;
1829 clk->name = hw->init->name;
1830 clk->ops = hw->init->ops;
1831 clk->hw = hw;
1832 clk->flags = hw->init->flags;
1833 clk->parent_names = hw->init->parent_names;
1834 clk->num_parents = hw->init->num_parents;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001835 if (dev && dev->driver)
1836 clk->owner = dev->driver->owner;
1837 else
1838 clk->owner = NULL;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001839
1840 ret = __clk_init(dev, clk);
1841 if (ret)
1842 return ERR_PTR(ret);
1843
1844 return clk;
1845}
1846EXPORT_SYMBOL_GPL(__clk_register);
1847
Stephen Boyd46c87732012-09-24 13:38:04 -07001848static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001849{
Mike Turquetted1302a32012-03-29 14:30:40 -07001850 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001851
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001852 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1853 if (!clk->name) {
1854 pr_err("%s: could not allocate clk->name\n", __func__);
1855 ret = -ENOMEM;
1856 goto fail_name;
1857 }
1858 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02001859 if (dev && dev->driver)
1860 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07001861 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001862 clk->flags = hw->init->flags;
1863 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001864 hw->clk = clk;
1865
Mike Turquetted1302a32012-03-29 14:30:40 -07001866 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001867 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
1868 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001869
Mike Turquetted1302a32012-03-29 14:30:40 -07001870 if (!clk->parent_names) {
1871 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1872 ret = -ENOMEM;
1873 goto fail_parent_names;
1874 }
1875
1876
1877 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001878 for (i = 0; i < clk->num_parents; i++) {
1879 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1880 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07001881 if (!clk->parent_names[i]) {
1882 pr_err("%s: could not copy parent_names\n", __func__);
1883 ret = -ENOMEM;
1884 goto fail_parent_names_copy;
1885 }
1886 }
1887
1888 ret = __clk_init(dev, clk);
1889 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07001890 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07001891
1892fail_parent_names_copy:
1893 while (--i >= 0)
1894 kfree(clk->parent_names[i]);
1895 kfree(clk->parent_names);
1896fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001897 kfree(clk->name);
1898fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07001899 return ret;
1900}
1901
1902/**
1903 * clk_register - allocate a new clock, register it and return an opaque cookie
1904 * @dev: device that is registering this clock
1905 * @hw: link to hardware-specific clock data
1906 *
1907 * clk_register is the primary interface for populating the clock tree with new
1908 * clock nodes. It returns a pointer to the newly allocated struct clk which
1909 * cannot be dereferenced by driver code but may be used in conjuction with the
1910 * rest of the clock API. In the event of an error clk_register will return an
1911 * error code; drivers must test for an error code after calling clk_register.
1912 */
1913struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1914{
1915 int ret;
1916 struct clk *clk;
1917
1918 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1919 if (!clk) {
1920 pr_err("%s: could not allocate clk\n", __func__);
1921 ret = -ENOMEM;
1922 goto fail_out;
1923 }
1924
1925 ret = _clk_register(dev, hw, clk);
1926 if (!ret)
1927 return clk;
1928
Mike Turquetted1302a32012-03-29 14:30:40 -07001929 kfree(clk);
1930fail_out:
1931 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001932}
1933EXPORT_SYMBOL_GPL(clk_register);
1934
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02001935/*
1936 * Free memory allocated for a clock.
1937 * Caller must hold prepare_lock.
1938 */
1939static void __clk_release(struct kref *ref)
1940{
1941 struct clk *clk = container_of(ref, struct clk, ref);
1942 int i = clk->num_parents;
1943
1944 kfree(clk->parents);
1945 while (--i >= 0)
1946 kfree(clk->parent_names[i]);
1947
1948 kfree(clk->parent_names);
1949 kfree(clk->name);
1950 kfree(clk);
1951}
1952
1953/*
1954 * Empty clk_ops for unregistered clocks. These are used temporarily
1955 * after clk_unregister() was called on a clock and until last clock
1956 * consumer calls clk_put() and the struct clk object is freed.
1957 */
1958static int clk_nodrv_prepare_enable(struct clk_hw *hw)
1959{
1960 return -ENXIO;
1961}
1962
1963static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
1964{
1965 WARN_ON_ONCE(1);
1966}
1967
1968static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
1969 unsigned long parent_rate)
1970{
1971 return -ENXIO;
1972}
1973
1974static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
1975{
1976 return -ENXIO;
1977}
1978
1979static const struct clk_ops clk_nodrv_ops = {
1980 .enable = clk_nodrv_prepare_enable,
1981 .disable = clk_nodrv_disable_unprepare,
1982 .prepare = clk_nodrv_prepare_enable,
1983 .unprepare = clk_nodrv_disable_unprepare,
1984 .set_rate = clk_nodrv_set_rate,
1985 .set_parent = clk_nodrv_set_parent,
1986};
1987
Mark Brown1df5c932012-04-18 09:07:12 +01001988/**
1989 * clk_unregister - unregister a currently registered clock
1990 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01001991 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02001992void clk_unregister(struct clk *clk)
1993{
1994 unsigned long flags;
1995
1996 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
1997 return;
1998
1999 clk_prepare_lock();
2000
2001 if (clk->ops == &clk_nodrv_ops) {
2002 pr_err("%s: unregistered clock: %s\n", __func__, clk->name);
2003 goto out;
2004 }
2005 /*
2006 * Assign empty clock ops for consumers that might still hold
2007 * a reference to this clock.
2008 */
2009 flags = clk_enable_lock();
2010 clk->ops = &clk_nodrv_ops;
2011 clk_enable_unlock(flags);
2012
2013 if (!hlist_empty(&clk->children)) {
2014 struct clk *child;
2015
2016 /* Reparent all children to the orphan list. */
2017 hlist_for_each_entry(child, &clk->children, child_node)
2018 clk_set_parent(child, NULL);
2019 }
2020
2021 clk_debug_unregister(clk);
2022
2023 hlist_del_init(&clk->child_node);
2024
2025 if (clk->prepare_count)
2026 pr_warn("%s: unregistering prepared clock: %s\n",
2027 __func__, clk->name);
2028
2029 kref_put(&clk->ref, __clk_release);
2030out:
2031 clk_prepare_unlock();
2032}
Mark Brown1df5c932012-04-18 09:07:12 +01002033EXPORT_SYMBOL_GPL(clk_unregister);
2034
Stephen Boyd46c87732012-09-24 13:38:04 -07002035static void devm_clk_release(struct device *dev, void *res)
2036{
2037 clk_unregister(res);
2038}
2039
2040/**
2041 * devm_clk_register - resource managed clk_register()
2042 * @dev: device that is registering this clock
2043 * @hw: link to hardware-specific clock data
2044 *
2045 * Managed clk_register(). Clocks returned from this function are
2046 * automatically clk_unregister()ed on driver detach. See clk_register() for
2047 * more information.
2048 */
2049struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2050{
2051 struct clk *clk;
2052 int ret;
2053
2054 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
2055 if (!clk)
2056 return ERR_PTR(-ENOMEM);
2057
2058 ret = _clk_register(dev, hw, clk);
2059 if (!ret) {
2060 devres_add(dev, clk);
2061 } else {
2062 devres_free(clk);
2063 clk = ERR_PTR(ret);
2064 }
2065
2066 return clk;
2067}
2068EXPORT_SYMBOL_GPL(devm_clk_register);
2069
2070static int devm_clk_match(struct device *dev, void *res, void *data)
2071{
2072 struct clk *c = res;
2073 if (WARN_ON(!c))
2074 return 0;
2075 return c == data;
2076}
2077
2078/**
2079 * devm_clk_unregister - resource managed clk_unregister()
2080 * @clk: clock to unregister
2081 *
2082 * Deallocate a clock allocated with devm_clk_register(). Normally
2083 * this function will not need to be called and the resource management
2084 * code will ensure that the resource is freed.
2085 */
2086void devm_clk_unregister(struct device *dev, struct clk *clk)
2087{
2088 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2089}
2090EXPORT_SYMBOL_GPL(devm_clk_unregister);
2091
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002092/*
2093 * clkdev helpers
2094 */
2095int __clk_get(struct clk *clk)
2096{
2097 if (clk && !try_module_get(clk->owner))
2098 return 0;
2099
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002100 kref_get(&clk->ref);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002101 return 1;
2102}
2103
2104void __clk_put(struct clk *clk)
2105{
2106 if (WARN_ON_ONCE(IS_ERR(clk)))
2107 return;
2108
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002109 clk_prepare_lock();
2110 kref_put(&clk->ref, __clk_release);
2111 clk_prepare_unlock();
2112
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002113 if (clk)
2114 module_put(clk->owner);
2115}
2116
Mike Turquetteb24764902012-03-15 23:11:19 -07002117/*** clk rate change notifiers ***/
2118
2119/**
2120 * clk_notifier_register - add a clk rate change notifier
2121 * @clk: struct clk * to watch
2122 * @nb: struct notifier_block * with callback info
2123 *
2124 * Request notification when clk's rate changes. This uses an SRCU
2125 * notifier because we want it to block and notifier unregistrations are
2126 * uncommon. The callbacks associated with the notifier must not
2127 * re-enter into the clk framework by calling any top-level clk APIs;
2128 * this will cause a nested prepare_lock mutex.
2129 *
2130 * Pre-change notifier callbacks will be passed the current, pre-change
2131 * rate of the clk via struct clk_notifier_data.old_rate. The new,
2132 * post-change rate of the clk is passed via struct
2133 * clk_notifier_data.new_rate.
2134 *
2135 * Post-change notifiers will pass the now-current, post-change rate of
2136 * the clk in both struct clk_notifier_data.old_rate and struct
2137 * clk_notifier_data.new_rate.
2138 *
2139 * Abort-change notifiers are effectively the opposite of pre-change
2140 * notifiers: the original pre-change clk rate is passed in via struct
2141 * clk_notifier_data.new_rate and the failed post-change rate is passed
2142 * in via struct clk_notifier_data.old_rate.
2143 *
2144 * clk_notifier_register() must be called from non-atomic context.
2145 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2146 * allocation failure; otherwise, passes along the return value of
2147 * srcu_notifier_chain_register().
2148 */
2149int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2150{
2151 struct clk_notifier *cn;
2152 int ret = -ENOMEM;
2153
2154 if (!clk || !nb)
2155 return -EINVAL;
2156
Mike Turquetteeab89f62013-03-28 13:59:01 -07002157 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002158
2159 /* search the list of notifiers for this clk */
2160 list_for_each_entry(cn, &clk_notifier_list, node)
2161 if (cn->clk == clk)
2162 break;
2163
2164 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2165 if (cn->clk != clk) {
2166 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2167 if (!cn)
2168 goto out;
2169
2170 cn->clk = clk;
2171 srcu_init_notifier_head(&cn->notifier_head);
2172
2173 list_add(&cn->node, &clk_notifier_list);
2174 }
2175
2176 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2177
2178 clk->notifier_count++;
2179
2180out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002181 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002182
2183 return ret;
2184}
2185EXPORT_SYMBOL_GPL(clk_notifier_register);
2186
2187/**
2188 * clk_notifier_unregister - remove a clk rate change notifier
2189 * @clk: struct clk *
2190 * @nb: struct notifier_block * with callback info
2191 *
2192 * Request no further notification for changes to 'clk' and frees memory
2193 * allocated in clk_notifier_register.
2194 *
2195 * Returns -EINVAL if called with null arguments; otherwise, passes
2196 * along the return value of srcu_notifier_chain_unregister().
2197 */
2198int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2199{
2200 struct clk_notifier *cn = NULL;
2201 int ret = -EINVAL;
2202
2203 if (!clk || !nb)
2204 return -EINVAL;
2205
Mike Turquetteeab89f62013-03-28 13:59:01 -07002206 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002207
2208 list_for_each_entry(cn, &clk_notifier_list, node)
2209 if (cn->clk == clk)
2210 break;
2211
2212 if (cn->clk == clk) {
2213 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2214
2215 clk->notifier_count--;
2216
2217 /* XXX the notifier code should handle this better */
2218 if (!cn->notifier_head.head) {
2219 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002220 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002221 kfree(cn);
2222 }
2223
2224 } else {
2225 ret = -ENOENT;
2226 }
2227
Mike Turquetteeab89f62013-03-28 13:59:01 -07002228 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002229
2230 return ret;
2231}
2232EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002233
2234#ifdef CONFIG_OF
2235/**
2236 * struct of_clk_provider - Clock provider registration structure
2237 * @link: Entry in global list of clock providers
2238 * @node: Pointer to device tree node of clock provider
2239 * @get: Get clock callback. Returns NULL or a struct clk for the
2240 * given clock specifier
2241 * @data: context pointer to be passed into @get callback
2242 */
2243struct of_clk_provider {
2244 struct list_head link;
2245
2246 struct device_node *node;
2247 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2248 void *data;
2249};
2250
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302251extern struct of_device_id __clk_of_table[];
2252
2253static const struct of_device_id __clk_of_table_sentinel
2254 __used __section(__clk_of_table_end);
2255
Grant Likely766e6a42012-04-09 14:50:06 -05002256static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002257static DEFINE_MUTEX(of_clk_mutex);
2258
2259/* of_clk_provider list locking helpers */
2260void of_clk_lock(void)
2261{
2262 mutex_lock(&of_clk_mutex);
2263}
2264
2265void of_clk_unlock(void)
2266{
2267 mutex_unlock(&of_clk_mutex);
2268}
Grant Likely766e6a42012-04-09 14:50:06 -05002269
2270struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2271 void *data)
2272{
2273 return data;
2274}
2275EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2276
Shawn Guo494bfec2012-08-22 21:36:27 +08002277struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2278{
2279 struct clk_onecell_data *clk_data = data;
2280 unsigned int idx = clkspec->args[0];
2281
2282 if (idx >= clk_data->clk_num) {
2283 pr_err("%s: invalid clock index %d\n", __func__, idx);
2284 return ERR_PTR(-EINVAL);
2285 }
2286
2287 return clk_data->clks[idx];
2288}
2289EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2290
Grant Likely766e6a42012-04-09 14:50:06 -05002291/**
2292 * of_clk_add_provider() - Register a clock provider for a node
2293 * @np: Device node pointer associated with clock provider
2294 * @clk_src_get: callback for decoding clock
2295 * @data: context pointer for @clk_src_get callback.
2296 */
2297int of_clk_add_provider(struct device_node *np,
2298 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2299 void *data),
2300 void *data)
2301{
2302 struct of_clk_provider *cp;
2303
2304 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2305 if (!cp)
2306 return -ENOMEM;
2307
2308 cp->node = of_node_get(np);
2309 cp->data = data;
2310 cp->get = clk_src_get;
2311
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002312 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002313 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002314 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002315 pr_debug("Added clock from %s\n", np->full_name);
2316
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(of_clk_add_provider);
2320
2321/**
2322 * of_clk_del_provider() - Remove a previously registered clock provider
2323 * @np: Device node pointer associated with clock provider
2324 */
2325void of_clk_del_provider(struct device_node *np)
2326{
2327 struct of_clk_provider *cp;
2328
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002329 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002330 list_for_each_entry(cp, &of_clk_providers, link) {
2331 if (cp->node == np) {
2332 list_del(&cp->link);
2333 of_node_put(cp->node);
2334 kfree(cp);
2335 break;
2336 }
2337 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002338 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002339}
2340EXPORT_SYMBOL_GPL(of_clk_del_provider);
2341
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002342struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05002343{
2344 struct of_clk_provider *provider;
2345 struct clk *clk = ERR_PTR(-ENOENT);
2346
2347 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002348 list_for_each_entry(provider, &of_clk_providers, link) {
2349 if (provider->node == clkspec->np)
2350 clk = provider->get(clkspec, provider->data);
2351 if (!IS_ERR(clk))
2352 break;
2353 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002354
2355 return clk;
2356}
2357
2358struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2359{
2360 struct clk *clk;
2361
2362 mutex_lock(&of_clk_mutex);
2363 clk = __of_clk_get_from_provider(clkspec);
2364 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002365
2366 return clk;
2367}
2368
Mike Turquettef6102742013-10-07 23:12:13 -07002369int of_clk_get_parent_count(struct device_node *np)
2370{
2371 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2372}
2373EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2374
Grant Likely766e6a42012-04-09 14:50:06 -05002375const char *of_clk_get_parent_name(struct device_node *np, int index)
2376{
2377 struct of_phandle_args clkspec;
2378 const char *clk_name;
2379 int rc;
2380
2381 if (index < 0)
2382 return NULL;
2383
2384 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2385 &clkspec);
2386 if (rc)
2387 return NULL;
2388
2389 if (of_property_read_string_index(clkspec.np, "clock-output-names",
2390 clkspec.args_count ? clkspec.args[0] : 0,
2391 &clk_name) < 0)
2392 clk_name = clkspec.np->name;
2393
2394 of_node_put(clkspec.np);
2395 return clk_name;
2396}
2397EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2398
2399/**
2400 * of_clk_init() - Scan and init clock providers from the DT
2401 * @matches: array of compatible values and init functions for providers.
2402 *
2403 * This function scans the device tree for matching clock providers and
2404 * calls their initialization functions
2405 */
2406void __init of_clk_init(const struct of_device_id *matches)
2407{
Alex Elder7f7ed582013-08-22 11:31:31 -05002408 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05002409 struct device_node *np;
2410
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302411 if (!matches)
2412 matches = __clk_of_table;
2413
Alex Elder7f7ed582013-08-22 11:31:31 -05002414 for_each_matching_node_and_match(np, matches, &match) {
Grant Likely766e6a42012-04-09 14:50:06 -05002415 of_clk_init_cb_t clk_init_cb = match->data;
2416 clk_init_cb(np);
2417 }
2418}
2419#endif