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