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