Home | History | Annotate | Line # | Download | only in dev
battery.c revision 1.5
      1 /*	$NetBSD: battery.c,v 1.5 2007/07/16 18:24:29 xtraeme Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  *    contributors may be used to endorse or promote products derived
     17  *    from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: battery.c,v 1.5 2007/07/16 18:24:29 xtraeme Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/proc.h>
     40 
     41 #include <dev/sysmon/sysmonvar.h>
     42 #include <dev/sysmon/sysmon_taskq.h>
     43 
     44 #include <macppc/dev/pmuvar.h>
     45 #include <macppc/dev/batteryvar.h>
     46 #include <machine/bus.h>
     47 #include "opt_battery.h"
     48 
     49 #ifdef BATTERY_DEBUG
     50 #define DPRINTF printf
     51 #else
     52 #define DPRINTF while (0) printf
     53 #endif
     54 
     55 #define BTYPE_COMET	1
     56 #define BTYPE_HOOPER	2
     57 
     58 #define BAT_CPU_TEMPERATURE	0
     59 #define BAT_AC_PRESENT		1
     60 #define BAT_PRESENT		2
     61 #define BAT_VOLTAGE		3
     62 #define BAT_CURRENT		4
     63 #define BAT_MAX_CHARGE		5
     64 #define BAT_CHARGE		6
     65 #define BAT_CHARGING		7
     66 #define BAT_FULL		8
     67 #define BAT_TEMPERATURE		9
     68 #define BAT_NSENSORS		10  /* number of sensors */
     69 
     70 struct battery_softc {
     71 	struct device sc_dev;
     72 	struct pmu_ops *sc_pmu_ops;
     73 	int sc_type;
     74 
     75 	/* envsys stuff */
     76 	struct sysmon_envsys sc_sysmon;
     77 	envsys_data_t sc_sensor[BAT_NSENSORS];
     78 	struct sysmon_pswitch sc_sm_acpower;
     79 
     80 	/* battery status */
     81 	int sc_flags;
     82 	int sc_oflags;
     83 	int sc_voltage;
     84 	int sc_charge;
     85 	int sc_current;
     86 	int sc_time;
     87 	int sc_cpu_temp;
     88 	int sc_bat_temp;
     89 	int sc_vmax_charged;
     90 	int sc_vmax_charging;
     91 	uint32_t sc_timestamp;
     92 };
     93 
     94 static void battery_attach(struct device *, struct device *, void *);
     95 static int battery_match(struct device *, struct cfdata *, void *);
     96 static int battery_update(struct battery_softc *, int);
     97 static void battery_setup_envsys(struct battery_softc *);
     98 static int battery_gtredata(struct sysmon_envsys *, envsys_data_t *);
     99 static void battery_poll(void *);
    100 
    101 CFATTACH_DECL(battery, sizeof(struct battery_softc),
    102     battery_match, battery_attach, NULL, NULL);
    103 
    104 static int
    105 battery_match(struct device *parent, struct cfdata *cf, void *aux)
    106 {
    107 	struct battery_attach_args *baa = aux;
    108 
    109 	if (baa->baa_type == BATTERY_TYPE_LEGACY)
    110 		return 1;
    111 
    112 	return 0;
    113 }
    114 
    115 static void
    116 battery_attach(struct device *parent, struct device *self, void *aux)
    117 {
    118 	struct battery_attach_args *baa = aux;
    119 	struct battery_softc *sc = (struct battery_softc *)self;
    120 	uint32_t reg;
    121 
    122 	sc->sc_pmu_ops = baa->baa_pmu_ops;
    123 	printf(": legacy battery ");
    124 
    125 	reg = in32rb(0xf3000034);
    126 	DPRINTF("reg: %08x\n", reg);
    127 	if (reg & 0x20000000) {
    128 		sc->sc_type = BTYPE_HOOPER;
    129 		sc->sc_vmax_charged = 330;
    130 		sc->sc_vmax_charging = 365;
    131 		printf("[hooper]\n");
    132 	} else {
    133 		sc->sc_type = BTYPE_COMET;
    134 		sc->sc_vmax_charged = 189;
    135 		sc->sc_vmax_charging = 213;
    136 		printf("[comet]\n");
    137 	}
    138 	battery_update(sc, 1);
    139 	/* trigger a status update */
    140 	sc->sc_oflags = ~sc->sc_flags;
    141 
    142 	battery_setup_envsys(sc);
    143 	sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie, battery_poll,
    144 	    sc);
    145 
    146 	memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    147 	sc->sc_sm_acpower.smpsw_name = "AC Power";
    148 	sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    149 	if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    150 		printf("%s: unable to register AC power status with sysmon\n",
    151 		    sc->sc_dev.dv_xname);
    152 
    153 }
    154 
    155 static int
    156 battery_update(struct battery_softc *sc, int out)
    157 {
    158 	int len, vmax, pcharge, vb;
    159 	uint8_t buf[16];
    160 
    161 	if (sc->sc_timestamp == time_second)
    162 		return 0;
    163 	sc->sc_timestamp = time_second;
    164 
    165 	len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
    166 	    PMU_BATTERY_STATE, 0, NULL, 16, buf);
    167 	if (len != 9)
    168 		return -1;
    169 
    170 	sc->sc_flags = buf[1];
    171 
    172 	if (out) {
    173 		if (buf[1] & PMU_PWR_AC_PRESENT)
    174 			printf(" AC");
    175 		if (buf[1] & PMU_PWR_BATT_CHARGING)
    176 			printf(" charging");
    177 		if (buf[1] & PMU_PWR_BATT_PRESENT)
    178 			printf(" present");
    179 		if (buf[1] & PMU_PWR_BATT_FULL)
    180 			printf(" full");
    181 		printf("\n");
    182 	}
    183 
    184 	sc->sc_cpu_temp = buf[4];
    185 
    186 	if ((sc->sc_flags & PMU_PWR_BATT_PRESENT) == 0) {
    187 		sc->sc_voltage = 0;
    188 		sc->sc_current = 0;
    189 		sc->sc_bat_temp = 0;
    190 		sc->sc_charge = 0;
    191 		sc->sc_time = 0;
    192 		return 0;
    193 	}
    194 
    195 	vmax = sc->sc_vmax_charged;
    196 	vb = (buf[2] << 8) | buf[3];
    197 	sc->sc_voltage = (vb * 265 + 72665) / 10;
    198 	sc->sc_current = buf[6];
    199 	if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == 0) {
    200 	    	if (sc->sc_current > 200)
    201 			vb += ((sc->sc_current - 200) * 15) / 100;
    202 	} else {
    203 		vmax = sc->sc_vmax_charging;
    204 	}
    205 	sc->sc_charge = (100 * vb) / vmax;
    206 	if (sc->sc_flags & PMU_PWR_PCHARGE_RESET) {
    207 		pcharge = (buf[7] << 8) | buf[8];
    208 		if (pcharge > 6500)
    209 			pcharge = 6500;
    210 		pcharge = 100 - pcharge * 100 / 6500;
    211 		if (pcharge < sc->sc_charge)
    212 			sc->sc_charge = pcharge;
    213 	}
    214 	if (sc->sc_current > 0) {
    215 		sc->sc_time = (sc->sc_charge * 16440) / sc->sc_current;
    216 	} else
    217 		sc->sc_time = 0;
    218 
    219 	sc->sc_bat_temp = buf[5];
    220 
    221 	if (out) {
    222 		printf("voltage: %d.%03d\n", sc->sc_voltage / 1000,
    223 		    sc->sc_voltage % 1000);
    224 		printf("charge:  %d%%\n", sc->sc_charge);
    225 		if (sc->sc_time > 0)
    226 			printf("time:    %d:%02d\n", sc->sc_time / 60,
    227 			    sc->sc_time % 60);
    228 	}
    229 
    230 	return 0;
    231 }
    232 
    233 #define INITDATA(index, unit, string)					\
    234 	sc->sc_sensor[index].sensor = index;				\
    235 	sc->sc_sensor[index].units = unit;     				\
    236 	sc->sc_sensor[index].state = ENVSYS_SVALID;			\
    237 	snprintf(sc->sc_sensor[index].desc,				\
    238 	    sizeof(sc->sc_sensor[index].desc), "%s", string);
    239 
    240 static void
    241 battery_setup_envsys(struct battery_softc *sc)
    242 {
    243 
    244 	INITDATA(BAT_CPU_TEMPERATURE, ENVSYS_STEMP, "CPU temperature");
    245 	INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present");
    246 	INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
    247 	INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
    248 	INITDATA(BAT_CHARGE, ENVSYS_INTEGER, "Battery charge");
    249 	INITDATA(BAT_MAX_CHARGE, ENVSYS_INTEGER, "Battery design cap");
    250 	INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
    251 	INITDATA(BAT_TEMPERATURE, ENVSYS_STEMP, "Battery temperature");
    252 	INITDATA(BAT_CHARGING, ENVSYS_INDICATOR, "Battery charging");
    253 	INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
    254 #undef INITDATA
    255 
    256 	sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
    257 	sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
    258 	sc->sc_sysmon.sme_cookie = sc;
    259 	sc->sc_sysmon.sme_gtredata = battery_gtredata;
    260 	sc->sc_sysmon.sme_nsensors = BAT_NSENSORS;
    261 
    262 	if (sysmon_envsys_register(&sc->sc_sysmon))
    263 		aprint_error("%s: unable to register with sysmon\n",
    264 		    sc->sc_dev.dv_xname);
    265 }
    266 
    267 static int
    268 battery_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
    269 {
    270 	struct battery_softc *sc = sme->sme_cookie;
    271 	int which = edata->sensor;
    272 
    273 	battery_update(sc, 0);
    274 
    275 	switch (which) {
    276 	case BAT_CPU_TEMPERATURE:
    277 		edata->value_cur = sc->sc_cpu_temp * 1000000 + 273150000;
    278 		break;
    279 	case BAT_AC_PRESENT:
    280 		edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
    281 		break;
    282 	case BAT_VOLTAGE:
    283 		edata->value_cur = sc->sc_voltage * 1000;
    284 		break;
    285 	case BAT_CURRENT:
    286 		edata->value_cur = sc->sc_current * 1000;
    287 		break;
    288 	case BAT_CHARGE:
    289 		edata->value_cur = sc->sc_charge;
    290 		break;
    291 	case BAT_MAX_CHARGE:
    292 		edata->value_cur = 100;
    293 		break;
    294 	case BAT_TEMPERATURE:
    295 		edata->value_cur = sc->sc_bat_temp * 1000000 + 273150000;
    296 		break;
    297 	case BAT_CHARGING:
    298 		if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
    299 		    (sc->sc_flags & PMU_PWR_AC_PRESENT))
    300 			edata->value_cur = 1;
    301 		else
    302 			edata->value_cur = 0;
    303 
    304 		break;
    305 	case BAT_FULL:
    306 		edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
    307 		break;
    308 	}
    309 
    310 	edata->state = ENVSYS_SVALID;
    311 	return 0;
    312 }
    313 
    314 static void
    315 battery_poll(void *cookie)
    316 {
    317 	struct battery_softc *sc = cookie;
    318 
    319 	battery_update(sc, 0);
    320 	if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
    321 		return;
    322 
    323 	sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
    324 
    325 	sysmon_pswitch_event(&sc->sc_sm_acpower,
    326 	    sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
    327 	    PSWITCH_EVENT_RELEASED);
    328 }
    329