1 1.15 macallan /* $NetBSD: battery.c,v 1.15 2011/07/26 08:37:45 macallan Exp $ */ 2 1.1 macallan 3 1.1 macallan /*- 4 1.1 macallan * Copyright (c) 2007 Michael Lorenz 5 1.1 macallan * All rights reserved. 6 1.1 macallan * 7 1.1 macallan * Redistribution and use in source and binary forms, with or without 8 1.1 macallan * modification, are permitted provided that the following conditions 9 1.1 macallan * are met: 10 1.1 macallan * 1. Redistributions of source code must retain the above copyright 11 1.1 macallan * notice, this list of conditions and the following disclaimer. 12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 macallan * notice, this list of conditions and the following disclaimer in the 14 1.1 macallan * documentation and/or other materials provided with the distribution. 15 1.1 macallan * 16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE. 27 1.1 macallan */ 28 1.1 macallan 29 1.1 macallan #include <sys/cdefs.h> 30 1.15 macallan __KERNEL_RCSID(0, "$NetBSD: battery.c,v 1.15 2011/07/26 08:37:45 macallan Exp $"); 31 1.1 macallan 32 1.1 macallan #include <sys/param.h> 33 1.1 macallan #include <sys/systm.h> 34 1.1 macallan #include <sys/kernel.h> 35 1.1 macallan #include <sys/device.h> 36 1.1 macallan #include <sys/proc.h> 37 1.1 macallan 38 1.1 macallan #include <dev/sysmon/sysmonvar.h> 39 1.1 macallan #include <dev/sysmon/sysmon_taskq.h> 40 1.1 macallan 41 1.1 macallan #include <macppc/dev/pmuvar.h> 42 1.1 macallan #include <macppc/dev/batteryvar.h> 43 1.13 dyoung #include <sys/bus.h> 44 1.6 garbled #include <machine/pio.h> 45 1.1 macallan #include "opt_battery.h" 46 1.1 macallan 47 1.1 macallan #ifdef BATTERY_DEBUG 48 1.1 macallan #define DPRINTF printf 49 1.1 macallan #else 50 1.1 macallan #define DPRINTF while (0) printf 51 1.1 macallan #endif 52 1.1 macallan 53 1.1 macallan #define BTYPE_COMET 1 54 1.1 macallan #define BTYPE_HOOPER 2 55 1.1 macallan 56 1.1 macallan #define BAT_CPU_TEMPERATURE 0 57 1.1 macallan #define BAT_AC_PRESENT 1 58 1.1 macallan #define BAT_PRESENT 2 59 1.1 macallan #define BAT_VOLTAGE 3 60 1.1 macallan #define BAT_CURRENT 4 61 1.1 macallan #define BAT_MAX_CHARGE 5 62 1.1 macallan #define BAT_CHARGE 6 63 1.1 macallan #define BAT_CHARGING 7 64 1.4 xtraeme #define BAT_FULL 8 65 1.4 xtraeme #define BAT_TEMPERATURE 9 66 1.4 xtraeme #define BAT_NSENSORS 10 /* number of sensors */ 67 1.1 macallan 68 1.1 macallan struct battery_softc { 69 1.12 matt device_t sc_dev; 70 1.1 macallan struct pmu_ops *sc_pmu_ops; 71 1.1 macallan int sc_type; 72 1.1 macallan 73 1.1 macallan /* envsys stuff */ 74 1.7 xtraeme struct sysmon_envsys *sc_sme; 75 1.4 xtraeme envsys_data_t sc_sensor[BAT_NSENSORS]; 76 1.3 macallan struct sysmon_pswitch sc_sm_acpower; 77 1.1 macallan 78 1.1 macallan /* battery status */ 79 1.1 macallan int sc_flags; 80 1.3 macallan int sc_oflags; 81 1.1 macallan int sc_voltage; 82 1.1 macallan int sc_charge; 83 1.1 macallan int sc_current; 84 1.1 macallan int sc_time; 85 1.1 macallan int sc_cpu_temp; 86 1.1 macallan int sc_bat_temp; 87 1.1 macallan int sc_vmax_charged; 88 1.1 macallan int sc_vmax_charging; 89 1.3 macallan uint32_t sc_timestamp; 90 1.1 macallan }; 91 1.1 macallan 92 1.12 matt static void battery_attach(device_t, device_t, void *); 93 1.12 matt static int battery_match(device_t, cfdata_t, void *); 94 1.1 macallan static int battery_update(struct battery_softc *, int); 95 1.1 macallan static void battery_setup_envsys(struct battery_softc *); 96 1.7 xtraeme static void battery_refresh(struct sysmon_envsys *, envsys_data_t *); 97 1.3 macallan static void battery_poll(void *); 98 1.1 macallan 99 1.12 matt CFATTACH_DECL_NEW(battery, sizeof(struct battery_softc), 100 1.1 macallan battery_match, battery_attach, NULL, NULL); 101 1.1 macallan 102 1.1 macallan static int 103 1.12 matt battery_match(device_t parent, cfdata_t cf, void *aux) 104 1.1 macallan { 105 1.1 macallan struct battery_attach_args *baa = aux; 106 1.1 macallan 107 1.1 macallan if (baa->baa_type == BATTERY_TYPE_LEGACY) 108 1.1 macallan return 1; 109 1.1 macallan 110 1.1 macallan return 0; 111 1.1 macallan } 112 1.1 macallan 113 1.1 macallan static void 114 1.12 matt battery_attach(device_t parent, device_t self, void *aux) 115 1.1 macallan { 116 1.1 macallan struct battery_attach_args *baa = aux; 117 1.12 matt struct battery_softc *sc = device_private(self); 118 1.1 macallan uint32_t reg; 119 1.1 macallan 120 1.15 macallan sc->sc_dev = self; 121 1.1 macallan sc->sc_pmu_ops = baa->baa_pmu_ops; 122 1.12 matt aprint_normal(": legacy battery "); 123 1.1 macallan 124 1.1 macallan reg = in32rb(0xf3000034); 125 1.1 macallan DPRINTF("reg: %08x\n", reg); 126 1.1 macallan if (reg & 0x20000000) { 127 1.1 macallan sc->sc_type = BTYPE_HOOPER; 128 1.1 macallan sc->sc_vmax_charged = 330; 129 1.1 macallan sc->sc_vmax_charging = 365; 130 1.1 macallan printf("[hooper]\n"); 131 1.1 macallan } else { 132 1.1 macallan sc->sc_type = BTYPE_COMET; 133 1.1 macallan sc->sc_vmax_charged = 189; 134 1.1 macallan sc->sc_vmax_charging = 213; 135 1.1 macallan printf("[comet]\n"); 136 1.1 macallan } 137 1.1 macallan battery_update(sc, 1); 138 1.3 macallan /* trigger a status update */ 139 1.3 macallan sc->sc_oflags = ~sc->sc_flags; 140 1.3 macallan 141 1.1 macallan battery_setup_envsys(sc); 142 1.3 macallan sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie, battery_poll, 143 1.3 macallan sc); 144 1.3 macallan 145 1.3 macallan memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch)); 146 1.3 macallan sc->sc_sm_acpower.smpsw_name = "AC Power"; 147 1.3 macallan sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER; 148 1.3 macallan if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0) 149 1.12 matt aprint_error_dev(self, 150 1.12 matt "unable to register AC power status with sysmon\n"); 151 1.1 macallan } 152 1.1 macallan 153 1.1 macallan static int 154 1.1 macallan battery_update(struct battery_softc *sc, int out) 155 1.1 macallan { 156 1.1 macallan int len, vmax, pcharge, vb; 157 1.1 macallan uint8_t buf[16]; 158 1.1 macallan 159 1.3 macallan if (sc->sc_timestamp == time_second) 160 1.3 macallan return 0; 161 1.3 macallan sc->sc_timestamp = time_second; 162 1.3 macallan 163 1.1 macallan len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie, 164 1.2 macallan PMU_BATTERY_STATE, 0, NULL, 16, buf); 165 1.1 macallan if (len != 9) 166 1.1 macallan return -1; 167 1.1 macallan 168 1.1 macallan sc->sc_flags = buf[1]; 169 1.1 macallan 170 1.1 macallan if (out) { 171 1.1 macallan if (buf[1] & PMU_PWR_AC_PRESENT) 172 1.1 macallan printf(" AC"); 173 1.1 macallan if (buf[1] & PMU_PWR_BATT_CHARGING) 174 1.1 macallan printf(" charging"); 175 1.1 macallan if (buf[1] & PMU_PWR_BATT_PRESENT) 176 1.1 macallan printf(" present"); 177 1.1 macallan if (buf[1] & PMU_PWR_BATT_FULL) 178 1.1 macallan printf(" full"); 179 1.1 macallan printf("\n"); 180 1.1 macallan } 181 1.1 macallan 182 1.1 macallan sc->sc_cpu_temp = buf[4]; 183 1.1 macallan 184 1.1 macallan if ((sc->sc_flags & PMU_PWR_BATT_PRESENT) == 0) { 185 1.1 macallan sc->sc_voltage = 0; 186 1.1 macallan sc->sc_current = 0; 187 1.1 macallan sc->sc_bat_temp = 0; 188 1.1 macallan sc->sc_charge = 0; 189 1.1 macallan sc->sc_time = 0; 190 1.1 macallan return 0; 191 1.1 macallan } 192 1.1 macallan 193 1.1 macallan vmax = sc->sc_vmax_charged; 194 1.1 macallan vb = (buf[2] << 8) | buf[3]; 195 1.1 macallan sc->sc_voltage = (vb * 265 + 72665) / 10; 196 1.1 macallan sc->sc_current = buf[6]; 197 1.1 macallan if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == 0) { 198 1.1 macallan if (sc->sc_current > 200) 199 1.1 macallan vb += ((sc->sc_current - 200) * 15) / 100; 200 1.1 macallan } else { 201 1.1 macallan vmax = sc->sc_vmax_charging; 202 1.1 macallan } 203 1.1 macallan sc->sc_charge = (100 * vb) / vmax; 204 1.1 macallan if (sc->sc_flags & PMU_PWR_PCHARGE_RESET) { 205 1.1 macallan pcharge = (buf[7] << 8) | buf[8]; 206 1.1 macallan if (pcharge > 6500) 207 1.1 macallan pcharge = 6500; 208 1.1 macallan pcharge = 100 - pcharge * 100 / 6500; 209 1.1 macallan if (pcharge < sc->sc_charge) 210 1.1 macallan sc->sc_charge = pcharge; 211 1.1 macallan } 212 1.1 macallan if (sc->sc_current > 0) { 213 1.1 macallan sc->sc_time = (sc->sc_charge * 16440) / sc->sc_current; 214 1.1 macallan } else 215 1.1 macallan sc->sc_time = 0; 216 1.1 macallan 217 1.1 macallan sc->sc_bat_temp = buf[5]; 218 1.1 macallan 219 1.1 macallan if (out) { 220 1.1 macallan printf("voltage: %d.%03d\n", sc->sc_voltage / 1000, 221 1.1 macallan sc->sc_voltage % 1000); 222 1.1 macallan printf("charge: %d%%\n", sc->sc_charge); 223 1.1 macallan if (sc->sc_time > 0) 224 1.1 macallan printf("time: %d:%02d\n", sc->sc_time / 60, 225 1.1 macallan sc->sc_time % 60); 226 1.1 macallan } 227 1.1 macallan 228 1.1 macallan return 0; 229 1.1 macallan } 230 1.1 macallan 231 1.4 xtraeme #define INITDATA(index, unit, string) \ 232 1.4 xtraeme sc->sc_sensor[index].units = unit; \ 233 1.14 pgoyette sc->sc_sensor[index].state = ENVSYS_SINVALID; \ 234 1.4 xtraeme snprintf(sc->sc_sensor[index].desc, \ 235 1.4 xtraeme sizeof(sc->sc_sensor[index].desc), "%s", string); 236 1.1 macallan 237 1.1 macallan static void 238 1.1 macallan battery_setup_envsys(struct battery_softc *sc) 239 1.1 macallan { 240 1.7 xtraeme int i; 241 1.7 xtraeme 242 1.7 xtraeme sc->sc_sme = sysmon_envsys_create(); 243 1.7 xtraeme 244 1.1 macallan INITDATA(BAT_CPU_TEMPERATURE, ENVSYS_STEMP, "CPU temperature"); 245 1.1 macallan INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present"); 246 1.1 macallan INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present"); 247 1.1 macallan INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage"); 248 1.11 aymeric INITDATA(BAT_CHARGE, ENVSYS_SAMPHOUR, "Battery charge"); 249 1.11 aymeric INITDATA(BAT_MAX_CHARGE, ENVSYS_SAMPHOUR, "Battery design cap"); 250 1.1 macallan INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current"); 251 1.1 macallan INITDATA(BAT_TEMPERATURE, ENVSYS_STEMP, "Battery temperature"); 252 1.11 aymeric INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging"); 253 1.1 macallan INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full"); 254 1.1 macallan #undef INITDATA 255 1.1 macallan 256 1.8 macallan for (i = 0; i < BAT_NSENSORS; i++) { 257 1.8 macallan if (sysmon_envsys_sensor_attach(sc->sc_sme, 258 1.8 macallan &sc->sc_sensor[i])) { 259 1.8 macallan sysmon_envsys_destroy(sc->sc_sme); 260 1.8 macallan return; 261 1.8 macallan } 262 1.8 macallan } 263 1.8 macallan 264 1.12 matt sc->sc_sme->sme_name = device_xname(sc->sc_dev); 265 1.7 xtraeme sc->sc_sme->sme_cookie = sc; 266 1.7 xtraeme sc->sc_sme->sme_refresh = battery_refresh; 267 1.1 macallan 268 1.7 xtraeme if (sysmon_envsys_register(sc->sc_sme)) { 269 1.12 matt aprint_error_dev(sc->sc_dev, 270 1.12 matt "unable to register with sysmon\n"); 271 1.7 xtraeme sysmon_envsys_destroy(sc->sc_sme); 272 1.7 xtraeme } 273 1.1 macallan } 274 1.1 macallan 275 1.7 xtraeme static void 276 1.7 xtraeme battery_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 277 1.1 macallan { 278 1.1 macallan struct battery_softc *sc = sme->sme_cookie; 279 1.4 xtraeme int which = edata->sensor; 280 1.1 macallan 281 1.1 macallan battery_update(sc, 0); 282 1.1 macallan 283 1.4 xtraeme switch (which) { 284 1.4 xtraeme case BAT_CPU_TEMPERATURE: 285 1.4 xtraeme edata->value_cur = sc->sc_cpu_temp * 1000000 + 273150000; 286 1.4 xtraeme break; 287 1.4 xtraeme case BAT_AC_PRESENT: 288 1.4 xtraeme edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT); 289 1.4 xtraeme break; 290 1.9 macallan case BAT_PRESENT: 291 1.9 macallan edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_PRESENT); 292 1.9 macallan break; 293 1.4 xtraeme case BAT_VOLTAGE: 294 1.4 xtraeme edata->value_cur = sc->sc_voltage * 1000; 295 1.4 xtraeme break; 296 1.4 xtraeme case BAT_CURRENT: 297 1.4 xtraeme edata->value_cur = sc->sc_current * 1000; 298 1.4 xtraeme break; 299 1.4 xtraeme case BAT_CHARGE: 300 1.4 xtraeme edata->value_cur = sc->sc_charge; 301 1.4 xtraeme break; 302 1.4 xtraeme case BAT_MAX_CHARGE: 303 1.4 xtraeme edata->value_cur = 100; 304 1.4 xtraeme break; 305 1.4 xtraeme case BAT_TEMPERATURE: 306 1.4 xtraeme edata->value_cur = sc->sc_bat_temp * 1000000 + 273150000; 307 1.4 xtraeme break; 308 1.4 xtraeme case BAT_CHARGING: 309 1.4 xtraeme if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) && 310 1.4 xtraeme (sc->sc_flags & PMU_PWR_AC_PRESENT)) 311 1.4 xtraeme edata->value_cur = 1; 312 1.4 xtraeme else 313 1.4 xtraeme edata->value_cur = 0; 314 1.4 xtraeme 315 1.4 xtraeme break; 316 1.4 xtraeme case BAT_FULL: 317 1.4 xtraeme edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL); 318 1.4 xtraeme break; 319 1.4 xtraeme } 320 1.1 macallan 321 1.5 xtraeme edata->state = ENVSYS_SVALID; 322 1.1 macallan } 323 1.3 macallan 324 1.3 macallan static void 325 1.3 macallan battery_poll(void *cookie) 326 1.3 macallan { 327 1.3 macallan struct battery_softc *sc = cookie; 328 1.3 macallan 329 1.6 garbled 330 1.3 macallan battery_update(sc, 0); 331 1.3 macallan if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags) 332 1.3 macallan return; 333 1.3 macallan 334 1.3 macallan sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT; 335 1.3 macallan 336 1.3 macallan sysmon_pswitch_event(&sc->sc_sm_acpower, 337 1.3 macallan sc->sc_oflags ? PSWITCH_EVENT_PRESSED : 338 1.3 macallan PSWITCH_EVENT_RELEASED); 339 1.3 macallan } 340