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