blob: aa8a9d2d0b73efb62ec1a4ecad23de93a8e42a0d [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>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020013#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070014#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/spinlock.h>
17#include <linux/err.h>
18#include <linux/list.h>
19#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050020#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070021#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053022#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070023#include <linux/sched.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Mike Turquetteeab89f62013-03-28 13:59:01 -070040/*** locking ***/
41static void clk_prepare_lock(void)
42{
Mike Turquette533ddeb2013-03-28 13:59:02 -070043 if (!mutex_trylock(&prepare_lock)) {
44 if (prepare_owner == current) {
45 prepare_refcnt++;
46 return;
47 }
48 mutex_lock(&prepare_lock);
49 }
50 WARN_ON_ONCE(prepare_owner != NULL);
51 WARN_ON_ONCE(prepare_refcnt != 0);
52 prepare_owner = current;
53 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070054}
55
56static void clk_prepare_unlock(void)
57{
Mike Turquette533ddeb2013-03-28 13:59:02 -070058 WARN_ON_ONCE(prepare_owner != current);
59 WARN_ON_ONCE(prepare_refcnt == 0);
60
61 if (--prepare_refcnt)
62 return;
63 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -070064 mutex_unlock(&prepare_lock);
65}
66
67static unsigned long clk_enable_lock(void)
68{
69 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -070070
71 if (!spin_trylock_irqsave(&enable_lock, flags)) {
72 if (enable_owner == current) {
73 enable_refcnt++;
74 return flags;
75 }
76 spin_lock_irqsave(&enable_lock, flags);
77 }
78 WARN_ON_ONCE(enable_owner != NULL);
79 WARN_ON_ONCE(enable_refcnt != 0);
80 enable_owner = current;
81 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070082 return flags;
83}
84
85static void clk_enable_unlock(unsigned long flags)
86{
Mike Turquette533ddeb2013-03-28 13:59:02 -070087 WARN_ON_ONCE(enable_owner != current);
88 WARN_ON_ONCE(enable_refcnt == 0);
89
90 if (--enable_refcnt)
91 return;
92 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -070093 spin_unlock_irqrestore(&enable_lock, flags);
94}
95
Mike Turquetteb24764902012-03-15 23:11:19 -070096/*** debugfs support ***/
97
Mike Turquetteea72dc22013-12-18 21:38:52 -080098#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -070099#include <linux/debugfs.h>
100
101static struct dentry *rootdir;
Mike Turquetteb24764902012-03-15 23:11:19 -0700102static int inited = 0;
Stephen Boyd6314b672014-09-04 23:37:49 -0700103static DEFINE_MUTEX(clk_debug_lock);
104static HLIST_HEAD(clk_debug_list);
Mike Turquetteb24764902012-03-15 23:11:19 -0700105
Sachin Kamat6b44c8542014-07-01 11:56:34 +0530106static struct hlist_head *all_lists[] = {
107 &clk_root_list,
108 &clk_orphan_list,
109 NULL,
110};
111
112static struct hlist_head *orphan_list[] = {
113 &clk_orphan_list,
114 NULL,
115};
116
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530117static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
118{
119 if (!c)
120 return;
121
Mike Turquettee59c5372014-02-18 21:21:25 -0800122 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530123 level * 3 + 1, "",
124 30 - level * 3, c->name,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100125 c->enable_count, c->prepare_count, clk_get_rate(c),
Mike Turquettee59c5372014-02-18 21:21:25 -0800126 clk_get_accuracy(c), clk_get_phase(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530127}
128
129static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
130 int level)
131{
132 struct clk *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530133
134 if (!c)
135 return;
136
137 clk_summary_show_one(s, c, level);
138
Sasha Levinb67bfe02013-02-27 17:06:00 -0800139 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530140 clk_summary_show_subtree(s, child, level + 1);
141}
142
143static int clk_summary_show(struct seq_file *s, void *data)
144{
145 struct clk *c;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300146 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530147
Mike Turquettee59c5372014-02-18 21:21:25 -0800148 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
149 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530150
Mike Turquetteeab89f62013-03-28 13:59:01 -0700151 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530152
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300153 for (; *lists; lists++)
154 hlist_for_each_entry(c, *lists, child_node)
155 clk_summary_show_subtree(s, c, 0);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530156
Mike Turquetteeab89f62013-03-28 13:59:01 -0700157 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530158
159 return 0;
160}
161
162
163static int clk_summary_open(struct inode *inode, struct file *file)
164{
165 return single_open(file, clk_summary_show, inode->i_private);
166}
167
168static const struct file_operations clk_summary_fops = {
169 .open = clk_summary_open,
170 .read = seq_read,
171 .llseek = seq_lseek,
172 .release = single_release,
173};
174
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530175static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
176{
177 if (!c)
178 return;
179
180 seq_printf(s, "\"%s\": { ", c->name);
181 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
182 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Peter De Schrijver670decd2013-06-05 18:06:35 +0300183 seq_printf(s, "\"rate\": %lu", clk_get_rate(c));
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100184 seq_printf(s, "\"accuracy\": %lu", clk_get_accuracy(c));
Mike Turquettee59c5372014-02-18 21:21:25 -0800185 seq_printf(s, "\"phase\": %d", clk_get_phase(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530186}
187
188static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
189{
190 struct clk *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530191
192 if (!c)
193 return;
194
195 clk_dump_one(s, c, level);
196
Sasha Levinb67bfe02013-02-27 17:06:00 -0800197 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530198 seq_printf(s, ",");
199 clk_dump_subtree(s, child, level + 1);
200 }
201
202 seq_printf(s, "}");
203}
204
205static int clk_dump(struct seq_file *s, void *data)
206{
207 struct clk *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530208 bool first_node = true;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300209 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530210
211 seq_printf(s, "{");
212
Mike Turquetteeab89f62013-03-28 13:59:01 -0700213 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530214
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300215 for (; *lists; lists++) {
216 hlist_for_each_entry(c, *lists, child_node) {
217 if (!first_node)
218 seq_puts(s, ",");
219 first_node = false;
220 clk_dump_subtree(s, c, 0);
221 }
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530222 }
223
Mike Turquetteeab89f62013-03-28 13:59:01 -0700224 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530225
226 seq_printf(s, "}");
227 return 0;
228}
229
230
231static int clk_dump_open(struct inode *inode, struct file *file)
232{
233 return single_open(file, clk_dump, inode->i_private);
234}
235
236static const struct file_operations clk_dump_fops = {
237 .open = clk_dump_open,
238 .read = seq_read,
239 .llseek = seq_lseek,
240 .release = single_release,
241};
242
Mike Turquetteb24764902012-03-15 23:11:19 -0700243static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
244{
245 struct dentry *d;
246 int ret = -ENOMEM;
247
248 if (!clk || !pdentry) {
249 ret = -EINVAL;
250 goto out;
251 }
252
253 d = debugfs_create_dir(clk->name, pdentry);
254 if (!d)
255 goto out;
256
257 clk->dentry = d;
258
259 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
260 (u32 *)&clk->rate);
261 if (!d)
262 goto err_out;
263
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100264 d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
265 (u32 *)&clk->accuracy);
266 if (!d)
267 goto err_out;
268
Mike Turquettee59c5372014-02-18 21:21:25 -0800269 d = debugfs_create_u32("clk_phase", S_IRUGO, clk->dentry,
270 (u32 *)&clk->phase);
271 if (!d)
272 goto err_out;
273
Mike Turquetteb24764902012-03-15 23:11:19 -0700274 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
275 (u32 *)&clk->flags);
276 if (!d)
277 goto err_out;
278
279 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
280 (u32 *)&clk->prepare_count);
281 if (!d)
282 goto err_out;
283
284 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
285 (u32 *)&clk->enable_count);
286 if (!d)
287 goto err_out;
288
289 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
290 (u32 *)&clk->notifier_count);
291 if (!d)
292 goto err_out;
293
Chris Brandabeab452014-07-03 14:01:29 -0700294 if (clk->ops->debug_init) {
295 ret = clk->ops->debug_init(clk->hw, clk->dentry);
296 if (ret)
Alex Elderc646cbf2014-03-21 06:43:56 -0500297 goto err_out;
Chris Brandabeab452014-07-03 14:01:29 -0700298 }
Alex Elderc646cbf2014-03-21 06:43:56 -0500299
Mike Turquetteb24764902012-03-15 23:11:19 -0700300 ret = 0;
301 goto out;
302
303err_out:
Alex Elderb5f98e62013-11-27 09:39:49 -0600304 debugfs_remove_recursive(clk->dentry);
305 clk->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700306out:
307 return ret;
308}
309
Mike Turquetteb24764902012-03-15 23:11:19 -0700310/**
311 * clk_debug_register - add a clk node to the debugfs clk tree
312 * @clk: the clk being added to the debugfs clk tree
313 *
314 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
315 * initialized. Otherwise it bails out early since the debugfs clk tree
316 * will be created lazily by clk_debug_init as part of a late_initcall.
Mike Turquetteb24764902012-03-15 23:11:19 -0700317 */
318static int clk_debug_register(struct clk *clk)
319{
Mike Turquetteb24764902012-03-15 23:11:19 -0700320 int ret = 0;
321
Stephen Boyd6314b672014-09-04 23:37:49 -0700322 mutex_lock(&clk_debug_lock);
323 hlist_add_head(&clk->debug_node, &clk_debug_list);
324
Mike Turquetteb24764902012-03-15 23:11:19 -0700325 if (!inited)
Stephen Boyd6314b672014-09-04 23:37:49 -0700326 goto unlock;
Mike Turquetteb24764902012-03-15 23:11:19 -0700327
Stephen Boyd6314b672014-09-04 23:37:49 -0700328 ret = clk_debug_create_one(clk, rootdir);
329unlock:
330 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700331
Mike Turquetteb24764902012-03-15 23:11:19 -0700332 return ret;
333}
334
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200335 /**
336 * clk_debug_unregister - remove a clk node from the debugfs clk tree
337 * @clk: the clk being removed from the debugfs clk tree
338 *
339 * Dynamically removes a clk and all it's children clk nodes from the
340 * debugfs clk tree if clk->dentry points to debugfs created by
341 * clk_debug_register in __clk_init.
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200342 */
343static void clk_debug_unregister(struct clk *clk)
344{
Stephen Boyd6314b672014-09-04 23:37:49 -0700345 mutex_lock(&clk_debug_lock);
Stephen Boyd6314b672014-09-04 23:37:49 -0700346 hlist_del_init(&clk->debug_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200347 debugfs_remove_recursive(clk->dentry);
Stephen Boyd6314b672014-09-04 23:37:49 -0700348 clk->dentry = NULL;
Stephen Boyd6314b672014-09-04 23:37:49 -0700349 mutex_unlock(&clk_debug_lock);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200350}
351
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100352struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300353 void *data, const struct file_operations *fops)
354{
355 struct dentry *d = NULL;
356
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100357 if (hw->clk->dentry)
358 d = debugfs_create_file(name, mode, hw->clk->dentry, data, fops);
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300359
360 return d;
361}
362EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
363
Mike Turquetteb24764902012-03-15 23:11:19 -0700364/**
365 * clk_debug_init - lazily create the debugfs clk tree visualization
366 *
367 * clks are often initialized very early during boot before memory can
368 * be dynamically allocated and well before debugfs is setup.
369 * clk_debug_init walks the clk tree hierarchy while holding
370 * prepare_lock and creates the topology as part of a late_initcall,
371 * thus insuring that clks initialized very early will still be
372 * represented in the debugfs clk tree. This function should only be
373 * called once at boot-time, and all other clks added dynamically will
374 * be done so with clk_debug_register.
375 */
376static int __init clk_debug_init(void)
377{
378 struct clk *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530379 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
381 rootdir = debugfs_create_dir("clk", NULL);
382
383 if (!rootdir)
384 return -ENOMEM;
385
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300386 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530387 &clk_summary_fops);
388 if (!d)
389 return -ENOMEM;
390
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300391 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530392 &clk_dump_fops);
393 if (!d)
394 return -ENOMEM;
395
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300396 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
397 &orphan_list, &clk_summary_fops);
398 if (!d)
399 return -ENOMEM;
Mike Turquetteb24764902012-03-15 23:11:19 -0700400
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300401 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
402 &orphan_list, &clk_dump_fops);
403 if (!d)
Mike Turquetteb24764902012-03-15 23:11:19 -0700404 return -ENOMEM;
405
Stephen Boyd6314b672014-09-04 23:37:49 -0700406 mutex_lock(&clk_debug_lock);
407 hlist_for_each_entry(clk, &clk_debug_list, debug_node)
408 clk_debug_create_one(clk, rootdir);
Mike Turquetteb24764902012-03-15 23:11:19 -0700409
410 inited = 1;
Stephen Boyd6314b672014-09-04 23:37:49 -0700411 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700412
413 return 0;
414}
415late_initcall(clk_debug_init);
416#else
417static inline int clk_debug_register(struct clk *clk) { return 0; }
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200418static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
419{
420}
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200421static inline void clk_debug_unregister(struct clk *clk)
422{
423}
Mike Turquette70d347e2012-03-26 11:53:47 -0700424#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700425
Mike Turquetteb24764902012-03-15 23:11:19 -0700426/* caller must hold prepare_lock */
Ulf Hansson1c155b32013-03-12 20:26:03 +0100427static void clk_unprepare_unused_subtree(struct clk *clk)
428{
429 struct clk *child;
430
431 if (!clk)
432 return;
433
434 hlist_for_each_entry(child, &clk->children, child_node)
435 clk_unprepare_unused_subtree(child);
436
437 if (clk->prepare_count)
438 return;
439
440 if (clk->flags & CLK_IGNORE_UNUSED)
441 return;
442
Ulf Hansson3cc82472013-03-12 20:26:04 +0100443 if (__clk_is_prepared(clk)) {
444 if (clk->ops->unprepare_unused)
445 clk->ops->unprepare_unused(clk->hw);
446 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100447 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100448 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100449}
450
451/* caller must hold prepare_lock */
Mike Turquetteb24764902012-03-15 23:11:19 -0700452static void clk_disable_unused_subtree(struct clk *clk)
453{
454 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700455 unsigned long flags;
456
457 if (!clk)
458 goto out;
459
Sasha Levinb67bfe02013-02-27 17:06:00 -0800460 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700461 clk_disable_unused_subtree(child);
462
Mike Turquetteeab89f62013-03-28 13:59:01 -0700463 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700464
465 if (clk->enable_count)
466 goto unlock_out;
467
468 if (clk->flags & CLK_IGNORE_UNUSED)
469 goto unlock_out;
470
Mike Turquette7c045a52012-12-04 11:00:35 -0800471 /*
472 * some gate clocks have special needs during the disable-unused
473 * sequence. call .disable_unused if available, otherwise fall
474 * back to .disable
475 */
476 if (__clk_is_enabled(clk)) {
477 if (clk->ops->disable_unused)
478 clk->ops->disable_unused(clk->hw);
479 else if (clk->ops->disable)
480 clk->ops->disable(clk->hw);
481 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700482
483unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700484 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700485
486out:
487 return;
488}
489
Olof Johansson1e435252013-04-27 14:10:18 -0700490static bool clk_ignore_unused;
491static int __init clk_ignore_unused_setup(char *__unused)
492{
493 clk_ignore_unused = true;
494 return 1;
495}
496__setup("clk_ignore_unused", clk_ignore_unused_setup);
497
Mike Turquetteb24764902012-03-15 23:11:19 -0700498static int clk_disable_unused(void)
499{
500 struct clk *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700501
Olof Johansson1e435252013-04-27 14:10:18 -0700502 if (clk_ignore_unused) {
503 pr_warn("clk: Not disabling unused clocks\n");
504 return 0;
505 }
506
Mike Turquetteeab89f62013-03-28 13:59:01 -0700507 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700508
Sasha Levinb67bfe02013-02-27 17:06:00 -0800509 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700510 clk_disable_unused_subtree(clk);
511
Sasha Levinb67bfe02013-02-27 17:06:00 -0800512 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700513 clk_disable_unused_subtree(clk);
514
Ulf Hansson1c155b32013-03-12 20:26:03 +0100515 hlist_for_each_entry(clk, &clk_root_list, child_node)
516 clk_unprepare_unused_subtree(clk);
517
518 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
519 clk_unprepare_unused_subtree(clk);
520
Mike Turquetteeab89f62013-03-28 13:59:01 -0700521 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700522
523 return 0;
524}
Saravana Kannand41d5802013-05-09 11:35:01 -0700525late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700526
527/*** helper functions ***/
528
Russ Dill65800b22012-11-26 11:20:09 -0800529const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700530{
531 return !clk ? NULL : clk->name;
532}
Niels de Vos48950842012-12-13 13:12:25 +0100533EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700534
Russ Dill65800b22012-11-26 11:20:09 -0800535struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700536{
537 return !clk ? NULL : clk->hw;
538}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800539EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700540
Russ Dill65800b22012-11-26 11:20:09 -0800541u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700542{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700543 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700544}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800545EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700546
Russ Dill65800b22012-11-26 11:20:09 -0800547struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700548{
549 return !clk ? NULL : clk->parent;
550}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800551EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700552
James Hogan7ef3dcc2013-07-29 12:24:58 +0100553struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
554{
555 if (!clk || index >= clk->num_parents)
556 return NULL;
557 else if (!clk->parents)
558 return __clk_lookup(clk->parent_names[index]);
559 else if (!clk->parents[index])
560 return clk->parents[index] =
561 __clk_lookup(clk->parent_names[index]);
562 else
563 return clk->parents[index];
564}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800565EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100566
Russ Dill65800b22012-11-26 11:20:09 -0800567unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700568{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700569 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700570}
571
Mike Turquetteb24764902012-03-15 23:11:19 -0700572unsigned long __clk_get_rate(struct clk *clk)
573{
574 unsigned long ret;
575
576 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530577 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700578 goto out;
579 }
580
581 ret = clk->rate;
582
583 if (clk->flags & CLK_IS_ROOT)
584 goto out;
585
586 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530587 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700588
589out:
590 return ret;
591}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800592EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700593
Tomeu Vizoso920f1c72014-12-02 08:54:20 +0100594static unsigned long __clk_get_accuracy(struct clk *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100595{
596 if (!clk)
597 return 0;
598
599 return clk->accuracy;
600}
601
Russ Dill65800b22012-11-26 11:20:09 -0800602unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700603{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700604 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700605}
Thierry Redingb05c6832013-09-03 09:43:51 +0200606EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700607
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100608bool __clk_is_prepared(struct clk *clk)
609{
610 int ret;
611
612 if (!clk)
613 return false;
614
615 /*
616 * .is_prepared is optional for clocks that can prepare
617 * fall back to software usage counter if it is missing
618 */
619 if (!clk->ops->is_prepared) {
620 ret = clk->prepare_count ? 1 : 0;
621 goto out;
622 }
623
624 ret = clk->ops->is_prepared(clk->hw);
625out:
626 return !!ret;
627}
628
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700629bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700630{
631 int ret;
632
633 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700634 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700635
636 /*
637 * .is_enabled is only mandatory for clocks that gate
638 * fall back to software usage counter if .is_enabled is missing
639 */
640 if (!clk->ops->is_enabled) {
641 ret = clk->enable_count ? 1 : 0;
642 goto out;
643 }
644
645 ret = clk->ops->is_enabled(clk->hw);
646out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700647 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700648}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800649EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700650
651static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
652{
653 struct clk *child;
654 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700655
656 if (!strcmp(clk->name, name))
657 return clk;
658
Sasha Levinb67bfe02013-02-27 17:06:00 -0800659 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700660 ret = __clk_lookup_subtree(name, child);
661 if (ret)
662 return ret;
663 }
664
665 return NULL;
666}
667
668struct clk *__clk_lookup(const char *name)
669{
670 struct clk *root_clk;
671 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700672
673 if (!name)
674 return NULL;
675
676 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800677 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700678 ret = __clk_lookup_subtree(name, root_clk);
679 if (ret)
680 return ret;
681 }
682
683 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800684 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700685 ret = __clk_lookup_subtree(name, root_clk);
686 if (ret)
687 return ret;
688 }
689
690 return NULL;
691}
692
Stephen Boyd15a02c12015-01-19 18:05:28 -0800693static bool mux_is_better_rate(unsigned long rate, unsigned long now,
694 unsigned long best, unsigned long flags)
695{
696 if (flags & CLK_MUX_ROUND_CLOSEST)
697 return abs(now - rate) < abs(best - rate);
698
699 return now <= rate && now > best;
700}
701
702static long
703clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
704 unsigned long *best_parent_rate,
705 struct clk_hw **best_parent_p,
706 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100707{
708 struct clk *clk = hw->clk, *parent, *best_parent = NULL;
709 int i, num_parents;
710 unsigned long parent_rate, best = 0;
711
712 /* if NO_REPARENT flag set, pass through to current parent */
713 if (clk->flags & CLK_SET_RATE_NO_REPARENT) {
714 parent = clk->parent;
715 if (clk->flags & CLK_SET_RATE_PARENT)
716 best = __clk_round_rate(parent, rate);
717 else if (parent)
718 best = __clk_get_rate(parent);
719 else
720 best = __clk_get_rate(clk);
721 goto out;
722 }
723
724 /* find the parent that can provide the fastest rate <= rate */
725 num_parents = clk->num_parents;
726 for (i = 0; i < num_parents; i++) {
727 parent = clk_get_parent_by_index(clk, i);
728 if (!parent)
729 continue;
730 if (clk->flags & CLK_SET_RATE_PARENT)
731 parent_rate = __clk_round_rate(parent, rate);
732 else
733 parent_rate = __clk_get_rate(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800734 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100735 best_parent = parent;
736 best = parent_rate;
737 }
738 }
739
740out:
741 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100742 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100743 *best_parent_rate = best;
744
745 return best;
746}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800747
748/*
749 * Helper for finding best parent to provide a given frequency. This can be used
750 * directly as a determine_rate callback (e.g. for a mux), or from a more
751 * complex clock that may combine a mux with other operations.
752 */
753long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
754 unsigned long *best_parent_rate,
755 struct clk_hw **best_parent_p)
756{
757 return clk_mux_determine_rate_flags(hw, rate, best_parent_rate,
758 best_parent_p, 0);
759}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800760EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100761
Stephen Boyd15a02c12015-01-19 18:05:28 -0800762long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
763 unsigned long *best_parent_rate,
764 struct clk_hw **best_parent_p)
765{
766 return clk_mux_determine_rate_flags(hw, rate, best_parent_rate,
767 best_parent_p,
768 CLK_MUX_ROUND_CLOSEST);
769}
770EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
771
Mike Turquetteb24764902012-03-15 23:11:19 -0700772/*** clk api ***/
773
774void __clk_unprepare(struct clk *clk)
775{
776 if (!clk)
777 return;
778
779 if (WARN_ON(clk->prepare_count == 0))
780 return;
781
782 if (--clk->prepare_count > 0)
783 return;
784
785 WARN_ON(clk->enable_count > 0);
786
787 if (clk->ops->unprepare)
788 clk->ops->unprepare(clk->hw);
789
790 __clk_unprepare(clk->parent);
791}
792
793/**
794 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200795 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700796 *
797 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
798 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
799 * if the operation may sleep. One example is a clk which is accessed over
800 * I2c. In the complex case a clk gate operation may require a fast and a slow
801 * part. It is this reason that clk_unprepare and clk_disable are not mutually
802 * exclusive. In fact clk_disable must be called before clk_unprepare.
803 */
804void clk_unprepare(struct clk *clk)
805{
Stephen Boyd63589e92014-03-26 16:06:37 -0700806 if (IS_ERR_OR_NULL(clk))
807 return;
808
Mike Turquetteeab89f62013-03-28 13:59:01 -0700809 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700810 __clk_unprepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700811 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700812}
813EXPORT_SYMBOL_GPL(clk_unprepare);
814
815int __clk_prepare(struct clk *clk)
816{
817 int ret = 0;
818
819 if (!clk)
820 return 0;
821
822 if (clk->prepare_count == 0) {
823 ret = __clk_prepare(clk->parent);
824 if (ret)
825 return ret;
826
827 if (clk->ops->prepare) {
828 ret = clk->ops->prepare(clk->hw);
829 if (ret) {
830 __clk_unprepare(clk->parent);
831 return ret;
832 }
833 }
834 }
835
836 clk->prepare_count++;
837
838 return 0;
839}
840
841/**
842 * clk_prepare - prepare a clock source
843 * @clk: the clk being prepared
844 *
845 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
846 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
847 * operation may sleep. One example is a clk which is accessed over I2c. In
848 * the complex case a clk ungate operation may require a fast and a slow part.
849 * It is this reason that clk_prepare and clk_enable are not mutually
850 * exclusive. In fact clk_prepare must be called before clk_enable.
851 * Returns 0 on success, -EERROR otherwise.
852 */
853int clk_prepare(struct clk *clk)
854{
855 int ret;
856
Mike Turquetteeab89f62013-03-28 13:59:01 -0700857 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700858 ret = __clk_prepare(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700859 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700860
861 return ret;
862}
863EXPORT_SYMBOL_GPL(clk_prepare);
864
865static void __clk_disable(struct clk *clk)
866{
867 if (!clk)
868 return;
869
870 if (WARN_ON(clk->enable_count == 0))
871 return;
872
873 if (--clk->enable_count > 0)
874 return;
875
876 if (clk->ops->disable)
877 clk->ops->disable(clk->hw);
878
879 __clk_disable(clk->parent);
880}
881
882/**
883 * clk_disable - gate a clock
884 * @clk: the clk being gated
885 *
886 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
887 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
888 * clk if the operation is fast and will never sleep. One example is a
889 * SoC-internal clk which is controlled via simple register writes. In the
890 * complex case a clk gate operation may require a fast and a slow part. It is
891 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
892 * In fact clk_disable must be called before clk_unprepare.
893 */
894void clk_disable(struct clk *clk)
895{
896 unsigned long flags;
897
Stephen Boyd63589e92014-03-26 16:06:37 -0700898 if (IS_ERR_OR_NULL(clk))
899 return;
900
Mike Turquetteeab89f62013-03-28 13:59:01 -0700901 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700902 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700903 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700904}
905EXPORT_SYMBOL_GPL(clk_disable);
906
907static int __clk_enable(struct clk *clk)
908{
909 int ret = 0;
910
911 if (!clk)
912 return 0;
913
914 if (WARN_ON(clk->prepare_count == 0))
915 return -ESHUTDOWN;
916
917 if (clk->enable_count == 0) {
918 ret = __clk_enable(clk->parent);
919
920 if (ret)
921 return ret;
922
923 if (clk->ops->enable) {
924 ret = clk->ops->enable(clk->hw);
925 if (ret) {
926 __clk_disable(clk->parent);
927 return ret;
928 }
929 }
930 }
931
932 clk->enable_count++;
933 return 0;
934}
935
936/**
937 * clk_enable - ungate a clock
938 * @clk: the clk being ungated
939 *
940 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
941 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
942 * if the operation will never sleep. One example is a SoC-internal clk which
943 * is controlled via simple register writes. In the complex case a clk ungate
944 * operation may require a fast and a slow part. It is this reason that
945 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
946 * must be called before clk_enable. Returns 0 on success, -EERROR
947 * otherwise.
948 */
949int clk_enable(struct clk *clk)
950{
951 unsigned long flags;
952 int ret;
953
Mike Turquetteeab89f62013-03-28 13:59:01 -0700954 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700955 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700956 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700957
958 return ret;
959}
960EXPORT_SYMBOL_GPL(clk_enable);
961
962/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700963 * __clk_round_rate - round the given rate for a clk
964 * @clk: round the rate of this clock
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200965 * @rate: the rate which is to be rounded
Mike Turquetteb24764902012-03-15 23:11:19 -0700966 *
967 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
968 */
969unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
970{
Shawn Guo81536e02012-04-12 20:50:17 +0800971 unsigned long parent_rate = 0;
James Hogan71472c02013-07-29 12:25:00 +0100972 struct clk *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100973 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700974
975 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700976 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700977
James Hogan71472c02013-07-29 12:25:00 +0100978 parent = clk->parent;
979 if (parent)
980 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700981
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100982 if (clk->ops->determine_rate) {
983 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +0100984 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100985 &parent_hw);
986 } else if (clk->ops->round_rate)
James Hogan71472c02013-07-29 12:25:00 +0100987 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
988 else if (clk->flags & CLK_SET_RATE_PARENT)
989 return __clk_round_rate(clk->parent, rate);
990 else
991 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700992}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200993EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700994
995/**
996 * clk_round_rate - round the given rate for a clk
997 * @clk: the clk for which we are rounding a rate
998 * @rate: the rate which is to be rounded
999 *
1000 * Takes in a rate as input and rounds it to a rate that the clk can actually
1001 * use which is then returned. If clk doesn't support round_rate operation
1002 * then the parent rate is returned.
1003 */
1004long clk_round_rate(struct clk *clk, unsigned long rate)
1005{
1006 unsigned long ret;
1007
Mike Turquetteeab89f62013-03-28 13:59:01 -07001008 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001009 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001010 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001011
1012 return ret;
1013}
1014EXPORT_SYMBOL_GPL(clk_round_rate);
1015
1016/**
1017 * __clk_notify - call clk notifier chain
1018 * @clk: struct clk * that is changing rate
1019 * @msg: clk notifier type (see include/linux/clk.h)
1020 * @old_rate: old clk rate
1021 * @new_rate: new clk rate
1022 *
1023 * Triggers a notifier call chain on the clk rate-change notification
1024 * for 'clk'. Passes a pointer to the struct clk and the previous
1025 * and current rates to the notifier callback. Intended to be called by
1026 * internal clock code only. Returns NOTIFY_DONE from the last driver
1027 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1028 * a driver returns that.
1029 */
1030static int __clk_notify(struct clk *clk, unsigned long msg,
1031 unsigned long old_rate, unsigned long new_rate)
1032{
1033 struct clk_notifier *cn;
1034 struct clk_notifier_data cnd;
1035 int ret = NOTIFY_DONE;
1036
1037 cnd.clk = clk;
1038 cnd.old_rate = old_rate;
1039 cnd.new_rate = new_rate;
1040
1041 list_for_each_entry(cn, &clk_notifier_list, node) {
1042 if (cn->clk == clk) {
1043 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1044 &cnd);
1045 break;
1046 }
1047 }
1048
1049 return ret;
1050}
1051
1052/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001053 * __clk_recalc_accuracies
1054 * @clk: first clk in the subtree
1055 *
1056 * Walks the subtree of clks starting with clk and recalculates accuracies as
1057 * it goes. Note that if a clk does not implement the .recalc_accuracy
1058 * callback then it is assumed that the clock will take on the accuracy of it's
1059 * parent.
1060 *
1061 * Caller must hold prepare_lock.
1062 */
1063static void __clk_recalc_accuracies(struct clk *clk)
1064{
1065 unsigned long parent_accuracy = 0;
1066 struct clk *child;
1067
1068 if (clk->parent)
1069 parent_accuracy = clk->parent->accuracy;
1070
1071 if (clk->ops->recalc_accuracy)
1072 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1073 parent_accuracy);
1074 else
1075 clk->accuracy = parent_accuracy;
1076
1077 hlist_for_each_entry(child, &clk->children, child_node)
1078 __clk_recalc_accuracies(child);
1079}
1080
1081/**
1082 * clk_get_accuracy - return the accuracy of clk
1083 * @clk: the clk whose accuracy is being returned
1084 *
1085 * Simply returns the cached accuracy of the clk, unless
1086 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1087 * issued.
1088 * If clk is NULL then returns 0.
1089 */
1090long clk_get_accuracy(struct clk *clk)
1091{
1092 unsigned long accuracy;
1093
1094 clk_prepare_lock();
1095 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1096 __clk_recalc_accuracies(clk);
1097
1098 accuracy = __clk_get_accuracy(clk);
1099 clk_prepare_unlock();
1100
1101 return accuracy;
1102}
1103EXPORT_SYMBOL_GPL(clk_get_accuracy);
1104
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001105static unsigned long clk_recalc(struct clk *clk, unsigned long parent_rate)
1106{
1107 if (clk->ops->recalc_rate)
1108 return clk->ops->recalc_rate(clk->hw, parent_rate);
1109 return parent_rate;
1110}
1111
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001112/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001113 * __clk_recalc_rates
1114 * @clk: first clk in the subtree
1115 * @msg: notification type (see include/linux/clk.h)
1116 *
1117 * Walks the subtree of clks starting with clk and recalculates rates as it
1118 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001119 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001120 *
1121 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1122 * if necessary.
1123 *
1124 * Caller must hold prepare_lock.
1125 */
1126static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
1127{
1128 unsigned long old_rate;
1129 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001130 struct clk *child;
1131
1132 old_rate = clk->rate;
1133
1134 if (clk->parent)
1135 parent_rate = clk->parent->rate;
1136
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001137 clk->rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001138
1139 /*
1140 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1141 * & ABORT_RATE_CHANGE notifiers
1142 */
1143 if (clk->notifier_count && msg)
1144 __clk_notify(clk, msg, old_rate, clk->rate);
1145
Sasha Levinb67bfe02013-02-27 17:06:00 -08001146 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001147 __clk_recalc_rates(child, msg);
1148}
1149
1150/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001151 * clk_get_rate - return the rate of clk
1152 * @clk: the clk whose rate is being returned
1153 *
1154 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1155 * is set, which means a recalc_rate will be issued.
1156 * If clk is NULL then returns 0.
1157 */
1158unsigned long clk_get_rate(struct clk *clk)
1159{
1160 unsigned long rate;
1161
Mike Turquetteeab89f62013-03-28 13:59:01 -07001162 clk_prepare_lock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001163
1164 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1165 __clk_recalc_rates(clk, 0);
1166
1167 rate = __clk_get_rate(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001168 clk_prepare_unlock();
Ulf Hanssona093bde2012-08-31 14:21:28 +02001169
1170 return rate;
1171}
1172EXPORT_SYMBOL_GPL(clk_get_rate);
1173
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001174static int clk_fetch_parent_index(struct clk *clk, struct clk *parent)
James Hogan4935b222013-07-29 12:24:59 +01001175{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001176 int i;
James Hogan4935b222013-07-29 12:24:59 +01001177
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001178 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001179 clk->parents = kcalloc(clk->num_parents,
1180 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001181 if (!clk->parents)
1182 return -ENOMEM;
1183 }
James Hogan4935b222013-07-29 12:24:59 +01001184
1185 /*
1186 * find index of new parent clock using cached parent ptrs,
1187 * or if not yet cached, use string name comparison and cache
1188 * them now to avoid future calls to __clk_lookup.
1189 */
1190 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001191 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001192 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001193
1194 if (clk->parents[i])
1195 continue;
1196
1197 if (!strcmp(clk->parent_names[i], parent->name)) {
1198 clk->parents[i] = __clk_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001199 return i;
James Hogan4935b222013-07-29 12:24:59 +01001200 }
1201 }
1202
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001203 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001204}
1205
1206static void clk_reparent(struct clk *clk, struct clk *new_parent)
1207{
1208 hlist_del(&clk->child_node);
1209
James Hogan903efc52013-08-29 12:10:51 +01001210 if (new_parent) {
1211 /* avoid duplicate POST_RATE_CHANGE notifications */
1212 if (new_parent->new_child == clk)
1213 new_parent->new_child = NULL;
1214
James Hogan4935b222013-07-29 12:24:59 +01001215 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001216 } else {
James Hogan4935b222013-07-29 12:24:59 +01001217 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001218 }
James Hogan4935b222013-07-29 12:24:59 +01001219
1220 clk->parent = new_parent;
1221}
1222
Stephen Boyd3fa22522014-01-15 10:47:22 -08001223static struct clk *__clk_set_parent_before(struct clk *clk, struct clk *parent)
James Hogan4935b222013-07-29 12:24:59 +01001224{
1225 unsigned long flags;
James Hogan4935b222013-07-29 12:24:59 +01001226 struct clk *old_parent = clk->parent;
1227
1228 /*
1229 * Migrate prepare state between parents and prevent race with
1230 * clk_enable().
1231 *
1232 * If the clock is not prepared, then a race with
1233 * clk_enable/disable() is impossible since we already have the
1234 * prepare lock (future calls to clk_enable() need to be preceded by
1235 * a clk_prepare()).
1236 *
1237 * If the clock is prepared, migrate the prepared state to the new
1238 * parent and also protect against a race with clk_enable() by
1239 * forcing the clock and the new parent on. This ensures that all
1240 * future calls to clk_enable() are practically NOPs with respect to
1241 * hardware and software states.
1242 *
1243 * See also: Comment for clk_set_parent() below.
1244 */
1245 if (clk->prepare_count) {
1246 __clk_prepare(parent);
1247 clk_enable(parent);
1248 clk_enable(clk);
1249 }
1250
1251 /* update the clk tree topology */
1252 flags = clk_enable_lock();
1253 clk_reparent(clk, parent);
1254 clk_enable_unlock(flags);
1255
Stephen Boyd3fa22522014-01-15 10:47:22 -08001256 return old_parent;
1257}
1258
1259static void __clk_set_parent_after(struct clk *clk, struct clk *parent,
1260 struct clk *old_parent)
1261{
1262 /*
1263 * Finish the migration of prepare state and undo the changes done
1264 * for preventing a race with clk_enable().
1265 */
1266 if (clk->prepare_count) {
1267 clk_disable(clk);
1268 clk_disable(old_parent);
1269 __clk_unprepare(old_parent);
1270 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001271}
1272
1273static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
1274{
1275 unsigned long flags;
1276 int ret = 0;
1277 struct clk *old_parent;
1278
1279 old_parent = __clk_set_parent_before(clk, parent);
1280
James Hogan4935b222013-07-29 12:24:59 +01001281 /* change clock input source */
1282 if (parent && clk->ops->set_parent)
1283 ret = clk->ops->set_parent(clk->hw, p_index);
1284
1285 if (ret) {
1286 flags = clk_enable_lock();
1287 clk_reparent(clk, old_parent);
1288 clk_enable_unlock(flags);
1289
1290 if (clk->prepare_count) {
1291 clk_disable(clk);
1292 clk_disable(parent);
1293 __clk_unprepare(parent);
1294 }
1295 return ret;
1296 }
1297
Stephen Boyd3fa22522014-01-15 10:47:22 -08001298 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001299
James Hogan4935b222013-07-29 12:24:59 +01001300 return 0;
1301}
1302
Ulf Hanssona093bde2012-08-31 14:21:28 +02001303/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001304 * __clk_speculate_rates
1305 * @clk: first clk in the subtree
1306 * @parent_rate: the "future" rate of clk's parent
1307 *
1308 * Walks the subtree of clks starting with clk, speculating rates as it
1309 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1310 *
1311 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1312 * pre-rate change notifications and returns early if no clks in the
1313 * subtree have subscribed to the notifications. Note that if a clk does not
1314 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001315 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001316 *
1317 * Caller must hold prepare_lock.
1318 */
1319static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
1320{
Mike Turquetteb24764902012-03-15 23:11:19 -07001321 struct clk *child;
1322 unsigned long new_rate;
1323 int ret = NOTIFY_DONE;
1324
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001325 new_rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001326
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001327 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001328 if (clk->notifier_count)
1329 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1330
Mike Turquette86bcfa22014-02-24 16:08:41 -08001331 if (ret & NOTIFY_STOP_MASK) {
1332 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1333 __func__, clk->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001334 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001335 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001336
Sasha Levinb67bfe02013-02-27 17:06:00 -08001337 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001338 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001339 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001340 break;
1341 }
1342
1343out:
1344 return ret;
1345}
1346
James Hogan71472c02013-07-29 12:25:00 +01001347static void clk_calc_subtree(struct clk *clk, unsigned long new_rate,
1348 struct clk *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001349{
1350 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001351
1352 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001353 clk->new_parent = new_parent;
1354 clk->new_parent_index = p_index;
1355 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1356 clk->new_child = NULL;
1357 if (new_parent && new_parent != clk->parent)
1358 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001359
Sasha Levinb67bfe02013-02-27 17:06:00 -08001360 hlist_for_each_entry(child, &clk->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001361 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001362 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001363 }
1364}
1365
1366/*
1367 * calculate the new rates returning the topmost clock that has to be
1368 * changed.
1369 */
1370static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
1371{
1372 struct clk *top = clk;
James Hogan71472c02013-07-29 12:25:00 +01001373 struct clk *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001374 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001375 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001376 unsigned long new_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001377 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001378
Mike Turquette7452b212012-03-26 14:45:36 -07001379 /* sanity */
1380 if (IS_ERR_OR_NULL(clk))
1381 return NULL;
1382
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001383 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001384 parent = old_parent = clk->parent;
1385 if (parent)
1386 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001387
James Hogan71472c02013-07-29 12:25:00 +01001388 /* find the closest rate and parent clk/rate */
1389 if (clk->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001390 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001391 new_rate = clk->ops->determine_rate(clk->hw, rate,
1392 &best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001393 &parent_hw);
Stanimir Varbanovc7662fc2015-01-05 18:04:23 +02001394 parent = parent_hw ? parent_hw->clk : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001395 } else if (clk->ops->round_rate) {
1396 new_rate = clk->ops->round_rate(clk->hw, rate,
1397 &best_parent_rate);
1398 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1399 /* pass-through clock without adjustable parent */
1400 clk->new_rate = clk->rate;
1401 return NULL;
1402 } else {
1403 /* pass-through clock with adjustable parent */
1404 top = clk_calc_new_rates(parent, rate);
1405 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001406 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001407 }
1408
James Hogan71472c02013-07-29 12:25:00 +01001409 /* some clocks must be gated to change parent */
1410 if (parent != old_parent &&
1411 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1412 pr_debug("%s: %s not gated but wants to reparent\n",
1413 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001414 return NULL;
1415 }
1416
James Hogan71472c02013-07-29 12:25:00 +01001417 /* try finding the new parent index */
Stephen Boyd4526e7b2014-12-22 11:26:42 -08001418 if (parent && clk->num_parents > 1) {
James Hogan71472c02013-07-29 12:25:00 +01001419 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001420 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001421 pr_debug("%s: clk %s can not be parent of clk %s\n",
1422 __func__, parent->name, clk->name);
1423 return NULL;
1424 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001425 }
1426
James Hogan71472c02013-07-29 12:25:00 +01001427 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1428 best_parent_rate != parent->rate)
1429 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001430
1431out:
James Hogan71472c02013-07-29 12:25:00 +01001432 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001433
1434 return top;
1435}
1436
1437/*
1438 * Notify about rate changes in a subtree. Always walk down the whole tree
1439 * so that in case of an error we can walk down the whole tree again and
1440 * abort the change.
1441 */
1442static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
1443{
James Hogan71472c02013-07-29 12:25:00 +01001444 struct clk *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001445 int ret = NOTIFY_DONE;
1446
1447 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301448 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001449
1450 if (clk->notifier_count) {
1451 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001452 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001453 fail_clk = clk;
1454 }
1455
Sasha Levinb67bfe02013-02-27 17:06:00 -08001456 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001457 /* Skip children who will be reparented to another clock */
1458 if (child->new_parent && child->new_parent != clk)
1459 continue;
1460 tmp_clk = clk_propagate_rate_change(child, event);
1461 if (tmp_clk)
1462 fail_clk = tmp_clk;
1463 }
1464
1465 /* handle the new child who might not be in clk->children yet */
1466 if (clk->new_child) {
1467 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1468 if (tmp_clk)
1469 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001470 }
1471
1472 return fail_clk;
1473}
1474
1475/*
1476 * walk down a subtree and set the new rates notifying the rate
1477 * change on the way
1478 */
1479static void clk_change_rate(struct clk *clk)
1480{
1481 struct clk *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001482 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001483 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001484 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001485 bool skip_set_rate = false;
1486 struct clk *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001487
1488 old_rate = clk->rate;
1489
Stephen Boyd3fa22522014-01-15 10:47:22 -08001490 if (clk->new_parent)
1491 best_parent_rate = clk->new_parent->rate;
1492 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001493 best_parent_rate = clk->parent->rate;
1494
Stephen Boyd3fa22522014-01-15 10:47:22 -08001495 if (clk->new_parent && clk->new_parent != clk->parent) {
1496 old_parent = __clk_set_parent_before(clk, clk->new_parent);
1497
1498 if (clk->ops->set_rate_and_parent) {
1499 skip_set_rate = true;
1500 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1501 best_parent_rate,
1502 clk->new_parent_index);
1503 } else if (clk->ops->set_parent) {
1504 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1505 }
1506
1507 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1508 }
1509
1510 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001511 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001512
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001513 clk->rate = clk_recalc(clk, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001514
1515 if (clk->notifier_count && old_rate != clk->rate)
1516 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1517
Tero Kristo067bb172014-08-21 16:47:45 +03001518 /*
1519 * Use safe iteration, as change_rate can actually swap parents
1520 * for certain clock types.
1521 */
1522 hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001523 /* Skip children who will be reparented to another clock */
1524 if (child->new_parent && child->new_parent != clk)
1525 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001526 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001527 }
1528
1529 /* handle the new child who might not be in clk->children yet */
1530 if (clk->new_child)
1531 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001532}
1533
1534/**
1535 * clk_set_rate - specify a new rate for clk
1536 * @clk: the clk whose rate is being changed
1537 * @rate: the new rate for clk
1538 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001539 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001540 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001541 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1542 * propagate up to clk's parent; whether or not this happens depends on the
1543 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1544 * after calling .round_rate then upstream parent propagation is ignored. If
1545 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001546 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001547 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1548 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001549 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001550 * Rate changes are accomplished via tree traversal that also recalculates the
1551 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001552 *
1553 * Returns 0 on success, -EERROR otherwise.
1554 */
1555int clk_set_rate(struct clk *clk, unsigned long rate)
1556{
1557 struct clk *top, *fail_clk;
1558 int ret = 0;
1559
Mike Turquette89ac8d72013-08-21 23:58:09 -07001560 if (!clk)
1561 return 0;
1562
Mike Turquetteb24764902012-03-15 23:11:19 -07001563 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001564 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001565
1566 /* bail early if nothing to do */
Peter De Schrijver34e452a2013-06-05 18:06:36 +03001567 if (rate == clk_get_rate(clk))
Mike Turquetteb24764902012-03-15 23:11:19 -07001568 goto out;
1569
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001570 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301571 ret = -EBUSY;
1572 goto out;
1573 }
1574
Mike Turquetteb24764902012-03-15 23:11:19 -07001575 /* calculate new rates and get the topmost changed clock */
1576 top = clk_calc_new_rates(clk, rate);
1577 if (!top) {
1578 ret = -EINVAL;
1579 goto out;
1580 }
1581
1582 /* notify that we are about to change rates */
1583 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1584 if (fail_clk) {
Sascha Hauerf7363862014-01-16 16:12:55 +01001585 pr_debug("%s: failed to set %s rate\n", __func__,
Mike Turquetteb24764902012-03-15 23:11:19 -07001586 fail_clk->name);
1587 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1588 ret = -EBUSY;
1589 goto out;
1590 }
1591
1592 /* change the rates */
1593 clk_change_rate(top);
1594
Mike Turquetteb24764902012-03-15 23:11:19 -07001595out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001596 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001597
1598 return ret;
1599}
1600EXPORT_SYMBOL_GPL(clk_set_rate);
1601
1602/**
1603 * clk_get_parent - return the parent of a clk
1604 * @clk: the clk whose parent gets returned
1605 *
1606 * Simply returns clk->parent. Returns NULL if clk is NULL.
1607 */
1608struct clk *clk_get_parent(struct clk *clk)
1609{
1610 struct clk *parent;
1611
Mike Turquetteeab89f62013-03-28 13:59:01 -07001612 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001613 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001614 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001615
1616 return parent;
1617}
1618EXPORT_SYMBOL_GPL(clk_get_parent);
1619
1620/*
1621 * .get_parent is mandatory for clocks with multiple possible parents. It is
1622 * optional for single-parent clocks. Always call .get_parent if it is
1623 * available and WARN if it is missing for multi-parent clocks.
1624 *
1625 * For single-parent clocks without .get_parent, first check to see if the
1626 * .parents array exists, and if so use it to avoid an expensive tree
1627 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1628 */
1629static struct clk *__clk_init_parent(struct clk *clk)
1630{
1631 struct clk *ret = NULL;
1632 u8 index;
1633
1634 /* handle the trivial cases */
1635
1636 if (!clk->num_parents)
1637 goto out;
1638
1639 if (clk->num_parents == 1) {
1640 if (IS_ERR_OR_NULL(clk->parent))
Zhen Lei40ba3f02014-11-14 10:10:40 +08001641 clk->parent = __clk_lookup(clk->parent_names[0]);
Mike Turquetteb24764902012-03-15 23:11:19 -07001642 ret = clk->parent;
1643 goto out;
1644 }
1645
1646 if (!clk->ops->get_parent) {
1647 WARN(!clk->ops->get_parent,
1648 "%s: multi-parent clocks must implement .get_parent\n",
1649 __func__);
1650 goto out;
1651 };
1652
1653 /*
1654 * Do our best to cache parent clocks in clk->parents. This prevents
1655 * unnecessary and expensive calls to __clk_lookup. We don't set
1656 * clk->parent here; that is done by the calling function
1657 */
1658
1659 index = clk->ops->get_parent(clk->hw);
1660
1661 if (!clk->parents)
1662 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001663 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001664 GFP_KERNEL);
1665
James Hogan7ef3dcc2013-07-29 12:24:58 +01001666 ret = clk_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001667
1668out:
1669 return ret;
1670}
1671
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001672void __clk_reparent(struct clk *clk, struct clk *new_parent)
1673{
1674 clk_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001675 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001676 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1677}
1678
Mike Turquetteb24764902012-03-15 23:11:19 -07001679/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001680 * clk_has_parent - check if a clock is a possible parent for another
1681 * @clk: clock source
1682 * @parent: parent clock source
1683 *
1684 * This function can be used in drivers that need to check that a clock can be
1685 * the parent of another without actually changing the parent.
1686 *
1687 * Returns true if @parent is a possible parent for @clk, false otherwise.
1688 */
1689bool clk_has_parent(struct clk *clk, struct clk *parent)
1690{
1691 unsigned int i;
1692
1693 /* NULL clocks should be nops, so return success if either is NULL. */
1694 if (!clk || !parent)
1695 return true;
1696
1697 /* Optimize for the case where the parent is already the parent. */
1698 if (clk->parent == parent)
1699 return true;
1700
1701 for (i = 0; i < clk->num_parents; i++)
1702 if (strcmp(clk->parent_names[i], parent->name) == 0)
1703 return true;
1704
1705 return false;
1706}
1707EXPORT_SYMBOL_GPL(clk_has_parent);
1708
1709/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001710 * clk_set_parent - switch the parent of a mux clk
1711 * @clk: the mux clk whose input we are switching
1712 * @parent: the new input to clk
1713 *
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001714 * Re-parent clk to use parent as its new input source. If clk is in
1715 * prepared state, the clk will get enabled for the duration of this call. If
1716 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1717 * that, the reparenting is glitchy in hardware, etc), use the
1718 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1719 *
1720 * After successfully changing clk's parent clk_set_parent will update the
1721 * clk topology, sysfs topology and propagate rate recalculation via
1722 * __clk_recalc_rates.
1723 *
1724 * Returns 0 on success, -EERROR otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001725 */
1726int clk_set_parent(struct clk *clk, struct clk *parent)
1727{
1728 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001729 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001730 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001731
Mike Turquette89ac8d72013-08-21 23:58:09 -07001732 if (!clk)
1733 return 0;
1734
Ulf Hansson031dcc92013-04-02 23:09:38 +02001735 /* verify ops for for multi-parent clks */
1736 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07001737 return -ENOSYS;
1738
1739 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001740 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001741
1742 if (clk->parent == parent)
1743 goto out;
1744
Ulf Hansson031dcc92013-04-02 23:09:38 +02001745 /* check that we are allowed to re-parent if the clock is in use */
1746 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1747 ret = -EBUSY;
1748 goto out;
1749 }
1750
1751 /* try finding the new parent index */
1752 if (parent) {
1753 p_index = clk_fetch_parent_index(clk, parent);
1754 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001755 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001756 pr_debug("%s: clk %s can not be parent of clk %s\n",
1757 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001758 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001759 goto out;
1760 }
1761 }
1762
Mike Turquetteb24764902012-03-15 23:11:19 -07001763 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07001764 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001765
1766 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001767 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001768 goto out;
1769
Ulf Hansson031dcc92013-04-02 23:09:38 +02001770 /* do the re-parent */
1771 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001772
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001773 /* propagate rate an accuracy recalculation accordingly */
1774 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001775 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001776 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001777 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001778 __clk_recalc_accuracies(clk);
1779 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001780
1781out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001782 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001783
1784 return ret;
1785}
1786EXPORT_SYMBOL_GPL(clk_set_parent);
1787
1788/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001789 * clk_set_phase - adjust the phase shift of a clock signal
1790 * @clk: clock signal source
1791 * @degrees: number of degrees the signal is shifted
1792 *
1793 * Shifts the phase of a clock signal by the specified
1794 * degrees. Returns 0 on success, -EERROR otherwise.
1795 *
1796 * This function makes no distinction about the input or reference
1797 * signal that we adjust the clock signal phase against. For example
1798 * phase locked-loop clock signal generators we may shift phase with
1799 * respect to feedback clock signal input, but for other cases the
1800 * clock phase may be shifted with respect to some other, unspecified
1801 * signal.
1802 *
1803 * Additionally the concept of phase shift does not propagate through
1804 * the clock tree hierarchy, which sets it apart from clock rates and
1805 * clock accuracy. A parent clock phase attribute does not have an
1806 * impact on the phase attribute of a child clock.
1807 */
1808int clk_set_phase(struct clk *clk, int degrees)
1809{
1810 int ret = 0;
1811
1812 if (!clk)
1813 goto out;
1814
1815 /* sanity check degrees */
1816 degrees %= 360;
1817 if (degrees < 0)
1818 degrees += 360;
1819
1820 clk_prepare_lock();
1821
1822 if (!clk->ops->set_phase)
1823 goto out_unlock;
1824
1825 ret = clk->ops->set_phase(clk->hw, degrees);
1826
1827 if (!ret)
1828 clk->phase = degrees;
1829
1830out_unlock:
1831 clk_prepare_unlock();
1832
1833out:
1834 return ret;
1835}
Maxime Ripard9767b042015-01-20 22:23:43 +01001836EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001837
1838/**
1839 * clk_get_phase - return the phase shift of a clock signal
1840 * @clk: clock signal source
1841 *
1842 * Returns the phase shift of a clock node in degrees, otherwise returns
1843 * -EERROR.
1844 */
1845int clk_get_phase(struct clk *clk)
1846{
1847 int ret = 0;
1848
1849 if (!clk)
1850 goto out;
1851
1852 clk_prepare_lock();
1853 ret = clk->phase;
1854 clk_prepare_unlock();
1855
1856out:
1857 return ret;
1858}
Maxime Ripard9767b042015-01-20 22:23:43 +01001859EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001860
1861/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001862 * __clk_init - initialize the data structures in a struct clk
1863 * @dev: device initializing this clk, placeholder for now
1864 * @clk: clk being initialized
1865 *
1866 * Initializes the lists in struct clk, queries the hardware for the
1867 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001868 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001869int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001870{
Mike Turquetted1302a32012-03-29 14:30:40 -07001871 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001872 struct clk *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001873 struct hlist_node *tmp2;
Mike Turquetteb24764902012-03-15 23:11:19 -07001874
1875 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001876 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001877
Mike Turquetteeab89f62013-03-28 13:59:01 -07001878 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001879
1880 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001881 if (__clk_lookup(clk->name)) {
1882 pr_debug("%s: clk %s already initialized\n",
1883 __func__, clk->name);
1884 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001885 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001886 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001887
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001888 /* check that clk_ops are sane. See Documentation/clk.txt */
1889 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01001890 !((clk->ops->round_rate || clk->ops->determine_rate) &&
1891 clk->ops->recalc_rate)) {
1892 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001893 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001894 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001895 goto out;
1896 }
1897
1898 if (clk->ops->set_parent && !clk->ops->get_parent) {
1899 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1900 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001901 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001902 goto out;
1903 }
1904
Stephen Boyd3fa22522014-01-15 10:47:22 -08001905 if (clk->ops->set_rate_and_parent &&
1906 !(clk->ops->set_parent && clk->ops->set_rate)) {
1907 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
1908 __func__, clk->name);
1909 ret = -EINVAL;
1910 goto out;
1911 }
1912
Mike Turquetteb24764902012-03-15 23:11:19 -07001913 /* throw a WARN if any entries in parent_names are NULL */
1914 for (i = 0; i < clk->num_parents; i++)
1915 WARN(!clk->parent_names[i],
1916 "%s: invalid NULL in %s's .parent_names\n",
1917 __func__, clk->name);
1918
1919 /*
1920 * Allocate an array of struct clk *'s to avoid unnecessary string
1921 * look-ups of clk's possible parents. This can fail for clocks passed
1922 * in to clk_init during early boot; thus any access to clk->parents[]
1923 * must always check for a NULL pointer and try to populate it if
1924 * necessary.
1925 *
1926 * If clk->parents is not NULL we skip this entire block. This allows
1927 * for clock drivers to statically initialize clk->parents.
1928 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301929 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001930 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
1931 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001932 /*
1933 * __clk_lookup returns NULL for parents that have not been
1934 * clk_init'd; thus any access to clk->parents[] must check
1935 * for a NULL pointer. We can always perform lazy lookups for
1936 * missing parents later on.
1937 */
1938 if (clk->parents)
1939 for (i = 0; i < clk->num_parents; i++)
1940 clk->parents[i] =
1941 __clk_lookup(clk->parent_names[i]);
1942 }
1943
1944 clk->parent = __clk_init_parent(clk);
1945
1946 /*
1947 * Populate clk->parent if parent has already been __clk_init'd. If
1948 * parent has not yet been __clk_init'd then place clk in the orphan
1949 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1950 * clk list.
1951 *
1952 * Every time a new clk is clk_init'd then we walk the list of orphan
1953 * clocks and re-parent any that are children of the clock currently
1954 * being clk_init'd.
1955 */
1956 if (clk->parent)
1957 hlist_add_head(&clk->child_node,
1958 &clk->parent->children);
1959 else if (clk->flags & CLK_IS_ROOT)
1960 hlist_add_head(&clk->child_node, &clk_root_list);
1961 else
1962 hlist_add_head(&clk->child_node, &clk_orphan_list);
1963
1964 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001965 * Set clk's accuracy. The preferred method is to use
1966 * .recalc_accuracy. For simple clocks and lazy developers the default
1967 * fallback is to use the parent's accuracy. If a clock doesn't have a
1968 * parent (or is orphaned) then accuracy is set to zero (perfect
1969 * clock).
1970 */
1971 if (clk->ops->recalc_accuracy)
1972 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1973 __clk_get_accuracy(clk->parent));
1974 else if (clk->parent)
1975 clk->accuracy = clk->parent->accuracy;
1976 else
1977 clk->accuracy = 0;
1978
1979 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02001980 * Set clk's phase.
1981 * Since a phase is by definition relative to its parent, just
1982 * query the current clock phase, or just assume it's in phase.
1983 */
1984 if (clk->ops->get_phase)
1985 clk->phase = clk->ops->get_phase(clk->hw);
1986 else
1987 clk->phase = 0;
1988
1989 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07001990 * Set clk's rate. The preferred method is to use .recalc_rate. For
1991 * simple clocks and lazy developers the default fallback is to use the
1992 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1993 * then rate is set to zero.
1994 */
1995 if (clk->ops->recalc_rate)
1996 clk->rate = clk->ops->recalc_rate(clk->hw,
1997 __clk_get_rate(clk->parent));
1998 else if (clk->parent)
1999 clk->rate = clk->parent->rate;
2000 else
2001 clk->rate = 0;
2002
2003 /*
2004 * walk the list of orphan clocks and reparent any that are children of
2005 * this clock
2006 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002007 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002008 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002009 i = orphan->ops->get_parent(orphan->hw);
2010 if (!strcmp(clk->name, orphan->parent_names[i]))
2011 __clk_reparent(orphan, clk);
2012 continue;
2013 }
2014
Mike Turquetteb24764902012-03-15 23:11:19 -07002015 for (i = 0; i < orphan->num_parents; i++)
2016 if (!strcmp(clk->name, orphan->parent_names[i])) {
2017 __clk_reparent(orphan, clk);
2018 break;
2019 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002020 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002021
2022 /*
2023 * optional platform-specific magic
2024 *
2025 * The .init callback is not used by any of the basic clock types, but
2026 * exists for weird hardware that must perform initialization magic.
2027 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002028 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002029 */
2030 if (clk->ops->init)
2031 clk->ops->init(clk->hw);
2032
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002033 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002034out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002035 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002036
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002037 if (!ret)
2038 clk_debug_register(clk);
2039
Mike Turquetted1302a32012-03-29 14:30:40 -07002040 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002041}
2042
2043/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002044 * __clk_register - register a clock and return a cookie.
2045 *
2046 * Same as clk_register, except that the .clk field inside hw shall point to a
2047 * preallocated (generally statically allocated) struct clk. None of the fields
2048 * of the struct clk need to be initialized.
2049 *
2050 * The data pointed to by .init and .clk field shall NOT be marked as init
2051 * data.
2052 *
2053 * __clk_register is only exposed via clk-private.h and is intended for use with
2054 * very large numbers of clocks that need to be statically initialized. It is
2055 * a layering violation to include clk-private.h from any code which implements
2056 * a clock's .ops; as such any statically initialized clock data MUST be in a
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002057 * separate C file from the logic that implements its operations. Returns 0
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002058 * on success, otherwise an error code.
2059 */
2060struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
2061{
2062 int ret;
2063 struct clk *clk;
2064
2065 clk = hw->clk;
2066 clk->name = hw->init->name;
2067 clk->ops = hw->init->ops;
2068 clk->hw = hw;
2069 clk->flags = hw->init->flags;
2070 clk->parent_names = hw->init->parent_names;
2071 clk->num_parents = hw->init->num_parents;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002072 if (dev && dev->driver)
2073 clk->owner = dev->driver->owner;
2074 else
2075 clk->owner = NULL;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002076
2077 ret = __clk_init(dev, clk);
2078 if (ret)
2079 return ERR_PTR(ret);
2080
2081 return clk;
2082}
2083EXPORT_SYMBOL_GPL(__clk_register);
2084
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002085/**
2086 * clk_register - allocate a new clock, register it and return an opaque cookie
2087 * @dev: device that is registering this clock
2088 * @hw: link to hardware-specific clock data
2089 *
2090 * clk_register is the primary interface for populating the clock tree with new
2091 * clock nodes. It returns a pointer to the newly allocated struct clk which
2092 * cannot be dereferenced by driver code but may be used in conjuction with the
2093 * rest of the clock API. In the event of an error clk_register will return an
2094 * error code; drivers must test for an error code after calling clk_register.
2095 */
2096struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002097{
Mike Turquetted1302a32012-03-29 14:30:40 -07002098 int i, ret;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002099 struct clk *clk;
2100
2101 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2102 if (!clk) {
2103 pr_err("%s: could not allocate clk\n", __func__);
2104 ret = -ENOMEM;
2105 goto fail_out;
2106 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002107
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002108 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
2109 if (!clk->name) {
2110 pr_err("%s: could not allocate clk->name\n", __func__);
2111 ret = -ENOMEM;
2112 goto fail_name;
2113 }
2114 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002115 if (dev && dev->driver)
2116 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07002117 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002118 clk->flags = hw->init->flags;
2119 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07002120 hw->clk = clk;
2121
Mike Turquetted1302a32012-03-29 14:30:40 -07002122 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002123 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
2124 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002125
Mike Turquetted1302a32012-03-29 14:30:40 -07002126 if (!clk->parent_names) {
2127 pr_err("%s: could not allocate clk->parent_names\n", __func__);
2128 ret = -ENOMEM;
2129 goto fail_parent_names;
2130 }
2131
2132
2133 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002134 for (i = 0; i < clk->num_parents; i++) {
2135 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
2136 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002137 if (!clk->parent_names[i]) {
2138 pr_err("%s: could not copy parent_names\n", __func__);
2139 ret = -ENOMEM;
2140 goto fail_parent_names_copy;
2141 }
2142 }
2143
2144 ret = __clk_init(dev, clk);
2145 if (!ret)
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002146 return clk;
Mike Turquetted1302a32012-03-29 14:30:40 -07002147
2148fail_parent_names_copy:
2149 while (--i >= 0)
2150 kfree(clk->parent_names[i]);
2151 kfree(clk->parent_names);
2152fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002153 kfree(clk->name);
2154fail_name:
Mike Turquetted1302a32012-03-29 14:30:40 -07002155 kfree(clk);
2156fail_out:
2157 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002158}
2159EXPORT_SYMBOL_GPL(clk_register);
2160
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002161/*
2162 * Free memory allocated for a clock.
2163 * Caller must hold prepare_lock.
2164 */
2165static void __clk_release(struct kref *ref)
2166{
2167 struct clk *clk = container_of(ref, struct clk, ref);
2168 int i = clk->num_parents;
2169
2170 kfree(clk->parents);
2171 while (--i >= 0)
2172 kfree(clk->parent_names[i]);
2173
2174 kfree(clk->parent_names);
2175 kfree(clk->name);
2176 kfree(clk);
2177}
2178
2179/*
2180 * Empty clk_ops for unregistered clocks. These are used temporarily
2181 * after clk_unregister() was called on a clock and until last clock
2182 * consumer calls clk_put() and the struct clk object is freed.
2183 */
2184static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2185{
2186 return -ENXIO;
2187}
2188
2189static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2190{
2191 WARN_ON_ONCE(1);
2192}
2193
2194static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2195 unsigned long parent_rate)
2196{
2197 return -ENXIO;
2198}
2199
2200static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2201{
2202 return -ENXIO;
2203}
2204
2205static const struct clk_ops clk_nodrv_ops = {
2206 .enable = clk_nodrv_prepare_enable,
2207 .disable = clk_nodrv_disable_unprepare,
2208 .prepare = clk_nodrv_prepare_enable,
2209 .unprepare = clk_nodrv_disable_unprepare,
2210 .set_rate = clk_nodrv_set_rate,
2211 .set_parent = clk_nodrv_set_parent,
2212};
2213
Mark Brown1df5c932012-04-18 09:07:12 +01002214/**
2215 * clk_unregister - unregister a currently registered clock
2216 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002217 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002218void clk_unregister(struct clk *clk)
2219{
2220 unsigned long flags;
2221
Stephen Boyd6314b672014-09-04 23:37:49 -07002222 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2223 return;
2224
2225 clk_debug_unregister(clk);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002226
2227 clk_prepare_lock();
2228
2229 if (clk->ops == &clk_nodrv_ops) {
2230 pr_err("%s: unregistered clock: %s\n", __func__, clk->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002231 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002232 }
2233 /*
2234 * Assign empty clock ops for consumers that might still hold
2235 * a reference to this clock.
2236 */
2237 flags = clk_enable_lock();
2238 clk->ops = &clk_nodrv_ops;
2239 clk_enable_unlock(flags);
2240
2241 if (!hlist_empty(&clk->children)) {
2242 struct clk *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002243 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002244
2245 /* Reparent all children to the orphan list. */
Stephen Boyd874f2242014-04-18 16:29:43 -07002246 hlist_for_each_entry_safe(child, t, &clk->children, child_node)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002247 clk_set_parent(child, NULL);
2248 }
2249
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002250 hlist_del_init(&clk->child_node);
2251
2252 if (clk->prepare_count)
2253 pr_warn("%s: unregistering prepared clock: %s\n",
2254 __func__, clk->name);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002255 kref_put(&clk->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002256
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002257 clk_prepare_unlock();
2258}
Mark Brown1df5c932012-04-18 09:07:12 +01002259EXPORT_SYMBOL_GPL(clk_unregister);
2260
Stephen Boyd46c87732012-09-24 13:38:04 -07002261static void devm_clk_release(struct device *dev, void *res)
2262{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002263 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002264}
2265
2266/**
2267 * devm_clk_register - resource managed clk_register()
2268 * @dev: device that is registering this clock
2269 * @hw: link to hardware-specific clock data
2270 *
2271 * Managed clk_register(). Clocks returned from this function are
2272 * automatically clk_unregister()ed on driver detach. See clk_register() for
2273 * more information.
2274 */
2275struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2276{
2277 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002278 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002279
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002280 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2281 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002282 return ERR_PTR(-ENOMEM);
2283
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002284 clk = clk_register(dev, hw);
2285 if (!IS_ERR(clk)) {
2286 *clkp = clk;
2287 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002288 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002289 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002290 }
2291
2292 return clk;
2293}
2294EXPORT_SYMBOL_GPL(devm_clk_register);
2295
2296static int devm_clk_match(struct device *dev, void *res, void *data)
2297{
2298 struct clk *c = res;
2299 if (WARN_ON(!c))
2300 return 0;
2301 return c == data;
2302}
2303
2304/**
2305 * devm_clk_unregister - resource managed clk_unregister()
2306 * @clk: clock to unregister
2307 *
2308 * Deallocate a clock allocated with devm_clk_register(). Normally
2309 * this function will not need to be called and the resource management
2310 * code will ensure that the resource is freed.
2311 */
2312void devm_clk_unregister(struct device *dev, struct clk *clk)
2313{
2314 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2315}
2316EXPORT_SYMBOL_GPL(devm_clk_unregister);
2317
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002318/*
2319 * clkdev helpers
2320 */
2321int __clk_get(struct clk *clk)
2322{
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002323 if (clk) {
2324 if (!try_module_get(clk->owner))
2325 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002326
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002327 kref_get(&clk->ref);
2328 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002329 return 1;
2330}
2331
2332void __clk_put(struct clk *clk)
2333{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002334 struct module *owner;
2335
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002336 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002337 return;
2338
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002339 clk_prepare_lock();
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002340 owner = clk->owner;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002341 kref_put(&clk->ref, __clk_release);
2342 clk_prepare_unlock();
2343
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002344 module_put(owner);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002345}
2346
Mike Turquetteb24764902012-03-15 23:11:19 -07002347/*** clk rate change notifiers ***/
2348
2349/**
2350 * clk_notifier_register - add a clk rate change notifier
2351 * @clk: struct clk * to watch
2352 * @nb: struct notifier_block * with callback info
2353 *
2354 * Request notification when clk's rate changes. This uses an SRCU
2355 * notifier because we want it to block and notifier unregistrations are
2356 * uncommon. The callbacks associated with the notifier must not
2357 * re-enter into the clk framework by calling any top-level clk APIs;
2358 * this will cause a nested prepare_lock mutex.
2359 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002360 * In all notification cases cases (pre, post and abort rate change) the
2361 * original clock rate is passed to the callback via struct
2362 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002363 * clk_notifier_data.new_rate.
2364 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002365 * clk_notifier_register() must be called from non-atomic context.
2366 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2367 * allocation failure; otherwise, passes along the return value of
2368 * srcu_notifier_chain_register().
2369 */
2370int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2371{
2372 struct clk_notifier *cn;
2373 int ret = -ENOMEM;
2374
2375 if (!clk || !nb)
2376 return -EINVAL;
2377
Mike Turquetteeab89f62013-03-28 13:59:01 -07002378 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002379
2380 /* search the list of notifiers for this clk */
2381 list_for_each_entry(cn, &clk_notifier_list, node)
2382 if (cn->clk == clk)
2383 break;
2384
2385 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2386 if (cn->clk != clk) {
2387 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2388 if (!cn)
2389 goto out;
2390
2391 cn->clk = clk;
2392 srcu_init_notifier_head(&cn->notifier_head);
2393
2394 list_add(&cn->node, &clk_notifier_list);
2395 }
2396
2397 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2398
2399 clk->notifier_count++;
2400
2401out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002402 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002403
2404 return ret;
2405}
2406EXPORT_SYMBOL_GPL(clk_notifier_register);
2407
2408/**
2409 * clk_notifier_unregister - remove a clk rate change notifier
2410 * @clk: struct clk *
2411 * @nb: struct notifier_block * with callback info
2412 *
2413 * Request no further notification for changes to 'clk' and frees memory
2414 * allocated in clk_notifier_register.
2415 *
2416 * Returns -EINVAL if called with null arguments; otherwise, passes
2417 * along the return value of srcu_notifier_chain_unregister().
2418 */
2419int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2420{
2421 struct clk_notifier *cn = NULL;
2422 int ret = -EINVAL;
2423
2424 if (!clk || !nb)
2425 return -EINVAL;
2426
Mike Turquetteeab89f62013-03-28 13:59:01 -07002427 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002428
2429 list_for_each_entry(cn, &clk_notifier_list, node)
2430 if (cn->clk == clk)
2431 break;
2432
2433 if (cn->clk == clk) {
2434 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2435
2436 clk->notifier_count--;
2437
2438 /* XXX the notifier code should handle this better */
2439 if (!cn->notifier_head.head) {
2440 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002441 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002442 kfree(cn);
2443 }
2444
2445 } else {
2446 ret = -ENOENT;
2447 }
2448
Mike Turquetteeab89f62013-03-28 13:59:01 -07002449 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002450
2451 return ret;
2452}
2453EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002454
2455#ifdef CONFIG_OF
2456/**
2457 * struct of_clk_provider - Clock provider registration structure
2458 * @link: Entry in global list of clock providers
2459 * @node: Pointer to device tree node of clock provider
2460 * @get: Get clock callback. Returns NULL or a struct clk for the
2461 * given clock specifier
2462 * @data: context pointer to be passed into @get callback
2463 */
2464struct of_clk_provider {
2465 struct list_head link;
2466
2467 struct device_node *node;
2468 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2469 void *data;
2470};
2471
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302472static const struct of_device_id __clk_of_table_sentinel
2473 __used __section(__clk_of_table_end);
2474
Grant Likely766e6a42012-04-09 14:50:06 -05002475static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002476static DEFINE_MUTEX(of_clk_mutex);
2477
2478/* of_clk_provider list locking helpers */
2479void of_clk_lock(void)
2480{
2481 mutex_lock(&of_clk_mutex);
2482}
2483
2484void of_clk_unlock(void)
2485{
2486 mutex_unlock(&of_clk_mutex);
2487}
Grant Likely766e6a42012-04-09 14:50:06 -05002488
2489struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2490 void *data)
2491{
2492 return data;
2493}
2494EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2495
Shawn Guo494bfec2012-08-22 21:36:27 +08002496struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2497{
2498 struct clk_onecell_data *clk_data = data;
2499 unsigned int idx = clkspec->args[0];
2500
2501 if (idx >= clk_data->clk_num) {
2502 pr_err("%s: invalid clock index %d\n", __func__, idx);
2503 return ERR_PTR(-EINVAL);
2504 }
2505
2506 return clk_data->clks[idx];
2507}
2508EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2509
Grant Likely766e6a42012-04-09 14:50:06 -05002510/**
2511 * of_clk_add_provider() - Register a clock provider for a node
2512 * @np: Device node pointer associated with clock provider
2513 * @clk_src_get: callback for decoding clock
2514 * @data: context pointer for @clk_src_get callback.
2515 */
2516int of_clk_add_provider(struct device_node *np,
2517 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2518 void *data),
2519 void *data)
2520{
2521 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002522 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002523
2524 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2525 if (!cp)
2526 return -ENOMEM;
2527
2528 cp->node = of_node_get(np);
2529 cp->data = data;
2530 cp->get = clk_src_get;
2531
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002532 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002533 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002534 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002535 pr_debug("Added clock from %s\n", np->full_name);
2536
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002537 ret = of_clk_set_defaults(np, true);
2538 if (ret < 0)
2539 of_clk_del_provider(np);
2540
2541 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002542}
2543EXPORT_SYMBOL_GPL(of_clk_add_provider);
2544
2545/**
2546 * of_clk_del_provider() - Remove a previously registered clock provider
2547 * @np: Device node pointer associated with clock provider
2548 */
2549void of_clk_del_provider(struct device_node *np)
2550{
2551 struct of_clk_provider *cp;
2552
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002553 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002554 list_for_each_entry(cp, &of_clk_providers, link) {
2555 if (cp->node == np) {
2556 list_del(&cp->link);
2557 of_node_put(cp->node);
2558 kfree(cp);
2559 break;
2560 }
2561 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002562 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002563}
2564EXPORT_SYMBOL_GPL(of_clk_del_provider);
2565
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002566struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05002567{
2568 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002569 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002570
2571 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002572 list_for_each_entry(provider, &of_clk_providers, link) {
2573 if (provider->node == clkspec->np)
2574 clk = provider->get(clkspec, provider->data);
2575 if (!IS_ERR(clk))
2576 break;
2577 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002578
2579 return clk;
2580}
2581
2582struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2583{
2584 struct clk *clk;
2585
2586 mutex_lock(&of_clk_mutex);
2587 clk = __of_clk_get_from_provider(clkspec);
2588 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002589
2590 return clk;
2591}
2592
Mike Turquettef6102742013-10-07 23:12:13 -07002593int of_clk_get_parent_count(struct device_node *np)
2594{
2595 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2596}
2597EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2598
Grant Likely766e6a42012-04-09 14:50:06 -05002599const char *of_clk_get_parent_name(struct device_node *np, int index)
2600{
2601 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002602 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002603 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002604 const __be32 *vp;
2605 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002606 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002607 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002608
2609 if (index < 0)
2610 return NULL;
2611
2612 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2613 &clkspec);
2614 if (rc)
2615 return NULL;
2616
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002617 index = clkspec.args_count ? clkspec.args[0] : 0;
2618 count = 0;
2619
2620 /* if there is an indices property, use it to transfer the index
2621 * specified into an array offset for the clock-output-names property.
2622 */
2623 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2624 if (index == pv) {
2625 index = count;
2626 break;
2627 }
2628 count++;
2629 }
2630
Grant Likely766e6a42012-04-09 14:50:06 -05002631 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002632 index,
Grant Likely766e6a42012-04-09 14:50:06 -05002633 &clk_name) < 0)
2634 clk_name = clkspec.np->name;
2635
2636 of_node_put(clkspec.np);
2637 return clk_name;
2638}
2639EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2640
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002641struct clock_provider {
2642 of_clk_init_cb_t clk_init_cb;
2643 struct device_node *np;
2644 struct list_head node;
2645};
2646
2647static LIST_HEAD(clk_provider_list);
2648
2649/*
2650 * This function looks for a parent clock. If there is one, then it
2651 * checks that the provider for this parent clock was initialized, in
2652 * this case the parent clock will be ready.
2653 */
2654static int parent_ready(struct device_node *np)
2655{
2656 int i = 0;
2657
2658 while (true) {
2659 struct clk *clk = of_clk_get(np, i);
2660
2661 /* this parent is ready we can check the next one */
2662 if (!IS_ERR(clk)) {
2663 clk_put(clk);
2664 i++;
2665 continue;
2666 }
2667
2668 /* at least one parent is not ready, we exit now */
2669 if (PTR_ERR(clk) == -EPROBE_DEFER)
2670 return 0;
2671
2672 /*
2673 * Here we make assumption that the device tree is
2674 * written correctly. So an error means that there is
2675 * no more parent. As we didn't exit yet, then the
2676 * previous parent are ready. If there is no clock
2677 * parent, no need to wait for them, then we can
2678 * consider their absence as being ready
2679 */
2680 return 1;
2681 }
2682}
2683
Grant Likely766e6a42012-04-09 14:50:06 -05002684/**
2685 * of_clk_init() - Scan and init clock providers from the DT
2686 * @matches: array of compatible values and init functions for providers.
2687 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002688 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01002689 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002690 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05002691 */
2692void __init of_clk_init(const struct of_device_id *matches)
2693{
Alex Elder7f7ed582013-08-22 11:31:31 -05002694 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05002695 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002696 struct clock_provider *clk_provider, *next;
2697 bool is_init_done;
2698 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05002699
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302700 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03002701 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302702
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002703 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05002704 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002705 struct clock_provider *parent =
2706 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
2707
2708 parent->clk_init_cb = match->data;
2709 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01002710 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002711 }
2712
2713 while (!list_empty(&clk_provider_list)) {
2714 is_init_done = false;
2715 list_for_each_entry_safe(clk_provider, next,
2716 &clk_provider_list, node) {
2717 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002718
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002719 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002720 of_clk_set_defaults(clk_provider->np, true);
2721
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002722 list_del(&clk_provider->node);
2723 kfree(clk_provider);
2724 is_init_done = true;
2725 }
2726 }
2727
2728 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01002729 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002730 * remaining providers during the last loop, so now we
2731 * initialize all the remaining ones unconditionally
2732 * in case the clock parent was not mandatory
2733 */
2734 if (!is_init_done)
2735 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05002736 }
2737}
2738#endif