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