Home | History | Annotate | Line # | Download | only in dev
j6x0pwr.c revision 1.1
      1  1.1  uwe /*	$NetBSD: j6x0pwr.c,v 1.1 2003/10/18 01:38:39 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.1  uwe 
     30  1.1  uwe #include <sys/param.h>
     31  1.1  uwe #include <sys/kernel.h>
     32  1.1  uwe #include <sys/device.h>
     33  1.1  uwe #include <sys/malloc.h>
     34  1.1  uwe #include <sys/systm.h>
     35  1.1  uwe #include <sys/callout.h>
     36  1.1  uwe #ifdef GPROF
     37  1.1  uwe #include <sys/gmon.h>
     38  1.1  uwe #endif
     39  1.1  uwe 
     40  1.1  uwe #include <sh3/exception.h>
     41  1.1  uwe #include <sh3/intcreg.h>
     42  1.1  uwe #include <sh3/pfcreg.h>
     43  1.1  uwe 
     44  1.1  uwe #include <machine/platid.h>
     45  1.1  uwe #include <machine/platid_mask.h>
     46  1.1  uwe 
     47  1.1  uwe 
     48  1.1  uwe /* SH7709_PGDR bits pertinent to Jornada 6x0 power */
     49  1.1  uwe #define PGDR_MAIN_BATTERY_OUT	0x04
     50  1.1  uwe #define PGDR_LID_OPEN		0x01
     51  1.1  uwe 
     52  1.1  uwe /* A/D covnerter channels to get power stats from */
     53  1.1  uwe #define ADC_CHANNEL_BATTERY	3
     54  1.1  uwe #define ADC_CHANNEL_BACKUP	4
     55  1.1  uwe #define ADC_CHANNEL_CHARGE	5
     56  1.1  uwe 
     57  1.1  uwe /* warn that main battery is low after drops below this value */
     58  1.1  uwe #define J6X0PWR_BATTERY_WARNING_THRESHOLD	200
     59  1.1  uwe 
     60  1.1  uwe 
     61  1.1  uwe extern int	adc_sample_channel(int); /* XXX: adcvar.h */
     62  1.1  uwe 
     63  1.1  uwe struct j6x0pwr_softc {
     64  1.1  uwe 	struct device sc_dev;
     65  1.1  uwe 
     66  1.1  uwe 	struct callout sc_poll_ch;
     67  1.1  uwe };
     68  1.1  uwe 
     69  1.1  uwe static int	j6x0pwr_match(struct device *, struct cfdata *, void *);
     70  1.1  uwe static void	j6x0pwr_attach(struct device *, struct device *, void *);
     71  1.1  uwe 
     72  1.1  uwe CFATTACH_DECL(j6x0pwr, sizeof(struct j6x0pwr_softc),
     73  1.1  uwe     j6x0pwr_match, j6x0pwr_attach, NULL, NULL);
     74  1.1  uwe 
     75  1.1  uwe 
     76  1.1  uwe static int	j6x0pwr_intr(void *);
     77  1.1  uwe static void	j6x0pwr_poll_callout(void *);
     78  1.1  uwe 
     79  1.1  uwe 
     80  1.1  uwe static int
     81  1.1  uwe j6x0pwr_match(struct device *parent, struct cfdata *cfp, void *aux)
     82  1.1  uwe {
     83  1.1  uwe 
     84  1.1  uwe 	/*
     85  1.1  uwe 	 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
     86  1.1  uwe 	 * Is 620 wired similarly?
     87  1.1  uwe 	 */
     88  1.1  uwe 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
     89  1.1  uwe 		return (0);
     90  1.1  uwe 
     91  1.1  uwe 	if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
     92  1.1  uwe 		return (0);
     93  1.1  uwe 
     94  1.1  uwe 	return (1);
     95  1.1  uwe }
     96  1.1  uwe 
     97  1.1  uwe 
     98  1.1  uwe static void
     99  1.1  uwe j6x0pwr_attach(struct device *parent, struct device *self, void *aux)
    100  1.1  uwe {
    101  1.1  uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    102  1.1  uwe 
    103  1.1  uwe 	intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
    104  1.1  uwe 			    j6x0pwr_intr, sc);
    105  1.1  uwe 
    106  1.1  uwe 	callout_init(&sc->sc_poll_ch);
    107  1.1  uwe 	callout_reset(&sc->sc_poll_ch, 5 * hz,
    108  1.1  uwe 		      j6x0pwr_poll_callout, sc);
    109  1.1  uwe 
    110  1.1  uwe 	printf("\n");
    111  1.1  uwe }
    112  1.1  uwe 
    113  1.1  uwe 
    114  1.1  uwe /*
    115  1.1  uwe  * Triggered when the On/Off button is pressed or the lid is closed.
    116  1.1  uwe  * The state of the lid is reflected in the bit PGDR[2].
    117  1.1  uwe  * Closing the lid can trigger several consecutive interrupts.
    118  1.1  uwe  *
    119  1.1  uwe  * XXX: Since we don't put the machine to sleep, I have no idea how
    120  1.1  uwe  * wakeup interrupt(s) look like.  Need to revisit when software
    121  1.1  uwe  * suspend is added to the kernel (which we need to support ACPI).
    122  1.1  uwe  */
    123  1.1  uwe static int
    124  1.1  uwe j6x0pwr_intr(void *self)
    125  1.1  uwe {
    126  1.1  uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    127  1.1  uwe 	uint8_t irr0;
    128  1.1  uwe 	uint8_t pgdr;
    129  1.1  uwe 
    130  1.1  uwe 	irr0 = _reg_read_1(SH7709_IRR0);
    131  1.1  uwe 	if ((irr0 & IRR0_IRQ0) == 0) {
    132  1.1  uwe #ifdef DIAGNOSTIC
    133  1.1  uwe 		printf_nolog("%s: irr0=0x%02x?\n", sc->sc_dev.dv_xname, irr0);
    134  1.1  uwe #endif
    135  1.1  uwe 		return (0);
    136  1.1  uwe 	}
    137  1.1  uwe 
    138  1.1  uwe 	/* clear the interrupt */
    139  1.1  uwe 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
    140  1.1  uwe 
    141  1.1  uwe 	pgdr = _reg_read_1(SH7709_PGDR);
    142  1.1  uwe 	if ((pgdr & PGDR_LID_OPEN) == 0)
    143  1.1  uwe 		printf("%s: lid closed\n", sc->sc_dev.dv_xname);
    144  1.1  uwe 	else
    145  1.1  uwe 		printf("%s: ON/OFF\n", sc->sc_dev.dv_xname);
    146  1.1  uwe 
    147  1.1  uwe 	return (1);
    148  1.1  uwe }
    149  1.1  uwe 
    150  1.1  uwe 
    151  1.1  uwe volatile int j6x0pwr_poll_verbose = 0; /* XXX: tweak from ddb */
    152  1.1  uwe 
    153  1.1  uwe static void
    154  1.1  uwe j6x0pwr_poll_callout(void *self)
    155  1.1  uwe {
    156  1.1  uwe 	struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
    157  1.1  uwe 	int battery, backup, charging;
    158  1.1  uwe 	uint8_t pgdr;
    159  1.1  uwe 	int s;
    160  1.1  uwe 
    161  1.1  uwe 	pgdr = _reg_read_1(SH7709_PGDR);
    162  1.1  uwe 
    163  1.1  uwe 	/* just check main battery charge it not verbose and battery is in */
    164  1.1  uwe 	if (!j6x0pwr_poll_verbose) {
    165  1.1  uwe 		if (pgdr & PGDR_MAIN_BATTERY_OUT)
    166  1.1  uwe 			goto reschedule;
    167  1.1  uwe 		else {
    168  1.1  uwe 			s = spltty();
    169  1.1  uwe 			battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    170  1.1  uwe 			splx(s);
    171  1.1  uwe 			goto check_battery;
    172  1.1  uwe 		}
    173  1.1  uwe 	}
    174  1.1  uwe 
    175  1.1  uwe 	s = spltty();
    176  1.1  uwe 	battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
    177  1.1  uwe 	splx(s);
    178  1.1  uwe 
    179  1.1  uwe 	s = spltty();
    180  1.1  uwe 	backup = adc_sample_channel(ADC_CHANNEL_BACKUP);
    181  1.1  uwe 	splx(s);
    182  1.1  uwe 
    183  1.1  uwe 	s = spltty();
    184  1.1  uwe 	charging = adc_sample_channel(ADC_CHANNEL_CHARGE);
    185  1.1  uwe 	splx(s);
    186  1.1  uwe 
    187  1.1  uwe 	printf_nolog("%s: main=", sc->sc_dev.dv_xname);
    188  1.1  uwe 	if (pgdr & PGDR_MAIN_BATTERY_OUT)
    189  1.1  uwe 		printf_nolog("<no>");
    190  1.1  uwe 	else
    191  1.1  uwe 		printf_nolog("%-4d", battery);
    192  1.1  uwe 
    193  1.1  uwe 	printf_nolog(" bkup=%-4d", backup);
    194  1.1  uwe 
    195  1.1  uwe 	/*
    196  1.1  uwe 	 * When main battery is being charged, ADC_CHANNEL_CHARGE
    197  1.1  uwe 	 * samples at almost zero.  It samples at 2^10 otherwise.
    198  1.1  uwe 	 */
    199  1.1  uwe 	if (charging < 8)
    200  1.1  uwe 		printf_nolog("charging");
    201  1.1  uwe 
    202  1.1  uwe 	printf_nolog("\n");
    203  1.1  uwe 
    204  1.1  uwe   check_battery:
    205  1.1  uwe 	if (!(pgdr & PGDR_MAIN_BATTERY_OUT)
    206  1.1  uwe 	    && (battery < J6X0PWR_BATTERY_WARNING_THRESHOLD))
    207  1.1  uwe 		printf("%s: WARNING: main battery %d is low!\n",
    208  1.1  uwe 		       sc->sc_dev.dv_xname, battery);
    209  1.1  uwe 
    210  1.1  uwe   reschedule:
    211  1.1  uwe 	callout_schedule(&sc->sc_poll_ch, 5*hz);
    212  1.1  uwe }
    213