blob: c42f58923750b4b3ef954d564069f82db7a39dd0 [file] [log] [blame]
Amit Arora728e0c92010-09-14 12:06:09 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora728e0c92010-09-14 12:06:09 +05303 *
4 * This file is part of PowerDebug.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors:
12 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
13 * - initial API and implementation
14 *******************************************************************************/
15
16#include "powerdebug.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053017#include "clocks.h"
Amit Arora24ed7d12010-09-14 12:12:58 +053018
Amit Arora24ed7d12010-09-14 12:12:58 +053019static char clk_dir_path[PATH_MAX];
Amit Arora24ed7d12010-09-14 12:12:58 +053020static char clk_name[NAME_MAX];
Amit Aroraac4e8652010-11-09 11:16:53 +053021static int bold[MAX_LINES];
Amit Arora24ed7d12010-09-14 12:12:58 +053022
Daniel Lezcano558a6d52011-03-23 14:37:41 +010023int init_clock_details(bool dump, int selectedwindow)
Amit Arora24ed7d12010-09-14 12:12:58 +053024{
Amit Arora6e774cd2010-10-28 11:31:24 +053025 char *path = debugfs_locate_mpoint();
26 struct stat buf;
Amit Aroraed3e5652010-10-27 12:02:53 +053027
Amit Arora6e774cd2010-10-28 11:31:24 +053028 if (path)
29 strcpy(clk_dir_path, path);
30 else {
Amit Arora81350772010-11-16 14:56:26 +053031 if (!dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +010032 create_selectedwindow(selectedwindow);
Amit Arora81350772010-11-16 14:56:26 +053033 sprintf(clock_lines[0], "Unable to locate debugfs "
Amit Kucheriaa0adae42011-01-12 10:54:23 -060034 "mount point. Mount debugfs "
35 "and try again..\n");
Amit Arora81350772010-11-16 14:56:26 +053036 print_one_clock(0, clock_lines[0], 1, 0);
37 old_clock_line_no = 1;
38 return(1);
39 } else {
40 fprintf(stderr, "powerdebug: Unable to locate debugfs "
Amit Kucheriaa0adae42011-01-12 10:54:23 -060041 "mount point. Mount debugfs and try "
42 "again..\n");
Amit Arora81350772010-11-16 14:56:26 +053043 exit(1);
44 }
Amit Arora6e774cd2010-10-28 11:31:24 +053045 }
46 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
Amit Arora59429a42010-11-16 11:30:20 +053047 //strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
Amit Arora6e774cd2010-10-28 11:31:24 +053048 if (stat(clk_dir_path, &buf)) {
Amit Arora81350772010-11-16 14:56:26 +053049 if (!dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +010050 create_selectedwindow(selectedwindow);
Amit Arora81350772010-11-16 14:56:26 +053051 sprintf(clock_lines[0], "Unable to find clock tree"
52 " information at %s.\n", clk_dir_path);
53 print_one_clock(0, clock_lines[0], 1, 0);
54 old_clock_line_no = 1;
55 return(1);
56 } else {
57 fprintf(stderr, "powerdebug: Unable to find clock tree"
58 " information at %s.\n", clk_dir_path);
59 exit(1);
60 }
Amit Arora6e774cd2010-10-28 11:31:24 +053061 }
62 strcpy(clk_name, "");
Amit Arora04f97742010-11-16 11:28:57 +053063 return(0);
Amit Arora24ed7d12010-09-14 12:12:58 +053064}
Amit Arora728e0c92010-09-14 12:06:09 +053065
66int get_int_from(char *file)
67{
Amit Arora6e774cd2010-10-28 11:31:24 +053068 FILE *filep;
69 char result[NAME_MAX];
70 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +053071
Amit Arora6e774cd2010-10-28 11:31:24 +053072 filep = fopen(file, "r");
Amit Arora728e0c92010-09-14 12:06:09 +053073
Amit Arora6e774cd2010-10-28 11:31:24 +053074 if (!filep)
75 return -1; //TBD : What should we return on failure, here ?
Amit Arora728e0c92010-09-14 12:06:09 +053076
Amit Arora6e774cd2010-10-28 11:31:24 +053077 ret = fscanf(filep, "%s", result);
78 fclose(filep);
Amit Arora728e0c92010-09-14 12:06:09 +053079
Amit Arora6e774cd2010-10-28 11:31:24 +053080 return atoi(result);
Amit Arora728e0c92010-09-14 12:06:09 +053081}
82
Daniel Lezcanoc6718332011-03-23 14:37:39 +010083static void dump_parent(struct clock_info *clk, int line, bool dump)
84{
85 char *unit = "Hz";
86 double drate;
87 static char spaces[64];
88 char str[256];
89 static int maxline;
90
91 if (maxline < line)
92 maxline = line;
93
94 if (clk && clk->parent)
95 dump_parent(clk->parent, ++line, dump);
96
97 drate = (double)clk->rate;
98 if (drate > 1000 && drate < 1000000) {
99 unit = "KHz";
100 drate /= 1000;
101 }
102 if (drate > 1000000) {
103 unit = "MHz";
104 drate /= 1000000;
105 }
106 if (clk == clocks_info) {
107 line++;
108 strcpy(spaces, "");
109 sprintf(str, "%s%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
110 spaces, clk->name, clk->flags, clk->usecount, drate,
111 unit);
112 } else {
113 if (!(clk->parent == clocks_info))
114 strcat(spaces, " ");
115 sprintf(str, "%s`- %s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
116 spaces, clk->name, clk->flags, clk->usecount, drate,
117 unit);
118 }
119 if (dump)
120 //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str);
121 printf("%s", str);
122 else
123 print_one_clock(maxline - line + 2, str, 1, 0);
124}
125
126static void dump_all_parents(char *clkarg, bool dump)
127{
128 struct clock_info *clk;
129 char spaces[1024];
130
131 strcpy(spaces, "");
132
133 clk = find_clock(clocks_info, clkarg);
134
135 if (!clk)
136 printf("Clock NOT found!\n");
137 else {
138 /* while(clk && clk != clocks_info) { */
139 /* printf("%s\n", clk->name); */
140 /* strcat(spaces, " "); */
141 /* clk = clk->parent; */
142 /* printf("%s <-- ", spaces); */
143 /* } */
144 /* printf(" /\n"); */
145 dump_parent(clk, 1, dump);
146 }
147}
148
Daniel Lezcano897f7332011-03-26 22:06:06 +0100149void find_parents_for_clock(char *clkname, int complete)
Amit Arora3bd79162010-12-01 13:51:42 +0530150{
151 char name[256];
152
153 name[0] = '\0';
154 if (!complete) {
155 char str[256];
156
157 strcat(name, clkname);
158 sprintf(str, "Enter Clock Name : %s\n", name);
159 print_one_clock(2, str, 1, 0);
160 return;
161 }
162 sprintf(name, "Parents for \"%s\" Clock : \n", clkname);
163 print_one_clock(0, name, 1, 1);
Daniel Lezcano897f7332011-03-26 22:06:06 +0100164 dump_all_parents(clkname, false);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600165}
Amit Arora3bd79162010-12-01 13:51:42 +0530166
Amit Arora24ed7d12010-09-14 12:12:58 +0530167int read_and_print_clock_info(int verbose, int hrow, int selected)
168{
Amit Arora3bd79162010-12-01 13:51:42 +0530169 print_one_clock(0, "Reading Clock Tree ...", 1, 1);
Amit Aroraa06a7302010-12-02 15:59:37 +0530170
171 if (!old_clock_line_no || selected == REFRESH_WINDOW) {
172 destroy_clocks_info();
Amit Aroraac4e8652010-11-09 11:16:53 +0530173 read_clock_info(clk_dir_path);
Amit Aroraa06a7302010-12-02 15:59:37 +0530174 }
Amit Arora29cb7572010-10-05 17:40:29 +0530175
Amit Arora031263a2010-11-09 11:12:41 +0530176 if (!clocks_info->num_children) {
177 fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
178 exit(1);
Amit Arora6e774cd2010-10-28 11:31:24 +0530179 }
Amit Arora728e0c92010-09-14 12:06:09 +0530180
Amit Aroraa06a7302010-12-02 15:59:37 +0530181 if (selected == CLOCK_SELECTED)
182 selected = 1;
183 else
184 selected = 0;
185
Amit Aroraac4e8652010-11-09 11:16:53 +0530186 print_clock_info(verbose, hrow, selected);
Amit Aroraac4e8652010-11-09 11:16:53 +0530187 hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
188
189 return hrow;
190}
191
Amit Arora3bc8c922010-11-16 11:27:38 +0530192int calc_delta_screen_size(int hrow)
193{
Amit Arora51d1b9c2010-11-30 13:55:15 +0530194 if (hrow >= (maxy - 3))
195 return hrow - (maxy - 4);
Amit Arora3bc8c922010-11-16 11:27:38 +0530196
197 return 0;
198}
199
Amit Aroraac4e8652010-11-09 11:16:53 +0530200void print_clock_info(int verbose, int hrow, int selected)
201{
Amit Arora3bc8c922010-11-16 11:27:38 +0530202 int i, count = 0, delta;
Amit Aroraac4e8652010-11-09 11:16:53 +0530203
204 (void)verbose;
205
206 print_clock_header();
Amit Arora728e0c92010-09-14 12:06:09 +0530207
Amit Arora031263a2010-11-09 11:12:41 +0530208 for (i = 0; i < clocks_info->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530209 add_clock_details_recur(clocks_info->children[i],
210 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530211
Amit Arora3bc8c922010-11-16 11:27:38 +0530212 delta = calc_delta_screen_size(hrow);
213
214 while (clock_lines[count + delta] &&
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600215 strcmp(clock_lines[count + delta], "")) {
Amit Arora3bc8c922010-11-16 11:27:38 +0530216 if (count < delta) {
217 count++;
218 continue;
219 }
220 print_one_clock(count - delta, clock_lines[count + delta],
221 bold[count + delta], (hrow == (count + delta)));
Amit Arora031263a2010-11-09 11:12:41 +0530222 count++;
223 }
224
Amit Aroraac4e8652010-11-09 11:16:53 +0530225 old_clock_line_no = clock_line_no;
Amit Arora031263a2010-11-09 11:12:41 +0530226 clock_line_no = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530227}
228
Amit Aroraac4e8652010-11-09 11:16:53 +0530229void prepare_name_str(char *namestr, struct clock_info *clock)
230{
231 int i;
232
233 strcpy(namestr, "");
234 if (clock->level > 1)
235 for (i = 0; i < (clock->level - 1); i++)
236 strcat(namestr, " ");
237 strcat(namestr, clock->name);
238}
239
240void add_clock_details_recur(struct clock_info *clock, int hrow, int selected)
Amit Arora031263a2010-11-09 11:12:41 +0530241{
242 int i;
243 char *unit = " Hz";
244 char rate_str[64];
Amit Aroraac4e8652010-11-09 11:16:53 +0530245 char name_str[256];
Amit Arora031263a2010-11-09 11:12:41 +0530246 double drate = (double)clock->rate;
247
248 if (drate > 1000 && drate < 1000000) {
249 unit = "KHz";
250 drate /= 1000;
251 }
252 if (drate > 1000000) {
253 unit = "MHz";
254 drate /= 1000000;
255 }
256 if (clock->usecount)
257 bold[clock_line_no] = 1;
258 else
259 bold[clock_line_no] = 0;
260
261 sprintf(rate_str, "%.2f %s", drate, unit);
Amit Aroraac4e8652010-11-09 11:16:53 +0530262 prepare_name_str(name_str, clock);
263 sprintf(clock_lines[clock_line_no++], "%-55s %-4d %-12s %-12d %-12d",
264 name_str, clock->flags, rate_str, clock->usecount,
265 clock->num_children);
266
267 if (selected && (hrow == (clock_line_no - 1))) {
268 if (clock->expanded)
269 collapse_all_subclocks(clock);
270 else
271 clock->expanded = 1;
272 selected = 0;
273 }
Amit Arora031263a2010-11-09 11:12:41 +0530274
275 if (clock->expanded && clock->num_children)
276 for (i = 0; i < clock->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530277 add_clock_details_recur(clock->children[i],
278 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530279 strcpy(clock_lines[clock_line_no], "");
280}
281
Amit Aroraac4e8652010-11-09 11:16:53 +0530282void collapse_all_subclocks(struct clock_info *clock)
283{
284 int i;
285
286 clock->expanded = 0;
287 if (clock->num_children)
288 for (i = 0; i < clock->num_children; i++)
289 collapse_all_subclocks(clock->children[i]);
290}
291
Amit Arora031263a2010-11-09 11:12:41 +0530292void destroy_clocks_info(void)
293{
294 int i;
295
Amit Aroraa06a7302010-12-02 15:59:37 +0530296 if (!clocks_info)
297 return;
298
Amit Arora031263a2010-11-09 11:12:41 +0530299 if (clocks_info->num_children) {
300 for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
301 destroy_clocks_info_recur(clocks_info->children[i]);
302 if (!i) {
303 free(clocks_info->children);
304 clocks_info->children = NULL;
305 }
306 }
307 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530308 clocks_info->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530309 free(clocks_info);
310 clocks_info = NULL;
311}
312
313void destroy_clocks_info_recur(struct clock_info *clock)
314{
315 int i;
316
317 if (clock && clock->num_children) {
318 for (i = (clock->num_children - 1); i >= 0; i--) {
319 fflush(stdin);
320 destroy_clocks_info_recur(clock->children[i]);
321 if (!i) {
Amit Arora031263a2010-11-09 11:12:41 +0530322 free(clock->children);
323 clock->children = NULL;
324 clock->num_children = 0;
325 }
326 }
327 }
Amit Arora728e0c92010-09-14 12:06:09 +0530328}
Amit Arora0e512722010-10-01 12:24:16 +0530329
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100330void read_and_dump_clock_info_one(char *clk, bool dump)
Amit Aroraf4fb8102010-11-30 13:55:50 +0530331{
Amit Arora3bd79162010-12-01 13:51:42 +0530332 printf("\nParents for \"%s\" Clock :\n\n", clk);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530333 read_clock_info(clk_dir_path);
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100334 dump_all_parents(clk, dump);
Amit Arora422c52f2010-12-02 16:22:29 +0530335 printf("\n\n");
Amit Aroraf4fb8102010-11-30 13:55:50 +0530336}
337
Amit Aroraac4e8652010-11-09 11:16:53 +0530338void read_and_dump_clock_info(int verbose)
Amit Arora0e512722010-10-01 12:24:16 +0530339{
Amit Arora6e774cd2010-10-28 11:31:24 +0530340 (void)verbose;
Amit Arora422c52f2010-12-02 16:22:29 +0530341 printf("\nClock Tree :\n");
Amit Arora6e774cd2010-10-28 11:31:24 +0530342 printf("**********\n");
343 read_clock_info(clk_dir_path);
Amit Aroraac4e8652010-11-09 11:16:53 +0530344 dump_clock_info(clocks_info, 1, 1);
Amit Arora422c52f2010-12-02 16:22:29 +0530345 printf("\n\n");
Amit Arora0e512722010-10-01 12:24:16 +0530346}
347
Amit Aroraeb6cba92010-10-25 16:03:21 +0530348void read_clock_info(char *clkpath)
Amit Arora0e512722010-10-01 12:24:16 +0530349{
Amit Arora6e774cd2010-10-28 11:31:24 +0530350 DIR *dir;
351 struct dirent *item;
352 char filename[NAME_MAX], clockname[NAME_MAX];
353 struct clock_info *child;
354 struct clock_info *cur;
Amit Arora0e512722010-10-01 12:24:16 +0530355
Amit Arora6e774cd2010-10-28 11:31:24 +0530356 dir = opendir(clkpath);
357 if (!dir)
358 return;
Amit Arora0e512722010-10-01 12:24:16 +0530359
Amit Arora6e774cd2010-10-28 11:31:24 +0530360 clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
361 memset(clocks_info, 0, sizeof(clocks_info));
362 strcpy(clocks_info->name, "/");
Amit Aroraac4e8652010-11-09 11:16:53 +0530363 clocks_info->level = 0;
Amit Arora0e512722010-10-01 12:24:16 +0530364
Amit Arora6e774cd2010-10-28 11:31:24 +0530365 while ((item = readdir(dir))) {
366 /* skip hidden dirs except ".." */
367 if (item->d_name[0] == '.')
368 continue;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530369
Amit Arora6e774cd2010-10-28 11:31:24 +0530370 strcpy(clockname, item->d_name);
371 sprintf(filename, "%s/%s", clkpath, item->d_name);
372 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
Amit Aroraac4e8652010-11-09 11:16:53 +0530373 memset(cur, 0, sizeof(struct clock_info));
Amit Arora6e774cd2010-10-28 11:31:24 +0530374 strcpy(cur->name, clockname);
375 cur->parent = clocks_info;
Amit Arora031263a2010-11-09 11:12:41 +0530376 cur->num_children = 0;
377 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530378 cur->level = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530379 insert_children(&clocks_info, cur);
380 child = read_clock_info_recur(filename, 2, cur);
381 }
382 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530383}
384
385struct clock_info *read_clock_info_recur(char *clkpath, int level,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600386 struct clock_info *parent)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530387{
Amit Arora6e774cd2010-10-28 11:31:24 +0530388 int ret = 0;
389 DIR *dir;
390 char filename[PATH_MAX];
391 struct dirent *item;
392 struct clock_info *cur = NULL;
393 struct stat buf;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530394
Amit Arora6e774cd2010-10-28 11:31:24 +0530395 dir = opendir(clkpath);
396 if (!dir)
397 return NULL;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530398
Amit Arora6e774cd2010-10-28 11:31:24 +0530399 while ((item = readdir(dir))) {
400 struct clock_info *child;
401 /* skip hidden dirs except ".." */
402 if (item->d_name[0] == '.' )
403 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530404
Amit Arora6e774cd2010-10-28 11:31:24 +0530405 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530406
Amit Arora6e774cd2010-10-28 11:31:24 +0530407 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530408
Amit Arora6e774cd2010-10-28 11:31:24 +0530409 if (ret < 0) {
410 printf("Error doing a stat on %s\n", filename);
411 exit(1);
412 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530413
Amit Arora6e774cd2010-10-28 11:31:24 +0530414 if (S_ISREG(buf.st_mode)) {
415 if (!strcmp(item->d_name, "flags"))
416 parent->flags = get_int_from(filename);
417 if (!strcmp(item->d_name, "rate"))
418 parent->rate = get_int_from(filename);
419 if (!strcmp(item->d_name, "usecount"))
420 parent->usecount = get_int_from(filename);
421 continue;
422 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530423
Amit Arora6e774cd2010-10-28 11:31:24 +0530424 if (!S_ISDIR(buf.st_mode))
425 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530426
Amit Arora6e774cd2010-10-28 11:31:24 +0530427 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
428 memset(cur, 0, sizeof(cur));
429 strcpy(cur->name, item->d_name);
430 cur->children = NULL;
431 cur->parent = NULL;
432 cur->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530433 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530434 cur->level = level;
Amit Arora6e774cd2010-10-28 11:31:24 +0530435 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora6e774cd2010-10-28 11:31:24 +0530436 insert_children(&parent, cur);
437 cur->parent = parent;
438 }
439 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530440
Amit Arora6e774cd2010-10-28 11:31:24 +0530441 return cur;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530442}
443
444void insert_children(struct clock_info **parent, struct clock_info *clk)
445{
Amit Arora031263a2010-11-09 11:12:41 +0530446 if (!(*parent)->num_children || (*parent)->children == NULL) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530447 (*parent)->children = (struct clock_info **)
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600448 malloc(sizeof(struct clock_info *)*2);
Amit Arora6e774cd2010-10-28 11:31:24 +0530449 (*parent)->num_children = 0;
450 } else
451 (*parent)->children = (struct clock_info **)
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600452 realloc((*parent)->children,
453 sizeof(struct clock_info *) *
454 ((*parent)->num_children + 2));
Amit Arora6e774cd2010-10-28 11:31:24 +0530455 if ((*parent)->num_children > 0)
456 (*parent)->children[(*parent)->num_children - 1]->last_child
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600457 = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530458 clk->last_child = 1;
459 (*parent)->children[(*parent)->num_children] = clk;
460 (*parent)->children[(*parent)->num_children + 1] = NULL;
461 (*parent)->num_children++;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530462}
463
Amit Aroraf4fb8102010-11-30 13:55:50 +0530464struct clock_info *find_clock(struct clock_info *clk, char *clkarg)
465{
466 int i;
467 struct clock_info *ret = clk;
468
469 if (!strcmp(clk->name, clkarg))
470 return ret;
471
472 if (clk->children) {
473 for (i = 0; i < clk->num_children; i++) {
474 if (!strcmp(clk->children[i]->name, clkarg))
475 return clk->children[i];
476 }
477 for (i = 0; i < clk->num_children; i++) {
478 ret = find_clock(clk->children[i], clkarg);
479 if (ret)
480 return ret;
481 }
482 }
483
484 return NULL;
485}
486
Amit Aroraeb6cba92010-10-25 16:03:21 +0530487
Amit Aroraac4e8652010-11-09 11:16:53 +0530488void dump_clock_info(struct clock_info *clk, int level, int bmp)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530489{
Amit Arora6e774cd2010-10-28 11:31:24 +0530490 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530491
Amit Arora6e774cd2010-10-28 11:31:24 +0530492 if (!clk)
493 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530494
Amit Arora6e774cd2010-10-28 11:31:24 +0530495 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
496 if (i == (level - 1)) {
497 if (clk->last_child)
498 printf("`-- ");
499 else
500 printf("|-- ");
501 } else {
502 if ((1<<j) & bmp)
503 printf("| ");
504 else
505 printf(" ");
506 }
507 }
Amit Arora031263a2010-11-09 11:12:41 +0530508
Amit Arora6e774cd2010-10-28 11:31:24 +0530509 if (clk == clocks_info)
510 printf("%s\n", clk->name);
511 else {
512 char *unit = "Hz";
513 double drate = (double)clk->rate;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600514
Amit Arora6e774cd2010-10-28 11:31:24 +0530515 if (drate > 1000 && drate < 1000000) {
516 unit = "KHz";
517 drate /= 1000;
518 }
519 if (drate > 1000000) {
520 unit = "MHz";
521 drate /= 1000000;
522 }
523 printf("%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
524 clk->name, clk->flags, clk->usecount, drate, unit);
Amit Arora6e774cd2010-10-28 11:31:24 +0530525 }
526 if (clk->children) {
527 int tbmp = bmp;
528 int xbmp = -1;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530529
Amit Arora6e774cd2010-10-28 11:31:24 +0530530 if (clk->last_child) {
531 xbmp ^= 1 << (level - 2);
532
533 xbmp = tbmp & xbmp;
534 } else
535 xbmp = bmp;
536 for (i = 0; i < clk->num_children; i++) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530537 tbmp = xbmp | (1 << level);
Amit Aroraac4e8652010-11-09 11:16:53 +0530538 dump_clock_info(clk->children[i], level + 1, tbmp);
Amit Arora6e774cd2010-10-28 11:31:24 +0530539 }
540 }
Amit Arora0e512722010-10-01 12:24:16 +0530541}
Amit Aroraed3e5652010-10-27 12:02:53 +0530542
543char *debugfs_locate_mpoint(void)
544{
545 int ret;
546 FILE *filep;
547 char **path;
548 char fsname[64];
549 struct statfs sfs;
550
551 path = likely_mpoints;
552 while (*path) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530553 ret = statfs(*path, &sfs);
554 if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
Amit Aroraed3e5652010-10-27 12:02:53 +0530555 return *path;
556 path++;
557 }
558
559 filep = fopen("/proc/mounts", "r");
560 if (filep == NULL) {
561 fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
Amit Arora6e774cd2010-10-28 11:31:24 +0530562 exit(1);
563 }
Amit Aroraed3e5652010-10-27 12:02:53 +0530564
565 while (fscanf(filep, "%*s %s %s %*s %*d %*d\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600566 debugfs_mntpoint, fsname) == 2)
Amit Aroraed3e5652010-10-27 12:02:53 +0530567 if (!strcmp(fsname, "debugfs"))
568 break;
569 fclose(filep);
570
571 if (strcmp(fsname, "debugfs"))
572 return NULL;
573
574 return debugfs_mntpoint;
575}