1 /* $NetBSD: j720pwr.c,v 1.3 2006/10/07 14:02:09 peter Exp $ */ 2 3 /*- 4 * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus and Peter Postma. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* Jornada 720 power management. */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: j720pwr.c,v 1.3 2006/10/07 14:02:09 peter Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/proc.h> 48 #include <sys/device.h> 49 50 #include <dev/apm/apmbios.h> 51 52 #include <machine/config_hook.h> 53 #include <machine/platid.h> 54 #include <machine/platid_mask.h> 55 56 #include <arm/sa11x0/sa11x0_var.h> 57 #include <arm/sa11x0/sa11x0_gpioreg.h> 58 #include <arm/sa11x0/sa11x0_ppcreg.h> 59 #include <arm/sa11x0/sa11x0_sspreg.h> 60 61 #include <hpcarm/dev/j720sspvar.h> 62 63 #ifdef DEBUG 64 #define DPRINTF(arg) printf arg 65 #else 66 #define DPRINTF(arg) /* nothing */ 67 #endif 68 69 #define arraysize(ary) (sizeof(ary) / sizeof(ary[0])) 70 71 struct j720pwr_softc { 72 struct device sc_dev; 73 74 struct j720ssp_softc *sc_ssp; 75 76 volatile int sc_state; 77 #define J720PWR_POWEROFF 0x01 78 #define J720PWR_SLEEPING 0x02 79 }; 80 81 static int j720pwr_match(struct device *, struct cfdata *, void *); 82 static void j720pwr_attach(struct device *, struct device *, void *); 83 84 static void j720pwr_sleep(void *); 85 static int j720pwr_suspend_hook(void *, int, long, void *); 86 static int j720pwr_event_hook(void *, int, long, void *); 87 static int j720pwr_apm_getpower_hook(void *, int, long, void *); 88 static int j720pwr_get_battery(struct j720pwr_softc *); 89 static int j720pwr_get_ac_status(struct j720pwr_softc *); 90 static int j720pwr_get_charge_status(struct j720pwr_softc *); 91 92 static const struct { 93 int percent; 94 int value; 95 int state; 96 } battery_table[] = { 97 { 100, 670, APM_BATT_FLAG_HIGH }, 98 { 90, 660, APM_BATT_FLAG_HIGH }, 99 { 80, 650, APM_BATT_FLAG_HIGH }, 100 { 70, 640, APM_BATT_FLAG_HIGH }, 101 { 60, 630, APM_BATT_FLAG_HIGH }, 102 { 50, 620, APM_BATT_FLAG_HIGH }, 103 { 40, 605, APM_BATT_FLAG_LOW }, 104 { 30, 580, APM_BATT_FLAG_LOW }, 105 { 20, 545, APM_BATT_FLAG_LOW }, 106 { 10, 500, APM_BATT_FLAG_CRITICAL }, 107 { 0, 430, APM_BATT_FLAG_CRITICAL }, 108 }; 109 110 CFATTACH_DECL(j720pwr, sizeof(struct j720pwr_softc), 111 j720pwr_match, j720pwr_attach, NULL, NULL); 112 113 114 static int 115 j720pwr_match(struct device *parent, struct cfdata *cf, void *aux) 116 { 117 118 if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX)) 119 return 0; 120 if (strcmp(cf->cf_name, "j720pwr") != 0) 121 return 0; 122 123 return 1; 124 } 125 126 static void 127 j720pwr_attach(struct device *parent, struct device *self, void *aux) 128 { 129 struct j720pwr_softc *sc = (struct j720pwr_softc *)self; 130 extern void (*__sleep_func)(void *); 131 extern void *__sleep_ctx; 132 133 printf("\n"); 134 135 sc->sc_ssp = (struct j720ssp_softc *)parent; 136 sc->sc_state = 0; 137 138 /* Register apm sleep function. */ 139 __sleep_func = j720pwr_sleep; 140 __sleep_ctx = sc; 141 142 /* Battery status query hook. */ 143 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL, 144 CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc); 145 146 /* Battery charge status query hook. */ 147 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE, 148 CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc); 149 150 /* AC status query hook. */ 151 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER, 152 CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc); 153 154 /* Suspend/resume button hook. */ 155 config_hook(CONFIG_HOOK_BUTTONEVENT, 156 CONFIG_HOOK_BUTTONEVENT_POWER, 157 CONFIG_HOOK_SHARE, j720pwr_suspend_hook, sc); 158 159 /* Receive suspend/resume events. */ 160 config_hook(CONFIG_HOOK_PMEVENT, 161 CONFIG_HOOK_PMEVENT_HARDPOWER, 162 CONFIG_HOOK_SHARE, j720pwr_event_hook, sc); 163 164 /* Attach hpcapm. */ 165 config_found_ia(self, "hpcapmif", NULL, NULL); 166 } 167 168 static void 169 j720pwr_sleep(void *ctx) 170 { 171 struct j720pwr_softc *sc = ctx; 172 struct j720ssp_softc *ssp = sc->sc_ssp; 173 uint32_t oldfer; 174 175 /* Disable falling-edge detect on all GPIO ports, except keyboard. */ 176 oldfer = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER); 177 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, 1 << 0); 178 179 while (sc->sc_state & J720PWR_POWEROFF) { 180 /* 181 * Just sleep here until the poweroff bit gets unset. 182 * We need to wait here because when machine_sleep() returns 183 * hpcapm(4) assumes that we are "resuming". 184 */ 185 (void)tsleep(&sc->sc_state, PWAIT, "j720slp", 0); 186 } 187 188 /* Restore previous FER value. */ 189 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, oldfer); 190 } 191 192 static int 193 j720pwr_suspend_hook(void *ctx, int type, long id, void *msg) 194 { 195 struct j720pwr_softc *sc = ctx; 196 197 if (type != CONFIG_HOOK_BUTTONEVENT || 198 id != CONFIG_HOOK_BUTTONEVENT_POWER) 199 return EINVAL; 200 201 if ((sc->sc_state & (J720PWR_POWEROFF | J720PWR_SLEEPING)) == 0) { 202 sc->sc_state |= J720PWR_POWEROFF; 203 } else if ((sc->sc_state & (J720PWR_POWEROFF | J720PWR_SLEEPING)) == 204 (J720PWR_POWEROFF | J720PWR_SLEEPING)) { 205 sc->sc_state &= ~J720PWR_POWEROFF; 206 wakeup(&sc->sc_state); 207 } else { 208 DPRINTF(("j720pwr_suspend_hook: busy\n")); 209 return EBUSY; 210 } 211 212 config_hook_call(CONFIG_HOOK_PMEVENT, 213 CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL); 214 215 return 0; 216 } 217 218 static int 219 j720pwr_event_hook(void *ctx, int type, long id, void *msg) 220 { 221 struct j720pwr_softc *sc = ctx; 222 int event = (int)msg; 223 224 if (type != CONFIG_HOOK_PMEVENT || 225 id != CONFIG_HOOK_PMEVENT_HARDPOWER) 226 return EINVAL; 227 228 switch (event) { 229 case PWR_SUSPEND: 230 sc->sc_state |= (J720PWR_SLEEPING | J720PWR_POWEROFF); 231 break; 232 case PWR_RESUME: 233 sc->sc_state &= ~(J720PWR_SLEEPING | J720PWR_POWEROFF); 234 break; 235 default: 236 return EINVAL; 237 } 238 239 return 0; 240 } 241 242 static int 243 j720pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg) 244 { 245 int * const pval = msg; 246 int val, tmp, i, state = 0; 247 248 if (type != CONFIG_HOOK_GET) 249 return EINVAL; 250 251 switch (id) { 252 case CONFIG_HOOK_BATTERYVAL: 253 val = j720pwr_get_battery(ctx); 254 255 if (val != -1) { 256 for (i = 0; i < arraysize(battery_table); i++) 257 if (val > battery_table[i].value) 258 break; 259 if (i == 0) { 260 /* Battery charge status is at maximum. */ 261 *pval = 100; 262 } else { 263 /* 264 * Use linear interpolation to calculate 265 * the estimated charge status. 266 */ 267 tmp = ((val - battery_table[i].value) * 100) / 268 (battery_table[i - 1].value - 269 battery_table[i].value); 270 *pval = battery_table[i].percent + 271 ((battery_table[i - 1].percent - 272 battery_table[i].percent) * tmp) / 100; 273 } 274 } else { 275 /* Battery is absent. */ 276 *pval = 0; 277 } 278 279 return 0; 280 281 case CONFIG_HOOK_CHARGE: 282 val = j720pwr_get_battery(ctx); 283 284 if (val != -1) { 285 for (i = 1; i < arraysize(battery_table); i++) { 286 if (val > battery_table[i].value) { 287 state = battery_table[i - 1].state; 288 break; 289 } 290 } 291 292 if (j720pwr_get_charge_status(ctx) == 0) 293 state |= APM_BATT_FLAG_CHARGING; 294 } else { 295 state = APM_BATT_FLAG_NO_SYSTEM_BATTERY; 296 } 297 298 *pval = state; 299 return 0; 300 301 case CONFIG_HOOK_ACADAPTER: 302 *pval = j720pwr_get_ac_status(ctx) ? APM_AC_OFF : APM_AC_ON; 303 304 return 0; 305 } 306 307 return EINVAL; 308 } 309 310 static int 311 j720pwr_get_battery(struct j720pwr_softc *sc) 312 { 313 struct j720ssp_softc *ssp = sc->sc_ssp; 314 int data, i, pmdata[3]; 315 316 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000); 317 318 if (j720ssp_readwrite(ssp, 1, 0xc0, &data, 500) < 0 || data != 0x11) { 319 DPRINTF(("j720pwr_get_battery: no dummy received\n")); 320 goto out; 321 } 322 323 for (i = 0; i < 3; i++) { 324 if (j720ssp_readwrite(ssp, 0, 0x11, &pmdata[i], 100) < 0) 325 goto out; 326 } 327 328 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000); 329 330 pmdata[0] |= (pmdata[2] & 0x3) << 8; /* Main battery. */ 331 pmdata[1] |= (pmdata[2] & 0xc) << 6; /* Backup battery (unused). */ 332 333 DPRINTF(("j720pwr_get_battery: data[0]=%d data[1]=%d data[2]=%d\n", 334 pmdata[0], pmdata[1], pmdata[2])); 335 336 /* If bit 0 and 1 are both set, the main battery is absent. */ 337 if ((pmdata[2] & 3) == 3) 338 return -1; 339 340 return pmdata[0]; 341 342 out: 343 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000); 344 345 /* reset SSP */ 346 bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307); 347 delay(100); 348 bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387); 349 350 DPRINTF(("j720pwr_get_battery: error %x\n", data)); 351 return 0; 352 } 353 354 static int 355 j720pwr_get_ac_status(struct j720pwr_softc *sc) 356 { 357 struct j720ssp_softc *ssp = sc->sc_ssp; 358 uint32_t status; 359 360 status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR); 361 362 return status & (1 << 4); 363 } 364 365 static int 366 j720pwr_get_charge_status(struct j720pwr_softc *sc) 367 { 368 struct j720ssp_softc *ssp = sc->sc_ssp; 369 uint32_t status; 370 371 status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR); 372 373 return status & (1 << 26); 374 } 375