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