blob: 0b3f39c037857ba9803d029bfdbde7ec1a0f2e84 [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
Michael Turquetteb09d6d92015-01-29 14:22:50 -080012#include <linux/clk-provider.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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010040static long clk_core_get_accuracy(struct clk_core *clk);
41static unsigned long clk_core_get_rate(struct clk_core *clk);
42static int clk_core_get_phase(struct clk_core *clk);
43static bool clk_core_is_prepared(struct clk_core *clk);
44static bool clk_core_is_enabled(struct clk_core *clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010045static struct clk_core *clk_core_lookup(const char *name);
46
Michael Turquetteb09d6d92015-01-29 14:22:50 -080047/*** private data structures ***/
48
49struct clk_core {
50 const char *name;
51 const struct clk_ops *ops;
52 struct clk_hw *hw;
53 struct module *owner;
54 struct clk_core *parent;
55 const char **parent_names;
56 struct clk_core **parents;
57 u8 num_parents;
58 u8 new_parent_index;
59 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010060 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080061 unsigned long new_rate;
62 struct clk_core *new_parent;
63 struct clk_core *new_child;
64 unsigned long flags;
65 unsigned int enable_count;
66 unsigned int prepare_count;
67 unsigned long accuracy;
68 int phase;
69 struct hlist_head children;
70 struct hlist_node child_node;
71 struct hlist_node debug_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010072 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080073 unsigned int notifier_count;
74#ifdef CONFIG_DEBUG_FS
75 struct dentry *dentry;
76#endif
77 struct kref ref;
78};
79
80struct clk {
81 struct clk_core *core;
82 const char *dev_id;
83 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010084 unsigned long min_rate;
85 unsigned long max_rate;
86 struct hlist_node child_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080087};
88
Mike Turquetteeab89f62013-03-28 13:59:01 -070089/*** locking ***/
90static void clk_prepare_lock(void)
91{
Mike Turquette533ddeb2013-03-28 13:59:02 -070092 if (!mutex_trylock(&prepare_lock)) {
93 if (prepare_owner == current) {
94 prepare_refcnt++;
95 return;
96 }
97 mutex_lock(&prepare_lock);
98 }
99 WARN_ON_ONCE(prepare_owner != NULL);
100 WARN_ON_ONCE(prepare_refcnt != 0);
101 prepare_owner = current;
102 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700103}
104
105static void clk_prepare_unlock(void)
106{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700107 WARN_ON_ONCE(prepare_owner != current);
108 WARN_ON_ONCE(prepare_refcnt == 0);
109
110 if (--prepare_refcnt)
111 return;
112 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700113 mutex_unlock(&prepare_lock);
114}
115
116static unsigned long clk_enable_lock(void)
117{
118 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700119
120 if (!spin_trylock_irqsave(&enable_lock, flags)) {
121 if (enable_owner == current) {
122 enable_refcnt++;
123 return flags;
124 }
125 spin_lock_irqsave(&enable_lock, flags);
126 }
127 WARN_ON_ONCE(enable_owner != NULL);
128 WARN_ON_ONCE(enable_refcnt != 0);
129 enable_owner = current;
130 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700131 return flags;
132}
133
134static void clk_enable_unlock(unsigned long flags)
135{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700136 WARN_ON_ONCE(enable_owner != current);
137 WARN_ON_ONCE(enable_refcnt == 0);
138
139 if (--enable_refcnt)
140 return;
141 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700142 spin_unlock_irqrestore(&enable_lock, flags);
143}
144
Mike Turquetteb24764902012-03-15 23:11:19 -0700145/*** debugfs support ***/
146
Mike Turquetteea72dc22013-12-18 21:38:52 -0800147#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -0700148#include <linux/debugfs.h>
149
150static struct dentry *rootdir;
Mike Turquetteb24764902012-03-15 23:11:19 -0700151static int inited = 0;
Stephen Boyd6314b672014-09-04 23:37:49 -0700152static DEFINE_MUTEX(clk_debug_lock);
153static HLIST_HEAD(clk_debug_list);
Mike Turquetteb24764902012-03-15 23:11:19 -0700154
Sachin Kamat6b44c8542014-07-01 11:56:34 +0530155static struct hlist_head *all_lists[] = {
156 &clk_root_list,
157 &clk_orphan_list,
158 NULL,
159};
160
161static struct hlist_head *orphan_list[] = {
162 &clk_orphan_list,
163 NULL,
164};
165
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100166static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
167 int level)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530168{
169 if (!c)
170 return;
171
Mike Turquettee59c5372014-02-18 21:21:25 -0800172 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530173 level * 3 + 1, "",
174 30 - level * 3, c->name,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100175 c->enable_count, c->prepare_count, clk_core_get_rate(c),
176 clk_core_get_accuracy(c), clk_core_get_phase(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530177}
178
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100179static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530180 int level)
181{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100182 struct clk_core *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530183
184 if (!c)
185 return;
186
187 clk_summary_show_one(s, c, level);
188
Sasha Levinb67bfe02013-02-27 17:06:00 -0800189 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530190 clk_summary_show_subtree(s, child, level + 1);
191}
192
193static int clk_summary_show(struct seq_file *s, void *data)
194{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100195 struct clk_core *c;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300196 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530197
Mike Turquettee59c5372014-02-18 21:21:25 -0800198 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
199 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530200
Mike Turquetteeab89f62013-03-28 13:59:01 -0700201 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530202
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300203 for (; *lists; lists++)
204 hlist_for_each_entry(c, *lists, child_node)
205 clk_summary_show_subtree(s, c, 0);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530206
Mike Turquetteeab89f62013-03-28 13:59:01 -0700207 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530208
209 return 0;
210}
211
212
213static int clk_summary_open(struct inode *inode, struct file *file)
214{
215 return single_open(file, clk_summary_show, inode->i_private);
216}
217
218static const struct file_operations clk_summary_fops = {
219 .open = clk_summary_open,
220 .read = seq_read,
221 .llseek = seq_lseek,
222 .release = single_release,
223};
224
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100225static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530226{
227 if (!c)
228 return;
229
230 seq_printf(s, "\"%s\": { ", c->name);
231 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
232 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100233 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
234 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
235 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530236}
237
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100238static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530239{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100240 struct clk_core *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530241
242 if (!c)
243 return;
244
245 clk_dump_one(s, c, level);
246
Sasha Levinb67bfe02013-02-27 17:06:00 -0800247 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530248 seq_printf(s, ",");
249 clk_dump_subtree(s, child, level + 1);
250 }
251
252 seq_printf(s, "}");
253}
254
255static int clk_dump(struct seq_file *s, void *data)
256{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100257 struct clk_core *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530258 bool first_node = true;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300259 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530260
261 seq_printf(s, "{");
262
Mike Turquetteeab89f62013-03-28 13:59:01 -0700263 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530264
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300265 for (; *lists; lists++) {
266 hlist_for_each_entry(c, *lists, child_node) {
267 if (!first_node)
268 seq_puts(s, ",");
269 first_node = false;
270 clk_dump_subtree(s, c, 0);
271 }
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530272 }
273
Mike Turquetteeab89f62013-03-28 13:59:01 -0700274 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530275
276 seq_printf(s, "}");
277 return 0;
278}
279
280
281static int clk_dump_open(struct inode *inode, struct file *file)
282{
283 return single_open(file, clk_dump, inode->i_private);
284}
285
286static const struct file_operations clk_dump_fops = {
287 .open = clk_dump_open,
288 .read = seq_read,
289 .llseek = seq_lseek,
290 .release = single_release,
291};
292
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100293static int clk_debug_create_one(struct clk_core *clk, struct dentry *pdentry)
Mike Turquetteb24764902012-03-15 23:11:19 -0700294{
295 struct dentry *d;
296 int ret = -ENOMEM;
297
298 if (!clk || !pdentry) {
299 ret = -EINVAL;
300 goto out;
301 }
302
303 d = debugfs_create_dir(clk->name, pdentry);
304 if (!d)
305 goto out;
306
307 clk->dentry = d;
308
309 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
310 (u32 *)&clk->rate);
311 if (!d)
312 goto err_out;
313
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100314 d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
315 (u32 *)&clk->accuracy);
316 if (!d)
317 goto err_out;
318
Mike Turquettee59c5372014-02-18 21:21:25 -0800319 d = debugfs_create_u32("clk_phase", S_IRUGO, clk->dentry,
320 (u32 *)&clk->phase);
321 if (!d)
322 goto err_out;
323
Mike Turquetteb24764902012-03-15 23:11:19 -0700324 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
325 (u32 *)&clk->flags);
326 if (!d)
327 goto err_out;
328
329 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
330 (u32 *)&clk->prepare_count);
331 if (!d)
332 goto err_out;
333
334 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
335 (u32 *)&clk->enable_count);
336 if (!d)
337 goto err_out;
338
339 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
340 (u32 *)&clk->notifier_count);
341 if (!d)
342 goto err_out;
343
Chris Brandabeab452014-07-03 14:01:29 -0700344 if (clk->ops->debug_init) {
345 ret = clk->ops->debug_init(clk->hw, clk->dentry);
346 if (ret)
Alex Elderc646cbf2014-03-21 06:43:56 -0500347 goto err_out;
Chris Brandabeab452014-07-03 14:01:29 -0700348 }
Alex Elderc646cbf2014-03-21 06:43:56 -0500349
Mike Turquetteb24764902012-03-15 23:11:19 -0700350 ret = 0;
351 goto out;
352
353err_out:
Alex Elderb5f98e62013-11-27 09:39:49 -0600354 debugfs_remove_recursive(clk->dentry);
355 clk->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700356out:
357 return ret;
358}
359
Mike Turquetteb24764902012-03-15 23:11:19 -0700360/**
361 * clk_debug_register - add a clk node to the debugfs clk tree
362 * @clk: the clk being added to the debugfs clk tree
363 *
364 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
365 * initialized. Otherwise it bails out early since the debugfs clk tree
366 * will be created lazily by clk_debug_init as part of a late_initcall.
Mike Turquetteb24764902012-03-15 23:11:19 -0700367 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100368static int clk_debug_register(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700369{
Mike Turquetteb24764902012-03-15 23:11:19 -0700370 int ret = 0;
371
Stephen Boyd6314b672014-09-04 23:37:49 -0700372 mutex_lock(&clk_debug_lock);
373 hlist_add_head(&clk->debug_node, &clk_debug_list);
374
Mike Turquetteb24764902012-03-15 23:11:19 -0700375 if (!inited)
Stephen Boyd6314b672014-09-04 23:37:49 -0700376 goto unlock;
Mike Turquetteb24764902012-03-15 23:11:19 -0700377
Stephen Boyd6314b672014-09-04 23:37:49 -0700378 ret = clk_debug_create_one(clk, rootdir);
379unlock:
380 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700381
Mike Turquetteb24764902012-03-15 23:11:19 -0700382 return ret;
383}
384
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200385 /**
386 * clk_debug_unregister - remove a clk node from the debugfs clk tree
387 * @clk: the clk being removed from the debugfs clk tree
388 *
389 * Dynamically removes a clk and all it's children clk nodes from the
390 * debugfs clk tree if clk->dentry points to debugfs created by
391 * clk_debug_register in __clk_init.
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200392 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100393static void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200394{
Stephen Boyd6314b672014-09-04 23:37:49 -0700395 mutex_lock(&clk_debug_lock);
Stephen Boyd6314b672014-09-04 23:37:49 -0700396 hlist_del_init(&clk->debug_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200397 debugfs_remove_recursive(clk->dentry);
Stephen Boyd6314b672014-09-04 23:37:49 -0700398 clk->dentry = NULL;
Stephen Boyd6314b672014-09-04 23:37:49 -0700399 mutex_unlock(&clk_debug_lock);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200400}
401
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100402struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300403 void *data, const struct file_operations *fops)
404{
405 struct dentry *d = NULL;
406
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100407 if (hw->core->dentry)
408 d = debugfs_create_file(name, mode, hw->core->dentry, data,
409 fops);
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300410
411 return d;
412}
413EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
414
Mike Turquetteb24764902012-03-15 23:11:19 -0700415/**
416 * clk_debug_init - lazily create the debugfs clk tree visualization
417 *
418 * clks are often initialized very early during boot before memory can
419 * be dynamically allocated and well before debugfs is setup.
420 * clk_debug_init walks the clk tree hierarchy while holding
421 * prepare_lock and creates the topology as part of a late_initcall,
422 * thus insuring that clks initialized very early will still be
423 * represented in the debugfs clk tree. This function should only be
424 * called once at boot-time, and all other clks added dynamically will
425 * be done so with clk_debug_register.
426 */
427static int __init clk_debug_init(void)
428{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100429 struct clk_core *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530430 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700431
432 rootdir = debugfs_create_dir("clk", NULL);
433
434 if (!rootdir)
435 return -ENOMEM;
436
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300437 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530438 &clk_summary_fops);
439 if (!d)
440 return -ENOMEM;
441
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300442 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530443 &clk_dump_fops);
444 if (!d)
445 return -ENOMEM;
446
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300447 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
448 &orphan_list, &clk_summary_fops);
449 if (!d)
450 return -ENOMEM;
Mike Turquetteb24764902012-03-15 23:11:19 -0700451
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300452 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
453 &orphan_list, &clk_dump_fops);
454 if (!d)
Mike Turquetteb24764902012-03-15 23:11:19 -0700455 return -ENOMEM;
456
Stephen Boyd6314b672014-09-04 23:37:49 -0700457 mutex_lock(&clk_debug_lock);
458 hlist_for_each_entry(clk, &clk_debug_list, debug_node)
459 clk_debug_create_one(clk, rootdir);
Mike Turquetteb24764902012-03-15 23:11:19 -0700460
461 inited = 1;
Stephen Boyd6314b672014-09-04 23:37:49 -0700462 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700463
464 return 0;
465}
466late_initcall(clk_debug_init);
467#else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100468static inline int clk_debug_register(struct clk_core *clk) { return 0; }
469static inline void clk_debug_reparent(struct clk_core *clk,
470 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200471{
472}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100473static inline void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200474{
475}
Mike Turquette70d347e2012-03-26 11:53:47 -0700476#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700477
Mike Turquetteb24764902012-03-15 23:11:19 -0700478/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100479static void clk_unprepare_unused_subtree(struct clk_core *clk)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100480{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100481 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100482
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100483 lockdep_assert_held(&prepare_lock);
484
Ulf Hansson1c155b32013-03-12 20:26:03 +0100485 hlist_for_each_entry(child, &clk->children, child_node)
486 clk_unprepare_unused_subtree(child);
487
488 if (clk->prepare_count)
489 return;
490
491 if (clk->flags & CLK_IGNORE_UNUSED)
492 return;
493
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100494 if (clk_core_is_prepared(clk)) {
Ulf Hansson3cc82472013-03-12 20:26:04 +0100495 if (clk->ops->unprepare_unused)
496 clk->ops->unprepare_unused(clk->hw);
497 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100498 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100499 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100500}
501
502/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100503static void clk_disable_unused_subtree(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700504{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100505 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700506 unsigned long flags;
507
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100508 lockdep_assert_held(&prepare_lock);
509
Sasha Levinb67bfe02013-02-27 17:06:00 -0800510 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700511 clk_disable_unused_subtree(child);
512
Mike Turquetteeab89f62013-03-28 13:59:01 -0700513 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700514
515 if (clk->enable_count)
516 goto unlock_out;
517
518 if (clk->flags & CLK_IGNORE_UNUSED)
519 goto unlock_out;
520
Mike Turquette7c045a52012-12-04 11:00:35 -0800521 /*
522 * some gate clocks have special needs during the disable-unused
523 * sequence. call .disable_unused if available, otherwise fall
524 * back to .disable
525 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100526 if (clk_core_is_enabled(clk)) {
Mike Turquette7c045a52012-12-04 11:00:35 -0800527 if (clk->ops->disable_unused)
528 clk->ops->disable_unused(clk->hw);
529 else if (clk->ops->disable)
530 clk->ops->disable(clk->hw);
531 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700532
533unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700534 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700535}
536
Olof Johansson1e435252013-04-27 14:10:18 -0700537static bool clk_ignore_unused;
538static int __init clk_ignore_unused_setup(char *__unused)
539{
540 clk_ignore_unused = true;
541 return 1;
542}
543__setup("clk_ignore_unused", clk_ignore_unused_setup);
544
Mike Turquetteb24764902012-03-15 23:11:19 -0700545static int clk_disable_unused(void)
546{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100547 struct clk_core *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700548
Olof Johansson1e435252013-04-27 14:10:18 -0700549 if (clk_ignore_unused) {
550 pr_warn("clk: Not disabling unused clocks\n");
551 return 0;
552 }
553
Mike Turquetteeab89f62013-03-28 13:59:01 -0700554 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700555
Sasha Levinb67bfe02013-02-27 17:06:00 -0800556 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700557 clk_disable_unused_subtree(clk);
558
Sasha Levinb67bfe02013-02-27 17:06:00 -0800559 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700560 clk_disable_unused_subtree(clk);
561
Ulf Hansson1c155b32013-03-12 20:26:03 +0100562 hlist_for_each_entry(clk, &clk_root_list, child_node)
563 clk_unprepare_unused_subtree(clk);
564
565 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
566 clk_unprepare_unused_subtree(clk);
567
Mike Turquetteeab89f62013-03-28 13:59:01 -0700568 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700569
570 return 0;
571}
Saravana Kannand41d5802013-05-09 11:35:01 -0700572late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700573
574/*** helper functions ***/
575
Russ Dill65800b22012-11-26 11:20:09 -0800576const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700577{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100578 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700579}
Niels de Vos48950842012-12-13 13:12:25 +0100580EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700581
Russ Dill65800b22012-11-26 11:20:09 -0800582struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700583{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100584 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700585}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800586EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700587
Russ Dill65800b22012-11-26 11:20:09 -0800588u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700589{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100590 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700591}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800592EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700593
Russ Dill65800b22012-11-26 11:20:09 -0800594struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700595{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100596 if (!clk)
597 return NULL;
598
599 /* TODO: Create a per-user clk and change callers to call clk_put */
600 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700601}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800602EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700603
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100604static struct clk_core *clk_core_get_parent_by_index(struct clk_core *clk,
605 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100606{
607 if (!clk || index >= clk->num_parents)
608 return NULL;
609 else if (!clk->parents)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100610 return clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100611 else if (!clk->parents[index])
612 return clk->parents[index] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100613 clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100614 else
615 return clk->parents[index];
616}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100617
618struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
619{
620 struct clk_core *parent;
621
622 if (!clk)
623 return NULL;
624
625 parent = clk_core_get_parent_by_index(clk->core, index);
626
627 return !parent ? NULL : parent->hw->clk;
628}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800629EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100630
Russ Dill65800b22012-11-26 11:20:09 -0800631unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700632{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100633 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700634}
635
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100636static unsigned long clk_core_get_rate_nolock(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700637{
638 unsigned long ret;
639
640 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530641 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700642 goto out;
643 }
644
645 ret = clk->rate;
646
647 if (clk->flags & CLK_IS_ROOT)
648 goto out;
649
650 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530651 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700652
653out:
654 return ret;
655}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100656
657unsigned long __clk_get_rate(struct clk *clk)
658{
659 if (!clk)
660 return 0;
661
662 return clk_core_get_rate_nolock(clk->core);
663}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800664EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700665
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100666static unsigned long __clk_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100667{
668 if (!clk)
669 return 0;
670
671 return clk->accuracy;
672}
673
Russ Dill65800b22012-11-26 11:20:09 -0800674unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700675{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100676 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700677}
Thierry Redingb05c6832013-09-03 09:43:51 +0200678EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700679
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100680static bool clk_core_is_prepared(struct clk_core *clk)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100681{
682 int ret;
683
684 if (!clk)
685 return false;
686
687 /*
688 * .is_prepared is optional for clocks that can prepare
689 * fall back to software usage counter if it is missing
690 */
691 if (!clk->ops->is_prepared) {
692 ret = clk->prepare_count ? 1 : 0;
693 goto out;
694 }
695
696 ret = clk->ops->is_prepared(clk->hw);
697out:
698 return !!ret;
699}
700
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100701bool __clk_is_prepared(struct clk *clk)
702{
703 if (!clk)
704 return false;
705
706 return clk_core_is_prepared(clk->core);
707}
708
709static bool clk_core_is_enabled(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700710{
711 int ret;
712
713 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700714 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700715
716 /*
717 * .is_enabled is only mandatory for clocks that gate
718 * fall back to software usage counter if .is_enabled is missing
719 */
720 if (!clk->ops->is_enabled) {
721 ret = clk->enable_count ? 1 : 0;
722 goto out;
723 }
724
725 ret = clk->ops->is_enabled(clk->hw);
726out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700727 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700728}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100729
730bool __clk_is_enabled(struct clk *clk)
731{
732 if (!clk)
733 return false;
734
735 return clk_core_is_enabled(clk->core);
736}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800737EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700738
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100739static struct clk_core *__clk_lookup_subtree(const char *name,
740 struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700741{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100742 struct clk_core *child;
743 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700744
745 if (!strcmp(clk->name, name))
746 return clk;
747
Sasha Levinb67bfe02013-02-27 17:06:00 -0800748 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700749 ret = __clk_lookup_subtree(name, child);
750 if (ret)
751 return ret;
752 }
753
754 return NULL;
755}
756
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100757static struct clk_core *clk_core_lookup(const char *name)
Mike Turquetteb24764902012-03-15 23:11:19 -0700758{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100759 struct clk_core *root_clk;
760 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700761
762 if (!name)
763 return NULL;
764
765 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800766 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700767 ret = __clk_lookup_subtree(name, root_clk);
768 if (ret)
769 return ret;
770 }
771
772 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800773 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700774 ret = __clk_lookup_subtree(name, root_clk);
775 if (ret)
776 return ret;
777 }
778
779 return NULL;
780}
781
Stephen Boyd15a02c12015-01-19 18:05:28 -0800782static bool mux_is_better_rate(unsigned long rate, unsigned long now,
783 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100784{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800785 if (flags & CLK_MUX_ROUND_CLOSEST)
786 return abs(now - rate) < abs(best - rate);
787
788 return now <= rate && now > best;
789}
790
791static long
792clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100793 unsigned long min_rate,
794 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800795 unsigned long *best_parent_rate,
796 struct clk_hw **best_parent_p,
797 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100798{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100799 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100800 int i, num_parents;
801 unsigned long parent_rate, best = 0;
802
803 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100804 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
805 parent = core->parent;
806 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100807 best = __clk_determine_rate(parent ? parent->hw : NULL,
808 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100809 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100810 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100811 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100812 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100813 goto out;
814 }
815
816 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100817 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100818 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100819 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100820 if (!parent)
821 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100822 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100823 parent_rate = __clk_determine_rate(parent->hw, rate,
824 min_rate,
825 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100826 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100827 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800828 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100829 best_parent = parent;
830 best = parent_rate;
831 }
832 }
833
834out:
835 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100836 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100837 *best_parent_rate = best;
838
839 return best;
840}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800841
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100842struct clk *__clk_lookup(const char *name)
843{
844 struct clk_core *core = clk_core_lookup(name);
845
846 return !core ? NULL : core->hw->clk;
847}
848
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100849static void clk_core_get_boundaries(struct clk_core *clk,
850 unsigned long *min_rate,
851 unsigned long *max_rate)
852{
853 struct clk *clk_user;
854
855 *min_rate = 0;
856 *max_rate = ULONG_MAX;
857
858 hlist_for_each_entry(clk_user, &clk->clks, child_node)
859 *min_rate = max(*min_rate, clk_user->min_rate);
860
861 hlist_for_each_entry(clk_user, &clk->clks, child_node)
862 *max_rate = min(*max_rate, clk_user->max_rate);
863}
864
Stephen Boyd15a02c12015-01-19 18:05:28 -0800865/*
866 * Helper for finding best parent to provide a given frequency. This can be used
867 * directly as a determine_rate callback (e.g. for a mux), or from a more
868 * complex clock that may combine a mux with other operations.
869 */
870long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100871 unsigned long min_rate,
872 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800873 unsigned long *best_parent_rate,
874 struct clk_hw **best_parent_p)
875{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100876 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
877 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800878 best_parent_p, 0);
879}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800880EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100881
Stephen Boyd15a02c12015-01-19 18:05:28 -0800882long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100883 unsigned long min_rate,
884 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800885 unsigned long *best_parent_rate,
886 struct clk_hw **best_parent_p)
887{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100888 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
889 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800890 best_parent_p,
891 CLK_MUX_ROUND_CLOSEST);
892}
893EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
894
Mike Turquetteb24764902012-03-15 23:11:19 -0700895/*** clk api ***/
896
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100897static void clk_core_unprepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700898{
899 if (!clk)
900 return;
901
902 if (WARN_ON(clk->prepare_count == 0))
903 return;
904
905 if (--clk->prepare_count > 0)
906 return;
907
908 WARN_ON(clk->enable_count > 0);
909
910 if (clk->ops->unprepare)
911 clk->ops->unprepare(clk->hw);
912
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100913 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700914}
915
916/**
917 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200918 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700919 *
920 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
921 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
922 * if the operation may sleep. One example is a clk which is accessed over
923 * I2c. In the complex case a clk gate operation may require a fast and a slow
924 * part. It is this reason that clk_unprepare and clk_disable are not mutually
925 * exclusive. In fact clk_disable must be called before clk_unprepare.
926 */
927void clk_unprepare(struct clk *clk)
928{
Stephen Boyd63589e92014-03-26 16:06:37 -0700929 if (IS_ERR_OR_NULL(clk))
930 return;
931
Mike Turquetteeab89f62013-03-28 13:59:01 -0700932 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100933 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700934 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700935}
936EXPORT_SYMBOL_GPL(clk_unprepare);
937
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100938static int clk_core_prepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700939{
940 int ret = 0;
941
942 if (!clk)
943 return 0;
944
945 if (clk->prepare_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100946 ret = clk_core_prepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700947 if (ret)
948 return ret;
949
950 if (clk->ops->prepare) {
951 ret = clk->ops->prepare(clk->hw);
952 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100953 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700954 return ret;
955 }
956 }
957 }
958
959 clk->prepare_count++;
960
961 return 0;
962}
963
964/**
965 * clk_prepare - prepare a clock source
966 * @clk: the clk being prepared
967 *
968 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
969 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
970 * operation may sleep. One example is a clk which is accessed over I2c. In
971 * the complex case a clk ungate operation may require a fast and a slow part.
972 * It is this reason that clk_prepare and clk_enable are not mutually
973 * exclusive. In fact clk_prepare must be called before clk_enable.
974 * Returns 0 on success, -EERROR otherwise.
975 */
976int clk_prepare(struct clk *clk)
977{
978 int ret;
979
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100980 if (!clk)
981 return 0;
982
Mike Turquetteeab89f62013-03-28 13:59:01 -0700983 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100984 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700985 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700986
987 return ret;
988}
989EXPORT_SYMBOL_GPL(clk_prepare);
990
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100991static void clk_core_disable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700992{
993 if (!clk)
994 return;
995
996 if (WARN_ON(clk->enable_count == 0))
997 return;
998
999 if (--clk->enable_count > 0)
1000 return;
1001
1002 if (clk->ops->disable)
1003 clk->ops->disable(clk->hw);
1004
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001005 clk_core_disable(clk->parent);
1006}
1007
1008static void __clk_disable(struct clk *clk)
1009{
1010 if (!clk)
1011 return;
1012
1013 clk_core_disable(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001014}
1015
1016/**
1017 * clk_disable - gate a clock
1018 * @clk: the clk being gated
1019 *
1020 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1021 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1022 * clk if the operation is fast and will never sleep. One example is a
1023 * SoC-internal clk which is controlled via simple register writes. In the
1024 * complex case a clk gate operation may require a fast and a slow part. It is
1025 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1026 * In fact clk_disable must be called before clk_unprepare.
1027 */
1028void clk_disable(struct clk *clk)
1029{
1030 unsigned long flags;
1031
Stephen Boyd63589e92014-03-26 16:06:37 -07001032 if (IS_ERR_OR_NULL(clk))
1033 return;
1034
Mike Turquetteeab89f62013-03-28 13:59:01 -07001035 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001036 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001037 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001038}
1039EXPORT_SYMBOL_GPL(clk_disable);
1040
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001041static int clk_core_enable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001042{
1043 int ret = 0;
1044
1045 if (!clk)
1046 return 0;
1047
1048 if (WARN_ON(clk->prepare_count == 0))
1049 return -ESHUTDOWN;
1050
1051 if (clk->enable_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001052 ret = clk_core_enable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001053
1054 if (ret)
1055 return ret;
1056
1057 if (clk->ops->enable) {
1058 ret = clk->ops->enable(clk->hw);
1059 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001060 clk_core_disable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001061 return ret;
1062 }
1063 }
1064 }
1065
1066 clk->enable_count++;
1067 return 0;
1068}
1069
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001070static int __clk_enable(struct clk *clk)
1071{
1072 if (!clk)
1073 return 0;
1074
1075 return clk_core_enable(clk->core);
1076}
1077
Mike Turquetteb24764902012-03-15 23:11:19 -07001078/**
1079 * clk_enable - ungate a clock
1080 * @clk: the clk being ungated
1081 *
1082 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1083 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1084 * if the operation will never sleep. One example is a SoC-internal clk which
1085 * is controlled via simple register writes. In the complex case a clk ungate
1086 * operation may require a fast and a slow part. It is this reason that
1087 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1088 * must be called before clk_enable. Returns 0 on success, -EERROR
1089 * otherwise.
1090 */
1091int clk_enable(struct clk *clk)
1092{
1093 unsigned long flags;
1094 int ret;
1095
Mike Turquetteeab89f62013-03-28 13:59:01 -07001096 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001097 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001098 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001099
1100 return ret;
1101}
1102EXPORT_SYMBOL_GPL(clk_enable);
1103
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001104static unsigned long clk_core_round_rate_nolock(struct clk_core *clk,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001105 unsigned long rate,
1106 unsigned long min_rate,
1107 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001108{
Shawn Guo81536e02012-04-12 20:50:17 +08001109 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001110 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001111 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001112
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001113 lockdep_assert_held(&prepare_lock);
1114
Mike Turquetteb24764902012-03-15 23:11:19 -07001115 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001116 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001117
James Hogan71472c02013-07-29 12:25:00 +01001118 parent = clk->parent;
1119 if (parent)
1120 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001121
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001122 if (clk->ops->determine_rate) {
1123 parent_hw = parent ? parent->hw : NULL;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001124 return clk->ops->determine_rate(clk->hw, rate,
1125 min_rate, max_rate,
1126 &parent_rate, &parent_hw);
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001127 } else if (clk->ops->round_rate)
James Hogan71472c02013-07-29 12:25:00 +01001128 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
1129 else if (clk->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001130 return clk_core_round_rate_nolock(clk->parent, rate, min_rate,
1131 max_rate);
James Hogan71472c02013-07-29 12:25:00 +01001132 else
1133 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001134}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001135
1136/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001137 * __clk_determine_rate - get the closest rate actually supported by a clock
1138 * @hw: determine the rate of this clock
1139 * @rate: target rate
1140 * @min_rate: returned rate must be greater than this rate
1141 * @max_rate: returned rate must be less than this rate
1142 *
1143 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
1144 * .determine_rate.
1145 */
1146unsigned long __clk_determine_rate(struct clk_hw *hw,
1147 unsigned long rate,
1148 unsigned long min_rate,
1149 unsigned long max_rate)
1150{
1151 if (!hw)
1152 return 0;
1153
1154 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
1155}
1156EXPORT_SYMBOL_GPL(__clk_determine_rate);
1157
1158/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001159 * __clk_round_rate - round the given rate for a clk
1160 * @clk: round the rate of this clock
1161 * @rate: the rate which is to be rounded
1162 *
1163 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1164 */
1165unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1166{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001167 unsigned long min_rate;
1168 unsigned long max_rate;
1169
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001170 if (!clk)
1171 return 0;
1172
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001173 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
1174
1175 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001176}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001177EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001178
1179/**
1180 * clk_round_rate - round the given rate for a clk
1181 * @clk: the clk for which we are rounding a rate
1182 * @rate: the rate which is to be rounded
1183 *
1184 * Takes in a rate as input and rounds it to a rate that the clk can actually
1185 * use which is then returned. If clk doesn't support round_rate operation
1186 * then the parent rate is returned.
1187 */
1188long clk_round_rate(struct clk *clk, unsigned long rate)
1189{
1190 unsigned long ret;
1191
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001192 if (!clk)
1193 return 0;
1194
Mike Turquetteeab89f62013-03-28 13:59:01 -07001195 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001196 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001197 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001198
1199 return ret;
1200}
1201EXPORT_SYMBOL_GPL(clk_round_rate);
1202
1203/**
1204 * __clk_notify - call clk notifier chain
1205 * @clk: struct clk * that is changing rate
1206 * @msg: clk notifier type (see include/linux/clk.h)
1207 * @old_rate: old clk rate
1208 * @new_rate: new clk rate
1209 *
1210 * Triggers a notifier call chain on the clk rate-change notification
1211 * for 'clk'. Passes a pointer to the struct clk and the previous
1212 * and current rates to the notifier callback. Intended to be called by
1213 * internal clock code only. Returns NOTIFY_DONE from the last driver
1214 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1215 * a driver returns that.
1216 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001217static int __clk_notify(struct clk_core *clk, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001218 unsigned long old_rate, unsigned long new_rate)
1219{
1220 struct clk_notifier *cn;
1221 struct clk_notifier_data cnd;
1222 int ret = NOTIFY_DONE;
1223
Mike Turquetteb24764902012-03-15 23:11:19 -07001224 cnd.old_rate = old_rate;
1225 cnd.new_rate = new_rate;
1226
1227 list_for_each_entry(cn, &clk_notifier_list, node) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001228 if (cn->clk->core == clk) {
1229 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001230 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1231 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001232 }
1233 }
1234
1235 return ret;
1236}
1237
1238/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001239 * __clk_recalc_accuracies
1240 * @clk: first clk in the subtree
1241 *
1242 * Walks the subtree of clks starting with clk and recalculates accuracies as
1243 * it goes. Note that if a clk does not implement the .recalc_accuracy
1244 * callback then it is assumed that the clock will take on the accuracy of it's
1245 * parent.
1246 *
1247 * Caller must hold prepare_lock.
1248 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001249static void __clk_recalc_accuracies(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001250{
1251 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001252 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001253
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001254 lockdep_assert_held(&prepare_lock);
1255
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001256 if (clk->parent)
1257 parent_accuracy = clk->parent->accuracy;
1258
1259 if (clk->ops->recalc_accuracy)
1260 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1261 parent_accuracy);
1262 else
1263 clk->accuracy = parent_accuracy;
1264
1265 hlist_for_each_entry(child, &clk->children, child_node)
1266 __clk_recalc_accuracies(child);
1267}
1268
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001269static long clk_core_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001270{
1271 unsigned long accuracy;
1272
1273 clk_prepare_lock();
1274 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1275 __clk_recalc_accuracies(clk);
1276
1277 accuracy = __clk_get_accuracy(clk);
1278 clk_prepare_unlock();
1279
1280 return accuracy;
1281}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001282
1283/**
1284 * clk_get_accuracy - return the accuracy of clk
1285 * @clk: the clk whose accuracy is being returned
1286 *
1287 * Simply returns the cached accuracy of the clk, unless
1288 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1289 * issued.
1290 * If clk is NULL then returns 0.
1291 */
1292long clk_get_accuracy(struct clk *clk)
1293{
1294 if (!clk)
1295 return 0;
1296
1297 return clk_core_get_accuracy(clk->core);
1298}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001299EXPORT_SYMBOL_GPL(clk_get_accuracy);
1300
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001301static unsigned long clk_recalc(struct clk_core *clk,
1302 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001303{
1304 if (clk->ops->recalc_rate)
1305 return clk->ops->recalc_rate(clk->hw, parent_rate);
1306 return parent_rate;
1307}
1308
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001309/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001310 * __clk_recalc_rates
1311 * @clk: first clk in the subtree
1312 * @msg: notification type (see include/linux/clk.h)
1313 *
1314 * Walks the subtree of clks starting with clk and recalculates rates as it
1315 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001316 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001317 *
1318 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1319 * if necessary.
1320 *
1321 * Caller must hold prepare_lock.
1322 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001323static void __clk_recalc_rates(struct clk_core *clk, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001324{
1325 unsigned long old_rate;
1326 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001327 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001328
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001329 lockdep_assert_held(&prepare_lock);
1330
Mike Turquetteb24764902012-03-15 23:11:19 -07001331 old_rate = clk->rate;
1332
1333 if (clk->parent)
1334 parent_rate = clk->parent->rate;
1335
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001336 clk->rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001337
1338 /*
1339 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1340 * & ABORT_RATE_CHANGE notifiers
1341 */
1342 if (clk->notifier_count && msg)
1343 __clk_notify(clk, msg, old_rate, clk->rate);
1344
Sasha Levinb67bfe02013-02-27 17:06:00 -08001345 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001346 __clk_recalc_rates(child, msg);
1347}
1348
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001349static unsigned long clk_core_get_rate(struct clk_core *clk)
1350{
1351 unsigned long rate;
1352
1353 clk_prepare_lock();
1354
1355 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1356 __clk_recalc_rates(clk, 0);
1357
1358 rate = clk_core_get_rate_nolock(clk);
1359 clk_prepare_unlock();
1360
1361 return rate;
1362}
1363EXPORT_SYMBOL_GPL(clk_core_get_rate);
1364
Mike Turquetteb24764902012-03-15 23:11:19 -07001365/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001366 * clk_get_rate - return the rate of clk
1367 * @clk: the clk whose rate is being returned
1368 *
1369 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1370 * is set, which means a recalc_rate will be issued.
1371 * If clk is NULL then returns 0.
1372 */
1373unsigned long clk_get_rate(struct clk *clk)
1374{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001375 if (!clk)
1376 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001377
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001378 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001379}
1380EXPORT_SYMBOL_GPL(clk_get_rate);
1381
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001382static int clk_fetch_parent_index(struct clk_core *clk,
1383 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001384{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001385 int i;
James Hogan4935b222013-07-29 12:24:59 +01001386
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001387 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001388 clk->parents = kcalloc(clk->num_parents,
1389 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001390 if (!clk->parents)
1391 return -ENOMEM;
1392 }
James Hogan4935b222013-07-29 12:24:59 +01001393
1394 /*
1395 * find index of new parent clock using cached parent ptrs,
1396 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001397 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001398 */
1399 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001400 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001401 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001402
1403 if (clk->parents[i])
1404 continue;
1405
1406 if (!strcmp(clk->parent_names[i], parent->name)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001407 clk->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001408 return i;
James Hogan4935b222013-07-29 12:24:59 +01001409 }
1410 }
1411
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001412 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001413}
1414
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001415static void clk_reparent(struct clk_core *clk, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001416{
1417 hlist_del(&clk->child_node);
1418
James Hogan903efc52013-08-29 12:10:51 +01001419 if (new_parent) {
1420 /* avoid duplicate POST_RATE_CHANGE notifications */
1421 if (new_parent->new_child == clk)
1422 new_parent->new_child = NULL;
1423
James Hogan4935b222013-07-29 12:24:59 +01001424 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001425 } else {
James Hogan4935b222013-07-29 12:24:59 +01001426 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001427 }
James Hogan4935b222013-07-29 12:24:59 +01001428
1429 clk->parent = new_parent;
1430}
1431
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001432static struct clk_core *__clk_set_parent_before(struct clk_core *clk,
1433 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001434{
1435 unsigned long flags;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001436 struct clk_core *old_parent = clk->parent;
James Hogan4935b222013-07-29 12:24:59 +01001437
1438 /*
1439 * Migrate prepare state between parents and prevent race with
1440 * clk_enable().
1441 *
1442 * If the clock is not prepared, then a race with
1443 * clk_enable/disable() is impossible since we already have the
1444 * prepare lock (future calls to clk_enable() need to be preceded by
1445 * a clk_prepare()).
1446 *
1447 * If the clock is prepared, migrate the prepared state to the new
1448 * parent and also protect against a race with clk_enable() by
1449 * forcing the clock and the new parent on. This ensures that all
1450 * future calls to clk_enable() are practically NOPs with respect to
1451 * hardware and software states.
1452 *
1453 * See also: Comment for clk_set_parent() below.
1454 */
1455 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001456 clk_core_prepare(parent);
1457 clk_core_enable(parent);
1458 clk_core_enable(clk);
James Hogan4935b222013-07-29 12:24:59 +01001459 }
1460
1461 /* update the clk tree topology */
1462 flags = clk_enable_lock();
1463 clk_reparent(clk, parent);
1464 clk_enable_unlock(flags);
1465
Stephen Boyd3fa22522014-01-15 10:47:22 -08001466 return old_parent;
1467}
1468
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001469static void __clk_set_parent_after(struct clk_core *core,
1470 struct clk_core *parent,
1471 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001472{
1473 /*
1474 * Finish the migration of prepare state and undo the changes done
1475 * for preventing a race with clk_enable().
1476 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001477 if (core->prepare_count) {
1478 clk_core_disable(core);
1479 clk_core_disable(old_parent);
1480 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001481 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001482}
1483
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001484static int __clk_set_parent(struct clk_core *clk, struct clk_core *parent,
1485 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001486{
1487 unsigned long flags;
1488 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001489 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001490
1491 old_parent = __clk_set_parent_before(clk, parent);
1492
James Hogan4935b222013-07-29 12:24:59 +01001493 /* change clock input source */
1494 if (parent && clk->ops->set_parent)
1495 ret = clk->ops->set_parent(clk->hw, p_index);
1496
1497 if (ret) {
1498 flags = clk_enable_lock();
1499 clk_reparent(clk, old_parent);
1500 clk_enable_unlock(flags);
1501
1502 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001503 clk_core_disable(clk);
1504 clk_core_disable(parent);
1505 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001506 }
1507 return ret;
1508 }
1509
Stephen Boyd3fa22522014-01-15 10:47:22 -08001510 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001511
James Hogan4935b222013-07-29 12:24:59 +01001512 return 0;
1513}
1514
Ulf Hanssona093bde2012-08-31 14:21:28 +02001515/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001516 * __clk_speculate_rates
1517 * @clk: first clk in the subtree
1518 * @parent_rate: the "future" rate of clk's parent
1519 *
1520 * Walks the subtree of clks starting with clk, speculating rates as it
1521 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1522 *
1523 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1524 * pre-rate change notifications and returns early if no clks in the
1525 * subtree have subscribed to the notifications. Note that if a clk does not
1526 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001527 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001528 *
1529 * Caller must hold prepare_lock.
1530 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001531static int __clk_speculate_rates(struct clk_core *clk,
1532 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001533{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001534 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001535 unsigned long new_rate;
1536 int ret = NOTIFY_DONE;
1537
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001538 lockdep_assert_held(&prepare_lock);
1539
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001540 new_rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001541
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001542 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001543 if (clk->notifier_count)
1544 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1545
Mike Turquette86bcfa22014-02-24 16:08:41 -08001546 if (ret & NOTIFY_STOP_MASK) {
1547 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1548 __func__, clk->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001549 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001550 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001551
Sasha Levinb67bfe02013-02-27 17:06:00 -08001552 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001553 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001554 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001555 break;
1556 }
1557
1558out:
1559 return ret;
1560}
1561
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001562static void clk_calc_subtree(struct clk_core *clk, unsigned long new_rate,
1563 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001564{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001565 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001566
1567 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001568 clk->new_parent = new_parent;
1569 clk->new_parent_index = p_index;
1570 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1571 clk->new_child = NULL;
1572 if (new_parent && new_parent != clk->parent)
1573 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001574
Sasha Levinb67bfe02013-02-27 17:06:00 -08001575 hlist_for_each_entry(child, &clk->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001576 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001577 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001578 }
1579}
1580
1581/*
1582 * calculate the new rates returning the topmost clock that has to be
1583 * changed.
1584 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001585static struct clk_core *clk_calc_new_rates(struct clk_core *clk,
1586 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001587{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001588 struct clk_core *top = clk;
1589 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001590 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001591 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001592 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001593 unsigned long min_rate;
1594 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001595 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001596
Mike Turquette7452b212012-03-26 14:45:36 -07001597 /* sanity */
1598 if (IS_ERR_OR_NULL(clk))
1599 return NULL;
1600
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001601 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001602 parent = old_parent = clk->parent;
1603 if (parent)
1604 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001605
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001606 clk_core_get_boundaries(clk, &min_rate, &max_rate);
1607
James Hogan71472c02013-07-29 12:25:00 +01001608 /* find the closest rate and parent clk/rate */
1609 if (clk->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001610 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001611 new_rate = clk->ops->determine_rate(clk->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001612 min_rate,
1613 max_rate,
James Hogan71472c02013-07-29 12:25:00 +01001614 &best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001615 &parent_hw);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001616 parent = parent_hw ? parent_hw->core : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001617 } else if (clk->ops->round_rate) {
1618 new_rate = clk->ops->round_rate(clk->hw, rate,
1619 &best_parent_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001620 if (new_rate < min_rate || new_rate > max_rate)
1621 return NULL;
James Hogan71472c02013-07-29 12:25:00 +01001622 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1623 /* pass-through clock without adjustable parent */
1624 clk->new_rate = clk->rate;
1625 return NULL;
1626 } else {
1627 /* pass-through clock with adjustable parent */
1628 top = clk_calc_new_rates(parent, rate);
1629 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001630 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001631 }
1632
James Hogan71472c02013-07-29 12:25:00 +01001633 /* some clocks must be gated to change parent */
1634 if (parent != old_parent &&
1635 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1636 pr_debug("%s: %s not gated but wants to reparent\n",
1637 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001638 return NULL;
1639 }
1640
James Hogan71472c02013-07-29 12:25:00 +01001641 /* try finding the new parent index */
Stephen Boyd4526e7b2014-12-22 11:26:42 -08001642 if (parent && clk->num_parents > 1) {
James Hogan71472c02013-07-29 12:25:00 +01001643 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001644 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001645 pr_debug("%s: clk %s can not be parent of clk %s\n",
1646 __func__, parent->name, clk->name);
1647 return NULL;
1648 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001649 }
1650
James Hogan71472c02013-07-29 12:25:00 +01001651 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1652 best_parent_rate != parent->rate)
1653 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001654
1655out:
James Hogan71472c02013-07-29 12:25:00 +01001656 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001657
1658 return top;
1659}
1660
1661/*
1662 * Notify about rate changes in a subtree. Always walk down the whole tree
1663 * so that in case of an error we can walk down the whole tree again and
1664 * abort the change.
1665 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001666static struct clk_core *clk_propagate_rate_change(struct clk_core *clk,
1667 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001668{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001669 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001670 int ret = NOTIFY_DONE;
1671
1672 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301673 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001674
1675 if (clk->notifier_count) {
1676 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001677 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001678 fail_clk = clk;
1679 }
1680
Sasha Levinb67bfe02013-02-27 17:06:00 -08001681 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001682 /* Skip children who will be reparented to another clock */
1683 if (child->new_parent && child->new_parent != clk)
1684 continue;
1685 tmp_clk = clk_propagate_rate_change(child, event);
1686 if (tmp_clk)
1687 fail_clk = tmp_clk;
1688 }
1689
1690 /* handle the new child who might not be in clk->children yet */
1691 if (clk->new_child) {
1692 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1693 if (tmp_clk)
1694 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001695 }
1696
1697 return fail_clk;
1698}
1699
1700/*
1701 * walk down a subtree and set the new rates notifying the rate
1702 * change on the way
1703 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001704static void clk_change_rate(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001705{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001706 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001707 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001708 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001709 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001710 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001711 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001712
1713 old_rate = clk->rate;
1714
Stephen Boyd3fa22522014-01-15 10:47:22 -08001715 if (clk->new_parent)
1716 best_parent_rate = clk->new_parent->rate;
1717 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001718 best_parent_rate = clk->parent->rate;
1719
Stephen Boyd3fa22522014-01-15 10:47:22 -08001720 if (clk->new_parent && clk->new_parent != clk->parent) {
1721 old_parent = __clk_set_parent_before(clk, clk->new_parent);
1722
1723 if (clk->ops->set_rate_and_parent) {
1724 skip_set_rate = true;
1725 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1726 best_parent_rate,
1727 clk->new_parent_index);
1728 } else if (clk->ops->set_parent) {
1729 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1730 }
1731
1732 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1733 }
1734
1735 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001736 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001737
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001738 clk->rate = clk_recalc(clk, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001739
1740 if (clk->notifier_count && old_rate != clk->rate)
1741 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1742
Tero Kristo067bb172014-08-21 16:47:45 +03001743 /*
1744 * Use safe iteration, as change_rate can actually swap parents
1745 * for certain clock types.
1746 */
1747 hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001748 /* Skip children who will be reparented to another clock */
1749 if (child->new_parent && child->new_parent != clk)
1750 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001751 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001752 }
1753
1754 /* handle the new child who might not be in clk->children yet */
1755 if (clk->new_child)
1756 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001757}
1758
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001759static int clk_core_set_rate_nolock(struct clk_core *clk,
1760 unsigned long req_rate)
1761{
1762 struct clk_core *top, *fail_clk;
1763 unsigned long rate = req_rate;
1764 int ret = 0;
1765
1766 if (!clk)
1767 return 0;
1768
1769 /* bail early if nothing to do */
1770 if (rate == clk_core_get_rate_nolock(clk))
1771 return 0;
1772
1773 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count)
1774 return -EBUSY;
1775
1776 /* calculate new rates and get the topmost changed clock */
1777 top = clk_calc_new_rates(clk, rate);
1778 if (!top)
1779 return -EINVAL;
1780
1781 /* notify that we are about to change rates */
1782 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1783 if (fail_clk) {
1784 pr_debug("%s: failed to set %s rate\n", __func__,
1785 fail_clk->name);
1786 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1787 return -EBUSY;
1788 }
1789
1790 /* change the rates */
1791 clk_change_rate(top);
1792
1793 clk->req_rate = req_rate;
1794
1795 return ret;
1796}
1797
Mike Turquetteb24764902012-03-15 23:11:19 -07001798/**
1799 * clk_set_rate - specify a new rate for clk
1800 * @clk: the clk whose rate is being changed
1801 * @rate: the new rate for clk
1802 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001803 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001804 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001805 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1806 * propagate up to clk's parent; whether or not this happens depends on the
1807 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1808 * after calling .round_rate then upstream parent propagation is ignored. If
1809 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001810 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001811 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1812 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001813 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001814 * Rate changes are accomplished via tree traversal that also recalculates the
1815 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001816 *
1817 * Returns 0 on success, -EERROR otherwise.
1818 */
1819int clk_set_rate(struct clk *clk, unsigned long rate)
1820{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001821 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001822
Mike Turquette89ac8d72013-08-21 23:58:09 -07001823 if (!clk)
1824 return 0;
1825
Mike Turquetteb24764902012-03-15 23:11:19 -07001826 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001827 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001828
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001829 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001830
Mike Turquetteeab89f62013-03-28 13:59:01 -07001831 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001832
1833 return ret;
1834}
1835EXPORT_SYMBOL_GPL(clk_set_rate);
1836
1837/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001838 * clk_set_rate_range - set a rate range for a clock source
1839 * @clk: clock source
1840 * @min: desired minimum clock rate in Hz, inclusive
1841 * @max: desired maximum clock rate in Hz, inclusive
1842 *
1843 * Returns success (0) or negative errno.
1844 */
1845int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1846{
1847 int ret = 0;
1848
1849 if (!clk)
1850 return 0;
1851
1852 if (min > max) {
1853 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1854 __func__, clk->core->name, clk->dev_id, clk->con_id,
1855 min, max);
1856 return -EINVAL;
1857 }
1858
1859 clk_prepare_lock();
1860
1861 if (min != clk->min_rate || max != clk->max_rate) {
1862 clk->min_rate = min;
1863 clk->max_rate = max;
1864 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1865 }
1866
1867 clk_prepare_unlock();
1868
1869 return ret;
1870}
1871EXPORT_SYMBOL_GPL(clk_set_rate_range);
1872
1873/**
1874 * clk_set_min_rate - set a minimum clock rate for a clock source
1875 * @clk: clock source
1876 * @rate: desired minimum clock rate in Hz, inclusive
1877 *
1878 * Returns success (0) or negative errno.
1879 */
1880int clk_set_min_rate(struct clk *clk, unsigned long rate)
1881{
1882 if (!clk)
1883 return 0;
1884
1885 return clk_set_rate_range(clk, rate, clk->max_rate);
1886}
1887EXPORT_SYMBOL_GPL(clk_set_min_rate);
1888
1889/**
1890 * clk_set_max_rate - set a maximum clock rate for a clock source
1891 * @clk: clock source
1892 * @rate: desired maximum clock rate in Hz, inclusive
1893 *
1894 * Returns success (0) or negative errno.
1895 */
1896int clk_set_max_rate(struct clk *clk, unsigned long rate)
1897{
1898 if (!clk)
1899 return 0;
1900
1901 return clk_set_rate_range(clk, clk->min_rate, rate);
1902}
1903EXPORT_SYMBOL_GPL(clk_set_max_rate);
1904
1905/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001906 * clk_get_parent - return the parent of a clk
1907 * @clk: the clk whose parent gets returned
1908 *
1909 * Simply returns clk->parent. Returns NULL if clk is NULL.
1910 */
1911struct clk *clk_get_parent(struct clk *clk)
1912{
1913 struct clk *parent;
1914
Mike Turquetteeab89f62013-03-28 13:59:01 -07001915 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001916 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001917 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001918
1919 return parent;
1920}
1921EXPORT_SYMBOL_GPL(clk_get_parent);
1922
1923/*
1924 * .get_parent is mandatory for clocks with multiple possible parents. It is
1925 * optional for single-parent clocks. Always call .get_parent if it is
1926 * available and WARN if it is missing for multi-parent clocks.
1927 *
1928 * For single-parent clocks without .get_parent, first check to see if the
1929 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001930 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001931 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001932static struct clk_core *__clk_init_parent(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001933{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001934 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001935 u8 index;
1936
1937 /* handle the trivial cases */
1938
1939 if (!clk->num_parents)
1940 goto out;
1941
1942 if (clk->num_parents == 1) {
1943 if (IS_ERR_OR_NULL(clk->parent))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001944 clk->parent = clk_core_lookup(clk->parent_names[0]);
Mike Turquetteb24764902012-03-15 23:11:19 -07001945 ret = clk->parent;
1946 goto out;
1947 }
1948
1949 if (!clk->ops->get_parent) {
1950 WARN(!clk->ops->get_parent,
1951 "%s: multi-parent clocks must implement .get_parent\n",
1952 __func__);
1953 goto out;
1954 };
1955
1956 /*
1957 * Do our best to cache parent clocks in clk->parents. This prevents
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001958 * unnecessary and expensive lookups. We don't set clk->parent here;
1959 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001960 */
1961
1962 index = clk->ops->get_parent(clk->hw);
1963
1964 if (!clk->parents)
1965 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001966 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001967 GFP_KERNEL);
1968
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001969 ret = clk_core_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001970
1971out:
1972 return ret;
1973}
1974
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001975static void clk_core_reparent(struct clk_core *clk,
1976 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001977{
1978 clk_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001979 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001980 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1981}
1982
Mike Turquetteb24764902012-03-15 23:11:19 -07001983/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001984 * clk_has_parent - check if a clock is a possible parent for another
1985 * @clk: clock source
1986 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001987 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001988 * This function can be used in drivers that need to check that a clock can be
1989 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001990 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001991 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001992 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001993bool clk_has_parent(struct clk *clk, struct clk *parent)
1994{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001995 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001996 unsigned int i;
1997
1998 /* NULL clocks should be nops, so return success if either is NULL. */
1999 if (!clk || !parent)
2000 return true;
2001
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002002 core = clk->core;
2003 parent_core = parent->core;
2004
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002005 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002006 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002007 return true;
2008
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002009 for (i = 0; i < core->num_parents; i++)
2010 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002011 return true;
2012
2013 return false;
2014}
2015EXPORT_SYMBOL_GPL(clk_has_parent);
2016
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002017static int clk_core_set_parent(struct clk_core *clk, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002018{
2019 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002020 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002021 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002022
Mike Turquette89ac8d72013-08-21 23:58:09 -07002023 if (!clk)
2024 return 0;
2025
Ulf Hansson031dcc92013-04-02 23:09:38 +02002026 /* verify ops for for multi-parent clks */
2027 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07002028 return -ENOSYS;
2029
2030 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002031 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002032
2033 if (clk->parent == parent)
2034 goto out;
2035
Ulf Hansson031dcc92013-04-02 23:09:38 +02002036 /* check that we are allowed to re-parent if the clock is in use */
2037 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
2038 ret = -EBUSY;
2039 goto out;
2040 }
2041
2042 /* try finding the new parent index */
2043 if (parent) {
2044 p_index = clk_fetch_parent_index(clk, parent);
2045 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002046 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002047 pr_debug("%s: clk %s can not be parent of clk %s\n",
2048 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002049 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002050 goto out;
2051 }
2052 }
2053
Mike Turquetteb24764902012-03-15 23:11:19 -07002054 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07002055 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002056
2057 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002058 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002059 goto out;
2060
Ulf Hansson031dcc92013-04-02 23:09:38 +02002061 /* do the re-parent */
2062 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002063
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002064 /* propagate rate an accuracy recalculation accordingly */
2065 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07002066 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002067 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02002068 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002069 __clk_recalc_accuracies(clk);
2070 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002071
2072out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002073 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002074
2075 return ret;
2076}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002077
2078/**
2079 * clk_set_parent - switch the parent of a mux clk
2080 * @clk: the mux clk whose input we are switching
2081 * @parent: the new input to clk
2082 *
2083 * Re-parent clk to use parent as its new input source. If clk is in
2084 * prepared state, the clk will get enabled for the duration of this call. If
2085 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2086 * that, the reparenting is glitchy in hardware, etc), use the
2087 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2088 *
2089 * After successfully changing clk's parent clk_set_parent will update the
2090 * clk topology, sysfs topology and propagate rate recalculation via
2091 * __clk_recalc_rates.
2092 *
2093 * Returns 0 on success, -EERROR otherwise.
2094 */
2095int clk_set_parent(struct clk *clk, struct clk *parent)
2096{
2097 if (!clk)
2098 return 0;
2099
2100 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2101}
Mike Turquetteb24764902012-03-15 23:11:19 -07002102EXPORT_SYMBOL_GPL(clk_set_parent);
2103
2104/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002105 * clk_set_phase - adjust the phase shift of a clock signal
2106 * @clk: clock signal source
2107 * @degrees: number of degrees the signal is shifted
2108 *
2109 * Shifts the phase of a clock signal by the specified
2110 * degrees. Returns 0 on success, -EERROR otherwise.
2111 *
2112 * This function makes no distinction about the input or reference
2113 * signal that we adjust the clock signal phase against. For example
2114 * phase locked-loop clock signal generators we may shift phase with
2115 * respect to feedback clock signal input, but for other cases the
2116 * clock phase may be shifted with respect to some other, unspecified
2117 * signal.
2118 *
2119 * Additionally the concept of phase shift does not propagate through
2120 * the clock tree hierarchy, which sets it apart from clock rates and
2121 * clock accuracy. A parent clock phase attribute does not have an
2122 * impact on the phase attribute of a child clock.
2123 */
2124int clk_set_phase(struct clk *clk, int degrees)
2125{
Stephen Boyd08b95752015-02-02 14:09:43 -08002126 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002127
2128 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002129 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002130
2131 /* sanity check degrees */
2132 degrees %= 360;
2133 if (degrees < 0)
2134 degrees += 360;
2135
2136 clk_prepare_lock();
2137
Stephen Boyd08b95752015-02-02 14:09:43 -08002138 if (clk->core->ops->set_phase)
2139 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002140
2141 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002142 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002143
Mike Turquettee59c5372014-02-18 21:21:25 -08002144 clk_prepare_unlock();
2145
Mike Turquettee59c5372014-02-18 21:21:25 -08002146 return ret;
2147}
Maxime Ripard9767b042015-01-20 22:23:43 +01002148EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002149
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002150static int clk_core_get_phase(struct clk_core *clk)
Mike Turquettee59c5372014-02-18 21:21:25 -08002151{
2152 int ret = 0;
2153
2154 if (!clk)
2155 goto out;
2156
2157 clk_prepare_lock();
2158 ret = clk->phase;
2159 clk_prepare_unlock();
2160
2161out:
2162 return ret;
2163}
Maxime Ripard9767b042015-01-20 22:23:43 +01002164EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002165
2166/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002167 * clk_get_phase - return the phase shift of a clock signal
2168 * @clk: clock signal source
2169 *
2170 * Returns the phase shift of a clock node in degrees, otherwise returns
2171 * -EERROR.
2172 */
2173int clk_get_phase(struct clk *clk)
2174{
2175 if (!clk)
2176 return 0;
2177
2178 return clk_core_get_phase(clk->core);
2179}
Mike Turquetteb24764902012-03-15 23:11:19 -07002180
2181/**
2182 * __clk_init - initialize the data structures in a struct clk
2183 * @dev: device initializing this clk, placeholder for now
2184 * @clk: clk being initialized
2185 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002186 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002187 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002188 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002189static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002190{
Mike Turquetted1302a32012-03-29 14:30:40 -07002191 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002192 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002193 struct hlist_node *tmp2;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002194 struct clk_core *clk;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002195 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002196
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002197 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002198 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002199
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002200 clk = clk_user->core;
2201
Mike Turquetteeab89f62013-03-28 13:59:01 -07002202 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002203
2204 /* check to see if a clock with this name is already registered */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002205 if (clk_core_lookup(clk->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002206 pr_debug("%s: clk %s already initialized\n",
2207 __func__, clk->name);
2208 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002209 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002210 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002211
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002212 /* check that clk_ops are sane. See Documentation/clk.txt */
2213 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01002214 !((clk->ops->round_rate || clk->ops->determine_rate) &&
2215 clk->ops->recalc_rate)) {
2216 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002217 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002218 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002219 goto out;
2220 }
2221
2222 if (clk->ops->set_parent && !clk->ops->get_parent) {
2223 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
2224 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002225 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002226 goto out;
2227 }
2228
Stephen Boyd3fa22522014-01-15 10:47:22 -08002229 if (clk->ops->set_rate_and_parent &&
2230 !(clk->ops->set_parent && clk->ops->set_rate)) {
2231 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
2232 __func__, clk->name);
2233 ret = -EINVAL;
2234 goto out;
2235 }
2236
Mike Turquetteb24764902012-03-15 23:11:19 -07002237 /* throw a WARN if any entries in parent_names are NULL */
2238 for (i = 0; i < clk->num_parents; i++)
2239 WARN(!clk->parent_names[i],
2240 "%s: invalid NULL in %s's .parent_names\n",
2241 __func__, clk->name);
2242
2243 /*
2244 * Allocate an array of struct clk *'s to avoid unnecessary string
2245 * look-ups of clk's possible parents. This can fail for clocks passed
2246 * in to clk_init during early boot; thus any access to clk->parents[]
2247 * must always check for a NULL pointer and try to populate it if
2248 * necessary.
2249 *
2250 * If clk->parents is not NULL we skip this entire block. This allows
2251 * for clock drivers to statically initialize clk->parents.
2252 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05302253 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002254 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
2255 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002256 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002257 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002258 * clk_init'd; thus any access to clk->parents[] must check
2259 * for a NULL pointer. We can always perform lazy lookups for
2260 * missing parents later on.
2261 */
2262 if (clk->parents)
2263 for (i = 0; i < clk->num_parents; i++)
2264 clk->parents[i] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002265 clk_core_lookup(clk->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002266 }
2267
2268 clk->parent = __clk_init_parent(clk);
2269
2270 /*
2271 * Populate clk->parent if parent has already been __clk_init'd. If
2272 * parent has not yet been __clk_init'd then place clk in the orphan
2273 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2274 * clk list.
2275 *
2276 * Every time a new clk is clk_init'd then we walk the list of orphan
2277 * clocks and re-parent any that are children of the clock currently
2278 * being clk_init'd.
2279 */
2280 if (clk->parent)
2281 hlist_add_head(&clk->child_node,
2282 &clk->parent->children);
2283 else if (clk->flags & CLK_IS_ROOT)
2284 hlist_add_head(&clk->child_node, &clk_root_list);
2285 else
2286 hlist_add_head(&clk->child_node, &clk_orphan_list);
2287
2288 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002289 * Set clk's accuracy. The preferred method is to use
2290 * .recalc_accuracy. For simple clocks and lazy developers the default
2291 * fallback is to use the parent's accuracy. If a clock doesn't have a
2292 * parent (or is orphaned) then accuracy is set to zero (perfect
2293 * clock).
2294 */
2295 if (clk->ops->recalc_accuracy)
2296 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
2297 __clk_get_accuracy(clk->parent));
2298 else if (clk->parent)
2299 clk->accuracy = clk->parent->accuracy;
2300 else
2301 clk->accuracy = 0;
2302
2303 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002304 * Set clk's phase.
2305 * Since a phase is by definition relative to its parent, just
2306 * query the current clock phase, or just assume it's in phase.
2307 */
2308 if (clk->ops->get_phase)
2309 clk->phase = clk->ops->get_phase(clk->hw);
2310 else
2311 clk->phase = 0;
2312
2313 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002314 * Set clk's rate. The preferred method is to use .recalc_rate. For
2315 * simple clocks and lazy developers the default fallback is to use the
2316 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2317 * then rate is set to zero.
2318 */
2319 if (clk->ops->recalc_rate)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002320 rate = clk->ops->recalc_rate(clk->hw,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002321 clk_core_get_rate_nolock(clk->parent));
Mike Turquetteb24764902012-03-15 23:11:19 -07002322 else if (clk->parent)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002323 rate = clk->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002324 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002325 rate = 0;
2326 clk->rate = clk->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002327
2328 /*
2329 * walk the list of orphan clocks and reparent any that are children of
2330 * this clock
2331 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002332 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002333 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002334 i = orphan->ops->get_parent(orphan->hw);
2335 if (!strcmp(clk->name, orphan->parent_names[i]))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002336 clk_core_reparent(orphan, clk);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002337 continue;
2338 }
2339
Mike Turquetteb24764902012-03-15 23:11:19 -07002340 for (i = 0; i < orphan->num_parents; i++)
2341 if (!strcmp(clk->name, orphan->parent_names[i])) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002342 clk_core_reparent(orphan, clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07002343 break;
2344 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002345 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002346
2347 /*
2348 * optional platform-specific magic
2349 *
2350 * The .init callback is not used by any of the basic clock types, but
2351 * exists for weird hardware that must perform initialization magic.
2352 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002353 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002354 */
2355 if (clk->ops->init)
2356 clk->ops->init(clk->hw);
2357
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002358 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002359out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002360 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002361
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002362 if (!ret)
2363 clk_debug_register(clk);
2364
Mike Turquetted1302a32012-03-29 14:30:40 -07002365 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002366}
2367
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002368struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2369 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002370{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002371 struct clk *clk;
2372
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002373 /* This is to allow this function to be chained to others */
2374 if (!hw || IS_ERR(hw))
2375 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002376
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002377 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2378 if (!clk)
2379 return ERR_PTR(-ENOMEM);
2380
2381 clk->core = hw->core;
2382 clk->dev_id = dev_id;
2383 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002384 clk->max_rate = ULONG_MAX;
2385
2386 clk_prepare_lock();
2387 hlist_add_head(&clk->child_node, &hw->core->clks);
2388 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002389
2390 return clk;
2391}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002392
Stephen Boyd73e0e492015-02-06 11:42:43 -08002393void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002394{
2395 clk_prepare_lock();
2396 hlist_del(&clk->child_node);
2397 clk_prepare_unlock();
2398
2399 kfree(clk);
2400}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002401
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002402/**
2403 * clk_register - allocate a new clock, register it and return an opaque cookie
2404 * @dev: device that is registering this clock
2405 * @hw: link to hardware-specific clock data
2406 *
2407 * clk_register is the primary interface for populating the clock tree with new
2408 * clock nodes. It returns a pointer to the newly allocated struct clk which
2409 * cannot be dereferenced by driver code but may be used in conjuction with the
2410 * rest of the clock API. In the event of an error clk_register will return an
2411 * error code; drivers must test for an error code after calling clk_register.
2412 */
2413struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002414{
Mike Turquetted1302a32012-03-29 14:30:40 -07002415 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002416 struct clk_core *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002417
2418 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2419 if (!clk) {
2420 pr_err("%s: could not allocate clk\n", __func__);
2421 ret = -ENOMEM;
2422 goto fail_out;
2423 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002424
Andrzej Hajda612936f2015-02-13 14:36:33 -08002425 clk->name = kstrdup_const(hw->init->name, GFP_KERNEL);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002426 if (!clk->name) {
2427 pr_err("%s: could not allocate clk->name\n", __func__);
2428 ret = -ENOMEM;
2429 goto fail_name;
2430 }
2431 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002432 if (dev && dev->driver)
2433 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07002434 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002435 clk->flags = hw->init->flags;
2436 clk->num_parents = hw->init->num_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002437 hw->core = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002438
Mike Turquetted1302a32012-03-29 14:30:40 -07002439 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002440 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
2441 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002442
Mike Turquetted1302a32012-03-29 14:30:40 -07002443 if (!clk->parent_names) {
2444 pr_err("%s: could not allocate clk->parent_names\n", __func__);
2445 ret = -ENOMEM;
2446 goto fail_parent_names;
2447 }
2448
2449
2450 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002451 for (i = 0; i < clk->num_parents; i++) {
Andrzej Hajda612936f2015-02-13 14:36:33 -08002452 clk->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002453 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002454 if (!clk->parent_names[i]) {
2455 pr_err("%s: could not copy parent_names\n", __func__);
2456 ret = -ENOMEM;
2457 goto fail_parent_names_copy;
2458 }
2459 }
2460
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002461 INIT_HLIST_HEAD(&clk->clks);
2462
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002463 hw->clk = __clk_create_clk(hw, NULL, NULL);
2464 if (IS_ERR(hw->clk)) {
2465 pr_err("%s: could not allocate per-user clk\n", __func__);
2466 ret = PTR_ERR(hw->clk);
2467 goto fail_parent_names_copy;
2468 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002469
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002470 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002471 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002472 return hw->clk;
2473
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002474 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002475 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002476
2477fail_parent_names_copy:
2478 while (--i >= 0)
Andrzej Hajda612936f2015-02-13 14:36:33 -08002479 kfree_const(clk->parent_names[i]);
Mike Turquetted1302a32012-03-29 14:30:40 -07002480 kfree(clk->parent_names);
2481fail_parent_names:
Andrzej Hajda612936f2015-02-13 14:36:33 -08002482 kfree_const(clk->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002483fail_name:
Mike Turquetted1302a32012-03-29 14:30:40 -07002484 kfree(clk);
2485fail_out:
2486 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002487}
2488EXPORT_SYMBOL_GPL(clk_register);
2489
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002490/*
2491 * Free memory allocated for a clock.
2492 * Caller must hold prepare_lock.
2493 */
2494static void __clk_release(struct kref *ref)
2495{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002496 struct clk_core *clk = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002497 int i = clk->num_parents;
2498
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002499 lockdep_assert_held(&prepare_lock);
2500
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002501 kfree(clk->parents);
2502 while (--i >= 0)
Andrzej Hajda612936f2015-02-13 14:36:33 -08002503 kfree_const(clk->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002504
2505 kfree(clk->parent_names);
Andrzej Hajda612936f2015-02-13 14:36:33 -08002506 kfree_const(clk->name);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002507 kfree(clk);
2508}
2509
2510/*
2511 * Empty clk_ops for unregistered clocks. These are used temporarily
2512 * after clk_unregister() was called on a clock and until last clock
2513 * consumer calls clk_put() and the struct clk object is freed.
2514 */
2515static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2516{
2517 return -ENXIO;
2518}
2519
2520static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2521{
2522 WARN_ON_ONCE(1);
2523}
2524
2525static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2526 unsigned long parent_rate)
2527{
2528 return -ENXIO;
2529}
2530
2531static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2532{
2533 return -ENXIO;
2534}
2535
2536static const struct clk_ops clk_nodrv_ops = {
2537 .enable = clk_nodrv_prepare_enable,
2538 .disable = clk_nodrv_disable_unprepare,
2539 .prepare = clk_nodrv_prepare_enable,
2540 .unprepare = clk_nodrv_disable_unprepare,
2541 .set_rate = clk_nodrv_set_rate,
2542 .set_parent = clk_nodrv_set_parent,
2543};
2544
Mark Brown1df5c932012-04-18 09:07:12 +01002545/**
2546 * clk_unregister - unregister a currently registered clock
2547 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002548 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002549void clk_unregister(struct clk *clk)
2550{
2551 unsigned long flags;
2552
Stephen Boyd6314b672014-09-04 23:37:49 -07002553 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2554 return;
2555
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002556 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002557
2558 clk_prepare_lock();
2559
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002560 if (clk->core->ops == &clk_nodrv_ops) {
2561 pr_err("%s: unregistered clock: %s\n", __func__,
2562 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002563 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002564 }
2565 /*
2566 * Assign empty clock ops for consumers that might still hold
2567 * a reference to this clock.
2568 */
2569 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002570 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002571 clk_enable_unlock(flags);
2572
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002573 if (!hlist_empty(&clk->core->children)) {
2574 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002575 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002576
2577 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002578 hlist_for_each_entry_safe(child, t, &clk->core->children,
2579 child_node)
2580 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002581 }
2582
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002583 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002584
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002585 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002586 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002587 __func__, clk->core->name);
2588 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002589
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002590 clk_prepare_unlock();
2591}
Mark Brown1df5c932012-04-18 09:07:12 +01002592EXPORT_SYMBOL_GPL(clk_unregister);
2593
Stephen Boyd46c87732012-09-24 13:38:04 -07002594static void devm_clk_release(struct device *dev, void *res)
2595{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002596 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002597}
2598
2599/**
2600 * devm_clk_register - resource managed clk_register()
2601 * @dev: device that is registering this clock
2602 * @hw: link to hardware-specific clock data
2603 *
2604 * Managed clk_register(). Clocks returned from this function are
2605 * automatically clk_unregister()ed on driver detach. See clk_register() for
2606 * more information.
2607 */
2608struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2609{
2610 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002611 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002612
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002613 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2614 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002615 return ERR_PTR(-ENOMEM);
2616
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002617 clk = clk_register(dev, hw);
2618 if (!IS_ERR(clk)) {
2619 *clkp = clk;
2620 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002621 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002622 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002623 }
2624
2625 return clk;
2626}
2627EXPORT_SYMBOL_GPL(devm_clk_register);
2628
2629static int devm_clk_match(struct device *dev, void *res, void *data)
2630{
2631 struct clk *c = res;
2632 if (WARN_ON(!c))
2633 return 0;
2634 return c == data;
2635}
2636
2637/**
2638 * devm_clk_unregister - resource managed clk_unregister()
2639 * @clk: clock to unregister
2640 *
2641 * Deallocate a clock allocated with devm_clk_register(). Normally
2642 * this function will not need to be called and the resource management
2643 * code will ensure that the resource is freed.
2644 */
2645void devm_clk_unregister(struct device *dev, struct clk *clk)
2646{
2647 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2648}
2649EXPORT_SYMBOL_GPL(devm_clk_unregister);
2650
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002651/*
2652 * clkdev helpers
2653 */
2654int __clk_get(struct clk *clk)
2655{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002656 struct clk_core *core = !clk ? NULL : clk->core;
2657
2658 if (core) {
2659 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002660 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002661
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002662 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002663 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002664 return 1;
2665}
2666
2667void __clk_put(struct clk *clk)
2668{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002669 struct module *owner;
2670
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002671 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002672 return;
2673
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002674 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002675
2676 hlist_del(&clk->child_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002677 if (clk->min_rate > clk->core->req_rate ||
2678 clk->max_rate < clk->core->req_rate)
2679 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2680
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002681 owner = clk->core->owner;
2682 kref_put(&clk->core->ref, __clk_release);
2683
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002684 clk_prepare_unlock();
2685
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002686 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002687
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002688 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002689}
2690
Mike Turquetteb24764902012-03-15 23:11:19 -07002691/*** clk rate change notifiers ***/
2692
2693/**
2694 * clk_notifier_register - add a clk rate change notifier
2695 * @clk: struct clk * to watch
2696 * @nb: struct notifier_block * with callback info
2697 *
2698 * Request notification when clk's rate changes. This uses an SRCU
2699 * notifier because we want it to block and notifier unregistrations are
2700 * uncommon. The callbacks associated with the notifier must not
2701 * re-enter into the clk framework by calling any top-level clk APIs;
2702 * this will cause a nested prepare_lock mutex.
2703 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002704 * In all notification cases cases (pre, post and abort rate change) the
2705 * original clock rate is passed to the callback via struct
2706 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002707 * clk_notifier_data.new_rate.
2708 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002709 * clk_notifier_register() must be called from non-atomic context.
2710 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2711 * allocation failure; otherwise, passes along the return value of
2712 * srcu_notifier_chain_register().
2713 */
2714int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2715{
2716 struct clk_notifier *cn;
2717 int ret = -ENOMEM;
2718
2719 if (!clk || !nb)
2720 return -EINVAL;
2721
Mike Turquetteeab89f62013-03-28 13:59:01 -07002722 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002723
2724 /* search the list of notifiers for this clk */
2725 list_for_each_entry(cn, &clk_notifier_list, node)
2726 if (cn->clk == clk)
2727 break;
2728
2729 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2730 if (cn->clk != clk) {
2731 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2732 if (!cn)
2733 goto out;
2734
2735 cn->clk = clk;
2736 srcu_init_notifier_head(&cn->notifier_head);
2737
2738 list_add(&cn->node, &clk_notifier_list);
2739 }
2740
2741 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2742
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002743 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002744
2745out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002746 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002747
2748 return ret;
2749}
2750EXPORT_SYMBOL_GPL(clk_notifier_register);
2751
2752/**
2753 * clk_notifier_unregister - remove a clk rate change notifier
2754 * @clk: struct clk *
2755 * @nb: struct notifier_block * with callback info
2756 *
2757 * Request no further notification for changes to 'clk' and frees memory
2758 * allocated in clk_notifier_register.
2759 *
2760 * Returns -EINVAL if called with null arguments; otherwise, passes
2761 * along the return value of srcu_notifier_chain_unregister().
2762 */
2763int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2764{
2765 struct clk_notifier *cn = NULL;
2766 int ret = -EINVAL;
2767
2768 if (!clk || !nb)
2769 return -EINVAL;
2770
Mike Turquetteeab89f62013-03-28 13:59:01 -07002771 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002772
2773 list_for_each_entry(cn, &clk_notifier_list, node)
2774 if (cn->clk == clk)
2775 break;
2776
2777 if (cn->clk == clk) {
2778 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2779
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002780 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002781
2782 /* XXX the notifier code should handle this better */
2783 if (!cn->notifier_head.head) {
2784 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002785 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002786 kfree(cn);
2787 }
2788
2789 } else {
2790 ret = -ENOENT;
2791 }
2792
Mike Turquetteeab89f62013-03-28 13:59:01 -07002793 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002794
2795 return ret;
2796}
2797EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002798
2799#ifdef CONFIG_OF
2800/**
2801 * struct of_clk_provider - Clock provider registration structure
2802 * @link: Entry in global list of clock providers
2803 * @node: Pointer to device tree node of clock provider
2804 * @get: Get clock callback. Returns NULL or a struct clk for the
2805 * given clock specifier
2806 * @data: context pointer to be passed into @get callback
2807 */
2808struct of_clk_provider {
2809 struct list_head link;
2810
2811 struct device_node *node;
2812 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2813 void *data;
2814};
2815
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302816static const struct of_device_id __clk_of_table_sentinel
2817 __used __section(__clk_of_table_end);
2818
Grant Likely766e6a42012-04-09 14:50:06 -05002819static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002820static DEFINE_MUTEX(of_clk_mutex);
2821
2822/* of_clk_provider list locking helpers */
2823void of_clk_lock(void)
2824{
2825 mutex_lock(&of_clk_mutex);
2826}
2827
2828void of_clk_unlock(void)
2829{
2830 mutex_unlock(&of_clk_mutex);
2831}
Grant Likely766e6a42012-04-09 14:50:06 -05002832
2833struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2834 void *data)
2835{
2836 return data;
2837}
2838EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2839
Shawn Guo494bfec2012-08-22 21:36:27 +08002840struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2841{
2842 struct clk_onecell_data *clk_data = data;
2843 unsigned int idx = clkspec->args[0];
2844
2845 if (idx >= clk_data->clk_num) {
2846 pr_err("%s: invalid clock index %d\n", __func__, idx);
2847 return ERR_PTR(-EINVAL);
2848 }
2849
2850 return clk_data->clks[idx];
2851}
2852EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2853
Grant Likely766e6a42012-04-09 14:50:06 -05002854/**
2855 * of_clk_add_provider() - Register a clock provider for a node
2856 * @np: Device node pointer associated with clock provider
2857 * @clk_src_get: callback for decoding clock
2858 * @data: context pointer for @clk_src_get callback.
2859 */
2860int of_clk_add_provider(struct device_node *np,
2861 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2862 void *data),
2863 void *data)
2864{
2865 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002866 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002867
2868 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2869 if (!cp)
2870 return -ENOMEM;
2871
2872 cp->node = of_node_get(np);
2873 cp->data = data;
2874 cp->get = clk_src_get;
2875
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002876 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002877 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002878 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002879 pr_debug("Added clock from %s\n", np->full_name);
2880
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002881 ret = of_clk_set_defaults(np, true);
2882 if (ret < 0)
2883 of_clk_del_provider(np);
2884
2885 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002886}
2887EXPORT_SYMBOL_GPL(of_clk_add_provider);
2888
2889/**
2890 * of_clk_del_provider() - Remove a previously registered clock provider
2891 * @np: Device node pointer associated with clock provider
2892 */
2893void of_clk_del_provider(struct device_node *np)
2894{
2895 struct of_clk_provider *cp;
2896
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002897 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002898 list_for_each_entry(cp, &of_clk_providers, link) {
2899 if (cp->node == np) {
2900 list_del(&cp->link);
2901 of_node_put(cp->node);
2902 kfree(cp);
2903 break;
2904 }
2905 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002906 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002907}
2908EXPORT_SYMBOL_GPL(of_clk_del_provider);
2909
Stephen Boyd73e0e492015-02-06 11:42:43 -08002910struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2911 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002912{
2913 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002914 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002915
2916 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002917 list_for_each_entry(provider, &of_clk_providers, link) {
2918 if (provider->node == clkspec->np)
2919 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002920 if (!IS_ERR(clk)) {
2921 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2922 con_id);
2923
2924 if (!IS_ERR(clk) && !__clk_get(clk)) {
2925 __clk_free_clk(clk);
2926 clk = ERR_PTR(-ENOENT);
2927 }
2928
Grant Likely766e6a42012-04-09 14:50:06 -05002929 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002930 }
Grant Likely766e6a42012-04-09 14:50:06 -05002931 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002932
2933 return clk;
2934}
2935
2936struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2937{
2938 struct clk *clk;
2939
2940 mutex_lock(&of_clk_mutex);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002941 clk = __of_clk_get_from_provider(clkspec, NULL, __func__);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002942 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002943
2944 return clk;
2945}
2946
Mike Turquettef6102742013-10-07 23:12:13 -07002947int of_clk_get_parent_count(struct device_node *np)
2948{
2949 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2950}
2951EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2952
Grant Likely766e6a42012-04-09 14:50:06 -05002953const char *of_clk_get_parent_name(struct device_node *np, int index)
2954{
2955 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002956 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002957 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002958 const __be32 *vp;
2959 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002960 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002961 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002962
2963 if (index < 0)
2964 return NULL;
2965
2966 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2967 &clkspec);
2968 if (rc)
2969 return NULL;
2970
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002971 index = clkspec.args_count ? clkspec.args[0] : 0;
2972 count = 0;
2973
2974 /* if there is an indices property, use it to transfer the index
2975 * specified into an array offset for the clock-output-names property.
2976 */
2977 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2978 if (index == pv) {
2979 index = count;
2980 break;
2981 }
2982 count++;
2983 }
2984
Grant Likely766e6a42012-04-09 14:50:06 -05002985 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002986 index,
Grant Likely766e6a42012-04-09 14:50:06 -05002987 &clk_name) < 0)
2988 clk_name = clkspec.np->name;
2989
2990 of_node_put(clkspec.np);
2991 return clk_name;
2992}
2993EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2994
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002995struct clock_provider {
2996 of_clk_init_cb_t clk_init_cb;
2997 struct device_node *np;
2998 struct list_head node;
2999};
3000
3001static LIST_HEAD(clk_provider_list);
3002
3003/*
3004 * This function looks for a parent clock. If there is one, then it
3005 * checks that the provider for this parent clock was initialized, in
3006 * this case the parent clock will be ready.
3007 */
3008static int parent_ready(struct device_node *np)
3009{
3010 int i = 0;
3011
3012 while (true) {
3013 struct clk *clk = of_clk_get(np, i);
3014
3015 /* this parent is ready we can check the next one */
3016 if (!IS_ERR(clk)) {
3017 clk_put(clk);
3018 i++;
3019 continue;
3020 }
3021
3022 /* at least one parent is not ready, we exit now */
3023 if (PTR_ERR(clk) == -EPROBE_DEFER)
3024 return 0;
3025
3026 /*
3027 * Here we make assumption that the device tree is
3028 * written correctly. So an error means that there is
3029 * no more parent. As we didn't exit yet, then the
3030 * previous parent are ready. If there is no clock
3031 * parent, no need to wait for them, then we can
3032 * consider their absence as being ready
3033 */
3034 return 1;
3035 }
3036}
3037
Grant Likely766e6a42012-04-09 14:50:06 -05003038/**
3039 * of_clk_init() - Scan and init clock providers from the DT
3040 * @matches: array of compatible values and init functions for providers.
3041 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003042 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003043 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003044 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003045 */
3046void __init of_clk_init(const struct of_device_id *matches)
3047{
Alex Elder7f7ed582013-08-22 11:31:31 -05003048 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003049 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003050 struct clock_provider *clk_provider, *next;
3051 bool is_init_done;
3052 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003053
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303054 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003055 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303056
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003057 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003058 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003059 struct clock_provider *parent =
3060 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3061
3062 parent->clk_init_cb = match->data;
3063 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003064 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003065 }
3066
3067 while (!list_empty(&clk_provider_list)) {
3068 is_init_done = false;
3069 list_for_each_entry_safe(clk_provider, next,
3070 &clk_provider_list, node) {
3071 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003072
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003073 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003074 of_clk_set_defaults(clk_provider->np, true);
3075
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003076 list_del(&clk_provider->node);
3077 kfree(clk_provider);
3078 is_init_done = true;
3079 }
3080 }
3081
3082 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003083 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003084 * remaining providers during the last loop, so now we
3085 * initialize all the remaining ones unconditionally
3086 * in case the clock parent was not mandatory
3087 */
3088 if (!is_init_done)
3089 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003090 }
3091}
3092#endif