blob: d540f2b729f9e60087fa0b2d679363f7701e0d0f [file] [log] [blame]
Matt Wagantalld64560fe2011-01-26 16:20:54 -08001/*
2 * Copyright (C) 2007 Google, Inc.
Duy Truong790f06d2013-02-13 16:38:12 -08003 * Copyright (c) 2007-2012, The Linux Foundation. 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>
Matt Wagantall33d01f52012-02-23 23:27:44 -080024#include <mach/clk-provider.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
Matt Wagantalld64560fe2011-01-26 16:20:54 -080026#include "clock.h"
Matt Wagantalld64560fe2011-01-26 16:20:54 -080027
28static int clock_debug_rate_set(void *data, u64 val)
29{
30 struct clk *clock = data;
31 int ret;
32
33 /* Only increases to max rate will succeed, but that's actually good
34 * for debugging purposes so we don't check for error. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035 if (clock->flags & CLKFLAG_MAX)
Matt Wagantalld64560fe2011-01-26 16:20:54 -080036 clk_set_max_rate(clock, val);
Matt Wagantall8e6126f2011-11-08 13:34:19 -080037 ret = clk_set_rate(clock, val);
38 if (ret)
Stephen Boyd753ab932012-08-02 13:14:38 -070039 pr_err("clk_set_rate(%s, %lu) failed (%d)\n", clock->dbg_name,
40 (unsigned long)val, ret);
Matt Wagantall8e6126f2011-11-08 13:34:19 -080041
Matt Wagantalld64560fe2011-01-26 16:20:54 -080042 return ret;
43}
44
45static int clock_debug_rate_get(void *data, u64 *val)
46{
47 struct clk *clock = data;
48 *val = clk_get_rate(clock);
49 return 0;
50}
51
52DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
53 clock_debug_rate_set, "%llu\n");
54
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055static struct clk *measure;
56
57static int clock_debug_measure_get(void *data, u64 *val)
58{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 struct clk *clock = data;
Stephen Boyda52d7e32011-11-10 11:59:00 -080060 int ret, is_hw_gated;
61
62 /* Check to see if the clock is in hardware gating mode */
Stephen Boyd0f7e5642012-08-02 12:59:33 -070063 if (clock->ops->in_hwcg_mode)
Stephen Boyda52d7e32011-11-10 11:59:00 -080064 is_hw_gated = clock->ops->in_hwcg_mode(clock);
65 else
66 is_hw_gated = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
68 ret = clk_set_parent(measure, clock);
Stephen Boyda52d7e32011-11-10 11:59:00 -080069 if (!ret) {
70 /*
71 * Disable hw gating to get accurate rate measurements. Only do
72 * this if the clock is explictly enabled by software. This
73 * allows us to detect errors where clocks are on even though
74 * software is not requesting them to be on due to broken
75 * hardware gating signals.
76 */
77 if (is_hw_gated && clock->count)
78 clock->ops->disable_hwcg(clock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 *val = clk_get_rate(measure);
Stephen Boyda52d7e32011-11-10 11:59:00 -080080 /* Reenable hwgating if it was disabled */
81 if (is_hw_gated && clock->count)
82 clock->ops->enable_hwcg(clock);
83 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084
85 return ret;
86}
87
88DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get,
89 NULL, "%lld\n");
90
Matt Wagantalld64560fe2011-01-26 16:20:54 -080091static int clock_debug_enable_set(void *data, u64 val)
92{
93 struct clk *clock = data;
94 int rc = 0;
95
96 if (val)
Stephen Boyd3bbf3462012-01-12 00:19:23 -080097 rc = clk_prepare_enable(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080098 else
Stephen Boyd3bbf3462012-01-12 00:19:23 -080099 clk_disable_unprepare(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800100
101 return rc;
102}
103
104static int clock_debug_enable_get(void *data, u64 *val)
105{
106 struct clk *clock = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 int enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 if (clock->ops->is_enabled)
110 enabled = clock->ops->is_enabled(clock);
111 else
112 enabled = !!(clock->count);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 *val = enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800115 return 0;
116}
117
118DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 clock_debug_enable_set, "%lld\n");
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800120
121static int clock_debug_local_get(void *data, u64 *val)
122{
123 struct clk *clock = data;
124
Matt Wagantallacb8d022012-02-14 15:28:23 -0800125 if (!clock->ops->is_local)
126 *val = true;
127 else
128 *val = clock->ops->is_local(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800129
130 return 0;
131}
132
133DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
134 NULL, "%llu\n");
135
Stephen Boyda52d7e32011-11-10 11:59:00 -0800136static int clock_debug_hwcg_get(void *data, u64 *val)
137{
138 struct clk *clock = data;
Stephen Boyd0f7e5642012-08-02 12:59:33 -0700139 if (clock->ops->in_hwcg_mode)
140 *val = !!clock->ops->in_hwcg_mode(clock);
141 else
142 *val = 0;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800143 return 0;
144}
145
146DEFINE_SIMPLE_ATTRIBUTE(clock_hwcg_fops, clock_debug_hwcg_get,
147 NULL, "%llu\n");
148
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800149static int fmax_rates_show(struct seq_file *m, void *unused)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800150{
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800151 struct clk *clock = m->private;
152 int level = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800154 int vdd_level = find_vdd_level(clock, clock->rate);
155 if (vdd_level < 0) {
156 seq_printf(m, "could not find_vdd_level for %s, %ld\n",
157 clock->dbg_name, clock->rate);
158 return 0;
159 }
Saravana Kannan55e959d2012-10-15 22:16:04 -0700160 for (level = 0; level < clock->num_fmax; level++) {
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800161 if (vdd_level == level)
162 seq_printf(m, "[%lu] ", clock->fmax[level]);
163 else
164 seq_printf(m, "%lu ", clock->fmax[level]);
165 }
166 seq_printf(m, "\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167
Stephen Boyd31c01e82012-04-13 15:22:00 -0700168 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169}
170
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800171static int fmax_rates_open(struct inode *inode, struct file *file)
Stephen Boydcd50fae2011-11-03 11:05:42 -0700172{
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800173 return single_open(file, fmax_rates_show, inode->i_private);
Stephen Boydcd50fae2011-11-03 11:05:42 -0700174}
175
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800176static const struct file_operations fmax_rates_fops = {
177 .open = fmax_rates_open,
178 .read = seq_read,
179 .llseek = seq_lseek,
180 .release = seq_release,
181};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
183static int list_rates_show(struct seq_file *m, void *unused)
184{
185 struct clk *clock = m->private;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700186 int rate, level, fmax = 0, i = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700188 /* Find max frequency supported within voltage constraints. */
189 if (!clock->vdd_class) {
Matt Wagantalle426f9042011-11-01 16:21:34 -0700190 fmax = INT_MAX;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700191 } else {
Saravana Kannan55e959d2012-10-15 22:16:04 -0700192 for (level = 0; level < clock->num_fmax; level++)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700193 if (clock->fmax[level])
194 fmax = clock->fmax[level];
195 }
196
197 /*
198 * List supported frequencies <= fmax. Higher frequencies may appear in
199 * the frequency table, but are not valid and should not be listed.
200 */
201 while ((rate = clock->ops->list_rate(clock, i++)) >= 0) {
202 if (rate <= fmax)
203 seq_printf(m, "%u\n", rate);
204 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800206 return 0;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800207}
208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209static int list_rates_open(struct inode *inode, struct file *file)
210{
211 return single_open(file, list_rates_show, inode->i_private);
212}
213
214static const struct file_operations list_rates_fops = {
215 .open = list_rates_open,
216 .read = seq_read,
217 .llseek = seq_lseek,
218 .release = seq_release,
219};
220
Saravana Kannan531051f2012-09-27 16:19:07 -0700221static int clock_parent_show(struct seq_file *m, void *unused)
222{
223 struct clk *clock = m->private;
224 struct clk *parent = clk_get_parent(clock);
225
226 seq_printf(m, "%s\n", (parent ? parent->dbg_name : "None"));
227
228 return 0;
229}
230
231static int clock_parent_open(struct inode *inode, struct file *file)
232{
233 return single_open(file, clock_parent_show, inode->i_private);
234}
235
236static const struct file_operations clock_parent_fops = {
237 .open = clock_parent_open,
238 .read = seq_read,
239 .llseek = seq_lseek,
240 .release = seq_release,
241};
242
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800243static struct dentry *debugfs_base;
244static u32 debug_suspend;
Patrick Daly0a78a0e2012-07-23 13:18:59 -0700245
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800246struct clk_table {
247 struct list_head node;
248 struct clk_lookup *clocks;
249 size_t num_clocks;
Patrick Daly0a78a0e2012-07-23 13:18:59 -0700250};
251
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800252static int clock_debug_add(struct clk *clock)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800253{
254 char temp[50], *ptr;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800255 struct dentry *clk_dir;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800256
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800257 if (!debugfs_base)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800258 return -ENOMEM;
259
Saravana Kannan7f626982011-09-26 19:02:02 -0700260 strlcpy(temp, clock->dbg_name, ARRAY_SIZE(temp));
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800261 for (ptr = temp; *ptr; ptr++)
262 *ptr = tolower(*ptr);
263
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800264 clk_dir = debugfs_create_dir(temp, debugfs_base);
265 if (!clk_dir)
266 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800267
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800268 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700279
Stephen Boyda52d7e32011-11-10 11:59:00 -0800280 if (!debugfs_create_file("has_hw_gating", S_IRUGO, clk_dir, clock,
281 &clock_hwcg_fops))
282 goto error;
283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 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
Patrick Daly0a78a0e2012-07-23 13:18:59 -0700295 if (clock->vdd_class && !debugfs_create_file("fmax_rates",
296 S_IRUGO, clk_dir, clock, &fmax_rates_fops))
297 goto error;
298
Saravana Kannan531051f2012-09-27 16:19:07 -0700299 if (!debugfs_create_file("parent", S_IRUGO, clk_dir, clock,
300 &clock_parent_fops))
301 goto error;
302
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800303 return 0;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800304error:
305 debugfs_remove_recursive(clk_dir);
306 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800307}
Matt Wagantall665f0cf2012-02-27 15:54:43 -0800308static LIST_HEAD(clk_list);
309static DEFINE_SPINLOCK(clk_list_lock);
310
311/**
312 * clock_debug_register() - Add additional clocks to clock debugfs hierarchy
313 * @table: Table of clocks to create debugfs nodes for
314 * @size: Size of @table
315 *
316 * Use this function to register additional clocks in debugfs. The clock debugfs
317 * hierarchy must have already been initialized with clock_debug_init() prior to
318 * calling this function. Unlike clock_debug_init(), this may be called multiple
319 * times with different clock lists and can be used after the kernel has
320 * finished booting.
321 */
322int clock_debug_register(struct clk_lookup *table, size_t size)
323{
324 struct clk_table *clk_table;
325 unsigned long flags;
326 int i;
327
328 clk_table = kmalloc(sizeof(*clk_table), GFP_KERNEL);
329 if (!clk_table)
330 return -ENOMEM;
331
332 clk_table->clocks = table;
333 clk_table->num_clocks = size;
334
335 spin_lock_irqsave(&clk_list_lock, flags);
336 list_add_tail(&clk_table->node, &clk_list);
337 spin_unlock_irqrestore(&clk_list_lock, flags);
338
339 for (i = 0; i < size; i++)
340 clock_debug_add(table[i].clk);
341
342 return 0;
343}
344
345/**
346 * clock_debug_init() - Initialize clock debugfs
347 */
348int __init clock_debug_init(void)
349{
350 debugfs_base = debugfs_create_dir("clk", NULL);
351 if (!debugfs_base)
352 return -ENOMEM;
353 if (!debugfs_create_u32("debug_suspend", S_IRUGO | S_IWUSR,
354 debugfs_base, &debug_suspend)) {
355 debugfs_remove_recursive(debugfs_base);
356 return -ENOMEM;
357 }
358
359 measure = clk_get_sys("debug", "measure");
360 if (IS_ERR(measure))
361 measure = NULL;
362
363 return 0;
364}
365
366static int clock_debug_print_clock(struct clk *c)
367{
368 char *start = "";
369
370 if (!c || !c->prepare_count)
371 return 0;
372
373 pr_info("\t");
374 do {
375 if (c->vdd_class)
376 pr_cont("%s%s:%u:%u [%ld, %lu]", start, c->dbg_name,
377 c->prepare_count, c->count, c->rate,
378 c->vdd_class->cur_level);
379 else
380 pr_cont("%s%s:%u:%u [%ld]", start, c->dbg_name,
381 c->prepare_count, c->count, c->rate);
382 start = " -> ";
383 } while ((c = clk_get_parent(c)));
384
385 pr_cont("\n");
386
387 return 1;
388}
389
390/**
391 * clock_debug_print_enabled() - Print names of enabled clocks for suspend debug
392 *
393 * Print the names of enabled clocks and their parents if debug_suspend is set
394 */
395void clock_debug_print_enabled(void)
396{
397 struct clk_table *table;
398 unsigned long flags;
399 int i, cnt = 0;
400
401 if (likely(!debug_suspend))
402 return;
403
404 pr_info("Enabled clocks:\n");
405 spin_lock_irqsave(&clk_list_lock, flags);
406 list_for_each_entry(table, &clk_list, node) {
407 for (i = 0; i < table->num_clocks; i++)
408 cnt += clock_debug_print_clock(table->clocks[i].clk);
409 }
410 spin_unlock_irqrestore(&clk_list_lock, flags);
411
412 if (cnt)
413 pr_info("Enabled clock count: %d\n", cnt);
414 else
415 pr_info("No clocks enabled.\n");
416
417}