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