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