blob: b5566447dd828217150235df18a9acc7a82dc7d8 [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 Lezcano9dc3fb32011-03-26 22:06:08 +010016#include <stdio.h>
17#include <mntent.h>
Daniel Lezcanoef323192011-03-26 22:06:20 +010018#include <sys/stat.h>
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010019
Amit Arora728e0c92010-09-14 12:06:09 +053020#include "powerdebug.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053021#include "clocks.h"
Amit Arora24ed7d12010-09-14 12:12:58 +053022
Daniel Lezcanoef323192011-03-26 22:06:20 +010023#define MAX_LINES 120
24
Amit Arora24ed7d12010-09-14 12:12:58 +053025static char clk_dir_path[PATH_MAX];
Amit Aroraac4e8652010-11-09 11:16:53 +053026static int bold[MAX_LINES];
Daniel Lezcanoef323192011-03-26 22:06:20 +010027static char clock_lines[MAX_LINES][128];
28static int clock_line_no;
29static int old_clock_line_no;
Amit Arora24ed7d12010-09-14 12:12:58 +053030
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010031static int locate_debugfs(char *clk_path)
32{
33 const char *mtab = "/proc/mounts";
34 struct mntent *mntent;
35 int ret = -1;
36 FILE *file = NULL;
37
38 file = setmntent(mtab, "r");
39 if (!file)
40 return -1;
41
42 while ((mntent = getmntent(file))) {
43
44 if (strcmp(mntent->mnt_type, "debugfs"))
45 continue;
46
47 strcpy(clk_path, mntent->mnt_dir);
48 ret = 0;
49 break;
50 }
51
52 fclose(file);
53 return ret;
54}
55
Daniel Lezcanof0e06652011-03-26 22:06:19 +010056int clock_init(void)
Amit Arora24ed7d12010-09-14 12:12:58 +053057{
Daniel Lezcanof0e06652011-03-26 22:06:19 +010058 if (locate_debugfs(clk_dir_path))
59 return -1;
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010060
Amit Arora6e774cd2010-10-28 11:31:24 +053061 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
Daniel Lezcanoc5a65bf2011-03-26 22:06:11 +010062
Daniel Lezcanof0e06652011-03-26 22:06:19 +010063 return access(clk_dir_path, F_OK);
Amit Arora24ed7d12010-09-14 12:12:58 +053064}
Amit Arora728e0c92010-09-14 12:06:09 +053065
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010066static int file_read_from_format(char *file, int *value, const char *format)
Amit Arora728e0c92010-09-14 12:06:09 +053067{
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010068 FILE *f;
Amit Arora6e774cd2010-10-28 11:31:24 +053069 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +053070
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010071 f = fopen(file, "r");
72 if (!f)
73 return -1;
74 ret = fscanf(f, format, value);
75 fclose(f);
Amit Arora728e0c92010-09-14 12:06:09 +053076
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010077 return !ret ? -1 : 0;
78}
Amit Arora728e0c92010-09-14 12:06:09 +053079
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010080static inline int file_read_int(char *file, int *value)
81{
82 return file_read_from_format(file, value, "%d");
83}
Amit Arora728e0c92010-09-14 12:06:09 +053084
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010085static inline int file_read_hex(char *file, int *value)
86{
87 return file_read_from_format(file, value, "%x");
Amit Arora728e0c92010-09-14 12:06:09 +053088}
89
Daniel Lezcanoc6718332011-03-23 14:37:39 +010090static void dump_parent(struct clock_info *clk, int line, bool dump)
91{
92 char *unit = "Hz";
93 double drate;
94 static char spaces[64];
95 char str[256];
96 static int maxline;
97
98 if (maxline < line)
99 maxline = line;
100
101 if (clk && clk->parent)
102 dump_parent(clk->parent, ++line, dump);
103
104 drate = (double)clk->rate;
105 if (drate > 1000 && drate < 1000000) {
106 unit = "KHz";
107 drate /= 1000;
108 }
109 if (drate > 1000000) {
110 unit = "MHz";
111 drate /= 1000000;
112 }
113 if (clk == clocks_info) {
114 line++;
115 strcpy(spaces, "");
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100116 sprintf(str, "%s%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100117 spaces, clk->name, clk->flags, clk->usecount, drate,
118 unit);
119 } else {
120 if (!(clk->parent == clocks_info))
121 strcat(spaces, " ");
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100122 sprintf(str, "%s`- %s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100123 spaces, clk->name, clk->flags, clk->usecount, drate,
124 unit);
125 }
126 if (dump)
127 //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str);
128 printf("%s", str);
129 else
130 print_one_clock(maxline - line + 2, str, 1, 0);
131}
132
Daniel Lezcanoef323192011-03-26 22:06:20 +0100133static struct clock_info *find_clock(struct clock_info *clk, char *clkarg)
134{
135 int i;
136 struct clock_info *ret = clk;
137
138 if (!strcmp(clk->name, clkarg))
139 return ret;
140
141 if (clk->children) {
142 for (i = 0; i < clk->num_children; i++) {
143 if (!strcmp(clk->children[i]->name, clkarg))
144 return clk->children[i];
145 }
146 for (i = 0; i < clk->num_children; i++) {
147 ret = find_clock(clk->children[i], clkarg);
148 if (ret)
149 return ret;
150 }
151 }
152
153 return NULL;
154}
155
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100156static void dump_all_parents(char *clkarg, bool dump)
157{
158 struct clock_info *clk;
159 char spaces[1024];
160
161 strcpy(spaces, "");
162
163 clk = find_clock(clocks_info, clkarg);
164
165 if (!clk)
166 printf("Clock NOT found!\n");
167 else {
168 /* while(clk && clk != clocks_info) { */
169 /* printf("%s\n", clk->name); */
170 /* strcat(spaces, " "); */
171 /* clk = clk->parent; */
172 /* printf("%s <-- ", spaces); */
173 /* } */
174 /* printf(" /\n"); */
175 dump_parent(clk, 1, dump);
176 }
177}
178
Daniel Lezcano897f7332011-03-26 22:06:06 +0100179void find_parents_for_clock(char *clkname, int complete)
Amit Arora3bd79162010-12-01 13:51:42 +0530180{
181 char name[256];
182
183 name[0] = '\0';
184 if (!complete) {
185 char str[256];
186
187 strcat(name, clkname);
188 sprintf(str, "Enter Clock Name : %s\n", name);
189 print_one_clock(2, str, 1, 0);
190 return;
191 }
192 sprintf(name, "Parents for \"%s\" Clock : \n", clkname);
193 print_one_clock(0, name, 1, 1);
Daniel Lezcano897f7332011-03-26 22:06:06 +0100194 dump_all_parents(clkname, false);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600195}
Amit Arora3bd79162010-12-01 13:51:42 +0530196
Daniel Lezcanoef323192011-03-26 22:06:20 +0100197static void destroy_clocks_info_recur(struct clock_info *clock)
198{
199 int i;
200
201 if (clock && clock->num_children) {
202 for (i = (clock->num_children - 1); i >= 0; i--) {
203 fflush(stdin);
204 destroy_clocks_info_recur(clock->children[i]);
205 if (!i) {
206 free(clock->children);
207 clock->children = NULL;
208 clock->num_children = 0;
209 }
210 }
211 }
212}
213
214static void destroy_clocks_info(void)
215{
216 int i;
217
218 if (!clocks_info)
219 return;
220
221 if (clocks_info->num_children) {
222 for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
223 destroy_clocks_info_recur(clocks_info->children[i]);
224 if (!i) {
225 free(clocks_info->children);
226 clocks_info->children = NULL;
227 }
228 }
229 }
230 clocks_info->num_children = 0;
231 free(clocks_info);
232 clocks_info = NULL;
233}
234
235
Amit Arora24ed7d12010-09-14 12:12:58 +0530236int read_and_print_clock_info(int verbose, int hrow, int selected)
237{
Amit Arora3bd79162010-12-01 13:51:42 +0530238 print_one_clock(0, "Reading Clock Tree ...", 1, 1);
Amit Aroraa06a7302010-12-02 15:59:37 +0530239
240 if (!old_clock_line_no || selected == REFRESH_WINDOW) {
241 destroy_clocks_info();
Amit Aroraac4e8652010-11-09 11:16:53 +0530242 read_clock_info(clk_dir_path);
Amit Aroraa06a7302010-12-02 15:59:37 +0530243 }
Amit Arora29cb7572010-10-05 17:40:29 +0530244
Amit Arora031263a2010-11-09 11:12:41 +0530245 if (!clocks_info->num_children) {
246 fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
247 exit(1);
Amit Arora6e774cd2010-10-28 11:31:24 +0530248 }
Amit Arora728e0c92010-09-14 12:06:09 +0530249
Amit Aroraa06a7302010-12-02 15:59:37 +0530250 if (selected == CLOCK_SELECTED)
251 selected = 1;
252 else
253 selected = 0;
254
Amit Aroraac4e8652010-11-09 11:16:53 +0530255 print_clock_info(verbose, hrow, selected);
Amit Aroraac4e8652010-11-09 11:16:53 +0530256 hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
257
258 return hrow;
259}
260
Daniel Lezcanoef323192011-03-26 22:06:20 +0100261static int calc_delta_screen_size(int hrow)
Amit Arora3bc8c922010-11-16 11:27:38 +0530262{
Amit Arora51d1b9c2010-11-30 13:55:15 +0530263 if (hrow >= (maxy - 3))
264 return hrow - (maxy - 4);
Amit Arora3bc8c922010-11-16 11:27:38 +0530265
266 return 0;
267}
268
Daniel Lezcanoef323192011-03-26 22:06:20 +0100269static void prepare_name_str(char *namestr, struct clock_info *clock)
Amit Aroraac4e8652010-11-09 11:16:53 +0530270{
271 int i;
272
273 strcpy(namestr, "");
274 if (clock->level > 1)
275 for (i = 0; i < (clock->level - 1); i++)
276 strcat(namestr, " ");
277 strcat(namestr, clock->name);
278}
279
Daniel Lezcanoef323192011-03-26 22:06:20 +0100280static void collapse_all_subclocks(struct clock_info *clock)
281{
282 int i;
283
284 clock->expanded = 0;
285 if (clock->num_children)
286 for (i = 0; i < clock->num_children; i++)
287 collapse_all_subclocks(clock->children[i]);
288}
289
290static void add_clock_details_recur(struct clock_info *clock,
291 int hrow, int selected)
Amit Arora031263a2010-11-09 11:12:41 +0530292{
293 int i;
294 char *unit = " Hz";
295 char rate_str[64];
Amit Aroraac4e8652010-11-09 11:16:53 +0530296 char name_str[256];
Amit Arora031263a2010-11-09 11:12:41 +0530297 double drate = (double)clock->rate;
298
299 if (drate > 1000 && drate < 1000000) {
300 unit = "KHz";
301 drate /= 1000;
302 }
303 if (drate > 1000000) {
304 unit = "MHz";
305 drate /= 1000000;
306 }
307 if (clock->usecount)
308 bold[clock_line_no] = 1;
309 else
310 bold[clock_line_no] = 0;
311
312 sprintf(rate_str, "%.2f %s", drate, unit);
Amit Aroraac4e8652010-11-09 11:16:53 +0530313 prepare_name_str(name_str, clock);
314 sprintf(clock_lines[clock_line_no++], "%-55s %-4d %-12s %-12d %-12d",
315 name_str, clock->flags, rate_str, clock->usecount,
316 clock->num_children);
317
318 if (selected && (hrow == (clock_line_no - 1))) {
319 if (clock->expanded)
320 collapse_all_subclocks(clock);
321 else
322 clock->expanded = 1;
323 selected = 0;
324 }
Amit Arora031263a2010-11-09 11:12:41 +0530325
326 if (clock->expanded && clock->num_children)
327 for (i = 0; i < clock->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530328 add_clock_details_recur(clock->children[i],
329 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530330 strcpy(clock_lines[clock_line_no], "");
331}
332
Daniel Lezcanoef323192011-03-26 22:06:20 +0100333void print_clock_info(int verbose, int hrow, int selected)
Amit Aroraac4e8652010-11-09 11:16:53 +0530334{
Daniel Lezcanoef323192011-03-26 22:06:20 +0100335 int i, count = 0, delta;
Amit Aroraac4e8652010-11-09 11:16:53 +0530336
Daniel Lezcanoef323192011-03-26 22:06:20 +0100337 (void)verbose;
Amit Aroraac4e8652010-11-09 11:16:53 +0530338
Daniel Lezcanoef323192011-03-26 22:06:20 +0100339 print_clock_header();
Amit Arora031263a2010-11-09 11:12:41 +0530340
Daniel Lezcanoef323192011-03-26 22:06:20 +0100341 for (i = 0; i < clocks_info->num_children; i++)
342 add_clock_details_recur(clocks_info->children[i],
343 hrow, selected);
Amit Aroraa06a7302010-12-02 15:59:37 +0530344
Daniel Lezcanoef323192011-03-26 22:06:20 +0100345 delta = calc_delta_screen_size(hrow);
346
347 while (clock_lines[count + delta] &&
348 strcmp(clock_lines[count + delta], "")) {
349 if (count < delta) {
350 count++;
351 continue;
Amit Arora031263a2010-11-09 11:12:41 +0530352 }
Daniel Lezcanoef323192011-03-26 22:06:20 +0100353 print_one_clock(count - delta, clock_lines[count + delta],
354 bold[count + delta], (hrow == (count + delta)));
355 count++;
Amit Arora031263a2010-11-09 11:12:41 +0530356 }
Amit Arora031263a2010-11-09 11:12:41 +0530357
Daniel Lezcanoef323192011-03-26 22:06:20 +0100358 old_clock_line_no = clock_line_no;
359 clock_line_no = 0;
Amit Arora728e0c92010-09-14 12:06:09 +0530360}
Amit Arora0e512722010-10-01 12:24:16 +0530361
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100362void read_and_dump_clock_info_one(char *clk, bool dump)
Amit Aroraf4fb8102010-11-30 13:55:50 +0530363{
Amit Arora3bd79162010-12-01 13:51:42 +0530364 printf("\nParents for \"%s\" Clock :\n\n", clk);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530365 read_clock_info(clk_dir_path);
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100366 dump_all_parents(clk, dump);
Amit Arora422c52f2010-12-02 16:22:29 +0530367 printf("\n\n");
Amit Aroraf4fb8102010-11-30 13:55:50 +0530368}
369
Amit Aroraac4e8652010-11-09 11:16:53 +0530370void read_and_dump_clock_info(int verbose)
Amit Arora0e512722010-10-01 12:24:16 +0530371{
Amit Arora6e774cd2010-10-28 11:31:24 +0530372 (void)verbose;
Amit Arora422c52f2010-12-02 16:22:29 +0530373 printf("\nClock Tree :\n");
Amit Arora6e774cd2010-10-28 11:31:24 +0530374 printf("**********\n");
375 read_clock_info(clk_dir_path);
Amit Aroraac4e8652010-11-09 11:16:53 +0530376 dump_clock_info(clocks_info, 1, 1);
Amit Arora422c52f2010-12-02 16:22:29 +0530377 printf("\n\n");
Amit Arora0e512722010-10-01 12:24:16 +0530378}
379
Amit Aroraeb6cba92010-10-25 16:03:21 +0530380void read_clock_info(char *clkpath)
Amit Arora0e512722010-10-01 12:24:16 +0530381{
Amit Arora6e774cd2010-10-28 11:31:24 +0530382 DIR *dir;
383 struct dirent *item;
384 char filename[NAME_MAX], clockname[NAME_MAX];
385 struct clock_info *child;
386 struct clock_info *cur;
Amit Arora0e512722010-10-01 12:24:16 +0530387
Amit Arora6e774cd2010-10-28 11:31:24 +0530388 dir = opendir(clkpath);
389 if (!dir)
390 return;
Amit Arora0e512722010-10-01 12:24:16 +0530391
Amit Arora6e774cd2010-10-28 11:31:24 +0530392 clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
393 memset(clocks_info, 0, sizeof(clocks_info));
394 strcpy(clocks_info->name, "/");
Amit Aroraac4e8652010-11-09 11:16:53 +0530395 clocks_info->level = 0;
Amit Arora0e512722010-10-01 12:24:16 +0530396
Amit Arora6e774cd2010-10-28 11:31:24 +0530397 while ((item = readdir(dir))) {
398 /* skip hidden dirs except ".." */
399 if (item->d_name[0] == '.')
400 continue;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530401
Amit Arora6e774cd2010-10-28 11:31:24 +0530402 strcpy(clockname, item->d_name);
403 sprintf(filename, "%s/%s", clkpath, item->d_name);
404 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
Amit Aroraac4e8652010-11-09 11:16:53 +0530405 memset(cur, 0, sizeof(struct clock_info));
Amit Arora6e774cd2010-10-28 11:31:24 +0530406 strcpy(cur->name, clockname);
407 cur->parent = clocks_info;
Amit Arora031263a2010-11-09 11:12:41 +0530408 cur->num_children = 0;
409 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530410 cur->level = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530411 insert_children(&clocks_info, cur);
412 child = read_clock_info_recur(filename, 2, cur);
413 }
414 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530415}
416
417struct clock_info *read_clock_info_recur(char *clkpath, int level,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600418 struct clock_info *parent)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530419{
Amit Arora6e774cd2010-10-28 11:31:24 +0530420 int ret = 0;
421 DIR *dir;
422 char filename[PATH_MAX];
423 struct dirent *item;
424 struct clock_info *cur = NULL;
425 struct stat buf;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530426
Amit Arora6e774cd2010-10-28 11:31:24 +0530427 dir = opendir(clkpath);
428 if (!dir)
429 return NULL;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530430
Amit Arora6e774cd2010-10-28 11:31:24 +0530431 while ((item = readdir(dir))) {
432 struct clock_info *child;
433 /* skip hidden dirs except ".." */
434 if (item->d_name[0] == '.' )
435 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530436
Amit Arora6e774cd2010-10-28 11:31:24 +0530437 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530438
Amit Arora6e774cd2010-10-28 11:31:24 +0530439 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530440
Amit Arora6e774cd2010-10-28 11:31:24 +0530441 if (ret < 0) {
442 printf("Error doing a stat on %s\n", filename);
443 exit(1);
444 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530445
Amit Arora6e774cd2010-10-28 11:31:24 +0530446 if (S_ISREG(buf.st_mode)) {
447 if (!strcmp(item->d_name, "flags"))
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100448 file_read_hex(filename, &parent->flags);
Amit Arora6e774cd2010-10-28 11:31:24 +0530449 if (!strcmp(item->d_name, "rate"))
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100450 file_read_int(filename, &parent->rate);
Amit Arora6e774cd2010-10-28 11:31:24 +0530451 if (!strcmp(item->d_name, "usecount"))
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100452 file_read_int(filename, &parent->usecount);
Amit Arora6e774cd2010-10-28 11:31:24 +0530453 continue;
454 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530455
Amit Arora6e774cd2010-10-28 11:31:24 +0530456 if (!S_ISDIR(buf.st_mode))
457 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530458
Amit Arora6e774cd2010-10-28 11:31:24 +0530459 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
460 memset(cur, 0, sizeof(cur));
461 strcpy(cur->name, item->d_name);
462 cur->children = NULL;
463 cur->parent = NULL;
464 cur->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530465 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530466 cur->level = level;
Amit Arora6e774cd2010-10-28 11:31:24 +0530467 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora6e774cd2010-10-28 11:31:24 +0530468 insert_children(&parent, cur);
469 cur->parent = parent;
470 }
471 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530472
Amit Arora6e774cd2010-10-28 11:31:24 +0530473 return cur;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530474}
475
476void insert_children(struct clock_info **parent, struct clock_info *clk)
477{
Amit Arora031263a2010-11-09 11:12:41 +0530478 if (!(*parent)->num_children || (*parent)->children == NULL) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530479 (*parent)->children = (struct clock_info **)
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600480 malloc(sizeof(struct clock_info *)*2);
Amit Arora6e774cd2010-10-28 11:31:24 +0530481 (*parent)->num_children = 0;
482 } else
483 (*parent)->children = (struct clock_info **)
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600484 realloc((*parent)->children,
485 sizeof(struct clock_info *) *
486 ((*parent)->num_children + 2));
Amit Arora6e774cd2010-10-28 11:31:24 +0530487 if ((*parent)->num_children > 0)
488 (*parent)->children[(*parent)->num_children - 1]->last_child
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600489 = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530490 clk->last_child = 1;
491 (*parent)->children[(*parent)->num_children] = clk;
492 (*parent)->children[(*parent)->num_children + 1] = NULL;
493 (*parent)->num_children++;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530494}
495
Amit Aroraac4e8652010-11-09 11:16:53 +0530496void dump_clock_info(struct clock_info *clk, int level, int bmp)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530497{
Amit Arora6e774cd2010-10-28 11:31:24 +0530498 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530499
Amit Arora6e774cd2010-10-28 11:31:24 +0530500 if (!clk)
501 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530502
Amit Arora6e774cd2010-10-28 11:31:24 +0530503 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
504 if (i == (level - 1)) {
505 if (clk->last_child)
506 printf("`-- ");
507 else
508 printf("|-- ");
509 } else {
510 if ((1<<j) & bmp)
511 printf("| ");
512 else
513 printf(" ");
514 }
515 }
Amit Arora031263a2010-11-09 11:12:41 +0530516
Amit Arora6e774cd2010-10-28 11:31:24 +0530517 if (clk == clocks_info)
518 printf("%s\n", clk->name);
519 else {
520 char *unit = "Hz";
521 double drate = (double)clk->rate;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600522
Amit Arora6e774cd2010-10-28 11:31:24 +0530523 if (drate > 1000 && drate < 1000000) {
524 unit = "KHz";
525 drate /= 1000;
526 }
527 if (drate > 1000000) {
528 unit = "MHz";
529 drate /= 1000000;
530 }
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100531 printf("%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Amit Arora6e774cd2010-10-28 11:31:24 +0530532 clk->name, clk->flags, clk->usecount, drate, unit);
Amit Arora6e774cd2010-10-28 11:31:24 +0530533 }
534 if (clk->children) {
535 int tbmp = bmp;
536 int xbmp = -1;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530537
Amit Arora6e774cd2010-10-28 11:31:24 +0530538 if (clk->last_child) {
539 xbmp ^= 1 << (level - 2);
540
541 xbmp = tbmp & xbmp;
542 } else
543 xbmp = bmp;
544 for (i = 0; i < clk->num_children; i++) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530545 tbmp = xbmp | (1 << level);
Amit Aroraac4e8652010-11-09 11:16:53 +0530546 dump_clock_info(clk->children[i], level + 1, tbmp);
Amit Arora6e774cd2010-10-28 11:31:24 +0530547 }
548 }
Amit Arora0e512722010-10-01 12:24:16 +0530549}