Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google, Inc. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2007-2011, 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); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | if (clock->flags & CLKFLAG_MIN) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 37 | ret = clk_set_min_rate(clock, val); |
| 38 | else |
| 39 | ret = clk_set_rate(clock, val); |
| 40 | if (ret != 0) |
| 41 | printk(KERN_ERR "clk_set%s_rate failed (%d)\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | (clock->flags & CLKFLAG_MIN) ? "_min" : "", ret); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static int clock_debug_rate_get(void *data, u64 *val) |
| 47 | { |
| 48 | struct clk *clock = data; |
| 49 | *val = clk_get_rate(clock); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get, |
| 54 | clock_debug_rate_set, "%llu\n"); |
| 55 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | static struct clk *measure; |
| 57 | |
| 58 | static int clock_debug_measure_get(void *data, u64 *val) |
| 59 | { |
| 60 | int ret; |
| 61 | struct clk *clock = data; |
| 62 | |
| 63 | ret = clk_set_parent(measure, clock); |
| 64 | if (!ret) |
| 65 | *val = clk_get_rate(measure); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get, |
| 71 | NULL, "%lld\n"); |
| 72 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 73 | static int clock_debug_enable_set(void *data, u64 val) |
| 74 | { |
| 75 | struct clk *clock = data; |
| 76 | int rc = 0; |
| 77 | |
| 78 | if (val) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | rc = clk_enable(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 80 | else |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | clk_disable(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 82 | |
| 83 | return rc; |
| 84 | } |
| 85 | |
| 86 | static int clock_debug_enable_get(void *data, u64 *val) |
| 87 | { |
| 88 | struct clk *clock = data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 89 | int enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 90 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | if (clock->ops->is_enabled) |
| 92 | enabled = clock->ops->is_enabled(clock); |
| 93 | else |
| 94 | enabled = !!(clock->count); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 95 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | *val = enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 101 | clock_debug_enable_set, "%lld\n"); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 102 | |
| 103 | static int clock_debug_local_get(void *data, u64 *val) |
| 104 | { |
| 105 | struct clk *clock = data; |
| 106 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | *val = clock->ops->is_local(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get, |
| 113 | NULL, "%llu\n"); |
| 114 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 115 | static struct dentry *debugfs_base; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 116 | static u32 debug_suspend; |
| 117 | static struct clk_lookup *msm_clocks; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 118 | static size_t num_msm_clocks; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 119 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 120 | int __init clock_debug_init(struct clock_init_data *data) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 121 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 122 | int ret = 0; |
| 123 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 124 | debugfs_base = debugfs_create_dir("clk", NULL); |
| 125 | if (!debugfs_base) |
| 126 | return -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | if (!debugfs_create_u32("debug_suspend", S_IRUGO | S_IWUSR, |
| 128 | debugfs_base, &debug_suspend)) { |
| 129 | debugfs_remove_recursive(debugfs_base); |
| 130 | return -ENOMEM; |
| 131 | } |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 132 | msm_clocks = data->table; |
| 133 | num_msm_clocks = data->size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | |
| 135 | measure = clk_get_sys("debug", "measure"); |
| 136 | if (IS_ERR(measure)) { |
| 137 | ret = PTR_ERR(measure); |
| 138 | measure = NULL; |
| 139 | } |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | void clock_debug_print_enabled(void) |
| 145 | { |
| 146 | struct clk *clk; |
| 147 | unsigned i; |
| 148 | int cnt = 0; |
| 149 | |
| 150 | if (likely(!debug_suspend)) |
| 151 | return; |
| 152 | |
| 153 | pr_info("Enabled clocks:\n"); |
| 154 | for (i = 0; i < num_msm_clocks; i++) { |
| 155 | clk = msm_clocks[i].clk; |
| 156 | |
Matt Wagantall | 96de0ea | 2011-09-20 14:01:21 -0700 | [diff] [blame] | 157 | if (clk && clk->ops->is_enabled && clk->ops->is_enabled(clk)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | pr_info("\t%s\n", clk->dbg_name); |
| 159 | cnt++; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (cnt) |
| 164 | pr_info("Enabled clock count: %d\n", cnt); |
| 165 | else |
| 166 | pr_info("No clocks enabled.\n"); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | static int list_rates_show(struct seq_file *m, void *unused) |
| 171 | { |
| 172 | struct clk *clock = m->private; |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame^] | 173 | int rate, level, fmax = 0, i = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 174 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame^] | 175 | /* Find max frequency supported within voltage constraints. */ |
| 176 | if (!clock->vdd_class) { |
| 177 | fmax = ULONG_MAX; |
| 178 | } else { |
| 179 | for (level = 0; level < ARRAY_SIZE(clock->fmax); level++) |
| 180 | if (clock->fmax[level]) |
| 181 | fmax = clock->fmax[level]; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * List supported frequencies <= fmax. Higher frequencies may appear in |
| 186 | * the frequency table, but are not valid and should not be listed. |
| 187 | */ |
| 188 | while ((rate = clock->ops->list_rate(clock, i++)) >= 0) { |
| 189 | if (rate <= fmax) |
| 190 | seq_printf(m, "%u\n", rate); |
| 191 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 193 | return 0; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | static int list_rates_open(struct inode *inode, struct file *file) |
| 197 | { |
| 198 | return single_open(file, list_rates_show, inode->i_private); |
| 199 | } |
| 200 | |
| 201 | static const struct file_operations list_rates_fops = { |
| 202 | .open = list_rates_open, |
| 203 | .read = seq_read, |
| 204 | .llseek = seq_lseek, |
| 205 | .release = seq_release, |
| 206 | }; |
| 207 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 208 | int __init clock_debug_add(struct clk *clock) |
| 209 | { |
| 210 | char temp[50], *ptr; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 211 | struct dentry *clk_dir; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 212 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 213 | if (!debugfs_base) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 214 | return -ENOMEM; |
| 215 | |
| 216 | strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1); |
| 217 | for (ptr = temp; *ptr; ptr++) |
| 218 | *ptr = tolower(*ptr); |
| 219 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 220 | clk_dir = debugfs_create_dir(temp, debugfs_base); |
| 221 | if (!clk_dir) |
| 222 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 223 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 224 | if (!debugfs_create_file("rate", S_IRUGO | S_IWUSR, clk_dir, |
| 225 | clock, &clock_rate_fops)) |
| 226 | goto error; |
| 227 | |
| 228 | if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, clk_dir, |
| 229 | clock, &clock_enable_fops)) |
| 230 | goto error; |
| 231 | |
| 232 | if (!debugfs_create_file("is_local", S_IRUGO, clk_dir, clock, |
| 233 | &clock_local_fops)) |
| 234 | goto error; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | |
| 236 | if (measure && |
| 237 | !clk_set_parent(measure, clock) && |
| 238 | !debugfs_create_file("measure", S_IRUGO, clk_dir, clock, |
| 239 | &clock_measure_fops)) |
| 240 | goto error; |
| 241 | |
| 242 | if (clock->ops->list_rate) |
| 243 | if (!debugfs_create_file("list_rates", |
| 244 | S_IRUGO, clk_dir, clock, &list_rates_fops)) |
| 245 | goto error; |
| 246 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 247 | return 0; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 248 | error: |
| 249 | debugfs_remove_recursive(clk_dir); |
| 250 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 251 | } |