blob: 848c52e73114933dea84bb1a625fbc82169f74bb [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
Daniel Lezcano141c0482011-06-08 23:30:00 +020016#ifndef _GNU_SOURCE
17#define _GNU_SOURCE
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010018#include <stdio.h>
Daniel Lezcano141c0482011-06-08 23:30:00 +020019#undef _GNU_SOURCE
20#endif
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010021#include <mntent.h>
Daniel Lezcanoef323192011-03-26 22:06:20 +010022#include <sys/stat.h>
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010023
Amit Arora728e0c92010-09-14 12:06:09 +053024#include "powerdebug.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053025#include "clocks.h"
Daniel Lezcanoc193b602011-06-08 23:30:00 +020026#include "tree.h"
Amit Arora24ed7d12010-09-14 12:12:58 +053027
Daniel Lezcanoef323192011-03-26 22:06:20 +010028#define MAX_LINES 120
29
Amit Arora24ed7d12010-09-14 12:12:58 +053030static char clk_dir_path[PATH_MAX];
Amit Aroraac4e8652010-11-09 11:16:53 +053031static int bold[MAX_LINES];
Daniel Lezcanoef323192011-03-26 22:06:20 +010032static char clock_lines[MAX_LINES][128];
33static int clock_line_no;
34static int old_clock_line_no;
Amit Arora24ed7d12010-09-14 12:12:58 +053035
Daniel Lezcanoc45662b2011-06-08 23:30:00 +020036struct clock_info {
37 char name[NAME_MAX];
38 int flags;
39 int rate;
40 int usecount;
41 int num_children;
42 int last_child;
43 int expanded;
44 int level;
45 struct clock_info *parent;
46 struct clock_info **children;
47} *clocks_info;
48
Daniel Lezcanoc193b602011-06-08 23:30:00 +020049static struct tree *clock_tree;
50
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010051static int locate_debugfs(char *clk_path)
52{
53 const char *mtab = "/proc/mounts";
54 struct mntent *mntent;
55 int ret = -1;
56 FILE *file = NULL;
57
58 file = setmntent(mtab, "r");
59 if (!file)
60 return -1;
61
62 while ((mntent = getmntent(file))) {
63
64 if (strcmp(mntent->mnt_type, "debugfs"))
65 continue;
66
67 strcpy(clk_path, mntent->mnt_dir);
68 ret = 0;
69 break;
70 }
71
72 fclose(file);
73 return ret;
74}
75
Daniel Lezcanof0e06652011-03-26 22:06:19 +010076int clock_init(void)
Amit Arora24ed7d12010-09-14 12:12:58 +053077{
Daniel Lezcanof0e06652011-03-26 22:06:19 +010078 if (locate_debugfs(clk_dir_path))
79 return -1;
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010080
Amit Arora6e774cd2010-10-28 11:31:24 +053081 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
Daniel Lezcanoc5a65bf2011-03-26 22:06:11 +010082
Daniel Lezcanoc193b602011-06-08 23:30:00 +020083 clock_tree = tree_load(clk_dir_path, NULL);
84 if (!clock_tree)
85 return -1;
86
Daniel Lezcanof0e06652011-03-26 22:06:19 +010087 return access(clk_dir_path, F_OK);
Amit Arora24ed7d12010-09-14 12:12:58 +053088}
Amit Arora728e0c92010-09-14 12:06:09 +053089
Daniel Lezcano141c0482011-06-08 23:30:00 +020090/*
91 * This functions is a helper to read a specific file content and store
92 * the content inside a variable pointer passed as parameter, the format
93 * parameter gives the variable type to be read from the file.
94 *
95 * @path : directory path containing the file
96 * @name : name of the file to be read
97 * @format : the format of the format
98 * @value : a pointer to a variable to store the content of the file
99 * Returns 0 on success, -1 otherwise
100 */
101int file_read_value(const char *path, const char *name,
102 const char *format, void *value)
103{
104 FILE *file;
105 char *rpath;
106 int ret;
107
108 ret = asprintf(&rpath, "%s/%s", path, name);
109 if (ret < 0)
110 return ret;
111
112 file = fopen(rpath, "r");
113 if (!file) {
114 ret = -1;
115 goto out_free;
116 }
117
118 ret = fscanf(file, format, value) == EOF ? -1 : 0;
119
120 fclose(file);
121out_free:
122 free(rpath);
123 return ret;
124}
125
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100126static int file_read_from_format(char *file, int *value, const char *format)
Amit Arora728e0c92010-09-14 12:06:09 +0530127{
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100128 FILE *f;
Amit Arora6e774cd2010-10-28 11:31:24 +0530129 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +0530130
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100131 f = fopen(file, "r");
132 if (!f)
133 return -1;
134 ret = fscanf(f, format, value);
135 fclose(f);
Amit Arora728e0c92010-09-14 12:06:09 +0530136
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100137 return !ret ? -1 : 0;
138}
Amit Arora728e0c92010-09-14 12:06:09 +0530139
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100140static inline int file_read_int(char *file, int *value)
141{
142 return file_read_from_format(file, value, "%d");
143}
Amit Arora728e0c92010-09-14 12:06:09 +0530144
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100145static inline int file_read_hex(char *file, int *value)
146{
147 return file_read_from_format(file, value, "%x");
Amit Arora728e0c92010-09-14 12:06:09 +0530148}
149
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100150static void dump_parent(struct clock_info *clk, int line, bool dump)
151{
152 char *unit = "Hz";
153 double drate;
154 static char spaces[64];
155 char str[256];
156 static int maxline;
157
158 if (maxline < line)
159 maxline = line;
160
161 if (clk && clk->parent)
162 dump_parent(clk->parent, ++line, dump);
163
164 drate = (double)clk->rate;
165 if (drate > 1000 && drate < 1000000) {
166 unit = "KHz";
167 drate /= 1000;
168 }
169 if (drate > 1000000) {
170 unit = "MHz";
171 drate /= 1000000;
172 }
173 if (clk == clocks_info) {
174 line++;
175 strcpy(spaces, "");
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100176 sprintf(str, "%s%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100177 spaces, clk->name, clk->flags, clk->usecount, drate,
178 unit);
179 } else {
180 if (!(clk->parent == clocks_info))
181 strcat(spaces, " ");
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100182 sprintf(str, "%s`- %s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100183 spaces, clk->name, clk->flags, clk->usecount, drate,
184 unit);
185 }
186 if (dump)
187 //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str);
188 printf("%s", str);
189 else
190 print_one_clock(maxline - line + 2, str, 1, 0);
191}
192
Daniel Lezcanoef323192011-03-26 22:06:20 +0100193static struct clock_info *find_clock(struct clock_info *clk, char *clkarg)
194{
195 int i;
196 struct clock_info *ret = clk;
197
198 if (!strcmp(clk->name, clkarg))
199 return ret;
200
201 if (clk->children) {
202 for (i = 0; i < clk->num_children; i++) {
203 if (!strcmp(clk->children[i]->name, clkarg))
204 return clk->children[i];
205 }
206 for (i = 0; i < clk->num_children; i++) {
207 ret = find_clock(clk->children[i], clkarg);
208 if (ret)
209 return ret;
210 }
211 }
212
213 return NULL;
214}
215
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100216static void dump_all_parents(char *clkarg, bool dump)
217{
218 struct clock_info *clk;
219 char spaces[1024];
220
221 strcpy(spaces, "");
222
223 clk = find_clock(clocks_info, clkarg);
224
225 if (!clk)
226 printf("Clock NOT found!\n");
227 else {
228 /* while(clk && clk != clocks_info) { */
229 /* printf("%s\n", clk->name); */
230 /* strcat(spaces, " "); */
231 /* clk = clk->parent; */
232 /* printf("%s <-- ", spaces); */
233 /* } */
234 /* printf(" /\n"); */
235 dump_parent(clk, 1, dump);
236 }
237}
238
Daniel Lezcano897f7332011-03-26 22:06:06 +0100239void find_parents_for_clock(char *clkname, int complete)
Amit Arora3bd79162010-12-01 13:51:42 +0530240{
241 char name[256];
242
243 name[0] = '\0';
244 if (!complete) {
245 char str[256];
246
247 strcat(name, clkname);
248 sprintf(str, "Enter Clock Name : %s\n", name);
249 print_one_clock(2, str, 1, 0);
250 return;
251 }
252 sprintf(name, "Parents for \"%s\" Clock : \n", clkname);
253 print_one_clock(0, name, 1, 1);
Daniel Lezcano897f7332011-03-26 22:06:06 +0100254 dump_all_parents(clkname, false);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600255}
Amit Arora3bd79162010-12-01 13:51:42 +0530256
Daniel Lezcanoef323192011-03-26 22:06:20 +0100257static void destroy_clocks_info_recur(struct clock_info *clock)
258{
259 int i;
260
261 if (clock && clock->num_children) {
262 for (i = (clock->num_children - 1); i >= 0; i--) {
263 fflush(stdin);
264 destroy_clocks_info_recur(clock->children[i]);
265 if (!i) {
266 free(clock->children);
267 clock->children = NULL;
268 clock->num_children = 0;
269 }
270 }
271 }
272}
273
274static void destroy_clocks_info(void)
275{
276 int i;
277
278 if (!clocks_info)
279 return;
280
281 if (clocks_info->num_children) {
282 for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
283 destroy_clocks_info_recur(clocks_info->children[i]);
284 if (!i) {
285 free(clocks_info->children);
286 clocks_info->children = NULL;
287 }
288 }
289 }
290 clocks_info->num_children = 0;
291 free(clocks_info);
292 clocks_info = NULL;
293}
294
295
Amit Arora24ed7d12010-09-14 12:12:58 +0530296int read_and_print_clock_info(int verbose, int hrow, int selected)
297{
Amit Arora3bd79162010-12-01 13:51:42 +0530298 print_one_clock(0, "Reading Clock Tree ...", 1, 1);
Amit Aroraa06a7302010-12-02 15:59:37 +0530299
300 if (!old_clock_line_no || selected == REFRESH_WINDOW) {
301 destroy_clocks_info();
Amit Aroraac4e8652010-11-09 11:16:53 +0530302 read_clock_info(clk_dir_path);
Amit Aroraa06a7302010-12-02 15:59:37 +0530303 }
Amit Arora29cb7572010-10-05 17:40:29 +0530304
Daniel Lezcano95b0dac2011-06-08 23:30:00 +0200305 if (!clocks_info || !clocks_info->num_children) {
Amit Arora031263a2010-11-09 11:12:41 +0530306 fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
307 exit(1);
Amit Arora6e774cd2010-10-28 11:31:24 +0530308 }
Amit Arora728e0c92010-09-14 12:06:09 +0530309
Amit Aroraa06a7302010-12-02 15:59:37 +0530310 if (selected == CLOCK_SELECTED)
311 selected = 1;
312 else
313 selected = 0;
314
Amit Aroraac4e8652010-11-09 11:16:53 +0530315 print_clock_info(verbose, hrow, selected);
Amit Aroraac4e8652010-11-09 11:16:53 +0530316 hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
317
318 return hrow;
319}
320
Daniel Lezcanoef323192011-03-26 22:06:20 +0100321static int calc_delta_screen_size(int hrow)
Amit Arora3bc8c922010-11-16 11:27:38 +0530322{
Amit Arora51d1b9c2010-11-30 13:55:15 +0530323 if (hrow >= (maxy - 3))
324 return hrow - (maxy - 4);
Amit Arora3bc8c922010-11-16 11:27:38 +0530325
326 return 0;
327}
328
Daniel Lezcanoef323192011-03-26 22:06:20 +0100329static void prepare_name_str(char *namestr, struct clock_info *clock)
Amit Aroraac4e8652010-11-09 11:16:53 +0530330{
331 int i;
332
333 strcpy(namestr, "");
334 if (clock->level > 1)
335 for (i = 0; i < (clock->level - 1); i++)
336 strcat(namestr, " ");
337 strcat(namestr, clock->name);
338}
339
Daniel Lezcanoef323192011-03-26 22:06:20 +0100340static void collapse_all_subclocks(struct clock_info *clock)
341{
342 int i;
343
344 clock->expanded = 0;
345 if (clock->num_children)
346 for (i = 0; i < clock->num_children; i++)
347 collapse_all_subclocks(clock->children[i]);
348}
349
350static void add_clock_details_recur(struct clock_info *clock,
351 int hrow, int selected)
Amit Arora031263a2010-11-09 11:12:41 +0530352{
353 int i;
354 char *unit = " Hz";
355 char rate_str[64];
Amit Aroraac4e8652010-11-09 11:16:53 +0530356 char name_str[256];
Amit Arora031263a2010-11-09 11:12:41 +0530357 double drate = (double)clock->rate;
358
359 if (drate > 1000 && drate < 1000000) {
360 unit = "KHz";
361 drate /= 1000;
362 }
363 if (drate > 1000000) {
364 unit = "MHz";
365 drate /= 1000000;
366 }
367 if (clock->usecount)
368 bold[clock_line_no] = 1;
369 else
370 bold[clock_line_no] = 0;
371
372 sprintf(rate_str, "%.2f %s", drate, unit);
Amit Aroraac4e8652010-11-09 11:16:53 +0530373 prepare_name_str(name_str, clock);
Daniel Lezcano4b669072011-05-24 15:27:49 +0200374 sprintf(clock_lines[clock_line_no++], "%-55s 0x%-4x %-12s %-12d %-12d",
Amit Aroraac4e8652010-11-09 11:16:53 +0530375 name_str, clock->flags, rate_str, clock->usecount,
376 clock->num_children);
377
378 if (selected && (hrow == (clock_line_no - 1))) {
379 if (clock->expanded)
380 collapse_all_subclocks(clock);
381 else
382 clock->expanded = 1;
383 selected = 0;
384 }
Amit Arora031263a2010-11-09 11:12:41 +0530385
386 if (clock->expanded && clock->num_children)
387 for (i = 0; i < clock->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530388 add_clock_details_recur(clock->children[i],
389 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530390 strcpy(clock_lines[clock_line_no], "");
391}
392
Daniel Lezcanoef323192011-03-26 22:06:20 +0100393void print_clock_info(int verbose, int hrow, int selected)
Amit Aroraac4e8652010-11-09 11:16:53 +0530394{
Daniel Lezcanoef323192011-03-26 22:06:20 +0100395 int i, count = 0, delta;
Amit Aroraac4e8652010-11-09 11:16:53 +0530396
Daniel Lezcanoef323192011-03-26 22:06:20 +0100397 (void)verbose;
Amit Aroraac4e8652010-11-09 11:16:53 +0530398
Daniel Lezcanoef323192011-03-26 22:06:20 +0100399 print_clock_header();
Amit Arora031263a2010-11-09 11:12:41 +0530400
Daniel Lezcanoef323192011-03-26 22:06:20 +0100401 for (i = 0; i < clocks_info->num_children; i++)
402 add_clock_details_recur(clocks_info->children[i],
403 hrow, selected);
Amit Aroraa06a7302010-12-02 15:59:37 +0530404
Daniel Lezcanoef323192011-03-26 22:06:20 +0100405 delta = calc_delta_screen_size(hrow);
406
407 while (clock_lines[count + delta] &&
408 strcmp(clock_lines[count + delta], "")) {
409 if (count < delta) {
410 count++;
411 continue;
Amit Arora031263a2010-11-09 11:12:41 +0530412 }
Daniel Lezcanoef323192011-03-26 22:06:20 +0100413 print_one_clock(count - delta, clock_lines[count + delta],
414 bold[count + delta], (hrow == (count + delta)));
415 count++;
Amit Arora031263a2010-11-09 11:12:41 +0530416 }
Amit Arora031263a2010-11-09 11:12:41 +0530417
Daniel Lezcanoef323192011-03-26 22:06:20 +0100418 old_clock_line_no = clock_line_no;
419 clock_line_no = 0;
Amit Arora728e0c92010-09-14 12:06:09 +0530420}
Amit Arora0e512722010-10-01 12:24:16 +0530421
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200422static void insert_children(struct clock_info **parent, struct clock_info *clk)
Amit Aroraf4fb8102010-11-30 13:55:50 +0530423{
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200424 if (!(*parent)->num_children || (*parent)->children == NULL) {
425 (*parent)->children = (struct clock_info **)
426 malloc(sizeof(struct clock_info *)*2);
427 (*parent)->num_children = 0;
428 } else
429 (*parent)->children = (struct clock_info **)
430 realloc((*parent)->children,
431 sizeof(struct clock_info *) *
432 ((*parent)->num_children + 2));
433 if ((*parent)->num_children > 0)
434 (*parent)->children[(*parent)->num_children - 1]->last_child
435 = 0;
436 clk->last_child = 1;
437 (*parent)->children[(*parent)->num_children] = clk;
438 (*parent)->children[(*parent)->num_children + 1] = NULL;
439 (*parent)->num_children++;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530440}
441
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200442static struct clock_info *read_clock_info_recur(char *clkpath, int level,
443 struct clock_info *parent)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530444{
Amit Arora6e774cd2010-10-28 11:31:24 +0530445 int ret = 0;
446 DIR *dir;
447 char filename[PATH_MAX];
448 struct dirent *item;
449 struct clock_info *cur = NULL;
450 struct stat buf;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530451
Amit Arora6e774cd2010-10-28 11:31:24 +0530452 dir = opendir(clkpath);
453 if (!dir)
454 return NULL;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530455
Amit Arora6e774cd2010-10-28 11:31:24 +0530456 while ((item = readdir(dir))) {
457 struct clock_info *child;
458 /* skip hidden dirs except ".." */
459 if (item->d_name[0] == '.' )
460 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530461
Amit Arora6e774cd2010-10-28 11:31:24 +0530462 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530463
Amit Arora6e774cd2010-10-28 11:31:24 +0530464 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530465
Amit Arora6e774cd2010-10-28 11:31:24 +0530466 if (ret < 0) {
467 printf("Error doing a stat on %s\n", filename);
468 exit(1);
469 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530470
Amit Arora6e774cd2010-10-28 11:31:24 +0530471 if (S_ISREG(buf.st_mode)) {
472 if (!strcmp(item->d_name, "flags"))
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100473 file_read_hex(filename, &parent->flags);
Amit Arora6e774cd2010-10-28 11:31:24 +0530474 if (!strcmp(item->d_name, "rate"))
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100475 file_read_int(filename, &parent->rate);
Amit Arora6e774cd2010-10-28 11:31:24 +0530476 if (!strcmp(item->d_name, "usecount"))
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100477 file_read_int(filename, &parent->usecount);
Amit Arora6e774cd2010-10-28 11:31:24 +0530478 continue;
479 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530480
Amit Arora6e774cd2010-10-28 11:31:24 +0530481 if (!S_ISDIR(buf.st_mode))
482 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530483
Amit Arora6e774cd2010-10-28 11:31:24 +0530484 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
485 memset(cur, 0, sizeof(cur));
486 strcpy(cur->name, item->d_name);
487 cur->children = NULL;
488 cur->parent = NULL;
489 cur->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530490 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530491 cur->level = level;
Amit Arora6e774cd2010-10-28 11:31:24 +0530492 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora6e774cd2010-10-28 11:31:24 +0530493 insert_children(&parent, cur);
494 cur->parent = parent;
495 }
496 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530497
Amit Arora6e774cd2010-10-28 11:31:24 +0530498 return cur;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530499}
500
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200501static struct clock_info *clock_alloc(const char *name)
502{
503 struct clock_info *ci;
504
505 ci = malloc(sizeof(*ci));
506 if (ci) {
507 memset(ci, 0, sizeof(*ci));
508 strcpy(ci->name, name);
509 }
510
511 return ci;
512}
513
514int read_clock_info(char *clkpath)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530515{
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200516 DIR *dir;
517 struct dirent *item;
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200518 char filename[NAME_MAX];
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200519 struct clock_info *child;
520 struct clock_info *cur;
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200521 int ret = -1;
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200522
523 dir = opendir(clkpath);
524 if (!dir)
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200525 return -1;
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200526
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200527 clocks_info = clock_alloc("/");
528 if (!clocks_info)
529 return -1;
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200530
531 while ((item = readdir(dir))) {
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200532
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200533 /* skip hidden dirs except ".." */
534 if (item->d_name[0] == '.')
535 continue;
536
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200537 sprintf(filename, "%s/%s", clkpath, item->d_name);
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200538
539 cur = clock_alloc(item->d_name);
540 if (!cur)
541 goto out;
542
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200543 cur->parent = clocks_info;
544 cur->num_children = 0;
545 cur->expanded = 0;
546 cur->level = 1;
547 insert_children(&clocks_info, cur);
548 child = read_clock_info_recur(filename, 2, cur);
549 }
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200550
551 ret = 0;
552
553out:
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200554 closedir(dir);
Daniel Lezcanob2565a82011-06-08 23:30:00 +0200555
556 return ret;
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200557}
558
559void read_and_dump_clock_info_one(char *clk, bool dump)
560{
561 printf("\nParents for \"%s\" Clock :\n\n", clk);
562 read_clock_info(clk_dir_path);
563 dump_all_parents(clk, dump);
564 printf("\n\n");
Amit Aroraeb6cba92010-10-25 16:03:21 +0530565}
566
Amit Aroraac4e8652010-11-09 11:16:53 +0530567void dump_clock_info(struct clock_info *clk, int level, int bmp)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530568{
Amit Arora6e774cd2010-10-28 11:31:24 +0530569 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530570
Amit Arora6e774cd2010-10-28 11:31:24 +0530571 if (!clk)
572 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530573
Amit Arora6e774cd2010-10-28 11:31:24 +0530574 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
575 if (i == (level - 1)) {
576 if (clk->last_child)
577 printf("`-- ");
578 else
579 printf("|-- ");
580 } else {
581 if ((1<<j) & bmp)
582 printf("| ");
583 else
584 printf(" ");
585 }
586 }
Amit Arora031263a2010-11-09 11:12:41 +0530587
Amit Arora6e774cd2010-10-28 11:31:24 +0530588 if (clk == clocks_info)
589 printf("%s\n", clk->name);
590 else {
591 char *unit = "Hz";
592 double drate = (double)clk->rate;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600593
Amit Arora6e774cd2010-10-28 11:31:24 +0530594 if (drate > 1000 && drate < 1000000) {
595 unit = "KHz";
596 drate /= 1000;
597 }
598 if (drate > 1000000) {
599 unit = "MHz";
600 drate /= 1000000;
601 }
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100602 printf("%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Amit Arora6e774cd2010-10-28 11:31:24 +0530603 clk->name, clk->flags, clk->usecount, drate, unit);
Amit Arora6e774cd2010-10-28 11:31:24 +0530604 }
605 if (clk->children) {
606 int tbmp = bmp;
607 int xbmp = -1;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530608
Amit Arora6e774cd2010-10-28 11:31:24 +0530609 if (clk->last_child) {
610 xbmp ^= 1 << (level - 2);
611
612 xbmp = tbmp & xbmp;
613 } else
614 xbmp = bmp;
615 for (i = 0; i < clk->num_children; i++) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530616 tbmp = xbmp | (1 << level);
Amit Aroraac4e8652010-11-09 11:16:53 +0530617 dump_clock_info(clk->children[i], level + 1, tbmp);
Amit Arora6e774cd2010-10-28 11:31:24 +0530618 }
619 }
Amit Arora0e512722010-10-01 12:24:16 +0530620}
Daniel Lezcano28b53cd2011-06-08 23:30:00 +0200621
622void read_and_dump_clock_info(int verbose)
623{
624 (void)verbose;
625 printf("\nClock Tree :\n");
626 printf("**********\n");
627 read_clock_info(clk_dir_path);
628 dump_clock_info(clocks_info, 1, 1);
629 printf("\n\n");
630}