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/sysctl.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/led/led.h>
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/md_var.h>
48 #include <machine/pio.h>
49 #include <machine/resource.h>
56 #include <dev/adb/adb.h>
64 static int pmu_probe(device_t);
65 static int pmu_attach(device_t);
66 static int pmu_detach(device_t);
68 static u_int pmu_adb_send(device_t dev, u_char command_byte, int len,
69 u_char *data, u_char poll);
70 static u_int pmu_adb_autopoll(device_t dev, uint16_t mask);
71 static u_int pmu_poll(device_t dev);
73 static void pmu_set_sleepled(void *xsc, int onoff);
74 static int pmu_server_mode(SYSCTL_HANDLER_ARGS);
75 static int pmu_acline_state(SYSCTL_HANDLER_ARGS);
76 static int pmu_query_battery(struct pmu_softc *sc, int batt,
77 struct pmu_battstate *info);
78 static int pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
81 * List of battery-related sysctls we might ask for
85 PMU_BATSYSCTL_PRESENT = 1 << 8,
86 PMU_BATSYSCTL_CHARGING = 2 << 8,
87 PMU_BATSYSCTL_CHARGE = 3 << 8,
88 PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
89 PMU_BATSYSCTL_CURRENT = 5 << 8,
90 PMU_BATSYSCTL_VOLTAGE = 6 << 8,
91 PMU_BATSYSCTL_TIME = 7 << 8,
92 PMU_BATSYSCTL_LIFE = 8 << 8
95 static device_method_t pmu_methods[] = {
96 /* Device interface */
97 DEVMETHOD(device_probe, pmu_probe),
98 DEVMETHOD(device_attach, pmu_attach),
99 DEVMETHOD(device_detach, pmu_detach),
100 DEVMETHOD(device_shutdown, bus_generic_shutdown),
101 DEVMETHOD(device_suspend, bus_generic_suspend),
102 DEVMETHOD(device_resume, bus_generic_resume),
104 /* bus interface, for ADB root */
105 DEVMETHOD(bus_print_child, bus_generic_print_child),
106 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
108 /* ADB bus interface */
109 DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send),
110 DEVMETHOD(adb_hb_controller_poll, pmu_poll),
111 DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
116 static driver_t pmu_driver = {
119 sizeof(struct pmu_softc),
122 static devclass_t pmu_devclass;
124 DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0);
125 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
127 static int pmuextint_probe(device_t);
128 static int pmuextint_attach(device_t);
130 static device_method_t pmuextint_methods[] = {
131 /* Device interface */
132 DEVMETHOD(device_probe, pmuextint_probe),
133 DEVMETHOD(device_attach, pmuextint_attach),
138 static driver_t pmuextint_driver = {
144 static devclass_t pmuextint_devclass;
146 DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass, 0, 0);
148 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
149 MODULE_DEPEND(pmu, usb, 1, 1, 1);
151 static void pmu_intr(void *arg);
152 static void pmu_in(struct pmu_softc *sc);
153 static void pmu_out(struct pmu_softc *sc);
154 static void pmu_ack_on(struct pmu_softc *sc);
155 static void pmu_ack_off(struct pmu_softc *sc);
156 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
157 int rlen, uint8_t *out_msg);
158 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
159 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
160 static int pmu_intr_state(struct pmu_softc *);
162 /* these values shows that number of data returned after 'send' cmd is sent */
163 static signed char pm_send_cmd_type[] = {
164 -1, -1, -1, -1, -1, -1, -1, -1,
165 -1, -1, -1, -1, -1, -1, -1, -1,
166 0x01, 0x01, -1, -1, -1, -1, -1, -1,
167 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
168 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
169 0x00, -1, -1, -1, -1, -1, -1, -1,
170 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
171 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
172 0x01, 0x01, -1, -1, -1, -1, -1, -1,
173 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
174 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
175 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
176 0x02, -1, -1, -1, -1, -1, -1, -1,
177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
178 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
179 0x00, 0x00, -1, -1, -1, -1, 0x04, 0x04,
180 0x04, -1, 0x00, -1, -1, -1, -1, -1,
181 0x00, -1, -1, -1, -1, -1, -1, -1,
182 0x01, 0x02, -1, -1, -1, -1, -1, -1,
183 0x00, 0x00, -1, -1, -1, -1, -1, -1,
184 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
185 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
186 -1, -1, -1, -1, -1, -1, -1, -1,
187 -1, -1, -1, -1, -1, -1, -1, -1,
188 -1, -1, -1, -1, -1, -1, -1, -1,
189 -1, -1, -1, -1, -1, -1, -1, -1,
190 0x00, -1, -1, -1, -1, -1, -1, -1,
191 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
192 -1, 0x04, 0x00, -1, -1, -1, -1, -1,
193 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
194 -1, -1, -1, -1, -1, -1, -1, -1,
195 -1, -1, -1, -1, -1, -1, -1, -1
198 /* these values shows that number of data returned after 'receive' cmd is sent */
199 static signed char pm_receive_cmd_type[] = {
200 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 -1, -1, -1, -1, -1, -1, -1, -1,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 -1, -1, -1, -1, -1, -1, -1, -1,
206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209 0x02, 0x02, -1, -1, -1, -1, -1, -1,
210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
214 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215 -1, -1, -1, -1, -1, -1, 0x01, 0x01,
216 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 0x06, -1, -1, -1, -1, -1, -1, -1,
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219 0x02, 0x02, -1, -1, -1, -1, -1, -1,
220 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
222 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223 -1, -1, -1, -1, -1, -1, -1, -1,
224 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225 -1, -1, -1, -1, -1, -1, -1, -1,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
228 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
229 -1, -1, 0x02, -1, -1, -1, -1, 0x00,
230 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
231 -1, -1, -1, -1, -1, -1, -1, -1,
234 /* We only have one of each device, so globals are safe */
235 static device_t pmu = NULL;
236 static device_t pmu_extint = NULL;
239 pmuextint_probe(device_t dev)
241 const char *type = ofw_bus_get_type(dev);
243 if (strcmp(type, "extint-gpio1") != 0)
246 device_set_desc(dev, "Apple PMU99 External Interrupt");
251 pmu_probe(device_t dev)
253 const char *type = ofw_bus_get_type(dev);
255 if (strcmp(type, "via-pmu") != 0)
258 device_set_desc(dev, "Apple PMU99 Controller");
264 setup_pmu_intr(device_t dev, device_t extint)
266 struct pmu_softc *sc;
267 sc = device_get_softc(dev);
270 sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
272 if (sc->sc_irq == NULL) {
273 device_printf(dev, "could not allocate interrupt\n");
277 if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
278 | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
279 device_printf(dev, "could not setup interrupt\n");
280 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
289 pmuextint_attach(device_t dev)
293 return (setup_pmu_intr(pmu,dev));
299 pmu_attach(device_t dev)
301 struct pmu_softc *sc;
305 uint8_t cmd[2] = {2, 0};
307 phandle_t node,child;
308 struct sysctl_ctx_list *ctx;
309 struct sysctl_oid *tree;
311 sc = device_get_softc(dev);
315 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
316 &sc->sc_memrid, RF_ACTIVE);
318 mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
320 if (sc->sc_memr == NULL) {
321 device_printf(dev, "Could not alloc mem resource!\n");
326 * Our interrupt is attached to a GPIO pin. Depending on probe order,
327 * we may not have found it yet. If we haven't, it will find us, and
328 * attach our interrupt then.
331 if (pmu_extint != NULL) {
332 if (setup_pmu_intr(dev,pmu_extint) != 0)
337 sc->sc_batteries = 0;
339 sc->sc_leddev = NULL;
343 reg = PMU_INT_TICK | PMU_INT_ADB | PMU_INT_PCEJECT | PMU_INT_SNDBRT;
344 reg |= PMU_INT_BATTERY;
345 reg |= PMU_INT_ENVIRONMENT;
346 pmu_send(sc, PMU_SET_IMASK, 1, ®, 16, resp);
348 pmu_write_reg(sc, vIER, 0x90); /* make sure VIA interrupts are on */
350 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
351 pmu_send(sc, PMU_GET_VERSION, 1, cmd, 16, resp);
353 /* Initialize child buses (ADB) */
354 node = ofw_bus_get_node(dev);
356 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
359 memset(name, 0, sizeof(name));
360 OF_getprop(child, "name", name, sizeof(name));
363 device_printf(dev, "PMU child <%s>\n",name);
365 if (strncmp(name, "adb", 4) == 0) {
366 sc->adb_bus = device_add_child(dev,"adb",-1);
369 if (strncmp(name, "power-mgt", 9) == 0) {
370 uint32_t prim_info[9];
372 if (OF_getprop(child, "prim-info", prim_info,
373 sizeof(prim_info)) >= 7)
374 sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
376 if (bootverbose && sc->sc_batteries > 0)
377 device_printf(dev, "%d batteries detected\n",
386 ctx = device_get_sysctl_ctx(dev);
387 tree = device_get_sysctl_tree(dev);
389 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
390 "server_mode", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
391 pmu_server_mode, "I", "Enable reboot after power failure");
393 if (sc->sc_batteries > 0) {
394 struct sysctl_oid *oid, *battroot;
397 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
398 "acline", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
399 pmu_acline_state, "I", "AC Line Status");
401 battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
402 "batteries", CTLFLAG_RD, 0, "Battery Information");
404 for (i = 0; i < sc->sc_batteries; i++) {
405 battnum[0] = i + '0';
408 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
409 OID_AUTO, battnum, CTLFLAG_RD, 0,
410 "Battery Information");
412 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
413 "present", CTLTYPE_INT | CTLFLAG_RD, sc,
414 PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
415 "I", "Battery present");
416 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
417 "charging", CTLTYPE_INT | CTLFLAG_RD, sc,
418 PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
419 "I", "Battery charging");
420 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
421 "charge", CTLTYPE_INT | CTLFLAG_RD, sc,
422 PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
423 "I", "Battery charge (mAh)");
424 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
425 "maxcharge", CTLTYPE_INT | CTLFLAG_RD, sc,
426 PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
427 "I", "Maximum battery capacity (mAh)");
428 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
429 "rate", CTLTYPE_INT | CTLFLAG_RD, sc,
430 PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
431 "I", "Battery discharge rate (mA)");
432 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
433 "voltage", CTLTYPE_INT | CTLFLAG_RD, sc,
434 PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
435 "I", "Battery voltage (mV)");
437 /* Knobs for mental compatibility with ACPI */
439 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
440 "time", CTLTYPE_INT | CTLFLAG_RD, sc,
441 PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
442 "I", "Time Remaining (minutes)");
443 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
444 "life", CTLTYPE_INT | CTLFLAG_RD, sc,
445 PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
446 "I", "Capacity remaining (percent)");
451 * Set up LED interface
454 sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
456 return (bus_generic_attach(dev));
460 pmu_detach(device_t dev)
462 struct pmu_softc *sc;
464 sc = device_get_softc(dev);
466 if (sc->sc_leddev != NULL)
467 led_destroy(sc->sc_leddev);
469 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
470 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
471 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
472 mtx_destroy(&sc->sc_mutex);
474 return (bus_generic_detach(dev));
478 pmu_read_reg(struct pmu_softc *sc, u_int offset)
480 return (bus_read_1(sc->sc_memr, offset));
484 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
486 bus_write_1(sc->sc_memr, offset, value);
490 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
494 pmu_write_reg(sc, vSR, data);
496 /* wait for intr to come up */
497 /* XXX should add a timeout and bail if it expires */
498 do {} while (pmu_intr_state(sc) == 0);
500 do {} while (pmu_intr_state(sc));
506 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
508 volatile uint8_t scratch;
510 scratch = pmu_read_reg(sc, vSR);
512 /* wait for intr to come up */
513 do {} while (pmu_intr_state(sc) == 0);
515 do {} while (pmu_intr_state(sc));
516 *data = pmu_read_reg(sc, vSR);
521 pmu_intr_state(struct pmu_softc *sc)
523 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
527 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
530 struct pmu_softc *sc = cookie;
532 uint8_t out_len, intreg;
534 intreg = pmu_read_reg(sc, vIER);
536 pmu_write_reg(sc, vIER, intreg);
539 do {} while (pmu_intr_state(sc));
542 pmu_send_byte(sc, cmd);
544 /* send length if necessary */
545 if (pm_send_cmd_type[cmd] < 0) {
546 pmu_send_byte(sc, length);
549 for (i = 0; i < length; i++) {
550 pmu_send_byte(sc, in_msg[i]);
553 /* see if there's data to read */
554 rcv_len = pm_receive_cmd_type[cmd];
560 pmu_read_byte(sc, out_msg);
565 pmu_read_byte(sc, &out_len);
566 rcv_len = out_len + 1;
568 for (i = 1; i < min(rcv_len, rlen); i++)
569 pmu_read_byte(sc, &out_msg[i]);
572 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
579 pmu_poll(device_t dev)
586 pmu_in(struct pmu_softc *sc)
590 reg = pmu_read_reg(sc, vACR);
593 pmu_write_reg(sc, vACR, reg);
597 pmu_out(struct pmu_softc *sc)
601 reg = pmu_read_reg(sc, vACR);
604 pmu_write_reg(sc, vACR, reg);
608 pmu_ack_off(struct pmu_softc *sc)
612 reg = pmu_read_reg(sc, vBufB);
614 pmu_write_reg(sc, vBufB, reg);
618 pmu_ack_on(struct pmu_softc *sc)
622 reg = pmu_read_reg(sc, vBufB);
624 pmu_write_reg(sc, vBufB, reg);
631 struct pmu_softc *sc;
638 sc = device_get_softc(dev);
640 mtx_lock(&sc->sc_mutex);
642 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
643 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
645 mtx_unlock(&sc->sc_mutex);
647 if ((len < 1) || (resp[1] == 0)) {
651 if (resp[1] & PMU_INT_ADB) {
653 * the PMU will turn off autopolling after each command that
654 * it did not issue, so we assume any but TALK R0 is ours and
655 * re-enable autopoll here whenever we receive an ACK for a
658 mtx_lock(&sc->sc_mutex);
660 if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
661 if (sc->sc_autopoll) {
662 uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
663 (sc->sc_autopoll >> 8) & 0xff,
664 sc->sc_autopoll & 0xff};
666 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
670 mtx_unlock(&sc->sc_mutex);
672 adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
678 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
681 struct pmu_softc *sc = device_get_softc(dev);
683 uint8_t packet[16], resp[16];
685 /* construct an ADB command packet and send it */
687 packet[0] = command_byte;
691 for (i = 0; i < len; i++)
692 packet[i + 3] = data[i];
694 mtx_lock(&sc->sc_mutex);
695 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
696 mtx_unlock(&sc->sc_mutex);
705 pmu_adb_autopoll(device_t dev, uint16_t mask)
707 struct pmu_softc *sc = device_get_softc(dev);
709 /* magical incantation to re-enable autopolling */
710 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
713 mtx_lock(&sc->sc_mutex);
715 if (sc->sc_autopoll == mask) {
716 mtx_unlock(&sc->sc_mutex);
720 sc->sc_autopoll = mask & 0xffff;
723 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
725 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
727 mtx_unlock(&sc->sc_mutex);
733 pmu_set_sleepled(void *xsc, int onoff)
735 struct pmu_softc *sc = xsc;
736 uint8_t cmd[] = {4, 0, 0};
740 mtx_lock(&sc->sc_mutex);
741 pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
742 mtx_unlock(&sc->sc_mutex);
746 pmu_server_mode(SYSCTL_HANDLER_ARGS)
748 struct pmu_softc *sc = arg1;
750 u_int server_mode = 0;
751 uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
752 uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
756 mtx_lock(&sc->sc_mutex);
757 len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
758 mtx_unlock(&sc->sc_mutex);
761 server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
763 error = sysctl_handle_int(oidp, &server_mode, 0, req);
768 if (error || !req->newptr)
771 if (server_mode == 1)
772 setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
773 else if (server_mode == 0)
774 setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
780 mtx_lock(&sc->sc_mutex);
781 pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
782 mtx_unlock(&sc->sc_mutex);
788 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
796 mtx_lock(&sc->sc_mutex);
797 len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, ®, 16, resp);
798 mtx_unlock(&sc->sc_mutex);
803 /* All PMU battery info replies share a common header:
804 * Byte 1 Payload Format
805 * Byte 2 Battery Flags
808 info->state = resp[2];
814 * Formats 3 and 4 appear to be the same:
821 info->charge = resp[3];
822 info->maxcharge = resp[4];
823 /* Current can be positive or negative */
824 info->current = (int8_t)resp[5];
825 info->voltage = resp[6];
829 * Formats 5 is a wider version of formats 3 and 4
831 * Byte 5-6 Max Charge
836 info->charge = (resp[3] << 8) | resp[4];
837 info->maxcharge = (resp[5] << 8) | resp[6];
838 /* Current can be positive or negative */
839 info->current = (int16_t)((resp[7] << 8) | resp[8]);
840 info->voltage = (resp[9] << 8) | resp[10];
843 device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
852 pmu_acline_state(SYSCTL_HANDLER_ARGS)
854 struct pmu_softc *sc;
855 struct pmu_battstate batt;
860 /* The PMU treats the AC line status as a property of the battery */
861 error = pmu_query_battery(sc, 0, &batt);
866 result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
867 error = sysctl_handle_int(oidp, &result, 0, req);
873 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
875 struct pmu_softc *sc;
876 struct pmu_battstate batt;
881 error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
886 switch (arg2 & 0xff00) {
887 case PMU_BATSYSCTL_PRESENT:
888 result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
890 case PMU_BATSYSCTL_CHARGING:
891 result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
893 case PMU_BATSYSCTL_CHARGE:
894 result = batt.charge;
896 case PMU_BATSYSCTL_MAXCHARGE:
897 result = batt.maxcharge;
899 case PMU_BATSYSCTL_CURRENT:
900 result = batt.current;
902 case PMU_BATSYSCTL_VOLTAGE:
903 result = batt.voltage;
905 case PMU_BATSYSCTL_TIME:
906 /* Time remaining until full charge/discharge, in minutes */
908 if (batt.current >= 0)
909 result = (batt.maxcharge - batt.charge) /* mAh */ * 60
910 / batt.current /* mA */;
912 result = (batt.charge /* mAh */ * 60)
913 / (-batt.current /* mA */);
915 case PMU_BATSYSCTL_LIFE:
916 /* Battery charge fraction, in percent */
917 result = (batt.charge * 100) / batt.maxcharge;
920 /* This should never happen */
924 error = sysctl_handle_int(oidp, &result, 0, req);