blob: 4e0d3e9fdcd507d042a2a7296771d7d6e837498e [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;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700173 int rate, level, fmax = 0, i = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700175 /* 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800193 return 0;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800194}
195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196static int list_rates_open(struct inode *inode, struct file *file)
197{
198 return single_open(file, list_rates_show, inode->i_private);
199}
200
201static 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 Wagantalld64560fe2011-01-26 16:20:54 -0800208int __init clock_debug_add(struct clk *clock)
209{
210 char temp[50], *ptr;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800211 struct dentry *clk_dir;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800212
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800213 if (!debugfs_base)
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800214 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 Boyd6e6d9b52011-01-26 16:20:55 -0800220 clk_dir = debugfs_create_dir(temp, debugfs_base);
221 if (!clk_dir)
222 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800223
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800224 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
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 Wagantalld64560fe2011-01-26 16:20:54 -0800247 return 0;
Stephen Boyd6e6d9b52011-01-26 16:20:55 -0800248error:
249 debugfs_remove_recursive(clk_dir);
250 return -ENOMEM;
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800251}