blob: 6a3a282b023eec0e44a35d3e93a9e22c6fdd129d [file] [log] [blame]
Brian Swetland600f7cf2008-09-09 11:04:14 -07001/* arch/arm/mach-msm/clock.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Tianyi Gou7949ecb2012-02-14 14:25:32 -08004 * Copyright (c) 2007-2012, Code Aurora Forum. All rights reserved.
Brian Swetland600f7cf2008-09-09 11:04:14 -07005 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
Brian Swetland600f7cf2008-09-09 11:04:14 -070017#include <linux/kernel.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070018#include <linux/err.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070019#include <linux/spinlock.h>
Stephen Boydbd323442011-02-23 09:37:42 -080020#include <linux/string.h>
21#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/clk.h>
Stephen Boydbd323442011-02-23 09:37:42 -080023#include <linux/clkdev.h>
Matt Wagantall158f73b2012-05-16 11:29:35 -070024#include <linux/list.h>
Stephen Boyd5bc44d52012-03-29 11:00:57 -070025#include <trace/events/power.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070026
27#include "clock.h"
Brian Swetland600f7cf2008-09-09 11:04:14 -070028
Matt Wagantall158f73b2012-05-16 11:29:35 -070029struct handoff_clk {
30 struct list_head list;
31 struct clk *clk;
32};
33static LIST_HEAD(handoff_list);
34
Matt Wagantalle18bbc82011-10-06 10:07:28 -070035/* Find the voltage level required for a given rate. */
Patrick Daly0a78a0e2012-07-23 13:18:59 -070036int find_vdd_level(struct clk *clk, unsigned long rate)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070037{
38 int level;
39
40 for (level = 0; level < ARRAY_SIZE(clk->fmax); level++)
41 if (rate <= clk->fmax[level])
42 break;
43
44 if (level == ARRAY_SIZE(clk->fmax)) {
45 pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
46 clk->dbg_name);
47 return -EINVAL;
48 }
49
50 return level;
51}
52
53/* Update voltage level given the current votes. */
54static int update_vdd(struct clk_vdd_class *vdd_class)
55{
56 int level, rc;
57
58 for (level = ARRAY_SIZE(vdd_class->level_votes)-1; level > 0; level--)
59 if (vdd_class->level_votes[level])
60 break;
61
62 if (level == vdd_class->cur_level)
63 return 0;
64
65 rc = vdd_class->set_vdd(vdd_class, level);
66 if (!rc)
67 vdd_class->cur_level = level;
68
69 return rc;
70}
71
72/* Vote for a voltage level. */
73int vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
74{
75 unsigned long flags;
76 int rc;
77
78 spin_lock_irqsave(&vdd_class->lock, flags);
79 vdd_class->level_votes[level]++;
80 rc = update_vdd(vdd_class);
81 if (rc)
82 vdd_class->level_votes[level]--;
83 spin_unlock_irqrestore(&vdd_class->lock, flags);
84
85 return rc;
86}
87
88/* Remove vote for a voltage level. */
89int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
90{
91 unsigned long flags;
92 int rc = 0;
93
94 spin_lock_irqsave(&vdd_class->lock, flags);
95 if (WARN(!vdd_class->level_votes[level],
96 "Reference counts are incorrect for %s level %d\n",
97 vdd_class->class_name, level))
98 goto out;
99 vdd_class->level_votes[level]--;
100 rc = update_vdd(vdd_class);
101 if (rc)
102 vdd_class->level_votes[level]++;
103out:
104 spin_unlock_irqrestore(&vdd_class->lock, flags);
105 return rc;
106}
107
108/* Vote for a voltage level corresponding to a clock's rate. */
109static int vote_rate_vdd(struct clk *clk, unsigned long rate)
110{
111 int level;
112
113 if (!clk->vdd_class)
114 return 0;
115
116 level = find_vdd_level(clk, rate);
117 if (level < 0)
118 return level;
119
120 return vote_vdd_level(clk->vdd_class, level);
121}
122
123/* Remove vote for a voltage level corresponding to a clock's rate. */
124static void unvote_rate_vdd(struct clk *clk, unsigned long rate)
125{
126 int level;
127
128 if (!clk->vdd_class)
129 return;
130
131 level = find_vdd_level(clk, rate);
132 if (level < 0)
133 return;
134
135 unvote_vdd_level(clk->vdd_class, level);
136}
137
Patrick Dalyc0900c22012-10-01 11:55:27 -0700138/* Returns true if the rate is valid without voting for it */
139static bool is_rate_valid(struct clk *clk, unsigned long rate)
140{
141 int level;
142
143 if (!clk->vdd_class)
144 return true;
145
146 level = find_vdd_level(clk, rate);
147 return level >= 0;
148}
149
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800150int clk_prepare(struct clk *clk)
151{
152 int ret = 0;
153 struct clk *parent;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700154
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800155 if (!clk)
156 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700157 if (IS_ERR(clk))
158 return -EINVAL;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800159
160 mutex_lock(&clk->prepare_lock);
161 if (clk->prepare_count == 0) {
162 parent = clk_get_parent(clk);
163
164 ret = clk_prepare(parent);
165 if (ret)
166 goto out;
167 ret = clk_prepare(clk->depends);
168 if (ret)
169 goto err_prepare_depends;
170
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800171 ret = vote_rate_vdd(clk, clk->rate);
172 if (ret)
173 goto err_vote_vdd;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800174 if (clk->ops->prepare)
175 ret = clk->ops->prepare(clk);
176 if (ret)
177 goto err_prepare_clock;
178 }
179 clk->prepare_count++;
180out:
181 mutex_unlock(&clk->prepare_lock);
182 return ret;
183err_prepare_clock:
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800184 unvote_rate_vdd(clk, clk->rate);
185err_vote_vdd:
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800186 clk_unprepare(clk->depends);
187err_prepare_depends:
188 clk_unprepare(parent);
189 goto out;
190}
191EXPORT_SYMBOL(clk_prepare);
192
Brian Swetland600f7cf2008-09-09 11:04:14 -0700193/*
Brian Swetland600f7cf2008-09-09 11:04:14 -0700194 * Standard clock functions defined in include/linux/clk.h
195 */
Brian Swetland600f7cf2008-09-09 11:04:14 -0700196int clk_enable(struct clk *clk)
197{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 int ret = 0;
Matt Wagantall7205eea2011-11-04 17:31:29 -0700199 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 struct clk *parent;
201
202 if (!clk)
203 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700204 if (IS_ERR(clk))
205 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206
207 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800208 if (WARN(!clk->warned && !clk->prepare_count,
209 "%s: Don't call enable on unprepared clocks\n",
210 clk->dbg_name))
211 clk->warned = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 if (clk->count == 0) {
213 parent = clk_get_parent(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700214
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 ret = clk_enable(parent);
216 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700217 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700218 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700219 if (ret)
220 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221
Stephen Boyd5bc44d52012-03-29 11:00:57 -0700222 trace_clock_enable(clk->dbg_name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 if (clk->ops->enable)
224 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700225 if (ret)
226 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700228 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 spin_unlock_irqrestore(&clk->lock, flags);
230
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700231 return 0;
232
233err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700234 clk_disable(clk->depends);
235err_enable_depends:
236 clk_disable(parent);
237err_enable_parent:
238 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700240}
241EXPORT_SYMBOL(clk_enable);
242
243void clk_disable(struct clk *clk)
244{
245 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700247 if (IS_ERR_OR_NULL(clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 return;
249
250 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800251 if (WARN(!clk->warned && !clk->prepare_count,
252 "%s: Never called prepare or calling disable "
253 "after unprepare\n",
254 clk->dbg_name))
255 clk->warned = true;
Stephen Boydd906b522011-07-26 10:51:41 -0700256 if (WARN(clk->count == 0, "%s is unbalanced", clk->dbg_name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 goto out;
258 if (clk->count == 1) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700259 struct clk *parent = clk_get_parent(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700260
Stephen Boyd5bc44d52012-03-29 11:00:57 -0700261 trace_clock_disable(clk->dbg_name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 if (clk->ops->disable)
263 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700264 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 clk_disable(parent);
266 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700267 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268out:
269 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700270}
271EXPORT_SYMBOL(clk_disable);
272
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800273void clk_unprepare(struct clk *clk)
274{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700275 if (IS_ERR_OR_NULL(clk))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800276 return;
277
278 mutex_lock(&clk->prepare_lock);
279 if (!clk->prepare_count) {
280 if (WARN(!clk->warned, "%s is unbalanced (prepare)",
281 clk->dbg_name))
282 clk->warned = true;
283 goto out;
284 }
285 if (clk->prepare_count == 1) {
286 struct clk *parent = clk_get_parent(clk);
287
288 if (WARN(!clk->warned && clk->count,
289 "%s: Don't call unprepare when the clock is enabled\n",
290 clk->dbg_name))
291 clk->warned = true;
292
293 if (clk->ops->unprepare)
294 clk->ops->unprepare(clk);
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800295 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800296 clk_unprepare(clk->depends);
297 clk_unprepare(parent);
298 }
299 clk->prepare_count--;
300out:
301 mutex_unlock(&clk->prepare_lock);
302}
303EXPORT_SYMBOL(clk_unprepare);
304
Daniel Walker5e96da52010-05-12 13:43:28 -0700305int clk_reset(struct clk *clk, enum clk_reset_action action)
306{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700307 if (IS_ERR_OR_NULL(clk))
308 return -EINVAL;
309
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 if (!clk->ops->reset)
311 return -ENOSYS;
312
313 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700314}
315EXPORT_SYMBOL(clk_reset);
316
Brian Swetland600f7cf2008-09-09 11:04:14 -0700317unsigned long clk_get_rate(struct clk *clk)
318{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700319 if (IS_ERR_OR_NULL(clk))
320 return 0;
321
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800323 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324
325 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700326}
327EXPORT_SYMBOL(clk_get_rate);
328
Matt Wagantall77952c42011-11-08 18:45:48 -0800329int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700330{
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800331 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700332 int rc = 0;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700333
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700334 if (IS_ERR_OR_NULL(clk))
335 return -EINVAL;
336
Matt Wagantall77952c42011-11-08 18:45:48 -0800337 if (!clk->ops->set_rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 return -ENOSYS;
Daniel Walker3a790bb2010-12-13 14:35:10 -0800339
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800340 mutex_lock(&clk->prepare_lock);
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700341
342 /* Return early if the rate isn't going to change */
343 if (clk->rate == rate)
344 goto out;
345
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800346 trace_clock_set_rate(clk->dbg_name, rate, raw_smp_processor_id());
347 if (clk->prepare_count) {
Matt Wagantall7205eea2011-11-04 17:31:29 -0700348 start_rate = clk->rate;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700349 /* Enforce vdd requirements for target frequency. */
350 rc = vote_rate_vdd(clk, rate);
351 if (rc)
Patrick Dalyc0900c22012-10-01 11:55:27 -0700352 goto out;
Matt Wagantall77952c42011-11-08 18:45:48 -0800353 rc = clk->ops->set_rate(clk, rate);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700354 if (rc)
355 goto err_set_rate;
356 /* Release vdd requirements for starting frequency. */
357 unvote_rate_vdd(clk, start_rate);
Patrick Dalyc0900c22012-10-01 11:55:27 -0700358 } else if (is_rate_valid(clk, rate)) {
Matt Wagantall77952c42011-11-08 18:45:48 -0800359 rc = clk->ops->set_rate(clk, rate);
Patrick Dalyc0900c22012-10-01 11:55:27 -0700360 } else {
361 rc = -EINVAL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700362 }
Matt Wagantall7205eea2011-11-04 17:31:29 -0700363
364 if (!rc)
365 clk->rate = rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700366out:
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800367 mutex_unlock(&clk->prepare_lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700368 return rc;
369
370err_set_rate:
371 unvote_rate_vdd(clk, rate);
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800372 goto out;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700373}
Matt Wagantall77952c42011-11-08 18:45:48 -0800374EXPORT_SYMBOL(clk_set_rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700375
Daniel Walker5e96da52010-05-12 13:43:28 -0700376long clk_round_rate(struct clk *clk, unsigned long rate)
377{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700378 if (IS_ERR_OR_NULL(clk))
379 return -EINVAL;
380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 if (!clk->ops->round_rate)
382 return -ENOSYS;
383
384 return clk->ops->round_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700385}
386EXPORT_SYMBOL(clk_round_rate);
387
Daniel Walker5e96da52010-05-12 13:43:28 -0700388int clk_set_max_rate(struct clk *clk, unsigned long rate)
389{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700390 if (IS_ERR_OR_NULL(clk))
391 return -EINVAL;
392
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 if (!clk->ops->set_max_rate)
394 return -ENOSYS;
395
396 return clk->ops->set_max_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700397}
398EXPORT_SYMBOL(clk_set_max_rate);
399
Brian Swetland600f7cf2008-09-09 11:04:14 -0700400int clk_set_parent(struct clk *clk, struct clk *parent)
401{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 if (!clk->ops->set_parent)
403 return 0;
404
405 return clk->ops->set_parent(clk, parent);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700406}
407EXPORT_SYMBOL(clk_set_parent);
408
409struct clk *clk_get_parent(struct clk *clk)
410{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700411 if (IS_ERR_OR_NULL(clk))
412 return NULL;
413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414 if (!clk->ops->get_parent)
415 return NULL;
416
417 return clk->ops->get_parent(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700418}
419EXPORT_SYMBOL(clk_get_parent);
420
421int clk_set_flags(struct clk *clk, unsigned long flags)
422{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700423 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700424 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 if (!clk->ops->set_flags)
426 return -ENOSYS;
427
428 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700429}
430EXPORT_SYMBOL(clk_set_flags);
431
Stephen Boydbb600ae2011-08-02 20:11:40 -0700432static struct clock_init_data __initdata *clk_init_data;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700433
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700434static enum handoff __init __handoff_clk(struct clk *clk)
435{
436 enum handoff ret;
437 struct handoff_clk *h;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700438 unsigned long rate;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700439 int err = 0;
440
441 /*
442 * Tree roots don't have parents, but need to be handed off. So,
443 * terminate recursion by returning "enabled". Also return "enabled"
444 * for clocks with non-zero enable counts since they must have already
445 * been handed off.
446 */
447 if (clk == NULL || clk->count)
448 return HANDOFF_ENABLED_CLK;
449
450 /* Clocks without handoff functions are assumed to be disabled. */
451 if (!clk->ops->handoff || (clk->flags & CLKFLAG_SKIP_HANDOFF))
452 return HANDOFF_DISABLED_CLK;
453
454 /*
455 * Handoff functions for children must be called before their parents'
456 * so that the correct parent is returned by the clk_get_parent() below.
457 */
458 ret = clk->ops->handoff(clk);
459 if (ret == HANDOFF_ENABLED_CLK) {
460 ret = __handoff_clk(clk_get_parent(clk));
461 if (ret == HANDOFF_ENABLED_CLK) {
462 h = kmalloc(sizeof(*h), GFP_KERNEL);
463 if (!h) {
464 err = -ENOMEM;
465 goto out;
466 }
467 err = clk_prepare_enable(clk);
468 if (err)
469 goto out;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700470 rate = clk_get_rate(clk);
471 if (rate)
472 pr_debug("%s rate=%lu\n", clk->dbg_name, rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700473 h->clk = clk;
474 list_add_tail(&h->list, &handoff_list);
475 }
476 }
477out:
478 if (err) {
479 pr_err("%s handoff failed (%d)\n", clk->dbg_name, err);
480 kfree(h);
481 ret = HANDOFF_DISABLED_CLK;
482 }
483 return ret;
484}
485
Stephen Boydbb600ae2011-08-02 20:11:40 -0700486void __init msm_clock_init(struct clock_init_data *data)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700487{
488 unsigned n;
Stephen Boyd94625ef2011-07-12 17:06:01 -0700489 struct clk_lookup *clock_tbl;
490 size_t num_clocks;
Matt Wagantallb37fea42012-04-04 16:47:23 -0700491 struct clk *clk;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700492
493 clk_init_data = data;
Matt Wagantallb64888f2012-04-02 21:35:07 -0700494 if (clk_init_data->pre_init)
495 clk_init_data->pre_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700496
Stephen Boyd94625ef2011-07-12 17:06:01 -0700497 clock_tbl = data->table;
498 num_clocks = data->size;
499
Stephen Boydbd323442011-02-23 09:37:42 -0800500 for (n = 0; n < num_clocks; n++) {
Matt Wagantallb37fea42012-04-04 16:47:23 -0700501 struct clk *parent;
502 clk = clock_tbl[n].clk;
503 parent = clk_get_parent(clk);
Matt Wagantalle1482bf2012-04-04 16:23:45 -0700504 if (parent && list_empty(&clk->siblings))
505 list_add(&clk->siblings, &parent->children);
Matt Wagantallb37fea42012-04-04 16:47:23 -0700506 }
507
508 /*
509 * Detect and preserve initial clock state until clock_late_init() or
510 * a driver explicitly changes it, whichever is first.
511 */
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700512 for (n = 0; n < num_clocks; n++)
513 __handoff_clk(clock_tbl[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 clkdev_add_table(clock_tbl, num_clocks);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700516
517 if (clk_init_data->post_init)
518 clk_init_data->post_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700519}
520
Brian Swetland600f7cf2008-09-09 11:04:14 -0700521static int __init clock_late_init(void)
522{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700523 struct handoff_clk *h, *h_temp;
Matt Wagantall647d1c12012-05-16 14:32:14 -0700524 int n, ret = 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700525
Stephen Boydbb600ae2011-08-02 20:11:40 -0700526 clock_debug_init(clk_init_data);
Matt Wagantall647d1c12012-05-16 14:32:14 -0700527 for (n = 0; n < clk_init_data->size; n++)
528 clock_debug_add(clk_init_data->table[n].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
Matt Wagantall647d1c12012-05-16 14:32:14 -0700530 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Matt Wagantall158f73b2012-05-16 11:29:35 -0700531 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
532 clk_disable_unprepare(h->clk);
533 list_del(&h->list);
534 kfree(h);
535 }
536
Stephen Boydbb600ae2011-08-02 20:11:40 -0700537 if (clk_init_data->late_init)
538 ret = clk_init_data->late_init();
539 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700540}
Brian Swetland600f7cf2008-09-09 11:04:14 -0700541late_initcall(clock_late_init);