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