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