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> |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 26 | #include <trace/events/power.h> |
Matt Wagantall | 33d01f5 | 2012-02-23 23:27:44 -0800 | [diff] [blame] | 27 | #include <mach/clk-provider.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 28 | #include "clock.h" |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 29 | |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 30 | struct handoff_clk { |
| 31 | struct list_head list; |
| 32 | struct clk *clk; |
| 33 | }; |
| 34 | static LIST_HEAD(handoff_list); |
| 35 | |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 36 | struct handoff_vdd { |
| 37 | struct list_head list; |
| 38 | struct clk_vdd_class *vdd_class; |
| 39 | }; |
| 40 | static LIST_HEAD(handoff_vdd_list); |
| 41 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 42 | /* Find the voltage level required for a given rate. */ |
Patrick Daly | 0a78a0e | 2012-07-23 13:18:59 -0700 | [diff] [blame] | 43 | int find_vdd_level(struct clk *clk, unsigned long rate) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 44 | { |
| 45 | int level; |
| 46 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 47 | for (level = 0; level < clk->num_fmax; level++) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 48 | if (rate <= clk->fmax[level]) |
| 49 | break; |
| 50 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 51 | if (level == clk->num_fmax) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 52 | pr_err("Rate %lu for %s is greater than highest Fmax\n", rate, |
| 53 | clk->dbg_name); |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | |
| 57 | return level; |
| 58 | } |
| 59 | |
| 60 | /* Update voltage level given the current votes. */ |
| 61 | static int update_vdd(struct clk_vdd_class *vdd_class) |
| 62 | { |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 63 | int level, rc = 0, i; |
| 64 | struct regulator **r = vdd_class->regulator; |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 65 | int *uv = vdd_class->vdd_uv; |
| 66 | int *ua = vdd_class->vdd_ua; |
| 67 | int n_reg = vdd_class->num_regulators; |
| 68 | int max_lvl = vdd_class->num_levels - 1; |
| 69 | int lvl_base; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 70 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 71 | for (level = max_lvl; level > 0; level--) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 72 | if (vdd_class->level_votes[level]) |
| 73 | break; |
| 74 | |
| 75 | if (level == vdd_class->cur_level) |
| 76 | return 0; |
| 77 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 78 | max_lvl = max_lvl * n_reg; |
| 79 | lvl_base = level * n_reg; |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 80 | for (i = 0; i < vdd_class->num_regulators; i++) { |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 81 | rc = regulator_set_voltage(r[i], uv[lvl_base + i], |
| 82 | uv[max_lvl + i]); |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 83 | if (rc) |
| 84 | goto set_voltage_fail; |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 85 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 86 | if (!ua) |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 87 | continue; |
| 88 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 89 | rc = regulator_set_optimum_mode(r[i], ua[lvl_base + i]); |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 90 | if (rc < 0) |
| 91 | goto set_mode_fail; |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 92 | } |
| 93 | if (vdd_class->set_vdd && !vdd_class->num_regulators) |
| 94 | rc = vdd_class->set_vdd(vdd_class, level); |
| 95 | |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 96 | if (rc < 0) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 97 | vdd_class->cur_level = level; |
| 98 | |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | |
| 101 | set_mode_fail: |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 102 | regulator_set_voltage(r[i], uv[vdd_class->cur_level * n_reg + i], |
| 103 | uv[max_lvl + i]); |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 104 | |
| 105 | set_voltage_fail: |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 106 | lvl_base = vdd_class->cur_level * n_reg; |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 107 | for (i--; i >= 0; i--) { |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 108 | 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] | 109 | |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 110 | if (!ua) |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 111 | continue; |
Junjie Wu | bb5a79e | 2013-05-15 13:12:39 -0700 | [diff] [blame^] | 112 | regulator_set_optimum_mode(r[i], ua[lvl_base + i]); |
Patrick Daly | 653c0b5 | 2013-04-16 17:18:28 -0700 | [diff] [blame] | 113 | } |
Patrick Daly | ebc26bc | 2013-02-05 11:49:07 -0800 | [diff] [blame] | 114 | |
| 115 | return rc; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* Vote for a voltage level. */ |
| 119 | int vote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 120 | { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 121 | int rc; |
| 122 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 123 | if (level >= vdd_class->num_levels) |
| 124 | return -EINVAL; |
| 125 | |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 126 | mutex_lock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 127 | vdd_class->level_votes[level]++; |
| 128 | rc = update_vdd(vdd_class); |
| 129 | if (rc) |
| 130 | vdd_class->level_votes[level]--; |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 131 | mutex_unlock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 132 | |
| 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | /* Remove vote for a voltage level. */ |
| 137 | int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 138 | { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 139 | int rc = 0; |
| 140 | |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 141 | if (level >= vdd_class->num_levels) |
| 142 | return -EINVAL; |
| 143 | |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 144 | mutex_lock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 145 | if (WARN(!vdd_class->level_votes[level], |
| 146 | "Reference counts are incorrect for %s level %d\n", |
| 147 | vdd_class->class_name, level)) |
| 148 | goto out; |
| 149 | vdd_class->level_votes[level]--; |
| 150 | rc = update_vdd(vdd_class); |
| 151 | if (rc) |
| 152 | vdd_class->level_votes[level]++; |
| 153 | out: |
Stephen Boyd | a1f610a | 2012-09-22 00:40:37 -0700 | [diff] [blame] | 154 | mutex_unlock(&vdd_class->lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 155 | return rc; |
| 156 | } |
| 157 | |
| 158 | /* Vote for a voltage level corresponding to a clock's rate. */ |
| 159 | static int vote_rate_vdd(struct clk *clk, unsigned long rate) |
| 160 | { |
| 161 | int level; |
| 162 | |
| 163 | if (!clk->vdd_class) |
| 164 | return 0; |
| 165 | |
| 166 | level = find_vdd_level(clk, rate); |
| 167 | if (level < 0) |
| 168 | return level; |
| 169 | |
| 170 | return vote_vdd_level(clk->vdd_class, level); |
| 171 | } |
| 172 | |
| 173 | /* Remove vote for a voltage level corresponding to a clock's rate. */ |
| 174 | static void unvote_rate_vdd(struct clk *clk, unsigned long rate) |
| 175 | { |
| 176 | int level; |
| 177 | |
| 178 | if (!clk->vdd_class) |
| 179 | return; |
| 180 | |
| 181 | level = find_vdd_level(clk, rate); |
| 182 | if (level < 0) |
| 183 | return; |
| 184 | |
| 185 | unvote_vdd_level(clk->vdd_class, level); |
| 186 | } |
| 187 | |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 188 | /* Check if the rate is within the voltage limits of the clock. */ |
Patrick Daly | c009d9e | 2012-10-01 11:55:27 -0700 | [diff] [blame] | 189 | static bool is_rate_valid(struct clk *clk, unsigned long rate) |
| 190 | { |
| 191 | int level; |
| 192 | |
| 193 | if (!clk->vdd_class) |
| 194 | return true; |
| 195 | |
| 196 | level = find_vdd_level(clk, rate); |
| 197 | return level >= 0; |
| 198 | } |
| 199 | |
Saravana Kannan | 33c6a20 | 2013-03-20 22:19:10 -0700 | [diff] [blame] | 200 | /** |
| 201 | * __clk_pre_reparent() - Set up the new parent before switching to it and |
| 202 | * prevent the enable state of the child clock from changing. |
| 203 | * @c: The child clock that's going to switch parents |
| 204 | * @new: The new parent that the child clock is going to switch to |
| 205 | * @flags: Pointer to scratch space to save spinlock flags |
| 206 | * |
| 207 | * Cannot be called from atomic context. |
| 208 | * |
| 209 | * Use this API to set up the @new parent clock to be able to support the |
| 210 | * current prepare and enable state of the child clock @c. Once the parent is |
| 211 | * set up, the child clock can safely switch to it. |
| 212 | * |
| 213 | * The caller shall grab the prepare_lock of clock @c before calling this API |
| 214 | * and only release it after calling __clk_post_reparent() for clock @c (or |
| 215 | * if this API fails). This is necessary to prevent the prepare state of the |
| 216 | * child clock @c from changing while the reparenting is in progress. Since |
| 217 | * this API takes care of grabbing the enable lock of @c, only atomic |
| 218 | * operation are allowed between calls to __clk_pre_reparent and |
| 219 | * __clk_post_reparent() |
| 220 | * |
| 221 | * The scratch space pointed to by @flags should not be altered before |
| 222 | * calling __clk_post_reparent() for clock @c. |
| 223 | * |
| 224 | * See also: __clk_post_reparent() |
| 225 | */ |
| 226 | int __clk_pre_reparent(struct clk *c, struct clk *new, unsigned long *flags) |
| 227 | { |
| 228 | int rc; |
| 229 | |
| 230 | if (c->prepare_count) { |
| 231 | rc = clk_prepare(new); |
| 232 | if (rc) |
| 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | spin_lock_irqsave(&c->lock, *flags); |
| 237 | if (c->count) { |
| 238 | rc = clk_enable(new); |
| 239 | if (rc) { |
| 240 | spin_unlock_irqrestore(&c->lock, *flags); |
| 241 | clk_unprepare(new); |
| 242 | return rc; |
| 243 | } |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * __clk_post_reparent() - Release requirements on old parent after switching |
| 250 | * away from it and allow changes to the child clock's enable state. |
| 251 | * @c: The child clock that switched parents |
| 252 | * @old: The old parent that the child clock switched away from or the new |
| 253 | * parent of a failed reparent attempt. |
| 254 | * @flags: Pointer to scratch space where spinlock flags were saved |
| 255 | * |
| 256 | * Cannot be called from atomic context. |
| 257 | * |
| 258 | * This API works in tandem with __clk_pre_reparent. Use this API to |
| 259 | * - Remove prepare and enable requirements from the @old parent after |
| 260 | * switching away from it |
| 261 | * - Or, undo the effects of __clk_pre_reparent() after a failed attempt to |
| 262 | * change parents |
| 263 | * |
| 264 | * The caller shall release the prepare_lock of @c that was grabbed before |
| 265 | * calling __clk_pre_reparent() only after this API is called (or if |
| 266 | * __clk_pre_reparent() fails). This is necessary to prevent the prepare |
| 267 | * state of the child clock @c from changing while the reparenting is in |
| 268 | * progress. Since this API releases the enable lock of @c, the limit to |
| 269 | * atomic operations set by __clk_pre_reparent() is no longer present. |
| 270 | * |
| 271 | * The scratch space pointed to by @flags shall not be altered since the call |
| 272 | * to __clk_pre_reparent() for clock @c. |
| 273 | * |
| 274 | * See also: __clk_pre_reparent() |
| 275 | */ |
| 276 | void __clk_post_reparent(struct clk *c, struct clk *old, unsigned long *flags) |
| 277 | { |
| 278 | if (c->count) |
| 279 | clk_disable(old); |
| 280 | spin_unlock_irqrestore(&c->lock, *flags); |
| 281 | |
| 282 | if (c->prepare_count) |
| 283 | clk_unprepare(old); |
| 284 | } |
| 285 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 286 | int clk_prepare(struct clk *clk) |
| 287 | { |
| 288 | int ret = 0; |
| 289 | struct clk *parent; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 290 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 291 | if (!clk) |
| 292 | return 0; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 293 | if (IS_ERR(clk)) |
| 294 | return -EINVAL; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 295 | |
| 296 | mutex_lock(&clk->prepare_lock); |
| 297 | if (clk->prepare_count == 0) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 298 | parent = clk->parent; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 299 | |
| 300 | ret = clk_prepare(parent); |
| 301 | if (ret) |
| 302 | goto out; |
| 303 | ret = clk_prepare(clk->depends); |
| 304 | if (ret) |
| 305 | goto err_prepare_depends; |
| 306 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 307 | ret = vote_rate_vdd(clk, clk->rate); |
| 308 | if (ret) |
| 309 | goto err_vote_vdd; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 310 | if (clk->ops->prepare) |
| 311 | ret = clk->ops->prepare(clk); |
| 312 | if (ret) |
| 313 | goto err_prepare_clock; |
| 314 | } |
| 315 | clk->prepare_count++; |
| 316 | out: |
| 317 | mutex_unlock(&clk->prepare_lock); |
| 318 | return ret; |
| 319 | err_prepare_clock: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 320 | unvote_rate_vdd(clk, clk->rate); |
| 321 | err_vote_vdd: |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 322 | clk_unprepare(clk->depends); |
| 323 | err_prepare_depends: |
| 324 | clk_unprepare(parent); |
| 325 | goto out; |
| 326 | } |
| 327 | EXPORT_SYMBOL(clk_prepare); |
| 328 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 329 | /* |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 330 | * Standard clock functions defined in include/linux/clk.h |
| 331 | */ |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 332 | int clk_enable(struct clk *clk) |
| 333 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 334 | int ret = 0; |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 335 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 336 | struct clk *parent; |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 337 | const char *name = clk ? clk->dbg_name : NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 338 | |
| 339 | if (!clk) |
| 340 | return 0; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 341 | if (IS_ERR(clk)) |
| 342 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 343 | |
| 344 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 345 | WARN(!clk->prepare_count, |
| 346 | "%s: Don't call enable on unprepared clocks\n", name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | if (clk->count == 0) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 348 | parent = clk->parent; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 349 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | ret = clk_enable(parent); |
| 351 | if (ret) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 352 | goto err_enable_parent; |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 353 | ret = clk_enable(clk->depends); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 354 | if (ret) |
| 355 | goto err_enable_depends; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 356 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 357 | trace_clock_enable(name, 1, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 358 | if (clk->ops->enable) |
| 359 | ret = clk->ops->enable(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 360 | if (ret) |
| 361 | goto err_enable_clock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 362 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 363 | clk->count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 364 | spin_unlock_irqrestore(&clk->lock, flags); |
| 365 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 366 | return 0; |
| 367 | |
| 368 | err_enable_clock: |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 369 | clk_disable(clk->depends); |
| 370 | err_enable_depends: |
| 371 | clk_disable(parent); |
| 372 | err_enable_parent: |
| 373 | spin_unlock_irqrestore(&clk->lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 374 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 375 | } |
| 376 | EXPORT_SYMBOL(clk_enable); |
| 377 | |
| 378 | void clk_disable(struct clk *clk) |
| 379 | { |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 380 | const char *name = clk ? clk->dbg_name : NULL; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 381 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 383 | if (IS_ERR_OR_NULL(clk)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | return; |
| 385 | |
| 386 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 387 | WARN(!clk->prepare_count, |
| 388 | "%s: Never called prepare or calling disable after unprepare\n", |
| 389 | name); |
| 390 | if (WARN(clk->count == 0, "%s is unbalanced", name)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 391 | goto out; |
| 392 | if (clk->count == 1) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 393 | struct clk *parent = clk->parent; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 394 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 395 | trace_clock_disable(name, 0, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 396 | if (clk->ops->disable) |
| 397 | clk->ops->disable(clk); |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 398 | clk_disable(clk->depends); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | clk_disable(parent); |
| 400 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 401 | clk->count--; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | out: |
| 403 | spin_unlock_irqrestore(&clk->lock, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 404 | } |
| 405 | EXPORT_SYMBOL(clk_disable); |
| 406 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 407 | void clk_unprepare(struct clk *clk) |
| 408 | { |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 409 | const char *name = clk ? clk->dbg_name : NULL; |
| 410 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 411 | if (IS_ERR_OR_NULL(clk)) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 412 | return; |
| 413 | |
| 414 | mutex_lock(&clk->prepare_lock); |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 415 | if (WARN(!clk->prepare_count, "%s is unbalanced (prepare)", name)) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 416 | goto out; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 417 | if (clk->prepare_count == 1) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 418 | struct clk *parent = clk->parent; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 419 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 420 | WARN(clk->count, |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 421 | "%s: Don't call unprepare when the clock is enabled\n", |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 422 | name); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 423 | |
| 424 | if (clk->ops->unprepare) |
| 425 | clk->ops->unprepare(clk); |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 426 | unvote_rate_vdd(clk, clk->rate); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 427 | clk_unprepare(clk->depends); |
| 428 | clk_unprepare(parent); |
| 429 | } |
| 430 | clk->prepare_count--; |
| 431 | out: |
| 432 | mutex_unlock(&clk->prepare_lock); |
| 433 | } |
| 434 | EXPORT_SYMBOL(clk_unprepare); |
| 435 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 436 | int clk_reset(struct clk *clk, enum clk_reset_action action) |
| 437 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 438 | if (IS_ERR_OR_NULL(clk)) |
| 439 | return -EINVAL; |
| 440 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 441 | if (!clk->ops->reset) |
| 442 | return -ENOSYS; |
| 443 | |
| 444 | return clk->ops->reset(clk, action); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 445 | } |
| 446 | EXPORT_SYMBOL(clk_reset); |
| 447 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 448 | unsigned long clk_get_rate(struct clk *clk) |
| 449 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 450 | if (IS_ERR_OR_NULL(clk)) |
| 451 | return 0; |
| 452 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 453 | if (!clk->ops->get_rate) |
Tianyi Gou | 7949ecb | 2012-02-14 14:25:32 -0800 | [diff] [blame] | 454 | return clk->rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 455 | |
| 456 | return clk->ops->get_rate(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 457 | } |
| 458 | EXPORT_SYMBOL(clk_get_rate); |
| 459 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 460 | int clk_set_rate(struct clk *clk, unsigned long rate) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 461 | { |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 462 | unsigned long start_rate; |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 463 | int rc = 0; |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 464 | const char *name = clk ? clk->dbg_name : NULL; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 465 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 466 | if (IS_ERR_OR_NULL(clk)) |
| 467 | return -EINVAL; |
| 468 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 469 | if (!clk->ops->set_rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 470 | return -ENOSYS; |
Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 471 | |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 472 | if (!is_rate_valid(clk, rate)) |
| 473 | return -EINVAL; |
| 474 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 475 | mutex_lock(&clk->prepare_lock); |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 476 | |
| 477 | /* Return early if the rate isn't going to change */ |
| 478 | if (clk->rate == rate) |
| 479 | goto out; |
| 480 | |
Stephen Boyd | 64dce90 | 2012-08-09 12:59:40 -0700 | [diff] [blame] | 481 | trace_clock_set_rate(name, rate, raw_smp_processor_id()); |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 482 | |
| 483 | start_rate = clk->rate; |
| 484 | |
| 485 | /* Enforce vdd requirements for target frequency. */ |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 486 | if (clk->prepare_count) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 487 | rc = vote_rate_vdd(clk, rate); |
| 488 | if (rc) |
Patrick Daly | c009d9e | 2012-10-01 11:55:27 -0700 | [diff] [blame] | 489 | goto out; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 490 | } |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 491 | |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 492 | rc = clk->ops->set_rate(clk, rate); |
| 493 | if (rc) |
| 494 | goto err_set_rate; |
| 495 | clk->rate = rate; |
| 496 | |
| 497 | /* Release vdd requirements for starting frequency. */ |
| 498 | if (clk->prepare_count) |
| 499 | unvote_rate_vdd(clk, start_rate); |
| 500 | |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 501 | out: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 502 | mutex_unlock(&clk->prepare_lock); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 503 | return rc; |
| 504 | |
| 505 | err_set_rate: |
Saravana Kannan | a8c9154 | 2013-03-18 21:15:18 -0700 | [diff] [blame] | 506 | if (clk->prepare_count) |
| 507 | unvote_rate_vdd(clk, rate); |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 508 | goto out; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 509 | } |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 510 | EXPORT_SYMBOL(clk_set_rate); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 511 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 512 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 513 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 514 | if (IS_ERR_OR_NULL(clk)) |
| 515 | return -EINVAL; |
| 516 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | if (!clk->ops->round_rate) |
| 518 | return -ENOSYS; |
| 519 | |
| 520 | return clk->ops->round_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 521 | } |
| 522 | EXPORT_SYMBOL(clk_round_rate); |
| 523 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 524 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 525 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 526 | if (IS_ERR_OR_NULL(clk)) |
| 527 | return -EINVAL; |
| 528 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 529 | if (!clk->ops->set_max_rate) |
| 530 | return -ENOSYS; |
| 531 | |
| 532 | return clk->ops->set_max_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 533 | } |
| 534 | EXPORT_SYMBOL(clk_set_max_rate); |
| 535 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 536 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 537 | { |
Saravana Kannan | 776bdfd | 2013-03-18 20:08:28 -0700 | [diff] [blame] | 538 | int rc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 539 | |
Saravana Kannan | 776bdfd | 2013-03-18 20:08:28 -0700 | [diff] [blame] | 540 | if (!clk->ops->set_parent) |
| 541 | return -ENOSYS; |
| 542 | |
| 543 | mutex_lock(&clk->prepare_lock); |
| 544 | if (clk->parent == parent) |
| 545 | goto out; |
| 546 | rc = clk->ops->set_parent(clk, parent); |
| 547 | if (!rc) |
| 548 | clk->parent = parent; |
| 549 | out: |
| 550 | mutex_unlock(&clk->prepare_lock); |
| 551 | |
| 552 | return rc; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 553 | } |
| 554 | EXPORT_SYMBOL(clk_set_parent); |
| 555 | |
| 556 | struct clk *clk_get_parent(struct clk *clk) |
| 557 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 558 | if (IS_ERR_OR_NULL(clk)) |
| 559 | return NULL; |
| 560 | |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 561 | return clk->parent; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 562 | } |
| 563 | EXPORT_SYMBOL(clk_get_parent); |
| 564 | |
| 565 | int clk_set_flags(struct clk *clk, unsigned long flags) |
| 566 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 567 | if (IS_ERR_OR_NULL(clk)) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 568 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 569 | if (!clk->ops->set_flags) |
| 570 | return -ENOSYS; |
| 571 | |
| 572 | return clk->ops->set_flags(clk, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 573 | } |
| 574 | EXPORT_SYMBOL(clk_set_flags); |
| 575 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 576 | static struct clock_init_data *clk_init_data; |
| 577 | |
Matt Wagantall | 20f8e0f | 2012-09-26 17:28:54 -0700 | [diff] [blame] | 578 | static void init_sibling_lists(struct clk_lookup *clock_tbl, size_t num_clocks) |
| 579 | { |
| 580 | struct clk *clk, *parent; |
| 581 | unsigned n; |
| 582 | |
| 583 | for (n = 0; n < num_clocks; n++) { |
| 584 | clk = clock_tbl[n].clk; |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 585 | parent = clk->parent; |
Matt Wagantall | 20f8e0f | 2012-09-26 17:28:54 -0700 | [diff] [blame] | 586 | if (parent && list_empty(&clk->siblings)) |
| 587 | list_add(&clk->siblings, &parent->children); |
| 588 | } |
| 589 | } |
| 590 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 591 | /** |
| 592 | * msm_clock_register() - Register additional clock tables |
| 593 | * @table: Table of clocks |
| 594 | * @size: Size of @table |
| 595 | * |
| 596 | * Upon return, clock APIs may be used to control clocks registered using this |
| 597 | * function. This API may only be used after msm_clock_init() has completed. |
| 598 | * Unlike msm_clock_init(), this function may be called multiple times with |
| 599 | * different clock lists and used after the kernel has finished booting. |
| 600 | */ |
| 601 | int msm_clock_register(struct clk_lookup *table, size_t size) |
| 602 | { |
| 603 | if (!clk_init_data) |
| 604 | return -ENODEV; |
| 605 | |
| 606 | if (!table) |
| 607 | return -EINVAL; |
| 608 | |
Matt Wagantall | 20f8e0f | 2012-09-26 17:28:54 -0700 | [diff] [blame] | 609 | init_sibling_lists(table, size); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 610 | clkdev_add_table(table, size); |
| 611 | clock_debug_register(table, size); |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | EXPORT_SYMBOL(msm_clock_register); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 616 | |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 617 | |
| 618 | static void vdd_class_init(struct clk_vdd_class *vdd) |
| 619 | { |
| 620 | struct handoff_vdd *v; |
| 621 | int i; |
| 622 | |
| 623 | if (!vdd) |
| 624 | return; |
| 625 | |
| 626 | list_for_each_entry(v, &handoff_vdd_list, list) { |
| 627 | if (v->vdd_class == vdd) |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | pr_debug("voting for vdd_class %s\n", vdd->class_name); |
| 632 | if (vote_vdd_level(vdd, vdd->num_levels - 1)) |
| 633 | pr_err("failed to vote for %s\n", vdd->class_name); |
| 634 | |
| 635 | for (i = 0; i < vdd->num_regulators; i++) |
| 636 | regulator_enable(vdd->regulator[i]); |
| 637 | |
| 638 | v = kmalloc(sizeof(*v), GFP_KERNEL); |
| 639 | if (!v) { |
| 640 | pr_err("Unable to kmalloc. %s will be stuck at max.\n", |
| 641 | vdd->class_name); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | v->vdd_class = vdd; |
| 646 | list_add_tail(&v->list, &handoff_vdd_list); |
| 647 | } |
| 648 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 649 | static int __init __handoff_clk(struct clk *clk) |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 650 | { |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 651 | enum handoff state = HANDOFF_DISABLED_CLK; |
| 652 | struct handoff_clk *h = NULL; |
| 653 | int rc; |
| 654 | |
| 655 | if (clk == NULL || clk->flags & CLKFLAG_INIT_DONE || |
| 656 | clk->flags & CLKFLAG_SKIP_HANDOFF) |
| 657 | return 0; |
| 658 | |
| 659 | if (clk->flags & CLKFLAG_INIT_ERR) |
| 660 | return -ENXIO; |
| 661 | |
| 662 | /* Handoff any 'depends' clock first. */ |
| 663 | rc = __handoff_clk(clk->depends); |
| 664 | if (rc) |
| 665 | goto err; |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 666 | |
| 667 | /* |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 668 | * Handoff functions for the parent must be called before the |
| 669 | * children can be handed off. Without handing off the parents and |
| 670 | * knowing their rate and state (on/off), it's impossible to figure |
| 671 | * out the rate and state of the children. |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 672 | */ |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 673 | if (clk->ops->get_parent) |
| 674 | clk->parent = clk->ops->get_parent(clk); |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 675 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 676 | if (IS_ERR(clk->parent)) { |
| 677 | rc = PTR_ERR(clk->parent); |
| 678 | goto err; |
| 679 | } |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 680 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 681 | rc = __handoff_clk(clk->parent); |
| 682 | if (rc) |
| 683 | goto err; |
| 684 | |
| 685 | if (clk->ops->handoff) |
| 686 | state = clk->ops->handoff(clk); |
| 687 | |
| 688 | if (state == HANDOFF_ENABLED_CLK) { |
| 689 | |
| 690 | h = kmalloc(sizeof(*h), GFP_KERNEL); |
| 691 | if (!h) { |
| 692 | rc = -ENOMEM; |
| 693 | goto err; |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 694 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 695 | |
| 696 | rc = clk_prepare_enable(clk->parent); |
| 697 | if (rc) |
| 698 | goto err; |
| 699 | |
| 700 | rc = clk_prepare_enable(clk->depends); |
| 701 | if (rc) |
| 702 | goto err_depends; |
| 703 | |
| 704 | rc = vote_rate_vdd(clk, clk->rate); |
| 705 | WARN(rc, "%s unable to vote for voltage!\n", clk->dbg_name); |
| 706 | |
| 707 | clk->count = 1; |
| 708 | clk->prepare_count = 1; |
| 709 | h->clk = clk; |
| 710 | list_add_tail(&h->list, &handoff_list); |
| 711 | |
| 712 | 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] | 713 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 714 | |
| 715 | clk->flags |= CLKFLAG_INIT_DONE; |
| 716 | |
| 717 | return 0; |
| 718 | |
| 719 | err_depends: |
| 720 | clk_disable_unprepare(clk->parent); |
| 721 | err: |
| 722 | kfree(h); |
| 723 | clk->flags |= CLKFLAG_INIT_ERR; |
| 724 | pr_err("%s handoff failed (%d)\n", clk->dbg_name, rc); |
| 725 | return rc; |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 728 | /** |
| 729 | * msm_clock_init() - Register and initialize a clock driver |
| 730 | * @data: Driver-specific clock initialization data |
| 731 | * |
| 732 | * Upon return from this call, clock APIs may be used to control |
| 733 | * clocks registered with this API. |
| 734 | */ |
| 735 | int __init msm_clock_init(struct clock_init_data *data) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 736 | { |
| 737 | unsigned n; |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame] | 738 | struct clk_lookup *clock_tbl; |
| 739 | size_t num_clocks; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 740 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 741 | if (!data) |
| 742 | return -EINVAL; |
| 743 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 744 | clk_init_data = data; |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 745 | if (clk_init_data->pre_init) |
| 746 | clk_init_data->pre_init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 747 | |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame] | 748 | clock_tbl = data->table; |
| 749 | num_clocks = data->size; |
| 750 | |
Matt Wagantall | fba2c5b | 2012-10-18 12:54:15 -0700 | [diff] [blame] | 751 | init_sibling_lists(clock_tbl, num_clocks); |
| 752 | |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 753 | /* |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 754 | * Enable regulators and temporarily set them up at maximum voltage. |
| 755 | * Once all the clocks have made their respective vote, remove this |
| 756 | * temporary vote. The removing of the temporary vote is done at |
| 757 | * late_init, by which time we assume all the clocks would have been |
| 758 | * handed off. |
| 759 | */ |
| 760 | for (n = 0; n < num_clocks; n++) |
| 761 | vdd_class_init(clock_tbl[n].clk->vdd_class); |
| 762 | |
| 763 | /* |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 764 | * Detect and preserve initial clock state until clock_late_init() or |
| 765 | * a driver explicitly changes it, whichever is first. |
| 766 | */ |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 767 | for (n = 0; n < num_clocks; n++) |
| 768 | __handoff_clk(clock_tbl[n].clk); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 769 | |
Matt Wagantall | fba2c5b | 2012-10-18 12:54:15 -0700 | [diff] [blame] | 770 | clkdev_add_table(clock_tbl, num_clocks); |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 771 | |
| 772 | if (clk_init_data->post_init) |
| 773 | clk_init_data->post_init(); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 774 | |
Matt Wagantall | fba2c5b | 2012-10-18 12:54:15 -0700 | [diff] [blame] | 775 | clock_debug_init(); |
| 776 | clock_debug_register(clock_tbl, num_clocks); |
| 777 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 778 | return 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 781 | static int __init clock_late_init(void) |
| 782 | { |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 783 | struct handoff_clk *h, *h_temp; |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 784 | struct handoff_vdd *v, *v_temp; |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 785 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 786 | |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 787 | if (clk_init_data->late_init) |
| 788 | ret = clk_init_data->late_init(); |
| 789 | |
Matt Wagantall | 647d1c1 | 2012-05-16 14:32:14 -0700 | [diff] [blame] | 790 | pr_info("%s: Removing enables held for handed-off clocks\n", __func__); |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 791 | list_for_each_entry_safe(h, h_temp, &handoff_list, list) { |
| 792 | clk_disable_unprepare(h->clk); |
| 793 | list_del(&h->list); |
| 794 | kfree(h); |
| 795 | } |
| 796 | |
Patrick Daly | 5ce9da3 | 2013-03-26 13:12:53 -0700 | [diff] [blame] | 797 | list_for_each_entry_safe(v, v_temp, &handoff_vdd_list, list) { |
| 798 | unvote_vdd_level(v->vdd_class, v->vdd_class->num_levels - 1); |
| 799 | list_del(&v->list); |
| 800 | kfree(v); |
| 801 | } |
| 802 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 803 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 804 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 805 | late_initcall(clock_late_init); |