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