blob: ecd25fc6414dda7e9e5f2a9e70c2df9c36b0d9d2 [file] [log] [blame]
Brian Swetland600f7cf2008-09-09 11:04:14 -07001/* arch/arm/mach-msm/clock.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Saravana Kannanc85ecf92013-01-21 17:58:35 -08004 * Copyright (c) 2007-2013, The Linux Foundation. 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>
Patrick Dalyebc26bc2013-02-05 11:49:07 -080025#include <linux/regulator/consumer.h>
Stephen Boyd5bc44d52012-03-29 11:00:57 -070026#include <trace/events/power.h>
Matt Wagantall33d01f52012-02-23 23:27:44 -080027#include <mach/clk-provider.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070028#include "clock.h"
Brian Swetland600f7cf2008-09-09 11:04:14 -070029
Matt Wagantall158f73b2012-05-16 11:29:35 -070030struct handoff_clk {
31 struct list_head list;
32 struct clk *clk;
33};
34static LIST_HEAD(handoff_list);
35
Matt Wagantalle18bbc82011-10-06 10:07:28 -070036/* Find the voltage level required for a given rate. */
Patrick Daly0a78a0e2012-07-23 13:18:59 -070037int find_vdd_level(struct clk *clk, unsigned long rate)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070038{
39 int level;
40
Saravana Kannan55e959d2012-10-15 22:16:04 -070041 for (level = 0; level < clk->num_fmax; level++)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070042 if (rate <= clk->fmax[level])
43 break;
44
Saravana Kannan55e959d2012-10-15 22:16:04 -070045 if (level == clk->num_fmax) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -070046 pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
47 clk->dbg_name);
48 return -EINVAL;
49 }
50
51 return level;
52}
53
54/* Update voltage level given the current votes. */
55static int update_vdd(struct clk_vdd_class *vdd_class)
56{
Patrick Dalyebc26bc2013-02-05 11:49:07 -080057 int level, rc = 0, i;
58 struct regulator **r = vdd_class->regulator;
59 const int **vdd_uv = vdd_class->vdd_uv;
60 int max_level = vdd_class->num_levels - 1;
Matt Wagantalle18bbc82011-10-06 10:07:28 -070061
Patrick Dalyebc26bc2013-02-05 11:49:07 -080062 for (level = max_level; level > 0; level--)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070063 if (vdd_class->level_votes[level])
64 break;
65
66 if (level == vdd_class->cur_level)
67 return 0;
68
Patrick Dalyebc26bc2013-02-05 11:49:07 -080069 for (i = 0; i < vdd_class->num_regulators; i++) {
70 rc = regulator_set_voltage(r[i], vdd_uv[level][i],
71 vdd_uv[max_level][i]);
72 if (rc)
73 goto set_voltage_fail;
74 }
75 if (vdd_class->set_vdd && !vdd_class->num_regulators)
76 rc = vdd_class->set_vdd(vdd_class, level);
77
Matt Wagantalle18bbc82011-10-06 10:07:28 -070078 if (!rc)
79 vdd_class->cur_level = level;
80
81 return rc;
Patrick Dalyebc26bc2013-02-05 11:49:07 -080082
83set_voltage_fail:
84 level = vdd_class->cur_level;
85 for (i--; i >= 0; i--)
86 regulator_set_voltage(r[i], vdd_uv[level][i],
87 vdd_uv[max_level][i]);
88
89 return rc;
Matt Wagantalle18bbc82011-10-06 10:07:28 -070090}
91
92/* Vote for a voltage level. */
93int vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
94{
Matt Wagantalle18bbc82011-10-06 10:07:28 -070095 int rc;
96
Saravana Kannan55e959d2012-10-15 22:16:04 -070097 if (level >= vdd_class->num_levels)
98 return -EINVAL;
99
Stephen Boyda1f610a2012-09-22 00:40:37 -0700100 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700101 vdd_class->level_votes[level]++;
102 rc = update_vdd(vdd_class);
103 if (rc)
104 vdd_class->level_votes[level]--;
Stephen Boyda1f610a2012-09-22 00:40:37 -0700105 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700106
107 return rc;
108}
109
110/* Remove vote for a voltage level. */
111int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
112{
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700113 int rc = 0;
114
Saravana Kannan55e959d2012-10-15 22:16:04 -0700115 if (level >= vdd_class->num_levels)
116 return -EINVAL;
117
Stephen Boyda1f610a2012-09-22 00:40:37 -0700118 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700119 if (WARN(!vdd_class->level_votes[level],
120 "Reference counts are incorrect for %s level %d\n",
121 vdd_class->class_name, level))
122 goto out;
123 vdd_class->level_votes[level]--;
124 rc = update_vdd(vdd_class);
125 if (rc)
126 vdd_class->level_votes[level]++;
127out:
Stephen Boyda1f610a2012-09-22 00:40:37 -0700128 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700129 return rc;
130}
131
132/* Vote for a voltage level corresponding to a clock's rate. */
133static int vote_rate_vdd(struct clk *clk, unsigned long rate)
134{
135 int level;
136
137 if (!clk->vdd_class)
138 return 0;
139
140 level = find_vdd_level(clk, rate);
141 if (level < 0)
142 return level;
143
144 return vote_vdd_level(clk->vdd_class, level);
145}
146
147/* Remove vote for a voltage level corresponding to a clock's rate. */
148static void unvote_rate_vdd(struct clk *clk, unsigned long rate)
149{
150 int level;
151
152 if (!clk->vdd_class)
153 return;
154
155 level = find_vdd_level(clk, rate);
156 if (level < 0)
157 return;
158
159 unvote_vdd_level(clk->vdd_class, level);
160}
161
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700162/* Returns true if the rate is valid without voting for it */
163static bool is_rate_valid(struct clk *clk, unsigned long rate)
164{
165 int level;
166
167 if (!clk->vdd_class)
168 return true;
169
170 level = find_vdd_level(clk, rate);
171 return level >= 0;
172}
173
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800174int clk_prepare(struct clk *clk)
175{
176 int ret = 0;
177 struct clk *parent;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700178
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800179 if (!clk)
180 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700181 if (IS_ERR(clk))
182 return -EINVAL;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800183
184 mutex_lock(&clk->prepare_lock);
185 if (clk->prepare_count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700186 parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800187
188 ret = clk_prepare(parent);
189 if (ret)
190 goto out;
191 ret = clk_prepare(clk->depends);
192 if (ret)
193 goto err_prepare_depends;
194
Stephen Boydd86d1f22012-01-24 17:36:34 -0800195 ret = vote_rate_vdd(clk, clk->rate);
196 if (ret)
197 goto err_vote_vdd;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800198 if (clk->ops->prepare)
199 ret = clk->ops->prepare(clk);
200 if (ret)
201 goto err_prepare_clock;
202 }
203 clk->prepare_count++;
204out:
205 mutex_unlock(&clk->prepare_lock);
206 return ret;
207err_prepare_clock:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800208 unvote_rate_vdd(clk, clk->rate);
209err_vote_vdd:
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800210 clk_unprepare(clk->depends);
211err_prepare_depends:
212 clk_unprepare(parent);
213 goto out;
214}
215EXPORT_SYMBOL(clk_prepare);
216
Brian Swetland600f7cf2008-09-09 11:04:14 -0700217/*
Brian Swetland600f7cf2008-09-09 11:04:14 -0700218 * Standard clock functions defined in include/linux/clk.h
219 */
Brian Swetland600f7cf2008-09-09 11:04:14 -0700220int clk_enable(struct clk *clk)
221{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 int ret = 0;
Matt Wagantall7205eea2011-11-04 17:31:29 -0700223 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 struct clk *parent;
Stephen Boyd64dce902012-08-09 12:59:40 -0700225 const char *name = clk ? clk->dbg_name : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226
227 if (!clk)
228 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700229 if (IS_ERR(clk))
230 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231
232 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700233 WARN(!clk->prepare_count,
234 "%s: Don't call enable on unprepared clocks\n", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 if (clk->count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700236 parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 ret = clk_enable(parent);
239 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700240 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700241 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700242 if (ret)
243 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244
Stephen Boyd64dce902012-08-09 12:59:40 -0700245 trace_clock_enable(name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 if (clk->ops->enable)
247 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700248 if (ret)
249 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700251 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 spin_unlock_irqrestore(&clk->lock, flags);
253
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700254 return 0;
255
256err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700257 clk_disable(clk->depends);
258err_enable_depends:
259 clk_disable(parent);
260err_enable_parent:
261 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700263}
264EXPORT_SYMBOL(clk_enable);
265
266void clk_disable(struct clk *clk)
267{
Stephen Boyd64dce902012-08-09 12:59:40 -0700268 const char *name = clk ? clk->dbg_name : NULL;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700269 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700271 if (IS_ERR_OR_NULL(clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 return;
273
274 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700275 WARN(!clk->prepare_count,
276 "%s: Never called prepare or calling disable after unprepare\n",
277 name);
278 if (WARN(clk->count == 0, "%s is unbalanced", name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 goto out;
280 if (clk->count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700281 struct clk *parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700282
Stephen Boyd64dce902012-08-09 12:59:40 -0700283 trace_clock_disable(name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 if (clk->ops->disable)
285 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700286 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 clk_disable(parent);
288 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700289 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290out:
291 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700292}
293EXPORT_SYMBOL(clk_disable);
294
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800295void clk_unprepare(struct clk *clk)
296{
Stephen Boyd64dce902012-08-09 12:59:40 -0700297 const char *name = clk ? clk->dbg_name : NULL;
298
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700299 if (IS_ERR_OR_NULL(clk))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800300 return;
301
302 mutex_lock(&clk->prepare_lock);
Stephen Boyd64dce902012-08-09 12:59:40 -0700303 if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800304 goto out;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800305 if (clk->prepare_count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700306 struct clk *parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800307
Stephen Boyd64dce902012-08-09 12:59:40 -0700308 WARN(clk->count,
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800309 "%s: Don't call unprepare when the clock is enabled\n",
Stephen Boyd64dce902012-08-09 12:59:40 -0700310 name);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800311
312 if (clk->ops->unprepare)
313 clk->ops->unprepare(clk);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800314 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800315 clk_unprepare(clk->depends);
316 clk_unprepare(parent);
317 }
318 clk->prepare_count--;
319out:
320 mutex_unlock(&clk->prepare_lock);
321}
322EXPORT_SYMBOL(clk_unprepare);
323
Daniel Walker5e96da52010-05-12 13:43:28 -0700324int clk_reset(struct clk *clk, enum clk_reset_action action)
325{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700326 if (IS_ERR_OR_NULL(clk))
327 return -EINVAL;
328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 if (!clk->ops->reset)
330 return -ENOSYS;
331
332 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700333}
334EXPORT_SYMBOL(clk_reset);
335
Brian Swetland600f7cf2008-09-09 11:04:14 -0700336unsigned long clk_get_rate(struct clk *clk)
337{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700338 if (IS_ERR_OR_NULL(clk))
339 return 0;
340
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800342 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343
344 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700345}
346EXPORT_SYMBOL(clk_get_rate);
347
Matt Wagantall77952c42011-11-08 18:45:48 -0800348int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700349{
Stephen Boydd86d1f22012-01-24 17:36:34 -0800350 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700351 int rc = 0;
Stephen Boyd64dce902012-08-09 12:59:40 -0700352 const char *name = clk ? clk->dbg_name : NULL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700353
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700354 if (IS_ERR_OR_NULL(clk))
355 return -EINVAL;
356
Matt Wagantall77952c42011-11-08 18:45:48 -0800357 if (!clk->ops->set_rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 return -ENOSYS;
Daniel Walker3a790bb2010-12-13 14:35:10 -0800359
Stephen Boydd86d1f22012-01-24 17:36:34 -0800360 mutex_lock(&clk->prepare_lock);
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700361
362 /* Return early if the rate isn't going to change */
363 if (clk->rate == rate)
364 goto out;
365
Stephen Boyd64dce902012-08-09 12:59:40 -0700366 trace_clock_set_rate(name, rate, raw_smp_processor_id());
Stephen Boydd86d1f22012-01-24 17:36:34 -0800367 if (clk->prepare_count) {
Matt Wagantall7205eea2011-11-04 17:31:29 -0700368 start_rate = clk->rate;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700369 /* Enforce vdd requirements for target frequency. */
370 rc = vote_rate_vdd(clk, rate);
371 if (rc)
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700372 goto out;
Matt Wagantall77952c42011-11-08 18:45:48 -0800373 rc = clk->ops->set_rate(clk, rate);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700374 if (rc)
375 goto err_set_rate;
376 /* Release vdd requirements for starting frequency. */
377 unvote_rate_vdd(clk, start_rate);
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700378 } else if (is_rate_valid(clk, rate)) {
Matt Wagantall77952c42011-11-08 18:45:48 -0800379 rc = clk->ops->set_rate(clk, rate);
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700380 } else {
381 rc = -EINVAL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700382 }
Matt Wagantall7205eea2011-11-04 17:31:29 -0700383
384 if (!rc)
385 clk->rate = rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700386out:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800387 mutex_unlock(&clk->prepare_lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700388 return rc;
389
390err_set_rate:
391 unvote_rate_vdd(clk, rate);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800392 goto out;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700393}
Matt Wagantall77952c42011-11-08 18:45:48 -0800394EXPORT_SYMBOL(clk_set_rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700395
Daniel Walker5e96da52010-05-12 13:43:28 -0700396long clk_round_rate(struct clk *clk, unsigned long rate)
397{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700398 if (IS_ERR_OR_NULL(clk))
399 return -EINVAL;
400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 if (!clk->ops->round_rate)
402 return -ENOSYS;
403
404 return clk->ops->round_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700405}
406EXPORT_SYMBOL(clk_round_rate);
407
Daniel Walker5e96da52010-05-12 13:43:28 -0700408int clk_set_max_rate(struct clk *clk, unsigned long rate)
409{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700410 if (IS_ERR_OR_NULL(clk))
411 return -EINVAL;
412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413 if (!clk->ops->set_max_rate)
414 return -ENOSYS;
415
416 return clk->ops->set_max_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700417}
418EXPORT_SYMBOL(clk_set_max_rate);
419
Brian Swetland600f7cf2008-09-09 11:04:14 -0700420int clk_set_parent(struct clk *clk, struct clk *parent)
421{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 if (!clk->ops->set_parent)
423 return 0;
424
425 return clk->ops->set_parent(clk, parent);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700426}
427EXPORT_SYMBOL(clk_set_parent);
428
429struct clk *clk_get_parent(struct clk *clk)
430{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700431 if (IS_ERR_OR_NULL(clk))
432 return NULL;
433
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700434 return clk->parent;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700435}
436EXPORT_SYMBOL(clk_get_parent);
437
438int clk_set_flags(struct clk *clk, unsigned long flags)
439{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700440 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700441 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 if (!clk->ops->set_flags)
443 return -ENOSYS;
444
445 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700446}
447EXPORT_SYMBOL(clk_set_flags);
448
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800449static struct clock_init_data *clk_init_data;
450
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700451static void init_sibling_lists(struct clk_lookup *clock_tbl, size_t num_clocks)
452{
453 struct clk *clk, *parent;
454 unsigned n;
455
456 for (n = 0; n < num_clocks; n++) {
457 clk = clock_tbl[n].clk;
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700458 parent = clk->parent;
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700459 if (parent && list_empty(&clk->siblings))
460 list_add(&clk->siblings, &parent->children);
461 }
462}
463
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800464/**
465 * msm_clock_register() - Register additional clock tables
466 * @table: Table of clocks
467 * @size: Size of @table
468 *
469 * Upon return, clock APIs may be used to control clocks registered using this
470 * function. This API may only be used after msm_clock_init() has completed.
471 * Unlike msm_clock_init(), this function may be called multiple times with
472 * different clock lists and used after the kernel has finished booting.
473 */
474int msm_clock_register(struct clk_lookup *table, size_t size)
475{
476 if (!clk_init_data)
477 return -ENODEV;
478
479 if (!table)
480 return -EINVAL;
481
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700482 init_sibling_lists(table, size);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800483 clkdev_add_table(table, size);
484 clock_debug_register(table, size);
485
486 return 0;
487}
488EXPORT_SYMBOL(msm_clock_register);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700489
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800490static int __init __handoff_clk(struct clk *clk)
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700491{
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800492 enum handoff state = HANDOFF_DISABLED_CLK;
493 struct handoff_clk *h = NULL;
494 int rc;
495
496 if (clk == NULL || clk->flags & CLKFLAG_INIT_DONE ||
497 clk->flags & CLKFLAG_SKIP_HANDOFF)
498 return 0;
499
500 if (clk->flags & CLKFLAG_INIT_ERR)
501 return -ENXIO;
502
503 /* Handoff any 'depends' clock first. */
504 rc = __handoff_clk(clk->depends);
505 if (rc)
506 goto err;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700507
508 /*
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800509 * Handoff functions for the parent must be called before the
510 * children can be handed off. Without handing off the parents and
511 * knowing their rate and state (on/off), it's impossible to figure
512 * out the rate and state of the children.
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700513 */
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800514 if (clk->ops->get_parent)
515 clk->parent = clk->ops->get_parent(clk);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700516
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800517 if (IS_ERR(clk->parent)) {
518 rc = PTR_ERR(clk->parent);
519 goto err;
520 }
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700521
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800522 rc = __handoff_clk(clk->parent);
523 if (rc)
524 goto err;
525
526 if (clk->ops->handoff)
527 state = clk->ops->handoff(clk);
528
529 if (state == HANDOFF_ENABLED_CLK) {
530
531 h = kmalloc(sizeof(*h), GFP_KERNEL);
532 if (!h) {
533 rc = -ENOMEM;
534 goto err;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700535 }
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800536
537 rc = clk_prepare_enable(clk->parent);
538 if (rc)
539 goto err;
540
541 rc = clk_prepare_enable(clk->depends);
542 if (rc)
543 goto err_depends;
544
545 rc = vote_rate_vdd(clk, clk->rate);
546 WARN(rc, "%s unable to vote for voltage!\n", clk->dbg_name);
547
548 clk->count = 1;
549 clk->prepare_count = 1;
550 h->clk = clk;
551 list_add_tail(&h->list, &handoff_list);
552
553 pr_debug("Handed off %s rate=%lu\n", clk->dbg_name, clk->rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700554 }
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800555
556 clk->flags |= CLKFLAG_INIT_DONE;
557
558 return 0;
559
560err_depends:
561 clk_disable_unprepare(clk->parent);
562err:
563 kfree(h);
564 clk->flags |= CLKFLAG_INIT_ERR;
565 pr_err("%s handoff failed (%d)\n", clk->dbg_name, rc);
566 return rc;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700567}
568
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800569/**
570 * msm_clock_init() - Register and initialize a clock driver
571 * @data: Driver-specific clock initialization data
572 *
573 * Upon return from this call, clock APIs may be used to control
574 * clocks registered with this API.
575 */
576int __init msm_clock_init(struct clock_init_data *data)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700577{
578 unsigned n;
Stephen Boyd94625ef2011-07-12 17:06:01 -0700579 struct clk_lookup *clock_tbl;
580 size_t num_clocks;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700581
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800582 if (!data)
583 return -EINVAL;
584
Stephen Boydbb600ae2011-08-02 20:11:40 -0700585 clk_init_data = data;
Matt Wagantallb64888f2012-04-02 21:35:07 -0700586 if (clk_init_data->pre_init)
587 clk_init_data->pre_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700588
Stephen Boyd94625ef2011-07-12 17:06:01 -0700589 clock_tbl = data->table;
590 num_clocks = data->size;
591
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700592 init_sibling_lists(clock_tbl, num_clocks);
593
Matt Wagantallb37fea42012-04-04 16:47:23 -0700594 /*
595 * Detect and preserve initial clock state until clock_late_init() or
596 * a driver explicitly changes it, whichever is first.
597 */
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700598 for (n = 0; n < num_clocks; n++)
599 __handoff_clk(clock_tbl[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700600
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700601 clkdev_add_table(clock_tbl, num_clocks);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700602
603 if (clk_init_data->post_init)
604 clk_init_data->post_init();
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800605
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700606 clock_debug_init();
607 clock_debug_register(clock_tbl, num_clocks);
608
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800609 return 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700610}
611
Brian Swetland600f7cf2008-09-09 11:04:14 -0700612static int __init clock_late_init(void)
613{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700614 struct handoff_clk *h, *h_temp;
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800615 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
Matt Wagantall647d1c12012-05-16 14:32:14 -0700617 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Matt Wagantall158f73b2012-05-16 11:29:35 -0700618 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
619 clk_disable_unprepare(h->clk);
620 list_del(&h->list);
621 kfree(h);
622 }
623
Stephen Boydbb600ae2011-08-02 20:11:40 -0700624 if (clk_init_data->late_init)
625 ret = clk_init_data->late_init();
626 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700627}
Brian Swetland600f7cf2008-09-09 11:04:14 -0700628late_initcall(clock_late_init);