blob: 237f23f68bfce18cdfee3306487ba924906125c4 [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
483 hlist_for_each_entry(child, &clk->children, child_node)
484 clk_unprepare_unused_subtree(child);
485
486 if (clk->prepare_count)
487 return;
488
489 if (clk->flags & CLK_IGNORE_UNUSED)
490 return;
491
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100492 if (clk_core_is_prepared(clk)) {
Ulf Hansson3cc82472013-03-12 20:26:04 +0100493 if (clk->ops->unprepare_unused)
494 clk->ops->unprepare_unused(clk->hw);
495 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100496 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100497 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100498}
499
500/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100501static void clk_disable_unused_subtree(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700502{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100503 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700504 unsigned long flags;
505
Sasha Levinb67bfe02013-02-27 17:06:00 -0800506 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700507 clk_disable_unused_subtree(child);
508
Mike Turquetteeab89f62013-03-28 13:59:01 -0700509 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700510
511 if (clk->enable_count)
512 goto unlock_out;
513
514 if (clk->flags & CLK_IGNORE_UNUSED)
515 goto unlock_out;
516
Mike Turquette7c045a52012-12-04 11:00:35 -0800517 /*
518 * some gate clocks have special needs during the disable-unused
519 * sequence. call .disable_unused if available, otherwise fall
520 * back to .disable
521 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100522 if (clk_core_is_enabled(clk)) {
Mike Turquette7c045a52012-12-04 11:00:35 -0800523 if (clk->ops->disable_unused)
524 clk->ops->disable_unused(clk->hw);
525 else if (clk->ops->disable)
526 clk->ops->disable(clk->hw);
527 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700528
529unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700530 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700531}
532
Olof Johansson1e435252013-04-27 14:10:18 -0700533static bool clk_ignore_unused;
534static int __init clk_ignore_unused_setup(char *__unused)
535{
536 clk_ignore_unused = true;
537 return 1;
538}
539__setup("clk_ignore_unused", clk_ignore_unused_setup);
540
Mike Turquetteb24764902012-03-15 23:11:19 -0700541static int clk_disable_unused(void)
542{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100543 struct clk_core *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700544
Olof Johansson1e435252013-04-27 14:10:18 -0700545 if (clk_ignore_unused) {
546 pr_warn("clk: Not disabling unused clocks\n");
547 return 0;
548 }
549
Mike Turquetteeab89f62013-03-28 13:59:01 -0700550 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700551
Sasha Levinb67bfe02013-02-27 17:06:00 -0800552 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700553 clk_disable_unused_subtree(clk);
554
Sasha Levinb67bfe02013-02-27 17:06:00 -0800555 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700556 clk_disable_unused_subtree(clk);
557
Ulf Hansson1c155b32013-03-12 20:26:03 +0100558 hlist_for_each_entry(clk, &clk_root_list, child_node)
559 clk_unprepare_unused_subtree(clk);
560
561 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
562 clk_unprepare_unused_subtree(clk);
563
Mike Turquetteeab89f62013-03-28 13:59:01 -0700564 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700565
566 return 0;
567}
Saravana Kannand41d5802013-05-09 11:35:01 -0700568late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700569
570/*** helper functions ***/
571
Russ Dill65800b22012-11-26 11:20:09 -0800572const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700573{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100574 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700575}
Niels de Vos48950842012-12-13 13:12:25 +0100576EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700577
Russ Dill65800b22012-11-26 11:20:09 -0800578struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700579{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100580 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700581}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800582EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700583
Russ Dill65800b22012-11-26 11:20:09 -0800584u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700585{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100586 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700587}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800588EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700589
Russ Dill65800b22012-11-26 11:20:09 -0800590struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700591{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100592 if (!clk)
593 return NULL;
594
595 /* TODO: Create a per-user clk and change callers to call clk_put */
596 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700597}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800598EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700599
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100600static struct clk_core *clk_core_get_parent_by_index(struct clk_core *clk,
601 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100602{
603 if (!clk || index >= clk->num_parents)
604 return NULL;
605 else if (!clk->parents)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100606 return clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100607 else if (!clk->parents[index])
608 return clk->parents[index] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100609 clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100610 else
611 return clk->parents[index];
612}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100613
614struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
615{
616 struct clk_core *parent;
617
618 if (!clk)
619 return NULL;
620
621 parent = clk_core_get_parent_by_index(clk->core, index);
622
623 return !parent ? NULL : parent->hw->clk;
624}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800625EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100626
Russ Dill65800b22012-11-26 11:20:09 -0800627unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700628{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100629 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700630}
631
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100632static unsigned long clk_core_get_rate_nolock(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700633{
634 unsigned long ret;
635
636 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530637 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700638 goto out;
639 }
640
641 ret = clk->rate;
642
643 if (clk->flags & CLK_IS_ROOT)
644 goto out;
645
646 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530647 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700648
649out:
650 return ret;
651}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100652
653unsigned long __clk_get_rate(struct clk *clk)
654{
655 if (!clk)
656 return 0;
657
658 return clk_core_get_rate_nolock(clk->core);
659}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800660EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700661
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100662static unsigned long __clk_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100663{
664 if (!clk)
665 return 0;
666
667 return clk->accuracy;
668}
669
Russ Dill65800b22012-11-26 11:20:09 -0800670unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700671{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100672 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700673}
Thierry Redingb05c6832013-09-03 09:43:51 +0200674EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700675
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100676static bool clk_core_is_prepared(struct clk_core *clk)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100677{
678 int ret;
679
680 if (!clk)
681 return false;
682
683 /*
684 * .is_prepared is optional for clocks that can prepare
685 * fall back to software usage counter if it is missing
686 */
687 if (!clk->ops->is_prepared) {
688 ret = clk->prepare_count ? 1 : 0;
689 goto out;
690 }
691
692 ret = clk->ops->is_prepared(clk->hw);
693out:
694 return !!ret;
695}
696
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100697bool __clk_is_prepared(struct clk *clk)
698{
699 if (!clk)
700 return false;
701
702 return clk_core_is_prepared(clk->core);
703}
704
705static bool clk_core_is_enabled(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700706{
707 int ret;
708
709 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700710 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700711
712 /*
713 * .is_enabled is only mandatory for clocks that gate
714 * fall back to software usage counter if .is_enabled is missing
715 */
716 if (!clk->ops->is_enabled) {
717 ret = clk->enable_count ? 1 : 0;
718 goto out;
719 }
720
721 ret = clk->ops->is_enabled(clk->hw);
722out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700723 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700724}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100725
726bool __clk_is_enabled(struct clk *clk)
727{
728 if (!clk)
729 return false;
730
731 return clk_core_is_enabled(clk->core);
732}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800733EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700734
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100735static struct clk_core *__clk_lookup_subtree(const char *name,
736 struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700737{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100738 struct clk_core *child;
739 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700740
741 if (!strcmp(clk->name, name))
742 return clk;
743
Sasha Levinb67bfe02013-02-27 17:06:00 -0800744 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700745 ret = __clk_lookup_subtree(name, child);
746 if (ret)
747 return ret;
748 }
749
750 return NULL;
751}
752
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100753static struct clk_core *clk_core_lookup(const char *name)
Mike Turquetteb24764902012-03-15 23:11:19 -0700754{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100755 struct clk_core *root_clk;
756 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700757
758 if (!name)
759 return NULL;
760
761 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800762 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700763 ret = __clk_lookup_subtree(name, root_clk);
764 if (ret)
765 return ret;
766 }
767
768 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800769 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700770 ret = __clk_lookup_subtree(name, root_clk);
771 if (ret)
772 return ret;
773 }
774
775 return NULL;
776}
777
Stephen Boyd15a02c12015-01-19 18:05:28 -0800778static bool mux_is_better_rate(unsigned long rate, unsigned long now,
779 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100780{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800781 if (flags & CLK_MUX_ROUND_CLOSEST)
782 return abs(now - rate) < abs(best - rate);
783
784 return now <= rate && now > best;
785}
786
787static long
788clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100789 unsigned long min_rate,
790 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800791 unsigned long *best_parent_rate,
792 struct clk_hw **best_parent_p,
793 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100794{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100795 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100796 int i, num_parents;
797 unsigned long parent_rate, best = 0;
798
799 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100800 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
801 parent = core->parent;
802 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100803 best = __clk_determine_rate(parent ? parent->hw : NULL,
804 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100805 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100806 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100807 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100808 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100809 goto out;
810 }
811
812 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100813 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100814 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100815 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100816 if (!parent)
817 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100818 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100819 parent_rate = __clk_determine_rate(parent->hw, rate,
820 min_rate,
821 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100822 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100823 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800824 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100825 best_parent = parent;
826 best = parent_rate;
827 }
828 }
829
830out:
831 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100832 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100833 *best_parent_rate = best;
834
835 return best;
836}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800837
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100838struct clk *__clk_lookup(const char *name)
839{
840 struct clk_core *core = clk_core_lookup(name);
841
842 return !core ? NULL : core->hw->clk;
843}
844
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100845static void clk_core_get_boundaries(struct clk_core *clk,
846 unsigned long *min_rate,
847 unsigned long *max_rate)
848{
849 struct clk *clk_user;
850
851 *min_rate = 0;
852 *max_rate = ULONG_MAX;
853
854 hlist_for_each_entry(clk_user, &clk->clks, child_node)
855 *min_rate = max(*min_rate, clk_user->min_rate);
856
857 hlist_for_each_entry(clk_user, &clk->clks, child_node)
858 *max_rate = min(*max_rate, clk_user->max_rate);
859}
860
Stephen Boyd15a02c12015-01-19 18:05:28 -0800861/*
862 * Helper for finding best parent to provide a given frequency. This can be used
863 * directly as a determine_rate callback (e.g. for a mux), or from a more
864 * complex clock that may combine a mux with other operations.
865 */
866long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100867 unsigned long min_rate,
868 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800869 unsigned long *best_parent_rate,
870 struct clk_hw **best_parent_p)
871{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100872 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
873 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800874 best_parent_p, 0);
875}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800876EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100877
Stephen Boyd15a02c12015-01-19 18:05:28 -0800878long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100879 unsigned long min_rate,
880 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800881 unsigned long *best_parent_rate,
882 struct clk_hw **best_parent_p)
883{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100884 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
885 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800886 best_parent_p,
887 CLK_MUX_ROUND_CLOSEST);
888}
889EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
890
Mike Turquetteb24764902012-03-15 23:11:19 -0700891/*** clk api ***/
892
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100893static void clk_core_unprepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700894{
895 if (!clk)
896 return;
897
898 if (WARN_ON(clk->prepare_count == 0))
899 return;
900
901 if (--clk->prepare_count > 0)
902 return;
903
904 WARN_ON(clk->enable_count > 0);
905
906 if (clk->ops->unprepare)
907 clk->ops->unprepare(clk->hw);
908
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100909 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700910}
911
912/**
913 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200914 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700915 *
916 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
917 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
918 * if the operation may sleep. One example is a clk which is accessed over
919 * I2c. In the complex case a clk gate operation may require a fast and a slow
920 * part. It is this reason that clk_unprepare and clk_disable are not mutually
921 * exclusive. In fact clk_disable must be called before clk_unprepare.
922 */
923void clk_unprepare(struct clk *clk)
924{
Stephen Boyd63589e92014-03-26 16:06:37 -0700925 if (IS_ERR_OR_NULL(clk))
926 return;
927
Mike Turquetteeab89f62013-03-28 13:59:01 -0700928 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100929 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700930 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700931}
932EXPORT_SYMBOL_GPL(clk_unprepare);
933
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100934static int clk_core_prepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700935{
936 int ret = 0;
937
938 if (!clk)
939 return 0;
940
941 if (clk->prepare_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100942 ret = clk_core_prepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700943 if (ret)
944 return ret;
945
946 if (clk->ops->prepare) {
947 ret = clk->ops->prepare(clk->hw);
948 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100949 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700950 return ret;
951 }
952 }
953 }
954
955 clk->prepare_count++;
956
957 return 0;
958}
959
960/**
961 * clk_prepare - prepare a clock source
962 * @clk: the clk being prepared
963 *
964 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
965 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
966 * operation may sleep. One example is a clk which is accessed over I2c. In
967 * the complex case a clk ungate operation may require a fast and a slow part.
968 * It is this reason that clk_prepare and clk_enable are not mutually
969 * exclusive. In fact clk_prepare must be called before clk_enable.
970 * Returns 0 on success, -EERROR otherwise.
971 */
972int clk_prepare(struct clk *clk)
973{
974 int ret;
975
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100976 if (!clk)
977 return 0;
978
Mike Turquetteeab89f62013-03-28 13:59:01 -0700979 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100980 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700981 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700982
983 return ret;
984}
985EXPORT_SYMBOL_GPL(clk_prepare);
986
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100987static void clk_core_disable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700988{
989 if (!clk)
990 return;
991
992 if (WARN_ON(clk->enable_count == 0))
993 return;
994
995 if (--clk->enable_count > 0)
996 return;
997
998 if (clk->ops->disable)
999 clk->ops->disable(clk->hw);
1000
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001001 clk_core_disable(clk->parent);
1002}
1003
1004static void __clk_disable(struct clk *clk)
1005{
1006 if (!clk)
1007 return;
1008
1009 clk_core_disable(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001010}
1011
1012/**
1013 * clk_disable - gate a clock
1014 * @clk: the clk being gated
1015 *
1016 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1017 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1018 * clk if the operation is fast and will never sleep. One example is a
1019 * SoC-internal clk which is controlled via simple register writes. In the
1020 * complex case a clk gate operation may require a fast and a slow part. It is
1021 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1022 * In fact clk_disable must be called before clk_unprepare.
1023 */
1024void clk_disable(struct clk *clk)
1025{
1026 unsigned long flags;
1027
Stephen Boyd63589e92014-03-26 16:06:37 -07001028 if (IS_ERR_OR_NULL(clk))
1029 return;
1030
Mike Turquetteeab89f62013-03-28 13:59:01 -07001031 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001032 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001033 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001034}
1035EXPORT_SYMBOL_GPL(clk_disable);
1036
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001037static int clk_core_enable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001038{
1039 int ret = 0;
1040
1041 if (!clk)
1042 return 0;
1043
1044 if (WARN_ON(clk->prepare_count == 0))
1045 return -ESHUTDOWN;
1046
1047 if (clk->enable_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001048 ret = clk_core_enable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001049
1050 if (ret)
1051 return ret;
1052
1053 if (clk->ops->enable) {
1054 ret = clk->ops->enable(clk->hw);
1055 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001056 clk_core_disable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001057 return ret;
1058 }
1059 }
1060 }
1061
1062 clk->enable_count++;
1063 return 0;
1064}
1065
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001066static int __clk_enable(struct clk *clk)
1067{
1068 if (!clk)
1069 return 0;
1070
1071 return clk_core_enable(clk->core);
1072}
1073
Mike Turquetteb24764902012-03-15 23:11:19 -07001074/**
1075 * clk_enable - ungate a clock
1076 * @clk: the clk being ungated
1077 *
1078 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1079 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1080 * if the operation will never sleep. One example is a SoC-internal clk which
1081 * is controlled via simple register writes. In the complex case a clk ungate
1082 * operation may require a fast and a slow part. It is this reason that
1083 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1084 * must be called before clk_enable. Returns 0 on success, -EERROR
1085 * otherwise.
1086 */
1087int clk_enable(struct clk *clk)
1088{
1089 unsigned long flags;
1090 int ret;
1091
Mike Turquetteeab89f62013-03-28 13:59:01 -07001092 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001093 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001094 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001095
1096 return ret;
1097}
1098EXPORT_SYMBOL_GPL(clk_enable);
1099
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001100static unsigned long clk_core_round_rate_nolock(struct clk_core *clk,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001101 unsigned long rate,
1102 unsigned long min_rate,
1103 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001104{
Shawn Guo81536e02012-04-12 20:50:17 +08001105 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001106 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001107 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001108
1109 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001110 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001111
James Hogan71472c02013-07-29 12:25:00 +01001112 parent = clk->parent;
1113 if (parent)
1114 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001115
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001116 if (clk->ops->determine_rate) {
1117 parent_hw = parent ? parent->hw : NULL;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001118 return clk->ops->determine_rate(clk->hw, rate,
1119 min_rate, max_rate,
1120 &parent_rate, &parent_hw);
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001121 } else if (clk->ops->round_rate)
James Hogan71472c02013-07-29 12:25:00 +01001122 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
1123 else if (clk->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001124 return clk_core_round_rate_nolock(clk->parent, rate, min_rate,
1125 max_rate);
James Hogan71472c02013-07-29 12:25:00 +01001126 else
1127 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001128}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001129
1130/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001131 * __clk_determine_rate - get the closest rate actually supported by a clock
1132 * @hw: determine the rate of this clock
1133 * @rate: target rate
1134 * @min_rate: returned rate must be greater than this rate
1135 * @max_rate: returned rate must be less than this rate
1136 *
1137 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
1138 * .determine_rate.
1139 */
1140unsigned long __clk_determine_rate(struct clk_hw *hw,
1141 unsigned long rate,
1142 unsigned long min_rate,
1143 unsigned long max_rate)
1144{
1145 if (!hw)
1146 return 0;
1147
1148 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
1149}
1150EXPORT_SYMBOL_GPL(__clk_determine_rate);
1151
1152/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001153 * __clk_round_rate - round the given rate for a clk
1154 * @clk: round the rate of this clock
1155 * @rate: the rate which is to be rounded
1156 *
1157 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1158 */
1159unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1160{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001161 unsigned long min_rate;
1162 unsigned long max_rate;
1163
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001164 if (!clk)
1165 return 0;
1166
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001167 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
1168
1169 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001170}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001171EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001172
1173/**
1174 * clk_round_rate - round the given rate for a clk
1175 * @clk: the clk for which we are rounding a rate
1176 * @rate: the rate which is to be rounded
1177 *
1178 * Takes in a rate as input and rounds it to a rate that the clk can actually
1179 * use which is then returned. If clk doesn't support round_rate operation
1180 * then the parent rate is returned.
1181 */
1182long clk_round_rate(struct clk *clk, unsigned long rate)
1183{
1184 unsigned long ret;
1185
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001186 if (!clk)
1187 return 0;
1188
Mike Turquetteeab89f62013-03-28 13:59:01 -07001189 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001190 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001191 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001192
1193 return ret;
1194}
1195EXPORT_SYMBOL_GPL(clk_round_rate);
1196
1197/**
1198 * __clk_notify - call clk notifier chain
1199 * @clk: struct clk * that is changing rate
1200 * @msg: clk notifier type (see include/linux/clk.h)
1201 * @old_rate: old clk rate
1202 * @new_rate: new clk rate
1203 *
1204 * Triggers a notifier call chain on the clk rate-change notification
1205 * for 'clk'. Passes a pointer to the struct clk and the previous
1206 * and current rates to the notifier callback. Intended to be called by
1207 * internal clock code only. Returns NOTIFY_DONE from the last driver
1208 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1209 * a driver returns that.
1210 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001211static int __clk_notify(struct clk_core *clk, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001212 unsigned long old_rate, unsigned long new_rate)
1213{
1214 struct clk_notifier *cn;
1215 struct clk_notifier_data cnd;
1216 int ret = NOTIFY_DONE;
1217
Mike Turquetteb24764902012-03-15 23:11:19 -07001218 cnd.old_rate = old_rate;
1219 cnd.new_rate = new_rate;
1220
1221 list_for_each_entry(cn, &clk_notifier_list, node) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001222 if (cn->clk->core == clk) {
1223 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001224 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1225 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001226 }
1227 }
1228
1229 return ret;
1230}
1231
1232/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001233 * __clk_recalc_accuracies
1234 * @clk: first clk in the subtree
1235 *
1236 * Walks the subtree of clks starting with clk and recalculates accuracies as
1237 * it goes. Note that if a clk does not implement the .recalc_accuracy
1238 * callback then it is assumed that the clock will take on the accuracy of it's
1239 * parent.
1240 *
1241 * Caller must hold prepare_lock.
1242 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001243static void __clk_recalc_accuracies(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001244{
1245 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001246 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001247
1248 if (clk->parent)
1249 parent_accuracy = clk->parent->accuracy;
1250
1251 if (clk->ops->recalc_accuracy)
1252 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1253 parent_accuracy);
1254 else
1255 clk->accuracy = parent_accuracy;
1256
1257 hlist_for_each_entry(child, &clk->children, child_node)
1258 __clk_recalc_accuracies(child);
1259}
1260
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001261static long clk_core_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001262{
1263 unsigned long accuracy;
1264
1265 clk_prepare_lock();
1266 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1267 __clk_recalc_accuracies(clk);
1268
1269 accuracy = __clk_get_accuracy(clk);
1270 clk_prepare_unlock();
1271
1272 return accuracy;
1273}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001274
1275/**
1276 * clk_get_accuracy - return the accuracy of clk
1277 * @clk: the clk whose accuracy is being returned
1278 *
1279 * Simply returns the cached accuracy of the clk, unless
1280 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1281 * issued.
1282 * If clk is NULL then returns 0.
1283 */
1284long clk_get_accuracy(struct clk *clk)
1285{
1286 if (!clk)
1287 return 0;
1288
1289 return clk_core_get_accuracy(clk->core);
1290}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001291EXPORT_SYMBOL_GPL(clk_get_accuracy);
1292
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001293static unsigned long clk_recalc(struct clk_core *clk,
1294 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001295{
1296 if (clk->ops->recalc_rate)
1297 return clk->ops->recalc_rate(clk->hw, parent_rate);
1298 return parent_rate;
1299}
1300
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001301/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001302 * __clk_recalc_rates
1303 * @clk: first clk in the subtree
1304 * @msg: notification type (see include/linux/clk.h)
1305 *
1306 * Walks the subtree of clks starting with clk and recalculates rates as it
1307 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001308 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001309 *
1310 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1311 * if necessary.
1312 *
1313 * Caller must hold prepare_lock.
1314 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001315static void __clk_recalc_rates(struct clk_core *clk, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001316{
1317 unsigned long old_rate;
1318 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001319 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001320
1321 old_rate = clk->rate;
1322
1323 if (clk->parent)
1324 parent_rate = clk->parent->rate;
1325
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001326 clk->rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001327
1328 /*
1329 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1330 * & ABORT_RATE_CHANGE notifiers
1331 */
1332 if (clk->notifier_count && msg)
1333 __clk_notify(clk, msg, old_rate, clk->rate);
1334
Sasha Levinb67bfe02013-02-27 17:06:00 -08001335 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001336 __clk_recalc_rates(child, msg);
1337}
1338
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001339static unsigned long clk_core_get_rate(struct clk_core *clk)
1340{
1341 unsigned long rate;
1342
1343 clk_prepare_lock();
1344
1345 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1346 __clk_recalc_rates(clk, 0);
1347
1348 rate = clk_core_get_rate_nolock(clk);
1349 clk_prepare_unlock();
1350
1351 return rate;
1352}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001353
Mike Turquetteb24764902012-03-15 23:11:19 -07001354/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001355 * clk_get_rate - return the rate of clk
1356 * @clk: the clk whose rate is being returned
1357 *
1358 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1359 * is set, which means a recalc_rate will be issued.
1360 * If clk is NULL then returns 0.
1361 */
1362unsigned long clk_get_rate(struct clk *clk)
1363{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001364 if (!clk)
1365 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001366
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001367 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001368}
1369EXPORT_SYMBOL_GPL(clk_get_rate);
1370
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001371static int clk_fetch_parent_index(struct clk_core *clk,
1372 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001373{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001374 int i;
James Hogan4935b222013-07-29 12:24:59 +01001375
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001376 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001377 clk->parents = kcalloc(clk->num_parents,
1378 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001379 if (!clk->parents)
1380 return -ENOMEM;
1381 }
James Hogan4935b222013-07-29 12:24:59 +01001382
1383 /*
1384 * find index of new parent clock using cached parent ptrs,
1385 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001386 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001387 */
1388 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001389 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001390 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001391
1392 if (clk->parents[i])
1393 continue;
1394
1395 if (!strcmp(clk->parent_names[i], parent->name)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001396 clk->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001397 return i;
James Hogan4935b222013-07-29 12:24:59 +01001398 }
1399 }
1400
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001401 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001402}
1403
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001404static void clk_reparent(struct clk_core *clk, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001405{
1406 hlist_del(&clk->child_node);
1407
James Hogan903efc52013-08-29 12:10:51 +01001408 if (new_parent) {
1409 /* avoid duplicate POST_RATE_CHANGE notifications */
1410 if (new_parent->new_child == clk)
1411 new_parent->new_child = NULL;
1412
James Hogan4935b222013-07-29 12:24:59 +01001413 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001414 } else {
James Hogan4935b222013-07-29 12:24:59 +01001415 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001416 }
James Hogan4935b222013-07-29 12:24:59 +01001417
1418 clk->parent = new_parent;
1419}
1420
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001421static struct clk_core *__clk_set_parent_before(struct clk_core *clk,
1422 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001423{
1424 unsigned long flags;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001425 struct clk_core *old_parent = clk->parent;
James Hogan4935b222013-07-29 12:24:59 +01001426
1427 /*
1428 * Migrate prepare state between parents and prevent race with
1429 * clk_enable().
1430 *
1431 * If the clock is not prepared, then a race with
1432 * clk_enable/disable() is impossible since we already have the
1433 * prepare lock (future calls to clk_enable() need to be preceded by
1434 * a clk_prepare()).
1435 *
1436 * If the clock is prepared, migrate the prepared state to the new
1437 * parent and also protect against a race with clk_enable() by
1438 * forcing the clock and the new parent on. This ensures that all
1439 * future calls to clk_enable() are practically NOPs with respect to
1440 * hardware and software states.
1441 *
1442 * See also: Comment for clk_set_parent() below.
1443 */
1444 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001445 clk_core_prepare(parent);
1446 clk_core_enable(parent);
1447 clk_core_enable(clk);
James Hogan4935b222013-07-29 12:24:59 +01001448 }
1449
1450 /* update the clk tree topology */
1451 flags = clk_enable_lock();
1452 clk_reparent(clk, parent);
1453 clk_enable_unlock(flags);
1454
Stephen Boyd3fa22522014-01-15 10:47:22 -08001455 return old_parent;
1456}
1457
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001458static void __clk_set_parent_after(struct clk_core *core,
1459 struct clk_core *parent,
1460 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001461{
1462 /*
1463 * Finish the migration of prepare state and undo the changes done
1464 * for preventing a race with clk_enable().
1465 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001466 if (core->prepare_count) {
1467 clk_core_disable(core);
1468 clk_core_disable(old_parent);
1469 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001470 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001471}
1472
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001473static int __clk_set_parent(struct clk_core *clk, struct clk_core *parent,
1474 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001475{
1476 unsigned long flags;
1477 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001478 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001479
1480 old_parent = __clk_set_parent_before(clk, parent);
1481
James Hogan4935b222013-07-29 12:24:59 +01001482 /* change clock input source */
1483 if (parent && clk->ops->set_parent)
1484 ret = clk->ops->set_parent(clk->hw, p_index);
1485
1486 if (ret) {
1487 flags = clk_enable_lock();
1488 clk_reparent(clk, old_parent);
1489 clk_enable_unlock(flags);
1490
1491 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001492 clk_core_disable(clk);
1493 clk_core_disable(parent);
1494 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001495 }
1496 return ret;
1497 }
1498
Stephen Boyd3fa22522014-01-15 10:47:22 -08001499 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001500
James Hogan4935b222013-07-29 12:24:59 +01001501 return 0;
1502}
1503
Ulf Hanssona093bde2012-08-31 14:21:28 +02001504/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001505 * __clk_speculate_rates
1506 * @clk: first clk in the subtree
1507 * @parent_rate: the "future" rate of clk's parent
1508 *
1509 * Walks the subtree of clks starting with clk, speculating rates as it
1510 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1511 *
1512 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1513 * pre-rate change notifications and returns early if no clks in the
1514 * subtree have subscribed to the notifications. Note that if a clk does not
1515 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001516 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001517 *
1518 * Caller must hold prepare_lock.
1519 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001520static int __clk_speculate_rates(struct clk_core *clk,
1521 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001522{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001523 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001524 unsigned long new_rate;
1525 int ret = NOTIFY_DONE;
1526
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001527 new_rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001528
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001529 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001530 if (clk->notifier_count)
1531 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1532
Mike Turquette86bcfa22014-02-24 16:08:41 -08001533 if (ret & NOTIFY_STOP_MASK) {
1534 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1535 __func__, clk->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001536 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001537 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001538
Sasha Levinb67bfe02013-02-27 17:06:00 -08001539 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001540 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001541 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001542 break;
1543 }
1544
1545out:
1546 return ret;
1547}
1548
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001549static void clk_calc_subtree(struct clk_core *clk, unsigned long new_rate,
1550 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001551{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001552 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001553
1554 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001555 clk->new_parent = new_parent;
1556 clk->new_parent_index = p_index;
1557 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1558 clk->new_child = NULL;
1559 if (new_parent && new_parent != clk->parent)
1560 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001561
Sasha Levinb67bfe02013-02-27 17:06:00 -08001562 hlist_for_each_entry(child, &clk->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001563 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001564 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001565 }
1566}
1567
1568/*
1569 * calculate the new rates returning the topmost clock that has to be
1570 * changed.
1571 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001572static struct clk_core *clk_calc_new_rates(struct clk_core *clk,
1573 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001574{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001575 struct clk_core *top = clk;
1576 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001577 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001578 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001579 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001580 unsigned long min_rate;
1581 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001582 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001583
Mike Turquette7452b212012-03-26 14:45:36 -07001584 /* sanity */
1585 if (IS_ERR_OR_NULL(clk))
1586 return NULL;
1587
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001588 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001589 parent = old_parent = clk->parent;
1590 if (parent)
1591 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001592
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001593 clk_core_get_boundaries(clk, &min_rate, &max_rate);
1594
James Hogan71472c02013-07-29 12:25:00 +01001595 /* find the closest rate and parent clk/rate */
1596 if (clk->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001597 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001598 new_rate = clk->ops->determine_rate(clk->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001599 min_rate,
1600 max_rate,
James Hogan71472c02013-07-29 12:25:00 +01001601 &best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001602 &parent_hw);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001603 parent = parent_hw ? parent_hw->core : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001604 } else if (clk->ops->round_rate) {
1605 new_rate = clk->ops->round_rate(clk->hw, rate,
1606 &best_parent_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001607 if (new_rate < min_rate || new_rate > max_rate)
1608 return NULL;
James Hogan71472c02013-07-29 12:25:00 +01001609 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1610 /* pass-through clock without adjustable parent */
1611 clk->new_rate = clk->rate;
1612 return NULL;
1613 } else {
1614 /* pass-through clock with adjustable parent */
1615 top = clk_calc_new_rates(parent, rate);
1616 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001617 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001618 }
1619
James Hogan71472c02013-07-29 12:25:00 +01001620 /* some clocks must be gated to change parent */
1621 if (parent != old_parent &&
1622 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1623 pr_debug("%s: %s not gated but wants to reparent\n",
1624 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001625 return NULL;
1626 }
1627
James Hogan71472c02013-07-29 12:25:00 +01001628 /* try finding the new parent index */
Stephen Boyd4526e7b2014-12-22 11:26:42 -08001629 if (parent && clk->num_parents > 1) {
James Hogan71472c02013-07-29 12:25:00 +01001630 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001631 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001632 pr_debug("%s: clk %s can not be parent of clk %s\n",
1633 __func__, parent->name, clk->name);
1634 return NULL;
1635 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001636 }
1637
James Hogan71472c02013-07-29 12:25:00 +01001638 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1639 best_parent_rate != parent->rate)
1640 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001641
1642out:
James Hogan71472c02013-07-29 12:25:00 +01001643 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001644
1645 return top;
1646}
1647
1648/*
1649 * Notify about rate changes in a subtree. Always walk down the whole tree
1650 * so that in case of an error we can walk down the whole tree again and
1651 * abort the change.
1652 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001653static struct clk_core *clk_propagate_rate_change(struct clk_core *clk,
1654 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001655{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001656 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001657 int ret = NOTIFY_DONE;
1658
1659 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301660 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001661
1662 if (clk->notifier_count) {
1663 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001664 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001665 fail_clk = clk;
1666 }
1667
Sasha Levinb67bfe02013-02-27 17:06:00 -08001668 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001669 /* Skip children who will be reparented to another clock */
1670 if (child->new_parent && child->new_parent != clk)
1671 continue;
1672 tmp_clk = clk_propagate_rate_change(child, event);
1673 if (tmp_clk)
1674 fail_clk = tmp_clk;
1675 }
1676
1677 /* handle the new child who might not be in clk->children yet */
1678 if (clk->new_child) {
1679 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1680 if (tmp_clk)
1681 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001682 }
1683
1684 return fail_clk;
1685}
1686
1687/*
1688 * walk down a subtree and set the new rates notifying the rate
1689 * change on the way
1690 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001691static void clk_change_rate(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001692{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001693 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001694 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001695 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001696 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001697 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001698 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001699
1700 old_rate = clk->rate;
1701
Stephen Boyd3fa22522014-01-15 10:47:22 -08001702 if (clk->new_parent)
1703 best_parent_rate = clk->new_parent->rate;
1704 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001705 best_parent_rate = clk->parent->rate;
1706
Stephen Boyd3fa22522014-01-15 10:47:22 -08001707 if (clk->new_parent && clk->new_parent != clk->parent) {
1708 old_parent = __clk_set_parent_before(clk, clk->new_parent);
1709
1710 if (clk->ops->set_rate_and_parent) {
1711 skip_set_rate = true;
1712 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1713 best_parent_rate,
1714 clk->new_parent_index);
1715 } else if (clk->ops->set_parent) {
1716 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1717 }
1718
1719 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1720 }
1721
1722 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001723 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001724
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001725 clk->rate = clk_recalc(clk, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001726
1727 if (clk->notifier_count && old_rate != clk->rate)
1728 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1729
Tero Kristo067bb172014-08-21 16:47:45 +03001730 /*
1731 * Use safe iteration, as change_rate can actually swap parents
1732 * for certain clock types.
1733 */
1734 hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001735 /* Skip children who will be reparented to another clock */
1736 if (child->new_parent && child->new_parent != clk)
1737 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001738 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001739 }
1740
1741 /* handle the new child who might not be in clk->children yet */
1742 if (clk->new_child)
1743 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001744}
1745
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001746static int clk_core_set_rate_nolock(struct clk_core *clk,
1747 unsigned long req_rate)
1748{
1749 struct clk_core *top, *fail_clk;
1750 unsigned long rate = req_rate;
1751 int ret = 0;
1752
1753 if (!clk)
1754 return 0;
1755
1756 /* bail early if nothing to do */
1757 if (rate == clk_core_get_rate_nolock(clk))
1758 return 0;
1759
1760 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count)
1761 return -EBUSY;
1762
1763 /* calculate new rates and get the topmost changed clock */
1764 top = clk_calc_new_rates(clk, rate);
1765 if (!top)
1766 return -EINVAL;
1767
1768 /* notify that we are about to change rates */
1769 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1770 if (fail_clk) {
1771 pr_debug("%s: failed to set %s rate\n", __func__,
1772 fail_clk->name);
1773 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1774 return -EBUSY;
1775 }
1776
1777 /* change the rates */
1778 clk_change_rate(top);
1779
1780 clk->req_rate = req_rate;
1781
1782 return ret;
1783}
1784
Mike Turquetteb24764902012-03-15 23:11:19 -07001785/**
1786 * clk_set_rate - specify a new rate for clk
1787 * @clk: the clk whose rate is being changed
1788 * @rate: the new rate for clk
1789 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001790 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001791 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001792 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1793 * propagate up to clk's parent; whether or not this happens depends on the
1794 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1795 * after calling .round_rate then upstream parent propagation is ignored. If
1796 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001797 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001798 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1799 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001800 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001801 * Rate changes are accomplished via tree traversal that also recalculates the
1802 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001803 *
1804 * Returns 0 on success, -EERROR otherwise.
1805 */
1806int clk_set_rate(struct clk *clk, unsigned long rate)
1807{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001808 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001809
Mike Turquette89ac8d72013-08-21 23:58:09 -07001810 if (!clk)
1811 return 0;
1812
Mike Turquetteb24764902012-03-15 23:11:19 -07001813 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001814 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001815
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001816 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001817
Mike Turquetteeab89f62013-03-28 13:59:01 -07001818 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001819
1820 return ret;
1821}
1822EXPORT_SYMBOL_GPL(clk_set_rate);
1823
1824/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001825 * clk_set_rate_range - set a rate range for a clock source
1826 * @clk: clock source
1827 * @min: desired minimum clock rate in Hz, inclusive
1828 * @max: desired maximum clock rate in Hz, inclusive
1829 *
1830 * Returns success (0) or negative errno.
1831 */
1832int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1833{
1834 int ret = 0;
1835
1836 if (!clk)
1837 return 0;
1838
1839 if (min > max) {
1840 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1841 __func__, clk->core->name, clk->dev_id, clk->con_id,
1842 min, max);
1843 return -EINVAL;
1844 }
1845
1846 clk_prepare_lock();
1847
1848 if (min != clk->min_rate || max != clk->max_rate) {
1849 clk->min_rate = min;
1850 clk->max_rate = max;
1851 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1852 }
1853
1854 clk_prepare_unlock();
1855
1856 return ret;
1857}
1858EXPORT_SYMBOL_GPL(clk_set_rate_range);
1859
1860/**
1861 * clk_set_min_rate - set a minimum clock rate for a clock source
1862 * @clk: clock source
1863 * @rate: desired minimum clock rate in Hz, inclusive
1864 *
1865 * Returns success (0) or negative errno.
1866 */
1867int clk_set_min_rate(struct clk *clk, unsigned long rate)
1868{
1869 if (!clk)
1870 return 0;
1871
1872 return clk_set_rate_range(clk, rate, clk->max_rate);
1873}
1874EXPORT_SYMBOL_GPL(clk_set_min_rate);
1875
1876/**
1877 * clk_set_max_rate - set a maximum clock rate for a clock source
1878 * @clk: clock source
1879 * @rate: desired maximum clock rate in Hz, inclusive
1880 *
1881 * Returns success (0) or negative errno.
1882 */
1883int clk_set_max_rate(struct clk *clk, unsigned long rate)
1884{
1885 if (!clk)
1886 return 0;
1887
1888 return clk_set_rate_range(clk, clk->min_rate, rate);
1889}
1890EXPORT_SYMBOL_GPL(clk_set_max_rate);
1891
1892/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001893 * clk_get_parent - return the parent of a clk
1894 * @clk: the clk whose parent gets returned
1895 *
1896 * Simply returns clk->parent. Returns NULL if clk is NULL.
1897 */
1898struct clk *clk_get_parent(struct clk *clk)
1899{
1900 struct clk *parent;
1901
Mike Turquetteeab89f62013-03-28 13:59:01 -07001902 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001903 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001904 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001905
1906 return parent;
1907}
1908EXPORT_SYMBOL_GPL(clk_get_parent);
1909
1910/*
1911 * .get_parent is mandatory for clocks with multiple possible parents. It is
1912 * optional for single-parent clocks. Always call .get_parent if it is
1913 * available and WARN if it is missing for multi-parent clocks.
1914 *
1915 * For single-parent clocks without .get_parent, first check to see if the
1916 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001917 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001918 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001919static struct clk_core *__clk_init_parent(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001920{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001921 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001922 u8 index;
1923
1924 /* handle the trivial cases */
1925
1926 if (!clk->num_parents)
1927 goto out;
1928
1929 if (clk->num_parents == 1) {
1930 if (IS_ERR_OR_NULL(clk->parent))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001931 clk->parent = clk_core_lookup(clk->parent_names[0]);
Mike Turquetteb24764902012-03-15 23:11:19 -07001932 ret = clk->parent;
1933 goto out;
1934 }
1935
1936 if (!clk->ops->get_parent) {
1937 WARN(!clk->ops->get_parent,
1938 "%s: multi-parent clocks must implement .get_parent\n",
1939 __func__);
1940 goto out;
1941 };
1942
1943 /*
1944 * Do our best to cache parent clocks in clk->parents. This prevents
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001945 * unnecessary and expensive lookups. We don't set clk->parent here;
1946 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001947 */
1948
1949 index = clk->ops->get_parent(clk->hw);
1950
1951 if (!clk->parents)
1952 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001953 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001954 GFP_KERNEL);
1955
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001956 ret = clk_core_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001957
1958out:
1959 return ret;
1960}
1961
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001962static void clk_core_reparent(struct clk_core *clk,
1963 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001964{
1965 clk_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001966 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001967 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1968}
1969
Mike Turquetteb24764902012-03-15 23:11:19 -07001970/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001971 * clk_has_parent - check if a clock is a possible parent for another
1972 * @clk: clock source
1973 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001974 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001975 * This function can be used in drivers that need to check that a clock can be
1976 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001977 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001978 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001979 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001980bool clk_has_parent(struct clk *clk, struct clk *parent)
1981{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001982 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001983 unsigned int i;
1984
1985 /* NULL clocks should be nops, so return success if either is NULL. */
1986 if (!clk || !parent)
1987 return true;
1988
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001989 core = clk->core;
1990 parent_core = parent->core;
1991
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001992 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001993 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001994 return true;
1995
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001996 for (i = 0; i < core->num_parents; i++)
1997 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001998 return true;
1999
2000 return false;
2001}
2002EXPORT_SYMBOL_GPL(clk_has_parent);
2003
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002004static int clk_core_set_parent(struct clk_core *clk, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002005{
2006 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002007 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002008 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002009
Mike Turquette89ac8d72013-08-21 23:58:09 -07002010 if (!clk)
2011 return 0;
2012
Ulf Hansson031dcc92013-04-02 23:09:38 +02002013 /* verify ops for for multi-parent clks */
2014 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07002015 return -ENOSYS;
2016
2017 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002018 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002019
2020 if (clk->parent == parent)
2021 goto out;
2022
Ulf Hansson031dcc92013-04-02 23:09:38 +02002023 /* check that we are allowed to re-parent if the clock is in use */
2024 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
2025 ret = -EBUSY;
2026 goto out;
2027 }
2028
2029 /* try finding the new parent index */
2030 if (parent) {
2031 p_index = clk_fetch_parent_index(clk, parent);
2032 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002033 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002034 pr_debug("%s: clk %s can not be parent of clk %s\n",
2035 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002036 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002037 goto out;
2038 }
2039 }
2040
Mike Turquetteb24764902012-03-15 23:11:19 -07002041 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07002042 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002043
2044 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002045 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002046 goto out;
2047
Ulf Hansson031dcc92013-04-02 23:09:38 +02002048 /* do the re-parent */
2049 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002050
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002051 /* propagate rate an accuracy recalculation accordingly */
2052 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07002053 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002054 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02002055 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002056 __clk_recalc_accuracies(clk);
2057 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002058
2059out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002060 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002061
2062 return ret;
2063}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002064
2065/**
2066 * clk_set_parent - switch the parent of a mux clk
2067 * @clk: the mux clk whose input we are switching
2068 * @parent: the new input to clk
2069 *
2070 * Re-parent clk to use parent as its new input source. If clk is in
2071 * prepared state, the clk will get enabled for the duration of this call. If
2072 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2073 * that, the reparenting is glitchy in hardware, etc), use the
2074 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2075 *
2076 * After successfully changing clk's parent clk_set_parent will update the
2077 * clk topology, sysfs topology and propagate rate recalculation via
2078 * __clk_recalc_rates.
2079 *
2080 * Returns 0 on success, -EERROR otherwise.
2081 */
2082int clk_set_parent(struct clk *clk, struct clk *parent)
2083{
2084 if (!clk)
2085 return 0;
2086
2087 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2088}
Mike Turquetteb24764902012-03-15 23:11:19 -07002089EXPORT_SYMBOL_GPL(clk_set_parent);
2090
2091/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002092 * clk_set_phase - adjust the phase shift of a clock signal
2093 * @clk: clock signal source
2094 * @degrees: number of degrees the signal is shifted
2095 *
2096 * Shifts the phase of a clock signal by the specified
2097 * degrees. Returns 0 on success, -EERROR otherwise.
2098 *
2099 * This function makes no distinction about the input or reference
2100 * signal that we adjust the clock signal phase against. For example
2101 * phase locked-loop clock signal generators we may shift phase with
2102 * respect to feedback clock signal input, but for other cases the
2103 * clock phase may be shifted with respect to some other, unspecified
2104 * signal.
2105 *
2106 * Additionally the concept of phase shift does not propagate through
2107 * the clock tree hierarchy, which sets it apart from clock rates and
2108 * clock accuracy. A parent clock phase attribute does not have an
2109 * impact on the phase attribute of a child clock.
2110 */
2111int clk_set_phase(struct clk *clk, int degrees)
2112{
2113 int ret = 0;
2114
2115 if (!clk)
2116 goto out;
2117
2118 /* sanity check degrees */
2119 degrees %= 360;
2120 if (degrees < 0)
2121 degrees += 360;
2122
2123 clk_prepare_lock();
2124
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002125 if (!clk->core->ops->set_phase)
Mike Turquettee59c5372014-02-18 21:21:25 -08002126 goto out_unlock;
2127
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002128 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002129
2130 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002131 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002132
2133out_unlock:
2134 clk_prepare_unlock();
2135
2136out:
2137 return ret;
2138}
Maxime Ripard9767b042015-01-20 22:23:43 +01002139EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002140
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002141static int clk_core_get_phase(struct clk_core *clk)
Mike Turquettee59c5372014-02-18 21:21:25 -08002142{
2143 int ret = 0;
2144
2145 if (!clk)
2146 goto out;
2147
2148 clk_prepare_lock();
2149 ret = clk->phase;
2150 clk_prepare_unlock();
2151
2152out:
2153 return ret;
2154}
Maxime Ripard9767b042015-01-20 22:23:43 +01002155EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002156
2157/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002158 * clk_get_phase - return the phase shift of a clock signal
2159 * @clk: clock signal source
2160 *
2161 * Returns the phase shift of a clock node in degrees, otherwise returns
2162 * -EERROR.
2163 */
2164int clk_get_phase(struct clk *clk)
2165{
2166 if (!clk)
2167 return 0;
2168
2169 return clk_core_get_phase(clk->core);
2170}
Mike Turquetteb24764902012-03-15 23:11:19 -07002171
2172/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002173 * clk_is_match - check if two clk's point to the same hardware clock
2174 * @p: clk compared against q
2175 * @q: clk compared against p
2176 *
2177 * Returns true if the two struct clk pointers both point to the same hardware
2178 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2179 * share the same struct clk_core object.
2180 *
2181 * Returns false otherwise. Note that two NULL clks are treated as matching.
2182 */
2183bool clk_is_match(const struct clk *p, const struct clk *q)
2184{
2185 /* trivial case: identical struct clk's or both NULL */
2186 if (p == q)
2187 return true;
2188
2189 /* true if clk->core pointers match. Avoid derefing garbage */
2190 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2191 if (p->core == q->core)
2192 return true;
2193
2194 return false;
2195}
2196EXPORT_SYMBOL_GPL(clk_is_match);
2197
2198/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002199 * __clk_init - initialize the data structures in a struct clk
2200 * @dev: device initializing this clk, placeholder for now
2201 * @clk: clk being initialized
2202 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002203 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002204 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002205 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002206static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002207{
Mike Turquetted1302a32012-03-29 14:30:40 -07002208 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002209 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002210 struct hlist_node *tmp2;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002211 struct clk_core *clk;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002212 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002213
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002214 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002215 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002216
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002217 clk = clk_user->core;
2218
Mike Turquetteeab89f62013-03-28 13:59:01 -07002219 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002220
2221 /* check to see if a clock with this name is already registered */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002222 if (clk_core_lookup(clk->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002223 pr_debug("%s: clk %s already initialized\n",
2224 __func__, clk->name);
2225 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002226 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002227 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002228
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002229 /* check that clk_ops are sane. See Documentation/clk.txt */
2230 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01002231 !((clk->ops->round_rate || clk->ops->determine_rate) &&
2232 clk->ops->recalc_rate)) {
2233 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002234 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002235 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002236 goto out;
2237 }
2238
2239 if (clk->ops->set_parent && !clk->ops->get_parent) {
2240 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
2241 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002242 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002243 goto out;
2244 }
2245
Stephen Boyd3fa22522014-01-15 10:47:22 -08002246 if (clk->ops->set_rate_and_parent &&
2247 !(clk->ops->set_parent && clk->ops->set_rate)) {
2248 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
2249 __func__, clk->name);
2250 ret = -EINVAL;
2251 goto out;
2252 }
2253
Mike Turquetteb24764902012-03-15 23:11:19 -07002254 /* throw a WARN if any entries in parent_names are NULL */
2255 for (i = 0; i < clk->num_parents; i++)
2256 WARN(!clk->parent_names[i],
2257 "%s: invalid NULL in %s's .parent_names\n",
2258 __func__, clk->name);
2259
2260 /*
2261 * Allocate an array of struct clk *'s to avoid unnecessary string
2262 * look-ups of clk's possible parents. This can fail for clocks passed
2263 * in to clk_init during early boot; thus any access to clk->parents[]
2264 * must always check for a NULL pointer and try to populate it if
2265 * necessary.
2266 *
2267 * If clk->parents is not NULL we skip this entire block. This allows
2268 * for clock drivers to statically initialize clk->parents.
2269 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05302270 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002271 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
2272 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002273 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002274 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002275 * clk_init'd; thus any access to clk->parents[] must check
2276 * for a NULL pointer. We can always perform lazy lookups for
2277 * missing parents later on.
2278 */
2279 if (clk->parents)
2280 for (i = 0; i < clk->num_parents; i++)
2281 clk->parents[i] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002282 clk_core_lookup(clk->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002283 }
2284
2285 clk->parent = __clk_init_parent(clk);
2286
2287 /*
2288 * Populate clk->parent if parent has already been __clk_init'd. If
2289 * parent has not yet been __clk_init'd then place clk in the orphan
2290 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2291 * clk list.
2292 *
2293 * Every time a new clk is clk_init'd then we walk the list of orphan
2294 * clocks and re-parent any that are children of the clock currently
2295 * being clk_init'd.
2296 */
2297 if (clk->parent)
2298 hlist_add_head(&clk->child_node,
2299 &clk->parent->children);
2300 else if (clk->flags & CLK_IS_ROOT)
2301 hlist_add_head(&clk->child_node, &clk_root_list);
2302 else
2303 hlist_add_head(&clk->child_node, &clk_orphan_list);
2304
2305 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002306 * Set clk's accuracy. The preferred method is to use
2307 * .recalc_accuracy. For simple clocks and lazy developers the default
2308 * fallback is to use the parent's accuracy. If a clock doesn't have a
2309 * parent (or is orphaned) then accuracy is set to zero (perfect
2310 * clock).
2311 */
2312 if (clk->ops->recalc_accuracy)
2313 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
2314 __clk_get_accuracy(clk->parent));
2315 else if (clk->parent)
2316 clk->accuracy = clk->parent->accuracy;
2317 else
2318 clk->accuracy = 0;
2319
2320 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002321 * Set clk's phase.
2322 * Since a phase is by definition relative to its parent, just
2323 * query the current clock phase, or just assume it's in phase.
2324 */
2325 if (clk->ops->get_phase)
2326 clk->phase = clk->ops->get_phase(clk->hw);
2327 else
2328 clk->phase = 0;
2329
2330 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002331 * Set clk's rate. The preferred method is to use .recalc_rate. For
2332 * simple clocks and lazy developers the default fallback is to use the
2333 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2334 * then rate is set to zero.
2335 */
2336 if (clk->ops->recalc_rate)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002337 rate = clk->ops->recalc_rate(clk->hw,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002338 clk_core_get_rate_nolock(clk->parent));
Mike Turquetteb24764902012-03-15 23:11:19 -07002339 else if (clk->parent)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002340 rate = clk->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002341 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002342 rate = 0;
2343 clk->rate = clk->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002344
2345 /*
2346 * walk the list of orphan clocks and reparent any that are children of
2347 * this clock
2348 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002349 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002350 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002351 i = orphan->ops->get_parent(orphan->hw);
2352 if (!strcmp(clk->name, orphan->parent_names[i]))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002353 clk_core_reparent(orphan, clk);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002354 continue;
2355 }
2356
Mike Turquetteb24764902012-03-15 23:11:19 -07002357 for (i = 0; i < orphan->num_parents; i++)
2358 if (!strcmp(clk->name, orphan->parent_names[i])) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002359 clk_core_reparent(orphan, clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07002360 break;
2361 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002362 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002363
2364 /*
2365 * optional platform-specific magic
2366 *
2367 * The .init callback is not used by any of the basic clock types, but
2368 * exists for weird hardware that must perform initialization magic.
2369 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002370 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002371 */
2372 if (clk->ops->init)
2373 clk->ops->init(clk->hw);
2374
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002375 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002376out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002377 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002378
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002379 if (!ret)
2380 clk_debug_register(clk);
2381
Mike Turquetted1302a32012-03-29 14:30:40 -07002382 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002383}
2384
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002385struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2386 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002387{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002388 struct clk *clk;
2389
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002390 /* This is to allow this function to be chained to others */
2391 if (!hw || IS_ERR(hw))
2392 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002393
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002394 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2395 if (!clk)
2396 return ERR_PTR(-ENOMEM);
2397
2398 clk->core = hw->core;
2399 clk->dev_id = dev_id;
2400 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002401 clk->max_rate = ULONG_MAX;
2402
2403 clk_prepare_lock();
2404 hlist_add_head(&clk->child_node, &hw->core->clks);
2405 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002406
2407 return clk;
2408}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002409
Stephen Boyd73e0e492015-02-06 11:42:43 -08002410void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002411{
2412 clk_prepare_lock();
2413 hlist_del(&clk->child_node);
2414 clk_prepare_unlock();
2415
2416 kfree(clk);
2417}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002418
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002419/**
2420 * clk_register - allocate a new clock, register it and return an opaque cookie
2421 * @dev: device that is registering this clock
2422 * @hw: link to hardware-specific clock data
2423 *
2424 * clk_register is the primary interface for populating the clock tree with new
2425 * clock nodes. It returns a pointer to the newly allocated struct clk which
2426 * cannot be dereferenced by driver code but may be used in conjuction with the
2427 * rest of the clock API. In the event of an error clk_register will return an
2428 * error code; drivers must test for an error code after calling clk_register.
2429 */
2430struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002431{
Mike Turquetted1302a32012-03-29 14:30:40 -07002432 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002433 struct clk_core *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002434
2435 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2436 if (!clk) {
2437 pr_err("%s: could not allocate clk\n", __func__);
2438 ret = -ENOMEM;
2439 goto fail_out;
2440 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002441
Andrzej Hajda612936f2015-02-13 14:36:33 -08002442 clk->name = kstrdup_const(hw->init->name, GFP_KERNEL);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002443 if (!clk->name) {
2444 pr_err("%s: could not allocate clk->name\n", __func__);
2445 ret = -ENOMEM;
2446 goto fail_name;
2447 }
2448 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002449 if (dev && dev->driver)
2450 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07002451 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002452 clk->flags = hw->init->flags;
2453 clk->num_parents = hw->init->num_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002454 hw->core = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002455
Mike Turquetted1302a32012-03-29 14:30:40 -07002456 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002457 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
2458 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002459
Mike Turquetted1302a32012-03-29 14:30:40 -07002460 if (!clk->parent_names) {
2461 pr_err("%s: could not allocate clk->parent_names\n", __func__);
2462 ret = -ENOMEM;
2463 goto fail_parent_names;
2464 }
2465
2466
2467 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002468 for (i = 0; i < clk->num_parents; i++) {
Andrzej Hajda612936f2015-02-13 14:36:33 -08002469 clk->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002470 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002471 if (!clk->parent_names[i]) {
2472 pr_err("%s: could not copy parent_names\n", __func__);
2473 ret = -ENOMEM;
2474 goto fail_parent_names_copy;
2475 }
2476 }
2477
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002478 INIT_HLIST_HEAD(&clk->clks);
2479
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002480 hw->clk = __clk_create_clk(hw, NULL, NULL);
2481 if (IS_ERR(hw->clk)) {
2482 pr_err("%s: could not allocate per-user clk\n", __func__);
2483 ret = PTR_ERR(hw->clk);
2484 goto fail_parent_names_copy;
2485 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002486
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002487 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002488 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002489 return hw->clk;
2490
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002491 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002492 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002493
2494fail_parent_names_copy:
2495 while (--i >= 0)
Andrzej Hajda612936f2015-02-13 14:36:33 -08002496 kfree_const(clk->parent_names[i]);
Mike Turquetted1302a32012-03-29 14:30:40 -07002497 kfree(clk->parent_names);
2498fail_parent_names:
Andrzej Hajda612936f2015-02-13 14:36:33 -08002499 kfree_const(clk->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002500fail_name:
Mike Turquetted1302a32012-03-29 14:30:40 -07002501 kfree(clk);
2502fail_out:
2503 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002504}
2505EXPORT_SYMBOL_GPL(clk_register);
2506
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002507/*
2508 * Free memory allocated for a clock.
2509 * Caller must hold prepare_lock.
2510 */
2511static void __clk_release(struct kref *ref)
2512{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002513 struct clk_core *clk = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002514 int i = clk->num_parents;
2515
2516 kfree(clk->parents);
2517 while (--i >= 0)
Andrzej Hajda612936f2015-02-13 14:36:33 -08002518 kfree_const(clk->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002519
2520 kfree(clk->parent_names);
Andrzej Hajda612936f2015-02-13 14:36:33 -08002521 kfree_const(clk->name);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002522 kfree(clk);
2523}
2524
2525/*
2526 * Empty clk_ops for unregistered clocks. These are used temporarily
2527 * after clk_unregister() was called on a clock and until last clock
2528 * consumer calls clk_put() and the struct clk object is freed.
2529 */
2530static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2531{
2532 return -ENXIO;
2533}
2534
2535static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2536{
2537 WARN_ON_ONCE(1);
2538}
2539
2540static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2541 unsigned long parent_rate)
2542{
2543 return -ENXIO;
2544}
2545
2546static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2547{
2548 return -ENXIO;
2549}
2550
2551static const struct clk_ops clk_nodrv_ops = {
2552 .enable = clk_nodrv_prepare_enable,
2553 .disable = clk_nodrv_disable_unprepare,
2554 .prepare = clk_nodrv_prepare_enable,
2555 .unprepare = clk_nodrv_disable_unprepare,
2556 .set_rate = clk_nodrv_set_rate,
2557 .set_parent = clk_nodrv_set_parent,
2558};
2559
Mark Brown1df5c932012-04-18 09:07:12 +01002560/**
2561 * clk_unregister - unregister a currently registered clock
2562 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002563 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002564void clk_unregister(struct clk *clk)
2565{
2566 unsigned long flags;
2567
Stephen Boyd6314b672014-09-04 23:37:49 -07002568 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2569 return;
2570
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002571 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002572
2573 clk_prepare_lock();
2574
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002575 if (clk->core->ops == &clk_nodrv_ops) {
2576 pr_err("%s: unregistered clock: %s\n", __func__,
2577 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002578 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002579 }
2580 /*
2581 * Assign empty clock ops for consumers that might still hold
2582 * a reference to this clock.
2583 */
2584 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002585 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002586 clk_enable_unlock(flags);
2587
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002588 if (!hlist_empty(&clk->core->children)) {
2589 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002590 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002591
2592 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002593 hlist_for_each_entry_safe(child, t, &clk->core->children,
2594 child_node)
2595 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002596 }
2597
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002598 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002599
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002600 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002601 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002602 __func__, clk->core->name);
2603 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002604
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002605 clk_prepare_unlock();
2606}
Mark Brown1df5c932012-04-18 09:07:12 +01002607EXPORT_SYMBOL_GPL(clk_unregister);
2608
Stephen Boyd46c87732012-09-24 13:38:04 -07002609static void devm_clk_release(struct device *dev, void *res)
2610{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002611 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002612}
2613
2614/**
2615 * devm_clk_register - resource managed clk_register()
2616 * @dev: device that is registering this clock
2617 * @hw: link to hardware-specific clock data
2618 *
2619 * Managed clk_register(). Clocks returned from this function are
2620 * automatically clk_unregister()ed on driver detach. See clk_register() for
2621 * more information.
2622 */
2623struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2624{
2625 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002626 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002627
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002628 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2629 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002630 return ERR_PTR(-ENOMEM);
2631
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002632 clk = clk_register(dev, hw);
2633 if (!IS_ERR(clk)) {
2634 *clkp = clk;
2635 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002636 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002637 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002638 }
2639
2640 return clk;
2641}
2642EXPORT_SYMBOL_GPL(devm_clk_register);
2643
2644static int devm_clk_match(struct device *dev, void *res, void *data)
2645{
2646 struct clk *c = res;
2647 if (WARN_ON(!c))
2648 return 0;
2649 return c == data;
2650}
2651
2652/**
2653 * devm_clk_unregister - resource managed clk_unregister()
2654 * @clk: clock to unregister
2655 *
2656 * Deallocate a clock allocated with devm_clk_register(). Normally
2657 * this function will not need to be called and the resource management
2658 * code will ensure that the resource is freed.
2659 */
2660void devm_clk_unregister(struct device *dev, struct clk *clk)
2661{
2662 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2663}
2664EXPORT_SYMBOL_GPL(devm_clk_unregister);
2665
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002666/*
2667 * clkdev helpers
2668 */
2669int __clk_get(struct clk *clk)
2670{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002671 struct clk_core *core = !clk ? NULL : clk->core;
2672
2673 if (core) {
2674 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002675 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002676
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002677 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002678 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002679 return 1;
2680}
2681
2682void __clk_put(struct clk *clk)
2683{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002684 struct module *owner;
2685
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002686 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002687 return;
2688
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002689 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002690
2691 hlist_del(&clk->child_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002692 if (clk->min_rate > clk->core->req_rate ||
2693 clk->max_rate < clk->core->req_rate)
2694 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2695
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002696 owner = clk->core->owner;
2697 kref_put(&clk->core->ref, __clk_release);
2698
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002699 clk_prepare_unlock();
2700
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002701 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002702
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002703 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002704}
2705
Mike Turquetteb24764902012-03-15 23:11:19 -07002706/*** clk rate change notifiers ***/
2707
2708/**
2709 * clk_notifier_register - add a clk rate change notifier
2710 * @clk: struct clk * to watch
2711 * @nb: struct notifier_block * with callback info
2712 *
2713 * Request notification when clk's rate changes. This uses an SRCU
2714 * notifier because we want it to block and notifier unregistrations are
2715 * uncommon. The callbacks associated with the notifier must not
2716 * re-enter into the clk framework by calling any top-level clk APIs;
2717 * this will cause a nested prepare_lock mutex.
2718 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002719 * In all notification cases cases (pre, post and abort rate change) the
2720 * original clock rate is passed to the callback via struct
2721 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002722 * clk_notifier_data.new_rate.
2723 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002724 * clk_notifier_register() must be called from non-atomic context.
2725 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2726 * allocation failure; otherwise, passes along the return value of
2727 * srcu_notifier_chain_register().
2728 */
2729int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2730{
2731 struct clk_notifier *cn;
2732 int ret = -ENOMEM;
2733
2734 if (!clk || !nb)
2735 return -EINVAL;
2736
Mike Turquetteeab89f62013-03-28 13:59:01 -07002737 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002738
2739 /* search the list of notifiers for this clk */
2740 list_for_each_entry(cn, &clk_notifier_list, node)
2741 if (cn->clk == clk)
2742 break;
2743
2744 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2745 if (cn->clk != clk) {
2746 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2747 if (!cn)
2748 goto out;
2749
2750 cn->clk = clk;
2751 srcu_init_notifier_head(&cn->notifier_head);
2752
2753 list_add(&cn->node, &clk_notifier_list);
2754 }
2755
2756 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2757
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002758 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002759
2760out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002761 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002762
2763 return ret;
2764}
2765EXPORT_SYMBOL_GPL(clk_notifier_register);
2766
2767/**
2768 * clk_notifier_unregister - remove a clk rate change notifier
2769 * @clk: struct clk *
2770 * @nb: struct notifier_block * with callback info
2771 *
2772 * Request no further notification for changes to 'clk' and frees memory
2773 * allocated in clk_notifier_register.
2774 *
2775 * Returns -EINVAL if called with null arguments; otherwise, passes
2776 * along the return value of srcu_notifier_chain_unregister().
2777 */
2778int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2779{
2780 struct clk_notifier *cn = NULL;
2781 int ret = -EINVAL;
2782
2783 if (!clk || !nb)
2784 return -EINVAL;
2785
Mike Turquetteeab89f62013-03-28 13:59:01 -07002786 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002787
2788 list_for_each_entry(cn, &clk_notifier_list, node)
2789 if (cn->clk == clk)
2790 break;
2791
2792 if (cn->clk == clk) {
2793 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2794
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002795 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002796
2797 /* XXX the notifier code should handle this better */
2798 if (!cn->notifier_head.head) {
2799 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002800 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002801 kfree(cn);
2802 }
2803
2804 } else {
2805 ret = -ENOENT;
2806 }
2807
Mike Turquetteeab89f62013-03-28 13:59:01 -07002808 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002809
2810 return ret;
2811}
2812EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002813
2814#ifdef CONFIG_OF
2815/**
2816 * struct of_clk_provider - Clock provider registration structure
2817 * @link: Entry in global list of clock providers
2818 * @node: Pointer to device tree node of clock provider
2819 * @get: Get clock callback. Returns NULL or a struct clk for the
2820 * given clock specifier
2821 * @data: context pointer to be passed into @get callback
2822 */
2823struct of_clk_provider {
2824 struct list_head link;
2825
2826 struct device_node *node;
2827 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2828 void *data;
2829};
2830
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302831static const struct of_device_id __clk_of_table_sentinel
2832 __used __section(__clk_of_table_end);
2833
Grant Likely766e6a42012-04-09 14:50:06 -05002834static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002835static DEFINE_MUTEX(of_clk_mutex);
2836
2837/* of_clk_provider list locking helpers */
2838void of_clk_lock(void)
2839{
2840 mutex_lock(&of_clk_mutex);
2841}
2842
2843void of_clk_unlock(void)
2844{
2845 mutex_unlock(&of_clk_mutex);
2846}
Grant Likely766e6a42012-04-09 14:50:06 -05002847
2848struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2849 void *data)
2850{
2851 return data;
2852}
2853EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2854
Shawn Guo494bfec2012-08-22 21:36:27 +08002855struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2856{
2857 struct clk_onecell_data *clk_data = data;
2858 unsigned int idx = clkspec->args[0];
2859
2860 if (idx >= clk_data->clk_num) {
2861 pr_err("%s: invalid clock index %d\n", __func__, idx);
2862 return ERR_PTR(-EINVAL);
2863 }
2864
2865 return clk_data->clks[idx];
2866}
2867EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2868
Grant Likely766e6a42012-04-09 14:50:06 -05002869/**
2870 * of_clk_add_provider() - Register a clock provider for a node
2871 * @np: Device node pointer associated with clock provider
2872 * @clk_src_get: callback for decoding clock
2873 * @data: context pointer for @clk_src_get callback.
2874 */
2875int of_clk_add_provider(struct device_node *np,
2876 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2877 void *data),
2878 void *data)
2879{
2880 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002881 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002882
2883 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2884 if (!cp)
2885 return -ENOMEM;
2886
2887 cp->node = of_node_get(np);
2888 cp->data = data;
2889 cp->get = clk_src_get;
2890
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002891 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002892 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002893 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002894 pr_debug("Added clock from %s\n", np->full_name);
2895
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002896 ret = of_clk_set_defaults(np, true);
2897 if (ret < 0)
2898 of_clk_del_provider(np);
2899
2900 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002901}
2902EXPORT_SYMBOL_GPL(of_clk_add_provider);
2903
2904/**
2905 * of_clk_del_provider() - Remove a previously registered clock provider
2906 * @np: Device node pointer associated with clock provider
2907 */
2908void of_clk_del_provider(struct device_node *np)
2909{
2910 struct of_clk_provider *cp;
2911
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002912 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002913 list_for_each_entry(cp, &of_clk_providers, link) {
2914 if (cp->node == np) {
2915 list_del(&cp->link);
2916 of_node_put(cp->node);
2917 kfree(cp);
2918 break;
2919 }
2920 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002921 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002922}
2923EXPORT_SYMBOL_GPL(of_clk_del_provider);
2924
Stephen Boyd73e0e492015-02-06 11:42:43 -08002925struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2926 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002927{
2928 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002929 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002930
2931 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002932 list_for_each_entry(provider, &of_clk_providers, link) {
2933 if (provider->node == clkspec->np)
2934 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002935 if (!IS_ERR(clk)) {
2936 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2937 con_id);
2938
2939 if (!IS_ERR(clk) && !__clk_get(clk)) {
2940 __clk_free_clk(clk);
2941 clk = ERR_PTR(-ENOENT);
2942 }
2943
Grant Likely766e6a42012-04-09 14:50:06 -05002944 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002945 }
Grant Likely766e6a42012-04-09 14:50:06 -05002946 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002947
2948 return clk;
2949}
2950
2951struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2952{
2953 struct clk *clk;
2954
2955 mutex_lock(&of_clk_mutex);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002956 clk = __of_clk_get_from_provider(clkspec, NULL, __func__);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002957 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002958
2959 return clk;
2960}
2961
Mike Turquettef6102742013-10-07 23:12:13 -07002962int of_clk_get_parent_count(struct device_node *np)
2963{
2964 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2965}
2966EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2967
Grant Likely766e6a42012-04-09 14:50:06 -05002968const char *of_clk_get_parent_name(struct device_node *np, int index)
2969{
2970 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002971 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002972 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002973 const __be32 *vp;
2974 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002975 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002976 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002977
2978 if (index < 0)
2979 return NULL;
2980
2981 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2982 &clkspec);
2983 if (rc)
2984 return NULL;
2985
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002986 index = clkspec.args_count ? clkspec.args[0] : 0;
2987 count = 0;
2988
2989 /* if there is an indices property, use it to transfer the index
2990 * specified into an array offset for the clock-output-names property.
2991 */
2992 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2993 if (index == pv) {
2994 index = count;
2995 break;
2996 }
2997 count++;
2998 }
2999
Grant Likely766e6a42012-04-09 14:50:06 -05003000 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003001 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003002 &clk_name) < 0)
3003 clk_name = clkspec.np->name;
3004
3005 of_node_put(clkspec.np);
3006 return clk_name;
3007}
3008EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3009
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003010struct clock_provider {
3011 of_clk_init_cb_t clk_init_cb;
3012 struct device_node *np;
3013 struct list_head node;
3014};
3015
3016static LIST_HEAD(clk_provider_list);
3017
3018/*
3019 * This function looks for a parent clock. If there is one, then it
3020 * checks that the provider for this parent clock was initialized, in
3021 * this case the parent clock will be ready.
3022 */
3023static int parent_ready(struct device_node *np)
3024{
3025 int i = 0;
3026
3027 while (true) {
3028 struct clk *clk = of_clk_get(np, i);
3029
3030 /* this parent is ready we can check the next one */
3031 if (!IS_ERR(clk)) {
3032 clk_put(clk);
3033 i++;
3034 continue;
3035 }
3036
3037 /* at least one parent is not ready, we exit now */
3038 if (PTR_ERR(clk) == -EPROBE_DEFER)
3039 return 0;
3040
3041 /*
3042 * Here we make assumption that the device tree is
3043 * written correctly. So an error means that there is
3044 * no more parent. As we didn't exit yet, then the
3045 * previous parent are ready. If there is no clock
3046 * parent, no need to wait for them, then we can
3047 * consider their absence as being ready
3048 */
3049 return 1;
3050 }
3051}
3052
Grant Likely766e6a42012-04-09 14:50:06 -05003053/**
3054 * of_clk_init() - Scan and init clock providers from the DT
3055 * @matches: array of compatible values and init functions for providers.
3056 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003057 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003058 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003059 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003060 */
3061void __init of_clk_init(const struct of_device_id *matches)
3062{
Alex Elder7f7ed582013-08-22 11:31:31 -05003063 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003064 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003065 struct clock_provider *clk_provider, *next;
3066 bool is_init_done;
3067 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003068
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303069 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003070 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303071
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003072 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003073 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003074 struct clock_provider *parent =
3075 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3076
3077 parent->clk_init_cb = match->data;
3078 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003079 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003080 }
3081
3082 while (!list_empty(&clk_provider_list)) {
3083 is_init_done = false;
3084 list_for_each_entry_safe(clk_provider, next,
3085 &clk_provider_list, node) {
3086 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003087
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003088 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003089 of_clk_set_defaults(clk_provider->np, true);
3090
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003091 list_del(&clk_provider->node);
3092 kfree(clk_provider);
3093 is_init_done = true;
3094 }
3095 }
3096
3097 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003098 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003099 * remaining providers during the last loop, so now we
3100 * initialize all the remaining ones unconditionally
3101 * in case the clock parent was not mandatory
3102 */
3103 if (!is_init_done)
3104 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003105 }
3106}
3107#endif