1/* $NetBSD: j6x0pwr.c,v 1.16 2023/12/20 14:50:02 thorpej Exp $ */ 2 3/* 4 * Copyright (c) 2003, 2006 Valeriy E. Ushakov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__KERNEL_RCSID(0, "$NetBSD: j6x0pwr.c,v 1.16 2023/12/20 14:50:02 thorpej Exp $"); 32 33#include <sys/param.h> 34#include <sys/kernel.h> 35#include <sys/device.h> 36#include <sys/systm.h> 37#include <sys/callout.h> 38 39#include <dev/apm/apmbios.h> 40 41#include <machine/platid.h> 42#include <machine/platid_mask.h> 43#include <machine/config_hook.h> 44 45#include <sh3/exception.h> 46#include <sh3/intcreg.h> 47#include <sh3/pfcreg.h> 48 49#include <sh3/dev/adcvar.h> 50 51 52/* SH7709 PFC bits pertinent to Jornada 6x0 power */ 53#define PGDR_MAIN_BATTERY_OUT 0x04 54#define PGDR_LID_OPEN 0x01 55#define PJDR_AC_POWER_OUT 0x10 56#define PLDR_BATTERY_CHARGED 0x20 57 58 59static inline int __attribute__((__always_inline__)) 60j6x0pwr_battery_is_absent(void) 61{ 62 63 return _reg_read_1(SH7709_PGDR) & PGDR_MAIN_BATTERY_OUT; 64} 65 66static inline int __attribute__((__always_inline__)) 67j6x0pwr_ac_is_off(void) 68{ 69 70 return _reg_read_1(SH7709_PJDR) & PJDR_AC_POWER_OUT; 71} 72 73 74static inline int __attribute__((__always_inline__)) 75j6x0pwr_is_not_charging(void) 76{ 77 78 return _reg_read_1(SH7709_PLDR) & PLDR_BATTERY_CHARGED; 79} 80 81 82/* A/D converter channels to get power stats from */ 83#define ADC_CHANNEL_BATTERY 3 /* main battery */ 84#define ADC_CHANNEL_BACKUP 4 /* backup battery - we don't report it */ 85#define ADC_CHANNEL_CHARGE 5 /* we use PLDR_BATTERY_CHARGED instead */ 86 87/* 88 * Empirical range of battery values. 89 * Thanks to Joseph Heenan for measurements. 90 */ 91#define J6X0PWR_BATTERY_MIN 630 92#define J6X0PWR_BATTERY_CRITICAL 635 93#define J6X0PWR_BATTERY_LOW 645 94#define J6X0PWR_BATTERY_FULL 910 /* can be slightly more */ 95 96 97/* Convenience aliases (XXX: should be provided by config_hook.h) */ 98#define CONFIG_HOOK_BUTTON_PRESSED ((void *)1) 99#define CONFIG_HOOK_BUTTON_RELEASED ((void *)0) 100 101 102 103/* allow only one instance */ 104static int j6x0pwr_attached = 0; 105 106struct j6x0pwr_softc { 107 device_t sc_dev; 108 109 void *sc_ih; 110 volatile int sc_poweroff; 111}; 112 113static int j6x0pwr_match(device_t, cfdata_t, void *); 114static void j6x0pwr_attach(device_t, device_t, void *); 115 116CFATTACH_DECL_NEW(j6x0pwr, sizeof(struct j6x0pwr_softc), 117 j6x0pwr_match, j6x0pwr_attach, NULL, NULL); 118 119 120static int j6x0pwr_apm_getpower_hook(void *, int, long, void *); 121static int j6x0pwr_get_battery(void); /* from ADC */ 122 123static int j6x0pwr_intr(void *); 124static void j6x0pwr_sleep(void *); 125static int j6x0pwr_clear_interrupt(void); 126 127 128static int 129j6x0pwr_match(device_t parent, cfdata_t cfp, void *aux) 130{ 131 132 /* 133 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too? 134 * Is 620 wired similarly? 135 */ 136 if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX)) 137 return 0; 138 139 if (strcmp(cfp->cf_name, "j6x0pwr") != 0) 140 return 0; 141 142 /* allow only one instance */ 143 return !j6x0pwr_attached; 144} 145 146 147static void 148j6x0pwr_attach(device_t parent, device_t self, void *aux) 149{ 150 struct j6x0pwr_softc *sc; 151 152 /* XXX: in machdep.c */ 153 extern void (*__sleep_func)(void *); 154 extern void *__sleep_ctx; 155 156 sc = device_private(self); 157 sc->sc_dev = self; 158 159 aprint_naive("\n"); 160 aprint_normal("\n"); 161 162 /* allow only one instance */ 163 j6x0pwr_attached = 1; 164 165 /* arrange for hpcapm to call us when power status is requested */ 166 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER, 167 CONFIG_HOOK_EXCLUSIVE, 168 j6x0pwr_apm_getpower_hook, sc); 169 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE, 170 CONFIG_HOOK_EXCLUSIVE, 171 j6x0pwr_apm_getpower_hook, sc); 172 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL, 173 CONFIG_HOOK_EXCLUSIVE, 174 j6x0pwr_apm_getpower_hook, sc); 175 176 /* register sleep function to APM */ 177 __sleep_func = j6x0pwr_sleep; 178 __sleep_ctx = sc; 179 180 sc->sc_poweroff = 0; 181 182 /* drain the old interrupt */ 183 j6x0pwr_clear_interrupt(); 184 185 sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY, 186 j6x0pwr_intr, sc); 187 188 _reg_write_1(SH7709_PKDR, 0); /* Green LED on */ 189 190 if (!pmf_device_register(self, NULL, NULL)) 191 aprint_error_dev(self, "unable to establish power handler\n"); 192} 193 194 195/* 196 * Triggered when the On/Off button is pressed or the lid is closed. 197 * The state of the lid is reflected in the bit PGDR[0]. 198 * Closing the lid can trigger several consecutive interrupts. 199 * 200 * XXX: Since we don't put the machine to sleep, I have no idea how 201 * wakeup interrupt(s) look like. Need to revisit when software 202 * suspend is added to the kernel (which we need to support ACPI). 203 */ 204static int 205j6x0pwr_intr(void *arg) 206{ 207 struct j6x0pwr_softc *sc = arg; 208 device_t self = sc->sc_dev; 209 uint8_t irr0; 210 uint8_t pgdr; 211 212 irr0 = j6x0pwr_clear_interrupt(); 213 if ((irr0 & IRR0_IRQ0) == 0) { 214#ifdef DIAGNOSTIC 215 aprint_normal_dev(self, "irr0=0x%02x?\n", irr0); 216#endif 217 return 0; 218 } 219 220 pgdr = _reg_read_1(SH7709_PGDR); 221 if ((pgdr & PGDR_LID_OPEN) == 0) { 222 aprint_normal_dev(self, "lid closed %d\n", sc->sc_poweroff); 223 if (sc->sc_poweroff) 224 return 1; 225 } else { 226 aprint_normal_dev(self, "ON/OFF %d\n", sc->sc_poweroff); 227 if (sc->sc_poweroff) 228 sc->sc_poweroff = 0; 229 } 230 231 config_hook_call(CONFIG_HOOK_BUTTONEVENT, 232 CONFIG_HOOK_BUTTONEVENT_POWER, 233 CONFIG_HOOK_BUTTON_PRESSED); 234 /* fake release */ 235 config_hook_call(CONFIG_HOOK_BUTTONEVENT, 236 CONFIG_HOOK_BUTTONEVENT_POWER, 237 CONFIG_HOOK_BUTTON_RELEASED); 238 239 return 1; 240} 241 242 243static int 244j6x0pwr_clear_interrupt(void) 245{ 246 uint8_t irr0; 247 248 irr0 = _reg_read_1(SH7709_IRR0); 249 if (irr0 & IRR0_IRQ0) 250 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0); 251 252 return irr0; 253} 254 255 256void 257j6x0pwr_sleep(void *arg) 258{ 259 /* splhigh on entry */ 260 struct j6x0pwr_softc *sc = arg; 261 int s; 262 263 /* Reinstall j6x0pwr_intr as a wakeup handler */ 264 intc_intr_disestablish(sc->sc_ih); 265 sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_HIGH, 266 j6x0pwr_intr, sc); 267 sc->sc_poweroff = 1; 268 do { 269 /* Disable interrupt except for power button. */ 270 s = _cpu_intr_resume(IPL_CLOCK << 4); 271 _reg_write_1(SH7709_PKDR, 0xff); /* Green LED off */ 272 273 __asm volatile("sleep"); 274 275 _reg_write_1(SH7709_PKDR, 0); /* Green LED on */ 276 _cpu_intr_resume(s); 277 } while (sc->sc_poweroff); 278 279 /* Return to normal power button */ 280 intc_intr_disestablish(sc->sc_ih); 281 sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY, 282 j6x0pwr_intr, sc); 283 /* splhigh on exit */ 284} 285 286 287static int 288j6x0pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg) 289{ 290 /* struct j6x0pwr_softc * const sc = ctx; */ 291 int * const pval = msg; 292 int battery, state; 293 294 if (type != CONFIG_HOOK_GET) 295 return EINVAL; 296 297 switch (id) { 298 299 case CONFIG_HOOK_ACADAPTER: 300 *pval = j6x0pwr_ac_is_off() ? APM_AC_OFF : APM_AC_ON; 301 return 0; 302 303 case CONFIG_HOOK_CHARGE: 304 if (j6x0pwr_battery_is_absent()) 305 state = APM_BATT_FLAG_NO_SYSTEM_BATTERY; 306 else { 307 battery = j6x0pwr_get_battery(); 308 if (battery < J6X0PWR_BATTERY_CRITICAL) 309 state = APM_BATT_FLAG_CRITICAL; 310 else if (battery < J6X0PWR_BATTERY_LOW) 311 state = APM_BATT_FLAG_LOW; 312 else 313 state = APM_BATT_FLAG_HIGH; /* XXX? */ 314 315 if (!j6x0pwr_is_not_charging()) 316 state |= APM_BATT_FLAG_CHARGING; 317 } 318 *pval = state; 319 return 0; 320 321 case CONFIG_HOOK_BATTERYVAL: 322 if (j6x0pwr_battery_is_absent()) 323 state = 0; 324 else { 325 battery = j6x0pwr_get_battery(); 326 if (battery > J6X0PWR_BATTERY_FULL) 327 state = 100; 328 else 329 state = 100 * (battery - J6X0PWR_BATTERY_MIN) 330 / (J6X0PWR_BATTERY_FULL 331 - J6X0PWR_BATTERY_MIN); 332 } 333 *pval = state; 334 return 0; 335 } 336 337 return EINVAL; 338} 339 340 341static int 342j6x0pwr_get_battery(void) 343{ 344 int battery; 345 int s; 346 347 if (j6x0pwr_battery_is_absent()) 348 return -1; 349 350 s = spltty(); 351 battery = adc_sample_channel(ADC_CHANNEL_BATTERY); 352 splx(s); 353 354 return battery; 355} 356