blob: 9577267a82347186e4f040a5d3617ccfa4913ddd [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>
18
Amit Arora728e0c92010-09-14 12:06:09 +053019#include "powerdebug.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053020#include "clocks.h"
Amit Arora24ed7d12010-09-14 12:12:58 +053021
Amit Arora24ed7d12010-09-14 12:12:58 +053022static char clk_dir_path[PATH_MAX];
Amit Aroraac4e8652010-11-09 11:16:53 +053023static int bold[MAX_LINES];
Amit Arora24ed7d12010-09-14 12:12:58 +053024
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010025static int locate_debugfs(char *clk_path)
26{
27 const char *mtab = "/proc/mounts";
28 struct mntent *mntent;
29 int ret = -1;
30 FILE *file = NULL;
31
32 file = setmntent(mtab, "r");
33 if (!file)
34 return -1;
35
36 while ((mntent = getmntent(file))) {
37
38 if (strcmp(mntent->mnt_type, "debugfs"))
39 continue;
40
41 strcpy(clk_path, mntent->mnt_dir);
42 ret = 0;
43 break;
44 }
45
46 fclose(file);
47 return ret;
48}
49
Daniel Lezcano558a6d52011-03-23 14:37:41 +010050int init_clock_details(bool dump, int selectedwindow)
Amit Arora24ed7d12010-09-14 12:12:58 +053051{
Amit Arora6e774cd2010-10-28 11:31:24 +053052 struct stat buf;
Amit Aroraed3e5652010-10-27 12:02:53 +053053
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010054 if (locate_debugfs(clk_dir_path)) {
Amit Arora81350772010-11-16 14:56:26 +053055 if (!dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +010056 create_selectedwindow(selectedwindow);
Amit Arora81350772010-11-16 14:56:26 +053057 sprintf(clock_lines[0], "Unable to locate debugfs "
Amit Kucheriaa0adae42011-01-12 10:54:23 -060058 "mount point. Mount debugfs "
59 "and try again..\n");
Amit Arora81350772010-11-16 14:56:26 +053060 print_one_clock(0, clock_lines[0], 1, 0);
61 old_clock_line_no = 1;
62 return(1);
63 } else {
64 fprintf(stderr, "powerdebug: Unable to locate debugfs "
Amit Kucheriaa0adae42011-01-12 10:54:23 -060065 "mount point. Mount debugfs and try "
66 "again..\n");
Daniel Lezcano6a0f6fb2011-03-26 22:06:12 +010067 return -1;
Amit Arora81350772010-11-16 14:56:26 +053068 }
Amit Arora6e774cd2010-10-28 11:31:24 +053069 }
Daniel Lezcano9dc3fb32011-03-26 22:06:08 +010070
Amit Arora6e774cd2010-10-28 11:31:24 +053071 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
Amit Arora59429a42010-11-16 11:30:20 +053072 //strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
Amit Arora6e774cd2010-10-28 11:31:24 +053073 if (stat(clk_dir_path, &buf)) {
Amit Arora81350772010-11-16 14:56:26 +053074 if (!dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +010075 create_selectedwindow(selectedwindow);
Amit Arora81350772010-11-16 14:56:26 +053076 sprintf(clock_lines[0], "Unable to find clock tree"
77 " information at %s.\n", clk_dir_path);
78 print_one_clock(0, clock_lines[0], 1, 0);
79 old_clock_line_no = 1;
80 return(1);
81 } else {
82 fprintf(stderr, "powerdebug: Unable to find clock tree"
83 " information at %s.\n", clk_dir_path);
Daniel Lezcano6a0f6fb2011-03-26 22:06:12 +010084 return -1;
Amit Arora81350772010-11-16 14:56:26 +053085 }
Amit Arora6e774cd2010-10-28 11:31:24 +053086 }
Daniel Lezcanoc5a65bf2011-03-26 22:06:11 +010087
88 return 0;
Amit Arora24ed7d12010-09-14 12:12:58 +053089}
Amit Arora728e0c92010-09-14 12:06:09 +053090
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010091static int file_read_from_format(char *file, int *value, const char *format)
Amit Arora728e0c92010-09-14 12:06:09 +053092{
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010093 FILE *f;
Amit Arora6e774cd2010-10-28 11:31:24 +053094 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +053095
Daniel Lezcano2d19ae82011-03-26 22:06:09 +010096 f = fopen(file, "r");
97 if (!f)
98 return -1;
99 ret = fscanf(f, format, value);
100 fclose(f);
Amit Arora728e0c92010-09-14 12:06:09 +0530101
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100102 return !ret ? -1 : 0;
103}
Amit Arora728e0c92010-09-14 12:06:09 +0530104
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100105static inline int file_read_int(char *file, int *value)
106{
107 return file_read_from_format(file, value, "%d");
108}
Amit Arora728e0c92010-09-14 12:06:09 +0530109
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100110static inline int file_read_hex(char *file, int *value)
111{
112 return file_read_from_format(file, value, "%x");
Amit Arora728e0c92010-09-14 12:06:09 +0530113}
114
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100115static void dump_parent(struct clock_info *clk, int line, bool dump)
116{
117 char *unit = "Hz";
118 double drate;
119 static char spaces[64];
120 char str[256];
121 static int maxline;
122
123 if (maxline < line)
124 maxline = line;
125
126 if (clk && clk->parent)
127 dump_parent(clk->parent, ++line, dump);
128
129 drate = (double)clk->rate;
130 if (drate > 1000 && drate < 1000000) {
131 unit = "KHz";
132 drate /= 1000;
133 }
134 if (drate > 1000000) {
135 unit = "MHz";
136 drate /= 1000000;
137 }
138 if (clk == clocks_info) {
139 line++;
140 strcpy(spaces, "");
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100141 sprintf(str, "%s%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100142 spaces, clk->name, clk->flags, clk->usecount, drate,
143 unit);
144 } else {
145 if (!(clk->parent == clocks_info))
146 strcat(spaces, " ");
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100147 sprintf(str, "%s`- %s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Daniel Lezcanoc6718332011-03-23 14:37:39 +0100148 spaces, clk->name, clk->flags, clk->usecount, drate,
149 unit);
150 }
151 if (dump)
152 //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str);
153 printf("%s", str);
154 else
155 print_one_clock(maxline - line + 2, str, 1, 0);
156}
157
158static void dump_all_parents(char *clkarg, bool dump)
159{
160 struct clock_info *clk;
161 char spaces[1024];
162
163 strcpy(spaces, "");
164
165 clk = find_clock(clocks_info, clkarg);
166
167 if (!clk)
168 printf("Clock NOT found!\n");
169 else {
170 /* while(clk && clk != clocks_info) { */
171 /* printf("%s\n", clk->name); */
172 /* strcat(spaces, " "); */
173 /* clk = clk->parent; */
174 /* printf("%s <-- ", spaces); */
175 /* } */
176 /* printf(" /\n"); */
177 dump_parent(clk, 1, dump);
178 }
179}
180
Daniel Lezcano897f7332011-03-26 22:06:06 +0100181void find_parents_for_clock(char *clkname, int complete)
Amit Arora3bd79162010-12-01 13:51:42 +0530182{
183 char name[256];
184
185 name[0] = '\0';
186 if (!complete) {
187 char str[256];
188
189 strcat(name, clkname);
190 sprintf(str, "Enter Clock Name : %s\n", name);
191 print_one_clock(2, str, 1, 0);
192 return;
193 }
194 sprintf(name, "Parents for \"%s\" Clock : \n", clkname);
195 print_one_clock(0, name, 1, 1);
Daniel Lezcano897f7332011-03-26 22:06:06 +0100196 dump_all_parents(clkname, false);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600197}
Amit Arora3bd79162010-12-01 13:51:42 +0530198
Amit Arora24ed7d12010-09-14 12:12:58 +0530199int read_and_print_clock_info(int verbose, int hrow, int selected)
200{
Amit Arora3bd79162010-12-01 13:51:42 +0530201 print_one_clock(0, "Reading Clock Tree ...", 1, 1);
Amit Aroraa06a7302010-12-02 15:59:37 +0530202
203 if (!old_clock_line_no || selected == REFRESH_WINDOW) {
204 destroy_clocks_info();
Amit Aroraac4e8652010-11-09 11:16:53 +0530205 read_clock_info(clk_dir_path);
Amit Aroraa06a7302010-12-02 15:59:37 +0530206 }
Amit Arora29cb7572010-10-05 17:40:29 +0530207
Amit Arora031263a2010-11-09 11:12:41 +0530208 if (!clocks_info->num_children) {
209 fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
210 exit(1);
Amit Arora6e774cd2010-10-28 11:31:24 +0530211 }
Amit Arora728e0c92010-09-14 12:06:09 +0530212
Amit Aroraa06a7302010-12-02 15:59:37 +0530213 if (selected == CLOCK_SELECTED)
214 selected = 1;
215 else
216 selected = 0;
217
Amit Aroraac4e8652010-11-09 11:16:53 +0530218 print_clock_info(verbose, hrow, selected);
Amit Aroraac4e8652010-11-09 11:16:53 +0530219 hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
220
221 return hrow;
222}
223
Amit Arora3bc8c922010-11-16 11:27:38 +0530224int calc_delta_screen_size(int hrow)
225{
Amit Arora51d1b9c2010-11-30 13:55:15 +0530226 if (hrow >= (maxy - 3))
227 return hrow - (maxy - 4);
Amit Arora3bc8c922010-11-16 11:27:38 +0530228
229 return 0;
230}
231
Amit Aroraac4e8652010-11-09 11:16:53 +0530232void print_clock_info(int verbose, int hrow, int selected)
233{
Amit Arora3bc8c922010-11-16 11:27:38 +0530234 int i, count = 0, delta;
Amit Aroraac4e8652010-11-09 11:16:53 +0530235
236 (void)verbose;
237
238 print_clock_header();
Amit Arora728e0c92010-09-14 12:06:09 +0530239
Amit Arora031263a2010-11-09 11:12:41 +0530240 for (i = 0; i < clocks_info->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530241 add_clock_details_recur(clocks_info->children[i],
242 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530243
Amit Arora3bc8c922010-11-16 11:27:38 +0530244 delta = calc_delta_screen_size(hrow);
245
246 while (clock_lines[count + delta] &&
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600247 strcmp(clock_lines[count + delta], "")) {
Amit Arora3bc8c922010-11-16 11:27:38 +0530248 if (count < delta) {
249 count++;
250 continue;
251 }
252 print_one_clock(count - delta, clock_lines[count + delta],
253 bold[count + delta], (hrow == (count + delta)));
Amit Arora031263a2010-11-09 11:12:41 +0530254 count++;
255 }
256
Amit Aroraac4e8652010-11-09 11:16:53 +0530257 old_clock_line_no = clock_line_no;
Amit Arora031263a2010-11-09 11:12:41 +0530258 clock_line_no = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530259}
260
Amit Aroraac4e8652010-11-09 11:16:53 +0530261void prepare_name_str(char *namestr, struct clock_info *clock)
262{
263 int i;
264
265 strcpy(namestr, "");
266 if (clock->level > 1)
267 for (i = 0; i < (clock->level - 1); i++)
268 strcat(namestr, " ");
269 strcat(namestr, clock->name);
270}
271
272void add_clock_details_recur(struct clock_info *clock, int hrow, int selected)
Amit Arora031263a2010-11-09 11:12:41 +0530273{
274 int i;
275 char *unit = " Hz";
276 char rate_str[64];
Amit Aroraac4e8652010-11-09 11:16:53 +0530277 char name_str[256];
Amit Arora031263a2010-11-09 11:12:41 +0530278 double drate = (double)clock->rate;
279
280 if (drate > 1000 && drate < 1000000) {
281 unit = "KHz";
282 drate /= 1000;
283 }
284 if (drate > 1000000) {
285 unit = "MHz";
286 drate /= 1000000;
287 }
288 if (clock->usecount)
289 bold[clock_line_no] = 1;
290 else
291 bold[clock_line_no] = 0;
292
293 sprintf(rate_str, "%.2f %s", drate, unit);
Amit Aroraac4e8652010-11-09 11:16:53 +0530294 prepare_name_str(name_str, clock);
295 sprintf(clock_lines[clock_line_no++], "%-55s %-4d %-12s %-12d %-12d",
296 name_str, clock->flags, rate_str, clock->usecount,
297 clock->num_children);
298
299 if (selected && (hrow == (clock_line_no - 1))) {
300 if (clock->expanded)
301 collapse_all_subclocks(clock);
302 else
303 clock->expanded = 1;
304 selected = 0;
305 }
Amit Arora031263a2010-11-09 11:12:41 +0530306
307 if (clock->expanded && clock->num_children)
308 for (i = 0; i < clock->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530309 add_clock_details_recur(clock->children[i],
310 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530311 strcpy(clock_lines[clock_line_no], "");
312}
313
Amit Aroraac4e8652010-11-09 11:16:53 +0530314void collapse_all_subclocks(struct clock_info *clock)
315{
316 int i;
317
318 clock->expanded = 0;
319 if (clock->num_children)
320 for (i = 0; i < clock->num_children; i++)
321 collapse_all_subclocks(clock->children[i]);
322}
323
Amit Arora031263a2010-11-09 11:12:41 +0530324void destroy_clocks_info(void)
325{
326 int i;
327
Amit Aroraa06a7302010-12-02 15:59:37 +0530328 if (!clocks_info)
329 return;
330
Amit Arora031263a2010-11-09 11:12:41 +0530331 if (clocks_info->num_children) {
332 for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
333 destroy_clocks_info_recur(clocks_info->children[i]);
334 if (!i) {
335 free(clocks_info->children);
336 clocks_info->children = NULL;
337 }
338 }
339 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530340 clocks_info->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530341 free(clocks_info);
342 clocks_info = NULL;
343}
344
345void destroy_clocks_info_recur(struct clock_info *clock)
346{
347 int i;
348
349 if (clock && clock->num_children) {
350 for (i = (clock->num_children - 1); i >= 0; i--) {
351 fflush(stdin);
352 destroy_clocks_info_recur(clock->children[i]);
353 if (!i) {
Amit Arora031263a2010-11-09 11:12:41 +0530354 free(clock->children);
355 clock->children = NULL;
356 clock->num_children = 0;
357 }
358 }
359 }
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 Aroraf4fb8102010-11-30 13:55:50 +0530496struct clock_info *find_clock(struct clock_info *clk, char *clkarg)
497{
498 int i;
499 struct clock_info *ret = clk;
500
501 if (!strcmp(clk->name, clkarg))
502 return ret;
503
504 if (clk->children) {
505 for (i = 0; i < clk->num_children; i++) {
506 if (!strcmp(clk->children[i]->name, clkarg))
507 return clk->children[i];
508 }
509 for (i = 0; i < clk->num_children; i++) {
510 ret = find_clock(clk->children[i], clkarg);
511 if (ret)
512 return ret;
513 }
514 }
515
516 return NULL;
517}
518
Amit Aroraeb6cba92010-10-25 16:03:21 +0530519
Amit Aroraac4e8652010-11-09 11:16:53 +0530520void dump_clock_info(struct clock_info *clk, int level, int bmp)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530521{
Amit Arora6e774cd2010-10-28 11:31:24 +0530522 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530523
Amit Arora6e774cd2010-10-28 11:31:24 +0530524 if (!clk)
525 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530526
Amit Arora6e774cd2010-10-28 11:31:24 +0530527 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
528 if (i == (level - 1)) {
529 if (clk->last_child)
530 printf("`-- ");
531 else
532 printf("|-- ");
533 } else {
534 if ((1<<j) & bmp)
535 printf("| ");
536 else
537 printf(" ");
538 }
539 }
Amit Arora031263a2010-11-09 11:12:41 +0530540
Amit Arora6e774cd2010-10-28 11:31:24 +0530541 if (clk == clocks_info)
542 printf("%s\n", clk->name);
543 else {
544 char *unit = "Hz";
545 double drate = (double)clk->rate;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600546
Amit Arora6e774cd2010-10-28 11:31:24 +0530547 if (drate > 1000 && drate < 1000000) {
548 unit = "KHz";
549 drate /= 1000;
550 }
551 if (drate > 1000000) {
552 unit = "MHz";
553 drate /= 1000000;
554 }
Daniel Lezcano2d19ae82011-03-26 22:06:09 +0100555 printf("%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
Amit Arora6e774cd2010-10-28 11:31:24 +0530556 clk->name, clk->flags, clk->usecount, drate, unit);
Amit Arora6e774cd2010-10-28 11:31:24 +0530557 }
558 if (clk->children) {
559 int tbmp = bmp;
560 int xbmp = -1;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530561
Amit Arora6e774cd2010-10-28 11:31:24 +0530562 if (clk->last_child) {
563 xbmp ^= 1 << (level - 2);
564
565 xbmp = tbmp & xbmp;
566 } else
567 xbmp = bmp;
568 for (i = 0; i < clk->num_children; i++) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530569 tbmp = xbmp | (1 << level);
Amit Aroraac4e8652010-11-09 11:16:53 +0530570 dump_clock_info(clk->children[i], level + 1, tbmp);
Amit Arora6e774cd2010-10-28 11:31:24 +0530571 }
572 }
Amit Arora0e512722010-10-01 12:24:16 +0530573}