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> |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 24 | #include <trace/events/power.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 25 | |
| 26 | #include "clock.h" |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 27 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 28 | /* Find the voltage level required for a given rate. */ |
| 29 | static int find_vdd_level(struct clk *clk, unsigned long rate) |
| 30 | { |
| 31 | int level; |
| 32 | |
| 33 | for (level = 0; level < ARRAY_SIZE(clk->fmax); level++) |
| 34 | if (rate <= clk->fmax[level]) |
| 35 | break; |
| 36 | |
| 37 | if (level == ARRAY_SIZE(clk->fmax)) { |
| 38 | pr_err("Rate %lu for %s is greater than highest Fmax\n", rate, |
| 39 | clk->dbg_name); |
| 40 | return -EINVAL; |
| 41 | } |
| 42 | |
| 43 | return level; |
| 44 | } |
| 45 | |
| 46 | /* Update voltage level given the current votes. */ |
| 47 | static int update_vdd(struct clk_vdd_class *vdd_class) |
| 48 | { |
| 49 | int level, rc; |
| 50 | |
| 51 | for (level = ARRAY_SIZE(vdd_class->level_votes)-1; level > 0; level--) |
| 52 | if (vdd_class->level_votes[level]) |
| 53 | break; |
| 54 | |
| 55 | if (level == vdd_class->cur_level) |
| 56 | return 0; |
| 57 | |
| 58 | rc = vdd_class->set_vdd(vdd_class, level); |
| 59 | if (!rc) |
| 60 | vdd_class->cur_level = level; |
| 61 | |
| 62 | return rc; |
| 63 | } |
| 64 | |
| 65 | /* Vote for a voltage level. */ |
| 66 | int vote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 67 | { |
| 68 | unsigned long flags; |
| 69 | int rc; |
| 70 | |
| 71 | spin_lock_irqsave(&vdd_class->lock, flags); |
| 72 | vdd_class->level_votes[level]++; |
| 73 | rc = update_vdd(vdd_class); |
| 74 | if (rc) |
| 75 | vdd_class->level_votes[level]--; |
| 76 | spin_unlock_irqrestore(&vdd_class->lock, flags); |
| 77 | |
| 78 | return rc; |
| 79 | } |
| 80 | |
| 81 | /* Remove vote for a voltage level. */ |
| 82 | int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level) |
| 83 | { |
| 84 | unsigned long flags; |
| 85 | int rc = 0; |
| 86 | |
| 87 | spin_lock_irqsave(&vdd_class->lock, flags); |
| 88 | if (WARN(!vdd_class->level_votes[level], |
| 89 | "Reference counts are incorrect for %s level %d\n", |
| 90 | vdd_class->class_name, level)) |
| 91 | goto out; |
| 92 | vdd_class->level_votes[level]--; |
| 93 | rc = update_vdd(vdd_class); |
| 94 | if (rc) |
| 95 | vdd_class->level_votes[level]++; |
| 96 | out: |
| 97 | spin_unlock_irqrestore(&vdd_class->lock, flags); |
| 98 | return rc; |
| 99 | } |
| 100 | |
| 101 | /* Vote for a voltage level corresponding to a clock's rate. */ |
| 102 | static int vote_rate_vdd(struct clk *clk, unsigned long rate) |
| 103 | { |
| 104 | int level; |
| 105 | |
| 106 | if (!clk->vdd_class) |
| 107 | return 0; |
| 108 | |
| 109 | level = find_vdd_level(clk, rate); |
| 110 | if (level < 0) |
| 111 | return level; |
| 112 | |
| 113 | return vote_vdd_level(clk->vdd_class, level); |
| 114 | } |
| 115 | |
| 116 | /* Remove vote for a voltage level corresponding to a clock's rate. */ |
| 117 | static void unvote_rate_vdd(struct clk *clk, unsigned long rate) |
| 118 | { |
| 119 | int level; |
| 120 | |
| 121 | if (!clk->vdd_class) |
| 122 | return; |
| 123 | |
| 124 | level = find_vdd_level(clk, rate); |
| 125 | if (level < 0) |
| 126 | return; |
| 127 | |
| 128 | unvote_vdd_level(clk->vdd_class, level); |
| 129 | } |
| 130 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 131 | int clk_prepare(struct clk *clk) |
| 132 | { |
| 133 | int ret = 0; |
| 134 | struct clk *parent; |
| 135 | if (!clk) |
| 136 | return 0; |
| 137 | |
| 138 | mutex_lock(&clk->prepare_lock); |
| 139 | if (clk->prepare_count == 0) { |
| 140 | parent = clk_get_parent(clk); |
| 141 | |
| 142 | ret = clk_prepare(parent); |
| 143 | if (ret) |
| 144 | goto out; |
| 145 | ret = clk_prepare(clk->depends); |
| 146 | if (ret) |
| 147 | goto err_prepare_depends; |
| 148 | |
| 149 | if (clk->ops->prepare) |
| 150 | ret = clk->ops->prepare(clk); |
| 151 | if (ret) |
| 152 | goto err_prepare_clock; |
| 153 | } |
| 154 | clk->prepare_count++; |
| 155 | out: |
| 156 | mutex_unlock(&clk->prepare_lock); |
| 157 | return ret; |
| 158 | err_prepare_clock: |
| 159 | clk_unprepare(clk->depends); |
| 160 | err_prepare_depends: |
| 161 | clk_unprepare(parent); |
| 162 | goto out; |
| 163 | } |
| 164 | EXPORT_SYMBOL(clk_prepare); |
| 165 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 166 | /* |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 167 | * Standard clock functions defined in include/linux/clk.h |
| 168 | */ |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 169 | int clk_enable(struct clk *clk) |
| 170 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | int ret = 0; |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 172 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 173 | struct clk *parent; |
| 174 | |
| 175 | if (!clk) |
| 176 | return 0; |
| 177 | |
| 178 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 179 | if (WARN(!clk->warned && !clk->prepare_count, |
| 180 | "%s: Don't call enable on unprepared clocks\n", |
| 181 | clk->dbg_name)) |
| 182 | clk->warned = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | if (clk->count == 0) { |
| 184 | parent = clk_get_parent(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 185 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | ret = clk_enable(parent); |
| 187 | if (ret) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 188 | goto err_enable_parent; |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 189 | ret = clk_enable(clk->depends); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 190 | if (ret) |
| 191 | goto err_enable_depends; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 193 | ret = vote_rate_vdd(clk, clk->rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 194 | if (ret) |
| 195 | goto err_vote_vdd; |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 196 | trace_clock_enable(clk->dbg_name, 1, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | if (clk->ops->enable) |
| 198 | ret = clk->ops->enable(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 199 | if (ret) |
| 200 | goto err_enable_clock; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 201 | } else if (clk->flags & CLKFLAG_HANDOFF_RATE) { |
| 202 | /* |
| 203 | * The clock was already enabled by handoff code so there is no |
| 204 | * need to enable it again here. Clearing the handoff flag will |
| 205 | * prevent the lateinit handoff code from disabling the clock if |
| 206 | * a client driver still has it enabled. |
| 207 | */ |
| 208 | clk->flags &= ~CLKFLAG_HANDOFF_RATE; |
| 209 | goto out; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 210 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 211 | clk->count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 212 | out: |
| 213 | spin_unlock_irqrestore(&clk->lock, flags); |
| 214 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | |
| 217 | err_enable_clock: |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 218 | unvote_rate_vdd(clk, clk->rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 219 | err_vote_vdd: |
| 220 | clk_disable(clk->depends); |
| 221 | err_enable_depends: |
| 222 | clk_disable(parent); |
| 223 | err_enable_parent: |
| 224 | spin_unlock_irqrestore(&clk->lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 225 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL(clk_enable); |
| 228 | |
| 229 | void clk_disable(struct clk *clk) |
| 230 | { |
| 231 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 232 | |
| 233 | if (!clk) |
| 234 | return; |
| 235 | |
| 236 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 237 | if (WARN(!clk->warned && !clk->prepare_count, |
| 238 | "%s: Never called prepare or calling disable " |
| 239 | "after unprepare\n", |
| 240 | clk->dbg_name)) |
| 241 | clk->warned = true; |
Stephen Boyd | d906b52 | 2011-07-26 10:51:41 -0700 | [diff] [blame] | 242 | if (WARN(clk->count == 0, "%s is unbalanced", clk->dbg_name)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 243 | goto out; |
| 244 | if (clk->count == 1) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 245 | struct clk *parent = clk_get_parent(clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 246 | |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 247 | trace_clock_disable(clk->dbg_name, 0, smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 248 | if (clk->ops->disable) |
| 249 | clk->ops->disable(clk); |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 250 | unvote_rate_vdd(clk, clk->rate); |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 251 | clk_disable(clk->depends); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 252 | clk_disable(parent); |
| 253 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 254 | clk->count--; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | out: |
| 256 | spin_unlock_irqrestore(&clk->lock, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 257 | } |
| 258 | EXPORT_SYMBOL(clk_disable); |
| 259 | |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 260 | void clk_unprepare(struct clk *clk) |
| 261 | { |
| 262 | if (!clk) |
| 263 | return; |
| 264 | |
| 265 | mutex_lock(&clk->prepare_lock); |
| 266 | if (!clk->prepare_count) { |
| 267 | if (WARN(!clk->warned, "%s is unbalanced (prepare)", |
| 268 | clk->dbg_name)) |
| 269 | clk->warned = true; |
| 270 | goto out; |
| 271 | } |
| 272 | if (clk->prepare_count == 1) { |
| 273 | struct clk *parent = clk_get_parent(clk); |
| 274 | |
| 275 | if (WARN(!clk->warned && clk->count, |
| 276 | "%s: Don't call unprepare when the clock is enabled\n", |
| 277 | clk->dbg_name)) |
| 278 | clk->warned = true; |
| 279 | |
| 280 | if (clk->ops->unprepare) |
| 281 | clk->ops->unprepare(clk); |
| 282 | clk_unprepare(clk->depends); |
| 283 | clk_unprepare(parent); |
| 284 | } |
| 285 | clk->prepare_count--; |
| 286 | out: |
| 287 | mutex_unlock(&clk->prepare_lock); |
| 288 | } |
| 289 | EXPORT_SYMBOL(clk_unprepare); |
| 290 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 291 | int clk_reset(struct clk *clk, enum clk_reset_action action) |
| 292 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | if (!clk->ops->reset) |
| 294 | return -ENOSYS; |
| 295 | |
| 296 | return clk->ops->reset(clk, action); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 297 | } |
| 298 | EXPORT_SYMBOL(clk_reset); |
| 299 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 300 | unsigned long clk_get_rate(struct clk *clk) |
| 301 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 302 | if (!clk->ops->get_rate) |
Tianyi Gou | 7949ecb | 2012-02-14 14:25:32 -0800 | [diff] [blame] | 303 | return clk->rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | |
| 305 | return clk->ops->get_rate(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 306 | } |
| 307 | EXPORT_SYMBOL(clk_get_rate); |
| 308 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 309 | int clk_set_rate(struct clk *clk, unsigned long rate) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 310 | { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 311 | unsigned long start_rate, flags; |
| 312 | int rc; |
| 313 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 314 | if (!clk->ops->set_rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 315 | return -ENOSYS; |
Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 316 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 317 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | 5bc44d5 | 2012-03-29 11:00:57 -0700 | [diff] [blame] | 318 | trace_clock_set_rate(clk->dbg_name, rate, smp_processor_id()); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 319 | if (clk->count) { |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 320 | start_rate = clk->rate; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 321 | /* Enforce vdd requirements for target frequency. */ |
| 322 | rc = vote_rate_vdd(clk, rate); |
| 323 | if (rc) |
| 324 | goto err_vote_vdd; |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 325 | rc = clk->ops->set_rate(clk, rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 326 | if (rc) |
| 327 | goto err_set_rate; |
| 328 | /* Release vdd requirements for starting frequency. */ |
| 329 | unvote_rate_vdd(clk, start_rate); |
| 330 | } else { |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 331 | rc = clk->ops->set_rate(clk, rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 332 | } |
Matt Wagantall | 7205eea | 2011-11-04 17:31:29 -0700 | [diff] [blame] | 333 | |
| 334 | if (!rc) |
| 335 | clk->rate = rate; |
| 336 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 337 | spin_unlock_irqrestore(&clk->lock, flags); |
| 338 | return rc; |
| 339 | |
| 340 | err_set_rate: |
| 341 | unvote_rate_vdd(clk, rate); |
| 342 | err_vote_vdd: |
| 343 | spin_unlock_irqrestore(&clk->lock, flags); |
| 344 | return rc; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 345 | } |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 346 | EXPORT_SYMBOL(clk_set_rate); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 347 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 348 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 349 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | if (!clk->ops->round_rate) |
| 351 | return -ENOSYS; |
| 352 | |
| 353 | return clk->ops->round_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 354 | } |
| 355 | EXPORT_SYMBOL(clk_round_rate); |
| 356 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 357 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 358 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 359 | if (!clk->ops->set_max_rate) |
| 360 | return -ENOSYS; |
| 361 | |
| 362 | return clk->ops->set_max_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 363 | } |
| 364 | EXPORT_SYMBOL(clk_set_max_rate); |
| 365 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 366 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 367 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 368 | if (!clk->ops->set_parent) |
| 369 | return 0; |
| 370 | |
| 371 | return clk->ops->set_parent(clk, parent); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 372 | } |
| 373 | EXPORT_SYMBOL(clk_set_parent); |
| 374 | |
| 375 | struct clk *clk_get_parent(struct clk *clk) |
| 376 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 377 | if (!clk->ops->get_parent) |
| 378 | return NULL; |
| 379 | |
| 380 | return clk->ops->get_parent(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL(clk_get_parent); |
| 383 | |
| 384 | int clk_set_flags(struct clk *clk, unsigned long flags) |
| 385 | { |
| 386 | if (clk == NULL || IS_ERR(clk)) |
| 387 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 388 | if (!clk->ops->set_flags) |
| 389 | return -ENOSYS; |
| 390 | |
| 391 | return clk->ops->set_flags(clk, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 392 | } |
| 393 | EXPORT_SYMBOL(clk_set_flags); |
| 394 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 395 | static struct clock_init_data __initdata *clk_init_data; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 396 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 397 | void __init msm_clock_init(struct clock_init_data *data) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 398 | { |
| 399 | unsigned n; |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame] | 400 | struct clk_lookup *clock_tbl; |
| 401 | size_t num_clocks; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 402 | |
| 403 | clk_init_data = data; |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 404 | if (clk_init_data->pre_init) |
| 405 | clk_init_data->pre_init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 406 | |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame] | 407 | clock_tbl = data->table; |
| 408 | num_clocks = data->size; |
| 409 | |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 410 | for (n = 0; n < num_clocks; n++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | struct clk *clk = clock_tbl[n].clk; |
| 412 | struct clk *parent = clk_get_parent(clk); |
| 413 | clk_set_parent(clk, parent); |
Matt Wagantall | 271a6cd | 2011-09-20 16:06:31 -0700 | [diff] [blame] | 414 | if (clk->ops->handoff && !(clk->flags & CLKFLAG_HANDOFF_RATE)) { |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame^] | 415 | if (clk->ops->handoff(clk) == HANDOFF_ENABLED_CLK) { |
Matt Wagantall | 271a6cd | 2011-09-20 16:06:31 -0700 | [diff] [blame] | 416 | clk->flags |= CLKFLAG_HANDOFF_RATE; |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 417 | clk_prepare_enable(clk); |
Matt Wagantall | 271a6cd | 2011-09-20 16:06:31 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 420 | } |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 421 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | clkdev_add_table(clock_tbl, num_clocks); |
Matt Wagantall | b64888f | 2012-04-02 21:35:07 -0700 | [diff] [blame] | 423 | |
| 424 | if (clk_init_data->post_init) |
| 425 | clk_init_data->post_init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 428 | /* |
| 429 | * The bootloader and/or AMSS may have left various clocks enabled. |
| 430 | * Disable any clocks that have not been explicitly enabled by a |
| 431 | * clk_enable() call and don't have the CLKFLAG_SKIP_AUTO_OFF flag. |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 432 | */ |
| 433 | static int __init clock_late_init(void) |
| 434 | { |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 435 | unsigned n, count = 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 436 | unsigned long flags; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 437 | int ret = 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 438 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 439 | clock_debug_init(clk_init_data); |
| 440 | for (n = 0; n < clk_init_data->size; n++) { |
| 441 | struct clk *clk = clk_init_data->table[n].clk; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 442 | bool handoff = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 444 | clock_debug_add(clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 445 | if (!(clk->flags & CLKFLAG_SKIP_AUTO_OFF)) { |
| 446 | spin_lock_irqsave(&clk->lock, flags); |
| 447 | if (!clk->count && clk->ops->auto_off) { |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 448 | count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 449 | clk->ops->auto_off(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 450 | } |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 451 | if (clk->flags & CLKFLAG_HANDOFF_RATE) { |
| 452 | clk->flags &= ~CLKFLAG_HANDOFF_RATE; |
| 453 | handoff = true; |
| 454 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 455 | spin_unlock_irqrestore(&clk->lock, flags); |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 456 | /* |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 457 | * Calling this outside the lock is safe since |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 458 | * it doesn't need to be atomic with the flag change. |
| 459 | */ |
| 460 | if (handoff) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 461 | clk_disable_unprepare(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 464 | pr_info("clock_late_init() disabled %d unused clocks\n", count); |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 465 | if (clk_init_data->late_init) |
| 466 | ret = clk_init_data->late_init(); |
| 467 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 468 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 469 | late_initcall(clock_late_init); |