blob: 0dc6ff3937814dc06c49a95214d5318b4c4985d5 [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 Wagantalld55b90f2012-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
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;
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700201 const char *name = clk ? clk->dbg_name : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202
203 if (!clk)
204 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700205 if (IS_ERR(clk))
206 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207
208 spin_lock_irqsave(&clk->lock, flags);
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700209 WARN(!clk->prepare_count,
210 "%s: Don't call enable on unprepared clocks\n", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 if (clk->count == 0) {
212 parent = clk_get_parent(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700213
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 ret = clk_enable(parent);
215 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700216 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700217 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700218 if (ret)
219 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700221 trace_clock_enable(name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 if (clk->ops->enable)
223 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700224 if (ret)
225 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700227 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 spin_unlock_irqrestore(&clk->lock, flags);
229
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700230 return 0;
231
232err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700233 clk_disable(clk->depends);
234err_enable_depends:
235 clk_disable(parent);
236err_enable_parent:
237 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700239}
240EXPORT_SYMBOL(clk_enable);
241
242void clk_disable(struct clk *clk)
243{
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700244 const char *name = clk ? clk->dbg_name : NULL;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700245 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 Boydc3b65ea2012-08-09 12:59:40 -0700251 WARN(!clk->prepare_count,
252 "%s: Never called prepare or calling disable after unprepare\n",
253 name);
254 if (WARN(clk->count == 0, "%s is unbalanced", name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 goto out;
256 if (clk->count == 1) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700257 struct clk *parent = clk_get_parent(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700258
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700259 trace_clock_disable(name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 if (clk->ops->disable)
261 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700262 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 clk_disable(parent);
264 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700265 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266out:
267 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700268}
269EXPORT_SYMBOL(clk_disable);
270
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800271void clk_unprepare(struct clk *clk)
272{
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700273 const char *name = clk ? clk->dbg_name : NULL;
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);
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700279 if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800280 goto out;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800281 if (clk->prepare_count == 1) {
282 struct clk *parent = clk_get_parent(clk);
283
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700284 WARN(clk->count,
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800285 "%s: Don't call unprepare when the clock is enabled\n",
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700286 name);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800287
288 if (clk->ops->unprepare)
289 clk->ops->unprepare(clk);
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800290 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800291 clk_unprepare(clk->depends);
292 clk_unprepare(parent);
293 }
294 clk->prepare_count--;
295out:
296 mutex_unlock(&clk->prepare_lock);
297}
298EXPORT_SYMBOL(clk_unprepare);
299
Daniel Walker5e96da52010-05-12 13:43:28 -0700300int clk_reset(struct clk *clk, enum clk_reset_action action)
301{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700302 if (IS_ERR_OR_NULL(clk))
303 return -EINVAL;
304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 if (!clk->ops->reset)
306 return -ENOSYS;
307
308 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700309}
310EXPORT_SYMBOL(clk_reset);
311
Brian Swetland600f7cf2008-09-09 11:04:14 -0700312unsigned long clk_get_rate(struct clk *clk)
313{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700314 if (IS_ERR_OR_NULL(clk))
315 return 0;
316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800318 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319
320 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700321}
322EXPORT_SYMBOL(clk_get_rate);
323
Matt Wagantall77952c42011-11-08 18:45:48 -0800324int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700325{
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800326 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700327 int rc = 0;
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700328 const char *name = clk ? clk->dbg_name : NULL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700329
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700330 if (IS_ERR_OR_NULL(clk))
331 return -EINVAL;
332
Matt Wagantall77952c42011-11-08 18:45:48 -0800333 if (!clk->ops->set_rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 return -ENOSYS;
Daniel Walker3a790bb2010-12-13 14:35:10 -0800335
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800336 mutex_lock(&clk->prepare_lock);
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700337
338 /* Return early if the rate isn't going to change */
339 if (clk->rate == rate)
340 goto out;
341
Stephen Boydc3b65ea2012-08-09 12:59:40 -0700342 trace_clock_set_rate(name, rate, raw_smp_processor_id());
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800343 if (clk->prepare_count) {
Matt Wagantall7205eea2011-11-04 17:31:29 -0700344 start_rate = clk->rate;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700345 /* Enforce vdd requirements for target frequency. */
346 rc = vote_rate_vdd(clk, rate);
347 if (rc)
Patrick Dalyc0900c22012-10-01 11:55:27 -0700348 goto out;
Matt Wagantall77952c42011-11-08 18:45:48 -0800349 rc = clk->ops->set_rate(clk, rate);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700350 if (rc)
351 goto err_set_rate;
352 /* Release vdd requirements for starting frequency. */
353 unvote_rate_vdd(clk, start_rate);
Patrick Dalyc0900c22012-10-01 11:55:27 -0700354 } else if (is_rate_valid(clk, rate)) {
Matt Wagantall77952c42011-11-08 18:45:48 -0800355 rc = clk->ops->set_rate(clk, rate);
Patrick Dalyc0900c22012-10-01 11:55:27 -0700356 } else {
357 rc = -EINVAL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700358 }
Matt Wagantall7205eea2011-11-04 17:31:29 -0700359
360 if (!rc)
361 clk->rate = rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700362out:
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800363 mutex_unlock(&clk->prepare_lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700364 return rc;
365
366err_set_rate:
367 unvote_rate_vdd(clk, rate);
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800368 goto out;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700369}
Matt Wagantall77952c42011-11-08 18:45:48 -0800370EXPORT_SYMBOL(clk_set_rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700371
Daniel Walker5e96da52010-05-12 13:43:28 -0700372long clk_round_rate(struct clk *clk, unsigned long rate)
373{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700374 if (IS_ERR_OR_NULL(clk))
375 return -EINVAL;
376
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 if (!clk->ops->round_rate)
378 return -ENOSYS;
379
380 return clk->ops->round_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700381}
382EXPORT_SYMBOL(clk_round_rate);
383
Daniel Walker5e96da52010-05-12 13:43:28 -0700384int clk_set_max_rate(struct clk *clk, unsigned long rate)
385{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700386 if (IS_ERR_OR_NULL(clk))
387 return -EINVAL;
388
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 if (!clk->ops->set_max_rate)
390 return -ENOSYS;
391
392 return clk->ops->set_max_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700393}
394EXPORT_SYMBOL(clk_set_max_rate);
395
Brian Swetland600f7cf2008-09-09 11:04:14 -0700396int clk_set_parent(struct clk *clk, struct clk *parent)
397{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398 if (!clk->ops->set_parent)
399 return 0;
400
401 return clk->ops->set_parent(clk, parent);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700402}
403EXPORT_SYMBOL(clk_set_parent);
404
405struct clk *clk_get_parent(struct clk *clk)
406{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700407 if (IS_ERR_OR_NULL(clk))
408 return NULL;
409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410 if (!clk->ops->get_parent)
411 return NULL;
412
413 return clk->ops->get_parent(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700414}
415EXPORT_SYMBOL(clk_get_parent);
416
417int clk_set_flags(struct clk *clk, unsigned long flags)
418{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700419 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700420 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 if (!clk->ops->set_flags)
422 return -ENOSYS;
423
424 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700425}
426EXPORT_SYMBOL(clk_set_flags);
427
Matt Wagantallc7764192012-02-27 15:54:43 -0800428static struct clock_init_data *clk_init_data;
429
430/**
431 * msm_clock_register() - Register additional clock tables
432 * @table: Table of clocks
433 * @size: Size of @table
434 *
435 * Upon return, clock APIs may be used to control clocks registered using this
436 * function. This API may only be used after msm_clock_init() has completed.
437 * Unlike msm_clock_init(), this function may be called multiple times with
438 * different clock lists and used after the kernel has finished booting.
439 */
440int msm_clock_register(struct clk_lookup *table, size_t size)
441{
442 if (!clk_init_data)
443 return -ENODEV;
444
445 if (!table)
446 return -EINVAL;
447
448 clkdev_add_table(table, size);
449 clock_debug_register(table, size);
450
451 return 0;
452}
453EXPORT_SYMBOL(msm_clock_register);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700454
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700455static enum handoff __init __handoff_clk(struct clk *clk)
456{
457 enum handoff ret;
458 struct handoff_clk *h;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700459 unsigned long rate;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700460 int err = 0;
461
462 /*
463 * Tree roots don't have parents, but need to be handed off. So,
464 * terminate recursion by returning "enabled". Also return "enabled"
465 * for clocks with non-zero enable counts since they must have already
466 * been handed off.
467 */
468 if (clk == NULL || clk->count)
469 return HANDOFF_ENABLED_CLK;
470
471 /* Clocks without handoff functions are assumed to be disabled. */
472 if (!clk->ops->handoff || (clk->flags & CLKFLAG_SKIP_HANDOFF))
473 return HANDOFF_DISABLED_CLK;
474
475 /*
476 * Handoff functions for children must be called before their parents'
477 * so that the correct parent is returned by the clk_get_parent() below.
478 */
479 ret = clk->ops->handoff(clk);
480 if (ret == HANDOFF_ENABLED_CLK) {
481 ret = __handoff_clk(clk_get_parent(clk));
482 if (ret == HANDOFF_ENABLED_CLK) {
483 h = kmalloc(sizeof(*h), GFP_KERNEL);
484 if (!h) {
485 err = -ENOMEM;
486 goto out;
487 }
488 err = clk_prepare_enable(clk);
489 if (err)
490 goto out;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700491 rate = clk_get_rate(clk);
492 if (rate)
493 pr_debug("%s rate=%lu\n", clk->dbg_name, rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700494 h->clk = clk;
495 list_add_tail(&h->list, &handoff_list);
496 }
497 }
498out:
499 if (err) {
500 pr_err("%s handoff failed (%d)\n", clk->dbg_name, err);
501 kfree(h);
502 ret = HANDOFF_DISABLED_CLK;
503 }
504 return ret;
505}
506
Matt Wagantallc7764192012-02-27 15:54:43 -0800507/**
508 * msm_clock_init() - Register and initialize a clock driver
509 * @data: Driver-specific clock initialization data
510 *
511 * Upon return from this call, clock APIs may be used to control
512 * clocks registered with this API.
513 */
514int __init msm_clock_init(struct clock_init_data *data)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700515{
516 unsigned n;
Stephen Boyd94625ef2011-07-12 17:06:01 -0700517 struct clk_lookup *clock_tbl;
518 size_t num_clocks;
Matt Wagantallb37fea42012-04-04 16:47:23 -0700519 struct clk *clk;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700520
Matt Wagantallc7764192012-02-27 15:54:43 -0800521 if (!data)
522 return -EINVAL;
523
Stephen Boydbb600ae2011-08-02 20:11:40 -0700524 clk_init_data = data;
Matt Wagantallb64888f2012-04-02 21:35:07 -0700525 if (clk_init_data->pre_init)
526 clk_init_data->pre_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700527
Stephen Boyd94625ef2011-07-12 17:06:01 -0700528 clock_tbl = data->table;
529 num_clocks = data->size;
530
Stephen Boydbd323442011-02-23 09:37:42 -0800531 for (n = 0; n < num_clocks; n++) {
Matt Wagantallb37fea42012-04-04 16:47:23 -0700532 struct clk *parent;
533 clk = clock_tbl[n].clk;
534 parent = clk_get_parent(clk);
Matt Wagantalle1482bf2012-04-04 16:23:45 -0700535 if (parent && list_empty(&clk->siblings))
536 list_add(&clk->siblings, &parent->children);
Matt Wagantallb37fea42012-04-04 16:47:23 -0700537 }
538
539 /*
540 * Detect and preserve initial clock state until clock_late_init() or
541 * a driver explicitly changes it, whichever is first.
542 */
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700543 for (n = 0; n < num_clocks; n++)
544 __handoff_clk(clock_tbl[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700545
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 clkdev_add_table(clock_tbl, num_clocks);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700547
548 if (clk_init_data->post_init)
549 clk_init_data->post_init();
Matt Wagantallc7764192012-02-27 15:54:43 -0800550
551 clock_debug_init();
552 clock_debug_register(clock_tbl, num_clocks);
553
554 return 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700555}
556
Brian Swetland600f7cf2008-09-09 11:04:14 -0700557static int __init clock_late_init(void)
558{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700559 struct handoff_clk *h, *h_temp;
Matt Wagantallc7764192012-02-27 15:54:43 -0800560 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
Matt Wagantall647d1c12012-05-16 14:32:14 -0700562 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Matt Wagantall158f73b2012-05-16 11:29:35 -0700563 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
564 clk_disable_unprepare(h->clk);
565 list_del(&h->list);
566 kfree(h);
567 }
568
Stephen Boydbb600ae2011-08-02 20:11:40 -0700569 if (clk_init_data->late_init)
570 ret = clk_init_data->late_init();
571 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700572}
Brian Swetland600f7cf2008-09-09 11:04:14 -0700573late_initcall(clock_late_init);