8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / powertop / common / battery.c
blobcb95bcab84fa760146e28b009a87f53d1c6a6284
1 /*
2 * Copyright 2009, Intel Corporation
3 * Copyright 2009, Sun Microsystems, Inc
5 * This file is part of PowerTOP
7 * This program file is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program in a file named COPYING; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
22 * Authors:
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Eric C Saxe <eric.saxe@sun.com>
25 * Aubrey Li <aubrey.li@intel.com>
29 * GPL Disclaimer
31 * For the avoidance of doubt, except that if any license choice other
32 * than GPL or LGPL is available it will apply instead, Sun elects to
33 * use only the General Public License version 2 (GPLv2) at this time
34 * for any software where a choice of GPL license versions is made
35 * available with the language indicating that GPLv2 or any later
36 * version may be used, or where a choice of which version of the GPL
37 * is applied is otherwise unspecified.
40 #include <string.h>
41 #include <kstat.h>
42 #include <errno.h>
43 #include "powertop.h"
45 #define mW2W(value) ((value) / 1000)
47 typedef struct battery_state {
48 uint32_t exist;
49 uint32_t power_unit;
50 uint32_t bst_state;
51 double present_rate;
52 double remain_cap;
53 double last_cap;
54 } battery_state_t;
56 static char *kstat_batt_mod[3] = {NULL, "battery", "acpi_drv"};
57 static uint_t kstat_batt_idx;
58 static battery_state_t battery_state;
60 static int pt_battery_stat_snapshot(void);
63 * Checks if the kstat module for battery information is present and
64 * whether it's called 'battery' or 'acpi_drv'
66 void
67 pt_battery_mod_lookup(void)
69 kstat_ctl_t *kc = kstat_open();
71 if (kstat_lookup(kc, kstat_batt_mod[1], 0, NULL))
72 kstat_batt_idx = 1;
73 else
74 if (kstat_lookup(kc, kstat_batt_mod[2], 0, NULL))
75 kstat_batt_idx = 2;
76 else
77 kstat_batt_idx = 0;
79 (void) kstat_close(kc);
82 void
83 pt_battery_print(void)
85 int err;
87 (void) memset(&battery_state, 0, sizeof (battery_state_t));
90 * The return value of pt_battery_stat_snapshot() can be used for
91 * debug or to show/hide the acpi power line. We currently don't
92 * make the distinction of a system that runs only on AC and one
93 * that runs on battery but has no kstat battery info.
95 * We still display the estimate power usage for systems
96 * running on AC with a fully charged battery because some
97 * batteries may still consume power.
99 * If pt_battery_mod_lookup() didn't find a kstat battery module, don't
100 * bother trying to take the snapshot
102 if (kstat_batt_idx > 0) {
103 if ((err = pt_battery_stat_snapshot()) < 0)
104 pt_error("battery kstat not found (%d)\n", err);
107 pt_display_acpi_power(battery_state.exist, battery_state.present_rate,
108 battery_state.remain_cap, battery_state.last_cap,
109 battery_state.bst_state);
112 static int
113 pt_battery_stat_snapshot(void)
115 kstat_ctl_t *kc;
116 kstat_t *ksp;
117 kstat_named_t *knp;
119 kc = kstat_open();
122 * power unit:
123 * 0 - Capacity information is reported in [mWh] and
124 * charge/discharge rate information in [mW]
125 * 1 - Capacity information is reported in [mAh] and
126 * charge/discharge rate information in [mA].
128 ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
129 "battery BIF0");
131 if (ksp == NULL) {
132 (void) kstat_close(kc);
133 return (-1);
136 (void) kstat_read(kc, ksp, NULL);
137 knp = kstat_data_lookup(ksp, "bif_unit");
139 if (knp == NULL) {
140 (void) kstat_close(kc);
141 return (-1);
144 battery_state.power_unit = knp->value.ui32;
147 * Present rate:
148 * the power or current being supplied or accepted
149 * through the battery's terminal
151 ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
152 "battery BST0");
154 if (ksp == NULL) {
155 (void) kstat_close(kc);
156 return (-1);
159 (void) kstat_read(kc, ksp, NULL);
160 knp = kstat_data_lookup(ksp, "bst_rate");
162 if (knp == NULL) {
163 (void) kstat_close(kc);
164 return (-1);
167 if (knp->value.ui32 == 0xFFFFFFFF)
168 battery_state.present_rate = 0;
169 else {
170 battery_state.exist = 1;
171 battery_state.present_rate = mW2W((double)(knp->value.ui32));
175 * Last Full charge capacity:
176 * Predicted battery capacity when fully charged.
178 ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
179 "battery BIF0");
181 if (ksp == NULL) {
182 (void) kstat_close(kc);
183 return (-1);
186 (void) kstat_read(kc, ksp, NULL);
187 knp = kstat_data_lookup(ksp, "bif_last_cap");
189 if (knp == NULL) {
190 (void) kstat_close(kc);
191 return (-1);
194 battery_state.last_cap = mW2W((double)(knp->value.ui32));
197 * Remaining capacity:
198 * the estimated remaining battery capacity
200 ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
201 "battery BST0");
203 if (ksp == NULL) {
204 (void) kstat_close(kc);
205 return (-1);
208 (void) kstat_read(kc, ksp, NULL);
209 knp = kstat_data_lookup(ksp, "bst_rem_cap");
211 if (knp == NULL) {
212 (void) kstat_close(kc);
213 return (-1);
216 battery_state.remain_cap = mW2W((double)(knp->value.ui32));
219 * Battery State:
220 * Bit0 - 1 : discharging
221 * Bit1 - 1 : charging
222 * Bit2 - 1 : critical energy state
224 ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
225 "battery BST0");
227 if (ksp == NULL) {
228 (void) kstat_close(kc);
229 return (-1);
232 (void) kstat_read(kc, ksp, NULL);
233 knp = kstat_data_lookup(ksp, "bst_state");
235 if (knp == NULL) {
236 (void) kstat_close(kc);
237 return (-1);
240 battery_state.bst_state = knp->value.ui32;
242 (void) kstat_close(kc);
244 return (0);