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