blob: 1ea18f4e563eca2d8ce6d917575433e5d899b5b3 [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>
Matt Wagantall33d01f52012-02-23 23:27:44 -080026#include <mach/clk-provider.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070027#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
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800138int clk_prepare(struct clk *clk)
139{
140 int ret = 0;
141 struct clk *parent;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700142
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800143 if (!clk)
144 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700145 if (IS_ERR(clk))
146 return -EINVAL;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800147
148 mutex_lock(&clk->prepare_lock);
149 if (clk->prepare_count == 0) {
150 parent = clk_get_parent(clk);
151
152 ret = clk_prepare(parent);
153 if (ret)
154 goto out;
155 ret = clk_prepare(clk->depends);
156 if (ret)
157 goto err_prepare_depends;
158
Stephen Boydd86d1f22012-01-24 17:36:34 -0800159 ret = vote_rate_vdd(clk, clk->rate);
160 if (ret)
161 goto err_vote_vdd;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800162 if (clk->ops->prepare)
163 ret = clk->ops->prepare(clk);
164 if (ret)
165 goto err_prepare_clock;
166 }
167 clk->prepare_count++;
168out:
169 mutex_unlock(&clk->prepare_lock);
170 return ret;
171err_prepare_clock:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800172 unvote_rate_vdd(clk, clk->rate);
173err_vote_vdd:
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800174 clk_unprepare(clk->depends);
175err_prepare_depends:
176 clk_unprepare(parent);
177 goto out;
178}
179EXPORT_SYMBOL(clk_prepare);
180
Brian Swetland600f7cf2008-09-09 11:04:14 -0700181/*
Brian Swetland600f7cf2008-09-09 11:04:14 -0700182 * Standard clock functions defined in include/linux/clk.h
183 */
Brian Swetland600f7cf2008-09-09 11:04:14 -0700184int clk_enable(struct clk *clk)
185{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 int ret = 0;
Matt Wagantall7205eea2011-11-04 17:31:29 -0700187 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 struct clk *parent;
Stephen Boyd64dce902012-08-09 12:59:40 -0700189 const char *name = clk ? clk->dbg_name : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 if (!clk)
192 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700193 if (IS_ERR(clk))
194 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195
196 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700197 WARN(!clk->prepare_count,
198 "%s: Don't call enable on unprepared clocks\n", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 if (clk->count == 0) {
200 parent = clk_get_parent(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700201
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 ret = clk_enable(parent);
203 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700204 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700205 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700206 if (ret)
207 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208
Stephen Boyd64dce902012-08-09 12:59:40 -0700209 trace_clock_enable(name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 if (clk->ops->enable)
211 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700212 if (ret)
213 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700215 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 spin_unlock_irqrestore(&clk->lock, flags);
217
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700218 return 0;
219
220err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700221 clk_disable(clk->depends);
222err_enable_depends:
223 clk_disable(parent);
224err_enable_parent:
225 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700227}
228EXPORT_SYMBOL(clk_enable);
229
230void clk_disable(struct clk *clk)
231{
Stephen Boyd64dce902012-08-09 12:59:40 -0700232 const char *name = clk ? clk->dbg_name : NULL;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700233 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700235 if (IS_ERR_OR_NULL(clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 return;
237
238 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700239 WARN(!clk->prepare_count,
240 "%s: Never called prepare or calling disable after unprepare\n",
241 name);
242 if (WARN(clk->count == 0, "%s is unbalanced", name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243 goto out;
244 if (clk->count == 1) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700245 struct clk *parent = clk_get_parent(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700246
Stephen Boyd64dce902012-08-09 12:59:40 -0700247 trace_clock_disable(name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 if (clk->ops->disable)
249 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700250 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 clk_disable(parent);
252 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700253 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254out:
255 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700256}
257EXPORT_SYMBOL(clk_disable);
258
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800259void clk_unprepare(struct clk *clk)
260{
Stephen Boyd64dce902012-08-09 12:59:40 -0700261 const char *name = clk ? clk->dbg_name : NULL;
262
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700263 if (IS_ERR_OR_NULL(clk))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800264 return;
265
266 mutex_lock(&clk->prepare_lock);
Stephen Boyd64dce902012-08-09 12:59:40 -0700267 if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800268 goto out;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800269 if (clk->prepare_count == 1) {
270 struct clk *parent = clk_get_parent(clk);
271
Stephen Boyd64dce902012-08-09 12:59:40 -0700272 WARN(clk->count,
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800273 "%s: Don't call unprepare when the clock is enabled\n",
Stephen Boyd64dce902012-08-09 12:59:40 -0700274 name);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800275
276 if (clk->ops->unprepare)
277 clk->ops->unprepare(clk);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800278 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800279 clk_unprepare(clk->depends);
280 clk_unprepare(parent);
281 }
282 clk->prepare_count--;
283out:
284 mutex_unlock(&clk->prepare_lock);
285}
286EXPORT_SYMBOL(clk_unprepare);
287
Daniel Walker5e96da52010-05-12 13:43:28 -0700288int clk_reset(struct clk *clk, enum clk_reset_action action)
289{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700290 if (IS_ERR_OR_NULL(clk))
291 return -EINVAL;
292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 if (!clk->ops->reset)
294 return -ENOSYS;
295
296 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700297}
298EXPORT_SYMBOL(clk_reset);
299
Brian Swetland600f7cf2008-09-09 11:04:14 -0700300unsigned long clk_get_rate(struct clk *clk)
301{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700302 if (IS_ERR_OR_NULL(clk))
303 return 0;
304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800306 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307
308 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700309}
310EXPORT_SYMBOL(clk_get_rate);
311
Matt Wagantall77952c42011-11-08 18:45:48 -0800312int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700313{
Stephen Boydd86d1f22012-01-24 17:36:34 -0800314 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700315 int rc = 0;
Stephen Boyd64dce902012-08-09 12:59:40 -0700316 const char *name = clk ? clk->dbg_name : NULL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700317
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700318 if (IS_ERR_OR_NULL(clk))
319 return -EINVAL;
320
Matt Wagantall77952c42011-11-08 18:45:48 -0800321 if (!clk->ops->set_rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 return -ENOSYS;
Daniel Walker3a790bb2010-12-13 14:35:10 -0800323
Stephen Boydd86d1f22012-01-24 17:36:34 -0800324 mutex_lock(&clk->prepare_lock);
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700325
326 /* Return early if the rate isn't going to change */
327 if (clk->rate == rate)
328 goto out;
329
Stephen Boyd64dce902012-08-09 12:59:40 -0700330 trace_clock_set_rate(name, rate, raw_smp_processor_id());
Stephen Boydd86d1f22012-01-24 17:36:34 -0800331 if (clk->prepare_count) {
Matt Wagantall7205eea2011-11-04 17:31:29 -0700332 start_rate = clk->rate;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700333 /* Enforce vdd requirements for target frequency. */
334 rc = vote_rate_vdd(clk, rate);
335 if (rc)
336 goto err_vote_vdd;
Matt Wagantall77952c42011-11-08 18:45:48 -0800337 rc = clk->ops->set_rate(clk, rate);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700338 if (rc)
339 goto err_set_rate;
340 /* Release vdd requirements for starting frequency. */
341 unvote_rate_vdd(clk, start_rate);
342 } else {
Matt Wagantall77952c42011-11-08 18:45:48 -0800343 rc = clk->ops->set_rate(clk, rate);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700344 }
Matt Wagantall7205eea2011-11-04 17:31:29 -0700345
346 if (!rc)
347 clk->rate = rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700348out:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800349 mutex_unlock(&clk->prepare_lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700350 return rc;
351
352err_set_rate:
353 unvote_rate_vdd(clk, rate);
354err_vote_vdd:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800355 goto out;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700356}
Matt Wagantall77952c42011-11-08 18:45:48 -0800357EXPORT_SYMBOL(clk_set_rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700358
Daniel Walker5e96da52010-05-12 13:43:28 -0700359long clk_round_rate(struct clk *clk, unsigned long rate)
360{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700361 if (IS_ERR_OR_NULL(clk))
362 return -EINVAL;
363
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 if (!clk->ops->round_rate)
365 return -ENOSYS;
366
367 return clk->ops->round_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700368}
369EXPORT_SYMBOL(clk_round_rate);
370
Daniel Walker5e96da52010-05-12 13:43:28 -0700371int clk_set_max_rate(struct clk *clk, unsigned long rate)
372{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700373 if (IS_ERR_OR_NULL(clk))
374 return -EINVAL;
375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 if (!clk->ops->set_max_rate)
377 return -ENOSYS;
378
379 return clk->ops->set_max_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700380}
381EXPORT_SYMBOL(clk_set_max_rate);
382
Brian Swetland600f7cf2008-09-09 11:04:14 -0700383int clk_set_parent(struct clk *clk, struct clk *parent)
384{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 if (!clk->ops->set_parent)
386 return 0;
387
388 return clk->ops->set_parent(clk, parent);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700389}
390EXPORT_SYMBOL(clk_set_parent);
391
392struct clk *clk_get_parent(struct clk *clk)
393{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700394 if (IS_ERR_OR_NULL(clk))
395 return NULL;
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 if (!clk->ops->get_parent)
398 return NULL;
399
400 return clk->ops->get_parent(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700401}
402EXPORT_SYMBOL(clk_get_parent);
403
404int clk_set_flags(struct clk *clk, unsigned long flags)
405{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700406 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700407 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 if (!clk->ops->set_flags)
409 return -ENOSYS;
410
411 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700412}
413EXPORT_SYMBOL(clk_set_flags);
414
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800415static struct clock_init_data *clk_init_data;
416
417/**
418 * msm_clock_register() - Register additional clock tables
419 * @table: Table of clocks
420 * @size: Size of @table
421 *
422 * Upon return, clock APIs may be used to control clocks registered using this
423 * function. This API may only be used after msm_clock_init() has completed.
424 * Unlike msm_clock_init(), this function may be called multiple times with
425 * different clock lists and used after the kernel has finished booting.
426 */
427int msm_clock_register(struct clk_lookup *table, size_t size)
428{
429 if (!clk_init_data)
430 return -ENODEV;
431
432 if (!table)
433 return -EINVAL;
434
435 clkdev_add_table(table, size);
436 clock_debug_register(table, size);
437
438 return 0;
439}
440EXPORT_SYMBOL(msm_clock_register);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700441
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700442static enum handoff __init __handoff_clk(struct clk *clk)
443{
444 enum handoff ret;
445 struct handoff_clk *h;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700446 unsigned long rate;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700447 int err = 0;
448
449 /*
450 * Tree roots don't have parents, but need to be handed off. So,
451 * terminate recursion by returning "enabled". Also return "enabled"
452 * for clocks with non-zero enable counts since they must have already
453 * been handed off.
454 */
455 if (clk == NULL || clk->count)
456 return HANDOFF_ENABLED_CLK;
457
458 /* Clocks without handoff functions are assumed to be disabled. */
459 if (!clk->ops->handoff || (clk->flags & CLKFLAG_SKIP_HANDOFF))
460 return HANDOFF_DISABLED_CLK;
461
462 /*
463 * Handoff functions for children must be called before their parents'
464 * so that the correct parent is returned by the clk_get_parent() below.
465 */
466 ret = clk->ops->handoff(clk);
467 if (ret == HANDOFF_ENABLED_CLK) {
468 ret = __handoff_clk(clk_get_parent(clk));
469 if (ret == HANDOFF_ENABLED_CLK) {
470 h = kmalloc(sizeof(*h), GFP_KERNEL);
471 if (!h) {
472 err = -ENOMEM;
473 goto out;
474 }
475 err = clk_prepare_enable(clk);
476 if (err)
477 goto out;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700478 rate = clk_get_rate(clk);
479 if (rate)
480 pr_debug("%s rate=%lu\n", clk->dbg_name, rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700481 h->clk = clk;
482 list_add_tail(&h->list, &handoff_list);
483 }
484 }
485out:
486 if (err) {
487 pr_err("%s handoff failed (%d)\n", clk->dbg_name, err);
488 kfree(h);
489 ret = HANDOFF_DISABLED_CLK;
490 }
491 return ret;
492}
493
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800494/**
495 * msm_clock_init() - Register and initialize a clock driver
496 * @data: Driver-specific clock initialization data
497 *
498 * Upon return from this call, clock APIs may be used to control
499 * clocks registered with this API.
500 */
501int __init msm_clock_init(struct clock_init_data *data)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700502{
503 unsigned n;
Stephen Boyd94625ef2011-07-12 17:06:01 -0700504 struct clk_lookup *clock_tbl;
505 size_t num_clocks;
Matt Wagantallb37fea42012-04-04 16:47:23 -0700506 struct clk *clk;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700507
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800508 if (!data)
509 return -EINVAL;
510
Stephen Boydbb600ae2011-08-02 20:11:40 -0700511 clk_init_data = data;
Matt Wagantallb64888f2012-04-02 21:35:07 -0700512 if (clk_init_data->pre_init)
513 clk_init_data->pre_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700514
Stephen Boyd94625ef2011-07-12 17:06:01 -0700515 clock_tbl = data->table;
516 num_clocks = data->size;
517
Stephen Boydbd323442011-02-23 09:37:42 -0800518 for (n = 0; n < num_clocks; n++) {
Matt Wagantallb37fea42012-04-04 16:47:23 -0700519 struct clk *parent;
520 clk = clock_tbl[n].clk;
521 parent = clk_get_parent(clk);
Matt Wagantalle1482bf2012-04-04 16:23:45 -0700522 if (parent && list_empty(&clk->siblings))
523 list_add(&clk->siblings, &parent->children);
Matt Wagantallb37fea42012-04-04 16:47:23 -0700524 }
525
526 /*
527 * Detect and preserve initial clock state until clock_late_init() or
528 * a driver explicitly changes it, whichever is first.
529 */
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700530 for (n = 0; n < num_clocks; n++)
531 __handoff_clk(clock_tbl[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 clkdev_add_table(clock_tbl, num_clocks);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700534
535 if (clk_init_data->post_init)
536 clk_init_data->post_init();
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800537
538 clock_debug_init();
539 clock_debug_register(clock_tbl, num_clocks);
540
541 return 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700542}
543
Brian Swetland600f7cf2008-09-09 11:04:14 -0700544static int __init clock_late_init(void)
545{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700546 struct handoff_clk *h, *h_temp;
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800547 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
Matt Wagantall647d1c12012-05-16 14:32:14 -0700549 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Matt Wagantall158f73b2012-05-16 11:29:35 -0700550 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
551 clk_disable_unprepare(h->clk);
552 list_del(&h->list);
553 kfree(h);
554 }
555
Stephen Boydbb600ae2011-08-02 20:11:40 -0700556 if (clk_init_data->late_init)
557 ret = clk_init_data->late_init();
558 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700559}
Brian Swetland600f7cf2008-09-09 11:04:14 -0700560late_initcall(clock_late_init);