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