Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google, Inc. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved. |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 4 | * |
| 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 21 | #include <linux/clk.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/clkdev.h> |
| 24 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 25 | #include "clock.h" |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 26 | |
| 27 | static 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | if (clock->flags & CLKFLAG_MAX) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 35 | clk_set_max_rate(clock, val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | if (clock->flags & CLKFLAG_MIN) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 37 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | (clock->flags & CLKFLAG_MIN) ? "_min" : "", ret); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static 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 | |
| 53 | DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get, |
| 54 | clock_debug_rate_set, "%llu\n"); |
| 55 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | static struct clk *measure; |
| 57 | |
| 58 | static 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 | |
| 70 | DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get, |
| 71 | NULL, "%lld\n"); |
| 72 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 73 | static int clock_debug_enable_set(void *data, u64 val) |
| 74 | { |
| 75 | struct clk *clock = data; |
| 76 | int rc = 0; |
| 77 | |
| 78 | if (val) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | rc = clk_enable(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 80 | else |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | clk_disable(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 82 | |
| 83 | return rc; |
| 84 | } |
| 85 | |
| 86 | static int clock_debug_enable_get(void *data, u64 *val) |
| 87 | { |
| 88 | struct clk *clock = data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 89 | int enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 90 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | if (clock->ops->is_enabled) |
| 92 | enabled = clock->ops->is_enabled(clock); |
| 93 | else |
| 94 | enabled = !!(clock->count); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 95 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | *val = enabled; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 101 | clock_debug_enable_set, "%lld\n"); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 102 | |
| 103 | static int clock_debug_local_get(void *data, u64 *val) |
| 104 | { |
| 105 | struct clk *clock = data; |
| 106 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | *val = clock->ops->is_local(clock); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get, |
| 113 | NULL, "%llu\n"); |
| 114 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 115 | static struct dentry *debugfs_base; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 116 | static u32 debug_suspend; |
| 117 | static struct clk_lookup *msm_clocks; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 118 | static size_t num_msm_clocks; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 119 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 120 | int __init clock_debug_init(struct clock_init_data *data) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 121 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 122 | int ret = 0; |
| 123 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 124 | debugfs_base = debugfs_create_dir("clk", NULL); |
| 125 | if (!debugfs_base) |
| 126 | return -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | 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 Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 132 | msm_clocks = data->table; |
| 133 | num_msm_clocks = data->size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | |
| 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 | |
| 144 | void 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 Wagantall | 96de0ea | 2011-09-20 14:01:21 -0700 | [diff] [blame^] | 157 | if (clk && clk->ops->is_enabled && clk->ops->is_enabled(clk)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | 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 | |
| 170 | static 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 Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 178 | return 0; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 181 | static int list_rates_open(struct inode *inode, struct file *file) |
| 182 | { |
| 183 | return single_open(file, list_rates_show, inode->i_private); |
| 184 | } |
| 185 | |
| 186 | static 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 Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 193 | int __init clock_debug_add(struct clk *clock) |
| 194 | { |
| 195 | char temp[50], *ptr; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 196 | struct dentry *clk_dir; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 197 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 198 | if (!debugfs_base) |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 199 | 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 Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 205 | clk_dir = debugfs_create_dir(temp, debugfs_base); |
| 206 | if (!clk_dir) |
| 207 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 208 | |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 209 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | |
| 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 Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 232 | return 0; |
Stephen Boyd | 6e6d9b5 | 2011-01-26 16:20:55 -0800 | [diff] [blame] | 233 | error: |
| 234 | debugfs_remove_recursive(clk_dir); |
| 235 | return -ENOMEM; |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 236 | } |