Home | History | Annotate | Line # | Download | only in dev
j6x0pwr.c revision 1.9
      1  1.9    uwe /*	$NetBSD: j6x0pwr.c,v 1.9 2006/02/25 16:43:36 uwe 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.9    uwe __KERNEL_RCSID(0, "$NetBSD: j6x0pwr.c,v 1.9 2006/02/25 16:43:36 uwe 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.3    uwe #include <machine/platid.h>
     44  1.3    uwe #include <machine/platid_mask.h>
     45  1.4    uch #include <machine/config_hook.h>
     46  1.3    uwe 
     47  1.1    uwe #include <sh3/exception.h>
     48  1.1    uwe #include <sh3/intcreg.h>
     49  1.1    uwe #include <sh3/pfcreg.h>
     50  1.1    uwe 
     51  1.3    uwe #include <sh3/dev/adcvar.h>
     52  1.1    uwe 
     53  1.1    uwe 
     54  1.1    uwe /* SH7709_PGDR bits pertinent to Jornada 6x0 power */
     55  1.1    uwe #define PGDR_MAIN_BATTERY_OUT	0x04
     56  1.1    uwe #define PGDR_LID_OPEN		0x01
     57  1.1    uwe 
     58  1.1    uwe /* A/D covnerter channels to get power stats from */
     59  1.1    uwe #define ADC_CHANNEL_BATTERY	3
     60  1.1    uwe #define ADC_CHANNEL_BACKUP	4
     61  1.1    uwe #define ADC_CHANNEL_CHARGE	5
     62  1.1    uwe 
     63  1.1    uwe /* warn that main battery is low after drops below this value */
     64  1.1    uwe #define J6X0PWR_BATTERY_WARNING_THRESHOLD	200
     65  1.1    uwe 
     66  1.1    uwe 
     67  1.1    uwe struct j6x0pwr_softc {
     68  1.1    uwe 	struct device sc_dev;
     69  1.1    uwe 
     70  1.1    uwe 	struct callout sc_poll_ch;
     71  1.4    uch 	void *sc_ih;
     72  1.4    uch 	volatile int sc_poweroff;
     73  1.1    uwe };
     74  1.1    uwe 
     75  1.1    uwe static int	j6x0pwr_match(struct device *, struct cfdata *, void *);
     76  1.1    uwe static void	j6x0pwr_attach(struct device *, struct device *, void *);
     77  1.1    uwe 
     78  1.1    uwe CFATTACH_DECL(j6x0pwr, sizeof(struct j6x0pwr_softc),
     79  1.1    uwe     j6x0pwr_match, j6x0pwr_attach, NULL, NULL);
     80  1.1    uwe 
     81  1.1    uwe 
     82  1.1    uwe static int	j6x0pwr_intr(void *);
     83  1.1    uwe static void	j6x0pwr_poll_callout(void *);
     84  1.4    uch static void	j6x0pwr_sleep(void *);
     85  1.4    uch static int	j6x0pwr_clear_interrupt(void);
     86  1.1    uwe 
     87  1.1    uwe static int
     88  1.1    uwe j6x0pwr_match(struct device *parent, struct cfdata *cfp, void *aux)
     89  1.1    uwe {
     90  1.1    uwe 
     91  1.1    uwe 	/*
     92  1.1    uwe 	 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
     93  1.1    uwe 	 * Is 620 wired similarly?
     94  1.1    uwe 	 */
     95  1.1    uwe 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
     96  1.9    uwe 		return 0;
     97  1.1    uwe 
     98  1.1    uwe 	if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
     99  1.9    uwe 		return 0;
    100  1.1    uwe 
    101  1.9    uwe 	return 1;
    102  1.1    uwe }
    103  1.1    uwe 
    104  1.1    uwe 
    105  1.1    uwe static void
    106  1.1    uwe j6x0pwr_attach(struct device *parent, struct device *self, void *aux)
    107  1.1    uwe {
    108  1.9    uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    109  1.9    uwe 
    110  1.9    uwe 	/* XXX: in machdep.c */
    111  1.4    uch 	extern void (*__sleep_func)(void *);
    112  1.4    uch 	extern void *__sleep_ctx;
    113  1.1    uwe 
    114  1.4    uch 	/* regsiter sleep function to APM */
    115  1.4    uch 	__sleep_func = j6x0pwr_sleep;
    116  1.4    uch 	__sleep_ctx = self;
    117  1.9    uwe 
    118  1.4    uch 	sc->sc_poweroff = 0;
    119  1.4    uch 
    120  1.4    uch 	/* drain the old interrupt */
    121  1.4    uch 	j6x0pwr_clear_interrupt();
    122  1.4    uch 
    123  1.4    uch 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    124  1.4    uch 	    j6x0pwr_intr, sc);
    125  1.1    uwe 
    126  1.1    uwe 	callout_init(&sc->sc_poll_ch);
    127  1.1    uwe 	callout_reset(&sc->sc_poll_ch, 5 * hz,
    128  1.1    uwe 		      j6x0pwr_poll_callout, sc);
    129  1.1    uwe 
    130  1.5    uwe 	_reg_write_1(SH7709_PKDR, 0);	/* Green LED on */
    131  1.1    uwe 	printf("\n");
    132  1.1    uwe }
    133  1.1    uwe 
    134  1.1    uwe 
    135  1.1    uwe /*
    136  1.1    uwe  * Triggered when the On/Off button is pressed or the lid is closed.
    137  1.6    uwe  * The state of the lid is reflected in the bit PGDR[0].
    138  1.1    uwe  * Closing the lid can trigger several consecutive interrupts.
    139  1.1    uwe  *
    140  1.1    uwe  * XXX: Since we don't put the machine to sleep, I have no idea how
    141  1.1    uwe  * wakeup interrupt(s) look like.  Need to revisit when software
    142  1.1    uwe  * suspend is added to the kernel (which we need to support ACPI).
    143  1.1    uwe  */
    144  1.1    uwe static int
    145  1.1    uwe j6x0pwr_intr(void *self)
    146  1.1    uwe {
    147  1.1    uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    148  1.1    uwe 	uint8_t irr0;
    149  1.1    uwe 	uint8_t pgdr;
    150  1.1    uwe 
    151  1.9    uwe 	irr0 = j6x0pwr_clear_interrupt();
    152  1.9    uwe 	if ((irr0 & IRR0_IRQ0) == 0) {
    153  1.1    uwe #ifdef DIAGNOSTIC
    154  1.1    uwe 		printf_nolog("%s: irr0=0x%02x?\n", sc->sc_dev.dv_xname, irr0);
    155  1.1    uwe #endif
    156  1.9    uwe 		return 0;
    157  1.1    uwe 	}
    158  1.1    uwe 
    159  1.4    uch 	pgdr = _reg_read_1(SH7709_PGDR);
    160  1.4    uch 	if ((pgdr & PGDR_LID_OPEN) == 0) {
    161  1.4    uch 		printf("%s: lid closed %d\n", sc->sc_dev.dv_xname,
    162  1.4    uch 		    sc->sc_poweroff);
    163  1.4    uch 		if (sc->sc_poweroff)
    164  1.4    uch 			return 1;
    165  1.4    uch 	} else {
    166  1.4    uch 		printf("%s: ON/OFF %d\n", sc->sc_dev.dv_xname, sc->sc_poweroff);
    167  1.4    uch 		if (sc->sc_poweroff)
    168  1.4    uch 			sc->sc_poweroff = 0;
    169  1.4    uch 	}
    170  1.1    uwe 
    171  1.4    uch 	/* push */
    172  1.4    uch 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, CONFIG_HOOK_BUTTONEVENT_POWER,
    173  1.4    uch 	    (void *)1);
    174  1.4    uch 	/* release (fake) */
    175  1.4    uch 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, CONFIG_HOOK_BUTTONEVENT_POWER,
    176  1.4    uch 	    (void *)0);
    177  1.1    uwe 
    178  1.9    uwe 	return 1;
    179  1.1    uwe }
    180  1.1    uwe 
    181  1.4    uch static int
    182  1.4    uch j6x0pwr_clear_interrupt()
    183  1.4    uch {
    184  1.4    uch 	uint8_t irr0;
    185  1.4    uch 
    186  1.4    uch 	irr0 = _reg_read_1(SH7709_IRR0);
    187  1.4    uch 	if (irr0 & IRR0_IRQ0)
    188  1.4    uch 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
    189  1.4    uch 
    190  1.4    uch 	return irr0;
    191  1.4    uch }
    192  1.4    uch 
    193  1.4    uch void
    194  1.4    uch j6x0pwr_sleep(void *self)
    195  1.4    uch {
    196  1.4    uch 	/* splhigh on entry */
    197  1.4    uch 	struct j6x0pwr_softc *sc = self;
    198  1.4    uch 	int s;
    199  1.4    uch 
    200  1.4    uch 	/* Reinstall j6x0pwr_intr as a wakeup handler */
    201  1.4    uch 	intc_intr_disestablish(sc->sc_ih);
    202  1.4    uch 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_HIGH,
    203  1.4    uch 	    j6x0pwr_intr, sc);
    204  1.4    uch 	sc->sc_poweroff = 1;
    205  1.4    uch 	do {
    206  1.4    uch 		/* Disable interrupt except for power button. */
    207  1.4    uch 		s = _cpu_intr_resume(IPL_CLOCK << 4);
    208  1.5    uwe 		_reg_write_1(SH7709_PKDR, 0xff);	/* Green LED off */
    209  1.9    uwe 
    210  1.8  perry 		__asm volatile("sleep");
    211  1.9    uwe 
    212  1.5    uwe 		_reg_write_1(SH7709_PKDR, 0);		/* Green LED on */
    213  1.4    uch 		_cpu_intr_resume(s);
    214  1.4    uch 	} while (sc->sc_poweroff);
    215  1.4    uch 
    216  1.4    uch 	/* Return to normal power button */
    217  1.4    uch 	intc_intr_disestablish(sc->sc_ih);
    218  1.4    uch 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    219  1.4    uch 	    j6x0pwr_intr, sc);
    220  1.4    uch 	/* splhigh on exit */
    221  1.4    uch }
    222  1.1    uwe 
    223  1.1    uwe volatile int j6x0pwr_poll_verbose = 0; /* XXX: tweak from ddb */
    224  1.1    uwe 
    225  1.1    uwe static void
    226  1.1    uwe j6x0pwr_poll_callout(void *self)
    227  1.1    uwe {
    228  1.1    uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    229  1.1    uwe 	int battery, backup, charging;
    230  1.1    uwe 	uint8_t pgdr;
    231  1.1    uwe 	int s;
    232  1.1    uwe 
    233  1.1    uwe 	pgdr = _reg_read_1(SH7709_PGDR);
    234  1.1    uwe 
    235  1.3    uwe 	/* just check main battery charge if not verbose and battery is in */
    236  1.1    uwe 	if (!j6x0pwr_poll_verbose) {
    237  1.1    uwe 		if (pgdr & PGDR_MAIN_BATTERY_OUT)
    238  1.1    uwe 			goto reschedule;
    239  1.1    uwe 		else {
    240  1.1    uwe 			s = spltty();
    241  1.1    uwe 			battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    242  1.1    uwe 			splx(s);
    243  1.1    uwe 			goto check_battery;
    244  1.1    uwe 		}
    245  1.1    uwe 	}
    246  1.1    uwe 
    247  1.1    uwe 	s = spltty();
    248  1.1    uwe 	battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    249  1.1    uwe 	splx(s);
    250  1.1    uwe 
    251  1.1    uwe 	s = spltty();
    252  1.1    uwe 	backup = adc_sample_channel(ADC_CHANNEL_BACKUP);
    253  1.1    uwe 	splx(s);
    254  1.1    uwe 
    255  1.1    uwe 	s = spltty();
    256  1.1    uwe 	charging = adc_sample_channel(ADC_CHANNEL_CHARGE);
    257  1.1    uwe 	splx(s);
    258  1.1    uwe 
    259  1.1    uwe 	printf_nolog("%s: main=", sc->sc_dev.dv_xname);
    260  1.1    uwe 	if (pgdr & PGDR_MAIN_BATTERY_OUT)
    261  1.1    uwe 		printf_nolog("<no>");
    262  1.1    uwe 	else
    263  1.1    uwe 		printf_nolog("%-4d", battery);
    264  1.1    uwe 
    265  1.1    uwe 	printf_nolog(" bkup=%-4d", backup);
    266  1.1    uwe 
    267  1.1    uwe 	/*
    268  1.1    uwe 	 * When main battery is being charged, ADC_CHANNEL_CHARGE
    269  1.1    uwe 	 * samples at almost zero.  It samples at 2^10 otherwise.
    270  1.1    uwe 	 */
    271  1.1    uwe 	if (charging < 8)
    272  1.1    uwe 		printf_nolog("charging");
    273  1.1    uwe 
    274  1.1    uwe 	printf_nolog("\n");
    275  1.1    uwe 
    276  1.1    uwe   check_battery:
    277  1.1    uwe 	if (!(pgdr & PGDR_MAIN_BATTERY_OUT)
    278  1.1    uwe 	    && (battery < J6X0PWR_BATTERY_WARNING_THRESHOLD))
    279  1.1    uwe 		printf("%s: WARNING: main battery %d is low!\n",
    280  1.1    uwe 		       sc->sc_dev.dv_xname, battery);
    281  1.1    uwe 
    282  1.1    uwe   reschedule:
    283  1.1    uwe 	callout_schedule(&sc->sc_poll_ch, 5*hz);
    284  1.1    uwe }
    285