Home | History | Annotate | Line # | Download | only in dev
smartbat.c revision 1.13
      1 /*	$NetBSD: smartbat.c,v 1.13 2012/10/31 05:46:49 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      5  *               2008 Magnus Henoch
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: smartbat.c,v 1.13 2012/10/31 05:46:49 macallan Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/device.h>
     37 #include <sys/proc.h>
     38 
     39 #include <dev/sysmon/sysmonvar.h>
     40 #include <dev/sysmon/sysmon_taskq.h>
     41 
     42 #include <macppc/dev/pmuvar.h>
     43 #include <macppc/dev/batteryvar.h>
     44 #include <sys/bus.h>
     45 #include "opt_battery.h"
     46 
     47 #ifdef SMARTBAT_DEBUG
     48 #define DPRINTF printf
     49 #define static /* static */
     50 #else
     51 #define DPRINTF while (0) printf
     52 #endif
     53 
     54 #define BAT_AC_PRESENT		0
     55 
     56 #define BAT_PRESENT		0
     57 #define BAT_VOLTAGE		1
     58 #define BAT_CURRENT		2
     59 #define BAT_MAX_CHARGE		3
     60 #define BAT_CHARGE		4
     61 #define BAT_CHARGING		5
     62 #define BAT_CHARGE_STATE	6
     63 #define BAT_FULL		7
     64 #define BAT_NSENSORS		8  /* number of sensors */
     65 
     66 struct smartbat_softc {
     67 	device_t sc_dev;
     68 	struct pmu_ops *sc_pmu_ops;
     69 	int sc_num;
     70 
     71 	/* envsys stuff */
     72 	struct sysmon_envsys *sc_bat_sme;
     73 	envsys_data_t sc_bat_sensor[BAT_NSENSORS];
     74 	struct sysmon_envsys *sc_ac_sme;
     75 	envsys_data_t sc_ac_sensor[1];
     76 	struct sysmon_pswitch sc_sm_acpower;
     77 	int sc_have_ac;
     78 
     79 	/* battery status */
     80 	int sc_flags;
     81 	int sc_oflags;
     82 	int sc_voltage;
     83 	int sc_charge;
     84 	int sc_max_charge;
     85 	int sc_draw;
     86 	int sc_time;
     87 	uint32_t sc_timestamp;
     88 };
     89 
     90 static void smartbat_attach(device_t, device_t, void *);
     91 static int smartbat_match(device_t, cfdata_t, void *);
     92 static void smartbat_setup_envsys(struct smartbat_softc *);
     93 static void smartbat_refresh(struct sysmon_envsys *, envsys_data_t *);
     94 static void smartbat_get_limits(struct sysmon_envsys *, envsys_data_t *,
     95     sysmon_envsys_lim_t *, uint32_t *);
     96 static void smartbat_refresh_ac(struct sysmon_envsys *, envsys_data_t *);
     97 static void smartbat_poll(void *);
     98 static int smartbat_update(struct smartbat_softc *, int);
     99 
    100 CFATTACH_DECL_NEW(smartbat, sizeof(struct smartbat_softc),
    101     smartbat_match, smartbat_attach, NULL, NULL);
    102 
    103 static int
    104 smartbat_match(device_t parent, cfdata_t cf, void *aux)
    105 {
    106 	struct battery_attach_args *baa = aux;
    107 
    108 	if (baa->baa_type == BATTERY_TYPE_SMART)
    109 		return 1;
    110 
    111 	return 0;
    112 }
    113 
    114 static void
    115 smartbat_attach(device_t parent, device_t self, void *aux)
    116 {
    117 	struct battery_attach_args *baa = aux;
    118 	struct smartbat_softc *sc = device_private(self);
    119 
    120 	sc->sc_dev = self;
    121 	sc->sc_pmu_ops = baa->baa_pmu_ops;
    122 	sc->sc_num = baa->baa_num;
    123 
    124 	/*
    125 	 * we can have more than one instance but only the first one needs
    126 	 * to report AC status
    127 	 */
    128 	sc->sc_have_ac = FALSE;
    129 	if (sc->sc_num == 0)
    130 		sc->sc_have_ac = TRUE;
    131 
    132 	printf(" addr %d: smart battery\n", sc->sc_num);
    133 
    134 	smartbat_update(sc, 1);
    135 	/* trigger a status update */
    136 	sc->sc_oflags = ~sc->sc_flags;
    137 
    138 	smartbat_setup_envsys(sc);
    139 
    140 	if (sc->sc_have_ac) {
    141 		memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    142 		sc->sc_sm_acpower.smpsw_name = "AC Power";
    143 		sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    144 		if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    145 			printf("%s: unable to register AC power status with " \
    146 			       "sysmon\n",
    147 			    device_xname(sc->sc_dev));
    148 		sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie,
    149 		    smartbat_poll, sc);
    150 	}
    151 }
    152 
    153 
    154 static void
    155 smartbat_setup_envsys(struct smartbat_softc *sc)
    156 {
    157 	int i;
    158 
    159 	if (sc->sc_have_ac) {
    160 
    161 #define INITDATA(index, unit, string)					\
    162 	sc->sc_ac_sensor[index].units = unit;     			\
    163 	sc->sc_ac_sensor[index].state = ENVSYS_SINVALID;		\
    164 	snprintf(sc->sc_ac_sensor[index].desc,				\
    165 	    sizeof(sc->sc_ac_sensor[index].desc), "%s", string);
    166 
    167 		INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "connected");
    168 #undef INITDATA
    169 
    170 		sc->sc_ac_sme = sysmon_envsys_create();
    171 
    172 		if (sysmon_envsys_sensor_attach(sc->sc_ac_sme,
    173 					&sc->sc_ac_sensor[0])) {
    174 			sysmon_envsys_destroy(sc->sc_ac_sme);
    175 			return;
    176 		}
    177 
    178 		sc->sc_ac_sme->sme_name = "AC Adaptor";
    179 		sc->sc_ac_sme->sme_cookie = sc;
    180 		sc->sc_ac_sme->sme_refresh = smartbat_refresh_ac;
    181 		sc->sc_ac_sme->sme_class = SME_CLASS_ACADAPTER;
    182 
    183 		if (sysmon_envsys_register(sc->sc_ac_sme)) {
    184 			aprint_error("%s: unable to register AC with sysmon\n",
    185 			    device_xname(sc->sc_dev));
    186 			sysmon_envsys_destroy(sc->sc_ac_sme);
    187 		}
    188 	}
    189 
    190 	sc->sc_bat_sme = sysmon_envsys_create();
    191 
    192 #define INITDATA(index, unit, string)					\
    193 	sc->sc_bat_sensor[index].units = unit;     			\
    194 	sc->sc_bat_sensor[index].state = ENVSYS_SINVALID;		\
    195 	snprintf(sc->sc_bat_sensor[index].desc,				\
    196 	    sizeof(sc->sc_bat_sensor[index].desc), "%s", string);
    197 
    198 	INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
    199 	INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
    200 	INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
    201 	INITDATA(BAT_MAX_CHARGE, ENVSYS_SWATTHOUR, "Battery design cap");
    202 	INITDATA(BAT_CHARGE, ENVSYS_SWATTHOUR, "Battery charge");
    203 	INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
    204 	INITDATA(BAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY,
    205 	    "Battery charge state");
    206 	INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
    207 #undef INITDATA
    208 
    209 	sc->sc_bat_sensor[BAT_CHARGE_STATE].value_cur =
    210 	    ENVSYS_BATTERY_CAPACITY_NORMAL;
    211 	sc->sc_bat_sensor[BAT_CHARGE_STATE].state = ENVSYS_SVALID;
    212 	sc->sc_bat_sensor[BAT_CHARGING].value_cur = TRUE;
    213 	sc->sc_bat_sensor[BAT_CHARGING].state = ENVSYS_SVALID;
    214 	sc->sc_bat_sensor[BAT_CHARGING].value_cur = TRUE;
    215 	sc->sc_bat_sensor[BAT_CHARGING].state = ENVSYS_SVALID;
    216 
    217 	for (i = 0; i < BAT_NSENSORS; i++)
    218 		sc->sc_bat_sensor[i].flags = ENVSYS_FMONNOTSUPP;
    219 
    220 	sc->sc_bat_sensor[BAT_CHARGE].flags =
    221 	    ENVSYS_FMONLIMITS | ENVSYS_FPERCENT | ENVSYS_FVALID_MAX;
    222 	sc->sc_bat_sensor[BAT_CHARGE_STATE].flags = ENVSYS_FMONSTCHANGED;
    223 
    224 	for (i = 0; i < BAT_NSENSORS; i++) {
    225 		if (sysmon_envsys_sensor_attach(sc->sc_bat_sme,
    226 						&sc->sc_bat_sensor[i])) {
    227 			sysmon_envsys_destroy(sc->sc_bat_sme);
    228 			return;
    229 		}
    230 	}
    231 
    232 	sc->sc_bat_sme->sme_name = device_xname(sc->sc_dev);
    233 	sc->sc_bat_sme->sme_cookie = sc;
    234 	sc->sc_bat_sme->sme_refresh = smartbat_refresh;
    235 	sc->sc_bat_sme->sme_class = SME_CLASS_BATTERY;
    236 	sc->sc_bat_sme->sme_flags = SME_POLL_ONLY | SME_INIT_REFRESH;
    237 	sc->sc_bat_sme->sme_get_limits = smartbat_get_limits;
    238 
    239 	if (sysmon_envsys_register(sc->sc_bat_sme)) {
    240 		aprint_error("%s: unable to register with sysmon\n",
    241 		    device_xname(sc->sc_dev));
    242 		sysmon_envsys_destroy(sc->sc_bat_sme);
    243 	}
    244 }
    245 
    246 static void
    247 smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    248 {
    249 	struct smartbat_softc *sc = sme->sme_cookie;
    250 	int which = edata->sensor, present, ch;
    251 
    252 	smartbat_update(sc, 0);
    253 	present = (sc->sc_flags & PMU_PWR_BATT_PRESENT) != 0;
    254 	ch = sc->sc_charge * 100 / sc->sc_max_charge;
    255 
    256 	if (present) {
    257 		edata->state = ENVSYS_SVALID;
    258 		switch (which) {
    259 		case BAT_PRESENT:
    260 			edata->value_cur = present;
    261 			break;
    262 		case BAT_VOLTAGE:
    263 			edata->value_cur = sc->sc_voltage * 1000;
    264 			break;
    265 		case BAT_CURRENT:
    266 			edata->value_cur = sc->sc_draw * 1000;
    267 			break;
    268 		case BAT_MAX_CHARGE:
    269 			edata->value_cur = sc->sc_max_charge * 1000;
    270 			break;
    271 		case BAT_CHARGE:
    272 			edata->value_cur = sc->sc_charge * 1000;
    273 			edata->value_max = sc->sc_max_charge * 1000;
    274 			if (ch < 6) {
    275 				edata->state = ENVSYS_SCRITICAL;
    276 			} else if (ch < 11) {
    277 				edata->state = ENVSYS_SCRITUNDER;
    278 			} else if (ch < 20) {
    279 				edata->state = ENVSYS_SWARNUNDER;
    280 			}
    281 			break;
    282 		case BAT_CHARGING:
    283 			if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
    284 			    (sc->sc_flags & PMU_PWR_AC_PRESENT))
    285 				edata->value_cur = 1;
    286 			else
    287 				edata->value_cur = 0;
    288 			break;
    289 		case BAT_CHARGE_STATE:
    290 			{
    291 				if (ch < 6) {
    292 					edata->value_cur =
    293 					    ENVSYS_BATTERY_CAPACITY_CRITICAL;
    294 				} else if (ch < 10) {
    295 					edata->value_cur =
    296 					    ENVSYS_BATTERY_CAPACITY_LOW;
    297 				} else if (ch < 20) {
    298 					edata->value_cur =
    299 					    ENVSYS_BATTERY_CAPACITY_WARNING;
    300 				} else {
    301 					edata->value_cur =
    302 					    ENVSYS_BATTERY_CAPACITY_NORMAL;
    303 				}
    304 			}
    305 			break;
    306 		case BAT_FULL:
    307 			edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
    308 			break;
    309 		}
    310 	} else {
    311 		/* battery isn't there */
    312 		switch (which) {
    313 		case BAT_PRESENT:
    314 			edata->value_cur = present;
    315 			edata->state = ENVSYS_SVALID;
    316 			break;
    317 		case BAT_CHARGE_STATE:
    318 			/*
    319 			 * envsys crashes if this isn't a valid value even
    320 			 * when the sensor itself is invalid
    321 			 */
    322 			edata->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
    323 			edata->state = ENVSYS_SINVALID;
    324 			break;
    325 		default:
    326 			edata->state = ENVSYS_SINVALID;
    327 			edata->value_cur = 0;
    328 		}
    329 	}
    330 }
    331 
    332 static void
    333 smartbat_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    334     sysmon_envsys_lim_t *limits, uint32_t *props)
    335 {
    336 	struct smartbat_softc *sc = sme->sme_cookie;
    337 
    338 	if (edata->sensor != BAT_CHARGE)
    339 		return;
    340 
    341 	limits->sel_critmin = sc->sc_max_charge * 1000 / 100 * 10; /* 20% */
    342 	limits->sel_warnmin = sc->sc_max_charge * 1000 / 100 * 20; /* 10% */
    343 
    344 	*props |= PROP_BATTCAP | PROP_BATTWARN | PROP_DRIVER_LIMITS;
    345 }
    346 
    347 static void
    348 smartbat_refresh_ac(struct sysmon_envsys *sme, envsys_data_t *edata)
    349 {
    350 	struct smartbat_softc *sc = sme->sme_cookie;
    351 	int which = edata->sensor;
    352 
    353 	smartbat_update(sc, 0);
    354 	switch (which) {
    355 		case BAT_AC_PRESENT:
    356 			edata->value_cur =
    357 			    ((sc->sc_flags & PMU_PWR_AC_PRESENT) != 0);
    358 			edata->state = ENVSYS_SVALID;
    359 			break;
    360 		default:
    361 			edata->value_cur = 0;
    362 			edata->state = ENVSYS_SINVALID;
    363 	}
    364 }
    365 
    366 /*
    367  * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
    368  * for a clear description of the PMU results.
    369  */
    370 static int
    371 smartbat_update(struct smartbat_softc *sc, int out)
    372 {
    373 	int len;
    374 	uint8_t buf[16];
    375 	uint8_t battery_number;
    376 
    377 	if (sc->sc_timestamp == time_second)
    378 		return 0;
    379 	sc->sc_timestamp = time_second;
    380 
    381 	/* sc_num starts from 0, but we need to start from 1 */
    382 	battery_number = sc->sc_num + 1;
    383 	len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
    384 					 PMU_SMART_BATTERY_STATE,
    385 					 1, &battery_number,
    386 					 16, buf);
    387 
    388 	if (len < 0) {
    389 		DPRINTF("%s: couldn't get battery data\n",
    390 		    device_xname(sc->sc_dev));
    391 		/* XXX: the return value is never checked */
    392 		return -1;
    393 	}
    394 
    395 	/* Now, buf[0] is the command number, which we already know.
    396 	   That's why all indexes are off by one compared to
    397 	   pm_battery_info_smart in pm_direct.c.
    398 	*/
    399 	sc->sc_flags = buf[2];
    400 
    401         /* XXX: are these all valid for smart batteries? */
    402 	if (out) {
    403 		printf(" flags: %x", buf[2]);
    404 		if (buf[2] & PMU_PWR_AC_PRESENT)
    405 			printf(" AC");
    406 		if (buf[2] & PMU_PWR_BATT_CHARGING)
    407 			printf(" charging");
    408 		if (buf[2] & PMU_PWR_BATT_PRESENT)
    409 			printf(" present");
    410 		if (buf[2] & PMU_PWR_BATT_FULL)
    411 			printf(" full");
    412 		printf("\n");
    413 	}
    414 
    415 	switch(buf[1]) {
    416 	case 3:
    417 	case 4:
    418 		sc->sc_charge = buf[3];
    419 		sc->sc_max_charge = buf[4];
    420 		sc->sc_draw = *((signed char *)&buf[5]);
    421 		sc->sc_voltage = buf[6];
    422 		break;
    423 	case 5:
    424 		sc->sc_charge = ((buf[3] << 8) | (buf[4]));
    425 		sc->sc_max_charge = ((buf[5] << 8) | (buf[6]));
    426 		sc->sc_draw = *((signed short *)&buf[7]);
    427 		sc->sc_voltage = ((buf[9] << 8) | (buf[8]));
    428 		break;
    429 	default:
    430 		/* XXX - Error condition */
    431 		DPRINTF("%s: why is buf[1] %x?\n", device_xname(sc->sc_dev),
    432 		   buf[1]);
    433 		sc->sc_charge = 0;
    434 		sc->sc_max_charge = 0;
    435 		sc->sc_draw = 0;
    436 		sc->sc_voltage = 0;
    437 		break;
    438 	}
    439 
    440 	return 1;
    441 }
    442 
    443 static void
    444 smartbat_poll(void *cookie)
    445 {
    446 	struct smartbat_softc *sc = cookie;
    447 
    448 	smartbat_update(sc, 0);
    449 	if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
    450 		return;
    451 
    452 	sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
    453 	sc->sc_ac_sensor[0].value_cur = sc->sc_oflags ? 1 : 0;
    454 	sysmon_pswitch_event(&sc->sc_sm_acpower,
    455 	    sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
    456 	    PSWITCH_EVENT_RELEASED);
    457 }
    458