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