blob: 807d587481a0ef673fdb2e3f4bcd3ca0bcdbe9b3 [file] [log] [blame]
Matt Wagantalld64560fe2011-01-26 16:20:54 -08001/*
2 * Copyright (C) 2007 Google, Inc.
Stephen Boyd3bbf3462012-01-12 00:19:23 -08003 * Copyright (c) 2007-2012, Code Aurora Forum. All rights reserved.
Matt Wagantalld64560fe2011-01-26 16:20:54 -08004 *
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 Huntsman3f2bc4d2011-08-16 17:27:22 -070020#include <linux/seq_file.h>
Matt Wagantalld64560fe2011-01-26 16:20:54 -080021#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/list.h>
23#include <linux/clkdev.h>
24
Matt Wagantalld64560fe2011-01-26 16:20:54 -080025#include "clock.h"
Matt Wagantalld64560fe2011-01-26 16:20:54 -080026
27static 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 Huntsman3f2bc4d2011-08-16 17:27:22 -070034 if (clock->flags & CLKFLAG_MAX)
Matt Wagantalld64560fe2011-01-26 16:20:54 -080035 clk_set_max_rate(clock, val);
Matt Wagantall8e6126f2011-11-08 13:34:19 -080036 ret = clk_set_rate(clock, val);
37 if (ret)
Stephen Boyd753ab932012-08-02 13:14:38 -070038 pr_err("clk_set_rate(%s, %lu) failed (%d)\n", clock->dbg_name,
39 (unsigned long)val, ret);
Matt Wagantall8e6126f2011-11-08 13:34:19 -080040
Matt Wagantalld64560fe2011-01-26 16:20:54 -080041 return ret;
42}
43
44static int clock_debug_rate_get(void *data, u64 *val)
45{
46 struct clk *clock = data;
47 *val = clk_get_rate(clock);
48 return 0;
49}
50
51DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
52 clock_debug_rate_set, "%llu\n");
53
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054static struct clk *measure;
55
56static int clock_debug_measure_get(void *data, u64 *val)
57{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058 struct clk *clock = data;
Stephen Boyda52d7e32011-11-10 11:59:00 -080059 int ret, is_hw_gated;
60
61 /* Check to see if the clock is in hardware gating mode */
Stephen Boyd0f7e5642012-08-02 12:59:33 -070062 if (clock->ops->in_hwcg_mode)
Stephen Boyda52d7e32011-11-10 11:59:00 -080063 is_hw_gated = clock->ops->in_hwcg_mode(clock);
64 else
65 is_hw_gated = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
67 ret = clk_set_parent(measure, clock);
Stephen Boyda52d7e32011-11-10 11:59:00 -080068 if (!ret) {
69 /*
70 * Disable hw gating to get accurate rate measurements. Only do
71 * this if the clock is explictly enabled by software. This
72 * allows us to detect errors where clocks are on even though
73 * software is not requesting them to be on due to broken
74 * hardware gating signals.
75 */
76 if (is_hw_gated && clock->count)
77 clock->ops->disable_hwcg(clock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 *val = clk_get_rate(measure);
Stephen Boyda52d7e32011-11-10 11:59:00 -080079 /* Reenable hwgating if it was disabled */
80 if (is_hw_gated && clock->count)
81 clock->ops->enable_hwcg(clock);
82 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083
84 return ret;
85}
86
87DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get,
88 NULL, "%lld\n");
89
Matt Wagantalld64560fe2011-01-26 16:20:54 -080090static int clock_debug_enable_set(void *data, u64 val)
91{
92 struct clk *clock = data;
93 int rc = 0;
94
95 if (val)
Stephen Boyd3bbf3462012-01-12 00:19:23 -080096 rc = clk_prepare_enable(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080097 else
Stephen Boyd3bbf3462012-01-12 00:19:23 -080098 clk_disable_unprepare(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080099
100 return rc;
101}
102
103static int clock_debug_enable_get(void *data, u64 *val)
104{
105 struct clk *clock = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 int enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 if (clock->ops->is_enabled)
109 enabled = clock->ops->is_enabled(clock);
110 else
111 enabled = !!(clock->count);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800112
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 *val = enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800114 return 0;
115}
116
117DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118 clock_debug_enable_set, "%lld\n");
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800119
120static int clock_debug_local_get(void *data, u64 *val)
121{
122 struct clk *clock = data;
123
Matt Wagantallacb8d022012-02-14 15:28:23 -0800124 if (!clock->ops->is_local)
125 *val = true;
126 else
127 *val = clock->ops->is_local(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800128
129 return 0;
130}
131
132DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
133 NULL, "%llu\n");
134
Stephen Boyda52d7e32011-11-10 11:59:00 -0800135static int clock_debug_hwcg_get(void *data, u64 *val)
136{
137 struct clk *clock = data;
Stephen Boyd0f7e5642012-08-02 12:59:33 -0700138 if (clock->ops->in_hwcg_mode)
139 *val = !!clock->ops->in_hwcg_mode(clock);
140 else
141 *val = 0;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800142 return 0;
143}
144
145DEFINE_SIMPLE_ATTRIBUTE(clock_hwcg_fops, clock_debug_hwcg_get,
146 NULL, "%llu\n");
147
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800148static struct dentry *debugfs_base;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149static u32 debug_suspend;
150static struct clk_lookup *msm_clocks;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700151static size_t num_msm_clocks;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800152
Stephen Boydbb600ae2011-08-02 20:11:40 -0700153int __init clock_debug_init(struct clock_init_data *data)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800154{
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800155 debugfs_base = debugfs_create_dir("clk", NULL);
156 if (!debugfs_base)
157 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 if (!debugfs_create_u32("debug_suspend", S_IRUGO | S_IWUSR,
159 debugfs_base, &debug_suspend)) {
160 debugfs_remove_recursive(debugfs_base);
161 return -ENOMEM;
162 }
Stephen Boydbb600ae2011-08-02 20:11:40 -0700163 msm_clocks = data->table;
164 num_msm_clocks = data->size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165
166 measure = clk_get_sys("debug", "measure");
Stephen Boyd31c01e82012-04-13 15:22:00 -0700167 if (IS_ERR(measure))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 measure = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169
Stephen Boyd31c01e82012-04-13 15:22:00 -0700170 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171}
172
Stephen Boydcd50fae2011-11-03 11:05:42 -0700173
174static int clock_debug_print_clock(struct clk *c)
175{
Vikram Mulukutla8d575ace2012-07-02 19:45:47 -0700176 char *start = "";
Stephen Boydcd50fae2011-11-03 11:05:42 -0700177
178 if (!c || !c->count)
179 return 0;
180
Vikram Mulukutla8d575ace2012-07-02 19:45:47 -0700181 pr_info("\t");
182 do {
183 if (c->vdd_class)
184 pr_cont("%s%s [%ld, %lu]", start, c->dbg_name, c->rate,
185 c->vdd_class->cur_level);
186 else
187 pr_cont("%s%s [%ld]", start, c->dbg_name, c->rate);
188 start = " -> ";
189 } while ((c = clk_get_parent(c)));
190
191 pr_cont("\n");
192
Stephen Boydcd50fae2011-11-03 11:05:42 -0700193 return 1;
194}
195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196void clock_debug_print_enabled(void)
197{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 unsigned i;
199 int cnt = 0;
200
201 if (likely(!debug_suspend))
202 return;
203
204 pr_info("Enabled clocks:\n");
Stephen Boydcd50fae2011-11-03 11:05:42 -0700205 for (i = 0; i < num_msm_clocks; i++)
206 cnt += clock_debug_print_clock(msm_clocks[i].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207
208 if (cnt)
209 pr_info("Enabled clock count: %d\n", cnt);
210 else
211 pr_info("No clocks enabled.\n");
212
213}
214
215static int list_rates_show(struct seq_file *m, void *unused)
216{
217 struct clk *clock = m->private;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700218 int rate, level, fmax = 0, i = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700220 /* Find max frequency supported within voltage constraints. */
221 if (!clock->vdd_class) {
Matt Wagantalle426f9042011-11-01 16:21:34 -0700222 fmax = INT_MAX;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700223 } else {
224 for (level = 0; level < ARRAY_SIZE(clock->fmax); level++)
225 if (clock->fmax[level])
226 fmax = clock->fmax[level];
227 }
228
229 /*
230 * List supported frequencies <= fmax. Higher frequencies may appear in
231 * the frequency table, but are not valid and should not be listed.
232 */
233 while ((rate = clock->ops->list_rate(clock, i++)) >= 0) {
234 if (rate <= fmax)
235 seq_printf(m, "%u\n", rate);
236 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800238 return 0;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800239}
240
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241static int list_rates_open(struct inode *inode, struct file *file)
242{
243 return single_open(file, list_rates_show, inode->i_private);
244}
245
246static const struct file_operations list_rates_fops = {
247 .open = list_rates_open,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = seq_release,
251};
252
Patrick Daly0a78a0e2012-07-23 13:18:59 -0700253static int fmax_rates_show(struct seq_file *m, void *unused)
254{
255 struct clk *clock = m->private;
256 int level = 0;
257
258 int vdd_level = find_vdd_level(clock, clock->rate);
259 if (vdd_level < 0) {
260 seq_printf(m, "could not find_vdd_level for %s, %ld\n",
261 clock->dbg_name, clock->rate);
262 return 0;
263 }
264 for (level = 0; level < ARRAY_SIZE(clock->fmax); level++) {
265 if (vdd_level == level)
266 seq_printf(m, "[%lu] ", clock->fmax[level]);
267 else
268 seq_printf(m, "%lu ", clock->fmax[level]);
269 }
270 seq_printf(m, "\n");
271
272 return 0;
273}
274
275static int fmax_rates_open(struct inode *inode, struct file *file)
276{
277 return single_open(file, fmax_rates_show, inode->i_private);
278}
279
280static const struct file_operations fmax_rates_fops = {
281 .open = fmax_rates_open,
282 .read = seq_read,
283 .llseek = seq_lseek,
284 .release = seq_release,
285};
286
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800287int __init clock_debug_add(struct clk *clock)
288{
289 char temp[50], *ptr;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800290 struct dentry *clk_dir;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800291
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800292 if (!debugfs_base)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800293 return -ENOMEM;
294
Saravana Kannan7f626982011-09-26 19:02:02 -0700295 strlcpy(temp, clock->dbg_name, ARRAY_SIZE(temp));
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800296 for (ptr = temp; *ptr; ptr++)
297 *ptr = tolower(*ptr);
298
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800299 clk_dir = debugfs_create_dir(temp, debugfs_base);
300 if (!clk_dir)
301 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800302
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800303 if (!debugfs_create_file("rate", S_IRUGO | S_IWUSR, clk_dir,
304 clock, &clock_rate_fops))
305 goto error;
306
307 if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, clk_dir,
308 clock, &clock_enable_fops))
309 goto error;
310
311 if (!debugfs_create_file("is_local", S_IRUGO, clk_dir, clock,
312 &clock_local_fops))
313 goto error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314
Stephen Boyda52d7e32011-11-10 11:59:00 -0800315 if (!debugfs_create_file("has_hw_gating", S_IRUGO, clk_dir, clock,
316 &clock_hwcg_fops))
317 goto error;
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 if (measure &&
320 !clk_set_parent(measure, clock) &&
321 !debugfs_create_file("measure", S_IRUGO, clk_dir, clock,
322 &clock_measure_fops))
323 goto error;
324
325 if (clock->ops->list_rate)
326 if (!debugfs_create_file("list_rates",
327 S_IRUGO, clk_dir, clock, &list_rates_fops))
328 goto error;
329
Patrick Daly0a78a0e2012-07-23 13:18:59 -0700330 if (clock->vdd_class && !debugfs_create_file("fmax_rates",
331 S_IRUGO, clk_dir, clock, &fmax_rates_fops))
332 goto error;
333
334
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800335 return 0;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800336error:
337 debugfs_remove_recursive(clk_dir);
338 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800339}