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