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