j6x0pwr.c revision 1.14.18.1 1 1.14.18.1 jym /* $NetBSD: j6x0pwr.c,v 1.14.18.1 2009/05/13 17:17:47 jym Exp $ */
2 1.1 uwe
3 1.1 uwe /*
4 1.12 uwe * Copyright (c) 2003, 2006 Valeriy E. Ushakov
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * Redistribution and use in source and binary forms, with or without
8 1.1 uwe * modification, are permitted provided that the following conditions
9 1.1 uwe * are met:
10 1.1 uwe * 1. Redistributions of source code must retain the above copyright
11 1.1 uwe * notice, this list of conditions and the following disclaimer.
12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uwe * notice, this list of conditions and the following disclaimer in the
14 1.1 uwe * documentation and/or other materials provided with the distribution.
15 1.1 uwe * 3. The name of the author may not be used to endorse or promote products
16 1.1 uwe * derived from this software without specific prior written permission
17 1.1 uwe *
18 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 uwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 uwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 uwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 uwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 uwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 uwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 uwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 uwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 uwe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 uwe */
29 1.2 uwe
30 1.2 uwe #include <sys/cdefs.h>
31 1.14.18.1 jym __KERNEL_RCSID(0, "$NetBSD: j6x0pwr.c,v 1.14.18.1 2009/05/13 17:17:47 jym Exp $");
32 1.1 uwe
33 1.1 uwe #include <sys/param.h>
34 1.1 uwe #include <sys/kernel.h>
35 1.1 uwe #include <sys/device.h>
36 1.1 uwe #include <sys/malloc.h>
37 1.1 uwe #include <sys/systm.h>
38 1.1 uwe #include <sys/callout.h>
39 1.1 uwe
40 1.10 uwe #include <dev/apm/apmbios.h>
41 1.10 uwe
42 1.3 uwe #include <machine/platid.h>
43 1.3 uwe #include <machine/platid_mask.h>
44 1.4 uch #include <machine/config_hook.h>
45 1.3 uwe
46 1.1 uwe #include <sh3/exception.h>
47 1.1 uwe #include <sh3/intcreg.h>
48 1.1 uwe #include <sh3/pfcreg.h>
49 1.1 uwe
50 1.3 uwe #include <sh3/dev/adcvar.h>
51 1.1 uwe
52 1.1 uwe
53 1.10 uwe /* SH7709 PFC bits pertinent to Jornada 6x0 power */
54 1.1 uwe #define PGDR_MAIN_BATTERY_OUT 0x04
55 1.1 uwe #define PGDR_LID_OPEN 0x01
56 1.10 uwe #define PJDR_AC_POWER_OUT 0x10
57 1.10 uwe #define PLDR_BATTERY_CHARGED 0x20
58 1.10 uwe
59 1.10 uwe
60 1.10 uwe static inline int __attribute__((__always_inline__))
61 1.10 uwe j6x0pwr_battery_is_absent(void)
62 1.10 uwe {
63 1.10 uwe
64 1.10 uwe return _reg_read_1(SH7709_PGDR) & PGDR_MAIN_BATTERY_OUT;
65 1.10 uwe }
66 1.10 uwe
67 1.10 uwe static inline int __attribute__((__always_inline__))
68 1.10 uwe j6x0pwr_ac_is_off(void)
69 1.10 uwe {
70 1.10 uwe
71 1.10 uwe return _reg_read_1(SH7709_PJDR) & PJDR_AC_POWER_OUT;
72 1.10 uwe }
73 1.10 uwe
74 1.10 uwe
75 1.10 uwe static inline int __attribute__((__always_inline__))
76 1.10 uwe j6x0pwr_is_not_charging(void)
77 1.10 uwe {
78 1.10 uwe
79 1.10 uwe return _reg_read_1(SH7709_PLDR) & PLDR_BATTERY_CHARGED;
80 1.10 uwe }
81 1.10 uwe
82 1.1 uwe
83 1.11 peter /* A/D converter channels to get power stats from */
84 1.10 uwe #define ADC_CHANNEL_BATTERY 3 /* main battery */
85 1.10 uwe #define ADC_CHANNEL_BACKUP 4 /* backup battery - we don't report it */
86 1.10 uwe #define ADC_CHANNEL_CHARGE 5 /* we use PLDR_BATTERY_CHARGED instead */
87 1.10 uwe
88 1.10 uwe /*
89 1.10 uwe * Empirical range of battery values.
90 1.10 uwe * Thanks to Joseph Heenan for measurements.
91 1.10 uwe */
92 1.10 uwe #define J6X0PWR_BATTERY_MIN 630
93 1.10 uwe #define J6X0PWR_BATTERY_CRITICAL 635
94 1.10 uwe #define J6X0PWR_BATTERY_LOW 645
95 1.10 uwe #define J6X0PWR_BATTERY_FULL 910 /* can be slightly more */
96 1.10 uwe
97 1.10 uwe
98 1.10 uwe /* Convenience aliases (XXX: should be provided by config_hook.h) */
99 1.10 uwe #define CONFIG_HOOK_BUTTON_PRESSED ((void *)1)
100 1.10 uwe #define CONFIG_HOOK_BUTTON_RELEASED ((void *)0)
101 1.1 uwe
102 1.1 uwe
103 1.1 uwe
104 1.10 uwe /* allow only one instance */
105 1.10 uwe static int j6x0pwr_attached = 0;
106 1.10 uwe
107 1.1 uwe struct j6x0pwr_softc {
108 1.14 uwe device_t sc_dev;
109 1.1 uwe
110 1.4 uch void *sc_ih;
111 1.4 uch volatile int sc_poweroff;
112 1.1 uwe };
113 1.1 uwe
114 1.14 uwe static int j6x0pwr_match(device_t, cfdata_t, void *);
115 1.14 uwe static void j6x0pwr_attach(device_t, device_t, void *);
116 1.1 uwe
117 1.14 uwe CFATTACH_DECL_NEW(j6x0pwr, sizeof(struct j6x0pwr_softc),
118 1.1 uwe j6x0pwr_match, j6x0pwr_attach, NULL, NULL);
119 1.1 uwe
120 1.1 uwe
121 1.10 uwe static int j6x0pwr_apm_getpower_hook(void *, int, long, void *);
122 1.10 uwe static int j6x0pwr_get_battery(void); /* from ADC */
123 1.10 uwe
124 1.1 uwe static int j6x0pwr_intr(void *);
125 1.4 uch static void j6x0pwr_sleep(void *);
126 1.4 uch static int j6x0pwr_clear_interrupt(void);
127 1.1 uwe
128 1.10 uwe
129 1.1 uwe static int
130 1.14 uwe j6x0pwr_match(device_t parent, cfdata_t cfp, void *aux)
131 1.1 uwe {
132 1.1 uwe
133 1.1 uwe /*
134 1.1 uwe * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
135 1.1 uwe * Is 620 wired similarly?
136 1.1 uwe */
137 1.1 uwe if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
138 1.9 uwe return 0;
139 1.1 uwe
140 1.1 uwe if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
141 1.9 uwe return 0;
142 1.1 uwe
143 1.10 uwe /* allow only one instance */
144 1.10 uwe return !j6x0pwr_attached;
145 1.1 uwe }
146 1.1 uwe
147 1.1 uwe
148 1.1 uwe static void
149 1.14 uwe j6x0pwr_attach(device_t parent, device_t self, void *aux)
150 1.1 uwe {
151 1.14 uwe struct j6x0pwr_softc *sc;
152 1.9 uwe
153 1.9 uwe /* XXX: in machdep.c */
154 1.4 uch extern void (*__sleep_func)(void *);
155 1.4 uch extern void *__sleep_ctx;
156 1.1 uwe
157 1.14 uwe sc = device_private(self);
158 1.14 uwe sc->sc_dev = self;
159 1.14 uwe
160 1.14 uwe aprint_naive("\n");
161 1.14 uwe aprint_normal("\n");
162 1.14 uwe
163 1.10 uwe /* allow only one instance */
164 1.10 uwe j6x0pwr_attached = 1;
165 1.10 uwe
166 1.10 uwe /* arrange for hpcapm to call us when power status is requested */
167 1.10 uwe config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
168 1.10 uwe CONFIG_HOOK_EXCLUSIVE,
169 1.10 uwe j6x0pwr_apm_getpower_hook, sc);
170 1.10 uwe config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
171 1.10 uwe CONFIG_HOOK_EXCLUSIVE,
172 1.10 uwe j6x0pwr_apm_getpower_hook, sc);
173 1.10 uwe config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
174 1.10 uwe CONFIG_HOOK_EXCLUSIVE,
175 1.10 uwe j6x0pwr_apm_getpower_hook, sc);
176 1.10 uwe
177 1.11 peter /* register sleep function to APM */
178 1.4 uch __sleep_func = j6x0pwr_sleep;
179 1.14 uwe __sleep_ctx = sc;
180 1.9 uwe
181 1.4 uch sc->sc_poweroff = 0;
182 1.4 uch
183 1.4 uch /* drain the old interrupt */
184 1.4 uch j6x0pwr_clear_interrupt();
185 1.4 uch
186 1.4 uch sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
187 1.4 uch j6x0pwr_intr, sc);
188 1.1 uwe
189 1.5 uwe _reg_write_1(SH7709_PKDR, 0); /* Green LED on */
190 1.14.18.1 jym
191 1.14.18.1 jym if (!pmf_device_register(self, NULL, NULL))
192 1.14.18.1 jym aprint_error_dev(self, "unable to establish power handler\n");
193 1.1 uwe }
194 1.1 uwe
195 1.1 uwe
196 1.1 uwe /*
197 1.1 uwe * Triggered when the On/Off button is pressed or the lid is closed.
198 1.6 uwe * The state of the lid is reflected in the bit PGDR[0].
199 1.1 uwe * Closing the lid can trigger several consecutive interrupts.
200 1.1 uwe *
201 1.1 uwe * XXX: Since we don't put the machine to sleep, I have no idea how
202 1.1 uwe * wakeup interrupt(s) look like. Need to revisit when software
203 1.1 uwe * suspend is added to the kernel (which we need to support ACPI).
204 1.1 uwe */
205 1.1 uwe static int
206 1.14 uwe j6x0pwr_intr(void *arg)
207 1.1 uwe {
208 1.14 uwe struct j6x0pwr_softc *sc = arg;
209 1.14 uwe device_t self = sc->sc_dev;
210 1.1 uwe uint8_t irr0;
211 1.1 uwe uint8_t pgdr;
212 1.1 uwe
213 1.9 uwe irr0 = j6x0pwr_clear_interrupt();
214 1.9 uwe if ((irr0 & IRR0_IRQ0) == 0) {
215 1.1 uwe #ifdef DIAGNOSTIC
216 1.14 uwe aprint_normal_dev(self, "irr0=0x%02x?\n", irr0);
217 1.1 uwe #endif
218 1.9 uwe return 0;
219 1.1 uwe }
220 1.1 uwe
221 1.4 uch pgdr = _reg_read_1(SH7709_PGDR);
222 1.4 uch if ((pgdr & PGDR_LID_OPEN) == 0) {
223 1.14 uwe aprint_normal_dev(self, "lid closed %d\n", sc->sc_poweroff);
224 1.4 uch if (sc->sc_poweroff)
225 1.4 uch return 1;
226 1.4 uch } else {
227 1.14 uwe aprint_normal_dev(self, "ON/OFF %d\n", sc->sc_poweroff);
228 1.4 uch if (sc->sc_poweroff)
229 1.4 uch sc->sc_poweroff = 0;
230 1.4 uch }
231 1.1 uwe
232 1.10 uwe config_hook_call(CONFIG_HOOK_BUTTONEVENT,
233 1.10 uwe CONFIG_HOOK_BUTTONEVENT_POWER,
234 1.10 uwe CONFIG_HOOK_BUTTON_PRESSED);
235 1.10 uwe /* fake release */
236 1.10 uwe config_hook_call(CONFIG_HOOK_BUTTONEVENT,
237 1.10 uwe CONFIG_HOOK_BUTTONEVENT_POWER,
238 1.10 uwe CONFIG_HOOK_BUTTON_RELEASED);
239 1.1 uwe
240 1.9 uwe return 1;
241 1.1 uwe }
242 1.1 uwe
243 1.10 uwe
244 1.4 uch static int
245 1.12 uwe j6x0pwr_clear_interrupt(void)
246 1.4 uch {
247 1.4 uch uint8_t irr0;
248 1.4 uch
249 1.4 uch irr0 = _reg_read_1(SH7709_IRR0);
250 1.4 uch if (irr0 & IRR0_IRQ0)
251 1.4 uch _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
252 1.4 uch
253 1.4 uch return irr0;
254 1.4 uch }
255 1.4 uch
256 1.10 uwe
257 1.4 uch void
258 1.14 uwe j6x0pwr_sleep(void *arg)
259 1.4 uch {
260 1.4 uch /* splhigh on entry */
261 1.14 uwe struct j6x0pwr_softc *sc = arg;
262 1.4 uch int s;
263 1.4 uch
264 1.4 uch /* Reinstall j6x0pwr_intr as a wakeup handler */
265 1.4 uch intc_intr_disestablish(sc->sc_ih);
266 1.4 uch sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_HIGH,
267 1.4 uch j6x0pwr_intr, sc);
268 1.4 uch sc->sc_poweroff = 1;
269 1.4 uch do {
270 1.4 uch /* Disable interrupt except for power button. */
271 1.4 uch s = _cpu_intr_resume(IPL_CLOCK << 4);
272 1.5 uwe _reg_write_1(SH7709_PKDR, 0xff); /* Green LED off */
273 1.9 uwe
274 1.8 perry __asm volatile("sleep");
275 1.9 uwe
276 1.5 uwe _reg_write_1(SH7709_PKDR, 0); /* Green LED on */
277 1.4 uch _cpu_intr_resume(s);
278 1.4 uch } while (sc->sc_poweroff);
279 1.4 uch
280 1.4 uch /* Return to normal power button */
281 1.4 uch intc_intr_disestablish(sc->sc_ih);
282 1.4 uch sc->sc_ih = intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
283 1.4 uch j6x0pwr_intr, sc);
284 1.4 uch /* splhigh on exit */
285 1.4 uch }
286 1.1 uwe
287 1.1 uwe
288 1.10 uwe static int
289 1.10 uwe j6x0pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
290 1.1 uwe {
291 1.10 uwe /* struct j6x0pwr_softc * const sc = ctx; */
292 1.10 uwe int * const pval = msg;
293 1.10 uwe int battery, state;
294 1.10 uwe
295 1.10 uwe if (type != CONFIG_HOOK_GET)
296 1.10 uwe return EINVAL;
297 1.10 uwe
298 1.10 uwe switch (id) {
299 1.10 uwe
300 1.10 uwe case CONFIG_HOOK_ACADAPTER:
301 1.10 uwe *pval = j6x0pwr_ac_is_off() ? APM_AC_OFF : APM_AC_ON;
302 1.10 uwe return 0;
303 1.10 uwe
304 1.10 uwe case CONFIG_HOOK_CHARGE:
305 1.10 uwe if (j6x0pwr_battery_is_absent())
306 1.10 uwe state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
307 1.10 uwe else {
308 1.10 uwe battery = j6x0pwr_get_battery();
309 1.10 uwe if (battery < J6X0PWR_BATTERY_CRITICAL)
310 1.10 uwe state = APM_BATT_FLAG_CRITICAL;
311 1.10 uwe else if (battery < J6X0PWR_BATTERY_LOW)
312 1.10 uwe state = APM_BATT_FLAG_LOW;
313 1.10 uwe else
314 1.10 uwe state = APM_BATT_FLAG_HIGH; /* XXX? */
315 1.1 uwe
316 1.10 uwe if (!j6x0pwr_is_not_charging())
317 1.10 uwe state |= APM_BATT_FLAG_CHARGING;
318 1.10 uwe }
319 1.10 uwe *pval = state;
320 1.10 uwe return 0;
321 1.1 uwe
322 1.10 uwe case CONFIG_HOOK_BATTERYVAL:
323 1.10 uwe if (j6x0pwr_battery_is_absent())
324 1.10 uwe state = 0;
325 1.1 uwe else {
326 1.10 uwe battery = j6x0pwr_get_battery();
327 1.10 uwe if (battery > J6X0PWR_BATTERY_FULL)
328 1.10 uwe state = 100;
329 1.10 uwe else
330 1.10 uwe state = 100 * (battery - J6X0PWR_BATTERY_MIN)
331 1.10 uwe / (J6X0PWR_BATTERY_FULL
332 1.10 uwe - J6X0PWR_BATTERY_MIN);
333 1.1 uwe }
334 1.10 uwe *pval = state;
335 1.10 uwe return 0;
336 1.1 uwe }
337 1.1 uwe
338 1.10 uwe return EINVAL;
339 1.10 uwe }
340 1.10 uwe
341 1.10 uwe
342 1.10 uwe static int
343 1.10 uwe j6x0pwr_get_battery(void)
344 1.10 uwe {
345 1.10 uwe int battery;
346 1.10 uwe int s;
347 1.1 uwe
348 1.10 uwe if (j6x0pwr_battery_is_absent())
349 1.10 uwe return -1;
350 1.1 uwe
351 1.1 uwe s = spltty();
352 1.10 uwe battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
353 1.1 uwe splx(s);
354 1.1 uwe
355 1.10 uwe return battery;
356 1.1 uwe }
357