blob: 60529edd4ccdd67a9ed3ab5f4d4c8bb5ff6279f6 [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 Lezcanofac6aea2011-03-26 22:05:58 +010056void regulator_print_info(struct regulator_info *reg_info, int nr_reg, 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
Daniel Lezcanofac6aea2011-03-26 22:05:58 +010063 for (i = 0; i < nr_reg; i++) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -060064 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
Daniel Lezcanofac6aea2011-03-26 22:05:58 +0100108 if (!nr_reg && verbose) {
Amit Arora17552782010-12-02 12:23:14 +0530109 printf("Could not find regulator information!");
Daniel Lezcanob8465412011-03-26 22:05:55 +0100110 printf(" Looks like %s is empty.\n\n", SYSFS_REGULATOR);
Amit Arora17552782010-12-02 12:23:14 +0530111 }
Amit Arora422c52f2010-12-02 16:22:29 +0530112
113 printf("\n\n");
Amit Arora17552782010-12-02 12:23:14 +0530114}
115
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100116static void read_info_from_dirent(struct regulator_info *reg_info,
117 struct dirent *ritem, char *str, int idx)
Amit Arora17552782010-12-02 12:23:14 +0530118{
119 if (!strcmp(ritem->d_name, "name"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100120 strcpy(reg_info[idx].name, str);
Amit Arora17552782010-12-02 12:23:14 +0530121 if (!strcmp(ritem->d_name, "state"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100122 strcpy(reg_info[idx].state, str);
Amit Arora17552782010-12-02 12:23:14 +0530123 if (!strcmp(ritem->d_name, "status"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100124 strcpy(reg_info[idx].status, str);
Amit Arora17552782010-12-02 12:23:14 +0530125
126 if (!strcmp(ritem->d_name, "type"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100127 strcpy(reg_info[idx].type, str);
Amit Arora17552782010-12-02 12:23:14 +0530128 if (!strcmp(ritem->d_name, "opmode"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100129 strcpy(reg_info[idx].opmode, str);
Amit Arora17552782010-12-02 12:23:14 +0530130
131 if (!strcmp(ritem->d_name, "microvolts"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100132 reg_info[idx].microvolts = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530133 if (!strcmp(ritem->d_name, "min_microvolts"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100134 reg_info[idx].min_microvolts = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530135 if (!strcmp(ritem->d_name, "max_microvolts"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100136 reg_info[idx].max_microvolts = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530137
138 if (!strcmp(ritem->d_name, "microamps"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100139 reg_info[idx].microamps = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530140 if (!strcmp(ritem->d_name, "min_microamps"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100141 reg_info[idx].min_microamps = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530142 if (!strcmp(ritem->d_name, "max_microamps"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100143 reg_info[idx].max_microamps = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530144 if (!strcmp(ritem->d_name, "requested_microamps"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100145 reg_info[idx].requested_microamps = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530146
147 if (!strcmp(ritem->d_name, "num_users"))
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100148 reg_info[idx].num_users = atoi(str);
Amit Arora17552782010-12-02 12:23:14 +0530149}
150
Daniel Lezcano408580e2011-03-26 22:05:59 +0100151int regulator_read_info(struct regulator_info *reg_info, int nr_reg)
Amit Arora17552782010-12-02 12:23:14 +0530152{
153 FILE *file = NULL;
154 DIR *regdir, *dir;
155 int len, count = 0, ret = 0;
156 char line[1024], filename[1024], *fptr;
157 struct dirent *item, *ritem;
158
Daniel Lezcanob8465412011-03-26 22:05:55 +0100159 regdir = opendir(SYSFS_REGULATOR);
Amit Arora17552782010-12-02 12:23:14 +0530160 if (!regdir)
161 return(1);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600162 while ((item = readdir(regdir))) {
Amit Arora17552782010-12-02 12:23:14 +0530163 if (strlen(item->d_name) < 3)
164 continue;
165
166 if (strncmp(item->d_name, "regulator", 9))
167 continue;
168
Daniel Lezcanob8465412011-03-26 22:05:55 +0100169 len = sprintf(filename, "%s/%s", SYSFS_REGULATOR, item->d_name);
Amit Arora17552782010-12-02 12:23:14 +0530170
171 dir = opendir(filename);
172 if (!dir)
173 continue;
174 count++;
175
Daniel Lezcano408580e2011-03-26 22:05:59 +0100176 if (count > nr_reg) {
Amit Arora17552782010-12-02 12:23:14 +0530177 ret = 1;
178 goto exit;
179 }
180
Daniel Lezcanob8465412011-03-26 22:05:55 +0100181 strcpy(reg_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;
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100195
Daniel Lezcanob8465412011-03-26 22:05:55 +0100196 read_info_from_dirent(reg_info, ritem,
Daniel Lezcano7d62f9f2011-03-26 22:05:54 +0100197 fptr, count - 1);
Amit Arora17552782010-12-02 12:23:14 +0530198 }
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600199 exit:
Amit Arora17552782010-12-02 12:23:14 +0530200 closedir(dir);
201 if (ret)
202 break;
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600203 }
Amit Arora17552782010-12-02 12:23:14 +0530204 closedir(regdir);
205
206 return ret;
207}