blob: 54b13e68d556ca5d9e8c61e165ed10678c81473f [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)
38 pr_err("clk_set_rate failed (%d)\n", ret);
39
Matt Wagantalld64560fe2011-01-26 16:20:54 -080040 return ret;
41}
42
43static 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
50DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
51 clock_debug_rate_set, "%llu\n");
52
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053static struct clk *measure;
54
55static int clock_debug_measure_get(void *data, u64 *val)
56{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057 struct clk *clock = data;
Stephen Boyda52d7e32011-11-10 11:59:00 -080058 int ret, is_hw_gated;
59
60 /* Check to see if the clock is in hardware gating mode */
61 if (clock->flags & CLKFLAG_HWCG)
62 is_hw_gated = clock->ops->in_hwcg_mode(clock);
63 else
64 is_hw_gated = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065
66 ret = clk_set_parent(measure, clock);
Stephen Boyda52d7e32011-11-10 11:59:00 -080067 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 Huntsman3f2bc4d2011-08-16 17:27:22 -070077 *val = clk_get_rate(measure);
Stephen Boyda52d7e32011-11-10 11:59:00 -080078 /* Reenable hwgating if it was disabled */
79 if (is_hw_gated && clock->count)
80 clock->ops->enable_hwcg(clock);
81 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082
83 return ret;
84}
85
86DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get,
87 NULL, "%lld\n");
88
Matt Wagantalld64560fe2011-01-26 16:20:54 -080089static int clock_debug_enable_set(void *data, u64 val)
90{
91 struct clk *clock = data;
92 int rc = 0;
93
94 if (val)
Stephen Boyd3bbf3462012-01-12 00:19:23 -080095 rc = clk_prepare_enable(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080096 else
Stephen Boyd3bbf3462012-01-12 00:19:23 -080097 clk_disable_unprepare(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080098
99 return rc;
100}
101
102static int clock_debug_enable_get(void *data, u64 *val)
103{
104 struct clk *clock = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 int enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 if (clock->ops->is_enabled)
108 enabled = clock->ops->is_enabled(clock);
109 else
110 enabled = !!(clock->count);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 *val = enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800113 return 0;
114}
115
116DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117 clock_debug_enable_set, "%lld\n");
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800118
119static int clock_debug_local_get(void *data, u64 *val)
120{
121 struct clk *clock = data;
122
Matt Wagantallacb8d022012-02-14 15:28:23 -0800123 if (!clock->ops->is_local)
124 *val = true;
125 else
126 *val = clock->ops->is_local(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800127
128 return 0;
129}
130
131DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
132 NULL, "%llu\n");
133
Stephen Boyda52d7e32011-11-10 11:59:00 -0800134static int clock_debug_hwcg_get(void *data, u64 *val)
135{
136 struct clk *clock = data;
137 *val = !!(clock->flags & CLKFLAG_HWCG);
138 return 0;
139}
140
141DEFINE_SIMPLE_ATTRIBUTE(clock_hwcg_fops, clock_debug_hwcg_get,
142 NULL, "%llu\n");
143
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800144static struct dentry *debugfs_base;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145static u32 debug_suspend;
146static struct clk_lookup *msm_clocks;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700147static size_t num_msm_clocks;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800148
Stephen Boydbb600ae2011-08-02 20:11:40 -0700149int __init clock_debug_init(struct clock_init_data *data)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800150{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 int ret = 0;
152
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800153 debugfs_base = debugfs_create_dir("clk", NULL);
154 if (!debugfs_base)
155 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 if (!debugfs_create_u32("debug_suspend", S_IRUGO | S_IWUSR,
157 debugfs_base, &debug_suspend)) {
158 debugfs_remove_recursive(debugfs_base);
159 return -ENOMEM;
160 }
Stephen Boydbb600ae2011-08-02 20:11:40 -0700161 msm_clocks = data->table;
162 num_msm_clocks = data->size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163
164 measure = clk_get_sys("debug", "measure");
165 if (IS_ERR(measure)) {
166 ret = PTR_ERR(measure);
167 measure = NULL;
168 }
169
170 return ret;
171}
172
Stephen Boydcd50fae2011-11-03 11:05:42 -0700173
174static int clock_debug_print_clock(struct clk *c)
175{
176 size_t ln = 0;
177 char s[128];
178
179 if (!c || !c->count)
180 return 0;
181
182 ln += snprintf(s, sizeof(s), "\t%s", c->dbg_name);
183 while (ln < sizeof(s) && (c = clk_get_parent(c)))
184 ln += snprintf(s + ln, sizeof(s) - ln, " -> %s", c->dbg_name);
185 pr_info("%s\n", s);
186 return 1;
187}
188
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189void clock_debug_print_enabled(void)
190{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 unsigned i;
192 int cnt = 0;
193
194 if (likely(!debug_suspend))
195 return;
196
197 pr_info("Enabled clocks:\n");
Stephen Boydcd50fae2011-11-03 11:05:42 -0700198 for (i = 0; i < num_msm_clocks; i++)
199 cnt += clock_debug_print_clock(msm_clocks[i].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200
201 if (cnt)
202 pr_info("Enabled clock count: %d\n", cnt);
203 else
204 pr_info("No clocks enabled.\n");
205
206}
207
208static int list_rates_show(struct seq_file *m, void *unused)
209{
210 struct clk *clock = m->private;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700211 int rate, level, fmax = 0, i = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700213 /* Find max frequency supported within voltage constraints. */
214 if (!clock->vdd_class) {
Matt Wagantalle426f9042011-11-01 16:21:34 -0700215 fmax = INT_MAX;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700216 } else {
217 for (level = 0; level < ARRAY_SIZE(clock->fmax); level++)
218 if (clock->fmax[level])
219 fmax = clock->fmax[level];
220 }
221
222 /*
223 * List supported frequencies <= fmax. Higher frequencies may appear in
224 * the frequency table, but are not valid and should not be listed.
225 */
226 while ((rate = clock->ops->list_rate(clock, i++)) >= 0) {
227 if (rate <= fmax)
228 seq_printf(m, "%u\n", rate);
229 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800231 return 0;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800232}
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234static int list_rates_open(struct inode *inode, struct file *file)
235{
236 return single_open(file, list_rates_show, inode->i_private);
237}
238
239static const struct file_operations list_rates_fops = {
240 .open = list_rates_open,
241 .read = seq_read,
242 .llseek = seq_lseek,
243 .release = seq_release,
244};
245
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800246int __init clock_debug_add(struct clk *clock)
247{
248 char temp[50], *ptr;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800249 struct dentry *clk_dir;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800250
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800251 if (!debugfs_base)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800252 return -ENOMEM;
253
Saravana Kannan7f626982011-09-26 19:02:02 -0700254 strlcpy(temp, clock->dbg_name, ARRAY_SIZE(temp));
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800255 for (ptr = temp; *ptr; ptr++)
256 *ptr = tolower(*ptr);
257
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800258 clk_dir = debugfs_create_dir(temp, debugfs_base);
259 if (!clk_dir)
260 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800261
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800262 if (!debugfs_create_file("rate", S_IRUGO | S_IWUSR, clk_dir,
263 clock, &clock_rate_fops))
264 goto error;
265
266 if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, clk_dir,
267 clock, &clock_enable_fops))
268 goto error;
269
270 if (!debugfs_create_file("is_local", S_IRUGO, clk_dir, clock,
271 &clock_local_fops))
272 goto error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273
Stephen Boyda52d7e32011-11-10 11:59:00 -0800274 if (!debugfs_create_file("has_hw_gating", S_IRUGO, clk_dir, clock,
275 &clock_hwcg_fops))
276 goto error;
277
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 if (measure &&
279 !clk_set_parent(measure, clock) &&
280 !debugfs_create_file("measure", S_IRUGO, clk_dir, clock,
281 &clock_measure_fops))
282 goto error;
283
284 if (clock->ops->list_rate)
285 if (!debugfs_create_file("list_rates",
286 S_IRUGO, clk_dir, clock, &list_rates_fops))
287 goto error;
288
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800289 return 0;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800290error:
291 debugfs_remove_recursive(clk_dir);
292 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800293}