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