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