Home | History | Annotate | Line # | Download | only in dev
stvii.c revision 1.5
      1  1.5  christos /*	$NetBSD: stvii.c,v 1.5 2016/02/29 18:24:31 christos Exp $	*/
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.2  macallan  * Copyright (C) 2011 Michael Lorenz.
      5  1.1  macallan  *
      6  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      7  1.1  macallan  * modification, are permitted provided that the following conditions
      8  1.1  macallan  * are met:
      9  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     10  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     11  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     14  1.1  macallan  *
     15  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  1.1  macallan  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  macallan  */
     26  1.1  macallan 
     27  1.1  macallan /*
     28  1.1  macallan  * a driver for the ST7 microcontroller found in Gdium Liberty 1000 notebooks
     29  1.1  macallan  */
     30  1.1  macallan 
     31  1.1  macallan 
     32  1.1  macallan #include <sys/cdefs.h>
     33  1.5  christos __KERNEL_RCSID(0, "$NetBSD: stvii.c,v 1.5 2016/02/29 18:24:31 christos Exp $");
     34  1.1  macallan 
     35  1.1  macallan #include <sys/param.h>
     36  1.1  macallan #include <sys/systm.h>
     37  1.1  macallan #include <sys/kernel.h>
     38  1.1  macallan #include <sys/device.h>
     39  1.1  macallan #include <sys/malloc.h>
     40  1.1  macallan #include <sys/sysctl.h>
     41  1.1  macallan #include <sys/kthread.h>
     42  1.1  macallan #include <sys/proc.h>
     43  1.1  macallan 
     44  1.1  macallan #include <dev/sysmon/sysmonvar.h>
     45  1.1  macallan #include <dev/sysmon/sysmon_taskq.h>
     46  1.1  macallan 
     47  1.1  macallan #include <dev/i2c/i2cvar.h>
     48  1.1  macallan 
     49  1.1  macallan #include "opt_stvii.h"
     50  1.1  macallan 
     51  1.1  macallan #ifdef STVII_DEBUG
     52  1.1  macallan #define DPRINTF aprint_error
     53  1.1  macallan #else
     54  1.1  macallan #define DPRINTF while (0) printf
     55  1.1  macallan #endif
     56  1.1  macallan 
     57  1.1  macallan /* register definitions from OpenBSD */
     58  1.1  macallan #define	ST7_VERSION	0x00	/* only on later mobos */
     59  1.1  macallan 
     60  1.1  macallan #define	ST7_STATUS	0x01
     61  1.1  macallan #define	STS_LID_CLOSED		0x01
     62  1.1  macallan #define	STS_POWER_BTN_DOWN	0x02
     63  1.1  macallan #define	STS_BATTERY_PRESENT	0x04	/* not available on old mobo */
     64  1.1  macallan #define	STS_POWER_AVAILABLE	0x08
     65  1.1  macallan #define	STS_WAVELAN_BTN_DOWN	0x10	/* ``enable'' on old mobo */
     66  1.1  macallan #define	STS_AC_AVAILABLE	0x20
     67  1.1  macallan #define	ST7_CONTROL	0x02
     68  1.1  macallan #define	STC_DDR_CLOCK		0x01
     69  1.1  macallan #define	STC_CHARGE_LED_LIT	0x02
     70  1.1  macallan #define	STC_BEEP		0x04
     71  1.1  macallan #define	STC_DDR_POWER		0x08
     72  1.1  macallan #define	STC_TRICKLE		0x10	/* trickle charge rate */
     73  1.1  macallan #define	STC_RADIO_ENABLE	0x20	/* enable wavelan rf, later mobos */
     74  1.1  macallan #define	STC_MAIN_POWER		0x40
     75  1.1  macallan #define	STC_CHARGE_ENABLE	0x80
     76  1.1  macallan #define	ST7_BATTERY_L	0x03
     77  1.1  macallan #define	ST7_BATTERY_H	0x04
     78  1.1  macallan #define	ST7_SIGNATURE	0x05
     79  1.1  macallan #define	STSIG_EC_CONTROL	0x00
     80  1.1  macallan #define	STSIG_OS_CONTROL	0xae
     81  1.1  macallan /* rough battery operating state limits */
     82  1.1  macallan #define STSEC_BAT_MIN_VOLT	7000000	/* 7V */
     83  1.1  macallan #define STSEC_BAT_MAX_VOLT	8000000	/* 8V */
     84  1.1  macallan 
     85  1.1  macallan #define BAT_AC_PRESENT		0
     86  1.1  macallan #define BAT_BATTERY_PRESENT	1
     87  1.1  macallan #define BAT_CHARGING		2
     88  1.1  macallan #define BAT_CHARGE		3
     89  1.1  macallan #define BAT_MAX_CHARGE		4
     90  1.1  macallan #define BAT_NSENSORS		5
     91  1.1  macallan 
     92  1.1  macallan struct stvii_softc {
     93  1.1  macallan 	device_t sc_dev;
     94  1.1  macallan 	i2c_tag_t sc_i2c;
     95  1.2  macallan 	int sc_address, sc_version;
     96  1.1  macallan 	int sc_sleep;
     97  1.4  macallan 	int sc_flags, sc_charge, sc_bat_level;
     98  1.4  macallan 	uint8_t sc_control;
     99  1.1  macallan 	struct sysmon_envsys *sc_sme;
    100  1.1  macallan 	envsys_data_t sc_sensor[BAT_NSENSORS];
    101  1.1  macallan 	struct sysmon_pswitch sc_sm_acpower;
    102  1.1  macallan 	struct sysmon_pswitch sc_sm_lid;
    103  1.2  macallan 	struct sysmon_pswitch sc_sm_powerbutton;
    104  1.1  macallan };
    105  1.1  macallan 
    106  1.1  macallan static void stvii_attach(device_t, device_t, void *);
    107  1.1  macallan static int stvii_match(device_t, cfdata_t, void *);
    108  1.1  macallan static void stvii_writereg(struct stvii_softc *, int, uint8_t);
    109  1.2  macallan static int stvii_readreg(struct stvii_softc *, int);
    110  1.1  macallan static void stvii_worker(void *);
    111  1.3  macallan static void stvii_setup_envsys(struct stvii_softc *);
    112  1.3  macallan static void stvii_refresh(struct sysmon_envsys *, envsys_data_t *);
    113  1.4  macallan static int  stvii_battery_level(struct stvii_softc *);
    114  1.1  macallan 
    115  1.1  macallan CFATTACH_DECL_NEW(stvii, sizeof(struct stvii_softc),
    116  1.1  macallan     stvii_match, stvii_attach, NULL, NULL);
    117  1.1  macallan 
    118  1.4  macallan void stvii_poweroff(void);
    119  1.4  macallan static device_t stvii_dev = NULL;
    120  1.4  macallan 
    121  1.4  macallan #define BAT_FULL	8000000
    122  1.4  macallan #define BAT_LOW		7100000
    123  1.4  macallan 
    124  1.1  macallan static int
    125  1.1  macallan stvii_match(device_t parent, cfdata_t cf, void *aux)
    126  1.1  macallan {
    127  1.1  macallan 	struct i2c_attach_args *args = aux;
    128  1.1  macallan 	int ret = -1;
    129  1.1  macallan 	uint8_t out = ST7_VERSION, in = 0;
    130  1.1  macallan 
    131  1.1  macallan 	/* see if we can talk to something at address 0x40 */
    132  1.1  macallan 	if (args->ia_addr == 0x40) {
    133  1.1  macallan 		iic_acquire_bus(args->ia_tag, 0);
    134  1.1  macallan 		ret = iic_exec(args->ia_tag, I2C_OP_READ_WITH_STOP, args->ia_addr,
    135  1.1  macallan 		    &out, 1, &in, 1, 0);
    136  1.1  macallan 		DPRINTF("%02x\n", in);
    137  1.1  macallan 		iic_release_bus(args->ia_tag, 0);
    138  1.1  macallan 	}
    139  1.1  macallan 	return (ret >= 0);
    140  1.1  macallan }
    141  1.1  macallan 
    142  1.1  macallan static void
    143  1.1  macallan stvii_attach(device_t parent, device_t self, void *aux)
    144  1.1  macallan {
    145  1.1  macallan 	struct stvii_softc *sc = device_private(self);
    146  1.1  macallan 	struct i2c_attach_args *args = aux;
    147  1.1  macallan 	uint8_t ver, reg;
    148  1.1  macallan 
    149  1.1  macallan 	sc->sc_dev = self;
    150  1.4  macallan 	stvii_dev = self;
    151  1.1  macallan 	sc->sc_address = args->ia_addr;
    152  1.1  macallan 	aprint_normal(": ST7 Microcontroller\n");
    153  1.1  macallan 	sc->sc_i2c = args->ia_tag;
    154  1.1  macallan 	ver = stvii_readreg(sc, ST7_VERSION);
    155  1.2  macallan 	sc->sc_version = ver;
    156  1.1  macallan 	aprint_normal_dev(sc->sc_dev, "firmware version %d.%d\n", (ver >> 4) & 0xf, ver & 0xf);
    157  1.1  macallan #ifdef STVII_DEBUG
    158  1.1  macallan 	{
    159  1.1  macallan 		int i;
    160  1.1  macallan 
    161  1.1  macallan 		for (i = 0; i < 6; i++) {
    162  1.1  macallan 			printf("%02x ", stvii_readreg(sc, i));
    163  1.1  macallan 		}
    164  1.1  macallan 		printf("\n");
    165  1.1  macallan 	}
    166  1.1  macallan #endif
    167  1.4  macallan 	stvii_writereg(sc, ST7_SIGNATURE, STSIG_OS_CONTROL);
    168  1.1  macallan 	reg = stvii_readreg(sc, ST7_CONTROL);
    169  1.1  macallan 	reg |= STC_RADIO_ENABLE;
    170  1.1  macallan 	stvii_writereg(sc, ST7_CONTROL, reg);
    171  1.4  macallan 	sc->sc_control = reg;
    172  1.1  macallan 	reg = stvii_readreg(sc, ST7_CONTROL);
    173  1.1  macallan 
    174  1.4  macallan 	sc->sc_bat_level = stvii_battery_level(sc);
    175  1.4  macallan 
    176  1.1  macallan 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, stvii_worker, sc,
    177  1.1  macallan 	    NULL, "stvii") != 0) {
    178  1.1  macallan 	    	aprint_error_dev(sc->sc_dev, "Failed to start kernel thread\n");
    179  1.1  macallan 	}
    180  1.1  macallan 
    181  1.1  macallan 	memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    182  1.1  macallan 	sc->sc_sm_acpower.smpsw_name = "AC Power";
    183  1.1  macallan 	sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    184  1.1  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    185  1.1  macallan 		printf("%s: unable to register AC power status with sysmon\n",
    186  1.1  macallan 		    device_xname(sc->sc_dev));
    187  1.1  macallan 	memset(&sc->sc_sm_lid, 0, sizeof(struct sysmon_pswitch));
    188  1.1  macallan 	sc->sc_sm_lid.smpsw_name = "Lid Switch";
    189  1.1  macallan 	sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
    190  1.1  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
    191  1.1  macallan 		printf("%s: unable to register lid switch with sysmon\n",
    192  1.1  macallan 		    device_xname(sc->sc_dev));
    193  1.2  macallan 	memset(&sc->sc_sm_powerbutton, 0, sizeof(struct sysmon_pswitch));
    194  1.2  macallan 	sc->sc_sm_powerbutton.smpsw_name = "Power Button";
    195  1.2  macallan 	sc->sc_sm_powerbutton.smpsw_type = PSWITCH_TYPE_POWER;
    196  1.2  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_powerbutton) != 0)
    197  1.2  macallan 		printf("%s: unable to register power button with sysmon\n",
    198  1.2  macallan 		    device_xname(sc->sc_dev));
    199  1.3  macallan 	stvii_setup_envsys(sc);
    200  1.1  macallan }
    201  1.1  macallan 
    202  1.1  macallan static void
    203  1.1  macallan stvii_writereg(struct stvii_softc *sc, int reg, uint8_t val)
    204  1.1  macallan {
    205  1.1  macallan 	uint8_t out[2] = {reg, val};
    206  1.1  macallan 
    207  1.1  macallan 	if ((reg < 0) || (reg > 5))
    208  1.1  macallan 		return;
    209  1.1  macallan 
    210  1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    211  1.1  macallan 	iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_address, out, 2, NULL, 0, 0);
    212  1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    213  1.1  macallan }
    214  1.1  macallan 
    215  1.2  macallan static int
    216  1.1  macallan stvii_readreg(struct stvii_softc *sc, int reg)
    217  1.1  macallan {
    218  1.1  macallan 	uint8_t inreg[1], outreg[1];
    219  1.2  macallan 	int ret = 1, bail = 0;
    220  1.1  macallan 
    221  1.1  macallan 	if ((reg < 0) || (reg > 5))
    222  1.1  macallan 		return 0xff;
    223  1.1  macallan 	inreg[0] = 0x77;
    224  1.1  macallan 	outreg[0] = reg;
    225  1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    226  1.2  macallan 	while ((ret != 0) && (bail < 10)) {
    227  1.2  macallan 		ret = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    228  1.2  macallan 		    sc->sc_address, outreg, 1, inreg, 1, 0);
    229  1.2  macallan 		bail++;
    230  1.2  macallan 		delay(10);
    231  1.2  macallan 	}
    232  1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    233  1.2  macallan 	if (ret != 0)
    234  1.2  macallan 		return -1;
    235  1.1  macallan 	return inreg[0];
    236  1.1  macallan }
    237  1.1  macallan 
    238  1.1  macallan static int
    239  1.1  macallan stvii_battery_level(struct stvii_softc *sc)
    240  1.1  macallan {
    241  1.3  macallan 	int bl, bh, ret;
    242  1.1  macallan 
    243  1.1  macallan 	bl = stvii_readreg(sc, ST7_BATTERY_L);
    244  1.1  macallan 	bh = stvii_readreg(sc, ST7_BATTERY_H);
    245  1.3  macallan 	ret = (bl & 3) | (bh << 2);
    246  1.3  macallan 	ret = ((ret * 10000) / 1024) * 1000;
    247  1.3  macallan 	return ret;
    248  1.1  macallan }
    249  1.1  macallan 
    250  1.1  macallan static void
    251  1.1  macallan stvii_worker(void *cookie)
    252  1.1  macallan {
    253  1.1  macallan 	struct stvii_softc *sc = cookie;
    254  1.4  macallan 	int status = 0, st, cnt = 4;
    255  1.4  macallan 	int bl;
    256  1.4  macallan 	int charging = 0;
    257  1.1  macallan 	int ok = TRUE;
    258  1.4  macallan 	uint8_t nctrl;
    259  1.4  macallan 
    260  1.4  macallan 	/* if we were charging when we took over, keep charging */
    261  1.4  macallan 	if (sc->sc_control & STC_CHARGE_ENABLE)
    262  1.4  macallan 		charging = 1;
    263  1.1  macallan 
    264  1.1  macallan 	while (ok) {
    265  1.1  macallan 		st = stvii_readreg(sc, ST7_STATUS);
    266  1.2  macallan 		/*
    267  1.2  macallan 		 * I get i2c timeouts when the power button is pressed.
    268  1.2  macallan 		 * According to the linux driver this happens on firmware
    269  1.2  macallan 		 * version 0x13 and newer, mine is 0x16.
    270  1.2  macallan 		 * So, when we see read errors on the right version we assume
    271  1.2  macallan 		 * it's the power button as long as the lid is open
    272  1.2  macallan 		 * ( the button is inside the lid )
    273  1.2  macallan 		 */
    274  1.2  macallan 		if ((st == -1) && (sc->sc_version >= 0x13)) {
    275  1.2  macallan 			if ((status & (STS_LID_CLOSED | STS_POWER_BTN_DOWN) )
    276  1.2  macallan 			    == 0) {
    277  1.2  macallan 			    	st = status | STS_POWER_BTN_DOWN;
    278  1.2  macallan 			}
    279  1.2  macallan 		}
    280  1.2  macallan 		if ((st != -1) && (st != status)) {
    281  1.1  macallan 			if ((status ^ st) & STS_LID_CLOSED) {
    282  1.1  macallan 				sysmon_pswitch_event(&sc->sc_sm_lid,
    283  1.1  macallan 				    ((st & STS_LID_CLOSED) ?
    284  1.1  macallan 				     PSWITCH_EVENT_PRESSED :
    285  1.1  macallan 				     PSWITCH_EVENT_RELEASED));
    286  1.1  macallan 			}
    287  1.1  macallan 			if ((status ^ st) & STS_AC_AVAILABLE) {
    288  1.1  macallan 				sysmon_pswitch_event(&sc->sc_sm_acpower,
    289  1.1  macallan 				    ((st & STS_AC_AVAILABLE) ?
    290  1.1  macallan 				     PSWITCH_EVENT_PRESSED :
    291  1.1  macallan 				     PSWITCH_EVENT_RELEASED));
    292  1.1  macallan 			}
    293  1.2  macallan 			if ((status ^ st) & STS_POWER_BTN_DOWN) {
    294  1.2  macallan 				sysmon_pswitch_event(&sc->sc_sm_powerbutton,
    295  1.2  macallan 				    ((st & STS_POWER_BTN_DOWN) ?
    296  1.2  macallan 				     PSWITCH_EVENT_PRESSED :
    297  1.2  macallan 				     PSWITCH_EVENT_RELEASED));
    298  1.2  macallan 			}
    299  1.1  macallan 			status = st;
    300  1.1  macallan 		}
    301  1.3  macallan 		sc->sc_flags = status;
    302  1.4  macallan 		if (cnt >= 4) {
    303  1.4  macallan 			nctrl = sc->sc_control & ~(STC_TRICKLE | STC_CHARGE_ENABLE);
    304  1.1  macallan 			bl = stvii_battery_level(sc);
    305  1.4  macallan 			sc->sc_bat_level = bl;
    306  1.5  christos 			if (charging && (bl > BAT_FULL)) {
    307  1.4  macallan 				/* stop charging, we're full */
    308  1.4  macallan 				charging = 0;
    309  1.5  christos 			} else if (!charging && (bl < BAT_LOW)) {
    310  1.4  macallan 				charging = 1;
    311  1.1  macallan 			}
    312  1.4  macallan 			if (st & STS_AC_AVAILABLE) {
    313  1.4  macallan 				if (charging) {
    314  1.4  macallan 					nctrl |= STC_CHARGE_ENABLE;
    315  1.4  macallan 				} else
    316  1.4  macallan 					nctrl |= STC_TRICKLE;
    317  1.4  macallan 			}
    318  1.4  macallan 			if (nctrl != sc->sc_control) {
    319  1.4  macallan 				sc->sc_control = nctrl;
    320  1.4  macallan 				stvii_writereg(sc, ST7_CONTROL, sc->sc_control);
    321  1.4  macallan 			}
    322  1.4  macallan 			cnt = 0;
    323  1.4  macallan 		} else
    324  1.4  macallan 			cnt++;
    325  1.1  macallan 		tsleep(&sc->sc_sleep, 0, "stvii", hz / 2);
    326  1.1  macallan 	}
    327  1.1  macallan }
    328  1.3  macallan 
    329  1.3  macallan #define INITDATA(index, unit, string)					\
    330  1.3  macallan 	sc->sc_sensor[index].units = unit;     				\
    331  1.3  macallan 	sc->sc_sensor[index].state = ENVSYS_SINVALID;			\
    332  1.3  macallan 	snprintf(sc->sc_sensor[index].desc,				\
    333  1.3  macallan 	    sizeof(sc->sc_sensor[index].desc), "%s", string);
    334  1.3  macallan 
    335  1.3  macallan static void
    336  1.3  macallan stvii_setup_envsys(struct stvii_softc *sc)
    337  1.3  macallan {
    338  1.3  macallan 	int i;
    339  1.3  macallan 
    340  1.3  macallan 	sc->sc_sme = sysmon_envsys_create();
    341  1.3  macallan 
    342  1.3  macallan 	INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present");
    343  1.3  macallan 	INITDATA(BAT_BATTERY_PRESENT, ENVSYS_INDICATOR, "Battery present");
    344  1.3  macallan 	INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
    345  1.3  macallan 	INITDATA(BAT_CHARGE, ENVSYS_SVOLTS_DC, "Battery voltage");
    346  1.3  macallan 	INITDATA(BAT_MAX_CHARGE, ENVSYS_SVOLTS_DC, "Battery design cap");
    347  1.3  macallan #undef INITDATA
    348  1.3  macallan 
    349  1.3  macallan 	for (i = 0; i < BAT_NSENSORS; i++) {
    350  1.3  macallan 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    351  1.3  macallan 						&sc->sc_sensor[i])) {
    352  1.3  macallan 			sysmon_envsys_destroy(sc->sc_sme);
    353  1.3  macallan 			return;
    354  1.3  macallan 		}
    355  1.3  macallan 	}
    356  1.3  macallan 
    357  1.3  macallan 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    358  1.3  macallan 	sc->sc_sme->sme_cookie = sc;
    359  1.3  macallan 	sc->sc_sme->sme_refresh = stvii_refresh;
    360  1.3  macallan 
    361  1.3  macallan 	if (sysmon_envsys_register(sc->sc_sme)) {
    362  1.3  macallan 		aprint_error_dev(sc->sc_dev,
    363  1.3  macallan 		    "unable to register with sysmon\n");
    364  1.3  macallan 		sysmon_envsys_destroy(sc->sc_sme);
    365  1.3  macallan 	}
    366  1.3  macallan }
    367  1.3  macallan 
    368  1.3  macallan static void
    369  1.3  macallan stvii_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    370  1.3  macallan {
    371  1.3  macallan 	struct stvii_softc *sc = sme->sme_cookie;
    372  1.4  macallan 	int which = edata->sensor;
    373  1.3  macallan 
    374  1.3  macallan 	edata->state = ENVSYS_SINVALID;
    375  1.3  macallan 	switch (which) {
    376  1.3  macallan 	case BAT_AC_PRESENT:
    377  1.3  macallan 		edata->value_cur = (sc->sc_flags & STS_AC_AVAILABLE);
    378  1.3  macallan 		edata->state = ENVSYS_SVALID;
    379  1.3  macallan 		break;
    380  1.3  macallan 	case BAT_BATTERY_PRESENT:
    381  1.3  macallan 		edata->value_cur = (sc->sc_flags & STS_BATTERY_PRESENT);
    382  1.3  macallan 		edata->state = ENVSYS_SVALID;
    383  1.3  macallan 		break;
    384  1.3  macallan 	case BAT_CHARGE:
    385  1.3  macallan 		if (sc->sc_flags & STS_BATTERY_PRESENT) {
    386  1.4  macallan 			edata->value_cur = sc->sc_bat_level;
    387  1.3  macallan 			edata->state = ENVSYS_SVALID;
    388  1.3  macallan 		}
    389  1.3  macallan 		break;
    390  1.3  macallan 	case BAT_MAX_CHARGE:
    391  1.3  macallan 		if (sc->sc_flags & STS_BATTERY_PRESENT) {
    392  1.4  macallan 			edata->value_cur = 8000000;
    393  1.3  macallan 			/*edata->state = ENVSYS_SVALID;*/
    394  1.3  macallan 		}
    395  1.3  macallan 		break;
    396  1.3  macallan 	case BAT_CHARGING:
    397  1.4  macallan 		edata->value_cur = sc->sc_control & STC_CHARGE_ENABLE;
    398  1.3  macallan 		edata->state = ENVSYS_SVALID;
    399  1.3  macallan 		break;
    400  1.3  macallan 	}
    401  1.3  macallan }
    402  1.4  macallan 
    403  1.4  macallan void
    404  1.4  macallan stvii_poweroff(void)
    405  1.4  macallan {
    406  1.4  macallan 	struct stvii_softc *sc = device_private(stvii_dev);
    407  1.4  macallan 	int ctl;
    408  1.4  macallan 
    409  1.4  macallan 	if (sc == NULL)
    410  1.4  macallan 		return;
    411  1.4  macallan 	ctl = stvii_readreg(sc, ST7_CONTROL);
    412  1.4  macallan 	if (ctl == -1)
    413  1.4  macallan 		return;
    414  1.4  macallan 	stvii_writereg(sc, ST7_CONTROL, ctl & ~(STC_MAIN_POWER | STC_DDR_POWER));
    415  1.4  macallan }
    416