blob: c0af9b512f21ec34b1ff1092b570ae282a269303 [file] [log] [blame]
Matt Wagantalld64560fe2011-01-26 16:20:54 -08001/*
2 * Copyright (C) 2007 Google, Inc.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 * Copyright (c) 2007-2011, 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);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036 if (clock->flags & CLKFLAG_MIN)
Matt Wagantalld64560fe2011-01-26 16:20:54 -080037 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 Huntsman3f2bc4d2011-08-16 17:27:22 -070042 (clock->flags & CLKFLAG_MIN) ? "_min" : "", ret);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080043 return ret;
44}
45
46static 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
53DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
54 clock_debug_rate_set, "%llu\n");
55
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056static struct clk *measure;
57
58static 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
70DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get,
71 NULL, "%lld\n");
72
Matt Wagantalld64560fe2011-01-26 16:20:54 -080073static int clock_debug_enable_set(void *data, u64 val)
74{
75 struct clk *clock = data;
76 int rc = 0;
77
78 if (val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 rc = clk_enable(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080080 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 clk_disable(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080082
83 return rc;
84}
85
86static int clock_debug_enable_get(void *data, u64 *val)
87{
88 struct clk *clock = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089 int enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -080090
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 if (clock->ops->is_enabled)
92 enabled = clock->ops->is_enabled(clock);
93 else
94 enabled = !!(clock->count);
Matt Wagantalld64560fe2011-01-26 16:20:54 -080095
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 *val = enabled;
Matt Wagantalld64560fe2011-01-26 16:20:54 -080097 return 0;
98}
99
100DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 clock_debug_enable_set, "%lld\n");
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800102
103static int clock_debug_local_get(void *data, u64 *val)
104{
105 struct clk *clock = data;
106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 *val = clock->ops->is_local(clock);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800108
109 return 0;
110}
111
112DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
113 NULL, "%llu\n");
114
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800115static struct dentry *debugfs_base;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116static u32 debug_suspend;
117static struct clk_lookup *msm_clocks;
Stephen Boydbb600ae2011-08-02 20:11:40 -0700118static size_t num_msm_clocks;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800119
Stephen Boydbb600ae2011-08-02 20:11:40 -0700120int __init clock_debug_init(struct clock_init_data *data)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800121{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 int ret = 0;
123
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800124 debugfs_base = debugfs_create_dir("clk", NULL);
125 if (!debugfs_base)
126 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 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 Boydbb600ae2011-08-02 20:11:40 -0700132 msm_clocks = data->table;
133 num_msm_clocks = data->size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
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
144void 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 Wagantall96de0ea2011-09-20 14:01:21 -0700157 if (clk && clk->ops->is_enabled && clk->ops->is_enabled(clk)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 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
170static int list_rates_show(struct seq_file *m, void *unused)
171{
172 struct clk *clock = m->private;
173 int rate, i = 0;
174
175 while ((rate = clock->ops->list_rate(clock, i++)) >= 0)
176 seq_printf(m, "%d\n", rate);
177
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800178 return 0;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800179}
180
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181static int list_rates_open(struct inode *inode, struct file *file)
182{
183 return single_open(file, list_rates_show, inode->i_private);
184}
185
186static const struct file_operations list_rates_fops = {
187 .open = list_rates_open,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .release = seq_release,
191};
192
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800193int __init clock_debug_add(struct clk *clock)
194{
195 char temp[50], *ptr;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800196 struct dentry *clk_dir;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800197
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800198 if (!debugfs_base)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800199 return -ENOMEM;
200
201 strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1);
202 for (ptr = temp; *ptr; ptr++)
203 *ptr = tolower(*ptr);
204
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800205 clk_dir = debugfs_create_dir(temp, debugfs_base);
206 if (!clk_dir)
207 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800208
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800209 if (!debugfs_create_file("rate", S_IRUGO | S_IWUSR, clk_dir,
210 clock, &clock_rate_fops))
211 goto error;
212
213 if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, clk_dir,
214 clock, &clock_enable_fops))
215 goto error;
216
217 if (!debugfs_create_file("is_local", S_IRUGO, clk_dir, clock,
218 &clock_local_fops))
219 goto error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
221 if (measure &&
222 !clk_set_parent(measure, clock) &&
223 !debugfs_create_file("measure", S_IRUGO, clk_dir, clock,
224 &clock_measure_fops))
225 goto error;
226
227 if (clock->ops->list_rate)
228 if (!debugfs_create_file("list_rates",
229 S_IRUGO, clk_dir, clock, &list_rates_fops))
230 goto error;
231
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800232 return 0;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800233error:
234 debugfs_remove_recursive(clk_dir);
235 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800236}