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