Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: smartbat.c,v 1.14 2012/11/01 15:54:28 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.14 2012/11/01 15:54:28 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_warn;
     86 	int sc_low;
     87 	int sc_draw;
     88 	int sc_time;
     89 	uint32_t sc_timestamp;
     90 };
     91 
     92 static void smartbat_attach(device_t, device_t, void *);
     93 static int smartbat_match(device_t, cfdata_t, void *);
     94 static void smartbat_setup_envsys(struct smartbat_softc *);
     95 static void smartbat_refresh(struct sysmon_envsys *, envsys_data_t *);
     96 static void smartbat_get_limits(struct sysmon_envsys *, envsys_data_t *,
     97     sysmon_envsys_lim_t *, uint32_t *);
     98 static void smartbat_refresh_ac(struct sysmon_envsys *, envsys_data_t *);
     99 static void smartbat_poll(void *);
    100 static int smartbat_update(struct smartbat_softc *, int);
    101 
    102 CFATTACH_DECL_NEW(smartbat, sizeof(struct smartbat_softc),
    103     smartbat_match, smartbat_attach, NULL, NULL);
    104 
    105 static int
    106 smartbat_match(device_t parent, cfdata_t cf, void *aux)
    107 {
    108 	struct battery_attach_args *baa = aux;
    109 
    110 	if (baa->baa_type == BATTERY_TYPE_SMART)
    111 		return 1;
    112 
    113 	return 0;
    114 }
    115 
    116 static void
    117 smartbat_attach(device_t parent, device_t self, void *aux)
    118 {
    119 	struct battery_attach_args *baa = aux;
    120 	struct smartbat_softc *sc = device_private(self);
    121 
    122 	sc->sc_dev = self;
    123 	sc->sc_pmu_ops = baa->baa_pmu_ops;
    124 	sc->sc_num = baa->baa_num;
    125 
    126 	/*
    127 	 * we can have more than one instance but only the first one needs
    128 	 * to report AC status
    129 	 */
    130 	sc->sc_have_ac = FALSE;
    131 	if (sc->sc_num == 0)
    132 		sc->sc_have_ac = TRUE;
    133 
    134 	printf(" addr %d: smart battery\n", sc->sc_num);
    135 
    136 	sc->sc_charge = 0;
    137 	sc->sc_max_charge = 0;
    138 	smartbat_update(sc, 1);
    139 	/* trigger a status update */
    140 	sc->sc_oflags = ~sc->sc_flags;
    141 
    142 	smartbat_setup_envsys(sc);
    143 
    144 	if (sc->sc_have_ac) {
    145 		memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    146 		sc->sc_sm_acpower.smpsw_name = "AC Power";
    147 		sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    148 		if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    149 			printf("%s: unable to register AC power status with " \
    150 			       "sysmon\n",
    151 			    device_xname(sc->sc_dev));
    152 		sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie,
    153 		    smartbat_poll, sc);
    154 	}
    155 }
    156 
    157 
    158 static void
    159 smartbat_setup_envsys(struct smartbat_softc *sc)
    160 {
    161 	int i;
    162 
    163 	if (sc->sc_have_ac) {
    164 
    165 #define INITDATA(index, unit, string)					\
    166 	sc->sc_ac_sensor[index].units = unit;     			\
    167 	sc->sc_ac_sensor[index].state = ENVSYS_SINVALID;		\
    168 	snprintf(sc->sc_ac_sensor[index].desc,				\
    169 	    sizeof(sc->sc_ac_sensor[index].desc), "%s", string);
    170 
    171 		INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "connected");
    172 #undef INITDATA
    173 
    174 		sc->sc_ac_sme = sysmon_envsys_create();
    175 
    176 		if (sysmon_envsys_sensor_attach(sc->sc_ac_sme,
    177 					&sc->sc_ac_sensor[0])) {
    178 			sysmon_envsys_destroy(sc->sc_ac_sme);
    179 			return;
    180 		}
    181 
    182 		sc->sc_ac_sme->sme_name = "AC Adaptor";
    183 		sc->sc_ac_sme->sme_cookie = sc;
    184 		sc->sc_ac_sme->sme_refresh = smartbat_refresh_ac;
    185 		sc->sc_ac_sme->sme_class = SME_CLASS_ACADAPTER;
    186 
    187 		if (sysmon_envsys_register(sc->sc_ac_sme)) {
    188 			aprint_error("%s: unable to register AC with sysmon\n",
    189 			    device_xname(sc->sc_dev));
    190 			sysmon_envsys_destroy(sc->sc_ac_sme);
    191 		}
    192 	}
    193 
    194 	sc->sc_bat_sme = sysmon_envsys_create();
    195 
    196 #define INITDATA(index, unit, string)					\
    197 	sc->sc_bat_sensor[index].units = unit;     			\
    198 	sc->sc_bat_sensor[index].state = ENVSYS_SINVALID;		\
    199 	snprintf(sc->sc_bat_sensor[index].desc,				\
    200 	    sizeof(sc->sc_bat_sensor[index].desc), "%s", string);
    201 
    202 	INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
    203 	INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
    204 	INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
    205 	INITDATA(BAT_MAX_CHARGE, ENVSYS_SWATTHOUR, "Battery design cap");
    206 	INITDATA(BAT_CHARGE, ENVSYS_SWATTHOUR, "Battery charge");
    207 	INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
    208 	INITDATA(BAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY,
    209 	    "Battery charge state");
    210 	INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
    211 #undef INITDATA
    212 
    213 	sc->sc_bat_sensor[BAT_CHARGE_STATE].value_cur =
    214 	    ENVSYS_BATTERY_CAPACITY_NORMAL;
    215 	sc->sc_bat_sensor[BAT_CHARGE_STATE].state = ENVSYS_SVALID;
    216 	sc->sc_bat_sensor[BAT_CHARGING].value_cur = TRUE;
    217 	sc->sc_bat_sensor[BAT_CHARGING].state = ENVSYS_SVALID;
    218 	sc->sc_bat_sensor[BAT_CHARGING].value_cur = TRUE;
    219 	sc->sc_bat_sensor[BAT_CHARGING].state = ENVSYS_SVALID;
    220 
    221 	for (i = 0; i < BAT_NSENSORS; i++)
    222 		sc->sc_bat_sensor[i].flags = ENVSYS_FMONNOTSUPP;
    223 
    224 	sc->sc_bat_sensor[BAT_CHARGE].flags =
    225 	    ENVSYS_FMONLIMITS | ENVSYS_FPERCENT | ENVSYS_FVALID_MAX;
    226 	sc->sc_bat_sensor[BAT_CHARGE_STATE].flags = ENVSYS_FMONSTCHANGED;
    227 
    228 	for (i = 0; i < BAT_NSENSORS; i++) {
    229 		if (sysmon_envsys_sensor_attach(sc->sc_bat_sme,
    230 						&sc->sc_bat_sensor[i])) {
    231 			sysmon_envsys_destroy(sc->sc_bat_sme);
    232 			return;
    233 		}
    234 	}
    235 
    236 	sc->sc_low  = sc->sc_max_charge * 1000 / 100 * 10; /* 10% */
    237 	sc->sc_warn = sc->sc_max_charge * 1000 / 100 * 20; /* 20% */
    238 
    239 
    240 	sc->sc_bat_sme->sme_name = device_xname(sc->sc_dev);
    241 	sc->sc_bat_sme->sme_cookie = sc;
    242 	sc->sc_bat_sme->sme_refresh = smartbat_refresh;
    243 	sc->sc_bat_sme->sme_class = SME_CLASS_BATTERY;
    244 	sc->sc_bat_sme->sme_flags = SME_POLL_ONLY | SME_INIT_REFRESH;
    245 	sc->sc_bat_sme->sme_get_limits = smartbat_get_limits;
    246 
    247 	if (sysmon_envsys_register(sc->sc_bat_sme)) {
    248 		aprint_error("%s: unable to register with sysmon\n",
    249 		    device_xname(sc->sc_dev));
    250 		sysmon_envsys_destroy(sc->sc_bat_sme);
    251 	}
    252 }
    253 
    254 static void
    255 smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    256 {
    257 	struct smartbat_softc *sc = sme->sme_cookie;
    258 	int which = edata->sensor, present, ch;
    259 
    260 	smartbat_update(sc, 0);
    261 	present = (sc->sc_flags & PMU_PWR_BATT_PRESENT) != 0;
    262 	ch = sc->sc_charge * 100 / sc->sc_max_charge;
    263 
    264 	if (present) {
    265 		edata->state = ENVSYS_SVALID;
    266 		switch (which) {
    267 		case BAT_PRESENT:
    268 			edata->value_cur = present;
    269 			break;
    270 		case BAT_VOLTAGE:
    271 			edata->value_cur = sc->sc_voltage * 1000;
    272 			break;
    273 		case BAT_CURRENT:
    274 			edata->value_cur = sc->sc_draw * 1000;
    275 			break;
    276 		case BAT_MAX_CHARGE:
    277 			edata->value_cur = sc->sc_max_charge * 1000;
    278 			break;
    279 		case BAT_CHARGE:
    280 			edata->value_cur = sc->sc_charge * 1000;
    281 			edata->value_max = sc->sc_max_charge * 1000;
    282 			if (ch < 6) {
    283 				edata->state = ENVSYS_SCRITICAL;
    284 			} else if (edata->value_cur < sc->sc_low) {
    285 				edata->state = ENVSYS_SCRITUNDER;
    286 			} else if (edata->value_cur < sc->sc_warn) {
    287 				edata->state = ENVSYS_SWARNUNDER;
    288 			}
    289 			break;
    290 		case BAT_CHARGING:
    291 			if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
    292 			    (sc->sc_flags & PMU_PWR_AC_PRESENT))
    293 				edata->value_cur = 1;
    294 			else
    295 				edata->value_cur = 0;
    296 			break;
    297 		case BAT_CHARGE_STATE:
    298 			{
    299 				int chr = sc->sc_charge * 1000;
    300 
    301 				if (ch < 6) {
    302 					edata->value_cur =
    303 					    ENVSYS_BATTERY_CAPACITY_CRITICAL;
    304 				} else if (chr < sc->sc_low) {
    305 					edata->value_cur =
    306 					    ENVSYS_BATTERY_CAPACITY_LOW;
    307 				} else if (chr < sc->sc_warn) {
    308 					edata->value_cur =
    309 					    ENVSYS_BATTERY_CAPACITY_WARNING;
    310 				} else {
    311 					edata->value_cur =
    312 					    ENVSYS_BATTERY_CAPACITY_NORMAL;
    313 				}
    314 			}
    315 			break;
    316 		case BAT_FULL:
    317 			edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
    318 			break;
    319 		}
    320 	} else {
    321 		/* battery isn't there */
    322 		switch (which) {
    323 		case BAT_PRESENT:
    324 			edata->value_cur = present;
    325 			edata->state = ENVSYS_SVALID;
    326 			break;
    327 		case BAT_CHARGE_STATE:
    328 			/*
    329 			 * envsys crashes if this isn't a valid value even
    330 			 * when the sensor itself is invalid
    331 			 */
    332 			edata->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
    333 			edata->state = ENVSYS_SINVALID;
    334 			break;
    335 		default:
    336 			edata->state = ENVSYS_SINVALID;
    337 			edata->value_cur = 0;
    338 		}
    339 	}
    340 }
    341 
    342 static void
    343 smartbat_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    344     sysmon_envsys_lim_t *limits, uint32_t *props)
    345 {
    346 	struct smartbat_softc *sc = sme->sme_cookie;
    347 
    348 	if (edata->sensor != BAT_CHARGE)
    349 		return;
    350 
    351 	limits->sel_critmin = sc->sc_low;
    352 	limits->sel_warnmin = sc->sc_warn;
    353 
    354 	*props |= PROP_BATTCAP | PROP_BATTWARN | PROP_DRIVER_LIMITS;
    355 }
    356 
    357 static void
    358 smartbat_refresh_ac(struct sysmon_envsys *sme, envsys_data_t *edata)
    359 {
    360 	struct smartbat_softc *sc = sme->sme_cookie;
    361 	int which = edata->sensor;
    362 
    363 	smartbat_update(sc, 0);
    364 	switch (which) {
    365 		case BAT_AC_PRESENT:
    366 			edata->value_cur =
    367 			    ((sc->sc_flags & PMU_PWR_AC_PRESENT) != 0);
    368 			edata->state = ENVSYS_SVALID;
    369 			break;
    370 		default:
    371 			edata->value_cur = 0;
    372 			edata->state = ENVSYS_SINVALID;
    373 	}
    374 }
    375 
    376 /*
    377  * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
    378  * for a clear description of the PMU results.
    379  */
    380 static int
    381 smartbat_update(struct smartbat_softc *sc, int out)
    382 {
    383 	int len;
    384 	uint8_t buf[16];
    385 	int8_t *sbuf = (int8_t *)buf;
    386 	uint8_t battery_number;
    387 
    388 	if (sc->sc_timestamp == time_second)
    389 		return 0;
    390 	sc->sc_timestamp = time_second;
    391 
    392 	/* sc_num starts from 0, but we need to start from 1 */
    393 	battery_number = sc->sc_num + 1;
    394 	len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
    395 					 PMU_SMART_BATTERY_STATE,
    396 					 1, &battery_number,
    397 					 16, buf);
    398 
    399 	if (len < 0) {
    400 		DPRINTF("%s: couldn't get battery data\n",
    401 		    device_xname(sc->sc_dev));
    402 		/* XXX: the return value is never checked */
    403 		return -1;
    404 	}
    405 
    406 	/* Now, buf[0] is the command number, which we already know.
    407 	   That's why all indexes are off by one compared to
    408 	   pm_battery_info_smart in pm_direct.c.
    409 	*/
    410 	sc->sc_flags = buf[2];
    411 
    412         /* XXX: are these all valid for smart batteries? */
    413 	if (out) {
    414 		printf(" flags: %x", buf[2]);
    415 		if (buf[2] & PMU_PWR_AC_PRESENT)
    416 			printf(" AC");
    417 		if (buf[2] & PMU_PWR_BATT_CHARGING)
    418 			printf(" charging");
    419 		if (buf[2] & PMU_PWR_BATT_PRESENT)
    420 			printf(" present");
    421 		if (buf[2] & PMU_PWR_BATT_FULL)
    422 			printf(" full");
    423 		printf("\n");
    424 	}
    425 
    426 	switch(buf[1]) {
    427 	case 3:
    428 	case 4:
    429 		sc->sc_charge = buf[3];
    430 		sc->sc_max_charge = buf[4];
    431 		sc->sc_draw = sbuf[5];
    432 		sc->sc_voltage = buf[6];
    433 		break;
    434 	case 5:
    435 		sc->sc_charge = ((buf[3] << 8) | (buf[4]));
    436 		sc->sc_max_charge = ((buf[5] << 8) | (buf[6]));
    437 		sc->sc_draw = sbuf[7];
    438 		sc->sc_voltage = ((buf[9] << 8) | (buf[8]));
    439 		break;
    440 	default:
    441 		/* XXX - Error condition */
    442 		DPRINTF("%s: why is buf[1] %x?\n", device_xname(sc->sc_dev),
    443 		   buf[1]);
    444 		sc->sc_charge = 0;
    445 		sc->sc_max_charge = 0;
    446 		sc->sc_draw = 0;
    447 		sc->sc_voltage = 0;
    448 		break;
    449 	}
    450 
    451 	return 1;
    452 }
    453 
    454 static void
    455 smartbat_poll(void *cookie)
    456 {
    457 	struct smartbat_softc *sc = cookie;
    458 
    459 	smartbat_update(sc, 0);
    460 	if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
    461 		return;
    462 
    463 	sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
    464 	sc->sc_ac_sensor[0].value_cur = sc->sc_oflags ? 1 : 0;
    465 	sysmon_pswitch_event(&sc->sc_sm_acpower,
    466 	    sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
    467 	    PSWITCH_EVENT_RELEASED);
    468 }
    469