Home | History | Annotate | Line # | Download | only in dev
stvii.c revision 1.1
      1 /*	$NetBSD: stvii.c,v 1.1 2011/09/01 14:07:37 macallan Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2005 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.1 2011/09/01 14:07:37 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;
     96 	int sc_sleep;
     97 	struct sysmon_envsys *sc_sme;
     98 	envsys_data_t sc_sensor[BAT_NSENSORS];
     99 	struct sysmon_pswitch sc_sm_acpower;
    100 	struct sysmon_pswitch sc_sm_lid;
    101 };
    102 
    103 static void stvii_attach(device_t, device_t, void *);
    104 static int stvii_match(device_t, cfdata_t, void *);
    105 static void stvii_writereg(struct stvii_softc *, int, uint8_t);
    106 static uint8_t stvii_readreg(struct stvii_softc *, int);
    107 static void stvii_worker(void *);
    108 
    109 CFATTACH_DECL_NEW(stvii, sizeof(struct stvii_softc),
    110     stvii_match, stvii_attach, NULL, NULL);
    111 
    112 static int
    113 stvii_match(device_t parent, cfdata_t cf, void *aux)
    114 {
    115 	struct i2c_attach_args *args = aux;
    116 	int ret = -1;
    117 	uint8_t out = ST7_VERSION, in = 0;
    118 
    119 	/* see if we can talk to something at address 0x40 */
    120 	if (args->ia_addr == 0x40) {
    121 		iic_acquire_bus(args->ia_tag, 0);
    122 		ret = iic_exec(args->ia_tag, I2C_OP_READ_WITH_STOP, args->ia_addr,
    123 		    &out, 1, &in, 1, 0);
    124 		DPRINTF("%02x\n", in);
    125 		iic_release_bus(args->ia_tag, 0);
    126 	}
    127 	return (ret >= 0);
    128 }
    129 
    130 static void
    131 stvii_attach(device_t parent, device_t self, void *aux)
    132 {
    133 	struct stvii_softc *sc = device_private(self);
    134 	struct i2c_attach_args *args = aux;
    135 	uint8_t ver, reg;
    136 
    137 	sc->sc_dev = self;
    138 	sc->sc_address = args->ia_addr;
    139 	aprint_normal(": ST7 Microcontroller\n");
    140 	sc->sc_i2c = args->ia_tag;
    141 	ver = stvii_readreg(sc, ST7_VERSION);
    142 
    143 	aprint_normal_dev(sc->sc_dev, "firmware version %d.%d\n", (ver >> 4) & 0xf, ver & 0xf);
    144 #ifdef STVII_DEBUG
    145 	{
    146 		int i;
    147 
    148 		for (i = 0; i < 6; i++) {
    149 			printf("%02x ", stvii_readreg(sc, i));
    150 		}
    151 		printf("\n");
    152 	}
    153 #endif
    154 	stvii_writereg(sc, ST7_SIGNATURE, STSIG_EC_CONTROL);
    155 	reg = stvii_readreg(sc, ST7_CONTROL);
    156 	reg |= STC_RADIO_ENABLE;
    157 	stvii_writereg(sc, ST7_CONTROL, reg);
    158 	reg = stvii_readreg(sc, ST7_CONTROL);
    159 
    160 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, stvii_worker, sc,
    161 	    NULL, "stvii") != 0) {
    162 	    	aprint_error_dev(sc->sc_dev, "Failed to start kernel thread\n");
    163 	}
    164 
    165 	memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    166 	sc->sc_sm_acpower.smpsw_name = "AC Power";
    167 	sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    168 	if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    169 		printf("%s: unable to register AC power status with sysmon\n",
    170 		    device_xname(sc->sc_dev));
    171 	memset(&sc->sc_sm_lid, 0, sizeof(struct sysmon_pswitch));
    172 	sc->sc_sm_lid.smpsw_name = "Lid Switch";
    173 	sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
    174 	if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
    175 		printf("%s: unable to register lid switch with sysmon\n",
    176 		    device_xname(sc->sc_dev));
    177 }
    178 
    179 static void
    180 stvii_writereg(struct stvii_softc *sc, int reg, uint8_t val)
    181 {
    182 	uint8_t out[2] = {reg, val};
    183 
    184 	if ((reg < 0) || (reg > 5))
    185 		return;
    186 
    187 	iic_acquire_bus(sc->sc_i2c, 0);
    188 	iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_address, out, 2, NULL, 0, 0);
    189 	iic_release_bus(sc->sc_i2c, 0);
    190 }
    191 
    192 static uint8_t
    193 stvii_readreg(struct stvii_softc *sc, int reg)
    194 {
    195 	uint8_t inreg[1], outreg[1];
    196 
    197 	if ((reg < 0) || (reg > 5))
    198 		return 0xff;
    199 	inreg[0] = 0x77;
    200 	outreg[0] = reg;
    201 	iic_acquire_bus(sc->sc_i2c, 0);
    202 	iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_address, outreg,
    203 	    1, inreg, 1, 0);
    204 	iic_release_bus(sc->sc_i2c, 0);
    205 	return inreg[0];
    206 }
    207 
    208 static int
    209 stvii_battery_level(struct stvii_softc *sc)
    210 {
    211 	int bl, bh;
    212 
    213 	bl = stvii_readreg(sc, ST7_BATTERY_L);
    214 	bh = stvii_readreg(sc, ST7_BATTERY_H);
    215 	return (bl & 3) | (bh << 2);
    216 }
    217 
    218 static void
    219 stvii_worker(void *cookie)
    220 {
    221 	struct stvii_softc *sc = cookie;
    222 	uint8_t status = 0, st;
    223 	int battery_level = 0, bl;
    224 	int ok = TRUE;
    225 
    226 	while (ok) {
    227 		st = stvii_readreg(sc, ST7_STATUS);
    228 		if (st != status) {
    229 			if ((status ^ st) & STS_LID_CLOSED) {
    230 				sysmon_pswitch_event(&sc->sc_sm_lid,
    231 				    ((st & STS_LID_CLOSED) ?
    232 				     PSWITCH_EVENT_PRESSED :
    233 				     PSWITCH_EVENT_RELEASED));
    234 			}
    235 			if ((status ^ st) & STS_AC_AVAILABLE) {
    236 				sysmon_pswitch_event(&sc->sc_sm_acpower,
    237 				    ((st & STS_AC_AVAILABLE) ?
    238 				     PSWITCH_EVENT_PRESSED :
    239 				     PSWITCH_EVENT_RELEASED));
    240 			}
    241 			status = st;
    242 		}
    243 		if (0) {
    244 			bl = stvii_battery_level(sc);
    245 			if (bl != battery_level) {
    246 				printf("battery: %d\n", bl);
    247 				battery_level = bl;
    248 			}
    249 		}
    250 		tsleep(&sc->sc_sleep, 0, "stvii", hz / 2);
    251 	}
    252 }
    253