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 /* ADB bus interface */
122 DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send),
123 DEVMETHOD(adb_hb_controller_poll, pmu_poll),
124 DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
126 /* Clock interface */
127 DEVMETHOD(clock_gettime, pmu_gettime),
128 DEVMETHOD(clock_settime, pmu_settime),
133 static driver_t pmu_driver = {
136 sizeof(struct pmu_softc),
139 static devclass_t pmu_devclass;
141 DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0);
142 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
144 static int pmuextint_probe(device_t);
145 static int pmuextint_attach(device_t);
147 static device_method_t pmuextint_methods[] = {
148 /* Device interface */
149 DEVMETHOD(device_probe, pmuextint_probe),
150 DEVMETHOD(device_attach, pmuextint_attach),
155 static driver_t pmuextint_driver = {
161 static devclass_t pmuextint_devclass;
163 DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass, 0, 0);
165 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
166 MODULE_DEPEND(pmu, usb, 1, 1, 1);
168 static void pmu_intr(void *arg);
169 static void pmu_in(struct pmu_softc *sc);
170 static void pmu_out(struct pmu_softc *sc);
171 static void pmu_ack_on(struct pmu_softc *sc);
172 static void pmu_ack_off(struct pmu_softc *sc);
173 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
174 int rlen, uint8_t *out_msg);
175 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
176 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
177 static int pmu_intr_state(struct pmu_softc *);
179 /* these values shows that number of data returned after 'send' cmd is sent */
180 static signed char pm_send_cmd_type[] = {
181 -1, -1, -1, -1, -1, -1, -1, -1,
182 -1, -1, -1, -1, -1, -1, -1, -1,
183 0x01, 0x01, -1, -1, -1, -1, -1, -1,
184 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
185 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
186 0x00, -1, -1, -1, -1, -1, -1, -1,
187 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
188 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
189 0x01, 0x01, -1, -1, -1, -1, -1, -1,
190 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
191 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
192 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
193 0x02, -1, -1, -1, -1, -1, -1, -1,
194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
195 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
196 0x00, 0x00, -1, -1, -1, -1, 0x04, 0x04,
197 0x04, -1, 0x00, -1, -1, -1, -1, -1,
198 0x00, -1, -1, -1, -1, -1, -1, -1,
199 0x01, 0x02, -1, -1, -1, -1, -1, -1,
200 0x00, 0x00, -1, -1, -1, -1, -1, -1,
201 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
202 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
203 -1, -1, -1, -1, -1, -1, -1, -1,
204 -1, -1, -1, -1, -1, -1, -1, -1,
205 -1, -1, -1, -1, -1, -1, -1, -1,
206 -1, -1, -1, -1, -1, -1, -1, -1,
207 0x00, -1, -1, -1, -1, -1, -1, -1,
208 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
209 -1, 0x04, 0x00, -1, -1, -1, -1, -1,
210 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
211 -1, -1, -1, -1, -1, -1, -1, -1,
212 -1, -1, -1, -1, -1, -1, -1, -1
215 /* these values shows that number of data returned after 'receive' cmd is sent */
216 static signed char pm_receive_cmd_type[] = {
217 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 -1, -1, -1, -1, -1, -1, -1, -1,
219 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
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 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 0x02, 0x02, -1, -1, -1, -1, -1, -1,
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
231 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232 -1, -1, -1, -1, -1, -1, 0x01, 0x01,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x06, -1, -1, -1, -1, -1, -1, -1,
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x02, 0x02, -1, -1, -1, -1, -1, -1,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240 -1, -1, -1, -1, -1, -1, -1, -1,
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 -1, -1, -1, -1, -1, -1, -1, -1,
243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
245 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
246 -1, -1, 0x02, -1, -1, -1, -1, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 -1, -1, -1, -1, -1, -1, -1, -1,
251 /* We only have one of each device, so globals are safe */
252 static device_t pmu = NULL;
253 static device_t pmu_extint = NULL;
256 pmuextint_probe(device_t dev)
258 const char *type = ofw_bus_get_type(dev);
260 if (strcmp(type, "extint-gpio1") != 0)
263 device_set_desc(dev, "Apple PMU99 External Interrupt");
268 pmu_probe(device_t dev)
270 const char *type = ofw_bus_get_type(dev);
272 if (strcmp(type, "via-pmu") != 0)
275 device_set_desc(dev, "Apple PMU99 Controller");
281 setup_pmu_intr(device_t dev, device_t extint)
283 struct pmu_softc *sc;
284 sc = device_get_softc(dev);
287 sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
289 if (sc->sc_irq == NULL) {
290 device_printf(dev, "could not allocate interrupt\n");
294 if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
295 | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
296 device_printf(dev, "could not setup interrupt\n");
297 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
306 pmuextint_attach(device_t dev)
310 return (setup_pmu_intr(pmu,dev));
316 pmu_attach(device_t dev)
318 struct pmu_softc *sc;
322 uint8_t cmd[2] = {2, 0};
324 phandle_t node,child;
325 struct sysctl_ctx_list *ctx;
326 struct sysctl_oid *tree;
328 sc = device_get_softc(dev);
332 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
333 &sc->sc_memrid, RF_ACTIVE);
335 mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
337 if (sc->sc_memr == NULL) {
338 device_printf(dev, "Could not alloc mem resource!\n");
343 * Our interrupt is attached to a GPIO pin. Depending on probe order,
344 * we may not have found it yet. If we haven't, it will find us, and
345 * attach our interrupt then.
348 if (pmu_extint != NULL) {
349 if (setup_pmu_intr(dev,pmu_extint) != 0)
354 sc->sc_batteries = 0;
356 sc->sc_leddev = NULL;
360 reg = PMU_INT_TICK | PMU_INT_ADB | PMU_INT_PCEJECT | PMU_INT_SNDBRT;
361 reg |= PMU_INT_BATTERY;
362 reg |= PMU_INT_ENVIRONMENT;
363 pmu_send(sc, PMU_SET_IMASK, 1, ®, 16, resp);
365 pmu_write_reg(sc, vIER, 0x90); /* make sure VIA interrupts are on */
367 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
368 pmu_send(sc, PMU_GET_VERSION, 1, cmd, 16, resp);
370 /* Initialize child buses (ADB) */
371 node = ofw_bus_get_node(dev);
373 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
376 memset(name, 0, sizeof(name));
377 OF_getprop(child, "name", name, sizeof(name));
380 device_printf(dev, "PMU child <%s>\n",name);
382 if (strncmp(name, "adb", 4) == 0) {
383 sc->adb_bus = device_add_child(dev,"adb",-1);
386 if (strncmp(name, "power-mgt", 9) == 0) {
387 uint32_t prim_info[9];
389 if (OF_getprop(child, "prim-info", prim_info,
390 sizeof(prim_info)) >= 7)
391 sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
393 if (bootverbose && sc->sc_batteries > 0)
394 device_printf(dev, "%d batteries detected\n",
403 ctx = device_get_sysctl_ctx(dev);
404 tree = device_get_sysctl_tree(dev);
406 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
407 "server_mode", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
408 pmu_server_mode, "I", "Enable reboot after power failure");
410 if (sc->sc_batteries > 0) {
411 struct sysctl_oid *oid, *battroot;
414 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
415 "acline", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
416 pmu_acline_state, "I", "AC Line Status");
418 battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
419 "batteries", CTLFLAG_RD, 0, "Battery Information");
421 for (i = 0; i < sc->sc_batteries; i++) {
422 battnum[0] = i + '0';
425 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
426 OID_AUTO, battnum, CTLFLAG_RD, 0,
427 "Battery Information");
429 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
430 "present", CTLTYPE_INT | CTLFLAG_RD, sc,
431 PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
432 "I", "Battery present");
433 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
434 "charging", CTLTYPE_INT | CTLFLAG_RD, sc,
435 PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
436 "I", "Battery charging");
437 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
438 "charge", CTLTYPE_INT | CTLFLAG_RD, sc,
439 PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
440 "I", "Battery charge (mAh)");
441 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
442 "maxcharge", CTLTYPE_INT | CTLFLAG_RD, sc,
443 PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
444 "I", "Maximum battery capacity (mAh)");
445 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
446 "rate", CTLTYPE_INT | CTLFLAG_RD, sc,
447 PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
448 "I", "Battery discharge rate (mA)");
449 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
450 "voltage", CTLTYPE_INT | CTLFLAG_RD, sc,
451 PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
452 "I", "Battery voltage (mV)");
454 /* Knobs for mental compatibility with ACPI */
456 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
457 "time", CTLTYPE_INT | CTLFLAG_RD, sc,
458 PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
459 "I", "Time Remaining (minutes)");
460 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
461 "life", CTLTYPE_INT | CTLFLAG_RD, sc,
462 PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
463 "I", "Capacity remaining (percent)");
468 * Set up LED interface
471 sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
477 clock_register(dev, 1000);
480 * Register power control handler
482 EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc,
485 return (bus_generic_attach(dev));
489 pmu_detach(device_t dev)
491 struct pmu_softc *sc;
493 sc = device_get_softc(dev);
495 if (sc->sc_leddev != NULL)
496 led_destroy(sc->sc_leddev);
498 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
499 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
500 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
501 mtx_destroy(&sc->sc_mutex);
503 return (bus_generic_detach(dev));
507 pmu_read_reg(struct pmu_softc *sc, u_int offset)
509 return (bus_read_1(sc->sc_memr, offset));
513 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
515 bus_write_1(sc->sc_memr, offset, value);
519 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
523 pmu_write_reg(sc, vSR, data);
525 /* wait for intr to come up */
526 /* XXX should add a timeout and bail if it expires */
527 do {} while (pmu_intr_state(sc) == 0);
529 do {} while (pmu_intr_state(sc));
535 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
537 volatile uint8_t scratch;
539 scratch = pmu_read_reg(sc, vSR);
541 /* wait for intr to come up */
542 do {} while (pmu_intr_state(sc) == 0);
544 do {} while (pmu_intr_state(sc));
545 *data = pmu_read_reg(sc, vSR);
550 pmu_intr_state(struct pmu_softc *sc)
552 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
556 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
559 struct pmu_softc *sc = cookie;
561 uint8_t out_len, intreg;
563 intreg = pmu_read_reg(sc, vIER);
565 pmu_write_reg(sc, vIER, intreg);
568 do {} while (pmu_intr_state(sc));
571 pmu_send_byte(sc, cmd);
573 /* send length if necessary */
574 if (pm_send_cmd_type[cmd] < 0) {
575 pmu_send_byte(sc, length);
578 for (i = 0; i < length; i++) {
579 pmu_send_byte(sc, in_msg[i]);
582 /* see if there's data to read */
583 rcv_len = pm_receive_cmd_type[cmd];
589 pmu_read_byte(sc, out_msg);
594 pmu_read_byte(sc, &out_len);
595 rcv_len = out_len + 1;
597 for (i = 1; i < min(rcv_len, rlen); i++)
598 pmu_read_byte(sc, &out_msg[i]);
601 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
608 pmu_poll(device_t dev)
615 pmu_in(struct pmu_softc *sc)
619 reg = pmu_read_reg(sc, vACR);
622 pmu_write_reg(sc, vACR, reg);
626 pmu_out(struct pmu_softc *sc)
630 reg = pmu_read_reg(sc, vACR);
633 pmu_write_reg(sc, vACR, reg);
637 pmu_ack_off(struct pmu_softc *sc)
641 reg = pmu_read_reg(sc, vBufB);
643 pmu_write_reg(sc, vBufB, reg);
647 pmu_ack_on(struct pmu_softc *sc)
651 reg = pmu_read_reg(sc, vBufB);
653 pmu_write_reg(sc, vBufB, reg);
660 struct pmu_softc *sc;
667 sc = device_get_softc(dev);
669 mtx_lock(&sc->sc_mutex);
671 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
672 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
674 mtx_unlock(&sc->sc_mutex);
676 if ((len < 1) || (resp[1] == 0)) {
680 if (resp[1] & PMU_INT_ADB) {
682 * the PMU will turn off autopolling after each command that
683 * it did not issue, so we assume any but TALK R0 is ours and
684 * re-enable autopoll here whenever we receive an ACK for a
687 mtx_lock(&sc->sc_mutex);
689 if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
690 if (sc->sc_autopoll) {
691 uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
692 (sc->sc_autopoll >> 8) & 0xff,
693 sc->sc_autopoll & 0xff};
695 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
699 mtx_unlock(&sc->sc_mutex);
701 adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
707 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
710 struct pmu_softc *sc = device_get_softc(dev);
712 uint8_t packet[16], resp[16];
714 /* construct an ADB command packet and send it */
716 packet[0] = command_byte;
720 for (i = 0; i < len; i++)
721 packet[i + 3] = data[i];
723 mtx_lock(&sc->sc_mutex);
724 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
725 mtx_unlock(&sc->sc_mutex);
734 pmu_adb_autopoll(device_t dev, uint16_t mask)
736 struct pmu_softc *sc = device_get_softc(dev);
738 /* magical incantation to re-enable autopolling */
739 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
742 mtx_lock(&sc->sc_mutex);
744 if (sc->sc_autopoll == mask) {
745 mtx_unlock(&sc->sc_mutex);
749 sc->sc_autopoll = mask & 0xffff;
752 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
754 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
756 mtx_unlock(&sc->sc_mutex);
762 pmu_shutdown(void *xsc, int howto)
764 struct pmu_softc *sc = xsc;
765 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
768 pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL);
770 pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL);
776 pmu_set_sleepled(void *xsc, int onoff)
778 struct pmu_softc *sc = xsc;
779 uint8_t cmd[] = {4, 0, 0};
783 mtx_lock(&sc->sc_mutex);
784 pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
785 mtx_unlock(&sc->sc_mutex);
789 pmu_server_mode(SYSCTL_HANDLER_ARGS)
791 struct pmu_softc *sc = arg1;
793 u_int server_mode = 0;
794 uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
795 uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
799 mtx_lock(&sc->sc_mutex);
800 len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
801 mtx_unlock(&sc->sc_mutex);
804 server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
806 error = sysctl_handle_int(oidp, &server_mode, 0, req);
811 if (error || !req->newptr)
814 if (server_mode == 1)
815 setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
816 else if (server_mode == 0)
817 setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
823 mtx_lock(&sc->sc_mutex);
824 pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
825 mtx_unlock(&sc->sc_mutex);
831 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
839 mtx_lock(&sc->sc_mutex);
840 len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, ®, 16, resp);
841 mtx_unlock(&sc->sc_mutex);
846 /* All PMU battery info replies share a common header:
847 * Byte 1 Payload Format
848 * Byte 2 Battery Flags
851 info->state = resp[2];
857 * Formats 3 and 4 appear to be the same:
864 info->charge = resp[3];
865 info->maxcharge = resp[4];
866 /* Current can be positive or negative */
867 info->current = (int8_t)resp[5];
868 info->voltage = resp[6];
872 * Formats 5 is a wider version of formats 3 and 4
874 * Byte 5-6 Max Charge
879 info->charge = (resp[3] << 8) | resp[4];
880 info->maxcharge = (resp[5] << 8) | resp[6];
881 /* Current can be positive or negative */
882 info->current = (int16_t)((resp[7] << 8) | resp[8]);
883 info->voltage = (resp[9] << 8) | resp[10];
886 device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
895 pmu_acline_state(SYSCTL_HANDLER_ARGS)
897 struct pmu_softc *sc;
898 struct pmu_battstate batt;
903 /* The PMU treats the AC line status as a property of the battery */
904 error = pmu_query_battery(sc, 0, &batt);
909 result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
910 error = sysctl_handle_int(oidp, &result, 0, req);
916 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
918 struct pmu_softc *sc;
919 struct pmu_battstate batt;
924 error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
929 switch (arg2 & 0xff00) {
930 case PMU_BATSYSCTL_PRESENT:
931 result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
933 case PMU_BATSYSCTL_CHARGING:
934 result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
936 case PMU_BATSYSCTL_CHARGE:
937 result = batt.charge;
939 case PMU_BATSYSCTL_MAXCHARGE:
940 result = batt.maxcharge;
942 case PMU_BATSYSCTL_CURRENT:
943 result = batt.current;
945 case PMU_BATSYSCTL_VOLTAGE:
946 result = batt.voltage;
948 case PMU_BATSYSCTL_TIME:
949 /* Time remaining until full charge/discharge, in minutes */
951 if (batt.current >= 0)
952 result = (batt.maxcharge - batt.charge) /* mAh */ * 60
953 / batt.current /* mA */;
955 result = (batt.charge /* mAh */ * 60)
956 / (-batt.current /* mA */);
958 case PMU_BATSYSCTL_LIFE:
959 /* Battery charge fraction, in percent */
960 result = (batt.charge * 100) / batt.maxcharge;
963 /* This should never happen */
967 error = sysctl_handle_int(oidp, &result, 0, req);
972 #define DIFF19041970 2082844800
975 pmu_gettime(device_t dev, struct timespec *ts)
977 struct pmu_softc *sc = device_get_softc(dev);
981 mtx_lock(&sc->sc_mutex);
982 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
983 mtx_unlock(&sc->sc_mutex);
985 memcpy(&sec, &resp[1], 4);
986 ts->tv_sec = sec - DIFF19041970;
993 pmu_settime(device_t dev, struct timespec *ts)
995 struct pmu_softc *sc = device_get_softc(dev);
998 sec = ts->tv_sec + DIFF19041970;
1000 mtx_lock(&sc->sc_mutex);
1001 pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL);
1002 mtx_unlock(&sc->sc_mutex);