| 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. | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 4 | * Copyright (c) 2007-2010, 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> | 
|  | 18 | #include <linux/init.h> | 
|  | 19 | #include <linux/module.h> | 
|  | 20 | #include <linux/list.h> | 
|  | 21 | #include <linux/err.h> | 
|  | 22 | #include <linux/clk.h> | 
|  | 23 | #include <linux/spinlock.h> | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 24 | #include <linux/debugfs.h> | 
|  | 25 | #include <linux/ctype.h> | 
|  | 26 | #include <linux/pm_qos_params.h> | 
|  | 27 | #include <mach/clk.h> | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | #include "clock.h" | 
|  | 30 | #include "proc_comm.h" | 
| Gregory Bean | 37a298f | 2010-04-30 22:22:07 -0700 | [diff] [blame] | 31 | #include "clock-7x30.h" | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | static DEFINE_MUTEX(clocks_mutex); | 
|  | 34 | static DEFINE_SPINLOCK(clocks_lock); | 
|  | 35 | static LIST_HEAD(clocks); | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 36 | struct clk *msm_clocks; | 
|  | 37 | unsigned msm_num_clocks; | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | /* | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 40 | * Bitmap of enabled clocks, excluding ACPU which is always | 
|  | 41 | * enabled | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 42 | */ | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 43 | static DECLARE_BITMAP(clock_map_enabled, NR_CLKS); | 
|  | 44 | static DEFINE_SPINLOCK(clock_map_lock); | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | /* | 
|  | 47 | * Standard clock functions defined in include/linux/clk.h | 
|  | 48 | */ | 
|  | 49 | struct clk *clk_get(struct device *dev, const char *id) | 
|  | 50 | { | 
|  | 51 | struct clk *clk; | 
|  | 52 |  | 
|  | 53 | mutex_lock(&clocks_mutex); | 
|  | 54 |  | 
|  | 55 | list_for_each_entry(clk, &clocks, list) | 
|  | 56 | if (!strcmp(id, clk->name) && clk->dev == dev) | 
|  | 57 | goto found_it; | 
|  | 58 |  | 
|  | 59 | list_for_each_entry(clk, &clocks, list) | 
|  | 60 | if (!strcmp(id, clk->name) && clk->dev == NULL) | 
|  | 61 | goto found_it; | 
|  | 62 |  | 
|  | 63 | clk = ERR_PTR(-ENOENT); | 
|  | 64 | found_it: | 
|  | 65 | mutex_unlock(&clocks_mutex); | 
|  | 66 | return clk; | 
|  | 67 | } | 
|  | 68 | EXPORT_SYMBOL(clk_get); | 
|  | 69 |  | 
|  | 70 | void clk_put(struct clk *clk) | 
|  | 71 | { | 
|  | 72 | } | 
|  | 73 | EXPORT_SYMBOL(clk_put); | 
|  | 74 |  | 
|  | 75 | int clk_enable(struct clk *clk) | 
|  | 76 | { | 
|  | 77 | unsigned long flags; | 
|  | 78 | spin_lock_irqsave(&clocks_lock, flags); | 
|  | 79 | clk->count++; | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 80 | if (clk->count == 1) { | 
|  | 81 | clk->ops->enable(clk->id); | 
|  | 82 | spin_lock(&clock_map_lock); | 
|  | 83 | clock_map_enabled[BIT_WORD(clk->id)] |= BIT_MASK(clk->id); | 
|  | 84 | spin_unlock(&clock_map_lock); | 
|  | 85 | } | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 86 | spin_unlock_irqrestore(&clocks_lock, flags); | 
|  | 87 | return 0; | 
|  | 88 | } | 
|  | 89 | EXPORT_SYMBOL(clk_enable); | 
|  | 90 |  | 
|  | 91 | void clk_disable(struct clk *clk) | 
|  | 92 | { | 
|  | 93 | unsigned long flags; | 
|  | 94 | spin_lock_irqsave(&clocks_lock, flags); | 
|  | 95 | BUG_ON(clk->count == 0); | 
|  | 96 | clk->count--; | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 97 | if (clk->count == 0) { | 
|  | 98 | clk->ops->disable(clk->id); | 
|  | 99 | spin_lock(&clock_map_lock); | 
|  | 100 | clock_map_enabled[BIT_WORD(clk->id)] &= ~BIT_MASK(clk->id); | 
|  | 101 | spin_unlock(&clock_map_lock); | 
|  | 102 | } | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 103 | spin_unlock_irqrestore(&clocks_lock, flags); | 
|  | 104 | } | 
|  | 105 | EXPORT_SYMBOL(clk_disable); | 
|  | 106 |  | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 107 | int clk_reset(struct clk *clk, enum clk_reset_action action) | 
|  | 108 | { | 
|  | 109 | if (!clk->ops->reset) | 
|  | 110 | clk->ops->reset = &pc_clk_reset; | 
|  | 111 | return clk->ops->reset(clk->remote_id, action); | 
|  | 112 | } | 
|  | 113 | EXPORT_SYMBOL(clk_reset); | 
|  | 114 |  | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 115 | unsigned long clk_get_rate(struct clk *clk) | 
|  | 116 | { | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 117 | return clk->ops->get_rate(clk->id); | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 118 | } | 
|  | 119 | EXPORT_SYMBOL(clk_get_rate); | 
|  | 120 |  | 
|  | 121 | int clk_set_rate(struct clk *clk, unsigned long rate) | 
|  | 122 | { | 
| Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 123 | int ret; | 
|  | 124 | if (clk->flags & CLKFLAG_MAX) { | 
|  | 125 | ret = clk->ops->set_max_rate(clk->id, rate); | 
|  | 126 | if (ret) | 
|  | 127 | return ret; | 
|  | 128 | } | 
|  | 129 | if (clk->flags & CLKFLAG_MIN) { | 
|  | 130 | ret = clk->ops->set_min_rate(clk->id, rate); | 
|  | 131 | if (ret) | 
|  | 132 | return ret; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN) | 
|  | 136 | return ret; | 
|  | 137 |  | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 138 | return clk->ops->set_rate(clk->id, rate); | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 139 | } | 
|  | 140 | EXPORT_SYMBOL(clk_set_rate); | 
|  | 141 |  | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 142 | long clk_round_rate(struct clk *clk, unsigned long rate) | 
|  | 143 | { | 
|  | 144 | return clk->ops->round_rate(clk->id, rate); | 
|  | 145 | } | 
|  | 146 | EXPORT_SYMBOL(clk_round_rate); | 
|  | 147 |  | 
|  | 148 | int clk_set_min_rate(struct clk *clk, unsigned long rate) | 
|  | 149 | { | 
|  | 150 | return clk->ops->set_min_rate(clk->id, rate); | 
|  | 151 | } | 
|  | 152 | EXPORT_SYMBOL(clk_set_min_rate); | 
|  | 153 |  | 
|  | 154 | int clk_set_max_rate(struct clk *clk, unsigned long rate) | 
|  | 155 | { | 
|  | 156 | return clk->ops->set_max_rate(clk->id, rate); | 
|  | 157 | } | 
|  | 158 | EXPORT_SYMBOL(clk_set_max_rate); | 
|  | 159 |  | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 160 | int clk_set_parent(struct clk *clk, struct clk *parent) | 
|  | 161 | { | 
|  | 162 | return -ENOSYS; | 
|  | 163 | } | 
|  | 164 | EXPORT_SYMBOL(clk_set_parent); | 
|  | 165 |  | 
|  | 166 | struct clk *clk_get_parent(struct clk *clk) | 
|  | 167 | { | 
|  | 168 | return ERR_PTR(-ENOSYS); | 
|  | 169 | } | 
|  | 170 | EXPORT_SYMBOL(clk_get_parent); | 
|  | 171 |  | 
|  | 172 | int clk_set_flags(struct clk *clk, unsigned long flags) | 
|  | 173 | { | 
|  | 174 | if (clk == NULL || IS_ERR(clk)) | 
|  | 175 | return -EINVAL; | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 176 | return clk->ops->set_flags(clk->id, flags); | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 177 | } | 
|  | 178 | EXPORT_SYMBOL(clk_set_flags); | 
|  | 179 |  | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 180 | /* EBI1 is the only shared clock that several clients want to vote on as of | 
|  | 181 | * this commit. If this changes in the future, then it might be better to | 
|  | 182 | * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more | 
|  | 183 | * generic to support different clocks. | 
|  | 184 | */ | 
|  | 185 | static struct clk *ebi1_clk; | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 186 |  | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 187 | static void __init set_clock_ops(struct clk *clk) | 
|  | 188 | { | 
|  | 189 | if (!clk->ops) { | 
|  | 190 | clk->ops = &clk_ops_pcom; | 
|  | 191 | clk->id = clk->remote_id; | 
|  | 192 | } | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks) | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 196 | { | 
|  | 197 | unsigned n; | 
|  | 198 |  | 
|  | 199 | spin_lock_init(&clocks_lock); | 
|  | 200 | mutex_lock(&clocks_mutex); | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 201 | msm_clocks = clock_tbl; | 
|  | 202 | msm_num_clocks = num_clocks; | 
|  | 203 | for (n = 0; n < msm_num_clocks; n++) { | 
|  | 204 | set_clock_ops(&msm_clocks[n]); | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 205 | list_add_tail(&msm_clocks[n].list, &clocks); | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 206 | } | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 207 | mutex_unlock(&clocks_mutex); | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 208 |  | 
|  | 209 | ebi1_clk = clk_get(NULL, "ebi1_clk"); | 
|  | 210 | BUG_ON(ebi1_clk == NULL); | 
|  | 211 |  | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 214 | #if defined(CONFIG_DEBUG_FS) | 
|  | 215 | static struct clk *msm_clock_get_nth(unsigned index) | 
|  | 216 | { | 
|  | 217 | if (index < msm_num_clocks) | 
|  | 218 | return msm_clocks + index; | 
|  | 219 | else | 
|  | 220 | return 0; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | static int clock_debug_rate_set(void *data, u64 val) | 
|  | 224 | { | 
|  | 225 | struct clk *clock = data; | 
|  | 226 | int ret; | 
|  | 227 |  | 
|  | 228 | /* Only increases to max rate will succeed, but that's actually good | 
|  | 229 | * for debugging purposes. So we don't check for error. */ | 
|  | 230 | if (clock->flags & CLK_MAX) | 
|  | 231 | clk_set_max_rate(clock, val); | 
|  | 232 | if (clock->flags & CLK_MIN) | 
|  | 233 | ret = clk_set_min_rate(clock, val); | 
|  | 234 | else | 
|  | 235 | ret = clk_set_rate(clock, val); | 
|  | 236 | if (ret != 0) | 
|  | 237 | printk(KERN_ERR "clk_set%s_rate failed (%d)\n", | 
|  | 238 | (clock->flags & CLK_MIN) ? "_min" : "", ret); | 
|  | 239 | return ret; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | static int clock_debug_rate_get(void *data, u64 *val) | 
|  | 243 | { | 
|  | 244 | struct clk *clock = data; | 
|  | 245 | *val = clk_get_rate(clock); | 
|  | 246 | return 0; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | static int clock_debug_enable_set(void *data, u64 val) | 
|  | 250 | { | 
|  | 251 | struct clk *clock = data; | 
|  | 252 | int rc = 0; | 
|  | 253 |  | 
|  | 254 | if (val) | 
|  | 255 | rc = clock->ops->enable(clock->id); | 
|  | 256 | else | 
|  | 257 | clock->ops->disable(clock->id); | 
|  | 258 |  | 
|  | 259 | return rc; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | static int clock_debug_enable_get(void *data, u64 *val) | 
|  | 263 | { | 
|  | 264 | struct clk *clock = data; | 
|  | 265 |  | 
|  | 266 | *val = clock->ops->is_enabled(clock->id); | 
|  | 267 |  | 
|  | 268 | return 0; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | static int clock_debug_local_get(void *data, u64 *val) | 
|  | 272 | { | 
|  | 273 | struct clk *clock = data; | 
|  | 274 |  | 
|  | 275 | *val = clock->ops != &clk_ops_pcom; | 
|  | 276 |  | 
|  | 277 | return 0; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get, | 
|  | 281 | clock_debug_rate_set, "%llu\n"); | 
|  | 282 | DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get, | 
|  | 283 | clock_debug_enable_set, "%llu\n"); | 
|  | 284 | DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get, | 
|  | 285 | NULL, "%llu\n"); | 
|  | 286 |  | 
|  | 287 | static int __init clock_debug_init(void) | 
|  | 288 | { | 
|  | 289 | struct dentry *dent_rate, *dent_enable, *dent_local; | 
|  | 290 | struct clk *clock; | 
|  | 291 | unsigned n = 0; | 
|  | 292 | char temp[50], *ptr; | 
|  | 293 |  | 
|  | 294 | dent_rate = debugfs_create_dir("clk_rate", 0); | 
|  | 295 | if (IS_ERR(dent_rate)) | 
|  | 296 | return PTR_ERR(dent_rate); | 
|  | 297 |  | 
|  | 298 | dent_enable = debugfs_create_dir("clk_enable", 0); | 
|  | 299 | if (IS_ERR(dent_enable)) | 
|  | 300 | return PTR_ERR(dent_enable); | 
|  | 301 |  | 
|  | 302 | dent_local = debugfs_create_dir("clk_local", NULL); | 
|  | 303 | if (IS_ERR(dent_local)) | 
|  | 304 | return PTR_ERR(dent_local); | 
|  | 305 |  | 
|  | 306 | while ((clock = msm_clock_get_nth(n++)) != 0) { | 
|  | 307 | strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1); | 
|  | 308 | for (ptr = temp; *ptr; ptr++) | 
|  | 309 | *ptr = tolower(*ptr); | 
|  | 310 | debugfs_create_file(temp, 0644, dent_rate, | 
|  | 311 | clock, &clock_rate_fops); | 
|  | 312 | debugfs_create_file(temp, 0644, dent_enable, | 
|  | 313 | clock, &clock_enable_fops); | 
|  | 314 | debugfs_create_file(temp, S_IRUGO, dent_local, | 
|  | 315 | clock, &clock_local_fops); | 
|  | 316 | } | 
|  | 317 | return 0; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | device_initcall(clock_debug_init); | 
|  | 321 | #endif | 
|  | 322 |  | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 323 | /* The bootloader and/or AMSS may have left various clocks enabled. | 
|  | 324 | * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have | 
|  | 325 | * not been explicitly enabled by a clk_enable() call. | 
|  | 326 | */ | 
|  | 327 | static int __init clock_late_init(void) | 
|  | 328 | { | 
|  | 329 | unsigned long flags; | 
|  | 330 | struct clk *clk; | 
|  | 331 | unsigned count = 0; | 
|  | 332 |  | 
|  | 333 | mutex_lock(&clocks_mutex); | 
|  | 334 | list_for_each_entry(clk, &clocks, list) { | 
|  | 335 | if (clk->flags & CLKFLAG_AUTO_OFF) { | 
|  | 336 | spin_lock_irqsave(&clocks_lock, flags); | 
|  | 337 | if (!clk->count) { | 
|  | 338 | count++; | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 339 | clk->ops->auto_off(clk->id); | 
| Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 340 | } | 
|  | 341 | spin_unlock_irqrestore(&clocks_lock, flags); | 
|  | 342 | } | 
|  | 343 | } | 
|  | 344 | mutex_unlock(&clocks_mutex); | 
|  | 345 | pr_info("clock_late_init() disabled %d unused clocks\n", count); | 
|  | 346 | return 0; | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | late_initcall(clock_late_init); | 
| Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 350 |  |