Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2010, Linaro |
| 3 | * Copyright (C) 2010, IBM Corporation |
| 4 | * |
| 5 | * This file is part of PowerDebug. |
| 6 | * |
| 7 | * All rights reserved. This program and the accompanying materials |
| 8 | * are made available under the terms of the Eclipse Public License v1.0 |
| 9 | * which accompanies this distribution, and is available at |
| 10 | * http://www.eclipse.org/legal/epl-v10.html |
| 11 | * |
| 12 | * Contributors: |
| 13 | * Amit Arora <amit.arora@linaro.org> (IBM Corporation) |
| 14 | * - initial API and implementation |
| 15 | *******************************************************************************/ |
| 16 | |
| 17 | #include "regulator.h" |
| 18 | |
| 19 | int init_regulator_ds(void) |
| 20 | { |
| 21 | DIR *regdir; |
| 22 | struct dirent *item; |
| 23 | |
| 24 | regdir = opendir("/sys/class/regulator"); |
| 25 | if (!regdir) |
| 26 | return(1); |
| 27 | while((item = readdir(regdir))) { |
| 28 | if (strncmp(item->d_name, "regulator", 9)) |
| 29 | continue; |
| 30 | |
| 31 | numregulators++; |
| 32 | } |
| 33 | closedir(regdir); |
| 34 | |
| 35 | regulators_info = (struct regulator_info *)malloc(numregulators* |
| 36 | sizeof(struct regulator_info)); |
| 37 | if (!regulators_info) { |
| 38 | fprintf(stderr, "init_regulator_ds: Not enough memory to " |
| 39 | "read information for %d regulators!\n", numregulators); |
| 40 | return(1); |
| 41 | } |
| 42 | |
| 43 | return(0); |
| 44 | } |
| 45 | |
| 46 | void print_string_val(char *name, char *val) |
| 47 | { |
| 48 | printf("\t%s=%s", name, val); |
| 49 | if(!strchr(val, '\n')) |
| 50 | printf("\n"); |
| 51 | } |
| 52 | |
| 53 | void print_regulator_info(int verbose) |
| 54 | { |
| 55 | int i; |
| 56 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame^] | 57 | printf("\nRegulator Information:\n"); |
| 58 | printf("*********************\n\n"); |
| 59 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 60 | for (i=0; i<numregulators; i++) { |
| 61 | printf("Regulator %d:\n", i+1); |
| 62 | print_string_val("name", regulators_info[i].name); |
| 63 | if (strcmp(regulators_info[i].status, "")) |
| 64 | print_string_val("status", regulators_info[i].status); |
| 65 | if (strcmp(regulators_info[i].state, "")) |
| 66 | print_string_val("state", regulators_info[i].state); |
| 67 | |
| 68 | if (!verbose) |
| 69 | continue; |
| 70 | |
| 71 | if (strcmp(regulators_info[i].type, "")) |
| 72 | print_string_val("type", regulators_info[i].type); |
| 73 | if (strcmp(regulators_info[i].opmode, "")) |
| 74 | print_string_val("opmode", regulators_info[i].opmode); |
| 75 | |
| 76 | if (regulators_info[i].microvolts) |
| 77 | printf("\tmicrovolts=%d\n", |
| 78 | regulators_info[i].microvolts); |
| 79 | if (regulators_info[i].min_microvolts) |
| 80 | printf("\tmin_microvolts=%d\n", |
| 81 | regulators_info[i].min_microvolts); |
| 82 | if (regulators_info[i].max_microvolts) |
| 83 | printf("\tmax_microvolts=%d\n", |
| 84 | regulators_info[i].max_microvolts); |
| 85 | |
| 86 | if (regulators_info[i].microamps) |
| 87 | printf("\tmicroamps=%d\n", |
| 88 | regulators_info[i].microamps); |
| 89 | if (regulators_info[i].min_microamps) |
| 90 | printf("\tmin_microamps=%d\n", |
| 91 | regulators_info[i].min_microamps); |
| 92 | if (regulators_info[i].max_microamps) |
| 93 | printf("\tmax_microamps=%d\n", |
| 94 | regulators_info[i].max_microamps); |
| 95 | if (regulators_info[i].requested_microamps) |
| 96 | printf("\trequested_microamps=%d\n", |
| 97 | regulators_info[i].requested_microamps); |
| 98 | |
| 99 | if (regulators_info[i].num_users) |
| 100 | printf("\tnum_users=%d\n", |
| 101 | regulators_info[i].num_users); |
| 102 | printf("\n"); |
| 103 | } |
| 104 | |
| 105 | if (!numregulators && verbose) { |
| 106 | printf("Could not find regulator information!"); |
| 107 | printf(" Looks like /sys/class/regulator is empty.\n\n"); |
| 108 | } |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame^] | 109 | |
| 110 | printf("\n\n"); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void read_info_from_dirent(struct dirent *ritem, char *str, int idx) |
| 114 | { |
| 115 | if (!strcmp(ritem->d_name, "name")) |
| 116 | strcpy(regulators_info[idx].name, str); |
| 117 | if (!strcmp(ritem->d_name, "state")) |
| 118 | strcpy(regulators_info[idx].state, str); |
| 119 | if (!strcmp(ritem->d_name, "status")) |
| 120 | strcpy(regulators_info[idx].status, str); |
| 121 | |
| 122 | if (!strcmp(ritem->d_name, "type")) |
| 123 | strcpy(regulators_info[idx].type, str); |
| 124 | if (!strcmp(ritem->d_name, "opmode")) |
| 125 | strcpy(regulators_info[idx].opmode, str); |
| 126 | |
| 127 | if (!strcmp(ritem->d_name, "microvolts")) |
| 128 | regulators_info[idx].microvolts = atoi(str); |
| 129 | if (!strcmp(ritem->d_name, "min_microvolts")) |
| 130 | regulators_info[idx].min_microvolts = atoi(str); |
| 131 | if (!strcmp(ritem->d_name, "max_microvolts")) |
| 132 | regulators_info[idx].max_microvolts = atoi(str); |
| 133 | |
| 134 | if (!strcmp(ritem->d_name, "microamps")) |
| 135 | regulators_info[idx].microamps = atoi(str); |
| 136 | if (!strcmp(ritem->d_name, "min_microamps")) |
| 137 | regulators_info[idx].min_microamps = atoi(str); |
| 138 | if (!strcmp(ritem->d_name, "max_microamps")) |
| 139 | regulators_info[idx].max_microamps = atoi(str); |
| 140 | if (!strcmp(ritem->d_name, "requested_microamps")) |
| 141 | regulators_info[idx].requested_microamps = atoi(str); |
| 142 | |
| 143 | if (!strcmp(ritem->d_name, "num_users")) |
| 144 | regulators_info[idx].num_users = atoi(str); |
| 145 | } |
| 146 | |
| 147 | int read_regulator_info(void) |
| 148 | { |
| 149 | FILE *file = NULL; |
| 150 | DIR *regdir, *dir; |
| 151 | int len, count = 0, ret = 0; |
| 152 | char line[1024], filename[1024], *fptr; |
| 153 | struct dirent *item, *ritem; |
| 154 | |
| 155 | regdir = opendir("/sys/class/regulator"); |
| 156 | if (!regdir) |
| 157 | return(1); |
| 158 | while((item = readdir(regdir))) { |
| 159 | if (strlen(item->d_name) < 3) |
| 160 | continue; |
| 161 | |
| 162 | if (strncmp(item->d_name, "regulator", 9)) |
| 163 | continue; |
| 164 | |
| 165 | len = sprintf(filename, "/sys/class/regulator/%s", |
| 166 | item->d_name); |
| 167 | |
| 168 | dir = opendir(filename); |
| 169 | if (!dir) |
| 170 | continue; |
| 171 | count++; |
| 172 | |
| 173 | if (count > numregulators) { |
| 174 | ret = 1; |
| 175 | goto exit; |
| 176 | } |
| 177 | |
| 178 | strcpy(regulators_info[count-1].name, item->d_name); |
| 179 | while((ritem = readdir(dir))) { |
| 180 | if (strlen(ritem->d_name) < 3) |
| 181 | continue; |
| 182 | |
| 183 | sprintf(filename + len, "/%s", ritem->d_name); |
| 184 | file = fopen(filename, "r"); |
| 185 | if (!file) |
| 186 | continue; |
| 187 | memset(line, 0, 1024); |
| 188 | fptr = fgets(line, 1024, file); |
| 189 | fclose(file); |
| 190 | if (!fptr) |
| 191 | continue; |
| 192 | read_info_from_dirent(ritem, fptr, count - 1); |
| 193 | } |
| 194 | exit: |
| 195 | closedir(dir); |
| 196 | if (ret) |
| 197 | break; |
| 198 | } |
| 199 | closedir(regdir); |
| 200 | |
| 201 | return ret; |
| 202 | } |