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