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