blob: 8d36100c00ce4eb8d0685693542f70c370927196 [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
Stephen Boydd6968fc2015-04-30 13:54:13 -070040static long clk_core_get_accuracy(struct clk_core *core);
41static unsigned long clk_core_get_rate(struct clk_core *core);
42static int clk_core_get_phase(struct clk_core *core);
43static bool clk_core_is_prepared(struct clk_core *core);
44static bool clk_core_is_enabled(struct clk_core *core);
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
Stephen Boyddfc202e2015-02-02 14:37:41 -080080#define CREATE_TRACE_POINTS
81#include <trace/events/clk.h>
82
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083struct clk {
84 struct clk_core *core;
85 const char *dev_id;
86 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010087 unsigned long min_rate;
88 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080089 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090};
91
Mike Turquetteeab89f62013-03-28 13:59:01 -070092/*** locking ***/
93static void clk_prepare_lock(void)
94{
Mike Turquette533ddeb2013-03-28 13:59:02 -070095 if (!mutex_trylock(&prepare_lock)) {
96 if (prepare_owner == current) {
97 prepare_refcnt++;
98 return;
99 }
100 mutex_lock(&prepare_lock);
101 }
102 WARN_ON_ONCE(prepare_owner != NULL);
103 WARN_ON_ONCE(prepare_refcnt != 0);
104 prepare_owner = current;
105 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700106}
107
108static void clk_prepare_unlock(void)
109{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700110 WARN_ON_ONCE(prepare_owner != current);
111 WARN_ON_ONCE(prepare_refcnt == 0);
112
113 if (--prepare_refcnt)
114 return;
115 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700116 mutex_unlock(&prepare_lock);
117}
118
119static unsigned long clk_enable_lock(void)
120{
121 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700122
123 if (!spin_trylock_irqsave(&enable_lock, flags)) {
124 if (enable_owner == current) {
125 enable_refcnt++;
126 return flags;
127 }
128 spin_lock_irqsave(&enable_lock, flags);
129 }
130 WARN_ON_ONCE(enable_owner != NULL);
131 WARN_ON_ONCE(enable_refcnt != 0);
132 enable_owner = current;
133 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700134 return flags;
135}
136
137static void clk_enable_unlock(unsigned long flags)
138{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700139 WARN_ON_ONCE(enable_owner != current);
140 WARN_ON_ONCE(enable_refcnt == 0);
141
142 if (--enable_refcnt)
143 return;
144 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700145 spin_unlock_irqrestore(&enable_lock, flags);
146}
147
Mike Turquetteb24764902012-03-15 23:11:19 -0700148/*** debugfs support ***/
149
Mike Turquetteea72dc22013-12-18 21:38:52 -0800150#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -0700151#include <linux/debugfs.h>
152
153static struct dentry *rootdir;
Mike Turquetteb24764902012-03-15 23:11:19 -0700154static int inited = 0;
Stephen Boyd6314b672014-09-04 23:37:49 -0700155static DEFINE_MUTEX(clk_debug_lock);
156static HLIST_HEAD(clk_debug_list);
Mike Turquetteb24764902012-03-15 23:11:19 -0700157
Sachin Kamat6b44c8542014-07-01 11:56:34 +0530158static struct hlist_head *all_lists[] = {
159 &clk_root_list,
160 &clk_orphan_list,
161 NULL,
162};
163
164static struct hlist_head *orphan_list[] = {
165 &clk_orphan_list,
166 NULL,
167};
168
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100169static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
170 int level)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530171{
172 if (!c)
173 return;
174
Mike Turquettee59c5372014-02-18 21:21:25 -0800175 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530176 level * 3 + 1, "",
177 30 - level * 3, c->name,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100178 c->enable_count, c->prepare_count, clk_core_get_rate(c),
179 clk_core_get_accuracy(c), clk_core_get_phase(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530180}
181
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100182static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530183 int level)
184{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100185 struct clk_core *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530186
187 if (!c)
188 return;
189
190 clk_summary_show_one(s, c, level);
191
Sasha Levinb67bfe02013-02-27 17:06:00 -0800192 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530193 clk_summary_show_subtree(s, child, level + 1);
194}
195
196static int clk_summary_show(struct seq_file *s, void *data)
197{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100198 struct clk_core *c;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300199 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530200
Mike Turquettee59c5372014-02-18 21:21:25 -0800201 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
202 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530203
Mike Turquetteeab89f62013-03-28 13:59:01 -0700204 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530205
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300206 for (; *lists; lists++)
207 hlist_for_each_entry(c, *lists, child_node)
208 clk_summary_show_subtree(s, c, 0);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530209
Mike Turquetteeab89f62013-03-28 13:59:01 -0700210 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530211
212 return 0;
213}
214
215
216static int clk_summary_open(struct inode *inode, struct file *file)
217{
218 return single_open(file, clk_summary_show, inode->i_private);
219}
220
221static const struct file_operations clk_summary_fops = {
222 .open = clk_summary_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
226};
227
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100228static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530229{
230 if (!c)
231 return;
232
233 seq_printf(s, "\"%s\": { ", c->name);
234 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
235 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100236 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
237 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
238 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530239}
240
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100241static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530242{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100243 struct clk_core *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530244
245 if (!c)
246 return;
247
248 clk_dump_one(s, c, level);
249
Sasha Levinb67bfe02013-02-27 17:06:00 -0800250 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530251 seq_printf(s, ",");
252 clk_dump_subtree(s, child, level + 1);
253 }
254
255 seq_printf(s, "}");
256}
257
258static int clk_dump(struct seq_file *s, void *data)
259{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100260 struct clk_core *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530261 bool first_node = true;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300262 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530263
264 seq_printf(s, "{");
265
Mike Turquetteeab89f62013-03-28 13:59:01 -0700266 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530267
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300268 for (; *lists; lists++) {
269 hlist_for_each_entry(c, *lists, child_node) {
270 if (!first_node)
271 seq_puts(s, ",");
272 first_node = false;
273 clk_dump_subtree(s, c, 0);
274 }
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530275 }
276
Mike Turquetteeab89f62013-03-28 13:59:01 -0700277 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530278
279 seq_printf(s, "}");
280 return 0;
281}
282
283
284static int clk_dump_open(struct inode *inode, struct file *file)
285{
286 return single_open(file, clk_dump, inode->i_private);
287}
288
289static const struct file_operations clk_dump_fops = {
290 .open = clk_dump_open,
291 .read = seq_read,
292 .llseek = seq_lseek,
293 .release = single_release,
294};
295
Stephen Boydd6968fc2015-04-30 13:54:13 -0700296static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Mike Turquetteb24764902012-03-15 23:11:19 -0700297{
298 struct dentry *d;
299 int ret = -ENOMEM;
300
Stephen Boydd6968fc2015-04-30 13:54:13 -0700301 if (!core || !pdentry) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700302 ret = -EINVAL;
303 goto out;
304 }
305
Stephen Boydd6968fc2015-04-30 13:54:13 -0700306 d = debugfs_create_dir(core->name, pdentry);
Mike Turquetteb24764902012-03-15 23:11:19 -0700307 if (!d)
308 goto out;
309
Stephen Boydd6968fc2015-04-30 13:54:13 -0700310 core->dentry = d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700311
Stephen Boydd6968fc2015-04-30 13:54:13 -0700312 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
313 (u32 *)&core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700314 if (!d)
315 goto err_out;
316
Stephen Boydd6968fc2015-04-30 13:54:13 -0700317 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
318 (u32 *)&core->accuracy);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100319 if (!d)
320 goto err_out;
321
Stephen Boydd6968fc2015-04-30 13:54:13 -0700322 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
323 (u32 *)&core->phase);
Mike Turquettee59c5372014-02-18 21:21:25 -0800324 if (!d)
325 goto err_out;
326
Stephen Boydd6968fc2015-04-30 13:54:13 -0700327 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
328 (u32 *)&core->flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700329 if (!d)
330 goto err_out;
331
Stephen Boydd6968fc2015-04-30 13:54:13 -0700332 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
333 (u32 *)&core->prepare_count);
Mike Turquetteb24764902012-03-15 23:11:19 -0700334 if (!d)
335 goto err_out;
336
Stephen Boydd6968fc2015-04-30 13:54:13 -0700337 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
338 (u32 *)&core->enable_count);
Mike Turquetteb24764902012-03-15 23:11:19 -0700339 if (!d)
340 goto err_out;
341
Stephen Boydd6968fc2015-04-30 13:54:13 -0700342 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
343 (u32 *)&core->notifier_count);
Mike Turquetteb24764902012-03-15 23:11:19 -0700344 if (!d)
345 goto err_out;
346
Stephen Boydd6968fc2015-04-30 13:54:13 -0700347 if (core->ops->debug_init) {
348 ret = core->ops->debug_init(core->hw, core->dentry);
Chris Brandabeab452014-07-03 14:01:29 -0700349 if (ret)
Alex Elderc646cbf2014-03-21 06:43:56 -0500350 goto err_out;
Chris Brandabeab452014-07-03 14:01:29 -0700351 }
Alex Elderc646cbf2014-03-21 06:43:56 -0500352
Mike Turquetteb24764902012-03-15 23:11:19 -0700353 ret = 0;
354 goto out;
355
356err_out:
Stephen Boydd6968fc2015-04-30 13:54:13 -0700357 debugfs_remove_recursive(core->dentry);
358 core->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700359out:
360 return ret;
361}
362
Mike Turquetteb24764902012-03-15 23:11:19 -0700363/**
364 * clk_debug_register - add a clk node to the debugfs clk tree
Stephen Boydd6968fc2015-04-30 13:54:13 -0700365 * @core: the clk being added to the debugfs clk tree
Mike Turquetteb24764902012-03-15 23:11:19 -0700366 *
367 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
368 * initialized. Otherwise it bails out early since the debugfs clk tree
369 * will be created lazily by clk_debug_init as part of a late_initcall.
Mike Turquetteb24764902012-03-15 23:11:19 -0700370 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700371static int clk_debug_register(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700372{
Mike Turquetteb24764902012-03-15 23:11:19 -0700373 int ret = 0;
374
Stephen Boyd6314b672014-09-04 23:37:49 -0700375 mutex_lock(&clk_debug_lock);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700376 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyd6314b672014-09-04 23:37:49 -0700377
Mike Turquetteb24764902012-03-15 23:11:19 -0700378 if (!inited)
Stephen Boyd6314b672014-09-04 23:37:49 -0700379 goto unlock;
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
Stephen Boydd6968fc2015-04-30 13:54:13 -0700381 ret = clk_debug_create_one(core, rootdir);
Stephen Boyd6314b672014-09-04 23:37:49 -0700382unlock:
383 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700384
Mike Turquetteb24764902012-03-15 23:11:19 -0700385 return ret;
386}
387
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200388 /**
389 * clk_debug_unregister - remove a clk node from the debugfs clk tree
Stephen Boydd6968fc2015-04-30 13:54:13 -0700390 * @core: the clk being removed from the debugfs clk tree
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200391 *
392 * Dynamically removes a clk and all it's children clk nodes from the
393 * debugfs clk tree if clk->dentry points to debugfs created by
394 * clk_debug_register in __clk_init.
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200395 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700396static void clk_debug_unregister(struct clk_core *core)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200397{
Stephen Boyd6314b672014-09-04 23:37:49 -0700398 mutex_lock(&clk_debug_lock);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700399 hlist_del_init(&core->debug_node);
400 debugfs_remove_recursive(core->dentry);
401 core->dentry = NULL;
Stephen Boyd6314b672014-09-04 23:37:49 -0700402 mutex_unlock(&clk_debug_lock);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200403}
404
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100405struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300406 void *data, const struct file_operations *fops)
407{
408 struct dentry *d = NULL;
409
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100410 if (hw->core->dentry)
411 d = debugfs_create_file(name, mode, hw->core->dentry, data,
412 fops);
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300413
414 return d;
415}
416EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
417
Mike Turquetteb24764902012-03-15 23:11:19 -0700418/**
419 * clk_debug_init - lazily create the debugfs clk tree visualization
420 *
421 * clks are often initialized very early during boot before memory can
422 * be dynamically allocated and well before debugfs is setup.
423 * clk_debug_init walks the clk tree hierarchy while holding
424 * prepare_lock and creates the topology as part of a late_initcall,
425 * thus insuring that clks initialized very early will still be
426 * represented in the debugfs clk tree. This function should only be
427 * called once at boot-time, and all other clks added dynamically will
428 * be done so with clk_debug_register.
429 */
430static int __init clk_debug_init(void)
431{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700432 struct clk_core *core;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530433 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700434
435 rootdir = debugfs_create_dir("clk", NULL);
436
437 if (!rootdir)
438 return -ENOMEM;
439
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300440 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530441 &clk_summary_fops);
442 if (!d)
443 return -ENOMEM;
444
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300445 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530446 &clk_dump_fops);
447 if (!d)
448 return -ENOMEM;
449
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300450 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
451 &orphan_list, &clk_summary_fops);
452 if (!d)
453 return -ENOMEM;
Mike Turquetteb24764902012-03-15 23:11:19 -0700454
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300455 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
456 &orphan_list, &clk_dump_fops);
457 if (!d)
Mike Turquetteb24764902012-03-15 23:11:19 -0700458 return -ENOMEM;
459
Stephen Boyd6314b672014-09-04 23:37:49 -0700460 mutex_lock(&clk_debug_lock);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700461 hlist_for_each_entry(core, &clk_debug_list, debug_node)
462 clk_debug_create_one(core, rootdir);
Mike Turquetteb24764902012-03-15 23:11:19 -0700463
464 inited = 1;
Stephen Boyd6314b672014-09-04 23:37:49 -0700465 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700466
467 return 0;
468}
469late_initcall(clk_debug_init);
470#else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700471static inline int clk_debug_register(struct clk_core *core) { return 0; }
472static inline void clk_debug_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100473 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200474{
475}
Stephen Boydd6968fc2015-04-30 13:54:13 -0700476static inline void clk_debug_unregister(struct clk_core *core)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200477{
478}
Mike Turquette70d347e2012-03-26 11:53:47 -0700479#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700480
Mike Turquetteb24764902012-03-15 23:11:19 -0700481/* caller must hold prepare_lock */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700482static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100483{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100484 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100485
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100486 lockdep_assert_held(&prepare_lock);
487
Stephen Boydd6968fc2015-04-30 13:54:13 -0700488 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100489 clk_unprepare_unused_subtree(child);
490
Stephen Boydd6968fc2015-04-30 13:54:13 -0700491 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100492 return;
493
Stephen Boydd6968fc2015-04-30 13:54:13 -0700494 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100495 return;
496
Stephen Boydd6968fc2015-04-30 13:54:13 -0700497 if (clk_core_is_prepared(core)) {
498 trace_clk_unprepare(core);
499 if (core->ops->unprepare_unused)
500 core->ops->unprepare_unused(core->hw);
501 else if (core->ops->unprepare)
502 core->ops->unprepare(core->hw);
503 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100504 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100505}
506
507/* caller must hold prepare_lock */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700508static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700509{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100510 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700511 unsigned long flags;
512
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100513 lockdep_assert_held(&prepare_lock);
514
Stephen Boydd6968fc2015-04-30 13:54:13 -0700515 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700516 clk_disable_unused_subtree(child);
517
Mike Turquetteeab89f62013-03-28 13:59:01 -0700518 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700519
Stephen Boydd6968fc2015-04-30 13:54:13 -0700520 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700521 goto unlock_out;
522
Stephen Boydd6968fc2015-04-30 13:54:13 -0700523 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700524 goto unlock_out;
525
Mike Turquette7c045a52012-12-04 11:00:35 -0800526 /*
527 * some gate clocks have special needs during the disable-unused
528 * sequence. call .disable_unused if available, otherwise fall
529 * back to .disable
530 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700531 if (clk_core_is_enabled(core)) {
532 trace_clk_disable(core);
533 if (core->ops->disable_unused)
534 core->ops->disable_unused(core->hw);
535 else if (core->ops->disable)
536 core->ops->disable(core->hw);
537 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800538 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700539
540unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700541 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700542}
543
Olof Johansson1e435252013-04-27 14:10:18 -0700544static bool clk_ignore_unused;
545static int __init clk_ignore_unused_setup(char *__unused)
546{
547 clk_ignore_unused = true;
548 return 1;
549}
550__setup("clk_ignore_unused", clk_ignore_unused_setup);
551
Mike Turquetteb24764902012-03-15 23:11:19 -0700552static int clk_disable_unused(void)
553{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700554 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700555
Olof Johansson1e435252013-04-27 14:10:18 -0700556 if (clk_ignore_unused) {
557 pr_warn("clk: Not disabling unused clocks\n");
558 return 0;
559 }
560
Mike Turquetteeab89f62013-03-28 13:59:01 -0700561 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700562
Stephen Boydd6968fc2015-04-30 13:54:13 -0700563 hlist_for_each_entry(core, &clk_root_list, child_node)
564 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700565
Stephen Boydd6968fc2015-04-30 13:54:13 -0700566 hlist_for_each_entry(core, &clk_orphan_list, child_node)
567 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700568
Stephen Boydd6968fc2015-04-30 13:54:13 -0700569 hlist_for_each_entry(core, &clk_root_list, child_node)
570 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100571
Stephen Boydd6968fc2015-04-30 13:54:13 -0700572 hlist_for_each_entry(core, &clk_orphan_list, child_node)
573 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100574
Mike Turquetteeab89f62013-03-28 13:59:01 -0700575 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700576
577 return 0;
578}
Saravana Kannand41d5802013-05-09 11:35:01 -0700579late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700580
581/*** helper functions ***/
582
Russ Dill65800b22012-11-26 11:20:09 -0800583const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700584{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100585 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700586}
Niels de Vos48950842012-12-13 13:12:25 +0100587EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700588
Russ Dill65800b22012-11-26 11:20:09 -0800589struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700590{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100591 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700592}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800593EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700594
Russ Dill65800b22012-11-26 11:20:09 -0800595u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700596{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100597 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700598}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800599EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700600
Russ Dill65800b22012-11-26 11:20:09 -0800601struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700602{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100603 if (!clk)
604 return NULL;
605
606 /* TODO: Create a per-user clk and change callers to call clk_put */
607 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700608}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800609EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700610
Stephen Boydd6968fc2015-04-30 13:54:13 -0700611static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100612 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100613{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700614 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100615 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700616 else if (!core->parents)
617 return clk_core_lookup(core->parent_names[index]);
618 else if (!core->parents[index])
619 return core->parents[index] =
620 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100621 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700622 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100623}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100624
625struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
626{
627 struct clk_core *parent;
628
629 if (!clk)
630 return NULL;
631
632 parent = clk_core_get_parent_by_index(clk->core, index);
633
634 return !parent ? NULL : parent->hw->clk;
635}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800636EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100637
Russ Dill65800b22012-11-26 11:20:09 -0800638unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700639{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100640 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700641}
642
Stephen Boydd6968fc2015-04-30 13:54:13 -0700643static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700644{
645 unsigned long ret;
646
Stephen Boydd6968fc2015-04-30 13:54:13 -0700647 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530648 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700649 goto out;
650 }
651
Stephen Boydd6968fc2015-04-30 13:54:13 -0700652 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700653
Stephen Boydd6968fc2015-04-30 13:54:13 -0700654 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700655 goto out;
656
Stephen Boydd6968fc2015-04-30 13:54:13 -0700657 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530658 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700659
660out:
661 return ret;
662}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100663
664unsigned long __clk_get_rate(struct clk *clk)
665{
666 if (!clk)
667 return 0;
668
669 return clk_core_get_rate_nolock(clk->core);
670}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800671EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700672
Stephen Boydd6968fc2015-04-30 13:54:13 -0700673static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100674{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700675 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100676 return 0;
677
Stephen Boydd6968fc2015-04-30 13:54:13 -0700678 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100679}
680
Russ Dill65800b22012-11-26 11:20:09 -0800681unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700682{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100683 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700684}
Thierry Redingb05c6832013-09-03 09:43:51 +0200685EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700686
Stephen Boydd6968fc2015-04-30 13:54:13 -0700687static bool clk_core_is_prepared(struct clk_core *core)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100688{
689 int ret;
690
Stephen Boydd6968fc2015-04-30 13:54:13 -0700691 if (!core)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100692 return false;
693
694 /*
695 * .is_prepared is optional for clocks that can prepare
696 * fall back to software usage counter if it is missing
697 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700698 if (!core->ops->is_prepared) {
699 ret = core->prepare_count ? 1 : 0;
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100700 goto out;
701 }
702
Stephen Boydd6968fc2015-04-30 13:54:13 -0700703 ret = core->ops->is_prepared(core->hw);
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100704out:
705 return !!ret;
706}
707
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100708bool __clk_is_prepared(struct clk *clk)
709{
710 if (!clk)
711 return false;
712
713 return clk_core_is_prepared(clk->core);
714}
715
Stephen Boydd6968fc2015-04-30 13:54:13 -0700716static bool clk_core_is_enabled(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700717{
718 int ret;
719
Stephen Boydd6968fc2015-04-30 13:54:13 -0700720 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700721 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700722
723 /*
724 * .is_enabled is only mandatory for clocks that gate
725 * fall back to software usage counter if .is_enabled is missing
726 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700727 if (!core->ops->is_enabled) {
728 ret = core->enable_count ? 1 : 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700729 goto out;
730 }
731
Stephen Boydd6968fc2015-04-30 13:54:13 -0700732 ret = core->ops->is_enabled(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700733out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700734 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700735}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100736
737bool __clk_is_enabled(struct clk *clk)
738{
739 if (!clk)
740 return false;
741
742 return clk_core_is_enabled(clk->core);
743}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800744EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700745
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100746static struct clk_core *__clk_lookup_subtree(const char *name,
Stephen Boydd6968fc2015-04-30 13:54:13 -0700747 struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700748{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100749 struct clk_core *child;
750 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700751
Stephen Boydd6968fc2015-04-30 13:54:13 -0700752 if (!strcmp(core->name, name))
753 return core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700754
Stephen Boydd6968fc2015-04-30 13:54:13 -0700755 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700756 ret = __clk_lookup_subtree(name, child);
757 if (ret)
758 return ret;
759 }
760
761 return NULL;
762}
763
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100764static struct clk_core *clk_core_lookup(const char *name)
Mike Turquetteb24764902012-03-15 23:11:19 -0700765{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100766 struct clk_core *root_clk;
767 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700768
769 if (!name)
770 return NULL;
771
772 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800773 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700774 ret = __clk_lookup_subtree(name, root_clk);
775 if (ret)
776 return ret;
777 }
778
779 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800780 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700781 ret = __clk_lookup_subtree(name, root_clk);
782 if (ret)
783 return ret;
784 }
785
786 return NULL;
787}
788
Stephen Boyd15a02c12015-01-19 18:05:28 -0800789static bool mux_is_better_rate(unsigned long rate, unsigned long now,
790 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100791{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800792 if (flags & CLK_MUX_ROUND_CLOSEST)
793 return abs(now - rate) < abs(best - rate);
794
795 return now <= rate && now > best;
796}
797
798static long
799clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100800 unsigned long min_rate,
801 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800802 unsigned long *best_parent_rate,
803 struct clk_hw **best_parent_p,
804 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100805{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100806 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100807 int i, num_parents;
808 unsigned long parent_rate, best = 0;
809
810 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100811 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
812 parent = core->parent;
813 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100814 best = __clk_determine_rate(parent ? parent->hw : NULL,
815 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100816 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100817 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100818 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100819 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100820 goto out;
821 }
822
823 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100824 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100825 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100826 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100827 if (!parent)
828 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100829 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100830 parent_rate = __clk_determine_rate(parent->hw, rate,
831 min_rate,
832 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100833 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100834 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800835 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100836 best_parent = parent;
837 best = parent_rate;
838 }
839 }
840
841out:
842 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100843 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100844 *best_parent_rate = best;
845
846 return best;
847}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800848
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100849struct clk *__clk_lookup(const char *name)
850{
851 struct clk_core *core = clk_core_lookup(name);
852
853 return !core ? NULL : core->hw->clk;
854}
855
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100857 unsigned long *min_rate,
858 unsigned long *max_rate)
859{
860 struct clk *clk_user;
861
862 *min_rate = 0;
863 *max_rate = ULONG_MAX;
864
Stephen Boydd6968fc2015-04-30 13:54:13 -0700865 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100866 *min_rate = max(*min_rate, clk_user->min_rate);
867
Stephen Boydd6968fc2015-04-30 13:54:13 -0700868 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100869 *max_rate = min(*max_rate, clk_user->max_rate);
870}
871
Stephen Boyd15a02c12015-01-19 18:05:28 -0800872/*
873 * Helper for finding best parent to provide a given frequency. This can be used
874 * directly as a determine_rate callback (e.g. for a mux), or from a more
875 * complex clock that may combine a mux with other operations.
876 */
877long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100878 unsigned long min_rate,
879 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800880 unsigned long *best_parent_rate,
881 struct clk_hw **best_parent_p)
882{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100883 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
884 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800885 best_parent_p, 0);
886}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800887EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100888
Stephen Boyd15a02c12015-01-19 18:05:28 -0800889long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100890 unsigned long min_rate,
891 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800892 unsigned long *best_parent_rate,
893 struct clk_hw **best_parent_p)
894{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100895 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
896 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800897 best_parent_p,
898 CLK_MUX_ROUND_CLOSEST);
899}
900EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
901
Mike Turquetteb24764902012-03-15 23:11:19 -0700902/*** clk api ***/
903
Stephen Boydd6968fc2015-04-30 13:54:13 -0700904static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700905{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700906 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700907 return;
908
Stephen Boydd6968fc2015-04-30 13:54:13 -0700909 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700910 return;
911
Stephen Boydd6968fc2015-04-30 13:54:13 -0700912 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700913 return;
914
Stephen Boydd6968fc2015-04-30 13:54:13 -0700915 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700916
Stephen Boydd6968fc2015-04-30 13:54:13 -0700917 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800918
Stephen Boydd6968fc2015-04-30 13:54:13 -0700919 if (core->ops->unprepare)
920 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700921
Stephen Boydd6968fc2015-04-30 13:54:13 -0700922 trace_clk_unprepare_complete(core);
923 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700924}
925
926/**
927 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200928 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700929 *
930 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
931 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
932 * if the operation may sleep. One example is a clk which is accessed over
933 * I2c. In the complex case a clk gate operation may require a fast and a slow
934 * part. It is this reason that clk_unprepare and clk_disable are not mutually
935 * exclusive. In fact clk_disable must be called before clk_unprepare.
936 */
937void clk_unprepare(struct clk *clk)
938{
Stephen Boyd63589e92014-03-26 16:06:37 -0700939 if (IS_ERR_OR_NULL(clk))
940 return;
941
Mike Turquetteeab89f62013-03-28 13:59:01 -0700942 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100943 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700944 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700945}
946EXPORT_SYMBOL_GPL(clk_unprepare);
947
Stephen Boydd6968fc2015-04-30 13:54:13 -0700948static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700949{
950 int ret = 0;
951
Stephen Boydd6968fc2015-04-30 13:54:13 -0700952 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700953 return 0;
954
Stephen Boydd6968fc2015-04-30 13:54:13 -0700955 if (core->prepare_count == 0) {
956 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700957 if (ret)
958 return ret;
959
Stephen Boydd6968fc2015-04-30 13:54:13 -0700960 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800961
Stephen Boydd6968fc2015-04-30 13:54:13 -0700962 if (core->ops->prepare)
963 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800964
Stephen Boydd6968fc2015-04-30 13:54:13 -0700965 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800966
967 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700968 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800969 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700970 }
971 }
972
Stephen Boydd6968fc2015-04-30 13:54:13 -0700973 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700974
975 return 0;
976}
977
978/**
979 * clk_prepare - prepare a clock source
980 * @clk: the clk being prepared
981 *
982 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
983 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
984 * operation may sleep. One example is a clk which is accessed over I2c. In
985 * the complex case a clk ungate operation may require a fast and a slow part.
986 * It is this reason that clk_prepare and clk_enable are not mutually
987 * exclusive. In fact clk_prepare must be called before clk_enable.
988 * Returns 0 on success, -EERROR otherwise.
989 */
990int clk_prepare(struct clk *clk)
991{
992 int ret;
993
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100994 if (!clk)
995 return 0;
996
Mike Turquetteeab89f62013-03-28 13:59:01 -0700997 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100998 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700999 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001000
1001 return ret;
1002}
1003EXPORT_SYMBOL_GPL(clk_prepare);
1004
Stephen Boydd6968fc2015-04-30 13:54:13 -07001005static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001006{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001007 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001008 return;
1009
Stephen Boydd6968fc2015-04-30 13:54:13 -07001010 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -07001011 return;
1012
Stephen Boydd6968fc2015-04-30 13:54:13 -07001013 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -07001014 return;
1015
Stephen Boydd6968fc2015-04-30 13:54:13 -07001016 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001017
Stephen Boydd6968fc2015-04-30 13:54:13 -07001018 if (core->ops->disable)
1019 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001020
Stephen Boydd6968fc2015-04-30 13:54:13 -07001021 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001022
Stephen Boydd6968fc2015-04-30 13:54:13 -07001023 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001024}
1025
Mike Turquetteb24764902012-03-15 23:11:19 -07001026/**
1027 * clk_disable - gate a clock
1028 * @clk: the clk being gated
1029 *
1030 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1031 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1032 * clk if the operation is fast and will never sleep. One example is a
1033 * SoC-internal clk which is controlled via simple register writes. In the
1034 * complex case a clk gate operation may require a fast and a slow part. It is
1035 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1036 * In fact clk_disable must be called before clk_unprepare.
1037 */
1038void clk_disable(struct clk *clk)
1039{
1040 unsigned long flags;
1041
Stephen Boyd63589e92014-03-26 16:06:37 -07001042 if (IS_ERR_OR_NULL(clk))
1043 return;
1044
Mike Turquetteeab89f62013-03-28 13:59:01 -07001045 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -07001046 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001047 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001048}
1049EXPORT_SYMBOL_GPL(clk_disable);
1050
Stephen Boydd6968fc2015-04-30 13:54:13 -07001051static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001052{
1053 int ret = 0;
1054
Stephen Boydd6968fc2015-04-30 13:54:13 -07001055 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001056 return 0;
1057
Stephen Boydd6968fc2015-04-30 13:54:13 -07001058 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -07001059 return -ESHUTDOWN;
1060
Stephen Boydd6968fc2015-04-30 13:54:13 -07001061 if (core->enable_count == 0) {
1062 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001063
1064 if (ret)
1065 return ret;
1066
Stephen Boydd6968fc2015-04-30 13:54:13 -07001067 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001068
Stephen Boydd6968fc2015-04-30 13:54:13 -07001069 if (core->ops->enable)
1070 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001071
Stephen Boydd6968fc2015-04-30 13:54:13 -07001072 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001073
1074 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001075 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001076 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001077 }
1078 }
1079
Stephen Boydd6968fc2015-04-30 13:54:13 -07001080 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001081 return 0;
1082}
1083
1084/**
1085 * clk_enable - ungate a clock
1086 * @clk: the clk being ungated
1087 *
1088 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1089 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1090 * if the operation will never sleep. One example is a SoC-internal clk which
1091 * is controlled via simple register writes. In the complex case a clk ungate
1092 * operation may require a fast and a slow part. It is this reason that
1093 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1094 * must be called before clk_enable. Returns 0 on success, -EERROR
1095 * otherwise.
1096 */
1097int clk_enable(struct clk *clk)
1098{
1099 unsigned long flags;
1100 int ret;
1101
Dong Aisheng864e1602015-04-30 14:02:19 -07001102 if (!clk)
1103 return 0;
1104
Mike Turquetteeab89f62013-03-28 13:59:01 -07001105 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -07001106 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001107 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001108
1109 return ret;
1110}
1111EXPORT_SYMBOL_GPL(clk_enable);
1112
Stephen Boydd6968fc2015-04-30 13:54:13 -07001113static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001114 unsigned long rate,
1115 unsigned long min_rate,
1116 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001117{
Shawn Guo81536e02012-04-12 20:50:17 +08001118 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001119 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001120 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001121
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001122 lockdep_assert_held(&prepare_lock);
1123
Stephen Boydd6968fc2015-04-30 13:54:13 -07001124 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001125 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001126
Stephen Boydd6968fc2015-04-30 13:54:13 -07001127 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001128 if (parent)
1129 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001130
Stephen Boydd6968fc2015-04-30 13:54:13 -07001131 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001132 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001133 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001134 min_rate, max_rate,
1135 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001136 } else if (core->ops->round_rate)
1137 return core->ops->round_rate(core->hw, rate, &parent_rate);
1138 else if (core->flags & CLK_SET_RATE_PARENT)
1139 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001140 max_rate);
James Hogan71472c02013-07-29 12:25:00 +01001141 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001142 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001143}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144
1145/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001146 * __clk_determine_rate - get the closest rate actually supported by a clock
1147 * @hw: determine the rate of this clock
1148 * @rate: target rate
1149 * @min_rate: returned rate must be greater than this rate
1150 * @max_rate: returned rate must be less than this rate
1151 *
1152 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
1153 * .determine_rate.
1154 */
1155unsigned long __clk_determine_rate(struct clk_hw *hw,
1156 unsigned long rate,
1157 unsigned long min_rate,
1158 unsigned long max_rate)
1159{
1160 if (!hw)
1161 return 0;
1162
1163 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
1164}
1165EXPORT_SYMBOL_GPL(__clk_determine_rate);
1166
1167/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001168 * __clk_round_rate - round the given rate for a clk
1169 * @clk: round the rate of this clock
1170 * @rate: the rate which is to be rounded
1171 *
1172 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1173 */
1174unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1175{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001176 unsigned long min_rate;
1177 unsigned long max_rate;
1178
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001179 if (!clk)
1180 return 0;
1181
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001182 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
1183
1184 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001185}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001186EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001187
1188/**
1189 * clk_round_rate - round the given rate for a clk
1190 * @clk: the clk for which we are rounding a rate
1191 * @rate: the rate which is to be rounded
1192 *
1193 * Takes in a rate as input and rounds it to a rate that the clk can actually
1194 * use which is then returned. If clk doesn't support round_rate operation
1195 * then the parent rate is returned.
1196 */
1197long clk_round_rate(struct clk *clk, unsigned long rate)
1198{
1199 unsigned long ret;
1200
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001201 if (!clk)
1202 return 0;
1203
Mike Turquetteeab89f62013-03-28 13:59:01 -07001204 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001205 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001206 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001207
1208 return ret;
1209}
1210EXPORT_SYMBOL_GPL(clk_round_rate);
1211
1212/**
1213 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001214 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001215 * @msg: clk notifier type (see include/linux/clk.h)
1216 * @old_rate: old clk rate
1217 * @new_rate: new clk rate
1218 *
1219 * Triggers a notifier call chain on the clk rate-change notification
1220 * for 'clk'. Passes a pointer to the struct clk and the previous
1221 * and current rates to the notifier callback. Intended to be called by
1222 * internal clock code only. Returns NOTIFY_DONE from the last driver
1223 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1224 * a driver returns that.
1225 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001226static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001227 unsigned long old_rate, unsigned long new_rate)
1228{
1229 struct clk_notifier *cn;
1230 struct clk_notifier_data cnd;
1231 int ret = NOTIFY_DONE;
1232
Mike Turquetteb24764902012-03-15 23:11:19 -07001233 cnd.old_rate = old_rate;
1234 cnd.new_rate = new_rate;
1235
1236 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001237 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001238 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001239 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1240 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001241 }
1242 }
1243
1244 return ret;
1245}
1246
1247/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001248 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001249 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001250 *
1251 * Walks the subtree of clks starting with clk and recalculates accuracies as
1252 * it goes. Note that if a clk does not implement the .recalc_accuracy
1253 * callback then it is assumed that the clock will take on the accuracy of it's
1254 * parent.
1255 *
1256 * Caller must hold prepare_lock.
1257 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001258static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001259{
1260 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001261 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001262
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001263 lockdep_assert_held(&prepare_lock);
1264
Stephen Boydd6968fc2015-04-30 13:54:13 -07001265 if (core->parent)
1266 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001267
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268 if (core->ops->recalc_accuracy)
1269 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001270 parent_accuracy);
1271 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001272 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001273
Stephen Boydd6968fc2015-04-30 13:54:13 -07001274 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001275 __clk_recalc_accuracies(child);
1276}
1277
Stephen Boydd6968fc2015-04-30 13:54:13 -07001278static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001279{
1280 unsigned long accuracy;
1281
1282 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001283 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1284 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001285
Stephen Boydd6968fc2015-04-30 13:54:13 -07001286 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001287 clk_prepare_unlock();
1288
1289 return accuracy;
1290}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001291
1292/**
1293 * clk_get_accuracy - return the accuracy of clk
1294 * @clk: the clk whose accuracy is being returned
1295 *
1296 * Simply returns the cached accuracy of the clk, unless
1297 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1298 * issued.
1299 * If clk is NULL then returns 0.
1300 */
1301long clk_get_accuracy(struct clk *clk)
1302{
1303 if (!clk)
1304 return 0;
1305
1306 return clk_core_get_accuracy(clk->core);
1307}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001308EXPORT_SYMBOL_GPL(clk_get_accuracy);
1309
Stephen Boydd6968fc2015-04-30 13:54:13 -07001310static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001311 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001312{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001313 if (core->ops->recalc_rate)
1314 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001315 return parent_rate;
1316}
1317
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001318/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001319 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001320 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001321 * @msg: notification type (see include/linux/clk.h)
1322 *
1323 * Walks the subtree of clks starting with clk and recalculates rates as it
1324 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001325 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001326 *
1327 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1328 * if necessary.
1329 *
1330 * Caller must hold prepare_lock.
1331 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001332static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001333{
1334 unsigned long old_rate;
1335 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001336 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001337
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001338 lockdep_assert_held(&prepare_lock);
1339
Stephen Boydd6968fc2015-04-30 13:54:13 -07001340 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001341
Stephen Boydd6968fc2015-04-30 13:54:13 -07001342 if (core->parent)
1343 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001344
Stephen Boydd6968fc2015-04-30 13:54:13 -07001345 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001346
1347 /*
1348 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1349 * & ABORT_RATE_CHANGE notifiers
1350 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001351 if (core->notifier_count && msg)
1352 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001353
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001355 __clk_recalc_rates(child, msg);
1356}
1357
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001359{
1360 unsigned long rate;
1361
1362 clk_prepare_lock();
1363
Stephen Boydd6968fc2015-04-30 13:54:13 -07001364 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1365 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001366
Stephen Boydd6968fc2015-04-30 13:54:13 -07001367 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001368 clk_prepare_unlock();
1369
1370 return rate;
1371}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001372
Mike Turquetteb24764902012-03-15 23:11:19 -07001373/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001374 * clk_get_rate - return the rate of clk
1375 * @clk: the clk whose rate is being returned
1376 *
1377 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1378 * is set, which means a recalc_rate will be issued.
1379 * If clk is NULL then returns 0.
1380 */
1381unsigned long clk_get_rate(struct clk *clk)
1382{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001383 if (!clk)
1384 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001385
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001386 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001387}
1388EXPORT_SYMBOL_GPL(clk_get_rate);
1389
Stephen Boydd6968fc2015-04-30 13:54:13 -07001390static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001391 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001392{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001393 int i;
James Hogan4935b222013-07-29 12:24:59 +01001394
Stephen Boydd6968fc2015-04-30 13:54:13 -07001395 if (!core->parents) {
1396 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001397 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001398 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001399 return -ENOMEM;
1400 }
James Hogan4935b222013-07-29 12:24:59 +01001401
1402 /*
1403 * find index of new parent clock using cached parent ptrs,
1404 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001405 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001406 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001407 for (i = 0; i < core->num_parents; i++) {
1408 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001409 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001410
Stephen Boydd6968fc2015-04-30 13:54:13 -07001411 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001412 continue;
1413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 if (!strcmp(core->parent_names[i], parent->name)) {
1415 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001416 return i;
James Hogan4935b222013-07-29 12:24:59 +01001417 }
1418 }
1419
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001420 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001421}
1422
Stephen Boydd6968fc2015-04-30 13:54:13 -07001423static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001424{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001425 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001426
James Hogan903efc52013-08-29 12:10:51 +01001427 if (new_parent) {
1428 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001430 new_parent->new_child = NULL;
1431
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001433 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001434 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001435 }
James Hogan4935b222013-07-29 12:24:59 +01001436
Stephen Boydd6968fc2015-04-30 13:54:13 -07001437 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001438}
1439
Stephen Boydd6968fc2015-04-30 13:54:13 -07001440static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001441 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001442{
1443 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001445
1446 /*
1447 * Migrate prepare state between parents and prevent race with
1448 * clk_enable().
1449 *
1450 * If the clock is not prepared, then a race with
1451 * clk_enable/disable() is impossible since we already have the
1452 * prepare lock (future calls to clk_enable() need to be preceded by
1453 * a clk_prepare()).
1454 *
1455 * If the clock is prepared, migrate the prepared state to the new
1456 * parent and also protect against a race with clk_enable() by
1457 * forcing the clock and the new parent on. This ensures that all
1458 * future calls to clk_enable() are practically NOPs with respect to
1459 * hardware and software states.
1460 *
1461 * See also: Comment for clk_set_parent() below.
1462 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001464 clk_core_prepare(parent);
1465 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001466 clk_core_enable(core);
James Hogan4935b222013-07-29 12:24:59 +01001467 }
1468
1469 /* update the clk tree topology */
1470 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001471 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001472 clk_enable_unlock(flags);
1473
Stephen Boyd3fa22522014-01-15 10:47:22 -08001474 return old_parent;
1475}
1476
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001477static void __clk_set_parent_after(struct clk_core *core,
1478 struct clk_core *parent,
1479 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001480{
1481 /*
1482 * Finish the migration of prepare state and undo the changes done
1483 * for preventing a race with clk_enable().
1484 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001485 if (core->prepare_count) {
1486 clk_core_disable(core);
1487 clk_core_disable(old_parent);
1488 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001489 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001490}
1491
Stephen Boydd6968fc2015-04-30 13:54:13 -07001492static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001493 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001494{
1495 unsigned long flags;
1496 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001497 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001498
Stephen Boydd6968fc2015-04-30 13:54:13 -07001499 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001500
Stephen Boydd6968fc2015-04-30 13:54:13 -07001501 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001502
James Hogan4935b222013-07-29 12:24:59 +01001503 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001504 if (parent && core->ops->set_parent)
1505 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001506
Stephen Boydd6968fc2015-04-30 13:54:13 -07001507 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001508
James Hogan4935b222013-07-29 12:24:59 +01001509 if (ret) {
1510 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001511 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001512 clk_enable_unlock(flags);
1513
Stephen Boydd6968fc2015-04-30 13:54:13 -07001514 if (core->prepare_count) {
1515 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001516 clk_core_disable(parent);
1517 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001518 }
1519 return ret;
1520 }
1521
Stephen Boydd6968fc2015-04-30 13:54:13 -07001522 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001523
James Hogan4935b222013-07-29 12:24:59 +01001524 return 0;
1525}
1526
Ulf Hanssona093bde2012-08-31 14:21:28 +02001527/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001528 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001529 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001530 * @parent_rate: the "future" rate of clk's parent
1531 *
1532 * Walks the subtree of clks starting with clk, speculating rates as it
1533 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1534 *
1535 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1536 * pre-rate change notifications and returns early if no clks in the
1537 * subtree have subscribed to the notifications. Note that if a clk does not
1538 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001539 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001540 *
1541 * Caller must hold prepare_lock.
1542 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001543static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001544 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001545{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001546 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001547 unsigned long new_rate;
1548 int ret = NOTIFY_DONE;
1549
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001550 lockdep_assert_held(&prepare_lock);
1551
Stephen Boydd6968fc2015-04-30 13:54:13 -07001552 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001553
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001554 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001555 if (core->notifier_count)
1556 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001557
Mike Turquette86bcfa22014-02-24 16:08:41 -08001558 if (ret & NOTIFY_STOP_MASK) {
1559 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001560 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001561 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001562 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001563
Stephen Boydd6968fc2015-04-30 13:54:13 -07001564 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001565 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001566 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001567 break;
1568 }
1569
1570out:
1571 return ret;
1572}
1573
Stephen Boydd6968fc2015-04-30 13:54:13 -07001574static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001575 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001576{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001577 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001578
Stephen Boydd6968fc2015-04-30 13:54:13 -07001579 core->new_rate = new_rate;
1580 core->new_parent = new_parent;
1581 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001582 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001583 core->new_child = NULL;
1584 if (new_parent && new_parent != core->parent)
1585 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001586
Stephen Boydd6968fc2015-04-30 13:54:13 -07001587 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001588 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001589 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001590 }
1591}
1592
1593/*
1594 * calculate the new rates returning the topmost clock that has to be
1595 * changed.
1596 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001597static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001598 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001599{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001600 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001601 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001602 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001603 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001604 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001605 unsigned long min_rate;
1606 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001607 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001608 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001609
Mike Turquette7452b212012-03-26 14:45:36 -07001610 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001611 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001612 return NULL;
1613
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001614 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001615 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001616 if (parent)
1617 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001618
Stephen Boydd6968fc2015-04-30 13:54:13 -07001619 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001620
James Hogan71472c02013-07-29 12:25:00 +01001621 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001622 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001623 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001624 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001625 min_rate,
1626 max_rate,
1627 &best_parent_rate,
1628 &parent_hw);
1629 if (ret < 0)
1630 return NULL;
1631
1632 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001633 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001634 } else if (core->ops->round_rate) {
1635 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001636 &best_parent_rate);
1637 if (ret < 0)
1638 return NULL;
1639
1640 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001641 if (new_rate < min_rate || new_rate > max_rate)
1642 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001643 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001644 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001645 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001646 return NULL;
1647 } else {
1648 /* pass-through clock with adjustable parent */
1649 top = clk_calc_new_rates(parent, rate);
1650 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001651 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001652 }
1653
James Hogan71472c02013-07-29 12:25:00 +01001654 /* some clocks must be gated to change parent */
1655 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001656 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001657 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001658 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001659 return NULL;
1660 }
1661
James Hogan71472c02013-07-29 12:25:00 +01001662 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001663 if (parent && core->num_parents > 1) {
1664 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001665 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001666 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001667 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001668 return NULL;
1669 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001670 }
1671
Stephen Boydd6968fc2015-04-30 13:54:13 -07001672 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001673 best_parent_rate != parent->rate)
1674 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001675
1676out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001678
1679 return top;
1680}
1681
1682/*
1683 * Notify about rate changes in a subtree. Always walk down the whole tree
1684 * so that in case of an error we can walk down the whole tree again and
1685 * abort the change.
1686 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001687static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001688 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001689{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001690 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001691 int ret = NOTIFY_DONE;
1692
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301694 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001695
Stephen Boydd6968fc2015-04-30 13:54:13 -07001696 if (core->notifier_count) {
1697 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001698 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001699 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001700 }
1701
Stephen Boydd6968fc2015-04-30 13:54:13 -07001702 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001703 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001704 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001705 continue;
1706 tmp_clk = clk_propagate_rate_change(child, event);
1707 if (tmp_clk)
1708 fail_clk = tmp_clk;
1709 }
1710
Stephen Boydd6968fc2015-04-30 13:54:13 -07001711 /* handle the new child who might not be in core->children yet */
1712 if (core->new_child) {
1713 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001714 if (tmp_clk)
1715 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001716 }
1717
1718 return fail_clk;
1719}
1720
1721/*
1722 * walk down a subtree and set the new rates notifying the rate
1723 * change on the way
1724 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001725static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001726{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001727 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001728 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001729 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001730 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001731 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001732 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001733
Stephen Boydd6968fc2015-04-30 13:54:13 -07001734 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001735
Stephen Boydd6968fc2015-04-30 13:54:13 -07001736 if (core->new_parent)
1737 best_parent_rate = core->new_parent->rate;
1738 else if (core->parent)
1739 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001740
Stephen Boydd6968fc2015-04-30 13:54:13 -07001741 if (core->new_parent && core->new_parent != core->parent) {
1742 old_parent = __clk_set_parent_before(core, core->new_parent);
1743 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001744
Stephen Boydd6968fc2015-04-30 13:54:13 -07001745 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001746 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001747 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001748 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001749 core->new_parent_index);
1750 } else if (core->ops->set_parent) {
1751 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001752 }
1753
Stephen Boydd6968fc2015-04-30 13:54:13 -07001754 trace_clk_set_parent_complete(core, core->new_parent);
1755 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001756 }
1757
Stephen Boydd6968fc2015-04-30 13:54:13 -07001758 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001759
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760 if (!skip_set_rate && core->ops->set_rate)
1761 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001762
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001764
Stephen Boydd6968fc2015-04-30 13:54:13 -07001765 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001766
Stephen Boydd6968fc2015-04-30 13:54:13 -07001767 if (core->notifier_count && old_rate != core->rate)
1768 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001769
Tero Kristo067bb172014-08-21 16:47:45 +03001770 /*
1771 * Use safe iteration, as change_rate can actually swap parents
1772 * for certain clock types.
1773 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001774 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001775 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001776 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001777 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001778 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001779 }
1780
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781 /* handle the new child who might not be in core->children yet */
1782 if (core->new_child)
1783 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001784}
1785
Stephen Boydd6968fc2015-04-30 13:54:13 -07001786static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001787 unsigned long req_rate)
1788{
1789 struct clk_core *top, *fail_clk;
1790 unsigned long rate = req_rate;
1791 int ret = 0;
1792
Stephen Boydd6968fc2015-04-30 13:54:13 -07001793 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001794 return 0;
1795
1796 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001797 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001798 return 0;
1799
Stephen Boydd6968fc2015-04-30 13:54:13 -07001800 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001801 return -EBUSY;
1802
1803 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001804 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001805 if (!top)
1806 return -EINVAL;
1807
1808 /* notify that we are about to change rates */
1809 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1810 if (fail_clk) {
1811 pr_debug("%s: failed to set %s rate\n", __func__,
1812 fail_clk->name);
1813 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1814 return -EBUSY;
1815 }
1816
1817 /* change the rates */
1818 clk_change_rate(top);
1819
Stephen Boydd6968fc2015-04-30 13:54:13 -07001820 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001821
1822 return ret;
1823}
1824
Mike Turquetteb24764902012-03-15 23:11:19 -07001825/**
1826 * clk_set_rate - specify a new rate for clk
1827 * @clk: the clk whose rate is being changed
1828 * @rate: the new rate for clk
1829 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001830 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001831 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001832 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1833 * propagate up to clk's parent; whether or not this happens depends on the
1834 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1835 * after calling .round_rate then upstream parent propagation is ignored. If
1836 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001837 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001838 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1839 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001840 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001841 * Rate changes are accomplished via tree traversal that also recalculates the
1842 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001843 *
1844 * Returns 0 on success, -EERROR otherwise.
1845 */
1846int clk_set_rate(struct clk *clk, unsigned long rate)
1847{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001848 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001849
Mike Turquette89ac8d72013-08-21 23:58:09 -07001850 if (!clk)
1851 return 0;
1852
Mike Turquetteb24764902012-03-15 23:11:19 -07001853 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001854 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001855
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001856 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001857
Mike Turquetteeab89f62013-03-28 13:59:01 -07001858 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001859
1860 return ret;
1861}
1862EXPORT_SYMBOL_GPL(clk_set_rate);
1863
1864/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001865 * clk_set_rate_range - set a rate range for a clock source
1866 * @clk: clock source
1867 * @min: desired minimum clock rate in Hz, inclusive
1868 * @max: desired maximum clock rate in Hz, inclusive
1869 *
1870 * Returns success (0) or negative errno.
1871 */
1872int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1873{
1874 int ret = 0;
1875
1876 if (!clk)
1877 return 0;
1878
1879 if (min > max) {
1880 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1881 __func__, clk->core->name, clk->dev_id, clk->con_id,
1882 min, max);
1883 return -EINVAL;
1884 }
1885
1886 clk_prepare_lock();
1887
1888 if (min != clk->min_rate || max != clk->max_rate) {
1889 clk->min_rate = min;
1890 clk->max_rate = max;
1891 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1892 }
1893
1894 clk_prepare_unlock();
1895
1896 return ret;
1897}
1898EXPORT_SYMBOL_GPL(clk_set_rate_range);
1899
1900/**
1901 * clk_set_min_rate - set a minimum clock rate for a clock source
1902 * @clk: clock source
1903 * @rate: desired minimum clock rate in Hz, inclusive
1904 *
1905 * Returns success (0) or negative errno.
1906 */
1907int clk_set_min_rate(struct clk *clk, unsigned long rate)
1908{
1909 if (!clk)
1910 return 0;
1911
1912 return clk_set_rate_range(clk, rate, clk->max_rate);
1913}
1914EXPORT_SYMBOL_GPL(clk_set_min_rate);
1915
1916/**
1917 * clk_set_max_rate - set a maximum clock rate for a clock source
1918 * @clk: clock source
1919 * @rate: desired maximum clock rate in Hz, inclusive
1920 *
1921 * Returns success (0) or negative errno.
1922 */
1923int clk_set_max_rate(struct clk *clk, unsigned long rate)
1924{
1925 if (!clk)
1926 return 0;
1927
1928 return clk_set_rate_range(clk, clk->min_rate, rate);
1929}
1930EXPORT_SYMBOL_GPL(clk_set_max_rate);
1931
1932/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001933 * clk_get_parent - return the parent of a clk
1934 * @clk: the clk whose parent gets returned
1935 *
1936 * Simply returns clk->parent. Returns NULL if clk is NULL.
1937 */
1938struct clk *clk_get_parent(struct clk *clk)
1939{
1940 struct clk *parent;
1941
Mike Turquetteeab89f62013-03-28 13:59:01 -07001942 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001943 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001944 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001945
1946 return parent;
1947}
1948EXPORT_SYMBOL_GPL(clk_get_parent);
1949
1950/*
1951 * .get_parent is mandatory for clocks with multiple possible parents. It is
1952 * optional for single-parent clocks. Always call .get_parent if it is
1953 * available and WARN if it is missing for multi-parent clocks.
1954 *
1955 * For single-parent clocks without .get_parent, first check to see if the
1956 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001957 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001958 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001959static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001960{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001961 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001962 u8 index;
1963
1964 /* handle the trivial cases */
1965
Stephen Boydd6968fc2015-04-30 13:54:13 -07001966 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001967 goto out;
1968
Stephen Boydd6968fc2015-04-30 13:54:13 -07001969 if (core->num_parents == 1) {
1970 if (IS_ERR_OR_NULL(core->parent))
1971 core->parent = clk_core_lookup(core->parent_names[0]);
1972 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001973 goto out;
1974 }
1975
Stephen Boydd6968fc2015-04-30 13:54:13 -07001976 if (!core->ops->get_parent) {
1977 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001978 "%s: multi-parent clocks must implement .get_parent\n",
1979 __func__);
1980 goto out;
1981 };
1982
1983 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001984 * Do our best to cache parent clocks in core->parents. This prevents
1985 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001986 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001987 */
1988
Stephen Boydd6968fc2015-04-30 13:54:13 -07001989 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001990
Stephen Boydd6968fc2015-04-30 13:54:13 -07001991 if (!core->parents)
1992 core->parents =
1993 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001994 GFP_KERNEL);
1995
Stephen Boydd6968fc2015-04-30 13:54:13 -07001996 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001997
1998out:
1999 return ret;
2000}
2001
Stephen Boydd6968fc2015-04-30 13:54:13 -07002002static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002003 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002004{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002005 clk_reparent(core, new_parent);
2006 __clk_recalc_accuracies(core);
2007 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002008}
2009
Mike Turquetteb24764902012-03-15 23:11:19 -07002010/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002011 * clk_has_parent - check if a clock is a possible parent for another
2012 * @clk: clock source
2013 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002014 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002015 * This function can be used in drivers that need to check that a clock can be
2016 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002017 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002018 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002019 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002020bool clk_has_parent(struct clk *clk, struct clk *parent)
2021{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002022 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002023 unsigned int i;
2024
2025 /* NULL clocks should be nops, so return success if either is NULL. */
2026 if (!clk || !parent)
2027 return true;
2028
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002029 core = clk->core;
2030 parent_core = parent->core;
2031
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002032 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002033 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002034 return true;
2035
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002036 for (i = 0; i < core->num_parents; i++)
2037 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002038 return true;
2039
2040 return false;
2041}
2042EXPORT_SYMBOL_GPL(clk_has_parent);
2043
Stephen Boydd6968fc2015-04-30 13:54:13 -07002044static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002045{
2046 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002047 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002048 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002049
Stephen Boydd6968fc2015-04-30 13:54:13 -07002050 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002051 return 0;
2052
Mike Turquetteb24764902012-03-15 23:11:19 -07002053 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002054 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002055
Stephen Boydd6968fc2015-04-30 13:54:13 -07002056 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002057 goto out;
2058
Stephen Boydb61c43c2015-02-02 14:11:25 -08002059 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002060 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08002061 ret = -ENOSYS;
2062 goto out;
2063 }
2064
Ulf Hansson031dcc92013-04-02 23:09:38 +02002065 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002066 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002067 ret = -EBUSY;
2068 goto out;
2069 }
2070
2071 /* try finding the new parent index */
2072 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002073 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02002074 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002075 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002076 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002077 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002078 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002079 goto out;
2080 }
2081 }
2082
Mike Turquetteb24764902012-03-15 23:11:19 -07002083 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002084 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002085
2086 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002087 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002088 goto out;
2089
Ulf Hansson031dcc92013-04-02 23:09:38 +02002090 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002091 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002092
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002093 /* propagate rate an accuracy recalculation accordingly */
2094 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002095 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002096 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002097 __clk_recalc_rates(core, POST_RATE_CHANGE);
2098 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002099 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002100
2101out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002102 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002103
2104 return ret;
2105}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002106
2107/**
2108 * clk_set_parent - switch the parent of a mux clk
2109 * @clk: the mux clk whose input we are switching
2110 * @parent: the new input to clk
2111 *
2112 * Re-parent clk to use parent as its new input source. If clk is in
2113 * prepared state, the clk will get enabled for the duration of this call. If
2114 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2115 * that, the reparenting is glitchy in hardware, etc), use the
2116 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2117 *
2118 * After successfully changing clk's parent clk_set_parent will update the
2119 * clk topology, sysfs topology and propagate rate recalculation via
2120 * __clk_recalc_rates.
2121 *
2122 * Returns 0 on success, -EERROR otherwise.
2123 */
2124int clk_set_parent(struct clk *clk, struct clk *parent)
2125{
2126 if (!clk)
2127 return 0;
2128
2129 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2130}
Mike Turquetteb24764902012-03-15 23:11:19 -07002131EXPORT_SYMBOL_GPL(clk_set_parent);
2132
2133/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002134 * clk_set_phase - adjust the phase shift of a clock signal
2135 * @clk: clock signal source
2136 * @degrees: number of degrees the signal is shifted
2137 *
2138 * Shifts the phase of a clock signal by the specified
2139 * degrees. Returns 0 on success, -EERROR otherwise.
2140 *
2141 * This function makes no distinction about the input or reference
2142 * signal that we adjust the clock signal phase against. For example
2143 * phase locked-loop clock signal generators we may shift phase with
2144 * respect to feedback clock signal input, but for other cases the
2145 * clock phase may be shifted with respect to some other, unspecified
2146 * signal.
2147 *
2148 * Additionally the concept of phase shift does not propagate through
2149 * the clock tree hierarchy, which sets it apart from clock rates and
2150 * clock accuracy. A parent clock phase attribute does not have an
2151 * impact on the phase attribute of a child clock.
2152 */
2153int clk_set_phase(struct clk *clk, int degrees)
2154{
Stephen Boyd08b95752015-02-02 14:09:43 -08002155 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002156
2157 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002158 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002159
2160 /* sanity check degrees */
2161 degrees %= 360;
2162 if (degrees < 0)
2163 degrees += 360;
2164
2165 clk_prepare_lock();
2166
Stephen Boyddfc202e2015-02-02 14:37:41 -08002167 trace_clk_set_phase(clk->core, degrees);
2168
Stephen Boyd08b95752015-02-02 14:09:43 -08002169 if (clk->core->ops->set_phase)
2170 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002171
Stephen Boyddfc202e2015-02-02 14:37:41 -08002172 trace_clk_set_phase_complete(clk->core, degrees);
2173
Mike Turquettee59c5372014-02-18 21:21:25 -08002174 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002175 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002176
Mike Turquettee59c5372014-02-18 21:21:25 -08002177 clk_prepare_unlock();
2178
Mike Turquettee59c5372014-02-18 21:21:25 -08002179 return ret;
2180}
Maxime Ripard9767b042015-01-20 22:23:43 +01002181EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002182
Stephen Boydd6968fc2015-04-30 13:54:13 -07002183static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002184{
2185 int ret = 0;
2186
Stephen Boydd6968fc2015-04-30 13:54:13 -07002187 if (!core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002188 goto out;
2189
2190 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002191 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002192 clk_prepare_unlock();
2193
2194out:
2195 return ret;
2196}
Maxime Ripard9767b042015-01-20 22:23:43 +01002197EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002198
2199/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002200 * clk_get_phase - return the phase shift of a clock signal
2201 * @clk: clock signal source
2202 *
2203 * Returns the phase shift of a clock node in degrees, otherwise returns
2204 * -EERROR.
2205 */
2206int clk_get_phase(struct clk *clk)
2207{
2208 if (!clk)
2209 return 0;
2210
2211 return clk_core_get_phase(clk->core);
2212}
Mike Turquetteb24764902012-03-15 23:11:19 -07002213
2214/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002215 * clk_is_match - check if two clk's point to the same hardware clock
2216 * @p: clk compared against q
2217 * @q: clk compared against p
2218 *
2219 * Returns true if the two struct clk pointers both point to the same hardware
2220 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2221 * share the same struct clk_core object.
2222 *
2223 * Returns false otherwise. Note that two NULL clks are treated as matching.
2224 */
2225bool clk_is_match(const struct clk *p, const struct clk *q)
2226{
2227 /* trivial case: identical struct clk's or both NULL */
2228 if (p == q)
2229 return true;
2230
2231 /* true if clk->core pointers match. Avoid derefing garbage */
2232 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2233 if (p->core == q->core)
2234 return true;
2235
2236 return false;
2237}
2238EXPORT_SYMBOL_GPL(clk_is_match);
2239
2240/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002241 * __clk_init - initialize the data structures in a struct clk
2242 * @dev: device initializing this clk, placeholder for now
2243 * @clk: clk being initialized
2244 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002245 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002246 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002247 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002248static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002249{
Mike Turquetted1302a32012-03-29 14:30:40 -07002250 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002251 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002252 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002253 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002254 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002255
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002256 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002257 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002258
Stephen Boydd6968fc2015-04-30 13:54:13 -07002259 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002260
Mike Turquetteeab89f62013-03-28 13:59:01 -07002261 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002262
2263 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002264 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002265 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002266 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002267 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002268 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002269 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002270
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002271 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002272 if (core->ops->set_rate &&
2273 !((core->ops->round_rate || core->ops->determine_rate) &&
2274 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002275 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002276 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002277 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002278 goto out;
2279 }
2280
Stephen Boydd6968fc2015-04-30 13:54:13 -07002281 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002282 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002283 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002284 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002285 goto out;
2286 }
2287
Stephen Boydd6968fc2015-04-30 13:54:13 -07002288 if (core->ops->set_rate_and_parent &&
2289 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002290 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002291 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002292 ret = -EINVAL;
2293 goto out;
2294 }
2295
Mike Turquetteb24764902012-03-15 23:11:19 -07002296 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002297 for (i = 0; i < core->num_parents; i++)
2298 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002299 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002300 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002301
2302 /*
2303 * Allocate an array of struct clk *'s to avoid unnecessary string
2304 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002305 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002306 * must always check for a NULL pointer and try to populate it if
2307 * necessary.
2308 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002309 * If core->parents is not NULL we skip this entire block. This allows
2310 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002311 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002312 if (core->num_parents > 1 && !core->parents) {
2313 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002314 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002315 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002316 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002317 * clk_init'd; thus any access to clk->parents[] must check
2318 * for a NULL pointer. We can always perform lazy lookups for
2319 * missing parents later on.
2320 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002321 if (core->parents)
2322 for (i = 0; i < core->num_parents; i++)
2323 core->parents[i] =
2324 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002325 }
2326
Stephen Boydd6968fc2015-04-30 13:54:13 -07002327 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002328
2329 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002330 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002331 * parent has not yet been __clk_init'd then place clk in the orphan
2332 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2333 * clk list.
2334 *
2335 * Every time a new clk is clk_init'd then we walk the list of orphan
2336 * clocks and re-parent any that are children of the clock currently
2337 * being clk_init'd.
2338 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002339 if (core->parent)
2340 hlist_add_head(&core->child_node,
2341 &core->parent->children);
2342 else if (core->flags & CLK_IS_ROOT)
2343 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002344 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002345 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002346
2347 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002348 * Set clk's accuracy. The preferred method is to use
2349 * .recalc_accuracy. For simple clocks and lazy developers the default
2350 * fallback is to use the parent's accuracy. If a clock doesn't have a
2351 * parent (or is orphaned) then accuracy is set to zero (perfect
2352 * clock).
2353 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002354 if (core->ops->recalc_accuracy)
2355 core->accuracy = core->ops->recalc_accuracy(core->hw,
2356 __clk_get_accuracy(core->parent));
2357 else if (core->parent)
2358 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002359 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002360 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002361
2362 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002363 * Set clk's phase.
2364 * Since a phase is by definition relative to its parent, just
2365 * query the current clock phase, or just assume it's in phase.
2366 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002367 if (core->ops->get_phase)
2368 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002369 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002370 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002371
2372 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002373 * Set clk's rate. The preferred method is to use .recalc_rate. For
2374 * simple clocks and lazy developers the default fallback is to use the
2375 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2376 * then rate is set to zero.
2377 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002378 if (core->ops->recalc_rate)
2379 rate = core->ops->recalc_rate(core->hw,
2380 clk_core_get_rate_nolock(core->parent));
2381 else if (core->parent)
2382 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002383 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002384 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002385 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002386
2387 /*
2388 * walk the list of orphan clocks and reparent any that are children of
2389 * this clock
2390 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002391 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002392 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002393 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002394 if (!strcmp(core->name, orphan->parent_names[i]))
2395 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002396 continue;
2397 }
2398
Mike Turquetteb24764902012-03-15 23:11:19 -07002399 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002400 if (!strcmp(core->name, orphan->parent_names[i])) {
2401 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002402 break;
2403 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002404 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002405
2406 /*
2407 * optional platform-specific magic
2408 *
2409 * The .init callback is not used by any of the basic clock types, but
2410 * exists for weird hardware that must perform initialization magic.
2411 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002412 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002413 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002414 if (core->ops->init)
2415 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002416
Stephen Boydd6968fc2015-04-30 13:54:13 -07002417 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002418out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002419 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002420
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002421 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002422 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002423
Mike Turquetted1302a32012-03-29 14:30:40 -07002424 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002425}
2426
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002427struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2428 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002429{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002430 struct clk *clk;
2431
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002432 /* This is to allow this function to be chained to others */
2433 if (!hw || IS_ERR(hw))
2434 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002435
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002436 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2437 if (!clk)
2438 return ERR_PTR(-ENOMEM);
2439
2440 clk->core = hw->core;
2441 clk->dev_id = dev_id;
2442 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002443 clk->max_rate = ULONG_MAX;
2444
2445 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002446 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002447 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002448
2449 return clk;
2450}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002451
Stephen Boyd73e0e492015-02-06 11:42:43 -08002452void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002453{
2454 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002455 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002456 clk_prepare_unlock();
2457
2458 kfree(clk);
2459}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002460
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002461/**
2462 * clk_register - allocate a new clock, register it and return an opaque cookie
2463 * @dev: device that is registering this clock
2464 * @hw: link to hardware-specific clock data
2465 *
2466 * clk_register is the primary interface for populating the clock tree with new
2467 * clock nodes. It returns a pointer to the newly allocated struct clk which
2468 * cannot be dereferenced by driver code but may be used in conjuction with the
2469 * rest of the clock API. In the event of an error clk_register will return an
2470 * error code; drivers must test for an error code after calling clk_register.
2471 */
2472struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002473{
Mike Turquetted1302a32012-03-29 14:30:40 -07002474 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002475 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002476
Stephen Boydd6968fc2015-04-30 13:54:13 -07002477 core = kzalloc(sizeof(*core), GFP_KERNEL);
2478 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002479 ret = -ENOMEM;
2480 goto fail_out;
2481 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002482
Stephen Boydd6968fc2015-04-30 13:54:13 -07002483 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2484 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002485 ret = -ENOMEM;
2486 goto fail_name;
2487 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002488 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002489 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002490 core->owner = dev->driver->owner;
2491 core->hw = hw;
2492 core->flags = hw->init->flags;
2493 core->num_parents = hw->init->num_parents;
2494 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002495
Mike Turquetted1302a32012-03-29 14:30:40 -07002496 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002497 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002498 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002499
Stephen Boydd6968fc2015-04-30 13:54:13 -07002500 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002501 ret = -ENOMEM;
2502 goto fail_parent_names;
2503 }
2504
2505
2506 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002507 for (i = 0; i < core->num_parents; i++) {
2508 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002509 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002510 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002511 ret = -ENOMEM;
2512 goto fail_parent_names_copy;
2513 }
2514 }
2515
Stephen Boydd6968fc2015-04-30 13:54:13 -07002516 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002517
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002518 hw->clk = __clk_create_clk(hw, NULL, NULL);
2519 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002520 ret = PTR_ERR(hw->clk);
2521 goto fail_parent_names_copy;
2522 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002523
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002524 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002525 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002526 return hw->clk;
2527
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002528 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002529 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002530
2531fail_parent_names_copy:
2532 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002533 kfree_const(core->parent_names[i]);
2534 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002535fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002536 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002537fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002538 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002539fail_out:
2540 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002541}
2542EXPORT_SYMBOL_GPL(clk_register);
2543
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002544/*
2545 * Free memory allocated for a clock.
2546 * Caller must hold prepare_lock.
2547 */
2548static void __clk_release(struct kref *ref)
2549{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002550 struct clk_core *core = container_of(ref, struct clk_core, ref);
2551 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002552
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002553 lockdep_assert_held(&prepare_lock);
2554
Stephen Boydd6968fc2015-04-30 13:54:13 -07002555 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002556 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002557 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002558
Stephen Boydd6968fc2015-04-30 13:54:13 -07002559 kfree(core->parent_names);
2560 kfree_const(core->name);
2561 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002562}
2563
2564/*
2565 * Empty clk_ops for unregistered clocks. These are used temporarily
2566 * after clk_unregister() was called on a clock and until last clock
2567 * consumer calls clk_put() and the struct clk object is freed.
2568 */
2569static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2570{
2571 return -ENXIO;
2572}
2573
2574static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2575{
2576 WARN_ON_ONCE(1);
2577}
2578
2579static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2580 unsigned long parent_rate)
2581{
2582 return -ENXIO;
2583}
2584
2585static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2586{
2587 return -ENXIO;
2588}
2589
2590static const struct clk_ops clk_nodrv_ops = {
2591 .enable = clk_nodrv_prepare_enable,
2592 .disable = clk_nodrv_disable_unprepare,
2593 .prepare = clk_nodrv_prepare_enable,
2594 .unprepare = clk_nodrv_disable_unprepare,
2595 .set_rate = clk_nodrv_set_rate,
2596 .set_parent = clk_nodrv_set_parent,
2597};
2598
Mark Brown1df5c932012-04-18 09:07:12 +01002599/**
2600 * clk_unregister - unregister a currently registered clock
2601 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002602 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002603void clk_unregister(struct clk *clk)
2604{
2605 unsigned long flags;
2606
Stephen Boyd6314b672014-09-04 23:37:49 -07002607 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2608 return;
2609
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002610 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002611
2612 clk_prepare_lock();
2613
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002614 if (clk->core->ops == &clk_nodrv_ops) {
2615 pr_err("%s: unregistered clock: %s\n", __func__,
2616 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002617 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002618 }
2619 /*
2620 * Assign empty clock ops for consumers that might still hold
2621 * a reference to this clock.
2622 */
2623 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002624 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002625 clk_enable_unlock(flags);
2626
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002627 if (!hlist_empty(&clk->core->children)) {
2628 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002629 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002630
2631 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002632 hlist_for_each_entry_safe(child, t, &clk->core->children,
2633 child_node)
2634 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002635 }
2636
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002637 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002638
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002639 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002640 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002641 __func__, clk->core->name);
2642 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002643
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002644 clk_prepare_unlock();
2645}
Mark Brown1df5c932012-04-18 09:07:12 +01002646EXPORT_SYMBOL_GPL(clk_unregister);
2647
Stephen Boyd46c87732012-09-24 13:38:04 -07002648static void devm_clk_release(struct device *dev, void *res)
2649{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002650 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002651}
2652
2653/**
2654 * devm_clk_register - resource managed clk_register()
2655 * @dev: device that is registering this clock
2656 * @hw: link to hardware-specific clock data
2657 *
2658 * Managed clk_register(). Clocks returned from this function are
2659 * automatically clk_unregister()ed on driver detach. See clk_register() for
2660 * more information.
2661 */
2662struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2663{
2664 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002665 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002666
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002667 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2668 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002669 return ERR_PTR(-ENOMEM);
2670
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002671 clk = clk_register(dev, hw);
2672 if (!IS_ERR(clk)) {
2673 *clkp = clk;
2674 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002675 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002676 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002677 }
2678
2679 return clk;
2680}
2681EXPORT_SYMBOL_GPL(devm_clk_register);
2682
2683static int devm_clk_match(struct device *dev, void *res, void *data)
2684{
2685 struct clk *c = res;
2686 if (WARN_ON(!c))
2687 return 0;
2688 return c == data;
2689}
2690
2691/**
2692 * devm_clk_unregister - resource managed clk_unregister()
2693 * @clk: clock to unregister
2694 *
2695 * Deallocate a clock allocated with devm_clk_register(). Normally
2696 * this function will not need to be called and the resource management
2697 * code will ensure that the resource is freed.
2698 */
2699void devm_clk_unregister(struct device *dev, struct clk *clk)
2700{
2701 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2702}
2703EXPORT_SYMBOL_GPL(devm_clk_unregister);
2704
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002705/*
2706 * clkdev helpers
2707 */
2708int __clk_get(struct clk *clk)
2709{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002710 struct clk_core *core = !clk ? NULL : clk->core;
2711
2712 if (core) {
2713 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002714 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002715
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002716 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002717 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002718 return 1;
2719}
2720
2721void __clk_put(struct clk *clk)
2722{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002723 struct module *owner;
2724
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002725 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002726 return;
2727
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002728 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002729
Stephen Boyd50595f82015-02-06 11:42:44 -08002730 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002731 if (clk->min_rate > clk->core->req_rate ||
2732 clk->max_rate < clk->core->req_rate)
2733 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2734
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002735 owner = clk->core->owner;
2736 kref_put(&clk->core->ref, __clk_release);
2737
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002738 clk_prepare_unlock();
2739
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002740 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002741
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002742 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002743}
2744
Mike Turquetteb24764902012-03-15 23:11:19 -07002745/*** clk rate change notifiers ***/
2746
2747/**
2748 * clk_notifier_register - add a clk rate change notifier
2749 * @clk: struct clk * to watch
2750 * @nb: struct notifier_block * with callback info
2751 *
2752 * Request notification when clk's rate changes. This uses an SRCU
2753 * notifier because we want it to block and notifier unregistrations are
2754 * uncommon. The callbacks associated with the notifier must not
2755 * re-enter into the clk framework by calling any top-level clk APIs;
2756 * this will cause a nested prepare_lock mutex.
2757 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002758 * In all notification cases cases (pre, post and abort rate change) the
2759 * original clock rate is passed to the callback via struct
2760 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002761 * clk_notifier_data.new_rate.
2762 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002763 * clk_notifier_register() must be called from non-atomic context.
2764 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2765 * allocation failure; otherwise, passes along the return value of
2766 * srcu_notifier_chain_register().
2767 */
2768int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2769{
2770 struct clk_notifier *cn;
2771 int ret = -ENOMEM;
2772
2773 if (!clk || !nb)
2774 return -EINVAL;
2775
Mike Turquetteeab89f62013-03-28 13:59:01 -07002776 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002777
2778 /* search the list of notifiers for this clk */
2779 list_for_each_entry(cn, &clk_notifier_list, node)
2780 if (cn->clk == clk)
2781 break;
2782
2783 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2784 if (cn->clk != clk) {
2785 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2786 if (!cn)
2787 goto out;
2788
2789 cn->clk = clk;
2790 srcu_init_notifier_head(&cn->notifier_head);
2791
2792 list_add(&cn->node, &clk_notifier_list);
2793 }
2794
2795 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2796
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002797 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002798
2799out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002800 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002801
2802 return ret;
2803}
2804EXPORT_SYMBOL_GPL(clk_notifier_register);
2805
2806/**
2807 * clk_notifier_unregister - remove a clk rate change notifier
2808 * @clk: struct clk *
2809 * @nb: struct notifier_block * with callback info
2810 *
2811 * Request no further notification for changes to 'clk' and frees memory
2812 * allocated in clk_notifier_register.
2813 *
2814 * Returns -EINVAL if called with null arguments; otherwise, passes
2815 * along the return value of srcu_notifier_chain_unregister().
2816 */
2817int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2818{
2819 struct clk_notifier *cn = NULL;
2820 int ret = -EINVAL;
2821
2822 if (!clk || !nb)
2823 return -EINVAL;
2824
Mike Turquetteeab89f62013-03-28 13:59:01 -07002825 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002826
2827 list_for_each_entry(cn, &clk_notifier_list, node)
2828 if (cn->clk == clk)
2829 break;
2830
2831 if (cn->clk == clk) {
2832 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2833
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002834 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002835
2836 /* XXX the notifier code should handle this better */
2837 if (!cn->notifier_head.head) {
2838 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002839 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002840 kfree(cn);
2841 }
2842
2843 } else {
2844 ret = -ENOENT;
2845 }
2846
Mike Turquetteeab89f62013-03-28 13:59:01 -07002847 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002848
2849 return ret;
2850}
2851EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002852
2853#ifdef CONFIG_OF
2854/**
2855 * struct of_clk_provider - Clock provider registration structure
2856 * @link: Entry in global list of clock providers
2857 * @node: Pointer to device tree node of clock provider
2858 * @get: Get clock callback. Returns NULL or a struct clk for the
2859 * given clock specifier
2860 * @data: context pointer to be passed into @get callback
2861 */
2862struct of_clk_provider {
2863 struct list_head link;
2864
2865 struct device_node *node;
2866 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2867 void *data;
2868};
2869
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302870static const struct of_device_id __clk_of_table_sentinel
2871 __used __section(__clk_of_table_end);
2872
Grant Likely766e6a42012-04-09 14:50:06 -05002873static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002874static DEFINE_MUTEX(of_clk_mutex);
2875
Grant Likely766e6a42012-04-09 14:50:06 -05002876struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2877 void *data)
2878{
2879 return data;
2880}
2881EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2882
Shawn Guo494bfec2012-08-22 21:36:27 +08002883struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2884{
2885 struct clk_onecell_data *clk_data = data;
2886 unsigned int idx = clkspec->args[0];
2887
2888 if (idx >= clk_data->clk_num) {
2889 pr_err("%s: invalid clock index %d\n", __func__, idx);
2890 return ERR_PTR(-EINVAL);
2891 }
2892
2893 return clk_data->clks[idx];
2894}
2895EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2896
Grant Likely766e6a42012-04-09 14:50:06 -05002897/**
2898 * of_clk_add_provider() - Register a clock provider for a node
2899 * @np: Device node pointer associated with clock provider
2900 * @clk_src_get: callback for decoding clock
2901 * @data: context pointer for @clk_src_get callback.
2902 */
2903int of_clk_add_provider(struct device_node *np,
2904 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2905 void *data),
2906 void *data)
2907{
2908 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002909 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002910
2911 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2912 if (!cp)
2913 return -ENOMEM;
2914
2915 cp->node = of_node_get(np);
2916 cp->data = data;
2917 cp->get = clk_src_get;
2918
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002919 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002920 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002921 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002922 pr_debug("Added clock from %s\n", np->full_name);
2923
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002924 ret = of_clk_set_defaults(np, true);
2925 if (ret < 0)
2926 of_clk_del_provider(np);
2927
2928 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002929}
2930EXPORT_SYMBOL_GPL(of_clk_add_provider);
2931
2932/**
2933 * of_clk_del_provider() - Remove a previously registered clock provider
2934 * @np: Device node pointer associated with clock provider
2935 */
2936void of_clk_del_provider(struct device_node *np)
2937{
2938 struct of_clk_provider *cp;
2939
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002940 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002941 list_for_each_entry(cp, &of_clk_providers, link) {
2942 if (cp->node == np) {
2943 list_del(&cp->link);
2944 of_node_put(cp->node);
2945 kfree(cp);
2946 break;
2947 }
2948 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002949 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002950}
2951EXPORT_SYMBOL_GPL(of_clk_del_provider);
2952
Stephen Boyd73e0e492015-02-06 11:42:43 -08002953struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2954 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002955{
2956 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002957 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002958
Stephen Boyd306c3422015-02-05 15:39:11 -08002959 if (!clkspec)
2960 return ERR_PTR(-EINVAL);
2961
Grant Likely766e6a42012-04-09 14:50:06 -05002962 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002963 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002964 list_for_each_entry(provider, &of_clk_providers, link) {
2965 if (provider->node == clkspec->np)
2966 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002967 if (!IS_ERR(clk)) {
2968 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2969 con_id);
2970
2971 if (!IS_ERR(clk) && !__clk_get(clk)) {
2972 __clk_free_clk(clk);
2973 clk = ERR_PTR(-ENOENT);
2974 }
2975
Grant Likely766e6a42012-04-09 14:50:06 -05002976 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002977 }
Grant Likely766e6a42012-04-09 14:50:06 -05002978 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002979 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002980
2981 return clk;
2982}
2983
Stephen Boyd306c3422015-02-05 15:39:11 -08002984/**
2985 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2986 * @clkspec: pointer to a clock specifier data structure
2987 *
2988 * This function looks up a struct clk from the registered list of clock
2989 * providers, an input is a clock specifier data structure as returned
2990 * from the of_parse_phandle_with_args() function call.
2991 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002992struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2993{
Stephen Boyd306c3422015-02-05 15:39:11 -08002994 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002995}
2996
Mike Turquettef6102742013-10-07 23:12:13 -07002997int of_clk_get_parent_count(struct device_node *np)
2998{
2999 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
3000}
3001EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3002
Grant Likely766e6a42012-04-09 14:50:06 -05003003const char *of_clk_get_parent_name(struct device_node *np, int index)
3004{
3005 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003006 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003007 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003008 const __be32 *vp;
3009 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003010 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003011 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05003012
3013 if (index < 0)
3014 return NULL;
3015
3016 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3017 &clkspec);
3018 if (rc)
3019 return NULL;
3020
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003021 index = clkspec.args_count ? clkspec.args[0] : 0;
3022 count = 0;
3023
3024 /* if there is an indices property, use it to transfer the index
3025 * specified into an array offset for the clock-output-names property.
3026 */
3027 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3028 if (index == pv) {
3029 index = count;
3030 break;
3031 }
3032 count++;
3033 }
3034
Grant Likely766e6a42012-04-09 14:50:06 -05003035 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003036 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003037 &clk_name) < 0)
3038 clk_name = clkspec.np->name;
3039
3040 of_node_put(clkspec.np);
3041 return clk_name;
3042}
3043EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3044
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003045struct clock_provider {
3046 of_clk_init_cb_t clk_init_cb;
3047 struct device_node *np;
3048 struct list_head node;
3049};
3050
3051static LIST_HEAD(clk_provider_list);
3052
3053/*
3054 * This function looks for a parent clock. If there is one, then it
3055 * checks that the provider for this parent clock was initialized, in
3056 * this case the parent clock will be ready.
3057 */
3058static int parent_ready(struct device_node *np)
3059{
3060 int i = 0;
3061
3062 while (true) {
3063 struct clk *clk = of_clk_get(np, i);
3064
3065 /* this parent is ready we can check the next one */
3066 if (!IS_ERR(clk)) {
3067 clk_put(clk);
3068 i++;
3069 continue;
3070 }
3071
3072 /* at least one parent is not ready, we exit now */
3073 if (PTR_ERR(clk) == -EPROBE_DEFER)
3074 return 0;
3075
3076 /*
3077 * Here we make assumption that the device tree is
3078 * written correctly. So an error means that there is
3079 * no more parent. As we didn't exit yet, then the
3080 * previous parent are ready. If there is no clock
3081 * parent, no need to wait for them, then we can
3082 * consider their absence as being ready
3083 */
3084 return 1;
3085 }
3086}
3087
Grant Likely766e6a42012-04-09 14:50:06 -05003088/**
3089 * of_clk_init() - Scan and init clock providers from the DT
3090 * @matches: array of compatible values and init functions for providers.
3091 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003092 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003093 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003094 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003095 */
3096void __init of_clk_init(const struct of_device_id *matches)
3097{
Alex Elder7f7ed582013-08-22 11:31:31 -05003098 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003099 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003100 struct clock_provider *clk_provider, *next;
3101 bool is_init_done;
3102 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003103
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303104 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003105 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303106
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003107 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003108 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003109 struct clock_provider *parent =
3110 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3111
3112 parent->clk_init_cb = match->data;
3113 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003114 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003115 }
3116
3117 while (!list_empty(&clk_provider_list)) {
3118 is_init_done = false;
3119 list_for_each_entry_safe(clk_provider, next,
3120 &clk_provider_list, node) {
3121 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003122
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003123 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003124 of_clk_set_defaults(clk_provider->np, true);
3125
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003126 list_del(&clk_provider->node);
3127 kfree(clk_provider);
3128 is_init_done = true;
3129 }
3130 }
3131
3132 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003133 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003134 * remaining providers during the last loop, so now we
3135 * initialize all the remaining ones unconditionally
3136 * in case the clock parent was not mandatory
3137 */
3138 if (!is_init_done)
3139 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003140 }
3141}
3142#endif