/*-
* Copyright (c) 2007 Michael Lorenz
* 2008 Magnus Henoch
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* battery status */
int sc_flags;
int sc_oflags;
int sc_voltage;
int sc_charge;
int sc_max_charge;
int sc_warn;
int sc_low;
int sc_draw;
int sc_time;
uint32_t sc_timestamp;
};
/*
* we can have more than one instance but only the first one needs
* to report AC status
*/
sc->sc_have_ac = FALSE;
if (sc->sc_num == 0)
sc->sc_have_ac = TRUE;
printf(" addr %d: smart battery\n", sc->sc_num);
sc->sc_charge = 0;
sc->sc_max_charge = 0;
smartbat_update(sc, 1);
/* trigger a status update */
sc->sc_oflags = ~sc->sc_flags;
smartbat_setup_envsys(sc);
if (sc->sc_have_ac) {
memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
sc->sc_sm_acpower.smpsw_name = "AC Power";
sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
printf("%s: unable to register AC power status with " \
"sysmon\n",
device_xname(sc->sc_dev));
sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie,
smartbat_poll, sc);
}
}
static void
smartbat_setup_envsys(struct smartbat_softc *sc)
{
int i;
if (sysmon_envsys_register(sc->sc_ac_sme)) {
aprint_error("%s: unable to register AC with sysmon\n",
device_xname(sc->sc_dev));
sysmon_envsys_destroy(sc->sc_ac_sme);
}
}
for (i = 0; i < BAT_NSENSORS; i++) {
if (sysmon_envsys_sensor_attach(sc->sc_bat_sme,
&sc->sc_bat_sensor[i])) {
sysmon_envsys_destroy(sc->sc_bat_sme);
return;
}
}
if (sysmon_envsys_register(sc->sc_bat_sme)) {
aprint_error("%s: unable to register with sysmon\n",
device_xname(sc->sc_dev));
sysmon_envsys_destroy(sc->sc_bat_sme);
}
}
static void
smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
struct smartbat_softc *sc = sme->sme_cookie;
int which = edata->sensor, present, ch;
/*
* Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
* for a clear description of the PMU results.
*/
static int
smartbat_update(struct smartbat_softc *sc, int out)
{
int len;
uint8_t buf[16];
int8_t *sbuf = (int8_t *)buf;
uint8_t battery_number;
if (sc->sc_timestamp == time_second)
return 0;
sc->sc_timestamp = time_second;
/* sc_num starts from 0, but we need to start from 1 */
battery_number = sc->sc_num + 1;
len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
PMU_SMART_BATTERY_STATE,
1, &battery_number,
16, buf);
if (len < 0) {
DPRINTF("%s: couldn't get battery data\n",
device_xname(sc->sc_dev));
/* XXX: the return value is never checked */
return -1;
}
/* Now, buf[0] is the command number, which we already know.
That's why all indexes are off by one compared to
pm_battery_info_smart in pm_direct.c.
*/
sc->sc_flags = buf[2];
/* XXX: are these all valid for smart batteries? */
if (out) {
printf(" flags: %x", buf[2]);
if (buf[2] & PMU_PWR_AC_PRESENT)
printf(" AC");
if (buf[2] & PMU_PWR_BATT_CHARGING)
printf(" charging");
if (buf[2] & PMU_PWR_BATT_PRESENT)
printf(" present");
if (buf[2] & PMU_PWR_BATT_FULL)
printf(" full");
printf("\n");
}