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