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