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> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Standard functionality for the common clock API. See Documentation/clk.txt |
| 10 | */ |
| 11 | |
Stephen Boyd | 3c37311 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 12 | #include <linux/clk.h> |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 13 | #include <linux/clk-provider.h> |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 14 | #include <linux/clk/clk-conf.h> |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/slab.h> |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 21 | #include <linux/of.h> |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 22 | #include <linux/device.h> |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 23 | #include <linux/init.h> |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 24 | #include <linux/sched.h> |
Stephen Boyd | 562ef0b | 2015-05-01 12:16:14 -0700 | [diff] [blame] | 25 | #include <linux/clkdev.h> |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 26 | |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 27 | #include "clk.h" |
| 28 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 29 | static DEFINE_SPINLOCK(enable_lock); |
| 30 | static DEFINE_MUTEX(prepare_lock); |
| 31 | |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 32 | static struct task_struct *prepare_owner; |
| 33 | static struct task_struct *enable_owner; |
| 34 | |
| 35 | static int prepare_refcnt; |
| 36 | static int enable_refcnt; |
| 37 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 38 | static HLIST_HEAD(clk_root_list); |
| 39 | static HLIST_HEAD(clk_orphan_list); |
| 40 | static LIST_HEAD(clk_notifier_list); |
| 41 | |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 42 | /*** private data structures ***/ |
| 43 | |
| 44 | struct clk_core { |
| 45 | const char *name; |
| 46 | const struct clk_ops *ops; |
| 47 | struct clk_hw *hw; |
| 48 | struct module *owner; |
| 49 | struct clk_core *parent; |
| 50 | const char **parent_names; |
| 51 | struct clk_core **parents; |
| 52 | u8 num_parents; |
| 53 | u8 new_parent_index; |
| 54 | unsigned long rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 55 | unsigned long req_rate; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 56 | unsigned long new_rate; |
| 57 | struct clk_core *new_parent; |
| 58 | struct clk_core *new_child; |
| 59 | unsigned long flags; |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 60 | bool orphan; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 61 | unsigned int enable_count; |
| 62 | unsigned int prepare_count; |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 63 | unsigned long min_rate; |
| 64 | unsigned long max_rate; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 65 | unsigned long accuracy; |
| 66 | int phase; |
| 67 | struct hlist_head children; |
| 68 | struct hlist_node child_node; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 69 | struct hlist_head clks; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 70 | unsigned int notifier_count; |
| 71 | #ifdef CONFIG_DEBUG_FS |
| 72 | struct dentry *dentry; |
Maxime Coquelin | 8c9a8a8 | 2015-06-10 13:28:27 +0200 | [diff] [blame] | 73 | struct hlist_node debug_node; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 74 | #endif |
| 75 | struct kref ref; |
| 76 | }; |
| 77 | |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 78 | #define CREATE_TRACE_POINTS |
| 79 | #include <trace/events/clk.h> |
| 80 | |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 81 | struct clk { |
| 82 | struct clk_core *core; |
| 83 | const char *dev_id; |
| 84 | const char *con_id; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 85 | unsigned long min_rate; |
| 86 | unsigned long max_rate; |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 87 | struct hlist_node clks_node; |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 90 | /*** locking ***/ |
| 91 | static void clk_prepare_lock(void) |
| 92 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 93 | if (!mutex_trylock(&prepare_lock)) { |
| 94 | if (prepare_owner == current) { |
| 95 | prepare_refcnt++; |
| 96 | return; |
| 97 | } |
| 98 | mutex_lock(&prepare_lock); |
| 99 | } |
| 100 | WARN_ON_ONCE(prepare_owner != NULL); |
| 101 | WARN_ON_ONCE(prepare_refcnt != 0); |
| 102 | prepare_owner = current; |
| 103 | prepare_refcnt = 1; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void clk_prepare_unlock(void) |
| 107 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 108 | WARN_ON_ONCE(prepare_owner != current); |
| 109 | WARN_ON_ONCE(prepare_refcnt == 0); |
| 110 | |
| 111 | if (--prepare_refcnt) |
| 112 | return; |
| 113 | prepare_owner = NULL; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 114 | mutex_unlock(&prepare_lock); |
| 115 | } |
| 116 | |
| 117 | static unsigned long clk_enable_lock(void) |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 118 | __acquires(enable_lock) |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 119 | { |
| 120 | unsigned long flags; |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 121 | |
| 122 | if (!spin_trylock_irqsave(&enable_lock, flags)) { |
| 123 | if (enable_owner == current) { |
| 124 | enable_refcnt++; |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 125 | __acquire(enable_lock); |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 126 | return flags; |
| 127 | } |
| 128 | spin_lock_irqsave(&enable_lock, flags); |
| 129 | } |
| 130 | WARN_ON_ONCE(enable_owner != NULL); |
| 131 | WARN_ON_ONCE(enable_refcnt != 0); |
| 132 | enable_owner = current; |
| 133 | enable_refcnt = 1; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 134 | return flags; |
| 135 | } |
| 136 | |
| 137 | static void clk_enable_unlock(unsigned long flags) |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 138 | __releases(enable_lock) |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 139 | { |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 140 | WARN_ON_ONCE(enable_owner != current); |
| 141 | WARN_ON_ONCE(enable_refcnt == 0); |
| 142 | |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 143 | if (--enable_refcnt) { |
| 144 | __release(enable_lock); |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 145 | return; |
Stephen Boyd | a57aa18 | 2015-07-24 12:24:48 -0700 | [diff] [blame] | 146 | } |
Mike Turquette | 533ddeb | 2013-03-28 13:59:02 -0700 | [diff] [blame] | 147 | enable_owner = NULL; |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 148 | spin_unlock_irqrestore(&enable_lock, flags); |
| 149 | } |
| 150 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 151 | static bool clk_core_is_prepared(struct clk_core *core) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 152 | { |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 153 | /* |
| 154 | * .is_prepared is optional for clocks that can prepare |
| 155 | * fall back to software usage counter if it is missing |
| 156 | */ |
| 157 | if (!core->ops->is_prepared) |
| 158 | return core->prepare_count; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 159 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 160 | return core->ops->is_prepared(core->hw); |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 161 | } |
| 162 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 163 | static bool clk_core_is_enabled(struct clk_core *core) |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 164 | { |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 165 | /* |
| 166 | * .is_enabled is only mandatory for clocks that gate |
| 167 | * fall back to software usage counter if .is_enabled is missing |
| 168 | */ |
| 169 | if (!core->ops->is_enabled) |
| 170 | return core->enable_count; |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 171 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 172 | return core->ops->is_enabled(core->hw); |
Prashant Gaikwad | 1af599d | 2012-12-26 19:16:22 +0530 | [diff] [blame] | 173 | } |
| 174 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 175 | static void clk_unprepare_unused_subtree(struct clk_core *core) |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 176 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 177 | struct clk_core *child; |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 178 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 179 | lockdep_assert_held(&prepare_lock); |
| 180 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 181 | hlist_for_each_entry(child, &core->children, child_node) |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 182 | clk_unprepare_unused_subtree(child); |
| 183 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 184 | if (core->prepare_count) |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 185 | return; |
| 186 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 187 | if (core->flags & CLK_IGNORE_UNUSED) |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 188 | return; |
| 189 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 190 | if (clk_core_is_prepared(core)) { |
| 191 | trace_clk_unprepare(core); |
| 192 | if (core->ops->unprepare_unused) |
| 193 | core->ops->unprepare_unused(core->hw); |
| 194 | else if (core->ops->unprepare) |
| 195 | core->ops->unprepare(core->hw); |
| 196 | trace_clk_unprepare_complete(core); |
Ulf Hansson | 3cc8247 | 2013-03-12 20:26:04 +0100 | [diff] [blame] | 197 | } |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 200 | static void clk_disable_unused_subtree(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 201 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 202 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 203 | unsigned long flags; |
| 204 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 205 | lockdep_assert_held(&prepare_lock); |
| 206 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 207 | hlist_for_each_entry(child, &core->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 208 | clk_disable_unused_subtree(child); |
| 209 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 210 | flags = clk_enable_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 211 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 212 | if (core->enable_count) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 213 | goto unlock_out; |
| 214 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 215 | if (core->flags & CLK_IGNORE_UNUSED) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 216 | goto unlock_out; |
| 217 | |
Mike Turquette | 7c045a5 | 2012-12-04 11:00:35 -0800 | [diff] [blame] | 218 | /* |
| 219 | * some gate clocks have special needs during the disable-unused |
| 220 | * sequence. call .disable_unused if available, otherwise fall |
| 221 | * back to .disable |
| 222 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 223 | if (clk_core_is_enabled(core)) { |
| 224 | trace_clk_disable(core); |
| 225 | if (core->ops->disable_unused) |
| 226 | core->ops->disable_unused(core->hw); |
| 227 | else if (core->ops->disable) |
| 228 | core->ops->disable(core->hw); |
| 229 | trace_clk_disable_complete(core); |
Mike Turquette | 7c045a5 | 2012-12-04 11:00:35 -0800 | [diff] [blame] | 230 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 231 | |
| 232 | unlock_out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 233 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Olof Johansson | 1e43525 | 2013-04-27 14:10:18 -0700 | [diff] [blame] | 236 | static bool clk_ignore_unused; |
| 237 | static int __init clk_ignore_unused_setup(char *__unused) |
| 238 | { |
| 239 | clk_ignore_unused = true; |
| 240 | return 1; |
| 241 | } |
| 242 | __setup("clk_ignore_unused", clk_ignore_unused_setup); |
| 243 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 244 | static int clk_disable_unused(void) |
| 245 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 246 | struct clk_core *core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 247 | |
Olof Johansson | 1e43525 | 2013-04-27 14:10:18 -0700 | [diff] [blame] | 248 | if (clk_ignore_unused) { |
| 249 | pr_warn("clk: Not disabling unused clocks\n"); |
| 250 | return 0; |
| 251 | } |
| 252 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 253 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 254 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 255 | hlist_for_each_entry(core, &clk_root_list, child_node) |
| 256 | clk_disable_unused_subtree(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 257 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 258 | hlist_for_each_entry(core, &clk_orphan_list, child_node) |
| 259 | clk_disable_unused_subtree(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 260 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 261 | hlist_for_each_entry(core, &clk_root_list, child_node) |
| 262 | clk_unprepare_unused_subtree(core); |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 263 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 264 | hlist_for_each_entry(core, &clk_orphan_list, child_node) |
| 265 | clk_unprepare_unused_subtree(core); |
Ulf Hansson | 1c155b3 | 2013-03-12 20:26:03 +0100 | [diff] [blame] | 266 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 267 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
Saravana Kannan | d41d580 | 2013-05-09 11:35:01 -0700 | [diff] [blame] | 271 | late_initcall_sync(clk_disable_unused); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 272 | |
| 273 | /*** helper functions ***/ |
| 274 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 275 | const char *__clk_get_name(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 276 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 277 | return !clk ? NULL : clk->core->name; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 278 | } |
Niels de Vos | 4895084 | 2012-12-13 13:12:25 +0100 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(__clk_get_name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 280 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 281 | const char *clk_hw_get_name(struct clk_hw *hw) |
| 282 | { |
| 283 | return hw->core->name; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(clk_hw_get_name); |
| 286 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 287 | struct clk_hw *__clk_get_hw(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 288 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 289 | return !clk ? NULL : clk->core->hw; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 290 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(__clk_get_hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 292 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 293 | u8 __clk_get_num_parents(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 294 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 295 | return !clk ? 0 : clk->core->num_parents; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 296 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 297 | EXPORT_SYMBOL_GPL(__clk_get_num_parents); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 298 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 299 | unsigned int clk_hw_get_num_parents(struct clk_hw *hw) |
| 300 | { |
| 301 | return hw->core->num_parents; |
| 302 | } |
| 303 | EXPORT_SYMBOL_GPL(clk_hw_get_num_parents); |
| 304 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 305 | struct clk *__clk_get_parent(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 306 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 307 | if (!clk) |
| 308 | return NULL; |
| 309 | |
| 310 | /* TODO: Create a per-user clk and change callers to call clk_put */ |
| 311 | return !clk->core->parent ? NULL : clk->core->parent->hw->clk; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 312 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(__clk_get_parent); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 314 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 315 | struct clk_hw *clk_hw_get_parent(struct clk_hw *hw) |
| 316 | { |
| 317 | return hw->core->parent ? hw->core->parent->hw : NULL; |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(clk_hw_get_parent); |
| 320 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 321 | static struct clk_core *__clk_lookup_subtree(const char *name, |
| 322 | struct clk_core *core) |
| 323 | { |
| 324 | struct clk_core *child; |
| 325 | struct clk_core *ret; |
| 326 | |
| 327 | if (!strcmp(core->name, name)) |
| 328 | return core; |
| 329 | |
| 330 | hlist_for_each_entry(child, &core->children, child_node) { |
| 331 | ret = __clk_lookup_subtree(name, child); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | static struct clk_core *clk_core_lookup(const char *name) |
| 340 | { |
| 341 | struct clk_core *root_clk; |
| 342 | struct clk_core *ret; |
| 343 | |
| 344 | if (!name) |
| 345 | return NULL; |
| 346 | |
| 347 | /* search the 'proper' clk tree first */ |
| 348 | hlist_for_each_entry(root_clk, &clk_root_list, child_node) { |
| 349 | ret = __clk_lookup_subtree(name, root_clk); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | /* if not found, then search the orphan tree */ |
| 355 | hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { |
| 356 | ret = __clk_lookup_subtree(name, root_clk); |
| 357 | if (ret) |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | return NULL; |
| 362 | } |
| 363 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 364 | 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] | 365 | u8 index) |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 366 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 367 | if (!core || index >= core->num_parents) |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 368 | return NULL; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 369 | else if (!core->parents) |
| 370 | return clk_core_lookup(core->parent_names[index]); |
| 371 | else if (!core->parents[index]) |
| 372 | return core->parents[index] = |
| 373 | clk_core_lookup(core->parent_names[index]); |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 374 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 375 | return core->parents[index]; |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 376 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 377 | |
| 378 | struct clk *clk_get_parent_by_index(struct clk *clk, u8 index) |
| 379 | { |
| 380 | struct clk_core *parent; |
| 381 | |
| 382 | if (!clk) |
| 383 | return NULL; |
| 384 | |
| 385 | parent = clk_core_get_parent_by_index(clk->core, index); |
| 386 | |
| 387 | return !parent ? NULL : parent->hw->clk; |
| 388 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 389 | EXPORT_SYMBOL_GPL(clk_get_parent_by_index); |
James Hogan | 7ef3dcc | 2013-07-29 12:24:58 +0100 | [diff] [blame] | 390 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 391 | struct clk_hw *clk_hw_get_parent_by_index(struct clk_hw *hw, unsigned int index) |
| 392 | { |
| 393 | struct clk_core *parent; |
| 394 | |
| 395 | parent = clk_core_get_parent_by_index(hw->core, index); |
| 396 | |
| 397 | return !parent ? NULL : parent->hw; |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index); |
| 400 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 401 | unsigned int __clk_get_enable_count(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 402 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 403 | return !clk ? 0 : clk->core->enable_count; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 406 | static unsigned long clk_core_get_rate_nolock(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 407 | { |
| 408 | unsigned long ret; |
| 409 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 410 | if (!core) { |
Rajendra Nayak | 34e44fe | 2012-03-26 19:01:48 +0530 | [diff] [blame] | 411 | ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 412 | goto out; |
| 413 | } |
| 414 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 415 | ret = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 416 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 417 | if (core->flags & CLK_IS_ROOT) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 418 | goto out; |
| 419 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 420 | if (!core->parent) |
Rajendra Nayak | 34e44fe | 2012-03-26 19:01:48 +0530 | [diff] [blame] | 421 | ret = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 422 | |
| 423 | out: |
| 424 | return ret; |
| 425 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 426 | |
| 427 | unsigned long __clk_get_rate(struct clk *clk) |
| 428 | { |
| 429 | if (!clk) |
| 430 | return 0; |
| 431 | |
| 432 | return clk_core_get_rate_nolock(clk->core); |
| 433 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 434 | EXPORT_SYMBOL_GPL(__clk_get_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 435 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 436 | unsigned long clk_hw_get_rate(struct clk_hw *hw) |
| 437 | { |
| 438 | return clk_core_get_rate_nolock(hw->core); |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(clk_hw_get_rate); |
| 441 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 442 | static unsigned long __clk_get_accuracy(struct clk_core *core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 443 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 444 | if (!core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 445 | return 0; |
| 446 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 447 | return core->accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Russ Dill | 65800b2 | 2012-11-26 11:20:09 -0800 | [diff] [blame] | 450 | unsigned long __clk_get_flags(struct clk *clk) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 451 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 452 | return !clk ? 0 : clk->core->flags; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 453 | } |
Thierry Reding | b05c683 | 2013-09-03 09:43:51 +0200 | [diff] [blame] | 454 | EXPORT_SYMBOL_GPL(__clk_get_flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 455 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 456 | unsigned long clk_hw_get_flags(struct clk_hw *hw) |
| 457 | { |
| 458 | return hw->core->flags; |
| 459 | } |
| 460 | EXPORT_SYMBOL_GPL(clk_hw_get_flags); |
| 461 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 462 | bool __clk_is_prepared(struct clk *clk) |
| 463 | { |
| 464 | if (!clk) |
| 465 | return false; |
| 466 | |
| 467 | return clk_core_is_prepared(clk->core); |
| 468 | } |
| 469 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 470 | bool clk_hw_is_prepared(struct clk_hw *hw) |
| 471 | { |
| 472 | return clk_core_is_prepared(hw->core); |
| 473 | } |
| 474 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 475 | bool __clk_is_enabled(struct clk *clk) |
| 476 | { |
| 477 | if (!clk) |
| 478 | return false; |
| 479 | |
| 480 | return clk_core_is_enabled(clk->core); |
| 481 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 482 | EXPORT_SYMBOL_GPL(__clk_is_enabled); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 483 | |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 484 | static bool mux_is_better_rate(unsigned long rate, unsigned long now, |
| 485 | unsigned long best, unsigned long flags) |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 486 | { |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 487 | if (flags & CLK_MUX_ROUND_CLOSEST) |
| 488 | return abs(now - rate) < abs(best - rate); |
| 489 | |
| 490 | return now <= rate && now > best; |
| 491 | } |
| 492 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 493 | static int |
| 494 | 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] | 495 | unsigned long flags) |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 496 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 497 | struct clk_core *core = hw->core, *parent, *best_parent = NULL; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 498 | int i, num_parents, ret; |
| 499 | unsigned long best = 0; |
| 500 | struct clk_rate_request parent_req = *req; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 501 | |
| 502 | /* if NO_REPARENT flag set, pass through to current parent */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 503 | if (core->flags & CLK_SET_RATE_NO_REPARENT) { |
| 504 | parent = core->parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 505 | if (core->flags & CLK_SET_RATE_PARENT) { |
| 506 | ret = __clk_determine_rate(parent ? parent->hw : NULL, |
| 507 | &parent_req); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | |
| 511 | best = parent_req.rate; |
| 512 | } else if (parent) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 513 | best = clk_core_get_rate_nolock(parent); |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 514 | } else { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 515 | best = clk_core_get_rate_nolock(core); |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 516 | } |
| 517 | |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | /* find the parent that can provide the fastest rate <= rate */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 522 | num_parents = core->num_parents; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 523 | for (i = 0; i < num_parents; i++) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 524 | parent = clk_core_get_parent_by_index(core, i); |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 525 | if (!parent) |
| 526 | continue; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 527 | |
| 528 | if (core->flags & CLK_SET_RATE_PARENT) { |
| 529 | parent_req = *req; |
| 530 | ret = __clk_determine_rate(parent->hw, &parent_req); |
| 531 | if (ret) |
| 532 | continue; |
| 533 | } else { |
| 534 | parent_req.rate = clk_core_get_rate_nolock(parent); |
| 535 | } |
| 536 | |
| 537 | if (mux_is_better_rate(req->rate, parent_req.rate, |
| 538 | best, flags)) { |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 539 | best_parent = parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 540 | best = parent_req.rate; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Boris Brezillon | 57d866e | 2015-07-09 22:39:38 +0200 | [diff] [blame] | 544 | if (!best_parent) |
| 545 | return -EINVAL; |
| 546 | |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 547 | out: |
| 548 | if (best_parent) |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 549 | req->best_parent_hw = best_parent->hw; |
| 550 | req->best_parent_rate = best; |
| 551 | req->rate = best; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 552 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 553 | return 0; |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 554 | } |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 555 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 556 | struct clk *__clk_lookup(const char *name) |
| 557 | { |
| 558 | struct clk_core *core = clk_core_lookup(name); |
| 559 | |
| 560 | return !core ? NULL : core->hw->clk; |
| 561 | } |
| 562 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 563 | static void clk_core_get_boundaries(struct clk_core *core, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 564 | unsigned long *min_rate, |
| 565 | unsigned long *max_rate) |
| 566 | { |
| 567 | struct clk *clk_user; |
| 568 | |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 569 | *min_rate = core->min_rate; |
| 570 | *max_rate = core->max_rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 571 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 572 | hlist_for_each_entry(clk_user, &core->clks, clks_node) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 573 | *min_rate = max(*min_rate, clk_user->min_rate); |
| 574 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 575 | hlist_for_each_entry(clk_user, &core->clks, clks_node) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 576 | *max_rate = min(*max_rate, clk_user->max_rate); |
| 577 | } |
| 578 | |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 579 | void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate, |
| 580 | unsigned long max_rate) |
| 581 | { |
| 582 | hw->core->min_rate = min_rate; |
| 583 | hw->core->max_rate = max_rate; |
| 584 | } |
| 585 | EXPORT_SYMBOL_GPL(clk_hw_set_rate_range); |
| 586 | |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 587 | /* |
| 588 | * Helper for finding best parent to provide a given frequency. This can be used |
| 589 | * directly as a determine_rate callback (e.g. for a mux), or from a more |
| 590 | * complex clock that may combine a mux with other operations. |
| 591 | */ |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 592 | int __clk_mux_determine_rate(struct clk_hw *hw, |
| 593 | struct clk_rate_request *req) |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 594 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 595 | return clk_mux_determine_rate_flags(hw, req, 0); |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 596 | } |
Stephen Boyd | 0b7f04b | 2014-01-17 19:47:17 -0800 | [diff] [blame] | 597 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate); |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 598 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 599 | int __clk_mux_determine_rate_closest(struct clk_hw *hw, |
| 600 | struct clk_rate_request *req) |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 601 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 602 | return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST); |
Stephen Boyd | 15a02c1 | 2015-01-19 18:05:28 -0800 | [diff] [blame] | 603 | } |
| 604 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest); |
| 605 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 606 | /*** clk api ***/ |
| 607 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 608 | static void clk_core_unprepare(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 609 | { |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 610 | lockdep_assert_held(&prepare_lock); |
| 611 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 612 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 613 | return; |
| 614 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 615 | if (WARN_ON(core->prepare_count == 0)) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 616 | return; |
| 617 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 618 | if (--core->prepare_count > 0) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 619 | return; |
| 620 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 621 | WARN_ON(core->enable_count > 0); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 622 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 623 | trace_clk_unprepare(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 624 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 625 | if (core->ops->unprepare) |
| 626 | core->ops->unprepare(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 627 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 628 | trace_clk_unprepare_complete(core); |
| 629 | clk_core_unprepare(core->parent); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /** |
| 633 | * clk_unprepare - undo preparation of a clock source |
Peter Meerwald | 24ee1a0 | 2013-06-29 15:14:19 +0200 | [diff] [blame] | 634 | * @clk: the clk being unprepared |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 635 | * |
| 636 | * clk_unprepare may sleep, which differentiates it from clk_disable. In a |
| 637 | * simple case, clk_unprepare can be used instead of clk_disable to gate a clk |
| 638 | * if the operation may sleep. One example is a clk which is accessed over |
| 639 | * I2c. In the complex case a clk gate operation may require a fast and a slow |
| 640 | * part. It is this reason that clk_unprepare and clk_disable are not mutually |
| 641 | * exclusive. In fact clk_disable must be called before clk_unprepare. |
| 642 | */ |
| 643 | void clk_unprepare(struct clk *clk) |
| 644 | { |
Stephen Boyd | 63589e9 | 2014-03-26 16:06:37 -0700 | [diff] [blame] | 645 | if (IS_ERR_OR_NULL(clk)) |
| 646 | return; |
| 647 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 648 | clk_prepare_lock(); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 649 | clk_core_unprepare(clk->core); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 650 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 651 | } |
| 652 | EXPORT_SYMBOL_GPL(clk_unprepare); |
| 653 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 654 | static int clk_core_prepare(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 655 | { |
| 656 | int ret = 0; |
| 657 | |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 658 | lockdep_assert_held(&prepare_lock); |
| 659 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 660 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 663 | if (core->prepare_count == 0) { |
| 664 | ret = clk_core_prepare(core->parent); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 665 | if (ret) |
| 666 | return ret; |
| 667 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 668 | trace_clk_prepare(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 669 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 670 | if (core->ops->prepare) |
| 671 | ret = core->ops->prepare(core->hw); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 672 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 673 | trace_clk_prepare_complete(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 674 | |
| 675 | if (ret) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 676 | clk_core_unprepare(core->parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 677 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 681 | core->prepare_count++; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * clk_prepare - prepare a clock source |
| 688 | * @clk: the clk being prepared |
| 689 | * |
| 690 | * clk_prepare may sleep, which differentiates it from clk_enable. In a simple |
| 691 | * case, clk_prepare can be used instead of clk_enable to ungate a clk if the |
| 692 | * operation may sleep. One example is a clk which is accessed over I2c. In |
| 693 | * the complex case a clk ungate operation may require a fast and a slow part. |
| 694 | * It is this reason that clk_prepare and clk_enable are not mutually |
| 695 | * exclusive. In fact clk_prepare must be called before clk_enable. |
| 696 | * Returns 0 on success, -EERROR otherwise. |
| 697 | */ |
| 698 | int clk_prepare(struct clk *clk) |
| 699 | { |
| 700 | int ret; |
| 701 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 702 | if (!clk) |
| 703 | return 0; |
| 704 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 705 | clk_prepare_lock(); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 706 | ret = clk_core_prepare(clk->core); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 707 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | EXPORT_SYMBOL_GPL(clk_prepare); |
| 712 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 713 | static void clk_core_disable(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 714 | { |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 715 | lockdep_assert_held(&enable_lock); |
| 716 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 717 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 718 | return; |
| 719 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 720 | if (WARN_ON(core->enable_count == 0)) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 721 | return; |
| 722 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 723 | if (--core->enable_count > 0) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 724 | return; |
| 725 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 726 | trace_clk_disable(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 727 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 728 | if (core->ops->disable) |
| 729 | core->ops->disable(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 730 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 731 | trace_clk_disable_complete(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 732 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 733 | clk_core_disable(core->parent); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 736 | /** |
| 737 | * clk_disable - gate a clock |
| 738 | * @clk: the clk being gated |
| 739 | * |
| 740 | * clk_disable must not sleep, which differentiates it from clk_unprepare. In |
| 741 | * a simple case, clk_disable can be used instead of clk_unprepare to gate a |
| 742 | * clk if the operation is fast and will never sleep. One example is a |
| 743 | * SoC-internal clk which is controlled via simple register writes. In the |
| 744 | * complex case a clk gate operation may require a fast and a slow part. It is |
| 745 | * this reason that clk_unprepare and clk_disable are not mutually exclusive. |
| 746 | * In fact clk_disable must be called before clk_unprepare. |
| 747 | */ |
| 748 | void clk_disable(struct clk *clk) |
| 749 | { |
| 750 | unsigned long flags; |
| 751 | |
Stephen Boyd | 63589e9 | 2014-03-26 16:06:37 -0700 | [diff] [blame] | 752 | if (IS_ERR_OR_NULL(clk)) |
| 753 | return; |
| 754 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 755 | flags = clk_enable_lock(); |
Dong Aisheng | 864e160 | 2015-04-30 14:02:19 -0700 | [diff] [blame] | 756 | clk_core_disable(clk->core); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 757 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 758 | } |
| 759 | EXPORT_SYMBOL_GPL(clk_disable); |
| 760 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 761 | static int clk_core_enable(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 762 | { |
| 763 | int ret = 0; |
| 764 | |
Stephen Boyd | a633472 | 2015-05-06 17:00:54 -0700 | [diff] [blame] | 765 | lockdep_assert_held(&enable_lock); |
| 766 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 767 | if (!core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 768 | return 0; |
| 769 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 770 | if (WARN_ON(core->prepare_count == 0)) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 771 | return -ESHUTDOWN; |
| 772 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 773 | if (core->enable_count == 0) { |
| 774 | ret = clk_core_enable(core->parent); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 775 | |
| 776 | if (ret) |
| 777 | return ret; |
| 778 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 779 | trace_clk_enable(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 780 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 781 | if (core->ops->enable) |
| 782 | ret = core->ops->enable(core->hw); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 783 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 784 | trace_clk_enable_complete(core); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 785 | |
| 786 | if (ret) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 787 | clk_core_disable(core->parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 788 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 792 | core->enable_count++; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * clk_enable - ungate a clock |
| 798 | * @clk: the clk being ungated |
| 799 | * |
| 800 | * clk_enable must not sleep, which differentiates it from clk_prepare. In a |
| 801 | * simple case, clk_enable can be used instead of clk_prepare to ungate a clk |
| 802 | * if the operation will never sleep. One example is a SoC-internal clk which |
| 803 | * is controlled via simple register writes. In the complex case a clk ungate |
| 804 | * operation may require a fast and a slow part. It is this reason that |
| 805 | * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare |
| 806 | * must be called before clk_enable. Returns 0 on success, -EERROR |
| 807 | * otherwise. |
| 808 | */ |
| 809 | int clk_enable(struct clk *clk) |
| 810 | { |
| 811 | unsigned long flags; |
| 812 | int ret; |
| 813 | |
Dong Aisheng | 864e160 | 2015-04-30 14:02:19 -0700 | [diff] [blame] | 814 | if (!clk) |
| 815 | return 0; |
| 816 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 817 | flags = clk_enable_lock(); |
Dong Aisheng | 864e160 | 2015-04-30 14:02:19 -0700 | [diff] [blame] | 818 | ret = clk_core_enable(clk->core); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 819 | clk_enable_unlock(flags); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 820 | |
| 821 | return ret; |
| 822 | } |
| 823 | EXPORT_SYMBOL_GPL(clk_enable); |
| 824 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 825 | static int clk_core_round_rate_nolock(struct clk_core *core, |
| 826 | struct clk_rate_request *req) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 827 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 828 | struct clk_core *parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 829 | long rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 830 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 831 | lockdep_assert_held(&prepare_lock); |
| 832 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 833 | if (!core) |
Stephen Boyd | 2ac6b1f | 2012-10-03 23:38:55 -0700 | [diff] [blame] | 834 | return 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 835 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 836 | parent = core->parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 837 | if (parent) { |
| 838 | req->best_parent_hw = parent->hw; |
| 839 | req->best_parent_rate = parent->rate; |
| 840 | } else { |
| 841 | req->best_parent_hw = NULL; |
| 842 | req->best_parent_rate = 0; |
| 843 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 844 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 845 | if (core->ops->determine_rate) { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 846 | return core->ops->determine_rate(core->hw, req); |
| 847 | } else if (core->ops->round_rate) { |
| 848 | rate = core->ops->round_rate(core->hw, req->rate, |
| 849 | &req->best_parent_rate); |
| 850 | if (rate < 0) |
| 851 | return rate; |
| 852 | |
| 853 | req->rate = rate; |
| 854 | } else if (core->flags & CLK_SET_RATE_PARENT) { |
| 855 | return clk_core_round_rate_nolock(parent, req); |
| 856 | } else { |
| 857 | req->rate = core->rate; |
| 858 | } |
| 859 | |
| 860 | return 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 861 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 862 | |
| 863 | /** |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 864 | * __clk_determine_rate - get the closest rate actually supported by a clock |
| 865 | * @hw: determine the rate of this clock |
| 866 | * @rate: target rate |
| 867 | * @min_rate: returned rate must be greater than this rate |
| 868 | * @max_rate: returned rate must be less than this rate |
| 869 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 870 | * Useful for clk_ops such as .set_rate and .determine_rate. |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 871 | */ |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 872 | 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] | 873 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 874 | if (!hw) { |
| 875 | req->rate = 0; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 876 | return 0; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 877 | } |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 878 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 879 | return clk_core_round_rate_nolock(hw->core, req); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 880 | } |
| 881 | EXPORT_SYMBOL_GPL(__clk_determine_rate); |
| 882 | |
| 883 | /** |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 884 | * __clk_round_rate - round the given rate for a clk |
| 885 | * @clk: round the rate of this clock |
| 886 | * @rate: the rate which is to be rounded |
| 887 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 888 | * Useful for clk_ops such as .set_rate |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 889 | */ |
| 890 | unsigned long __clk_round_rate(struct clk *clk, unsigned long rate) |
| 891 | { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 892 | struct clk_rate_request req; |
| 893 | int ret; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 894 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 895 | if (!clk) |
| 896 | return 0; |
| 897 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 898 | clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); |
| 899 | req.rate = rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 900 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 901 | ret = clk_core_round_rate_nolock(clk->core, &req); |
| 902 | if (ret) |
| 903 | return 0; |
| 904 | |
| 905 | return req.rate; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 906 | } |
Arnd Bergmann | 1cdf8ee | 2014-06-03 11:40:14 +0200 | [diff] [blame] | 907 | EXPORT_SYMBOL_GPL(__clk_round_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 908 | |
Stephen Boyd | 1a9c069 | 2015-06-25 15:55:14 -0700 | [diff] [blame^] | 909 | unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate) |
| 910 | { |
| 911 | int ret; |
| 912 | struct clk_rate_request req; |
| 913 | |
| 914 | clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); |
| 915 | req.rate = rate; |
| 916 | |
| 917 | ret = clk_core_round_rate_nolock(hw->core, &req); |
| 918 | if (ret) |
| 919 | return 0; |
| 920 | |
| 921 | return req.rate; |
| 922 | } |
| 923 | EXPORT_SYMBOL_GPL(clk_hw_round_rate); |
| 924 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 925 | /** |
| 926 | * clk_round_rate - round the given rate for a clk |
| 927 | * @clk: the clk for which we are rounding a rate |
| 928 | * @rate: the rate which is to be rounded |
| 929 | * |
| 930 | * Takes in a rate as input and rounds it to a rate that the clk can actually |
| 931 | * use which is then returned. If clk doesn't support round_rate operation |
| 932 | * then the parent rate is returned. |
| 933 | */ |
| 934 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 935 | { |
| 936 | unsigned long ret; |
| 937 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 938 | if (!clk) |
| 939 | return 0; |
| 940 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 941 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 942 | ret = __clk_round_rate(clk, rate); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 943 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 944 | |
| 945 | return ret; |
| 946 | } |
| 947 | EXPORT_SYMBOL_GPL(clk_round_rate); |
| 948 | |
| 949 | /** |
| 950 | * __clk_notify - call clk notifier chain |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 951 | * @core: clk that is changing rate |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 952 | * @msg: clk notifier type (see include/linux/clk.h) |
| 953 | * @old_rate: old clk rate |
| 954 | * @new_rate: new clk rate |
| 955 | * |
| 956 | * Triggers a notifier call chain on the clk rate-change notification |
| 957 | * for 'clk'. Passes a pointer to the struct clk and the previous |
| 958 | * and current rates to the notifier callback. Intended to be called by |
| 959 | * internal clock code only. Returns NOTIFY_DONE from the last driver |
| 960 | * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if |
| 961 | * a driver returns that. |
| 962 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 963 | static int __clk_notify(struct clk_core *core, unsigned long msg, |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 964 | unsigned long old_rate, unsigned long new_rate) |
| 965 | { |
| 966 | struct clk_notifier *cn; |
| 967 | struct clk_notifier_data cnd; |
| 968 | int ret = NOTIFY_DONE; |
| 969 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 970 | cnd.old_rate = old_rate; |
| 971 | cnd.new_rate = new_rate; |
| 972 | |
| 973 | list_for_each_entry(cn, &clk_notifier_list, node) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 974 | if (cn->clk->core == core) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 975 | cnd.clk = cn->clk; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 976 | ret = srcu_notifier_call_chain(&cn->notifier_head, msg, |
| 977 | &cnd); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | /** |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 985 | * __clk_recalc_accuracies |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 986 | * @core: first clk in the subtree |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 987 | * |
| 988 | * Walks the subtree of clks starting with clk and recalculates accuracies as |
| 989 | * 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] | 990 | * 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] | 991 | * parent. |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 992 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 993 | static void __clk_recalc_accuracies(struct clk_core *core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 994 | { |
| 995 | unsigned long parent_accuracy = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 996 | struct clk_core *child; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 997 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 998 | lockdep_assert_held(&prepare_lock); |
| 999 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1000 | if (core->parent) |
| 1001 | parent_accuracy = core->parent->accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1002 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1003 | if (core->ops->recalc_accuracy) |
| 1004 | core->accuracy = core->ops->recalc_accuracy(core->hw, |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1005 | parent_accuracy); |
| 1006 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1007 | core->accuracy = parent_accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1008 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1009 | hlist_for_each_entry(child, &core->children, child_node) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1010 | __clk_recalc_accuracies(child); |
| 1011 | } |
| 1012 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1013 | static long clk_core_get_accuracy(struct clk_core *core) |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1014 | { |
| 1015 | unsigned long accuracy; |
| 1016 | |
| 1017 | clk_prepare_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1018 | if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) |
| 1019 | __clk_recalc_accuracies(core); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1020 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1021 | accuracy = __clk_get_accuracy(core); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1022 | clk_prepare_unlock(); |
| 1023 | |
| 1024 | return accuracy; |
| 1025 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1026 | |
| 1027 | /** |
| 1028 | * clk_get_accuracy - return the accuracy of clk |
| 1029 | * @clk: the clk whose accuracy is being returned |
| 1030 | * |
| 1031 | * Simply returns the cached accuracy of the clk, unless |
| 1032 | * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be |
| 1033 | * issued. |
| 1034 | * If clk is NULL then returns 0. |
| 1035 | */ |
| 1036 | long clk_get_accuracy(struct clk *clk) |
| 1037 | { |
| 1038 | if (!clk) |
| 1039 | return 0; |
| 1040 | |
| 1041 | return clk_core_get_accuracy(clk->core); |
| 1042 | } |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1043 | EXPORT_SYMBOL_GPL(clk_get_accuracy); |
| 1044 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1045 | static unsigned long clk_recalc(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1046 | unsigned long parent_rate) |
Stephen Boyd | 8f2c2db | 2014-03-26 16:06:36 -0700 | [diff] [blame] | 1047 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1048 | if (core->ops->recalc_rate) |
| 1049 | return core->ops->recalc_rate(core->hw, parent_rate); |
Stephen Boyd | 8f2c2db | 2014-03-26 16:06:36 -0700 | [diff] [blame] | 1050 | return parent_rate; |
| 1051 | } |
| 1052 | |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1053 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1054 | * __clk_recalc_rates |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1055 | * @core: first clk in the subtree |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1056 | * @msg: notification type (see include/linux/clk.h) |
| 1057 | * |
| 1058 | * Walks the subtree of clks starting with clk and recalculates rates as it |
| 1059 | * 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] | 1060 | * 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] | 1061 | * |
| 1062 | * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, |
| 1063 | * if necessary. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1064 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1065 | static void __clk_recalc_rates(struct clk_core *core, unsigned long msg) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1066 | { |
| 1067 | unsigned long old_rate; |
| 1068 | unsigned long parent_rate = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1069 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1070 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 1071 | lockdep_assert_held(&prepare_lock); |
| 1072 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1073 | old_rate = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1074 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1075 | if (core->parent) |
| 1076 | parent_rate = core->parent->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1077 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1078 | core->rate = clk_recalc(core, parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1079 | |
| 1080 | /* |
| 1081 | * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE |
| 1082 | * & ABORT_RATE_CHANGE notifiers |
| 1083 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1084 | if (core->notifier_count && msg) |
| 1085 | __clk_notify(core, msg, old_rate, core->rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1086 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1087 | hlist_for_each_entry(child, &core->children, child_node) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1088 | __clk_recalc_rates(child, msg); |
| 1089 | } |
| 1090 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1091 | static unsigned long clk_core_get_rate(struct clk_core *core) |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1092 | { |
| 1093 | unsigned long rate; |
| 1094 | |
| 1095 | clk_prepare_lock(); |
| 1096 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1097 | if (core && (core->flags & CLK_GET_RATE_NOCACHE)) |
| 1098 | __clk_recalc_rates(core, 0); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1099 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1100 | rate = clk_core_get_rate_nolock(core); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1101 | clk_prepare_unlock(); |
| 1102 | |
| 1103 | return rate; |
| 1104 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1105 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1106 | /** |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1107 | * clk_get_rate - return the rate of clk |
| 1108 | * @clk: the clk whose rate is being returned |
| 1109 | * |
| 1110 | * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag |
| 1111 | * is set, which means a recalc_rate will be issued. |
| 1112 | * If clk is NULL then returns 0. |
| 1113 | */ |
| 1114 | unsigned long clk_get_rate(struct clk *clk) |
| 1115 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1116 | if (!clk) |
| 1117 | return 0; |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1118 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1119 | return clk_core_get_rate(clk->core); |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1120 | } |
| 1121 | EXPORT_SYMBOL_GPL(clk_get_rate); |
| 1122 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1123 | static int clk_fetch_parent_index(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1124 | struct clk_core *parent) |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1125 | { |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1126 | int i; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1127 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1128 | if (!core->parents) { |
| 1129 | core->parents = kcalloc(core->num_parents, |
Tomasz Figa | 96a7ed9 | 2013-09-29 02:37:15 +0200 | [diff] [blame] | 1130 | sizeof(struct clk *), GFP_KERNEL); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1131 | if (!core->parents) |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1132 | return -ENOMEM; |
| 1133 | } |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | * find index of new parent clock using cached parent ptrs, |
| 1137 | * or if not yet cached, use string name comparison and cache |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1138 | * them now to avoid future calls to clk_core_lookup. |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1139 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1140 | for (i = 0; i < core->num_parents; i++) { |
| 1141 | if (core->parents[i] == parent) |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1142 | return i; |
Tomasz Figa | da0f0b2 | 2013-09-29 02:37:16 +0200 | [diff] [blame] | 1143 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1144 | if (core->parents[i]) |
Tomasz Figa | da0f0b2 | 2013-09-29 02:37:16 +0200 | [diff] [blame] | 1145 | continue; |
| 1146 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1147 | if (!strcmp(core->parent_names[i], parent->name)) { |
| 1148 | core->parents[i] = clk_core_lookup(parent->name); |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1149 | return i; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1153 | return -EINVAL; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1156 | /* |
| 1157 | * Update the orphan status of @core and all its children. |
| 1158 | */ |
| 1159 | static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan) |
| 1160 | { |
| 1161 | struct clk_core *child; |
| 1162 | |
| 1163 | core->orphan = is_orphan; |
| 1164 | |
| 1165 | hlist_for_each_entry(child, &core->children, child_node) |
| 1166 | clk_core_update_orphan_status(child, is_orphan); |
| 1167 | } |
| 1168 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1169 | 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] | 1170 | { |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1171 | bool was_orphan = core->orphan; |
| 1172 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1173 | hlist_del(&core->child_node); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1174 | |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1175 | if (new_parent) { |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1176 | bool becomes_orphan = new_parent->orphan; |
| 1177 | |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1178 | /* avoid duplicate POST_RATE_CHANGE notifications */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1179 | if (new_parent->new_child == core) |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1180 | new_parent->new_child = NULL; |
| 1181 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1182 | hlist_add_head(&core->child_node, &new_parent->children); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1183 | |
| 1184 | if (was_orphan != becomes_orphan) |
| 1185 | clk_core_update_orphan_status(core, becomes_orphan); |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1186 | } else { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1187 | hlist_add_head(&core->child_node, &clk_orphan_list); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 1188 | if (!was_orphan) |
| 1189 | clk_core_update_orphan_status(core, true); |
James Hogan | 903efc5 | 2013-08-29 12:10:51 +0100 | [diff] [blame] | 1190 | } |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1191 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1192 | core->parent = new_parent; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1195 | static struct clk_core *__clk_set_parent_before(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1196 | struct clk_core *parent) |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1197 | { |
| 1198 | unsigned long flags; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1199 | struct clk_core *old_parent = core->parent; |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* |
| 1202 | * Migrate prepare state between parents and prevent race with |
| 1203 | * clk_enable(). |
| 1204 | * |
| 1205 | * If the clock is not prepared, then a race with |
| 1206 | * clk_enable/disable() is impossible since we already have the |
| 1207 | * prepare lock (future calls to clk_enable() need to be preceded by |
| 1208 | * a clk_prepare()). |
| 1209 | * |
| 1210 | * If the clock is prepared, migrate the prepared state to the new |
| 1211 | * parent and also protect against a race with clk_enable() by |
| 1212 | * forcing the clock and the new parent on. This ensures that all |
| 1213 | * future calls to clk_enable() are practically NOPs with respect to |
| 1214 | * hardware and software states. |
| 1215 | * |
| 1216 | * See also: Comment for clk_set_parent() below. |
| 1217 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1218 | if (core->prepare_count) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1219 | clk_core_prepare(parent); |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1220 | flags = clk_enable_lock(); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1221 | clk_core_enable(parent); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1222 | clk_core_enable(core); |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1223 | clk_enable_unlock(flags); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /* update the clk tree topology */ |
| 1227 | flags = clk_enable_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1228 | clk_reparent(core, parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1229 | clk_enable_unlock(flags); |
| 1230 | |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1231 | return old_parent; |
| 1232 | } |
| 1233 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1234 | static void __clk_set_parent_after(struct clk_core *core, |
| 1235 | struct clk_core *parent, |
| 1236 | struct clk_core *old_parent) |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1237 | { |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1238 | unsigned long flags; |
| 1239 | |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1240 | /* |
| 1241 | * Finish the migration of prepare state and undo the changes done |
| 1242 | * for preventing a race with clk_enable(). |
| 1243 | */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1244 | if (core->prepare_count) { |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1245 | flags = clk_enable_lock(); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1246 | clk_core_disable(core); |
| 1247 | clk_core_disable(old_parent); |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1248 | clk_enable_unlock(flags); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1249 | clk_core_unprepare(old_parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1250 | } |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1253 | 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] | 1254 | u8 p_index) |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1255 | { |
| 1256 | unsigned long flags; |
| 1257 | int ret = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1258 | struct clk_core *old_parent; |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1259 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1260 | old_parent = __clk_set_parent_before(core, parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1261 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1262 | trace_clk_set_parent(core, parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1263 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1264 | /* change clock input source */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1265 | if (parent && core->ops->set_parent) |
| 1266 | ret = core->ops->set_parent(core->hw, p_index); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1267 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1268 | trace_clk_set_parent_complete(core, parent); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1269 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1270 | if (ret) { |
| 1271 | flags = clk_enable_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1272 | clk_reparent(core, old_parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1273 | clk_enable_unlock(flags); |
| 1274 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1275 | if (core->prepare_count) { |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1276 | flags = clk_enable_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1277 | clk_core_disable(core); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1278 | clk_core_disable(parent); |
Dong Aisheng | d2a5d46 | 2015-04-15 22:26:36 +0800 | [diff] [blame] | 1279 | clk_enable_unlock(flags); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1280 | clk_core_unprepare(parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1281 | } |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1285 | __clk_set_parent_after(core, parent, old_parent); |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1286 | |
James Hogan | 4935b22 | 2013-07-29 12:24:59 +0100 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
| 1289 | |
Ulf Hansson | a093bde | 2012-08-31 14:21:28 +0200 | [diff] [blame] | 1290 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1291 | * __clk_speculate_rates |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1292 | * @core: first clk in the subtree |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1293 | * @parent_rate: the "future" rate of clk's parent |
| 1294 | * |
| 1295 | * Walks the subtree of clks starting with clk, speculating rates as it |
| 1296 | * goes and firing off PRE_RATE_CHANGE notifications as necessary. |
| 1297 | * |
| 1298 | * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending |
| 1299 | * pre-rate change notifications and returns early if no clks in the |
| 1300 | * subtree have subscribed to the notifications. Note that if a clk does not |
| 1301 | * 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] | 1302 | * take on the rate of its parent. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1303 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1304 | static int __clk_speculate_rates(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1305 | unsigned long parent_rate) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1306 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1307 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1308 | unsigned long new_rate; |
| 1309 | int ret = NOTIFY_DONE; |
| 1310 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 1311 | lockdep_assert_held(&prepare_lock); |
| 1312 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1313 | new_rate = clk_recalc(core, parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1314 | |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1315 | /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1316 | if (core->notifier_count) |
| 1317 | ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1318 | |
Mike Turquette | 86bcfa2 | 2014-02-24 16:08:41 -0800 | [diff] [blame] | 1319 | if (ret & NOTIFY_STOP_MASK) { |
| 1320 | 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] | 1321 | __func__, core->name, ret); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1322 | goto out; |
Mike Turquette | 86bcfa2 | 2014-02-24 16:08:41 -0800 | [diff] [blame] | 1323 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1324 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1325 | hlist_for_each_entry(child, &core->children, child_node) { |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1326 | ret = __clk_speculate_rates(child, new_rate); |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1327 | if (ret & NOTIFY_STOP_MASK) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | out: |
| 1332 | return ret; |
| 1333 | } |
| 1334 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1335 | 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] | 1336 | struct clk_core *new_parent, u8 p_index) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1337 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1338 | struct clk_core *child; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1339 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1340 | core->new_rate = new_rate; |
| 1341 | core->new_parent = new_parent; |
| 1342 | core->new_parent_index = p_index; |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1343 | /* include clk in new parent's PRE_RATE_CHANGE notifications */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1344 | core->new_child = NULL; |
| 1345 | if (new_parent && new_parent != core->parent) |
| 1346 | new_parent->new_child = core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1347 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1348 | hlist_for_each_entry(child, &core->children, child_node) { |
Stephen Boyd | 8f2c2db | 2014-03-26 16:06:36 -0700 | [diff] [blame] | 1349 | child->new_rate = clk_recalc(child, new_rate); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1350 | clk_calc_subtree(child, child->new_rate, NULL, 0); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * calculate the new rates returning the topmost clock that has to be |
| 1356 | * changed. |
| 1357 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1358 | static struct clk_core *clk_calc_new_rates(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1359 | unsigned long rate) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1360 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1361 | struct clk_core *top = core; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1362 | struct clk_core *old_parent, *parent; |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 1363 | unsigned long best_parent_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1364 | unsigned long new_rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1365 | unsigned long min_rate; |
| 1366 | unsigned long max_rate; |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1367 | int p_index = 0; |
Boris Brezillon | 03bc10a | 2015-03-29 03:48:48 +0200 | [diff] [blame] | 1368 | long ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1369 | |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1370 | /* sanity */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1371 | if (IS_ERR_OR_NULL(core)) |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1372 | return NULL; |
| 1373 | |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1374 | /* save parent rate, if it exists */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1375 | parent = old_parent = core->parent; |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1376 | if (parent) |
| 1377 | best_parent_rate = parent->rate; |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1378 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1379 | clk_core_get_boundaries(core, &min_rate, &max_rate); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1380 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1381 | /* find the closest rate and parent clk/rate */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1382 | if (core->ops->determine_rate) { |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1383 | struct clk_rate_request req; |
| 1384 | |
| 1385 | req.rate = rate; |
| 1386 | req.min_rate = min_rate; |
| 1387 | req.max_rate = max_rate; |
| 1388 | if (parent) { |
| 1389 | req.best_parent_hw = parent->hw; |
| 1390 | req.best_parent_rate = parent->rate; |
| 1391 | } else { |
| 1392 | req.best_parent_hw = NULL; |
| 1393 | req.best_parent_rate = 0; |
| 1394 | } |
| 1395 | |
| 1396 | ret = core->ops->determine_rate(core->hw, &req); |
Boris Brezillon | 03bc10a | 2015-03-29 03:48:48 +0200 | [diff] [blame] | 1397 | if (ret < 0) |
| 1398 | return NULL; |
| 1399 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1400 | best_parent_rate = req.best_parent_rate; |
| 1401 | new_rate = req.rate; |
| 1402 | parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1403 | } else if (core->ops->round_rate) { |
| 1404 | ret = core->ops->round_rate(core->hw, rate, |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 1405 | &best_parent_rate); |
Boris Brezillon | 03bc10a | 2015-03-29 03:48:48 +0200 | [diff] [blame] | 1406 | if (ret < 0) |
| 1407 | return NULL; |
| 1408 | |
| 1409 | new_rate = ret; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1410 | if (new_rate < min_rate || new_rate > max_rate) |
| 1411 | return NULL; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1412 | } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1413 | /* pass-through clock without adjustable parent */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1414 | core->new_rate = core->rate; |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1415 | return NULL; |
| 1416 | } else { |
| 1417 | /* pass-through clock with adjustable parent */ |
| 1418 | top = clk_calc_new_rates(parent, rate); |
| 1419 | new_rate = parent->new_rate; |
Mike Turquette | 63f5c3b | 2012-05-02 16:23:43 -0700 | [diff] [blame] | 1420 | goto out; |
Mike Turquette | 7452b21 | 2012-03-26 14:45:36 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1423 | /* some clocks must be gated to change parent */ |
| 1424 | if (parent != old_parent && |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1425 | (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1426 | pr_debug("%s: %s not gated but wants to reparent\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1427 | __func__, core->name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1428 | return NULL; |
| 1429 | } |
| 1430 | |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1431 | /* try finding the new parent index */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1432 | if (parent && core->num_parents > 1) { |
| 1433 | p_index = clk_fetch_parent_index(core, parent); |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1434 | if (p_index < 0) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1435 | 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] | 1436 | __func__, parent->name, core->name); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1437 | return NULL; |
| 1438 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1441 | if ((core->flags & CLK_SET_RATE_PARENT) && parent && |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1442 | best_parent_rate != parent->rate) |
| 1443 | top = clk_calc_new_rates(parent, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1444 | |
| 1445 | out: |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1446 | clk_calc_subtree(core, new_rate, parent, p_index); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1447 | |
| 1448 | return top; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Notify about rate changes in a subtree. Always walk down the whole tree |
| 1453 | * so that in case of an error we can walk down the whole tree again and |
| 1454 | * abort the change. |
| 1455 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1456 | static struct clk_core *clk_propagate_rate_change(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1457 | unsigned long event) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1458 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1459 | struct clk_core *child, *tmp_clk, *fail_clk = NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1460 | int ret = NOTIFY_DONE; |
| 1461 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1462 | if (core->rate == core->new_rate) |
Sachin Kamat | 5fda685 | 2013-03-13 15:17:49 +0530 | [diff] [blame] | 1463 | return NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1464 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1465 | if (core->notifier_count) { |
| 1466 | ret = __clk_notify(core, event, core->rate, core->new_rate); |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1467 | if (ret & NOTIFY_STOP_MASK) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1468 | fail_clk = core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1471 | hlist_for_each_entry(child, &core->children, child_node) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1472 | /* Skip children who will be reparented to another clock */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1473 | if (child->new_parent && child->new_parent != core) |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1474 | continue; |
| 1475 | tmp_clk = clk_propagate_rate_change(child, event); |
| 1476 | if (tmp_clk) |
| 1477 | fail_clk = tmp_clk; |
| 1478 | } |
| 1479 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1480 | /* handle the new child who might not be in core->children yet */ |
| 1481 | if (core->new_child) { |
| 1482 | tmp_clk = clk_propagate_rate_change(core->new_child, event); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1483 | if (tmp_clk) |
| 1484 | fail_clk = tmp_clk; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | return fail_clk; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * walk down a subtree and set the new rates notifying the rate |
| 1492 | * change on the way |
| 1493 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1494 | static void clk_change_rate(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1495 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1496 | struct clk_core *child; |
Tero Kristo | 067bb17 | 2014-08-21 16:47:45 +0300 | [diff] [blame] | 1497 | struct hlist_node *tmp; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1498 | unsigned long old_rate; |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1499 | unsigned long best_parent_rate = 0; |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1500 | bool skip_set_rate = false; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1501 | struct clk_core *old_parent; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1502 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1503 | old_rate = core->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1504 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1505 | if (core->new_parent) |
| 1506 | best_parent_rate = core->new_parent->rate; |
| 1507 | else if (core->parent) |
| 1508 | best_parent_rate = core->parent->rate; |
Pawel Moll | bf47b4f | 2012-06-08 14:04:06 +0100 | [diff] [blame] | 1509 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1510 | if (core->new_parent && core->new_parent != core->parent) { |
| 1511 | old_parent = __clk_set_parent_before(core, core->new_parent); |
| 1512 | trace_clk_set_parent(core, core->new_parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1513 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1514 | if (core->ops->set_rate_and_parent) { |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1515 | skip_set_rate = true; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1516 | core->ops->set_rate_and_parent(core->hw, core->new_rate, |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1517 | best_parent_rate, |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1518 | core->new_parent_index); |
| 1519 | } else if (core->ops->set_parent) { |
| 1520 | core->ops->set_parent(core->hw, core->new_parent_index); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1523 | trace_clk_set_parent_complete(core, core->new_parent); |
| 1524 | __clk_set_parent_after(core, core->new_parent, old_parent); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1527 | trace_clk_set_rate(core, core->new_rate); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1528 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1529 | if (!skip_set_rate && core->ops->set_rate) |
| 1530 | core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1531 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1532 | trace_clk_set_rate_complete(core, core->new_rate); |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1533 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1534 | core->rate = clk_recalc(core, best_parent_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1535 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1536 | if (core->notifier_count && old_rate != core->rate) |
| 1537 | __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1538 | |
Michael Turquette | 85e88fa | 2015-06-20 12:18:03 -0700 | [diff] [blame] | 1539 | if (core->flags & CLK_RECALC_NEW_RATES) |
| 1540 | (void)clk_calc_new_rates(core, core->new_rate); |
Bartlomiej Zolnierkiewicz | d8d9198 | 2015-04-03 18:43:44 +0200 | [diff] [blame] | 1541 | |
Tero Kristo | 067bb17 | 2014-08-21 16:47:45 +0300 | [diff] [blame] | 1542 | /* |
| 1543 | * Use safe iteration, as change_rate can actually swap parents |
| 1544 | * for certain clock types. |
| 1545 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1546 | hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1547 | /* Skip children who will be reparented to another clock */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1548 | if (child->new_parent && child->new_parent != core) |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1549 | continue; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1550 | clk_change_rate(child); |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 1551 | } |
| 1552 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1553 | /* handle the new child who might not be in core->children yet */ |
| 1554 | if (core->new_child) |
| 1555 | clk_change_rate(core->new_child); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1558 | static int clk_core_set_rate_nolock(struct clk_core *core, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1559 | unsigned long req_rate) |
| 1560 | { |
| 1561 | struct clk_core *top, *fail_clk; |
| 1562 | unsigned long rate = req_rate; |
| 1563 | int ret = 0; |
| 1564 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1565 | if (!core) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1566 | return 0; |
| 1567 | |
| 1568 | /* bail early if nothing to do */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1569 | if (rate == clk_core_get_rate_nolock(core)) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1570 | return 0; |
| 1571 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1572 | if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1573 | return -EBUSY; |
| 1574 | |
| 1575 | /* calculate new rates and get the topmost changed clock */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1576 | top = clk_calc_new_rates(core, rate); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1577 | if (!top) |
| 1578 | return -EINVAL; |
| 1579 | |
| 1580 | /* notify that we are about to change rates */ |
| 1581 | fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); |
| 1582 | if (fail_clk) { |
| 1583 | pr_debug("%s: failed to set %s rate\n", __func__, |
| 1584 | fail_clk->name); |
| 1585 | clk_propagate_rate_change(top, ABORT_RATE_CHANGE); |
| 1586 | return -EBUSY; |
| 1587 | } |
| 1588 | |
| 1589 | /* change the rates */ |
| 1590 | clk_change_rate(top); |
| 1591 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1592 | core->req_rate = req_rate; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1593 | |
| 1594 | return ret; |
| 1595 | } |
| 1596 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1597 | /** |
| 1598 | * clk_set_rate - specify a new rate for clk |
| 1599 | * @clk: the clk whose rate is being changed |
| 1600 | * @rate: the new rate for clk |
| 1601 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1602 | * 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] | 1603 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1604 | * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to |
| 1605 | * propagate up to clk's parent; whether or not this happens depends on the |
| 1606 | * outcome of clk's .round_rate implementation. If *parent_rate is unchanged |
| 1607 | * after calling .round_rate then upstream parent propagation is ignored. If |
| 1608 | * *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] | 1609 | * 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] | 1610 | * until either a clk does not support the CLK_SET_RATE_PARENT flag or |
| 1611 | * .round_rate stops requesting changes to clk's parent_rate. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1612 | * |
Mike Turquette | 5654dc9 | 2012-03-26 11:51:34 -0700 | [diff] [blame] | 1613 | * Rate changes are accomplished via tree traversal that also recalculates the |
| 1614 | * rates for the clocks and fires off POST_RATE_CHANGE notifiers. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1615 | * |
| 1616 | * Returns 0 on success, -EERROR otherwise. |
| 1617 | */ |
| 1618 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 1619 | { |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1620 | int ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1621 | |
Mike Turquette | 89ac8d7 | 2013-08-21 23:58:09 -0700 | [diff] [blame] | 1622 | if (!clk) |
| 1623 | return 0; |
| 1624 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1625 | /* prevent racing with updates to the clock topology */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1626 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1627 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1628 | ret = clk_core_set_rate_nolock(clk->core, rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1629 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1630 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1631 | |
| 1632 | return ret; |
| 1633 | } |
| 1634 | EXPORT_SYMBOL_GPL(clk_set_rate); |
| 1635 | |
| 1636 | /** |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 1637 | * clk_set_rate_range - set a rate range for a clock source |
| 1638 | * @clk: clock source |
| 1639 | * @min: desired minimum clock rate in Hz, inclusive |
| 1640 | * @max: desired maximum clock rate in Hz, inclusive |
| 1641 | * |
| 1642 | * Returns success (0) or negative errno. |
| 1643 | */ |
| 1644 | int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) |
| 1645 | { |
| 1646 | int ret = 0; |
| 1647 | |
| 1648 | if (!clk) |
| 1649 | return 0; |
| 1650 | |
| 1651 | if (min > max) { |
| 1652 | pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", |
| 1653 | __func__, clk->core->name, clk->dev_id, clk->con_id, |
| 1654 | min, max); |
| 1655 | return -EINVAL; |
| 1656 | } |
| 1657 | |
| 1658 | clk_prepare_lock(); |
| 1659 | |
| 1660 | if (min != clk->min_rate || max != clk->max_rate) { |
| 1661 | clk->min_rate = min; |
| 1662 | clk->max_rate = max; |
| 1663 | ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate); |
| 1664 | } |
| 1665 | |
| 1666 | clk_prepare_unlock(); |
| 1667 | |
| 1668 | return ret; |
| 1669 | } |
| 1670 | EXPORT_SYMBOL_GPL(clk_set_rate_range); |
| 1671 | |
| 1672 | /** |
| 1673 | * clk_set_min_rate - set a minimum clock rate for a clock source |
| 1674 | * @clk: clock source |
| 1675 | * @rate: desired minimum clock rate in Hz, inclusive |
| 1676 | * |
| 1677 | * Returns success (0) or negative errno. |
| 1678 | */ |
| 1679 | int clk_set_min_rate(struct clk *clk, unsigned long rate) |
| 1680 | { |
| 1681 | if (!clk) |
| 1682 | return 0; |
| 1683 | |
| 1684 | return clk_set_rate_range(clk, rate, clk->max_rate); |
| 1685 | } |
| 1686 | EXPORT_SYMBOL_GPL(clk_set_min_rate); |
| 1687 | |
| 1688 | /** |
| 1689 | * clk_set_max_rate - set a maximum clock rate for a clock source |
| 1690 | * @clk: clock source |
| 1691 | * @rate: desired maximum clock rate in Hz, inclusive |
| 1692 | * |
| 1693 | * Returns success (0) or negative errno. |
| 1694 | */ |
| 1695 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 1696 | { |
| 1697 | if (!clk) |
| 1698 | return 0; |
| 1699 | |
| 1700 | return clk_set_rate_range(clk, clk->min_rate, rate); |
| 1701 | } |
| 1702 | EXPORT_SYMBOL_GPL(clk_set_max_rate); |
| 1703 | |
| 1704 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1705 | * clk_get_parent - return the parent of a clk |
| 1706 | * @clk: the clk whose parent gets returned |
| 1707 | * |
| 1708 | * Simply returns clk->parent. Returns NULL if clk is NULL. |
| 1709 | */ |
| 1710 | struct clk *clk_get_parent(struct clk *clk) |
| 1711 | { |
| 1712 | struct clk *parent; |
| 1713 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1714 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1715 | parent = __clk_get_parent(clk); |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1716 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1717 | |
| 1718 | return parent; |
| 1719 | } |
| 1720 | EXPORT_SYMBOL_GPL(clk_get_parent); |
| 1721 | |
| 1722 | /* |
| 1723 | * .get_parent is mandatory for clocks with multiple possible parents. It is |
| 1724 | * optional for single-parent clocks. Always call .get_parent if it is |
| 1725 | * available and WARN if it is missing for multi-parent clocks. |
| 1726 | * |
| 1727 | * For single-parent clocks without .get_parent, first check to see if the |
| 1728 | * .parents array exists, and if so use it to avoid an expensive tree |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1729 | * traversal. If .parents does not exist then walk the tree. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1730 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1731 | static struct clk_core *__clk_init_parent(struct clk_core *core) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1732 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1733 | struct clk_core *ret = NULL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1734 | u8 index; |
| 1735 | |
| 1736 | /* handle the trivial cases */ |
| 1737 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1738 | if (!core->num_parents) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1739 | goto out; |
| 1740 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1741 | if (core->num_parents == 1) { |
| 1742 | if (IS_ERR_OR_NULL(core->parent)) |
| 1743 | core->parent = clk_core_lookup(core->parent_names[0]); |
| 1744 | ret = core->parent; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1745 | goto out; |
| 1746 | } |
| 1747 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1748 | if (!core->ops->get_parent) { |
| 1749 | WARN(!core->ops->get_parent, |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1750 | "%s: multi-parent clocks must implement .get_parent\n", |
| 1751 | __func__); |
| 1752 | goto out; |
| 1753 | }; |
| 1754 | |
| 1755 | /* |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1756 | * Do our best to cache parent clocks in core->parents. This prevents |
| 1757 | * unnecessary and expensive lookups. We don't set core->parent here; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1758 | * that is done by the calling function. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1759 | */ |
| 1760 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1761 | index = core->ops->get_parent(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1762 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1763 | if (!core->parents) |
| 1764 | core->parents = |
| 1765 | kcalloc(core->num_parents, sizeof(struct clk *), |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1766 | GFP_KERNEL); |
| 1767 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1768 | ret = clk_core_get_parent_by_index(core, index); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1769 | |
| 1770 | out: |
| 1771 | return ret; |
| 1772 | } |
| 1773 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1774 | static void clk_core_reparent(struct clk_core *core, |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1775 | struct clk_core *new_parent) |
Ulf Hansson | b33d212 | 2013-04-02 23:09:37 +0200 | [diff] [blame] | 1776 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1777 | clk_reparent(core, new_parent); |
| 1778 | __clk_recalc_accuracies(core); |
| 1779 | __clk_recalc_rates(core, POST_RATE_CHANGE); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Tomeu Vizoso | 42c8654 | 2015-03-11 11:34:25 +0100 | [diff] [blame] | 1782 | void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent) |
| 1783 | { |
| 1784 | if (!hw) |
| 1785 | return; |
| 1786 | |
| 1787 | clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); |
| 1788 | } |
| 1789 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1790 | /** |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1791 | * clk_has_parent - check if a clock is a possible parent for another |
| 1792 | * @clk: clock source |
| 1793 | * @parent: parent clock source |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1794 | * |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1795 | * This function can be used in drivers that need to check that a clock can be |
| 1796 | * the parent of another without actually changing the parent. |
Saravana Kannan | f8aa0bd | 2013-05-15 21:07:24 -0700 | [diff] [blame] | 1797 | * |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1798 | * Returns true if @parent is a possible parent for @clk, false otherwise. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1799 | */ |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1800 | bool clk_has_parent(struct clk *clk, struct clk *parent) |
| 1801 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1802 | struct clk_core *core, *parent_core; |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1803 | unsigned int i; |
| 1804 | |
| 1805 | /* NULL clocks should be nops, so return success if either is NULL. */ |
| 1806 | if (!clk || !parent) |
| 1807 | return true; |
| 1808 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1809 | core = clk->core; |
| 1810 | parent_core = parent->core; |
| 1811 | |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1812 | /* Optimize for the case where the parent is already the parent. */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1813 | if (core->parent == parent_core) |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1814 | return true; |
| 1815 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1816 | for (i = 0; i < core->num_parents; i++) |
| 1817 | if (strcmp(core->parent_names[i], parent_core->name) == 0) |
Thierry Reding | 4e88f3d | 2015-01-21 17:13:00 +0100 | [diff] [blame] | 1818 | return true; |
| 1819 | |
| 1820 | return false; |
| 1821 | } |
| 1822 | EXPORT_SYMBOL_GPL(clk_has_parent); |
| 1823 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | int ret = 0; |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1827 | int p_index = 0; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1828 | unsigned long p_rate = 0; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1829 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1830 | if (!core) |
Mike Turquette | 89ac8d7 | 2013-08-21 23:58:09 -0700 | [diff] [blame] | 1831 | return 0; |
| 1832 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1833 | /* prevent racing with updates to the clock topology */ |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1834 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1835 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1836 | if (core->parent == parent) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1837 | goto out; |
| 1838 | |
Stephen Boyd | b61c43c | 2015-02-02 14:11:25 -0800 | [diff] [blame] | 1839 | /* verify ops for for multi-parent clks */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1840 | if ((core->num_parents > 1) && (!core->ops->set_parent)) { |
Stephen Boyd | b61c43c | 2015-02-02 14:11:25 -0800 | [diff] [blame] | 1841 | ret = -ENOSYS; |
| 1842 | goto out; |
| 1843 | } |
| 1844 | |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1845 | /* 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] | 1846 | if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1847 | ret = -EBUSY; |
| 1848 | goto out; |
| 1849 | } |
| 1850 | |
| 1851 | /* try finding the new parent index */ |
| 1852 | if (parent) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1853 | p_index = clk_fetch_parent_index(core, parent); |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1854 | p_rate = parent->rate; |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1855 | if (p_index < 0) { |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1856 | 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] | 1857 | __func__, parent->name, core->name); |
Tomasz Figa | f1c8b2e | 2013-09-29 02:37:14 +0200 | [diff] [blame] | 1858 | ret = p_index; |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1859 | goto out; |
| 1860 | } |
| 1861 | } |
| 1862 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1863 | /* propagate PRE_RATE_CHANGE notifications */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1864 | ret = __clk_speculate_rates(core, p_rate); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1865 | |
| 1866 | /* abort if a driver objects */ |
Soren Brinkmann | fb72a05 | 2013-04-03 12:17:12 -0700 | [diff] [blame] | 1867 | if (ret & NOTIFY_STOP_MASK) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1868 | goto out; |
| 1869 | |
Ulf Hansson | 031dcc9 | 2013-04-02 23:09:38 +0200 | [diff] [blame] | 1870 | /* do the re-parent */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1871 | ret = __clk_set_parent(core, parent, p_index); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1872 | |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1873 | /* propagate rate an accuracy recalculation accordingly */ |
| 1874 | if (ret) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1875 | __clk_recalc_rates(core, ABORT_RATE_CHANGE); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1876 | } else { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1877 | __clk_recalc_rates(core, POST_RATE_CHANGE); |
| 1878 | __clk_recalc_accuracies(core); |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 1879 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1880 | |
| 1881 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 1882 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1883 | |
| 1884 | return ret; |
| 1885 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1886 | |
| 1887 | /** |
| 1888 | * clk_set_parent - switch the parent of a mux clk |
| 1889 | * @clk: the mux clk whose input we are switching |
| 1890 | * @parent: the new input to clk |
| 1891 | * |
| 1892 | * Re-parent clk to use parent as its new input source. If clk is in |
| 1893 | * prepared state, the clk will get enabled for the duration of this call. If |
| 1894 | * that's not acceptable for a specific clk (Eg: the consumer can't handle |
| 1895 | * that, the reparenting is glitchy in hardware, etc), use the |
| 1896 | * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared. |
| 1897 | * |
| 1898 | * After successfully changing clk's parent clk_set_parent will update the |
| 1899 | * clk topology, sysfs topology and propagate rate recalculation via |
| 1900 | * __clk_recalc_rates. |
| 1901 | * |
| 1902 | * Returns 0 on success, -EERROR otherwise. |
| 1903 | */ |
| 1904 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 1905 | { |
| 1906 | if (!clk) |
| 1907 | return 0; |
| 1908 | |
| 1909 | return clk_core_set_parent(clk->core, parent ? parent->core : NULL); |
| 1910 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1911 | EXPORT_SYMBOL_GPL(clk_set_parent); |
| 1912 | |
| 1913 | /** |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1914 | * clk_set_phase - adjust the phase shift of a clock signal |
| 1915 | * @clk: clock signal source |
| 1916 | * @degrees: number of degrees the signal is shifted |
| 1917 | * |
| 1918 | * Shifts the phase of a clock signal by the specified |
| 1919 | * degrees. Returns 0 on success, -EERROR otherwise. |
| 1920 | * |
| 1921 | * This function makes no distinction about the input or reference |
| 1922 | * signal that we adjust the clock signal phase against. For example |
| 1923 | * phase locked-loop clock signal generators we may shift phase with |
| 1924 | * respect to feedback clock signal input, but for other cases the |
| 1925 | * clock phase may be shifted with respect to some other, unspecified |
| 1926 | * signal. |
| 1927 | * |
| 1928 | * Additionally the concept of phase shift does not propagate through |
| 1929 | * the clock tree hierarchy, which sets it apart from clock rates and |
| 1930 | * clock accuracy. A parent clock phase attribute does not have an |
| 1931 | * impact on the phase attribute of a child clock. |
| 1932 | */ |
| 1933 | int clk_set_phase(struct clk *clk, int degrees) |
| 1934 | { |
Stephen Boyd | 08b9575 | 2015-02-02 14:09:43 -0800 | [diff] [blame] | 1935 | int ret = -EINVAL; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1936 | |
| 1937 | if (!clk) |
Stephen Boyd | 08b9575 | 2015-02-02 14:09:43 -0800 | [diff] [blame] | 1938 | return 0; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1939 | |
| 1940 | /* sanity check degrees */ |
| 1941 | degrees %= 360; |
| 1942 | if (degrees < 0) |
| 1943 | degrees += 360; |
| 1944 | |
| 1945 | clk_prepare_lock(); |
| 1946 | |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1947 | trace_clk_set_phase(clk->core, degrees); |
| 1948 | |
Stephen Boyd | 08b9575 | 2015-02-02 14:09:43 -0800 | [diff] [blame] | 1949 | if (clk->core->ops->set_phase) |
| 1950 | ret = clk->core->ops->set_phase(clk->core->hw, degrees); |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1951 | |
Stephen Boyd | dfc202e | 2015-02-02 14:37:41 -0800 | [diff] [blame] | 1952 | trace_clk_set_phase_complete(clk->core, degrees); |
| 1953 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1954 | if (!ret) |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1955 | clk->core->phase = degrees; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1956 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1957 | clk_prepare_unlock(); |
| 1958 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1959 | return ret; |
| 1960 | } |
Maxime Ripard | 9767b04f | 2015-01-20 22:23:43 +0100 | [diff] [blame] | 1961 | EXPORT_SYMBOL_GPL(clk_set_phase); |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1962 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1963 | static int clk_core_get_phase(struct clk_core *core) |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1964 | { |
Stephen Boyd | 1f3e198 | 2015-04-30 14:21:56 -0700 | [diff] [blame] | 1965 | int ret; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1966 | |
| 1967 | clk_prepare_lock(); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 1968 | ret = core->phase; |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1969 | clk_prepare_unlock(); |
| 1970 | |
Mike Turquette | e59c537 | 2014-02-18 21:21:25 -0800 | [diff] [blame] | 1971 | return ret; |
| 1972 | } |
| 1973 | |
| 1974 | /** |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 1975 | * clk_get_phase - return the phase shift of a clock signal |
| 1976 | * @clk: clock signal source |
| 1977 | * |
| 1978 | * Returns the phase shift of a clock node in degrees, otherwise returns |
| 1979 | * -EERROR. |
| 1980 | */ |
| 1981 | int clk_get_phase(struct clk *clk) |
| 1982 | { |
| 1983 | if (!clk) |
| 1984 | return 0; |
| 1985 | |
| 1986 | return clk_core_get_phase(clk->core); |
| 1987 | } |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 1988 | EXPORT_SYMBOL_GPL(clk_get_phase); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 1989 | |
| 1990 | /** |
Michael Turquette | 3d3801e | 2015-02-25 09:11:01 -0800 | [diff] [blame] | 1991 | * clk_is_match - check if two clk's point to the same hardware clock |
| 1992 | * @p: clk compared against q |
| 1993 | * @q: clk compared against p |
| 1994 | * |
| 1995 | * Returns true if the two struct clk pointers both point to the same hardware |
| 1996 | * clock node. Put differently, returns true if struct clk *p and struct clk *q |
| 1997 | * share the same struct clk_core object. |
| 1998 | * |
| 1999 | * Returns false otherwise. Note that two NULL clks are treated as matching. |
| 2000 | */ |
| 2001 | bool clk_is_match(const struct clk *p, const struct clk *q) |
| 2002 | { |
| 2003 | /* trivial case: identical struct clk's or both NULL */ |
| 2004 | if (p == q) |
| 2005 | return true; |
| 2006 | |
| 2007 | /* true if clk->core pointers match. Avoid derefing garbage */ |
| 2008 | if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q)) |
| 2009 | if (p->core == q->core) |
| 2010 | return true; |
| 2011 | |
| 2012 | return false; |
| 2013 | } |
| 2014 | EXPORT_SYMBOL_GPL(clk_is_match); |
| 2015 | |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2016 | /*** debugfs support ***/ |
| 2017 | |
| 2018 | #ifdef CONFIG_DEBUG_FS |
| 2019 | #include <linux/debugfs.h> |
| 2020 | |
| 2021 | static struct dentry *rootdir; |
| 2022 | static int inited = 0; |
| 2023 | static DEFINE_MUTEX(clk_debug_lock); |
| 2024 | static HLIST_HEAD(clk_debug_list); |
| 2025 | |
| 2026 | static struct hlist_head *all_lists[] = { |
| 2027 | &clk_root_list, |
| 2028 | &clk_orphan_list, |
| 2029 | NULL, |
| 2030 | }; |
| 2031 | |
| 2032 | static struct hlist_head *orphan_list[] = { |
| 2033 | &clk_orphan_list, |
| 2034 | NULL, |
| 2035 | }; |
| 2036 | |
| 2037 | static void clk_summary_show_one(struct seq_file *s, struct clk_core *c, |
| 2038 | int level) |
| 2039 | { |
| 2040 | if (!c) |
| 2041 | return; |
| 2042 | |
| 2043 | seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n", |
| 2044 | level * 3 + 1, "", |
| 2045 | 30 - level * 3, c->name, |
| 2046 | c->enable_count, c->prepare_count, clk_core_get_rate(c), |
| 2047 | clk_core_get_accuracy(c), clk_core_get_phase(c)); |
| 2048 | } |
| 2049 | |
| 2050 | static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c, |
| 2051 | int level) |
| 2052 | { |
| 2053 | struct clk_core *child; |
| 2054 | |
| 2055 | if (!c) |
| 2056 | return; |
| 2057 | |
| 2058 | clk_summary_show_one(s, c, level); |
| 2059 | |
| 2060 | hlist_for_each_entry(child, &c->children, child_node) |
| 2061 | clk_summary_show_subtree(s, child, level + 1); |
| 2062 | } |
| 2063 | |
| 2064 | static int clk_summary_show(struct seq_file *s, void *data) |
| 2065 | { |
| 2066 | struct clk_core *c; |
| 2067 | struct hlist_head **lists = (struct hlist_head **)s->private; |
| 2068 | |
| 2069 | seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n"); |
| 2070 | seq_puts(s, "----------------------------------------------------------------------------------------\n"); |
| 2071 | |
| 2072 | clk_prepare_lock(); |
| 2073 | |
| 2074 | for (; *lists; lists++) |
| 2075 | hlist_for_each_entry(c, *lists, child_node) |
| 2076 | clk_summary_show_subtree(s, c, 0); |
| 2077 | |
| 2078 | clk_prepare_unlock(); |
| 2079 | |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | |
| 2084 | static int clk_summary_open(struct inode *inode, struct file *file) |
| 2085 | { |
| 2086 | return single_open(file, clk_summary_show, inode->i_private); |
| 2087 | } |
| 2088 | |
| 2089 | static const struct file_operations clk_summary_fops = { |
| 2090 | .open = clk_summary_open, |
| 2091 | .read = seq_read, |
| 2092 | .llseek = seq_lseek, |
| 2093 | .release = single_release, |
| 2094 | }; |
| 2095 | |
| 2096 | static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level) |
| 2097 | { |
| 2098 | if (!c) |
| 2099 | return; |
| 2100 | |
Stefan Wahren | 7cb8113 | 2015-04-29 16:36:43 +0000 | [diff] [blame] | 2101 | /* This should be JSON format, i.e. elements separated with a comma */ |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2102 | seq_printf(s, "\"%s\": { ", c->name); |
| 2103 | seq_printf(s, "\"enable_count\": %d,", c->enable_count); |
| 2104 | seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); |
Stefan Wahren | 7cb8113 | 2015-04-29 16:36:43 +0000 | [diff] [blame] | 2105 | seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c)); |
| 2106 | seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c)); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2107 | seq_printf(s, "\"phase\": %d", clk_core_get_phase(c)); |
| 2108 | } |
| 2109 | |
| 2110 | static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level) |
| 2111 | { |
| 2112 | struct clk_core *child; |
| 2113 | |
| 2114 | if (!c) |
| 2115 | return; |
| 2116 | |
| 2117 | clk_dump_one(s, c, level); |
| 2118 | |
| 2119 | hlist_for_each_entry(child, &c->children, child_node) { |
| 2120 | seq_printf(s, ","); |
| 2121 | clk_dump_subtree(s, child, level + 1); |
| 2122 | } |
| 2123 | |
| 2124 | seq_printf(s, "}"); |
| 2125 | } |
| 2126 | |
| 2127 | static int clk_dump(struct seq_file *s, void *data) |
| 2128 | { |
| 2129 | struct clk_core *c; |
| 2130 | bool first_node = true; |
| 2131 | struct hlist_head **lists = (struct hlist_head **)s->private; |
| 2132 | |
| 2133 | seq_printf(s, "{"); |
| 2134 | |
| 2135 | clk_prepare_lock(); |
| 2136 | |
| 2137 | for (; *lists; lists++) { |
| 2138 | hlist_for_each_entry(c, *lists, child_node) { |
| 2139 | if (!first_node) |
| 2140 | seq_puts(s, ","); |
| 2141 | first_node = false; |
| 2142 | clk_dump_subtree(s, c, 0); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | clk_prepare_unlock(); |
| 2147 | |
Felipe Balbi | 70e9f4d | 2015-05-01 09:48:37 -0500 | [diff] [blame] | 2148 | seq_puts(s, "}\n"); |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | |
| 2153 | static int clk_dump_open(struct inode *inode, struct file *file) |
| 2154 | { |
| 2155 | return single_open(file, clk_dump, inode->i_private); |
| 2156 | } |
| 2157 | |
| 2158 | static const struct file_operations clk_dump_fops = { |
| 2159 | .open = clk_dump_open, |
| 2160 | .read = seq_read, |
| 2161 | .llseek = seq_lseek, |
| 2162 | .release = single_release, |
| 2163 | }; |
| 2164 | |
| 2165 | static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry) |
| 2166 | { |
| 2167 | struct dentry *d; |
| 2168 | int ret = -ENOMEM; |
| 2169 | |
| 2170 | if (!core || !pdentry) { |
| 2171 | ret = -EINVAL; |
| 2172 | goto out; |
| 2173 | } |
| 2174 | |
| 2175 | d = debugfs_create_dir(core->name, pdentry); |
| 2176 | if (!d) |
| 2177 | goto out; |
| 2178 | |
| 2179 | core->dentry = d; |
| 2180 | |
| 2181 | d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry, |
| 2182 | (u32 *)&core->rate); |
| 2183 | if (!d) |
| 2184 | goto err_out; |
| 2185 | |
| 2186 | d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry, |
| 2187 | (u32 *)&core->accuracy); |
| 2188 | if (!d) |
| 2189 | goto err_out; |
| 2190 | |
| 2191 | d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry, |
| 2192 | (u32 *)&core->phase); |
| 2193 | if (!d) |
| 2194 | goto err_out; |
| 2195 | |
| 2196 | d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry, |
| 2197 | (u32 *)&core->flags); |
| 2198 | if (!d) |
| 2199 | goto err_out; |
| 2200 | |
| 2201 | d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry, |
| 2202 | (u32 *)&core->prepare_count); |
| 2203 | if (!d) |
| 2204 | goto err_out; |
| 2205 | |
| 2206 | d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry, |
| 2207 | (u32 *)&core->enable_count); |
| 2208 | if (!d) |
| 2209 | goto err_out; |
| 2210 | |
| 2211 | d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry, |
| 2212 | (u32 *)&core->notifier_count); |
| 2213 | if (!d) |
| 2214 | goto err_out; |
| 2215 | |
| 2216 | if (core->ops->debug_init) { |
| 2217 | ret = core->ops->debug_init(core->hw, core->dentry); |
| 2218 | if (ret) |
| 2219 | goto err_out; |
| 2220 | } |
| 2221 | |
| 2222 | ret = 0; |
| 2223 | goto out; |
| 2224 | |
| 2225 | err_out: |
| 2226 | debugfs_remove_recursive(core->dentry); |
| 2227 | core->dentry = NULL; |
| 2228 | out: |
| 2229 | return ret; |
| 2230 | } |
| 2231 | |
| 2232 | /** |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2233 | * clk_debug_register - add a clk node to the debugfs clk directory |
| 2234 | * @core: the clk being added to the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2235 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2236 | * Dynamically adds a clk to the debugfs clk directory if debugfs has been |
| 2237 | * initialized. Otherwise it bails out early since the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2238 | * will be created lazily by clk_debug_init as part of a late_initcall. |
| 2239 | */ |
| 2240 | static int clk_debug_register(struct clk_core *core) |
| 2241 | { |
| 2242 | int ret = 0; |
| 2243 | |
| 2244 | mutex_lock(&clk_debug_lock); |
| 2245 | hlist_add_head(&core->debug_node, &clk_debug_list); |
| 2246 | |
| 2247 | if (!inited) |
| 2248 | goto unlock; |
| 2249 | |
| 2250 | ret = clk_debug_create_one(core, rootdir); |
| 2251 | unlock: |
| 2252 | mutex_unlock(&clk_debug_lock); |
| 2253 | |
| 2254 | return ret; |
| 2255 | } |
| 2256 | |
| 2257 | /** |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2258 | * clk_debug_unregister - remove a clk node from the debugfs clk directory |
| 2259 | * @core: the clk being removed from the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2260 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2261 | * Dynamically removes a clk and all its child nodes from the |
| 2262 | * debugfs clk directory if clk->dentry points to debugfs created by |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2263 | * clk_debug_register in __clk_init. |
| 2264 | */ |
| 2265 | static void clk_debug_unregister(struct clk_core *core) |
| 2266 | { |
| 2267 | mutex_lock(&clk_debug_lock); |
| 2268 | hlist_del_init(&core->debug_node); |
| 2269 | debugfs_remove_recursive(core->dentry); |
| 2270 | core->dentry = NULL; |
| 2271 | mutex_unlock(&clk_debug_lock); |
| 2272 | } |
| 2273 | |
| 2274 | struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode, |
| 2275 | void *data, const struct file_operations *fops) |
| 2276 | { |
| 2277 | struct dentry *d = NULL; |
| 2278 | |
| 2279 | if (hw->core->dentry) |
| 2280 | d = debugfs_create_file(name, mode, hw->core->dentry, data, |
| 2281 | fops); |
| 2282 | |
| 2283 | return d; |
| 2284 | } |
| 2285 | EXPORT_SYMBOL_GPL(clk_debugfs_add_file); |
| 2286 | |
| 2287 | /** |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2288 | * clk_debug_init - lazily populate the debugfs clk directory |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2289 | * |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2290 | * clks are often initialized very early during boot before memory can be |
| 2291 | * dynamically allocated and well before debugfs is setup. This function |
| 2292 | * populates the debugfs clk directory once at boot-time when we know that |
| 2293 | * debugfs is setup. It should only be called once at boot-time, all other clks |
| 2294 | * added dynamically will be done so with clk_debug_register. |
Stephen Boyd | 4dff95d | 2015-04-30 14:43:22 -0700 | [diff] [blame] | 2295 | */ |
| 2296 | static int __init clk_debug_init(void) |
| 2297 | { |
| 2298 | struct clk_core *core; |
| 2299 | struct dentry *d; |
| 2300 | |
| 2301 | rootdir = debugfs_create_dir("clk", NULL); |
| 2302 | |
| 2303 | if (!rootdir) |
| 2304 | return -ENOMEM; |
| 2305 | |
| 2306 | d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists, |
| 2307 | &clk_summary_fops); |
| 2308 | if (!d) |
| 2309 | return -ENOMEM; |
| 2310 | |
| 2311 | d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists, |
| 2312 | &clk_dump_fops); |
| 2313 | if (!d) |
| 2314 | return -ENOMEM; |
| 2315 | |
| 2316 | d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir, |
| 2317 | &orphan_list, &clk_summary_fops); |
| 2318 | if (!d) |
| 2319 | return -ENOMEM; |
| 2320 | |
| 2321 | d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir, |
| 2322 | &orphan_list, &clk_dump_fops); |
| 2323 | if (!d) |
| 2324 | return -ENOMEM; |
| 2325 | |
| 2326 | mutex_lock(&clk_debug_lock); |
| 2327 | hlist_for_each_entry(core, &clk_debug_list, debug_node) |
| 2328 | clk_debug_create_one(core, rootdir); |
| 2329 | |
| 2330 | inited = 1; |
| 2331 | mutex_unlock(&clk_debug_lock); |
| 2332 | |
| 2333 | return 0; |
| 2334 | } |
| 2335 | late_initcall(clk_debug_init); |
| 2336 | #else |
| 2337 | static inline int clk_debug_register(struct clk_core *core) { return 0; } |
| 2338 | static inline void clk_debug_reparent(struct clk_core *core, |
| 2339 | struct clk_core *new_parent) |
| 2340 | { |
| 2341 | } |
| 2342 | static inline void clk_debug_unregister(struct clk_core *core) |
| 2343 | { |
| 2344 | } |
| 2345 | #endif |
| 2346 | |
Michael Turquette | 3d3801e | 2015-02-25 09:11:01 -0800 | [diff] [blame] | 2347 | /** |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2348 | * __clk_init - initialize the data structures in a struct clk |
| 2349 | * @dev: device initializing this clk, placeholder for now |
| 2350 | * @clk: clk being initialized |
| 2351 | * |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2352 | * Initializes the lists in struct clk_core, queries the hardware for the |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2353 | * parent and rate and sets them both. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2354 | */ |
Michael Turquette | b09d6d9 | 2015-01-29 14:22:50 -0800 | [diff] [blame] | 2355 | static int __clk_init(struct device *dev, struct clk *clk_user) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2356 | { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2357 | int i, ret = 0; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2358 | struct clk_core *orphan; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2359 | struct hlist_node *tmp2; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2360 | struct clk_core *core; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2361 | unsigned long rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2362 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2363 | if (!clk_user) |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2364 | return -EINVAL; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2365 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2366 | core = clk_user->core; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2367 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2368 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2369 | |
| 2370 | /* check to see if a clock with this name is already registered */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2371 | if (clk_core_lookup(core->name)) { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2372 | pr_debug("%s: clk %s already initialized\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2373 | __func__, core->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2374 | ret = -EEXIST; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2375 | goto out; |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2376 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2377 | |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2378 | /* check that clk_ops are sane. See Documentation/clk.txt */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2379 | if (core->ops->set_rate && |
| 2380 | !((core->ops->round_rate || core->ops->determine_rate) && |
| 2381 | core->ops->recalc_rate)) { |
James Hogan | 71472c0 | 2013-07-29 12:25:00 +0100 | [diff] [blame] | 2382 | pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2383 | __func__, core->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2384 | ret = -EINVAL; |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2385 | goto out; |
| 2386 | } |
| 2387 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2388 | if (core->ops->set_parent && !core->ops->get_parent) { |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2389 | pr_warning("%s: %s must implement .get_parent & .set_parent\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2390 | __func__, core->name); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2391 | ret = -EINVAL; |
Mike Turquette | d4d7e3d | 2012-03-26 16:15:52 -0700 | [diff] [blame] | 2392 | goto out; |
| 2393 | } |
| 2394 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2395 | if (core->ops->set_rate_and_parent && |
| 2396 | !(core->ops->set_parent && core->ops->set_rate)) { |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 2397 | pr_warn("%s: %s must implement .set_parent & .set_rate\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2398 | __func__, core->name); |
Stephen Boyd | 3fa2252 | 2014-01-15 10:47:22 -0800 | [diff] [blame] | 2399 | ret = -EINVAL; |
| 2400 | goto out; |
| 2401 | } |
| 2402 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2403 | /* throw a WARN if any entries in parent_names are NULL */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2404 | for (i = 0; i < core->num_parents; i++) |
| 2405 | WARN(!core->parent_names[i], |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2406 | "%s: invalid NULL in %s's .parent_names\n", |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2407 | __func__, core->name); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2408 | |
| 2409 | /* |
| 2410 | * Allocate an array of struct clk *'s to avoid unnecessary string |
| 2411 | * look-ups of clk's possible parents. This can fail for clocks passed |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2412 | * in to clk_init during early boot; thus any access to core->parents[] |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2413 | * must always check for a NULL pointer and try to populate it if |
| 2414 | * necessary. |
| 2415 | * |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2416 | * If core->parents is not NULL we skip this entire block. This allows |
| 2417 | * for clock drivers to statically initialize core->parents. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2418 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2419 | if (core->num_parents > 1 && !core->parents) { |
| 2420 | core->parents = kcalloc(core->num_parents, sizeof(struct clk *), |
Tomasz Figa | 96a7ed9 | 2013-09-29 02:37:15 +0200 | [diff] [blame] | 2421 | GFP_KERNEL); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2422 | /* |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2423 | * clk_core_lookup returns NULL for parents that have not been |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2424 | * clk_init'd; thus any access to clk->parents[] must check |
| 2425 | * for a NULL pointer. We can always perform lazy lookups for |
| 2426 | * missing parents later on. |
| 2427 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2428 | if (core->parents) |
| 2429 | for (i = 0; i < core->num_parents; i++) |
| 2430 | core->parents[i] = |
| 2431 | clk_core_lookup(core->parent_names[i]); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2432 | } |
| 2433 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2434 | core->parent = __clk_init_parent(core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2435 | |
| 2436 | /* |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2437 | * Populate core->parent if parent has already been __clk_init'd. If |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2438 | * parent has not yet been __clk_init'd then place clk in the orphan |
| 2439 | * list. If clk has set the CLK_IS_ROOT flag then place it in the root |
| 2440 | * clk list. |
| 2441 | * |
| 2442 | * Every time a new clk is clk_init'd then we walk the list of orphan |
| 2443 | * clocks and re-parent any that are children of the clock currently |
| 2444 | * being clk_init'd. |
| 2445 | */ |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2446 | if (core->parent) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2447 | hlist_add_head(&core->child_node, |
| 2448 | &core->parent->children); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2449 | core->orphan = core->parent->orphan; |
| 2450 | } else if (core->flags & CLK_IS_ROOT) { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2451 | hlist_add_head(&core->child_node, &clk_root_list); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2452 | core->orphan = false; |
| 2453 | } else { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2454 | hlist_add_head(&core->child_node, &clk_orphan_list); |
Heiko Stuebner | e650034 | 2015-04-22 22:53:05 +0200 | [diff] [blame] | 2455 | core->orphan = true; |
| 2456 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2457 | |
| 2458 | /* |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2459 | * Set clk's accuracy. The preferred method is to use |
| 2460 | * .recalc_accuracy. For simple clocks and lazy developers the default |
| 2461 | * fallback is to use the parent's accuracy. If a clock doesn't have a |
| 2462 | * parent (or is orphaned) then accuracy is set to zero (perfect |
| 2463 | * clock). |
| 2464 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2465 | if (core->ops->recalc_accuracy) |
| 2466 | core->accuracy = core->ops->recalc_accuracy(core->hw, |
| 2467 | __clk_get_accuracy(core->parent)); |
| 2468 | else if (core->parent) |
| 2469 | core->accuracy = core->parent->accuracy; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2470 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2471 | core->accuracy = 0; |
Boris BREZILLON | 5279fc4 | 2013-12-21 10:34:47 +0100 | [diff] [blame] | 2472 | |
| 2473 | /* |
Maxime Ripard | 9824cf7 | 2014-07-14 13:53:27 +0200 | [diff] [blame] | 2474 | * Set clk's phase. |
| 2475 | * Since a phase is by definition relative to its parent, just |
| 2476 | * query the current clock phase, or just assume it's in phase. |
| 2477 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2478 | if (core->ops->get_phase) |
| 2479 | core->phase = core->ops->get_phase(core->hw); |
Maxime Ripard | 9824cf7 | 2014-07-14 13:53:27 +0200 | [diff] [blame] | 2480 | else |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2481 | core->phase = 0; |
Maxime Ripard | 9824cf7 | 2014-07-14 13:53:27 +0200 | [diff] [blame] | 2482 | |
| 2483 | /* |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2484 | * Set clk's rate. The preferred method is to use .recalc_rate. For |
| 2485 | * simple clocks and lazy developers the default fallback is to use the |
| 2486 | * parent's rate. If a clock doesn't have a parent (or is orphaned) |
| 2487 | * then rate is set to zero. |
| 2488 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2489 | if (core->ops->recalc_rate) |
| 2490 | rate = core->ops->recalc_rate(core->hw, |
| 2491 | clk_core_get_rate_nolock(core->parent)); |
| 2492 | else if (core->parent) |
| 2493 | rate = core->parent->rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2494 | else |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2495 | rate = 0; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2496 | core->rate = core->req_rate = rate; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2497 | |
| 2498 | /* |
| 2499 | * walk the list of orphan clocks and reparent any that are children of |
| 2500 | * this clock |
| 2501 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2502 | hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { |
Alex Elder | 12d29886 | 2013-09-05 08:33:24 -0500 | [diff] [blame] | 2503 | if (orphan->num_parents && orphan->ops->get_parent) { |
Martin Fuzzey | 1f61e5f | 2012-11-22 20:15:05 +0100 | [diff] [blame] | 2504 | i = orphan->ops->get_parent(orphan->hw); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2505 | if (!strcmp(core->name, orphan->parent_names[i])) |
| 2506 | clk_core_reparent(orphan, core); |
Martin Fuzzey | 1f61e5f | 2012-11-22 20:15:05 +0100 | [diff] [blame] | 2507 | continue; |
| 2508 | } |
| 2509 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2510 | for (i = 0; i < orphan->num_parents; i++) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2511 | if (!strcmp(core->name, orphan->parent_names[i])) { |
| 2512 | clk_core_reparent(orphan, core); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2513 | break; |
| 2514 | } |
Martin Fuzzey | 1f61e5f | 2012-11-22 20:15:05 +0100 | [diff] [blame] | 2515 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2516 | |
| 2517 | /* |
| 2518 | * optional platform-specific magic |
| 2519 | * |
| 2520 | * The .init callback is not used by any of the basic clock types, but |
| 2521 | * exists for weird hardware that must perform initialization magic. |
| 2522 | * Please consider other ways of solving initialization problems before |
Peter Meerwald | 24ee1a0 | 2013-06-29 15:14:19 +0200 | [diff] [blame] | 2523 | * using this callback, as its use is discouraged. |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2524 | */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2525 | if (core->ops->init) |
| 2526 | core->ops->init(core->hw); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2527 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2528 | kref_init(&core->ref); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2529 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2530 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2531 | |
Stephen Boyd | 89f7e9d | 2014-12-12 15:04:16 -0800 | [diff] [blame] | 2532 | if (!ret) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2533 | clk_debug_register(core); |
Stephen Boyd | 89f7e9d | 2014-12-12 15:04:16 -0800 | [diff] [blame] | 2534 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2535 | return ret; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2538 | struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id, |
| 2539 | const char *con_id) |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2540 | { |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2541 | struct clk *clk; |
| 2542 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2543 | /* This is to allow this function to be chained to others */ |
| 2544 | if (!hw || IS_ERR(hw)) |
| 2545 | return (struct clk *) hw; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2546 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2547 | clk = kzalloc(sizeof(*clk), GFP_KERNEL); |
| 2548 | if (!clk) |
| 2549 | return ERR_PTR(-ENOMEM); |
| 2550 | |
| 2551 | clk->core = hw->core; |
| 2552 | clk->dev_id = dev_id; |
| 2553 | clk->con_id = con_id; |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2554 | clk->max_rate = ULONG_MAX; |
| 2555 | |
| 2556 | clk_prepare_lock(); |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 2557 | hlist_add_head(&clk->clks_node, &hw->core->clks); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2558 | clk_prepare_unlock(); |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2559 | |
| 2560 | return clk; |
| 2561 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2562 | |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 2563 | void __clk_free_clk(struct clk *clk) |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2564 | { |
| 2565 | clk_prepare_lock(); |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 2566 | hlist_del(&clk->clks_node); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2567 | clk_prepare_unlock(); |
| 2568 | |
| 2569 | kfree(clk); |
| 2570 | } |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2571 | |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2572 | /** |
| 2573 | * clk_register - allocate a new clock, register it and return an opaque cookie |
| 2574 | * @dev: device that is registering this clock |
| 2575 | * @hw: link to hardware-specific clock data |
| 2576 | * |
| 2577 | * clk_register is the primary interface for populating the clock tree with new |
| 2578 | * 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] | 2579 | * 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] | 2580 | * rest of the clock API. In the event of an error clk_register will return an |
| 2581 | * error code; drivers must test for an error code after calling clk_register. |
| 2582 | */ |
| 2583 | struct clk *clk_register(struct device *dev, struct clk_hw *hw) |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2584 | { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2585 | int i, ret; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2586 | struct clk_core *core; |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2587 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2588 | core = kzalloc(sizeof(*core), GFP_KERNEL); |
| 2589 | if (!core) { |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2590 | ret = -ENOMEM; |
| 2591 | goto fail_out; |
| 2592 | } |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2593 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2594 | core->name = kstrdup_const(hw->init->name, GFP_KERNEL); |
| 2595 | if (!core->name) { |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2596 | ret = -ENOMEM; |
| 2597 | goto fail_name; |
| 2598 | } |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2599 | core->ops = hw->init->ops; |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 2600 | if (dev && dev->driver) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2601 | core->owner = dev->driver->owner; |
| 2602 | core->hw = hw; |
| 2603 | core->flags = hw->init->flags; |
| 2604 | core->num_parents = hw->init->num_parents; |
Stephen Boyd | 9783c0d | 2015-07-16 12:50:27 -0700 | [diff] [blame] | 2605 | core->min_rate = 0; |
| 2606 | core->max_rate = ULONG_MAX; |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2607 | hw->core = core; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2608 | |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2609 | /* allocate local copy in case parent_names is __initdata */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2610 | core->parent_names = kcalloc(core->num_parents, sizeof(char *), |
Tomasz Figa | 96a7ed9 | 2013-09-29 02:37:15 +0200 | [diff] [blame] | 2611 | GFP_KERNEL); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2612 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2613 | if (!core->parent_names) { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2614 | ret = -ENOMEM; |
| 2615 | goto fail_parent_names; |
| 2616 | } |
| 2617 | |
| 2618 | |
| 2619 | /* copy each string name in case parent_names is __initdata */ |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2620 | for (i = 0; i < core->num_parents; i++) { |
| 2621 | core->parent_names[i] = kstrdup_const(hw->init->parent_names[i], |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2622 | GFP_KERNEL); |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2623 | if (!core->parent_names[i]) { |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2624 | ret = -ENOMEM; |
| 2625 | goto fail_parent_names_copy; |
| 2626 | } |
| 2627 | } |
| 2628 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2629 | INIT_HLIST_HEAD(&core->clks); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2630 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2631 | hw->clk = __clk_create_clk(hw, NULL, NULL); |
| 2632 | if (IS_ERR(hw->clk)) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2633 | ret = PTR_ERR(hw->clk); |
| 2634 | goto fail_parent_names_copy; |
| 2635 | } |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2636 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2637 | ret = __clk_init(dev, hw->clk); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2638 | if (!ret) |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2639 | return hw->clk; |
| 2640 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2641 | __clk_free_clk(hw->clk); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2642 | hw->clk = NULL; |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2643 | |
| 2644 | fail_parent_names_copy: |
| 2645 | while (--i >= 0) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2646 | kfree_const(core->parent_names[i]); |
| 2647 | kfree(core->parent_names); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2648 | fail_parent_names: |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2649 | kfree_const(core->name); |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 2650 | fail_name: |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2651 | kfree(core); |
Mike Turquette | d1302a3 | 2012-03-29 14:30:40 -0700 | [diff] [blame] | 2652 | fail_out: |
| 2653 | return ERR_PTR(ret); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2654 | } |
| 2655 | EXPORT_SYMBOL_GPL(clk_register); |
| 2656 | |
Stephen Boyd | 6e5ab41 | 2015-04-30 15:11:31 -0700 | [diff] [blame] | 2657 | /* Free memory allocated for a clock. */ |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2658 | static void __clk_release(struct kref *ref) |
| 2659 | { |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2660 | struct clk_core *core = container_of(ref, struct clk_core, ref); |
| 2661 | int i = core->num_parents; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2662 | |
Krzysztof Kozlowski | 496eadf | 2015-01-09 09:28:10 +0100 | [diff] [blame] | 2663 | lockdep_assert_held(&prepare_lock); |
| 2664 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2665 | kfree(core->parents); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2666 | while (--i >= 0) |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2667 | kfree_const(core->parent_names[i]); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2668 | |
Stephen Boyd | d6968fc | 2015-04-30 13:54:13 -0700 | [diff] [blame] | 2669 | kfree(core->parent_names); |
| 2670 | kfree_const(core->name); |
| 2671 | kfree(core); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * Empty clk_ops for unregistered clocks. These are used temporarily |
| 2676 | * after clk_unregister() was called on a clock and until last clock |
| 2677 | * consumer calls clk_put() and the struct clk object is freed. |
| 2678 | */ |
| 2679 | static int clk_nodrv_prepare_enable(struct clk_hw *hw) |
| 2680 | { |
| 2681 | return -ENXIO; |
| 2682 | } |
| 2683 | |
| 2684 | static void clk_nodrv_disable_unprepare(struct clk_hw *hw) |
| 2685 | { |
| 2686 | WARN_ON_ONCE(1); |
| 2687 | } |
| 2688 | |
| 2689 | static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate, |
| 2690 | unsigned long parent_rate) |
| 2691 | { |
| 2692 | return -ENXIO; |
| 2693 | } |
| 2694 | |
| 2695 | static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index) |
| 2696 | { |
| 2697 | return -ENXIO; |
| 2698 | } |
| 2699 | |
| 2700 | static const struct clk_ops clk_nodrv_ops = { |
| 2701 | .enable = clk_nodrv_prepare_enable, |
| 2702 | .disable = clk_nodrv_disable_unprepare, |
| 2703 | .prepare = clk_nodrv_prepare_enable, |
| 2704 | .unprepare = clk_nodrv_disable_unprepare, |
| 2705 | .set_rate = clk_nodrv_set_rate, |
| 2706 | .set_parent = clk_nodrv_set_parent, |
| 2707 | }; |
| 2708 | |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 2709 | /** |
| 2710 | * clk_unregister - unregister a currently registered clock |
| 2711 | * @clk: clock to unregister |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 2712 | */ |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2713 | void clk_unregister(struct clk *clk) |
| 2714 | { |
| 2715 | unsigned long flags; |
| 2716 | |
Stephen Boyd | 6314b67 | 2014-09-04 23:37:49 -0700 | [diff] [blame] | 2717 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
| 2718 | return; |
| 2719 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2720 | clk_debug_unregister(clk->core); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2721 | |
| 2722 | clk_prepare_lock(); |
| 2723 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2724 | if (clk->core->ops == &clk_nodrv_ops) { |
| 2725 | pr_err("%s: unregistered clock: %s\n", __func__, |
| 2726 | clk->core->name); |
Stephen Boyd | 6314b67 | 2014-09-04 23:37:49 -0700 | [diff] [blame] | 2727 | return; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2728 | } |
| 2729 | /* |
| 2730 | * Assign empty clock ops for consumers that might still hold |
| 2731 | * a reference to this clock. |
| 2732 | */ |
| 2733 | flags = clk_enable_lock(); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2734 | clk->core->ops = &clk_nodrv_ops; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2735 | clk_enable_unlock(flags); |
| 2736 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2737 | if (!hlist_empty(&clk->core->children)) { |
| 2738 | struct clk_core *child; |
Stephen Boyd | 874f224 | 2014-04-18 16:29:43 -0700 | [diff] [blame] | 2739 | struct hlist_node *t; |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2740 | |
| 2741 | /* Reparent all children to the orphan list. */ |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2742 | hlist_for_each_entry_safe(child, t, &clk->core->children, |
| 2743 | child_node) |
| 2744 | clk_core_set_parent(child, NULL); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2745 | } |
| 2746 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2747 | hlist_del_init(&clk->core->child_node); |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2748 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2749 | if (clk->core->prepare_count) |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2750 | pr_warn("%s: unregistering prepared clock: %s\n", |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2751 | __func__, clk->core->name); |
| 2752 | kref_put(&clk->core->ref, __clk_release); |
Stephen Boyd | 6314b67 | 2014-09-04 23:37:49 -0700 | [diff] [blame] | 2753 | |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2754 | clk_prepare_unlock(); |
| 2755 | } |
Mark Brown | 1df5c93 | 2012-04-18 09:07:12 +0100 | [diff] [blame] | 2756 | EXPORT_SYMBOL_GPL(clk_unregister); |
| 2757 | |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 2758 | static void devm_clk_release(struct device *dev, void *res) |
| 2759 | { |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2760 | clk_unregister(*(struct clk **)res); |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | /** |
| 2764 | * devm_clk_register - resource managed clk_register() |
| 2765 | * @dev: device that is registering this clock |
| 2766 | * @hw: link to hardware-specific clock data |
| 2767 | * |
| 2768 | * Managed clk_register(). Clocks returned from this function are |
| 2769 | * automatically clk_unregister()ed on driver detach. See clk_register() for |
| 2770 | * more information. |
| 2771 | */ |
| 2772 | struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) |
| 2773 | { |
| 2774 | struct clk *clk; |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2775 | struct clk **clkp; |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 2776 | |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2777 | clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL); |
| 2778 | if (!clkp) |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 2779 | return ERR_PTR(-ENOMEM); |
| 2780 | |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2781 | clk = clk_register(dev, hw); |
| 2782 | if (!IS_ERR(clk)) { |
| 2783 | *clkp = clk; |
| 2784 | devres_add(dev, clkp); |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 2785 | } else { |
Stephen Boyd | 293ba3b | 2014-04-18 16:29:42 -0700 | [diff] [blame] | 2786 | devres_free(clkp); |
Stephen Boyd | 46c8773 | 2012-09-24 13:38:04 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | return clk; |
| 2790 | } |
| 2791 | EXPORT_SYMBOL_GPL(devm_clk_register); |
| 2792 | |
| 2793 | static int devm_clk_match(struct device *dev, void *res, void *data) |
| 2794 | { |
| 2795 | struct clk *c = res; |
| 2796 | if (WARN_ON(!c)) |
| 2797 | return 0; |
| 2798 | return c == data; |
| 2799 | } |
| 2800 | |
| 2801 | /** |
| 2802 | * devm_clk_unregister - resource managed clk_unregister() |
| 2803 | * @clk: clock to unregister |
| 2804 | * |
| 2805 | * Deallocate a clock allocated with devm_clk_register(). Normally |
| 2806 | * this function will not need to be called and the resource management |
| 2807 | * code will ensure that the resource is freed. |
| 2808 | */ |
| 2809 | void devm_clk_unregister(struct device *dev, struct clk *clk) |
| 2810 | { |
| 2811 | WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); |
| 2812 | } |
| 2813 | EXPORT_SYMBOL_GPL(devm_clk_unregister); |
| 2814 | |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 2815 | /* |
| 2816 | * clkdev helpers |
| 2817 | */ |
| 2818 | int __clk_get(struct clk *clk) |
| 2819 | { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2820 | struct clk_core *core = !clk ? NULL : clk->core; |
| 2821 | |
| 2822 | if (core) { |
| 2823 | if (!try_module_get(core->owner)) |
Sylwester Nawrocki | 00efcb1 | 2014-01-07 13:03:43 +0100 | [diff] [blame] | 2824 | return 0; |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 2825 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2826 | kref_get(&core->ref); |
Sylwester Nawrocki | 00efcb1 | 2014-01-07 13:03:43 +0100 | [diff] [blame] | 2827 | } |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 2828 | return 1; |
| 2829 | } |
| 2830 | |
| 2831 | void __clk_put(struct clk *clk) |
| 2832 | { |
Tomeu Vizoso | 10cdfe5 | 2014-12-02 08:54:19 +0100 | [diff] [blame] | 2833 | struct module *owner; |
| 2834 | |
Sylwester Nawrocki | 00efcb1 | 2014-01-07 13:03:43 +0100 | [diff] [blame] | 2835 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 2836 | return; |
| 2837 | |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2838 | clk_prepare_lock(); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2839 | |
Stephen Boyd | 50595f8 | 2015-02-06 11:42:44 -0800 | [diff] [blame] | 2840 | hlist_del(&clk->clks_node); |
Tomeu Vizoso | ec02ace | 2015-02-06 15:13:01 +0100 | [diff] [blame] | 2841 | if (clk->min_rate > clk->core->req_rate || |
| 2842 | clk->max_rate < clk->core->req_rate) |
| 2843 | clk_core_set_rate_nolock(clk->core, clk->core->req_rate); |
| 2844 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2845 | owner = clk->core->owner; |
| 2846 | kref_put(&clk->core->ref, __clk_release); |
| 2847 | |
Sylwester Nawrocki | fcb0ee6 | 2013-08-24 15:00:10 +0200 | [diff] [blame] | 2848 | clk_prepare_unlock(); |
| 2849 | |
Tomeu Vizoso | 10cdfe5 | 2014-12-02 08:54:19 +0100 | [diff] [blame] | 2850 | module_put(owner); |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 2851 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2852 | kfree(clk); |
Sylwester Nawrocki | ac2df52 | 2013-08-24 20:10:41 +0200 | [diff] [blame] | 2853 | } |
| 2854 | |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2855 | /*** clk rate change notifiers ***/ |
| 2856 | |
| 2857 | /** |
| 2858 | * clk_notifier_register - add a clk rate change notifier |
| 2859 | * @clk: struct clk * to watch |
| 2860 | * @nb: struct notifier_block * with callback info |
| 2861 | * |
| 2862 | * Request notification when clk's rate changes. This uses an SRCU |
| 2863 | * notifier because we want it to block and notifier unregistrations are |
| 2864 | * uncommon. The callbacks associated with the notifier must not |
| 2865 | * re-enter into the clk framework by calling any top-level clk APIs; |
| 2866 | * this will cause a nested prepare_lock mutex. |
| 2867 | * |
Soren Brinkmann | 5324fda | 2014-01-22 11:48:37 -0800 | [diff] [blame] | 2868 | * In all notification cases cases (pre, post and abort rate change) the |
| 2869 | * original clock rate is passed to the callback via struct |
| 2870 | * clk_notifier_data.old_rate and the new frequency is passed via struct |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2871 | * clk_notifier_data.new_rate. |
| 2872 | * |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2873 | * clk_notifier_register() must be called from non-atomic context. |
| 2874 | * Returns -EINVAL if called with null arguments, -ENOMEM upon |
| 2875 | * allocation failure; otherwise, passes along the return value of |
| 2876 | * srcu_notifier_chain_register(). |
| 2877 | */ |
| 2878 | int clk_notifier_register(struct clk *clk, struct notifier_block *nb) |
| 2879 | { |
| 2880 | struct clk_notifier *cn; |
| 2881 | int ret = -ENOMEM; |
| 2882 | |
| 2883 | if (!clk || !nb) |
| 2884 | return -EINVAL; |
| 2885 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2886 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2887 | |
| 2888 | /* search the list of notifiers for this clk */ |
| 2889 | list_for_each_entry(cn, &clk_notifier_list, node) |
| 2890 | if (cn->clk == clk) |
| 2891 | break; |
| 2892 | |
| 2893 | /* if clk wasn't in the notifier list, allocate new clk_notifier */ |
| 2894 | if (cn->clk != clk) { |
| 2895 | cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL); |
| 2896 | if (!cn) |
| 2897 | goto out; |
| 2898 | |
| 2899 | cn->clk = clk; |
| 2900 | srcu_init_notifier_head(&cn->notifier_head); |
| 2901 | |
| 2902 | list_add(&cn->node, &clk_notifier_list); |
| 2903 | } |
| 2904 | |
| 2905 | ret = srcu_notifier_chain_register(&cn->notifier_head, nb); |
| 2906 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2907 | clk->core->notifier_count++; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2908 | |
| 2909 | out: |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2910 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2911 | |
| 2912 | return ret; |
| 2913 | } |
| 2914 | EXPORT_SYMBOL_GPL(clk_notifier_register); |
| 2915 | |
| 2916 | /** |
| 2917 | * clk_notifier_unregister - remove a clk rate change notifier |
| 2918 | * @clk: struct clk * |
| 2919 | * @nb: struct notifier_block * with callback info |
| 2920 | * |
| 2921 | * Request no further notification for changes to 'clk' and frees memory |
| 2922 | * allocated in clk_notifier_register. |
| 2923 | * |
| 2924 | * Returns -EINVAL if called with null arguments; otherwise, passes |
| 2925 | * along the return value of srcu_notifier_chain_unregister(). |
| 2926 | */ |
| 2927 | int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) |
| 2928 | { |
| 2929 | struct clk_notifier *cn = NULL; |
| 2930 | int ret = -EINVAL; |
| 2931 | |
| 2932 | if (!clk || !nb) |
| 2933 | return -EINVAL; |
| 2934 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2935 | clk_prepare_lock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2936 | |
| 2937 | list_for_each_entry(cn, &clk_notifier_list, node) |
| 2938 | if (cn->clk == clk) |
| 2939 | break; |
| 2940 | |
| 2941 | if (cn->clk == clk) { |
| 2942 | ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); |
| 2943 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 2944 | clk->core->notifier_count--; |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2945 | |
| 2946 | /* XXX the notifier code should handle this better */ |
| 2947 | if (!cn->notifier_head.head) { |
| 2948 | srcu_cleanup_notifier_head(&cn->notifier_head); |
Lai Jiangshan | 72b5322 | 2013-06-03 17:17:15 +0800 | [diff] [blame] | 2949 | list_del(&cn->node); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2950 | kfree(cn); |
| 2951 | } |
| 2952 | |
| 2953 | } else { |
| 2954 | ret = -ENOENT; |
| 2955 | } |
| 2956 | |
Mike Turquette | eab89f6 | 2013-03-28 13:59:01 -0700 | [diff] [blame] | 2957 | clk_prepare_unlock(); |
Mike Turquette | b2476490 | 2012-03-15 23:11:19 -0700 | [diff] [blame] | 2958 | |
| 2959 | return ret; |
| 2960 | } |
| 2961 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 2962 | |
| 2963 | #ifdef CONFIG_OF |
| 2964 | /** |
| 2965 | * struct of_clk_provider - Clock provider registration structure |
| 2966 | * @link: Entry in global list of clock providers |
| 2967 | * @node: Pointer to device tree node of clock provider |
| 2968 | * @get: Get clock callback. Returns NULL or a struct clk for the |
| 2969 | * given clock specifier |
| 2970 | * @data: context pointer to be passed into @get callback |
| 2971 | */ |
| 2972 | struct of_clk_provider { |
| 2973 | struct list_head link; |
| 2974 | |
| 2975 | struct device_node *node; |
| 2976 | struct clk *(*get)(struct of_phandle_args *clkspec, void *data); |
| 2977 | void *data; |
| 2978 | }; |
| 2979 | |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 2980 | static const struct of_device_id __clk_of_table_sentinel |
| 2981 | __used __section(__clk_of_table_end); |
| 2982 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 2983 | static LIST_HEAD(of_clk_providers); |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 2984 | static DEFINE_MUTEX(of_clk_mutex); |
| 2985 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 2986 | struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, |
| 2987 | void *data) |
| 2988 | { |
| 2989 | return data; |
| 2990 | } |
| 2991 | EXPORT_SYMBOL_GPL(of_clk_src_simple_get); |
| 2992 | |
Shawn Guo | 494bfec | 2012-08-22 21:36:27 +0800 | [diff] [blame] | 2993 | struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) |
| 2994 | { |
| 2995 | struct clk_onecell_data *clk_data = data; |
| 2996 | unsigned int idx = clkspec->args[0]; |
| 2997 | |
| 2998 | if (idx >= clk_data->clk_num) { |
| 2999 | pr_err("%s: invalid clock index %d\n", __func__, idx); |
| 3000 | return ERR_PTR(-EINVAL); |
| 3001 | } |
| 3002 | |
| 3003 | return clk_data->clks[idx]; |
| 3004 | } |
| 3005 | EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); |
| 3006 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3007 | /** |
| 3008 | * of_clk_add_provider() - Register a clock provider for a node |
| 3009 | * @np: Device node pointer associated with clock provider |
| 3010 | * @clk_src_get: callback for decoding clock |
| 3011 | * @data: context pointer for @clk_src_get callback. |
| 3012 | */ |
| 3013 | int of_clk_add_provider(struct device_node *np, |
| 3014 | struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, |
| 3015 | void *data), |
| 3016 | void *data) |
| 3017 | { |
| 3018 | struct of_clk_provider *cp; |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 3019 | int ret; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3020 | |
| 3021 | cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); |
| 3022 | if (!cp) |
| 3023 | return -ENOMEM; |
| 3024 | |
| 3025 | cp->node = of_node_get(np); |
| 3026 | cp->data = data; |
| 3027 | cp->get = clk_src_get; |
| 3028 | |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3029 | mutex_lock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3030 | list_add(&cp->link, &of_clk_providers); |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3031 | mutex_unlock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3032 | pr_debug("Added clock from %s\n", np->full_name); |
| 3033 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 3034 | ret = of_clk_set_defaults(np, true); |
| 3035 | if (ret < 0) |
| 3036 | of_clk_del_provider(np); |
| 3037 | |
| 3038 | return ret; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3039 | } |
| 3040 | EXPORT_SYMBOL_GPL(of_clk_add_provider); |
| 3041 | |
| 3042 | /** |
| 3043 | * of_clk_del_provider() - Remove a previously registered clock provider |
| 3044 | * @np: Device node pointer associated with clock provider |
| 3045 | */ |
| 3046 | void of_clk_del_provider(struct device_node *np) |
| 3047 | { |
| 3048 | struct of_clk_provider *cp; |
| 3049 | |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3050 | mutex_lock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3051 | list_for_each_entry(cp, &of_clk_providers, link) { |
| 3052 | if (cp->node == np) { |
| 3053 | list_del(&cp->link); |
| 3054 | of_node_put(cp->node); |
| 3055 | kfree(cp); |
| 3056 | break; |
| 3057 | } |
| 3058 | } |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3059 | mutex_unlock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3060 | } |
| 3061 | EXPORT_SYMBOL_GPL(of_clk_del_provider); |
| 3062 | |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3063 | struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec, |
| 3064 | const char *dev_id, const char *con_id) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3065 | { |
| 3066 | struct of_clk_provider *provider; |
Jean-Francois Moine | a34cd46 | 2013-11-25 19:47:04 +0100 | [diff] [blame] | 3067 | struct clk *clk = ERR_PTR(-EPROBE_DEFER); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3068 | |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3069 | if (!clkspec) |
| 3070 | return ERR_PTR(-EINVAL); |
| 3071 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3072 | /* Check if we have such a provider in our array */ |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3073 | mutex_lock(&of_clk_mutex); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3074 | list_for_each_entry(provider, &of_clk_providers, link) { |
| 3075 | if (provider->node == clkspec->np) |
| 3076 | clk = provider->get(clkspec, provider->data); |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3077 | if (!IS_ERR(clk)) { |
| 3078 | clk = __clk_create_clk(__clk_get_hw(clk), dev_id, |
| 3079 | con_id); |
| 3080 | |
| 3081 | if (!IS_ERR(clk) && !__clk_get(clk)) { |
| 3082 | __clk_free_clk(clk); |
| 3083 | clk = ERR_PTR(-ENOENT); |
| 3084 | } |
| 3085 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3086 | break; |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 3087 | } |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3088 | } |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3089 | mutex_unlock(&of_clk_mutex); |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3090 | |
| 3091 | return clk; |
| 3092 | } |
| 3093 | |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3094 | /** |
| 3095 | * of_clk_get_from_provider() - Lookup a clock from a clock provider |
| 3096 | * @clkspec: pointer to a clock specifier data structure |
| 3097 | * |
| 3098 | * This function looks up a struct clk from the registered list of clock |
| 3099 | * providers, an input is a clock specifier data structure as returned |
| 3100 | * from the of_parse_phandle_with_args() function call. |
| 3101 | */ |
Sylwester Nawrocki | d6782c2 | 2013-08-23 17:03:43 +0200 | [diff] [blame] | 3102 | struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) |
| 3103 | { |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 3104 | return __of_clk_get_from_provider(clkspec, NULL, __func__); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3105 | } |
| 3106 | |
Mike Turquette | f610274 | 2013-10-07 23:12:13 -0700 | [diff] [blame] | 3107 | int of_clk_get_parent_count(struct device_node *np) |
| 3108 | { |
| 3109 | return of_count_phandle_with_args(np, "clocks", "#clock-cells"); |
| 3110 | } |
| 3111 | EXPORT_SYMBOL_GPL(of_clk_get_parent_count); |
| 3112 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3113 | const char *of_clk_get_parent_name(struct device_node *np, int index) |
| 3114 | { |
| 3115 | struct of_phandle_args clkspec; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3116 | struct property *prop; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3117 | const char *clk_name; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3118 | const __be32 *vp; |
| 3119 | u32 pv; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3120 | int rc; |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3121 | int count; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3122 | |
| 3123 | if (index < 0) |
| 3124 | return NULL; |
| 3125 | |
| 3126 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, |
| 3127 | &clkspec); |
| 3128 | if (rc) |
| 3129 | return NULL; |
| 3130 | |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3131 | index = clkspec.args_count ? clkspec.args[0] : 0; |
| 3132 | count = 0; |
| 3133 | |
| 3134 | /* if there is an indices property, use it to transfer the index |
| 3135 | * specified into an array offset for the clock-output-names property. |
| 3136 | */ |
| 3137 | of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { |
| 3138 | if (index == pv) { |
| 3139 | index = count; |
| 3140 | break; |
| 3141 | } |
| 3142 | count++; |
| 3143 | } |
| 3144 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3145 | if (of_property_read_string_index(clkspec.np, "clock-output-names", |
Ben Dooks | 7a0fc1a | 2014-02-13 18:02:49 +0000 | [diff] [blame] | 3146 | index, |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3147 | &clk_name) < 0) |
| 3148 | clk_name = clkspec.np->name; |
| 3149 | |
| 3150 | of_node_put(clkspec.np); |
| 3151 | return clk_name; |
| 3152 | } |
| 3153 | EXPORT_SYMBOL_GPL(of_clk_get_parent_name); |
| 3154 | |
Dinh Nguyen | 2e61dfb | 2015-06-05 11:26:13 -0500 | [diff] [blame] | 3155 | /** |
| 3156 | * of_clk_parent_fill() - Fill @parents with names of @np's parents and return |
| 3157 | * number of parents |
| 3158 | * @np: Device node pointer associated with clock provider |
| 3159 | * @parents: pointer to char array that hold the parents' names |
| 3160 | * @size: size of the @parents array |
| 3161 | * |
| 3162 | * Return: number of parents for the clock node. |
| 3163 | */ |
| 3164 | int of_clk_parent_fill(struct device_node *np, const char **parents, |
| 3165 | unsigned int size) |
| 3166 | { |
| 3167 | unsigned int i = 0; |
| 3168 | |
| 3169 | while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL) |
| 3170 | i++; |
| 3171 | |
| 3172 | return i; |
| 3173 | } |
| 3174 | EXPORT_SYMBOL_GPL(of_clk_parent_fill); |
| 3175 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3176 | struct clock_provider { |
| 3177 | of_clk_init_cb_t clk_init_cb; |
| 3178 | struct device_node *np; |
| 3179 | struct list_head node; |
| 3180 | }; |
| 3181 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3182 | /* |
| 3183 | * This function looks for a parent clock. If there is one, then it |
| 3184 | * checks that the provider for this parent clock was initialized, in |
| 3185 | * this case the parent clock will be ready. |
| 3186 | */ |
| 3187 | static int parent_ready(struct device_node *np) |
| 3188 | { |
| 3189 | int i = 0; |
| 3190 | |
| 3191 | while (true) { |
| 3192 | struct clk *clk = of_clk_get(np, i); |
| 3193 | |
| 3194 | /* this parent is ready we can check the next one */ |
| 3195 | if (!IS_ERR(clk)) { |
| 3196 | clk_put(clk); |
| 3197 | i++; |
| 3198 | continue; |
| 3199 | } |
| 3200 | |
| 3201 | /* at least one parent is not ready, we exit now */ |
| 3202 | if (PTR_ERR(clk) == -EPROBE_DEFER) |
| 3203 | return 0; |
| 3204 | |
| 3205 | /* |
| 3206 | * Here we make assumption that the device tree is |
| 3207 | * written correctly. So an error means that there is |
| 3208 | * no more parent. As we didn't exit yet, then the |
| 3209 | * previous parent are ready. If there is no clock |
| 3210 | * parent, no need to wait for them, then we can |
| 3211 | * consider their absence as being ready |
| 3212 | */ |
| 3213 | return 1; |
| 3214 | } |
| 3215 | } |
| 3216 | |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3217 | /** |
| 3218 | * of_clk_init() - Scan and init clock providers from the DT |
| 3219 | * @matches: array of compatible values and init functions for providers. |
| 3220 | * |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3221 | * This function scans the device tree for matching clock providers |
Sylwester Nawrocki | e5ca8fb4 | 2014-03-27 12:08:36 +0100 | [diff] [blame] | 3222 | * and calls their initialization functions. It also does it by trying |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3223 | * to follow the dependencies. |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3224 | */ |
| 3225 | void __init of_clk_init(const struct of_device_id *matches) |
| 3226 | { |
Alex Elder | 7f7ed58 | 2013-08-22 11:31:31 -0500 | [diff] [blame] | 3227 | const struct of_device_id *match; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3228 | struct device_node *np; |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3229 | struct clock_provider *clk_provider, *next; |
| 3230 | bool is_init_done; |
| 3231 | bool force = false; |
Stephen Boyd | 2573a02 | 2015-07-06 16:50:00 -0700 | [diff] [blame] | 3232 | LIST_HEAD(clk_provider_list); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3233 | |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 3234 | if (!matches) |
Tero Kristo | 819b486 | 2013-10-22 11:39:36 +0300 | [diff] [blame] | 3235 | matches = &__clk_of_table; |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 3236 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3237 | /* First prepare the list of the clocks providers */ |
Alex Elder | 7f7ed58 | 2013-08-22 11:31:31 -0500 | [diff] [blame] | 3238 | for_each_matching_node_and_match(np, matches, &match) { |
Stephen Boyd | 2e3b19f | 2015-07-06 16:48:19 -0700 | [diff] [blame] | 3239 | struct clock_provider *parent; |
| 3240 | |
| 3241 | parent = kzalloc(sizeof(*parent), GFP_KERNEL); |
| 3242 | if (!parent) { |
| 3243 | list_for_each_entry_safe(clk_provider, next, |
| 3244 | &clk_provider_list, node) { |
| 3245 | list_del(&clk_provider->node); |
| 3246 | kfree(clk_provider); |
| 3247 | } |
| 3248 | return; |
| 3249 | } |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3250 | |
| 3251 | parent->clk_init_cb = match->data; |
| 3252 | parent->np = np; |
Sylwester Nawrocki | 3f6d439 | 2014-03-27 11:43:32 +0100 | [diff] [blame] | 3253 | list_add_tail(&parent->node, &clk_provider_list); |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | while (!list_empty(&clk_provider_list)) { |
| 3257 | is_init_done = false; |
| 3258 | list_for_each_entry_safe(clk_provider, next, |
| 3259 | &clk_provider_list, node) { |
| 3260 | if (force || parent_ready(clk_provider->np)) { |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 3261 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3262 | clk_provider->clk_init_cb(clk_provider->np); |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 3263 | of_clk_set_defaults(clk_provider->np, true); |
| 3264 | |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3265 | list_del(&clk_provider->node); |
| 3266 | kfree(clk_provider); |
| 3267 | is_init_done = true; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | /* |
Sylwester Nawrocki | e5ca8fb4 | 2014-03-27 12:08:36 +0100 | [diff] [blame] | 3272 | * We didn't manage to initialize any of the |
Gregory CLEMENT | 1771b10 | 2014-02-24 19:10:13 +0100 | [diff] [blame] | 3273 | * remaining providers during the last loop, so now we |
| 3274 | * initialize all the remaining ones unconditionally |
| 3275 | * in case the clock parent was not mandatory |
| 3276 | */ |
| 3277 | if (!is_init_done) |
| 3278 | force = true; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 3279 | } |
| 3280 | } |
| 3281 | #endif |