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