blob: ddbeea34953957275ce94bec8abdb508bd1f7cef [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 Lezcano4aab2fe2011-03-26 22:05:53 +010020struct regulator_info *regulator_init(int *nr_regulators)
Amit Arora17552782010-12-02 12:23:14 +053021{
22 DIR *regdir;
23 struct dirent *item;
24
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +010025 *nr_regulators = 0;
26
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010027 regdir = opendir(SYSFS_REGULATOR);
28 if (!regdir) {
29 fprintf(stderr, "failed to open '%s': %m\n", SYSFS_REGULATOR);
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +010030 return NULL;
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010031 }
32
Amit Kucheriaa0adae42011-01-12 10:54:23 -060033 while ((item = readdir(regdir))) {
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010034
35 if (!strcmp(item->d_name, "."))
36 continue;
37
38 if (!strcmp(item->d_name, ".."))
Amit Arora17552782010-12-02 12:23:14 +053039 continue;
40
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +010041 (*nr_regulators)++;
Amit Arora17552782010-12-02 12:23:14 +053042 }
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010043
Amit Arora17552782010-12-02 12:23:14 +053044 closedir(regdir);
45
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +010046 return malloc(*nr_regulators * sizeof(struct regulator_info));
Amit Arora17552782010-12-02 12:23:14 +053047}
48
Daniel Lezcanof28e4882011-03-26 22:05:50 +010049static void print_string_val(char *name, char *val)
Amit Arora17552782010-12-02 12:23:14 +053050{
51 printf("\t%s=%s", name, val);
Amit Kucheriaa0adae42011-01-12 10:54:23 -060052 if (!strchr(val, '\n'))
Amit Arora17552782010-12-02 12:23:14 +053053 printf("\n");
54}
55
Daniel Lezcanof45321e2011-03-26 22:05:52 +010056void regulator_print_info(struct regulator_info *reg_info, int verbose)
Amit Arora17552782010-12-02 12:23:14 +053057{
58 int i;
59
Amit Arora422c52f2010-12-02 16:22:29 +053060 printf("\nRegulator Information:\n");
61 printf("*********************\n\n");
62
Amit Kucheriaa0adae42011-01-12 10:54:23 -060063 for (i = 0; i < numregulators; i++) {
64 printf("Regulator %d:\n", i + 1);
Daniel Lezcanof45321e2011-03-26 22:05:52 +010065 print_string_val("name", reg_info[i].name);
66 if (strcmp(reg_info[i].status, ""))
67 print_string_val("status", reg_info[i].status);
68 if (strcmp(reg_info[i].state, ""))
69 print_string_val("state", reg_info[i].state);
Amit Arora17552782010-12-02 12:23:14 +053070
71 if (!verbose)
72 continue;
73
Daniel Lezcanof45321e2011-03-26 22:05:52 +010074 if (strcmp(reg_info[i].type, ""))
75 print_string_val("type", reg_info[i].type);
76 if (strcmp(reg_info[i].opmode, ""))
77 print_string_val("opmode", reg_info[i].opmode);
Amit Arora17552782010-12-02 12:23:14 +053078
Daniel Lezcanof45321e2011-03-26 22:05:52 +010079 if (reg_info[i].microvolts)
Amit Arora17552782010-12-02 12:23:14 +053080 printf("\tmicrovolts=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +010081 reg_info[i].microvolts);
82 if (reg_info[i].min_microvolts)
Amit Arora17552782010-12-02 12:23:14 +053083 printf("\tmin_microvolts=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +010084 reg_info[i].min_microvolts);
85 if (reg_info[i].max_microvolts)
Amit Arora17552782010-12-02 12:23:14 +053086 printf("\tmax_microvolts=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +010087 reg_info[i].max_microvolts);
Amit Arora17552782010-12-02 12:23:14 +053088
Daniel Lezcanof45321e2011-03-26 22:05:52 +010089 if (reg_info[i].microamps)
Amit Arora17552782010-12-02 12:23:14 +053090 printf("\tmicroamps=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +010091 reg_info[i].microamps);
92 if (reg_info[i].min_microamps)
Amit Arora17552782010-12-02 12:23:14 +053093 printf("\tmin_microamps=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +010094 reg_info[i].min_microamps);
95 if (reg_info[i].max_microamps)
Amit Arora17552782010-12-02 12:23:14 +053096 printf("\tmax_microamps=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +010097 reg_info[i].max_microamps);
98 if (reg_info[i].requested_microamps)
Amit Arora17552782010-12-02 12:23:14 +053099 printf("\trequested_microamps=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +0100100 reg_info[i].requested_microamps);
Amit Arora17552782010-12-02 12:23:14 +0530101
Daniel Lezcanof45321e2011-03-26 22:05:52 +0100102 if (reg_info[i].num_users)
Amit Arora17552782010-12-02 12:23:14 +0530103 printf("\tnum_users=%d\n",
Daniel Lezcanof45321e2011-03-26 22:05:52 +0100104 reg_info[i].num_users);
Amit Arora17552782010-12-02 12:23:14 +0530105 printf("\n");
106 }
107
108 if (!numregulators && verbose) {
109 printf("Could not find regulator information!");
110 printf(" Looks like /sys/class/regulator is empty.\n\n");
111 }
Amit Arora422c52f2010-12-02 16:22:29 +0530112
113 printf("\n\n");
Amit Arora17552782010-12-02 12:23:14 +0530114}
115
Daniel Lezcanof28e4882011-03-26 22:05:50 +0100116static void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
Amit Arora17552782010-12-02 12:23:14 +0530117{
118 if (!strcmp(ritem->d_name, "name"))
119 strcpy(regulators_info[idx].name, str);
120 if (!strcmp(ritem->d_name, "state"))
121 strcpy(regulators_info[idx].state, str);
122 if (!strcmp(ritem->d_name, "status"))
123 strcpy(regulators_info[idx].status, str);
124
125 if (!strcmp(ritem->d_name, "type"))
126 strcpy(regulators_info[idx].type, str);
127 if (!strcmp(ritem->d_name, "opmode"))
128 strcpy(regulators_info[idx].opmode, str);
129
130 if (!strcmp(ritem->d_name, "microvolts"))
131 regulators_info[idx].microvolts = atoi(str);
132 if (!strcmp(ritem->d_name, "min_microvolts"))
133 regulators_info[idx].min_microvolts = atoi(str);
134 if (!strcmp(ritem->d_name, "max_microvolts"))
135 regulators_info[idx].max_microvolts = atoi(str);
136
137 if (!strcmp(ritem->d_name, "microamps"))
138 regulators_info[idx].microamps = atoi(str);
139 if (!strcmp(ritem->d_name, "min_microamps"))
140 regulators_info[idx].min_microamps = atoi(str);
141 if (!strcmp(ritem->d_name, "max_microamps"))
142 regulators_info[idx].max_microamps = atoi(str);
143 if (!strcmp(ritem->d_name, "requested_microamps"))
144 regulators_info[idx].requested_microamps = atoi(str);
145
146 if (!strcmp(ritem->d_name, "num_users"))
147 regulators_info[idx].num_users = atoi(str);
148}
149
Daniel Lezcanof28e4882011-03-26 22:05:50 +0100150int regulator_read_info(void)
Amit Arora17552782010-12-02 12:23:14 +0530151{
152 FILE *file = NULL;
153 DIR *regdir, *dir;
154 int len, count = 0, ret = 0;
155 char line[1024], filename[1024], *fptr;
156 struct dirent *item, *ritem;
157
158 regdir = opendir("/sys/class/regulator");
159 if (!regdir)
160 return(1);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600161 while ((item = readdir(regdir))) {
Amit Arora17552782010-12-02 12:23:14 +0530162 if (strlen(item->d_name) < 3)
163 continue;
164
165 if (strncmp(item->d_name, "regulator", 9))
166 continue;
167
168 len = sprintf(filename, "/sys/class/regulator/%s",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600169 item->d_name);
Amit Arora17552782010-12-02 12:23:14 +0530170
171 dir = opendir(filename);
172 if (!dir)
173 continue;
174 count++;
175
176 if (count > numregulators) {
177 ret = 1;
178 goto exit;
179 }
180
181 strcpy(regulators_info[count-1].name, item->d_name);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600182 while ((ritem = readdir(dir))) {
Amit Arora17552782010-12-02 12:23:14 +0530183 if (strlen(ritem->d_name) < 3)
184 continue;
185
186 sprintf(filename + len, "/%s", ritem->d_name);
187 file = fopen(filename, "r");
188 if (!file)
189 continue;
190 memset(line, 0, 1024);
191 fptr = fgets(line, 1024, file);
192 fclose(file);
193 if (!fptr)
194 continue;
195 read_info_from_dirent(ritem, fptr, count - 1);
196 }
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600197 exit:
Amit Arora17552782010-12-02 12:23:14 +0530198 closedir(dir);
199 if (ret)
200 break;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600201 }
Amit Arora17552782010-12-02 12:23:14 +0530202 closedir(regdir);
203
204 return ret;
205}