Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google, Inc. |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 3 | * Copyright (c) 2007-2012, Code Aurora Forum. 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> |
| 24 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 25 | #include "clock.h" |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 26 | |
| 27 | static int clock_debug_rate_set(void *data, u64 val) |
| 28 | { |
| 29 | struct clk *clock = data; |
| 30 | int ret; |
| 31 | |
| 32 | /* Only increases to max rate will succeed, but that's actually good |
| 33 | * for debugging purposes so we don't check for error. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | if (clock->flags & CLKFLAG_MAX) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 35 | clk_set_max_rate(clock, val); |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 36 | ret = clk_set_rate(clock, val); |
| 37 | if (ret) |
| 38 | pr_err("clk_set_rate failed (%d)\n", ret); |
| 39 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | static int clock_debug_rate_get(void *data, u64 *val) |
| 44 | { |
| 45 | struct clk *clock = data; |
| 46 | *val = clk_get_rate(clock); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get, |
| 51 | clock_debug_rate_set, "%llu\n"); |
| 52 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 53 | static struct clk *measure; |
| 54 | |
| 55 | static int clock_debug_measure_get(void *data, u64 *val) |
| 56 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | struct clk *clock = data; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 58 | int ret, is_hw_gated; |
| 59 | |
| 60 | /* Check to see if the clock is in hardware gating mode */ |
Stephen Boyd | 0f7e564 | 2012-08-02 12:59:33 -0700 | [diff] [blame^] | 61 | if (clock->ops->in_hwcg_mode) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 62 | is_hw_gated = clock->ops->in_hwcg_mode(clock); |
| 63 | else |
| 64 | is_hw_gated = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 65 | |
| 66 | ret = clk_set_parent(measure, clock); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 67 | if (!ret) { |
| 68 | /* |
| 69 | * Disable hw gating to get accurate rate measurements. Only do |
| 70 | * this if the clock is explictly enabled by software. This |
| 71 | * allows us to detect errors where clocks are on even though |
| 72 | * software is not requesting them to be on due to broken |
| 73 | * hardware gating signals. |
| 74 | */ |
| 75 | if (is_hw_gated && clock->count) |
| 76 | clock->ops->disable_hwcg(clock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | *val = clk_get_rate(measure); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 78 | /* Reenable hwgating if it was disabled */ |
| 79 | if (is_hw_gated && clock->count) |
| 80 | clock->ops->enable_hwcg(clock); |
| 81 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get, |
| 87 | NULL, "%lld\n"); |
| 88 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 89 | static int clock_debug_enable_set(void *data, u64 val) |
| 90 | { |
| 91 | struct clk *clock = data; |
| 92 | int rc = 0; |
| 93 | |
| 94 | if (val) |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 95 | rc = clk_prepare_enable(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 96 | else |
Stephen Boyd | 3bbf346 | 2012-01-12 00:19:23 -0800 | [diff] [blame] | 97 | clk_disable_unprepare(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 98 | |
| 99 | return rc; |
| 100 | } |
| 101 | |
| 102 | static int clock_debug_enable_get(void *data, u64 *val) |
| 103 | { |
| 104 | struct clk *clock = data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 105 | int enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 106 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | if (clock->ops->is_enabled) |
| 108 | enabled = clock->ops->is_enabled(clock); |
| 109 | else |
| 110 | enabled = !!(clock->count); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 111 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | *val = enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 117 | clock_debug_enable_set, "%lld\n"); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 118 | |
| 119 | static int clock_debug_local_get(void *data, u64 *val) |
| 120 | { |
| 121 | struct clk *clock = data; |
| 122 | |
Matt Wagantall | acb8d02 | 2012-02-14 15:28:23 -0800 | [diff] [blame] | 123 | if (!clock->ops->is_local) |
| 124 | *val = true; |
| 125 | else |
| 126 | *val = clock->ops->is_local(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get, |
| 132 | NULL, "%llu\n"); |
| 133 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 134 | static int clock_debug_hwcg_get(void *data, u64 *val) |
| 135 | { |
| 136 | struct clk *clock = data; |
Stephen Boyd | 0f7e564 | 2012-08-02 12:59:33 -0700 | [diff] [blame^] | 137 | if (clock->ops->in_hwcg_mode) |
| 138 | *val = !!clock->ops->in_hwcg_mode(clock); |
| 139 | else |
| 140 | *val = 0; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | DEFINE_SIMPLE_ATTRIBUTE(clock_hwcg_fops, clock_debug_hwcg_get, |
| 145 | NULL, "%llu\n"); |
| 146 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 147 | static struct dentry *debugfs_base; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 148 | static u32 debug_suspend; |
| 149 | static struct clk_lookup *msm_clocks; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 150 | static size_t num_msm_clocks; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 151 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 152 | int __init clock_debug_init(struct clock_init_data *data) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 153 | { |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 154 | debugfs_base = debugfs_create_dir("clk", NULL); |
| 155 | if (!debugfs_base) |
| 156 | return -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | if (!debugfs_create_u32("debug_suspend", S_IRUGO | S_IWUSR, |
| 158 | debugfs_base, &debug_suspend)) { |
| 159 | debugfs_remove_recursive(debugfs_base); |
| 160 | return -ENOMEM; |
| 161 | } |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 162 | msm_clocks = data->table; |
| 163 | num_msm_clocks = data->size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 164 | |
| 165 | measure = clk_get_sys("debug", "measure"); |
Stephen Boyd | 31c01e8 | 2012-04-13 15:22:00 -0700 | [diff] [blame] | 166 | if (IS_ERR(measure)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | measure = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 168 | |
Stephen Boyd | 31c01e8 | 2012-04-13 15:22:00 -0700 | [diff] [blame] | 169 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Stephen Boyd | cd50fae | 2011-11-03 11:05:42 -0700 | [diff] [blame] | 172 | |
| 173 | static int clock_debug_print_clock(struct clk *c) |
| 174 | { |
Vikram Mulukutla | 8d575ace | 2012-07-02 19:45:47 -0700 | [diff] [blame] | 175 | char *start = ""; |
Stephen Boyd | cd50fae | 2011-11-03 11:05:42 -0700 | [diff] [blame] | 176 | |
| 177 | if (!c || !c->count) |
| 178 | return 0; |
| 179 | |
Vikram Mulukutla | 8d575ace | 2012-07-02 19:45:47 -0700 | [diff] [blame] | 180 | pr_info("\t"); |
| 181 | do { |
| 182 | if (c->vdd_class) |
| 183 | pr_cont("%s%s [%ld, %lu]", start, c->dbg_name, c->rate, |
| 184 | c->vdd_class->cur_level); |
| 185 | else |
| 186 | pr_cont("%s%s [%ld]", start, c->dbg_name, c->rate); |
| 187 | start = " -> "; |
| 188 | } while ((c = clk_get_parent(c))); |
| 189 | |
| 190 | pr_cont("\n"); |
| 191 | |
Stephen Boyd | cd50fae | 2011-11-03 11:05:42 -0700 | [diff] [blame] | 192 | return 1; |
| 193 | } |
| 194 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | void clock_debug_print_enabled(void) |
| 196 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | unsigned i; |
| 198 | int cnt = 0; |
| 199 | |
| 200 | if (likely(!debug_suspend)) |
| 201 | return; |
| 202 | |
| 203 | pr_info("Enabled clocks:\n"); |
Stephen Boyd | cd50fae | 2011-11-03 11:05:42 -0700 | [diff] [blame] | 204 | for (i = 0; i < num_msm_clocks; i++) |
| 205 | cnt += clock_debug_print_clock(msm_clocks[i].clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | |
| 207 | if (cnt) |
| 208 | pr_info("Enabled clock count: %d\n", cnt); |
| 209 | else |
| 210 | pr_info("No clocks enabled.\n"); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | static int list_rates_show(struct seq_file *m, void *unused) |
| 215 | { |
| 216 | struct clk *clock = m->private; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 217 | int rate, level, fmax = 0, i = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 218 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 219 | /* Find max frequency supported within voltage constraints. */ |
| 220 | if (!clock->vdd_class) { |
Matt Wagantall | e426f904 | 2011-11-01 16:21:34 -0700 | [diff] [blame] | 221 | fmax = INT_MAX; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 222 | } else { |
| 223 | for (level = 0; level < ARRAY_SIZE(clock->fmax); level++) |
| 224 | if (clock->fmax[level]) |
| 225 | fmax = clock->fmax[level]; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * List supported frequencies <= fmax. Higher frequencies may appear in |
| 230 | * the frequency table, but are not valid and should not be listed. |
| 231 | */ |
| 232 | while ((rate = clock->ops->list_rate(clock, i++)) >= 0) { |
| 233 | if (rate <= fmax) |
| 234 | seq_printf(m, "%u\n", rate); |
| 235 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 236 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 237 | return 0; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | static int list_rates_open(struct inode *inode, struct file *file) |
| 241 | { |
| 242 | return single_open(file, list_rates_show, inode->i_private); |
| 243 | } |
| 244 | |
| 245 | static const struct file_operations list_rates_fops = { |
| 246 | .open = list_rates_open, |
| 247 | .read = seq_read, |
| 248 | .llseek = seq_lseek, |
| 249 | .release = seq_release, |
| 250 | }; |
| 251 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 252 | int __init clock_debug_add(struct clk *clock) |
| 253 | { |
| 254 | char temp[50], *ptr; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 255 | struct dentry *clk_dir; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 256 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 257 | if (!debugfs_base) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 258 | return -ENOMEM; |
| 259 | |
Saravana Kannan | 7f62698 | 2011-09-26 19:02:02 -0700 | [diff] [blame] | 260 | strlcpy(temp, clock->dbg_name, ARRAY_SIZE(temp)); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 261 | for (ptr = temp; *ptr; ptr++) |
| 262 | *ptr = tolower(*ptr); |
| 263 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 264 | clk_dir = debugfs_create_dir(temp, debugfs_base); |
| 265 | if (!clk_dir) |
| 266 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 267 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 268 | if (!debugfs_create_file("rate", S_IRUGO | S_IWUSR, clk_dir, |
| 269 | clock, &clock_rate_fops)) |
| 270 | goto error; |
| 271 | |
| 272 | if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, clk_dir, |
| 273 | clock, &clock_enable_fops)) |
| 274 | goto error; |
| 275 | |
| 276 | if (!debugfs_create_file("is_local", S_IRUGO, clk_dir, clock, |
| 277 | &clock_local_fops)) |
| 278 | goto error; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 279 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 280 | if (!debugfs_create_file("has_hw_gating", S_IRUGO, clk_dir, clock, |
| 281 | &clock_hwcg_fops)) |
| 282 | goto error; |
| 283 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 284 | if (measure && |
| 285 | !clk_set_parent(measure, clock) && |
| 286 | !debugfs_create_file("measure", S_IRUGO, clk_dir, clock, |
| 287 | &clock_measure_fops)) |
| 288 | goto error; |
| 289 | |
| 290 | if (clock->ops->list_rate) |
| 291 | if (!debugfs_create_file("list_rates", |
| 292 | S_IRUGO, clk_dir, clock, &list_rates_fops)) |
| 293 | goto error; |
| 294 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 295 | return 0; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 296 | error: |
| 297 | debugfs_remove_recursive(clk_dir); |
| 298 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 299 | } |