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. |
Tianyi Gou | 7949ecb | 2012-02-14 14:25:32 -0800 | [diff] [blame] | 4 | * Copyright (c) 2007-2012, Code Aurora Forum. 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> |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 25 | #include <trace/events/power.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 26 | |
| 27 | #include "clock.h" |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 28 | |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 29 | struct handoff_clk { |
| 30 | struct list_head list; |
| 31 | struct clk *clk; |
| 32 | }; |
| 33 | static LIST_HEAD(handoff_list); |
| 34 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 35 | /* Find the voltage level required for a given rate. */ |
| 36 | static int find_vdd_level(struct clk *clk, unsigned long rate) |
| 37 | { |
| 38 | int level; |
| 39 | |
| 40 | for (level = 0; level < ARRAY_SIZE(clk->fmax); level++) |
| 41 | if (rate <= clk->fmax[level]) |
| 42 | break; |
| 43 | |
| 44 | if (level == ARRAY_SIZE(clk->fmax)) { |
| 45 | pr_err("Rate %lu for %s is greater than highest Fmax\n", rate, |
| 46 | clk->dbg_name); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | return level; |
| 51 | } |
| 52 | |
| 53 | /* Update voltage level given the current votes. */ |
| 54 | static int update_vdd(struct clk_vdd_class *vdd_class) |
| 55 | { |
| 56 | int level, rc; |
| 57 | |
| 58 | for (level = ARRAY_SIZE(vdd_class->level_votes)-1; level > 0; level--) |
| 59 | if (vdd_class->level_votes[level]) |
| 60 | break; |
| 61 | |
| 62 | if (level == vdd_class->cur_level) |
| 63 | return 0; |
| 64 | |
| 65 | rc = vdd_class->set_vdd(vdd_class, level); |
| 66 | if (!rc) |
| 67 | vdd_class->cur_level = level; |
| 68 | |
| 69 | return rc; |
| 70 | } |
| 71 | |
| 72 | /* Vote for a voltage level. */ |
| 73 | int vote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 74 | { |
| 75 | unsigned long flags; |
| 76 | int rc; |
| 77 | |
| 78 | spin_lock_irqsave(&vdd_class->lock, flags); |
| 79 | vdd_class->level_votes[level]++; |
| 80 | rc = update_vdd(vdd_class); |
| 81 | if (rc) |
| 82 | vdd_class->level_votes[level]--; |
| 83 | spin_unlock_irqrestore(&vdd_class->lock, flags); |
| 84 | |
| 85 | return rc; |
| 86 | } |
| 87 | |
| 88 | /* Remove vote for a voltage level. */ |
| 89 | int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 90 | { |
| 91 | unsigned long flags; |
| 92 | int rc = 0; |
| 93 | |
| 94 | spin_lock_irqsave(&vdd_class->lock, flags); |
| 95 | if (WARN(!vdd_class->level_votes[level], |
| 96 | "Reference counts are incorrect for %s level %d\n", |
| 97 | vdd_class->class_name, level)) |
| 98 | goto out; |
| 99 | vdd_class->level_votes[level]--; |
| 100 | rc = update_vdd(vdd_class); |
| 101 | if (rc) |
| 102 | vdd_class->level_votes[level]++; |
| 103 | out: |
| 104 | spin_unlock_irqrestore(&vdd_class->lock, flags); |
| 105 | return rc; |
| 106 | } |
| 107 | |
| 108 | /* Vote for a voltage level corresponding to a clock's rate. */ |
| 109 | static int vote_rate_vdd(struct clk *clk, unsigned long rate) |
| 110 | { |
| 111 | int level; |
| 112 | |
| 113 | if (!clk->vdd_class) |
| 114 | return 0; |
| 115 | |
| 116 | level = find_vdd_level(clk, rate); |
| 117 | if (level < 0) |
| 118 | return level; |
| 119 | |
| 120 | return vote_vdd_level(clk->vdd_class, level); |
| 121 | } |
| 122 | |
| 123 | /* Remove vote for a voltage level corresponding to a clock's rate. */ |
| 124 | static void unvote_rate_vdd(struct clk *clk, unsigned long rate) |
| 125 | { |
| 126 | int level; |
| 127 | |
| 128 | if (!clk->vdd_class) |
| 129 | return; |
| 130 | |
| 131 | level = find_vdd_level(clk, rate); |
| 132 | if (level < 0) |
| 133 | return; |
| 134 | |
| 135 | unvote_vdd_level(clk->vdd_class, level); |
| 136 | } |
| 137 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 138 | int clk_prepare(struct clk *clk) |
| 139 | { |
| 140 | int ret = 0; |
| 141 | struct clk *parent; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 142 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 143 | if (!clk) |
| 144 | return 0; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 145 | if (IS_ERR(clk)) |
| 146 | return -EINVAL; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 147 | |
| 148 | mutex_lock(&clk->prepare_lock); |
| 149 | if (clk->prepare_count == 0) { |
| 150 | parent = clk_get_parent(clk); |
| 151 | |
| 152 | ret = clk_prepare(parent); |
| 153 | if (ret) |
| 154 | goto out; |
| 155 | ret = clk_prepare(clk->depends); |
| 156 | if (ret) |
| 157 | goto err_prepare_depends; |
| 158 | |
| 159 | if (clk->ops->prepare) |
| 160 | ret = clk->ops->prepare(clk); |
| 161 | if (ret) |
| 162 | goto err_prepare_clock; |
| 163 | } |
| 164 | clk->prepare_count++; |
| 165 | out: |
| 166 | mutex_unlock(&clk->prepare_lock); |
| 167 | return ret; |
| 168 | err_prepare_clock: |
| 169 | clk_unprepare(clk->depends); |
| 170 | err_prepare_depends: |
| 171 | clk_unprepare(parent); |
| 172 | goto out; |
| 173 | } |
| 174 | EXPORT_SYMBOL(clk_prepare); |
| 175 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 176 | /* |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 177 | * Standard clock functions defined in include/linux/clk.h |
| 178 | */ |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 179 | int clk_enable(struct clk *clk) |
| 180 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 181 | int ret = 0; |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 182 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | struct clk *parent; |
| 184 | |
| 185 | if (!clk) |
| 186 | return 0; |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 187 | if (IS_ERR(clk)) |
| 188 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | |
| 190 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 191 | if (WARN(!clk->warned && !clk->prepare_count, |
| 192 | "%s: Don't call enable on unprepared clocks\n", |
| 193 | clk->dbg_name)) |
| 194 | clk->warned = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | if (clk->count == 0) { |
| 196 | parent = clk_get_parent(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 197 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | ret = clk_enable(parent); |
| 199 | if (ret) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 200 | goto err_enable_parent; |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 201 | ret = clk_enable(clk->depends); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 202 | if (ret) |
| 203 | goto err_enable_depends; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 205 | ret = vote_rate_vdd(clk, clk->rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 206 | if (ret) |
| 207 | goto err_vote_vdd; |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 208 | trace_clock_enable(clk->dbg_name, 1, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | if (clk->ops->enable) |
| 210 | ret = clk->ops->enable(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 211 | if (ret) |
| 212 | goto err_enable_clock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 214 | clk->count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | spin_unlock_irqrestore(&clk->lock, flags); |
| 216 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | |
| 219 | err_enable_clock: |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 220 | unvote_rate_vdd(clk, clk->rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 221 | err_vote_vdd: |
| 222 | clk_disable(clk->depends); |
| 223 | err_enable_depends: |
| 224 | clk_disable(parent); |
| 225 | err_enable_parent: |
| 226 | spin_unlock_irqrestore(&clk->lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 227 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 228 | } |
| 229 | EXPORT_SYMBOL(clk_enable); |
| 230 | |
| 231 | void clk_disable(struct clk *clk) |
| 232 | { |
| 233 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 234 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 235 | if (IS_ERR_OR_NULL(clk)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 236 | return; |
| 237 | |
| 238 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 239 | if (WARN(!clk->warned && !clk->prepare_count, |
| 240 | "%s: Never called prepare or calling disable " |
| 241 | "after unprepare\n", |
| 242 | clk->dbg_name)) |
| 243 | clk->warned = true; |
Stephen Boyd | d906b52 | 2011-07-26 10:51:41 -0700 | [diff] [blame] | 244 | if (WARN(clk->count == 0, "%s is unbalanced", clk->dbg_name)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 245 | goto out; |
| 246 | if (clk->count == 1) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 247 | struct clk *parent = clk_get_parent(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 248 | |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 249 | trace_clock_disable(clk->dbg_name, 0, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | if (clk->ops->disable) |
| 251 | clk->ops->disable(clk); |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 252 | unvote_rate_vdd(clk, clk->rate); |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 253 | clk_disable(clk->depends); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 254 | clk_disable(parent); |
| 255 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 256 | clk->count--; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | out: |
| 258 | spin_unlock_irqrestore(&clk->lock, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL(clk_disable); |
| 261 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 262 | void clk_unprepare(struct clk *clk) |
| 263 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 264 | if (IS_ERR_OR_NULL(clk)) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 265 | return; |
| 266 | |
| 267 | mutex_lock(&clk->prepare_lock); |
| 268 | if (!clk->prepare_count) { |
| 269 | if (WARN(!clk->warned, "%s is unbalanced (prepare)", |
| 270 | clk->dbg_name)) |
| 271 | clk->warned = true; |
| 272 | goto out; |
| 273 | } |
| 274 | if (clk->prepare_count == 1) { |
| 275 | struct clk *parent = clk_get_parent(clk); |
| 276 | |
| 277 | if (WARN(!clk->warned && clk->count, |
| 278 | "%s: Don't call unprepare when the clock is enabled\n", |
| 279 | clk->dbg_name)) |
| 280 | clk->warned = true; |
| 281 | |
| 282 | if (clk->ops->unprepare) |
| 283 | clk->ops->unprepare(clk); |
| 284 | clk_unprepare(clk->depends); |
| 285 | clk_unprepare(parent); |
| 286 | } |
| 287 | clk->prepare_count--; |
| 288 | out: |
| 289 | mutex_unlock(&clk->prepare_lock); |
| 290 | } |
| 291 | EXPORT_SYMBOL(clk_unprepare); |
| 292 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 293 | int clk_reset(struct clk *clk, enum clk_reset_action action) |
| 294 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 295 | if (IS_ERR_OR_NULL(clk)) |
| 296 | return -EINVAL; |
| 297 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 298 | if (!clk->ops->reset) |
| 299 | return -ENOSYS; |
| 300 | |
| 301 | return clk->ops->reset(clk, action); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL(clk_reset); |
| 304 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 305 | unsigned long clk_get_rate(struct clk *clk) |
| 306 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 307 | if (IS_ERR_OR_NULL(clk)) |
| 308 | return 0; |
| 309 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 310 | if (!clk->ops->get_rate) |
Tianyi Gou | 7949ecb | 2012-02-14 14:25:32 -0800 | [diff] [blame] | 311 | return clk->rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 312 | |
| 313 | return clk->ops->get_rate(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 314 | } |
| 315 | EXPORT_SYMBOL(clk_get_rate); |
| 316 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 317 | int clk_set_rate(struct clk *clk, unsigned long rate) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 318 | { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 319 | unsigned long start_rate, flags; |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 320 | int rc = 0; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 321 | |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 322 | if (IS_ERR_OR_NULL(clk)) |
| 323 | return -EINVAL; |
| 324 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 325 | if (!clk->ops->set_rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 326 | return -ENOSYS; |
Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 327 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 328 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 329 | |
| 330 | /* Return early if the rate isn't going to change */ |
| 331 | if (clk->rate == rate) |
| 332 | goto out; |
| 333 | |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 334 | trace_clock_set_rate(clk->dbg_name, rate, smp_processor_id()); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 335 | if (clk->count) { |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 336 | start_rate = clk->rate; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 337 | /* Enforce vdd requirements for target frequency. */ |
| 338 | rc = vote_rate_vdd(clk, rate); |
| 339 | if (rc) |
| 340 | goto err_vote_vdd; |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 341 | rc = clk->ops->set_rate(clk, rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 342 | if (rc) |
| 343 | goto err_set_rate; |
| 344 | /* Release vdd requirements for starting frequency. */ |
| 345 | unvote_rate_vdd(clk, start_rate); |
| 346 | } else { |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 347 | rc = clk->ops->set_rate(clk, rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 348 | } |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 349 | |
| 350 | if (!rc) |
| 351 | clk->rate = rate; |
Stephen Boyd | 4fefefc | 2012-04-13 13:37:46 -0700 | [diff] [blame] | 352 | out: |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 353 | spin_unlock_irqrestore(&clk->lock, flags); |
| 354 | return rc; |
| 355 | |
| 356 | err_set_rate: |
| 357 | unvote_rate_vdd(clk, rate); |
| 358 | err_vote_vdd: |
| 359 | spin_unlock_irqrestore(&clk->lock, flags); |
| 360 | return rc; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 361 | } |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 362 | EXPORT_SYMBOL(clk_set_rate); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 363 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 364 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 365 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 366 | if (IS_ERR_OR_NULL(clk)) |
| 367 | return -EINVAL; |
| 368 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 369 | if (!clk->ops->round_rate) |
| 370 | return -ENOSYS; |
| 371 | |
| 372 | return clk->ops->round_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 373 | } |
| 374 | EXPORT_SYMBOL(clk_round_rate); |
| 375 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 376 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 377 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 378 | if (IS_ERR_OR_NULL(clk)) |
| 379 | return -EINVAL; |
| 380 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 381 | if (!clk->ops->set_max_rate) |
| 382 | return -ENOSYS; |
| 383 | |
| 384 | return clk->ops->set_max_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL(clk_set_max_rate); |
| 387 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 388 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 389 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 390 | if (!clk->ops->set_parent) |
| 391 | return 0; |
| 392 | |
| 393 | return clk->ops->set_parent(clk, parent); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 394 | } |
| 395 | EXPORT_SYMBOL(clk_set_parent); |
| 396 | |
| 397 | struct clk *clk_get_parent(struct clk *clk) |
| 398 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 399 | if (IS_ERR_OR_NULL(clk)) |
| 400 | return NULL; |
| 401 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | if (!clk->ops->get_parent) |
| 403 | return NULL; |
| 404 | |
| 405 | return clk->ops->get_parent(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 406 | } |
| 407 | EXPORT_SYMBOL(clk_get_parent); |
| 408 | |
| 409 | int clk_set_flags(struct clk *clk, unsigned long flags) |
| 410 | { |
Vikram Mulukutla | 55e8f99 | 2012-04-17 19:25:10 -0700 | [diff] [blame] | 411 | if (IS_ERR_OR_NULL(clk)) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 412 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | if (!clk->ops->set_flags) |
| 414 | return -ENOSYS; |
| 415 | |
| 416 | return clk->ops->set_flags(clk, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 417 | } |
| 418 | EXPORT_SYMBOL(clk_set_flags); |
| 419 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 420 | static struct clock_init_data __initdata *clk_init_data; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 421 | |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 422 | static enum handoff __init __handoff_clk(struct clk *clk) |
| 423 | { |
| 424 | enum handoff ret; |
| 425 | struct handoff_clk *h; |
Matt Wagantall | 2a59b21 | 2012-06-12 19:16:01 -0700 | [diff] [blame] | 426 | unsigned long rate; |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 427 | int err = 0; |
| 428 | |
| 429 | /* |
| 430 | * Tree roots don't have parents, but need to be handed off. So, |
| 431 | * terminate recursion by returning "enabled". Also return "enabled" |
| 432 | * for clocks with non-zero enable counts since they must have already |
| 433 | * been handed off. |
| 434 | */ |
| 435 | if (clk == NULL || clk->count) |
| 436 | return HANDOFF_ENABLED_CLK; |
| 437 | |
| 438 | /* Clocks without handoff functions are assumed to be disabled. */ |
| 439 | if (!clk->ops->handoff || (clk->flags & CLKFLAG_SKIP_HANDOFF)) |
| 440 | return HANDOFF_DISABLED_CLK; |
| 441 | |
| 442 | /* |
| 443 | * Handoff functions for children must be called before their parents' |
| 444 | * so that the correct parent is returned by the clk_get_parent() below. |
| 445 | */ |
| 446 | ret = clk->ops->handoff(clk); |
| 447 | if (ret == HANDOFF_ENABLED_CLK) { |
| 448 | ret = __handoff_clk(clk_get_parent(clk)); |
| 449 | if (ret == HANDOFF_ENABLED_CLK) { |
| 450 | h = kmalloc(sizeof(*h), GFP_KERNEL); |
| 451 | if (!h) { |
| 452 | err = -ENOMEM; |
| 453 | goto out; |
| 454 | } |
| 455 | err = clk_prepare_enable(clk); |
| 456 | if (err) |
| 457 | goto out; |
Matt Wagantall | 2a59b21 | 2012-06-12 19:16:01 -0700 | [diff] [blame] | 458 | rate = clk_get_rate(clk); |
| 459 | if (rate) |
| 460 | pr_debug("%s rate=%lu\n", clk->dbg_name, rate); |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 461 | h->clk = clk; |
| 462 | list_add_tail(&h->list, &handoff_list); |
| 463 | } |
| 464 | } |
| 465 | out: |
| 466 | if (err) { |
| 467 | pr_err("%s handoff failed (%d)\n", clk->dbg_name, err); |
| 468 | kfree(h); |
| 469 | ret = HANDOFF_DISABLED_CLK; |
| 470 | } |
| 471 | return ret; |
| 472 | } |
| 473 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 474 | void __init msm_clock_init(struct clock_init_data *data) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 475 | { |
| 476 | unsigned n; |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame] | 477 | struct clk_lookup *clock_tbl; |
| 478 | size_t num_clocks; |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 479 | struct clk *clk; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 480 | |
| 481 | clk_init_data = data; |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 482 | if (clk_init_data->pre_init) |
| 483 | clk_init_data->pre_init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 484 | |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame] | 485 | clock_tbl = data->table; |
| 486 | num_clocks = data->size; |
| 487 | |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 488 | for (n = 0; n < num_clocks; n++) { |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 489 | struct clk *parent; |
| 490 | clk = clock_tbl[n].clk; |
| 491 | parent = clk_get_parent(clk); |
Matt Wagantall | e1482bf | 2012-04-04 16:23:45 -0700 | [diff] [blame] | 492 | if (parent && list_empty(&clk->siblings)) |
| 493 | list_add(&clk->siblings, &parent->children); |
Matt Wagantall | b37fea4 | 2012-04-04 16:47:23 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Detect and preserve initial clock state until clock_late_init() or |
| 498 | * a driver explicitly changes it, whichever is first. |
| 499 | */ |
Matt Wagantall | f30fceb | 2012-06-12 19:13:11 -0700 | [diff] [blame] | 500 | for (n = 0; n < num_clocks; n++) |
| 501 | __handoff_clk(clock_tbl[n].clk); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 502 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 503 | clkdev_add_table(clock_tbl, num_clocks); |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 504 | |
| 505 | if (clk_init_data->post_init) |
| 506 | clk_init_data->post_init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 509 | static int __init clock_late_init(void) |
| 510 | { |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 511 | struct handoff_clk *h, *h_temp; |
Matt Wagantall | 647d1c1 | 2012-05-16 14:32:14 -0700 | [diff] [blame] | 512 | int n, ret = 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 513 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 514 | clock_debug_init(clk_init_data); |
Matt Wagantall | 647d1c1 | 2012-05-16 14:32:14 -0700 | [diff] [blame] | 515 | for (n = 0; n < clk_init_data->size; n++) |
| 516 | clock_debug_add(clk_init_data->table[n].clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | |
Matt Wagantall | 647d1c1 | 2012-05-16 14:32:14 -0700 | [diff] [blame] | 518 | pr_info("%s: Removing enables held for handed-off clocks\n", __func__); |
Matt Wagantall | 158f73b | 2012-05-16 11:29:35 -0700 | [diff] [blame] | 519 | list_for_each_entry_safe(h, h_temp, &handoff_list, list) { |
| 520 | clk_disable_unprepare(h->clk); |
| 521 | list_del(&h->list); |
| 522 | kfree(h); |
| 523 | } |
| 524 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 525 | if (clk_init_data->late_init) |
| 526 | ret = clk_init_data->late_init(); |
| 527 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 528 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 529 | late_initcall(clock_late_init); |