xref: /netbsd-src/sys/arch/hpcsh/dev/j6x0pwr.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: j6x0pwr.c,v 1.15 2009/04/05 02:18:59 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.15 2009/04/05 02:18:59 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 	if (!pmf_device_register(self, NULL, NULL))
192 		aprint_error_dev(self, "unable to establish power handler\n");
193 }
194 
195 
196 /*
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.
200  *
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).
204  */
205 static int
206 j6x0pwr_intr(void *arg)
207 {
208 	struct j6x0pwr_softc *sc = arg;
209 	device_t self = sc->sc_dev;
210 	uint8_t irr0;
211 	uint8_t pgdr;
212 
213 	irr0 = j6x0pwr_clear_interrupt();
214 	if ((irr0 & IRR0_IRQ0) == 0) {
215 #ifdef DIAGNOSTIC
216 		aprint_normal_dev(self, "irr0=0x%02x?\n", irr0);
217 #endif
218 		return 0;
219 	}
220 
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);
224 		if (sc->sc_poweroff)
225 			return 1;
226 	} else {
227 		aprint_normal_dev(self, "ON/OFF %d\n", sc->sc_poweroff);
228 		if (sc->sc_poweroff)
229 			sc->sc_poweroff = 0;
230 	}
231 
232 	config_hook_call(CONFIG_HOOK_BUTTONEVENT,
233 			 CONFIG_HOOK_BUTTONEVENT_POWER,
234 			 CONFIG_HOOK_BUTTON_PRESSED);
235 	/* fake release */
236 	config_hook_call(CONFIG_HOOK_BUTTONEVENT,
237 			 CONFIG_HOOK_BUTTONEVENT_POWER,
238 			 CONFIG_HOOK_BUTTON_RELEASED);
239 
240 	return 1;
241 }
242 
243 
244 static int
245 j6x0pwr_clear_interrupt(void)
246 {
247 	uint8_t irr0;
248 
249 	irr0 = _reg_read_1(SH7709_IRR0);
250 	if (irr0 & IRR0_IRQ0)
251 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
252 
253 	return irr0;
254 }
255 
256 
257 void
258 j6x0pwr_sleep(void *arg)
259 {
260 	/* splhigh on entry */
261 	struct j6x0pwr_softc *sc = arg;
262 	int s;
263 
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,
267 	    j6x0pwr_intr, sc);
268 	sc->sc_poweroff = 1;
269 	do {
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 */
273 
274 		__asm volatile("sleep");
275 
276 		_reg_write_1(SH7709_PKDR, 0);		/* Green LED on */
277 		_cpu_intr_resume(s);
278 	} while (sc->sc_poweroff);
279 
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,
283 	    j6x0pwr_intr, sc);
284 	/* splhigh on exit */
285 }
286 
287 
288 static int
289 j6x0pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
290 {
291 	/* struct j6x0pwr_softc * const sc = ctx; */
292 	int * const pval = msg;
293 	int battery, state;
294 
295 	if (type != CONFIG_HOOK_GET)
296 		return EINVAL;
297 
298 	switch (id) {
299 
300 	case CONFIG_HOOK_ACADAPTER:
301 		*pval = j6x0pwr_ac_is_off() ? APM_AC_OFF : APM_AC_ON;
302 		return 0;
303 
304 	case CONFIG_HOOK_CHARGE:
305 		if (j6x0pwr_battery_is_absent())
306 			state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
307 		else {
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;
313 			else
314 				state = APM_BATT_FLAG_HIGH; /* XXX? */
315 
316 			if (!j6x0pwr_is_not_charging())
317 				state |= APM_BATT_FLAG_CHARGING;
318 		}
319 		*pval = state;
320 		return 0;
321 
322 	case CONFIG_HOOK_BATTERYVAL:
323 		if (j6x0pwr_battery_is_absent())
324 			state = 0;
325 		else {
326 			battery = j6x0pwr_get_battery();
327 			if (battery > J6X0PWR_BATTERY_FULL)
328 				state = 100;
329 			else
330 				state = 100 * (battery - J6X0PWR_BATTERY_MIN)
331 					/ (J6X0PWR_BATTERY_FULL
332 					   - J6X0PWR_BATTERY_MIN);
333 		}
334 		*pval = state;
335 		return 0;
336 	}
337 
338 	return EINVAL;
339 }
340 
341 
342 static int
343 j6x0pwr_get_battery(void)
344 {
345 	int battery;
346 	int s;
347 
348 	if (j6x0pwr_battery_is_absent())
349 		return -1;
350 
351 	s = spltty();
352 	battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
353 	splx(s);
354 
355 	return battery;
356 }
357