Module Name:    src
Committed By:   macallan
Date:           Thu Feb 28 13:21:16 UTC 2013

Modified Files:
        src/sys/arch/evbmips/conf: files.loongson
        src/sys/arch/evbmips/loongson/dev: stvii.c

Log Message:
add poweroff support, make sure we actually charge the battery


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/evbmips/conf/files.loongson
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/evbmips/loongson/dev/stvii.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/evbmips/conf/files.loongson
diff -u src/sys/arch/evbmips/conf/files.loongson:1.3 src/sys/arch/evbmips/conf/files.loongson:1.4
--- src/sys/arch/evbmips/conf/files.loongson:1.3	Fri Mar  2 13:20:57 2012
+++ src/sys/arch/evbmips/conf/files.loongson	Thu Feb 28 13:21:15 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: files.loongson,v 1.3 2012/03/02 13:20:57 nonaka Exp $
+#	$NetBSD: files.loongson,v 1.4 2013/02/28 13:21:15 macallan Exp $
 
 # Standard stanzas config(8) can't run without
 maxpartitions 16
@@ -77,7 +77,7 @@ file	arch/evbmips/loongson/dev/kb3310.c	
 # Gdium ST7 controller
 device	stvii
 attach	stvii at iic
-file	arch/evbmips/loongson/dev/stvii.c	stvii
+file	arch/evbmips/loongson/dev/stvii.c			stvii needs-flag
 defflag opt_stvii.h STVII_DEBUG
 
 # SM502 OHCI

Index: src/sys/arch/evbmips/loongson/dev/stvii.c
diff -u src/sys/arch/evbmips/loongson/dev/stvii.c:1.3 src/sys/arch/evbmips/loongson/dev/stvii.c:1.4
--- src/sys/arch/evbmips/loongson/dev/stvii.c:1.3	Thu Sep 15 19:29:23 2011
+++ src/sys/arch/evbmips/loongson/dev/stvii.c	Thu Feb 28 13:21:15 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: stvii.c,v 1.3 2011/09/15 19:29:23 macallan Exp $	*/
+/*	$NetBSD: stvii.c,v 1.4 2013/02/28 13:21:15 macallan Exp $	*/
 
 /*-
  * Copyright (C) 2011 Michael Lorenz.
@@ -30,7 +30,7 @@
 
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: stvii.c,v 1.3 2011/09/15 19:29:23 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: stvii.c,v 1.4 2013/02/28 13:21:15 macallan Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -94,7 +94,8 @@ struct stvii_softc {
 	i2c_tag_t sc_i2c;
 	int sc_address, sc_version;
 	int sc_sleep;
-	int sc_flags, sc_charge;
+	int sc_flags, sc_charge, sc_bat_level;
+	uint8_t sc_control;
 	struct sysmon_envsys *sc_sme;
 	envsys_data_t sc_sensor[BAT_NSENSORS];
 	struct sysmon_pswitch sc_sm_acpower;
@@ -109,10 +110,17 @@ static int stvii_readreg(struct stvii_so
 static void stvii_worker(void *);
 static void stvii_setup_envsys(struct stvii_softc *);
 static void stvii_refresh(struct sysmon_envsys *, envsys_data_t *);
+static int  stvii_battery_level(struct stvii_softc *);
 
 CFATTACH_DECL_NEW(stvii, sizeof(struct stvii_softc),
     stvii_match, stvii_attach, NULL, NULL);
 
+void stvii_poweroff(void);
+static device_t stvii_dev = NULL;
+
+#define BAT_FULL	8000000
+#define BAT_LOW		7100000
+
 static int
 stvii_match(device_t parent, cfdata_t cf, void *aux)
 {
@@ -139,6 +147,7 @@ stvii_attach(device_t parent, device_t s
 	uint8_t ver, reg;
 
 	sc->sc_dev = self;
+	stvii_dev = self;
 	sc->sc_address = args->ia_addr;
 	aprint_normal(": ST7 Microcontroller\n");
 	sc->sc_i2c = args->ia_tag;
@@ -155,12 +164,15 @@ stvii_attach(device_t parent, device_t s
 		printf("\n");
 	}
 #endif
-	stvii_writereg(sc, ST7_SIGNATURE, STSIG_EC_CONTROL);
+	stvii_writereg(sc, ST7_SIGNATURE, STSIG_OS_CONTROL);
 	reg = stvii_readreg(sc, ST7_CONTROL);
 	reg |= STC_RADIO_ENABLE;
 	stvii_writereg(sc, ST7_CONTROL, reg);
+	sc->sc_control = reg;
 	reg = stvii_readreg(sc, ST7_CONTROL);
 
+	sc->sc_bat_level = stvii_battery_level(sc);
+
 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, stvii_worker, sc,
 	    NULL, "stvii") != 0) {
 	    	aprint_error_dev(sc->sc_dev, "Failed to start kernel thread\n");
@@ -239,9 +251,15 @@ static void
 stvii_worker(void *cookie)
 {
 	struct stvii_softc *sc = cookie;
-	int status = 0, st;
-	int battery_level = 0, bl;
+	int status = 0, st, cnt = 4;
+	int bl;
+	int charging = 0;
 	int ok = TRUE;
+	uint8_t nctrl;
+
+	/* if we were charging when we took over, keep charging */
+	if (sc->sc_control & STC_CHARGE_ENABLE)
+		charging = 1;
 
 	while (ok) {
 		st = stvii_readreg(sc, ST7_STATUS);
@@ -281,13 +299,29 @@ stvii_worker(void *cookie)
 			status = st;
 		}
 		sc->sc_flags = status;
