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