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
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
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Eric C Saxe <eric.saxe@sun.com>
25 * Aubrey Li <aubrey.li@intel.com>
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.
45 #define mW2W(value) ((value) / 1000)
47 typedef struct battery_state
{
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'
67 pt_battery_mod_lookup(void)
69 kstat_ctl_t
*kc
= kstat_open();
71 if (kstat_lookup(kc
, kstat_batt_mod
[1], 0, NULL
))
74 if (kstat_lookup(kc
, kstat_batt_mod
[2], 0, NULL
))
79 (void) kstat_close(kc
);
83 pt_battery_print(void)
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
);
113 pt_battery_stat_snapshot(void)
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,
132 (void) kstat_close(kc
);
136 (void) kstat_read(kc
, ksp
, NULL
);
137 knp
= kstat_data_lookup(ksp
, "bif_unit");
140 (void) kstat_close(kc
);
144 battery_state
.power_unit
= knp
->value
.ui32
;
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,
155 (void) kstat_close(kc
);
159 (void) kstat_read(kc
, ksp
, NULL
);
160 knp
= kstat_data_lookup(ksp
, "bst_rate");
163 (void) kstat_close(kc
);
167 if (knp
->value
.ui32
== 0xFFFFFFFF)
168 battery_state
.present_rate
= 0;
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,
182 (void) kstat_close(kc
);
186 (void) kstat_read(kc
, ksp
, NULL
);
187 knp
= kstat_data_lookup(ksp
, "bif_last_cap");
190 (void) kstat_close(kc
);
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,
204 (void) kstat_close(kc
);
208 (void) kstat_read(kc
, ksp
, NULL
);
209 knp
= kstat_data_lookup(ksp
, "bst_rem_cap");
212 (void) kstat_close(kc
);
216 battery_state
.remain_cap
= mW2W((double)(knp
->value
.ui32
));
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,
228 (void) kstat_close(kc
);
232 (void) kstat_read(kc
, ksp
, NULL
);
233 knp
= kstat_data_lookup(ksp
, "bst_state");
236 (void) kstat_close(kc
);
240 battery_state
.bst_state
= knp
->value
.ui32
;
242 (void) kstat_close(kc
);