blob: d5571efbeecd000e826730a5a119b79e29bb6c0d [file] [log] [blame]
Amit Arorae9e16b02010-08-03 10:15:20 +05301#include <getopt.h>
2
3#include "powerdebug.h"
4
5
6int numregulators;
7
8int init_regulator_ds(void)
9{
10 DIR *regdir;
11 struct dirent *item;
12
13 regdir = opendir("/sys/class/regulator");
14 if (!regdir)
15 return(1);
16 while((item = readdir(regdir))) {
17 if (strncmp(item->d_name, "regulator", 9))
18 continue;
19
20 numregulators++;
21 }
22 closedir(regdir);
23
24 regulators_info = (struct regulator_info *)malloc(numregulators*
25 sizeof(struct regulator_info));
26 if (!regulators_info) {
27 fprintf(stderr, "init_regulator_ds: Not enough memory to read information for %d regulators!\n",
28 numregulators);
29 return(1);
30 }
31
32 return(0);
33}
34
35int read_and_print_sensor_info(int verbose)
36{
37 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053038 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053039 char filename[PATH_MAX], devpath[PATH_MAX];
40 char device[PATH_MAX];
41 struct dirent *item, *subitem;
42
43/*
44 1. readdir /sys/class/hwmon
45 2. foreach subdir in above, do following
46 a) check if its virtual or physical (check for device)
47 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
48 i> search for "_" (strrchr... check for last)
49 ii> check the *item* depending on type
50 i.e for "in" type items=min, max, input, label
51 for "fan" type items=min,max,input,div,target,label
52 for "pwm" type items=enable, mode, freq
53 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
54 for "curr" type items=max,min,input
55 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
56 for "energy" type items=input
57 for "intrusion" type items=alarm,beep
58*/
59
60 sprintf(filename, "%s", "/sys/class/hwmon");
61
62 dir = opendir(filename);
63 if (!dir)
64 return errno;
65
66 while ((item = readdir(dir))) {
67 if (item->d_name[0] == '.') /* skip the hidden files */
68 continue;
69
Amit Arora1c037892010-08-05 13:46:10 +053070 found = 1;
71
Amit Arorae9e16b02010-08-03 10:15:20 +053072 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
73 sprintf(devpath, "%s/device", filename);
74
75 len = readlink(devpath, device, PATH_MAX - 1);
76
77 if (len < 0)
78 strcpy(devpath, filename);
79 else
80 device[len] = '\0';
81
82 subdir = opendir(devpath);
83
84 printf("\nSensor Information for %s :\n", item->d_name);
85 fflush(stdin);
86 while ((subitem = readdir(subdir))) {
87 if (subitem->d_name[0] == '.') /* skip hidden files */
88 continue;
89
90 if(!strncmp(subitem->d_name, "in", 2))
91 get_sensor_info(devpath, subitem->d_name, "in",
92 verbose);
93 else if (!strncmp(subitem->d_name, "temp", 4))
94 get_sensor_info(devpath, subitem->d_name,
95 "temp", verbose);
96 else if (!strncmp(subitem->d_name, "fan", 4))
97 get_sensor_info(devpath, subitem->d_name,
98 "fan", verbose);
99 else if (!strncmp(subitem->d_name, "pwm", 4))
100 get_sensor_info(devpath, subitem->d_name,
101 "pwm", verbose);
102
103 }
104
105 closedir(subdir);
106 }
107 closedir(dir);
108
Amit Arora1c037892010-08-05 13:46:10 +0530109 if(!found && verbose) {
110 printf("Could not find sensor information!");
111 printf(" Looks like /sys/class/hwmon is empty.\n");
112 }
113
Amit Arorae9e16b02010-08-03 10:15:20 +0530114 return 0;
115}
116
117
118int read_regulator_info(int verbose)
119{
120 FILE *file = NULL;
121 DIR *regdir, *dir;
122 int len, count = 0, ret = 0;
123 char line[1024], filename[1024], *fptr;
124 struct dirent *item, *ritem;
125
126 regdir = opendir("/sys/class/regulator");
127 if (!regdir)
128 return(1);
129 while((item = readdir(regdir))) {
130 if (strlen(item->d_name) < 3)
131 continue;
132
133 if (strncmp(item->d_name, "regulator", 9))
134 continue;
135
136 len = sprintf(filename, "/sys/class/regulator/%s",item->d_name);
137
138 dir = opendir(filename);
139 if (!dir) {
Amit Arora7ef178c2010-08-03 15:57:21 +0530140 //ret = 1;
141 //goto exit2;
142 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530143 }
144
145 count++;
146 if (count > numregulators) {
147 ret = 1;
148 goto exit1;
149 }
150
Amit Arorafccac912010-08-03 16:15:09 +0530151 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530152 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530153 if (strlen(ritem->d_name) < 3)
154 continue;
155
156 sprintf(filename + len, "/%s", ritem->d_name);
157 // printf("Inside %s directory. Opening file %s!\n", item->d_name, ritem->d_name);
158
159 file = fopen(filename, "r");
160 if (!file)
161 continue;
162
163 memset(line, 0, 1024);
164 fptr = fgets(line, 1024, file);
165 fclose(file);
166 if (!fptr) {
Amit Arora7ef178c2010-08-03 15:57:21 +0530167 //ret = 1;
168 //goto exit1;
169 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530170 }
171 // printf("Read file %s, data=%s, count = %d\n", filename, fptr, count);
172
173 if (!strcmp(ritem->d_name, "name"))
174 strcpy(regulators_info[count-1].name, fptr);
175 if (!strcmp(ritem->d_name, "state"))
176 strcpy(regulators_info[count-1].state, fptr);
177 if (!strcmp(ritem->d_name, "status"))
178 strcpy(regulators_info[count-1].status, fptr);
179
180 /* Read following _only_ if verbose option specified */
181 if(!verbose)
182 continue;
183
184 if (!strcmp(ritem->d_name, "type"))
185 strcpy(regulators_info[count-1].type, fptr);
186 if (!strcmp(ritem->d_name, "opmode"))
187 strcpy(regulators_info[count-1].opmode, fptr);
188
189 if (!strcmp(ritem->d_name, "microvolts"))
190 regulators_info[count-1].microvolts = atoi(fptr);
Amit Arora83faf0e2010-08-05 12:27:42 +0530191 if (!strcmp(ritem->d_name, "min_microvolts"))
192 regulators_info[count-1].min_microvolts = atoi(fptr);
193 if (!strcmp(ritem->d_name, "max_microvolts"))
194 regulators_info[count-1].max_microvolts = atoi(fptr);
195
Amit Arorae9e16b02010-08-03 10:15:20 +0530196 if (!strcmp(ritem->d_name, "microamps"))
197 regulators_info[count-1].microamps = atoi(fptr);
Amit Arora83faf0e2010-08-05 12:27:42 +0530198 if (!strcmp(ritem->d_name, "min_microamps"))
199 regulators_info[count-1].min_microamps = atoi(fptr);
200 if (!strcmp(ritem->d_name, "max_microamps"))
201 regulators_info[count-1].max_microamps = atoi(fptr);
202 if (!strcmp(ritem->d_name, "requested_microamps"))
203 regulators_info[count-1].requested_microamps = atoi(fptr);
204
Amit Arorae9e16b02010-08-03 10:15:20 +0530205 if (!strcmp(ritem->d_name, "num_users"))
206 regulators_info[count-1].num_users = atoi(fptr);
Amit Arorae9e16b02010-08-03 10:15:20 +0530207 }
208exit1:
209 closedir(dir);
210 if (ret)
211 break;
212 }
Amit Arora7ef178c2010-08-03 15:57:21 +0530213//exit2:
Amit Arorae9e16b02010-08-03 10:15:20 +0530214 closedir(regdir);
215
216 return ret;
217}
218
219
220int main(int argc, char **argv)
221{
222 int c;
223 int regulators = 0, sensors = 0, verbose = 0;
224
Amit Arorafefe8bf2010-08-05 13:31:20 +0530225 /*
226 * Options:
227 * -r, --regulator : regulator
228 * -s, --sensor : sensors
229 * -v, --verbose : verbose
230 * -V, --version : version
231 * -h, --help : help
232 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530233 */
234
Amit Arorafefe8bf2010-08-05 13:31:20 +0530235 while (1) {
236 int optindex = 0;
237 static struct option long_options[] = {
238 {"regulator", 0, 0, 'r'},
239 {"sensor", 0, 0, 's'},
240 {"verbose", 0, 0, 'v'},
241 {"version", 0, 0, 'V'},
242 {"help", 0, 0, 'h'},
243 {0, 0, 0, 0}
244 };
245
246 c = getopt_long(argc, argv, "rsvVh", long_options, &optindex);
247 if (c == -1)
248 break;
249
250 switch (c) {
251 case 'r':
252 regulators = 1;
253 break;
254 case 's':
255 sensors = 1;
256 break;
257 case 'v':
258 verbose = 1;
259 break;
260 case 'V':
261 version();
262 break;
263 case 'h':
264 usage(argv);
265 break;
266 case '?':
267 fprintf (stderr, "%s: Unknown option %c'.\n",
268 argv[0], optopt);
269 exit(1);
270 default:
271 usage(argv);
272 break;
273 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530274 }
275
Amit Arorafefe8bf2010-08-05 13:31:20 +0530276
277 /* Need atleast one option specified */
Amit Arorae9e16b02010-08-03 10:15:20 +0530278 if (!regulators && !sensors) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530279 usage(argv);
280 }
281
282 init_regulator_ds();
283
284 if (regulators) {
285 read_regulator_info(verbose);
286 print_regulator_info(verbose);
287 }
288
289 if (sensors) {
290 read_and_print_sensor_info(verbose);
291 }
292
293 return 0;
294
295}