j720pwr.c revision 1.3.52.1 1 /* $NetBSD: j720pwr.c,v 1.3.52.1 2008/06/02 13:22:10 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus and Peter Postma.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /* Jornada 720 power management. */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: j720pwr.c,v 1.3.52.1 2008/06/02 13:22:10 mjf Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42
43 #include <dev/apm/apmbios.h>
44
45 #include <machine/config_hook.h>
46 #include <machine/platid.h>
47 #include <machine/platid_mask.h>
48
49 #include <arm/sa11x0/sa11x0_var.h>
50 #include <arm/sa11x0/sa11x0_gpioreg.h>
51 #include <arm/sa11x0/sa11x0_ppcreg.h>
52 #include <arm/sa11x0/sa11x0_sspreg.h>
53
54 #include <hpcarm/dev/j720sspvar.h>
55
56 #ifdef DEBUG
57 #define DPRINTF(arg) printf arg
58 #else
59 #define DPRINTF(arg) /* nothing */
60 #endif
61
62 #define arraysize(ary) (sizeof(ary) / sizeof(ary[0]))
63
64 struct j720pwr_softc {
65 struct device sc_dev;
66
67 struct j720ssp_softc *sc_ssp;
68
69 volatile int sc_state;
70 #define J720PWR_POWEROFF 0x01
71 #define J720PWR_SLEEPING 0x02
72 };
73
74 static int j720pwr_match(struct device *, struct cfdata *, void *);
75 static void j720pwr_attach(struct device *, struct device *, void *);
76
77 static void j720pwr_sleep(void *);
78 static int j720pwr_suspend_hook(void *, int, long, void *);
79 static int j720pwr_event_hook(void *, int, long, void *);
80 static int j720pwr_apm_getpower_hook(void *, int, long, void *);
81 static int j720pwr_get_battery(struct j720pwr_softc *);
82 static int j720pwr_get_ac_status(struct j720pwr_softc *);
83 static int j720pwr_get_charge_status(struct j720pwr_softc *);
84
85 static const struct {
86 int percent;
87 int value;
88 int state;
89 } battery_table[] = {
90 { 100, 670, APM_BATT_FLAG_HIGH },
91 { 90, 660, APM_BATT_FLAG_HIGH },
92 { 80, 650, APM_BATT_FLAG_HIGH },
93 { 70, 640, APM_BATT_FLAG_HIGH },
94 { 60, 630, APM_BATT_FLAG_HIGH },
95 { 50, 620, APM_BATT_FLAG_HIGH },
96 { 40, 605, APM_BATT_FLAG_LOW },
97 { 30, 580, APM_BATT_FLAG_LOW },
98 { 20, 545, APM_BATT_FLAG_LOW },
99 { 10, 500, APM_BATT_FLAG_CRITICAL },
100 { 0, 430, APM_BATT_FLAG_CRITICAL },
101 };
102
103 CFATTACH_DECL(j720pwr, sizeof(struct j720pwr_softc),
104 j720pwr_match, j720pwr_attach, NULL, NULL);
105
106
107 static int
108 j720pwr_match(struct device *parent, struct cfdata *cf, void *aux)
109 {
110
111 if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
112 return 0;
113 if (strcmp(cf->cf_name, "j720pwr") != 0)
114 return 0;
115
116 return 1;
117 }
118
119 static void
120 j720pwr_attach(struct device *parent, struct device *self, void *aux)
121 {
122 struct j720pwr_softc *sc = (struct j720pwr_softc *)self;
123 extern void (*__sleep_func)(void *);
124 extern void *__sleep_ctx;
125
126 printf("\n");
127
128 sc->sc_ssp = (struct j720ssp_softc *)parent;
129 sc->sc_state = 0;
130
131 /* Register apm sleep function. */
132 __sleep_func = j720pwr_sleep;
133 __sleep_ctx = sc;
134
135 /* Battery status query hook. */
136 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
137 CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
138
139 /* Battery charge status query hook. */
140 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
141 CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
142
143 /* AC status query hook. */
144 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
145 CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
146
147 /* Suspend/resume button hook. */
148 config_hook(CONFIG_HOOK_BUTTONEVENT,
149 CONFIG_HOOK_BUTTONEVENT_POWER,
150 CONFIG_HOOK_SHARE, j720pwr_suspend_hook, sc);
151
152 /* Receive suspend/resume events. */
153 config_hook(CONFIG_HOOK_PMEVENT,
154 CONFIG_HOOK_PMEVENT_HARDPOWER,
155 CONFIG_HOOK_SHARE, j720pwr_event_hook, sc);
156
157 /* Attach hpcapm. */
158 config_found_ia(self, "hpcapmif", NULL, NULL);
159 }
160
161 static void
162 j720pwr_sleep(void *ctx)
163 {
164 struct j720pwr_softc *sc = ctx;
165 struct j720ssp_softc *ssp = sc->sc_ssp;
166 uint32_t oldfer;
167
168 /* Disable falling-edge detect on all GPIO ports, except keyboard. */
169 oldfer = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER);
170 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, 1 << 0);
171
172 while (sc->sc_state & J720PWR_POWEROFF) {
173 /*
174 * Just sleep here until the poweroff bit gets unset.
175 * We need to wait here because when machine_sleep() returns
176 * hpcapm(4) assumes that we are "resuming".
177 */
178 (void)tsleep(&sc->sc_state, PWAIT, "j720slp", 0);
179 }
180
181 /* Restore previous FER value. */
182 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, oldfer);
183 }
184
185 static int
186 j720pwr_suspend_hook(void *ctx, int type, long id, void *msg)
187 {
188 struct j720pwr_softc *sc = ctx;
189
190 if (type != CONFIG_HOOK_BUTTONEVENT ||
191 id != CONFIG_HOOK_BUTTONEVENT_POWER)
192 return EINVAL;
193
194 if ((sc->sc_state & (J720PWR_POWEROFF | J720PWR_SLEEPING)) == 0) {
195 sc->sc_state |= J720PWR_POWEROFF;
196 } else if ((sc->sc_state & (J720PWR_POWEROFF | J720PWR_SLEEPING)) ==
197 (J720PWR_POWEROFF | J720PWR_SLEEPING)) {
198 sc->sc_state &= ~J720PWR_POWEROFF;
199 wakeup(&sc->sc_state);
200 } else {
201 DPRINTF(("j720pwr_suspend_hook: busy\n"));
202 return EBUSY;
203 }
204
205 config_hook_call(CONFIG_HOOK_PMEVENT,
206 CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
207
208 return 0;
209 }
210
211 static int
212 j720pwr_event_hook(void *ctx, int type, long id, void *msg)
213 {
214 struct j720pwr_softc *sc = ctx;
215 int event = (int)msg;
216
217 if (type != CONFIG_HOOK_PMEVENT ||
218 id != CONFIG_HOOK_PMEVENT_HARDPOWER)
219 return EINVAL;
220
221 switch (event) {
222 case PWR_SUSPEND:
223 sc->sc_state |= (J720PWR_SLEEPING | J720PWR_POWEROFF);
224 break;
225 case PWR_RESUME:
226 sc->sc_state &= ~(J720PWR_SLEEPING | J720PWR_POWEROFF);
227 break;
228 default:
229 return EINVAL;
230 }
231
232 return 0;
233 }
234
235 static int
236 j720pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
237 {
238 int * const pval = msg;
239 int val, tmp, i, state = 0;
240
241 if (type != CONFIG_HOOK_GET)
242 return EINVAL;
243
244 switch (id) {
245 case CONFIG_HOOK_BATTERYVAL:
246 val = j720pwr_get_battery(ctx);
247
248 if (val != -1) {
249 for (i = 0; i < arraysize(battery_table); i++)
250 if (val > battery_table[i].value)
251 break;
252 if (i == 0) {
253 /* Battery charge status is at maximum. */
254 *pval = 100;
255 } else {
256 /*
257 * Use linear interpolation to calculate
258 * the estimated charge status.
259 */
260 tmp = ((val - battery_table[i].value) * 100) /
261 (battery_table[i - 1].value -
262 battery_table[i].value);
263 *pval = battery_table[i].percent +
264 ((battery_table[i - 1].percent -
265 battery_table[i].percent) * tmp) / 100;
266 }
267 } else {
268 /* Battery is absent. */
269 *pval = 0;
270 }
271
272 return 0;
273
274 case CONFIG_HOOK_CHARGE:
275 val = j720pwr_get_battery(ctx);
276
277 if (val != -1) {
278 for (i = 1; i < arraysize(battery_table); i++) {
279 if (val > battery_table[i].value) {
280 state = battery_table[i - 1].state;
281 break;
282 }
283 }
284
285 if (j720pwr_get_charge_status(ctx) == 0)
286 state |= APM_BATT_FLAG_CHARGING;
287 } else {
288 state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
289 }
290
291 *pval = state;
292 return 0;
293
294 case CONFIG_HOOK_ACADAPTER:
295 *pval = j720pwr_get_ac_status(ctx) ? APM_AC_OFF : APM_AC_ON;
296
297 return 0;
298 }
299
300 return EINVAL;
301 }
302
303 static int
304 j720pwr_get_battery(struct j720pwr_softc *sc)
305 {
306 struct j720ssp_softc *ssp = sc->sc_ssp;
307 int data, i, pmdata[3];
308
309 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
310
311 if (j720ssp_readwrite(ssp, 1, 0xc0, &data, 500) < 0 || data != 0x11) {
312 DPRINTF(("j720pwr_get_battery: no dummy received\n"));
313 goto out;
314 }
315
316 for (i = 0; i < 3; i++) {
317 if (j720ssp_readwrite(ssp, 0, 0x11, &pmdata[i], 100) < 0)
318 goto out;
319 }
320
321 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
322
323 pmdata[0] |= (pmdata[2] & 0x3) << 8; /* Main battery. */
324 pmdata[1] |= (pmdata[2] & 0xc) << 6; /* Backup battery (unused). */
325
326 DPRINTF(("j720pwr_get_battery: data[0]=%d data[1]=%d data[2]=%d\n",
327 pmdata[0], pmdata[1], pmdata[2]));
328
329 /* If bit 0 and 1 are both set, the main battery is absent. */
330 if ((pmdata[2] & 3) == 3)
331 return -1;
332
333 return pmdata[0];
334
335 out:
336 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
337
338 /* reset SSP */
339 bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
340 delay(100);
341 bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
342
343 DPRINTF(("j720pwr_get_battery: error %x\n", data));
344 return 0;
345 }
346
347 static int
348 j720pwr_get_ac_status(struct j720pwr_softc *sc)
349 {
350 struct j720ssp_softc *ssp = sc->sc_ssp;
351 uint32_t status;
352
353 status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR);
354
355 return status & (1 << 4);
356 }
357
358 static int
359 j720pwr_get_charge_status(struct j720pwr_softc *sc)
360 {
361 struct j720ssp_softc *ssp = sc->sc_ssp;
362 uint32_t status;
363
364 status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR);
365
366 return status & (1 << 26);
367 }
368