Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> |
| 3 | * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Standard functionality for the common clock API. See Documentation/clk.txt |
| 10 | */ |
| 11 | |
| 12 | #include <linux/clk-private.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/slab.h> |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 19 | #include <linux/of.h> |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 21 | #include <linux/init.h> |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame^] | 22 | #include <linux/sched.h> |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 23 | |
| 24 | static DEFINE_SPINLOCK(enable_lock); |
| 25 | static DEFINE_MUTEX(prepare_lock); |
| 26 | |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame^] | 27 | static struct task_struct *prepare_owner; |
| 28 | static struct task_struct *enable_owner; |
| 29 | |
| 30 | static int prepare_refcnt; |
| 31 | static int enable_refcnt; |
| 32 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 33 | static HLIST_HEAD(clk_root_list); |
| 34 | static HLIST_HEAD(clk_orphan_list); |
| 35 | static LIST_HEAD(clk_notifier_list); |
| 36 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 37 | /*** locking ***/ |
| 38 | static void clk_prepare_lock(void) |
| 39 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame^] | 40 | if (!mutex_trylock(&prepare_lock)) { |
| 41 | if (prepare_owner == current) { |
| 42 | prepare_refcnt++; |
| 43 | return; |
| 44 | } |
| 45 | mutex_lock(&prepare_lock); |
| 46 | } |
| 47 | WARN_ON_ONCE(prepare_owner != NULL); |
| 48 | WARN_ON_ONCE(prepare_refcnt != 0); |
| 49 | prepare_owner = current; |
| 50 | prepare_refcnt = 1; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void clk_prepare_unlock(void) |
| 54 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame^] | 55 | WARN_ON_ONCE(prepare_owner != current); |
| 56 | WARN_ON_ONCE(prepare_refcnt == 0); |
| 57 | |
| 58 | if (--prepare_refcnt) |
| 59 | return; |
| 60 | prepare_owner = NULL; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 61 | mutex_unlock(&prepare_lock); |
| 62 | } |
| 63 | |
| 64 | static unsigned long clk_enable_lock(void) |
| 65 | { |
| 66 | unsigned long flags; |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame^] | 67 | |
| 68 | if (!spin_trylock_irqsave(&enable_lock, flags)) { |
| 69 | if (enable_owner == current) { |
| 70 | enable_refcnt++; |
| 71 | return flags; |
| 72 | } |
| 73 | spin_lock_irqsave(&enable_lock, flags); |
| 74 | } |
| 75 | WARN_ON_ONCE(enable_owner != NULL); |
| 76 | WARN_ON_ONCE(enable_refcnt != 0); |
| 77 | enable_owner = current; |
| 78 | enable_refcnt = 1; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 79 | return flags; |
| 80 | } |
| 81 | |
| 82 | static void clk_enable_unlock(unsigned long flags) |
| 83 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame^] | 84 | WARN_ON_ONCE(enable_owner != current); |
| 85 | WARN_ON_ONCE(enable_refcnt == 0); |
| 86 | |
| 87 | if (--enable_refcnt) |
| 88 | return; |
| 89 | enable_owner = NULL; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 90 | spin_unlock_irqrestore(&enable_lock, flags); |
| 91 | } |
| 92 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 93 | /*** debugfs support ***/ |
| 94 | |
| 95 | #ifdef CONFIG_COMMON_CLK_DEBUG |
| 96 | #include <linux/debugfs.h> |
| 97 | |
| 98 | static struct dentry *rootdir; |
| 99 | static struct dentry *orphandir; |
| 100 | static int inited = 0; |
| 101 | |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 102 | static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level) |
| 103 | { |
| 104 | if (!c) |
| 105 | return; |
| 106 | |
| 107 | seq_printf(s, "%*s%-*s %-11d %-12d %-10lu", |
| 108 | level * 3 + 1, "", |
| 109 | 30 - level * 3, c->name, |
| 110 | c->enable_count, c->prepare_count, c->rate); |
| 111 | seq_printf(s, "\n"); |
| 112 | } |
| 113 | |
| 114 | static void clk_summary_show_subtree(struct seq_file *s, struct clk *c, |
| 115 | int level) |
| 116 | { |
| 117 | struct clk *child; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 118 | |
| 119 | if (!c) |
| 120 | return; |
| 121 | |
| 122 | clk_summary_show_one(s, c, level); |
| 123 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 124 | hlist_for_each_entry(child, &c->children, child_node) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 125 | clk_summary_show_subtree(s, child, level + 1); |
| 126 | } |
| 127 | |
| 128 | static int clk_summary_show(struct seq_file *s, void *data) |
| 129 | { |
| 130 | struct clk *c; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 131 | |
| 132 | seq_printf(s, " clock enable_cnt prepare_cnt rate\n"); |
| 133 | seq_printf(s, "---------------------------------------------------------------------\n"); |
| 134 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 135 | clk_prepare_lock(); |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 136 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 137 | hlist_for_each_entry(c, &clk_root_list, child_node) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 138 | clk_summary_show_subtree(s, c, 0); |
| 139 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 140 | hlist_for_each_entry(c, &clk_orphan_list, child_node) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 141 | clk_summary_show_subtree(s, c, 0); |
| 142 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 143 | clk_prepare_unlock(); |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static int clk_summary_open(struct inode *inode, struct file *file) |
| 150 | { |
| 151 | return single_open(file, clk_summary_show, inode->i_private); |
| 152 | } |
| 153 | |
| 154 | static const struct file_operations clk_summary_fops = { |
| 155 | .open = clk_summary_open, |
| 156 | .read = seq_read, |
| 157 | .llseek = seq_lseek, |
| 158 | .release = single_release, |
| 159 | }; |
| 160 | |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 161 | static void clk_dump_one(struct seq_file *s, struct clk *c, int level) |
| 162 | { |
| 163 | if (!c) |
| 164 | return; |
| 165 | |
| 166 | seq_printf(s, "\"%s\": { ", c->name); |
| 167 | seq_printf(s, "\"enable_count\": %d,", c->enable_count); |
| 168 | seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); |
| 169 | seq_printf(s, "\"rate\": %lu", c->rate); |
| 170 | } |
| 171 | |
| 172 | static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level) |
| 173 | { |
| 174 | struct clk *child; |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 175 | |
| 176 | if (!c) |
| 177 | return; |
| 178 | |
| 179 | clk_dump_one(s, c, level); |
| 180 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 181 | hlist_for_each_entry(child, &c->children, child_node) { |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 182 | seq_printf(s, ","); |
| 183 | clk_dump_subtree(s, child, level + 1); |
| 184 | } |
| 185 | |
| 186 | seq_printf(s, "}"); |
| 187 | } |
| 188 | |
| 189 | static int clk_dump(struct seq_file *s, void *data) |
| 190 | { |
| 191 | struct clk *c; |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 192 | bool first_node = true; |
| 193 | |
| 194 | seq_printf(s, "{"); |
| 195 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 196 | clk_prepare_lock(); |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 197 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 198 | hlist_for_each_entry(c, &clk_root_list, child_node) { |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 199 | if (!first_node) |
| 200 | seq_printf(s, ","); |
| 201 | first_node = false; |
| 202 | clk_dump_subtree(s, c, 0); |
| 203 | } |
| 204 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 205 | hlist_for_each_entry(c, &clk_orphan_list, child_node) { |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 206 | seq_printf(s, ","); |
| 207 | clk_dump_subtree(s, c, 0); |
| 208 | } |
| 209 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 210 | clk_prepare_unlock(); |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 211 | |
| 212 | seq_printf(s, "}"); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static int clk_dump_open(struct inode *inode, struct file *file) |
| 218 | { |
| 219 | return single_open(file, clk_dump, inode->i_private); |
| 220 | } |
| 221 | |
| 222 | static const struct file_operations clk_dump_fops = { |
| 223 | .open = clk_dump_open, |
| 224 | .read = seq_read, |
| 225 | .llseek = seq_lseek, |
| 226 | .release = single_release, |
| 227 | }; |
| 228 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 229 | /* caller must hold prepare_lock */ |
| 230 | static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry) |
| 231 | { |
| 232 | struct dentry *d; |
| 233 | int ret = -ENOMEM; |
| 234 | |
| 235 | if (!clk || !pdentry) { |
| 236 | ret = -EINVAL; |
| 237 | goto out; |
| 238 | } |
| 239 | |
| 240 | d = debugfs_create_dir(clk->name, pdentry); |
| 241 | if (!d) |
| 242 | goto out; |
| 243 | |
| 244 | clk->dentry = d; |
| 245 | |
| 246 | d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry, |
| 247 | (u32 *)&clk->rate); |
| 248 | if (!d) |
| 249 | goto err_out; |
| 250 | |
| 251 | d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry, |
| 252 | (u32 *)&clk->flags); |
| 253 | if (!d) |
| 254 | goto err_out; |
| 255 | |
| 256 | d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry, |
| 257 | (u32 *)&clk->prepare_count); |
| 258 | if (!d) |
| 259 | goto err_out; |
| 260 | |
| 261 | d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry, |
| 262 | (u32 *)&clk->enable_count); |
| 263 | if (!d) |
| 264 | goto err_out; |
| 265 | |
| 266 | d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry, |
| 267 | (u32 *)&clk->notifier_count); |
| 268 | if (!d) |
| 269 | goto err_out; |
| 270 | |
| 271 | ret = 0; |
| 272 | goto out; |
| 273 | |
| 274 | err_out: |
| 275 | debugfs_remove(clk->dentry); |
| 276 | out: |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | /* caller must hold prepare_lock */ |
| 281 | static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry) |
| 282 | { |
| 283 | struct clk *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 284 | int ret = -EINVAL;; |
| 285 | |
| 286 | if (!clk || !pdentry) |
| 287 | goto out; |
| 288 | |
| 289 | ret = clk_debug_create_one(clk, pdentry); |
| 290 | |
| 291 | if (ret) |
| 292 | goto out; |
| 293 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 294 | hlist_for_each_entry(child, &clk->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 295 | clk_debug_create_subtree(child, clk->dentry); |
| 296 | |
| 297 | ret = 0; |
| 298 | out: |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * clk_debug_register - add a clk node to the debugfs clk tree |
| 304 | * @clk: the clk being added to the debugfs clk tree |
| 305 | * |
| 306 | * Dynamically adds a clk to the debugfs clk tree if debugfs has been |
| 307 | * initialized. Otherwise it bails out early since the debugfs clk tree |
| 308 | * will be created lazily by clk_debug_init as part of a late_initcall. |
| 309 | * |
| 310 | * Caller must hold prepare_lock. Only clk_init calls this function (so |
| 311 | * far) so this is taken care. |
| 312 | */ |
| 313 | static int clk_debug_register(struct clk *clk) |
| 314 | { |
| 315 | struct clk *parent; |
| 316 | struct dentry *pdentry; |
| 317 | int ret = 0; |
| 318 | |
| 319 | if (!inited) |
| 320 | goto out; |
| 321 | |
| 322 | parent = clk->parent; |
| 323 | |
| 324 | /* |
| 325 | * Check to see if a clk is a root clk. Also check that it is |
| 326 | * safe to add this clk to debugfs |
| 327 | */ |
| 328 | if (!parent) |
| 329 | if (clk->flags & CLK_IS_ROOT) |
| 330 | pdentry = rootdir; |
| 331 | else |
| 332 | pdentry = orphandir; |
| 333 | else |
| 334 | if (parent->dentry) |
| 335 | pdentry = parent->dentry; |
| 336 | else |
| 337 | goto out; |
| 338 | |
| 339 | ret = clk_debug_create_subtree(clk, pdentry); |
| 340 | |
| 341 | out: |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * clk_debug_init - lazily create the debugfs clk tree visualization |
| 347 | * |
| 348 | * clks are often initialized very early during boot before memory can |
| 349 | * be dynamically allocated and well before debugfs is setup. |
| 350 | * clk_debug_init walks the clk tree hierarchy while holding |
| 351 | * prepare_lock and creates the topology as part of a late_initcall, |
| 352 | * thus insuring that clks initialized very early will still be |
| 353 | * represented in the debugfs clk tree. This function should only be |
| 354 | * called once at boot-time, and all other clks added dynamically will |
| 355 | * be done so with clk_debug_register. |
| 356 | */ |
| 357 | static int __init clk_debug_init(void) |
| 358 | { |
| 359 | struct clk *clk; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 360 | struct dentry *d; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 361 | |
| 362 | rootdir = debugfs_create_dir("clk", NULL); |
| 363 | |
| 364 | if (!rootdir) |
| 365 | return -ENOMEM; |
| 366 | |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 367 | d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL, |
| 368 | &clk_summary_fops); |
| 369 | if (!d) |
| 370 | return -ENOMEM; |
| 371 | |
Prashant Gaikwad | bddca89 | 2012-12-26 19:16:23 +0530 | [diff] [blame] | 372 | d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL, |
| 373 | &clk_dump_fops); |
| 374 | if (!d) |
| 375 | return -ENOMEM; |
| 376 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 377 | orphandir = debugfs_create_dir("orphans", rootdir); |
| 378 | |
| 379 | if (!orphandir) |
| 380 | return -ENOMEM; |
| 381 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 382 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 383 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 384 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 385 | clk_debug_create_subtree(clk, rootdir); |
| 386 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 387 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 388 | clk_debug_create_subtree(clk, orphandir); |
| 389 | |
| 390 | inited = 1; |
| 391 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 392 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | late_initcall(clk_debug_init); |
| 397 | #else |
| 398 | static inline int clk_debug_register(struct clk *clk) { return 0; } |
Mike Turquette | 70d347e | 2012-03-26 11:53:47 -0700 | [diff] [blame] | 399 | #endif |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 400 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 401 | /* caller must hold prepare_lock */ |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 402 | static void clk_unprepare_unused_subtree(struct clk *clk) |
| 403 | { |
| 404 | struct clk *child; |
| 405 | |
| 406 | if (!clk) |
| 407 | return; |
| 408 | |
| 409 | hlist_for_each_entry(child, &clk->children, child_node) |
| 410 | clk_unprepare_unused_subtree(child); |
| 411 | |
| 412 | if (clk->prepare_count) |
| 413 | return; |
| 414 | |
| 415 | if (clk->flags & CLK_IGNORE_UNUSED) |
| 416 | return; |
| 417 | |
Ulf Hansson | 3cc8247 | 2013-03-12 20:26:04 +0100 | [diff] [blame] | 418 | if (__clk_is_prepared(clk)) { |
| 419 | if (clk->ops->unprepare_unused) |
| 420 | clk->ops->unprepare_unused(clk->hw); |
| 421 | else if (clk->ops->unprepare) |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 422 | clk->ops->unprepare(clk->hw); |
Ulf Hansson | 3cc8247 | 2013-03-12 20:26:04 +0100 | [diff] [blame] | 423 | } |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* caller must hold prepare_lock */ |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 427 | static void clk_disable_unused_subtree(struct clk *clk) |
| 428 | { |
| 429 | struct clk *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 430 | unsigned long flags; |
| 431 | |
| 432 | if (!clk) |
| 433 | goto out; |
| 434 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 435 | hlist_for_each_entry(child, &clk->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 436 | clk_disable_unused_subtree(child); |
| 437 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 438 | flags = clk_enable_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 439 | |
| 440 | if (clk->enable_count) |
| 441 | goto unlock_out; |
| 442 | |
| 443 | if (clk->flags & CLK_IGNORE_UNUSED) |
| 444 | goto unlock_out; |
| 445 | |
Mike Turquette | 7c045a5 | 2012-12-04 11:00:35 -0800 | [diff] [blame] | 446 | /* |
| 447 | * some gate clocks have special needs during the disable-unused |
| 448 | * sequence. call .disable_unused if available, otherwise fall |
| 449 | * back to .disable |
| 450 | */ |
| 451 | if (__clk_is_enabled(clk)) { |
| 452 | if (clk->ops->disable_unused) |
| 453 | clk->ops->disable_unused(clk->hw); |
| 454 | else if (clk->ops->disable) |
| 455 | clk->ops->disable(clk->hw); |
| 456 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 457 | |
| 458 | unlock_out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 459 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 460 | |
| 461 | out: |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | static int clk_disable_unused(void) |
| 466 | { |
| 467 | struct clk *clk; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 468 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 469 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 470 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 471 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 472 | clk_disable_unused_subtree(clk); |
| 473 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 474 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 475 | clk_disable_unused_subtree(clk); |
| 476 | |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 477 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
| 478 | clk_unprepare_unused_subtree(clk); |
| 479 | |
| 480 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
| 481 | clk_unprepare_unused_subtree(clk); |
| 482 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 483 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | late_initcall(clk_disable_unused); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 488 | |
| 489 | /*** helper functions ***/ |
| 490 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 491 | const char *__clk_get_name(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 492 | { |
| 493 | return !clk ? NULL : clk->name; |
| 494 | } |
Niels de Vos | 4895084 | 2012-12-13 13:12:25 +0100 | [diff] [blame] | 495 | EXPORT_SYMBOL_GPL(__clk_get_name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 496 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 497 | struct clk_hw *__clk_get_hw(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 498 | { |
| 499 | return !clk ? NULL : clk->hw; |
| 500 | } |
| 501 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 502 | u8 __clk_get_num_parents(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 503 | { |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 504 | return !clk ? 0 : clk->num_parents; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 507 | struct clk *__clk_get_parent(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 508 | { |
| 509 | return !clk ? NULL : clk->parent; |
| 510 | } |
| 511 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 512 | unsigned int __clk_get_enable_count(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 513 | { |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 514 | return !clk ? 0 : clk->enable_count; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 517 | unsigned int __clk_get_prepare_count(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 518 | { |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 519 | return !clk ? 0 : clk->prepare_count; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | unsigned long __clk_get_rate(struct clk *clk) |
| 523 | { |
| 524 | unsigned long ret; |
| 525 | |
| 526 | if (!clk) { |
Rajendra Nayak | 34e44fe | 2012-03-26 19:01:48 +0530 | [diff] [blame] | 527 | ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 528 | goto out; |
| 529 | } |
| 530 | |
| 531 | ret = clk->rate; |
| 532 | |
| 533 | if (clk->flags & CLK_IS_ROOT) |
| 534 | goto out; |
| 535 | |
| 536 | if (!clk->parent) |
Rajendra Nayak | 34e44fe | 2012-03-26 19:01:48 +0530 | [diff] [blame] | 537 | ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 538 | |
| 539 | out: |
| 540 | return ret; |
| 541 | } |
| 542 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 543 | unsigned long __clk_get_flags(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 544 | { |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 545 | return !clk ? 0 : clk->flags; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Ulf Hansson | 3d6ee28 | 2013-03-12 20:26:02 +0100 | [diff] [blame] | 548 | bool __clk_is_prepared(struct clk *clk) |
| 549 | { |
| 550 | int ret; |
| 551 | |
| 552 | if (!clk) |
| 553 | return false; |
| 554 | |
| 555 | /* |
| 556 | * .is_prepared is optional for clocks that can prepare |
| 557 | * fall back to software usage counter if it is missing |
| 558 | */ |
| 559 | if (!clk->ops->is_prepared) { |
| 560 | ret = clk->prepare_count ? 1 : 0; |
| 561 | goto out; |
| 562 | } |
| 563 | |
| 564 | ret = clk->ops->is_prepared(clk->hw); |
| 565 | out: |
| 566 | return !!ret; |
| 567 | } |
| 568 | |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 569 | bool __clk_is_enabled(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 570 | { |
| 571 | int ret; |
| 572 | |
| 573 | if (!clk) |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 574 | return false; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * .is_enabled is only mandatory for clocks that gate |
| 578 | * fall back to software usage counter if .is_enabled is missing |
| 579 | */ |
| 580 | if (!clk->ops->is_enabled) { |
| 581 | ret = clk->enable_count ? 1 : 0; |
| 582 | goto out; |
| 583 | } |
| 584 | |
| 585 | ret = clk->ops->is_enabled(clk->hw); |
| 586 | out: |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 587 | return !!ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk) |
| 591 | { |
| 592 | struct clk *child; |
| 593 | struct clk *ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 594 | |
| 595 | if (!strcmp(clk->name, name)) |
| 596 | return clk; |
| 597 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 598 | hlist_for_each_entry(child, &clk->children, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 599 | ret = __clk_lookup_subtree(name, child); |
| 600 | if (ret) |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | struct clk *__clk_lookup(const char *name) |
| 608 | { |
| 609 | struct clk *root_clk; |
| 610 | struct clk *ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 611 | |
| 612 | if (!name) |
| 613 | return NULL; |
| 614 | |
| 615 | /* search the 'proper' clk tree first */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 616 | hlist_for_each_entry(root_clk, &clk_root_list, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 617 | ret = __clk_lookup_subtree(name, root_clk); |
| 618 | if (ret) |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | /* if not found, then search the orphan tree */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 623 | hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 624 | ret = __clk_lookup_subtree(name, root_clk); |
| 625 | if (ret) |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | /*** clk api ***/ |
| 633 | |
| 634 | void __clk_unprepare(struct clk *clk) |
| 635 | { |
| 636 | if (!clk) |
| 637 | return; |
| 638 | |
| 639 | if (WARN_ON(clk->prepare_count == 0)) |
| 640 | return; |
| 641 | |
| 642 | if (--clk->prepare_count > 0) |
| 643 | return; |
| 644 | |
| 645 | WARN_ON(clk->enable_count > 0); |
| 646 | |
| 647 | if (clk->ops->unprepare) |
| 648 | clk->ops->unprepare(clk->hw); |
| 649 | |
| 650 | __clk_unprepare(clk->parent); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * clk_unprepare - undo preparation of a clock source |
| 655 | * @clk: the clk being unprepare |
| 656 | * |
| 657 | * clk_unprepare may sleep, which differentiates it from clk_disable. In a |
| 658 | * simple case, clk_unprepare can be used instead of clk_disable to gate a clk |
| 659 | * if the operation may sleep. One example is a clk which is accessed over |
| 660 | * I2c. In the complex case a clk gate operation may require a fast and a slow |
| 661 | * part. It is this reason that clk_unprepare and clk_disable are not mutually |
| 662 | * exclusive. In fact clk_disable must be called before clk_unprepare. |
| 663 | */ |
| 664 | void clk_unprepare(struct clk *clk) |
| 665 | { |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 666 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 667 | __clk_unprepare(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 668 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 669 | } |
| 670 | EXPORT_SYMBOL_GPL(clk_unprepare); |
| 671 | |
| 672 | int __clk_prepare(struct clk *clk) |
| 673 | { |
| 674 | int ret = 0; |
| 675 | |
| 676 | if (!clk) |
| 677 | return 0; |
| 678 | |
| 679 | if (clk->prepare_count == 0) { |
| 680 | ret = __clk_prepare(clk->parent); |
| 681 | if (ret) |
| 682 | return ret; |
| 683 | |
| 684 | if (clk->ops->prepare) { |
| 685 | ret = clk->ops->prepare(clk->hw); |
| 686 | if (ret) { |
| 687 | __clk_unprepare(clk->parent); |
| 688 | return ret; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | clk->prepare_count++; |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * clk_prepare - prepare a clock source |
| 700 | * @clk: the clk being prepared |
| 701 | * |
| 702 | * clk_prepare may sleep, which differentiates it from clk_enable. In a simple |
| 703 | * case, clk_prepare can be used instead of clk_enable to ungate a clk if the |
| 704 | * operation may sleep. One example is a clk which is accessed over I2c. In |
| 705 | * the complex case a clk ungate operation may require a fast and a slow part. |
| 706 | * It is this reason that clk_prepare and clk_enable are not mutually |
| 707 | * exclusive. In fact clk_prepare must be called before clk_enable. |
| 708 | * Returns 0 on success, -EERROR otherwise. |
| 709 | */ |
| 710 | int clk_prepare(struct clk *clk) |
| 711 | { |
| 712 | int ret; |
| 713 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 714 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 715 | ret = __clk_prepare(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 716 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 717 | |
| 718 | return ret; |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(clk_prepare); |
| 721 | |
| 722 | static void __clk_disable(struct clk *clk) |
| 723 | { |
| 724 | if (!clk) |
| 725 | return; |
| 726 | |
Fengguang Wu | e47c6a3 | 2012-07-30 14:39:54 -0700 | [diff] [blame] | 727 | if (WARN_ON(IS_ERR(clk))) |
| 728 | return; |
| 729 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 730 | if (WARN_ON(clk->enable_count == 0)) |
| 731 | return; |
| 732 | |
| 733 | if (--clk->enable_count > 0) |
| 734 | return; |
| 735 | |
| 736 | if (clk->ops->disable) |
| 737 | clk->ops->disable(clk->hw); |
| 738 | |
| 739 | __clk_disable(clk->parent); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * clk_disable - gate a clock |
| 744 | * @clk: the clk being gated |
| 745 | * |
| 746 | * clk_disable must not sleep, which differentiates it from clk_unprepare. In |
| 747 | * a simple case, clk_disable can be used instead of clk_unprepare to gate a |
| 748 | * clk if the operation is fast and will never sleep. One example is a |
| 749 | * SoC-internal clk which is controlled via simple register writes. In the |
| 750 | * complex case a clk gate operation may require a fast and a slow part. It is |
| 751 | * this reason that clk_unprepare and clk_disable are not mutually exclusive. |
| 752 | * In fact clk_disable must be called before clk_unprepare. |
| 753 | */ |
| 754 | void clk_disable(struct clk *clk) |
| 755 | { |
| 756 | unsigned long flags; |
| 757 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 758 | flags = clk_enable_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 759 | __clk_disable(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 760 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 761 | } |
| 762 | EXPORT_SYMBOL_GPL(clk_disable); |
| 763 | |
| 764 | static int __clk_enable(struct clk *clk) |
| 765 | { |
| 766 | int ret = 0; |
| 767 | |
| 768 | if (!clk) |
| 769 | return 0; |
| 770 | |
| 771 | if (WARN_ON(clk->prepare_count == 0)) |
| 772 | return -ESHUTDOWN; |
| 773 | |
| 774 | if (clk->enable_count == 0) { |
| 775 | ret = __clk_enable(clk->parent); |
| 776 | |
| 777 | if (ret) |
| 778 | return ret; |
| 779 | |
| 780 | if (clk->ops->enable) { |
| 781 | ret = clk->ops->enable(clk->hw); |
| 782 | if (ret) { |
| 783 | __clk_disable(clk->parent); |
| 784 | return ret; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | clk->enable_count++; |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * clk_enable - ungate a clock |
| 795 | * @clk: the clk being ungated |
| 796 | * |
| 797 | * clk_enable must not sleep, which differentiates it from clk_prepare. In a |
| 798 | * simple case, clk_enable can be used instead of clk_prepare to ungate a clk |
| 799 | * if the operation will never sleep. One example is a SoC-internal clk which |
| 800 | * is controlled via simple register writes. In the complex case a clk ungate |
| 801 | * operation may require a fast and a slow part. It is this reason that |
| 802 | * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare |
| 803 | * must be called before clk_enable. Returns 0 on success, -EERROR |
| 804 | * otherwise. |
| 805 | */ |
| 806 | int clk_enable(struct clk *clk) |
| 807 | { |
| 808 | unsigned long flags; |
| 809 | int ret; |
| 810 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 811 | flags = clk_enable_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 812 | ret = __clk_enable(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 813 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 814 | |
| 815 | return ret; |
| 816 | } |
| 817 | EXPORT_SYMBOL_GPL(clk_enable); |
| 818 | |
| 819 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 820 | * __clk_round_rate - round the given rate for a clk |
| 821 | * @clk: round the rate of this clock |
| 822 | * |
| 823 | * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate |
| 824 | */ |
| 825 | unsigned long __clk_round_rate(struct clk *clk, unsigned long rate) |
| 826 | { |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 827 | unsigned long parent_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 828 | |
| 829 | if (!clk) |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 830 | return 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 831 | |
Shawn Guo | f4d8af2 | 2012-04-12 20:50:19 +0800 | [diff] [blame] | 832 | if (!clk->ops->round_rate) { |
| 833 | if (clk->flags & CLK_SET_RATE_PARENT) |
| 834 | return __clk_round_rate(clk->parent, rate); |
| 835 | else |
| 836 | return clk->rate; |
| 837 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 838 | |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 839 | if (clk->parent) |
| 840 | parent_rate = clk->parent->rate; |
| 841 | |
| 842 | return clk->ops->round_rate(clk->hw, rate, &parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
| 846 | * clk_round_rate - round the given rate for a clk |
| 847 | * @clk: the clk for which we are rounding a rate |
| 848 | * @rate: the rate which is to be rounded |
| 849 | * |
| 850 | * Takes in a rate as input and rounds it to a rate that the clk can actually |
| 851 | * use which is then returned. If clk doesn't support round_rate operation |
| 852 | * then the parent rate is returned. |
| 853 | */ |
| 854 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 855 | { |
| 856 | unsigned long ret; |
| 857 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 858 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 859 | ret = __clk_round_rate(clk, rate); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 860 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 861 | |
| 862 | return ret; |
| 863 | } |
| 864 | EXPORT_SYMBOL_GPL(clk_round_rate); |
| 865 | |
| 866 | /** |
| 867 | * __clk_notify - call clk notifier chain |
| 868 | * @clk: struct clk * that is changing rate |
| 869 | * @msg: clk notifier type (see include/linux/clk.h) |
| 870 | * @old_rate: old clk rate |
| 871 | * @new_rate: new clk rate |
| 872 | * |
| 873 | * Triggers a notifier call chain on the clk rate-change notification |
| 874 | * for 'clk'. Passes a pointer to the struct clk and the previous |
| 875 | * and current rates to the notifier callback. Intended to be called by |
| 876 | * internal clock code only. Returns NOTIFY_DONE from the last driver |
| 877 | * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if |
| 878 | * a driver returns that. |
| 879 | */ |
| 880 | static int __clk_notify(struct clk *clk, unsigned long msg, |
| 881 | unsigned long old_rate, unsigned long new_rate) |
| 882 | { |
| 883 | struct clk_notifier *cn; |
| 884 | struct clk_notifier_data cnd; |
| 885 | int ret = NOTIFY_DONE; |
| 886 | |
| 887 | cnd.clk = clk; |
| 888 | cnd.old_rate = old_rate; |
| 889 | cnd.new_rate = new_rate; |
| 890 | |
| 891 | list_for_each_entry(cn, &clk_notifier_list, node) { |
| 892 | if (cn->clk == clk) { |
| 893 | ret = srcu_notifier_call_chain(&cn->notifier_head, msg, |
| 894 | &cnd); |
| 895 | break; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | return ret; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * __clk_recalc_rates |
| 904 | * @clk: first clk in the subtree |
| 905 | * @msg: notification type (see include/linux/clk.h) |
| 906 | * |
| 907 | * Walks the subtree of clks starting with clk and recalculates rates as it |
| 908 | * goes. Note that if a clk does not implement the .recalc_rate callback then |
| 909 | * it is assumed that the clock will take on the rate of it's parent. |
| 910 | * |
| 911 | * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, |
| 912 | * if necessary. |
| 913 | * |
| 914 | * Caller must hold prepare_lock. |
| 915 | */ |
| 916 | static void __clk_recalc_rates(struct clk *clk, unsigned long msg) |
| 917 | { |
| 918 | unsigned long old_rate; |
| 919 | unsigned long parent_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 920 | struct clk *child; |
| 921 | |
| 922 | old_rate = clk->rate; |
| 923 | |
| 924 | if (clk->parent) |
| 925 | parent_rate = clk->parent->rate; |
| 926 | |
| 927 | if (clk->ops->recalc_rate) |
| 928 | clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate); |
| 929 | else |
| 930 | clk->rate = parent_rate; |
| 931 | |
| 932 | /* |
| 933 | * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE |
| 934 | * & ABORT_RATE_CHANGE notifiers |
| 935 | */ |
| 936 | if (clk->notifier_count && msg) |
| 937 | __clk_notify(clk, msg, old_rate, clk->rate); |
| 938 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 939 | hlist_for_each_entry(child, &clk->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 940 | __clk_recalc_rates(child, msg); |
| 941 | } |
| 942 | |
| 943 | /** |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 944 | * clk_get_rate - return the rate of clk |
| 945 | * @clk: the clk whose rate is being returned |
| 946 | * |
| 947 | * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag |
| 948 | * is set, which means a recalc_rate will be issued. |
| 949 | * If clk is NULL then returns 0. |
| 950 | */ |
| 951 | unsigned long clk_get_rate(struct clk *clk) |
| 952 | { |
| 953 | unsigned long rate; |
| 954 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 955 | clk_prepare_lock(); |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 956 | |
| 957 | if (clk && (clk->flags & CLK_GET_RATE_NOCACHE)) |
| 958 | __clk_recalc_rates(clk, 0); |
| 959 | |
| 960 | rate = __clk_get_rate(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 961 | clk_prepare_unlock(); |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 962 | |
| 963 | return rate; |
| 964 | } |
| 965 | EXPORT_SYMBOL_GPL(clk_get_rate); |
| 966 | |
| 967 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 968 | * __clk_speculate_rates |
| 969 | * @clk: first clk in the subtree |
| 970 | * @parent_rate: the "future" rate of clk's parent |
| 971 | * |
| 972 | * Walks the subtree of clks starting with clk, speculating rates as it |
| 973 | * goes and firing off PRE_RATE_CHANGE notifications as necessary. |
| 974 | * |
| 975 | * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending |
| 976 | * pre-rate change notifications and returns early if no clks in the |
| 977 | * subtree have subscribed to the notifications. Note that if a clk does not |
| 978 | * implement the .recalc_rate callback then it is assumed that the clock will |
| 979 | * take on the rate of it's parent. |
| 980 | * |
| 981 | * Caller must hold prepare_lock. |
| 982 | */ |
| 983 | static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate) |
| 984 | { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 985 | struct clk *child; |
| 986 | unsigned long new_rate; |
| 987 | int ret = NOTIFY_DONE; |
| 988 | |
| 989 | if (clk->ops->recalc_rate) |
| 990 | new_rate = clk->ops->recalc_rate(clk->hw, parent_rate); |
| 991 | else |
| 992 | new_rate = parent_rate; |
| 993 | |
| 994 | /* abort the rate change if a driver returns NOTIFY_BAD */ |
| 995 | if (clk->notifier_count) |
| 996 | ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate); |
| 997 | |
| 998 | if (ret == NOTIFY_BAD) |
| 999 | goto out; |
| 1000 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1001 | hlist_for_each_entry(child, &clk->children, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1002 | ret = __clk_speculate_rates(child, new_rate); |
| 1003 | if (ret == NOTIFY_BAD) |
| 1004 | break; |
| 1005 | } |
| 1006 | |
| 1007 | out: |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | static void clk_calc_subtree(struct clk *clk, unsigned long new_rate) |
| 1012 | { |
| 1013 | struct clk *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1014 | |
| 1015 | clk->new_rate = new_rate; |
| 1016 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1017 | hlist_for_each_entry(child, &clk->children, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1018 | if (child->ops->recalc_rate) |
| 1019 | child->new_rate = child->ops->recalc_rate(child->hw, new_rate); |
| 1020 | else |
| 1021 | child->new_rate = new_rate; |
| 1022 | clk_calc_subtree(child, child->new_rate); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * calculate the new rates returning the topmost clock that has to be |
| 1028 | * changed. |
| 1029 | */ |
| 1030 | static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate) |
| 1031 | { |
| 1032 | struct clk *top = clk; |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 1033 | unsigned long best_parent_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1034 | unsigned long new_rate; |
| 1035 | |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1036 | /* sanity */ |
| 1037 | if (IS_ERR_OR_NULL(clk)) |
| 1038 | return NULL; |
| 1039 | |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1040 | /* save parent rate, if it exists */ |
| 1041 | if (clk->parent) |
| 1042 | best_parent_rate = clk->parent->rate; |
| 1043 | |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1044 | /* never propagate up to the parent */ |
| 1045 | if (!(clk->flags & CLK_SET_RATE_PARENT)) { |
| 1046 | if (!clk->ops->round_rate) { |
| 1047 | clk->new_rate = clk->rate; |
| 1048 | return NULL; |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1049 | } |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1050 | new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate); |
| 1051 | goto out; |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* need clk->parent from here on out */ |
| 1055 | if (!clk->parent) { |
| 1056 | pr_debug("%s: %s has NULL parent\n", __func__, clk->name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1057 | return NULL; |
| 1058 | } |
| 1059 | |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1060 | if (!clk->ops->round_rate) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1061 | top = clk_calc_new_rates(clk->parent, rate); |
Viresh Kumar | 1b2f990 | 2012-04-17 16:45:38 +0530 | [diff] [blame] | 1062 | new_rate = clk->parent->new_rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1063 | |
| 1064 | goto out; |
| 1065 | } |
| 1066 | |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1067 | new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1068 | |
| 1069 | if (best_parent_rate != clk->parent->rate) { |
| 1070 | top = clk_calc_new_rates(clk->parent, best_parent_rate); |
| 1071 | |
| 1072 | goto out; |
| 1073 | } |
| 1074 | |
| 1075 | out: |
| 1076 | clk_calc_subtree(clk, new_rate); |
| 1077 | |
| 1078 | return top; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Notify about rate changes in a subtree. Always walk down the whole tree |
| 1083 | * so that in case of an error we can walk down the whole tree again and |
| 1084 | * abort the change. |
| 1085 | */ |
| 1086 | static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event) |
| 1087 | { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1088 | struct clk *child, *fail_clk = NULL; |
| 1089 | int ret = NOTIFY_DONE; |
| 1090 | |
| 1091 | if (clk->rate == clk->new_rate) |
Sachin Kamat | 5fda685 | 2013-03-13 15:17:49 +0530 | [diff] [blame] | 1092 | return NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1093 | |
| 1094 | if (clk->notifier_count) { |
| 1095 | ret = __clk_notify(clk, event, clk->rate, clk->new_rate); |
| 1096 | if (ret == NOTIFY_BAD) |
| 1097 | fail_clk = clk; |
| 1098 | } |
| 1099 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1100 | hlist_for_each_entry(child, &clk->children, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1101 | clk = clk_propagate_rate_change(child, event); |
| 1102 | if (clk) |
| 1103 | fail_clk = clk; |
| 1104 | } |
| 1105 | |
| 1106 | return fail_clk; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * walk down a subtree and set the new rates notifying the rate |
| 1111 | * change on the way |
| 1112 | */ |
| 1113 | static void clk_change_rate(struct clk *clk) |
| 1114 | { |
| 1115 | struct clk *child; |
| 1116 | unsigned long old_rate; |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1117 | unsigned long best_parent_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1118 | |
| 1119 | old_rate = clk->rate; |
| 1120 | |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1121 | if (clk->parent) |
| 1122 | best_parent_rate = clk->parent->rate; |
| 1123 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1124 | if (clk->ops->set_rate) |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1125 | clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1126 | |
| 1127 | if (clk->ops->recalc_rate) |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1128 | clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1129 | else |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1130 | clk->rate = best_parent_rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1131 | |
| 1132 | if (clk->notifier_count && old_rate != clk->rate) |
| 1133 | __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate); |
| 1134 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1135 | hlist_for_each_entry(child, &clk->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1136 | clk_change_rate(child); |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * clk_set_rate - specify a new rate for clk |
| 1141 | * @clk: the clk whose rate is being changed |
| 1142 | * @rate: the new rate for clk |
| 1143 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1144 | * In the simplest case clk_set_rate will only adjust the rate of clk. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1145 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1146 | * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to |
| 1147 | * propagate up to clk's parent; whether or not this happens depends on the |
| 1148 | * outcome of clk's .round_rate implementation. If *parent_rate is unchanged |
| 1149 | * after calling .round_rate then upstream parent propagation is ignored. If |
| 1150 | * *parent_rate comes back with a new rate for clk's parent then we propagate |
| 1151 | * up to clk's parent and set it's rate. Upward propagation will continue |
| 1152 | * until either a clk does not support the CLK_SET_RATE_PARENT flag or |
| 1153 | * .round_rate stops requesting changes to clk's parent_rate. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1154 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1155 | * Rate changes are accomplished via tree traversal that also recalculates the |
| 1156 | * rates for the clocks and fires off POST_RATE_CHANGE notifiers. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1157 | * |
| 1158 | * Returns 0 on success, -EERROR otherwise. |
| 1159 | */ |
| 1160 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 1161 | { |
| 1162 | struct clk *top, *fail_clk; |
| 1163 | int ret = 0; |
| 1164 | |
| 1165 | /* prevent racing with updates to the clock topology */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1166 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1167 | |
| 1168 | /* bail early if nothing to do */ |
| 1169 | if (rate == clk->rate) |
| 1170 | goto out; |
| 1171 | |
Saravana Kannan | 7e0fa1b | 2012-05-15 13:43:42 -0700 | [diff] [blame] | 1172 | if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) { |
Viresh Kumar | 0e1c030 | 2012-04-11 16:03:42 +0530 | [diff] [blame] | 1173 | ret = -EBUSY; |
| 1174 | goto out; |
| 1175 | } |
| 1176 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1177 | /* calculate new rates and get the topmost changed clock */ |
| 1178 | top = clk_calc_new_rates(clk, rate); |
| 1179 | if (!top) { |
| 1180 | ret = -EINVAL; |
| 1181 | goto out; |
| 1182 | } |
| 1183 | |
| 1184 | /* notify that we are about to change rates */ |
| 1185 | fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); |
| 1186 | if (fail_clk) { |
| 1187 | pr_warn("%s: failed to set %s rate\n", __func__, |
| 1188 | fail_clk->name); |
| 1189 | clk_propagate_rate_change(top, ABORT_RATE_CHANGE); |
| 1190 | ret = -EBUSY; |
| 1191 | goto out; |
| 1192 | } |
| 1193 | |
| 1194 | /* change the rates */ |
| 1195 | clk_change_rate(top); |
| 1196 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1197 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1198 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1199 | |
| 1200 | return ret; |
| 1201 | } |
| 1202 | EXPORT_SYMBOL_GPL(clk_set_rate); |
| 1203 | |
| 1204 | /** |
| 1205 | * clk_get_parent - return the parent of a clk |
| 1206 | * @clk: the clk whose parent gets returned |
| 1207 | * |
| 1208 | * Simply returns clk->parent. Returns NULL if clk is NULL. |
| 1209 | */ |
| 1210 | struct clk *clk_get_parent(struct clk *clk) |
| 1211 | { |
| 1212 | struct clk *parent; |
| 1213 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1214 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1215 | parent = __clk_get_parent(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1216 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1217 | |
| 1218 | return parent; |
| 1219 | } |
| 1220 | EXPORT_SYMBOL_GPL(clk_get_parent); |
| 1221 | |
| 1222 | /* |
| 1223 | * .get_parent is mandatory for clocks with multiple possible parents. It is |
| 1224 | * optional for single-parent clocks. Always call .get_parent if it is |
| 1225 | * available and WARN if it is missing for multi-parent clocks. |
| 1226 | * |
| 1227 | * For single-parent clocks without .get_parent, first check to see if the |
| 1228 | * .parents array exists, and if so use it to avoid an expensive tree |
| 1229 | * traversal. If .parents does not exist then walk the tree with __clk_lookup. |
| 1230 | */ |
| 1231 | static struct clk *__clk_init_parent(struct clk *clk) |
| 1232 | { |
| 1233 | struct clk *ret = NULL; |
| 1234 | u8 index; |
| 1235 | |
| 1236 | /* handle the trivial cases */ |
| 1237 | |
| 1238 | if (!clk->num_parents) |
| 1239 | goto out; |
| 1240 | |
| 1241 | if (clk->num_parents == 1) { |
| 1242 | if (IS_ERR_OR_NULL(clk->parent)) |
| 1243 | ret = clk->parent = __clk_lookup(clk->parent_names[0]); |
| 1244 | ret = clk->parent; |
| 1245 | goto out; |
| 1246 | } |
| 1247 | |
| 1248 | if (!clk->ops->get_parent) { |
| 1249 | WARN(!clk->ops->get_parent, |
| 1250 | "%s: multi-parent clocks must implement .get_parent\n", |
| 1251 | __func__); |
| 1252 | goto out; |
| 1253 | }; |
| 1254 | |
| 1255 | /* |
| 1256 | * Do our best to cache parent clocks in clk->parents. This prevents |
| 1257 | * unnecessary and expensive calls to __clk_lookup. We don't set |
| 1258 | * clk->parent here; that is done by the calling function |
| 1259 | */ |
| 1260 | |
| 1261 | index = clk->ops->get_parent(clk->hw); |
| 1262 | |
| 1263 | if (!clk->parents) |
| 1264 | clk->parents = |
Rajendra Nayak | 7975059 | 2012-06-06 14:41:31 +0530 | [diff] [blame] | 1265 | kzalloc((sizeof(struct clk*) * clk->num_parents), |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1266 | GFP_KERNEL); |
| 1267 | |
| 1268 | if (!clk->parents) |
| 1269 | ret = __clk_lookup(clk->parent_names[index]); |
| 1270 | else if (!clk->parents[index]) |
| 1271 | ret = clk->parents[index] = |
| 1272 | __clk_lookup(clk->parent_names[index]); |
| 1273 | else |
| 1274 | ret = clk->parents[index]; |
| 1275 | |
| 1276 | out: |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
| 1280 | void __clk_reparent(struct clk *clk, struct clk *new_parent) |
| 1281 | { |
| 1282 | #ifdef CONFIG_COMMON_CLK_DEBUG |
| 1283 | struct dentry *d; |
| 1284 | struct dentry *new_parent_d; |
| 1285 | #endif |
| 1286 | |
| 1287 | if (!clk || !new_parent) |
| 1288 | return; |
| 1289 | |
| 1290 | hlist_del(&clk->child_node); |
| 1291 | |
| 1292 | if (new_parent) |
| 1293 | hlist_add_head(&clk->child_node, &new_parent->children); |
| 1294 | else |
| 1295 | hlist_add_head(&clk->child_node, &clk_orphan_list); |
| 1296 | |
| 1297 | #ifdef CONFIG_COMMON_CLK_DEBUG |
| 1298 | if (!inited) |
| 1299 | goto out; |
| 1300 | |
| 1301 | if (new_parent) |
| 1302 | new_parent_d = new_parent->dentry; |
| 1303 | else |
| 1304 | new_parent_d = orphandir; |
| 1305 | |
| 1306 | d = debugfs_rename(clk->dentry->d_parent, clk->dentry, |
| 1307 | new_parent_d, clk->name); |
| 1308 | if (d) |
| 1309 | clk->dentry = d; |
| 1310 | else |
| 1311 | pr_debug("%s: failed to rename debugfs entry for %s\n", |
| 1312 | __func__, clk->name); |
| 1313 | out: |
| 1314 | #endif |
| 1315 | |
| 1316 | clk->parent = new_parent; |
| 1317 | |
| 1318 | __clk_recalc_rates(clk, POST_RATE_CHANGE); |
| 1319 | } |
| 1320 | |
| 1321 | static int __clk_set_parent(struct clk *clk, struct clk *parent) |
| 1322 | { |
| 1323 | struct clk *old_parent; |
| 1324 | unsigned long flags; |
| 1325 | int ret = -EINVAL; |
| 1326 | u8 i; |
| 1327 | |
| 1328 | old_parent = clk->parent; |
| 1329 | |
Rajendra Nayak | 863b132 | 2012-07-03 12:11:41 +0530 | [diff] [blame] | 1330 | if (!clk->parents) |
Rajendra Nayak | 7975059 | 2012-06-06 14:41:31 +0530 | [diff] [blame] | 1331 | clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents), |
| 1332 | GFP_KERNEL); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1333 | |
| 1334 | /* |
Rajendra Nayak | 863b132 | 2012-07-03 12:11:41 +0530 | [diff] [blame] | 1335 | * find index of new parent clock using cached parent ptrs, |
| 1336 | * or if not yet cached, use string name comparison and cache |
| 1337 | * them now to avoid future calls to __clk_lookup. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1338 | */ |
Rajendra Nayak | 863b132 | 2012-07-03 12:11:41 +0530 | [diff] [blame] | 1339 | for (i = 0; i < clk->num_parents; i++) { |
| 1340 | if (clk->parents && clk->parents[i] == parent) |
| 1341 | break; |
| 1342 | else if (!strcmp(clk->parent_names[i], parent->name)) { |
| 1343 | if (clk->parents) |
| 1344 | clk->parents[i] = __clk_lookup(parent->name); |
| 1345 | break; |
| 1346 | } |
| 1347 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1348 | |
| 1349 | if (i == clk->num_parents) { |
| 1350 | pr_debug("%s: clock %s is not a possible parent of clock %s\n", |
| 1351 | __func__, parent->name, clk->name); |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
| 1355 | /* migrate prepare and enable */ |
| 1356 | if (clk->prepare_count) |
| 1357 | __clk_prepare(parent); |
| 1358 | |
| 1359 | /* FIXME replace with clk_is_enabled(clk) someday */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1360 | flags = clk_enable_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1361 | if (clk->enable_count) |
| 1362 | __clk_enable(parent); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1363 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1364 | |
| 1365 | /* change clock input source */ |
| 1366 | ret = clk->ops->set_parent(clk->hw, i); |
| 1367 | |
| 1368 | /* clean up old prepare and enable */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1369 | flags = clk_enable_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1370 | if (clk->enable_count) |
| 1371 | __clk_disable(old_parent); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1372 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1373 | |
| 1374 | if (clk->prepare_count) |
| 1375 | __clk_unprepare(old_parent); |
| 1376 | |
| 1377 | out: |
| 1378 | return ret; |
| 1379 | } |
| 1380 | |
| 1381 | /** |
| 1382 | * clk_set_parent - switch the parent of a mux clk |
| 1383 | * @clk: the mux clk whose input we are switching |
| 1384 | * @parent: the new input to clk |
| 1385 | * |
| 1386 | * Re-parent clk to use parent as it's new input source. If clk has the |
| 1387 | * CLK_SET_PARENT_GATE flag set then clk must be gated for this |
| 1388 | * operation to succeed. After successfully changing clk's parent |
| 1389 | * clk_set_parent will update the clk topology, sysfs topology and |
| 1390 | * propagate rate recalculation via __clk_recalc_rates. Returns 0 on |
| 1391 | * success, -EERROR otherwise. |
| 1392 | */ |
| 1393 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 1394 | { |
| 1395 | int ret = 0; |
| 1396 | |
| 1397 | if (!clk || !clk->ops) |
| 1398 | return -EINVAL; |
| 1399 | |
| 1400 | if (!clk->ops->set_parent) |
| 1401 | return -ENOSYS; |
| 1402 | |
| 1403 | /* prevent racing with updates to the clock topology */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1404 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1405 | |
| 1406 | if (clk->parent == parent) |
| 1407 | goto out; |
| 1408 | |
| 1409 | /* propagate PRE_RATE_CHANGE notifications */ |
| 1410 | if (clk->notifier_count) |
| 1411 | ret = __clk_speculate_rates(clk, parent->rate); |
| 1412 | |
| 1413 | /* abort if a driver objects */ |
| 1414 | if (ret == NOTIFY_STOP) |
| 1415 | goto out; |
| 1416 | |
| 1417 | /* only re-parent if the clock is not in use */ |
| 1418 | if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) |
| 1419 | ret = -EBUSY; |
| 1420 | else |
| 1421 | ret = __clk_set_parent(clk, parent); |
| 1422 | |
| 1423 | /* propagate ABORT_RATE_CHANGE if .set_parent failed */ |
| 1424 | if (ret) { |
| 1425 | __clk_recalc_rates(clk, ABORT_RATE_CHANGE); |
| 1426 | goto out; |
| 1427 | } |
| 1428 | |
| 1429 | /* propagate rate recalculation downstream */ |
| 1430 | __clk_reparent(clk, parent); |
| 1431 | |
| 1432 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1433 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1434 | |
| 1435 | return ret; |
| 1436 | } |
| 1437 | EXPORT_SYMBOL_GPL(clk_set_parent); |
| 1438 | |
| 1439 | /** |
| 1440 | * __clk_init - initialize the data structures in a struct clk |
| 1441 | * @dev: device initializing this clk, placeholder for now |
| 1442 | * @clk: clk being initialized |
| 1443 | * |
| 1444 | * Initializes the lists in struct clk, queries the hardware for the |
| 1445 | * parent and rate and sets them both. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1446 | */ |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1447 | int __clk_init(struct device *dev, struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1448 | { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1449 | int i, ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1450 | struct clk *orphan; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1451 | struct hlist_node *tmp2; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1452 | |
| 1453 | if (!clk) |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1454 | return -EINVAL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1455 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1456 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1457 | |
| 1458 | /* check to see if a clock with this name is already registered */ |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1459 | if (__clk_lookup(clk->name)) { |
| 1460 | pr_debug("%s: clk %s already initialized\n", |
| 1461 | __func__, clk->name); |
| 1462 | ret = -EEXIST; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1463 | goto out; |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1464 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1465 | |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 1466 | /* check that clk_ops are sane. See Documentation/clk.txt */ |
| 1467 | if (clk->ops->set_rate && |
| 1468 | !(clk->ops->round_rate && clk->ops->recalc_rate)) { |
| 1469 | pr_warning("%s: %s must implement .round_rate & .recalc_rate\n", |
| 1470 | __func__, clk->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1471 | ret = -EINVAL; |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 1472 | goto out; |
| 1473 | } |
| 1474 | |
| 1475 | if (clk->ops->set_parent && !clk->ops->get_parent) { |
| 1476 | pr_warning("%s: %s must implement .get_parent & .set_parent\n", |
| 1477 | __func__, clk->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1478 | ret = -EINVAL; |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 1479 | goto out; |
| 1480 | } |
| 1481 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1482 | /* throw a WARN if any entries in parent_names are NULL */ |
| 1483 | for (i = 0; i < clk->num_parents; i++) |
| 1484 | WARN(!clk->parent_names[i], |
| 1485 | "%s: invalid NULL in %s's .parent_names\n", |
| 1486 | __func__, clk->name); |
| 1487 | |
| 1488 | /* |
| 1489 | * Allocate an array of struct clk *'s to avoid unnecessary string |
| 1490 | * look-ups of clk's possible parents. This can fail for clocks passed |
| 1491 | * in to clk_init during early boot; thus any access to clk->parents[] |
| 1492 | * must always check for a NULL pointer and try to populate it if |
| 1493 | * necessary. |
| 1494 | * |
| 1495 | * If clk->parents is not NULL we skip this entire block. This allows |
| 1496 | * for clock drivers to statically initialize clk->parents. |
| 1497 | */ |
Rajendra Nayak | 9ca1c5a | 2012-06-06 14:41:30 +0530 | [diff] [blame] | 1498 | if (clk->num_parents > 1 && !clk->parents) { |
| 1499 | clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents), |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1500 | GFP_KERNEL); |
| 1501 | /* |
| 1502 | * __clk_lookup returns NULL for parents that have not been |
| 1503 | * clk_init'd; thus any access to clk->parents[] must check |
| 1504 | * for a NULL pointer. We can always perform lazy lookups for |
| 1505 | * missing parents later on. |
| 1506 | */ |
| 1507 | if (clk->parents) |
| 1508 | for (i = 0; i < clk->num_parents; i++) |
| 1509 | clk->parents[i] = |
| 1510 | __clk_lookup(clk->parent_names[i]); |
| 1511 | } |
| 1512 | |
| 1513 | clk->parent = __clk_init_parent(clk); |
| 1514 | |
| 1515 | /* |
| 1516 | * Populate clk->parent if parent has already been __clk_init'd. If |
| 1517 | * parent has not yet been __clk_init'd then place clk in the orphan |
| 1518 | * list. If clk has set the CLK_IS_ROOT flag then place it in the root |
| 1519 | * clk list. |
| 1520 | * |
| 1521 | * Every time a new clk is clk_init'd then we walk the list of orphan |
| 1522 | * clocks and re-parent any that are children of the clock currently |
| 1523 | * being clk_init'd. |
| 1524 | */ |
| 1525 | if (clk->parent) |
| 1526 | hlist_add_head(&clk->child_node, |
| 1527 | &clk->parent->children); |
| 1528 | else if (clk->flags & CLK_IS_ROOT) |
| 1529 | hlist_add_head(&clk->child_node, &clk_root_list); |
| 1530 | else |
| 1531 | hlist_add_head(&clk->child_node, &clk_orphan_list); |
| 1532 | |
| 1533 | /* |
| 1534 | * Set clk's rate. The preferred method is to use .recalc_rate. For |
| 1535 | * simple clocks and lazy developers the default fallback is to use the |
| 1536 | * parent's rate. If a clock doesn't have a parent (or is orphaned) |
| 1537 | * then rate is set to zero. |
| 1538 | */ |
| 1539 | if (clk->ops->recalc_rate) |
| 1540 | clk->rate = clk->ops->recalc_rate(clk->hw, |
| 1541 | __clk_get_rate(clk->parent)); |
| 1542 | else if (clk->parent) |
| 1543 | clk->rate = clk->parent->rate; |
| 1544 | else |
| 1545 | clk->rate = 0; |
| 1546 | |
| 1547 | /* |
| 1548 | * walk the list of orphan clocks and reparent any that are children of |
| 1549 | * this clock |
| 1550 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1551 | hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { |
Martin Fuzzey | 1f61e5f | 2012-11-22 20:15:05 +0100 | [diff] [blame] | 1552 | if (orphan->ops->get_parent) { |
| 1553 | i = orphan->ops->get_parent(orphan->hw); |
| 1554 | if (!strcmp(clk->name, orphan->parent_names[i])) |
| 1555 | __clk_reparent(orphan, clk); |
| 1556 | continue; |
| 1557 | } |
| 1558 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1559 | for (i = 0; i < orphan->num_parents; i++) |
| 1560 | if (!strcmp(clk->name, orphan->parent_names[i])) { |
| 1561 | __clk_reparent(orphan, clk); |
| 1562 | break; |
| 1563 | } |
Martin Fuzzey | 1f61e5f | 2012-11-22 20:15:05 +0100 | [diff] [blame] | 1564 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * optional platform-specific magic |
| 1568 | * |
| 1569 | * The .init callback is not used by any of the basic clock types, but |
| 1570 | * exists for weird hardware that must perform initialization magic. |
| 1571 | * Please consider other ways of solving initialization problems before |
| 1572 | * using this callback, as it's use is discouraged. |
| 1573 | */ |
| 1574 | if (clk->ops->init) |
| 1575 | clk->ops->init(clk->hw); |
| 1576 | |
| 1577 | clk_debug_register(clk); |
| 1578 | |
| 1579 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1580 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1581 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1582 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | /** |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 1586 | * __clk_register - register a clock and return a cookie. |
| 1587 | * |
| 1588 | * Same as clk_register, except that the .clk field inside hw shall point to a |
| 1589 | * preallocated (generally statically allocated) struct clk. None of the fields |
| 1590 | * of the struct clk need to be initialized. |
| 1591 | * |
| 1592 | * The data pointed to by .init and .clk field shall NOT be marked as init |
| 1593 | * data. |
| 1594 | * |
| 1595 | * __clk_register is only exposed via clk-private.h and is intended for use with |
| 1596 | * very large numbers of clocks that need to be statically initialized. It is |
| 1597 | * a layering violation to include clk-private.h from any code which implements |
| 1598 | * a clock's .ops; as such any statically initialized clock data MUST be in a |
| 1599 | * separate C file from the logic that implements it's operations. Returns 0 |
| 1600 | * on success, otherwise an error code. |
| 1601 | */ |
| 1602 | struct clk *__clk_register(struct device *dev, struct clk_hw *hw) |
| 1603 | { |
| 1604 | int ret; |
| 1605 | struct clk *clk; |
| 1606 | |
| 1607 | clk = hw->clk; |
| 1608 | clk->name = hw->init->name; |
| 1609 | clk->ops = hw->init->ops; |
| 1610 | clk->hw = hw; |
| 1611 | clk->flags = hw->init->flags; |
| 1612 | clk->parent_names = hw->init->parent_names; |
| 1613 | clk->num_parents = hw->init->num_parents; |
| 1614 | |
| 1615 | ret = __clk_init(dev, clk); |
| 1616 | if (ret) |
| 1617 | return ERR_PTR(ret); |
| 1618 | |
| 1619 | return clk; |
| 1620 | } |
| 1621 | EXPORT_SYMBOL_GPL(__clk_register); |
| 1622 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 1623 | static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1624 | { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1625 | int i, ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1626 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 1627 | clk->name = kstrdup(hw->init->name, GFP_KERNEL); |
| 1628 | if (!clk->name) { |
| 1629 | pr_err("%s: could not allocate clk->name\n", __func__); |
| 1630 | ret = -ENOMEM; |
| 1631 | goto fail_name; |
| 1632 | } |
| 1633 | clk->ops = hw->init->ops; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1634 | clk->hw = hw; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 1635 | clk->flags = hw->init->flags; |
| 1636 | clk->num_parents = hw->init->num_parents; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1637 | hw->clk = clk; |
| 1638 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1639 | /* allocate local copy in case parent_names is __initdata */ |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 1640 | clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents), |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1641 | GFP_KERNEL); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1642 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1643 | if (!clk->parent_names) { |
| 1644 | pr_err("%s: could not allocate clk->parent_names\n", __func__); |
| 1645 | ret = -ENOMEM; |
| 1646 | goto fail_parent_names; |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | /* copy each string name in case parent_names is __initdata */ |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 1651 | for (i = 0; i < clk->num_parents; i++) { |
| 1652 | clk->parent_names[i] = kstrdup(hw->init->parent_names[i], |
| 1653 | GFP_KERNEL); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1654 | if (!clk->parent_names[i]) { |
| 1655 | pr_err("%s: could not copy parent_names\n", __func__); |
| 1656 | ret = -ENOMEM; |
| 1657 | goto fail_parent_names_copy; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | ret = __clk_init(dev, clk); |
| 1662 | if (!ret) |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 1663 | return 0; |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1664 | |
| 1665 | fail_parent_names_copy: |
| 1666 | while (--i >= 0) |
| 1667 | kfree(clk->parent_names[i]); |
| 1668 | kfree(clk->parent_names); |
| 1669 | fail_parent_names: |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 1670 | kfree(clk->name); |
| 1671 | fail_name: |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * clk_register - allocate a new clock, register it and return an opaque cookie |
| 1677 | * @dev: device that is registering this clock |
| 1678 | * @hw: link to hardware-specific clock data |
| 1679 | * |
| 1680 | * clk_register is the primary interface for populating the clock tree with new |
| 1681 | * clock nodes. It returns a pointer to the newly allocated struct clk which |
| 1682 | * cannot be dereferenced by driver code but may be used in conjuction with the |
| 1683 | * rest of the clock API. In the event of an error clk_register will return an |
| 1684 | * error code; drivers must test for an error code after calling clk_register. |
| 1685 | */ |
| 1686 | struct clk *clk_register(struct device *dev, struct clk_hw *hw) |
| 1687 | { |
| 1688 | int ret; |
| 1689 | struct clk *clk; |
| 1690 | |
| 1691 | clk = kzalloc(sizeof(*clk), GFP_KERNEL); |
| 1692 | if (!clk) { |
| 1693 | pr_err("%s: could not allocate clk\n", __func__); |
| 1694 | ret = -ENOMEM; |
| 1695 | goto fail_out; |
| 1696 | } |
| 1697 | |
| 1698 | ret = _clk_register(dev, hw, clk); |
| 1699 | if (!ret) |
| 1700 | return clk; |
| 1701 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 1702 | kfree(clk); |
| 1703 | fail_out: |
| 1704 | return ERR_PTR(ret); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1705 | } |
| 1706 | EXPORT_SYMBOL_GPL(clk_register); |
| 1707 | |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 1708 | /** |
| 1709 | * clk_unregister - unregister a currently registered clock |
| 1710 | * @clk: clock to unregister |
| 1711 | * |
| 1712 | * Currently unimplemented. |
| 1713 | */ |
| 1714 | void clk_unregister(struct clk *clk) {} |
| 1715 | EXPORT_SYMBOL_GPL(clk_unregister); |
| 1716 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 1717 | static void devm_clk_release(struct device *dev, void *res) |
| 1718 | { |
| 1719 | clk_unregister(res); |
| 1720 | } |
| 1721 | |
| 1722 | /** |
| 1723 | * devm_clk_register - resource managed clk_register() |
| 1724 | * @dev: device that is registering this clock |
| 1725 | * @hw: link to hardware-specific clock data |
| 1726 | * |
| 1727 | * Managed clk_register(). Clocks returned from this function are |
| 1728 | * automatically clk_unregister()ed on driver detach. See clk_register() for |
| 1729 | * more information. |
| 1730 | */ |
| 1731 | struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) |
| 1732 | { |
| 1733 | struct clk *clk; |
| 1734 | int ret; |
| 1735 | |
| 1736 | clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL); |
| 1737 | if (!clk) |
| 1738 | return ERR_PTR(-ENOMEM); |
| 1739 | |
| 1740 | ret = _clk_register(dev, hw, clk); |
| 1741 | if (!ret) { |
| 1742 | devres_add(dev, clk); |
| 1743 | } else { |
| 1744 | devres_free(clk); |
| 1745 | clk = ERR_PTR(ret); |
| 1746 | } |
| 1747 | |
| 1748 | return clk; |
| 1749 | } |
| 1750 | EXPORT_SYMBOL_GPL(devm_clk_register); |
| 1751 | |
| 1752 | static int devm_clk_match(struct device *dev, void *res, void *data) |
| 1753 | { |
| 1754 | struct clk *c = res; |
| 1755 | if (WARN_ON(!c)) |
| 1756 | return 0; |
| 1757 | return c == data; |
| 1758 | } |
| 1759 | |
| 1760 | /** |
| 1761 | * devm_clk_unregister - resource managed clk_unregister() |
| 1762 | * @clk: clock to unregister |
| 1763 | * |
| 1764 | * Deallocate a clock allocated with devm_clk_register(). Normally |
| 1765 | * this function will not need to be called and the resource management |
| 1766 | * code will ensure that the resource is freed. |
| 1767 | */ |
| 1768 | void devm_clk_unregister(struct device *dev, struct clk *clk) |
| 1769 | { |
| 1770 | WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); |
| 1771 | } |
| 1772 | EXPORT_SYMBOL_GPL(devm_clk_unregister); |
| 1773 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1774 | /*** clk rate change notifiers ***/ |
| 1775 | |
| 1776 | /** |
| 1777 | * clk_notifier_register - add a clk rate change notifier |
| 1778 | * @clk: struct clk * to watch |
| 1779 | * @nb: struct notifier_block * with callback info |
| 1780 | * |
| 1781 | * Request notification when clk's rate changes. This uses an SRCU |
| 1782 | * notifier because we want it to block and notifier unregistrations are |
| 1783 | * uncommon. The callbacks associated with the notifier must not |
| 1784 | * re-enter into the clk framework by calling any top-level clk APIs; |
| 1785 | * this will cause a nested prepare_lock mutex. |
| 1786 | * |
| 1787 | * Pre-change notifier callbacks will be passed the current, pre-change |
| 1788 | * rate of the clk via struct clk_notifier_data.old_rate. The new, |
| 1789 | * post-change rate of the clk is passed via struct |
| 1790 | * clk_notifier_data.new_rate. |
| 1791 | * |
| 1792 | * Post-change notifiers will pass the now-current, post-change rate of |
| 1793 | * the clk in both struct clk_notifier_data.old_rate and struct |
| 1794 | * clk_notifier_data.new_rate. |
| 1795 | * |
| 1796 | * Abort-change notifiers are effectively the opposite of pre-change |
| 1797 | * notifiers: the original pre-change clk rate is passed in via struct |
| 1798 | * clk_notifier_data.new_rate and the failed post-change rate is passed |
| 1799 | * in via struct clk_notifier_data.old_rate. |
| 1800 | * |
| 1801 | * clk_notifier_register() must be called from non-atomic context. |
| 1802 | * Returns -EINVAL if called with null arguments, -ENOMEM upon |
| 1803 | * allocation failure; otherwise, passes along the return value of |
| 1804 | * srcu_notifier_chain_register(). |
| 1805 | */ |
| 1806 | int clk_notifier_register(struct clk *clk, struct notifier_block *nb) |
| 1807 | { |
| 1808 | struct clk_notifier *cn; |
| 1809 | int ret = -ENOMEM; |
| 1810 | |
| 1811 | if (!clk || !nb) |
| 1812 | return -EINVAL; |
| 1813 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1814 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1815 | |
| 1816 | /* search the list of notifiers for this clk */ |
| 1817 | list_for_each_entry(cn, &clk_notifier_list, node) |
| 1818 | if (cn->clk == clk) |
| 1819 | break; |
| 1820 | |
| 1821 | /* if clk wasn't in the notifier list, allocate new clk_notifier */ |
| 1822 | if (cn->clk != clk) { |
| 1823 | cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL); |
| 1824 | if (!cn) |
| 1825 | goto out; |
| 1826 | |
| 1827 | cn->clk = clk; |
| 1828 | srcu_init_notifier_head(&cn->notifier_head); |
| 1829 | |
| 1830 | list_add(&cn->node, &clk_notifier_list); |
| 1831 | } |
| 1832 | |
| 1833 | ret = srcu_notifier_chain_register(&cn->notifier_head, nb); |
| 1834 | |
| 1835 | clk->notifier_count++; |
| 1836 | |
| 1837 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1838 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1839 | |
| 1840 | return ret; |
| 1841 | } |
| 1842 | EXPORT_SYMBOL_GPL(clk_notifier_register); |
| 1843 | |
| 1844 | /** |
| 1845 | * clk_notifier_unregister - remove a clk rate change notifier |
| 1846 | * @clk: struct clk * |
| 1847 | * @nb: struct notifier_block * with callback info |
| 1848 | * |
| 1849 | * Request no further notification for changes to 'clk' and frees memory |
| 1850 | * allocated in clk_notifier_register. |
| 1851 | * |
| 1852 | * Returns -EINVAL if called with null arguments; otherwise, passes |
| 1853 | * along the return value of srcu_notifier_chain_unregister(). |
| 1854 | */ |
| 1855 | int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) |
| 1856 | { |
| 1857 | struct clk_notifier *cn = NULL; |
| 1858 | int ret = -EINVAL; |
| 1859 | |
| 1860 | if (!clk || !nb) |
| 1861 | return -EINVAL; |
| 1862 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1863 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1864 | |
| 1865 | list_for_each_entry(cn, &clk_notifier_list, node) |
| 1866 | if (cn->clk == clk) |
| 1867 | break; |
| 1868 | |
| 1869 | if (cn->clk == clk) { |
| 1870 | ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); |
| 1871 | |
| 1872 | clk->notifier_count--; |
| 1873 | |
| 1874 | /* XXX the notifier code should handle this better */ |
| 1875 | if (!cn->notifier_head.head) { |
| 1876 | srcu_cleanup_notifier_head(&cn->notifier_head); |
| 1877 | kfree(cn); |
| 1878 | } |
| 1879 | |
| 1880 | } else { |
| 1881 | ret = -ENOENT; |
| 1882 | } |
| 1883 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1884 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1885 | |
| 1886 | return ret; |
| 1887 | } |
| 1888 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 1889 | |
| 1890 | #ifdef CONFIG_OF |
| 1891 | /** |
| 1892 | * struct of_clk_provider - Clock provider registration structure |
| 1893 | * @link: Entry in global list of clock providers |
| 1894 | * @node: Pointer to device tree node of clock provider |
| 1895 | * @get: Get clock callback. Returns NULL or a struct clk for the |
| 1896 | * given clock specifier |
| 1897 | * @data: context pointer to be passed into @get callback |
| 1898 | */ |
| 1899 | struct of_clk_provider { |
| 1900 | struct list_head link; |
| 1901 | |
| 1902 | struct device_node *node; |
| 1903 | struct clk *(*get)(struct of_phandle_args *clkspec, void *data); |
| 1904 | void *data; |
| 1905 | }; |
| 1906 | |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 1907 | extern struct of_device_id __clk_of_table[]; |
| 1908 | |
| 1909 | static const struct of_device_id __clk_of_table_sentinel |
| 1910 | __used __section(__clk_of_table_end); |
| 1911 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 1912 | static LIST_HEAD(of_clk_providers); |
| 1913 | static DEFINE_MUTEX(of_clk_lock); |
| 1914 | |
| 1915 | struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, |
| 1916 | void *data) |
| 1917 | { |
| 1918 | return data; |
| 1919 | } |
| 1920 | EXPORT_SYMBOL_GPL(of_clk_src_simple_get); |
| 1921 | |
Shawn Guo | 494bfec | 2012-08-22 21:36:27 +0800 | [diff] [blame] | 1922 | struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) |
| 1923 | { |
| 1924 | struct clk_onecell_data *clk_data = data; |
| 1925 | unsigned int idx = clkspec->args[0]; |
| 1926 | |
| 1927 | if (idx >= clk_data->clk_num) { |
| 1928 | pr_err("%s: invalid clock index %d\n", __func__, idx); |
| 1929 | return ERR_PTR(-EINVAL); |
| 1930 | } |
| 1931 | |
| 1932 | return clk_data->clks[idx]; |
| 1933 | } |
| 1934 | EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); |
| 1935 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 1936 | /** |
| 1937 | * of_clk_add_provider() - Register a clock provider for a node |
| 1938 | * @np: Device node pointer associated with clock provider |
| 1939 | * @clk_src_get: callback for decoding clock |
| 1940 | * @data: context pointer for @clk_src_get callback. |
| 1941 | */ |
| 1942 | int of_clk_add_provider(struct device_node *np, |
| 1943 | struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, |
| 1944 | void *data), |
| 1945 | void *data) |
| 1946 | { |
| 1947 | struct of_clk_provider *cp; |
| 1948 | |
| 1949 | cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); |
| 1950 | if (!cp) |
| 1951 | return -ENOMEM; |
| 1952 | |
| 1953 | cp->node = of_node_get(np); |
| 1954 | cp->data = data; |
| 1955 | cp->get = clk_src_get; |
| 1956 | |
| 1957 | mutex_lock(&of_clk_lock); |
| 1958 | list_add(&cp->link, &of_clk_providers); |
| 1959 | mutex_unlock(&of_clk_lock); |
| 1960 | pr_debug("Added clock from %s\n", np->full_name); |
| 1961 | |
| 1962 | return 0; |
| 1963 | } |
| 1964 | EXPORT_SYMBOL_GPL(of_clk_add_provider); |
| 1965 | |
| 1966 | /** |
| 1967 | * of_clk_del_provider() - Remove a previously registered clock provider |
| 1968 | * @np: Device node pointer associated with clock provider |
| 1969 | */ |
| 1970 | void of_clk_del_provider(struct device_node *np) |
| 1971 | { |
| 1972 | struct of_clk_provider *cp; |
| 1973 | |
| 1974 | mutex_lock(&of_clk_lock); |
| 1975 | list_for_each_entry(cp, &of_clk_providers, link) { |
| 1976 | if (cp->node == np) { |
| 1977 | list_del(&cp->link); |
| 1978 | of_node_put(cp->node); |
| 1979 | kfree(cp); |
| 1980 | break; |
| 1981 | } |
| 1982 | } |
| 1983 | mutex_unlock(&of_clk_lock); |
| 1984 | } |
| 1985 | EXPORT_SYMBOL_GPL(of_clk_del_provider); |
| 1986 | |
| 1987 | struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) |
| 1988 | { |
| 1989 | struct of_clk_provider *provider; |
| 1990 | struct clk *clk = ERR_PTR(-ENOENT); |
| 1991 | |
| 1992 | /* Check if we have such a provider in our array */ |
| 1993 | mutex_lock(&of_clk_lock); |
| 1994 | list_for_each_entry(provider, &of_clk_providers, link) { |
| 1995 | if (provider->node == clkspec->np) |
| 1996 | clk = provider->get(clkspec, provider->data); |
| 1997 | if (!IS_ERR(clk)) |
| 1998 | break; |
| 1999 | } |
| 2000 | mutex_unlock(&of_clk_lock); |
| 2001 | |
| 2002 | return clk; |
| 2003 | } |
| 2004 | |
| 2005 | const char *of_clk_get_parent_name(struct device_node *np, int index) |
| 2006 | { |
| 2007 | struct of_phandle_args clkspec; |
| 2008 | const char *clk_name; |
| 2009 | int rc; |
| 2010 | |
| 2011 | if (index < 0) |
| 2012 | return NULL; |
| 2013 | |
| 2014 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, |
| 2015 | &clkspec); |
| 2016 | if (rc) |
| 2017 | return NULL; |
| 2018 | |
| 2019 | if (of_property_read_string_index(clkspec.np, "clock-output-names", |
| 2020 | clkspec.args_count ? clkspec.args[0] : 0, |
| 2021 | &clk_name) < 0) |
| 2022 | clk_name = clkspec.np->name; |
| 2023 | |
| 2024 | of_node_put(clkspec.np); |
| 2025 | return clk_name; |
| 2026 | } |
| 2027 | EXPORT_SYMBOL_GPL(of_clk_get_parent_name); |
| 2028 | |
| 2029 | /** |
| 2030 | * of_clk_init() - Scan and init clock providers from the DT |
| 2031 | * @matches: array of compatible values and init functions for providers. |
| 2032 | * |
| 2033 | * This function scans the device tree for matching clock providers and |
| 2034 | * calls their initialization functions |
| 2035 | */ |
| 2036 | void __init of_clk_init(const struct of_device_id *matches) |
| 2037 | { |
| 2038 | struct device_node *np; |
| 2039 | |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 2040 | if (!matches) |
| 2041 | matches = __clk_of_table; |
| 2042 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 2043 | for_each_matching_node(np, matches) { |
| 2044 | const struct of_device_id *match = of_match_node(matches, np); |
| 2045 | of_clk_init_cb_t clk_init_cb = match->data; |
| 2046 | clk_init_cb(np); |
| 2047 | } |
| 2048 | } |
| 2049 | #endif |