Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/clock.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Google, Inc. |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 4 | * Copyright (c) 2007-2013, The Linux Foundation. All rights reserved. |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 5 | * |
| 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 Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/clk.h> |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 23 | #include <linux/clkdev.h> |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 24 | #include <linux/list.h> |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 25 | #include <linux/regulator/consumer.h> |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 27 | #include <trace/events/power.h> |
Matt Wagantall | 33d01f5 | 2012-02-23 23:27:44 -0800 | [diff] [blame] | 28 | #include <mach/clk-provider.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 29 | #include "clock.h" |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 30 | |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 31 | struct handoff_clk { |
| 32 | struct list_head list; |
| 33 | struct clk *clk; |
| 34 | }; |
| 35 | static LIST_HEAD(handoff_list); |
| 36 | |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 37 | struct handoff_vdd { |
| 38 | struct list_head list; |
| 39 | struct clk_vdd_class *vdd_class; |
| 40 | }; |
| 41 | static LIST_HEAD(handoff_vdd_list); |
| 42 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 43 | static DEFINE_MUTEX(msm_clock_init_lock); |
| 44 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 45 | /* Find the voltage level required for a given rate. */ |
Patrick Daly | 0a78a0e | 2012-07-23 13:18:59 -0700 | [diff] [blame] | 46 | int find_vdd_level(struct clk *clk, unsigned long rate) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 47 | { |
| 48 | int level; |
| 49 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 50 | for (level = 0; level < clk->num_fmax; level++) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 51 | if (rate <= clk->fmax[level]) |
| 52 | break; |
| 53 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 54 | if (level == clk->num_fmax) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 55 | 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. */ |
| 64 | static int update_vdd(struct clk_vdd_class *vdd_class) |
| 65 | { |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 66 | int level, rc = 0, i; |
| 67 | struct regulator **r = vdd_class->regulator; |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 68 | 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 Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 73 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 74 | for (level = max_lvl; level > 0; level--) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 75 | if (vdd_class->level_votes[level]) |
| 76 | break; |
| 77 | |
| 78 | if (level == vdd_class->cur_level) |
| 79 | return 0; |
| 80 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 81 | max_lvl = max_lvl * n_reg; |
| 82 | lvl_base = level * n_reg; |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 83 | for (i = 0; i < vdd_class->num_regulators; i++) { |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 84 | rc = regulator_set_voltage(r[i], uv[lvl_base + i], |
| 85 | uv[max_lvl + i]); |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 86 | if (rc) |
| 87 | goto set_voltage_fail; |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 88 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 89 | if (!ua) |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 90 | continue; |
| 91 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 92 | rc = regulator_set_optimum_mode(r[i], ua[lvl_base + i]); |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 93 | if (rc < 0) |
| 94 | goto set_mode_fail; |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 95 | } |
| 96 | if (vdd_class->set_vdd && !vdd_class->num_regulators) |
| 97 | rc = vdd_class->set_vdd(vdd_class, level); |
| 98 | |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 99 | if (rc < 0) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 100 | vdd_class->cur_level = level; |
| 101 | |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | |
| 104 | set_mode_fail: |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 105 | regulator_set_voltage(r[i], uv[vdd_class->cur_level * n_reg + i], |
| 106 | uv[max_lvl + i]); |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 107 | |
| 108 | set_voltage_fail: |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 109 | lvl_base = vdd_class->cur_level * n_reg; |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 110 | for (i--; i >= 0; i--) { |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 111 | regulator_set_voltage(r[i], uv[lvl_base + i], uv[max_lvl + i]); |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 112 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 113 | if (!ua) |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 114 | continue; |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame] | 115 | regulator_set_optimum_mode(r[i], ua[lvl_base + i]); |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 116 | } |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 117 | |
| 118 | return rc; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* Vote for a voltage level. */ |
| 122 | int vote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 123 | { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 124 | int rc; |
| 125 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 126 | if (level >= vdd_class->num_levels) |
| 127 | return -EINVAL; |
| 128 | |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 129 | mutex_lock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 130 | vdd_class->level_votes[level]++; |
| 131 | rc = update_vdd(vdd_class); |
| 132 | if (rc) |
| 133 | vdd_class->level_votes[level]--; |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 134 | mutex_unlock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 135 | |
| 136 | return rc; |
| 137 | } |
| 138 | |
| 139 | /* Remove vote for a voltage level. */ |
| 140 | int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 141 | { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 142 | int rc = 0; |
| 143 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 144 | if (level >= vdd_class->num_levels) |
| 145 | return -EINVAL; |
| 146 | |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 147 | mutex_lock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 148 | 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]++; |
| 156 | out: |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 157 | mutex_unlock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | /* Vote for a voltage level corresponding to a clock's rate. */ |
| 162 | static 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. */ |
| 177 | static 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 Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 191 | /* Check if the rate is within the voltage limits of the clock. */ |
Patrick Daly | c009d9e | 2012-10-01 11:55:27 -0700 | [diff] [blame] | 192 | static 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 Kannan | 33c6a20 | 2013-03-20 22:19:10 -0700 | [diff] [blame] | 203 | /** |
| 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 | */ |
| 229 | int __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 | */ |
| 279 | void __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 Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 289 | int clk_prepare(struct clk *clk) |
| 290 | { |
| 291 | int ret = 0; |
| 292 | struct clk *parent; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 293 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 294 | if (!clk) |
| 295 | return 0; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 296 | if (IS_ERR(clk)) |
| 297 | return -EINVAL; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 298 | |
| 299 | mutex_lock(&clk->prepare_lock); |
| 300 | if (clk->prepare_count == 0) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 301 | parent = clk->parent; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 302 | |
| 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 Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 310 | ret = vote_rate_vdd(clk, clk->rate); |
| 311 | if (ret) |
| 312 | goto err_vote_vdd; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 313 | if (clk->ops->prepare) |
| 314 | ret = clk->ops->prepare(clk); |
| 315 | if (ret) |
| 316 | goto err_prepare_clock; |
| 317 | } |
| 318 | clk->prepare_count++; |
| 319 | out: |
| 320 | mutex_unlock(&clk->prepare_lock); |
| 321 | return ret; |
| 322 | err_prepare_clock: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 323 | unvote_rate_vdd(clk, clk->rate); |
| 324 | err_vote_vdd: |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 325 | clk_unprepare(clk->depends); |
| 326 | err_prepare_depends: |
| 327 | clk_unprepare(parent); |
| 328 | goto out; |
| 329 | } |
| 330 | EXPORT_SYMBOL(clk_prepare); |
| 331 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 332 | /* |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 333 | * Standard clock functions defined in include/linux/clk.h |
| 334 | */ |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 335 | int clk_enable(struct clk *clk) |
| 336 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 337 | int ret = 0; |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 338 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | struct clk *parent; |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 340 | const char *name = clk ? clk->dbg_name : NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 341 | |
| 342 | if (!clk) |
| 343 | return 0; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 344 | if (IS_ERR(clk)) |
| 345 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 346 | |
| 347 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 348 | WARN(!clk->prepare_count, |
| 349 | "%s: Don't call enable on unprepared clocks\n", name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | if (clk->count == 0) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 351 | parent = clk->parent; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 352 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 353 | ret = clk_enable(parent); |
| 354 | if (ret) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 355 | goto err_enable_parent; |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 356 | ret = clk_enable(clk->depends); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 357 | if (ret) |
| 358 | goto err_enable_depends; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 359 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 360 | trace_clock_enable(name, 1, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 361 | if (clk->ops->enable) |
| 362 | ret = clk->ops->enable(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 363 | if (ret) |
| 364 | goto err_enable_clock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 365 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 366 | clk->count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 367 | spin_unlock_irqrestore(&clk->lock, flags); |
| 368 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 369 | return 0; |
| 370 | |
| 371 | err_enable_clock: |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 372 | clk_disable(clk->depends); |
| 373 | err_enable_depends: |
| 374 | clk_disable(parent); |
| 375 | err_enable_parent: |
| 376 | spin_unlock_irqrestore(&clk->lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 377 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 378 | } |
| 379 | EXPORT_SYMBOL(clk_enable); |
| 380 | |
| 381 | void clk_disable(struct clk *clk) |
| 382 | { |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 383 | const char *name = clk ? clk->dbg_name : NULL; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 384 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 385 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 386 | if (IS_ERR_OR_NULL(clk)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 387 | return; |
| 388 | |
| 389 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 390 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 394 | goto out; |
| 395 | if (clk->count == 1) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 396 | struct clk *parent = clk->parent; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 397 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 398 | trace_clock_disable(name, 0, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | if (clk->ops->disable) |
| 400 | clk->ops->disable(clk); |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 401 | clk_disable(clk->depends); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | clk_disable(parent); |
| 403 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 404 | clk->count--; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 405 | out: |
| 406 | spin_unlock_irqrestore(&clk->lock, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 407 | } |
| 408 | EXPORT_SYMBOL(clk_disable); |
| 409 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 410 | void clk_unprepare(struct clk *clk) |
| 411 | { |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 412 | const char *name = clk ? clk->dbg_name : NULL; |
| 413 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 414 | if (IS_ERR_OR_NULL(clk)) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 415 | return; |
| 416 | |
| 417 | mutex_lock(&clk->prepare_lock); |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 418 | if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name)) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 419 | goto out; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 420 | if (clk->prepare_count == 1) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 421 | struct clk *parent = clk->parent; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 422 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 423 | WARN(clk->count, |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 424 | "%s: Don't call unprepare when the clock is enabled\n", |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 425 | name); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 426 | |
| 427 | if (clk->ops->unprepare) |
| 428 | clk->ops->unprepare(clk); |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 429 | unvote_rate_vdd(clk, clk->rate); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 430 | clk_unprepare(clk->depends); |
| 431 | clk_unprepare(parent); |
| 432 | } |
| 433 | clk->prepare_count--; |
| 434 | out: |
| 435 | mutex_unlock(&clk->prepare_lock); |
| 436 | } |
| 437 | EXPORT_SYMBOL(clk_unprepare); |
| 438 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 439 | int clk_reset(struct clk *clk, enum clk_reset_action action) |
| 440 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 441 | if (IS_ERR_OR_NULL(clk)) |
| 442 | return -EINVAL; |
| 443 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 444 | if (!clk->ops->reset) |
| 445 | return -ENOSYS; |
| 446 | |
| 447 | return clk->ops->reset(clk, action); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 448 | } |
| 449 | EXPORT_SYMBOL(clk_reset); |
| 450 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 451 | unsigned long clk_get_rate(struct clk *clk) |
| 452 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 453 | if (IS_ERR_OR_NULL(clk)) |
| 454 | return 0; |
| 455 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 456 | if (!clk->ops->get_rate) |
Tianyi Gou | 7949ecb | 2012-02-14 14:25:32 -0800 | [diff] [blame] | 457 | return clk->rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 458 | |
| 459 | return clk->ops->get_rate(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 460 | } |
| 461 | EXPORT_SYMBOL(clk_get_rate); |
| 462 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 463 | int clk_set_rate(struct clk *clk, unsigned long rate) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 464 | { |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 465 | unsigned long start_rate; |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 466 | int rc = 0; |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 467 | const char *name = clk ? clk->dbg_name : NULL; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 468 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 469 | if (IS_ERR_OR_NULL(clk)) |
| 470 | return -EINVAL; |
| 471 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 472 | if (!clk->ops->set_rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 473 | return -ENOSYS; |
Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 474 | |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 475 | if (!is_rate_valid(clk, rate)) |
| 476 | return -EINVAL; |
| 477 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 478 | mutex_lock(&clk->prepare_lock); |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 479 | |
| 480 | /* Return early if the rate isn't going to change */ |
Vikram Mulukutla | 0090eb1 | 2013-05-15 19:57:18 -0700 | [diff] [blame] | 481 | if (clk->rate == rate && !(clk->flags & CLKFLAG_NO_RATE_CACHE)) |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 482 | goto out; |
| 483 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 484 | trace_clock_set_rate(name, rate, raw_smp_processor_id()); |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 485 | |
| 486 | start_rate = clk->rate; |
| 487 | |
Saravana Kannan | 4867ef4 | 2013-03-20 21:22:05 -0700 | [diff] [blame] | 488 | if (clk->ops->pre_set_rate) |
| 489 | rc = clk->ops->pre_set_rate(clk, rate); |
| 490 | if (rc) |
| 491 | goto out; |
| 492 | |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 493 | /* Enforce vdd requirements for target frequency. */ |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 494 | if (clk->prepare_count) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 495 | rc = vote_rate_vdd(clk, rate); |
| 496 | if (rc) |
Saravana Kannan | 4867ef4 | 2013-03-20 21:22:05 -0700 | [diff] [blame] | 497 | goto err_vote_vdd; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 498 | } |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 499 | |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 500 | 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 Kannan | 4867ef4 | 2013-03-20 21:22:05 -0700 | [diff] [blame] | 509 | if (clk->ops->post_set_rate) |
| 510 | clk->ops->post_set_rate(clk, start_rate); |
| 511 | |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 512 | out: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 513 | mutex_unlock(&clk->prepare_lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 514 | return rc; |
| 515 | |
| 516 | err_set_rate: |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 517 | if (clk->prepare_count) |
| 518 | unvote_rate_vdd(clk, rate); |
Saravana Kannan | 4867ef4 | 2013-03-20 21:22:05 -0700 | [diff] [blame] | 519 | err_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 Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 523 | goto out; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 524 | } |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 525 | EXPORT_SYMBOL(clk_set_rate); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 526 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 527 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 528 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 529 | if (IS_ERR_OR_NULL(clk)) |
| 530 | return -EINVAL; |
| 531 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 532 | if (!clk->ops->round_rate) |
| 533 | return -ENOSYS; |
| 534 | |
| 535 | return clk->ops->round_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 536 | } |
| 537 | EXPORT_SYMBOL(clk_round_rate); |
| 538 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 539 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 540 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 541 | if (IS_ERR_OR_NULL(clk)) |
| 542 | return -EINVAL; |
| 543 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 544 | if (!clk->ops->set_max_rate) |
| 545 | return -ENOSYS; |
| 546 | |
| 547 | return clk->ops->set_max_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 548 | } |
| 549 | EXPORT_SYMBOL(clk_set_max_rate); |
| 550 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 551 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 552 | { |
Saravana Kannan | 776bdfd | 2013-03-18 20:08:28 -0700 | [diff] [blame] | 553 | int rc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 554 | |
Saravana Kannan | 776bdfd | 2013-03-18 20:08:28 -0700 | [diff] [blame] | 555 | 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; |
| 564 | out: |
| 565 | mutex_unlock(&clk->prepare_lock); |
| 566 | |
| 567 | return rc; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 568 | } |
| 569 | EXPORT_SYMBOL(clk_set_parent); |
| 570 | |
| 571 | struct clk *clk_get_parent(struct clk *clk) |
| 572 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 573 | if (IS_ERR_OR_NULL(clk)) |
| 574 | return NULL; |
| 575 | |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 576 | return clk->parent; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 577 | } |
| 578 | EXPORT_SYMBOL(clk_get_parent); |
| 579 | |
| 580 | int clk_set_flags(struct clk *clk, unsigned long flags) |
| 581 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 582 | if (IS_ERR_OR_NULL(clk)) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 583 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 584 | if (!clk->ops->set_flags) |
| 585 | return -ENOSYS; |
| 586 | |
| 587 | return clk->ops->set_flags(clk, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 588 | } |
| 589 | EXPORT_SYMBOL(clk_set_flags); |
| 590 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 591 | static LIST_HEAD(initdata_list); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 592 | |
Matt Wagantall | 20f8e0f | 2012-09-26 17:28:54 -0700 | [diff] [blame] | 593 | static 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 Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 600 | parent = clk->parent; |
Matt Wagantall | 20f8e0f | 2012-09-26 17:28:54 -0700 | [diff] [blame] | 601 | if (parent && list_empty(&clk->siblings)) |
| 602 | list_add(&clk->siblings, &parent->children); |
| 603 | } |
| 604 | } |
| 605 | |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 606 | static 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 Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 637 | static int __handoff_clk(struct clk *clk) |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 638 | { |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 639 | 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 Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 654 | |
| 655 | /* |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 656 | * 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 Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 660 | */ |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 661 | if (clk->ops->get_parent) |
| 662 | clk->parent = clk->ops->get_parent(clk); |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 663 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 664 | if (IS_ERR(clk->parent)) { |
| 665 | rc = PTR_ERR(clk->parent); |
| 666 | goto err; |
| 667 | } |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 668 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 669 | 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 Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 682 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 683 | |
| 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 Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 701 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 702 | |
| 703 | clk->flags |= CLKFLAG_INIT_DONE; |
| 704 | |
| 705 | return 0; |
| 706 | |
| 707 | err_depends: |
| 708 | clk_disable_unprepare(clk->parent); |
| 709 | err: |
| 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 Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 716 | /** |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 717 | * msm_clock_register() - Register additional clock tables |
| 718 | * @table: Table of clocks |
| 719 | * @size: Size of @table |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 720 | * |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 721 | * Upon return, clock APIs may be used to control clocks registered using this |
| 722 | * function. |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 723 | */ |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 724 | int msm_clock_register(struct clk_lookup *table, size_t size) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 725 | { |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 726 | int n = 0; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 727 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 728 | mutex_lock(&msm_clock_init_lock); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 729 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 730 | init_sibling_lists(table, size); |
Matt Wagantall | fba2c5b | 2012-10-18 12:54:15 -0700 | [diff] [blame] | 731 | |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 732 | /* |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 733 | * 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 Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 739 | for (n = 0; n < size; n++) |
| 740 | vdd_class_init(table[n].clk->vdd_class); |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 741 | |
| 742 | /* |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 743 | * Detect and preserve initial clock state until clock_late_init() or |
| 744 | * a driver explicitly changes it, whichever is first. |
| 745 | */ |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 746 | for (n = 0; n < size; n++) |
| 747 | __handoff_clk(table[n].clk); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 748 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 749 | clkdev_add_table(table, size); |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 750 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 751 | clock_debug_register(table, size); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 752 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 753 | mutex_unlock(&msm_clock_init_lock); |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | EXPORT_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 | */ |
| 766 | int __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 Wagantall | fba2c5b | 2012-10-18 12:54:15 -0700 | [diff] [blame] | 783 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 784 | return 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 787 | static int __init clock_late_init(void) |
| 788 | { |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 789 | struct handoff_clk *h, *h_temp; |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 790 | struct handoff_vdd *v, *v_temp; |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 791 | struct clock_init_data *initdata, *initdata_temp; |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 792 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | |
Matt Wagantall | 647d1c1 | 2012-05-16 14:32:14 -0700 | [diff] [blame] | 794 | pr_info("%s: Removing enables held for handed-off clocks\n", __func__); |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 795 | |
| 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 Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 806 | 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 Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 812 | 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 Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 818 | mutex_unlock(&msm_clock_init_lock); |
| 819 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 820 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 821 | } |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 822 | /* clock_late_init should run only after all deferred probing |
| 823 | * (excluding DLKM probes) has completed. |
| 824 | */ |
| 825 | late_initcall_sync(clock_late_init); |