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 | |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame^] | 18 | #define SYSFS_REGULATOR "/sys/class/regulator" |
| 19 | |
Daniel Lezcano | cac52d9 | 2011-03-26 22:05:49 +0100 | [diff] [blame] | 20 | int regulator_init(void) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 21 | { |
| 22 | DIR *regdir; |
| 23 | struct dirent *item; |
| 24 | |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame^] | 25 | regdir = opendir(SYSFS_REGULATOR); |
| 26 | if (!regdir) { |
| 27 | fprintf(stderr, "failed to open '%s': %m\n", SYSFS_REGULATOR); |
| 28 | return -1; |
| 29 | } |
| 30 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 31 | while ((item = readdir(regdir))) { |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame^] | 32 | |
| 33 | if (!strcmp(item->d_name, ".")) |
| 34 | continue; |
| 35 | |
| 36 | if (!strcmp(item->d_name, "..")) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 37 | continue; |
| 38 | |
| 39 | numregulators++; |
| 40 | } |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame^] | 41 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 42 | closedir(regdir); |
| 43 | |
| 44 | regulators_info = (struct regulator_info *)malloc(numregulators* |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 45 | sizeof(struct regulator_info)); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 46 | if (!regulators_info) { |
| 47 | fprintf(stderr, "init_regulator_ds: Not enough memory to " |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 48 | "read information for %d regulators!\n", numregulators); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 49 | return(1); |
| 50 | } |
| 51 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 52 | return(0); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 55 | static void print_string_val(char *name, char *val) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 56 | { |
| 57 | printf("\t%s=%s", name, val); |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 58 | if (!strchr(val, '\n')) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 59 | printf("\n"); |
| 60 | } |
| 61 | |
Daniel Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 62 | void regulator_print_info(int verbose) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 63 | { |
| 64 | int i; |
| 65 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 66 | printf("\nRegulator Information:\n"); |
| 67 | printf("*********************\n\n"); |
| 68 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 69 | for (i = 0; i < numregulators; i++) { |
| 70 | printf("Regulator %d:\n", i + 1); |
| 71 | print_string_val("name", regulators_info[i].name); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 72 | 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 Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 118 | |
| 119 | printf("\n\n"); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 120 | } |
| 121 | |
Daniel Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 122 | static void read_info_from_dirent(struct dirent *ritem, char *str, int idx) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 123 | { |
| 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 Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 156 | int regulator_read_info(void) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 157 | { |
| 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 167 | while ((item = readdir(regdir))) { |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 168 | 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 175 | item->d_name); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 176 | |
| 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 188 | while ((ritem = readdir(dir))) { |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 189 | 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 203 | exit: |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 204 | closedir(dir); |
| 205 | if (ret) |
| 206 | break; |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 207 | } |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 208 | closedir(regdir); |
| 209 | |
| 210 | return ret; |
| 211 | } |