Home | History | Annotate | Line # | Download | only in dev
stvii.c revision 1.2
      1  1.2  macallan /*	$NetBSD: stvii.c,v 1.2 2011/09/07 13:37:49 macallan 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.2  macallan __KERNEL_RCSID(0, "$NetBSD: stvii.c,v 1.2 2011/09/07 13:37:49 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.2  macallan 	int sc_address, sc_version;
     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.2  macallan 	struct sysmon_pswitch sc_sm_powerbutton;
    102  1.1  macallan };
    103  1.1  macallan 
    104  1.1  macallan static void stvii_attach(device_t, device_t, void *);
    105  1.1  macallan static int stvii_match(device_t, cfdata_t, void *);
    106  1.1  macallan static void stvii_writereg(struct stvii_softc *, int, uint8_t);
    107  1.2  macallan static int stvii_readreg(struct stvii_softc *, int);
    108  1.1  macallan static void stvii_worker(void *);
    109  1.1  macallan 
    110  1.1  macallan CFATTACH_DECL_NEW(stvii, sizeof(struct stvii_softc),
    111  1.1  macallan     stvii_match, stvii_attach, NULL, NULL);
    112  1.1  macallan 
    113  1.1  macallan static int
    114  1.1  macallan stvii_match(device_t parent, cfdata_t cf, void *aux)
    115  1.1  macallan {
    116  1.1  macallan 	struct i2c_attach_args *args = aux;
    117  1.1  macallan 	int ret = -1;
    118  1.1  macallan 	uint8_t out = ST7_VERSION, in = 0;
    119  1.1  macallan 
    120  1.1  macallan 	/* see if we can talk to something at address 0x40 */
    121  1.1  macallan 	if (args->ia_addr == 0x40) {
    122  1.1  macallan 		iic_acquire_bus(args->ia_tag, 0);
    123  1.1  macallan 		ret = iic_exec(args->ia_tag, I2C_OP_READ_WITH_STOP, args->ia_addr,
    124  1.1  macallan 		    &out, 1, &in, 1, 0);
    125  1.1  macallan 		DPRINTF("%02x\n", in);
    126  1.1  macallan 		iic_release_bus(args->ia_tag, 0);
    127  1.1  macallan 	}
    128  1.1  macallan 	return (ret >= 0);
    129  1.1  macallan }
    130  1.1  macallan 
    131  1.1  macallan static void
    132  1.1  macallan stvii_attach(device_t parent, device_t self, void *aux)
    133  1.1  macallan {
    134  1.1  macallan 	struct stvii_softc *sc = device_private(self);
    135  1.1  macallan 	struct i2c_attach_args *args = aux;
    136  1.1  macallan 	uint8_t ver, reg;
    137  1.1  macallan 
    138  1.1  macallan 	sc->sc_dev = self;
    139  1.1  macallan 	sc->sc_address = args->ia_addr;
    140  1.1  macallan 	aprint_normal(": ST7 Microcontroller\n");
    141  1.1  macallan 	sc->sc_i2c = args->ia_tag;
    142  1.1  macallan 	ver = stvii_readreg(sc, ST7_VERSION);
    143  1.2  macallan 	sc->sc_version = ver;
    144  1.1  macallan 	aprint_normal_dev(sc->sc_dev, "firmware version %d.%d\n", (ver >> 4) & 0xf, ver & 0xf);
    145  1.1  macallan #ifdef STVII_DEBUG
    146  1.1  macallan 	{
    147  1.1  macallan 		int i;
    148  1.1  macallan 
    149  1.1  macallan 		for (i = 0; i < 6; i++) {
    150  1.1  macallan 			printf("%02x ", stvii_readreg(sc, i));
    151  1.1  macallan 		}
    152  1.1  macallan 		printf("\n");
    153  1.1  macallan 	}
    154  1.1  macallan #endif
    155  1.1  macallan 	stvii_writereg(sc, ST7_SIGNATURE, STSIG_EC_CONTROL);
    156  1.1  macallan 	reg = stvii_readreg(sc, ST7_CONTROL);
    157  1.1  macallan 	reg |= STC_RADIO_ENABLE;
    158  1.1  macallan 	stvii_writereg(sc, ST7_CONTROL, reg);
    159  1.1  macallan 	reg = stvii_readreg(sc, ST7_CONTROL);
    160  1.1  macallan 
    161  1.1  macallan 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, stvii_worker, sc,
    162  1.1  macallan 	    NULL, "stvii") != 0) {
    163  1.1  macallan 	    	aprint_error_dev(sc->sc_dev, "Failed to start kernel thread\n");
    164  1.1  macallan 	}
    165  1.1  macallan 
    166  1.1  macallan 	memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    167  1.1  macallan 	sc->sc_sm_acpower.smpsw_name = "AC Power";
    168  1.1  macallan 	sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    169  1.1  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    170  1.1  macallan 		printf("%s: unable to register AC power status with sysmon\n",
    171  1.1  macallan 		    device_xname(sc->sc_dev));
    172  1.1  macallan 	memset(&sc->sc_sm_lid, 0, sizeof(struct sysmon_pswitch));
    173  1.1  macallan 	sc->sc_sm_lid.smpsw_name = "Lid Switch";
    174  1.1  macallan 	sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
    175  1.1  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
    176  1.1  macallan 		printf("%s: unable to register lid switch with sysmon\n",
    177  1.1  macallan 		    device_xname(sc->sc_dev));
    178  1.2  macallan 	memset(&sc->sc_sm_powerbutton, 0, sizeof(struct sysmon_pswitch));
    179  1.2  macallan 	sc->sc_sm_powerbutton.smpsw_name = "Power Button";
    180  1.2  macallan 	sc->sc_sm_powerbutton.smpsw_type = PSWITCH_TYPE_POWER;
    181  1.2  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_powerbutton) != 0)
    182  1.2  macallan 		printf("%s: unable to register power button with sysmon\n",
    183  1.2  macallan 		    device_xname(sc->sc_dev));
    184  1.1  macallan }
    185  1.1  macallan 
    186  1.1  macallan static void
    187  1.1  macallan stvii_writereg(struct stvii_softc *sc, int reg, uint8_t val)
    188  1.1  macallan {
    189  1.1  macallan 	uint8_t out[2] = {reg, val};
    190  1.1  macallan 
    191  1.1  macallan 	if ((reg < 0) || (reg > 5))
    192  1.1  macallan 		return;
    193  1.1  macallan 
    194  1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    195  1.1  macallan 	iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_address, out, 2, NULL, 0, 0);
    196  1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    197  1.1  macallan }
    198  1.1  macallan 
    199  1.2  macallan static int
    200  1.1  macallan stvii_readreg(struct stvii_softc *sc, int reg)
    201  1.1  macallan {
    202  1.1  macallan 	uint8_t inreg[1], outreg[1];
    203  1.2  macallan 	int ret = 1, bail = 0;
    204  1.1  macallan 
    205  1.1  macallan 	if ((reg < 0) || (reg > 5))
    206  1.1  macallan 		return 0xff;
    207  1.1  macallan 	inreg[0] = 0x77;
    208  1.1  macallan 	outreg[0] = reg;
    209  1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    210  1.2  macallan 	while ((ret != 0) && (bail < 10)) {
    211  1.2  macallan 		ret = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    212  1.2  macallan 		    sc->sc_address, outreg, 1, inreg, 1, 0);
    213  1.2  macallan 		bail++;
    214  1.2  macallan 		delay(10);
    215  1.2  macallan 	}
    216  1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    217  1.2  macallan 	if (ret != 0)
    218  1.2  macallan 		return -1;
    219  1.1  macallan 	return inreg[0];
    220  1.1  macallan }
    221  1.1  macallan 
    222  1.1  macallan static int
    223  1.1  macallan stvii_battery_level(struct stvii_softc *sc)
    224  1.1  macallan {
    225  1.1  macallan 	int bl, bh;
    226  1.1  macallan 
    227  1.1  macallan 	bl = stvii_readreg(sc, ST7_BATTERY_L);
    228  1.1  macallan 	bh = stvii_readreg(sc, ST7_BATTERY_H);
    229  1.1  macallan 	return (bl & 3) | (bh << 2);
    230  1.1  macallan }
    231  1.1  macallan 
    232  1.1  macallan static void
    233  1.1  macallan stvii_worker(void *cookie)
    234  1.1  macallan {
    235  1.1  macallan 	struct stvii_softc *sc = cookie;
    236  1.2  macallan 	int status = 0, st;
    237  1.1  macallan 	int battery_level = 0, bl;
    238  1.1  macallan 	int ok = TRUE;
    239  1.1  macallan 
    240  1.1  macallan 	while (ok) {
    241  1.1  macallan 		st = stvii_readreg(sc, ST7_STATUS);
    242  1.2  macallan 		/*
    243  1.2  macallan 		 * I get i2c timeouts when the power button is pressed.
    244  1.2  macallan 		 * According to the linux driver this happens on firmware
    245  1.2  macallan 		 * version 0x13 and newer, mine is 0x16.
    246  1.2  macallan 		 * So, when we see read errors on the right version we assume
    247  1.2  macallan 		 * it's the power button as long as the lid is open
    248  1.2  macallan 		 * ( the button is inside the lid )
    249  1.2  macallan 		 */
    250  1.2  macallan 		if ((st == -1) && (sc->sc_version >= 0x13)) {
    251  1.2  macallan 			if ((status & (STS_LID_CLOSED | STS_POWER_BTN_DOWN) )
    252  1.2  macallan 			    == 0) {
    253  1.2  macallan 			    	st = status | STS_POWER_BTN_DOWN;
    254  1.2  macallan 			}
    255  1.2  macallan 		}
    256  1.2  macallan 		if ((st != -1) && (st != status)) {
    257  1.1  macallan 			if ((status ^ st) & STS_LID_CLOSED) {
    258  1.1  macallan 				sysmon_pswitch_event(&sc->sc_sm_lid,
    259  1.1  macallan 				    ((st & STS_LID_CLOSED) ?
    260  1.1  macallan 				     PSWITCH_EVENT_PRESSED :
    261  1.1  macallan 				     PSWITCH_EVENT_RELEASED));
    262  1.1  macallan 			}
    263  1.1  macallan 			if ((status ^ st) & STS_AC_AVAILABLE) {
    264  1.1  macallan 				sysmon_pswitch_event(&sc->sc_sm_acpower,
    265  1.1  macallan 				    ((st & STS_AC_AVAILABLE) ?
    266  1.1  macallan 				     PSWITCH_EVENT_PRESSED :
    267  1.1  macallan 				     PSWITCH_EVENT_RELEASED));
    268  1.1  macallan 			}
    269  1.2  macallan 			if ((status ^ st) & STS_POWER_BTN_DOWN) {
    270  1.2  macallan 				sysmon_pswitch_event(&sc->sc_sm_powerbutton,
    271  1.2  macallan 				    ((st & STS_POWER_BTN_DOWN) ?
    272  1.2  macallan 				     PSWITCH_EVENT_PRESSED :
    273  1.2  macallan 				     PSWITCH_EVENT_RELEASED));
    274  1.2  macallan 			}
    275  1.1  macallan 			status = st;
    276  1.1  macallan 		}
    277  1.1  macallan 		if (0) {
    278  1.1  macallan 			bl = stvii_battery_level(sc);
    279  1.1  macallan 			if (bl != battery_level) {
    280  1.1  macallan 				printf("battery: %d\n", bl);
    281  1.1  macallan 				battery_level = bl;
    282  1.1  macallan 			}
    283  1.1  macallan 		}
    284  1.1  macallan 		tsleep(&sc->sc_sleep, 0, "stvii", hz / 2);
    285  1.1  macallan 	}
    286  1.1  macallan }
    287