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