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