Home | History | Annotate | Line # | Download | only in dev
j6x0pwr.c revision 1.5
      1  1.5  uwe /*	$NetBSD: j6x0pwr.c,v 1.5 2004/07/04 23:19: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.5  uwe __KERNEL_RCSID(0, "$NetBSD: j6x0pwr.c,v 1.5 2004/07/04 23:19: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.1  uwe 		return (0);
     97  1.1  uwe 
     98  1.1  uwe 	if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
     99  1.1  uwe 		return (0);
    100  1.1  uwe 
    101  1.1  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.4  uch 	extern void (*__sleep_func)(void *);
    109  1.4  uch 	extern void *__sleep_ctx;
    110  1.1  uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    111  1.1  uwe 
    112  1.4  uch 	/* regsiter sleep function to APM */
    113  1.4  uch 	__sleep_func = j6x0pwr_sleep;
    114  1.4  uch 	__sleep_ctx = self;
    115  1.4  uch 	sc->sc_poweroff = 0;
    116  1.4  uch 
    117  1.4  uch 	/* drain the old interrupt */
    118  1.4  uch 	j6x0pwr_clear_interrupt();
    119  1.4  uch 
    120  1.4  uch 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    121  1.4  uch 	    j6x0pwr_intr, sc);
    122  1.1  uwe 
    123  1.1  uwe 	callout_init(&sc->sc_poll_ch);
    124  1.1  uwe 	callout_reset(&sc->sc_poll_ch, 5 * hz,
    125  1.1  uwe 		      j6x0pwr_poll_callout, sc);
    126  1.1  uwe 
    127  1.5  uwe 	_reg_write_1(SH7709_PKDR, 0);	/* Green LED on */
    128  1.1  uwe 	printf("\n");
    129  1.1  uwe }
    130  1.1  uwe 
    131  1.1  uwe 
    132  1.1  uwe /*
    133  1.1  uwe  * Triggered when the On/Off button is pressed or the lid is closed.
    134  1.1  uwe  * The state of the lid is reflected in the bit PGDR[2].
    135  1.1  uwe  * Closing the lid can trigger several consecutive interrupts.
    136  1.1  uwe  *
    137  1.1  uwe  * XXX: Since we don't put the machine to sleep, I have no idea how
    138  1.1  uwe  * wakeup interrupt(s) look like.  Need to revisit when software
    139  1.1  uwe  * suspend is added to the kernel (which we need to support ACPI).
    140  1.1  uwe  */
    141  1.1  uwe static int
    142  1.1  uwe j6x0pwr_intr(void *self)
    143  1.1  uwe {
    144  1.1  uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    145  1.1  uwe 	uint8_t irr0;
    146  1.1  uwe 	uint8_t pgdr;
    147  1.1  uwe 
    148  1.4  uch 	if (((irr0 = j6x0pwr_clear_interrupt()) & IRR0_IRQ0) == 0) {
    149  1.1  uwe #ifdef DIAGNOSTIC
    150  1.1  uwe 		printf_nolog("%s: irr0=0x%02x?\n", sc->sc_dev.dv_xname, irr0);
    151  1.1  uwe #endif
    152  1.1  uwe 		return (0);
    153  1.1  uwe 	}
    154  1.1  uwe 
    155  1.4  uch 	pgdr = _reg_read_1(SH7709_PGDR);
    156  1.4  uch 	if ((pgdr & PGDR_LID_OPEN) == 0) {
    157  1.4  uch 		printf("%s: lid closed %d\n", sc->sc_dev.dv_xname,
    158  1.4  uch 		    sc->sc_poweroff);
    159  1.4  uch 		if (sc->sc_poweroff)
    160  1.4  uch 			return 1;
    161  1.4  uch 	} else {
    162  1.4  uch 		printf("%s: ON/OFF %d\n", sc->sc_dev.dv_xname, sc->sc_poweroff);
    163  1.4  uch 		if (sc->sc_poweroff)
    164  1.4  uch 			sc->sc_poweroff = 0;
    165  1.4  uch 	}
    166  1.1  uwe 
    167  1.4  uch 	/* push */
    168  1.4  uch 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, CONFIG_HOOK_BUTTONEVENT_POWER,
    169  1.4  uch 	    (void *)1);
    170  1.4  uch 	/* release (fake) */
    171  1.4  uch 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, CONFIG_HOOK_BUTTONEVENT_POWER,
    172  1.4  uch 	    (void *)0);
    173  1.1  uwe 
    174  1.1  uwe 	return (1);
    175  1.1  uwe }
    176  1.1  uwe 
    177  1.4  uch static int
    178  1.4  uch j6x0pwr_clear_interrupt()
    179  1.4  uch {
    180  1.4  uch 	uint8_t irr0;
    181  1.4  uch 
    182  1.4  uch 	irr0 = _reg_read_1(SH7709_IRR0);
    183  1.4  uch 	if (irr0 & IRR0_IRQ0)
    184  1.4  uch 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
    185  1.4  uch 
    186  1.4  uch 	return irr0;
    187  1.4  uch }
    188  1.4  uch 
    189  1.4  uch void
    190  1.4  uch j6x0pwr_sleep(void *self)
    191  1.4  uch {
    192  1.4  uch 	/* splhigh on entry */
    193  1.4  uch 	struct j6x0pwr_softc *sc = self;
    194  1.4  uch 	int s;
    195  1.4  uch 
    196  1.4  uch 	/* Reinstall j6x0pwr_intr as a wakeup handler */
    197  1.4  uch 	intc_intr_disestablish(sc->sc_ih);
    198  1.4  uch 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_HIGH,
    199  1.4  uch 	    j6x0pwr_intr, sc);
    200  1.4  uch 	sc->sc_poweroff = 1;
    201  1.4  uch 	do {
    202  1.4  uch 		/* Disable interrupt except for power button. */
    203  1.4  uch 		s = _cpu_intr_resume(IPL_CLOCK << 4);
    204  1.5  uwe 		_reg_write_1(SH7709_PKDR, 0xff);	/* Green LED off */
    205  1.4  uch 		__asm__ __volatile__("sleep");
    206  1.5  uwe 		_reg_write_1(SH7709_PKDR, 0);		/* Green LED on */
    207  1.4  uch 		_cpu_intr_resume(s);
    208  1.4  uch 	} while (sc->sc_poweroff);
    209  1.4  uch 
    210  1.4  uch 	/* Return to normal power button */
    211  1.4  uch 	intc_intr_disestablish(sc->sc_ih);
    212  1.4  uch 	sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    213  1.4  uch 	    j6x0pwr_intr, sc);
    214  1.4  uch 	/* splhigh on exit */
    215  1.4  uch }
    216  1.1  uwe 
    217  1.1  uwe volatile int j6x0pwr_poll_verbose = 0; /* XXX: tweak from ddb */
    218  1.1  uwe 
    219  1.1  uwe static void
    220  1.1  uwe j6x0pwr_poll_callout(void *self)
    221  1.1  uwe {
    222  1.1  uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    223  1.1  uwe 	int battery, backup, charging;
    224  1.1  uwe 	uint8_t pgdr;
    225  1.1  uwe 	int s;
    226  1.1  uwe 
    227  1.1  uwe 	pgdr = _reg_read_1(SH7709_PGDR);
    228  1.1  uwe 
    229  1.3  uwe 	/* just check main battery charge if not verbose and battery is in */
    230  1.1  uwe 	if (!j6x0pwr_poll_verbose) {
    231  1.1  uwe 		if (pgdr & PGDR_MAIN_BATTERY_OUT)
    232  1.1  uwe 			goto reschedule;
    233  1.1  uwe 		else {
    234  1.1  uwe 			s = spltty();
    235  1.1  uwe 			battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    236  1.1  uwe 			splx(s);
    237  1.1  uwe 			goto check_battery;
    238  1.1  uwe 		}
    239  1.1  uwe 	}
    240  1.1  uwe 
    241  1.1  uwe 	s = spltty();
    242  1.1  uwe 	battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    243  1.1  uwe 	splx(s);
    244  1.1  uwe 
    245  1.1  uwe 	s = spltty();
    246  1.1  uwe 	backup = adc_sample_channel(ADC_CHANNEL_BACKUP);
    247  1.1  uwe 	splx(s);
    248  1.1  uwe 
    249  1.1  uwe 	s = spltty();
    250  1.1  uwe 	charging = adc_sample_channel(ADC_CHANNEL_CHARGE);
    251  1.1  uwe 	splx(s);
    252  1.1  uwe 
    253  1.1  uwe 	printf_nolog("%s: main=", sc->sc_dev.dv_xname);
    254  1.1  uwe 	if (pgdr & PGDR_MAIN_BATTERY_OUT)
    255  1.1  uwe 		printf_nolog("<no>");
    256  1.1  uwe 	else
    257  1.1  uwe 		printf_nolog("%-4d", battery);
    258  1.1  uwe 
    259  1.1  uwe 	printf_nolog(" bkup=%-4d", backup);
    260  1.1  uwe 
    261  1.1  uwe 	/*
    262  1.1  uwe 	 * When main battery is being charged, ADC_CHANNEL_CHARGE
    263  1.1  uwe 	 * samples at almost zero.  It samples at 2^10 otherwise.
    264  1.1  uwe 	 */
    265  1.1  uwe 	if (charging < 8)
    266  1.1  uwe 		printf_nolog("charging");
    267  1.1  uwe 
    268  1.1  uwe 	printf_nolog("\n");
    269  1.1  uwe 
    270  1.1  uwe   check_battery:
    271  1.1  uwe 	if (!(pgdr & PGDR_MAIN_BATTERY_OUT)
    272  1.1  uwe 	    && (battery < J6X0PWR_BATTERY_WARNING_THRESHOLD))
    273  1.1  uwe 		printf("%s: WARNING: main battery %d is low!\n",
    274  1.1  uwe 		       sc->sc_dev.dv_xname, battery);
    275  1.1  uwe 
    276  1.1  uwe   reschedule:
    277  1.1  uwe 	callout_schedule(&sc->sc_poll_ch, 5*hz);
    278  1.1  uwe }
    279