Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Powerdebug : power debugging tool |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 3 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016, Linaro Limited. |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 5 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 6 | * Author: |
| 7 | * Daniel Lezcano <daniel.lezcano@linaro.org> |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 8 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public |
| 11 | * License as published by the Free Software Foundation; either |
| 12 | * version 2.1 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 23 | #define SYSFS_REGULATOR "/sys/class/regulator" |
Daniel Lezcano | 1562748 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 24 | #define VALUE_MAX 16 |
| 25 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 26 | #define _GNU_SOURCE |
| 27 | #include <stdio.h> |
| 28 | #undef _GNU_SOURCE |
| 29 | #include <sys/types.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <dirent.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
Sanjay Singh Rawat | 03fdbc0 | 2014-05-27 16:52:27 +0530 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
Daniel Lezcano | b301b08 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 36 | #include "display.h" |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 37 | #include "powerdebug.h" |
| 38 | #include "tree.h" |
| 39 | #include "utils.h" |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 40 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 41 | struct regulator_info { |
| 42 | char name[NAME_MAX]; |
| 43 | char state[VALUE_MAX]; |
| 44 | char status[VALUE_MAX]; |
| 45 | char type[VALUE_MAX]; |
| 46 | char opmode[VALUE_MAX]; |
| 47 | int microvolts; |
| 48 | int min_microvolts; |
| 49 | int max_microvolts; |
| 50 | int microamps; |
| 51 | int min_microamps; |
| 52 | int max_microamps; |
| 53 | int requested_microamps; |
| 54 | int num_users; |
| 55 | }; |
| 56 | |
| 57 | struct regulator_data { |
| 58 | const char *name; |
| 59 | const char *ifmt; |
| 60 | const char *ofmt; |
| 61 | bool derefme; |
| 62 | }; |
| 63 | |
| 64 | static struct regulator_data regdata[] = { |
| 65 | { "name", "%s", "\tname: %s\n" }, |
| 66 | { "status", "%s", "\tstatus: %s\n" }, |
| 67 | { "state", "%s", "\tstate: %s\n" }, |
| 68 | { "type", "%s", "\ttype: %s\n" }, |
| 69 | { "num_users", "%d", "\tnum_users: %d\n", true }, |
| 70 | { "microvolts", "%d", "\tmicrovolts: %d\n", true }, |
| 71 | { "max_microvolts", "%d", "\tmax_microvolts: %d\n", true }, |
| 72 | { "min_microvolts", "%d", "\tmin_microvolts: %d\n", true }, |
| 73 | }; |
| 74 | |
| 75 | static struct tree *reg_tree; |
| 76 | |
| 77 | static struct regulator_info *regulator_alloc(void) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 78 | { |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 79 | struct regulator_info *regi; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 80 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 81 | regi = malloc(sizeof(*regi)); |
| 82 | if (regi) |
| 83 | memset(regi, 0, sizeof(*regi)); |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame] | 84 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 85 | return regi; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 86 | } |
| 87 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 88 | static int regulator_dump_cb(struct tree *tree, void *data) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 89 | { |
| 90 | int i; |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 91 | char buffer[NAME_MAX]; |
| 92 | size_t nregdata = sizeof(regdata) / sizeof(regdata[0]); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 93 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 94 | if (!strncmp("regulator.", tree->name, strlen("regulator."))) |
| 95 | printf("\n%s:\n", tree->name); |
| 96 | |
| 97 | for (i = 0; i < nregdata; i++) { |
Sanjay Singh Rawat | 9fe0c05 | 2013-02-22 17:57:18 +0530 | [diff] [blame] | 98 | int val; |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 99 | |
| 100 | if (file_read_value(tree->path, regdata[i].name, |
| 101 | regdata[i].ifmt, buffer)) |
| 102 | continue; |
| 103 | |
Sanjay Singh Rawat | 9fe0c05 | 2013-02-22 17:57:18 +0530 | [diff] [blame] | 104 | if (regdata[i].derefme) { |
| 105 | val = atoi(buffer); |
| 106 | printf(regdata[i].ofmt, val); |
| 107 | } else |
| 108 | printf(regdata[i].ofmt, buffer); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 114 | static int regulator_display_cb(struct tree *t, void *data) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 115 | { |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 116 | struct regulator_info *reg = t->private; |
| 117 | int *line = data; |
| 118 | char *buf; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 119 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 120 | /* we skip the root node of the tree */ |
| 121 | if (!t->parent) |
| 122 | return 0; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 123 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 124 | if (!strlen(reg->name)) |
| 125 | return 0; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 126 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 127 | if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11d %-11d %-11d %-12d", |
| 128 | reg->name, reg->status, reg->state, reg->type, |
| 129 | reg->num_users, reg->microvolts, reg->min_microvolts, |
| 130 | reg->max_microvolts) < 0) |
| 131 | return -1; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 132 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 133 | display_print_line(REGULATOR, *line, buf, reg->num_users, t); |
| 134 | |
| 135 | (*line)++; |
| 136 | |
| 137 | free(buf); |
| 138 | |
| 139 | return 0; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 142 | static int regulator_print_header(void) |
| 143 | { |
| 144 | char *buf; |
| 145 | int ret; |
| 146 | |
| 147 | if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s", |
| 148 | "Name", "Status", "State", "Type", "Users", "Microvolts", |
| 149 | "Min u-volts", "Max u-volts") < 0) |
| 150 | return -1; |
| 151 | |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 152 | ret = display_column_name(buf); |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 153 | |
| 154 | free(buf); |
| 155 | |
| 156 | return ret; |
| 157 | |
| 158 | } |
| 159 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 160 | static int regulator_filter_cb(const char *name) |
| 161 | { |
| 162 | /* let's ignore some directories in order to avoid to be |
| 163 | * pulled inside the sysfs circular symlinks mess/hell |
| 164 | * (choose the word which fit better) |
| 165 | */ |
| 166 | if (!strcmp(name, "device")) |
| 167 | return 1; |
| 168 | |
| 169 | if (!strcmp(name, "subsystem")) |
| 170 | return 1; |
| 171 | |
| 172 | if (!strcmp(name, "driver")) |
| 173 | return 1; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static inline int read_regulator_cb(struct tree *t, void *data) |
| 179 | { |
| 180 | struct regulator_info *reg = t->private; |
| 181 | |
| 182 | file_read_value(t->path, "name", "%s", reg->name); |
| 183 | file_read_value(t->path, "state", "%s", reg->state); |
| 184 | file_read_value(t->path, "status", "%s", reg->status); |
| 185 | file_read_value(t->path, "type", "%s", reg->type); |
| 186 | file_read_value(t->path, "opmode", "%s", reg->opmode); |
| 187 | file_read_value(t->path, "num_users", "%d", ®->num_users); |
| 188 | file_read_value(t->path, "microvolts", "%d", ®->microvolts); |
| 189 | file_read_value(t->path, "min_microvolts", "%d", ®->min_microvolts); |
| 190 | file_read_value(t->path, "max_microvolts", "%d", ®->max_microvolts); |
| 191 | file_read_value(t->path, "microamps", "%d", ®->microamps); |
| 192 | file_read_value(t->path, "min_microamps", "%d", ®->min_microamps); |
| 193 | file_read_value(t->path, "max_microamps", "%d", ®->max_microamps); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Shaojie Sun | c1462e7 | 2013-08-20 20:35:15 +0800 | [diff] [blame] | 198 | static int read_regulator_info(struct tree *tree) |
| 199 | { |
| 200 | return tree_for_each(tree, read_regulator_cb, NULL); |
| 201 | } |
| 202 | |
| 203 | static int regulator_print_info(struct tree *tree) |
| 204 | { |
| 205 | int ret, line = 0; |
| 206 | |
| 207 | display_reset_cursor(REGULATOR); |
| 208 | |
| 209 | regulator_print_header(); |
| 210 | |
| 211 | ret = tree_for_each(tree, regulator_display_cb, &line); |
| 212 | |
| 213 | display_refresh_pad(REGULATOR); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 218 | static int fill_regulator_cb(struct tree *t, void *data) |
| 219 | { |
| 220 | struct regulator_info *reg; |
| 221 | |
| 222 | reg = regulator_alloc(); |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 223 | if (!reg) { |
| 224 | printf("error: unable to allocate memory for regulator\n"); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 225 | return -1; |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 226 | } |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 227 | t->private = reg; |
| 228 | |
| 229 | /* we skip the root node but we set it expanded for its children */ |
| 230 | if (!t->parent) |
| 231 | return 0; |
| 232 | |
| 233 | return read_regulator_cb(t, data); |
| 234 | } |
| 235 | |
| 236 | static int fill_regulator_tree(void) |
| 237 | { |
| 238 | return tree_for_each(reg_tree, fill_regulator_cb, NULL); |
| 239 | } |
| 240 | |
Daniel Lezcano | d42d7ad | 2016-02-22 15:02:57 +0100 | [diff] [blame] | 241 | static int regulator_info_load(void) |
| 242 | { |
| 243 | if (reg_tree) |
| 244 | return 0; |
| 245 | |
| 246 | reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, false); |
| 247 | if (!reg_tree) |
| 248 | return -1; |
| 249 | |
| 250 | if (fill_regulator_tree()) |
| 251 | return -1; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Thara Gopinath | d78818c | 2017-07-14 13:15:45 -0400 | [diff] [blame] | 256 | int regulator_dump(void) |
| 257 | { |
| 258 | |
| 259 | if (!reg_tree) { |
| 260 | reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, |
| 261 | false); |
| 262 | if (!reg_tree) { |
| 263 | printf("Failed to load regulator tree\n"); |
| 264 | return -1; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | printf("\nRegulator Information:\n"); |
| 269 | printf("*********************\n\n"); |
| 270 | |
| 271 | return tree_for_each(reg_tree, regulator_dump_cb, NULL); |
| 272 | } |
| 273 | |
Daniel Lezcano | d42d7ad | 2016-02-22 15:02:57 +0100 | [diff] [blame] | 274 | static int regulator_display(bool refresh) |
| 275 | { |
| 276 | if (regulator_info_load()) { |
| 277 | display_print_error(REGULATOR, 0, "Failed to read regulator info"); |
| 278 | return 0; /* we don't want this to be a critical error */ |
| 279 | } |
| 280 | |
| 281 | if (refresh && read_regulator_info(reg_tree)) |
| 282 | return -1; |
| 283 | |
| 284 | return regulator_print_info(reg_tree); |
| 285 | } |
| 286 | |
Daniel Lezcano | b301b08 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 287 | static struct display_ops regulator_ops = { |
| 288 | .display = regulator_display, |
| 289 | }; |
| 290 | |
Daniel Lezcano | b4eec7e | 2016-02-18 16:44:55 +0000 | [diff] [blame] | 291 | int regulator_init(struct powerdebug_options *options) |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 292 | { |
Daniel Lezcano | b4eec7e | 2016-02-18 16:44:55 +0000 | [diff] [blame] | 293 | if (!(options->flags & REGULATOR_OPTION)) |
| 294 | return 0; |
| 295 | |
Daniel Lezcano | 1eedd48 | 2016-02-22 11:05:51 +0000 | [diff] [blame] | 296 | return display_register(REGULATOR, ®ulator_ops); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 297 | } |