blob: 342a1a759ed2c056d7bbe9b245fa0909a297bac6 [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
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010018#define SYSFS_REGULATOR "/sys/class/regulator"
19
Daniel Lezcanocac52d92011-03-26 22:05:49 +010020int regulator_init(void)
Amit Arora17552782010-12-02 12:23:14 +053021{
22 DIR *regdir;
23 struct dirent *item;
24
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010025 regdir = opendir(SYSFS_REGULATOR);
26 if (!regdir) {
27 fprintf(stderr, "failed to open '%s': %m\n", SYSFS_REGULATOR);
28 return -1;
29 }
30
Amit Kucheriaa0adae42011-01-12 10:54:23 -060031 while ((item = readdir(regdir))) {
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010032
33 if (!strcmp(item->d_name, "."))
34 continue;
35
36 if (!strcmp(item->d_name, ".."))
Amit Arora17552782010-12-02 12:23:14 +053037 continue;
38
39 numregulators++;
40 }
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010041
Amit Arora17552782010-12-02 12:23:14 +053042 closedir(regdir);
43
44 regulators_info = (struct regulator_info *)malloc(numregulators*
Amit Kucheriaa0adae42011-01-12 10:54:23 -060045 sizeof(struct regulator_info));
Amit Arora17552782010-12-02 12:23:14 +053046 if (!regulators_info) {
47 fprintf(stderr, "init_regulator_ds: Not enough memory to "
Amit Kucheriaa0adae42011-01-12 10:54:23 -060048 "read information for %d regulators!\n", numregulators);
Amit Arora17552782010-12-02 12:23:14 +053049 return(1);
50 }
51
Amit Kucheriaa0adae42011-01-12 10:54:23 -060052 return(0);
Amit Arora17552782010-12-02 12:23:14 +053053}
54
Daniel Lezcanof28e4882011-03-26 22:05:50 +010055static void print_string_val(char *name, char *val)
Amit Arora17552782010-12-02 12:23:14 +053056{
57 printf("\t%s=%s", name, val);
Amit Kucheriaa0adae42011-01-12 10:54:23 -060058 if (!strchr(val, '\n'))
Amit Arora17552782010-12-02 12:23:14 +053059 printf("\n");
60}
61
Daniel Lezcanof28e4882011-03-26 22:05:50 +010062void regulator_print_info(int verbose)
Amit Arora17552782010-12-02 12:23:14 +053063{
64 int i;
65
Amit Arora422c52f2010-12-02 16:22:29 +053066 printf("\nRegulator Information:\n");
67 printf("*********************\n\n");
68
Amit Kucheriaa0adae42011-01-12 10:54:23 -060069 for (i = 0; i < numregulators; i++) {
70 printf("Regulator %d:\n", i + 1);
71 print_string_val("name", regulators_info[i].name);
Amit Arora17552782010-12-02 12:23:14 +053072 if (strcmp(regulators_info[i].status, ""))
73 print_string_val("status", regulators_info[i].status);
74 if (strcmp(regulators_info[i].state, ""))
75 print_string_val("state", regulators_info[i].state);
76
77 if (!verbose)
78 continue;
79
80 if (strcmp(regulators_info[i].type, ""))
81 print_string_val("type", regulators_info[i].type);
82 if (strcmp(regulators_info[i].opmode, ""))
83 print_string_val("opmode", regulators_info[i].opmode);
84
85 if (regulators_info[i].microvolts)
86 printf("\tmicrovolts=%d\n",
87 regulators_info[i].microvolts);
88 if (regulators_info[i].min_microvolts)
89 printf("\tmin_microvolts=%d\n",
90 regulators_info[i].min_microvolts);
91 if (regulators_info[i].max_microvolts)
92 printf("\tmax_microvolts=%d\n",
93 regulators_info[i].max_microvolts);
94
95 if (regulators_info[i].microamps)
96 printf("\tmicroamps=%d\n",
97 regulators_info[i].microamps);
98 if (regulators_info[i].min_microamps)
99 printf("\tmin_microamps=%d\n",
100 regulators_info[i].min_microamps);
101 if (regulators_info[i].max_microamps)
102 printf("\tmax_microamps=%d\n",
103 regulators_info[i].max_microamps);
104 if (regulators_info[i].requested_microamps)
105 printf("\trequested_microamps=%d\n",
106 regulators_info[i].requested_microamps);
107
108 if (regulators_info[i].num_users)
109 printf("\tnum_users=%d\n",
110 regulators_info[i].num_users);
111 printf("\n");
112 }
113
114 if (!numregulators && verbose) {
115 printf("Could not find regulator information!");
116 printf(" Looks like /sys/class/regulator is empty.\n\n");
117 }
Amit Arora422c52f2010-12-02 16:22:29 +0530118
119 printf("\n\n");
Amit Arora17552782010-12-02 12:23:14 +0530120}
121
Daniel Lezcanof28e4882011-03-26 22:05:50 +0100122static void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
Amit Arora17552782010-12-02 12:23:14 +0530123{
124 if (!strcmp(ritem->d_name, "name"))
125 strcpy(regulators_info[idx].name, str);
126 if (!strcmp(ritem->d_name, "state"))
127 strcpy(regulators_info[idx].state, str);
128 if (!strcmp(ritem->d_name, "status"))
129 strcpy(regulators_info[idx].status, str);
130
131 if (!strcmp(ritem->d_name, "type"))
132 strcpy(regulators_info[idx].type, str);
133 if (!strcmp(ritem->d_name, "opmode"))
134 strcpy(regulators_info[idx].opmode, str);
135
136 if (!strcmp(ritem->d_name, "microvolts"))
137 regulators_info[idx].microvolts = atoi(str);
138 if (!strcmp(ritem->d_name, "min_microvolts"))
139 regulators_info[idx].min_microvolts = atoi(str);
140 if (!strcmp(ritem->d_name, "max_microvolts"))
141 regulators_info[idx].max_microvolts = atoi(str);
142
143 if (!strcmp(ritem->d_name, "microamps"))
144 regulators_info[idx].microamps = atoi(str);
145 if (!strcmp(ritem->d_name, "min_microamps"))
146 regulators_info[idx].min_microamps = atoi(str);
147 if (!strcmp(ritem->d_name, "max_microamps"))
148 regulators_info[idx].max_microamps = atoi(str);
149 if (!strcmp(ritem->d_name, "requested_microamps"))
150 regulators_info[idx].requested_microamps = atoi(str);
151
152 if (!strcmp(ritem->d_name, "num_users"))
153 regulators_info[idx].num_users = atoi(str);
154}
155
Daniel Lezcanof28e4882011-03-26 22:05:50 +0100156int regulator_read_info(void)
Amit Arora17552782010-12-02 12:23:14 +0530157{
158 FILE *file = NULL;
159 DIR *regdir, *dir;
160 int len, count = 0, ret = 0;
161 char line[1024], filename[1024], *fptr;
162 struct dirent *item, *ritem;
163
164 regdir = opendir("/sys/class/regulator");
165 if (!regdir)
166 return(1);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600167 while ((item = readdir(regdir))) {
Amit Arora17552782010-12-02 12:23:14 +0530168 if (strlen(item->d_name) < 3)
169 continue;
170
171 if (strncmp(item->d_name, "regulator", 9))
172 continue;
173
174 len = sprintf(filename, "/sys/class/regulator/%s",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600175 item->d_name);
Amit Arora17552782010-12-02 12:23:14 +0530176
177 dir = opendir(filename);
178 if (!dir)
179 continue;
180 count++;
181
182 if (count > numregulators) {
183 ret = 1;
184 goto exit;
185 }
186
187 strcpy(regulators_info[count-1].name, item->d_name);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600188 while ((ritem = readdir(dir))) {
Amit Arora17552782010-12-02 12:23:14 +0530189 if (strlen(ritem->d_name) < 3)
190 continue;
191
192 sprintf(filename + len, "/%s", ritem->d_name);
193 file = fopen(filename, "r");
194 if (!file)
195 continue;
196 memset(line, 0, 1024);
197 fptr = fgets(line, 1024, file);
198 fclose(file);
199 if (!fptr)
200 continue;
201 read_info_from_dirent(ritem, fptr, count - 1);
202 }
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600203 exit:
Amit Arora17552782010-12-02 12:23:14 +0530204 closedir(dir);
205 if (ret)
206 break;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600207 }
Amit Arora17552782010-12-02 12:23:14 +0530208 closedir(regdir);
209
210 return ret;
211}