Home | History | Annotate | Line # | Download | only in dev
j6x0pwr.c revision 1.12
      1 /*	$NetBSD: j6x0pwr.c,v 1.12 2006/03/05 02:17:47 uwe 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.12 2006/03/05 02:17:47 uwe Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/malloc.h>
     37 #include <sys/systm.h>
     38 #include <sys/callout.h>
     39 #ifdef GPROF
     40 #include <sys/gmon.h>
     41 #endif
     42 
     43 #include <dev/apm/apmbios.h>
     44 
     45 #include <machine/platid.h>
     46 #include <machine/platid_mask.h>
     47 #include <machine/config_hook.h>
     48 
     49 #include <sh3/exception.h>
     50 #include <sh3/intcreg.h>
     51 #include <sh3/pfcreg.h>
     52 
     53 #include <sh3/dev/adcvar.h>
     54 
     55 
     56 /* SH7709 PFC bits pertinent to Jornada 6x0 power */
     57 #define PGDR_MAIN_BATTERY_OUT	0x04
     58 #define PGDR_LID_OPEN		0x01
     59 #define PJDR_AC_POWER_OUT	0x10
     60 #define PLDR_BATTERY_CHARGED	0x20
     61 
     62 
     63 static inline int __attribute__((__always_inline__))
     64 j6x0pwr_battery_is_absent(void)
     65 {
     66 
     67 	return  _reg_read_1(SH7709_PGDR) & PGDR_MAIN_BATTERY_OUT;
     68 }
     69 
     70 static inline int __attribute__((__always_inline__))
     71 j6x0pwr_ac_is_off(void)
     72 {
     73 
     74 	return _reg_read_1(SH7709_PJDR) & PJDR_AC_POWER_OUT;
     75 }
     76 
     77 
     78 static inline int __attribute__((__always_inline__))
     79 j6x0pwr_is_not_charging(void)
     80 {
     81 
     82 	return _reg_read_1(SH7709_PLDR) & PLDR_BATTERY_CHARGED;
     83 }
     84 
     85 
     86 /* A/D converter channels to get power stats from */
     87 #define ADC_CHANNEL_BATTERY	3 /* main battery */
     88 #define ADC_CHANNEL_BACKUP	4 /* backup battery - we don't report it */
     89 #define ADC_CHANNEL_CHARGE	5 /* we use PLDR_BATTERY_CHARGED instead */
     90 
     91 /*
     92  * Empirical range of battery values.
     93  * Thanks to Joseph Heenan for measurements.
     94  */
     95 #define J6X0PWR_BATTERY_MIN		630
     96 #define J6X0PWR_BATTERY_CRITICAL	635
     97 #define J6X0PWR_BATTERY_LOW		645
     98 #define J6X0PWR_BATTERY_FULL		910 /* can be slightly more */
     99 
    100 
    101 /* Convenience aliases (XXX: should be provided by config_hook.h) */
    102 #define CONFIG_HOOK_BUTTON_PRESSED	((void *)1)
    103 #define CONFIG_HOOK_BUTTON_RELEASED	((void *)0)
    104 
    105 
    106 
    107 /* allow only one instance */
    108 static int j6x0pwr_attached = 0;
    109 
    110 struct j6x0pwr_softc {
    111 	struct device sc_dev;
    112 
    113 	void *sc_ih;
    114 	volatile int sc_poweroff;
    115 };
    116 
    117 static int	j6x0pwr_match(struct device *, struct cfdata *, void *);
    118 static void	j6x0pwr_attach(struct device *, struct device *, void *);
    119 
    120 CFATTACH_DECL(j6x0pwr, sizeof(struct j6x0pwr_softc),
    121     j6x0pwr_match, j6x0pwr_attach, NULL, NULL);
    122 
    123 
    124 static int	j6x0pwr_apm_getpower_hook(void *, int, long, void *);
    125 static int	j6x0pwr_get_battery(void); /* from ADC */
    126 
    127 static int	j6x0pwr_intr(void *);
    128 static void	j6x0pwr_sleep(void *);
    129 static int	j6x0pwr_clear_interrupt(void);
    130 
    131 
    132 static int
    133 j6x0pwr_match(struct device *parent, struct cfdata *cfp, void *aux)
    134 {
    135 
    136 	/*
    137 	 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
    138 	 * Is 620 wired similarly?
    139 	 */
    140 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
    141 		return 0;
    142 
    143 	if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
    144 		return 0;
    145 
    146 	/* allow only one instance */
    147 	return !j6x0pwr_attached;
    148 }
    149 
    150 
    151 static void
    152 j6x0pwr_attach(struct device *parent, struct device *self, void *aux)
    153 {
    154 	struct j6x0pwr_softc *sc = (void *)self;
    155 
    156 	/* XXX: in machdep.c */
    157 	extern void (*__sleep_func)(void *);
    158 	extern void *__sleep_ctx;
    159 
    160 	/* allow only one instance */
    161 	j6x0pwr_attached = 1;
    162 
    163 	/* arrange for hpcapm to call us when power status is requested */
    164 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
    165 		    CONFIG_HOOK_EXCLUSIVE,
    166 		    j6x0pwr_apm_getpower_hook, sc);
    167 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
    168 		    CONFIG_HOOK_EXCLUSIVE,
    169 		    j6x0pwr_apm_getpower_hook, sc);
    170 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
    171 		    CONFIG_HOOK_EXCLUSIVE,
    172 		    j6x0pwr_apm_getpower_hook, sc);
    173 
    174 	/* register sleep function to APM */
    175 	__sleep_func = j6x0pwr_sleep;
    176 	__sleep_ctx = self;
    177 
    178 	sc->sc_poweroff = 0;
    179 
    180 	/* drain the old interrupt */
    181 	j6x0pwr_clear_interrupt();
    182 
    183 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    184 	    j6x0pwr_intr, sc);
    185 
    186 	_reg_write_1(SH7709_PKDR, 0);	/* Green LED on */
    187 	printf("\n");
    188 }
    189 
    190 
    191 /*
    192  * Triggered when the On/Off button is pressed or the lid is closed.
    193  * The state of the lid is reflected in the bit PGDR[0].
    194  * Closing the lid can trigger several consecutive interrupts.
    195  *
    196  * XXX: Since we don't put the machine to sleep, I have no idea how
    197  * wakeup interrupt(s) look like.  Need to revisit when software
    198  * suspend is added to the kernel (which we need to support ACPI).
    199  */
    200 static int
    201 j6x0pwr_intr(void *self)
    202 {
    203 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    204 	uint8_t irr0;
    205 	uint8_t pgdr;
    206 
    207 	irr0 = j6x0pwr_clear_interrupt();
    208 	if ((irr0 & IRR0_IRQ0) == 0) {
    209 #ifdef DIAGNOSTIC
    210 		printf_nolog("%s: irr0=0x%02x?\n", sc->sc_dev.dv_xname, irr0);
    211 #endif
    212 		return 0;
    213 	}
    214 
    215 	pgdr = _reg_read_1(SH7709_PGDR);
    216 	if ((pgdr & PGDR_LID_OPEN) == 0) {
    217 		printf("%s: lid closed %d\n", sc->sc_dev.dv_xname,
    218 		    sc->sc_poweroff);
    219 		if (sc->sc_poweroff)
    220 			return 1;
    221 	} else {
    222 		printf("%s: ON/OFF %d\n", sc->sc_dev.dv_xname, sc->sc_poweroff);
    223 		if (sc->sc_poweroff)
    224 			sc->sc_poweroff = 0;
    225 	}
    226 
    227 	config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    228 			 CONFIG_HOOK_BUTTONEVENT_POWER,
    229 			 CONFIG_HOOK_BUTTON_PRESSED);
    230 	/* fake release */
    231 	config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    232 			 CONFIG_HOOK_BUTTONEVENT_POWER,
    233 			 CONFIG_HOOK_BUTTON_RELEASED);
    234 
    235 	return 1;
    236 }
    237 
    238 
    239 static int
    240 j6x0pwr_clear_interrupt(void)
    241 {
    242 	uint8_t irr0;
    243 
    244 	irr0 = _reg_read_1(SH7709_IRR0);
    245 	if (irr0 & IRR0_IRQ0)
    246 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
    247 
    248 	return irr0;
    249 }
    250 
    251 
    252 void
    253 j6x0pwr_sleep(void *self)
    254 {
    255 	/* splhigh on entry */
    256 	struct j6x0pwr_softc *sc = self;
    257 	int s;
    258 
    259 	/* Reinstall j6x0pwr_intr as a wakeup handler */
    260 	intc_intr_disestablish(sc->sc_ih);
    261 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_HIGH,
    262 	    j6x0pwr_intr, sc);
    263 	sc->sc_poweroff = 1;
    264 	do {
    265 		/* Disable interrupt except for power button. */
    266 		s = _cpu_intr_resume(IPL_CLOCK << 4);
    267 		_reg_write_1(SH7709_PKDR, 0xff);	/* Green LED off */
    268 
    269 		__asm volatile("sleep");
    270 
    271 		_reg_write_1(SH7709_PKDR, 0);		/* Green LED on */
    272 		_cpu_intr_resume(s);
    273 	} while (sc->sc_poweroff);
    274 
    275 	/* Return to normal power button */
    276 	intc_intr_disestablish(sc->sc_ih);
    277 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    278 	    j6x0pwr_intr, sc);
    279 	/* splhigh on exit */
    280 }
    281 
    282 
    283 static int
    284 j6x0pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
    285 {
    286 	/* struct j6x0pwr_softc * const sc = ctx; */
    287 	int * const pval = msg;
    288 	int battery, state;
    289 
    290 	if (type != CONFIG_HOOK_GET)
    291 		return EINVAL;
    292 
    293 	switch (id) {
    294 
    295 	case CONFIG_HOOK_ACADAPTER:
    296 		*pval = j6x0pwr_ac_is_off() ? APM_AC_OFF : APM_AC_ON;
    297 		return 0;
    298 
    299 	case CONFIG_HOOK_CHARGE:
    300 		if (j6x0pwr_battery_is_absent())
    301 			state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
    302 		else {
    303 			battery = j6x0pwr_get_battery();
    304 			if (battery < J6X0PWR_BATTERY_CRITICAL)
    305 				state = APM_BATT_FLAG_CRITICAL;
    306 			else if (battery < J6X0PWR_BATTERY_LOW)
    307 				state = APM_BATT_FLAG_LOW;
    308 			else
    309 				state = APM_BATT_FLAG_HIGH; /* XXX? */
    310 
    311 			if (!j6x0pwr_is_not_charging())
    312 				state |= APM_BATT_FLAG_CHARGING;
    313 		}
    314 		*pval = state;
    315 		return 0;
    316 
    317 	case CONFIG_HOOK_BATTERYVAL:
    318 		if (j6x0pwr_battery_is_absent())
    319 			state = 0;
    320 		else {
    321 			battery = j6x0pwr_get_battery();
    322 			if (battery > J6X0PWR_BATTERY_FULL)
    323 				state = 100;
    324 			else
    325 				state = 100 * (battery - J6X0PWR_BATTERY_MIN)
    326 					/ (J6X0PWR_BATTERY_FULL
    327 					   - J6X0PWR_BATTERY_MIN);
    328 		}
    329 		*pval = state;
    330 		return 0;
    331 	}
    332 
    333 	return EINVAL;
    334 }
    335 
    336 
    337 static int
    338 j6x0pwr_get_battery(void)
    339 {
    340 	int battery;
    341 	int s;
    342 
    343 	if (j6x0pwr_battery_is_absent())
    344 		return -1;
    345 
    346 	s = spltty();
    347 	battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    348 	splx(s);
    349 
    350 	return battery;
    351 }
    352