Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> |
| 3 | * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Standard functionality for the common clock API. See Documentation/clk.txt |
| 10 | */ |
| 11 | |
Stephen Boyd | 3c37311 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 12 | #include <linux/clk.h> |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 13 | #include <linux/clk-provider.h> |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 14 | #include <linux/clk/clk-conf.h> |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/slab.h> |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 21 | #include <linux/of.h> |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 22 | #include <linux/device.h> |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 23 | #include <linux/init.h> |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 25 | #include <linux/sched.h> |
Stephen Boyd | 562ef0b | 2015-05-01 12:16:14 -0700 | [diff] [blame] | 26 | #include <linux/clkdev.h> |
Geert Uytterhoeven | a6059ab | 2018-01-03 12:06:16 +0100 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 28 | |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 29 | #include "clk.h" |
| 30 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 31 | static DEFINE_SPINLOCK(enable_lock); |
| 32 | static DEFINE_MUTEX(prepare_lock); |
| 33 | |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 34 | static struct task_struct *prepare_owner; |
| 35 | static struct task_struct *enable_owner; |
| 36 | |
| 37 | static int prepare_refcnt; |
| 38 | static int enable_refcnt; |
| 39 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 40 | static HLIST_HEAD(clk_root_list); |
| 41 | static HLIST_HEAD(clk_orphan_list); |
| 42 | static LIST_HEAD(clk_notifier_list); |
| 43 | |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 44 | /*** private data structures ***/ |
| 45 | |
| 46 | struct clk_core { |
| 47 | const char *name; |
| 48 | const struct clk_ops *ops; |
| 49 | struct clk_hw *hw; |
| 50 | struct module *owner; |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 51 | struct device *dev; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 52 | struct clk_core *parent; |
| 53 | const char **parent_names; |
| 54 | struct clk_core **parents; |
| 55 | u8 num_parents; |
| 56 | u8 new_parent_index; |
| 57 | unsigned long rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 58 | unsigned long req_rate; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 59 | unsigned long new_rate; |
| 60 | struct clk_core *new_parent; |
| 61 | struct clk_core *new_child; |
| 62 | unsigned long flags; |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 63 | bool orphan; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 64 | unsigned int enable_count; |
| 65 | unsigned int prepare_count; |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 66 | unsigned int protect_count; |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 67 | unsigned long min_rate; |
| 68 | unsigned long max_rate; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 69 | unsigned long accuracy; |
| 70 | int phase; |
| 71 | struct hlist_head children; |
| 72 | struct hlist_node child_node; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 73 | struct hlist_head clks; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 74 | unsigned int notifier_count; |
| 75 | #ifdef CONFIG_DEBUG_FS |
| 76 | struct dentry *dentry; |
Maxime Coquelin | 8c9a8a8 | 2015-06-10 13:28:27 +0200 | [diff] [blame] | 77 | struct hlist_node debug_node; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 78 | #endif |
| 79 | struct kref ref; |
| 80 | }; |
| 81 | |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 82 | #define CREATE_TRACE_POINTS |
| 83 | #include <trace/events/clk.h> |
| 84 | |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 85 | struct clk { |
| 86 | struct clk_core *core; |
| 87 | const char *dev_id; |
| 88 | const char *con_id; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 89 | unsigned long min_rate; |
| 90 | unsigned long max_rate; |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 91 | unsigned int exclusive_count; |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 92 | struct hlist_node clks_node; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 95 | /*** runtime pm ***/ |
| 96 | static int clk_pm_runtime_get(struct clk_core *core) |
| 97 | { |
| 98 | int ret = 0; |
| 99 | |
| 100 | if (!core->dev) |
| 101 | return 0; |
| 102 | |
| 103 | ret = pm_runtime_get_sync(core->dev); |
| 104 | return ret < 0 ? ret : 0; |
| 105 | } |
| 106 | |
| 107 | static void clk_pm_runtime_put(struct clk_core *core) |
| 108 | { |
| 109 | if (!core->dev) |
| 110 | return; |
| 111 | |
| 112 | pm_runtime_put_sync(core->dev); |
| 113 | } |
| 114 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 115 | /*** locking ***/ |
| 116 | static void clk_prepare_lock(void) |
| 117 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 118 | if (!mutex_trylock(&prepare_lock)) { |
| 119 | if (prepare_owner == current) { |
| 120 | prepare_refcnt++; |
| 121 | return; |
| 122 | } |
| 123 | mutex_lock(&prepare_lock); |
| 124 | } |
| 125 | WARN_ON_ONCE(prepare_owner != NULL); |
| 126 | WARN_ON_ONCE(prepare_refcnt != 0); |
| 127 | prepare_owner = current; |
| 128 | prepare_refcnt = 1; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void clk_prepare_unlock(void) |
| 132 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 133 | WARN_ON_ONCE(prepare_owner != current); |
| 134 | WARN_ON_ONCE(prepare_refcnt == 0); |
| 135 | |
| 136 | if (--prepare_refcnt) |
| 137 | return; |
| 138 | prepare_owner = NULL; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 139 | mutex_unlock(&prepare_lock); |
| 140 | } |
| 141 | |
| 142 | static unsigned long clk_enable_lock(void) |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 143 | __acquires(enable_lock) |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 144 | { |
| 145 | unsigned long flags; |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 146 | |
David Lechner | a12aa8a | 2018-01-04 19:46:08 -0600 | [diff] [blame] | 147 | /* |
| 148 | * On UP systems, spin_trylock_irqsave() always returns true, even if |
| 149 | * we already hold the lock. So, in that case, we rely only on |
| 150 | * reference counting. |
| 151 | */ |
| 152 | if (!IS_ENABLED(CONFIG_SMP) || |
| 153 | !spin_trylock_irqsave(&enable_lock, flags)) { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 154 | if (enable_owner == current) { |
| 155 | enable_refcnt++; |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 156 | __acquire(enable_lock); |
David Lechner | a12aa8a | 2018-01-04 19:46:08 -0600 | [diff] [blame] | 157 | if (!IS_ENABLED(CONFIG_SMP)) |
| 158 | local_save_flags(flags); |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 159 | return flags; |
| 160 | } |
| 161 | spin_lock_irqsave(&enable_lock, flags); |
| 162 | } |
| 163 | WARN_ON_ONCE(enable_owner != NULL); |
| 164 | WARN_ON_ONCE(enable_refcnt != 0); |
| 165 | enable_owner = current; |
| 166 | enable_refcnt = 1; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 167 | return flags; |
| 168 | } |
| 169 | |
| 170 | static void clk_enable_unlock(unsigned long flags) |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 171 | __releases(enable_lock) |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 172 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 173 | WARN_ON_ONCE(enable_owner != current); |
| 174 | WARN_ON_ONCE(enable_refcnt == 0); |
| 175 | |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 176 | if (--enable_refcnt) { |
| 177 | __release(enable_lock); |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 178 | return; |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 179 | } |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 180 | enable_owner = NULL; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 181 | spin_unlock_irqrestore(&enable_lock, flags); |
| 182 | } |
| 183 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 184 | static bool clk_core_rate_is_protected(struct clk_core *core) |
| 185 | { |
| 186 | return core->protect_count; |
| 187 | } |
| 188 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 189 | static bool clk_core_is_prepared(struct clk_core *core) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 190 | { |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 191 | bool ret = false; |
| 192 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 193 | /* |
| 194 | * .is_prepared is optional for clocks that can prepare |
| 195 | * fall back to software usage counter if it is missing |
| 196 | */ |
| 197 | if (!core->ops->is_prepared) |
| 198 | return core->prepare_count; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 199 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 200 | if (!clk_pm_runtime_get(core)) { |
| 201 | ret = core->ops->is_prepared(core->hw); |
| 202 | clk_pm_runtime_put(core); |
| 203 | } |
| 204 | |
| 205 | return ret; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 206 | } |
| 207 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 208 | static bool clk_core_is_enabled(struct clk_core *core) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 209 | { |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 210 | bool ret = false; |
| 211 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 212 | /* |
| 213 | * .is_enabled is only mandatory for clocks that gate |
| 214 | * fall back to software usage counter if .is_enabled is missing |
| 215 | */ |
| 216 | if (!core->ops->is_enabled) |
| 217 | return core->enable_count; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 218 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 219 | /* |
| 220 | * Check if clock controller's device is runtime active before |
| 221 | * calling .is_enabled callback. If not, assume that clock is |
| 222 | * disabled, because we might be called from atomic context, from |
| 223 | * which pm_runtime_get() is not allowed. |
| 224 | * This function is called mainly from clk_disable_unused_subtree, |
| 225 | * which ensures proper runtime pm activation of controller before |
| 226 | * taking enable spinlock, but the below check is needed if one tries |
| 227 | * to call it from other places. |
| 228 | */ |
| 229 | if (core->dev) { |
| 230 | pm_runtime_get_noresume(core->dev); |
| 231 | if (!pm_runtime_active(core->dev)) { |
| 232 | ret = false; |
| 233 | goto done; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | ret = core->ops->is_enabled(core->hw); |
| 238 | done: |
Dong Aisheng | 756efe1 | 2017-12-22 17:46:04 +0800 | [diff] [blame] | 239 | if (core->dev) |
| 240 | pm_runtime_put(core->dev); |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 241 | |
| 242 | return ret; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 243 | } |
| 244 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 245 | /*** helper functions ***/ |
| 246 | |
Geert Uytterhoeven | b76281c | 2015-10-16 14:35:21 +0200 | [diff] [blame] | 247 | const char *__clk_get_name(const struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 248 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 249 | return !clk ? NULL : clk->core->name; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 250 | } |
Niels de Vos | 4895084 | 2012-12-13 13:12:25 +0100 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(__clk_get_name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 252 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 253 | const char *clk_hw_get_name(const struct clk_hw *hw) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 254 | { |
| 255 | return hw->core->name; |
| 256 | } |
| 257 | EXPORT_SYMBOL_GPL(clk_hw_get_name); |
| 258 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 259 | struct clk_hw *__clk_get_hw(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 260 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 261 | return !clk ? NULL : clk->core->hw; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 262 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 263 | EXPORT_SYMBOL_GPL(__clk_get_hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 264 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 265 | unsigned int clk_hw_get_num_parents(const struct clk_hw *hw) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 266 | { |
| 267 | return hw->core->num_parents; |
| 268 | } |
| 269 | EXPORT_SYMBOL_GPL(clk_hw_get_num_parents); |
| 270 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 271 | struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 272 | { |
| 273 | return hw->core->parent ? hw->core->parent->hw : NULL; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(clk_hw_get_parent); |
| 276 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 277 | static struct clk_core *__clk_lookup_subtree(const char *name, |
| 278 | struct clk_core *core) |
| 279 | { |
| 280 | struct clk_core *child; |
| 281 | struct clk_core *ret; |
| 282 | |
| 283 | if (!strcmp(core->name, name)) |
| 284 | return core; |
| 285 | |
| 286 | hlist_for_each_entry(child, &core->children, child_node) { |
| 287 | ret = __clk_lookup_subtree(name, child); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | static struct clk_core *clk_core_lookup(const char *name) |
| 296 | { |
| 297 | struct clk_core *root_clk; |
| 298 | struct clk_core *ret; |
| 299 | |
| 300 | if (!name) |
| 301 | return NULL; |
| 302 | |
| 303 | /* search the 'proper' clk tree first */ |
| 304 | hlist_for_each_entry(root_clk, &clk_root_list, child_node) { |
| 305 | ret = __clk_lookup_subtree(name, root_clk); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* if not found, then search the orphan tree */ |
| 311 | hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { |
| 312 | ret = __clk_lookup_subtree(name, root_clk); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 320 | 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] | 321 | u8 index) |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 322 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 323 | if (!core || index >= core->num_parents) |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 324 | return NULL; |
Masahiro Yamada | 88cfbef | 2015-12-28 19:23:01 +0900 | [diff] [blame] | 325 | |
| 326 | if (!core->parents[index]) |
| 327 | core->parents[index] = |
| 328 | clk_core_lookup(core->parent_names[index]); |
| 329 | |
| 330 | return core->parents[index]; |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 331 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 332 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 333 | struct clk_hw * |
| 334 | clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 335 | { |
| 336 | struct clk_core *parent; |
| 337 | |
| 338 | parent = clk_core_get_parent_by_index(hw->core, index); |
| 339 | |
| 340 | return !parent ? NULL : parent->hw; |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index); |
| 343 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 344 | unsigned int __clk_get_enable_count(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 345 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 346 | return !clk ? 0 : clk->core->enable_count; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 349 | static unsigned long clk_core_get_rate_nolock(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 350 | { |
| 351 | unsigned long ret; |
| 352 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 353 | if (!core) { |
Rajendra Nayak | 34e44fe | 2012-03-26 19:01:48 +0530 | [diff] [blame] | 354 | ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 355 | goto out; |
| 356 | } |
| 357 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 358 | ret = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 359 | |
Stephen Boyd | 47b0eeb | 2016-02-02 17:24:56 -0800 | [diff] [blame] | 360 | if (!core->num_parents) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 361 | goto out; |
| 362 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 363 | if (!core->parent) |
Rajendra Nayak | 34e44fe | 2012-03-26 19:01:48 +0530 | [diff] [blame] | 364 | ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 365 | |
| 366 | out: |
| 367 | return ret; |
| 368 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 369 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 370 | unsigned long clk_hw_get_rate(const struct clk_hw *hw) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 371 | { |
| 372 | return clk_core_get_rate_nolock(hw->core); |
| 373 | } |
| 374 | EXPORT_SYMBOL_GPL(clk_hw_get_rate); |
| 375 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 376 | static unsigned long __clk_get_accuracy(struct clk_core *core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 377 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 378 | if (!core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 379 | return 0; |
| 380 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 381 | return core->accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 384 | unsigned long __clk_get_flags(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 385 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 386 | return !clk ? 0 : clk->core->flags; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 387 | } |
Thierry Reding | b05c683 | 2013-09-03 09:43:51 +0200 | [diff] [blame] | 388 | EXPORT_SYMBOL_GPL(__clk_get_flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 389 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 390 | unsigned long clk_hw_get_flags(const struct clk_hw *hw) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 391 | { |
| 392 | return hw->core->flags; |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(clk_hw_get_flags); |
| 395 | |
Stephen Boyd | e7df6f6 | 2015-08-12 13:04:56 -0700 | [diff] [blame] | 396 | bool clk_hw_is_prepared(const struct clk_hw *hw) |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 397 | { |
| 398 | return clk_core_is_prepared(hw->core); |
| 399 | } |
| 400 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 401 | bool clk_hw_rate_is_protected(const struct clk_hw *hw) |
| 402 | { |
| 403 | return clk_core_rate_is_protected(hw->core); |
| 404 | } |
| 405 | |
Joachim Eastwood | be68bf8 | 2015-10-24 18:55:22 +0200 | [diff] [blame] | 406 | bool clk_hw_is_enabled(const struct clk_hw *hw) |
| 407 | { |
| 408 | return clk_core_is_enabled(hw->core); |
| 409 | } |
| 410 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 411 | bool __clk_is_enabled(struct clk *clk) |
| 412 | { |
| 413 | if (!clk) |
| 414 | return false; |
| 415 | |
| 416 | return clk_core_is_enabled(clk->core); |
| 417 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(__clk_is_enabled); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 419 | |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 420 | static bool mux_is_better_rate(unsigned long rate, unsigned long now, |
| 421 | unsigned long best, unsigned long flags) |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 422 | { |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 423 | if (flags & CLK_MUX_ROUND_CLOSEST) |
| 424 | return abs(now - rate) < abs(best - rate); |
| 425 | |
| 426 | return now <= rate && now > best; |
| 427 | } |
| 428 | |
Jerome Brunet | 4ad69b80 | 2018-04-09 15:59:20 +0200 | [diff] [blame] | 429 | int clk_mux_determine_rate_flags(struct clk_hw *hw, |
| 430 | struct clk_rate_request *req, |
| 431 | unsigned long flags) |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 432 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 433 | struct clk_core *core = hw->core, *parent, *best_parent = NULL; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 434 | int i, num_parents, ret; |
| 435 | unsigned long best = 0; |
| 436 | struct clk_rate_request parent_req = *req; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 437 | |
| 438 | /* if NO_REPARENT flag set, pass through to current parent */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 439 | if (core->flags & CLK_SET_RATE_NO_REPARENT) { |
| 440 | parent = core->parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 441 | if (core->flags & CLK_SET_RATE_PARENT) { |
| 442 | ret = __clk_determine_rate(parent ? parent->hw : NULL, |
| 443 | &parent_req); |
| 444 | if (ret) |
| 445 | return ret; |
| 446 | |
| 447 | best = parent_req.rate; |
| 448 | } else if (parent) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 449 | best = clk_core_get_rate_nolock(parent); |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 450 | } else { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 451 | best = clk_core_get_rate_nolock(core); |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 452 | } |
| 453 | |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 454 | goto out; |
| 455 | } |
| 456 | |
| 457 | /* find the parent that can provide the fastest rate <= rate */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 458 | num_parents = core->num_parents; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 459 | for (i = 0; i < num_parents; i++) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 460 | parent = clk_core_get_parent_by_index(core, i); |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 461 | if (!parent) |
| 462 | continue; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 463 | |
| 464 | if (core->flags & CLK_SET_RATE_PARENT) { |
| 465 | parent_req = *req; |
| 466 | ret = __clk_determine_rate(parent->hw, &parent_req); |
| 467 | if (ret) |
| 468 | continue; |
| 469 | } else { |
| 470 | parent_req.rate = clk_core_get_rate_nolock(parent); |
| 471 | } |
| 472 | |
| 473 | if (mux_is_better_rate(req->rate, parent_req.rate, |
| 474 | best, flags)) { |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 475 | best_parent = parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 476 | best = parent_req.rate; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Boris Brezillon | 57d866e | 2015-07-09 22:39:38 +0200 | [diff] [blame] | 480 | if (!best_parent) |
| 481 | return -EINVAL; |
| 482 | |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 483 | out: |
| 484 | if (best_parent) |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 485 | req->best_parent_hw = best_parent->hw; |
| 486 | req->best_parent_rate = best; |
| 487 | req->rate = best; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 488 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 489 | return 0; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 490 | } |
Jerome Brunet | 4ad69b80 | 2018-04-09 15:59:20 +0200 | [diff] [blame] | 491 | EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags); |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 492 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 493 | struct clk *__clk_lookup(const char *name) |
| 494 | { |
| 495 | struct clk_core *core = clk_core_lookup(name); |
| 496 | |
| 497 | return !core ? NULL : core->hw->clk; |
| 498 | } |
| 499 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 500 | static void clk_core_get_boundaries(struct clk_core *core, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 501 | unsigned long *min_rate, |
| 502 | unsigned long *max_rate) |
| 503 | { |
| 504 | struct clk *clk_user; |
| 505 | |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 506 | *min_rate = core->min_rate; |
| 507 | *max_rate = core->max_rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 508 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 509 | hlist_for_each_entry(clk_user, &core->clks, clks_node) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 510 | *min_rate = max(*min_rate, clk_user->min_rate); |
| 511 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 512 | hlist_for_each_entry(clk_user, &core->clks, clks_node) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 513 | *max_rate = min(*max_rate, clk_user->max_rate); |
| 514 | } |
| 515 | |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 516 | void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate, |
| 517 | unsigned long max_rate) |
| 518 | { |
| 519 | hw->core->min_rate = min_rate; |
| 520 | hw->core->max_rate = max_rate; |
| 521 | } |
| 522 | EXPORT_SYMBOL_GPL(clk_hw_set_rate_range); |
| 523 | |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 524 | /* |
| 525 | * Helper for finding best parent to provide a given frequency. This can be used |
| 526 | * directly as a determine_rate callback (e.g. for a mux), or from a more |
| 527 | * complex clock that may combine a mux with other operations. |
| 528 | */ |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 529 | int __clk_mux_determine_rate(struct clk_hw *hw, |
| 530 | struct clk_rate_request *req) |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 531 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 532 | return clk_mux_determine_rate_flags(hw, req, 0); |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 533 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 534 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate); |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 535 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 536 | int __clk_mux_determine_rate_closest(struct clk_hw *hw, |
| 537 | struct clk_rate_request *req) |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 538 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 539 | return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST); |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 540 | } |
| 541 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest); |
| 542 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 543 | /*** clk api ***/ |
| 544 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 545 | static void clk_core_rate_unprotect(struct clk_core *core) |
| 546 | { |
| 547 | lockdep_assert_held(&prepare_lock); |
| 548 | |
| 549 | if (!core) |
| 550 | return; |
| 551 | |
| 552 | if (WARN_ON(core->protect_count == 0)) |
| 553 | return; |
| 554 | |
| 555 | if (--core->protect_count > 0) |
| 556 | return; |
| 557 | |
| 558 | clk_core_rate_unprotect(core->parent); |
| 559 | } |
| 560 | |
| 561 | static int clk_core_rate_nuke_protect(struct clk_core *core) |
| 562 | { |
| 563 | int ret; |
| 564 | |
| 565 | lockdep_assert_held(&prepare_lock); |
| 566 | |
| 567 | if (!core) |
| 568 | return -EINVAL; |
| 569 | |
| 570 | if (core->protect_count == 0) |
| 571 | return 0; |
| 572 | |
| 573 | ret = core->protect_count; |
| 574 | core->protect_count = 1; |
| 575 | clk_core_rate_unprotect(core); |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 580 | /** |
| 581 | * clk_rate_exclusive_put - release exclusivity over clock rate control |
| 582 | * @clk: the clk over which the exclusivity is released |
| 583 | * |
| 584 | * clk_rate_exclusive_put() completes a critical section during which a clock |
| 585 | * consumer cannot tolerate any other consumer making any operation on the |
| 586 | * clock which could result in a rate change or rate glitch. Exclusive clocks |
| 587 | * cannot have their rate changed, either directly or indirectly due to changes |
| 588 | * further up the parent chain of clocks. As a result, clocks up parent chain |
| 589 | * also get under exclusive control of the calling consumer. |
| 590 | * |
| 591 | * If exlusivity is claimed more than once on clock, even by the same consumer, |
| 592 | * the rate effectively gets locked as exclusivity can't be preempted. |
| 593 | * |
| 594 | * Calls to clk_rate_exclusive_put() must be balanced with calls to |
| 595 | * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return |
| 596 | * error status. |
| 597 | */ |
| 598 | void clk_rate_exclusive_put(struct clk *clk) |
| 599 | { |
| 600 | if (!clk) |
| 601 | return; |
| 602 | |
| 603 | clk_prepare_lock(); |
| 604 | |
| 605 | /* |
| 606 | * if there is something wrong with this consumer protect count, stop |
| 607 | * here before messing with the provider |
| 608 | */ |
| 609 | if (WARN_ON(clk->exclusive_count <= 0)) |
| 610 | goto out; |
| 611 | |
| 612 | clk_core_rate_unprotect(clk->core); |
| 613 | clk->exclusive_count--; |
| 614 | out: |
| 615 | clk_prepare_unlock(); |
| 616 | } |
| 617 | EXPORT_SYMBOL_GPL(clk_rate_exclusive_put); |
| 618 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 619 | static void clk_core_rate_protect(struct clk_core *core) |
| 620 | { |
| 621 | lockdep_assert_held(&prepare_lock); |
| 622 | |
| 623 | if (!core) |
| 624 | return; |
| 625 | |
| 626 | if (core->protect_count == 0) |
| 627 | clk_core_rate_protect(core->parent); |
| 628 | |
| 629 | core->protect_count++; |
| 630 | } |
| 631 | |
| 632 | static void clk_core_rate_restore_protect(struct clk_core *core, int count) |
| 633 | { |
| 634 | lockdep_assert_held(&prepare_lock); |
| 635 | |
| 636 | if (!core) |
| 637 | return; |
| 638 | |
| 639 | if (count == 0) |
| 640 | return; |
| 641 | |
| 642 | clk_core_rate_protect(core); |
| 643 | core->protect_count = count; |
| 644 | } |
| 645 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 646 | /** |
| 647 | * clk_rate_exclusive_get - get exclusivity over the clk rate control |
| 648 | * @clk: the clk over which the exclusity of rate control is requested |
| 649 | * |
| 650 | * clk_rate_exlusive_get() begins a critical section during which a clock |
| 651 | * consumer cannot tolerate any other consumer making any operation on the |
| 652 | * clock which could result in a rate change or rate glitch. Exclusive clocks |
| 653 | * cannot have their rate changed, either directly or indirectly due to changes |
| 654 | * further up the parent chain of clocks. As a result, clocks up parent chain |
| 655 | * also get under exclusive control of the calling consumer. |
| 656 | * |
| 657 | * If exlusivity is claimed more than once on clock, even by the same consumer, |
| 658 | * the rate effectively gets locked as exclusivity can't be preempted. |
| 659 | * |
| 660 | * Calls to clk_rate_exclusive_get() should be balanced with calls to |
| 661 | * clk_rate_exclusive_put(). Calls to this function may sleep. |
| 662 | * Returns 0 on success, -EERROR otherwise |
| 663 | */ |
| 664 | int clk_rate_exclusive_get(struct clk *clk) |
| 665 | { |
| 666 | if (!clk) |
| 667 | return 0; |
| 668 | |
| 669 | clk_prepare_lock(); |
| 670 | clk_core_rate_protect(clk->core); |
| 671 | clk->exclusive_count++; |
| 672 | clk_prepare_unlock(); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | EXPORT_SYMBOL_GPL(clk_rate_exclusive_get); |
| 677 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 678 | static void clk_core_unprepare(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 679 | { |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 680 | lockdep_assert_held(&prepare_lock); |
| 681 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 682 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 683 | return; |
| 684 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 685 | if (WARN_ON(core->prepare_count == 0)) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 686 | return; |
| 687 | |
Lee Jones | 2e20fbf | 2016-02-11 13:19:10 -0800 | [diff] [blame] | 688 | if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL)) |
| 689 | return; |
| 690 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 691 | if (--core->prepare_count > 0) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 692 | return; |
| 693 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 694 | WARN_ON(core->enable_count > 0); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 695 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 696 | trace_clk_unprepare(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 697 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 698 | if (core->ops->unprepare) |
| 699 | core->ops->unprepare(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 700 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 701 | clk_pm_runtime_put(core); |
| 702 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 703 | trace_clk_unprepare_complete(core); |
| 704 | clk_core_unprepare(core->parent); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 707 | static void clk_core_unprepare_lock(struct clk_core *core) |
| 708 | { |
| 709 | clk_prepare_lock(); |
| 710 | clk_core_unprepare(core); |
| 711 | clk_prepare_unlock(); |
| 712 | } |
| 713 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 714 | /** |
| 715 | * clk_unprepare - undo preparation of a clock source |
Peter Meerwald | 24ee1a0 | 2013-06-29 15:14:19 +0200 | [diff] [blame] | 716 | * @clk: the clk being unprepared |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 717 | * |
| 718 | * clk_unprepare may sleep, which differentiates it from clk_disable. In a |
| 719 | * simple case, clk_unprepare can be used instead of clk_disable to gate a clk |
| 720 | * if the operation may sleep. One example is a clk which is accessed over |
| 721 | * I2c. In the complex case a clk gate operation may require a fast and a slow |
| 722 | * part. It is this reason that clk_unprepare and clk_disable are not mutually |
| 723 | * exclusive. In fact clk_disable must be called before clk_unprepare. |
| 724 | */ |
| 725 | void clk_unprepare(struct clk *clk) |
| 726 | { |
Stephen Boyd | 63589e9 | 2014-03-26 16:06:37 -0700 | [diff] [blame] | 727 | if (IS_ERR_OR_NULL(clk)) |
| 728 | return; |
| 729 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 730 | clk_core_unprepare_lock(clk->core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 731 | } |
| 732 | EXPORT_SYMBOL_GPL(clk_unprepare); |
| 733 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 734 | static int clk_core_prepare(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 735 | { |
| 736 | int ret = 0; |
| 737 | |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 738 | lockdep_assert_held(&prepare_lock); |
| 739 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 740 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 741 | return 0; |
| 742 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 743 | if (core->prepare_count == 0) { |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 744 | ret = clk_pm_runtime_get(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 745 | if (ret) |
| 746 | return ret; |
| 747 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 748 | ret = clk_core_prepare(core->parent); |
| 749 | if (ret) |
| 750 | goto runtime_put; |
| 751 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 752 | trace_clk_prepare(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 753 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 754 | if (core->ops->prepare) |
| 755 | ret = core->ops->prepare(core->hw); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 756 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 757 | trace_clk_prepare_complete(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 758 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 759 | if (ret) |
| 760 | goto unprepare; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 763 | core->prepare_count++; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 764 | |
| 765 | return 0; |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 766 | unprepare: |
| 767 | clk_core_unprepare(core->parent); |
| 768 | runtime_put: |
| 769 | clk_pm_runtime_put(core); |
| 770 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 773 | static int clk_core_prepare_lock(struct clk_core *core) |
| 774 | { |
| 775 | int ret; |
| 776 | |
| 777 | clk_prepare_lock(); |
| 778 | ret = clk_core_prepare(core); |
| 779 | clk_prepare_unlock(); |
| 780 | |
| 781 | return ret; |
| 782 | } |
| 783 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 784 | /** |
| 785 | * clk_prepare - prepare a clock source |
| 786 | * @clk: the clk being prepared |
| 787 | * |
| 788 | * clk_prepare may sleep, which differentiates it from clk_enable. In a simple |
| 789 | * case, clk_prepare can be used instead of clk_enable to ungate a clk if the |
| 790 | * operation may sleep. One example is a clk which is accessed over I2c. In |
| 791 | * the complex case a clk ungate operation may require a fast and a slow part. |
| 792 | * It is this reason that clk_prepare and clk_enable are not mutually |
| 793 | * exclusive. In fact clk_prepare must be called before clk_enable. |
| 794 | * Returns 0 on success, -EERROR otherwise. |
| 795 | */ |
| 796 | int clk_prepare(struct clk *clk) |
| 797 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 798 | if (!clk) |
| 799 | return 0; |
| 800 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 801 | return clk_core_prepare_lock(clk->core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 802 | } |
| 803 | EXPORT_SYMBOL_GPL(clk_prepare); |
| 804 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 805 | static void clk_core_disable(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 806 | { |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 807 | lockdep_assert_held(&enable_lock); |
| 808 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 809 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 810 | return; |
| 811 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 812 | if (WARN_ON(core->enable_count == 0)) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 813 | return; |
| 814 | |
Lee Jones | 2e20fbf | 2016-02-11 13:19:10 -0800 | [diff] [blame] | 815 | if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL)) |
| 816 | return; |
| 817 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 818 | if (--core->enable_count > 0) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 819 | return; |
| 820 | |
Paul E. McKenney | 2f87a6e | 2016-04-26 12:43:57 -0700 | [diff] [blame] | 821 | trace_clk_disable_rcuidle(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 822 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 823 | if (core->ops->disable) |
| 824 | core->ops->disable(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 825 | |
Paul E. McKenney | 2f87a6e | 2016-04-26 12:43:57 -0700 | [diff] [blame] | 826 | trace_clk_disable_complete_rcuidle(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 827 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 828 | clk_core_disable(core->parent); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 831 | static void clk_core_disable_lock(struct clk_core *core) |
| 832 | { |
| 833 | unsigned long flags; |
| 834 | |
| 835 | flags = clk_enable_lock(); |
| 836 | clk_core_disable(core); |
| 837 | clk_enable_unlock(flags); |
| 838 | } |
| 839 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 840 | /** |
| 841 | * clk_disable - gate a clock |
| 842 | * @clk: the clk being gated |
| 843 | * |
| 844 | * clk_disable must not sleep, which differentiates it from clk_unprepare. In |
| 845 | * a simple case, clk_disable can be used instead of clk_unprepare to gate a |
| 846 | * clk if the operation is fast and will never sleep. One example is a |
| 847 | * SoC-internal clk which is controlled via simple register writes. In the |
| 848 | * complex case a clk gate operation may require a fast and a slow part. It is |
| 849 | * this reason that clk_unprepare and clk_disable are not mutually exclusive. |
| 850 | * In fact clk_disable must be called before clk_unprepare. |
| 851 | */ |
| 852 | void clk_disable(struct clk *clk) |
| 853 | { |
Stephen Boyd | 63589e9 | 2014-03-26 16:06:37 -0700 | [diff] [blame] | 854 | if (IS_ERR_OR_NULL(clk)) |
| 855 | return; |
| 856 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 857 | clk_core_disable_lock(clk->core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 858 | } |
| 859 | EXPORT_SYMBOL_GPL(clk_disable); |
| 860 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 861 | static int clk_core_enable(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 862 | { |
| 863 | int ret = 0; |
| 864 | |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 865 | lockdep_assert_held(&enable_lock); |
| 866 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 867 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 868 | return 0; |
| 869 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 870 | if (WARN_ON(core->prepare_count == 0)) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 871 | return -ESHUTDOWN; |
| 872 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 873 | if (core->enable_count == 0) { |
| 874 | ret = clk_core_enable(core->parent); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 875 | |
| 876 | if (ret) |
| 877 | return ret; |
| 878 | |
Paul E. McKenney | f17a0dd | 2016-04-26 14:02:23 -0700 | [diff] [blame] | 879 | trace_clk_enable_rcuidle(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 880 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 881 | if (core->ops->enable) |
| 882 | ret = core->ops->enable(core->hw); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 883 | |
Paul E. McKenney | f17a0dd | 2016-04-26 14:02:23 -0700 | [diff] [blame] | 884 | trace_clk_enable_complete_rcuidle(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 885 | |
| 886 | if (ret) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 887 | clk_core_disable(core->parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 888 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 892 | core->enable_count++; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 896 | static int clk_core_enable_lock(struct clk_core *core) |
| 897 | { |
| 898 | unsigned long flags; |
| 899 | int ret; |
| 900 | |
| 901 | flags = clk_enable_lock(); |
| 902 | ret = clk_core_enable(core); |
| 903 | clk_enable_unlock(flags); |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 908 | /** |
| 909 | * clk_enable - ungate a clock |
| 910 | * @clk: the clk being ungated |
| 911 | * |
| 912 | * clk_enable must not sleep, which differentiates it from clk_prepare. In a |
| 913 | * simple case, clk_enable can be used instead of clk_prepare to ungate a clk |
| 914 | * if the operation will never sleep. One example is a SoC-internal clk which |
| 915 | * is controlled via simple register writes. In the complex case a clk ungate |
| 916 | * operation may require a fast and a slow part. It is this reason that |
| 917 | * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare |
| 918 | * must be called before clk_enable. Returns 0 on success, -EERROR |
| 919 | * otherwise. |
| 920 | */ |
| 921 | int clk_enable(struct clk *clk) |
| 922 | { |
Dong Aisheng | 864e160 | 2015-04-30 14:02:19 -0700 | [diff] [blame] | 923 | if (!clk) |
| 924 | return 0; |
| 925 | |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 926 | return clk_core_enable_lock(clk->core); |
| 927 | } |
| 928 | EXPORT_SYMBOL_GPL(clk_enable); |
| 929 | |
| 930 | static int clk_core_prepare_enable(struct clk_core *core) |
| 931 | { |
| 932 | int ret; |
| 933 | |
| 934 | ret = clk_core_prepare_lock(core); |
| 935 | if (ret) |
| 936 | return ret; |
| 937 | |
| 938 | ret = clk_core_enable_lock(core); |
| 939 | if (ret) |
| 940 | clk_core_unprepare_lock(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 941 | |
| 942 | return ret; |
| 943 | } |
Dong Aisheng | a6adc30 | 2016-06-30 17:31:11 +0800 | [diff] [blame] | 944 | |
| 945 | static void clk_core_disable_unprepare(struct clk_core *core) |
| 946 | { |
| 947 | clk_core_disable_lock(core); |
| 948 | clk_core_unprepare_lock(core); |
| 949 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 950 | |
Dong Aisheng | 7ec986e | 2016-06-30 17:31:12 +0800 | [diff] [blame] | 951 | static void clk_unprepare_unused_subtree(struct clk_core *core) |
| 952 | { |
| 953 | struct clk_core *child; |
| 954 | |
| 955 | lockdep_assert_held(&prepare_lock); |
| 956 | |
| 957 | hlist_for_each_entry(child, &core->children, child_node) |
| 958 | clk_unprepare_unused_subtree(child); |
| 959 | |
| 960 | if (core->prepare_count) |
| 961 | return; |
| 962 | |
| 963 | if (core->flags & CLK_IGNORE_UNUSED) |
| 964 | return; |
| 965 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 966 | if (clk_pm_runtime_get(core)) |
| 967 | return; |
| 968 | |
Dong Aisheng | 7ec986e | 2016-06-30 17:31:12 +0800 | [diff] [blame] | 969 | if (clk_core_is_prepared(core)) { |
| 970 | trace_clk_unprepare(core); |
| 971 | if (core->ops->unprepare_unused) |
| 972 | core->ops->unprepare_unused(core->hw); |
| 973 | else if (core->ops->unprepare) |
| 974 | core->ops->unprepare(core->hw); |
| 975 | trace_clk_unprepare_complete(core); |
| 976 | } |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 977 | |
| 978 | clk_pm_runtime_put(core); |
Dong Aisheng | 7ec986e | 2016-06-30 17:31:12 +0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static void clk_disable_unused_subtree(struct clk_core *core) |
| 982 | { |
| 983 | struct clk_core *child; |
| 984 | unsigned long flags; |
| 985 | |
| 986 | lockdep_assert_held(&prepare_lock); |
| 987 | |
| 988 | hlist_for_each_entry(child, &core->children, child_node) |
| 989 | clk_disable_unused_subtree(child); |
| 990 | |
Dong Aisheng | a4b3518 | 2016-06-30 17:31:13 +0800 | [diff] [blame] | 991 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
| 992 | clk_core_prepare_enable(core->parent); |
| 993 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 994 | if (clk_pm_runtime_get(core)) |
| 995 | goto unprepare_out; |
| 996 | |
Dong Aisheng | 7ec986e | 2016-06-30 17:31:12 +0800 | [diff] [blame] | 997 | flags = clk_enable_lock(); |
| 998 | |
| 999 | if (core->enable_count) |
| 1000 | goto unlock_out; |
| 1001 | |
| 1002 | if (core->flags & CLK_IGNORE_UNUSED) |
| 1003 | goto unlock_out; |
| 1004 | |
| 1005 | /* |
| 1006 | * some gate clocks have special needs during the disable-unused |
| 1007 | * sequence. call .disable_unused if available, otherwise fall |
| 1008 | * back to .disable |
| 1009 | */ |
| 1010 | if (clk_core_is_enabled(core)) { |
| 1011 | trace_clk_disable(core); |
| 1012 | if (core->ops->disable_unused) |
| 1013 | core->ops->disable_unused(core->hw); |
| 1014 | else if (core->ops->disable) |
| 1015 | core->ops->disable(core->hw); |
| 1016 | trace_clk_disable_complete(core); |
| 1017 | } |
| 1018 | |
| 1019 | unlock_out: |
| 1020 | clk_enable_unlock(flags); |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1021 | clk_pm_runtime_put(core); |
| 1022 | unprepare_out: |
Dong Aisheng | a4b3518 | 2016-06-30 17:31:13 +0800 | [diff] [blame] | 1023 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
| 1024 | clk_core_disable_unprepare(core->parent); |
Dong Aisheng | 7ec986e | 2016-06-30 17:31:12 +0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static bool clk_ignore_unused; |
| 1028 | static int __init clk_ignore_unused_setup(char *__unused) |
| 1029 | { |
| 1030 | clk_ignore_unused = true; |
| 1031 | return 1; |
| 1032 | } |
| 1033 | __setup("clk_ignore_unused", clk_ignore_unused_setup); |
| 1034 | |
| 1035 | static int clk_disable_unused(void) |
| 1036 | { |
| 1037 | struct clk_core *core; |
| 1038 | |
| 1039 | if (clk_ignore_unused) { |
| 1040 | pr_warn("clk: Not disabling unused clocks\n"); |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | clk_prepare_lock(); |
| 1045 | |
| 1046 | hlist_for_each_entry(core, &clk_root_list, child_node) |
| 1047 | clk_disable_unused_subtree(core); |
| 1048 | |
| 1049 | hlist_for_each_entry(core, &clk_orphan_list, child_node) |
| 1050 | clk_disable_unused_subtree(core); |
| 1051 | |
| 1052 | hlist_for_each_entry(core, &clk_root_list, child_node) |
| 1053 | clk_unprepare_unused_subtree(core); |
| 1054 | |
| 1055 | hlist_for_each_entry(core, &clk_orphan_list, child_node) |
| 1056 | clk_unprepare_unused_subtree(core); |
| 1057 | |
| 1058 | clk_prepare_unlock(); |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | late_initcall_sync(clk_disable_unused); |
| 1063 | |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1064 | static int clk_core_determine_round_nolock(struct clk_core *core, |
| 1065 | struct clk_rate_request *req) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1066 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1067 | long rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1068 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 1069 | lockdep_assert_held(&prepare_lock); |
| 1070 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1071 | if (!core) |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 1072 | return 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1073 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 1074 | /* |
| 1075 | * At this point, core protection will be disabled if |
| 1076 | * - if the provider is not protected at all |
| 1077 | * - if the calling consumer is the only one which has exclusivity |
| 1078 | * over the provider |
| 1079 | */ |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 1080 | if (clk_core_rate_is_protected(core)) { |
| 1081 | req->rate = core->rate; |
| 1082 | } else if (core->ops->determine_rate) { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1083 | return core->ops->determine_rate(core->hw, req); |
| 1084 | } else if (core->ops->round_rate) { |
| 1085 | rate = core->ops->round_rate(core->hw, req->rate, |
| 1086 | &req->best_parent_rate); |
| 1087 | if (rate < 0) |
| 1088 | return rate; |
| 1089 | |
| 1090 | req->rate = rate; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1091 | } else { |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1092 | return -EINVAL; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | return 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1096 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1097 | |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1098 | static void clk_core_init_rate_req(struct clk_core * const core, |
| 1099 | struct clk_rate_request *req) |
| 1100 | { |
| 1101 | struct clk_core *parent; |
| 1102 | |
| 1103 | if (WARN_ON(!core || !req)) |
| 1104 | return; |
| 1105 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1106 | parent = core->parent; |
| 1107 | if (parent) { |
| 1108 | req->best_parent_hw = parent->hw; |
| 1109 | req->best_parent_rate = parent->rate; |
| 1110 | } else { |
| 1111 | req->best_parent_hw = NULL; |
| 1112 | req->best_parent_rate = 0; |
| 1113 | } |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1114 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1115 | |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1116 | static bool clk_core_can_round(struct clk_core * const core) |
| 1117 | { |
| 1118 | if (core->ops->determine_rate || core->ops->round_rate) |
| 1119 | return true; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1120 | |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1121 | return false; |
| 1122 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1123 | |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1124 | static int clk_core_round_rate_nolock(struct clk_core *core, |
| 1125 | struct clk_rate_request *req) |
| 1126 | { |
| 1127 | lockdep_assert_held(&prepare_lock); |
| 1128 | |
Jerome Brunet | 04bf9ab | 2018-02-14 14:43:35 +0100 | [diff] [blame] | 1129 | if (!core) { |
| 1130 | req->rate = 0; |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1131 | return 0; |
Jerome Brunet | 04bf9ab | 2018-02-14 14:43:35 +0100 | [diff] [blame] | 1132 | } |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1133 | |
| 1134 | clk_core_init_rate_req(core, req); |
| 1135 | |
| 1136 | if (clk_core_can_round(core)) |
| 1137 | return clk_core_determine_round_nolock(core, req); |
| 1138 | else if (core->flags & CLK_SET_RATE_PARENT) |
| 1139 | return clk_core_round_rate_nolock(core->parent, req); |
| 1140 | |
| 1141 | req->rate = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1144 | |
| 1145 | /** |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1146 | * __clk_determine_rate - get the closest rate actually supported by a clock |
| 1147 | * @hw: determine the rate of this clock |
Peng Fan | 2d5b520 | 2016-06-13 19:34:21 +0800 | [diff] [blame] | 1148 | * @req: target rate request |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1149 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 1150 | * Useful for clk_ops such as .set_rate and .determine_rate. |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1151 | */ |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1152 | 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] | 1153 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1154 | if (!hw) { |
| 1155 | req->rate = 0; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1156 | return 0; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1157 | } |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1158 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1159 | return clk_core_round_rate_nolock(hw->core, req); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(__clk_determine_rate); |
| 1162 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame] | 1163 | unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate) |
| 1164 | { |
| 1165 | int ret; |
| 1166 | struct clk_rate_request req; |
| 1167 | |
| 1168 | clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); |
| 1169 | req.rate = rate; |
| 1170 | |
| 1171 | ret = clk_core_round_rate_nolock(hw->core, &req); |
| 1172 | if (ret) |
| 1173 | return 0; |
| 1174 | |
| 1175 | return req.rate; |
| 1176 | } |
| 1177 | EXPORT_SYMBOL_GPL(clk_hw_round_rate); |
| 1178 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1179 | /** |
| 1180 | * clk_round_rate - round the given rate for a clk |
| 1181 | * @clk: the clk for which we are rounding a rate |
| 1182 | * @rate: the rate which is to be rounded |
| 1183 | * |
| 1184 | * Takes in a rate as input and rounds it to a rate that the clk can actually |
| 1185 | * use which is then returned. If clk doesn't support round_rate operation |
| 1186 | * then the parent rate is returned. |
| 1187 | */ |
| 1188 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 1189 | { |
Stephen Boyd | fc4a05d | 2015-06-25 17:24:15 -0700 | [diff] [blame] | 1190 | struct clk_rate_request req; |
| 1191 | int ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1192 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1193 | if (!clk) |
| 1194 | return 0; |
| 1195 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1196 | clk_prepare_lock(); |
Stephen Boyd | fc4a05d | 2015-06-25 17:24:15 -0700 | [diff] [blame] | 1197 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 1198 | if (clk->exclusive_count) |
| 1199 | clk_core_rate_unprotect(clk->core); |
| 1200 | |
Stephen Boyd | fc4a05d | 2015-06-25 17:24:15 -0700 | [diff] [blame] | 1201 | clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); |
| 1202 | req.rate = rate; |
| 1203 | |
| 1204 | ret = clk_core_round_rate_nolock(clk->core, &req); |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 1205 | |
| 1206 | if (clk->exclusive_count) |
| 1207 | clk_core_rate_protect(clk->core); |
| 1208 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1209 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1210 | |
Stephen Boyd | fc4a05d | 2015-06-25 17:24:15 -0700 | [diff] [blame] | 1211 | if (ret) |
| 1212 | return ret; |
| 1213 | |
| 1214 | return req.rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1215 | } |
| 1216 | EXPORT_SYMBOL_GPL(clk_round_rate); |
| 1217 | |
| 1218 | /** |
| 1219 | * __clk_notify - call clk notifier chain |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1220 | * @core: clk that is changing rate |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1221 | * @msg: clk notifier type (see include/linux/clk.h) |
| 1222 | * @old_rate: old clk rate |
| 1223 | * @new_rate: new clk rate |
| 1224 | * |
| 1225 | * Triggers a notifier call chain on the clk rate-change notification |
| 1226 | * for 'clk'. Passes a pointer to the struct clk and the previous |
| 1227 | * and current rates to the notifier callback. Intended to be called by |
| 1228 | * internal clock code only. Returns NOTIFY_DONE from the last driver |
| 1229 | * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if |
| 1230 | * a driver returns that. |
| 1231 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1232 | static int __clk_notify(struct clk_core *core, unsigned long msg, |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1233 | unsigned long old_rate, unsigned long new_rate) |
| 1234 | { |
| 1235 | struct clk_notifier *cn; |
| 1236 | struct clk_notifier_data cnd; |
| 1237 | int ret = NOTIFY_DONE; |
| 1238 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1239 | cnd.old_rate = old_rate; |
| 1240 | cnd.new_rate = new_rate; |
| 1241 | |
| 1242 | list_for_each_entry(cn, &clk_notifier_list, node) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1243 | if (cn->clk->core == core) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1244 | cnd.clk = cn->clk; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1245 | ret = srcu_notifier_call_chain(&cn->notifier_head, msg, |
| 1246 | &cnd); |
Peter De Schrijver | 17c34c5 | 2017-03-21 12:16:26 +0200 | [diff] [blame] | 1247 | if (ret & NOTIFY_STOP_MASK) |
| 1248 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1256 | * __clk_recalc_accuracies |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1257 | * @core: first clk in the subtree |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1258 | * |
| 1259 | * Walks the subtree of clks starting with clk and recalculates accuracies as |
| 1260 | * 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] | 1261 | * 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] | 1262 | * parent. |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1263 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1264 | static void __clk_recalc_accuracies(struct clk_core *core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1265 | { |
| 1266 | unsigned long parent_accuracy = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1267 | struct clk_core *child; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1268 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 1269 | lockdep_assert_held(&prepare_lock); |
| 1270 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1271 | if (core->parent) |
| 1272 | parent_accuracy = core->parent->accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1273 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1274 | if (core->ops->recalc_accuracy) |
| 1275 | core->accuracy = core->ops->recalc_accuracy(core->hw, |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1276 | parent_accuracy); |
| 1277 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1278 | core->accuracy = parent_accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1279 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1280 | hlist_for_each_entry(child, &core->children, child_node) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1281 | __clk_recalc_accuracies(child); |
| 1282 | } |
| 1283 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1284 | static long clk_core_get_accuracy(struct clk_core *core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1285 | { |
| 1286 | unsigned long accuracy; |
| 1287 | |
| 1288 | clk_prepare_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1289 | if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) |
| 1290 | __clk_recalc_accuracies(core); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1291 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1292 | accuracy = __clk_get_accuracy(core); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1293 | clk_prepare_unlock(); |
| 1294 | |
| 1295 | return accuracy; |
| 1296 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1297 | |
| 1298 | /** |
| 1299 | * clk_get_accuracy - return the accuracy of clk |
| 1300 | * @clk: the clk whose accuracy is being returned |
| 1301 | * |
| 1302 | * Simply returns the cached accuracy of the clk, unless |
| 1303 | * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be |
| 1304 | * issued. |
| 1305 | * If clk is NULL then returns 0. |
| 1306 | */ |
| 1307 | long clk_get_accuracy(struct clk *clk) |
| 1308 | { |
| 1309 | if (!clk) |
| 1310 | return 0; |
| 1311 | |
| 1312 | return clk_core_get_accuracy(clk->core); |
| 1313 | } |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1314 | EXPORT_SYMBOL_GPL(clk_get_accuracy); |
| 1315 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1316 | static unsigned long clk_recalc(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1317 | unsigned long parent_rate) |
Stephen Boyd | 8f2c2db | 2014-03-26 16:06:36 -0700 | [diff] [blame] | 1318 | { |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1319 | unsigned long rate = parent_rate; |
| 1320 | |
| 1321 | if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { |
| 1322 | rate = core->ops->recalc_rate(core->hw, parent_rate); |
| 1323 | clk_pm_runtime_put(core); |
| 1324 | } |
| 1325 | return rate; |
Stephen Boyd | 8f2c2db | 2014-03-26 16:06:36 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1328 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1329 | * __clk_recalc_rates |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1330 | * @core: first clk in the subtree |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1331 | * @msg: notification type (see include/linux/clk.h) |
| 1332 | * |
| 1333 | * Walks the subtree of clks starting with clk and recalculates rates as it |
| 1334 | * 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] | 1335 | * 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] | 1336 | * |
| 1337 | * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, |
| 1338 | * if necessary. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1339 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1340 | static void __clk_recalc_rates(struct clk_core *core, unsigned long msg) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1341 | { |
| 1342 | unsigned long old_rate; |
| 1343 | unsigned long parent_rate = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1344 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1345 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 1346 | lockdep_assert_held(&prepare_lock); |
| 1347 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1348 | old_rate = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1349 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1350 | if (core->parent) |
| 1351 | parent_rate = core->parent->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1352 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1353 | core->rate = clk_recalc(core, parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1354 | |
| 1355 | /* |
| 1356 | * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE |
| 1357 | * & ABORT_RATE_CHANGE notifiers |
| 1358 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1359 | if (core->notifier_count && msg) |
| 1360 | __clk_notify(core, msg, old_rate, core->rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1361 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1362 | hlist_for_each_entry(child, &core->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1363 | __clk_recalc_rates(child, msg); |
| 1364 | } |
| 1365 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1366 | static unsigned long clk_core_get_rate(struct clk_core *core) |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1367 | { |
| 1368 | unsigned long rate; |
| 1369 | |
| 1370 | clk_prepare_lock(); |
| 1371 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1372 | if (core && (core->flags & CLK_GET_RATE_NOCACHE)) |
| 1373 | __clk_recalc_rates(core, 0); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1374 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1375 | rate = clk_core_get_rate_nolock(core); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1376 | clk_prepare_unlock(); |
| 1377 | |
| 1378 | return rate; |
| 1379 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1380 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1381 | /** |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1382 | * clk_get_rate - return the rate of clk |
| 1383 | * @clk: the clk whose rate is being returned |
| 1384 | * |
| 1385 | * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag |
| 1386 | * is set, which means a recalc_rate will be issued. |
| 1387 | * If clk is NULL then returns 0. |
| 1388 | */ |
| 1389 | unsigned long clk_get_rate(struct clk *clk) |
| 1390 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1391 | if (!clk) |
| 1392 | return 0; |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1393 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1394 | return clk_core_get_rate(clk->core); |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1395 | } |
| 1396 | EXPORT_SYMBOL_GPL(clk_get_rate); |
| 1397 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1398 | static int clk_fetch_parent_index(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1399 | struct clk_core *parent) |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1400 | { |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1401 | int i; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1402 | |
Masahiro Yamada | 508f884 | 2015-12-28 19:23:08 +0900 | [diff] [blame] | 1403 | if (!parent) |
| 1404 | return -EINVAL; |
| 1405 | |
Masahiro Yamada | 470b5e2 | 2015-12-28 19:23:09 +0900 | [diff] [blame] | 1406 | for (i = 0; i < core->num_parents; i++) |
| 1407 | if (clk_core_get_parent_by_index(core, i) == parent) |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1408 | return i; |
Tomasz Figa | da0f0b2 | 2013-09-29 02:37:16 +0200 | [diff] [blame] | 1409 | |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1410 | return -EINVAL; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1413 | /* |
| 1414 | * Update the orphan status of @core and all its children. |
| 1415 | */ |
| 1416 | static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan) |
| 1417 | { |
| 1418 | struct clk_core *child; |
| 1419 | |
| 1420 | core->orphan = is_orphan; |
| 1421 | |
| 1422 | hlist_for_each_entry(child, &core->children, child_node) |
| 1423 | clk_core_update_orphan_status(child, is_orphan); |
| 1424 | } |
| 1425 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1426 | 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] | 1427 | { |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1428 | bool was_orphan = core->orphan; |
| 1429 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1430 | hlist_del(&core->child_node); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1431 | |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1432 | if (new_parent) { |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1433 | bool becomes_orphan = new_parent->orphan; |
| 1434 | |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1435 | /* avoid duplicate POST_RATE_CHANGE notifications */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1436 | if (new_parent->new_child == core) |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1437 | new_parent->new_child = NULL; |
| 1438 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1439 | hlist_add_head(&core->child_node, &new_parent->children); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1440 | |
| 1441 | if (was_orphan != becomes_orphan) |
| 1442 | clk_core_update_orphan_status(core, becomes_orphan); |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1443 | } else { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1444 | hlist_add_head(&core->child_node, &clk_orphan_list); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1445 | if (!was_orphan) |
| 1446 | clk_core_update_orphan_status(core, true); |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1447 | } |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1448 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1449 | core->parent = new_parent; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1452 | static struct clk_core *__clk_set_parent_before(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1453 | struct clk_core *parent) |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1454 | { |
| 1455 | unsigned long flags; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1456 | struct clk_core *old_parent = core->parent; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1457 | |
| 1458 | /* |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1459 | * 1. enable parents for CLK_OPS_PARENT_ENABLE clock |
| 1460 | * |
| 1461 | * 2. Migrate prepare state between parents and prevent race with |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1462 | * clk_enable(). |
| 1463 | * |
| 1464 | * If the clock is not prepared, then a race with |
| 1465 | * clk_enable/disable() is impossible since we already have the |
| 1466 | * prepare lock (future calls to clk_enable() need to be preceded by |
| 1467 | * a clk_prepare()). |
| 1468 | * |
| 1469 | * If the clock is prepared, migrate the prepared state to the new |
| 1470 | * parent and also protect against a race with clk_enable() by |
| 1471 | * forcing the clock and the new parent on. This ensures that all |
| 1472 | * future calls to clk_enable() are practically NOPs with respect to |
| 1473 | * hardware and software states. |
| 1474 | * |
| 1475 | * See also: Comment for clk_set_parent() below. |
| 1476 | */ |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1477 | |
| 1478 | /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */ |
| 1479 | if (core->flags & CLK_OPS_PARENT_ENABLE) { |
| 1480 | clk_core_prepare_enable(old_parent); |
| 1481 | clk_core_prepare_enable(parent); |
| 1482 | } |
| 1483 | |
| 1484 | /* migrate prepare count if > 0 */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1485 | if (core->prepare_count) { |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1486 | clk_core_prepare_enable(parent); |
| 1487 | clk_core_enable_lock(core); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* update the clk tree topology */ |
| 1491 | flags = clk_enable_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1492 | clk_reparent(core, parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1493 | clk_enable_unlock(flags); |
| 1494 | |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1495 | return old_parent; |
| 1496 | } |
| 1497 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1498 | static void __clk_set_parent_after(struct clk_core *core, |
| 1499 | struct clk_core *parent, |
| 1500 | struct clk_core *old_parent) |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1501 | { |
| 1502 | /* |
| 1503 | * Finish the migration of prepare state and undo the changes done |
| 1504 | * for preventing a race with clk_enable(). |
| 1505 | */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1506 | if (core->prepare_count) { |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1507 | clk_core_disable_lock(core); |
| 1508 | clk_core_disable_unprepare(old_parent); |
| 1509 | } |
| 1510 | |
| 1511 | /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ |
| 1512 | if (core->flags & CLK_OPS_PARENT_ENABLE) { |
| 1513 | clk_core_disable_unprepare(parent); |
| 1514 | clk_core_disable_unprepare(old_parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1515 | } |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1518 | 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] | 1519 | u8 p_index) |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1520 | { |
| 1521 | unsigned long flags; |
| 1522 | int ret = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1523 | struct clk_core *old_parent; |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1524 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1525 | old_parent = __clk_set_parent_before(core, parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1526 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1527 | trace_clk_set_parent(core, parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1528 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1529 | /* change clock input source */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1530 | if (parent && core->ops->set_parent) |
| 1531 | ret = core->ops->set_parent(core->hw, p_index); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1532 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1533 | trace_clk_set_parent_complete(core, parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1534 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1535 | if (ret) { |
| 1536 | flags = clk_enable_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1537 | clk_reparent(core, old_parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1538 | clk_enable_unlock(flags); |
Dong Aisheng | c660b2eb | 2015-07-28 21:19:41 +0800 | [diff] [blame] | 1539 | __clk_set_parent_after(core, old_parent, parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1540 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1541 | return ret; |
| 1542 | } |
| 1543 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1544 | __clk_set_parent_after(core, parent, old_parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1545 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1549 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1550 | * __clk_speculate_rates |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1551 | * @core: first clk in the subtree |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1552 | * @parent_rate: the "future" rate of clk's parent |
| 1553 | * |
| 1554 | * Walks the subtree of clks starting with clk, speculating rates as it |
| 1555 | * goes and firing off PRE_RATE_CHANGE notifications as necessary. |
| 1556 | * |
| 1557 | * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending |
| 1558 | * pre-rate change notifications and returns early if no clks in the |
| 1559 | * subtree have subscribed to the notifications. Note that if a clk does not |
| 1560 | * 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] | 1561 | * take on the rate of its parent. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1562 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1563 | static int __clk_speculate_rates(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1564 | unsigned long parent_rate) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1565 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1566 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1567 | unsigned long new_rate; |
| 1568 | int ret = NOTIFY_DONE; |
| 1569 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 1570 | lockdep_assert_held(&prepare_lock); |
| 1571 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1572 | new_rate = clk_recalc(core, parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1573 | |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1574 | /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1575 | if (core->notifier_count) |
| 1576 | ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1577 | |
Mike Turquette | 86bcfa2 | 2014-02-24 16:08:41 -0800 | [diff] [blame] | 1578 | if (ret & NOTIFY_STOP_MASK) { |
| 1579 | 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] | 1580 | __func__, core->name, ret); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1581 | goto out; |
Mike Turquette | 86bcfa2 | 2014-02-24 16:08:41 -0800 | [diff] [blame] | 1582 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1583 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1584 | hlist_for_each_entry(child, &core->children, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1585 | ret = __clk_speculate_rates(child, new_rate); |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1586 | if (ret & NOTIFY_STOP_MASK) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | out: |
| 1591 | return ret; |
| 1592 | } |
| 1593 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1594 | 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] | 1595 | struct clk_core *new_parent, u8 p_index) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1596 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1597 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1598 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1599 | core->new_rate = new_rate; |
| 1600 | core->new_parent = new_parent; |
| 1601 | core->new_parent_index = p_index; |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1602 | /* include clk in new parent's PRE_RATE_CHANGE notifications */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1603 | core->new_child = NULL; |
| 1604 | if (new_parent && new_parent != core->parent) |
| 1605 | new_parent->new_child = core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1606 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1607 | hlist_for_each_entry(child, &core->children, child_node) { |
Stephen Boyd | 8f2c2db | 2014-03-26 16:06:36 -0700 | [diff] [blame] | 1608 | child->new_rate = clk_recalc(child, new_rate); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1609 | clk_calc_subtree(child, child->new_rate, NULL, 0); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * calculate the new rates returning the topmost clock that has to be |
| 1615 | * changed. |
| 1616 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1617 | static struct clk_core *clk_calc_new_rates(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1618 | unsigned long rate) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1619 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1620 | struct clk_core *top = core; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1621 | struct clk_core *old_parent, *parent; |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 1622 | unsigned long best_parent_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1623 | unsigned long new_rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1624 | unsigned long min_rate; |
| 1625 | unsigned long max_rate; |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1626 | int p_index = 0; |
Boris Brezillon | 03bc10a | 2015-03-29 03:48:48 +0200 | [diff] [blame] | 1627 | long ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1628 | |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1629 | /* sanity */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1630 | if (IS_ERR_OR_NULL(core)) |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1631 | return NULL; |
| 1632 | |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1633 | /* save parent rate, if it exists */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1634 | parent = old_parent = core->parent; |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1635 | if (parent) |
| 1636 | best_parent_rate = parent->rate; |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1637 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1638 | clk_core_get_boundaries(core, &min_rate, &max_rate); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1639 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1640 | /* find the closest rate and parent clk/rate */ |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1641 | if (clk_core_can_round(core)) { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1642 | struct clk_rate_request req; |
| 1643 | |
| 1644 | req.rate = rate; |
| 1645 | req.min_rate = min_rate; |
| 1646 | req.max_rate = max_rate; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1647 | |
Jerome Brunet | 0f6cc2b | 2017-12-01 22:51:54 +0100 | [diff] [blame] | 1648 | clk_core_init_rate_req(core, &req); |
| 1649 | |
| 1650 | ret = clk_core_determine_round_nolock(core, &req); |
Boris Brezillon | 03bc10a | 2015-03-29 03:48:48 +0200 | [diff] [blame] | 1651 | if (ret < 0) |
| 1652 | return NULL; |
| 1653 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1654 | best_parent_rate = req.best_parent_rate; |
| 1655 | new_rate = req.rate; |
| 1656 | parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; |
Boris Brezillon | 03bc10a | 2015-03-29 03:48:48 +0200 | [diff] [blame] | 1657 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1658 | if (new_rate < min_rate || new_rate > max_rate) |
| 1659 | return NULL; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1660 | } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1661 | /* pass-through clock without adjustable parent */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1662 | core->new_rate = core->rate; |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1663 | return NULL; |
| 1664 | } else { |
| 1665 | /* pass-through clock with adjustable parent */ |
| 1666 | top = clk_calc_new_rates(parent, rate); |
| 1667 | new_rate = parent->new_rate; |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1668 | goto out; |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1671 | /* some clocks must be gated to change parent */ |
| 1672 | if (parent != old_parent && |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1673 | (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1674 | pr_debug("%s: %s not gated but wants to reparent\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1675 | __func__, core->name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1676 | return NULL; |
| 1677 | } |
| 1678 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1679 | /* try finding the new parent index */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1680 | if (parent && core->num_parents > 1) { |
| 1681 | p_index = clk_fetch_parent_index(core, parent); |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1682 | if (p_index < 0) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1683 | 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] | 1684 | __func__, parent->name, core->name); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1685 | return NULL; |
| 1686 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1689 | if ((core->flags & CLK_SET_RATE_PARENT) && parent && |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1690 | best_parent_rate != parent->rate) |
| 1691 | top = clk_calc_new_rates(parent, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1692 | |
| 1693 | out: |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1694 | clk_calc_subtree(core, new_rate, parent, p_index); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1695 | |
| 1696 | return top; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | * Notify about rate changes in a subtree. Always walk down the whole tree |
| 1701 | * so that in case of an error we can walk down the whole tree again and |
| 1702 | * abort the change. |
| 1703 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1704 | static struct clk_core *clk_propagate_rate_change(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1705 | unsigned long event) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1706 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1707 | struct clk_core *child, *tmp_clk, *fail_clk = NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1708 | int ret = NOTIFY_DONE; |
| 1709 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1710 | if (core->rate == core->new_rate) |
Sachin Kamat | 5fda685 | 2013-03-13 15:17:49 +0530 | [diff] [blame] | 1711 | return NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1712 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1713 | if (core->notifier_count) { |
| 1714 | ret = __clk_notify(core, event, core->rate, core->new_rate); |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1715 | if (ret & NOTIFY_STOP_MASK) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1716 | fail_clk = core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1719 | hlist_for_each_entry(child, &core->children, child_node) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1720 | /* Skip children who will be reparented to another clock */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1721 | if (child->new_parent && child->new_parent != core) |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1722 | continue; |
| 1723 | tmp_clk = clk_propagate_rate_change(child, event); |
| 1724 | if (tmp_clk) |
| 1725 | fail_clk = tmp_clk; |
| 1726 | } |
| 1727 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1728 | /* handle the new child who might not be in core->children yet */ |
| 1729 | if (core->new_child) { |
| 1730 | tmp_clk = clk_propagate_rate_change(core->new_child, event); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1731 | if (tmp_clk) |
| 1732 | fail_clk = tmp_clk; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | return fail_clk; |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * walk down a subtree and set the new rates notifying the rate |
| 1740 | * change on the way |
| 1741 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1742 | static void clk_change_rate(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1743 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1744 | struct clk_core *child; |
Tero Kristo | 067bb17 | 2014-08-21 16:47:45 +0300 | [diff] [blame] | 1745 | struct hlist_node *tmp; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1746 | unsigned long old_rate; |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1747 | unsigned long best_parent_rate = 0; |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1748 | bool skip_set_rate = false; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1749 | struct clk_core *old_parent; |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1750 | struct clk_core *parent = NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1751 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1752 | old_rate = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1753 | |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1754 | if (core->new_parent) { |
| 1755 | parent = core->new_parent; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1756 | best_parent_rate = core->new_parent->rate; |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1757 | } else if (core->parent) { |
| 1758 | parent = core->parent; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1759 | best_parent_rate = core->parent->rate; |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1760 | } |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1761 | |
Marek Szyprowski | 588fb54 | 2017-11-30 13:14:51 +0100 | [diff] [blame] | 1762 | if (clk_pm_runtime_get(core)) |
| 1763 | return; |
| 1764 | |
Heiko Stuebner | 2eb8c71 | 2015-12-22 22:27:58 +0100 | [diff] [blame] | 1765 | if (core->flags & CLK_SET_RATE_UNGATE) { |
| 1766 | unsigned long flags; |
| 1767 | |
| 1768 | clk_core_prepare(core); |
| 1769 | flags = clk_enable_lock(); |
| 1770 | clk_core_enable(core); |
| 1771 | clk_enable_unlock(flags); |
| 1772 | } |
| 1773 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1774 | if (core->new_parent && core->new_parent != core->parent) { |
| 1775 | old_parent = __clk_set_parent_before(core, core->new_parent); |
| 1776 | trace_clk_set_parent(core, core->new_parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1777 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1778 | if (core->ops->set_rate_and_parent) { |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1779 | skip_set_rate = true; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1780 | core->ops->set_rate_and_parent(core->hw, core->new_rate, |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1781 | best_parent_rate, |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1782 | core->new_parent_index); |
| 1783 | } else if (core->ops->set_parent) { |
| 1784 | core->ops->set_parent(core->hw, core->new_parent_index); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1785 | } |
| 1786 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1787 | trace_clk_set_parent_complete(core, core->new_parent); |
| 1788 | __clk_set_parent_after(core, core->new_parent, old_parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1791 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
| 1792 | clk_core_prepare_enable(parent); |
| 1793 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1794 | trace_clk_set_rate(core, core->new_rate); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1795 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1796 | if (!skip_set_rate && core->ops->set_rate) |
| 1797 | core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1798 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1799 | trace_clk_set_rate_complete(core, core->new_rate); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1800 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1801 | core->rate = clk_recalc(core, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1802 | |
Heiko Stuebner | 2eb8c71 | 2015-12-22 22:27:58 +0100 | [diff] [blame] | 1803 | if (core->flags & CLK_SET_RATE_UNGATE) { |
| 1804 | unsigned long flags; |
| 1805 | |
| 1806 | flags = clk_enable_lock(); |
| 1807 | clk_core_disable(core); |
| 1808 | clk_enable_unlock(flags); |
| 1809 | clk_core_unprepare(core); |
| 1810 | } |
| 1811 | |
Dong Aisheng | fc8726a | 2016-06-30 17:31:14 +0800 | [diff] [blame] | 1812 | if (core->flags & CLK_OPS_PARENT_ENABLE) |
| 1813 | clk_core_disable_unprepare(parent); |
| 1814 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1815 | if (core->notifier_count && old_rate != core->rate) |
| 1816 | __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1817 | |
Michael Turquette | 85e88fa | 2015-06-20 12:18:03 -0700 | [diff] [blame] | 1818 | if (core->flags & CLK_RECALC_NEW_RATES) |
| 1819 | (void)clk_calc_new_rates(core, core->new_rate); |
Bartlomiej Zolnierkiewicz | d8d9198 | 2015-04-03 18:43:44 +0200 | [diff] [blame] | 1820 | |
Tero Kristo | 067bb17 | 2014-08-21 16:47:45 +0300 | [diff] [blame] | 1821 | /* |
| 1822 | * Use safe iteration, as change_rate can actually swap parents |
| 1823 | * for certain clock types. |
| 1824 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1825 | hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1826 | /* Skip children who will be reparented to another clock */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1827 | if (child->new_parent && child->new_parent != core) |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1828 | continue; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1829 | clk_change_rate(child); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1830 | } |
| 1831 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1832 | /* handle the new child who might not be in core->children yet */ |
| 1833 | if (core->new_child) |
| 1834 | clk_change_rate(core->new_child); |
Marek Szyprowski | 588fb54 | 2017-11-30 13:14:51 +0100 | [diff] [blame] | 1835 | |
| 1836 | clk_pm_runtime_put(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1839 | static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core, |
| 1840 | unsigned long req_rate) |
| 1841 | { |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 1842 | int ret, cnt; |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1843 | struct clk_rate_request req; |
| 1844 | |
| 1845 | lockdep_assert_held(&prepare_lock); |
| 1846 | |
| 1847 | if (!core) |
| 1848 | return 0; |
| 1849 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 1850 | /* simulate what the rate would be if it could be freely set */ |
| 1851 | cnt = clk_core_rate_nuke_protect(core); |
| 1852 | if (cnt < 0) |
| 1853 | return cnt; |
| 1854 | |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1855 | clk_core_get_boundaries(core, &req.min_rate, &req.max_rate); |
| 1856 | req.rate = req_rate; |
| 1857 | |
| 1858 | ret = clk_core_round_rate_nolock(core, &req); |
| 1859 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 1860 | /* restore the protection */ |
| 1861 | clk_core_rate_restore_protect(core, cnt); |
| 1862 | |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1863 | return ret ? 0 : req.rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1866 | static int clk_core_set_rate_nolock(struct clk_core *core, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1867 | unsigned long req_rate) |
| 1868 | { |
| 1869 | struct clk_core *top, *fail_clk; |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1870 | unsigned long rate; |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1871 | int ret = 0; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1872 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1873 | if (!core) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1874 | return 0; |
| 1875 | |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1876 | rate = clk_core_req_round_rate_nolock(core, req_rate); |
| 1877 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1878 | /* bail early if nothing to do */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1879 | if (rate == clk_core_get_rate_nolock(core)) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1880 | return 0; |
| 1881 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 1882 | /* fail on a direct rate set of a protected provider */ |
| 1883 | if (clk_core_rate_is_protected(core)) |
| 1884 | return -EBUSY; |
| 1885 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1886 | if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1887 | return -EBUSY; |
| 1888 | |
| 1889 | /* calculate new rates and get the topmost changed clock */ |
Jerome Brunet | ca5e089 | 2017-12-01 22:51:55 +0100 | [diff] [blame] | 1890 | top = clk_calc_new_rates(core, req_rate); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1891 | if (!top) |
| 1892 | return -EINVAL; |
| 1893 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1894 | ret = clk_pm_runtime_get(core); |
| 1895 | if (ret) |
| 1896 | return ret; |
| 1897 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1898 | /* notify that we are about to change rates */ |
| 1899 | fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); |
| 1900 | if (fail_clk) { |
| 1901 | pr_debug("%s: failed to set %s rate\n", __func__, |
| 1902 | fail_clk->name); |
| 1903 | clk_propagate_rate_change(top, ABORT_RATE_CHANGE); |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1904 | ret = -EBUSY; |
| 1905 | goto err; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /* change the rates */ |
| 1909 | clk_change_rate(top); |
| 1910 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1911 | core->req_rate = req_rate; |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1912 | err: |
| 1913 | clk_pm_runtime_put(core); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1914 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 1915 | return ret; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1916 | } |
| 1917 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1918 | /** |
| 1919 | * clk_set_rate - specify a new rate for clk |
| 1920 | * @clk: the clk whose rate is being changed |
| 1921 | * @rate: the new rate for clk |
| 1922 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1923 | * 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] | 1924 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1925 | * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to |
| 1926 | * propagate up to clk's parent; whether or not this happens depends on the |
| 1927 | * outcome of clk's .round_rate implementation. If *parent_rate is unchanged |
| 1928 | * after calling .round_rate then upstream parent propagation is ignored. If |
| 1929 | * *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] | 1930 | * 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] | 1931 | * until either a clk does not support the CLK_SET_RATE_PARENT flag or |
| 1932 | * .round_rate stops requesting changes to clk's parent_rate. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1933 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1934 | * Rate changes are accomplished via tree traversal that also recalculates the |
| 1935 | * rates for the clocks and fires off POST_RATE_CHANGE notifiers. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1936 | * |
| 1937 | * Returns 0 on success, -EERROR otherwise. |
| 1938 | */ |
| 1939 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 1940 | { |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1941 | int ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1942 | |
Mike Turquette | 89ac8d7 | 2013-08-21 23:58:09 -0700 | [diff] [blame] | 1943 | if (!clk) |
| 1944 | return 0; |
| 1945 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1946 | /* prevent racing with updates to the clock topology */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1947 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1948 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 1949 | if (clk->exclusive_count) |
| 1950 | clk_core_rate_unprotect(clk->core); |
| 1951 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1952 | ret = clk_core_set_rate_nolock(clk->core, rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1953 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 1954 | if (clk->exclusive_count) |
| 1955 | clk_core_rate_protect(clk->core); |
| 1956 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1957 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1958 | |
| 1959 | return ret; |
| 1960 | } |
| 1961 | EXPORT_SYMBOL_GPL(clk_set_rate); |
| 1962 | |
| 1963 | /** |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 1964 | * clk_set_rate_exclusive - specify a new rate get exclusive control |
| 1965 | * @clk: the clk whose rate is being changed |
| 1966 | * @rate: the new rate for clk |
| 1967 | * |
| 1968 | * This is a combination of clk_set_rate() and clk_rate_exclusive_get() |
| 1969 | * within a critical section |
| 1970 | * |
| 1971 | * This can be used initially to ensure that at least 1 consumer is |
| 1972 | * statisfied when several consumers are competing for exclusivity over the |
| 1973 | * same clock provider. |
| 1974 | * |
| 1975 | * The exclusivity is not applied if setting the rate failed. |
| 1976 | * |
| 1977 | * Calls to clk_rate_exclusive_get() should be balanced with calls to |
| 1978 | * clk_rate_exclusive_put(). |
| 1979 | * |
| 1980 | * Returns 0 on success, -EERROR otherwise. |
| 1981 | */ |
| 1982 | int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) |
| 1983 | { |
| 1984 | int ret; |
| 1985 | |
| 1986 | if (!clk) |
| 1987 | return 0; |
| 1988 | |
| 1989 | /* prevent racing with updates to the clock topology */ |
| 1990 | clk_prepare_lock(); |
| 1991 | |
| 1992 | /* |
| 1993 | * The temporary protection removal is not here, on purpose |
| 1994 | * This function is meant to be used instead of clk_rate_protect, |
| 1995 | * so before the consumer code path protect the clock provider |
| 1996 | */ |
| 1997 | |
| 1998 | ret = clk_core_set_rate_nolock(clk->core, rate); |
| 1999 | if (!ret) { |
| 2000 | clk_core_rate_protect(clk->core); |
| 2001 | clk->exclusive_count++; |
| 2002 | } |
| 2003 | |
| 2004 | clk_prepare_unlock(); |
| 2005 | |
| 2006 | return ret; |
| 2007 | } |
| 2008 | EXPORT_SYMBOL_GPL(clk_set_rate_exclusive); |
| 2009 | |
| 2010 | /** |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2011 | * clk_set_rate_range - set a rate range for a clock source |
| 2012 | * @clk: clock source |
| 2013 | * @min: desired minimum clock rate in Hz, inclusive |
| 2014 | * @max: desired maximum clock rate in Hz, inclusive |
| 2015 | * |
| 2016 | * Returns success (0) or negative errno. |
| 2017 | */ |
| 2018 | int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) |
| 2019 | { |
| 2020 | int ret = 0; |
Jerome Brunet | 6562fbc | 2017-12-01 22:52:00 +0100 | [diff] [blame] | 2021 | unsigned long old_min, old_max, rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2022 | |
| 2023 | if (!clk) |
| 2024 | return 0; |
| 2025 | |
| 2026 | if (min > max) { |
| 2027 | pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", |
| 2028 | __func__, clk->core->name, clk->dev_id, clk->con_id, |
| 2029 | min, max); |
| 2030 | return -EINVAL; |
| 2031 | } |
| 2032 | |
| 2033 | clk_prepare_lock(); |
| 2034 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 2035 | if (clk->exclusive_count) |
| 2036 | clk_core_rate_unprotect(clk->core); |
| 2037 | |
Jerome Brunet | 6562fbc | 2017-12-01 22:52:00 +0100 | [diff] [blame] | 2038 | /* Save the current values in case we need to rollback the change */ |
| 2039 | old_min = clk->min_rate; |
| 2040 | old_max = clk->max_rate; |
| 2041 | clk->min_rate = min; |
| 2042 | clk->max_rate = max; |
| 2043 | |
| 2044 | rate = clk_core_get_rate_nolock(clk->core); |
| 2045 | if (rate < min || rate > max) { |
| 2046 | /* |
| 2047 | * FIXME: |
| 2048 | * We are in bit of trouble here, current rate is outside the |
| 2049 | * the requested range. We are going try to request appropriate |
| 2050 | * range boundary but there is a catch. It may fail for the |
| 2051 | * usual reason (clock broken, clock protected, etc) but also |
| 2052 | * because: |
| 2053 | * - round_rate() was not favorable and fell on the wrong |
| 2054 | * side of the boundary |
| 2055 | * - the determine_rate() callback does not really check for |
| 2056 | * this corner case when determining the rate |
| 2057 | */ |
| 2058 | |
| 2059 | if (rate < min) |
| 2060 | rate = min; |
| 2061 | else |
| 2062 | rate = max; |
| 2063 | |
| 2064 | ret = clk_core_set_rate_nolock(clk->core, rate); |
| 2065 | if (ret) { |
| 2066 | /* rollback the changes */ |
| 2067 | clk->min_rate = old_min; |
| 2068 | clk->max_rate = old_max; |
| 2069 | } |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 2072 | if (clk->exclusive_count) |
| 2073 | clk_core_rate_protect(clk->core); |
| 2074 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2075 | clk_prepare_unlock(); |
| 2076 | |
| 2077 | return ret; |
| 2078 | } |
| 2079 | EXPORT_SYMBOL_GPL(clk_set_rate_range); |
| 2080 | |
| 2081 | /** |
| 2082 | * clk_set_min_rate - set a minimum clock rate for a clock source |
| 2083 | * @clk: clock source |
| 2084 | * @rate: desired minimum clock rate in Hz, inclusive |
| 2085 | * |
| 2086 | * Returns success (0) or negative errno. |
| 2087 | */ |
| 2088 | int clk_set_min_rate(struct clk *clk, unsigned long rate) |
| 2089 | { |
| 2090 | if (!clk) |
| 2091 | return 0; |
| 2092 | |
| 2093 | return clk_set_rate_range(clk, rate, clk->max_rate); |
| 2094 | } |
| 2095 | EXPORT_SYMBOL_GPL(clk_set_min_rate); |
| 2096 | |
| 2097 | /** |
| 2098 | * clk_set_max_rate - set a maximum clock rate for a clock source |
| 2099 | * @clk: clock source |
| 2100 | * @rate: desired maximum clock rate in Hz, inclusive |
| 2101 | * |
| 2102 | * Returns success (0) or negative errno. |
| 2103 | */ |
| 2104 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 2105 | { |
| 2106 | if (!clk) |
| 2107 | return 0; |
| 2108 | |
| 2109 | return clk_set_rate_range(clk, clk->min_rate, rate); |
| 2110 | } |
| 2111 | EXPORT_SYMBOL_GPL(clk_set_max_rate); |
| 2112 | |
| 2113 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2114 | * clk_get_parent - return the parent of a clk |
| 2115 | * @clk: the clk whose parent gets returned |
| 2116 | * |
| 2117 | * Simply returns clk->parent. Returns NULL if clk is NULL. |
| 2118 | */ |
| 2119 | struct clk *clk_get_parent(struct clk *clk) |
| 2120 | { |
| 2121 | struct clk *parent; |
| 2122 | |
Stephen Boyd | fc4a05d | 2015-06-25 17:24:15 -0700 | [diff] [blame] | 2123 | if (!clk) |
| 2124 | return NULL; |
| 2125 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2126 | clk_prepare_lock(); |
Stephen Boyd | fc4a05d | 2015-06-25 17:24:15 -0700 | [diff] [blame] | 2127 | /* TODO: Create a per-user clk and change callers to call clk_put */ |
| 2128 | parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2129 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2130 | |
| 2131 | return parent; |
| 2132 | } |
| 2133 | EXPORT_SYMBOL_GPL(clk_get_parent); |
| 2134 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2135 | static struct clk_core *__clk_init_parent(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2136 | { |
Masahiro Yamada | 5146e0b | 2015-12-28 19:23:04 +0900 | [diff] [blame] | 2137 | u8 index = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2138 | |
Masahiro Yamada | 2430a94 | 2016-02-09 20:19:14 +0900 | [diff] [blame] | 2139 | if (core->num_parents > 1 && core->ops->get_parent) |
Masahiro Yamada | 5146e0b | 2015-12-28 19:23:04 +0900 | [diff] [blame] | 2140 | index = core->ops->get_parent(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2141 | |
Masahiro Yamada | 5146e0b | 2015-12-28 19:23:04 +0900 | [diff] [blame] | 2142 | return clk_core_get_parent_by_index(core, index); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2145 | static void clk_core_reparent(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2146 | struct clk_core *new_parent) |
Ulf Hansson | b33d212 | 2013-04-02 23:09:37 +0200 | [diff] [blame] | 2147 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2148 | clk_reparent(core, new_parent); |
| 2149 | __clk_recalc_accuracies(core); |
| 2150 | __clk_recalc_rates(core, POST_RATE_CHANGE); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Tomeu Vizoso | 42c8654 | 2015-03-11 11:34:25 +0100 | [diff] [blame] | 2153 | void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent) |
| 2154 | { |
| 2155 | if (!hw) |
| 2156 | return; |
| 2157 | |
| 2158 | clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); |
| 2159 | } |
| 2160 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2161 | /** |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2162 | * clk_has_parent - check if a clock is a possible parent for another |
| 2163 | * @clk: clock source |
| 2164 | * @parent: parent clock source |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2165 | * |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2166 | * This function can be used in drivers that need to check that a clock can be |
| 2167 | * the parent of another without actually changing the parent. |
Saravana Kannan | f8aa0bd | 2013-05-15 21:07:24 -0700 | [diff] [blame] | 2168 | * |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2169 | * Returns true if @parent is a possible parent for @clk, false otherwise. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2170 | */ |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2171 | bool clk_has_parent(struct clk *clk, struct clk *parent) |
| 2172 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2173 | struct clk_core *core, *parent_core; |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2174 | unsigned int i; |
| 2175 | |
| 2176 | /* NULL clocks should be nops, so return success if either is NULL. */ |
| 2177 | if (!clk || !parent) |
| 2178 | return true; |
| 2179 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2180 | core = clk->core; |
| 2181 | parent_core = parent->core; |
| 2182 | |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2183 | /* Optimize for the case where the parent is already the parent. */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2184 | if (core->parent == parent_core) |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2185 | return true; |
| 2186 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2187 | for (i = 0; i < core->num_parents; i++) |
| 2188 | if (strcmp(core->parent_names[i], parent_core->name) == 0) |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 2189 | return true; |
| 2190 | |
| 2191 | return false; |
| 2192 | } |
| 2193 | EXPORT_SYMBOL_GPL(clk_has_parent); |
| 2194 | |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2195 | static int clk_core_set_parent_nolock(struct clk_core *core, |
| 2196 | struct clk_core *parent) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2197 | { |
| 2198 | int ret = 0; |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 2199 | int p_index = 0; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2200 | unsigned long p_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2201 | |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2202 | lockdep_assert_held(&prepare_lock); |
| 2203 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2204 | if (!core) |
Mike Turquette | 89ac8d7 | 2013-08-21 23:58:09 -0700 | [diff] [blame] | 2205 | return 0; |
| 2206 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2207 | if (core->parent == parent) |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2208 | return 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2209 | |
Stephen Boyd | b61c43c | 2015-02-02 14:11:25 -0800 | [diff] [blame] | 2210 | /* verify ops for for multi-parent clks */ |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2211 | if (core->num_parents > 1 && !core->ops->set_parent) |
| 2212 | return -EPERM; |
Stephen Boyd | b61c43c | 2015-02-02 14:11:25 -0800 | [diff] [blame] | 2213 | |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2214 | /* check that we are allowed to re-parent if the clock is in use */ |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2215 | if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) |
| 2216 | return -EBUSY; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2217 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 2218 | if (clk_core_rate_is_protected(core)) |
| 2219 | return -EBUSY; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2220 | |
| 2221 | /* try finding the new parent index */ |
| 2222 | if (parent) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2223 | p_index = clk_fetch_parent_index(core, parent); |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 2224 | if (p_index < 0) { |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2225 | 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] | 2226 | __func__, parent->name, core->name); |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2227 | return p_index; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2228 | } |
Masahiro Yamada | e8f0e68 | 2015-12-28 19:23:10 +0900 | [diff] [blame] | 2229 | p_rate = parent->rate; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2230 | } |
| 2231 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2232 | ret = clk_pm_runtime_get(core); |
| 2233 | if (ret) |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2234 | return ret; |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2235 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2236 | /* propagate PRE_RATE_CHANGE notifications */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2237 | ret = __clk_speculate_rates(core, p_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2238 | |
| 2239 | /* abort if a driver objects */ |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 2240 | if (ret & NOTIFY_STOP_MASK) |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2241 | goto runtime_put; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2242 | |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 2243 | /* do the re-parent */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2244 | ret = __clk_set_parent(core, parent, p_index); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2245 | |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2246 | /* propagate rate an accuracy recalculation accordingly */ |
| 2247 | if (ret) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2248 | __clk_recalc_rates(core, ABORT_RATE_CHANGE); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2249 | } else { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2250 | __clk_recalc_rates(core, POST_RATE_CHANGE); |
| 2251 | __clk_recalc_accuracies(core); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2252 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2253 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2254 | runtime_put: |
| 2255 | clk_pm_runtime_put(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2256 | |
| 2257 | return ret; |
| 2258 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2259 | |
| 2260 | /** |
| 2261 | * clk_set_parent - switch the parent of a mux clk |
| 2262 | * @clk: the mux clk whose input we are switching |
| 2263 | * @parent: the new input to clk |
| 2264 | * |
| 2265 | * Re-parent clk to use parent as its new input source. If clk is in |
| 2266 | * prepared state, the clk will get enabled for the duration of this call. If |
| 2267 | * that's not acceptable for a specific clk (Eg: the consumer can't handle |
| 2268 | * that, the reparenting is glitchy in hardware, etc), use the |
| 2269 | * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared. |
| 2270 | * |
| 2271 | * After successfully changing clk's parent clk_set_parent will update the |
| 2272 | * clk topology, sysfs topology and propagate rate recalculation via |
| 2273 | * __clk_recalc_rates. |
| 2274 | * |
| 2275 | * Returns 0 on success, -EERROR otherwise. |
| 2276 | */ |
| 2277 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 2278 | { |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2279 | int ret; |
| 2280 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2281 | if (!clk) |
| 2282 | return 0; |
| 2283 | |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2284 | clk_prepare_lock(); |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 2285 | |
| 2286 | if (clk->exclusive_count) |
| 2287 | clk_core_rate_unprotect(clk->core); |
| 2288 | |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2289 | ret = clk_core_set_parent_nolock(clk->core, |
| 2290 | parent ? parent->core : NULL); |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 2291 | |
| 2292 | if (clk->exclusive_count) |
| 2293 | clk_core_rate_protect(clk->core); |
| 2294 | |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 2295 | clk_prepare_unlock(); |
| 2296 | |
| 2297 | return ret; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2298 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2299 | EXPORT_SYMBOL_GPL(clk_set_parent); |
| 2300 | |
Jerome Brunet | 9e4d04a | 2017-12-01 22:51:53 +0100 | [diff] [blame] | 2301 | static int clk_core_set_phase_nolock(struct clk_core *core, int degrees) |
| 2302 | { |
| 2303 | int ret = -EINVAL; |
| 2304 | |
| 2305 | lockdep_assert_held(&prepare_lock); |
| 2306 | |
| 2307 | if (!core) |
| 2308 | return 0; |
| 2309 | |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 2310 | if (clk_core_rate_is_protected(core)) |
| 2311 | return -EBUSY; |
| 2312 | |
Jerome Brunet | 9e4d04a | 2017-12-01 22:51:53 +0100 | [diff] [blame] | 2313 | trace_clk_set_phase(core, degrees); |
| 2314 | |
Shawn Lin | 7f95bee | 2018-03-08 14:49:41 +0800 | [diff] [blame] | 2315 | if (core->ops->set_phase) { |
Jerome Brunet | 9e4d04a | 2017-12-01 22:51:53 +0100 | [diff] [blame] | 2316 | ret = core->ops->set_phase(core->hw, degrees); |
Shawn Lin | 7f95bee | 2018-03-08 14:49:41 +0800 | [diff] [blame] | 2317 | if (!ret) |
| 2318 | core->phase = degrees; |
| 2319 | } |
Jerome Brunet | 9e4d04a | 2017-12-01 22:51:53 +0100 | [diff] [blame] | 2320 | |
| 2321 | trace_clk_set_phase_complete(core, degrees); |
| 2322 | |
| 2323 | return ret; |
| 2324 | } |
| 2325 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2326 | /** |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2327 | * clk_set_phase - adjust the phase shift of a clock signal |
| 2328 | * @clk: clock signal source |
| 2329 | * @degrees: number of degrees the signal is shifted |
| 2330 | * |
| 2331 | * Shifts the phase of a clock signal by the specified |
| 2332 | * degrees. Returns 0 on success, -EERROR otherwise. |
| 2333 | * |
| 2334 | * This function makes no distinction about the input or reference |
| 2335 | * signal that we adjust the clock signal phase against. For example |
| 2336 | * phase locked-loop clock signal generators we may shift phase with |
| 2337 | * respect to feedback clock signal input, but for other cases the |
| 2338 | * clock phase may be shifted with respect to some other, unspecified |
| 2339 | * signal. |
| 2340 | * |
| 2341 | * Additionally the concept of phase shift does not propagate through |
| 2342 | * the clock tree hierarchy, which sets it apart from clock rates and |
| 2343 | * clock accuracy. A parent clock phase attribute does not have an |
| 2344 | * impact on the phase attribute of a child clock. |
| 2345 | */ |
| 2346 | int clk_set_phase(struct clk *clk, int degrees) |
| 2347 | { |
Jerome Brunet | 9e4d04a | 2017-12-01 22:51:53 +0100 | [diff] [blame] | 2348 | int ret; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2349 | |
| 2350 | if (!clk) |
Stephen Boyd | 08b9575 | 2015-02-02 14:09:43 -0800 | [diff] [blame] | 2351 | return 0; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2352 | |
| 2353 | /* sanity check degrees */ |
| 2354 | degrees %= 360; |
| 2355 | if (degrees < 0) |
| 2356 | degrees += 360; |
| 2357 | |
| 2358 | clk_prepare_lock(); |
| 2359 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 2360 | if (clk->exclusive_count) |
| 2361 | clk_core_rate_unprotect(clk->core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 2362 | |
Jerome Brunet | 9e4d04a | 2017-12-01 22:51:53 +0100 | [diff] [blame] | 2363 | ret = clk_core_set_phase_nolock(clk->core, degrees); |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2364 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 2365 | if (clk->exclusive_count) |
| 2366 | clk_core_rate_protect(clk->core); |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2367 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2368 | clk_prepare_unlock(); |
| 2369 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2370 | return ret; |
| 2371 | } |
Maxime Ripard | 9767b04f | 2015-01-20 22:23:43 +0100 | [diff] [blame] | 2372 | EXPORT_SYMBOL_GPL(clk_set_phase); |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2373 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2374 | static int clk_core_get_phase(struct clk_core *core) |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2375 | { |
Stephen Boyd | 1f3e198 | 2015-04-30 14:21:56 -0700 | [diff] [blame] | 2376 | int ret; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2377 | |
| 2378 | clk_prepare_lock(); |
Shawn Lin | 1f9c63e | 2018-03-14 08:28:31 +0800 | [diff] [blame] | 2379 | /* Always try to update cached phase if possible */ |
| 2380 | if (core->ops->get_phase) |
| 2381 | core->phase = core->ops->get_phase(core->hw); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2382 | ret = core->phase; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2383 | clk_prepare_unlock(); |
| 2384 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 2385 | return ret; |
| 2386 | } |
| 2387 | |
| 2388 | /** |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2389 | * clk_get_phase - return the phase shift of a clock signal |
| 2390 | * @clk: clock signal source |
| 2391 | * |
| 2392 | * Returns the phase shift of a clock node in degrees, otherwise returns |
| 2393 | * -EERROR. |
| 2394 | */ |
| 2395 | int clk_get_phase(struct clk *clk) |
| 2396 | { |
| 2397 | if (!clk) |
| 2398 | return 0; |
| 2399 | |
| 2400 | return clk_core_get_phase(clk->core); |
| 2401 | } |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2402 | EXPORT_SYMBOL_GPL(clk_get_phase); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2403 | |
| 2404 | /** |
Michael Turquette | 3d3801e | 2015-02-25 09:11:01 -0800 | [diff] [blame] | 2405 | * clk_is_match - check if two clk's point to the same hardware clock |
| 2406 | * @p: clk compared against q |
| 2407 | * @q: clk compared against p |
| 2408 | * |
| 2409 | * Returns true if the two struct clk pointers both point to the same hardware |
| 2410 | * clock node. Put differently, returns true if struct clk *p and struct clk *q |
| 2411 | * share the same struct clk_core object. |
| 2412 | * |
| 2413 | * Returns false otherwise. Note that two NULL clks are treated as matching. |
| 2414 | */ |
| 2415 | bool clk_is_match(const struct clk *p, const struct clk *q) |
| 2416 | { |
| 2417 | /* trivial case: identical struct clk's or both NULL */ |
| 2418 | if (p == q) |
| 2419 | return true; |
| 2420 | |
Geert Uytterhoeven | 3fe003f | 2015-10-29 20:55:00 +0100 | [diff] [blame] | 2421 | /* true if clk->core pointers match. Avoid dereferencing garbage */ |
Michael Turquette | 3d3801e | 2015-02-25 09:11:01 -0800 | [diff] [blame] | 2422 | if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q)) |
| 2423 | if (p->core == q->core) |
| 2424 | return true; |
| 2425 | |
| 2426 | return false; |
| 2427 | } |
| 2428 | EXPORT_SYMBOL_GPL(clk_is_match); |
| 2429 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2430 | /*** debugfs support ***/ |
| 2431 | |
| 2432 | #ifdef CONFIG_DEBUG_FS |
| 2433 | #include <linux/debugfs.h> |
| 2434 | |
| 2435 | static struct dentry *rootdir; |
| 2436 | static int inited = 0; |
| 2437 | static DEFINE_MUTEX(clk_debug_lock); |
| 2438 | static HLIST_HEAD(clk_debug_list); |
| 2439 | |
| 2440 | static struct hlist_head *all_lists[] = { |
| 2441 | &clk_root_list, |
| 2442 | &clk_orphan_list, |
| 2443 | NULL, |
| 2444 | }; |
| 2445 | |
| 2446 | static struct hlist_head *orphan_list[] = { |
| 2447 | &clk_orphan_list, |
| 2448 | NULL, |
| 2449 | }; |
| 2450 | |
| 2451 | static void clk_summary_show_one(struct seq_file *s, struct clk_core *c, |
| 2452 | int level) |
| 2453 | { |
| 2454 | if (!c) |
| 2455 | return; |
| 2456 | |
Jerome Brunet | c5ce26e | 2017-12-01 22:51:57 +0100 | [diff] [blame] | 2457 | seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n", |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2458 | level * 3 + 1, "", |
| 2459 | 30 - level * 3, c->name, |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 2460 | c->enable_count, c->prepare_count, c->protect_count, |
| 2461 | clk_core_get_rate(c), clk_core_get_accuracy(c), |
| 2462 | clk_core_get_phase(c)); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c, |
| 2466 | int level) |
| 2467 | { |
| 2468 | struct clk_core *child; |
| 2469 | |
| 2470 | if (!c) |
| 2471 | return; |
| 2472 | |
| 2473 | clk_summary_show_one(s, c, level); |
| 2474 | |
| 2475 | hlist_for_each_entry(child, &c->children, child_node) |
| 2476 | clk_summary_show_subtree(s, child, level + 1); |
| 2477 | } |
| 2478 | |
| 2479 | static int clk_summary_show(struct seq_file *s, void *data) |
| 2480 | { |
| 2481 | struct clk_core *c; |
| 2482 | struct hlist_head **lists = (struct hlist_head **)s->private; |
| 2483 | |
Jerome Brunet | c5ce26e | 2017-12-01 22:51:57 +0100 | [diff] [blame] | 2484 | seq_puts(s, " enable prepare protect \n"); |
| 2485 | seq_puts(s, " clock count count count rate accuracy phase\n"); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2486 | seq_puts(s, "----------------------------------------------------------------------------------------\n"); |
| 2487 | |
| 2488 | clk_prepare_lock(); |
| 2489 | |
| 2490 | for (; *lists; lists++) |
| 2491 | hlist_for_each_entry(c, *lists, child_node) |
| 2492 | clk_summary_show_subtree(s, c, 0); |
| 2493 | |
| 2494 | clk_prepare_unlock(); |
| 2495 | |
| 2496 | return 0; |
| 2497 | } |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2498 | DEFINE_SHOW_ATTRIBUTE(clk_summary); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2499 | |
| 2500 | static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level) |
| 2501 | { |
| 2502 | if (!c) |
| 2503 | return; |
| 2504 | |
Stefan Wahren | 7cb8113 | 2015-04-29 16:36:43 +0000 | [diff] [blame] | 2505 | /* This should be JSON format, i.e. elements separated with a comma */ |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2506 | seq_printf(s, "\"%s\": { ", c->name); |
| 2507 | seq_printf(s, "\"enable_count\": %d,", c->enable_count); |
| 2508 | seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 2509 | seq_printf(s, "\"protect_count\": %d,", c->protect_count); |
Stefan Wahren | 7cb8113 | 2015-04-29 16:36:43 +0000 | [diff] [blame] | 2510 | seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c)); |
| 2511 | seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c)); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2512 | seq_printf(s, "\"phase\": %d", clk_core_get_phase(c)); |
| 2513 | } |
| 2514 | |
| 2515 | static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level) |
| 2516 | { |
| 2517 | struct clk_core *child; |
| 2518 | |
| 2519 | if (!c) |
| 2520 | return; |
| 2521 | |
| 2522 | clk_dump_one(s, c, level); |
| 2523 | |
| 2524 | hlist_for_each_entry(child, &c->children, child_node) { |
Markus Elfring | 4d32758 | 2017-04-20 08:45:43 +0200 | [diff] [blame] | 2525 | seq_putc(s, ','); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2526 | clk_dump_subtree(s, child, level + 1); |
| 2527 | } |
| 2528 | |
Markus Elfring | 4d32758 | 2017-04-20 08:45:43 +0200 | [diff] [blame] | 2529 | seq_putc(s, '}'); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2532 | static int clk_dump_show(struct seq_file *s, void *data) |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2533 | { |
| 2534 | struct clk_core *c; |
| 2535 | bool first_node = true; |
| 2536 | struct hlist_head **lists = (struct hlist_head **)s->private; |
| 2537 | |
Markus Elfring | 4d32758 | 2017-04-20 08:45:43 +0200 | [diff] [blame] | 2538 | seq_putc(s, '{'); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2539 | clk_prepare_lock(); |
| 2540 | |
| 2541 | for (; *lists; lists++) { |
| 2542 | hlist_for_each_entry(c, *lists, child_node) { |
| 2543 | if (!first_node) |
Markus Elfring | 4d32758 | 2017-04-20 08:45:43 +0200 | [diff] [blame] | 2544 | seq_putc(s, ','); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2545 | first_node = false; |
| 2546 | clk_dump_subtree(s, c, 0); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | clk_prepare_unlock(); |
| 2551 | |
Felipe Balbi | 70e9f4d | 2015-05-01 09:48:37 -0500 | [diff] [blame] | 2552 | seq_puts(s, "}\n"); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2553 | return 0; |
| 2554 | } |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2555 | DEFINE_SHOW_ATTRIBUTE(clk_dump); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2556 | |
Geert Uytterhoeven | a6059ab | 2018-01-03 12:06:16 +0100 | [diff] [blame] | 2557 | static const struct { |
| 2558 | unsigned long flag; |
| 2559 | const char *name; |
| 2560 | } clk_flags[] = { |
| 2561 | #define ENTRY(f) { f, __stringify(f) } |
| 2562 | ENTRY(CLK_SET_RATE_GATE), |
| 2563 | ENTRY(CLK_SET_PARENT_GATE), |
| 2564 | ENTRY(CLK_SET_RATE_PARENT), |
| 2565 | ENTRY(CLK_IGNORE_UNUSED), |
| 2566 | ENTRY(CLK_IS_BASIC), |
| 2567 | ENTRY(CLK_GET_RATE_NOCACHE), |
| 2568 | ENTRY(CLK_SET_RATE_NO_REPARENT), |
| 2569 | ENTRY(CLK_GET_ACCURACY_NOCACHE), |
| 2570 | ENTRY(CLK_RECALC_NEW_RATES), |
| 2571 | ENTRY(CLK_SET_RATE_UNGATE), |
| 2572 | ENTRY(CLK_IS_CRITICAL), |
| 2573 | ENTRY(CLK_OPS_PARENT_ENABLE), |
| 2574 | #undef ENTRY |
| 2575 | }; |
| 2576 | |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2577 | static int clk_flags_show(struct seq_file *s, void *data) |
Geert Uytterhoeven | a6059ab | 2018-01-03 12:06:16 +0100 | [diff] [blame] | 2578 | { |
| 2579 | struct clk_core *core = s->private; |
| 2580 | unsigned long flags = core->flags; |
| 2581 | unsigned int i; |
| 2582 | |
| 2583 | for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) { |
| 2584 | if (flags & clk_flags[i].flag) { |
| 2585 | seq_printf(s, "%s\n", clk_flags[i].name); |
| 2586 | flags &= ~clk_flags[i].flag; |
| 2587 | } |
| 2588 | } |
| 2589 | if (flags) { |
| 2590 | /* Unknown flags */ |
| 2591 | seq_printf(s, "0x%lx\n", flags); |
| 2592 | } |
| 2593 | |
| 2594 | return 0; |
| 2595 | } |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2596 | DEFINE_SHOW_ATTRIBUTE(clk_flags); |
Geert Uytterhoeven | a6059ab | 2018-01-03 12:06:16 +0100 | [diff] [blame] | 2597 | |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2598 | static int possible_parents_show(struct seq_file *s, void *data) |
Peter De Schrijver | 9203157 | 2017-03-21 15:20:31 +0200 | [diff] [blame] | 2599 | { |
| 2600 | struct clk_core *core = s->private; |
| 2601 | int i; |
| 2602 | |
| 2603 | for (i = 0; i < core->num_parents - 1; i++) |
| 2604 | seq_printf(s, "%s ", core->parent_names[i]); |
| 2605 | |
| 2606 | seq_printf(s, "%s\n", core->parent_names[i]); |
| 2607 | |
| 2608 | return 0; |
| 2609 | } |
Andy Shevchenko | fec0ef3 | 2018-02-14 17:48:00 +0200 | [diff] [blame] | 2610 | DEFINE_SHOW_ATTRIBUTE(possible_parents); |
Peter De Schrijver | 9203157 | 2017-03-21 15:20:31 +0200 | [diff] [blame] | 2611 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2612 | static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry) |
| 2613 | { |
| 2614 | struct dentry *d; |
| 2615 | int ret = -ENOMEM; |
| 2616 | |
| 2617 | if (!core || !pdentry) { |
| 2618 | ret = -EINVAL; |
| 2619 | goto out; |
| 2620 | } |
| 2621 | |
| 2622 | d = debugfs_create_dir(core->name, pdentry); |
| 2623 | if (!d) |
| 2624 | goto out; |
| 2625 | |
| 2626 | core->dentry = d; |
| 2627 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2628 | d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2629 | if (!d) |
| 2630 | goto err_out; |
| 2631 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2632 | d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry, |
| 2633 | &core->accuracy); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2634 | if (!d) |
| 2635 | goto err_out; |
| 2636 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2637 | d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2638 | if (!d) |
| 2639 | goto err_out; |
| 2640 | |
Geert Uytterhoeven | a6059ab | 2018-01-03 12:06:16 +0100 | [diff] [blame] | 2641 | d = debugfs_create_file("clk_flags", 0444, core->dentry, core, |
| 2642 | &clk_flags_fops); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2643 | if (!d) |
| 2644 | goto err_out; |
| 2645 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2646 | d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry, |
| 2647 | &core->prepare_count); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2648 | if (!d) |
| 2649 | goto err_out; |
| 2650 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2651 | d = debugfs_create_u32("clk_enable_count", 0444, core->dentry, |
| 2652 | &core->enable_count); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2653 | if (!d) |
| 2654 | goto err_out; |
| 2655 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2656 | d = debugfs_create_u32("clk_protect_count", 0444, core->dentry, |
| 2657 | &core->protect_count); |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 2658 | if (!d) |
| 2659 | goto err_out; |
| 2660 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2661 | d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry, |
| 2662 | &core->notifier_count); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2663 | if (!d) |
| 2664 | goto err_out; |
| 2665 | |
Peter De Schrijver | 9203157 | 2017-03-21 15:20:31 +0200 | [diff] [blame] | 2666 | if (core->num_parents > 1) { |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2667 | d = debugfs_create_file("clk_possible_parents", 0444, |
Peter De Schrijver | 9203157 | 2017-03-21 15:20:31 +0200 | [diff] [blame] | 2668 | core->dentry, core, &possible_parents_fops); |
| 2669 | if (!d) |
| 2670 | goto err_out; |
| 2671 | } |
| 2672 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2673 | if (core->ops->debug_init) { |
| 2674 | ret = core->ops->debug_init(core->hw, core->dentry); |
| 2675 | if (ret) |
| 2676 | goto err_out; |
| 2677 | } |
| 2678 | |
| 2679 | ret = 0; |
| 2680 | goto out; |
| 2681 | |
| 2682 | err_out: |
| 2683 | debugfs_remove_recursive(core->dentry); |
| 2684 | core->dentry = NULL; |
| 2685 | out: |
| 2686 | return ret; |
| 2687 | } |
| 2688 | |
| 2689 | /** |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2690 | * clk_debug_register - add a clk node to the debugfs clk directory |
| 2691 | * @core: the clk being added to the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2692 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2693 | * Dynamically adds a clk to the debugfs clk directory if debugfs has been |
| 2694 | * initialized. Otherwise it bails out early since the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2695 | * will be created lazily by clk_debug_init as part of a late_initcall. |
| 2696 | */ |
| 2697 | static int clk_debug_register(struct clk_core *core) |
| 2698 | { |
| 2699 | int ret = 0; |
| 2700 | |
| 2701 | mutex_lock(&clk_debug_lock); |
| 2702 | hlist_add_head(&core->debug_node, &clk_debug_list); |
Stephen Boyd | db3188fa | 2018-01-03 16:44:37 -0800 | [diff] [blame] | 2703 | if (inited) |
| 2704 | ret = clk_debug_create_one(core, rootdir); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2705 | mutex_unlock(&clk_debug_lock); |
| 2706 | |
| 2707 | return ret; |
| 2708 | } |
| 2709 | |
| 2710 | /** |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2711 | * clk_debug_unregister - remove a clk node from the debugfs clk directory |
| 2712 | * @core: the clk being removed from the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2713 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2714 | * Dynamically removes a clk and all its child nodes from the |
| 2715 | * debugfs clk directory if clk->dentry points to debugfs created by |
Stephen Boyd | 706d5c7 | 2016-02-22 15:43:41 -0800 | [diff] [blame] | 2716 | * clk_debug_register in __clk_core_init. |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2717 | */ |
| 2718 | static void clk_debug_unregister(struct clk_core *core) |
| 2719 | { |
| 2720 | mutex_lock(&clk_debug_lock); |
| 2721 | hlist_del_init(&core->debug_node); |
| 2722 | debugfs_remove_recursive(core->dentry); |
| 2723 | core->dentry = NULL; |
| 2724 | mutex_unlock(&clk_debug_lock); |
| 2725 | } |
| 2726 | |
| 2727 | struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode, |
| 2728 | void *data, const struct file_operations *fops) |
| 2729 | { |
| 2730 | struct dentry *d = NULL; |
| 2731 | |
| 2732 | if (hw->core->dentry) |
| 2733 | d = debugfs_create_file(name, mode, hw->core->dentry, data, |
| 2734 | fops); |
| 2735 | |
| 2736 | return d; |
| 2737 | } |
| 2738 | EXPORT_SYMBOL_GPL(clk_debugfs_add_file); |
| 2739 | |
| 2740 | /** |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2741 | * clk_debug_init - lazily populate the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2742 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2743 | * clks are often initialized very early during boot before memory can be |
| 2744 | * dynamically allocated and well before debugfs is setup. This function |
| 2745 | * populates the debugfs clk directory once at boot-time when we know that |
| 2746 | * debugfs is setup. It should only be called once at boot-time, all other clks |
| 2747 | * added dynamically will be done so with clk_debug_register. |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2748 | */ |
| 2749 | static int __init clk_debug_init(void) |
| 2750 | { |
| 2751 | struct clk_core *core; |
| 2752 | struct dentry *d; |
| 2753 | |
| 2754 | rootdir = debugfs_create_dir("clk", NULL); |
| 2755 | |
| 2756 | if (!rootdir) |
| 2757 | return -ENOMEM; |
| 2758 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2759 | d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists, |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2760 | &clk_summary_fops); |
| 2761 | if (!d) |
| 2762 | return -ENOMEM; |
| 2763 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2764 | d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists, |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2765 | &clk_dump_fops); |
| 2766 | if (!d) |
| 2767 | return -ENOMEM; |
| 2768 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2769 | d = debugfs_create_file("clk_orphan_summary", 0444, rootdir, |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2770 | &orphan_list, &clk_summary_fops); |
| 2771 | if (!d) |
| 2772 | return -ENOMEM; |
| 2773 | |
Geert Uytterhoeven | 4c8326d | 2018-01-03 12:06:15 +0100 | [diff] [blame] | 2774 | d = debugfs_create_file("clk_orphan_dump", 0444, rootdir, |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2775 | &orphan_list, &clk_dump_fops); |
| 2776 | if (!d) |
| 2777 | return -ENOMEM; |
| 2778 | |
| 2779 | mutex_lock(&clk_debug_lock); |
| 2780 | hlist_for_each_entry(core, &clk_debug_list, debug_node) |
| 2781 | clk_debug_create_one(core, rootdir); |
| 2782 | |
| 2783 | inited = 1; |
| 2784 | mutex_unlock(&clk_debug_lock); |
| 2785 | |
| 2786 | return 0; |
| 2787 | } |
| 2788 | late_initcall(clk_debug_init); |
| 2789 | #else |
| 2790 | static inline int clk_debug_register(struct clk_core *core) { return 0; } |
| 2791 | static inline void clk_debug_reparent(struct clk_core *core, |
| 2792 | struct clk_core *new_parent) |
| 2793 | { |
| 2794 | } |
| 2795 | static inline void clk_debug_unregister(struct clk_core *core) |
| 2796 | { |
| 2797 | } |
| 2798 | #endif |
| 2799 | |
Michael Turquette | 3d3801e | 2015-02-25 09:11:01 -0800 | [diff] [blame] | 2800 | /** |
Masahiro Yamada | be45ebf | 2015-12-28 19:22:57 +0900 | [diff] [blame] | 2801 | * __clk_core_init - initialize the data structures in a struct clk_core |
Masahiro Yamada | d35c80c | 2015-12-28 19:22:56 +0900 | [diff] [blame] | 2802 | * @core: clk_core being initialized |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2803 | * |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2804 | * Initializes the lists in struct clk_core, queries the hardware for the |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2805 | * parent and rate and sets them both. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2806 | */ |
Masahiro Yamada | be45ebf | 2015-12-28 19:22:57 +0900 | [diff] [blame] | 2807 | static int __clk_core_init(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2808 | { |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2809 | int i, ret; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2810 | struct clk_core *orphan; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2811 | struct hlist_node *tmp2; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2812 | unsigned long rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2813 | |
Masahiro Yamada | d35c80c | 2015-12-28 19:22:56 +0900 | [diff] [blame] | 2814 | if (!core) |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2815 | return -EINVAL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2816 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2817 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2818 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2819 | ret = clk_pm_runtime_get(core); |
| 2820 | if (ret) |
| 2821 | goto unlock; |
| 2822 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2823 | /* check to see if a clock with this name is already registered */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2824 | if (clk_core_lookup(core->name)) { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2825 | pr_debug("%s: clk %s already initialized\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2826 | __func__, core->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2827 | ret = -EEXIST; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2828 | goto out; |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2829 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2830 | |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2831 | /* check that clk_ops are sane. See Documentation/clk.txt */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2832 | if (core->ops->set_rate && |
| 2833 | !((core->ops->round_rate || core->ops->determine_rate) && |
| 2834 | core->ops->recalc_rate)) { |
Masahiro Yamada | c44fccb | 2015-12-28 19:23:03 +0900 | [diff] [blame] | 2835 | pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n", |
| 2836 | __func__, core->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2837 | ret = -EINVAL; |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2838 | goto out; |
| 2839 | } |
| 2840 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2841 | if (core->ops->set_parent && !core->ops->get_parent) { |
Masahiro Yamada | c44fccb | 2015-12-28 19:23:03 +0900 | [diff] [blame] | 2842 | pr_err("%s: %s must implement .get_parent & .set_parent\n", |
| 2843 | __func__, core->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2844 | ret = -EINVAL; |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2845 | goto out; |
| 2846 | } |
| 2847 | |
Masahiro Yamada | 3c8e77d | 2015-12-28 19:23:04 +0900 | [diff] [blame] | 2848 | if (core->num_parents > 1 && !core->ops->get_parent) { |
| 2849 | pr_err("%s: %s must implement .get_parent as it has multi parents\n", |
| 2850 | __func__, core->name); |
| 2851 | ret = -EINVAL; |
| 2852 | goto out; |
| 2853 | } |
| 2854 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2855 | if (core->ops->set_rate_and_parent && |
| 2856 | !(core->ops->set_parent && core->ops->set_rate)) { |
Masahiro Yamada | c44fccb | 2015-12-28 19:23:03 +0900 | [diff] [blame] | 2857 | pr_err("%s: %s must implement .set_parent & .set_rate\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2858 | __func__, core->name); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 2859 | ret = -EINVAL; |
| 2860 | goto out; |
| 2861 | } |
| 2862 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2863 | /* throw a WARN if any entries in parent_names are NULL */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2864 | for (i = 0; i < core->num_parents; i++) |
| 2865 | WARN(!core->parent_names[i], |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2866 | "%s: invalid NULL in %s's .parent_names\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2867 | __func__, core->name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2868 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2869 | core->parent = __clk_init_parent(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2870 | |
| 2871 | /* |
Stephen Boyd | 706d5c7 | 2016-02-22 15:43:41 -0800 | [diff] [blame] | 2872 | * Populate core->parent if parent has already been clk_core_init'd. If |
| 2873 | * parent has not yet been clk_core_init'd then place clk in the orphan |
Stephen Boyd | 47b0eeb | 2016-02-02 17:24:56 -0800 | [diff] [blame] | 2874 | * list. If clk doesn't have any parents then place it in the root |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2875 | * clk list. |
| 2876 | * |
| 2877 | * Every time a new clk is clk_init'd then we walk the list of orphan |
| 2878 | * clocks and re-parent any that are children of the clock currently |
| 2879 | * being clk_init'd. |
| 2880 | */ |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2881 | if (core->parent) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2882 | hlist_add_head(&core->child_node, |
| 2883 | &core->parent->children); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2884 | core->orphan = core->parent->orphan; |
Stephen Boyd | 47b0eeb | 2016-02-02 17:24:56 -0800 | [diff] [blame] | 2885 | } else if (!core->num_parents) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2886 | hlist_add_head(&core->child_node, &clk_root_list); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2887 | core->orphan = false; |
| 2888 | } else { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2889 | hlist_add_head(&core->child_node, &clk_orphan_list); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2890 | core->orphan = true; |
| 2891 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2892 | |
| 2893 | /* |
Jerome Brunet | 541deba | 2018-02-14 14:43:37 +0100 | [diff] [blame] | 2894 | * optional platform-specific magic |
| 2895 | * |
| 2896 | * The .init callback is not used by any of the basic clock types, but |
| 2897 | * exists for weird hardware that must perform initialization magic. |
| 2898 | * Please consider other ways of solving initialization problems before |
| 2899 | * using this callback, as its use is discouraged. |
| 2900 | */ |
| 2901 | if (core->ops->init) |
| 2902 | core->ops->init(core->hw); |
| 2903 | |
| 2904 | /* |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2905 | * Set clk's accuracy. The preferred method is to use |
| 2906 | * .recalc_accuracy. For simple clocks and lazy developers the default |
| 2907 | * fallback is to use the parent's accuracy. If a clock doesn't have a |
| 2908 | * parent (or is orphaned) then accuracy is set to zero (perfect |
| 2909 | * clock). |
| 2910 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2911 | if (core->ops->recalc_accuracy) |
| 2912 | core->accuracy = core->ops->recalc_accuracy(core->hw, |
| 2913 | __clk_get_accuracy(core->parent)); |
| 2914 | else if (core->parent) |
| 2915 | core->accuracy = core->parent->accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2916 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2917 | core->accuracy = 0; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2918 | |
| 2919 | /* |
Maxime Ripard | 9824cf7 | 2014-07-14 13:53:27 +0200 | [diff] [blame] | 2920 | * Set clk's phase. |
| 2921 | * Since a phase is by definition relative to its parent, just |
| 2922 | * query the current clock phase, or just assume it's in phase. |
| 2923 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2924 | if (core->ops->get_phase) |
| 2925 | core->phase = core->ops->get_phase(core->hw); |
Maxime Ripard | 9824cf7 | 2014-07-14 13:53:27 +0200 | [diff] [blame] | 2926 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2927 | core->phase = 0; |
Maxime Ripard | 9824cf7 | 2014-07-14 13:53:27 +0200 | [diff] [blame] | 2928 | |
| 2929 | /* |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2930 | * Set clk's rate. The preferred method is to use .recalc_rate. For |
| 2931 | * simple clocks and lazy developers the default fallback is to use the |
| 2932 | * parent's rate. If a clock doesn't have a parent (or is orphaned) |
| 2933 | * then rate is set to zero. |
| 2934 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2935 | if (core->ops->recalc_rate) |
| 2936 | rate = core->ops->recalc_rate(core->hw, |
| 2937 | clk_core_get_rate_nolock(core->parent)); |
| 2938 | else if (core->parent) |
| 2939 | rate = core->parent->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2940 | else |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2941 | rate = 0; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2942 | core->rate = core->req_rate = rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2943 | |
| 2944 | /* |
Jerome Brunet | 99652a4 | 2018-02-14 14:43:36 +0100 | [diff] [blame] | 2945 | * Enable CLK_IS_CRITICAL clocks so newly added critical clocks |
| 2946 | * don't get accidentally disabled when walking the orphan tree and |
| 2947 | * reparenting clocks |
| 2948 | */ |
| 2949 | if (core->flags & CLK_IS_CRITICAL) { |
| 2950 | unsigned long flags; |
| 2951 | |
| 2952 | clk_core_prepare(core); |
| 2953 | |
| 2954 | flags = clk_enable_lock(); |
| 2955 | clk_core_enable(core); |
| 2956 | clk_enable_unlock(flags); |
| 2957 | } |
| 2958 | |
| 2959 | /* |
Masahiro Yamada | 0e8f6e4 | 2015-12-28 19:23:07 +0900 | [diff] [blame] | 2960 | * walk the list of orphan clocks and reparent any that newly finds a |
| 2961 | * parent. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2962 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2963 | hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { |
Masahiro Yamada | 0e8f6e4 | 2015-12-28 19:23:07 +0900 | [diff] [blame] | 2964 | struct clk_core *parent = __clk_init_parent(orphan); |
Martin Fuzzey | 1f61e5f | 2012-11-22 20:15:05 +0100 | [diff] [blame] | 2965 | |
Michael Turquette | 904e6ea | 2016-07-08 16:32:10 -0700 | [diff] [blame] | 2966 | /* |
Jerome Brunet | 99652a4 | 2018-02-14 14:43:36 +0100 | [diff] [blame] | 2967 | * We need to use __clk_set_parent_before() and _after() to |
| 2968 | * to properly migrate any prepare/enable count of the orphan |
| 2969 | * clock. This is important for CLK_IS_CRITICAL clocks, which |
| 2970 | * are enabled during init but might not have a parent yet. |
Michael Turquette | 904e6ea | 2016-07-08 16:32:10 -0700 | [diff] [blame] | 2971 | */ |
| 2972 | if (parent) { |
Stephen Boyd | f8f8f1d | 2017-11-02 00:36:09 -0700 | [diff] [blame] | 2973 | /* update the clk tree topology */ |
Jerome Brunet | 99652a4 | 2018-02-14 14:43:36 +0100 | [diff] [blame] | 2974 | __clk_set_parent_before(orphan, parent); |
| 2975 | __clk_set_parent_after(orphan, parent, NULL); |
Michael Turquette | 904e6ea | 2016-07-08 16:32:10 -0700 | [diff] [blame] | 2976 | __clk_recalc_accuracies(orphan); |
| 2977 | __clk_recalc_rates(orphan, 0); |
| 2978 | } |
Masahiro Yamada | 0e8f6e4 | 2015-12-28 19:23:07 +0900 | [diff] [blame] | 2979 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2980 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2981 | kref_init(&core->ref); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2982 | out: |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 2983 | clk_pm_runtime_put(core); |
| 2984 | unlock: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2985 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2986 | |
Stephen Boyd | 89f7e9d | 2014-12-12 15:04:16 -0800 | [diff] [blame] | 2987 | if (!ret) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2988 | clk_debug_register(core); |
Stephen Boyd | 89f7e9d | 2014-12-12 15:04:16 -0800 | [diff] [blame] | 2989 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2990 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2991 | } |
| 2992 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2993 | struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id, |
| 2994 | const char *con_id) |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2995 | { |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2996 | struct clk *clk; |
| 2997 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2998 | /* This is to allow this function to be chained to others */ |
Masahiro Yamada | c1de135 | 2015-11-20 14:38:49 +0900 | [diff] [blame] | 2999 | if (IS_ERR_OR_NULL(hw)) |
Masahiro Yamada | 8a23133 | 2016-07-19 16:28:47 +0900 | [diff] [blame] | 3000 | return ERR_CAST(hw); |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 3001 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3002 | clk = kzalloc(sizeof(*clk), GFP_KERNEL); |
| 3003 | if (!clk) |
| 3004 | return ERR_PTR(-ENOMEM); |
| 3005 | |
| 3006 | clk->core = hw->core; |
| 3007 | clk->dev_id = dev_id; |
Leonard Crestez | 253160a | 2017-02-20 15:20:56 +0200 | [diff] [blame] | 3008 | clk->con_id = kstrdup_const(con_id, GFP_KERNEL); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3009 | clk->max_rate = ULONG_MAX; |
| 3010 | |
| 3011 | clk_prepare_lock(); |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 3012 | hlist_add_head(&clk->clks_node, &hw->core->clks); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3013 | clk_prepare_unlock(); |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 3014 | |
| 3015 | return clk; |
| 3016 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3017 | |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3018 | void __clk_free_clk(struct clk *clk) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3019 | { |
| 3020 | clk_prepare_lock(); |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 3021 | hlist_del(&clk->clks_node); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3022 | clk_prepare_unlock(); |
| 3023 | |
Leonard Crestez | 253160a | 2017-02-20 15:20:56 +0200 | [diff] [blame] | 3024 | kfree_const(clk->con_id); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3025 | kfree(clk); |
| 3026 | } |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 3027 | |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3028 | /** |
| 3029 | * clk_register - allocate a new clock, register it and return an opaque cookie |
| 3030 | * @dev: device that is registering this clock |
| 3031 | * @hw: link to hardware-specific clock data |
| 3032 | * |
| 3033 | * clk_register is the primary interface for populating the clock tree with new |
| 3034 | * 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] | 3035 | * 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] | 3036 | * rest of the clock API. In the event of an error clk_register will return an |
| 3037 | * error code; drivers must test for an error code after calling clk_register. |
| 3038 | */ |
| 3039 | struct clk *clk_register(struct device *dev, struct clk_hw *hw) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3040 | { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3041 | int i, ret; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3042 | struct clk_core *core; |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3043 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3044 | core = kzalloc(sizeof(*core), GFP_KERNEL); |
| 3045 | if (!core) { |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3046 | ret = -ENOMEM; |
| 3047 | goto fail_out; |
| 3048 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3049 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3050 | core->name = kstrdup_const(hw->init->name, GFP_KERNEL); |
| 3051 | if (!core->name) { |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 3052 | ret = -ENOMEM; |
| 3053 | goto fail_name; |
| 3054 | } |
Jerome Brunet | 29fd2a3 | 2017-12-19 09:33:29 +0100 | [diff] [blame] | 3055 | |
| 3056 | if (WARN_ON(!hw->init->ops)) { |
| 3057 | ret = -EINVAL; |
| 3058 | goto fail_ops; |
| 3059 | } |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3060 | core->ops = hw->init->ops; |
Jerome Brunet | 29fd2a3 | 2017-12-19 09:33:29 +0100 | [diff] [blame] | 3061 | |
Marek Szyprowski | 9a34b45 | 2017-08-21 10:04:59 +0200 | [diff] [blame] | 3062 | if (dev && pm_runtime_enabled(dev)) |
| 3063 | core->dev = dev; |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 3064 | if (dev && dev->driver) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3065 | core->owner = dev->driver->owner; |
| 3066 | core->hw = hw; |
| 3067 | core->flags = hw->init->flags; |
| 3068 | core->num_parents = hw->init->num_parents; |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 3069 | core->min_rate = 0; |
| 3070 | core->max_rate = ULONG_MAX; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3071 | hw->core = core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3072 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3073 | /* allocate local copy in case parent_names is __initdata */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3074 | core->parent_names = kcalloc(core->num_parents, sizeof(char *), |
Tomasz Figa | 96a7ed9 | 2013-09-29 02:37:15 +0200 | [diff] [blame] | 3075 | GFP_KERNEL); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3076 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3077 | if (!core->parent_names) { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3078 | ret = -ENOMEM; |
| 3079 | goto fail_parent_names; |
| 3080 | } |
| 3081 | |
| 3082 | |
| 3083 | /* copy each string name in case parent_names is __initdata */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3084 | for (i = 0; i < core->num_parents; i++) { |
| 3085 | core->parent_names[i] = kstrdup_const(hw->init->parent_names[i], |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 3086 | GFP_KERNEL); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3087 | if (!core->parent_names[i]) { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3088 | ret = -ENOMEM; |
| 3089 | goto fail_parent_names_copy; |
| 3090 | } |
| 3091 | } |
| 3092 | |
Masahiro Yamada | 176d116 | 2015-12-28 19:23:00 +0900 | [diff] [blame] | 3093 | /* avoid unnecessary string look-ups of clk_core's possible parents. */ |
| 3094 | core->parents = kcalloc(core->num_parents, sizeof(*core->parents), |
| 3095 | GFP_KERNEL); |
| 3096 | if (!core->parents) { |
| 3097 | ret = -ENOMEM; |
| 3098 | goto fail_parents; |
| 3099 | }; |
| 3100 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3101 | INIT_HLIST_HEAD(&core->clks); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3102 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3103 | hw->clk = __clk_create_clk(hw, NULL, NULL); |
| 3104 | if (IS_ERR(hw->clk)) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3105 | ret = PTR_ERR(hw->clk); |
Masahiro Yamada | 176d116 | 2015-12-28 19:23:00 +0900 | [diff] [blame] | 3106 | goto fail_parents; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3107 | } |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3108 | |
Masahiro Yamada | be45ebf | 2015-12-28 19:22:57 +0900 | [diff] [blame] | 3109 | ret = __clk_core_init(core); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3110 | if (!ret) |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3111 | return hw->clk; |
| 3112 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3113 | __clk_free_clk(hw->clk); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3114 | hw->clk = NULL; |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3115 | |
Masahiro Yamada | 176d116 | 2015-12-28 19:23:00 +0900 | [diff] [blame] | 3116 | fail_parents: |
| 3117 | kfree(core->parents); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3118 | fail_parent_names_copy: |
| 3119 | while (--i >= 0) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3120 | kfree_const(core->parent_names[i]); |
| 3121 | kfree(core->parent_names); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3122 | fail_parent_names: |
Jerome Brunet | 29fd2a3 | 2017-12-19 09:33:29 +0100 | [diff] [blame] | 3123 | fail_ops: |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3124 | kfree_const(core->name); |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 3125 | fail_name: |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3126 | kfree(core); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 3127 | fail_out: |
| 3128 | return ERR_PTR(ret); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3129 | } |
| 3130 | EXPORT_SYMBOL_GPL(clk_register); |
| 3131 | |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3132 | /** |
| 3133 | * clk_hw_register - register a clk_hw and return an error code |
| 3134 | * @dev: device that is registering this clock |
| 3135 | * @hw: link to hardware-specific clock data |
| 3136 | * |
| 3137 | * clk_hw_register is the primary interface for populating the clock tree with |
| 3138 | * new clock nodes. It returns an integer equal to zero indicating success or |
| 3139 | * less than zero indicating failure. Drivers must test for an error code after |
| 3140 | * calling clk_hw_register(). |
| 3141 | */ |
| 3142 | int clk_hw_register(struct device *dev, struct clk_hw *hw) |
| 3143 | { |
| 3144 | return PTR_ERR_OR_ZERO(clk_register(dev, hw)); |
| 3145 | } |
| 3146 | EXPORT_SYMBOL_GPL(clk_hw_register); |
| 3147 | |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 3148 | /* Free memory allocated for a clock. */ |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3149 | static void __clk_release(struct kref *ref) |
| 3150 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3151 | struct clk_core *core = container_of(ref, struct clk_core, ref); |
| 3152 | int i = core->num_parents; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3153 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 3154 | lockdep_assert_held(&prepare_lock); |
| 3155 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3156 | kfree(core->parents); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3157 | while (--i >= 0) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3158 | kfree_const(core->parent_names[i]); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3159 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 3160 | kfree(core->parent_names); |
| 3161 | kfree_const(core->name); |
| 3162 | kfree(core); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /* |
| 3166 | * Empty clk_ops for unregistered clocks. These are used temporarily |
| 3167 | * after clk_unregister() was called on a clock and until last clock |
| 3168 | * consumer calls clk_put() and the struct clk object is freed. |
| 3169 | */ |
| 3170 | static int clk_nodrv_prepare_enable(struct clk_hw *hw) |
| 3171 | { |
| 3172 | return -ENXIO; |
| 3173 | } |
| 3174 | |
| 3175 | static void clk_nodrv_disable_unprepare(struct clk_hw *hw) |
| 3176 | { |
| 3177 | WARN_ON_ONCE(1); |
| 3178 | } |
| 3179 | |
| 3180 | static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate, |
| 3181 | unsigned long parent_rate) |
| 3182 | { |
| 3183 | return -ENXIO; |
| 3184 | } |
| 3185 | |
| 3186 | static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index) |
| 3187 | { |
| 3188 | return -ENXIO; |
| 3189 | } |
| 3190 | |
| 3191 | static const struct clk_ops clk_nodrv_ops = { |
| 3192 | .enable = clk_nodrv_prepare_enable, |
| 3193 | .disable = clk_nodrv_disable_unprepare, |
| 3194 | .prepare = clk_nodrv_prepare_enable, |
| 3195 | .unprepare = clk_nodrv_disable_unprepare, |
| 3196 | .set_rate = clk_nodrv_set_rate, |
| 3197 | .set_parent = clk_nodrv_set_parent, |
| 3198 | }; |
| 3199 | |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 3200 | /** |
| 3201 | * clk_unregister - unregister a currently registered clock |
| 3202 | * @clk: clock to unregister |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 3203 | */ |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3204 | void clk_unregister(struct clk *clk) |
| 3205 | { |
| 3206 | unsigned long flags; |
| 3207 | |
Stephen Boyd | 6314b67 | 2014-09-04 23:37:49 -0700 | [diff] [blame] | 3208 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
| 3209 | return; |
| 3210 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3211 | clk_debug_unregister(clk->core); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3212 | |
| 3213 | clk_prepare_lock(); |
| 3214 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3215 | if (clk->core->ops == &clk_nodrv_ops) { |
| 3216 | pr_err("%s: unregistered clock: %s\n", __func__, |
| 3217 | clk->core->name); |
Insu Yun | 4106a3d | 2016-01-30 10:12:04 -0500 | [diff] [blame] | 3218 | goto unlock; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3219 | } |
| 3220 | /* |
| 3221 | * Assign empty clock ops for consumers that might still hold |
| 3222 | * a reference to this clock. |
| 3223 | */ |
| 3224 | flags = clk_enable_lock(); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3225 | clk->core->ops = &clk_nodrv_ops; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3226 | clk_enable_unlock(flags); |
| 3227 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3228 | if (!hlist_empty(&clk->core->children)) { |
| 3229 | struct clk_core *child; |
Stephen Boyd | 874f224 | 2014-04-18 16:29:43 -0700 | [diff] [blame] | 3230 | struct hlist_node *t; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3231 | |
| 3232 | /* Reparent all children to the orphan list. */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3233 | hlist_for_each_entry_safe(child, t, &clk->core->children, |
| 3234 | child_node) |
Jerome Brunet | 91baa9f | 2017-12-01 22:51:52 +0100 | [diff] [blame] | 3235 | clk_core_set_parent_nolock(child, NULL); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3238 | hlist_del_init(&clk->core->child_node); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3239 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3240 | if (clk->core->prepare_count) |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3241 | pr_warn("%s: unregistering prepared clock: %s\n", |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3242 | __func__, clk->core->name); |
Jerome Brunet | e55a839 | 2017-12-01 22:51:56 +0100 | [diff] [blame] | 3243 | |
| 3244 | if (clk->core->protect_count) |
| 3245 | pr_warn("%s: unregistering protected clock: %s\n", |
| 3246 | __func__, clk->core->name); |
| 3247 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3248 | kref_put(&clk->core->ref, __clk_release); |
Insu Yun | 4106a3d | 2016-01-30 10:12:04 -0500 | [diff] [blame] | 3249 | unlock: |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3250 | clk_prepare_unlock(); |
| 3251 | } |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 3252 | EXPORT_SYMBOL_GPL(clk_unregister); |
| 3253 | |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3254 | /** |
| 3255 | * clk_hw_unregister - unregister a currently registered clk_hw |
| 3256 | * @hw: hardware-specific clock data to unregister |
| 3257 | */ |
| 3258 | void clk_hw_unregister(struct clk_hw *hw) |
| 3259 | { |
| 3260 | clk_unregister(hw->clk); |
| 3261 | } |
| 3262 | EXPORT_SYMBOL_GPL(clk_hw_unregister); |
| 3263 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3264 | static void devm_clk_release(struct device *dev, void *res) |
| 3265 | { |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3266 | clk_unregister(*(struct clk **)res); |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3267 | } |
| 3268 | |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3269 | static void devm_clk_hw_release(struct device *dev, void *res) |
| 3270 | { |
| 3271 | clk_hw_unregister(*(struct clk_hw **)res); |
| 3272 | } |
| 3273 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3274 | /** |
| 3275 | * devm_clk_register - resource managed clk_register() |
| 3276 | * @dev: device that is registering this clock |
| 3277 | * @hw: link to hardware-specific clock data |
| 3278 | * |
| 3279 | * Managed clk_register(). Clocks returned from this function are |
| 3280 | * automatically clk_unregister()ed on driver detach. See clk_register() for |
| 3281 | * more information. |
| 3282 | */ |
| 3283 | struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) |
| 3284 | { |
| 3285 | struct clk *clk; |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3286 | struct clk **clkp; |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3287 | |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3288 | clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL); |
| 3289 | if (!clkp) |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3290 | return ERR_PTR(-ENOMEM); |
| 3291 | |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3292 | clk = clk_register(dev, hw); |
| 3293 | if (!IS_ERR(clk)) { |
| 3294 | *clkp = clk; |
| 3295 | devres_add(dev, clkp); |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3296 | } else { |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 3297 | devres_free(clkp); |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | return clk; |
| 3301 | } |
| 3302 | EXPORT_SYMBOL_GPL(devm_clk_register); |
| 3303 | |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3304 | /** |
| 3305 | * devm_clk_hw_register - resource managed clk_hw_register() |
| 3306 | * @dev: device that is registering this clock |
| 3307 | * @hw: link to hardware-specific clock data |
| 3308 | * |
Masahiro Yamada | c47265a | 2016-05-01 19:56:08 +0900 | [diff] [blame] | 3309 | * Managed clk_hw_register(). Clocks registered by this function are |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3310 | * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register() |
| 3311 | * for more information. |
| 3312 | */ |
| 3313 | int devm_clk_hw_register(struct device *dev, struct clk_hw *hw) |
| 3314 | { |
| 3315 | struct clk_hw **hwp; |
| 3316 | int ret; |
| 3317 | |
| 3318 | hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL); |
| 3319 | if (!hwp) |
| 3320 | return -ENOMEM; |
| 3321 | |
| 3322 | ret = clk_hw_register(dev, hw); |
| 3323 | if (!ret) { |
| 3324 | *hwp = hw; |
| 3325 | devres_add(dev, hwp); |
| 3326 | } else { |
| 3327 | devres_free(hwp); |
| 3328 | } |
| 3329 | |
| 3330 | return ret; |
| 3331 | } |
| 3332 | EXPORT_SYMBOL_GPL(devm_clk_hw_register); |
| 3333 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3334 | static int devm_clk_match(struct device *dev, void *res, void *data) |
| 3335 | { |
| 3336 | struct clk *c = res; |
| 3337 | if (WARN_ON(!c)) |
| 3338 | return 0; |
| 3339 | return c == data; |
| 3340 | } |
| 3341 | |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3342 | static int devm_clk_hw_match(struct device *dev, void *res, void *data) |
| 3343 | { |
| 3344 | struct clk_hw *hw = res; |
| 3345 | |
| 3346 | if (WARN_ON(!hw)) |
| 3347 | return 0; |
| 3348 | return hw == data; |
| 3349 | } |
| 3350 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 3351 | /** |
| 3352 | * devm_clk_unregister - resource managed clk_unregister() |
| 3353 | * @clk: clock to unregister |
| 3354 | * |
| 3355 | * Deallocate a clock allocated with devm_clk_register(). Normally |
| 3356 | * this function will not need to be called and the resource management |
| 3357 | * code will ensure that the resource is freed. |
| 3358 | */ |
| 3359 | void devm_clk_unregister(struct device *dev, struct clk *clk) |
| 3360 | { |
| 3361 | WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); |
| 3362 | } |
| 3363 | EXPORT_SYMBOL_GPL(devm_clk_unregister); |
| 3364 | |
Stephen Boyd | 4143804 | 2016-02-05 17:02:52 -0800 | [diff] [blame] | 3365 | /** |
| 3366 | * devm_clk_hw_unregister - resource managed clk_hw_unregister() |
| 3367 | * @dev: device that is unregistering the hardware-specific clock data |
| 3368 | * @hw: link to hardware-specific clock data |
| 3369 | * |
| 3370 | * Unregister a clk_hw registered with devm_clk_hw_register(). Normally |
| 3371 | * this function will not need to be called and the resource management |
| 3372 | * code will ensure that the resource is freed. |
| 3373 | */ |
| 3374 | void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw) |
| 3375 | { |
| 3376 | WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match, |
| 3377 | hw)); |
| 3378 | } |
| 3379 | EXPORT_SYMBOL_GPL(devm_clk_hw_unregister); |
| 3380 | |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 3381 | /* |
| 3382 | * clkdev helpers |
| 3383 | */ |
| 3384 | int __clk_get(struct clk *clk) |
| 3385 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3386 | struct clk_core *core = !clk ? NULL : clk->core; |
| 3387 | |
| 3388 | if (core) { |
| 3389 | if (!try_module_get(core->owner)) |
Sylwester Nawrocki | 00efcb1 | 2014-01-07 13:03:43 +0100 | [diff] [blame] | 3390 | return 0; |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 3391 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3392 | kref_get(&core->ref); |
Sylwester Nawrocki | 00efcb1 | 2014-01-07 13:03:43 +0100 | [diff] [blame] | 3393 | } |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 3394 | return 1; |
| 3395 | } |
| 3396 | |
| 3397 | void __clk_put(struct clk *clk) |
| 3398 | { |
Tomeu Vizoso | 10cdfe5 | 2014-12-02 08:54:19 +0100 | [diff] [blame] | 3399 | struct module *owner; |
| 3400 | |
Sylwester Nawrocki | 00efcb1 | 2014-01-07 13:03:43 +0100 | [diff] [blame] | 3401 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 3402 | return; |
| 3403 | |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3404 | clk_prepare_lock(); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3405 | |
Jerome Brunet | 55e9b8b | 2017-12-01 22:51:59 +0100 | [diff] [blame] | 3406 | /* |
| 3407 | * Before calling clk_put, all calls to clk_rate_exclusive_get() from a |
| 3408 | * given user should be balanced with calls to clk_rate_exclusive_put() |
| 3409 | * and by that same consumer |
| 3410 | */ |
| 3411 | if (WARN_ON(clk->exclusive_count)) { |
| 3412 | /* We voiced our concern, let's sanitize the situation */ |
| 3413 | clk->core->protect_count -= (clk->exclusive_count - 1); |
| 3414 | clk_core_rate_unprotect(clk->core); |
| 3415 | clk->exclusive_count = 0; |
| 3416 | } |
| 3417 | |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 3418 | hlist_del(&clk->clks_node); |
Tomeu Vizoso | ec02ace | 2015-02-06 15:13:01 +0100 | [diff] [blame] | 3419 | if (clk->min_rate > clk->core->req_rate || |
| 3420 | clk->max_rate < clk->core->req_rate) |
| 3421 | clk_core_set_rate_nolock(clk->core, clk->core->req_rate); |
| 3422 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3423 | owner = clk->core->owner; |
| 3424 | kref_put(&clk->core->ref, __clk_release); |
| 3425 | |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 3426 | clk_prepare_unlock(); |
| 3427 | |
Tomeu Vizoso | 10cdfe5 | 2014-12-02 08:54:19 +0100 | [diff] [blame] | 3428 | module_put(owner); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 3429 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3430 | kfree(clk); |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 3431 | } |
| 3432 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3433 | /*** clk rate change notifiers ***/ |
| 3434 | |
| 3435 | /** |
| 3436 | * clk_notifier_register - add a clk rate change notifier |
| 3437 | * @clk: struct clk * to watch |
| 3438 | * @nb: struct notifier_block * with callback info |
| 3439 | * |
| 3440 | * Request notification when clk's rate changes. This uses an SRCU |
| 3441 | * notifier because we want it to block and notifier unregistrations are |
| 3442 | * uncommon. The callbacks associated with the notifier must not |
| 3443 | * re-enter into the clk framework by calling any top-level clk APIs; |
| 3444 | * this will cause a nested prepare_lock mutex. |
| 3445 | * |
Masahiro Yamada | 198bb59 | 2015-11-30 16:40:51 +0900 | [diff] [blame] | 3446 | * In all notification cases (pre, post and abort rate change) the original |
| 3447 | * clock rate is passed to the callback via struct clk_notifier_data.old_rate |
| 3448 | * and the new frequency is passed via struct clk_notifier_data.new_rate. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3449 | * |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3450 | * clk_notifier_register() must be called from non-atomic context. |
| 3451 | * Returns -EINVAL if called with null arguments, -ENOMEM upon |
| 3452 | * allocation failure; otherwise, passes along the return value of |
| 3453 | * srcu_notifier_chain_register(). |
| 3454 | */ |
| 3455 | int clk_notifier_register(struct clk *clk, struct notifier_block *nb) |
| 3456 | { |
| 3457 | struct clk_notifier *cn; |
| 3458 | int ret = -ENOMEM; |
| 3459 | |
| 3460 | if (!clk || !nb) |
| 3461 | return -EINVAL; |
| 3462 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 3463 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3464 | |
| 3465 | /* search the list of notifiers for this clk */ |
| 3466 | list_for_each_entry(cn, &clk_notifier_list, node) |
| 3467 | if (cn->clk == clk) |
| 3468 | break; |
| 3469 | |
| 3470 | /* if clk wasn't in the notifier list, allocate new clk_notifier */ |
| 3471 | if (cn->clk != clk) { |
Markus Elfring | 1808a32 | 2017-04-20 09:30:52 +0200 | [diff] [blame] | 3472 | cn = kzalloc(sizeof(*cn), GFP_KERNEL); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3473 | if (!cn) |
| 3474 | goto out; |
| 3475 | |
| 3476 | cn->clk = clk; |
| 3477 | srcu_init_notifier_head(&cn->notifier_head); |
| 3478 | |
| 3479 | list_add(&cn->node, &clk_notifier_list); |
| 3480 | } |
| 3481 | |
| 3482 | ret = srcu_notifier_chain_register(&cn->notifier_head, nb); |
| 3483 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3484 | clk->core->notifier_count++; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3485 | |
| 3486 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 3487 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3488 | |
| 3489 | return ret; |
| 3490 | } |
| 3491 | EXPORT_SYMBOL_GPL(clk_notifier_register); |
| 3492 | |
| 3493 | /** |
| 3494 | * clk_notifier_unregister - remove a clk rate change notifier |
| 3495 | * @clk: struct clk * |
| 3496 | * @nb: struct notifier_block * with callback info |
| 3497 | * |
| 3498 | * Request no further notification for changes to 'clk' and frees memory |
| 3499 | * allocated in clk_notifier_register. |
| 3500 | * |
| 3501 | * Returns -EINVAL if called with null arguments; otherwise, passes |
| 3502 | * along the return value of srcu_notifier_chain_unregister(). |
| 3503 | */ |
| 3504 | int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) |
| 3505 | { |
| 3506 | struct clk_notifier *cn = NULL; |
| 3507 | int ret = -EINVAL; |
| 3508 | |
| 3509 | if (!clk || !nb) |
| 3510 | return -EINVAL; |
| 3511 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 3512 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3513 | |
| 3514 | list_for_each_entry(cn, &clk_notifier_list, node) |
| 3515 | if (cn->clk == clk) |
| 3516 | break; |
| 3517 | |
| 3518 | if (cn->clk == clk) { |
| 3519 | ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); |
| 3520 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 3521 | clk->core->notifier_count--; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3522 | |
| 3523 | /* XXX the notifier code should handle this better */ |
| 3524 | if (!cn->notifier_head.head) { |
| 3525 | srcu_cleanup_notifier_head(&cn->notifier_head); |
Lai Jiangshan | 72b5322 | 2013-06-03 17:17:15 +0800 | [diff] [blame] | 3526 | list_del(&cn->node); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3527 | kfree(cn); |
| 3528 | } |
| 3529 | |
| 3530 | } else { |
| 3531 | ret = -ENOENT; |
| 3532 | } |
| 3533 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 3534 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 3535 | |
| 3536 | return ret; |
| 3537 | } |
| 3538 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3539 | |
| 3540 | #ifdef CONFIG_OF |
| 3541 | /** |
| 3542 | * struct of_clk_provider - Clock provider registration structure |
| 3543 | * @link: Entry in global list of clock providers |
| 3544 | * @node: Pointer to device tree node of clock provider |
| 3545 | * @get: Get clock callback. Returns NULL or a struct clk for the |
| 3546 | * given clock specifier |
| 3547 | * @data: context pointer to be passed into @get callback |
| 3548 | */ |
| 3549 | struct of_clk_provider { |
| 3550 | struct list_head link; |
| 3551 | |
| 3552 | struct device_node *node; |
| 3553 | struct clk *(*get)(struct of_phandle_args *clkspec, void *data); |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3554 | struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3555 | void *data; |
| 3556 | }; |
| 3557 | |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 3558 | static const struct of_device_id __clk_of_table_sentinel |
| 3559 | __used __section(__clk_of_table_end); |
| 3560 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3561 | static LIST_HEAD(of_clk_providers); |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3562 | static DEFINE_MUTEX(of_clk_mutex); |
| 3563 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3564 | struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, |
| 3565 | void *data) |
| 3566 | { |
| 3567 | return data; |
| 3568 | } |
| 3569 | EXPORT_SYMBOL_GPL(of_clk_src_simple_get); |
| 3570 | |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3571 | struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data) |
| 3572 | { |
| 3573 | return data; |
| 3574 | } |
| 3575 | EXPORT_SYMBOL_GPL(of_clk_hw_simple_get); |
| 3576 | |
Shawn Guo | 494bfec | 2012-08-22 21:36:27 +0800 | [diff] [blame] | 3577 | struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) |
| 3578 | { |
| 3579 | struct clk_onecell_data *clk_data = data; |
| 3580 | unsigned int idx = clkspec->args[0]; |
| 3581 | |
| 3582 | if (idx >= clk_data->clk_num) { |
Geert Uytterhoeven | 7e96353 | 2015-10-16 17:12:32 +0200 | [diff] [blame] | 3583 | pr_err("%s: invalid clock index %u\n", __func__, idx); |
Shawn Guo | 494bfec | 2012-08-22 21:36:27 +0800 | [diff] [blame] | 3584 | return ERR_PTR(-EINVAL); |
| 3585 | } |
| 3586 | |
| 3587 | return clk_data->clks[idx]; |
| 3588 | } |
| 3589 | EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); |
| 3590 | |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3591 | struct clk_hw * |
| 3592 | of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data) |
| 3593 | { |
| 3594 | struct clk_hw_onecell_data *hw_data = data; |
| 3595 | unsigned int idx = clkspec->args[0]; |
| 3596 | |
| 3597 | if (idx >= hw_data->num) { |
| 3598 | pr_err("%s: invalid index %u\n", __func__, idx); |
| 3599 | return ERR_PTR(-EINVAL); |
| 3600 | } |
| 3601 | |
| 3602 | return hw_data->hws[idx]; |
| 3603 | } |
| 3604 | EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get); |
| 3605 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3606 | /** |
| 3607 | * of_clk_add_provider() - Register a clock provider for a node |
| 3608 | * @np: Device node pointer associated with clock provider |
| 3609 | * @clk_src_get: callback for decoding clock |
| 3610 | * @data: context pointer for @clk_src_get callback. |
| 3611 | */ |
| 3612 | int of_clk_add_provider(struct device_node *np, |
| 3613 | struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, |
| 3614 | void *data), |
| 3615 | void *data) |
| 3616 | { |
| 3617 | struct of_clk_provider *cp; |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 3618 | int ret; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3619 | |
Markus Elfring | 1808a32 | 2017-04-20 09:30:52 +0200 | [diff] [blame] | 3620 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3621 | if (!cp) |
| 3622 | return -ENOMEM; |
| 3623 | |
| 3624 | cp->node = of_node_get(np); |
| 3625 | cp->data = data; |
| 3626 | cp->get = clk_src_get; |
| 3627 | |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3628 | mutex_lock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3629 | list_add(&cp->link, &of_clk_providers); |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3630 | mutex_unlock(&of_clk_mutex); |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 3631 | pr_debug("Added clock from %pOF\n", np); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3632 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 3633 | ret = of_clk_set_defaults(np, true); |
| 3634 | if (ret < 0) |
| 3635 | of_clk_del_provider(np); |
| 3636 | |
| 3637 | return ret; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3638 | } |
| 3639 | EXPORT_SYMBOL_GPL(of_clk_add_provider); |
| 3640 | |
| 3641 | /** |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3642 | * of_clk_add_hw_provider() - Register a clock provider for a node |
| 3643 | * @np: Device node pointer associated with clock provider |
| 3644 | * @get: callback for decoding clk_hw |
| 3645 | * @data: context pointer for @get callback. |
| 3646 | */ |
| 3647 | int of_clk_add_hw_provider(struct device_node *np, |
| 3648 | struct clk_hw *(*get)(struct of_phandle_args *clkspec, |
| 3649 | void *data), |
| 3650 | void *data) |
| 3651 | { |
| 3652 | struct of_clk_provider *cp; |
| 3653 | int ret; |
| 3654 | |
| 3655 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
| 3656 | if (!cp) |
| 3657 | return -ENOMEM; |
| 3658 | |
| 3659 | cp->node = of_node_get(np); |
| 3660 | cp->data = data; |
| 3661 | cp->get_hw = get; |
| 3662 | |
| 3663 | mutex_lock(&of_clk_mutex); |
| 3664 | list_add(&cp->link, &of_clk_providers); |
| 3665 | mutex_unlock(&of_clk_mutex); |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 3666 | pr_debug("Added clk_hw provider from %pOF\n", np); |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3667 | |
| 3668 | ret = of_clk_set_defaults(np, true); |
| 3669 | if (ret < 0) |
| 3670 | of_clk_del_provider(np); |
| 3671 | |
| 3672 | return ret; |
| 3673 | } |
| 3674 | EXPORT_SYMBOL_GPL(of_clk_add_hw_provider); |
| 3675 | |
Stephen Boyd | aa795c4 | 2017-09-01 16:16:40 -0700 | [diff] [blame] | 3676 | static void devm_of_clk_release_provider(struct device *dev, void *res) |
| 3677 | { |
| 3678 | of_clk_del_provider(*(struct device_node **)res); |
| 3679 | } |
| 3680 | |
| 3681 | int devm_of_clk_add_hw_provider(struct device *dev, |
| 3682 | struct clk_hw *(*get)(struct of_phandle_args *clkspec, |
| 3683 | void *data), |
| 3684 | void *data) |
| 3685 | { |
| 3686 | struct device_node **ptr, *np; |
| 3687 | int ret; |
| 3688 | |
| 3689 | ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr), |
| 3690 | GFP_KERNEL); |
| 3691 | if (!ptr) |
| 3692 | return -ENOMEM; |
| 3693 | |
| 3694 | np = dev->of_node; |
| 3695 | ret = of_clk_add_hw_provider(np, get, data); |
| 3696 | if (!ret) { |
| 3697 | *ptr = np; |
| 3698 | devres_add(dev, ptr); |
| 3699 | } else { |
| 3700 | devres_free(ptr); |
| 3701 | } |
| 3702 | |
| 3703 | return ret; |
| 3704 | } |
| 3705 | EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider); |
| 3706 | |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3707 | /** |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3708 | * of_clk_del_provider() - Remove a previously registered clock provider |
| 3709 | * @np: Device node pointer associated with clock provider |
| 3710 | */ |
| 3711 | void of_clk_del_provider(struct device_node *np) |
| 3712 | { |
| 3713 | struct of_clk_provider *cp; |
| 3714 | |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3715 | mutex_lock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3716 | list_for_each_entry(cp, &of_clk_providers, link) { |
| 3717 | if (cp->node == np) { |
| 3718 | list_del(&cp->link); |
| 3719 | of_node_put(cp->node); |
| 3720 | kfree(cp); |
| 3721 | break; |
| 3722 | } |
| 3723 | } |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3724 | mutex_unlock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3725 | } |
| 3726 | EXPORT_SYMBOL_GPL(of_clk_del_provider); |
| 3727 | |
Stephen Boyd | aa795c4 | 2017-09-01 16:16:40 -0700 | [diff] [blame] | 3728 | static int devm_clk_provider_match(struct device *dev, void *res, void *data) |
| 3729 | { |
| 3730 | struct device_node **np = res; |
| 3731 | |
| 3732 | if (WARN_ON(!np || !*np)) |
| 3733 | return 0; |
| 3734 | |
| 3735 | return *np == data; |
| 3736 | } |
| 3737 | |
| 3738 | void devm_of_clk_del_provider(struct device *dev) |
| 3739 | { |
| 3740 | int ret; |
| 3741 | |
| 3742 | ret = devres_release(dev, devm_of_clk_release_provider, |
| 3743 | devm_clk_provider_match, dev->of_node); |
| 3744 | |
| 3745 | WARN_ON(ret); |
| 3746 | } |
| 3747 | EXPORT_SYMBOL(devm_of_clk_del_provider); |
| 3748 | |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3749 | static struct clk_hw * |
| 3750 | __of_clk_get_hw_from_provider(struct of_clk_provider *provider, |
| 3751 | struct of_phandle_args *clkspec) |
| 3752 | { |
| 3753 | struct clk *clk; |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3754 | |
Stephen Boyd | 74002fc | 2016-08-25 13:35:36 -0700 | [diff] [blame] | 3755 | if (provider->get_hw) |
| 3756 | return provider->get_hw(clkspec, provider->data); |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3757 | |
Stephen Boyd | 74002fc | 2016-08-25 13:35:36 -0700 | [diff] [blame] | 3758 | clk = provider->get(clkspec, provider->data); |
| 3759 | if (IS_ERR(clk)) |
| 3760 | return ERR_CAST(clk); |
| 3761 | return __clk_get_hw(clk); |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3762 | } |
| 3763 | |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3764 | struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec, |
| 3765 | const char *dev_id, const char *con_id) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3766 | { |
| 3767 | struct of_clk_provider *provider; |
Jean-Francois Moine | a34cd46 | 2013-11-25 19:47:04 +0100 | [diff] [blame] | 3768 | struct clk *clk = ERR_PTR(-EPROBE_DEFER); |
Stephen Boyd | f155d15 | 2016-08-15 14:32:23 -0700 | [diff] [blame] | 3769 | struct clk_hw *hw; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3770 | |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3771 | if (!clkspec) |
| 3772 | return ERR_PTR(-EINVAL); |
| 3773 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3774 | /* Check if we have such a provider in our array */ |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3775 | mutex_lock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3776 | list_for_each_entry(provider, &of_clk_providers, link) { |
Stephen Boyd | f155d15 | 2016-08-15 14:32:23 -0700 | [diff] [blame] | 3777 | if (provider->node == clkspec->np) { |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3778 | hw = __of_clk_get_hw_from_provider(provider, clkspec); |
Stephen Boyd | 0861e5b | 2016-02-05 17:38:26 -0800 | [diff] [blame] | 3779 | clk = __clk_create_clk(hw, dev_id, con_id); |
Stephen Boyd | f155d15 | 2016-08-15 14:32:23 -0700 | [diff] [blame] | 3780 | } |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3781 | |
Stephen Boyd | f155d15 | 2016-08-15 14:32:23 -0700 | [diff] [blame] | 3782 | if (!IS_ERR(clk)) { |
| 3783 | if (!__clk_get(clk)) { |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3784 | __clk_free_clk(clk); |
| 3785 | clk = ERR_PTR(-ENOENT); |
| 3786 | } |
| 3787 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3788 | break; |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3789 | } |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3790 | } |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3791 | mutex_unlock(&of_clk_mutex); |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3792 | |
| 3793 | return clk; |
| 3794 | } |
| 3795 | |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3796 | /** |
| 3797 | * of_clk_get_from_provider() - Lookup a clock from a clock provider |
| 3798 | * @clkspec: pointer to a clock specifier data structure |
| 3799 | * |
| 3800 | * This function looks up a struct clk from the registered list of clock |
| 3801 | * providers, an input is a clock specifier data structure as returned |
| 3802 | * from the of_parse_phandle_with_args() function call. |
| 3803 | */ |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3804 | struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) |
| 3805 | { |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3806 | return __of_clk_get_from_provider(clkspec, NULL, __func__); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3807 | } |
Andrew F. Davis | fb4dd22 | 2016-02-12 12:50:16 -0600 | [diff] [blame] | 3808 | EXPORT_SYMBOL_GPL(of_clk_get_from_provider); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3809 | |
Stephen Boyd | 929e7f3 | 2016-02-19 15:52:32 -0800 | [diff] [blame] | 3810 | /** |
| 3811 | * of_clk_get_parent_count() - Count the number of clocks a device node has |
| 3812 | * @np: device node to count |
| 3813 | * |
| 3814 | * Returns: The number of clocks that are possible parents of this node |
| 3815 | */ |
| 3816 | unsigned int of_clk_get_parent_count(struct device_node *np) |
Mike Turquette | f610274 | 2013-10-07 23:12:13 -0700 | [diff] [blame] | 3817 | { |
Stephen Boyd | 929e7f3 | 2016-02-19 15:52:32 -0800 | [diff] [blame] | 3818 | int count; |
| 3819 | |
| 3820 | count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); |
| 3821 | if (count < 0) |
| 3822 | return 0; |
| 3823 | |
| 3824 | return count; |
Mike Turquette | f610274 | 2013-10-07 23:12:13 -0700 | [diff] [blame] | 3825 | } |
| 3826 | EXPORT_SYMBOL_GPL(of_clk_get_parent_count); |
| 3827 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3828 | const char *of_clk_get_parent_name(struct device_node *np, int index) |
| 3829 | { |
| 3830 | struct of_phandle_args clkspec; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3831 | struct property *prop; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3832 | const char *clk_name; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3833 | const __be32 *vp; |
| 3834 | u32 pv; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3835 | int rc; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3836 | int count; |
Stephen Boyd | 0a4807c | 2015-10-14 14:03:07 -0700 | [diff] [blame] | 3837 | struct clk *clk; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3838 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3839 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, |
| 3840 | &clkspec); |
| 3841 | if (rc) |
| 3842 | return NULL; |
| 3843 | |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3844 | index = clkspec.args_count ? clkspec.args[0] : 0; |
| 3845 | count = 0; |
| 3846 | |
| 3847 | /* if there is an indices property, use it to transfer the index |
| 3848 | * specified into an array offset for the clock-output-names property. |
| 3849 | */ |
| 3850 | of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { |
| 3851 | if (index == pv) { |
| 3852 | index = count; |
| 3853 | break; |
| 3854 | } |
| 3855 | count++; |
| 3856 | } |
Masahiro Yamada | 8da411c | 2015-12-03 11:20:35 +0900 | [diff] [blame] | 3857 | /* We went off the end of 'clock-indices' without finding it */ |
| 3858 | if (prop && !vp) |
| 3859 | return NULL; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3860 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3861 | if (of_property_read_string_index(clkspec.np, "clock-output-names", |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3862 | index, |
Stephen Boyd | 0a4807c | 2015-10-14 14:03:07 -0700 | [diff] [blame] | 3863 | &clk_name) < 0) { |
| 3864 | /* |
| 3865 | * Best effort to get the name if the clock has been |
| 3866 | * registered with the framework. If the clock isn't |
| 3867 | * registered, we return the node name as the name of |
| 3868 | * the clock as long as #clock-cells = 0. |
| 3869 | */ |
| 3870 | clk = of_clk_get_from_provider(&clkspec); |
| 3871 | if (IS_ERR(clk)) { |
| 3872 | if (clkspec.args_count == 0) |
| 3873 | clk_name = clkspec.np->name; |
| 3874 | else |
| 3875 | clk_name = NULL; |
| 3876 | } else { |
| 3877 | clk_name = __clk_get_name(clk); |
| 3878 | clk_put(clk); |
| 3879 | } |
| 3880 | } |
| 3881 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3882 | |
| 3883 | of_node_put(clkspec.np); |
| 3884 | return clk_name; |
| 3885 | } |
| 3886 | EXPORT_SYMBOL_GPL(of_clk_get_parent_name); |
| 3887 | |
Dinh Nguyen | 2e61dfb | 2015-06-05 11:26:13 -0500 | [diff] [blame] | 3888 | /** |
| 3889 | * of_clk_parent_fill() - Fill @parents with names of @np's parents and return |
| 3890 | * number of parents |
| 3891 | * @np: Device node pointer associated with clock provider |
| 3892 | * @parents: pointer to char array that hold the parents' names |
| 3893 | * @size: size of the @parents array |
| 3894 | * |
| 3895 | * Return: number of parents for the clock node. |
| 3896 | */ |
| 3897 | int of_clk_parent_fill(struct device_node *np, const char **parents, |
| 3898 | unsigned int size) |
| 3899 | { |
| 3900 | unsigned int i = 0; |
| 3901 | |
| 3902 | while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL) |
| 3903 | i++; |
| 3904 | |
| 3905 | return i; |
| 3906 | } |
| 3907 | EXPORT_SYMBOL_GPL(of_clk_parent_fill); |
| 3908 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3909 | struct clock_provider { |
| 3910 | of_clk_init_cb_t clk_init_cb; |
| 3911 | struct device_node *np; |
| 3912 | struct list_head node; |
| 3913 | }; |
| 3914 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3915 | /* |
| 3916 | * This function looks for a parent clock. If there is one, then it |
| 3917 | * checks that the provider for this parent clock was initialized, in |
| 3918 | * this case the parent clock will be ready. |
| 3919 | */ |
| 3920 | static int parent_ready(struct device_node *np) |
| 3921 | { |
| 3922 | int i = 0; |
| 3923 | |
| 3924 | while (true) { |
| 3925 | struct clk *clk = of_clk_get(np, i); |
| 3926 | |
| 3927 | /* this parent is ready we can check the next one */ |
| 3928 | if (!IS_ERR(clk)) { |
| 3929 | clk_put(clk); |
| 3930 | i++; |
| 3931 | continue; |
| 3932 | } |
| 3933 | |
| 3934 | /* at least one parent is not ready, we exit now */ |
| 3935 | if (PTR_ERR(clk) == -EPROBE_DEFER) |
| 3936 | return 0; |
| 3937 | |
| 3938 | /* |
| 3939 | * Here we make assumption that the device tree is |
| 3940 | * written correctly. So an error means that there is |
| 3941 | * no more parent. As we didn't exit yet, then the |
| 3942 | * previous parent are ready. If there is no clock |
| 3943 | * parent, no need to wait for them, then we can |
| 3944 | * consider their absence as being ready |
| 3945 | */ |
| 3946 | return 1; |
| 3947 | } |
| 3948 | } |
| 3949 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3950 | /** |
Lee Jones | d56f899 | 2016-02-11 13:19:11 -0800 | [diff] [blame] | 3951 | * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree |
| 3952 | * @np: Device node pointer associated with clock provider |
| 3953 | * @index: clock index |
Geert Uytterhoeven | f7ae750 | 2018-01-03 12:06:14 +0100 | [diff] [blame] | 3954 | * @flags: pointer to top-level framework flags |
Lee Jones | d56f899 | 2016-02-11 13:19:11 -0800 | [diff] [blame] | 3955 | * |
| 3956 | * Detects if the clock-critical property exists and, if so, sets the |
| 3957 | * corresponding CLK_IS_CRITICAL flag. |
| 3958 | * |
| 3959 | * Do not use this function. It exists only for legacy Device Tree |
| 3960 | * bindings, such as the one-clock-per-node style that are outdated. |
| 3961 | * Those bindings typically put all clock data into .dts and the Linux |
| 3962 | * driver has no clock data, thus making it impossible to set this flag |
| 3963 | * correctly from the driver. Only those drivers may call |
| 3964 | * of_clk_detect_critical from their setup functions. |
| 3965 | * |
| 3966 | * Return: error code or zero on success |
| 3967 | */ |
| 3968 | int of_clk_detect_critical(struct device_node *np, |
| 3969 | int index, unsigned long *flags) |
| 3970 | { |
| 3971 | struct property *prop; |
| 3972 | const __be32 *cur; |
| 3973 | uint32_t idx; |
| 3974 | |
| 3975 | if (!np || !flags) |
| 3976 | return -EINVAL; |
| 3977 | |
| 3978 | of_property_for_each_u32(np, "clock-critical", prop, cur, idx) |
| 3979 | if (index == idx) |
| 3980 | *flags |= CLK_IS_CRITICAL; |
| 3981 | |
| 3982 | return 0; |
| 3983 | } |
| 3984 | |
| 3985 | /** |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3986 | * of_clk_init() - Scan and init clock providers from the DT |
| 3987 | * @matches: array of compatible values and init functions for providers. |
| 3988 | * |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3989 | * This function scans the device tree for matching clock providers |
Sylwester Nawrocki | e5ca8fb4 | 2014-03-27 12:08:36 +0100 | [diff] [blame] | 3990 | * and calls their initialization functions. It also does it by trying |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3991 | * to follow the dependencies. |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3992 | */ |
| 3993 | void __init of_clk_init(const struct of_device_id *matches) |
| 3994 | { |
Alex Elder | 7f7ed58 | 2013-08-22 11:31:31 -0500 | [diff] [blame] | 3995 | const struct of_device_id *match; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3996 | struct device_node *np; |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3997 | struct clock_provider *clk_provider, *next; |
| 3998 | bool is_init_done; |
| 3999 | bool force = false; |
Stephen Boyd | 2573a02 | 2015-07-06 16:50:00 -0700 | [diff] [blame] | 4000 | LIST_HEAD(clk_provider_list); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 4001 | |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 4002 | if (!matches) |
Tero Kristo | 819b486 | 2013-10-22 11:39:36 +0300 | [diff] [blame] | 4003 | matches = &__clk_of_table; |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 4004 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4005 | /* First prepare the list of the clocks providers */ |
Alex Elder | 7f7ed58 | 2013-08-22 11:31:31 -0500 | [diff] [blame] | 4006 | for_each_matching_node_and_match(np, matches, &match) { |
Stephen Boyd | 2e3b19f | 2015-07-06 16:48:19 -0700 | [diff] [blame] | 4007 | struct clock_provider *parent; |
| 4008 | |
Geert Uytterhoeven | 3e5dd6f | 2016-02-26 16:54:31 +0100 | [diff] [blame] | 4009 | if (!of_device_is_available(np)) |
| 4010 | continue; |
| 4011 | |
Stephen Boyd | 2e3b19f | 2015-07-06 16:48:19 -0700 | [diff] [blame] | 4012 | parent = kzalloc(sizeof(*parent), GFP_KERNEL); |
| 4013 | if (!parent) { |
| 4014 | list_for_each_entry_safe(clk_provider, next, |
| 4015 | &clk_provider_list, node) { |
| 4016 | list_del(&clk_provider->node); |
Julia Lawall | 6bc9d9d | 2015-10-21 22:41:36 +0200 | [diff] [blame] | 4017 | of_node_put(clk_provider->np); |
Stephen Boyd | 2e3b19f | 2015-07-06 16:48:19 -0700 | [diff] [blame] | 4018 | kfree(clk_provider); |
| 4019 | } |
Julia Lawall | 6bc9d9d | 2015-10-21 22:41:36 +0200 | [diff] [blame] | 4020 | of_node_put(np); |
Stephen Boyd | 2e3b19f | 2015-07-06 16:48:19 -0700 | [diff] [blame] | 4021 | return; |
| 4022 | } |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4023 | |
| 4024 | parent->clk_init_cb = match->data; |
Julia Lawall | 6bc9d9d | 2015-10-21 22:41:36 +0200 | [diff] [blame] | 4025 | parent->np = of_node_get(np); |
Sylwester Nawrocki | 3f6d439 | 2014-03-27 11:43:32 +0100 | [diff] [blame] | 4026 | list_add_tail(&parent->node, &clk_provider_list); |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | while (!list_empty(&clk_provider_list)) { |
| 4030 | is_init_done = false; |
| 4031 | list_for_each_entry_safe(clk_provider, next, |
| 4032 | &clk_provider_list, node) { |
| 4033 | if (force || parent_ready(clk_provider->np)) { |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 4034 | |
Ricardo Ribalda Delgado | 989eafd | 2016-07-05 18:23:32 +0200 | [diff] [blame] | 4035 | /* Don't populate platform devices */ |
| 4036 | of_node_set_flag(clk_provider->np, |
| 4037 | OF_POPULATED); |
| 4038 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4039 | clk_provider->clk_init_cb(clk_provider->np); |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 4040 | of_clk_set_defaults(clk_provider->np, true); |
| 4041 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4042 | list_del(&clk_provider->node); |
Julia Lawall | 6bc9d9d | 2015-10-21 22:41:36 +0200 | [diff] [blame] | 4043 | of_node_put(clk_provider->np); |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4044 | kfree(clk_provider); |
| 4045 | is_init_done = true; |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | /* |
Sylwester Nawrocki | e5ca8fb4 | 2014-03-27 12:08:36 +0100 | [diff] [blame] | 4050 | * We didn't manage to initialize any of the |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 4051 | * remaining providers during the last loop, so now we |
| 4052 | * initialize all the remaining ones unconditionally |
| 4053 | * in case the clock parent was not mandatory |
| 4054 | */ |
| 4055 | if (!is_init_done) |
| 4056 | force = true; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 4057 | } |
| 4058 | } |
| 4059 | #endif |