j6x0pwr.c revision 1.14 1 /* $NetBSD: j6x0pwr.c,v 1.14 2008/03/27 03:34:14 uwe Exp $ */
2
3 /*
4 * Copyright (c) 2003, 2006 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.14 2008/03/27 03:34:14 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
40 #include <dev/apm/apmbios.h>
41
42 #include <machine/platid.h>
43 #include <machine/platid_mask.h>
44 #include <machine/config_hook.h>
45
46 #include <sh3/exception.h>
47 #include <sh3/intcreg.h>
48 #include <sh3/pfcreg.h>
49
50 #include <sh3/dev/adcvar.h>
51
52
53 /* SH7709 PFC bits pertinent to Jornada 6x0 power */
54 #define PGDR_MAIN_BATTERY_OUT 0x04
55 #define PGDR_LID_OPEN 0x01
56 #define PJDR_AC_POWER_OUT 0x10
57 #define PLDR_BATTERY_CHARGED 0x20
58
59
60 static inline int __attribute__((__always_inline__))
61 j6x0pwr_battery_is_absent(void)
62 {
63
64 return _reg_read_1(SH7709_PGDR) & PGDR_MAIN_BATTERY_OUT;
65 }
66
67 static inline int __attribute__((__always_inline__))
68 j6x0pwr_ac_is_off(void)
69 {
70
71 return _reg_read_1(SH7709_PJDR) & PJDR_AC_POWER_OUT;
72 }
73
74
75 static inline int __attribute__((__always_inline__))
76 j6x0pwr_is_not_charging(void)
77 {
78
79 return _reg_read_1(SH7709_PLDR) & PLDR_BATTERY_CHARGED;
80 }
81
82
83 /* A/D converter channels to get power stats from */
84 #define ADC_CHANNEL_BATTERY 3 /* main battery */
85 #define ADC_CHANNEL_BACKUP 4 /* backup battery - we don't report it */
86 #define ADC_CHANNEL_CHARGE 5 /* we use PLDR_BATTERY_CHARGED instead */
87
88 /*
89 * Empirical range of battery values.
90 * Thanks to Joseph Heenan for measurements.
91 */
92 #define J6X0PWR_BATTERY_MIN 630
93 #define J6X0PWR_BATTERY_CRITICAL 635
94 #define J6X0PWR_BATTERY_LOW 645
95 #define J6X0PWR_BATTERY_FULL 910 /* can be slightly more */
96
97
98 /* Convenience aliases (XXX: should be provided by config_hook.h) */
99 #define CONFIG_HOOK_BUTTON_PRESSED ((void *)1)
100 #define CONFIG_HOOK_BUTTON_RELEASED ((void *)0)
101
102
103
104 /* allow only one instance */
105 static int j6x0pwr_attached = 0;
106
107 struct j6x0pwr_softc {
108 device_t sc_dev;
109
110 void *sc_ih;
111 volatile int sc_poweroff;
112 };
113
114 static int j6x0pwr_match(device_t, cfdata_t, void *);
115 static void j6x0pwr_attach(device_t, device_t, void *);
116
117 CFATTACH_DECL_NEW(j6x0pwr, sizeof(struct j6x0pwr_softc),
118 j6x0pwr_match, j6x0pwr_attach, NULL, NULL);
119
120
121 static int j6x0pwr_apm_getpower_hook(void *, int, long, void *);
122 static int j6x0pwr_get_battery(void); /* from ADC */
123
124 static int j6x0pwr_intr(void *);
125 static void j6x0pwr_sleep(void *);
126 static int j6x0pwr_clear_interrupt(void);
127
128
129 static int
130 j6x0pwr_match(device_t parent, cfdata_t cfp, void *aux)
131 {
132
133 /*
134 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
135 * Is 620 wired similarly?
136 */
137 if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
138 return 0;
139
140 if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
141 return 0;
142
143 /* allow only one instance */
144 return !j6x0pwr_attached;
145 }
146
147
148 static void
149 j6x0pwr_attach(device_t parent, device_t self, void *aux)
150 {
151 struct j6x0pwr_softc *sc;
152
153 /* XXX: in machdep.c */
154 extern void (*__sleep_func)(void *);
155 extern void *__sleep_ctx;
156
157 sc = device_private(self);
158 sc->sc_dev = self;
159
160 aprint_naive("\n");
161 aprint_normal("\n");
162
163 /* allow only one instance */
164 j6x0pwr_attached = 1;
165
166 /* arrange for hpcapm to call us when power status is requested */
167 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
168 CONFIG_HOOK_EXCLUSIVE,
169 j6x0pwr_apm_getpower_hook, sc);
170 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
171 CONFIG_HOOK_EXCLUSIVE,
172 j6x0pwr_apm_getpower_hook, sc);
173 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
174 CONFIG_HOOK_EXCLUSIVE,
175 j6x0pwr_apm_getpower_hook, sc);
176
177 /* register sleep function to APM */
178 __sleep_func = j6x0pwr_sleep;
179 __sleep_ctx = sc;
180
181 sc->sc_poweroff = 0;
182
183 /* drain the old interrupt */
184 j6x0pwr_clear_interrupt();
185
186 sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
187 j6x0pwr_intr, sc);
188
189 _reg_write_1(SH7709_PKDR, 0); /* Green LED on */
190 }
191
192
193 /*
194 * Triggered when the On/Off button is pressed or the lid is closed.
195 * The state of the lid is reflected in the bit PGDR[0].
196 * Closing the lid can trigger several consecutive interrupts.
197 *
198 * XXX: Since we don't put the machine to sleep, I have no idea how
199 * wakeup interrupt(s) look like. Need to revisit when software
200 * suspend is added to the kernel (which we need to support ACPI).
201 */
202 static int
203 j6x0pwr_intr(void *arg)
204 {
205 struct j6x0pwr_softc *sc = arg;
206 device_t self = sc->sc_dev;
207 uint8_t irr0;
208 uint8_t pgdr;
209
210 irr0 = j6x0pwr_clear_interrupt();
211 if ((irr0 & IRR0_IRQ0) == 0) {
212 #ifdef DIAGNOSTIC
213 aprint_normal_dev(self, "irr0=0x%02x?\n", irr0);
214 #endif
215 return 0;
216 }
217
218 pgdr = _reg_read_1(SH7709_PGDR);
219 if ((pgdr & PGDR_LID_OPEN) == 0) {
220 aprint_normal_dev(self, "lid closed %d\n", sc->sc_poweroff);
221 if (sc->sc_poweroff)
222 return 1;
223 } else {
224 aprint_normal_dev(self, "ON/OFF %d\n", sc->sc_poweroff);
225 if (sc->sc_poweroff)
226 sc->sc_poweroff = 0;
227 }
228
229 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
230 CONFIG_HOOK_BUTTONEVENT_POWER,
231 CONFIG_HOOK_BUTTON_PRESSED);
232 /* fake release */
233 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
234 CONFIG_HOOK_BUTTONEVENT_POWER,
235 CONFIG_HOOK_BUTTON_RELEASED);
236
237 return 1;
238 }
239
240
241 static int
242 j6x0pwr_clear_interrupt(void)
243 {
244 uint8_t irr0;
245
246 irr0 = _reg_read_1(SH7709_IRR0);
247 if (irr0 & IRR0_IRQ0)
248 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
249
250 return irr0;
251 }
252
253
254 void
255 j6x0pwr_sleep(void *arg)
256 {
257 /* splhigh on entry */
258 struct j6x0pwr_softc *sc = arg;
259 int s;
260
261 /* Reinstall j6x0pwr_intr as a wakeup handler */
262 intc_intr_disestablish(sc->sc_ih);
263 sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_HIGH,
264 j6x0pwr_intr, sc);
265 sc->sc_poweroff = 1;
266 do {
267 /* Disable interrupt except for power button. */
268 s = _cpu_intr_resume(IPL_CLOCK << 4);
269 _reg_write_1(SH7709_PKDR, 0xff); /* Green LED off */
270
271 __asm volatile("sleep");
272
273 _reg_write_1(SH7709_PKDR, 0); /* Green LED on */
274 _cpu_intr_resume(s);
275 } while (sc->sc_poweroff);
276
277 /* Return to normal power button */
278 intc_intr_disestablish(sc->sc_ih);
279 sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
280 j6x0pwr_intr, sc);
281 /* splhigh on exit */
282 }
283
284
285 static int
286 j6x0pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
287 {
288 /* struct j6x0pwr_softc * const sc = ctx; */
289 int * const pval = msg;
290 int battery, state;
291
292 if (type != CONFIG_HOOK_GET)
293 return EINVAL;
294
295 switch (id) {
296
297 case CONFIG_HOOK_ACADAPTER:
298 *pval = j6x0pwr_ac_is_off() ? APM_AC_OFF : APM_AC_ON;
299 return 0;
300
301 case CONFIG_HOOK_CHARGE:
302 if (j6x0pwr_battery_is_absent())
303 state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
304 else {
305 battery = j6x0pwr_get_battery();
306 if (battery < J6X0PWR_BATTERY_CRITICAL)
307 state = APM_BATT_FLAG_CRITICAL;
308 else if (battery < J6X0PWR_BATTERY_LOW)
309 state = APM_BATT_FLAG_LOW;
310 else
311 state = APM_BATT_FLAG_HIGH; /* XXX? */
312
313 if (!j6x0pwr_is_not_charging())
314 state |= APM_BATT_FLAG_CHARGING;
315 }
316 *pval = state;
317 return 0;
318
319 case CONFIG_HOOK_BATTERYVAL:
320 if (j6x0pwr_battery_is_absent())
321 state = 0;
322 else {
323 battery = j6x0pwr_get_battery();
324 if (battery > J6X0PWR_BATTERY_FULL)
325 state = 100;
326 else
327 state = 100 * (battery - J6X0PWR_BATTERY_MIN)
328 / (J6X0PWR_BATTERY_FULL
329 - J6X0PWR_BATTERY_MIN);
330 }
331 *pval = state;
332 return 0;
333 }
334
335 return EINVAL;
336 }
337
338
339 static int
340 j6x0pwr_get_battery(void)
341 {
342 int battery;
343 int s;
344
345 if (j6x0pwr_battery_is_absent())
346 return -1;
347
348 s = spltty();
349 battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
350 splx(s);
351
352 return battery;
353 }
354