Home | History | Annotate | Line # | Download | only in dev
battery.c revision 1.1
      1 /*	$NetBSD: battery.c,v 1.1 2007/02/15 01:48:40 macallan 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.1 2007/02/15 01:48:40 macallan 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_DISCHARGING		8
     67 #define BAT_FULL		9
     68 #define BAT_TEMPERATURE		10
     69 #define BAT_NSENSORS		11  /* number of sensors */
     70 
     71 struct battery_softc {
     72 	struct device sc_dev;
     73 	struct pmu_ops *sc_pmu_ops;
     74 	int sc_type;
     75 
     76 	/* envsys stuff */
     77 	struct sysmon_envsys sc_sysmon;
     78 	struct envsys_basic_info sc_sinfo[BAT_NSENSORS];
     79 	struct envsys_tre_data sc_sdata[BAT_NSENSORS];
     80 
     81 	/* battery status */
     82 	int sc_flags;
     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 };
     92 
     93 static void battery_attach(struct device *, struct device *, void *);
     94 static int battery_match(struct device *, struct cfdata *, void *);
     95 static int battery_update(struct battery_softc *, int);
     96 static void battery_setup_envsys(struct battery_softc *);
     97 static int battery_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
     98 static int battery_streinfo(struct sysmon_envsys *,
     99     struct envsys_basic_info *);
    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 	battery_setup_envsys(sc);
    140 }
    141 
    142 static int
    143 battery_update(struct battery_softc *sc, int out)
    144 {
    145 	int len, vmax, pcharge, vb;
    146 	uint8_t buf[16];
    147 
    148 	len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
    149 	    PMU_BATTERY_STATE, 0, NULL, buf);
    150 	if (len != 9)
    151 		return -1;
    152 
    153 	sc->sc_flags = buf[1];
    154 
    155 	if (out) {
    156 		if (buf[1] & PMU_PWR_AC_PRESENT)
    157 			printf(" AC");
    158 		if (buf[1] & PMU_PWR_BATT_CHARGING)
    159 			printf(" charging");
    160 		if (buf[1] & PMU_PWR_BATT_PRESENT)
    161 			printf(" present");
    162 		if (buf[1] & PMU_PWR_BATT_FULL)
    163 			printf(" full");
    164 		printf("\n");
    165 	}
    166 
    167 	sc->sc_cpu_temp = buf[4];
    168 
    169 	if ((sc->sc_flags & PMU_PWR_BATT_PRESENT) == 0) {
    170 		sc->sc_voltage = 0;
    171 		sc->sc_current = 0;
    172 		sc->sc_bat_temp = 0;
    173 		sc->sc_charge = 0;
    174 		sc->sc_time = 0;
    175 		return 0;
    176 	}
    177 
    178 	vmax = sc->sc_vmax_charged;
    179 	vb = (buf[2] << 8) | buf[3];
    180 	sc->sc_voltage = (vb * 265 + 72665) / 10;
    181 	sc->sc_current = buf[6];
    182 	if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == 0) {
    183 	    	if (sc->sc_current > 200)
    184 			vb += ((sc->sc_current - 200) * 15) / 100;
    185 	} else {
    186 		vmax = sc->sc_vmax_charging;
    187 	}
    188 	sc->sc_charge = (100 * vb) / vmax;
    189 	if (sc->sc_flags & PMU_PWR_PCHARGE_RESET) {
    190 		pcharge = (buf[7] << 8) | buf[8];
    191 		if (pcharge > 6500)
    192 			pcharge = 6500;
    193 		pcharge = 100 - pcharge * 100 / 6500;
    194 		if (pcharge < sc->sc_charge)
    195 			sc->sc_charge = pcharge;
    196 	}
    197 	if (sc->sc_current > 0) {
    198 		sc->sc_time = (sc->sc_charge * 16440) / sc->sc_current;
    199 	} else
    200 		sc->sc_time = 0;
    201 
    202 	sc->sc_bat_temp = buf[5];
    203 
    204 	if (out) {
    205 		printf("voltage: %d.%03d\n", sc->sc_voltage / 1000,
    206 		    sc->sc_voltage % 1000);
    207 		printf("charge:  %d%%\n", sc->sc_charge);
    208 		if (sc->sc_time > 0)
    209 			printf("time:    %d:%02d\n", sc->sc_time / 60,
    210 			    sc->sc_time % 60);
    211 	}
    212 
    213 	return 0;
    214 }
    215 
    216 #define INITDATA(index, unit, string) \
    217 	sc->sc_sdata[index].sensor = index;				\
    218 	sc->sc_sdata[index].units = unit;     				\
    219 	sc->sc_sdata[index].validflags = ENVSYS_FVALID;			\
    220 	sc->sc_sdata[index].warnflags = 0;				\
    221 	sc->sc_sinfo[index].sensor = index;				\
    222 	sc->sc_sinfo[index].units = unit;     				\
    223 	sc->sc_sinfo[index].rfact = 1;     				\
    224 	sc->sc_sinfo[index].validflags = ENVSYS_FVALID;			\
    225 	snprintf(sc->sc_sinfo[index].desc, sizeof(sc->sc_sinfo[index].desc),	\
    226 	    "%s", string);
    227 
    228 static void
    229 battery_setup_envsys(struct battery_softc *sc)
    230 {
    231 
    232 	INITDATA(BAT_CPU_TEMPERATURE, ENVSYS_STEMP, "CPU temperature");
    233 	INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present");
    234 	INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
    235 	INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
    236 	INITDATA(BAT_CHARGE, ENVSYS_INTEGER, "Battery charge");
    237 	INITDATA(BAT_MAX_CHARGE, ENVSYS_INTEGER, "Battery design cap");
    238 	INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
    239 	INITDATA(BAT_TEMPERATURE, ENVSYS_STEMP, "Battery temperature");
    240 	INITDATA(BAT_CHARGING, ENVSYS_INDICATOR, "Battery charging");
    241 	INITDATA(BAT_DISCHARGING, ENVSYS_INDICATOR, "Battery discharging");
    242 	INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
    243 #undef INITDATA
    244 
    245 	sc->sc_sysmon.sme_sensor_info = sc->sc_sinfo;
    246 	sc->sc_sysmon.sme_sensor_data = sc->sc_sdata;
    247 	sc->sc_sysmon.sme_cookie = sc;
    248 	sc->sc_sysmon.sme_gtredata = battery_gtredata;
    249 	sc->sc_sysmon.sme_streinfo = battery_streinfo;
    250 	sc->sc_sysmon.sme_nsensors = BAT_NSENSORS;
    251 	sc->sc_sysmon.sme_envsys_version = 1000;
    252 
    253 	if (sysmon_envsys_register(&sc->sc_sysmon))
    254 		printf("%s: unable to register with sysmon\n",
    255 		    sc->sc_dev.dv_xname);
    256 }
    257 
    258 static int
    259 battery_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    260 {
    261 	struct battery_softc *sc = sme->sme_cookie;
    262 	struct envsys_tre_data *cur;
    263 	int which = tred->sensor;
    264 
    265 	battery_update(sc, 0);
    266 
    267 	cur = &sc->sc_sdata[BAT_CPU_TEMPERATURE];
    268 	cur->cur.data_s = sc->sc_cpu_temp * 1000000 + 273150000;
    269 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    270 
    271 	cur = &sc->sc_sdata[BAT_AC_PRESENT];
    272 	cur->cur.data_s = (sc->sc_flags & PMU_PWR_AC_PRESENT);
    273 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    274 
    275 	cur = &sc->sc_sdata[BAT_PRESENT];
    276 	cur->cur.data_s = (sc->sc_flags & PMU_PWR_BATT_PRESENT);
    277 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    278 
    279 	cur = &sc->sc_sdata[BAT_VOLTAGE];
    280 	cur->cur.data_s = sc->sc_voltage * 1000;
    281 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    282 
    283 	cur = &sc->sc_sdata[BAT_CURRENT];
    284 	cur->cur.data_s = sc->sc_current * 1000;
    285 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    286 
    287 	cur = &sc->sc_sdata[BAT_CHARGE];
    288 	cur->cur.data_s = sc->sc_charge;
    289 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    290 
    291 	cur = &sc->sc_sdata[BAT_MAX_CHARGE];
    292 	cur->cur.data_s = 100;
    293 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    294 
    295 	cur = &sc->sc_sdata[BAT_TEMPERATURE];
    296 	cur->cur.data_s = sc->sc_bat_temp * 1000000 + 273150000;
    297 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    298 
    299 	cur = &sc->sc_sdata[BAT_CHARGING];
    300 	cur->cur.data_s = (sc->sc_flags & PMU_PWR_BATT_CHARGING);
    301 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    302 
    303 	cur = &sc->sc_sdata[BAT_DISCHARGING];
    304 	cur->cur.data_s = (!(sc->sc_flags & PMU_PWR_BATT_CHARGING)) &&
    305 	    (!(sc->sc_flags & PMU_PWR_AC_PRESENT));
    306 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    307 
    308 	cur = &sc->sc_sdata[BAT_FULL];
    309 	cur->cur.data_s = (sc->sc_flags & PMU_PWR_BATT_FULL);
    310 	cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
    311 
    312 	memcpy(tred, &sc->sc_sdata[which], sizeof(struct envsys_tre_data));
    313 	return 0;
    314 }
    315 
    316 static int
    317 battery_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    318 {
    319 
    320 	/* XXX Not implemented */
    321 	binfo->validflags = 0;
    322 
    323 	return 0;
    324 }
    325