Home | History | Annotate | Line # | Download | only in dev
pmu.c revision 1.43
      1 /*	$NetBSD: pmu.c,v 1.43 2025/09/07 21:45:14 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: pmu.c,v 1.43 2025/09/07 21:45:14 thorpej Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/proc.h>
     37 #include <sys/kthread.h>
     38 #include <sys/atomic.h>
     39 #include <sys/condvar.h>
     40 #include <sys/mutex.h>
     41 
     42 #include <sys/bus.h>
     43 #include <machine/pio.h>
     44 #include <machine/autoconf.h>
     45 #include <dev/clock_subr.h>
     46 #include <dev/i2c/i2cvar.h>
     47 
     48 #include <dev/sysmon/sysmonvar.h>
     49 
     50 #include <macppc/dev/viareg.h>
     51 #include <macppc/dev/pmuvar.h>
     52 #include <macppc/dev/batteryvar.h>
     53 
     54 #include <dev/ofw/openfirm.h>
     55 #include <dev/adb/adbvar.h>
     56 #include "opt_pmu.h"
     57 #include "nadb.h"
     58 
     59 #ifdef PMU_DEBUG
     60 #define DPRINTF printf
     61 #else
     62 #define DPRINTF while (0) printf
     63 #endif
     64 
     65 #define PMU_NOTREADY	0x1	/* has not been initialized yet */
     66 #define PMU_IDLE	0x2	/* the bus is currently idle */
     67 #define PMU_OUT		0x3	/* sending out a command */
     68 #define PMU_IN		0x4	/* receiving data */
     69 
     70 static void pmu_attach(device_t, device_t, void *);
     71 static int pmu_match(device_t, cfdata_t, void *);
     72 static void pmu_autopoll(void *, int);
     73 
     74 static int pmu_intr(void *);
     75 
     76 
     77 /* bits for sc_pending, as signals to the event thread */
     78 #define PMU_EV_CARD0	1
     79 #define PMU_EV_CARD1	2
     80 #define PMU_EV_BUTTON	4
     81 #define PMU_EV_LID	8
     82 
     83 struct pmu_softc {
     84 	device_t sc_dev;
     85 	void *sc_ih;
     86 	struct todr_chip_handle sc_todr;
     87 	struct adb_bus_accessops sc_adbops;
     88 	struct i2c_controller sc_i2c;
     89 	struct pmu_ops sc_pmu_ops;
     90 	struct sysmon_pswitch sc_lidswitch;
     91 	struct sysmon_pswitch sc_powerbutton;
     92 	bus_space_tag_t sc_memt;
     93 	bus_space_handle_t sc_memh;
     94 	uint32_t sc_flags;
     95 #define PMU_HAS_BACKLIGHT_CONTROL	1
     96 	int sc_node;
     97 	int sc_error;
     98 	int sc_autopoll;
     99 	int sc_brightness, sc_brightness_wanted;
    100 	int sc_volume, sc_volume_wanted;
    101 	int sc_lid_closed;
    102 	int sc_is_rackmac;
    103 	int sc_button;
    104 	uint8_t sc_env_old;
    105 	uint8_t sc_env_mask;
    106 	/* deferred processing */
    107 	lwp_t *sc_thread;
    108 	int sc_pending;
    109 	/* signalling the event thread */
    110 	kcondvar_t sc_event;
    111 	kmutex_t sc_evmtx;
    112 	/* ADB */
    113 	void (*sc_adb_handler)(void *, int, uint8_t *);
    114 	void *sc_adb_cookie;
    115 	void (*sc_callback)(void *);
    116 	void *sc_cb_cookie;
    117 };
    118 
    119 CFATTACH_DECL_NEW(pmu, sizeof(struct pmu_softc),
    120     pmu_match, pmu_attach, NULL, NULL);
    121 
    122 static inline void pmu_write_reg(struct pmu_softc *, int, uint8_t);
    123 static inline uint8_t pmu_read_reg(struct pmu_softc *, int);
    124 static void pmu_in(struct pmu_softc *);
    125 static void pmu_out(struct pmu_softc *);
    126 static void pmu_ack_off(struct pmu_softc *);
    127 static void pmu_ack_on(struct pmu_softc *);
    128 static int pmu_intr_state(struct pmu_softc *);
    129 
    130 static void pmu_init(struct pmu_softc *);
    131 static void pmu_thread(void *);
    132 static void pmu_eject_card(struct pmu_softc *, int);
    133 static void pmu_update_brightness(struct pmu_softc *);
    134 static void pmu_register_callback(void *, void (*)(void *), void *);
    135 /*
    136  * send a message to the PMU.
    137  */
    138 static int pmu_send(void *, int, int, uint8_t *, int, uint8_t *);
    139 static void pmu_adb_poll(void *);
    140 static int pmu_todr_set(todr_chip_handle_t, struct timeval *);
    141 static int pmu_todr_get(todr_chip_handle_t, struct timeval *);
    142 
    143 static int pmu_adb_handler(void *, int, uint8_t *);
    144 
    145 static struct pmu_softc *pmu0 = NULL;
    146 
    147 /* ADB bus attachment stuff */
    148 static 	int pmu_adb_send(void *, int, int, int, uint8_t *);
    149 static	int pmu_adb_set_handler(void *, void (*)(void *, int, uint8_t *), void *);
    150 
    151 /* i2c stuff */
    152 static int pmu_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
    153 		    void *, size_t, int);
    154 
    155 static void pmu_attach_legacy_battery(struct pmu_softc *);
    156 static void pmu_attach_smart_battery(struct pmu_softc *, int);
    157 static int  pmu_print(void *, const char *);
    158 
    159 /* these values shows that number of data returned after 'send' cmd is sent */
    160 static signed char pm_send_cmd_type[] = {
    161 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    162 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    163 	0x01, 0x01,   -1,   -1,   -1,   -1,   -1,   -1,
    164 	0x00, 0x00,   -1,   -1,   -1,   -1,   -1, 0x00,
    165 	  -1, 0x00, 0x02, 0x01, 0x01,   -1,   -1,   -1,
    166 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    167 	0x04, 0x14,   -1, 0x03,   -1,   -1,   -1,   -1,
    168 	0x00, 0x00, 0x02, 0x02,   -1,   -1,   -1,   -1,
    169 	0x01, 0x01,   -1,   -1,   -1,   -1,   -1,   -1,
    170 	0x00, 0x00,   -1,   -1, 0x01,   -1,   -1,   -1,
    171 	0x01, 0x00, 0x02, 0x02,   -1, 0x01, 0x03, 0x01,
    172 	0x00, 0x01, 0x00, 0x00, 0x00,   -1,   -1,   -1,
    173 	0x02,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    174 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   -1,   -1,
    175 	0x01, 0x01, 0x01,   -1,   -1,   -1,   -1,   -1,
    176 	0x00, 0x00,   -1,   -1,   -1,   -1, 0x04, 0x04,
    177 	0x04,   -1, 0x00,   -1,   -1,   -1,   -1,   -1,
    178 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    179 	0x01, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
    180 	0x00, 0x00,   -1,   -1,   -1,   -1,   -1,   -1,
    181 	0x02, 0x02, 0x02, 0x04,   -1, 0x00,   -1,   -1,
    182 	0x01, 0x01, 0x03, 0x02,   -1,   -1,   -1,   -1,
    183 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    184 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    185 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    186 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    187 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    188 	0x01, 0x01,   -1,   -1, 0x00, 0x00,   -1,   -1,
    189 	  -1, 0x04, 0x00,   -1,   -1,   -1,   -1,   -1,
    190 	0x03,   -1, 0x00,   -1, 0x00,   -1,   -1, 0x00,
    191 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    192 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1
    193 };
    194 
    195 /* these values shows that number of data returned after 'receive' cmd is sent */
    196 static signed char pm_receive_cmd_type[] = {
    197 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    198 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    199 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    200 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1, 0x00,
    201 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    202 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    203 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    204 	0x05, 0x15,   -1, 0x02,   -1,   -1,   -1,   -1,
    205 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    206 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
    207 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    208 	0x02, 0x00, 0x03, 0x03,   -1,   -1,   -1,   -1,
    209 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    210 	0x04, 0x04, 0x03, 0x09,   -1,   -1,   -1,   -1,
    211 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    212 	  -1,   -1,   -1,   -1,   -1,   -1, 0x01, 0x01,
    213 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    214 	0x06,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    215 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    216 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
    217 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    218 	0x02, 0x00, 0x00, 0x00,   -1,   -1,   -1,   -1,
    219 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    220 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    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, 0x02,   -1,   -1,   -1,
    225 	0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
    226 	  -1,   -1, 0x02,   -1,   -1,   -1,   -1, 0x00,
    227 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    228 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
    229 };
    230 
    231 static const char *has_legacy_battery[] = {
    232 	"AAPL,3500",
    233 	"AAPL,3400/2400",
    234 	NULL };
    235 
    236 static const char *has_two_smart_batteries[] = {
    237 	"AAPL,PowerBook1998",
    238 	"PowerBook1,1",
    239 	NULL };
    240 
    241 static int
    242 pmu_match(device_t parent, cfdata_t cf, void *aux)
    243 {
    244 	struct confargs *ca = aux;
    245 
    246 	if (ca->ca_nreg < 8)
    247 		return 0;
    248 
    249 	if (ca->ca_nintr < 4)
    250 		return 0;
    251 
    252 	if (strcmp(ca->ca_name, "via-pmu") == 0) {
    253 		return 10;
    254 	}
    255 
    256 	return 0;
    257 }
    258 
    259 static void
    260 pmu_attach(device_t parent, device_t self, void *aux)
    261 {
    262 	struct confargs *ca = aux;
    263 	struct pmu_softc *sc = device_private(self);
    264 	struct i2cbus_attach_args iba;
    265 	uint32_t regs[16];
    266 	int irq = ca->ca_intr[0];
    267 	int node, extint_node, root_node;
    268 	int nbat = 1, i, pmnode;
    269 	int type = IST_EDGE;
    270 	uint8_t cmd[2] = {2, 0};
    271 	uint8_t resp[16];
    272 	char name[256], model[32];
    273 	prop_dictionary_t dict = device_properties(self);
    274 
    275 	extint_node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1");
    276 	if (extint_node) {
    277 
    278 		OF_getprop(extint_node, "interrupts", &irq, 4);
    279 		type = IST_LEVEL;
    280 	}
    281 
    282 	aprint_normal(" irq %d: ", irq);
    283 
    284 	sc->sc_dev = self;
    285 	sc->sc_node = ca->ca_node;
    286 	sc->sc_memt = ca->ca_tag;
    287 
    288 	root_node = OF_finddevice("/");
    289 
    290 	sc->sc_error = 0;
    291 	sc->sc_autopoll = 0;
    292 	sc->sc_pending = 0;
    293 	sc->sc_env_old = 0;
    294 	sc->sc_brightness = sc->sc_brightness_wanted = 0x80;
    295 	sc->sc_volume = sc->sc_volume_wanted = 0x80;
    296 	sc->sc_flags = 0;
    297 	sc->sc_callback = NULL;
    298 	sc->sc_lid_closed = 0;
    299 	sc->sc_button = 0;
    300 	sc->sc_env_mask = 0xff;
    301 	sc->sc_is_rackmac = 0;
    302 
    303 	cv_init(&sc->sc_event, "pmu_event");
    304 	mutex_init(&sc->sc_evmtx, MUTEX_DEFAULT, IPL_NONE);
    305 
    306 	/*
    307 	 * core99 PowerMacs like to send environment messages with the lid
    308 	 * switch bit set - since that doesn't make any sense here and it
    309 	 * probably means something else anyway we mask it out
    310 	 */
    311 
    312 	if (OF_getprop(root_node, "model", model, 32) != 0) {
    313 		if (strncmp(model, "PowerMac", 8) == 0) {
    314 			sc->sc_env_mask = PMU_ENV_POWER_BUTTON;
    315 		}
    316 		if (strncmp(model, "RackMac1", 8) == 0) {
    317 			sc->sc_is_rackmac = 1;
    318 		}
    319 	}
    320 
    321 	if (bus_space_map(sc->sc_memt, ca->ca_reg[0] + ca->ca_baseaddr,
    322 	    ca->ca_reg[1], 0, &sc->sc_memh) != 0) {
    323 		aprint_error_dev(self, "unable to map registers\n");
    324 		return;
    325 	}
    326 	sc->sc_ih = intr_establish_xname(irq, type, IPL_TTY, pmu_intr, sc,
    327 	    device_xname(self));
    328 
    329 	pmu_init(sc);
    330 
    331 	sc->sc_pmu_ops.cookie = sc;
    332 	sc->sc_pmu_ops.do_command = pmu_send;
    333 	sc->sc_pmu_ops.register_callback = pmu_register_callback;
    334 
    335 	if (pmu0 == NULL)
    336 		pmu0 = sc;
    337 
    338 	pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
    339 
    340 	/* check what kind of PMU we're talking to */
    341 	if (pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp) > 1)
    342 		aprint_normal(" rev. %d", resp[1]);
    343 	aprint_normal("\n");
    344 
    345 	node = OF_child(sc->sc_node);
    346 
    347 	while (node != 0) {
    348 
    349 		if (OF_getprop(node, "name", name, 256) <= 0)
    350 			goto next;
    351 
    352 		if (strncmp(name, "pmu-i2c", 8) == 0) {
    353 			int devs, sensors;
    354 			uint32_t addr, reg;
    355 			char compat[256];
    356 			prop_array_t cfg;
    357 			prop_dictionary_t dev;
    358 			prop_data_t data;
    359 
    360 			aprint_normal_dev(self, "initializing IIC bus\n");
    361 
    362 			cfg = prop_array_create();
    363 			prop_dictionary_set(dict, "i2c-child-devices", cfg);
    364 			prop_object_release(cfg);
    365 
    366 			/* look for i2c devices */
    367 			devs = OF_child(node);
    368 			while (devs != 0) {
    369 				int clen;
    370 				if (OF_getprop(devs, "name", name, 256) <= 0)
    371 					goto skip;
    372 				if ((clen = OF_getprop(devs, "compatible",
    373 				    compat, 256)) <= 0)
    374 					goto skip;
    375 				if (OF_getprop(devs, "reg", &addr, 4) <= 0)
    376 					goto skip;
    377 				addr = (addr & 0xff) >> 1;
    378 				DPRINTF("-> %s@%x\n", name, addr);
    379 				dev = prop_dictionary_create();
    380 				prop_dictionary_set_string(dev, "name", name);
    381 				data = prop_data_create_copy(compat, clen);
    382 				prop_dictionary_set(dev, "compatible", data);
    383 				prop_object_release(data);
    384 				prop_dictionary_set_uint32(dev, "addr", addr);
    385 				prop_dictionary_set_uint64(dev, "cookie", devs);
    386 				if (OF_getprop(devs, "config-reg", &reg, 4) == 4) {
    387 					prop_dictionary_set_uint32(dev, "config-reg", reg);
    388 				}
    389 				sensors = OF_child(devs);
    390 				while (sensors != 0) {
    391 					char loc[64];
    392 					char pname[8];
    393 					if (OF_getprop(sensors, "reg", &reg, 4) != 4)
    394 						goto nope;
    395 					if (OF_getprop(sensors, "location", loc, 63) <= 0)
    396 						goto nope;
    397 					snprintf(pname, 7, "s%02x", reg);
    398 					prop_dictionary_set_string(dev, pname, loc);
    399 				nope:
    400 					sensors = OF_peer(sensors);
    401 				}
    402 				prop_array_add(cfg, dev);
    403 				prop_object_release(dev);
    404 			skip:
    405 				devs = OF_peer(devs);
    406 			}
    407 			memset(&iba, 0, sizeof(iba));
    408 			iba.iba_tag = &sc->sc_i2c;
    409 			iic_tag_init(&sc->sc_i2c);
    410 			sc->sc_i2c.ic_cookie = sc;
    411 			sc->sc_i2c.ic_exec = pmu_i2c_exec;
    412 			config_found(sc->sc_dev, &iba, iicbus_print,
    413 			    CFARGS(.iattr = "i2cbus"));
    414 			goto next;
    415 		}
    416 		if (strncmp(name, "adb", 4) == 0) {
    417 			aprint_normal_dev(self, "initializing ADB\n");
    418 			sc->sc_adbops.cookie = sc;
    419 			sc->sc_adbops.send = pmu_adb_send;
    420 			sc->sc_adbops.poll = pmu_adb_poll;
    421 			sc->sc_adbops.autopoll = pmu_autopoll;
    422 			sc->sc_adbops.set_handler = pmu_adb_set_handler;
    423 #if NNADB > 0
    424 			config_found(self, &sc->sc_adbops, nadb_print,
    425 			    CFARGS(.iattr = "adb_bus"));
    426 #endif
    427 			goto next;
    428 		}
    429 		if (strncmp(name, "rtc", 4) == 0) {
    430 
    431 			aprint_normal_dev(self, "initializing RTC\n");
    432 			sc->sc_todr.todr_gettime = pmu_todr_get;
    433 			sc->sc_todr.todr_settime = pmu_todr_set;
    434 			sc->sc_todr.todr_dev = self;
    435 			todr_attach(&sc->sc_todr);
    436 			goto next;
    437 		}
    438 		if (strncmp(name, "battery", 8) == 0)
    439 			goto next;
    440 
    441 		aprint_normal_dev(self, "%s not configured\n", name);
    442 next:
    443 		node = OF_peer(node);
    444 	}
    445 
    446 	if (OF_finddevice("/bandit/ohare") != -1) {
    447 		aprint_normal_dev(self, "enabling ohare backlight control\n");
    448 		sc->sc_flags |= PMU_HAS_BACKLIGHT_CONTROL;
    449 		cmd[0] = 0;
    450 		cmd[1] = 0;
    451 		memset(resp, 0, 6);
    452 		if (pmu_send(sc, PMU_READ_BRIGHTNESS, 1, cmd, 16, resp) > 1) {
    453 			sc->sc_brightness_wanted = resp[1];
    454 			pmu_update_brightness(sc);
    455 		}
    456 	}
    457 
    458 	/* attach batteries */
    459 	if (of_compatible(root_node, has_legacy_battery)) {
    460 
    461 		pmu_attach_legacy_battery(sc);
    462 	} else if (of_compatible(root_node, has_two_smart_batteries)) {
    463 
    464 		pmu_attach_smart_battery(sc, 0);
    465 		pmu_attach_smart_battery(sc, 1);
    466 	} else {
    467 
    468 		/* check how many batteries we have */
    469 		pmnode = of_getnode_byname(ca->ca_node, "power-mgt");
    470 		if (pmnode == -1)
    471 			goto bat_done;
    472 		if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24)
    473 			goto bat_done;
    474 		nbat = regs[6] >> 16;
    475 		for (i = 0; i < nbat; i++)
    476 			pmu_attach_smart_battery(sc, i);
    477 	}
    478 bat_done:
    479 
    480 	if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread,
    481 	    "%s", "pmu") != 0) {
    482 		aprint_error_dev(self, "unable to create event kthread\n");
    483 	}
    484 
    485 	sc->sc_lidswitch.smpsw_name = "Lid switch";
    486 	sc->sc_lidswitch.smpsw_type = PSWITCH_TYPE_LID;
    487 	if (sysmon_pswitch_register(&sc->sc_lidswitch) != 0)
    488 		aprint_error_dev(self,
    489 		    "unable to register lid switch with sysmon\n");
    490 
    491 	sc->sc_powerbutton.smpsw_name = "Power button";
    492 	sc->sc_powerbutton.smpsw_type = PSWITCH_TYPE_POWER;
    493 	if (sysmon_pswitch_register(&sc->sc_powerbutton) != 0)
    494 		aprint_error_dev(self,
    495 		    "unable to register power button with sysmon\n");
    496 }
    497 
    498 static void
    499 pmu_register_callback(void *pmu_cookie, void (*cb)(void *), void *cookie)
    500 {
    501 	struct pmu_softc *sc = pmu_cookie;
    502 
    503 	sc->sc_callback = cb;
    504 	sc->sc_cb_cookie = cookie;
    505 }
    506 
    507 static void
    508 pmu_init(struct pmu_softc *sc)
    509 {
    510 	uint8_t pmu_imask, resp[16];
    511 
    512 	pmu_imask =
    513 	    PMU_INT_PCEJECT | PMU_INT_SNDBRT | PMU_INT_ADB/* | PMU_INT_TICK*/;
    514 	pmu_imask |= PMU_INT_BATTERY;
    515 	pmu_imask |= PMU_INT_ENVIRONMENT;
    516 	pmu_send(sc, PMU_SET_IMASK, 1, &pmu_imask, 16, resp);
    517 
    518 	pmu_write_reg(sc, vIER, 0x90);	/* enable VIA interrupts */
    519 }
    520 
    521 static inline void
    522 pmu_write_reg(struct pmu_softc *sc, int offset, uint8_t value)
    523 {
    524 
    525 	bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value);
    526 }
    527 
    528 static inline uint8_t
    529 pmu_read_reg(struct pmu_softc *sc, int offset)
    530 {
    531 
    532 	return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset);
    533 }
    534 
    535 static inline int
    536 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
    537 {
    538 
    539 	pmu_out(sc);
    540 	pmu_write_reg(sc, vSR, data);
    541 	pmu_ack_off(sc);
    542 	/* wait for intr to come up */
    543 	/* XXX should add a timeout and bail if it expires */
    544 	do {} while (pmu_intr_state(sc) == 0);
    545 	pmu_ack_on(sc);
    546 	do {} while (pmu_intr_state(sc));
    547 	pmu_ack_on(sc);
    548 	DPRINTF(" %02x>", data);
    549 	return 0;
    550 }
    551 
    552 static inline int
    553 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
    554 {
    555 	volatile uint8_t junk;
    556 	pmu_in(sc);
    557 	junk = pmu_read_reg(sc, vSR);
    558 	__USE(junk);
    559 	pmu_ack_off(sc);
    560 	/* wait for intr to come up */
    561 	do {} while (pmu_intr_state(sc) == 0);
    562 	pmu_ack_on(sc);
    563 	do {} while (pmu_intr_state(sc));
    564 	*data = pmu_read_reg(sc, vSR);
    565 	DPRINTF(" <%02x", *data);
    566 	return 0;
    567 }
    568 
    569 static int
    570 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
    571     uint8_t *out_msg)
    572 {
    573 	struct pmu_softc *sc = cookie;
    574 	int i, rcv_len = -1, s;
    575 	uint8_t out_len, intreg;
    576 
    577 	DPRINTF("pmu_send: ");
    578 
    579 	s = splhigh();
    580 	intreg = pmu_read_reg(sc, vIER);
    581 	intreg &= 0x10;
    582 	pmu_write_reg(sc, vIER, intreg);
    583 
    584 	/* wait idle */
    585 	do {} while (pmu_intr_state(sc));
    586 	sc->sc_error = 0;
    587 
    588 	/* send command */
    589 	pmu_send_byte(sc, cmd);
    590 
    591 	/* send length if necessary */
    592 	if (pm_send_cmd_type[cmd] < 0) {
    593 		pmu_send_byte(sc, length);
    594 	}
    595 
    596 	for (i = 0; i < length; i++) {
    597 		pmu_send_byte(sc, in_msg[i]);
    598 		DPRINTF(" next ");
    599 	}
    600 	DPRINTF("done sending\n");
    601 
    602 	/* see if there's data to read */
    603 	rcv_len = pm_receive_cmd_type[cmd];
    604 	if (rcv_len == 0)
    605 		goto done;
    606 
    607 	/* read command */
    608 	if (rcv_len == 1) {
    609 		pmu_read_byte(sc, out_msg);
    610 		goto done;
    611 	} else
    612 		out_msg[0] = cmd;
    613 	if (rcv_len < 0) {
    614 		pmu_read_byte(sc, &out_len);
    615 		rcv_len = out_len + 1;
    616 	}
    617 	for (i = 1; i < uimin(rcv_len, rlen); i++)
    618 		pmu_read_byte(sc, &out_msg[i]);
    619 
    620 done:
    621 	DPRINTF("\n");
    622 	pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
    623 	splx(s);
    624 
    625 	return rcv_len;
    626 }
    627 
    628 static void
    629 pmu_adb_poll(void *cookie)
    630 {
    631 	struct pmu_softc *sc = cookie;
    632 	int s;
    633 
    634 	s = spltty();
    635 	pmu_intr(sc);
    636 	splx(s);
    637 }
    638 
    639 static void
    640 pmu_in(struct pmu_softc *sc)
    641 {
    642 	volatile uint8_t reg;
    643 
    644 	reg = pmu_read_reg(sc, vACR);
    645 	reg &= ~vSR_OUT;
    646 	reg |= 0x0c;
    647 	pmu_write_reg(sc, vACR, reg);
    648 }
    649 
    650 static void
    651 pmu_out(struct pmu_softc *sc)
    652 {
    653 	volatile uint8_t reg;
    654 
    655 	reg = pmu_read_reg(sc, vACR);
    656 	reg |= vSR_OUT;
    657 	reg |= 0x0c;
    658 	pmu_write_reg(sc, vACR, reg);
    659 }
    660 
    661 static void
    662 pmu_ack_off(struct pmu_softc *sc)
    663 {
    664 	volatile uint8_t reg;
    665 
    666 	reg = pmu_read_reg(sc, vBufB);
    667 	reg &= ~vPB4;
    668 	pmu_write_reg(sc, vBufB, reg);
    669 }
    670 
    671 static void
    672 pmu_ack_on(struct pmu_softc *sc)
    673 {
    674 	volatile uint8_t reg;
    675 
    676 	reg = pmu_read_reg(sc, vBufB);
    677 	reg |= vPB4;
    678 	pmu_write_reg(sc, vBufB, reg);
    679 }
    680 
    681 static int
    682 pmu_intr_state(struct pmu_softc *sc)
    683 {
    684 	return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
    685 }
    686 
    687 static int
    688 pmu_intr(void *arg)
    689 {
    690 	struct pmu_softc *sc = arg;
    691 	unsigned int len, i;
    692 	uint8_t resp[16];
    693 
    694 	DPRINTF(":");
    695 
    696 	pmu_write_reg(sc, vIFR, 0x90);	/* Clear 'em */
    697 	len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
    698 	if ((len < 1) || (resp[1] == 0))
    699 		goto done;
    700 #ifdef PMU_DEBUG
    701 	{
    702 		DPRINTF("intr: %02x", resp[0]);
    703 		for (i = 1; i < len; i++)
    704 			DPRINTF(" %02x", resp[i]);
    705 		DPRINTF("\n");
    706 	}
    707 #endif
    708 	if (resp[1] & PMU_INT_ADB) {
    709 		pmu_adb_handler(sc, len - 1, &resp[1]);
    710 		goto done;
    711 	}
    712 	if (resp[1] & PMU_INT_SNDBRT) {
    713 		/* deal with the brightness / volume control buttons */
    714 		DPRINTF("brightness: %d volume %d\n", resp[2], resp[3]);
    715 		sc->sc_brightness_wanted = resp[2];
    716 		sc->sc_volume_wanted = resp[3];
    717 		cv_signal(&sc->sc_event);
    718 		goto done;
    719 	}
    720 	if (resp[1] & PMU_INT_PCEJECT) {
    721 		/* deal with PCMCIA eject buttons */
    722 		DPRINTF("card eject %d\n", resp[3]);
    723 		atomic_or_32(&sc->sc_pending, (resp[3] & 3));
    724 		cv_signal(&sc->sc_event);
    725 		goto done;
    726 	}
    727 	if (resp[1] & PMU_INT_BATTERY) {
    728 		/* deal with battery messages */
    729 		printf("battery:");
    730 		for (i = 2; i < len; i++)
    731 			printf(" %02x", resp[i]);
    732 		printf("\n");
    733 		goto done;
    734 	}
    735 	if (resp[1] & PMU_INT_ENVIRONMENT) {
    736 		uint8_t diff;
    737 #ifdef PMU_DEBUG
    738 		/* deal with environment messages */
    739 		printf("environment:");
    740 		for (i = 2; i < len; i++)
    741 			printf(" %02x", resp[i]);
    742 		printf("\n");
    743 #endif
    744 		diff = (resp[2] ^ sc->sc_env_old ) & sc->sc_env_mask;
    745 		if (diff == 0) goto done;
    746 		sc->sc_env_old = resp[2];
    747 		if (diff & PMU_ENV_LID_CLOSED) {
    748 			sc->sc_lid_closed = (resp[2] & PMU_ENV_LID_CLOSED) != 0;
    749 			atomic_or_32(&sc->sc_pending, PMU_EV_LID);
    750 			cv_signal(&sc->sc_event);
    751 		}
    752 		if (diff & PMU_ENV_POWER_BUTTON) {
    753 			sc->sc_button = (resp[2] & PMU_ENV_POWER_BUTTON) != 0;
    754 			atomic_or_32(&sc->sc_pending, PMU_EV_BUTTON);
    755 			cv_signal(&sc->sc_event);
    756 		}
    757 		goto done;
    758 	}
    759 	if (resp[1] & PMU_INT_TICK) {
    760 		/* don't bother */
    761 		goto done;
    762 	}
    763 
    764 	/* unknown interrupt code?! */
    765 #ifdef PMU_DEBUG
    766 	printf("pmu intr: %02x:", resp[1]);
    767 	for (i = 2; i < len; i++)
    768 		printf(" %02x", resp[i]);
    769 	printf("\n");
    770 #endif
    771 done:
    772 	return 1;
    773 }
    774 
    775 #define DIFF19041970 2082844800
    776 
    777 static int
    778 pmu_todr_get(todr_chip_handle_t tch, struct timeval *tvp)
    779 {
    780 	struct pmu_softc *sc = device_private(tch->todr_dev);
    781 	uint32_t sec;
    782 	int count = 10;
    783 	int ok = FALSE;
    784 	uint8_t resp[16];
    785 
    786 	DPRINTF("pmu_todr_get\n");
    787 	while ((count > 0) && (!ok)) {
    788 		pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
    789 
    790 		memcpy(&sec, &resp[1], 4);
    791 		tvp->tv_sec = sec - DIFF19041970;
    792 		ok = (sec > DIFF19041970) && (sec < 0xf0000000);
    793 		if (!ok) aprint_error_dev(sc->sc_dev,
    794 		    "got garbage from rtc (%08x)\n", sec);
    795 		count--;
    796 	}
    797 	if (count == 0) {
    798 		aprint_error_dev(sc->sc_dev,
    799 		    "unable to get a sane time value\n");
    800 		tvp->tv_sec = 0;
    801 	}
    802 	DPRINTF("tod: %" PRIo64 "\n", tvp->tv_sec);
    803 	tvp->tv_usec = 0;
    804 	return 0;
    805 }
    806 
    807 static int
    808 pmu_todr_set(todr_chip_handle_t tch, struct timeval *tvp)
    809 {
    810 	struct pmu_softc *sc = device_private(tch->todr_dev);
    811 	uint32_t sec;
    812 	uint8_t resp[16];
    813 
    814 	sec = tvp->tv_sec + DIFF19041970;
    815 	if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0)
    816 		return 0;
    817 	return -1;
    818 }
    819 
    820 void
    821 pmu_poweroff(void)
    822 {
    823 	struct pmu_softc *sc;
    824 	uint8_t cmd[] = {'M', 'A', 'T', 'T'};
    825 	uint8_t resp[16];
    826 
    827 	if (pmu0 == NULL)
    828 		return;
    829 	sc = pmu0;
    830 	if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0)
    831 		while (1);
    832 }
    833 
    834 void
    835 pmu_restart(void)
    836 {
    837 	struct pmu_softc *sc;
    838 	uint8_t resp[16];
    839 
    840 	if (pmu0 == NULL)
    841 		return;
    842 	sc = pmu0;
    843 	if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0)
    844 		while (1);
    845 }
    846 
    847 void
    848 pmu_modem(int on)
    849 {
    850 	struct pmu_softc *sc;
    851 	uint8_t resp[16], cmd[2] = {0, 0};
    852 
    853 	if (pmu0 == NULL)
    854 		return;
    855 
    856 	sc = pmu0;
    857 	cmd[0] = PMU_POW0_MODEM | (on ? PMU_POW0_ON : 0);
    858 	pmu_send(sc, PMU_POWER_CTRL0, 1, cmd, 16, resp);
    859 }
    860 
    861 static void
    862 pmu_autopoll(void *cookie, int flag)
    863 {
    864 	struct pmu_softc *sc = cookie;
    865 	/* magical incantation to re-enable autopolling */
    866 	uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff};
    867 	uint8_t resp[16];
    868 
    869 	if (sc->sc_autopoll == flag)
    870 		return;
    871 
    872 	if (flag) {
    873 		pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
    874 	} else {
    875 		pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
    876 	}
    877 	sc->sc_autopoll = flag & 0xffff;
    878 }
    879 
    880 static int
    881 pmu_adb_handler(void *cookie, int len, uint8_t *data)
    882 {
    883 	struct pmu_softc *sc = cookie;
    884 	uint8_t resp[16];
    885 
    886 	if (sc->sc_adb_handler != NULL) {
    887 		sc->sc_adb_handler(sc->sc_adb_cookie, len, data);
    888 		/*
    889 		 * the PMU will turn off autopolling after each LISTEN so we
    890 		 * need to re-enable it here whenever we receive an ACK for a
    891 		 * LISTEN command
    892 		 */
    893 		if ((data[1] & 0x0c) == 0x08) {
    894 			uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff,
    895 			    sc->sc_autopoll & 0xff};
    896 			pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
    897 		}
    898 		return 0;
    899 	}
    900 	return -1;
    901 }
    902 
    903 static int
    904 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
    905 {
    906 	struct pmu_softc *sc = cookie;
    907 	int i;
    908 	uint8_t packet[16], resp[16];
    909 
    910 	/* construct an ADB command packet and send it */
    911 	packet[0] = command;
    912 	packet[1] = 0;
    913 	packet[2] = len;
    914 	for (i = 0; i < len; i++)
    915 		packet[i + 3] = data[i];
    916 	(void)pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
    917 
    918 	return 0;
    919 }
    920 
    921 static int
    922 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
    923     void *hcookie)
    924 {
    925 	struct pmu_softc *sc = cookie;
    926 
    927 	/* register a callback for incoming ADB messages */
    928 	sc->sc_adb_handler = handler;
    929 	sc->sc_adb_cookie = hcookie;
    930 	return 0;
    931 }
    932 
    933 static int
    934 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
    935     size_t send_len, void *_recv, size_t recv_len, int flags)
    936 {
    937 	struct pmu_softc *sc = cookie;
    938 	const uint8_t *send = _send;
    939 	const uint8_t *recv = _recv;
    940 	uint8_t command[64] = {1,	/* bus number */
    941 				PMU_I2C_MODE_SIMPLE,
    942 				0,	/* bus2 */
    943 				addr,
    944 				0,	/* sub address */
    945 				0,	/* comb address */
    946 				0,	/* count */
    947 				0	/* data */
    948 				};
    949 	uint8_t resp[16];
    950 	int len, rw;
    951 
    952 	rw = (addr & 0x7f) << 1;
    953 	command[3] = rw;
    954 	/* see if we're supposed to read */
    955 	if (I2C_OP_READ_P(op)) {
    956 		if (send_len == 1) {
    957 			command[1] = PMU_I2C_MODE_COMBINED;
    958 			command[4] = *send;
    959 			command[5] = rw | 1;
    960 			command[6] = recv_len;
    961 			if (sc->sc_is_rackmac) delay(20000);
    962 			len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp);
    963 			DPRINTF("resp1(%d): %2x %2x ", len, resp[0], resp[1]);
    964 			command[0] = 0;
    965 			int ok = 0;
    966 			int bail = 10;
    967 			while ((!ok) && (bail > 0)) {
    968 				len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp);
    969 				if (resp[1] != 0xfe) {
    970 					ok = 1;
    971 				} else delay(10000);
    972 				bail--;
    973 			}
    974 #ifdef PMU_DEBUG
    975 			if (bail < 9) printf("bail %d\n", bail);
    976 #endif
    977 		} else {
    978 			command[6] = send_len;
    979 			memcpy(&command[7], send, send_len);
    980 			/*
    981 			 * Xserve G4's PMU needs these, if we send i2c commands back to
    982 			 * back we get busy responses.
    983 			 * No such problem on Minis, which have temperature sensors
    984 			 * here.
    985 			 */
    986 			if (sc->sc_is_rackmac) delay(20000);
    987 			len = pmu_send(sc, PMU_I2C_CMD, send_len + 7, command, 16, resp);
    988 			DPRINTF("resp1(%d): %2x %2x ", len, resp[0], resp[1]);
    989 			rw |= 1;
    990 			command[3] = rw;
    991 			command[6] = recv_len;
    992 			if (sc->sc_is_rackmac) delay(20000);
    993 			len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp);
    994 			DPRINTF("resp2(%d): %2x %2x ", len, resp[0], resp[1]);
    995 			command[0] = 0;
    996 			int ok = 0;
    997 			int bail = 10;
    998 			while ((!ok) && (bail > 0)) {
    999 				len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp);
   1000 				if (resp[1] != 0xfe) {
   1001 					ok = 1;
   1002 				} else delay(10000);
   1003 				bail--;
   1004 			}
   1005 #ifdef PMU_DEBUG
   1006 			if (bail < 9) printf("bail %d\n", bail);
   1007 #endif
   1008 		}
   1009 		DPRINTF("resp3(%d): %2x %2x %2x ", len, resp[0], resp[1],
   1010 			resp[2]);
   1011 		if ((len - 2) != recv_len) {
   1012 			DPRINTF("%s: %s(%d) - got %d\n",
   1013 			    device_xname(sc->sc_dev),
   1014 			    __func__, recv_len, len - 2);
   1015 			return -1;
   1016 		}
   1017 		memcpy(_recv, &resp[2], len - 2);
   1018 		return 0;
   1019 	} else {
   1020 		int clen = send_len + recv_len;
   1021 		uint8_t *dptr = &command[7];
   1022 		if (clen > 57) return -1;
   1023 		command[6] = clen;
   1024 		memcpy(dptr, send, send_len);
   1025 		if (recv_len > 0) {
   1026 			dptr += send_len;
   1027 			memcpy(dptr, recv, recv_len);
   1028 		}
   1029 		len = clen + 7;
   1030 		DPRINTF("pmu_i2c_exec(%02x, %d)\n", addr, send_len);
   1031 
   1032 		if (sc->sc_is_rackmac) delay(20000);
   1033 		len = pmu_send(sc, PMU_I2C_CMD, len, command, 16, resp);
   1034 		DPRINTF("resp(%d): %2x %2x\n", len, resp[0], resp[1]);
   1035 
   1036 		if (resp[1] != PMU_I2C_STATUS_OK) {
   1037 			DPRINTF("%s: iic error %d\n", __func__, resp[1]);
   1038 			return -1;
   1039 		}
   1040 	}
   1041 	return 0;
   1042 }
   1043 
   1044 static void
   1045 pmu_eject_card(struct pmu_softc *sc, int socket)
   1046 {
   1047 	uint8_t buf[] = {socket | 4};
   1048 	uint8_t res[4];
   1049 
   1050 	atomic_and_32(&sc->sc_pending, ~socket);
   1051 	pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res);
   1052 }
   1053 
   1054 static void
   1055 pmu_update_brightness(struct pmu_softc *sc)
   1056 {
   1057 	int val;
   1058 	uint8_t cmd[2], resp[16];
   1059 
   1060 	if (sc->sc_brightness == sc->sc_brightness_wanted)
   1061 		return;
   1062 
   1063 	if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) {
   1064 
   1065 		aprint_normal_dev(sc->sc_dev,
   1066 		     "this PMU doesn't support backlight control\n");
   1067 		sc->sc_brightness = sc->sc_brightness_wanted;
   1068 		return;
   1069 	}
   1070 
   1071 	if (sc->sc_brightness_wanted == 0) {
   1072 
   1073 		/* turn backlight off completely */
   1074 		cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT;
   1075 		pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
   1076 		sc->sc_brightness = sc->sc_brightness_wanted;
   1077 
   1078 		/* don't bother with brightness */
   1079 		return;
   1080 	}
   1081 
   1082 	/* turn backlight on if needed */
   1083 	if (sc->sc_brightness == 0) {
   1084 		cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT;
   1085 		pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
   1086 	}
   1087 
   1088 	DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness,
   1089 	    sc->sc_brightness_wanted);
   1090 
   1091 	val = 0x7f - (sc->sc_brightness_wanted >> 1);
   1092 	if (val < 0x08)
   1093 		val = 0x08;
   1094 	if (val > 0x78)
   1095 		val = 0x78;
   1096 	cmd[0] = val;
   1097 	pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp);
   1098 
   1099 	sc->sc_brightness = sc->sc_brightness_wanted;
   1100 }
   1101 
   1102 static void
   1103 pmu_thread(void *cookie)
   1104 {
   1105 	struct pmu_softc *sc = cookie;
   1106 	//time_t time_bat = time_second;
   1107 	int ticks = hz, i;
   1108 
   1109 	while (1) {
   1110 		mutex_enter(&sc->sc_evmtx);
   1111 		cv_timedwait(&sc->sc_event, &sc->sc_evmtx, ticks);
   1112 		if ((sc->sc_pending & 3) != 0) {
   1113 			DPRINTF("eject %d\n", sc->sc_pending & 3);
   1114 			for (i = 1; i < 3; i++) {
   1115 				if (i & sc->sc_pending)
   1116 					pmu_eject_card(sc, i);
   1117 			}
   1118 		}
   1119 		mutex_exit(&sc->sc_evmtx);
   1120 		/* see if we need to update brightness */
   1121 		if (sc->sc_brightness_wanted != sc->sc_brightness) {
   1122 			pmu_update_brightness(sc);
   1123 		}
   1124 
   1125 		/* see if we need to update audio volume */
   1126 		if (sc->sc_volume_wanted != sc->sc_volume) {
   1127 #if 0
   1128 			set_volume(sc->sc_volume_wanted);
   1129 #endif
   1130 			sc->sc_volume = sc->sc_volume_wanted;
   1131 		}
   1132 
   1133 		if (sc->sc_pending & PMU_EV_LID) {
   1134 			atomic_and_32(&sc->sc_pending, ~PMU_EV_LID);
   1135 			sysmon_pswitch_event(&sc->sc_lidswitch,
   1136 	    		    sc->sc_lid_closed ? PSWITCH_EVENT_PRESSED :
   1137 			    PSWITCH_EVENT_RELEASED);
   1138 		}
   1139 
   1140 		if (sc->sc_pending & PMU_EV_BUTTON) {
   1141 			atomic_and_32(&sc->sc_pending, ~PMU_EV_BUTTON);
   1142 			sysmon_pswitch_event(&sc->sc_powerbutton,
   1143 	    		    sc->sc_button ? PSWITCH_EVENT_PRESSED :
   1144 			    PSWITCH_EVENT_RELEASED);
   1145 		}
   1146 
   1147 		if (sc->sc_callback != NULL)
   1148 			sc->sc_callback(sc->sc_cb_cookie);
   1149 	}
   1150 }
   1151 
   1152 static int
   1153 pmu_print(void *aux, const char *what)
   1154 {
   1155 
   1156 	return 0;
   1157 }
   1158 
   1159 static void
   1160 pmu_attach_legacy_battery(struct pmu_softc *sc)
   1161 {
   1162 	struct battery_attach_args baa;
   1163 
   1164 	baa.baa_type = BATTERY_TYPE_LEGACY;
   1165 	baa.baa_pmu_ops = &sc->sc_pmu_ops;
   1166 	config_found(sc->sc_dev, &baa, pmu_print,
   1167 	    CFARGS(.iattr = "pmu_bus"));
   1168 }
   1169 
   1170 static void
   1171 pmu_attach_smart_battery(struct pmu_softc *sc, int num)
   1172 {
   1173 	struct battery_attach_args baa;
   1174 
   1175 	baa.baa_type = BATTERY_TYPE_SMART;
   1176 	baa.baa_pmu_ops = &sc->sc_pmu_ops;
   1177 	baa.baa_num = num;
   1178 	config_found(sc->sc_dev, &baa, pmu_print,
   1179 	    CFARGS(.iattr = "pmu_bus"));
   1180 }
   1181