blob: e3c11c5d8be052947020ee1cf89be715a1f253cf [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>
Vikram Mulukutla4785ab62012-12-10 20:51:22 -080026#include <linux/mutex.h>
Stephen Boyd5bc44d52012-03-29 11:00:57 -070027#include <trace/events/power.h>
Matt Wagantall33d01f52012-02-23 23:27:44 -080028#include <mach/clk-provider.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070029#include "clock.h"
Brian Swetland600f7cf2008-09-09 11:04:14 -070030
Matt Wagantall158f73b2012-05-16 11:29:35 -070031struct handoff_clk {
32 struct list_head list;
33 struct clk *clk;
34};
35static LIST_HEAD(handoff_list);
36
Patrick Daly5ce9da32013-03-26 13:12:53 -070037struct handoff_vdd {
38 struct list_head list;
39 struct clk_vdd_class *vdd_class;
40};
41static LIST_HEAD(handoff_vdd_list);
42
Vikram Mulukutla4785ab62012-12-10 20:51:22 -080043static DEFINE_MUTEX(msm_clock_init_lock);
44
Matt Wagantalle18bbc82011-10-06 10:07:28 -070045/* Find the voltage level required for a given rate. */
Patrick Daly0a78a0e2012-07-23 13:18:59 -070046int find_vdd_level(struct clk *clk, unsigned long rate)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070047{
48 int level;
49
Saravana Kannan55e959d2012-10-15 22:16:04 -070050 for (level = 0; level < clk->num_fmax; level++)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070051 if (rate <= clk->fmax[level])
52 break;
53
Saravana Kannan55e959d2012-10-15 22:16:04 -070054 if (level == clk->num_fmax) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -070055 pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
56 clk->dbg_name);
57 return -EINVAL;
58 }
59
60 return level;
61}
62
63/* Update voltage level given the current votes. */
64static int update_vdd(struct clk_vdd_class *vdd_class)
65{
Patrick Dalyebc26bc2013-02-05 11:49:07 -080066 int level, rc = 0, i;
67 struct regulator **r = vdd_class->regulator;
Junjie Wubb5a79e2013-05-15 13:12:39 -070068 int *uv = vdd_class->vdd_uv;
69 int *ua = vdd_class->vdd_ua;
70 int n_reg = vdd_class->num_regulators;
71 int max_lvl = vdd_class->num_levels - 1;
72 int lvl_base;
Matt Wagantalle18bbc82011-10-06 10:07:28 -070073
Junjie Wubb5a79e2013-05-15 13:12:39 -070074 for (level = max_lvl; level > 0; level--)
Matt Wagantalle18bbc82011-10-06 10:07:28 -070075 if (vdd_class->level_votes[level])
76 break;
77
78 if (level == vdd_class->cur_level)
79 return 0;
80
Junjie Wubb5a79e2013-05-15 13:12:39 -070081 max_lvl = max_lvl * n_reg;
82 lvl_base = level * n_reg;
Patrick Dalyebc26bc2013-02-05 11:49:07 -080083 for (i = 0; i < vdd_class->num_regulators; i++) {
Junjie Wubb5a79e2013-05-15 13:12:39 -070084 rc = regulator_set_voltage(r[i], uv[lvl_base + i],
85 uv[max_lvl + i]);
Patrick Dalyebc26bc2013-02-05 11:49:07 -080086 if (rc)
87 goto set_voltage_fail;
Patrick Daly653c0b52013-04-16 17:18:28 -070088
Junjie Wubb5a79e2013-05-15 13:12:39 -070089 if (!ua)
Patrick Daly653c0b52013-04-16 17:18:28 -070090 continue;
91
Junjie Wubb5a79e2013-05-15 13:12:39 -070092 rc = regulator_set_optimum_mode(r[i], ua[lvl_base + i]);
Patrick Dalybcdd7502013-07-16 14:19:54 -070093 rc = rc > 0 ? 0 : rc;
94 if (rc)
Patrick Daly653c0b52013-04-16 17:18:28 -070095 goto set_mode_fail;
Patrick Dalyebc26bc2013-02-05 11:49:07 -080096 }
97 if (vdd_class->set_vdd && !vdd_class->num_regulators)
98 rc = vdd_class->set_vdd(vdd_class, level);
99
Patrick Dalybcdd7502013-07-16 14:19:54 -0700100 if (!rc)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700101 vdd_class->cur_level = level;
102
Patrick Dalybcdd7502013-07-16 14:19:54 -0700103 return rc;
Patrick Daly653c0b52013-04-16 17:18:28 -0700104
105set_mode_fail:
Junjie Wubb5a79e2013-05-15 13:12:39 -0700106 regulator_set_voltage(r[i], uv[vdd_class->cur_level * n_reg + i],
107 uv[max_lvl + i]);
Patrick Dalyebc26bc2013-02-05 11:49:07 -0800108
109set_voltage_fail:
Junjie Wubb5a79e2013-05-15 13:12:39 -0700110 lvl_base = vdd_class->cur_level * n_reg;
Patrick Daly653c0b52013-04-16 17:18:28 -0700111 for (i--; i >= 0; i--) {
Junjie Wubb5a79e2013-05-15 13:12:39 -0700112 regulator_set_voltage(r[i], uv[lvl_base + i], uv[max_lvl + i]);
Patrick Daly653c0b52013-04-16 17:18:28 -0700113
Junjie Wubb5a79e2013-05-15 13:12:39 -0700114 if (!ua)
Patrick Daly653c0b52013-04-16 17:18:28 -0700115 continue;
Junjie Wubb5a79e2013-05-15 13:12:39 -0700116 regulator_set_optimum_mode(r[i], ua[lvl_base + i]);
Patrick Daly653c0b52013-04-16 17:18:28 -0700117 }
Patrick Dalyebc26bc2013-02-05 11:49:07 -0800118
119 return rc;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700120}
121
122/* Vote for a voltage level. */
123int vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
124{
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700125 int rc;
126
Saravana Kannan55e959d2012-10-15 22:16:04 -0700127 if (level >= vdd_class->num_levels)
128 return -EINVAL;
129
Stephen Boyda1f610a2012-09-22 00:40:37 -0700130 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700131 vdd_class->level_votes[level]++;
132 rc = update_vdd(vdd_class);
133 if (rc)
134 vdd_class->level_votes[level]--;
Stephen Boyda1f610a2012-09-22 00:40:37 -0700135 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700136
137 return rc;
138}
139
140/* Remove vote for a voltage level. */
141int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
142{
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700143 int rc = 0;
144
Saravana Kannan55e959d2012-10-15 22:16:04 -0700145 if (level >= vdd_class->num_levels)
146 return -EINVAL;
147
Stephen Boyda1f610a2012-09-22 00:40:37 -0700148 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700149 if (WARN(!vdd_class->level_votes[level],
150 "Reference counts are incorrect for %s level %d\n",
151 vdd_class->class_name, level))
152 goto out;
153 vdd_class->level_votes[level]--;
154 rc = update_vdd(vdd_class);
155 if (rc)
156 vdd_class->level_votes[level]++;
157out:
Stephen Boyda1f610a2012-09-22 00:40:37 -0700158 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700159 return rc;
160}
161
162/* Vote for a voltage level corresponding to a clock's rate. */
163static int vote_rate_vdd(struct clk *clk, unsigned long rate)
164{
165 int level;
166
167 if (!clk->vdd_class)
168 return 0;
169
170 level = find_vdd_level(clk, rate);
171 if (level < 0)
172 return level;
173
174 return vote_vdd_level(clk->vdd_class, level);
175}
176
177/* Remove vote for a voltage level corresponding to a clock's rate. */
178static void unvote_rate_vdd(struct clk *clk, unsigned long rate)
179{
180 int level;
181
182 if (!clk->vdd_class)
183 return;
184
185 level = find_vdd_level(clk, rate);
186 if (level < 0)
187 return;
188
189 unvote_vdd_level(clk->vdd_class, level);
190}
191
Saravana Kannana8c91542013-03-18 21:15:18 -0700192/* Check if the rate is within the voltage limits of the clock. */
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700193static bool is_rate_valid(struct clk *clk, unsigned long rate)
194{
195 int level;
196
197 if (!clk->vdd_class)
198 return true;
199
200 level = find_vdd_level(clk, rate);
201 return level >= 0;
202}
203
Saravana Kannan33c6a202013-03-20 22:19:10 -0700204/**
205 * __clk_pre_reparent() - Set up the new parent before switching to it and
206 * prevent the enable state of the child clock from changing.
207 * @c: The child clock that's going to switch parents
208 * @new: The new parent that the child clock is going to switch to
209 * @flags: Pointer to scratch space to save spinlock flags
210 *
211 * Cannot be called from atomic context.
212 *
213 * Use this API to set up the @new parent clock to be able to support the
214 * current prepare and enable state of the child clock @c. Once the parent is
215 * set up, the child clock can safely switch to it.
216 *
217 * The caller shall grab the prepare_lock of clock @c before calling this API
218 * and only release it after calling __clk_post_reparent() for clock @c (or
219 * if this API fails). This is necessary to prevent the prepare state of the
220 * child clock @c from changing while the reparenting is in progress. Since
221 * this API takes care of grabbing the enable lock of @c, only atomic
222 * operation are allowed between calls to __clk_pre_reparent and
223 * __clk_post_reparent()
224 *
225 * The scratch space pointed to by @flags should not be altered before
226 * calling __clk_post_reparent() for clock @c.
227 *
228 * See also: __clk_post_reparent()
229 */
230int __clk_pre_reparent(struct clk *c, struct clk *new, unsigned long *flags)
231{
232 int rc;
233
234 if (c->prepare_count) {
235 rc = clk_prepare(new);
236 if (rc)
237 return rc;
238 }
239
240 spin_lock_irqsave(&c->lock, *flags);
241 if (c->count) {
242 rc = clk_enable(new);
243 if (rc) {
244 spin_unlock_irqrestore(&c->lock, *flags);
245 clk_unprepare(new);
246 return rc;
247 }
248 }
249 return 0;
250}
251
252/**
253 * __clk_post_reparent() - Release requirements on old parent after switching
254 * away from it and allow changes to the child clock's enable state.
255 * @c: The child clock that switched parents
256 * @old: The old parent that the child clock switched away from or the new
257 * parent of a failed reparent attempt.
258 * @flags: Pointer to scratch space where spinlock flags were saved
259 *
260 * Cannot be called from atomic context.
261 *
262 * This API works in tandem with __clk_pre_reparent. Use this API to
263 * - Remove prepare and enable requirements from the @old parent after
264 * switching away from it
265 * - Or, undo the effects of __clk_pre_reparent() after a failed attempt to
266 * change parents
267 *
268 * The caller shall release the prepare_lock of @c that was grabbed before
269 * calling __clk_pre_reparent() only after this API is called (or if
270 * __clk_pre_reparent() fails). This is necessary to prevent the prepare
271 * state of the child clock @c from changing while the reparenting is in
272 * progress. Since this API releases the enable lock of @c, the limit to
273 * atomic operations set by __clk_pre_reparent() is no longer present.
274 *
275 * The scratch space pointed to by @flags shall not be altered since the call
276 * to __clk_pre_reparent() for clock @c.
277 *
278 * See also: __clk_pre_reparent()
279 */
280void __clk_post_reparent(struct clk *c, struct clk *old, unsigned long *flags)
281{
282 if (c->count)
283 clk_disable(old);
284 spin_unlock_irqrestore(&c->lock, *flags);
285
286 if (c->prepare_count)
287 clk_unprepare(old);
288}
289
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800290int clk_prepare(struct clk *clk)
291{
292 int ret = 0;
293 struct clk *parent;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700294
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800295 if (!clk)
296 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700297 if (IS_ERR(clk))
298 return -EINVAL;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800299
300 mutex_lock(&clk->prepare_lock);
301 if (clk->prepare_count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700302 parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800303
304 ret = clk_prepare(parent);
305 if (ret)
306 goto out;
307 ret = clk_prepare(clk->depends);
308 if (ret)
309 goto err_prepare_depends;
310
Stephen Boydd86d1f22012-01-24 17:36:34 -0800311 ret = vote_rate_vdd(clk, clk->rate);
312 if (ret)
313 goto err_vote_vdd;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800314 if (clk->ops->prepare)
315 ret = clk->ops->prepare(clk);
316 if (ret)
317 goto err_prepare_clock;
318 }
319 clk->prepare_count++;
320out:
321 mutex_unlock(&clk->prepare_lock);
322 return ret;
323err_prepare_clock:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800324 unvote_rate_vdd(clk, clk->rate);
325err_vote_vdd:
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800326 clk_unprepare(clk->depends);
327err_prepare_depends:
328 clk_unprepare(parent);
329 goto out;
330}
331EXPORT_SYMBOL(clk_prepare);
332
Brian Swetland600f7cf2008-09-09 11:04:14 -0700333/*
Brian Swetland600f7cf2008-09-09 11:04:14 -0700334 * Standard clock functions defined in include/linux/clk.h
335 */
Brian Swetland600f7cf2008-09-09 11:04:14 -0700336int clk_enable(struct clk *clk)
337{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 int ret = 0;
Matt Wagantall7205eea2011-11-04 17:31:29 -0700339 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 struct clk *parent;
Stephen Boyd64dce902012-08-09 12:59:40 -0700341 const char *name = clk ? clk->dbg_name : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342
343 if (!clk)
344 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700345 if (IS_ERR(clk))
346 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347
348 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700349 WARN(!clk->prepare_count,
350 "%s: Don't call enable on unprepared clocks\n", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 if (clk->count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700352 parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 ret = clk_enable(parent);
355 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700356 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700357 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700358 if (ret)
359 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360
Stephen Boyd64dce902012-08-09 12:59:40 -0700361 trace_clock_enable(name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 if (clk->ops->enable)
363 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700364 if (ret)
365 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700367 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368 spin_unlock_irqrestore(&clk->lock, flags);
369
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700370 return 0;
371
372err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700373 clk_disable(clk->depends);
374err_enable_depends:
375 clk_disable(parent);
376err_enable_parent:
377 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700379}
380EXPORT_SYMBOL(clk_enable);
381
382void clk_disable(struct clk *clk)
383{
Stephen Boyd64dce902012-08-09 12:59:40 -0700384 const char *name = clk ? clk->dbg_name : NULL;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700385 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700387 if (IS_ERR_OR_NULL(clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 return;
389
390 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700391 WARN(!clk->prepare_count,
392 "%s: Never called prepare or calling disable after unprepare\n",
393 name);
394 if (WARN(clk->count == 0, "%s is unbalanced", name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 goto out;
396 if (clk->count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700397 struct clk *parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700398
Stephen Boyd64dce902012-08-09 12:59:40 -0700399 trace_clock_disable(name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 if (clk->ops->disable)
401 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700402 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 clk_disable(parent);
404 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700405 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406out:
407 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700408}
409EXPORT_SYMBOL(clk_disable);
410
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800411void clk_unprepare(struct clk *clk)
412{
Stephen Boyd64dce902012-08-09 12:59:40 -0700413 const char *name = clk ? clk->dbg_name : NULL;
414
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700415 if (IS_ERR_OR_NULL(clk))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800416 return;
417
418 mutex_lock(&clk->prepare_lock);
Stephen Boyd64dce902012-08-09 12:59:40 -0700419 if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800420 goto out;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800421 if (clk->prepare_count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700422 struct clk *parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800423
Stephen Boyd64dce902012-08-09 12:59:40 -0700424 WARN(clk->count,
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800425 "%s: Don't call unprepare when the clock is enabled\n",
Stephen Boyd64dce902012-08-09 12:59:40 -0700426 name);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800427
428 if (clk->ops->unprepare)
429 clk->ops->unprepare(clk);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800430 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800431 clk_unprepare(clk->depends);
432 clk_unprepare(parent);
433 }
434 clk->prepare_count--;
435out:
436 mutex_unlock(&clk->prepare_lock);
437}
438EXPORT_SYMBOL(clk_unprepare);
439
Daniel Walker5e96da52010-05-12 13:43:28 -0700440int clk_reset(struct clk *clk, enum clk_reset_action action)
441{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700442 if (IS_ERR_OR_NULL(clk))
443 return -EINVAL;
444
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 if (!clk->ops->reset)
446 return -ENOSYS;
447
448 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700449}
450EXPORT_SYMBOL(clk_reset);
451
Brian Swetland600f7cf2008-09-09 11:04:14 -0700452unsigned long clk_get_rate(struct clk *clk)
453{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700454 if (IS_ERR_OR_NULL(clk))
455 return 0;
456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800458 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459
460 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700461}
462EXPORT_SYMBOL(clk_get_rate);
463
Matt Wagantall77952c42011-11-08 18:45:48 -0800464int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700465{
Stephen Boydd86d1f22012-01-24 17:36:34 -0800466 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700467 int rc = 0;
Stephen Boyd64dce902012-08-09 12:59:40 -0700468 const char *name = clk ? clk->dbg_name : NULL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700469
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700470 if (IS_ERR_OR_NULL(clk))
471 return -EINVAL;
472
Matt Wagantall77952c42011-11-08 18:45:48 -0800473 if (!clk->ops->set_rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 return -ENOSYS;
Daniel Walker3a790bb2010-12-13 14:35:10 -0800475
Saravana Kannana8c91542013-03-18 21:15:18 -0700476 if (!is_rate_valid(clk, rate))
477 return -EINVAL;
478
Stephen Boydd86d1f22012-01-24 17:36:34 -0800479 mutex_lock(&clk->prepare_lock);
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700480
481 /* Return early if the rate isn't going to change */
Vikram Mulukutla0090eb12013-05-15 19:57:18 -0700482 if (clk->rate == rate && !(clk->flags & CLKFLAG_NO_RATE_CACHE))
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700483 goto out;
484
Stephen Boyd64dce902012-08-09 12:59:40 -0700485 trace_clock_set_rate(name, rate, raw_smp_processor_id());
Saravana Kannana8c91542013-03-18 21:15:18 -0700486
487 start_rate = clk->rate;
488
Saravana Kannan4867ef42013-03-20 21:22:05 -0700489 if (clk->ops->pre_set_rate)
490 rc = clk->ops->pre_set_rate(clk, rate);
491 if (rc)
492 goto out;
493
Saravana Kannana8c91542013-03-18 21:15:18 -0700494 /* Enforce vdd requirements for target frequency. */
Stephen Boydd86d1f22012-01-24 17:36:34 -0800495 if (clk->prepare_count) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700496 rc = vote_rate_vdd(clk, rate);
497 if (rc)
Saravana Kannan4867ef42013-03-20 21:22:05 -0700498 goto err_vote_vdd;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700499 }
Matt Wagantall7205eea2011-11-04 17:31:29 -0700500
Saravana Kannana8c91542013-03-18 21:15:18 -0700501 rc = clk->ops->set_rate(clk, rate);
502 if (rc)
503 goto err_set_rate;
504 clk->rate = rate;
505
506 /* Release vdd requirements for starting frequency. */
507 if (clk->prepare_count)
508 unvote_rate_vdd(clk, start_rate);
509
Saravana Kannan4867ef42013-03-20 21:22:05 -0700510 if (clk->ops->post_set_rate)
511 clk->ops->post_set_rate(clk, start_rate);
512
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700513out:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800514 mutex_unlock(&clk->prepare_lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700515 return rc;
516
517err_set_rate:
Saravana Kannana8c91542013-03-18 21:15:18 -0700518 if (clk->prepare_count)
519 unvote_rate_vdd(clk, rate);
Saravana Kannan4867ef42013-03-20 21:22:05 -0700520err_vote_vdd:
521 /* clk->rate is still the old rate. So, pass the new rate instead. */
522 if (clk->ops->post_set_rate)
523 clk->ops->post_set_rate(clk, rate);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800524 goto out;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700525}
Matt Wagantall77952c42011-11-08 18:45:48 -0800526EXPORT_SYMBOL(clk_set_rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700527
Daniel Walker5e96da52010-05-12 13:43:28 -0700528long clk_round_rate(struct clk *clk, unsigned long rate)
529{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700530 if (IS_ERR_OR_NULL(clk))
531 return -EINVAL;
532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 if (!clk->ops->round_rate)
534 return -ENOSYS;
535
536 return clk->ops->round_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700537}
538EXPORT_SYMBOL(clk_round_rate);
539
Daniel Walker5e96da52010-05-12 13:43:28 -0700540int clk_set_max_rate(struct clk *clk, unsigned long rate)
541{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700542 if (IS_ERR_OR_NULL(clk))
543 return -EINVAL;
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 if (!clk->ops->set_max_rate)
546 return -ENOSYS;
547
548 return clk->ops->set_max_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700549}
550EXPORT_SYMBOL(clk_set_max_rate);
551
Brian Swetland600f7cf2008-09-09 11:04:14 -0700552int clk_set_parent(struct clk *clk, struct clk *parent)
553{
Saravana Kannan776bdfd2013-03-18 20:08:28 -0700554 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555
Saravana Kannan776bdfd2013-03-18 20:08:28 -0700556 if (!clk->ops->set_parent)
557 return -ENOSYS;
558
559 mutex_lock(&clk->prepare_lock);
560 if (clk->parent == parent)
561 goto out;
562 rc = clk->ops->set_parent(clk, parent);
563 if (!rc)
564 clk->parent = parent;
565out:
566 mutex_unlock(&clk->prepare_lock);
567
568 return rc;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700569}
570EXPORT_SYMBOL(clk_set_parent);
571
572struct clk *clk_get_parent(struct clk *clk)
573{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700574 if (IS_ERR_OR_NULL(clk))
575 return NULL;
576
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700577 return clk->parent;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700578}
579EXPORT_SYMBOL(clk_get_parent);
580
581int clk_set_flags(struct clk *clk, unsigned long flags)
582{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700583 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700584 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 if (!clk->ops->set_flags)
586 return -ENOSYS;
587
588 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700589}
590EXPORT_SYMBOL(clk_set_flags);
591
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800592static LIST_HEAD(initdata_list);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800593
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700594static void init_sibling_lists(struct clk_lookup *clock_tbl, size_t num_clocks)
595{
596 struct clk *clk, *parent;
597 unsigned n;
598
599 for (n = 0; n < num_clocks; n++) {
600 clk = clock_tbl[n].clk;
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700601 parent = clk->parent;
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700602 if (parent && list_empty(&clk->siblings))
603 list_add(&clk->siblings, &parent->children);
604 }
605}
606
Patrick Daly5ce9da32013-03-26 13:12:53 -0700607static void vdd_class_init(struct clk_vdd_class *vdd)
608{
609 struct handoff_vdd *v;
610 int i;
611
612 if (!vdd)
613 return;
614
615 list_for_each_entry(v, &handoff_vdd_list, list) {
616 if (v->vdd_class == vdd)
617 return;
618 }
619
620 pr_debug("voting for vdd_class %s\n", vdd->class_name);
621 if (vote_vdd_level(vdd, vdd->num_levels - 1))
622 pr_err("failed to vote for %s\n", vdd->class_name);
623
624 for (i = 0; i < vdd->num_regulators; i++)
625 regulator_enable(vdd->regulator[i]);
626
627 v = kmalloc(sizeof(*v), GFP_KERNEL);
628 if (!v) {
629 pr_err("Unable to kmalloc. %s will be stuck at max.\n",
630 vdd->class_name);
631 return;
632 }
633
634 v->vdd_class = vdd;
635 list_add_tail(&v->list, &handoff_vdd_list);
636}
637
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800638static int __handoff_clk(struct clk *clk)
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700639{
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800640 enum handoff state = HANDOFF_DISABLED_CLK;
641 struct handoff_clk *h = NULL;
642 int rc;
643
644 if (clk == NULL || clk->flags & CLKFLAG_INIT_DONE ||
645 clk->flags & CLKFLAG_SKIP_HANDOFF)
646 return 0;
647
648 if (clk->flags & CLKFLAG_INIT_ERR)
649 return -ENXIO;
650
651 /* Handoff any 'depends' clock first. */
652 rc = __handoff_clk(clk->depends);
653 if (rc)
654 goto err;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700655
656 /*
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800657 * Handoff functions for the parent must be called before the
658 * children can be handed off. Without handing off the parents and
659 * knowing their rate and state (on/off), it's impossible to figure
660 * out the rate and state of the children.
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700661 */
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800662 if (clk->ops->get_parent)
663 clk->parent = clk->ops->get_parent(clk);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700664
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800665 if (IS_ERR(clk->parent)) {
666 rc = PTR_ERR(clk->parent);
667 goto err;
668 }
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700669
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800670 rc = __handoff_clk(clk->parent);
671 if (rc)
672 goto err;
673
674 if (clk->ops->handoff)
675 state = clk->ops->handoff(clk);
676
677 if (state == HANDOFF_ENABLED_CLK) {
678
679 h = kmalloc(sizeof(*h), GFP_KERNEL);
680 if (!h) {
681 rc = -ENOMEM;
682 goto err;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700683 }
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800684
685 rc = clk_prepare_enable(clk->parent);
686 if (rc)
687 goto err;
688
689 rc = clk_prepare_enable(clk->depends);
690 if (rc)
691 goto err_depends;
692
693 rc = vote_rate_vdd(clk, clk->rate);
694 WARN(rc, "%s unable to vote for voltage!\n", clk->dbg_name);
695
696 clk->count = 1;
697 clk->prepare_count = 1;
698 h->clk = clk;
699 list_add_tail(&h->list, &handoff_list);
700
701 pr_debug("Handed off %s rate=%lu\n", clk->dbg_name, clk->rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700702 }
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800703
704 clk->flags |= CLKFLAG_INIT_DONE;
705
706 return 0;
707
708err_depends:
709 clk_disable_unprepare(clk->parent);
710err:
711 kfree(h);
712 clk->flags |= CLKFLAG_INIT_ERR;
713 pr_err("%s handoff failed (%d)\n", clk->dbg_name, rc);
714 return rc;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700715}
716
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800717/**
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800718 * msm_clock_register() - Register additional clock tables
719 * @table: Table of clocks
720 * @size: Size of @table
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800721 *
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800722 * Upon return, clock APIs may be used to control clocks registered using this
723 * function.
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800724 */
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800725int msm_clock_register(struct clk_lookup *table, size_t size)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700726{
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800727 int n = 0;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700728
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800729 mutex_lock(&msm_clock_init_lock);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800730
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800731 init_sibling_lists(table, size);
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700732
Matt Wagantallb37fea42012-04-04 16:47:23 -0700733 /*
Patrick Daly5ce9da32013-03-26 13:12:53 -0700734 * Enable regulators and temporarily set them up at maximum voltage.
735 * Once all the clocks have made their respective vote, remove this
736 * temporary vote. The removing of the temporary vote is done at
737 * late_init, by which time we assume all the clocks would have been
738 * handed off.
739 */
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800740 for (n = 0; n < size; n++)
741 vdd_class_init(table[n].clk->vdd_class);
Patrick Daly5ce9da32013-03-26 13:12:53 -0700742
743 /*
Matt Wagantallb37fea42012-04-04 16:47:23 -0700744 * Detect and preserve initial clock state until clock_late_init() or
745 * a driver explicitly changes it, whichever is first.
746 */
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800747 for (n = 0; n < size; n++)
748 __handoff_clk(table[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700749
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800750 clkdev_add_table(table, size);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700751
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800752 clock_debug_register(table, size);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800753
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800754 mutex_unlock(&msm_clock_init_lock);
755
756 return 0;
757}
758EXPORT_SYMBOL(msm_clock_register);
759
760/**
761 * msm_clock_init() - Register and initialize a clock driver
762 * @data: Driver-specific clock initialization data
763 *
764 * Upon return from this call, clock APIs may be used to control
765 * clocks registered with this API.
766 */
767int __init msm_clock_init(struct clock_init_data *data)
768{
769 if (!data)
770 return -EINVAL;
771
772 if (data->pre_init)
773 data->pre_init();
774
775 mutex_lock(&msm_clock_init_lock);
776 if (data->late_init)
777 list_add(&data->list, &initdata_list);
778 mutex_unlock(&msm_clock_init_lock);
779
780 msm_clock_register(data->table, data->size);
781
782 if (data->post_init)
783 data->post_init();
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700784
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800785 return 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700786}
787
Brian Swetland600f7cf2008-09-09 11:04:14 -0700788static int __init clock_late_init(void)
789{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700790 struct handoff_clk *h, *h_temp;
Patrick Daly5ce9da32013-03-26 13:12:53 -0700791 struct handoff_vdd *v, *v_temp;
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800792 struct clock_init_data *initdata, *initdata_temp;
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800793 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794
Matt Wagantall647d1c12012-05-16 14:32:14 -0700795 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800796
797 mutex_lock(&msm_clock_init_lock);
798
799 list_for_each_entry_safe(initdata, initdata_temp,
800 &initdata_list, list) {
801 ret = initdata->late_init();
802 if (ret)
803 pr_err("%s: %pS failed late_init.\n", __func__,
804 initdata);
805 }
806
Matt Wagantall158f73b2012-05-16 11:29:35 -0700807 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
808 clk_disable_unprepare(h->clk);
809 list_del(&h->list);
810 kfree(h);
811 }
812
Patrick Daly5ce9da32013-03-26 13:12:53 -0700813 list_for_each_entry_safe(v, v_temp, &handoff_vdd_list, list) {
814 unvote_vdd_level(v->vdd_class, v->vdd_class->num_levels - 1);
815 list_del(&v->list);
816 kfree(v);
817 }
818
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800819 mutex_unlock(&msm_clock_init_lock);
820
Stephen Boydbb600ae2011-08-02 20:11:40 -0700821 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700822}
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800823/* clock_late_init should run only after all deferred probing
824 * (excluding DLKM probes) has completed.
825 */
826late_initcall_sync(clock_late_init);