blob: fa5a00e5ee41b5261e323ba5602528f6fbe8582e [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Michael Turquetteb09d6d92015-01-29 14:22:50 -080012#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020013#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070014#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/spinlock.h>
17#include <linux/err.h>
18#include <linux/list.h>
19#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050020#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070021#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053022#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070023#include <linux/sched.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010040static long clk_core_get_accuracy(struct clk_core *clk);
41static unsigned long clk_core_get_rate(struct clk_core *clk);
42static int clk_core_get_phase(struct clk_core *clk);
43static bool clk_core_is_prepared(struct clk_core *clk);
44static bool clk_core_is_enabled(struct clk_core *clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010045static struct clk_core *clk_core_lookup(const char *name);
46
Michael Turquetteb09d6d92015-01-29 14:22:50 -080047/*** private data structures ***/
48
49struct clk_core {
50 const char *name;
51 const struct clk_ops *ops;
52 struct clk_hw *hw;
53 struct module *owner;
54 struct clk_core *parent;
55 const char **parent_names;
56 struct clk_core **parents;
57 u8 num_parents;
58 u8 new_parent_index;
59 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010060 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080061 unsigned long new_rate;
62 struct clk_core *new_parent;
63 struct clk_core *new_child;
64 unsigned long flags;
65 unsigned int enable_count;
66 unsigned int prepare_count;
67 unsigned long accuracy;
68 int phase;
69 struct hlist_head children;
70 struct hlist_node child_node;
71 struct hlist_node debug_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010072 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080073 unsigned int notifier_count;
74#ifdef CONFIG_DEBUG_FS
75 struct dentry *dentry;
76#endif
77 struct kref ref;
78};
79
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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100296static int clk_debug_create_one(struct clk_core *clk, struct dentry *pdentry)
Mike Turquetteb24764902012-03-15 23:11:19 -0700297{
298 struct dentry *d;
299 int ret = -ENOMEM;
300
301 if (!clk || !pdentry) {
302 ret = -EINVAL;
303 goto out;
304 }
305
306 d = debugfs_create_dir(clk->name, pdentry);
307 if (!d)
308 goto out;
309
310 clk->dentry = d;
311
312 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
313 (u32 *)&clk->rate);
314 if (!d)
315 goto err_out;
316
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100317 d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
318 (u32 *)&clk->accuracy);
319 if (!d)
320 goto err_out;
321
Mike Turquettee59c5372014-02-18 21:21:25 -0800322 d = debugfs_create_u32("clk_phase", S_IRUGO, clk->dentry,
323 (u32 *)&clk->phase);
324 if (!d)
325 goto err_out;
326
Mike Turquetteb24764902012-03-15 23:11:19 -0700327 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
328 (u32 *)&clk->flags);
329 if (!d)
330 goto err_out;
331
332 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
333 (u32 *)&clk->prepare_count);
334 if (!d)
335 goto err_out;
336
337 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
338 (u32 *)&clk->enable_count);
339 if (!d)
340 goto err_out;
341
342 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
343 (u32 *)&clk->notifier_count);
344 if (!d)
345 goto err_out;
346
Chris Brandabeab452014-07-03 14:01:29 -0700347 if (clk->ops->debug_init) {
348 ret = clk->ops->debug_init(clk->hw, clk->dentry);
349 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:
Alex Elderb5f98e62013-11-27 09:39:49 -0600357 debugfs_remove_recursive(clk->dentry);
358 clk->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
365 * @clk: the clk being added to the debugfs clk tree
366 *
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 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100371static int clk_debug_register(struct clk_core *clk)
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);
376 hlist_add_head(&clk->debug_node, &clk_debug_list);
377
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 Boyd6314b672014-09-04 23:37:49 -0700381 ret = clk_debug_create_one(clk, rootdir);
382unlock:
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
390 * @clk: the clk being removed from the debugfs clk tree
391 *
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 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100396static void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200397{
Stephen Boyd6314b672014-09-04 23:37:49 -0700398 mutex_lock(&clk_debug_lock);
Stephen Boyd6314b672014-09-04 23:37:49 -0700399 hlist_del_init(&clk->debug_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200400 debugfs_remove_recursive(clk->dentry);
Stephen Boyd6314b672014-09-04 23:37:49 -0700401 clk->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{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100432 struct clk_core *clk;
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);
461 hlist_for_each_entry(clk, &clk_debug_list, debug_node)
462 clk_debug_create_one(clk, 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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100471static inline int clk_debug_register(struct clk_core *clk) { return 0; }
472static inline void clk_debug_reparent(struct clk_core *clk,
473 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200474{
475}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100476static inline void clk_debug_unregister(struct clk_core *clk)
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 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100482static void clk_unprepare_unused_subtree(struct clk_core *clk)
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
Ulf Hansson1c155b32013-03-12 20:26:03 +0100488 hlist_for_each_entry(child, &clk->children, child_node)
489 clk_unprepare_unused_subtree(child);
490
491 if (clk->prepare_count)
492 return;
493
494 if (clk->flags & CLK_IGNORE_UNUSED)
495 return;
496
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100497 if (clk_core_is_prepared(clk)) {
Stephen Boyddfc202e2015-02-02 14:37:41 -0800498 trace_clk_unprepare(clk);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100499 if (clk->ops->unprepare_unused)
500 clk->ops->unprepare_unused(clk->hw);
501 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100502 clk->ops->unprepare(clk->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800503 trace_clk_unprepare_complete(clk);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100504 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100505}
506
507/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100508static void clk_disable_unused_subtree(struct clk_core *clk)
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
Sasha Levinb67bfe02013-02-27 17:06:00 -0800515 hlist_for_each_entry(child, &clk->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
520 if (clk->enable_count)
521 goto unlock_out;
522
523 if (clk->flags & CLK_IGNORE_UNUSED)
524 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 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100531 if (clk_core_is_enabled(clk)) {
Stephen Boyddfc202e2015-02-02 14:37:41 -0800532 trace_clk_disable(clk);
Mike Turquette7c045a52012-12-04 11:00:35 -0800533 if (clk->ops->disable_unused)
534 clk->ops->disable_unused(clk->hw);
535 else if (clk->ops->disable)
536 clk->ops->disable(clk->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800537 trace_clk_disable_complete(clk);
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{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100554 struct clk_core *clk;
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
Sasha Levinb67bfe02013-02-27 17:06:00 -0800563 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700564 clk_disable_unused_subtree(clk);
565
Sasha Levinb67bfe02013-02-27 17:06:00 -0800566 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700567 clk_disable_unused_subtree(clk);
568
Ulf Hansson1c155b32013-03-12 20:26:03 +0100569 hlist_for_each_entry(clk, &clk_root_list, child_node)
570 clk_unprepare_unused_subtree(clk);
571
572 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
573 clk_unprepare_unused_subtree(clk);
574
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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100611static struct clk_core *clk_core_get_parent_by_index(struct clk_core *clk,
612 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100613{
614 if (!clk || index >= clk->num_parents)
615 return NULL;
616 else if (!clk->parents)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100617 return clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100618 else if (!clk->parents[index])
619 return clk->parents[index] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100620 clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100621 else
622 return clk->parents[index];
623}
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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100643static unsigned long clk_core_get_rate_nolock(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700644{
645 unsigned long ret;
646
647 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530648 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700649 goto out;
650 }
651
652 ret = clk->rate;
653
654 if (clk->flags & CLK_IS_ROOT)
655 goto out;
656
657 if (!clk->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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100673static unsigned long __clk_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100674{
675 if (!clk)
676 return 0;
677
678 return clk->accuracy;
679}
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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100687static bool clk_core_is_prepared(struct clk_core *clk)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100688{
689 int ret;
690
691 if (!clk)
692 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 */
698 if (!clk->ops->is_prepared) {
699 ret = clk->prepare_count ? 1 : 0;
700 goto out;
701 }
702
703 ret = clk->ops->is_prepared(clk->hw);
704out:
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
716static bool clk_core_is_enabled(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700717{
718 int ret;
719
720 if (!clk)
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 */
727 if (!clk->ops->is_enabled) {
728 ret = clk->enable_count ? 1 : 0;
729 goto out;
730 }
731
732 ret = clk->ops->is_enabled(clk->hw);
733out:
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,
747 struct clk_core *clk)
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
752 if (!strcmp(clk->name, name))
753 return clk;
754
Sasha Levinb67bfe02013-02-27 17:06:00 -0800755 hlist_for_each_entry(child, &clk->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
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100856static void clk_core_get_boundaries(struct clk_core *clk,
857 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 Boyd50595f82015-02-06 11:42:44 -0800865 hlist_for_each_entry(clk_user, &clk->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100866 *min_rate = max(*min_rate, clk_user->min_rate);
867
Stephen Boyd50595f82015-02-06 11:42:44 -0800868 hlist_for_each_entry(clk_user, &clk->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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100904static void clk_core_unprepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700905{
906 if (!clk)
907 return;
908
909 if (WARN_ON(clk->prepare_count == 0))
910 return;
911
912 if (--clk->prepare_count > 0)
913 return;
914
915 WARN_ON(clk->enable_count > 0);
916
Stephen Boyddfc202e2015-02-02 14:37:41 -0800917 trace_clk_unprepare(clk);
918
Mike Turquetteb24764902012-03-15 23:11:19 -0700919 if (clk->ops->unprepare)
920 clk->ops->unprepare(clk->hw);
921
Stephen Boyddfc202e2015-02-02 14:37:41 -0800922 trace_clk_unprepare_complete(clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100923 clk_core_unprepare(clk->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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100948static int clk_core_prepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700949{
950 int ret = 0;
951
952 if (!clk)
953 return 0;
954
955 if (clk->prepare_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100956 ret = clk_core_prepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700957 if (ret)
958 return ret;
959
Stephen Boyddfc202e2015-02-02 14:37:41 -0800960 trace_clk_prepare(clk);
961
962 if (clk->ops->prepare)
Mike Turquetteb24764902012-03-15 23:11:19 -0700963 ret = clk->ops->prepare(clk->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800964
965 trace_clk_prepare_complete(clk);
966
967 if (ret) {
968 clk_core_unprepare(clk->parent);
969 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700970 }
971 }
972
973 clk->prepare_count++;
974
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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001005static void clk_core_disable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001006{
1007 if (!clk)
1008 return;
1009
1010 if (WARN_ON(clk->enable_count == 0))
1011 return;
1012
1013 if (--clk->enable_count > 0)
1014 return;
1015
Stephen Boyddfc202e2015-02-02 14:37:41 -08001016 trace_clk_disable(clk);
1017
Mike Turquetteb24764902012-03-15 23:11:19 -07001018 if (clk->ops->disable)
1019 clk->ops->disable(clk->hw);
1020
Stephen Boyddfc202e2015-02-02 14:37:41 -08001021 trace_clk_disable_complete(clk);
1022
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001023 clk_core_disable(clk->parent);
1024}
1025
1026static void __clk_disable(struct clk *clk)
1027{
1028 if (!clk)
1029 return;
1030
1031 clk_core_disable(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001032}
1033
1034/**
1035 * clk_disable - gate a clock
1036 * @clk: the clk being gated
1037 *
1038 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1039 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1040 * clk if the operation is fast and will never sleep. One example is a
1041 * SoC-internal clk which is controlled via simple register writes. In the
1042 * complex case a clk gate operation may require a fast and a slow part. It is
1043 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1044 * In fact clk_disable must be called before clk_unprepare.
1045 */
1046void clk_disable(struct clk *clk)
1047{
1048 unsigned long flags;
1049
Stephen Boyd63589e92014-03-26 16:06:37 -07001050 if (IS_ERR_OR_NULL(clk))
1051 return;
1052
Mike Turquetteeab89f62013-03-28 13:59:01 -07001053 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001054 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001055 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001056}
1057EXPORT_SYMBOL_GPL(clk_disable);
1058
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001059static int clk_core_enable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001060{
1061 int ret = 0;
1062
1063 if (!clk)
1064 return 0;
1065
1066 if (WARN_ON(clk->prepare_count == 0))
1067 return -ESHUTDOWN;
1068
1069 if (clk->enable_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001070 ret = clk_core_enable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001071
1072 if (ret)
1073 return ret;
1074
Stephen Boyddfc202e2015-02-02 14:37:41 -08001075 trace_clk_enable(clk);
1076
1077 if (clk->ops->enable)
Mike Turquetteb24764902012-03-15 23:11:19 -07001078 ret = clk->ops->enable(clk->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001079
1080 trace_clk_enable_complete(clk);
1081
1082 if (ret) {
1083 clk_core_disable(clk->parent);
1084 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001085 }
1086 }
1087
1088 clk->enable_count++;
1089 return 0;
1090}
1091
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001092static int __clk_enable(struct clk *clk)
1093{
1094 if (!clk)
1095 return 0;
1096
1097 return clk_core_enable(clk->core);
1098}
1099
Mike Turquetteb24764902012-03-15 23:11:19 -07001100/**
1101 * clk_enable - ungate a clock
1102 * @clk: the clk being ungated
1103 *
1104 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1105 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1106 * if the operation will never sleep. One example is a SoC-internal clk which
1107 * is controlled via simple register writes. In the complex case a clk ungate
1108 * operation may require a fast and a slow part. It is this reason that
1109 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1110 * must be called before clk_enable. Returns 0 on success, -EERROR
1111 * otherwise.
1112 */
1113int clk_enable(struct clk *clk)
1114{
1115 unsigned long flags;
1116 int ret;
1117
Mike Turquetteeab89f62013-03-28 13:59:01 -07001118 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001119 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001120 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001121
1122 return ret;
1123}
1124EXPORT_SYMBOL_GPL(clk_enable);
1125
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001126static unsigned long clk_core_round_rate_nolock(struct clk_core *clk,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001127 unsigned long rate,
1128 unsigned long min_rate,
1129 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001130{
Shawn Guo81536e02012-04-12 20:50:17 +08001131 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001132 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001133 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001134
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001135 lockdep_assert_held(&prepare_lock);
1136
Mike Turquetteb24764902012-03-15 23:11:19 -07001137 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001138 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001139
James Hogan71472c02013-07-29 12:25:00 +01001140 parent = clk->parent;
1141 if (parent)
1142 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001143
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001144 if (clk->ops->determine_rate) {
1145 parent_hw = parent ? parent->hw : NULL;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001146 return clk->ops->determine_rate(clk->hw, rate,
1147 min_rate, max_rate,
1148 &parent_rate, &parent_hw);
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001149 } else if (clk->ops->round_rate)
James Hogan71472c02013-07-29 12:25:00 +01001150 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
1151 else if (clk->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001152 return clk_core_round_rate_nolock(clk->parent, rate, min_rate,
1153 max_rate);
James Hogan71472c02013-07-29 12:25:00 +01001154 else
1155 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001156}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001157
1158/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001159 * __clk_determine_rate - get the closest rate actually supported by a clock
1160 * @hw: determine the rate of this clock
1161 * @rate: target rate
1162 * @min_rate: returned rate must be greater than this rate
1163 * @max_rate: returned rate must be less than this rate
1164 *
1165 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
1166 * .determine_rate.
1167 */
1168unsigned long __clk_determine_rate(struct clk_hw *hw,
1169 unsigned long rate,
1170 unsigned long min_rate,
1171 unsigned long max_rate)
1172{
1173 if (!hw)
1174 return 0;
1175
1176 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
1177}
1178EXPORT_SYMBOL_GPL(__clk_determine_rate);
1179
1180/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001181 * __clk_round_rate - round the given rate for a clk
1182 * @clk: round the rate of this clock
1183 * @rate: the rate which is to be rounded
1184 *
1185 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1186 */
1187unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1188{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001189 unsigned long min_rate;
1190 unsigned long max_rate;
1191
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001192 if (!clk)
1193 return 0;
1194
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001195 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
1196
1197 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001198}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001199EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001200
1201/**
1202 * clk_round_rate - round the given rate for a clk
1203 * @clk: the clk for which we are rounding a rate
1204 * @rate: the rate which is to be rounded
1205 *
1206 * Takes in a rate as input and rounds it to a rate that the clk can actually
1207 * use which is then returned. If clk doesn't support round_rate operation
1208 * then the parent rate is returned.
1209 */
1210long clk_round_rate(struct clk *clk, unsigned long rate)
1211{
1212 unsigned long ret;
1213
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001214 if (!clk)
1215 return 0;
1216
Mike Turquetteeab89f62013-03-28 13:59:01 -07001217 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001218 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001219 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001220
1221 return ret;
1222}
1223EXPORT_SYMBOL_GPL(clk_round_rate);
1224
1225/**
1226 * __clk_notify - call clk notifier chain
1227 * @clk: struct clk * that is changing rate
1228 * @msg: clk notifier type (see include/linux/clk.h)
1229 * @old_rate: old clk rate
1230 * @new_rate: new clk rate
1231 *
1232 * Triggers a notifier call chain on the clk rate-change notification
1233 * for 'clk'. Passes a pointer to the struct clk and the previous
1234 * and current rates to the notifier callback. Intended to be called by
1235 * internal clock code only. Returns NOTIFY_DONE from the last driver
1236 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1237 * a driver returns that.
1238 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001239static int __clk_notify(struct clk_core *clk, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001240 unsigned long old_rate, unsigned long new_rate)
1241{
1242 struct clk_notifier *cn;
1243 struct clk_notifier_data cnd;
1244 int ret = NOTIFY_DONE;
1245
Mike Turquetteb24764902012-03-15 23:11:19 -07001246 cnd.old_rate = old_rate;
1247 cnd.new_rate = new_rate;
1248
1249 list_for_each_entry(cn, &clk_notifier_list, node) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001250 if (cn->clk->core == clk) {
1251 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001252 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1253 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001254 }
1255 }
1256
1257 return ret;
1258}
1259
1260/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001261 * __clk_recalc_accuracies
1262 * @clk: first clk in the subtree
1263 *
1264 * Walks the subtree of clks starting with clk and recalculates accuracies as
1265 * it goes. Note that if a clk does not implement the .recalc_accuracy
1266 * callback then it is assumed that the clock will take on the accuracy of it's
1267 * parent.
1268 *
1269 * Caller must hold prepare_lock.
1270 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001271static void __clk_recalc_accuracies(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001272{
1273 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001274 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001275
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001276 lockdep_assert_held(&prepare_lock);
1277
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001278 if (clk->parent)
1279 parent_accuracy = clk->parent->accuracy;
1280
1281 if (clk->ops->recalc_accuracy)
1282 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1283 parent_accuracy);
1284 else
1285 clk->accuracy = parent_accuracy;
1286
1287 hlist_for_each_entry(child, &clk->children, child_node)
1288 __clk_recalc_accuracies(child);
1289}
1290
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001291static long clk_core_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001292{
1293 unsigned long accuracy;
1294
1295 clk_prepare_lock();
1296 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1297 __clk_recalc_accuracies(clk);
1298
1299 accuracy = __clk_get_accuracy(clk);
1300 clk_prepare_unlock();
1301
1302 return accuracy;
1303}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001304
1305/**
1306 * clk_get_accuracy - return the accuracy of clk
1307 * @clk: the clk whose accuracy is being returned
1308 *
1309 * Simply returns the cached accuracy of the clk, unless
1310 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1311 * issued.
1312 * If clk is NULL then returns 0.
1313 */
1314long clk_get_accuracy(struct clk *clk)
1315{
1316 if (!clk)
1317 return 0;
1318
1319 return clk_core_get_accuracy(clk->core);
1320}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001321EXPORT_SYMBOL_GPL(clk_get_accuracy);
1322
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001323static unsigned long clk_recalc(struct clk_core *clk,
1324 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001325{
1326 if (clk->ops->recalc_rate)
1327 return clk->ops->recalc_rate(clk->hw, parent_rate);
1328 return parent_rate;
1329}
1330
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001331/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001332 * __clk_recalc_rates
1333 * @clk: first clk in the subtree
1334 * @msg: notification type (see include/linux/clk.h)
1335 *
1336 * Walks the subtree of clks starting with clk and recalculates rates as it
1337 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001338 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001339 *
1340 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1341 * if necessary.
1342 *
1343 * Caller must hold prepare_lock.
1344 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001345static void __clk_recalc_rates(struct clk_core *clk, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001346{
1347 unsigned long old_rate;
1348 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001349 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001350
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001351 lockdep_assert_held(&prepare_lock);
1352
Mike Turquetteb24764902012-03-15 23:11:19 -07001353 old_rate = clk->rate;
1354
1355 if (clk->parent)
1356 parent_rate = clk->parent->rate;
1357
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001358 clk->rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001359
1360 /*
1361 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1362 * & ABORT_RATE_CHANGE notifiers
1363 */
1364 if (clk->notifier_count && msg)
1365 __clk_notify(clk, msg, old_rate, clk->rate);
1366
Sasha Levinb67bfe02013-02-27 17:06:00 -08001367 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001368 __clk_recalc_rates(child, msg);
1369}
1370
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001371static unsigned long clk_core_get_rate(struct clk_core *clk)
1372{
1373 unsigned long rate;
1374
1375 clk_prepare_lock();
1376
1377 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1378 __clk_recalc_rates(clk, 0);
1379
1380 rate = clk_core_get_rate_nolock(clk);
1381 clk_prepare_unlock();
1382
1383 return rate;
1384}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001385
Mike Turquetteb24764902012-03-15 23:11:19 -07001386/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001387 * clk_get_rate - return the rate of clk
1388 * @clk: the clk whose rate is being returned
1389 *
1390 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1391 * is set, which means a recalc_rate will be issued.
1392 * If clk is NULL then returns 0.
1393 */
1394unsigned long clk_get_rate(struct clk *clk)
1395{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001396 if (!clk)
1397 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001398
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001399 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001400}
1401EXPORT_SYMBOL_GPL(clk_get_rate);
1402
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001403static int clk_fetch_parent_index(struct clk_core *clk,
1404 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001405{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001406 int i;
James Hogan4935b222013-07-29 12:24:59 +01001407
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001408 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001409 clk->parents = kcalloc(clk->num_parents,
1410 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001411 if (!clk->parents)
1412 return -ENOMEM;
1413 }
James Hogan4935b222013-07-29 12:24:59 +01001414
1415 /*
1416 * find index of new parent clock using cached parent ptrs,
1417 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001418 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001419 */
1420 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001421 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001422 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001423
1424 if (clk->parents[i])
1425 continue;
1426
1427 if (!strcmp(clk->parent_names[i], parent->name)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001428 clk->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001429 return i;
James Hogan4935b222013-07-29 12:24:59 +01001430 }
1431 }
1432
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001433 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001434}
1435
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001436static void clk_reparent(struct clk_core *clk, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001437{
1438 hlist_del(&clk->child_node);
1439
James Hogan903efc52013-08-29 12:10:51 +01001440 if (new_parent) {
1441 /* avoid duplicate POST_RATE_CHANGE notifications */
1442 if (new_parent->new_child == clk)
1443 new_parent->new_child = NULL;
1444
James Hogan4935b222013-07-29 12:24:59 +01001445 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001446 } else {
James Hogan4935b222013-07-29 12:24:59 +01001447 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001448 }
James Hogan4935b222013-07-29 12:24:59 +01001449
1450 clk->parent = new_parent;
1451}
1452
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001453static struct clk_core *__clk_set_parent_before(struct clk_core *clk,
1454 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001455{
1456 unsigned long flags;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001457 struct clk_core *old_parent = clk->parent;
James Hogan4935b222013-07-29 12:24:59 +01001458
1459 /*
1460 * Migrate prepare state between parents and prevent race with
1461 * clk_enable().
1462 *
1463 * If the clock is not prepared, then a race with
1464 * clk_enable/disable() is impossible since we already have the
1465 * prepare lock (future calls to clk_enable() need to be preceded by
1466 * a clk_prepare()).
1467 *
1468 * If the clock is prepared, migrate the prepared state to the new
1469 * parent and also protect against a race with clk_enable() by
1470 * forcing the clock and the new parent on. This ensures that all
1471 * future calls to clk_enable() are practically NOPs with respect to
1472 * hardware and software states.
1473 *
1474 * See also: Comment for clk_set_parent() below.
1475 */
1476 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001477 clk_core_prepare(parent);
1478 clk_core_enable(parent);
1479 clk_core_enable(clk);
James Hogan4935b222013-07-29 12:24:59 +01001480 }
1481
1482 /* update the clk tree topology */
1483 flags = clk_enable_lock();
1484 clk_reparent(clk, parent);
1485 clk_enable_unlock(flags);
1486
Stephen Boyd3fa22522014-01-15 10:47:22 -08001487 return old_parent;
1488}
1489
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001490static void __clk_set_parent_after(struct clk_core *core,
1491 struct clk_core *parent,
1492 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001493{
1494 /*
1495 * Finish the migration of prepare state and undo the changes done
1496 * for preventing a race with clk_enable().
1497 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001498 if (core->prepare_count) {
1499 clk_core_disable(core);
1500 clk_core_disable(old_parent);
1501 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001502 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001503}
1504
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001505static int __clk_set_parent(struct clk_core *clk, struct clk_core *parent,
1506 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001507{
1508 unsigned long flags;
1509 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001510 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001511
1512 old_parent = __clk_set_parent_before(clk, parent);
1513
Stephen Boyddfc202e2015-02-02 14:37:41 -08001514 trace_clk_set_parent(clk, parent);
1515
James Hogan4935b222013-07-29 12:24:59 +01001516 /* change clock input source */
1517 if (parent && clk->ops->set_parent)
1518 ret = clk->ops->set_parent(clk->hw, p_index);
1519
Stephen Boyddfc202e2015-02-02 14:37:41 -08001520 trace_clk_set_parent_complete(clk, parent);
1521
James Hogan4935b222013-07-29 12:24:59 +01001522 if (ret) {
1523 flags = clk_enable_lock();
1524 clk_reparent(clk, old_parent);
1525 clk_enable_unlock(flags);
1526
1527 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001528 clk_core_disable(clk);
1529 clk_core_disable(parent);
1530 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001531 }
1532 return ret;
1533 }
1534
Stephen Boyd3fa22522014-01-15 10:47:22 -08001535 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001536
James Hogan4935b222013-07-29 12:24:59 +01001537 return 0;
1538}
1539
Ulf Hanssona093bde2012-08-31 14:21:28 +02001540/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001541 * __clk_speculate_rates
1542 * @clk: first clk in the subtree
1543 * @parent_rate: the "future" rate of clk's parent
1544 *
1545 * Walks the subtree of clks starting with clk, speculating rates as it
1546 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1547 *
1548 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1549 * pre-rate change notifications and returns early if no clks in the
1550 * subtree have subscribed to the notifications. Note that if a clk does not
1551 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001552 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001553 *
1554 * Caller must hold prepare_lock.
1555 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001556static int __clk_speculate_rates(struct clk_core *clk,
1557 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001558{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001559 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001560 unsigned long new_rate;
1561 int ret = NOTIFY_DONE;
1562
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001563 lockdep_assert_held(&prepare_lock);
1564
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001565 new_rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001566
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001567 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001568 if (clk->notifier_count)
1569 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1570
Mike Turquette86bcfa22014-02-24 16:08:41 -08001571 if (ret & NOTIFY_STOP_MASK) {
1572 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1573 __func__, clk->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001574 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001575 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001576
Sasha Levinb67bfe02013-02-27 17:06:00 -08001577 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001578 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001579 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001580 break;
1581 }
1582
1583out:
1584 return ret;
1585}
1586
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001587static void clk_calc_subtree(struct clk_core *clk, unsigned long new_rate,
1588 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001589{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001590 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001591
1592 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001593 clk->new_parent = new_parent;
1594 clk->new_parent_index = p_index;
1595 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1596 clk->new_child = NULL;
1597 if (new_parent && new_parent != clk->parent)
1598 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001599
Sasha Levinb67bfe02013-02-27 17:06:00 -08001600 hlist_for_each_entry(child, &clk->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001601 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001602 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001603 }
1604}
1605
1606/*
1607 * calculate the new rates returning the topmost clock that has to be
1608 * changed.
1609 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001610static struct clk_core *clk_calc_new_rates(struct clk_core *clk,
1611 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001612{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001613 struct clk_core *top = clk;
1614 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001615 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001616 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001617 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001618 unsigned long min_rate;
1619 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001620 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001621
Mike Turquette7452b212012-03-26 14:45:36 -07001622 /* sanity */
1623 if (IS_ERR_OR_NULL(clk))
1624 return NULL;
1625
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001626 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001627 parent = old_parent = clk->parent;
1628 if (parent)
1629 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001630
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001631 clk_core_get_boundaries(clk, &min_rate, &max_rate);
1632
James Hogan71472c02013-07-29 12:25:00 +01001633 /* find the closest rate and parent clk/rate */
1634 if (clk->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001635 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001636 new_rate = clk->ops->determine_rate(clk->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001637 min_rate,
1638 max_rate,
James Hogan71472c02013-07-29 12:25:00 +01001639 &best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001640 &parent_hw);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001641 parent = parent_hw ? parent_hw->core : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001642 } else if (clk->ops->round_rate) {
1643 new_rate = clk->ops->round_rate(clk->hw, rate,
1644 &best_parent_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001645 if (new_rate < min_rate || new_rate > max_rate)
1646 return NULL;
James Hogan71472c02013-07-29 12:25:00 +01001647 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1648 /* pass-through clock without adjustable parent */
1649 clk->new_rate = clk->rate;
1650 return NULL;
1651 } else {
1652 /* pass-through clock with adjustable parent */
1653 top = clk_calc_new_rates(parent, rate);
1654 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001655 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001656 }
1657
James Hogan71472c02013-07-29 12:25:00 +01001658 /* some clocks must be gated to change parent */
1659 if (parent != old_parent &&
1660 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1661 pr_debug("%s: %s not gated but wants to reparent\n",
1662 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001663 return NULL;
1664 }
1665
James Hogan71472c02013-07-29 12:25:00 +01001666 /* try finding the new parent index */
Stephen Boyd4526e7b2014-12-22 11:26:42 -08001667 if (parent && clk->num_parents > 1) {
James Hogan71472c02013-07-29 12:25:00 +01001668 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001669 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001670 pr_debug("%s: clk %s can not be parent of clk %s\n",
1671 __func__, parent->name, clk->name);
1672 return NULL;
1673 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001674 }
1675
James Hogan71472c02013-07-29 12:25:00 +01001676 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1677 best_parent_rate != parent->rate)
1678 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001679
1680out:
James Hogan71472c02013-07-29 12:25:00 +01001681 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001682
1683 return top;
1684}
1685
1686/*
1687 * Notify about rate changes in a subtree. Always walk down the whole tree
1688 * so that in case of an error we can walk down the whole tree again and
1689 * abort the change.
1690 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001691static struct clk_core *clk_propagate_rate_change(struct clk_core *clk,
1692 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001693{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001694 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001695 int ret = NOTIFY_DONE;
1696
1697 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301698 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001699
1700 if (clk->notifier_count) {
1701 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001702 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001703 fail_clk = clk;
1704 }
1705
Sasha Levinb67bfe02013-02-27 17:06:00 -08001706 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001707 /* Skip children who will be reparented to another clock */
1708 if (child->new_parent && child->new_parent != clk)
1709 continue;
1710 tmp_clk = clk_propagate_rate_change(child, event);
1711 if (tmp_clk)
1712 fail_clk = tmp_clk;
1713 }
1714
1715 /* handle the new child who might not be in clk->children yet */
1716 if (clk->new_child) {
1717 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1718 if (tmp_clk)
1719 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001720 }
1721
1722 return fail_clk;
1723}
1724
1725/*
1726 * walk down a subtree and set the new rates notifying the rate
1727 * change on the way
1728 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001729static void clk_change_rate(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001730{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001731 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001732 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001733 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001734 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001735 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001736 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001737
1738 old_rate = clk->rate;
1739
Stephen Boyd3fa22522014-01-15 10:47:22 -08001740 if (clk->new_parent)
1741 best_parent_rate = clk->new_parent->rate;
1742 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001743 best_parent_rate = clk->parent->rate;
1744
Stephen Boyd3fa22522014-01-15 10:47:22 -08001745 if (clk->new_parent && clk->new_parent != clk->parent) {
1746 old_parent = __clk_set_parent_before(clk, clk->new_parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001747 trace_clk_set_parent(clk, clk->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001748
1749 if (clk->ops->set_rate_and_parent) {
1750 skip_set_rate = true;
1751 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1752 best_parent_rate,
1753 clk->new_parent_index);
1754 } else if (clk->ops->set_parent) {
1755 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1756 }
1757
Stephen Boyddfc202e2015-02-02 14:37:41 -08001758 trace_clk_set_parent_complete(clk, clk->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001759 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1760 }
1761
Stephen Boyddfc202e2015-02-02 14:37:41 -08001762 trace_clk_set_rate(clk, clk->new_rate);
1763
Stephen Boyd3fa22522014-01-15 10:47:22 -08001764 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001765 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001766
Stephen Boyddfc202e2015-02-02 14:37:41 -08001767 trace_clk_set_rate_complete(clk, clk->new_rate);
1768
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001769 clk->rate = clk_recalc(clk, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001770
1771 if (clk->notifier_count && old_rate != clk->rate)
1772 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1773
Tero Kristo067bb172014-08-21 16:47:45 +03001774 /*
1775 * Use safe iteration, as change_rate can actually swap parents
1776 * for certain clock types.
1777 */
1778 hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001779 /* Skip children who will be reparented to another clock */
1780 if (child->new_parent && child->new_parent != clk)
1781 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001782 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001783 }
1784
1785 /* handle the new child who might not be in clk->children yet */
1786 if (clk->new_child)
1787 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001788}
1789
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001790static int clk_core_set_rate_nolock(struct clk_core *clk,
1791 unsigned long req_rate)
1792{
1793 struct clk_core *top, *fail_clk;
1794 unsigned long rate = req_rate;
1795 int ret = 0;
1796
1797 if (!clk)
1798 return 0;
1799
1800 /* bail early if nothing to do */
1801 if (rate == clk_core_get_rate_nolock(clk))
1802 return 0;
1803
1804 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count)
1805 return -EBUSY;
1806
1807 /* calculate new rates and get the topmost changed clock */
1808 top = clk_calc_new_rates(clk, rate);
1809 if (!top)
1810 return -EINVAL;
1811
1812 /* notify that we are about to change rates */
1813 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1814 if (fail_clk) {
1815 pr_debug("%s: failed to set %s rate\n", __func__,
1816 fail_clk->name);
1817 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1818 return -EBUSY;
1819 }
1820
1821 /* change the rates */
1822 clk_change_rate(top);
1823
1824 clk->req_rate = req_rate;
1825
1826 return ret;
1827}
1828
Mike Turquetteb24764902012-03-15 23:11:19 -07001829/**
1830 * clk_set_rate - specify a new rate for clk
1831 * @clk: the clk whose rate is being changed
1832 * @rate: the new rate for clk
1833 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001834 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001835 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001836 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1837 * propagate up to clk's parent; whether or not this happens depends on the
1838 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1839 * after calling .round_rate then upstream parent propagation is ignored. If
1840 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001841 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001842 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1843 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001844 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001845 * Rate changes are accomplished via tree traversal that also recalculates the
1846 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001847 *
1848 * Returns 0 on success, -EERROR otherwise.
1849 */
1850int clk_set_rate(struct clk *clk, unsigned long rate)
1851{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001852 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001853
Mike Turquette89ac8d72013-08-21 23:58:09 -07001854 if (!clk)
1855 return 0;
1856
Mike Turquetteb24764902012-03-15 23:11:19 -07001857 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001858 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001859
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001860 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001861
Mike Turquetteeab89f62013-03-28 13:59:01 -07001862 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001863
1864 return ret;
1865}
1866EXPORT_SYMBOL_GPL(clk_set_rate);
1867
1868/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001869 * clk_set_rate_range - set a rate range for a clock source
1870 * @clk: clock source
1871 * @min: desired minimum clock rate in Hz, inclusive
1872 * @max: desired maximum clock rate in Hz, inclusive
1873 *
1874 * Returns success (0) or negative errno.
1875 */
1876int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1877{
1878 int ret = 0;
1879
1880 if (!clk)
1881 return 0;
1882
1883 if (min > max) {
1884 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1885 __func__, clk->core->name, clk->dev_id, clk->con_id,
1886 min, max);
1887 return -EINVAL;
1888 }
1889
1890 clk_prepare_lock();
1891
1892 if (min != clk->min_rate || max != clk->max_rate) {
1893 clk->min_rate = min;
1894 clk->max_rate = max;
1895 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1896 }
1897
1898 clk_prepare_unlock();
1899
1900 return ret;
1901}
1902EXPORT_SYMBOL_GPL(clk_set_rate_range);
1903
1904/**
1905 * clk_set_min_rate - set a minimum clock rate for a clock source
1906 * @clk: clock source
1907 * @rate: desired minimum clock rate in Hz, inclusive
1908 *
1909 * Returns success (0) or negative errno.
1910 */
1911int clk_set_min_rate(struct clk *clk, unsigned long rate)
1912{
1913 if (!clk)
1914 return 0;
1915
1916 return clk_set_rate_range(clk, rate, clk->max_rate);
1917}
1918EXPORT_SYMBOL_GPL(clk_set_min_rate);
1919
1920/**
1921 * clk_set_max_rate - set a maximum clock rate for a clock source
1922 * @clk: clock source
1923 * @rate: desired maximum clock rate in Hz, inclusive
1924 *
1925 * Returns success (0) or negative errno.
1926 */
1927int clk_set_max_rate(struct clk *clk, unsigned long rate)
1928{
1929 if (!clk)
1930 return 0;
1931
1932 return clk_set_rate_range(clk, clk->min_rate, rate);
1933}
1934EXPORT_SYMBOL_GPL(clk_set_max_rate);
1935
1936/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001937 * clk_get_parent - return the parent of a clk
1938 * @clk: the clk whose parent gets returned
1939 *
1940 * Simply returns clk->parent. Returns NULL if clk is NULL.
1941 */
1942struct clk *clk_get_parent(struct clk *clk)
1943{
1944 struct clk *parent;
1945
Mike Turquetteeab89f62013-03-28 13:59:01 -07001946 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001947 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001948 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001949
1950 return parent;
1951}
1952EXPORT_SYMBOL_GPL(clk_get_parent);
1953
1954/*
1955 * .get_parent is mandatory for clocks with multiple possible parents. It is
1956 * optional for single-parent clocks. Always call .get_parent if it is
1957 * available and WARN if it is missing for multi-parent clocks.
1958 *
1959 * For single-parent clocks without .get_parent, first check to see if the
1960 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001961 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001962 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001963static struct clk_core *__clk_init_parent(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001964{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001965 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001966 u8 index;
1967
1968 /* handle the trivial cases */
1969
1970 if (!clk->num_parents)
1971 goto out;
1972
1973 if (clk->num_parents == 1) {
1974 if (IS_ERR_OR_NULL(clk->parent))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001975 clk->parent = clk_core_lookup(clk->parent_names[0]);
Mike Turquetteb24764902012-03-15 23:11:19 -07001976 ret = clk->parent;
1977 goto out;
1978 }
1979
1980 if (!clk->ops->get_parent) {
1981 WARN(!clk->ops->get_parent,
1982 "%s: multi-parent clocks must implement .get_parent\n",
1983 __func__);
1984 goto out;
1985 };
1986
1987 /*
1988 * Do our best to cache parent clocks in clk->parents. This prevents
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001989 * unnecessary and expensive lookups. We don't set clk->parent here;
1990 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001991 */
1992
1993 index = clk->ops->get_parent(clk->hw);
1994
1995 if (!clk->parents)
1996 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001997 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001998 GFP_KERNEL);
1999
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002000 ret = clk_core_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002001
2002out:
2003 return ret;
2004}
2005
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002006static void clk_core_reparent(struct clk_core *clk,
2007 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002008{
2009 clk_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002010 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07002011 __clk_recalc_rates(clk, POST_RATE_CHANGE);
2012}
2013
Mike Turquetteb24764902012-03-15 23:11:19 -07002014/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002015 * clk_has_parent - check if a clock is a possible parent for another
2016 * @clk: clock source
2017 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002018 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002019 * This function can be used in drivers that need to check that a clock can be
2020 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002021 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002022 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002023 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002024bool clk_has_parent(struct clk *clk, struct clk *parent)
2025{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002026 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002027 unsigned int i;
2028
2029 /* NULL clocks should be nops, so return success if either is NULL. */
2030 if (!clk || !parent)
2031 return true;
2032
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002033 core = clk->core;
2034 parent_core = parent->core;
2035
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002036 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002037 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002038 return true;
2039
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002040 for (i = 0; i < core->num_parents; i++)
2041 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002042 return true;
2043
2044 return false;
2045}
2046EXPORT_SYMBOL_GPL(clk_has_parent);
2047
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002048static int clk_core_set_parent(struct clk_core *clk, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002049{
2050 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002051 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002052 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002053
Mike Turquette89ac8d72013-08-21 23:58:09 -07002054 if (!clk)
2055 return 0;
2056
Mike Turquetteb24764902012-03-15 23:11:19 -07002057 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002058 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002059
2060 if (clk->parent == parent)
2061 goto out;
2062
Stephen Boydb61c43c2015-02-02 14:11:25 -08002063 /* verify ops for for multi-parent clks */
2064 if ((clk->num_parents > 1) && (!clk->ops->set_parent)) {
2065 ret = -ENOSYS;
2066 goto out;
2067 }
2068
Ulf Hansson031dcc92013-04-02 23:09:38 +02002069 /* check that we are allowed to re-parent if the clock is in use */
2070 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
2071 ret = -EBUSY;
2072 goto out;
2073 }
2074
2075 /* try finding the new parent index */
2076 if (parent) {
2077 p_index = clk_fetch_parent_index(clk, parent);
2078 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002079 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002080 pr_debug("%s: clk %s can not be parent of clk %s\n",
2081 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002082 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002083 goto out;
2084 }
2085 }
2086
Mike Turquetteb24764902012-03-15 23:11:19 -07002087 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07002088 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002089
2090 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002091 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002092 goto out;
2093
Ulf Hansson031dcc92013-04-02 23:09:38 +02002094 /* do the re-parent */
2095 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002096
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002097 /* propagate rate an accuracy recalculation accordingly */
2098 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07002099 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002100 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02002101 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002102 __clk_recalc_accuracies(clk);
2103 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002104
2105out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002106 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002107
2108 return ret;
2109}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002110
2111/**
2112 * clk_set_parent - switch the parent of a mux clk
2113 * @clk: the mux clk whose input we are switching
2114 * @parent: the new input to clk
2115 *
2116 * Re-parent clk to use parent as its new input source. If clk is in
2117 * prepared state, the clk will get enabled for the duration of this call. If
2118 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2119 * that, the reparenting is glitchy in hardware, etc), use the
2120 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2121 *
2122 * After successfully changing clk's parent clk_set_parent will update the
2123 * clk topology, sysfs topology and propagate rate recalculation via
2124 * __clk_recalc_rates.
2125 *
2126 * Returns 0 on success, -EERROR otherwise.
2127 */
2128int clk_set_parent(struct clk *clk, struct clk *parent)
2129{
2130 if (!clk)
2131 return 0;
2132
2133 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2134}
Mike Turquetteb24764902012-03-15 23:11:19 -07002135EXPORT_SYMBOL_GPL(clk_set_parent);
2136
2137/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002138 * clk_set_phase - adjust the phase shift of a clock signal
2139 * @clk: clock signal source
2140 * @degrees: number of degrees the signal is shifted
2141 *
2142 * Shifts the phase of a clock signal by the specified
2143 * degrees. Returns 0 on success, -EERROR otherwise.
2144 *
2145 * This function makes no distinction about the input or reference
2146 * signal that we adjust the clock signal phase against. For example
2147 * phase locked-loop clock signal generators we may shift phase with
2148 * respect to feedback clock signal input, but for other cases the
2149 * clock phase may be shifted with respect to some other, unspecified
2150 * signal.
2151 *
2152 * Additionally the concept of phase shift does not propagate through
2153 * the clock tree hierarchy, which sets it apart from clock rates and
2154 * clock accuracy. A parent clock phase attribute does not have an
2155 * impact on the phase attribute of a child clock.
2156 */
2157int clk_set_phase(struct clk *clk, int degrees)
2158{
Stephen Boyd08b95752015-02-02 14:09:43 -08002159 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002160
2161 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002162 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002163
2164 /* sanity check degrees */
2165 degrees %= 360;
2166 if (degrees < 0)
2167 degrees += 360;
2168
2169 clk_prepare_lock();
2170
Stephen Boyddfc202e2015-02-02 14:37:41 -08002171 trace_clk_set_phase(clk->core, degrees);
2172
Stephen Boyd08b95752015-02-02 14:09:43 -08002173 if (clk->core->ops->set_phase)
2174 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002175
Stephen Boyddfc202e2015-02-02 14:37:41 -08002176 trace_clk_set_phase_complete(clk->core, degrees);
2177
Mike Turquettee59c5372014-02-18 21:21:25 -08002178 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002179 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002180
Mike Turquettee59c5372014-02-18 21:21:25 -08002181 clk_prepare_unlock();
2182
Mike Turquettee59c5372014-02-18 21:21:25 -08002183 return ret;
2184}
Maxime Ripard9767b042015-01-20 22:23:43 +01002185EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002186
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002187static int clk_core_get_phase(struct clk_core *clk)
Mike Turquettee59c5372014-02-18 21:21:25 -08002188{
2189 int ret = 0;
2190
2191 if (!clk)
2192 goto out;
2193
2194 clk_prepare_lock();
2195 ret = clk->phase;
2196 clk_prepare_unlock();
2197
2198out:
2199 return ret;
2200}
Maxime Ripard9767b042015-01-20 22:23:43 +01002201EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002202
2203/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002204 * clk_get_phase - return the phase shift of a clock signal
2205 * @clk: clock signal source
2206 *
2207 * Returns the phase shift of a clock node in degrees, otherwise returns
2208 * -EERROR.
2209 */
2210int clk_get_phase(struct clk *clk)
2211{
2212 if (!clk)
2213 return 0;
2214
2215 return clk_core_get_phase(clk->core);
2216}
Mike Turquetteb24764902012-03-15 23:11:19 -07002217
2218/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002219 * clk_is_match - check if two clk's point to the same hardware clock
2220 * @p: clk compared against q
2221 * @q: clk compared against p
2222 *
2223 * Returns true if the two struct clk pointers both point to the same hardware
2224 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2225 * share the same struct clk_core object.
2226 *
2227 * Returns false otherwise. Note that two NULL clks are treated as matching.
2228 */
2229bool clk_is_match(const struct clk *p, const struct clk *q)
2230{
2231 /* trivial case: identical struct clk's or both NULL */
2232 if (p == q)
2233 return true;
2234
2235 /* true if clk->core pointers match. Avoid derefing garbage */
2236 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2237 if (p->core == q->core)
2238 return true;
2239
2240 return false;
2241}
2242EXPORT_SYMBOL_GPL(clk_is_match);
2243
2244/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002245 * __clk_init - initialize the data structures in a struct clk
2246 * @dev: device initializing this clk, placeholder for now
2247 * @clk: clk being initialized
2248 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002249 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002250 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002251 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002252static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002253{
Mike Turquetted1302a32012-03-29 14:30:40 -07002254 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002255 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002256 struct hlist_node *tmp2;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002257 struct clk_core *clk;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002258 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002259
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002260 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002261 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002262
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002263 clk = clk_user->core;
2264
Mike Turquetteeab89f62013-03-28 13:59:01 -07002265 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002266
2267 /* check to see if a clock with this name is already registered */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002268 if (clk_core_lookup(clk->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002269 pr_debug("%s: clk %s already initialized\n",
2270 __func__, clk->name);
2271 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002272 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002273 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002274
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002275 /* check that clk_ops are sane. See Documentation/clk.txt */
2276 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01002277 !((clk->ops->round_rate || clk->ops->determine_rate) &&
2278 clk->ops->recalc_rate)) {
2279 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002280 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002281 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002282 goto out;
2283 }
2284
2285 if (clk->ops->set_parent && !clk->ops->get_parent) {
2286 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
2287 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002288 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002289 goto out;
2290 }
2291
Stephen Boyd3fa22522014-01-15 10:47:22 -08002292 if (clk->ops->set_rate_and_parent &&
2293 !(clk->ops->set_parent && clk->ops->set_rate)) {
2294 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
2295 __func__, clk->name);
2296 ret = -EINVAL;
2297 goto out;
2298 }
2299
Mike Turquetteb24764902012-03-15 23:11:19 -07002300 /* throw a WARN if any entries in parent_names are NULL */
2301 for (i = 0; i < clk->num_parents; i++)
2302 WARN(!clk->parent_names[i],
2303 "%s: invalid NULL in %s's .parent_names\n",
2304 __func__, clk->name);
2305
2306 /*
2307 * Allocate an array of struct clk *'s to avoid unnecessary string
2308 * look-ups of clk's possible parents. This can fail for clocks passed
2309 * in to clk_init during early boot; thus any access to clk->parents[]
2310 * must always check for a NULL pointer and try to populate it if
2311 * necessary.
2312 *
2313 * If clk->parents is not NULL we skip this entire block. This allows
2314 * for clock drivers to statically initialize clk->parents.
2315 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05302316 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002317 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
2318 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002319 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002320 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002321 * clk_init'd; thus any access to clk->parents[] must check
2322 * for a NULL pointer. We can always perform lazy lookups for
2323 * missing parents later on.
2324 */
2325 if (clk->parents)
2326 for (i = 0; i < clk->num_parents; i++)
2327 clk->parents[i] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002328 clk_core_lookup(clk->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002329 }
2330
2331 clk->parent = __clk_init_parent(clk);
2332
2333 /*
2334 * Populate clk->parent if parent has already been __clk_init'd. If
2335 * parent has not yet been __clk_init'd then place clk in the orphan
2336 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2337 * clk list.
2338 *
2339 * Every time a new clk is clk_init'd then we walk the list of orphan
2340 * clocks and re-parent any that are children of the clock currently
2341 * being clk_init'd.
2342 */
2343 if (clk->parent)
2344 hlist_add_head(&clk->child_node,
2345 &clk->parent->children);
2346 else if (clk->flags & CLK_IS_ROOT)
2347 hlist_add_head(&clk->child_node, &clk_root_list);
2348 else
2349 hlist_add_head(&clk->child_node, &clk_orphan_list);
2350
2351 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002352 * Set clk's accuracy. The preferred method is to use
2353 * .recalc_accuracy. For simple clocks and lazy developers the default
2354 * fallback is to use the parent's accuracy. If a clock doesn't have a
2355 * parent (or is orphaned) then accuracy is set to zero (perfect
2356 * clock).
2357 */
2358 if (clk->ops->recalc_accuracy)
2359 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
2360 __clk_get_accuracy(clk->parent));
2361 else if (clk->parent)
2362 clk->accuracy = clk->parent->accuracy;
2363 else
2364 clk->accuracy = 0;
2365
2366 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002367 * Set clk's phase.
2368 * Since a phase is by definition relative to its parent, just
2369 * query the current clock phase, or just assume it's in phase.
2370 */
2371 if (clk->ops->get_phase)
2372 clk->phase = clk->ops->get_phase(clk->hw);
2373 else
2374 clk->phase = 0;
2375
2376 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002377 * Set clk's rate. The preferred method is to use .recalc_rate. For
2378 * simple clocks and lazy developers the default fallback is to use the
2379 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2380 * then rate is set to zero.
2381 */
2382 if (clk->ops->recalc_rate)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002383 rate = clk->ops->recalc_rate(clk->hw,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002384 clk_core_get_rate_nolock(clk->parent));
Mike Turquetteb24764902012-03-15 23:11:19 -07002385 else if (clk->parent)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002386 rate = clk->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002387 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002388 rate = 0;
2389 clk->rate = clk->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002390
2391 /*
2392 * walk the list of orphan clocks and reparent any that are children of
2393 * this clock
2394 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002395 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002396 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002397 i = orphan->ops->get_parent(orphan->hw);
2398 if (!strcmp(clk->name, orphan->parent_names[i]))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002399 clk_core_reparent(orphan, clk);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002400 continue;
2401 }
2402
Mike Turquetteb24764902012-03-15 23:11:19 -07002403 for (i = 0; i < orphan->num_parents; i++)
2404 if (!strcmp(clk->name, orphan->parent_names[i])) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002405 clk_core_reparent(orphan, clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07002406 break;
2407 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002408 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002409
2410 /*
2411 * optional platform-specific magic
2412 *
2413 * The .init callback is not used by any of the basic clock types, but
2414 * exists for weird hardware that must perform initialization magic.
2415 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002416 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002417 */
2418 if (clk->ops->init)
2419 clk->ops->init(clk->hw);
2420
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002421 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002422out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002423 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002424
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002425 if (!ret)
2426 clk_debug_register(clk);
2427
Mike Turquetted1302a32012-03-29 14:30:40 -07002428 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002429}
2430
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002431struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2432 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002433{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002434 struct clk *clk;
2435
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002436 /* This is to allow this function to be chained to others */
2437 if (!hw || IS_ERR(hw))
2438 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002439
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002440 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2441 if (!clk)
2442 return ERR_PTR(-ENOMEM);
2443
2444 clk->core = hw->core;
2445 clk->dev_id = dev_id;
2446 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002447 clk->max_rate = ULONG_MAX;
2448
2449 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002450 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002451 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002452
2453 return clk;
2454}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002455
Stephen Boyd73e0e492015-02-06 11:42:43 -08002456void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002457{
2458 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002459 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002460 clk_prepare_unlock();
2461
2462 kfree(clk);
2463}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002464
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002465/**
2466 * clk_register - allocate a new clock, register it and return an opaque cookie
2467 * @dev: device that is registering this clock
2468 * @hw: link to hardware-specific clock data
2469 *
2470 * clk_register is the primary interface for populating the clock tree with new
2471 * clock nodes. It returns a pointer to the newly allocated struct clk which
2472 * cannot be dereferenced by driver code but may be used in conjuction with the
2473 * rest of the clock API. In the event of an error clk_register will return an
2474 * error code; drivers must test for an error code after calling clk_register.
2475 */
2476struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002477{
Mike Turquetted1302a32012-03-29 14:30:40 -07002478 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002479 struct clk_core *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002480
2481 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2482 if (!clk) {
2483 pr_err("%s: could not allocate clk\n", __func__);
2484 ret = -ENOMEM;
2485 goto fail_out;
2486 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002487
Andrzej Hajda612936f2015-02-13 14:36:33 -08002488 clk->name = kstrdup_const(hw->init->name, GFP_KERNEL);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002489 if (!clk->name) {
2490 pr_err("%s: could not allocate clk->name\n", __func__);
2491 ret = -ENOMEM;
2492 goto fail_name;
2493 }
2494 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002495 if (dev && dev->driver)
2496 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07002497 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002498 clk->flags = hw->init->flags;
2499 clk->num_parents = hw->init->num_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002500 hw->core = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002501
Mike Turquetted1302a32012-03-29 14:30:40 -07002502 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002503 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
2504 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002505
Mike Turquetted1302a32012-03-29 14:30:40 -07002506 if (!clk->parent_names) {
2507 pr_err("%s: could not allocate clk->parent_names\n", __func__);
2508 ret = -ENOMEM;
2509 goto fail_parent_names;
2510 }
2511
2512
2513 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002514 for (i = 0; i < clk->num_parents; i++) {
Andrzej Hajda612936f2015-02-13 14:36:33 -08002515 clk->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002516 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002517 if (!clk->parent_names[i]) {
2518 pr_err("%s: could not copy parent_names\n", __func__);
2519 ret = -ENOMEM;
2520 goto fail_parent_names_copy;
2521 }
2522 }
2523
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002524 INIT_HLIST_HEAD(&clk->clks);
2525
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002526 hw->clk = __clk_create_clk(hw, NULL, NULL);
2527 if (IS_ERR(hw->clk)) {
2528 pr_err("%s: could not allocate per-user clk\n", __func__);
2529 ret = PTR_ERR(hw->clk);
2530 goto fail_parent_names_copy;
2531 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002532
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002533 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002534 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002535 return hw->clk;
2536
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002537 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002538 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002539
2540fail_parent_names_copy:
2541 while (--i >= 0)
Andrzej Hajda612936f2015-02-13 14:36:33 -08002542 kfree_const(clk->parent_names[i]);
Mike Turquetted1302a32012-03-29 14:30:40 -07002543 kfree(clk->parent_names);
2544fail_parent_names:
Andrzej Hajda612936f2015-02-13 14:36:33 -08002545 kfree_const(clk->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002546fail_name:
Mike Turquetted1302a32012-03-29 14:30:40 -07002547 kfree(clk);
2548fail_out:
2549 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002550}
2551EXPORT_SYMBOL_GPL(clk_register);
2552
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002553/*
2554 * Free memory allocated for a clock.
2555 * Caller must hold prepare_lock.
2556 */
2557static void __clk_release(struct kref *ref)
2558{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002559 struct clk_core *clk = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002560 int i = clk->num_parents;
2561
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002562 lockdep_assert_held(&prepare_lock);
2563
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002564 kfree(clk->parents);
2565 while (--i >= 0)
Andrzej Hajda612936f2015-02-13 14:36:33 -08002566 kfree_const(clk->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002567
2568 kfree(clk->parent_names);
Andrzej Hajda612936f2015-02-13 14:36:33 -08002569 kfree_const(clk->name);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002570 kfree(clk);
2571}
2572
2573/*
2574 * Empty clk_ops for unregistered clocks. These are used temporarily
2575 * after clk_unregister() was called on a clock and until last clock
2576 * consumer calls clk_put() and the struct clk object is freed.
2577 */
2578static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2579{
2580 return -ENXIO;
2581}
2582
2583static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2584{
2585 WARN_ON_ONCE(1);
2586}
2587
2588static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2589 unsigned long parent_rate)
2590{
2591 return -ENXIO;
2592}
2593
2594static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2595{
2596 return -ENXIO;
2597}
2598
2599static const struct clk_ops clk_nodrv_ops = {
2600 .enable = clk_nodrv_prepare_enable,
2601 .disable = clk_nodrv_disable_unprepare,
2602 .prepare = clk_nodrv_prepare_enable,
2603 .unprepare = clk_nodrv_disable_unprepare,
2604 .set_rate = clk_nodrv_set_rate,
2605 .set_parent = clk_nodrv_set_parent,
2606};
2607
Mark Brown1df5c932012-04-18 09:07:12 +01002608/**
2609 * clk_unregister - unregister a currently registered clock
2610 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002611 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002612void clk_unregister(struct clk *clk)
2613{
2614 unsigned long flags;
2615
Stephen Boyd6314b672014-09-04 23:37:49 -07002616 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2617 return;
2618
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002619 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002620
2621 clk_prepare_lock();
2622
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002623 if (clk->core->ops == &clk_nodrv_ops) {
2624 pr_err("%s: unregistered clock: %s\n", __func__,
2625 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002626 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002627 }
2628 /*
2629 * Assign empty clock ops for consumers that might still hold
2630 * a reference to this clock.
2631 */
2632 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002633 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002634 clk_enable_unlock(flags);
2635
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002636 if (!hlist_empty(&clk->core->children)) {
2637 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002638 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002639
2640 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002641 hlist_for_each_entry_safe(child, t, &clk->core->children,
2642 child_node)
2643 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002644 }
2645
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002646 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002647
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002648 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002649 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002650 __func__, clk->core->name);
2651 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002652
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002653 clk_prepare_unlock();
2654}
Mark Brown1df5c932012-04-18 09:07:12 +01002655EXPORT_SYMBOL_GPL(clk_unregister);
2656
Stephen Boyd46c87732012-09-24 13:38:04 -07002657static void devm_clk_release(struct device *dev, void *res)
2658{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002659 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002660}
2661
2662/**
2663 * devm_clk_register - resource managed clk_register()
2664 * @dev: device that is registering this clock
2665 * @hw: link to hardware-specific clock data
2666 *
2667 * Managed clk_register(). Clocks returned from this function are
2668 * automatically clk_unregister()ed on driver detach. See clk_register() for
2669 * more information.
2670 */
2671struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2672{
2673 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002674 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002675
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002676 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2677 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002678 return ERR_PTR(-ENOMEM);
2679
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002680 clk = clk_register(dev, hw);
2681 if (!IS_ERR(clk)) {
2682 *clkp = clk;
2683 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002684 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002685 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002686 }
2687
2688 return clk;
2689}
2690EXPORT_SYMBOL_GPL(devm_clk_register);
2691
2692static int devm_clk_match(struct device *dev, void *res, void *data)
2693{
2694 struct clk *c = res;
2695 if (WARN_ON(!c))
2696 return 0;
2697 return c == data;
2698}
2699
2700/**
2701 * devm_clk_unregister - resource managed clk_unregister()
2702 * @clk: clock to unregister
2703 *
2704 * Deallocate a clock allocated with devm_clk_register(). Normally
2705 * this function will not need to be called and the resource management
2706 * code will ensure that the resource is freed.
2707 */
2708void devm_clk_unregister(struct device *dev, struct clk *clk)
2709{
2710 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2711}
2712EXPORT_SYMBOL_GPL(devm_clk_unregister);
2713
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002714/*
2715 * clkdev helpers
2716 */
2717int __clk_get(struct clk *clk)
2718{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002719 struct clk_core *core = !clk ? NULL : clk->core;
2720
2721 if (core) {
2722 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002723 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002724
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002725 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002726 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002727 return 1;
2728}
2729
2730void __clk_put(struct clk *clk)
2731{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002732 struct module *owner;
2733
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002734 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002735 return;
2736
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002737 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002738
Stephen Boyd50595f82015-02-06 11:42:44 -08002739 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002740 if (clk->min_rate > clk->core->req_rate ||
2741 clk->max_rate < clk->core->req_rate)
2742 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2743
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002744 owner = clk->core->owner;
2745 kref_put(&clk->core->ref, __clk_release);
2746
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002747 clk_prepare_unlock();
2748
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002749 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002750
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002751 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002752}
2753
Mike Turquetteb24764902012-03-15 23:11:19 -07002754/*** clk rate change notifiers ***/
2755
2756/**
2757 * clk_notifier_register - add a clk rate change notifier
2758 * @clk: struct clk * to watch
2759 * @nb: struct notifier_block * with callback info
2760 *
2761 * Request notification when clk's rate changes. This uses an SRCU
2762 * notifier because we want it to block and notifier unregistrations are
2763 * uncommon. The callbacks associated with the notifier must not
2764 * re-enter into the clk framework by calling any top-level clk APIs;
2765 * this will cause a nested prepare_lock mutex.
2766 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002767 * In all notification cases cases (pre, post and abort rate change) the
2768 * original clock rate is passed to the callback via struct
2769 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002770 * clk_notifier_data.new_rate.
2771 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002772 * clk_notifier_register() must be called from non-atomic context.
2773 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2774 * allocation failure; otherwise, passes along the return value of
2775 * srcu_notifier_chain_register().
2776 */
2777int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2778{
2779 struct clk_notifier *cn;
2780 int ret = -ENOMEM;
2781
2782 if (!clk || !nb)
2783 return -EINVAL;
2784
Mike Turquetteeab89f62013-03-28 13:59:01 -07002785 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002786
2787 /* search the list of notifiers for this clk */
2788 list_for_each_entry(cn, &clk_notifier_list, node)
2789 if (cn->clk == clk)
2790 break;
2791
2792 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2793 if (cn->clk != clk) {
2794 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2795 if (!cn)
2796 goto out;
2797
2798 cn->clk = clk;
2799 srcu_init_notifier_head(&cn->notifier_head);
2800
2801 list_add(&cn->node, &clk_notifier_list);
2802 }
2803
2804 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2805
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002806 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002807
2808out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002809 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002810
2811 return ret;
2812}
2813EXPORT_SYMBOL_GPL(clk_notifier_register);
2814
2815/**
2816 * clk_notifier_unregister - remove a clk rate change notifier
2817 * @clk: struct clk *
2818 * @nb: struct notifier_block * with callback info
2819 *
2820 * Request no further notification for changes to 'clk' and frees memory
2821 * allocated in clk_notifier_register.
2822 *
2823 * Returns -EINVAL if called with null arguments; otherwise, passes
2824 * along the return value of srcu_notifier_chain_unregister().
2825 */
2826int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2827{
2828 struct clk_notifier *cn = NULL;
2829 int ret = -EINVAL;
2830
2831 if (!clk || !nb)
2832 return -EINVAL;
2833
Mike Turquetteeab89f62013-03-28 13:59:01 -07002834 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002835
2836 list_for_each_entry(cn, &clk_notifier_list, node)
2837 if (cn->clk == clk)
2838 break;
2839
2840 if (cn->clk == clk) {
2841 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2842
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002843 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002844
2845 /* XXX the notifier code should handle this better */
2846 if (!cn->notifier_head.head) {
2847 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002848 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002849 kfree(cn);
2850 }
2851
2852 } else {
2853 ret = -ENOENT;
2854 }
2855
Mike Turquetteeab89f62013-03-28 13:59:01 -07002856 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002857
2858 return ret;
2859}
2860EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002861
2862#ifdef CONFIG_OF
2863/**
2864 * struct of_clk_provider - Clock provider registration structure
2865 * @link: Entry in global list of clock providers
2866 * @node: Pointer to device tree node of clock provider
2867 * @get: Get clock callback. Returns NULL or a struct clk for the
2868 * given clock specifier
2869 * @data: context pointer to be passed into @get callback
2870 */
2871struct of_clk_provider {
2872 struct list_head link;
2873
2874 struct device_node *node;
2875 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2876 void *data;
2877};
2878
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302879static const struct of_device_id __clk_of_table_sentinel
2880 __used __section(__clk_of_table_end);
2881
Grant Likely766e6a42012-04-09 14:50:06 -05002882static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002883static DEFINE_MUTEX(of_clk_mutex);
2884
Grant Likely766e6a42012-04-09 14:50:06 -05002885struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2886 void *data)
2887{
2888 return data;
2889}
2890EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2891
Shawn Guo494bfec2012-08-22 21:36:27 +08002892struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2893{
2894 struct clk_onecell_data *clk_data = data;
2895 unsigned int idx = clkspec->args[0];
2896
2897 if (idx >= clk_data->clk_num) {
2898 pr_err("%s: invalid clock index %d\n", __func__, idx);
2899 return ERR_PTR(-EINVAL);
2900 }
2901
2902 return clk_data->clks[idx];
2903}
2904EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2905
Grant Likely766e6a42012-04-09 14:50:06 -05002906/**
2907 * of_clk_add_provider() - Register a clock provider for a node
2908 * @np: Device node pointer associated with clock provider
2909 * @clk_src_get: callback for decoding clock
2910 * @data: context pointer for @clk_src_get callback.
2911 */
2912int of_clk_add_provider(struct device_node *np,
2913 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2914 void *data),
2915 void *data)
2916{
2917 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002918 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002919
2920 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2921 if (!cp)
2922 return -ENOMEM;
2923
2924 cp->node = of_node_get(np);
2925 cp->data = data;
2926 cp->get = clk_src_get;
2927
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002928 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002929 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002930 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002931 pr_debug("Added clock from %s\n", np->full_name);
2932
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002933 ret = of_clk_set_defaults(np, true);
2934 if (ret < 0)
2935 of_clk_del_provider(np);
2936
2937 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002938}
2939EXPORT_SYMBOL_GPL(of_clk_add_provider);
2940
2941/**
2942 * of_clk_del_provider() - Remove a previously registered clock provider
2943 * @np: Device node pointer associated with clock provider
2944 */
2945void of_clk_del_provider(struct device_node *np)
2946{
2947 struct of_clk_provider *cp;
2948
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002949 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002950 list_for_each_entry(cp, &of_clk_providers, link) {
2951 if (cp->node == np) {
2952 list_del(&cp->link);
2953 of_node_put(cp->node);
2954 kfree(cp);
2955 break;
2956 }
2957 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002958 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002959}
2960EXPORT_SYMBOL_GPL(of_clk_del_provider);
2961
Stephen Boyd73e0e492015-02-06 11:42:43 -08002962struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2963 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002964{
2965 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002966 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002967
Stephen Boyd306c3422015-02-05 15:39:11 -08002968 if (!clkspec)
2969 return ERR_PTR(-EINVAL);
2970
Grant Likely766e6a42012-04-09 14:50:06 -05002971 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002972 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002973 list_for_each_entry(provider, &of_clk_providers, link) {
2974 if (provider->node == clkspec->np)
2975 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002976 if (!IS_ERR(clk)) {
2977 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2978 con_id);
2979
2980 if (!IS_ERR(clk) && !__clk_get(clk)) {
2981 __clk_free_clk(clk);
2982 clk = ERR_PTR(-ENOENT);
2983 }
2984
Grant Likely766e6a42012-04-09 14:50:06 -05002985 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002986 }
Grant Likely766e6a42012-04-09 14:50:06 -05002987 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002988 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002989
2990 return clk;
2991}
2992
Stephen Boyd306c3422015-02-05 15:39:11 -08002993/**
2994 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2995 * @clkspec: pointer to a clock specifier data structure
2996 *
2997 * This function looks up a struct clk from the registered list of clock
2998 * providers, an input is a clock specifier data structure as returned
2999 * from the of_parse_phandle_with_args() function call.
3000 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003001struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3002{
Stephen Boyd306c3422015-02-05 15:39:11 -08003003 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003004}
3005
Mike Turquettef6102742013-10-07 23:12:13 -07003006int of_clk_get_parent_count(struct device_node *np)
3007{
3008 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
3009}
3010EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3011
Grant Likely766e6a42012-04-09 14:50:06 -05003012const char *of_clk_get_parent_name(struct device_node *np, int index)
3013{
3014 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003015 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003016 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003017 const __be32 *vp;
3018 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003019 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003020 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05003021
3022 if (index < 0)
3023 return NULL;
3024
3025 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3026 &clkspec);
3027 if (rc)
3028 return NULL;
3029
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003030 index = clkspec.args_count ? clkspec.args[0] : 0;
3031 count = 0;
3032
3033 /* if there is an indices property, use it to transfer the index
3034 * specified into an array offset for the clock-output-names property.
3035 */
3036 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3037 if (index == pv) {
3038 index = count;
3039 break;
3040 }
3041 count++;
3042 }
3043
Grant Likely766e6a42012-04-09 14:50:06 -05003044 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003045 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003046 &clk_name) < 0)
3047 clk_name = clkspec.np->name;
3048
3049 of_node_put(clkspec.np);
3050 return clk_name;
3051}
3052EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3053
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003054struct clock_provider {
3055 of_clk_init_cb_t clk_init_cb;
3056 struct device_node *np;
3057 struct list_head node;
3058};
3059
3060static LIST_HEAD(clk_provider_list);
3061
3062/*
3063 * This function looks for a parent clock. If there is one, then it
3064 * checks that the provider for this parent clock was initialized, in
3065 * this case the parent clock will be ready.
3066 */
3067static int parent_ready(struct device_node *np)
3068{
3069 int i = 0;
3070
3071 while (true) {
3072 struct clk *clk = of_clk_get(np, i);
3073
3074 /* this parent is ready we can check the next one */
3075 if (!IS_ERR(clk)) {
3076 clk_put(clk);
3077 i++;
3078 continue;
3079 }
3080
3081 /* at least one parent is not ready, we exit now */
3082 if (PTR_ERR(clk) == -EPROBE_DEFER)
3083 return 0;
3084
3085 /*
3086 * Here we make assumption that the device tree is
3087 * written correctly. So an error means that there is
3088 * no more parent. As we didn't exit yet, then the
3089 * previous parent are ready. If there is no clock
3090 * parent, no need to wait for them, then we can
3091 * consider their absence as being ready
3092 */
3093 return 1;
3094 }
3095}
3096
Grant Likely766e6a42012-04-09 14:50:06 -05003097/**
3098 * of_clk_init() - Scan and init clock providers from the DT
3099 * @matches: array of compatible values and init functions for providers.
3100 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003101 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003102 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003103 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003104 */
3105void __init of_clk_init(const struct of_device_id *matches)
3106{
Alex Elder7f7ed582013-08-22 11:31:31 -05003107 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003108 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003109 struct clock_provider *clk_provider, *next;
3110 bool is_init_done;
3111 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003112
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303113 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003114 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303115
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003116 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003117 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003118 struct clock_provider *parent =
3119 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3120
3121 parent->clk_init_cb = match->data;
3122 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003123 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003124 }
3125
3126 while (!list_empty(&clk_provider_list)) {
3127 is_init_done = false;
3128 list_for_each_entry_safe(clk_provider, next,
3129 &clk_provider_list, node) {
3130 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003131
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003132 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003133 of_clk_set_defaults(clk_provider->np, true);
3134
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003135 list_del(&clk_provider->node);
3136 kfree(clk_provider);
3137 is_init_done = true;
3138 }
3139 }
3140
3141 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003142 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003143 * remaining providers during the last loop, so now we
3144 * initialize all the remaining ones unconditionally
3145 * in case the clock parent was not mandatory
3146 */
3147 if (!is_init_done)
3148 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003149 }
3150}
3151#endif