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 | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame^] | 20 | struct regulator_info *regulator_init(int *nr_regulators) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 21 | { |
| 22 | DIR *regdir; |
| 23 | struct dirent *item; |
| 24 | |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame^] | 25 | *nr_regulators = 0; |
| 26 | |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 27 | regdir = opendir(SYSFS_REGULATOR); |
| 28 | if (!regdir) { |
| 29 | fprintf(stderr, "failed to open '%s': %m\n", SYSFS_REGULATOR); |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame^] | 30 | return NULL; |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 33 | while ((item = readdir(regdir))) { |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 34 | |
| 35 | if (!strcmp(item->d_name, ".")) |
| 36 | continue; |
| 37 | |
| 38 | if (!strcmp(item->d_name, "..")) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 39 | continue; |
| 40 | |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame^] | 41 | (*nr_regulators)++; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 42 | } |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 43 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 44 | closedir(regdir); |
| 45 | |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame^] | 46 | return malloc(*nr_regulators * sizeof(struct regulator_info)); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 49 | static void print_string_val(char *name, char *val) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 50 | { |
| 51 | printf("\t%s=%s", name, val); |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 52 | if (!strchr(val, '\n')) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 53 | printf("\n"); |
| 54 | } |
| 55 | |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 56 | void regulator_print_info(struct regulator_info *reg_info, int verbose) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 57 | { |
| 58 | int i; |
| 59 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 60 | printf("\nRegulator Information:\n"); |
| 61 | printf("*********************\n\n"); |
| 62 | |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 63 | for (i = 0; i < numregulators; i++) { |
| 64 | printf("Regulator %d:\n", i + 1); |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 65 | 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 Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 70 | |
| 71 | if (!verbose) |
| 72 | continue; |
| 73 | |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 74 | 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 Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 78 | |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 79 | if (reg_info[i].microvolts) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 80 | printf("\tmicrovolts=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 81 | reg_info[i].microvolts); |
| 82 | if (reg_info[i].min_microvolts) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 83 | printf("\tmin_microvolts=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 84 | reg_info[i].min_microvolts); |
| 85 | if (reg_info[i].max_microvolts) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 86 | printf("\tmax_microvolts=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 87 | reg_info[i].max_microvolts); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 88 | |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 89 | if (reg_info[i].microamps) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 90 | printf("\tmicroamps=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 91 | reg_info[i].microamps); |
| 92 | if (reg_info[i].min_microamps) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 93 | printf("\tmin_microamps=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 94 | reg_info[i].min_microamps); |
| 95 | if (reg_info[i].max_microamps) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 96 | printf("\tmax_microamps=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 97 | reg_info[i].max_microamps); |
| 98 | if (reg_info[i].requested_microamps) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 99 | printf("\trequested_microamps=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 100 | reg_info[i].requested_microamps); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 101 | |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 102 | if (reg_info[i].num_users) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 103 | printf("\tnum_users=%d\n", |
Daniel Lezcano | f45321e | 2011-03-26 22:05:52 +0100 | [diff] [blame] | 104 | reg_info[i].num_users); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 105 | 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 Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 112 | |
| 113 | printf("\n\n"); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 114 | } |
| 115 | |
Daniel Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 116 | 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] | 117 | { |
| 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 Lezcano | f28e488 | 2011-03-26 22:05:50 +0100 | [diff] [blame] | 150 | int regulator_read_info(void) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 151 | { |
| 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 161 | while ((item = readdir(regdir))) { |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 162 | 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 169 | item->d_name); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 170 | |
| 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 182 | while ((ritem = readdir(dir))) { |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 183 | 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 Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 197 | exit: |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 198 | closedir(dir); |
| 199 | if (ret) |
| 200 | break; |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 201 | } |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 202 | closedir(regdir); |
| 203 | |
| 204 | return ret; |
| 205 | } |