xref: /netbsd-src/sys/arch/hpcsh/dev/psh3pwr.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: psh3pwr.c,v 1.3 2008/03/31 15:49:29 kiyohara Exp $	*/
2 /*
3  * Copyright (c) 2005, 2007 KIYOHARA Takashi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: psh3pwr.c,v 1.3 2008/03/31 15:49:29 kiyohara Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/callout.h>
37 
38 #include <machine/config_hook.h>
39 #include <machine/platid.h>
40 #include <machine/platid_mask.h>
41 
42 #include <dev/apm/apmbios.h>
43 
44 #include <sh3/exception.h>
45 #include <sh3/intcreg.h>
46 #include <sh3/pfcreg.h>
47 
48 #include <sh3/dev/adcvar.h>
49 
50 
51 #ifdef PSH3PWR_DEBUG
52 #define DPRINTF(arg)            printf arg
53 #else
54 #define DPRINTF(arg)            ((void)0)
55 #endif
56 
57 
58 /* A/D covnerter channels to get power stats from */
59 #define ADC_CHANNEL_BATTERY	3
60 
61 /* On/Off bit for Green LED. pin 7 in SH7709 GPIO port H. */
62 #define PSH3_GREEN_LED_ON	0x80
63 
64 /*
65  * XXXX:
66  *   WindowsCE seem to be using this as a flag.
67  *   pin 6 in SH7709 GPIO port SCPDR.
68  */
69 #define PSH3PWR_PLUG_OUT	0x40
70 
71 
72 static inline int __attribute__((__always_inline__))
73 psh3pwr_ac_is_off(void)
74 {
75 
76 	return _reg_read_1(SH7709_SCPDR) & PSH3PWR_PLUG_OUT;
77 }
78 
79 
80 /*
81  * Empirical range of battery values.
82  * Thanks to Joseph Heenan for measurements.
83  */
84 #define PSH3PWR_BATTERY_MIN		630
85 #define PSH3PWR_BATTERY_CRITICAL	635
86 #define PSH3PWR_BATTERY_LOW		645
87 #define PSH3PWR_BATTERY_FULL		910 /* can be slightly more */
88 
89 
90 struct psh3pwr_softc {
91 	device_t sc_dev;
92 
93 	void *sc_ih_pin;
94 	void *sc_ih_pout;
95 };
96 
97 static int psh3pwr_match(device_t, struct cfdata *, void *);
98 static void psh3pwr_attach(device_t, device_t, void *);
99 
100 CFATTACH_DECL_NEW(psh3pwr, sizeof(struct psh3pwr_softc),
101     psh3pwr_match, psh3pwr_attach, NULL, NULL);
102 
103 static int psh3pwr_intr_plug_out(void *);
104 static int psh3pwr_intr_plug_in(void *);
105 static void psh3pwr_sleep(void *);
106 static int psh3pwr_apm_getpower_hook(void *, int, long, void *);
107 static int psh3pwr_get_battery(void);
108 
109 
110 static int
111 psh3pwr_match(device_t parent, struct cfdata *cfp, void *aux)
112 {
113 
114 	if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
115 		return 0;
116 
117 	if (strcmp(cfp->cf_name, "psh3pwr") != 0)
118 		return 0;
119 
120 	return 1;
121 }
122 
123 
124 static void
125 psh3pwr_attach(device_t parent, device_t self, void *aux)
126 {
127 	extern void (*__sleep_func)(void *);
128 	extern void *__sleep_ctx;
129 	struct psh3pwr_softc *sc = device_private(self);
130 	uint8_t phdr;
131 
132 	sc->sc_dev = self;
133 
134 	/* arrange for hpcapm to call us when power status is requested */
135 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
136 	    CONFIG_HOOK_EXCLUSIVE, psh3pwr_apm_getpower_hook, sc);
137 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
138 	    CONFIG_HOOK_EXCLUSIVE, psh3pwr_apm_getpower_hook, sc);
139 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
140 	    CONFIG_HOOK_EXCLUSIVE, psh3pwr_apm_getpower_hook, sc);
141 
142 	/* regisiter sleep function to APM */
143 	__sleep_func = psh3pwr_sleep;
144 	__sleep_ctx = self;
145 
146 	phdr = _reg_read_1(SH7709_PHDR);
147 	_reg_write_1(SH7709_PHDR, phdr | PSH3_GREEN_LED_ON);
148 
149 	aprint_naive("\n");
150 	aprint_normal("\n");
151 
152 	sc->sc_ih_pout = intc_intr_establish(SH7709_INTEVT2_IRQ0,
153 	    IST_EDGE, IPL_TTY, psh3pwr_intr_plug_out, sc);
154 	sc->sc_ih_pin = intc_intr_establish(SH7709_INTEVT2_IRQ1,
155 	    IST_EDGE, IPL_TTY, psh3pwr_intr_plug_in, sc);
156 
157 	/* XXXX: WindowsCE sets this bit. */
158 	aprint_normal_dev(self, "plug status: %s\n",
159 	    psh3pwr_ac_is_off() ? "out" : "in");
160 }
161 
162 
163 static int
164 psh3pwr_intr_plug_out(void *self)
165 {
166 	struct psh3pwr_softc *sc __attribute__((__unused__)) =
167 	    (struct psh3pwr_softc *)self;
168 	uint8_t irr0, scpdr;
169 
170 	irr0 = _reg_read_1(SH7709_IRR0);
171 	if (!(irr0 & IRR0_IRQ0)) {
172 		return 0;
173 	}
174 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
175 
176 	/* XXXX: WindowsCE sets this bit. */
177 	scpdr = _reg_read_1(SH7709_SCPDR);
178 	_reg_write_1(SH7709_SCPDR, scpdr | PSH3PWR_PLUG_OUT);
179 
180 	DPRINTF(("%s: plug out\n", device_xname(&sc->sc_dev)));
181 
182 	return 1;
183 }
184 
185 static int
186 psh3pwr_intr_plug_in(void *self)
187 {
188 	struct psh3pwr_softc *sc __attribute__((__unused__)) =
189 	    (struct psh3pwr_softc *)self;
190 	uint8_t irr0, scpdr;
191 
192 	irr0 = _reg_read_1(SH7709_IRR0);
193 	if (!(irr0 & IRR0_IRQ1))
194 		return 0;
195 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ1);
196 
197 	/* XXXX: WindowsCE sets this bit. */
198 	scpdr = _reg_read_1(SH7709_SCPDR);
199 	_reg_write_1(SH7709_SCPDR, scpdr & ~PSH3PWR_PLUG_OUT);
200 
201 	DPRINTF(("%s: plug in\n", device_xname(&sc->sc_dev)));
202 
203 	return 1;
204 }
205 
206 void
207 psh3pwr_sleep(void *self)
208 {
209 	/* splhigh on entry */
210 	extern void pfckbd_poll_hitachi_power(void);
211 
212 	uint8_t phdr;
213 
214 	phdr = _reg_read_1(SH7709_PHDR);
215 	_reg_write_1(SH7709_PHDR, phdr & ~PSH3_GREEN_LED_ON);
216 
217 	pfckbd_poll_hitachi_power();
218 
219 	phdr = _reg_read_1(SH7709_PHDR);
220 	_reg_write_1(SH7709_PHDR, phdr | PSH3_GREEN_LED_ON);
221 }
222 
223 static int
224 psh3pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
225 {
226 	/* struct psh0pwr_softc * const sc = ctx; */
227 	int * const pval = msg;
228 	int battery, state;
229 
230 	if (type != CONFIG_HOOK_GET)
231 		return EINVAL;
232 
233 	switch (id) {
234 
235 	case CONFIG_HOOK_ACADAPTER:
236 		*pval = psh3pwr_ac_is_off() ? APM_AC_OFF : APM_AC_ON;
237 		return 0;
238 
239 	case CONFIG_HOOK_CHARGE:
240 		battery = psh3pwr_get_battery();
241 		if (battery < PSH3PWR_BATTERY_CRITICAL)
242 			state = APM_BATT_FLAG_CRITICAL;
243 		else if (battery < PSH3PWR_BATTERY_LOW)
244 			state = APM_BATT_FLAG_LOW;
245 		else
246 			state = APM_BATT_FLAG_HIGH; /* XXX? */
247 		*pval = state;
248 		return 0;
249 
250 	case CONFIG_HOOK_BATTERYVAL:
251 		battery = psh3pwr_get_battery();
252 		if (battery > PSH3PWR_BATTERY_FULL)
253 			state = 100;
254 		else
255 			state = 100 * (battery - PSH3PWR_BATTERY_MIN) /
256 			    (PSH3PWR_BATTERY_FULL - PSH3PWR_BATTERY_MIN);
257 		*pval = state;
258 		return 0;
259 	}
260 
261 	return EINVAL;
262 }
263 
264 
265 static int
266 psh3pwr_get_battery(void)
267 {
268 	int battery;
269 	int s;
270 
271 	s = spltty();
272 	battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
273 	splx(s);
274 
275 	return battery;
276 }
277