blob: 18c9777acc879210120e5861f320533d4916a4fe [file] [log] [blame]
Amit Arora17552782010-12-02 12:23:14 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora17552782010-12-02 12:23:14 +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 "regulator.h"
17
18int init_regulator_ds(void)
19{
20 DIR *regdir;
21 struct dirent *item;
22
23 regdir = opendir("/sys/class/regulator");
24 if (!regdir)
25 return(1);
Amit Kucheriaa0adae42011-01-12 10:54:23 -060026 while ((item = readdir(regdir))) {
Amit Arora17552782010-12-02 12:23:14 +053027 if (strncmp(item->d_name, "regulator", 9))
28 continue;
29
30 numregulators++;
31 }
32 closedir(regdir);
33
34 regulators_info = (struct regulator_info *)malloc(numregulators*
Amit Kucheriaa0adae42011-01-12 10:54:23 -060035 sizeof(struct regulator_info));
Amit Arora17552782010-12-02 12:23:14 +053036 if (!regulators_info) {
37 fprintf(stderr, "init_regulator_ds: Not enough memory to "
Amit Kucheriaa0adae42011-01-12 10:54:23 -060038 "read information for %d regulators!\n", numregulators);
Amit Arora17552782010-12-02 12:23:14 +053039 return(1);
40 }
41
Amit Kucheriaa0adae42011-01-12 10:54:23 -060042 return(0);
Amit Arora17552782010-12-02 12:23:14 +053043}
44
45void print_string_val(char *name, char *val)
46{
47 printf("\t%s=%s", name, val);
Amit Kucheriaa0adae42011-01-12 10:54:23 -060048 if (!strchr(val, '\n'))
Amit Arora17552782010-12-02 12:23:14 +053049 printf("\n");
50}
51
52void print_regulator_info(int verbose)
53{
54 int i;
55
Amit Arora422c52f2010-12-02 16:22:29 +053056 printf("\nRegulator Information:\n");
57 printf("*********************\n\n");
58
Amit Kucheriaa0adae42011-01-12 10:54:23 -060059 for (i = 0; i < numregulators; i++) {
60 printf("Regulator %d:\n", i + 1);
61 print_string_val("name", regulators_info[i].name);
Amit Arora17552782010-12-02 12:23:14 +053062 if (strcmp(regulators_info[i].status, ""))
63 print_string_val("status", regulators_info[i].status);
64 if (strcmp(regulators_info[i].state, ""))
65 print_string_val("state", regulators_info[i].state);
66
67 if (!verbose)
68 continue;
69
70 if (strcmp(regulators_info[i].type, ""))
71 print_string_val("type", regulators_info[i].type);
72 if (strcmp(regulators_info[i].opmode, ""))
73 print_string_val("opmode", regulators_info[i].opmode);
74
75 if (regulators_info[i].microvolts)
76 printf("\tmicrovolts=%d\n",
77 regulators_info[i].microvolts);
78 if (regulators_info[i].min_microvolts)
79 printf("\tmin_microvolts=%d\n",
80 regulators_info[i].min_microvolts);
81 if (regulators_info[i].max_microvolts)
82 printf("\tmax_microvolts=%d\n",
83 regulators_info[i].max_microvolts);
84
85 if (regulators_info[i].microamps)
86 printf("\tmicroamps=%d\n",
87 regulators_info[i].microamps);
88 if (regulators_info[i].min_microamps)
89 printf("\tmin_microamps=%d\n",
90 regulators_info[i].min_microamps);
91 if (regulators_info[i].max_microamps)
92 printf("\tmax_microamps=%d\n",
93 regulators_info[i].max_microamps);
94 if (regulators_info[i].requested_microamps)
95 printf("\trequested_microamps=%d\n",
96 regulators_info[i].requested_microamps);
97
98 if (regulators_info[i].num_users)
99 printf("\tnum_users=%d\n",
100 regulators_info[i].num_users);
101 printf("\n");
102 }
103
104 if (!numregulators && verbose) {
105 printf("Could not find regulator information!");
106 printf(" Looks like /sys/class/regulator is empty.\n\n");
107 }
Amit Arora422c52f2010-12-02 16:22:29 +0530108
109 printf("\n\n");
Amit Arora17552782010-12-02 12:23:14 +0530110}
111
112void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
113{
114 if (!strcmp(ritem->d_name, "name"))
115 strcpy(regulators_info[idx].name, str);
116 if (!strcmp(ritem->d_name, "state"))
117 strcpy(regulators_info[idx].state, str);
118 if (!strcmp(ritem->d_name, "status"))
119 strcpy(regulators_info[idx].status, str);
120
121 if (!strcmp(ritem->d_name, "type"))
122 strcpy(regulators_info[idx].type, str);
123 if (!strcmp(ritem->d_name, "opmode"))
124 strcpy(regulators_info[idx].opmode, str);
125
126 if (!strcmp(ritem->d_name, "microvolts"))
127 regulators_info[idx].microvolts = atoi(str);
128 if (!strcmp(ritem->d_name, "min_microvolts"))
129 regulators_info[idx].min_microvolts = atoi(str);
130 if (!strcmp(ritem->d_name, "max_microvolts"))
131 regulators_info[idx].max_microvolts = atoi(str);
132
133 if (!strcmp(ritem->d_name, "microamps"))
134 regulators_info[idx].microamps = atoi(str);
135 if (!strcmp(ritem->d_name, "min_microamps"))
136 regulators_info[idx].min_microamps = atoi(str);
137 if (!strcmp(ritem->d_name, "max_microamps"))
138 regulators_info[idx].max_microamps = atoi(str);
139 if (!strcmp(ritem->d_name, "requested_microamps"))
140 regulators_info[idx].requested_microamps = atoi(str);
141
142 if (!strcmp(ritem->d_name, "num_users"))
143 regulators_info[idx].num_users = atoi(str);
144}
145
146int read_regulator_info(void)
147{
148 FILE *file = NULL;
149 DIR *regdir, *dir;
150 int len, count = 0, ret = 0;
151 char line[1024], filename[1024], *fptr;
152 struct dirent *item, *ritem;
153
154 regdir = opendir("/sys/class/regulator");
155 if (!regdir)
156 return(1);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600157 while ((item = readdir(regdir))) {
Amit Arora17552782010-12-02 12:23:14 +0530158 if (strlen(item->d_name) < 3)
159 continue;
160
161 if (strncmp(item->d_name, "regulator", 9))
162 continue;
163
164 len = sprintf(filename, "/sys/class/regulator/%s",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600165 item->d_name);
Amit Arora17552782010-12-02 12:23:14 +0530166
167 dir = opendir(filename);
168 if (!dir)
169 continue;
170 count++;
171
172 if (count > numregulators) {
173 ret = 1;
174 goto exit;
175 }
176
177 strcpy(regulators_info[count-1].name, item->d_name);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600178 while ((ritem = readdir(dir))) {
Amit Arora17552782010-12-02 12:23:14 +0530179 if (strlen(ritem->d_name) < 3)
180 continue;
181
182 sprintf(filename + len, "/%s", ritem->d_name);
183 file = fopen(filename, "r");
184 if (!file)
185 continue;
186 memset(line, 0, 1024);
187 fptr = fgets(line, 1024, file);
188 fclose(file);
189 if (!fptr)
190 continue;
191 read_info_from_dirent(ritem, fptr, count - 1);
192 }
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600193 exit:
Amit Arora17552782010-12-02 12:23:14 +0530194 closedir(dir);
195 if (ret)
196 break;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600197 }
Amit Arora17552782010-12-02 12:23:14 +0530198 closedir(regdir);
199
200 return ret;
201}