-		if (0) {
+		if (cnt >= 4) {
+			nctrl = sc->sc_control & ~(STC_TRICKLE | STC_CHARGE_ENABLE);
 			bl = stvii_battery_level(sc);
-			if (bl != battery_level) {
-				printf("battery: %d\n", bl);
-				battery_level = bl;
+			sc->sc_bat_level = bl;
+			if (charging & (bl > BAT_FULL)) {
+				/* stop charging, we're full */
+				charging = 0;
+			} else if (!charging & (bl < BAT_LOW)) {
+				charging = 1;
 			}
-		}
+			if (st & STS_AC_AVAILABLE) {
+				if (charging) {
+					nctrl |= STC_CHARGE_ENABLE;
+				} else
+					nctrl |= STC_TRICKLE;
+			}
+			if (nctrl != sc->sc_control) {
+				sc->sc_control = nctrl;
+				stvii_writereg(sc, ST7_CONTROL, sc->sc_control);
+			}
+			cnt = 0;				
+		} else
+			cnt++;
 		tsleep(&sc->sc_sleep, 0, "stvii", hz / 2);
 	}
 }
@@ -335,7 +369,7 @@ static void
 stvii_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
 {
 	struct stvii_softc *sc = sme->sme_cookie;
-	int which = edata->sensor, ctl, bat;
+	int which = edata->sensor;
 
 	edata->state = ENVSYS_SINVALID;
 	switch (which) {
@@ -349,23 +383,33 @@ stvii_refresh(struct sysmon_envsys *sme,
 		break;
 	case BAT_CHARGE:
 		if (sc->sc_flags & STS_BATTERY_PRESENT) {
-			bat = stvii_battery_level(sc);
-			edata->value_cur = bat;
+			edata->value_cur = sc->sc_bat_level;
 			edata->state = ENVSYS_SVALID;
 		}
 		break;
 	case BAT_MAX_CHARGE:
 		if (sc->sc_flags & STS_BATTERY_PRESENT) {
-			edata->value_cur = 8300000;
+			edata->value_cur = 8000000;
 			/*edata->state = ENVSYS_SVALID;*/
 		}
 		break;
 	case BAT_CHARGING:
-		ctl = stvii_readreg(sc, ST7_CONTROL);
-		if (ctl != -1) {
-			edata->value_cur = ctl & STC_CHARGE_ENABLE;
-		}
+		edata->value_cur = sc->sc_control & STC_CHARGE_ENABLE;
 		edata->state = ENVSYS_SVALID;
 		break;
 	}
 }
+
+void
+stvii_poweroff(void)
+{
+	struct stvii_softc *sc = device_private(stvii_dev);
+	int ctl;
+
+	if (sc == NULL)
+		return;
+	ctl = stvii_readreg(sc, ST7_CONTROL);
+	if (ctl == -1)
+		return;
+	stvii_writereg(sc, ST7_CONTROL, ctl & ~(STC_MAIN_POWER | STC_DDR_POWER));
+}

Reply via email to