Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.70
      1 /* $NetBSD: acpi_tz.c,v 1.70 2010/04/24 19:16:10 jruoho Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2003 Jared D. McNeill <jmcneill (at) invisible.ca>
      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. The name of the author may not be used to endorse or promote products
     13  *    derived from this software without specific prior written permission.
     14  *
     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
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * ACPI Thermal Zone driver
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.70 2010/04/24 19:16:10 jruoho Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/device.h>
     37 #include <sys/callout.h>
     38 #include <sys/kernel.h>
     39 #include <sys/systm.h>
     40 
     41 #include <dev/acpi/acpireg.h>
     42 #include <dev/acpi/acpivar.h>
     43 #include <dev/acpi/acpi_power.h>
     44 
     45 #define _COMPONENT		ACPI_TZ_COMPONENT
     46 ACPI_MODULE_NAME		("acpi_tz")
     47 
     48 #define ATZ_F_CRITICAL		0x01	/* zone critical */
     49 #define ATZ_F_HOT		0x02	/* zone hot */
     50 #define ATZ_F_PASSIVE		0x04	/* zone passive cooling */
     51 #define ATZ_F_PASSIVEONLY	0x08	/* zone is passive cooling only */
     52 
     53 #define ATZ_ACTIVE_NONE		  -1
     54 
     55 /*
     56  * The constants are as follows:
     57  *
     58  *   ATZ_TZP_RATE	default polling interval (30 seconds) if no _TZP
     59  *   ATZ_NLEVELS	number of cooling levels for _ACx and _ALx
     60  *   ATZ_ZEROC		0 C, measured in 0.1 Kelvin
     61  *   ATZ_TMP_INVALID	temporarily invalid temperature
     62  *   ATZ_ZONE_EXPIRE	zone info refetch interval (15 minutes)
     63  */
     64 #define ATZ_TZP_RATE		300
     65 #define ATZ_NLEVELS		10
     66 #define ATZ_ZEROC		2732
     67 #define ATZ_TMP_INVALID		0xffffffff
     68 #define ATZ_ZONE_EXPIRE		9000
     69 
     70 /*
     71  * All temperatures are reported in 0.1 Kelvin.
     72  * The ACPI specification assumes that K = C + 273.2
     73  * rather than the nominal 273.15 used by envsys(4).
     74  */
     75 #define	ATZ2UKELVIN(t) ((t) * 100000 - 50000)
     76 
     77 struct acpitz_zone {
     78 	ACPI_BUFFER		 al[ATZ_NLEVELS];
     79 	uint32_t		 ac[ATZ_NLEVELS];
     80 	uint32_t		 crt;
     81 	uint32_t		 hot;
     82 	uint32_t		 rtv;
     83 	uint32_t		 psv;
     84 	uint32_t		 tc1;
     85 	uint32_t		 tc2;
     86 	uint32_t		 tmp;
     87 	uint32_t		 prevtmp;
     88 	uint32_t		 tzp;
     89 	uint32_t		 fanmin;
     90 	uint32_t		 fanmax;
     91 	uint32_t		 fancurrent;
     92 };
     93 
     94 struct acpitz_softc {
     95 	struct acpi_devnode	*sc_node;
     96 	struct sysmon_envsys	*sc_sme;
     97 	struct acpitz_zone	 sc_zone;
     98 	struct callout		 sc_callout;
     99 	envsys_data_t		 sc_temp_sensor;
    100 	envsys_data_t		 sc_fan_sensor;
    101 	int			 sc_active;
    102 	int			 sc_flags;
    103 	int			 sc_zone_expire;
    104 	bool			 sc_first;
    105 	bool			 sc_have_fan;
    106 };
    107 
    108 static int		acpitz_match(device_t, cfdata_t, void *);
    109 static void		acpitz_attach(device_t, device_t, void *);
    110 static int		acpitz_detach(device_t, int);
    111 static void		acpitz_get_status(void *);
    112 static void		acpitz_get_zone(void *, int);
    113 static void		acpitz_get_zone_quiet(void *);
    114 static char	       *acpitz_celcius_string(int);
    115 static void		acpitz_power_off(struct acpitz_softc *);
    116 static void		acpitz_power_zone(struct acpitz_softc *, int, int);
    117 static void		acpitz_sane_temp(uint32_t *tmp);
    118 static ACPI_STATUS	acpitz_switch_cooler(ACPI_OBJECT *, void *);
    119 static void		acpitz_notify_handler(ACPI_HANDLE, uint32_t, void *);
    120 static int		acpitz_get_integer(device_t, const char *, uint32_t *);
    121 static void		acpitz_tick(void *);
    122 static void		acpitz_init_envsys(device_t);
    123 static void		acpitz_get_limits(struct sysmon_envsys *,
    124 					  envsys_data_t *,
    125 					  sysmon_envsys_lim_t *, uint32_t *);
    126 static int		acpitz_get_fanspeed(device_t, uint32_t *,
    127 					    uint32_t *, uint32_t *);
    128 #ifdef notyet
    129 static ACPI_STATUS	acpitz_set_fanspeed(device_t, uint32_t);
    130 #endif
    131 
    132 CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc),
    133     acpitz_match, acpitz_attach, acpitz_detach, NULL);
    134 
    135 /*
    136  * acpitz_match: autoconf(9) match routine
    137  */
    138 static int
    139 acpitz_match(device_t parent, cfdata_t match, void *aux)
    140 {
    141 	struct acpi_attach_args *aa = aux;
    142 
    143 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
    144 		return 0;
    145 
    146 	return 1;
    147 }
    148 
    149 /*
    150  * acpitz_attach: autoconf(9) attach routine
    151  */
    152 static void
    153 acpitz_attach(device_t parent, device_t self, void *aux)
    154 {
    155 	struct acpitz_softc *sc = device_private(self);
    156 	struct acpi_attach_args *aa = aux;
    157 	ACPI_INTEGER val;
    158 	ACPI_STATUS rv;
    159 
    160 	aprint_naive("\n");
    161 	aprint_normal(": ACPI Thermal Zone\n");
    162 
    163 	sc->sc_first = true;
    164 	sc->sc_have_fan = false;
    165 	sc->sc_node = aa->aa_node;
    166 	sc->sc_zone.tzp = ATZ_TZP_RATE;
    167 
    168 	/*
    169 	 * The _TZP (ACPI 4.0, p. 430) defines the recommended
    170 	 * polling interval (in tenths of seconds). A value zero
    171 	 * means that polling "should not be necessary".
    172 	 */
    173 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TZP", &val);
    174 
    175 	if (ACPI_SUCCESS(rv) && val != 0)
    176 		sc->sc_zone.tzp = val;
    177 
    178 	aprint_debug_dev(self, "sample rate %d.%ds\n",
    179 	    sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10);
    180 
    181 	sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    182 
    183 	/*
    184 	 * XXX: The fan controls seen here are available on
    185 	 *	some HP laptops. Arguably these should not
    186 	 *	appear in a generic device driver like this.
    187 	 */
    188 	if (acpitz_get_fanspeed(self, &sc->sc_zone.fanmin,
    189 		&sc->sc_zone.fanmax, &sc->sc_zone.fancurrent) == 0)
    190 		sc->sc_have_fan = true;
    191 
    192 	acpitz_get_zone(self, 1);
    193 	acpitz_get_status(self);
    194 
    195 	(void)pmf_device_register(self, NULL, NULL);
    196 	(void)acpi_register_notify(sc->sc_node, acpitz_notify_handler);
    197 
    198 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
    199 	callout_setfunc(&sc->sc_callout, acpitz_tick, self);
    200 
    201 	acpitz_init_envsys(self);
    202 
    203 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    204 }
    205 
    206 static int
    207 acpitz_detach(device_t self, int flags)
    208 {
    209 	struct acpitz_softc *sc = device_private(self);
    210 	ACPI_HANDLE hdl;
    211 	ACPI_BUFFER al;
    212 	ACPI_STATUS rv;
    213 	int i;
    214 
    215 	callout_halt(&sc->sc_callout, NULL);
    216 	callout_destroy(&sc->sc_callout);
    217 
    218 	pmf_device_deregister(self);
    219 	acpi_deregister_notify(sc->sc_node);
    220 
    221 	/*
    222 	 * Although the device itself should not contain any power
    223 	 * resources, we have possibly used the resources of active
    224 	 * cooling devices. To unregister these, first fetch a fresh
    225 	 * active cooling zone, and then detach the resources from
    226 	 * the reference handles contained in the cooling zone.
    227 	 */
    228 	acpitz_get_zone(self, 0);
    229 
    230 	for (i = 0; i < ATZ_NLEVELS; i++) {
    231 
    232 		if (sc->sc_zone.al[i].Pointer == NULL)
    233 			continue;
    234 
    235 		al = sc->sc_zone.al[i];
    236 		rv = acpi_eval_reference_handle(al.Pointer, &hdl);
    237 
    238 		if (ACPI_SUCCESS(rv))
    239 			acpi_power_deregister_from_handle(hdl);
    240 
    241 		ACPI_FREE(sc->sc_zone.al[i].Pointer);
    242 	}
    243 
    244 	if (sc->sc_sme != NULL)
    245 		sysmon_envsys_unregister(sc->sc_sme);
    246 
    247 	return 0;
    248 }
    249 
    250 static void
    251 acpitz_get_zone_quiet(void *opaque)
    252 {
    253 	acpitz_get_zone(opaque, 0);
    254 }
    255 
    256 static void
    257 acpitz_get_status(void *opaque)
    258 {
    259 	device_t dv = opaque;
    260 	struct acpitz_softc *sc = device_private(dv);
    261 	uint32_t tmp, active, fmin, fmax, fcurrent;
    262 	int changed, flags, i;
    263 
    264 	sc->sc_zone_expire--;
    265 
    266 	if (sc->sc_zone_expire <= 0) {
    267 		sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    268 
    269 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
    270 			"%s: zone refetch forced\n", device_xname(dv)));
    271 
    272 		acpitz_get_zone(dv, 0);
    273 	}
    274 
    275 	if (acpitz_get_integer(dv, "_TMP", &tmp) != 0)
    276 		return;
    277 
    278 	sc->sc_zone.prevtmp = sc->sc_zone.tmp;
    279 	sc->sc_zone.tmp = tmp;
    280 
    281 	if (sc->sc_first != false)
    282 		sc->sc_zone.prevtmp = tmp; /* XXX: Sanity check? */
    283 
    284 	if (acpitz_get_fanspeed(dv, &fmin, &fmax, &fcurrent) == 0) {
    285 
    286 		if (fcurrent != ATZ_TMP_INVALID)
    287 			sc->sc_zone.fancurrent = fcurrent;
    288 	}
    289 
    290 	sc->sc_temp_sensor.state = ENVSYS_SVALID;
    291 	sc->sc_temp_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
    292 
    293 	sc->sc_fan_sensor.state = ENVSYS_SVALID;
    294 	sc->sc_fan_sensor.value_cur = sc->sc_zone.fancurrent;
    295 
    296 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: zone temperature is %s C\n",
    297 		device_xname(dv), acpitz_celcius_string(sc->sc_zone.tmp)));
    298 
    299 	/*
    300 	 * XXX: Passive cooling is not yet supported.
    301 	 */
    302 	if ((sc->sc_flags & ATZ_F_PASSIVEONLY) != 0)
    303 		return;
    304 
    305 	/*
    306 	 * As noted in ACPI 4.0 (p. 420), the temperature
    307 	 * thresholds are conveyed in the optional _ACx
    308 	 * object (x = 0 ... 9). The smaller the x, the
    309 	 * greater the cooling level. We prefer to keep
    310 	 * the highest cooling mode when in "active".
    311 	 */
    312 	active = ATZ_ACTIVE_NONE;
    313 
    314 	for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
    315 
    316 		if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
    317 			continue;
    318 
    319 		if (sc->sc_zone.ac[i] <= tmp)
    320 			active = i;
    321 	}
    322 
    323 	flags = sc->sc_flags & ~(ATZ_F_CRITICAL | ATZ_F_HOT | ATZ_F_PASSIVE);
    324 
    325 	if (sc->sc_zone.psv != ATZ_TMP_INVALID && tmp >= sc->sc_zone.psv)
    326 		flags |= ATZ_F_PASSIVE;
    327 
    328 	if (sc->sc_zone.hot != ATZ_TMP_INVALID && tmp >= sc->sc_zone.hot)
    329 		flags |= ATZ_F_HOT;
    330 
    331 	if (sc->sc_zone.crt != ATZ_TMP_INVALID && tmp >= sc->sc_zone.crt)
    332 		flags |= ATZ_F_CRITICAL;
    333 
    334 	if (flags != sc->sc_flags) {
    335 
    336 		changed = (sc->sc_flags ^ flags) & flags;
    337 		sc->sc_flags = flags;
    338 
    339 		if ((changed & ATZ_F_CRITICAL) != 0) {
    340 			sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    341 
    342 			aprint_debug_dev(dv, "zone went critical, %s C\n",
    343 			    acpitz_celcius_string(tmp));
    344 
    345 		} else if ((changed & ATZ_F_HOT) != 0) {
    346 			sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    347 
    348 			aprint_debug_dev(dv, "zone went hot, %s C\n",
    349 			    acpitz_celcius_string(tmp));
    350 		}
    351 	}
    352 
    353 	/* Power on the fans. */
    354 	if (sc->sc_active != active) {
    355 
    356 		if (sc->sc_active != ATZ_ACTIVE_NONE)
    357 			acpitz_power_zone(sc, sc->sc_active, 0);
    358 
    359 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: active cooling "
    360 			"level %u\n", device_xname(dv), active));
    361 
    362 		if (active != ATZ_ACTIVE_NONE)
    363 			acpitz_power_zone(sc, active, 1);
    364 
    365 		sc->sc_active = active;
    366 	}
    367 }
    368 
    369 static char *
    370 acpitz_celcius_string(int dk)
    371 {
    372 	static char buf[10];
    373 	int dc;
    374 
    375 	dc = abs(dk - ATZ_ZEROC);
    376 
    377 	(void)snprintf(buf, sizeof(buf), "%s%d.%d",
    378 	    (dk >= ATZ_ZEROC) ? "" : "-", dc / 10, dc % 10);
    379 
    380 	return buf;
    381 }
    382 
    383 static ACPI_STATUS
    384 acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
    385 {
    386 	int flag, pwr_state;
    387 	ACPI_HANDLE cooler;
    388 	ACPI_STATUS rv;
    389 
    390 	/*
    391 	 * The _ALx object is a package in which the elements
    392 	 * are reference handles to an active cooling device
    393 	 * (typically PNP0C0B, ACPI fan device). Try to turn
    394 	 * on (or off) the power resources behind these handles
    395 	 * to start (or terminate) the active cooling.
    396 	 */
    397 	flag = *(int *)arg;
    398 	pwr_state = (flag != 0) ? ACPI_STATE_D0 : ACPI_STATE_D3;
    399 
    400 	rv = acpi_eval_reference_handle(obj, &cooler);
    401 
    402 	if (ACPI_FAILURE(rv))
    403 		return rv;
    404 
    405 	(void)acpi_power_set_from_handle(cooler, pwr_state);
    406 
    407 	return AE_OK;
    408 }
    409 
    410 /*
    411  * acpitz_power_zone:
    412  *
    413  *	Power on or off the i:th part of the zone zone.
    414  */
    415 static void
    416 acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
    417 {
    418 
    419 	KASSERT(i >= 0 && i < ATZ_NLEVELS);
    420 
    421 	(void)acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
    422 	    acpitz_switch_cooler, &on);
    423 }
    424 
    425 
    426 /*
    427  * acpitz_power_off:
    428  *
    429  *	Power off parts of the zone.
    430  */
    431 static void
    432 acpitz_power_off(struct acpitz_softc *sc)
    433 {
    434 	int i;
    435 
    436 	for (i = 0 ; i < ATZ_NLEVELS; i++) {
    437 
    438 		if (sc->sc_zone.al[i].Pointer == NULL)
    439 			continue;
    440 
    441 		acpitz_power_zone(sc, i, 0);
    442 	}
    443 
    444 	sc->sc_active = ATZ_ACTIVE_NONE;
    445 	sc->sc_flags &= ~(ATZ_F_CRITICAL | ATZ_F_HOT | ATZ_F_PASSIVE);
    446 }
    447 
    448 static void
    449 acpitz_get_zone(void *opaque, int verbose)
    450 {
    451 	device_t dv = opaque;
    452 	struct acpitz_softc *sc = device_private(dv);
    453 	int comma, i, valid_levels;
    454 	ACPI_OBJECT *obj;
    455 	ACPI_STATUS rv;
    456 	char buf[5];
    457 
    458 	if (sc->sc_first != true) {
    459 		acpitz_power_off(sc);
    460 
    461 		for (i = 0; i < ATZ_NLEVELS; i++) {
    462 
    463 			if (sc->sc_zone.al[i].Pointer != NULL)
    464 				ACPI_FREE(sc->sc_zone.al[i].Pointer);
    465 
    466 			sc->sc_zone.al[i].Pointer = NULL;
    467 		}
    468 	}
    469 
    470 	valid_levels = 0;
    471 
    472 	for (i = 0; i < ATZ_NLEVELS; i++) {
    473 
    474 		(void)snprintf(buf, sizeof(buf), "_AC%d", i);
    475 
    476 		if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
    477 			continue;
    478 
    479 		(void)snprintf(buf, sizeof(buf), "_AL%d", i);
    480 
    481 		rv = acpi_eval_struct(sc->sc_node->ad_handle, buf,
    482 		    &sc->sc_zone.al[i]);
    483 
    484 		if (ACPI_FAILURE(rv)) {
    485 			sc->sc_zone.al[i].Pointer = NULL;
    486 			continue;
    487 		}
    488 
    489 		obj = sc->sc_zone.al[i].Pointer;
    490 
    491 		if (obj->Type != ACPI_TYPE_PACKAGE) {
    492 			sc->sc_zone.al[i].Pointer = NULL;
    493 			ACPI_FREE(obj);
    494 			continue;
    495 		}
    496 
    497 		if (sc->sc_first != false)
    498 			aprint_normal(" active cooling level %d: %sC", i,
    499 			    acpitz_celcius_string(sc->sc_zone.ac[i]));
    500 
    501 		valid_levels++;
    502 	}
    503 
    504 	/*
    505 	 * A brief summary (ACPI 4.0, section 11.4):
    506 	 *
    507 	 *    _TMP : current temperature (in tenths of degrees)
    508 	 *    _CRT : critical trip-point at which to shutdown
    509 	 *    _HOT : critical trip-point at which to go to S4
    510 	 *    _PSV : passive cooling policy threshold
    511 	 *    _TC1 : thermal constant for passive cooling
    512 	 *    _TC2 : thermal constant for passive cooling
    513 	 */
    514 	(void)acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
    515 	(void)acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
    516 	(void)acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
    517 	(void)acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
    518 	(void)acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
    519 	(void)acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
    520 
    521 	/*
    522 	 * If _RTV is not present or present and zero,
    523 	 * values are absolute (see ACPI 4.0, 425).
    524 	 */
    525 	acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
    526 
    527 	if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
    528 		sc->sc_zone.rtv = 0;
    529 
    530 	acpitz_sane_temp(&sc->sc_zone.tmp);
    531 	acpitz_sane_temp(&sc->sc_zone.crt);
    532 	acpitz_sane_temp(&sc->sc_zone.hot);
    533 	acpitz_sane_temp(&sc->sc_zone.psv);
    534 
    535 	if (verbose != 0) {
    536 		comma = 0;
    537 
    538 		aprint_verbose_dev(dv, "");
    539 
    540 		if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
    541 			aprint_verbose("critical %s C",
    542 			    acpitz_celcius_string(sc->sc_zone.crt));
    543 			comma = 1;
    544 		}
    545 
    546 		if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
    547 			aprint_verbose("%shot %s C", comma ? ", " : "",
    548 			    acpitz_celcius_string(sc->sc_zone.hot));
    549 			comma = 1;
    550 		}
    551 
    552 		if (sc->sc_zone.psv != ATZ_TMP_INVALID) {
    553 			aprint_normal("%spassive %s C", comma ? ", " : "",
    554 			    acpitz_celcius_string(sc->sc_zone.psv));
    555 			comma = 1;
    556 		}
    557 
    558 		if (valid_levels == 0) {
    559 			sc->sc_flags |= ATZ_F_PASSIVEONLY;
    560 
    561 			if (sc->sc_first != false)
    562 				aprint_verbose("%spassive cooling", comma ?
    563 				    ", " : "");
    564 		}
    565 
    566 		aprint_verbose("\n");
    567 	}
    568 
    569 	for (i = 0; i < ATZ_NLEVELS; i++)
    570 		acpitz_sane_temp(&sc->sc_zone.ac[i]);
    571 
    572 	acpitz_power_off(sc);
    573 	sc->sc_first = false;
    574 }
    575 
    576 static void
    577 acpitz_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
    578 {
    579 	ACPI_OSD_EXEC_CALLBACK func = NULL;
    580 	device_t dv = opaque;
    581 
    582 	switch (notify) {
    583 
    584 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    585 		func = acpitz_get_status;
    586 		break;
    587 
    588 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    589 	case ACPI_NOTIFY_DeviceListsChanged:
    590 		func = acpitz_get_zone_quiet;
    591 		break;
    592 
    593 	default:
    594 		aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify);
    595 		return;
    596 	}
    597 
    598 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
    599 }
    600 
    601 static void
    602 acpitz_sane_temp(uint32_t *tmp)
    603 {
    604 	/* Sane temperatures are beteen 0 and 150 C. */
    605 	if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
    606 		*tmp = ATZ_TMP_INVALID;
    607 }
    608 
    609 static int
    610 acpitz_get_integer(device_t dv, const char *cm, uint32_t *val)
    611 {
    612 	struct acpitz_softc *sc = device_private(dv);
    613 	ACPI_INTEGER tmp;
    614 	ACPI_STATUS rv;
    615 
    616 	rv = acpi_eval_integer(sc->sc_node->ad_handle, cm, &tmp);
    617 
    618 	if (ACPI_FAILURE(rv)) {
    619 		*val = ATZ_TMP_INVALID;
    620 
    621 		ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT,
    622 			"%s: failed to evaluate %s: %s\n",
    623 			device_xname(dv), cm, AcpiFormatException(rv)));
    624 
    625 		return 1;
    626 	}
    627 
    628 	*val = tmp;
    629 
    630 	return 0;
    631 }
    632 
    633 static int
    634 acpitz_get_fanspeed(device_t dv,
    635     uint32_t *fanmin, uint32_t *fanmax, uint32_t *fancurrent)
    636 {
    637 	struct acpitz_softc *sc = device_private(dv);
    638 	ACPI_INTEGER fmin, fmax, fcurr;
    639 	ACPI_HANDLE handle;
    640 	ACPI_STATUS rv;
    641 	int rc = 0;
    642 
    643 	handle = sc->sc_node->ad_handle;
    644 
    645 	rv = acpi_eval_integer(handle, "FMIN", &fmin);
    646 
    647 	if (ACPI_FAILURE(rv)) {
    648 		fmin = ATZ_TMP_INVALID;
    649 		rc = 1;
    650 	}
    651 
    652 	rv = acpi_eval_integer(handle, "FMAX", &fmax);
    653 
    654 	if (ACPI_FAILURE(rv)) {
    655 		fmax = ATZ_TMP_INVALID;
    656 		rc = 1;
    657 	}
    658 	rv = acpi_eval_integer(handle, "FRSP", &fcurr);
    659 
    660 	if (ACPI_FAILURE(rv)) {
    661 		fcurr = ATZ_TMP_INVALID;
    662 		rc = 1;
    663 	}
    664 
    665 	if (fanmin != NULL)
    666 		*fanmin = fmin;
    667 
    668 	if (fanmax != NULL)
    669 		*fanmax = fmax;
    670 
    671 	if (fancurrent != NULL)
    672 		*fancurrent = fcurr;
    673 
    674 	return rc;
    675 }
    676 
    677 #ifdef notyet
    678 static ACPI_STATUS
    679 acpitz_set_fanspeed(device_t dv, uint32_t fanspeed)
    680 {
    681 	struct acpitz_softc *sc = device_private(dv);
    682 	ACPI_HANDLE handle;
    683 	ACPI_STATUS rv;
    684 
    685 	handle = sc->sc_node->ad_handle;
    686 
    687 	rv = acpi_eval_set_integer(handle, "FSSP", fanspeed);
    688 
    689 	if (ACPI_FAILURE(rv))
    690 		aprint_debug_dev(dv, "failed to set fan speed to %u RPM: %s\n",
    691 		    fanspeed, AcpiFormatException(rv));
    692 
    693 	return rv;
    694 }
    695 #endif
    696 
    697 static void
    698 acpitz_tick(void *opaque)
    699 {
    700 	device_t dv = opaque;
    701 	struct acpitz_softc *sc = device_private(dv);
    702 
    703 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
    704 
    705 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    706 }
    707 
    708 static void
    709 acpitz_init_envsys(device_t dv)
    710 {
    711 	const int flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    712 	struct acpitz_softc *sc = device_private(dv);
    713 
    714 	sc->sc_sme = sysmon_envsys_create();
    715 
    716 	sc->sc_sme->sme_cookie = sc;
    717 	sc->sc_sme->sme_name = device_xname(dv);
    718 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    719 	sc->sc_sme->sme_get_limits = acpitz_get_limits;
    720 
    721 	sc->sc_temp_sensor.flags = flags;
    722 	sc->sc_temp_sensor.units = ENVSYS_STEMP;
    723 
    724 	(void)strlcpy(sc->sc_temp_sensor.desc, "temperature",
    725 	    sizeof(sc->sc_temp_sensor.desc));
    726 
    727 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_temp_sensor))
    728 		goto out;
    729 
    730 	if (sc->sc_have_fan != false) {
    731 
    732 		sc->sc_fan_sensor.flags = flags;
    733 		sc->sc_fan_sensor.units = ENVSYS_SFANRPM;
    734 
    735 		(void)strlcpy(sc->sc_fan_sensor.desc,
    736 		    "FAN", sizeof(sc->sc_fan_sensor.desc));
    737 
    738 		/* Ignore error because fan sensor is optional. */
    739 		(void)sysmon_envsys_sensor_attach(sc->sc_sme,
    740 		    &sc->sc_fan_sensor);
    741 	}
    742 
    743 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    744 		return;
    745 
    746 out:
    747 	aprint_error_dev(dv, "unable to register with sysmon\n");
    748 
    749 	sysmon_envsys_destroy(sc->sc_sme);
    750 	sc->sc_sme = NULL;
    751 }
    752 
    753 static void
    754 acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    755 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    756 {
    757 	struct acpitz_softc *sc = sme->sme_cookie;
    758 	int i;
    759 
    760 	switch (edata->units) {
    761 	case ENVSYS_STEMP:
    762 		*props = 0;
    763 		if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
    764 			*props |= PROP_CRITMAX;
    765 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
    766 		} else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
    767 			*props |= PROP_CRITMAX;
    768 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
    769 		}
    770 		for (i = 0; i < ATZ_NLEVELS; i++) {
    771 			if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
    772 				limits->sel_warnmax =
    773 				    ATZ2UKELVIN(sc->sc_zone.ac[i]);
    774 				*props |= PROP_WARNMAX;
    775 				break;
    776 			}
    777 		}
    778 		break;
    779 
    780 	case ENVSYS_SFANRPM:
    781 		*props = 0;
    782 		if (sc->sc_zone.fanmin != ATZ_TMP_INVALID) {
    783 			*props |= PROP_WARNMIN;
    784 			limits->sel_warnmin = sc->sc_zone.fanmin;
    785 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MIN;
    786 		}
    787 		if (sc->sc_zone.fanmax != ATZ_TMP_INVALID) {
    788 			*props |= PROP_WARNMAX;
    789 			limits->sel_warnmax = sc->sc_zone.fanmax;
    790 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MAX;
    791 		}
    792 		break;
    793 	}
    794 }
    795