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