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