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