blob: 527d73dc9e5e4650b504dd1587434f97e2fc6413 [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 Daly653c0b52013-04-16 17:18:28 -070093 if (rc < 0)
94 goto set_mode_fail;
Patrick Dalyebc26bc2013-02-05 11:49:07 -080095 }
96 if (vdd_class->set_vdd && !vdd_class->num_regulators)
97 rc = vdd_class->set_vdd(vdd_class, level);
98
Patrick Daly653c0b52013-04-16 17:18:28 -070099 if (rc < 0)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700100 vdd_class->cur_level = level;
101
Patrick Daly653c0b52013-04-16 17:18:28 -0700102 return 0;
103
104set_mode_fail:
Junjie Wubb5a79e2013-05-15 13:12:39 -0700105 regulator_set_voltage(r[i], uv[vdd_class->cur_level * n_reg + i],
106 uv[max_lvl + i]);
Patrick Dalyebc26bc2013-02-05 11:49:07 -0800107
108set_voltage_fail:
Junjie Wubb5a79e2013-05-15 13:12:39 -0700109 lvl_base = vdd_class->cur_level * n_reg;
Patrick Daly653c0b52013-04-16 17:18:28 -0700110 for (i--; i >= 0; i--) {
Junjie Wubb5a79e2013-05-15 13:12:39 -0700111 regulator_set_voltage(r[i], uv[lvl_base + i], uv[max_lvl + i]);
Patrick Daly653c0b52013-04-16 17:18:28 -0700112
Junjie Wubb5a79e2013-05-15 13:12:39 -0700113 if (!ua)
Patrick Daly653c0b52013-04-16 17:18:28 -0700114 continue;
Junjie Wubb5a79e2013-05-15 13:12:39 -0700115 regulator_set_optimum_mode(r[i], ua[lvl_base + i]);
Patrick Daly653c0b52013-04-16 17:18:28 -0700116 }
Patrick Dalyebc26bc2013-02-05 11:49:07 -0800117
118 return rc;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700119}
120
121/* Vote for a voltage level. */
122int vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
123{
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700124 int rc;
125
Saravana Kannan55e959d2012-10-15 22:16:04 -0700126 if (level >= vdd_class->num_levels)
127 return -EINVAL;
128
Stephen Boyda1f610a2012-09-22 00:40:37 -0700129 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700130 vdd_class->level_votes[level]++;
131 rc = update_vdd(vdd_class);
132 if (rc)
133 vdd_class->level_votes[level]--;
Stephen Boyda1f610a2012-09-22 00:40:37 -0700134 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700135
136 return rc;
137}
138
139/* Remove vote for a voltage level. */
140int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
141{
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700142 int rc = 0;
143
Saravana Kannan55e959d2012-10-15 22:16:04 -0700144 if (level >= vdd_class->num_levels)
145 return -EINVAL;
146
Stephen Boyda1f610a2012-09-22 00:40:37 -0700147 mutex_lock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700148 if (WARN(!vdd_class->level_votes[level],
149 "Reference counts are incorrect for %s level %d\n",
150 vdd_class->class_name, level))
151 goto out;
152 vdd_class->level_votes[level]--;
153 rc = update_vdd(vdd_class);
154 if (rc)
155 vdd_class->level_votes[level]++;
156out:
Stephen Boyda1f610a2012-09-22 00:40:37 -0700157 mutex_unlock(&vdd_class->lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700158 return rc;
159}
160
161/* Vote for a voltage level corresponding to a clock's rate. */
162static int vote_rate_vdd(struct clk *clk, unsigned long rate)
163{
164 int level;
165
166 if (!clk->vdd_class)
167 return 0;
168
169 level = find_vdd_level(clk, rate);
170 if (level < 0)
171 return level;
172
173 return vote_vdd_level(clk->vdd_class, level);
174}
175
176/* Remove vote for a voltage level corresponding to a clock's rate. */
177static void unvote_rate_vdd(struct clk *clk, unsigned long rate)
178{
179 int level;
180
181 if (!clk->vdd_class)
182 return;
183
184 level = find_vdd_level(clk, rate);
185 if (level < 0)
186 return;
187
188 unvote_vdd_level(clk->vdd_class, level);
189}
190
Saravana Kannana8c91542013-03-18 21:15:18 -0700191/* Check if the rate is within the voltage limits of the clock. */
Patrick Dalyc009d9e2012-10-01 11:55:27 -0700192static bool is_rate_valid(struct clk *clk, unsigned long rate)
193{
194 int level;
195
196 if (!clk->vdd_class)
197 return true;
198
199 level = find_vdd_level(clk, rate);
200 return level >= 0;
201}
202
Saravana Kannan33c6a202013-03-20 22:19:10 -0700203/**
204 * __clk_pre_reparent() - Set up the new parent before switching to it and
205 * prevent the enable state of the child clock from changing.
206 * @c: The child clock that's going to switch parents
207 * @new: The new parent that the child clock is going to switch to
208 * @flags: Pointer to scratch space to save spinlock flags
209 *
210 * Cannot be called from atomic context.
211 *
212 * Use this API to set up the @new parent clock to be able to support the
213 * current prepare and enable state of the child clock @c. Once the parent is
214 * set up, the child clock can safely switch to it.
215 *
216 * The caller shall grab the prepare_lock of clock @c before calling this API
217 * and only release it after calling __clk_post_reparent() for clock @c (or
218 * if this API fails). This is necessary to prevent the prepare state of the
219 * child clock @c from changing while the reparenting is in progress. Since
220 * this API takes care of grabbing the enable lock of @c, only atomic
221 * operation are allowed between calls to __clk_pre_reparent and
222 * __clk_post_reparent()
223 *
224 * The scratch space pointed to by @flags should not be altered before
225 * calling __clk_post_reparent() for clock @c.
226 *
227 * See also: __clk_post_reparent()
228 */
229int __clk_pre_reparent(struct clk *c, struct clk *new, unsigned long *flags)
230{
231 int rc;
232
233 if (c->prepare_count) {
234 rc = clk_prepare(new);
235 if (rc)
236 return rc;
237 }
238
239 spin_lock_irqsave(&c->lock, *flags);
240 if (c->count) {
241 rc = clk_enable(new);
242 if (rc) {
243 spin_unlock_irqrestore(&c->lock, *flags);
244 clk_unprepare(new);
245 return rc;
246 }
247 }
248 return 0;
249}
250
251/**
252 * __clk_post_reparent() - Release requirements on old parent after switching
253 * away from it and allow changes to the child clock's enable state.
254 * @c: The child clock that switched parents
255 * @old: The old parent that the child clock switched away from or the new
256 * parent of a failed reparent attempt.
257 * @flags: Pointer to scratch space where spinlock flags were saved
258 *
259 * Cannot be called from atomic context.
260 *
261 * This API works in tandem with __clk_pre_reparent. Use this API to
262 * - Remove prepare and enable requirements from the @old parent after
263 * switching away from it
264 * - Or, undo the effects of __clk_pre_reparent() after a failed attempt to
265 * change parents
266 *
267 * The caller shall release the prepare_lock of @c that was grabbed before
268 * calling __clk_pre_reparent() only after this API is called (or if
269 * __clk_pre_reparent() fails). This is necessary to prevent the prepare
270 * state of the child clock @c from changing while the reparenting is in
271 * progress. Since this API releases the enable lock of @c, the limit to
272 * atomic operations set by __clk_pre_reparent() is no longer present.
273 *
274 * The scratch space pointed to by @flags shall not be altered since the call
275 * to __clk_pre_reparent() for clock @c.
276 *
277 * See also: __clk_pre_reparent()
278 */
279void __clk_post_reparent(struct clk *c, struct clk *old, unsigned long *flags)
280{
281 if (c->count)
282 clk_disable(old);
283 spin_unlock_irqrestore(&c->lock, *flags);
284
285 if (c->prepare_count)
286 clk_unprepare(old);
287}
288
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800289int clk_prepare(struct clk *clk)
290{
291 int ret = 0;
292 struct clk *parent;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700293
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800294 if (!clk)
295 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700296 if (IS_ERR(clk))
297 return -EINVAL;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800298
299 mutex_lock(&clk->prepare_lock);
300 if (clk->prepare_count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700301 parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800302
303 ret = clk_prepare(parent);
304 if (ret)
305 goto out;
306 ret = clk_prepare(clk->depends);
307 if (ret)
308 goto err_prepare_depends;
309
Stephen Boydd86d1f22012-01-24 17:36:34 -0800310 ret = vote_rate_vdd(clk, clk->rate);
311 if (ret)
312 goto err_vote_vdd;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800313 if (clk->ops->prepare)
314 ret = clk->ops->prepare(clk);
315 if (ret)
316 goto err_prepare_clock;
317 }
318 clk->prepare_count++;
319out:
320 mutex_unlock(&clk->prepare_lock);
321 return ret;
322err_prepare_clock:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800323 unvote_rate_vdd(clk, clk->rate);
324err_vote_vdd:
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800325 clk_unprepare(clk->depends);
326err_prepare_depends:
327 clk_unprepare(parent);
328 goto out;
329}
330EXPORT_SYMBOL(clk_prepare);
331
Brian Swetland600f7cf2008-09-09 11:04:14 -0700332/*
Brian Swetland600f7cf2008-09-09 11:04:14 -0700333 * Standard clock functions defined in include/linux/clk.h
334 */
Brian Swetland600f7cf2008-09-09 11:04:14 -0700335int clk_enable(struct clk *clk)
336{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 int ret = 0;
Matt Wagantall7205eea2011-11-04 17:31:29 -0700338 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 struct clk *parent;
Stephen Boyd64dce902012-08-09 12:59:40 -0700340 const char *name = clk ? clk->dbg_name : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341
342 if (!clk)
343 return 0;
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700344 if (IS_ERR(clk))
345 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346
347 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700348 WARN(!clk->prepare_count,
349 "%s: Don't call enable on unprepared clocks\n", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 if (clk->count == 0) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700351 parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700352
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 ret = clk_enable(parent);
354 if (ret)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700355 goto err_enable_parent;
Stephen Boyd7fa26742011-08-11 23:22:29 -0700356 ret = clk_enable(clk->depends);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700357 if (ret)
358 goto err_enable_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359
Stephen Boyd64dce902012-08-09 12:59:40 -0700360 trace_clock_enable(name, 1, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 if (clk->ops->enable)
362 ret = clk->ops->enable(clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700363 if (ret)
364 goto err_enable_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700366 clk->count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 spin_unlock_irqrestore(&clk->lock, flags);
368
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700369 return 0;
370
371err_enable_clock:
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700372 clk_disable(clk->depends);
373err_enable_depends:
374 clk_disable(parent);
375err_enable_parent:
376 spin_unlock_irqrestore(&clk->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700378}
379EXPORT_SYMBOL(clk_enable);
380
381void clk_disable(struct clk *clk)
382{
Stephen Boyd64dce902012-08-09 12:59:40 -0700383 const char *name = clk ? clk->dbg_name : NULL;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700384 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700386 if (IS_ERR_OR_NULL(clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 return;
388
389 spin_lock_irqsave(&clk->lock, flags);
Stephen Boyd64dce902012-08-09 12:59:40 -0700390 WARN(!clk->prepare_count,
391 "%s: Never called prepare or calling disable after unprepare\n",
392 name);
393 if (WARN(clk->count == 0, "%s is unbalanced", name))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 goto out;
395 if (clk->count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700396 struct clk *parent = clk->parent;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700397
Stephen Boyd64dce902012-08-09 12:59:40 -0700398 trace_clock_disable(name, 0, smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 if (clk->ops->disable)
400 clk->ops->disable(clk);
Stephen Boyd7fa26742011-08-11 23:22:29 -0700401 clk_disable(clk->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 clk_disable(parent);
403 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700404 clk->count--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405out:
406 spin_unlock_irqrestore(&clk->lock, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700407}
408EXPORT_SYMBOL(clk_disable);
409
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800410void clk_unprepare(struct clk *clk)
411{
Stephen Boyd64dce902012-08-09 12:59:40 -0700412 const char *name = clk ? clk->dbg_name : NULL;
413
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700414 if (IS_ERR_OR_NULL(clk))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800415 return;
416
417 mutex_lock(&clk->prepare_lock);
Stephen Boyd64dce902012-08-09 12:59:40 -0700418 if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name))
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800419 goto out;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800420 if (clk->prepare_count == 1) {
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700421 struct clk *parent = clk->parent;
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800422
Stephen Boyd64dce902012-08-09 12:59:40 -0700423 WARN(clk->count,
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800424 "%s: Don't call unprepare when the clock is enabled\n",
Stephen Boyd64dce902012-08-09 12:59:40 -0700425 name);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800426
427 if (clk->ops->unprepare)
428 clk->ops->unprepare(clk);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800429 unvote_rate_vdd(clk, clk->rate);
Stephen Boyd3bbf3462012-01-12 00:19:23 -0800430 clk_unprepare(clk->depends);
431 clk_unprepare(parent);
432 }
433 clk->prepare_count--;
434out:
435 mutex_unlock(&clk->prepare_lock);
436}
437EXPORT_SYMBOL(clk_unprepare);
438
Daniel Walker5e96da52010-05-12 13:43:28 -0700439int clk_reset(struct clk *clk, enum clk_reset_action action)
440{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700441 if (IS_ERR_OR_NULL(clk))
442 return -EINVAL;
443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 if (!clk->ops->reset)
445 return -ENOSYS;
446
447 return clk->ops->reset(clk, action);
Daniel Walker5e96da52010-05-12 13:43:28 -0700448}
449EXPORT_SYMBOL(clk_reset);
450
Brian Swetland600f7cf2008-09-09 11:04:14 -0700451unsigned long clk_get_rate(struct clk *clk)
452{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700453 if (IS_ERR_OR_NULL(clk))
454 return 0;
455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 if (!clk->ops->get_rate)
Tianyi Gou7949ecb2012-02-14 14:25:32 -0800457 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
459 return clk->ops->get_rate(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700460}
461EXPORT_SYMBOL(clk_get_rate);
462
Matt Wagantall77952c42011-11-08 18:45:48 -0800463int clk_set_rate(struct clk *clk, unsigned long rate)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700464{
Stephen Boydd86d1f22012-01-24 17:36:34 -0800465 unsigned long start_rate;
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700466 int rc = 0;
Stephen Boyd64dce902012-08-09 12:59:40 -0700467 const char *name = clk ? clk->dbg_name : NULL;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700468
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700469 if (IS_ERR_OR_NULL(clk))
470 return -EINVAL;
471
Matt Wagantall77952c42011-11-08 18:45:48 -0800472 if (!clk->ops->set_rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 return -ENOSYS;
Daniel Walker3a790bb2010-12-13 14:35:10 -0800474
Saravana Kannana8c91542013-03-18 21:15:18 -0700475 if (!is_rate_valid(clk, rate))
476 return -EINVAL;
477
Stephen Boydd86d1f22012-01-24 17:36:34 -0800478 mutex_lock(&clk->prepare_lock);
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700479
480 /* Return early if the rate isn't going to change */
Vikram Mulukutla0090eb12013-05-15 19:57:18 -0700481 if (clk->rate == rate && !(clk->flags & CLKFLAG_NO_RATE_CACHE))
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700482 goto out;
483
Stephen Boyd64dce902012-08-09 12:59:40 -0700484 trace_clock_set_rate(name, rate, raw_smp_processor_id());
Saravana Kannana8c91542013-03-18 21:15:18 -0700485
486 start_rate = clk->rate;
487
Saravana Kannan4867ef42013-03-20 21:22:05 -0700488 if (clk->ops->pre_set_rate)
489 rc = clk->ops->pre_set_rate(clk, rate);
490 if (rc)
491 goto out;
492
Saravana Kannana8c91542013-03-18 21:15:18 -0700493 /* Enforce vdd requirements for target frequency. */
Stephen Boydd86d1f22012-01-24 17:36:34 -0800494 if (clk->prepare_count) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700495 rc = vote_rate_vdd(clk, rate);
496 if (rc)
Saravana Kannan4867ef42013-03-20 21:22:05 -0700497 goto err_vote_vdd;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700498 }
Matt Wagantall7205eea2011-11-04 17:31:29 -0700499
Saravana Kannana8c91542013-03-18 21:15:18 -0700500 rc = clk->ops->set_rate(clk, rate);
501 if (rc)
502 goto err_set_rate;
503 clk->rate = rate;
504
505 /* Release vdd requirements for starting frequency. */
506 if (clk->prepare_count)
507 unvote_rate_vdd(clk, start_rate);
508
Saravana Kannan4867ef42013-03-20 21:22:05 -0700509 if (clk->ops->post_set_rate)
510 clk->ops->post_set_rate(clk, start_rate);
511
Stephen Boyd4fefefc2012-04-13 13:37:46 -0700512out:
Stephen Boydd86d1f22012-01-24 17:36:34 -0800513 mutex_unlock(&clk->prepare_lock);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700514 return rc;
515
516err_set_rate:
Saravana Kannana8c91542013-03-18 21:15:18 -0700517 if (clk->prepare_count)
518 unvote_rate_vdd(clk, rate);
Saravana Kannan4867ef42013-03-20 21:22:05 -0700519err_vote_vdd:
520 /* clk->rate is still the old rate. So, pass the new rate instead. */
521 if (clk->ops->post_set_rate)
522 clk->ops->post_set_rate(clk, rate);
Stephen Boydd86d1f22012-01-24 17:36:34 -0800523 goto out;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700524}
Matt Wagantall77952c42011-11-08 18:45:48 -0800525EXPORT_SYMBOL(clk_set_rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700526
Daniel Walker5e96da52010-05-12 13:43:28 -0700527long clk_round_rate(struct clk *clk, unsigned long rate)
528{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700529 if (IS_ERR_OR_NULL(clk))
530 return -EINVAL;
531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 if (!clk->ops->round_rate)
533 return -ENOSYS;
534
535 return clk->ops->round_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700536}
537EXPORT_SYMBOL(clk_round_rate);
538
Daniel Walker5e96da52010-05-12 13:43:28 -0700539int clk_set_max_rate(struct clk *clk, unsigned long rate)
540{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700541 if (IS_ERR_OR_NULL(clk))
542 return -EINVAL;
543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 if (!clk->ops->set_max_rate)
545 return -ENOSYS;
546
547 return clk->ops->set_max_rate(clk, rate);
Daniel Walker5e96da52010-05-12 13:43:28 -0700548}
549EXPORT_SYMBOL(clk_set_max_rate);
550
Brian Swetland600f7cf2008-09-09 11:04:14 -0700551int clk_set_parent(struct clk *clk, struct clk *parent)
552{
Saravana Kannan776bdfd2013-03-18 20:08:28 -0700553 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554
Saravana Kannan776bdfd2013-03-18 20:08:28 -0700555 if (!clk->ops->set_parent)
556 return -ENOSYS;
557
558 mutex_lock(&clk->prepare_lock);
559 if (clk->parent == parent)
560 goto out;
561 rc = clk->ops->set_parent(clk, parent);
562 if (!rc)
563 clk->parent = parent;
564out:
565 mutex_unlock(&clk->prepare_lock);
566
567 return rc;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700568}
569EXPORT_SYMBOL(clk_set_parent);
570
571struct clk *clk_get_parent(struct clk *clk)
572{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700573 if (IS_ERR_OR_NULL(clk))
574 return NULL;
575
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700576 return clk->parent;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700577}
578EXPORT_SYMBOL(clk_get_parent);
579
580int clk_set_flags(struct clk *clk, unsigned long flags)
581{
Vikram Mulukutla55e8f992012-04-17 19:25:10 -0700582 if (IS_ERR_OR_NULL(clk))
Brian Swetland600f7cf2008-09-09 11:04:14 -0700583 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 if (!clk->ops->set_flags)
585 return -ENOSYS;
586
587 return clk->ops->set_flags(clk, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700588}
589EXPORT_SYMBOL(clk_set_flags);
590
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800591static LIST_HEAD(initdata_list);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800592
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700593static void init_sibling_lists(struct clk_lookup *clock_tbl, size_t num_clocks)
594{
595 struct clk *clk, *parent;
596 unsigned n;
597
598 for (n = 0; n < num_clocks; n++) {
599 clk = clock_tbl[n].clk;
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700600 parent = clk->parent;
Matt Wagantall20f8e0f2012-09-26 17:28:54 -0700601 if (parent && list_empty(&clk->siblings))
602 list_add(&clk->siblings, &parent->children);
603 }
604}
605
Patrick Daly5ce9da32013-03-26 13:12:53 -0700606static void vdd_class_init(struct clk_vdd_class *vdd)
607{
608 struct handoff_vdd *v;
609 int i;
610
611 if (!vdd)
612 return;
613
614 list_for_each_entry(v, &handoff_vdd_list, list) {
615 if (v->vdd_class == vdd)
616 return;
617 }
618
619 pr_debug("voting for vdd_class %s\n", vdd->class_name);
620 if (vote_vdd_level(vdd, vdd->num_levels - 1))
621 pr_err("failed to vote for %s\n", vdd->class_name);
622
623 for (i = 0; i < vdd->num_regulators; i++)
624 regulator_enable(vdd->regulator[i]);
625
626 v = kmalloc(sizeof(*v), GFP_KERNEL);
627 if (!v) {
628 pr_err("Unable to kmalloc. %s will be stuck at max.\n",
629 vdd->class_name);
630 return;
631 }
632
633 v->vdd_class = vdd;
634 list_add_tail(&v->list, &handoff_vdd_list);
635}
636
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800637static int __handoff_clk(struct clk *clk)
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700638{
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800639 enum handoff state = HANDOFF_DISABLED_CLK;
640 struct handoff_clk *h = NULL;
641 int rc;
642
643 if (clk == NULL || clk->flags & CLKFLAG_INIT_DONE ||
644 clk->flags & CLKFLAG_SKIP_HANDOFF)
645 return 0;
646
647 if (clk->flags & CLKFLAG_INIT_ERR)
648 return -ENXIO;
649
650 /* Handoff any 'depends' clock first. */
651 rc = __handoff_clk(clk->depends);
652 if (rc)
653 goto err;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700654
655 /*
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800656 * Handoff functions for the parent must be called before the
657 * children can be handed off. Without handing off the parents and
658 * knowing their rate and state (on/off), it's impossible to figure
659 * out the rate and state of the children.
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700660 */
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800661 if (clk->ops->get_parent)
662 clk->parent = clk->ops->get_parent(clk);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700663
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800664 if (IS_ERR(clk->parent)) {
665 rc = PTR_ERR(clk->parent);
666 goto err;
667 }
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700668
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800669 rc = __handoff_clk(clk->parent);
670 if (rc)
671 goto err;
672
673 if (clk->ops->handoff)
674 state = clk->ops->handoff(clk);
675
676 if (state == HANDOFF_ENABLED_CLK) {
677
678 h = kmalloc(sizeof(*h), GFP_KERNEL);
679 if (!h) {
680 rc = -ENOMEM;
681 goto err;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700682 }
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800683
684 rc = clk_prepare_enable(clk->parent);
685 if (rc)
686 goto err;
687
688 rc = clk_prepare_enable(clk->depends);
689 if (rc)
690 goto err_depends;
691
692 rc = vote_rate_vdd(clk, clk->rate);
693 WARN(rc, "%s unable to vote for voltage!\n", clk->dbg_name);
694
695 clk->count = 1;
696 clk->prepare_count = 1;
697 h->clk = clk;
698 list_add_tail(&h->list, &handoff_list);
699
700 pr_debug("Handed off %s rate=%lu\n", clk->dbg_name, clk->rate);
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700701 }
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800702
703 clk->flags |= CLKFLAG_INIT_DONE;
704
705 return 0;
706
707err_depends:
708 clk_disable_unprepare(clk->parent);
709err:
710 kfree(h);
711 clk->flags |= CLKFLAG_INIT_ERR;
712 pr_err("%s handoff failed (%d)\n", clk->dbg_name, rc);
713 return rc;
Matt Wagantallf30fceb2012-06-12 19:13:11 -0700714}
715
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800716/**
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800717 * msm_clock_register() - Register additional clock tables
718 * @table: Table of clocks
719 * @size: Size of @table
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800720 *
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800721 * Upon return, clock APIs may be used to control clocks registered using this
722 * function.
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800723 */
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800724int msm_clock_register(struct clk_lookup *table, size_t size)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700725{
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800726 int n = 0;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700727
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800728 mutex_lock(&msm_clock_init_lock);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800729
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800730 init_sibling_lists(table, size);
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700731
Matt Wagantallb37fea42012-04-04 16:47:23 -0700732 /*
Patrick Daly5ce9da32013-03-26 13:12:53 -0700733 * Enable regulators and temporarily set them up at maximum voltage.
734 * Once all the clocks have made their respective vote, remove this
735 * temporary vote. The removing of the temporary vote is done at
736 * late_init, by which time we assume all the clocks would have been
737 * handed off.
738 */
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800739 for (n = 0; n < size; n++)
740 vdd_class_init(table[n].clk->vdd_class);
Patrick Daly5ce9da32013-03-26 13:12:53 -0700741
742 /*
Matt Wagantallb37fea42012-04-04 16:47:23 -0700743 * Detect and preserve initial clock state until clock_late_init() or
744 * a driver explicitly changes it, whichever is first.
745 */
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800746 for (n = 0; n < size; n++)
747 __handoff_clk(table[n].clk);
Daniel Walker5e96da52010-05-12 13:43:28 -0700748
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800749 clkdev_add_table(table, size);
Matt Wagantallb64888f2012-04-02 21:35:07 -0700750
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800751 clock_debug_register(table, size);
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800752
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800753 mutex_unlock(&msm_clock_init_lock);
754
755 return 0;
756}
757EXPORT_SYMBOL(msm_clock_register);
758
759/**
760 * msm_clock_init() - Register and initialize a clock driver
761 * @data: Driver-specific clock initialization data
762 *
763 * Upon return from this call, clock APIs may be used to control
764 * clocks registered with this API.
765 */
766int __init msm_clock_init(struct clock_init_data *data)
767{
768 if (!data)
769 return -EINVAL;
770
771 if (data->pre_init)
772 data->pre_init();
773
774 mutex_lock(&msm_clock_init_lock);
775 if (data->late_init)
776 list_add(&data->list, &initdata_list);
777 mutex_unlock(&msm_clock_init_lock);
778
779 msm_clock_register(data->table, data->size);
780
781 if (data->post_init)
782 data->post_init();
Matt Wagantallfba2c5b2012-10-18 12:54:15 -0700783
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800784 return 0;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700785}
786
Brian Swetland600f7cf2008-09-09 11:04:14 -0700787static int __init clock_late_init(void)
788{
Matt Wagantall158f73b2012-05-16 11:29:35 -0700789 struct handoff_clk *h, *h_temp;
Patrick Daly5ce9da32013-03-26 13:12:53 -0700790 struct handoff_vdd *v, *v_temp;
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800791 struct clock_init_data *initdata, *initdata_temp;
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800792 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
Matt Wagantall647d1c12012-05-16 14:32:14 -0700794 pr_info("%s: Removing enables held for handed-off clocks\n", __func__);
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800795
796 mutex_lock(&msm_clock_init_lock);
797
798 list_for_each_entry_safe(initdata, initdata_temp,
799 &initdata_list, list) {
800 ret = initdata->late_init();
801 if (ret)
802 pr_err("%s: %pS failed late_init.\n", __func__,
803 initdata);
804 }
805
Matt Wagantall158f73b2012-05-16 11:29:35 -0700806 list_for_each_entry_safe(h, h_temp, &handoff_list, list) {
807 clk_disable_unprepare(h->clk);
808 list_del(&h->list);
809 kfree(h);
810 }
811
Patrick Daly5ce9da32013-03-26 13:12:53 -0700812 list_for_each_entry_safe(v, v_temp, &handoff_vdd_list, list) {
813 unvote_vdd_level(v->vdd_class, v->vdd_class->num_levels - 1);
814 list_del(&v->list);
815 kfree(v);
816 }
817
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800818 mutex_unlock(&msm_clock_init_lock);
819
Stephen Boydbb600ae2011-08-02 20:11:40 -0700820 return ret;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700821}
Vikram Mulukutla4785ab62012-12-10 20:51:22 -0800822/* clock_late_init should run only after all deferred probing
823 * (excluding DLKM probes) has completed.
824 */
825late_initcall_sync(clock_late_init);