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