Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google, Inc. |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 3 | * Copyright (c) 2007-2013, The Linux Foundation. All rights reserved. |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/ctype.h> |
| 19 | #include <linux/debugfs.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 21 | #include <linux/clk.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/clkdev.h> |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Patrick Daly | 0ed3691 | 2013-09-26 18:14:51 -0700 | [diff] [blame] | 26 | #include <linux/io.h> |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 27 | |
Matt Wagantall | 33d01f5 | 2012-02-23 23:27:44 -0800 | [diff] [blame] | 28 | #include <mach/clk-provider.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 29 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 30 | #include "clock.h" |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 31 | |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 32 | static LIST_HEAD(clk_list); |
| 33 | static DEFINE_SPINLOCK(clk_list_lock); |
| 34 | |
| 35 | static struct dentry *debugfs_base; |
| 36 | static u32 debug_suspend; |
| 37 | |
| 38 | struct clk_table { |
| 39 | struct list_head node; |
| 40 | struct clk_lookup *clocks; |
| 41 | size_t num_clocks; |
| 42 | }; |
| 43 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 44 | static int clock_debug_rate_set(void *data, u64 val) |
| 45 | { |
| 46 | struct clk *clock = data; |
| 47 | int ret; |
| 48 | |
| 49 | /* Only increases to max rate will succeed, but that's actually good |
| 50 | * for debugging purposes so we don't check for error. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | if (clock->flags & CLKFLAG_MAX) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 52 | clk_set_max_rate(clock, val); |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 53 | ret = clk_set_rate(clock, val); |
| 54 | if (ret) |
Stephen Boyd | 753ab93 | 2012-08-02 13:14:38 -0700 | [diff] [blame] | 55 | pr_err("clk_set_rate(%s, %lu) failed (%d)\n", clock->dbg_name, |
| 56 | (unsigned long)val, ret); |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 57 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | static int clock_debug_rate_get(void *data, u64 *val) |
| 62 | { |
| 63 | struct clk *clock = data; |
| 64 | *val = clk_get_rate(clock); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get, |
| 69 | clock_debug_rate_set, "%llu\n"); |
| 70 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 71 | static struct clk *measure; |
| 72 | |
| 73 | static int clock_debug_measure_get(void *data, u64 *val) |
| 74 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 75 | struct clk *clock = data; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 76 | int ret, is_hw_gated; |
| 77 | |
| 78 | /* Check to see if the clock is in hardware gating mode */ |
Stephen Boyd | 0f7e564 | 2012-08-02 12:59:33 -0700 | [diff] [blame] | 79 | if (clock->ops->in_hwcg_mode) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 80 | is_hw_gated = clock->ops->in_hwcg_mode(clock); |
| 81 | else |
| 82 | is_hw_gated = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 83 | |
| 84 | ret = clk_set_parent(measure, clock); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 85 | if (!ret) { |
| 86 | /* |
| 87 | * Disable hw gating to get accurate rate measurements. Only do |
| 88 | * this if the clock is explictly enabled by software. This |
| 89 | * allows us to detect errors where clocks are on even though |
| 90 | * software is not requesting them to be on due to broken |
| 91 | * hardware gating signals. |
| 92 | */ |
| 93 | if (is_hw_gated && clock->count) |
| 94 | clock->ops->disable_hwcg(clock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 95 | *val = clk_get_rate(measure); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 96 | /* Reenable hwgating if it was disabled */ |
| 97 | if (is_hw_gated && clock->count) |
| 98 | clock->ops->enable_hwcg(clock); |
| 99 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get, |
| 105 | NULL, "%lld\n"); |
| 106 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 107 | static int clock_debug_enable_set(void *data, u64 val) |
| 108 | { |
| 109 | struct clk *clock = data; |
| 110 | int rc = 0; |
| 111 | |
| 112 | if (val) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 113 | rc = clk_prepare_enable(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 114 | else |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 115 | clk_disable_unprepare(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 116 | |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | static int clock_debug_enable_get(void *data, u64 *val) |
| 121 | { |
| 122 | struct clk *clock = data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 123 | int enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 124 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | if (clock->ops->is_enabled) |
| 126 | enabled = clock->ops->is_enabled(clock); |
| 127 | else |
| 128 | enabled = !!(clock->count); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 129 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 130 | *val = enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 135 | clock_debug_enable_set, "%lld\n"); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 136 | |
| 137 | static int clock_debug_local_get(void *data, u64 *val) |
| 138 | { |
| 139 | struct clk *clock = data; |
| 140 | |
Matt Wagantall | acb8d02 | 2012-02-14 15:28:23 -0800 | [diff] [blame] | 141 | if (!clock->ops->is_local) |
| 142 | *val = true; |
| 143 | else |
| 144 | *val = clock->ops->is_local(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get, |
| 150 | NULL, "%llu\n"); |
| 151 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 152 | static int clock_debug_hwcg_get(void *data, u64 *val) |
| 153 | { |
| 154 | struct clk *clock = data; |
Stephen Boyd | 0f7e564 | 2012-08-02 12:59:33 -0700 | [diff] [blame] | 155 | if (clock->ops->in_hwcg_mode) |
| 156 | *val = !!clock->ops->in_hwcg_mode(clock); |
| 157 | else |
| 158 | *val = 0; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | DEFINE_SIMPLE_ATTRIBUTE(clock_hwcg_fops, clock_debug_hwcg_get, |
| 163 | NULL, "%llu\n"); |
| 164 | |
xiaogang | c2a9ff1 | 2013-07-05 03:34:17 +0800 | [diff] [blame] | 165 | static void clock_print_fmax_by_level(struct seq_file *m, int level) |
| 166 | { |
| 167 | struct clk *clock = m->private; |
| 168 | struct clk_vdd_class *vdd_class = clock->vdd_class; |
| 169 | int off, i, vdd_level, nregs = vdd_class->num_regulators; |
| 170 | |
| 171 | vdd_level = find_vdd_level(clock, clock->rate); |
| 172 | |
| 173 | seq_printf(m, "%2s%10lu", vdd_level == level ? "[" : "", |
| 174 | clock->fmax[level]); |
| 175 | for (i = 0; i < nregs; i++) { |
| 176 | off = nregs*level + i; |
| 177 | if (vdd_class->vdd_uv) |
| 178 | seq_printf(m, "%10u", vdd_class->vdd_uv[off]); |
| 179 | if (vdd_class->vdd_ua) |
| 180 | seq_printf(m, "%10u", vdd_class->vdd_ua[off]); |
| 181 | } |
| 182 | |
| 183 | if (vdd_level == level) |
| 184 | seq_puts(m, "]"); |
| 185 | seq_puts(m, "\n"); |
| 186 | } |
| 187 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 188 | static int fmax_rates_show(struct seq_file *m, void *unused) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 189 | { |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 190 | struct clk *clock = m->private; |
xiaogang | c2a9ff1 | 2013-07-05 03:34:17 +0800 | [diff] [blame] | 191 | struct clk_vdd_class *vdd_class = clock->vdd_class; |
| 192 | int level = 0, i, nregs = vdd_class->num_regulators; |
| 193 | char reg_name[10]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 194 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 195 | int vdd_level = find_vdd_level(clock, clock->rate); |
| 196 | if (vdd_level < 0) { |
| 197 | seq_printf(m, "could not find_vdd_level for %s, %ld\n", |
xiaogang | c2a9ff1 | 2013-07-05 03:34:17 +0800 | [diff] [blame] | 198 | clock->dbg_name, clock->rate); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 199 | return 0; |
| 200 | } |
xiaogang | c2a9ff1 | 2013-07-05 03:34:17 +0800 | [diff] [blame] | 201 | |
| 202 | seq_printf(m, "%12s", ""); |
| 203 | for (i = 0; i < nregs; i++) { |
| 204 | snprintf(reg_name, ARRAY_SIZE(reg_name), "reg %d", i); |
| 205 | seq_printf(m, "%10s", reg_name); |
| 206 | if (vdd_class->vdd_ua) |
| 207 | seq_printf(m, "%10s", ""); |
| 208 | } |
| 209 | |
| 210 | seq_printf(m, "\n%12s", "freq"); |
| 211 | for (i = 0; i < nregs; i++) { |
| 212 | seq_printf(m, "%10s", "uV"); |
| 213 | if (vdd_class->vdd_ua) |
| 214 | seq_printf(m, "%10s", "uA"); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 215 | } |
| 216 | seq_printf(m, "\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 217 | |
xiaogang | c2a9ff1 | 2013-07-05 03:34:17 +0800 | [diff] [blame] | 218 | for (level = 0; level < clock->num_fmax; level++) |
| 219 | clock_print_fmax_by_level(m, level); |
| 220 | |
Stephen Boyd | 31c01e8 | 2012-04-13 15:22:00 -0700 | [diff] [blame] | 221 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 224 | static int fmax_rates_open(struct inode *inode, struct file *file) |
Stephen Boyd | cd50fae | 2011-11-03 11:05:42 -0700 | [diff] [blame] | 225 | { |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 226 | return single_open(file, fmax_rates_show, inode->i_private); |
Stephen Boyd | cd50fae | 2011-11-03 11:05:42 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 229 | static const struct file_operations fmax_rates_fops = { |
| 230 | .open = fmax_rates_open, |
| 231 | .read = seq_read, |
| 232 | .llseek = seq_lseek, |
| 233 | .release = seq_release, |
| 234 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | |
Xiaogang Cui | c7f4edd | 2013-07-13 06:45:45 +0800 | [diff] [blame] | 236 | #define clock_debug_output(m, c, fmt, ...) \ |
| 237 | do { \ |
| 238 | if (m) \ |
| 239 | seq_printf(m, fmt, ##__VA_ARGS__); \ |
| 240 | else if (c) \ |
| 241 | pr_cont(fmt, ##__VA_ARGS__); \ |
| 242 | else \ |
| 243 | pr_info(fmt, ##__VA_ARGS__); \ |
| 244 | } while (0) |
| 245 | |
| 246 | static int clock_debug_print_clock(struct clk *c, struct seq_file *m) |
| 247 | { |
| 248 | char *start = ""; |
| 249 | |
| 250 | if (!c || !c->prepare_count) |
| 251 | return 0; |
| 252 | |
| 253 | clock_debug_output(m, 0, "\t"); |
| 254 | do { |
| 255 | if (c->vdd_class) |
| 256 | clock_debug_output(m, 1, "%s%s:%u:%u [%ld, %lu]", start, |
| 257 | c->dbg_name, c->prepare_count, c->count, |
| 258 | c->rate, c->vdd_class->cur_level); |
| 259 | else |
| 260 | clock_debug_output(m, 1, "%s%s:%u:%u [%ld]", start, |
| 261 | c->dbg_name, c->prepare_count, c->count, |
| 262 | c->rate); |
| 263 | start = " -> "; |
| 264 | } while ((c = clk_get_parent(c))); |
| 265 | |
| 266 | clock_debug_output(m, 1, "\n"); |
| 267 | |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * clock_debug_print_enabled_clocks() - Print names of enabled clocks |
| 273 | * |
| 274 | */ |
| 275 | static void clock_debug_print_enabled_clocks(struct seq_file *m) |
| 276 | { |
| 277 | struct clk_table *table; |
| 278 | unsigned long flags; |
| 279 | int i, cnt = 0; |
| 280 | |
| 281 | clock_debug_output(m, 0, "Enabled clocks:\n"); |
| 282 | spin_lock_irqsave(&clk_list_lock, flags); |
| 283 | list_for_each_entry(table, &clk_list, node) { |
| 284 | for (i = 0; i < table->num_clocks; i++) |
| 285 | cnt += clock_debug_print_clock(table->clocks[i].clk, m); |
| 286 | } |
| 287 | spin_unlock_irqrestore(&clk_list_lock, flags); |
| 288 | |
| 289 | if (cnt) |
| 290 | clock_debug_output(m, 0, "Enabled clock count: %d\n", cnt); |
| 291 | else |
| 292 | clock_debug_output(m, 0, "No clocks enabled.\n"); |
| 293 | } |
| 294 | |
| 295 | static int enabled_clocks_show(struct seq_file *m, void *unused) |
| 296 | { |
| 297 | clock_debug_print_enabled_clocks(m); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int enabled_clocks_open(struct inode *inode, struct file *file) |
| 302 | { |
| 303 | return single_open(file, enabled_clocks_show, inode->i_private); |
| 304 | } |
| 305 | |
| 306 | static const struct file_operations enabled_clocks_fops = { |
| 307 | .open = enabled_clocks_open, |
| 308 | .read = seq_read, |
| 309 | .llseek = seq_lseek, |
| 310 | .release = seq_release, |
| 311 | }; |
| 312 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 313 | static int list_rates_show(struct seq_file *m, void *unused) |
| 314 | { |
| 315 | struct clk *clock = m->private; |
Saravana Kannan | e02bd3a | 2013-07-02 10:31:18 -0700 | [diff] [blame] | 316 | int level, i = 0; |
| 317 | unsigned long rate, fmax = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 318 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 319 | /* Find max frequency supported within voltage constraints. */ |
| 320 | if (!clock->vdd_class) { |
Saravana Kannan | e02bd3a | 2013-07-02 10:31:18 -0700 | [diff] [blame] | 321 | fmax = ULONG_MAX; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 322 | } else { |
Saravana Kannan | 55e959d | 2012-10-15 22:16:04 -0700 | [diff] [blame] | 323 | for (level = 0; level < clock->num_fmax; level++) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 324 | if (clock->fmax[level]) |
| 325 | fmax = clock->fmax[level]; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * List supported frequencies <= fmax. Higher frequencies may appear in |
| 330 | * the frequency table, but are not valid and should not be listed. |
| 331 | */ |
Saravana Kannan | e02bd3a | 2013-07-02 10:31:18 -0700 | [diff] [blame] | 332 | while (!IS_ERR_VALUE(rate = clock->ops->list_rate(clock, i++))) { |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 333 | if (rate <= fmax) |
Saravana Kannan | e02bd3a | 2013-07-02 10:31:18 -0700 | [diff] [blame] | 334 | seq_printf(m, "%lu\n", rate); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 335 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 336 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 337 | return 0; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 340 | static int list_rates_open(struct inode *inode, struct file *file) |
| 341 | { |
| 342 | return single_open(file, list_rates_show, inode->i_private); |
| 343 | } |
| 344 | |
| 345 | static const struct file_operations list_rates_fops = { |
| 346 | .open = list_rates_open, |
| 347 | .read = seq_read, |
| 348 | .llseek = seq_lseek, |
| 349 | .release = seq_release, |
| 350 | }; |
| 351 | |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 352 | static ssize_t clock_parent_read(struct file *filp, char __user *ubuf, |
| 353 | size_t cnt, loff_t *ppos) |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 354 | { |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 355 | struct clk *clock = filp->private_data; |
| 356 | struct clk *p = clock->parent; |
| 357 | char name[256] = {0}; |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 358 | |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 359 | snprintf(name, sizeof(name), "%s\n", p ? p->dbg_name : "None\n"); |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 360 | |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 361 | return simple_read_from_buffer(ubuf, cnt, ppos, name, strlen(name)); |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 364 | |
| 365 | static ssize_t clock_parent_write(struct file *filp, |
| 366 | const char __user *ubuf, size_t cnt, loff_t *ppos) |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 367 | { |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 368 | struct clk *clock = filp->private_data; |
| 369 | char buf[256]; |
| 370 | char *cmp; |
| 371 | unsigned long flags; |
| 372 | struct clk_table *table; |
| 373 | int i, ret; |
| 374 | struct clk *parent = NULL; |
| 375 | |
| 376 | cnt = min(cnt, sizeof(buf) - 1); |
| 377 | if (copy_from_user(&buf, ubuf, cnt)) |
| 378 | return -EFAULT; |
| 379 | buf[cnt] = '\0'; |
| 380 | cmp = strstrip(buf); |
| 381 | |
| 382 | spin_lock_irqsave(&clk_list_lock, flags); |
| 383 | list_for_each_entry(table, &clk_list, node) { |
| 384 | for (i = 0; i < table->num_clocks; i++) |
| 385 | if (!strcmp(cmp, table->clocks[i].clk->dbg_name)) { |
| 386 | parent = table->clocks[i].clk; |
| 387 | break; |
| 388 | } |
| 389 | if (parent) |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | if (!parent) { |
| 394 | ret = -EINVAL; |
| 395 | goto err; |
| 396 | } |
| 397 | |
| 398 | spin_unlock_irqrestore(&clk_list_lock, flags); |
| 399 | ret = clk_set_parent(clock, table->clocks[i].clk); |
| 400 | if (ret) |
| 401 | return ret; |
| 402 | |
| 403 | return cnt; |
| 404 | err: |
| 405 | spin_unlock_irqrestore(&clk_list_lock, flags); |
| 406 | return ret; |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 409 | |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 410 | static const struct file_operations clock_parent_fops = { |
Vikram Mulukutla | a0073af | 2013-04-10 14:24:38 -0700 | [diff] [blame] | 411 | .open = simple_open, |
| 412 | .read = clock_parent_read, |
| 413 | .write = clock_parent_write, |
Patrick Daly | 0a78a0e | 2012-07-23 13:18:59 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
Patrick Daly | 0ed3691 | 2013-09-26 18:14:51 -0700 | [diff] [blame] | 416 | void clk_debug_print_hw(struct clk *clk, struct seq_file *f) |
| 417 | { |
| 418 | void __iomem *base; |
| 419 | struct clk_register_data *regs; |
| 420 | u32 i, j, size; |
| 421 | |
| 422 | if (IS_ERR_OR_NULL(clk)) |
| 423 | return; |
| 424 | |
| 425 | clk_debug_print_hw(clk->parent, f); |
| 426 | |
| 427 | clock_debug_output(f, false, "%s\n", clk->dbg_name); |
| 428 | |
| 429 | if (!clk->ops->list_registers) |
| 430 | return; |
| 431 | |
| 432 | j = 0; |
| 433 | base = clk->ops->list_registers(clk, j, ®s, &size); |
| 434 | while (!IS_ERR(base)) { |
| 435 | for (i = 0; i < size; i++) { |
| 436 | u32 val = readl_relaxed(base + regs[i].offset); |
| 437 | clock_debug_output(f, false, "%20s: 0x%.8x\n", |
| 438 | regs[i].name, val); |
| 439 | } |
| 440 | j++; |
| 441 | base = clk->ops->list_registers(clk, j, ®s, &size); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static int print_hw_show(struct seq_file *m, void *unused) |
| 446 | { |
| 447 | struct clk *c = m->private; |
| 448 | clk_debug_print_hw(c, m); |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static int print_hw_open(struct inode *inode, struct file *file) |
| 454 | { |
| 455 | return single_open(file, print_hw_show, inode->i_private); |
| 456 | } |
| 457 | |
| 458 | static const struct file_operations clock_print_hw_fops = { |
| 459 | .open = print_hw_open, |
| 460 | .read = seq_read, |
| 461 | .llseek = seq_lseek, |
| 462 | .release = seq_release, |
| 463 | }; |
| 464 | |
| 465 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 466 | static int clock_debug_add(struct clk *clock) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 467 | { |
| 468 | char temp[50], *ptr; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 469 | struct dentry *clk_dir; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 470 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 471 | if (!debugfs_base) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 472 | return -ENOMEM; |
| 473 | |
Saravana Kannan | 7f62698 | 2011-09-26 19:02:02 -0700 | [diff] [blame] | 474 | strlcpy(temp, clock->dbg_name, ARRAY_SIZE(temp)); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 475 | for (ptr = temp; *ptr; ptr++) |
| 476 | *ptr = tolower(*ptr); |
| 477 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 478 | clk_dir = debugfs_create_dir(temp, debugfs_base); |
| 479 | if (!clk_dir) |
| 480 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 481 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 482 | if (!debugfs_create_file("rate", S_IRUGO | S_IWUSR, clk_dir, |
| 483 | clock, &clock_rate_fops)) |
| 484 | goto error; |
| 485 | |
| 486 | if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, clk_dir, |
| 487 | clock, &clock_enable_fops)) |
| 488 | goto error; |
| 489 | |
| 490 | if (!debugfs_create_file("is_local", S_IRUGO, clk_dir, clock, |
| 491 | &clock_local_fops)) |
| 492 | goto error; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 493 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 494 | if (!debugfs_create_file("has_hw_gating", S_IRUGO, clk_dir, clock, |
| 495 | &clock_hwcg_fops)) |
| 496 | goto error; |
| 497 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 498 | if (measure && |
| 499 | !clk_set_parent(measure, clock) && |
| 500 | !debugfs_create_file("measure", S_IRUGO, clk_dir, clock, |
| 501 | &clock_measure_fops)) |
| 502 | goto error; |
| 503 | |
| 504 | if (clock->ops->list_rate) |
| 505 | if (!debugfs_create_file("list_rates", |
| 506 | S_IRUGO, clk_dir, clock, &list_rates_fops)) |
| 507 | goto error; |
| 508 | |
Patrick Daly | 0a78a0e | 2012-07-23 13:18:59 -0700 | [diff] [blame] | 509 | if (clock->vdd_class && !debugfs_create_file("fmax_rates", |
| 510 | S_IRUGO, clk_dir, clock, &fmax_rates_fops)) |
| 511 | goto error; |
| 512 | |
Saravana Kannan | 531051f | 2012-09-27 16:19:07 -0700 | [diff] [blame] | 513 | if (!debugfs_create_file("parent", S_IRUGO, clk_dir, clock, |
| 514 | &clock_parent_fops)) |
| 515 | goto error; |
| 516 | |
Patrick Daly | 0ed3691 | 2013-09-26 18:14:51 -0700 | [diff] [blame] | 517 | if (!debugfs_create_file("print", S_IRUGO, clk_dir, clock, |
| 518 | &clock_print_hw_fops)) |
| 519 | goto error; |
| 520 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 521 | return 0; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 522 | error: |
| 523 | debugfs_remove_recursive(clk_dir); |
| 524 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 525 | } |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 526 | static DEFINE_MUTEX(clk_debug_lock); |
| 527 | static int clk_debug_init_once; |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * clock_debug_init() - Initialize clock debugfs |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 531 | * Lock clk_debug_lock before invoking this function. |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 532 | */ |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 533 | static int clock_debug_init(void) |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 534 | { |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 535 | if (clk_debug_init_once) |
| 536 | return 0; |
| 537 | |
| 538 | clk_debug_init_once = 1; |
| 539 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 540 | debugfs_base = debugfs_create_dir("clk", NULL); |
| 541 | if (!debugfs_base) |
| 542 | return -ENOMEM; |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 543 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 544 | if (!debugfs_create_u32("debug_suspend", S_IRUGO | S_IWUSR, |
| 545 | debugfs_base, &debug_suspend)) { |
| 546 | debugfs_remove_recursive(debugfs_base); |
| 547 | return -ENOMEM; |
| 548 | } |
| 549 | |
Xiaogang Cui | c7f4edd | 2013-07-13 06:45:45 +0800 | [diff] [blame] | 550 | if (!debugfs_create_file("enabled_clocks", S_IRUGO, debugfs_base, NULL, |
| 551 | &enabled_clocks_fops)) |
| 552 | return -ENOMEM; |
| 553 | |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 554 | measure = clk_get_sys("debug", "measure"); |
| 555 | if (IS_ERR(measure)) |
| 556 | measure = NULL; |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Vikram Mulukutla | 4785ab6 | 2012-12-10 20:51:22 -0800 | [diff] [blame] | 561 | /** |
| 562 | * clock_debug_register() - Add additional clocks to clock debugfs hierarchy |
| 563 | * @table: Table of clocks to create debugfs nodes for |
| 564 | * @size: Size of @table |
| 565 | * |
| 566 | */ |
| 567 | int clock_debug_register(struct clk_lookup *table, size_t size) |
| 568 | { |
| 569 | struct clk_table *clk_table; |
| 570 | unsigned long flags; |
| 571 | int i, ret; |
| 572 | |
| 573 | mutex_lock(&clk_debug_lock); |
| 574 | |
| 575 | ret = clock_debug_init(); |
| 576 | if (ret) |
| 577 | goto out; |
| 578 | |
| 579 | clk_table = kmalloc(sizeof(*clk_table), GFP_KERNEL); |
| 580 | if (!clk_table) { |
| 581 | ret = -ENOMEM; |
| 582 | goto out; |
| 583 | } |
| 584 | |
| 585 | clk_table->clocks = table; |
| 586 | clk_table->num_clocks = size; |
| 587 | |
| 588 | spin_lock_irqsave(&clk_list_lock, flags); |
| 589 | list_add_tail(&clk_table->node, &clk_list); |
| 590 | spin_unlock_irqrestore(&clk_list_lock, flags); |
| 591 | |
| 592 | for (i = 0; i < size; i++) |
| 593 | clock_debug_add(table[i].clk); |
| 594 | |
| 595 | out: |
| 596 | mutex_unlock(&clk_debug_lock); |
| 597 | return ret; |
| 598 | } |
| 599 | |
Xiaogang Cui | c7f4edd | 2013-07-13 06:45:45 +0800 | [diff] [blame] | 600 | /* |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 601 | * Print the names of enabled clocks and their parents if debug_suspend is set |
| 602 | */ |
| 603 | void clock_debug_print_enabled(void) |
| 604 | { |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 605 | if (likely(!debug_suspend)) |
| 606 | return; |
| 607 | |
Xiaogang Cui | c7f4edd | 2013-07-13 06:45:45 +0800 | [diff] [blame] | 608 | clock_debug_print_enabled_clocks(NULL); |
Matt Wagantall | 665f0cf | 2012-02-27 15:54:43 -0800 | [diff] [blame] | 609 | } |