1 /* $NetBSD: stvii.c,v 1.5 2016/02/29 18:24:31 christos 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.5 2016/02/29 18:24:31 christos 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, sc_bat_level; 98 uint8_t sc_control; 99 struct sysmon_envsys *sc_sme; 100 envsys_data_t sc_sensor[BAT_NSENSORS]; 101 struct sysmon_pswitch sc_sm_acpower; 102 struct sysmon_pswitch sc_sm_lid; 103 struct sysmon_pswitch sc_sm_powerbutton; 104 }; 105 106 static void stvii_attach(device_t, device_t, void *); 107 static int stvii_match(device_t, cfdata_t, void *); 108 static void stvii_writereg(struct stvii_softc *, int, uint8_t); 109 static int stvii_readreg(struct stvii_softc *, int); 110 static void stvii_worker(void *); 111 static void stvii_setup_envsys(struct stvii_softc *); 112 static void stvii_refresh(struct sysmon_envsys *, envsys_data_t *); 113 static int stvii_battery_level(struct stvii_softc *); 114 115 CFATTACH_DECL_NEW(stvii, sizeof(struct stvii_softc), 116 stvii_match, stvii_attach, NULL, NULL); 117 118 void stvii_poweroff(void); 119 static device_t stvii_dev = NULL; 120 121 #define BAT_FULL 8000000 122 #define BAT_LOW 7100000 123 124 static int 125 stvii_match(device_t parent, cfdata_t cf, void *aux) 126 { 127 struct i2c_attach_args *args = aux; 128 int ret = -1; 129 uint8_t out = ST7_VERSION, in = 0; 130 131 /* see if we can talk to something at address 0x40 */ 132 if (args->ia_addr == 0x40) { 133 iic_acquire_bus(args->ia_tag, 0); 134 ret = iic_exec(args->ia_tag, I2C_OP_READ_WITH_STOP, args->ia_addr, 135 &out, 1, &in, 1, 0); 136 DPRINTF("%02x\n", in); 137 iic_release_bus(args->ia_tag, 0); 138 } 139 return (ret >= 0); 140 } 141 142 static void 143 stvii_attach(device_t parent, device_t self, void *aux) 144 { 145 struct stvii_softc *sc = device_private(self); 146 struct i2c_attach_args *args = aux; 147 uint8_t ver, reg; 148 149 sc->sc_dev = self; 150 stvii_dev = self; 151 sc->sc_address = args->ia_addr; 152 aprint_normal(": ST7 Microcontroller\n"); 153 sc->sc_i2c = args->ia_tag; 154 ver = stvii_readreg(sc, ST7_VERSION); 155 sc->sc_version = ver; 156 aprint_normal_dev(sc->sc_dev, "firmware version %d.%d\n", (ver >> 4) & 0xf, ver & 0xf); 157 #ifdef STVII_DEBUG 158 { 159 int i; 160 161 for (i = 0; i < 6; i++) { 162 printf("%02x ", stvii_readreg(sc, i)); 163 } 164 printf("\n"); 165 } 166 #endif 167 stvii_writereg(sc, ST7_SIGNATURE, STSIG_OS_CONTROL); 168 reg = stvii_readreg(sc, ST7_CONTROL); 169 reg |= STC_RADIO_ENABLE; 170 stvii_writereg(sc, ST7_CONTROL, reg); 171 sc->sc_control = reg; 172 reg = stvii_readreg(sc, ST7_CONTROL); 173 174 sc->sc_bat_level = stvii_battery_level(sc); 175 176 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, stvii_worker, sc, 177 NULL, "stvii") != 0) { 178 aprint_error_dev(sc->sc_dev, "Failed to start kernel thread\n"); 179 } 180 181 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch)); 182 sc->sc_sm_acpower.smpsw_name = "AC Power"; 183 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER; 184 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0) 185 printf("%s: unable to register AC power status with sysmon\n", 186 device_xname(sc->sc_dev)); 187 memset(&sc->sc_sm_lid, 0, sizeof(struct sysmon_pswitch)); 188 sc->sc_sm_lid.smpsw_name = "Lid Switch"; 189 sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID; 190 if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0) 191 printf("%s: unable to register lid switch with sysmon\n", 192 device_xname(sc->sc_dev)); 193 memset(&sc->sc_sm_powerbutton, 0, sizeof(struct sysmon_pswitch)); 194 sc->sc_sm_powerbutton.smpsw_name = "Power Button"; 195 sc->sc_sm_powerbutton.smpsw_type = PSWITCH_TYPE_POWER; 196 if (sysmon_pswitch_register(&sc->sc_sm_powerbutton) != 0) 197 printf("%s: unable to register power button with sysmon\n", 198 device_xname(sc->sc_dev)); 199 stvii_setup_envsys(sc); 200 } 201 202 static void 203 stvii_writereg(struct stvii_softc *sc, int reg, uint8_t val) 204 { 205 uint8_t out[2] = {reg, val}; 206 207 if ((reg < 0) || (reg > 5)) 208 return; 209 210 iic_acquire_bus(sc->sc_i2c, 0); 211 iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_address, out, 2, NULL, 0, 0); 212 iic_release_bus(sc->sc_i2c, 0); 213 } 214 215 static int 216 stvii_readreg(struct stvii_softc *sc, int reg) 217 { 218 uint8_t inreg[1], outreg[1]; 219 int ret = 1, bail = 0; 220 221 if ((reg < 0) || (reg > 5)) 222 return 0xff; 223 inreg[0] = 0x77; 224 outreg[0] = reg; 225 iic_acquire_bus(sc->sc_i2c, 0); 226 while ((ret != 0) && (bail < 10)) { 227 ret = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, 228 sc->sc_address, outreg, 1, inreg, 1, 0); 229 bail++; 230 delay(10); 231 } 232 iic_release_bus(sc->sc_i2c, 0); 233 if (ret != 0) 234 return -1; 235 return inreg[0]; 236 } 237 238 static int 239 stvii_battery_level(struct stvii_softc *sc) 240 { 241 int bl, bh, ret; 242 243 bl = stvii_readreg(sc, ST7_BATTERY_L); 244 bh = stvii_readreg(sc, ST7_BATTERY_H); 245 ret = (bl & 3) | (bh << 2); 246 ret = ((ret * 10000) / 1024) * 1000; 247 return ret; 248 } 249 250 static void 251 stvii_worker(void *cookie) 252 { 253 struct stvii_softc *sc = cookie; 254 int status = 0, st, cnt = 4; 255 int bl; 256 int charging = 0; 257 int ok = TRUE; 258 uint8_t nctrl; 259 260 /* if we were charging when we took over, keep charging */ 261 if (sc->sc_control & STC_CHARGE_ENABLE) 262 charging = 1; 263 264 while (ok) { 265 st = stvii_readreg(sc, ST7_STATUS); 266 /* 267 * I get i2c timeouts when the power button is pressed. 268 * According to the linux driver this happens on firmware 269 * version 0x13 and newer, mine is 0x16. 270 * So, when we see read errors on the right version we assume 271 * it's the power button as long as the lid is open 272 * ( the button is inside the lid ) 273 */ 274 if ((st == -1) && (sc->sc_version >= 0x13)) { 275 if ((status & (STS_LID_CLOSED | STS_POWER_BTN_DOWN) ) 276 == 0) { 277 st = status | STS_POWER_BTN_DOWN; 278 } 279 } 280 if ((st != -1) && (st != status)) { 281 if ((status ^ st) & STS_LID_CLOSED) { 282 sysmon_pswitch_event(&sc->sc_sm_lid, 283 ((st & STS_LID_CLOSED) ? 284 PSWITCH_EVENT_PRESSED : 285 PSWITCH_EVENT_RELEASED)); 286 } 287 if ((status ^ st) & STS_AC_AVAILABLE) { 288 sysmon_pswitch_event(&sc->sc_sm_acpower, 289 ((st & STS_AC_AVAILABLE) ? 290 PSWITCH_EVENT_PRESSED : 291 PSWITCH_EVENT_RELEASED)); 292 } 293 if ((status ^ st) & STS_POWER_BTN_DOWN) { 294 sysmon_pswitch_event(&sc->sc_sm_powerbutton, 295 ((st & STS_POWER_BTN_DOWN) ? 296 PSWITCH_EVENT_PRESSED : 297 PSWITCH_EVENT_RELEASED)); 298 } 299 status = st; 300 } 301 sc->sc_flags = status; 302 if (cnt >= 4) { 303 nctrl = sc->sc_control & ~(STC_TRICKLE | STC_CHARGE_ENABLE); 304 bl = stvii_battery_level(sc); 305 sc->sc_bat_level = bl; 306 if (charging && (bl > BAT_FULL)) { 307 /* stop charging, we're full */ 308 charging = 0; 309 } else if (!charging && (bl < BAT_LOW)) { 310 charging = 1; 311 } 312 if (st & STS_AC_AVAILABLE) { 313 if (charging) { 314 nctrl |= STC_CHARGE_ENABLE; 315 } else 316 nctrl |= STC_TRICKLE; 317 } 318 if (nctrl != sc->sc_control) { 319 sc->sc_control = nctrl; 320 stvii_writereg(sc, ST7_CONTROL, sc->sc_control); 321 } 322 cnt = 0; 323 } else 324 cnt++; 325 tsleep(&sc->sc_sleep, 0, "stvii", hz / 2); 326 } 327 } 328 329 #define INITDATA(index, unit, string) \ 330 sc->sc_sensor[index].units = unit; \ 331 sc->sc_sensor[index].state = ENVSYS_SINVALID; \ 332 snprintf(sc->sc_sensor[index].desc, \ 333 sizeof(sc->sc_sensor[index].desc), "%s", string); 334 335 static void 336 stvii_setup_envsys(struct stvii_softc *sc) 337 { 338 int i; 339 340 sc->sc_sme = sysmon_envsys_create(); 341 342 INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present"); 343 INITDATA(BAT_BATTERY_PRESENT, ENVSYS_INDICATOR, "Battery present"); 344 INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging"); 345 INITDATA(BAT_CHARGE, ENVSYS_SVOLTS_DC, "Battery voltage"); 346 INITDATA(BAT_MAX_CHARGE, ENVSYS_SVOLTS_DC, "Battery design cap"); 347 #undef INITDATA 348 349 for (i = 0; i < BAT_NSENSORS; i++) { 350 if (sysmon_envsys_sensor_attach(sc->sc_sme, 351 &sc->sc_sensor[i])) { 352 sysmon_envsys_destroy(sc->sc_sme); 353 return; 354 } 355 } 356 357 sc->sc_sme->sme_name = device_xname(sc->sc_dev); 358 sc->sc_sme->sme_cookie = sc; 359 sc->sc_sme->sme_refresh = stvii_refresh; 360 361 if (sysmon_envsys_register(sc->sc_sme)) { 362 aprint_error_dev(sc->sc_dev, 363 "unable to register with sysmon\n"); 364 sysmon_envsys_destroy(sc->sc_sme); 365 } 366 } 367 368 static void 369 stvii_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 370 { 371 struct stvii_softc *sc = sme->sme_cookie; 372 int which = edata->sensor; 373 374 edata->state = ENVSYS_SINVALID; 375 switch (which) { 376 case BAT_AC_PRESENT: 377 edata->value_cur = (sc->sc_flags & STS_AC_AVAILABLE); 378 edata->state = ENVSYS_SVALID; 379 break; 380 case BAT_BATTERY_PRESENT: 381 edata->value_cur = (sc->sc_flags & STS_BATTERY_PRESENT); 382 edata->state = ENVSYS_SVALID; 383 break; 384 case BAT_CHARGE: 385 if (sc->sc_flags & STS_BATTERY_PRESENT) { 386 edata->value_cur = sc->sc_bat_level; 387 edata->state = ENVSYS_SVALID; 388 } 389 break; 390 case BAT_MAX_CHARGE: 391 if (sc->sc_flags & STS_BATTERY_PRESENT) { 392 edata->value_cur = 8000000; 393 /*edata->state = ENVSYS_SVALID;*/ 394 } 395 break; 396 case BAT_CHARGING: 397 edata->value_cur = sc->sc_control & STC_CHARGE_ENABLE; 398 edata->state = ENVSYS_SVALID; 399 break; 400 } 401 } 402 403 void 404 stvii_poweroff(void) 405 { 406 struct stvii_softc *sc = device_private(stvii_dev); 407 int ctl; 408 409 if (sc == NULL) 410 return; 411 ctl = stvii_readreg(sc, ST7_CONTROL); 412 if (ctl == -1) 413 return; 414 stvii_writereg(sc, ST7_CONTROL, ctl & ~(STC_MAIN_POWER | STC_DDR_POWER)); 415 } 416