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 |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 14 | * Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation) |
| 15 | * - rewrote code and API based on the tree |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 16 | *******************************************************************************/ |
| 17 | |
| 18 | #include "regulator.h" |
| 19 | |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 20 | #define SYSFS_REGULATOR "/sys/class/regulator" |
Daniel Lezcano | 1562748 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 21 | #define VALUE_MAX 16 |
| 22 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 23 | #define _GNU_SOURCE |
| 24 | #include <stdio.h> |
| 25 | #undef _GNU_SOURCE |
| 26 | #include <sys/types.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <dirent.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
Daniel Lezcano | b301b08 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 31 | #include "display.h" |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 32 | #include "powerdebug.h" |
| 33 | #include "tree.h" |
| 34 | #include "utils.h" |
Daniel Lezcano | 2e6ecca | 2011-03-26 22:05:51 +0100 | [diff] [blame] | 35 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 36 | struct regulator_info { |
| 37 | char name[NAME_MAX]; |
| 38 | char state[VALUE_MAX]; |
| 39 | char status[VALUE_MAX]; |
| 40 | char type[VALUE_MAX]; |
| 41 | char opmode[VALUE_MAX]; |
| 42 | int microvolts; |
| 43 | int min_microvolts; |
| 44 | int max_microvolts; |
| 45 | int microamps; |
| 46 | int min_microamps; |
| 47 | int max_microamps; |
| 48 | int requested_microamps; |
| 49 | int num_users; |
| 50 | }; |
| 51 | |
| 52 | struct regulator_data { |
| 53 | const char *name; |
| 54 | const char *ifmt; |
| 55 | const char *ofmt; |
| 56 | bool derefme; |
| 57 | }; |
| 58 | |
| 59 | static struct regulator_data regdata[] = { |
| 60 | { "name", "%s", "\tname: %s\n" }, |
| 61 | { "status", "%s", "\tstatus: %s\n" }, |
| 62 | { "state", "%s", "\tstate: %s\n" }, |
| 63 | { "type", "%s", "\ttype: %s\n" }, |
| 64 | { "num_users", "%d", "\tnum_users: %d\n", true }, |
| 65 | { "microvolts", "%d", "\tmicrovolts: %d\n", true }, |
| 66 | { "max_microvolts", "%d", "\tmax_microvolts: %d\n", true }, |
| 67 | { "min_microvolts", "%d", "\tmin_microvolts: %d\n", true }, |
| 68 | }; |
| 69 | |
| 70 | static struct tree *reg_tree; |
| 71 | |
| 72 | static struct regulator_info *regulator_alloc(void) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 73 | { |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 74 | struct regulator_info *regi; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 75 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 76 | regi = malloc(sizeof(*regi)); |
| 77 | if (regi) |
| 78 | memset(regi, 0, sizeof(*regi)); |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame] | 79 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 80 | return regi; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 83 | static int regulator_dump_cb(struct tree *tree, void *data) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 84 | { |
| 85 | int i; |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 86 | char buffer[NAME_MAX]; |
| 87 | size_t nregdata = sizeof(regdata) / sizeof(regdata[0]); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 88 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 89 | if (!strncmp("regulator.", tree->name, strlen("regulator."))) |
| 90 | printf("\n%s:\n", tree->name); |
| 91 | |
| 92 | for (i = 0; i < nregdata; i++) { |
Sanjay Singh Rawat | 9fe0c05 | 2013-02-22 17:57:18 +0530 | [diff] [blame] | 93 | int val; |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 94 | |
| 95 | if (file_read_value(tree->path, regdata[i].name, |
| 96 | regdata[i].ifmt, buffer)) |
| 97 | continue; |
| 98 | |
Sanjay Singh Rawat | 9fe0c05 | 2013-02-22 17:57:18 +0530 | [diff] [blame] | 99 | if (regdata[i].derefme) { |
| 100 | val = atoi(buffer); |
| 101 | printf(regdata[i].ofmt, val); |
| 102 | } else |
| 103 | printf(regdata[i].ofmt, buffer); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int regulator_dump(void) |
| 110 | { |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 111 | printf("\nRegulator Information:\n"); |
| 112 | printf("*********************\n\n"); |
| 113 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 114 | return tree_for_each(reg_tree, regulator_dump_cb, NULL); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 115 | } |
| 116 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 117 | static int regulator_display_cb(struct tree *t, void *data) |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 118 | { |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 119 | struct regulator_info *reg = t->private; |
| 120 | int *line = data; |
| 121 | char *buf; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 122 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 123 | /* we skip the root node of the tree */ |
| 124 | if (!t->parent) |
| 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 (!strlen(reg->name)) |
| 128 | return 0; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 129 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 130 | if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11d %-11d %-11d %-12d", |
| 131 | reg->name, reg->status, reg->state, reg->type, |
| 132 | reg->num_users, reg->microvolts, reg->min_microvolts, |
| 133 | reg->max_microvolts) < 0) |
| 134 | return -1; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 135 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 136 | display_print_line(REGULATOR, *line, buf, reg->num_users, t); |
| 137 | |
| 138 | (*line)++; |
| 139 | |
| 140 | free(buf); |
| 141 | |
| 142 | return 0; |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 143 | } |
| 144 | |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 145 | static int regulator_print_header(void) |
| 146 | { |
| 147 | char *buf; |
| 148 | int ret; |
| 149 | |
| 150 | if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s", |
| 151 | "Name", "Status", "State", "Type", "Users", "Microvolts", |
| 152 | "Min u-volts", "Max u-volts") < 0) |
| 153 | return -1; |
| 154 | |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 155 | ret = display_column_name(buf); |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 156 | |
| 157 | free(buf); |
| 158 | |
| 159 | return ret; |
| 160 | |
| 161 | } |
| 162 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 163 | static int regulator_filter_cb(const char *name) |
| 164 | { |
| 165 | /* let's ignore some directories in order to avoid to be |
| 166 | * pulled inside the sysfs circular symlinks mess/hell |
| 167 | * (choose the word which fit better) |
| 168 | */ |
| 169 | if (!strcmp(name, "device")) |
| 170 | return 1; |
| 171 | |
| 172 | if (!strcmp(name, "subsystem")) |
| 173 | return 1; |
| 174 | |
| 175 | if (!strcmp(name, "driver")) |
| 176 | return 1; |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static inline int read_regulator_cb(struct tree *t, void *data) |
| 182 | { |
| 183 | struct regulator_info *reg = t->private; |
| 184 | |
| 185 | file_read_value(t->path, "name", "%s", reg->name); |
| 186 | file_read_value(t->path, "state", "%s", reg->state); |
| 187 | file_read_value(t->path, "status", "%s", reg->status); |
| 188 | file_read_value(t->path, "type", "%s", reg->type); |
| 189 | file_read_value(t->path, "opmode", "%s", reg->opmode); |
| 190 | file_read_value(t->path, "num_users", "%d", ®->num_users); |
| 191 | file_read_value(t->path, "microvolts", "%d", ®->microvolts); |
| 192 | file_read_value(t->path, "min_microvolts", "%d", ®->min_microvolts); |
| 193 | file_read_value(t->path, "max_microvolts", "%d", ®->max_microvolts); |
| 194 | file_read_value(t->path, "microamps", "%d", ®->microamps); |
| 195 | file_read_value(t->path, "min_microamps", "%d", ®->min_microamps); |
| 196 | file_read_value(t->path, "max_microamps", "%d", ®->max_microamps); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Shaojie Sun | c1462e7 | 2013-08-20 20:35:15 +0800 | [diff] [blame^] | 201 | static int read_regulator_info(struct tree *tree) |
| 202 | { |
| 203 | return tree_for_each(tree, read_regulator_cb, NULL); |
| 204 | } |
| 205 | |
| 206 | static int regulator_print_info(struct tree *tree) |
| 207 | { |
| 208 | int ret, line = 0; |
| 209 | |
| 210 | display_reset_cursor(REGULATOR); |
| 211 | |
| 212 | regulator_print_header(); |
| 213 | |
| 214 | ret = tree_for_each(tree, regulator_display_cb, &line); |
| 215 | |
| 216 | display_refresh_pad(REGULATOR); |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static int regulator_display(bool refresh) |
| 222 | { |
| 223 | if (refresh && read_regulator_info(reg_tree)) |
| 224 | return -1; |
| 225 | |
| 226 | return regulator_print_info(reg_tree); |
| 227 | } |
| 228 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 229 | static int fill_regulator_cb(struct tree *t, void *data) |
| 230 | { |
| 231 | struct regulator_info *reg; |
| 232 | |
| 233 | reg = regulator_alloc(); |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 234 | if (!reg) { |
| 235 | printf("error: unable to allocate memory for regulator\n"); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 236 | return -1; |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 237 | } |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 238 | t->private = reg; |
| 239 | |
| 240 | /* we skip the root node but we set it expanded for its children */ |
| 241 | if (!t->parent) |
| 242 | return 0; |
| 243 | |
| 244 | return read_regulator_cb(t, data); |
| 245 | } |
| 246 | |
| 247 | static int fill_regulator_tree(void) |
| 248 | { |
| 249 | return tree_for_each(reg_tree, fill_regulator_cb, NULL); |
| 250 | } |
| 251 | |
Daniel Lezcano | b301b08 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 252 | static struct display_ops regulator_ops = { |
| 253 | .display = regulator_display, |
| 254 | }; |
| 255 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 256 | int regulator_init(void) |
| 257 | { |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 258 | reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, false); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 259 | if (!reg_tree) |
| 260 | return -1; |
| 261 | |
Daniel Lezcano | caafece | 2011-06-27 22:59:17 +0200 | [diff] [blame] | 262 | if (fill_regulator_tree()) |
| 263 | return -1; |
| 264 | |
| 265 | return display_register(REGULATOR, ®ulator_ops); |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 266 | } |