2 * Copyright (c) 2006 Michael Lorenz
3 * Copyright 2008 by Nathan Whitehorn
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #include <sys/clock.h>
39 #include <sys/reboot.h>
40 #include <sys/sysctl.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/led/led.h>
46 #include <machine/bus.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/md_var.h>
49 #include <machine/pio.h>
50 #include <machine/resource.h>
57 #include <dev/adb/adb.h>
66 static int pmu_probe(device_t);
67 static int pmu_attach(device_t);
68 static int pmu_detach(device_t);
73 static int pmu_gettime(device_t dev, struct timespec *ts);
74 static int pmu_settime(device_t dev, struct timespec *ts);
80 static u_int pmu_adb_send(device_t dev, u_char command_byte, int len,
81 u_char *data, u_char poll);
82 static u_int pmu_adb_autopoll(device_t dev, uint16_t mask);
83 static u_int pmu_poll(device_t dev);
89 static void pmu_shutdown(void *xsc, int howto);
90 static void pmu_set_sleepled(void *xsc, int onoff);
91 static int pmu_server_mode(SYSCTL_HANDLER_ARGS);
92 static int pmu_acline_state(SYSCTL_HANDLER_ARGS);
93 static int pmu_query_battery(struct pmu_softc *sc, int batt,
94 struct pmu_battstate *info);
95 static int pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
98 * List of battery-related sysctls we might ask for
102 PMU_BATSYSCTL_PRESENT = 1 << 8,
103 PMU_BATSYSCTL_CHARGING = 2 << 8,
104 PMU_BATSYSCTL_CHARGE = 3 << 8,
105 PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
106 PMU_BATSYSCTL_CURRENT = 5 << 8,
107 PMU_BATSYSCTL_VOLTAGE = 6 << 8,
108 PMU_BATSYSCTL_TIME = 7 << 8,
109 PMU_BATSYSCTL_LIFE = 8 << 8
112 static device_method_t pmu_methods[] = {
113 /* Device interface */
114 DEVMETHOD(device_probe, pmu_probe),
115 DEVMETHOD(device_attach, pmu_attach),
116 DEVMETHOD(device_detach, pmu_detach),
117 DEVMETHOD(device_shutdown, bus_generic_shutdown),
118 DEVMETHOD(device_suspend, bus_generic_suspend),
119 DEVMETHOD(device_resume, bus_generic_resume),
121 /* bus interface, for ADB root */
122 DEVMETHOD(bus_print_child, bus_generic_print_child),
123 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
125 /* ADB bus interface */
126 DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send),
127 DEVMETHOD(adb_hb_controller_poll, pmu_poll),
128 DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
130 /* Clock interface */
131 DEVMETHOD(clock_gettime, pmu_gettime),
132 DEVMETHOD(clock_settime, pmu_settime),
137 static driver_t pmu_driver = {
140 sizeof(struct pmu_softc),
143 static devclass_t pmu_devclass;
145 DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0);
146 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
148 static int pmuextint_probe(device_t);
149 static int pmuextint_attach(device_t);
151 static device_method_t pmuextint_methods[] = {
152 /* Device interface */
153 DEVMETHOD(device_probe, pmuextint_probe),
154 DEVMETHOD(device_attach, pmuextint_attach),
159 static driver_t pmuextint_driver = {
165 static devclass_t pmuextint_devclass;
167 DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass, 0, 0);
169 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
170 MODULE_DEPEND(pmu, usb, 1, 1, 1);
172 static void pmu_intr(void *arg);
173 static void pmu_in(struct pmu_softc *sc);
174 static void pmu_out(struct pmu_softc *sc);
175 static void pmu_ack_on(struct pmu_softc *sc);
176 static void pmu_ack_off(struct pmu_softc *sc);
177 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
178 int rlen, uint8_t *out_msg);
179 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
180 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
181 static int pmu_intr_state(struct pmu_softc *);
183 /* these values shows that number of data returned after 'send' cmd is sent */
184 static signed char pm_send_cmd_type[] = {
185 -1, -1, -1, -1, -1, -1, -1, -1,
186 -1, -1, -1, -1, -1, -1, -1, -1,
187 0x01, 0x01, -1, -1, -1, -1, -1, -1,
188 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
189 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
190 0x00, -1, -1, -1, -1, -1, -1, -1,
191 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
192 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
193 0x01, 0x01, -1, -1, -1, -1, -1, -1,
194 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
195 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
196 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
197 0x02, -1, -1, -1, -1, -1, -1, -1,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
199 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
200 0x00, 0x00, -1, -1, -1, -1, 0x04, 0x04,
201 0x04, -1, 0x00, -1, -1, -1, -1, -1,
202 0x00, -1, -1, -1, -1, -1, -1, -1,
203 0x01, 0x02, -1, -1, -1, -1, -1, -1,
204 0x00, 0x00, -1, -1, -1, -1, -1, -1,
205 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
206 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
207 -1, -1, -1, -1, -1, -1, -1, -1,
208 -1, -1, -1, -1, -1, -1, -1, -1,
209 -1, -1, -1, -1, -1, -1, -1, -1,
210 -1, -1, -1, -1, -1, -1, -1, -1,
211 0x00, -1, -1, -1, -1, -1, -1, -1,
212 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
213 -1, 0x04, 0x00, -1, -1, -1, -1, -1,
214 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
215 -1, -1, -1, -1, -1, -1, -1, -1,
216 -1, -1, -1, -1, -1, -1, -1, -1
219 /* these values shows that number of data returned after 'receive' cmd is sent */
220 static signed char pm_receive_cmd_type[] = {
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222 -1, -1, -1, -1, -1, -1, -1, -1,
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 -1, -1, -1, -1, -1, -1, -1, -1,
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230 0x02, 0x02, -1, -1, -1, -1, -1, -1,
231 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 -1, -1, -1, -1, -1, -1, 0x01, 0x01,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x06, -1, -1, -1, -1, -1, -1, -1,
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240 0x02, 0x02, -1, -1, -1, -1, -1, -1,
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244 -1, -1, -1, -1, -1, -1, -1, -1,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 -1, -1, -1, -1, -1, -1, -1, -1,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
249 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
250 -1, -1, 0x02, -1, -1, -1, -1, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 -1, -1, -1, -1, -1, -1, -1, -1,
255 /* We only have one of each device, so globals are safe */
256 static device_t pmu = NULL;
257 static device_t pmu_extint = NULL;
260 pmuextint_probe(device_t dev)
262 const char *type = ofw_bus_get_type(dev);
264 if (strcmp(type, "extint-gpio1") != 0)
267 device_set_desc(dev, "Apple PMU99 External Interrupt");
272 pmu_probe(device_t dev)
274 const char *type = ofw_bus_get_type(dev);
276 if (strcmp(type, "via-pmu") != 0)
279 device_set_desc(dev, "Apple PMU99 Controller");
285 setup_pmu_intr(device_t dev, device_t extint)
287 struct pmu_softc *sc;
288 sc = device_get_softc(dev);
291 sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
293 if (sc->sc_irq == NULL) {
294 device_printf(dev, "could not allocate interrupt\n");
298 if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
299 | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
300 device_printf(dev, "could not setup interrupt\n");
301 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
310 pmuextint_attach(device_t dev)
314 return (setup_pmu_intr(pmu,dev));
320 pmu_attach(device_t dev)
322 struct pmu_softc *sc;
326 uint8_t cmd[2] = {2, 0};
328 phandle_t node,child;
329 struct sysctl_ctx_list *ctx;
330 struct sysctl_oid *tree;
332 sc = device_get_softc(dev);
336 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
337 &sc->sc_memrid, RF_ACTIVE);
339 mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
341 if (sc->sc_memr == NULL) {
342 device_printf(dev, "Could not alloc mem resource!\n");
347 * Our interrupt is attached to a GPIO pin. Depending on probe order,
348 * we may not have found it yet. If we haven't, it will find us, and
349 * attach our interrupt then.
352 if (pmu_extint != NULL) {
353 if (setup_pmu_intr(dev,pmu_extint) != 0)
358 sc->sc_batteries = 0;
360 sc->sc_leddev = NULL;
364 reg = PMU_INT_TICK | PMU_INT_ADB | PMU_INT_PCEJECT | PMU_INT_SNDBRT;
365 reg |= PMU_INT_BATTERY;
366 reg |= PMU_INT_ENVIRONMENT;
367 pmu_send(sc, PMU_SET_IMASK, 1, ®, 16, resp);
369 pmu_write_reg(sc, vIER, 0x90); /* make sure VIA interrupts are on */
371 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
372 pmu_send(sc, PMU_GET_VERSION, 1, cmd, 16, resp);
374 /* Initialize child buses (ADB) */
375 node = ofw_bus_get_node(dev);
377 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
380 memset(name, 0, sizeof(name));
381 OF_getprop(child, "name", name, sizeof(name));
384 device_printf(dev, "PMU child <%s>\n",name);
386 if (strncmp(name, "adb", 4) == 0) {
387 sc->adb_bus = device_add_child(dev,"adb",-1);
390 if (strncmp(name, "power-mgt", 9) == 0) {
391 uint32_t prim_info[9];
393 if (OF_getprop(child, "prim-info", prim_info,
394 sizeof(prim_info)) >= 7)
395 sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
397 if (bootverbose && sc->sc_batteries > 0)
398 device_printf(dev, "%d batteries detected\n",
407 ctx = device_get_sysctl_ctx(dev);
408 tree = device_get_sysctl_tree(dev);
410 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
411 "server_mode", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
412 pmu_server_mode, "I", "Enable reboot after power failure");
414 if (sc->sc_batteries > 0) {
415 struct sysctl_oid *oid, *battroot;
418 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
419 "acline", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
420 pmu_acline_state, "I", "AC Line Status");
422 battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
423 "batteries", CTLFLAG_RD, 0, "Battery Information");
425 for (i = 0; i < sc->sc_batteries; i++) {
426 battnum[0] = i + '0';
429 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
430 OID_AUTO, battnum, CTLFLAG_RD, 0,
431 "Battery Information");
433 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
434 "present", CTLTYPE_INT | CTLFLAG_RD, sc,
435 PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
436 "I", "Battery present");
437 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
438 "charging", CTLTYPE_INT | CTLFLAG_RD, sc,
439 PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
440 "I", "Battery charging");
441 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
442 "charge", CTLTYPE_INT | CTLFLAG_RD, sc,
443 PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
444 "I", "Battery charge (mAh)");
445 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
446 "maxcharge", CTLTYPE_INT | CTLFLAG_RD, sc,
447 PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
448 "I", "Maximum battery capacity (mAh)");
449 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
450 "rate", CTLTYPE_INT | CTLFLAG_RD, sc,
451 PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
452 "I", "Battery discharge rate (mA)");
453 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
454 "voltage", CTLTYPE_INT | CTLFLAG_RD, sc,
455 PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
456 "I", "Battery voltage (mV)");
458 /* Knobs for mental compatibility with ACPI */
460 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
461 "time", CTLTYPE_INT | CTLFLAG_RD, sc,
462 PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
463 "I", "Time Remaining (minutes)");
464 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
465 "life", CTLTYPE_INT | CTLFLAG_RD, sc,
466 PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
467 "I", "Capacity remaining (percent)");
472 * Set up LED interface
475 sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
481 clock_register(dev, 1000);
484 * Register power control handler
486 EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc,
489 return (bus_generic_attach(dev));
493 pmu_detach(device_t dev)
495 struct pmu_softc *sc;
497 sc = device_get_softc(dev);
499 if (sc->sc_leddev != NULL)
500 led_destroy(sc->sc_leddev);
502 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
503 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
504 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
505 mtx_destroy(&sc->sc_mutex);
507 return (bus_generic_detach(dev));
511 pmu_read_reg(struct pmu_softc *sc, u_int offset)
513 return (bus_read_1(sc->sc_memr, offset));
517 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
519 bus_write_1(sc->sc_memr, offset, value);
523 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
527 pmu_write_reg(sc, vSR, data);
529 /* wait for intr to come up */
530 /* XXX should add a timeout and bail if it expires */
531 do {} while (pmu_intr_state(sc) == 0);
533 do {} while (pmu_intr_state(sc));
539 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
541 volatile uint8_t scratch;
543 scratch = pmu_read_reg(sc, vSR);
545 /* wait for intr to come up */
546 do {} while (pmu_intr_state(sc) == 0);
548 do {} while (pmu_intr_state(sc));
549 *data = pmu_read_reg(sc, vSR);
554 pmu_intr_state(struct pmu_softc *sc)
556 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
560 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
563 struct pmu_softc *sc = cookie;
565 uint8_t out_len, intreg;
567 intreg = pmu_read_reg(sc, vIER);
569 pmu_write_reg(sc, vIER, intreg);
572 do {} while (pmu_intr_state(sc));
575 pmu_send_byte(sc, cmd);
577 /* send length if necessary */
578 if (pm_send_cmd_type[cmd] < 0) {
579 pmu_send_byte(sc, length);
582 for (i = 0; i < length; i++) {
583 pmu_send_byte(sc, in_msg[i]);
586 /* see if there's data to read */
587 rcv_len = pm_receive_cmd_type[cmd];
593 pmu_read_byte(sc, out_msg);
598 pmu_read_byte(sc, &out_len);
599 rcv_len = out_len + 1;
601 for (i = 1; i < min(rcv_len, rlen); i++)
602 pmu_read_byte(sc, &out_msg[i]);
605 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
612 pmu_poll(device_t dev)
619 pmu_in(struct pmu_softc *sc)
623 reg = pmu_read_reg(sc, vACR);
626 pmu_write_reg(sc, vACR, reg);
630 pmu_out(struct pmu_softc *sc)
634 reg = pmu_read_reg(sc, vACR);
637 pmu_write_reg(sc, vACR, reg);
641 pmu_ack_off(struct pmu_softc *sc)
645 reg = pmu_read_reg(sc, vBufB);
647 pmu_write_reg(sc, vBufB, reg);
651 pmu_ack_on(struct pmu_softc *sc)
655 reg = pmu_read_reg(sc, vBufB);
657 pmu_write_reg(sc, vBufB, reg);
664 struct pmu_softc *sc;
671 sc = device_get_softc(dev);
673 mtx_lock(&sc->sc_mutex);
675 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
676 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
678 mtx_unlock(&sc->sc_mutex);
680 if ((len < 1) || (resp[1] == 0)) {
684 if (resp[1] & PMU_INT_ADB) {
686 * the PMU will turn off autopolling after each command that
687 * it did not issue, so we assume any but TALK R0 is ours and
688 * re-enable autopoll here whenever we receive an ACK for a
691 mtx_lock(&sc->sc_mutex);
693 if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
694 if (sc->sc_autopoll) {
695 uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
696 (sc->sc_autopoll >> 8) & 0xff,
697 sc->sc_autopoll & 0xff};
699 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
703 mtx_unlock(&sc->sc_mutex);
705 adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
711 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
714 struct pmu_softc *sc = device_get_softc(dev);
716 uint8_t packet[16], resp[16];
718 /* construct an ADB command packet and send it */
720 packet[0] = command_byte;
724 for (i = 0; i < len; i++)
725 packet[i + 3] = data[i];
727 mtx_lock(&sc->sc_mutex);
728 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
729 mtx_unlock(&sc->sc_mutex);
738 pmu_adb_autopoll(device_t dev, uint16_t mask)
740 struct pmu_softc *sc = device_get_softc(dev);
742 /* magical incantation to re-enable autopolling */
743 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
746 mtx_lock(&sc->sc_mutex);
748 if (sc->sc_autopoll == mask) {
749 mtx_unlock(&sc->sc_mutex);
753 sc->sc_autopoll = mask & 0xffff;
756 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
758 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
760 mtx_unlock(&sc->sc_mutex);
766 pmu_shutdown(void *xsc, int howto)
768 struct pmu_softc *sc = xsc;
769 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
772 pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL);
774 pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL);
780 pmu_set_sleepled(void *xsc, int onoff)
782 struct pmu_softc *sc = xsc;
783 uint8_t cmd[] = {4, 0, 0};
787 mtx_lock(&sc->sc_mutex);
788 pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
789 mtx_unlock(&sc->sc_mutex);
793 pmu_server_mode(SYSCTL_HANDLER_ARGS)
795 struct pmu_softc *sc = arg1;
797 u_int server_mode = 0;
798 uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
799 uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
803 mtx_lock(&sc->sc_mutex);
804 len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
805 mtx_unlock(&sc->sc_mutex);
808 server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
810 error = sysctl_handle_int(oidp, &server_mode, 0, req);
815 if (error || !req->newptr)
818 if (server_mode == 1)
819 setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
820 else if (server_mode == 0)
821 setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
827 mtx_lock(&sc->sc_mutex);
828 pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
829 mtx_unlock(&sc->sc_mutex);
835 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
843 mtx_lock(&sc->sc_mutex);
844 len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, ®, 16, resp);
845 mtx_unlock(&sc->sc_mutex);
850 /* All PMU battery info replies share a common header:
851 * Byte 1 Payload Format
852 * Byte 2 Battery Flags
855 info->state = resp[2];
861 * Formats 3 and 4 appear to be the same:
868 info->charge = resp[3];
869 info->maxcharge = resp[4];
870 /* Current can be positive or negative */
871 info->current = (int8_t)resp[5];
872 info->voltage = resp[6];
876 * Formats 5 is a wider version of formats 3 and 4
878 * Byte 5-6 Max Charge
883 info->charge = (resp[3] << 8) | resp[4];
884 info->maxcharge = (resp[5] << 8) | resp[6];
885 /* Current can be positive or negative */
886 info->current = (int16_t)((resp[7] << 8) | resp[8]);
887 info->voltage = (resp[9] << 8) | resp[10];
890 device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
899 pmu_acline_state(SYSCTL_HANDLER_ARGS)
901 struct pmu_softc *sc;
902 struct pmu_battstate batt;
907 /* The PMU treats the AC line status as a property of the battery */
908 error = pmu_query_battery(sc, 0, &batt);
913 result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
914 error = sysctl_handle_int(oidp, &result, 0, req);
920 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
922 struct pmu_softc *sc;
923 struct pmu_battstate batt;
928 error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
933 switch (arg2 & 0xff00) {
934 case PMU_BATSYSCTL_PRESENT:
935 result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
937 case PMU_BATSYSCTL_CHARGING:
938 result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
940 case PMU_BATSYSCTL_CHARGE:
941 result = batt.charge;
943 case PMU_BATSYSCTL_MAXCHARGE:
944 result = batt.maxcharge;
946 case PMU_BATSYSCTL_CURRENT:
947 result = batt.current;
949 case PMU_BATSYSCTL_VOLTAGE:
950 result = batt.voltage;
952 case PMU_BATSYSCTL_TIME:
953 /* Time remaining until full charge/discharge, in minutes */
955 if (batt.current >= 0)
956 result = (batt.maxcharge - batt.charge) /* mAh */ * 60
957 / batt.current /* mA */;
959 result = (batt.charge /* mAh */ * 60)
960 / (-batt.current /* mA */);
962 case PMU_BATSYSCTL_LIFE:
963 /* Battery charge fraction, in percent */
964 result = (batt.charge * 100) / batt.maxcharge;
967 /* This should never happen */
971 error = sysctl_handle_int(oidp, &result, 0, req);
976 #define DIFF19041970 2082844800
979 pmu_gettime(device_t dev, struct timespec *ts)
981 struct pmu_softc *sc = device_get_softc(dev);
985 mtx_lock(&sc->sc_mutex);
986 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
987 mtx_unlock(&sc->sc_mutex);
989 memcpy(&sec, &resp[1], 4);
990 ts->tv_sec = sec - DIFF19041970;
997 pmu_settime(device_t dev, struct timespec *ts)
999 struct pmu_softc *sc = device_get_softc(dev);
1002 sec = ts->tv_sec + DIFF19041970;
1004 mtx_lock(&sc->sc_mutex);
1005 pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL);
1006 mtx_unlock(&sc->sc_mutex);