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