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