1 /* $NetBSD: j6x0pwr.c,v 1.14 2008/03/27 03:34:14 uwe Exp $ */
4 * Copyright (c) 2003, 2006 Valeriy E. Ushakov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: j6x0pwr.c,v 1.14 2008/03/27 03:34:14 uwe Exp $");
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>
40 #include <dev/apm/apmbios.h>
42 #include <machine/platid.h>
43 #include <machine/platid_mask.h>
44 #include <machine/config_hook.h>
46 #include <sh3/exception.h>
47 #include <sh3/intcreg.h>
48 #include <sh3/pfcreg.h>
50 #include <sh3/dev/adcvar.h>
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
60 static inline int __attribute__((__always_inline__
))
61 j6x0pwr_battery_is_absent(void)
64 return _reg_read_1(SH7709_PGDR
) & PGDR_MAIN_BATTERY_OUT
;
67 static inline int __attribute__((__always_inline__
))
68 j6x0pwr_ac_is_off(void)
71 return _reg_read_1(SH7709_PJDR
) & PJDR_AC_POWER_OUT
;
75 static inline int __attribute__((__always_inline__
))
76 j6x0pwr_is_not_charging(void)
79 return _reg_read_1(SH7709_PLDR
) & PLDR_BATTERY_CHARGED
;
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 */
89 * Empirical range of battery values.
90 * Thanks to Joseph Heenan for measurements.
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 */
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)
104 /* allow only one instance */
105 static int j6x0pwr_attached
= 0;
107 struct j6x0pwr_softc
{
111 volatile int sc_poweroff
;
114 static int j6x0pwr_match(device_t
, cfdata_t
, void *);
115 static void j6x0pwr_attach(device_t
, device_t
, void *);
117 CFATTACH_DECL_NEW(j6x0pwr
, sizeof(struct j6x0pwr_softc
),
118 j6x0pwr_match
, j6x0pwr_attach
, NULL
, NULL
);
121 static int j6x0pwr_apm_getpower_hook(void *, int, long, void *);
122 static int j6x0pwr_get_battery(void); /* from ADC */
124 static int j6x0pwr_intr(void *);
125 static void j6x0pwr_sleep(void *);
126 static int j6x0pwr_clear_interrupt(void);
130 j6x0pwr_match(device_t parent
, cfdata_t cfp
, void *aux
)
134 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
135 * Is 620 wired similarly?
137 if (!platid_match(&platid
, &platid_mask_MACH_HP_JORNADA_6XX
))
140 if (strcmp(cfp
->cf_name
, "j6x0pwr") != 0)
143 /* allow only one instance */
144 return !j6x0pwr_attached
;
149 j6x0pwr_attach(device_t parent
, device_t self
, void *aux
)
151 struct j6x0pwr_softc
*sc
;
153 /* XXX: in machdep.c */
154 extern void (*__sleep_func
)(void *);
155 extern void *__sleep_ctx
;
157 sc
= device_private(self
);
163 /* allow only one instance */
164 j6x0pwr_attached
= 1;
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
);
177 /* register sleep function to APM */
178 __sleep_func
= j6x0pwr_sleep
;
183 /* drain the old interrupt */
184 j6x0pwr_clear_interrupt();
186 sc
->sc_ih
= intc_intr_establish(SH7709_INTEVT2_IRQ0
, IST_EDGE
, IPL_TTY
,
189 _reg_write_1(SH7709_PKDR
, 0); /* Green LED on */
191 if (!pmf_device_register(self
, NULL
, NULL
))
192 aprint_error_dev(self
, "unable to establish power handler\n");
197 * Triggered when the On/Off button is pressed or the lid is closed.
198 * The state of the lid is reflected in the bit PGDR[0].
199 * Closing the lid can trigger several consecutive interrupts.
201 * XXX: Since we don't put the machine to sleep, I have no idea how
202 * wakeup interrupt(s) look like. Need to revisit when software
203 * suspend is added to the kernel (which we need to support ACPI).
206 j6x0pwr_intr(void *arg
)
208 struct j6x0pwr_softc
*sc
= arg
;
209 device_t self
= sc
->sc_dev
;
213 irr0
= j6x0pwr_clear_interrupt();
214 if ((irr0
& IRR0_IRQ0
) == 0) {
216 aprint_normal_dev(self
, "irr0=0x%02x?\n", irr0
);
221 pgdr
= _reg_read_1(SH7709_PGDR
);
222 if ((pgdr
& PGDR_LID_OPEN
) == 0) {
223 aprint_normal_dev(self
, "lid closed %d\n", sc
->sc_poweroff
);
227 aprint_normal_dev(self
, "ON/OFF %d\n", sc
->sc_poweroff
);
232 config_hook_call(CONFIG_HOOK_BUTTONEVENT
,
233 CONFIG_HOOK_BUTTONEVENT_POWER
,
234 CONFIG_HOOK_BUTTON_PRESSED
);
236 config_hook_call(CONFIG_HOOK_BUTTONEVENT
,
237 CONFIG_HOOK_BUTTONEVENT_POWER
,
238 CONFIG_HOOK_BUTTON_RELEASED
);
245 j6x0pwr_clear_interrupt(void)
249 irr0
= _reg_read_1(SH7709_IRR0
);
250 if (irr0
& IRR0_IRQ0
)
251 _reg_write_1(SH7709_IRR0
, irr0
& ~IRR0_IRQ0
);
258 j6x0pwr_sleep(void *arg
)
260 /* splhigh on entry */
261 struct j6x0pwr_softc
*sc
= arg
;
264 /* Reinstall j6x0pwr_intr as a wakeup handler */
265 intc_intr_disestablish(sc
->sc_ih
);
266 sc
->sc_ih
= intc_intr_establish(SH7709_INTEVT2_IRQ0
, IST_EDGE
, IPL_HIGH
,
270 /* Disable interrupt except for power button. */
271 s
= _cpu_intr_resume(IPL_CLOCK
<< 4);
272 _reg_write_1(SH7709_PKDR
, 0xff); /* Green LED off */
274 __asm
volatile("sleep");
276 _reg_write_1(SH7709_PKDR
, 0); /* Green LED on */
278 } while (sc
->sc_poweroff
);
280 /* Return to normal power button */
281 intc_intr_disestablish(sc
->sc_ih
);
282 sc
->sc_ih
= intc_intr_establish(SH7709_INTEVT2_IRQ0
, IST_EDGE
, IPL_TTY
,
284 /* splhigh on exit */
289 j6x0pwr_apm_getpower_hook(void *ctx
, int type
, long id
, void *msg
)
291 /* struct j6x0pwr_softc * const sc = ctx; */
292 int * const pval
= msg
;
295 if (type
!= CONFIG_HOOK_GET
)
300 case CONFIG_HOOK_ACADAPTER
:
301 *pval
= j6x0pwr_ac_is_off() ? APM_AC_OFF
: APM_AC_ON
;
304 case CONFIG_HOOK_CHARGE
:
305 if (j6x0pwr_battery_is_absent())
306 state
= APM_BATT_FLAG_NO_SYSTEM_BATTERY
;
308 battery
= j6x0pwr_get_battery();
309 if (battery
< J6X0PWR_BATTERY_CRITICAL
)
310 state
= APM_BATT_FLAG_CRITICAL
;
311 else if (battery
< J6X0PWR_BATTERY_LOW
)
312 state
= APM_BATT_FLAG_LOW
;
314 state
= APM_BATT_FLAG_HIGH
; /* XXX? */
316 if (!j6x0pwr_is_not_charging())
317 state
|= APM_BATT_FLAG_CHARGING
;
322 case CONFIG_HOOK_BATTERYVAL
:
323 if (j6x0pwr_battery_is_absent())
326 battery
= j6x0pwr_get_battery();
327 if (battery
> J6X0PWR_BATTERY_FULL
)
330 state
= 100 * (battery
- J6X0PWR_BATTERY_MIN
)
331 / (J6X0PWR_BATTERY_FULL
332 - J6X0PWR_BATTERY_MIN
);
343 j6x0pwr_get_battery(void)
348 if (j6x0pwr_battery_is_absent())
352 battery
= adc_sample_channel(ADC_CHANNEL_BATTERY
);