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