blob: c2bf5bac8f1f227bb7129cbacf378d08ae3381ff [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
Saravana Kannan55e959d2012-10-15 22:16:04 -070040 for (level = 0; level < clk->num_fmax; level++)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070041 if (rate <= clk->fmax[level])
42 break;
43
Saravana Kannan55e959d2012-10-15 22:16:04 -070044 if (level == clk->num_fmax) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -070045 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
Saravana Kannan55e959d2012-10-15 22:16:04 -070058 for (level = vdd_class->num_levels-1; level > 0; level--)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070059 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{
Matt Wagantalle18bbc82011-10-06 10:07:28 -070075 int rc;
76
Saravana Kannan55e959d2012-10-15 22:16:04 -070077 if (level >= vdd_class->num_levels)
78 return -EINVAL;
79
Stephen Boyda1f610a2012-09-22 00:40:37 -070080 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -070081 vdd_class->level_votes[level]++;
82 rc = update_vdd(vdd_class);
83 if (rc)
84 vdd_class->level_votes[level]--;
Stephen Boyda1f610a2012-09-22 00:40:37 -070085 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -070086
87 return rc;
88}
89
90/* Remove vote for a voltage level. */
91int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
92{
Matt Wagantalle18bbc82011-10-06 10:07:28 -070093 int rc = 0;
94
Saravana Kannan55e959d2012-10-15 22:16:04 -070095 if (level >= vdd_class->num_levels)
96 return -EINVAL;
97
Stephen Boyda1f610a2012-09-22 00:40:37 -070098 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -070099 if (WARN(!vdd_class->level_votes[level],
100 "Reference counts are incorrect for %s level %d\n",
101 vdd_class->class_name, level))
102 goto out;
103 vdd_class->level_votes[level]--;
104 rc = update_vdd(vdd_class);
105 if (rc)
106 vdd_class->level_votes[level]++;
107out:
Stephen Boyda1f610a2012-09-22 00:40:37 -0700108 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700109 return rc;
110}
111
112/* Vote for a voltage level corresponding to a clock's rate. */
113static int vote_rate_vdd(struct clk *clk, unsigned long rate)
114{
115 int level;
116
117 if (!clk->vdd_class)
118 return 0;
119
120 level = find_vdd_level(clk, rate);
121 if (level < 0)
122 return level;
123
124 return vote_vdd_level(clk->vdd_class, level);
125}
126
127/* Remove vote for a voltage level corresponding to a clock's rate. */
128static void unvote_rate_vdd(struct clk *clk, unsigned long rate)
129{
130 int level;
131
132 if (!clk->vdd_class)
133 return;
134
135 level = find_vdd_level(clk, rate);
136 if (level < 0)
137 return;
138
139 unvote_vdd_level(clk->vdd_class, level);
140}
141
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700142/* Returns true if the rate is valid without voting for it */
143static bool is_rate_valid(struct clk *clk, unsigned long rate)
144{
145 int level;
146
147 if (!clk->vdd_class)
148 return true;
149
150 level = find_vdd_level(clk, rate);
151 return level >= 0;
152}
153
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800154int clk_prepare(struct clk *clk)
155{
156 int ret = 0;
157 struct clk *parent;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700158
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800159 if (!clk)
160 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700161 if (IS_ERR(clk))
162 return -EINVAL;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800163
164 mutex_lock(&clk->prepare_lock);
165 if (clk->prepare_count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700166 parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800167
168 ret = clk_prepare(parent);
169 if (ret)
170 goto out;
171 ret = clk_prepare(clk->depends);
172 if (ret)
173 goto err_prepare_depends;
174
Stephen Boydd86d1f22012-01-24 17:36:34 -0800175 ret = vote_rate_vdd(clk, clk->rate);
176 if (ret)
177 goto err_vote_vdd;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800178 if (clk->ops->prepare)
179 ret = clk->ops->prepare(clk);
180 if (ret)
181 goto err_prepare_clock;
182 }
183 clk->prepare_count++;
184out:
185 mutex_unlock(&clk->prepare_lock);
186 return ret;
187err_prepare_clock:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800188 unvote_rate_vdd(clk, clk->rate);
189err_vote_vdd:
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800190 clk_unprepare(clk->depends);
191err_prepare_depends:
192 clk_unprepare(parent);
193 goto out;
194}
195EXPORT_SYMBOL(clk_prepare);
196
Brian Swetland600f7cf2008-09-09 11:04:14 -0700197/*
Brian Swetland600f7cf2008-09-09 11:04:14 -0700198 * Standard clock functions defined in include/linux/clk.h
199 */
Brian Swetland600f7cf2008-09-09 11:04:14 -0700200int clk_enable(struct clk *clk)
201{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 int ret = 0;
Matt Wagantall7205eea2011-11-04 17:31:29 -0700203 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 struct clk *parent;
Stephen Boyd64dce902012-08-09 12:59:40 -0700205 const char *name = clk ? clk->dbg_name : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206
207 if (!clk)
208 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700209 if (IS_ERR(clk))
210 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211
212 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700213 WARN(!clk->prepare_count,
214 "%s: Don't call enable on unprepared clocks\n", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 if (clk->count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700216 parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 ret = clk_enable(parent);
219 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700220 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700221 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700222 if (ret)
223 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224
Stephen Boyd64dce902012-08-09 12:59:40 -0700225 trace_clock_enable(name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 if (clk->ops->enable)
227 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700228 if (ret)
229 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700231 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 spin_unlock_irqrestore(&clk->lock, flags);
233
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700234 return 0;
235
236err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700237 clk_disable(clk->depends);
238err_enable_depends:
239 clk_disable(parent);
240err_enable_parent:
241 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700243}
244EXPORT_SYMBOL(clk_enable);
245
246void clk_disable(struct clk *clk)
247{
Stephen Boyd64dce902012-08-09 12:59:40 -0700248 const char *name = clk ? clk->dbg_name : NULL;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700249 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700251 if (IS_ERR_OR_NULL(clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 return;
253
254 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700255 WARN(!clk->prepare_count,
256 "%s: Never called prepare or calling disable after unprepare\n",
257 name);
258 if (WARN(clk->count == 0, "%s is unbalanced", name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 goto out;
260 if (clk->count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700261 struct clk *parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700262
Stephen Boyd64dce902012-08-09 12:59:40 -0700263 trace_clock_disable(name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 if (clk->ops->disable)
265 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700266 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 clk_disable(parent);
268 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700269 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270out:
271 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700272}
273EXPORT_SYMBOL(clk_disable);
274
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800275void clk_unprepare(struct clk *clk)
276{
Stephen Boyd64dce902012-08-09 12:59:40 -0700277 const char *name = clk ? clk->dbg_name : NULL;
278
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700279 if (IS_ERR_OR_NULL(clk))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800280 return;
281
282 mutex_lock(&clk->prepare_lock);
Stephen Boyd64dce902012-08-09 12:59:40 -0700283 if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800284 goto out;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800285 if (clk->prepare_count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700286 struct clk *parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800287
Stephen Boyd64dce902012-08-09 12:59:40 -0700288 WARN(clk->count,
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800289 "%s: Don't call unprepare when the clock is enabled\n",
Stephen Boyd64dce902012-08-09 12:59:40 -0700290 name);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800291
292 if (clk->ops->unprepare)
293 clk->ops->unprepare(clk);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800294 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800295 clk_unprepare(clk->depends);
296 clk_unprepare(parent);
297 }
298 clk->prepare_count--;
299out:
300 mutex_unlock(&clk->prepare_lock);
301}
302EXPORT_SYMBOL(clk_unprepare);
303
Daniel Walker5e96da52010-05-12 13:43:28 -0700304int clk_reset(struct clk *clk, enum clk_reset_action action)
305{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700306 if (IS_ERR_OR_NULL(clk))
307 return -EINVAL;
308
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 if (!clk->ops->reset)
310 return -ENOSYS;
311
312 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700313}
314EXPORT_SYMBOL(clk_reset);
315
Brian Swetland600f7cf2008-09-09 11:04:14 -0700316unsigned long clk_get_rate(struct clk *clk)
317{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700318 if (IS_ERR_OR_NULL(clk))
319 return 0;
320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800322 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
324 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700325}
326EXPORT_SYMBOL(clk_get_rate);
327
Matt Wagantall77952c42011-11-08 18:45:48 -0800328int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700329{
Stephen Boydd86d1f22012-01-24 17:36:34 -0800330 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700331 int rc = 0;
Stephen Boyd64dce902012-08-09 12:59:40 -0700332 const char *name = clk ? clk->dbg_name : NULL;
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 Boydd86d1f22012-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 Boyd64dce902012-08-09 12:59:40 -0700346 trace_clock_set_rate(name, rate, raw_smp_processor_id());
Stephen Boydd86d1f22012-01-24 17:36:34 -0800347 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 Dalyc009d9e2012-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 Dalyc009d9e2012-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 Dalyc009d9e2012-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 Boydd86d1f22012-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 Boydd86d1f22012-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
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700414 return clk->parent;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700415}
416EXPORT_SYMBOL(clk_get_parent);
417
418int clk_set_flags(struct clk *clk, unsigned long flags)
419{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700420 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700421 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 if (!clk->ops->set_flags)
423 return -ENOSYS;
424
425 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700426}
427EXPORT_SYMBOL(clk_set_flags);
428
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800429static struct clock_init_data *clk_init_data;
430
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700431static void init_sibling_lists(struct clk_lookup *clock_tbl, size_t num_clocks)
432{
433 struct clk *clk, *parent;
434 unsigned n;
435
436 for (n = 0; n < num_clocks; n++) {
437 clk = clock_tbl[n].clk;
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700438 parent = clk->parent;
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700439 if (parent && list_empty(&clk->siblings))
440 list_add(&clk->siblings, &parent->children);
441 }
442}
443
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800444/**
445 * msm_clock_register() - Register additional clock tables
446 * @table: Table of clocks
447 * @size: Size of @table
448 *
449 * Upon return, clock APIs may be used to control clocks registered using this
450 * function. This API may only be used after msm_clock_init() has completed.
451 * Unlike msm_clock_init(), this function may be called multiple times with
452 * different clock lists and used after the kernel has finished booting.
453 */
454int msm_clock_register(struct clk_lookup *table, size_t size)
455{
456 if (!clk_init_data)
457 return -ENODEV;
458
459 if (!table)
460 return -EINVAL;
461
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700462 init_sibling_lists(table, size);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800463 clkdev_add_table(table, size);
464 clock_debug_register(table, size);
465
466 return 0;
467}
468EXPORT_SYMBOL(msm_clock_register);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700469
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700470static enum handoff __init __handoff_clk(struct clk *clk)
471{
472 enum handoff ret;
473 struct handoff_clk *h;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700474 unsigned long rate;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700475 int err = 0;
476
477 /*
478 * Tree roots don't have parents, but need to be handed off. So,
479 * terminate recursion by returning "enabled". Also return "enabled"
480 * for clocks with non-zero enable counts since they must have already
481 * been handed off.
482 */
483 if (clk == NULL || clk->count)
484 return HANDOFF_ENABLED_CLK;
485
486 /* Clocks without handoff functions are assumed to be disabled. */
487 if (!clk->ops->handoff || (clk->flags & CLKFLAG_SKIP_HANDOFF))
488 return HANDOFF_DISABLED_CLK;
489
490 /*
491 * Handoff functions for children must be called before their parents'
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700492 * so that the correct parent is available below.
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700493 */
494 ret = clk->ops->handoff(clk);
495 if (ret == HANDOFF_ENABLED_CLK) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700496 ret = __handoff_clk(clk->parent);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700497 if (ret == HANDOFF_ENABLED_CLK) {
498 h = kmalloc(sizeof(*h), GFP_KERNEL);
499 if (!h) {
500 err = -ENOMEM;
501 goto out;
502 }
503 err = clk_prepare_enable(clk);
504 if (err)
505 goto out;
Matt Wagantall2a59b212012-06-12 19:16:01 -0700506 rate = clk_get_rate(clk);
507 if (rate)
508 pr_debug("%s rate=%lu\n", clk->dbg_name, rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700509 h->clk = clk;
510 list_add_tail(&h->list, &handoff_list);
511 }
512 }
513out:
514 if (err) {
515 pr_err("%s handoff failed (%d)\n", clk->dbg_name, err);
516 kfree(h);
517 ret = HANDOFF_DISABLED_CLK;
518 }
519 return ret;
520}
521
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800522/**
523 * msm_clock_init() - Register and initialize a clock driver
524 * @data: Driver-specific clock initialization data
525 *
526 * Upon return from this call, clock APIs may be used to control
527 * clocks registered with this API.
528 */
529int __init msm_clock_init(struct clock_init_data *data)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700530{
531 unsigned n;
Stephen Boyd94625ef2011-07-12 17:06:01 -0700532 struct clk_lookup *clock_tbl;
533 size_t num_clocks;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700534
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800535 if (!data)
536 return -EINVAL;
537
Stephen Boydbb600ae2011-08-02 20:11:40 -0700538 clk_init_data = data;
Matt Wagantallb64888f2012-04-02 21:35:07 -0700539 if (clk_init_data->pre_init)
540 clk_init_data->pre_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700541
Stephen Boyd94625ef2011-07-12 17:06:01 -0700542 clock_tbl = data->table;
543 num_clocks = data->size;
544
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700545 init_sibling_lists(clock_tbl, num_clocks);
546
Matt Wagantallb37fea42012-04-04 16:47:23 -0700547 /*
548 * Detect and preserve initial clock state until clock_late_init() or
549 * a driver explicitly changes it, whichever is first.
550 */
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700551 for (n = 0; n < num_clocks; n++)
552 __handoff_clk(clock_tbl[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700553
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700554 clkdev_add_table(clock_tbl, num_clocks);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700555
556 if (clk_init_data->post_init)
557 clk_init_data->post_init();
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800558
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700559 clock_debug_init();
560 clock_debug_register(clock_tbl, num_clocks);
561
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800562 return 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700563}
564
Brian Swetland600f7cf2008-09-09 11:04:14 -0700565static int __init clock_late_init(void)
566{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700567 struct handoff_clk *h, *h_temp;
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800568 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
Matt Wagantall647d1c12012-05-16 14:32:14 -0700570 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Matt Wagantall158f73b2012-05-16 11:29:35 -0700571 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
572 clk_disable_unprepare(h->clk);
573 list_del(&h->list);
574 kfree(h);
575 }
576
Stephen Boydbb600ae2011-08-02 20:11:40 -0700577 if (clk_init_data->late_init)
578 ret = clk_init_data->late_init();
579 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700580}
Brian Swetland600f7cf2008-09-09 11:04:14 -0700581late_initcall(clock_late_init);