Home | History | Annotate | Line # | Download | only in i2c
sdtemp.c revision 1.23.6.2
      1 /*      $NetBSD: sdtemp.c,v 1.23.6.2 2015/06/06 14:40:07 skrll Exp $        */
      2 
      3 /*
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Goyette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sdtemp.c,v 1.23.6.2 2015/06/06 14:40:07 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kmem.h>
     38 #include <sys/device.h>
     39 #include <sys/kernel.h>
     40 #include <sys/endian.h>
     41 #include <sys/module.h>
     42 
     43 #include <dev/sysmon/sysmonvar.h>
     44 
     45 #include <dev/i2c/i2cvar.h>
     46 #include <dev/i2c/sdtemp_reg.h>
     47 
     48 struct sdtemp_softc {
     49 	device_t sc_dev;
     50 	i2c_tag_t sc_tag;
     51 	int sc_address;
     52 
     53 	struct sysmon_envsys *sc_sme;
     54 	envsys_data_t *sc_sensor;
     55 	sysmon_envsys_lim_t sc_deflims;
     56 	uint32_t sc_defprops;
     57 	int sc_resolution;
     58 	uint16_t sc_capability;
     59 };
     60 
     61 static int  sdtemp_match(device_t, cfdata_t, void *);
     62 static void sdtemp_attach(device_t, device_t, void *);
     63 static int  sdtemp_detach(device_t, int);
     64 
     65 CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
     66 	sdtemp_match, sdtemp_attach, sdtemp_detach, NULL);
     67 
     68 static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     69 static void	sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
     70 				  sysmon_envsys_lim_t *, uint32_t *);
     71 static void	sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
     72 				  sysmon_envsys_lim_t *, uint32_t *);
     73 #ifdef NOT_YET
     74 static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
     75 static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
     76 #endif /* NOT YET */
     77 static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
     78 static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
     79 static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
     80 static bool	sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
     81 static bool	sdtemp_pmf_resume(device_t, const pmf_qual_t *);
     82 
     83 struct sdtemp_dev_entry {
     84 	const uint16_t sdtemp_mfg_id;
     85 	const uint16_t  sdtemp_devrev;
     86 	const uint16_t  sdtemp_mask;
     87 	const uint8_t  sdtemp_resolution;
     88 	const char    *sdtemp_desc;
     89 };
     90 
     91 /* Convert sysmon_envsys uKelvin value to simple degC */
     92 
     93 #define	__UK2C(uk) (((uk) - 273150000) / 1000000)
     94 
     95 /*
     96  * List of devices known to conform to JEDEC JC42.4
     97  *
     98  * NOTE: A non-negative value for resolution indicates that the sensor
     99  * resolution is fixed at that number of fractional bits;  a negative
    100  * value indicates that the sensor needs to be configured.  In either
    101  * case, trip-point registers are fixed at two-bit (0.25C) resolution.
    102  */
    103 static const struct sdtemp_dev_entry
    104 sdtemp_dev_table[] = {
    105     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,    MAX_6604_MASK,   3,
    106 	"Maxim MAX6604" },
    107     { MCP_MANUFACTURER_ID,   MCP_9805_DEVICE_ID,    MCP_9805_MASK,   2,
    108 	"Microchip Tech MCP9805/MCP9843" },
    109     { MCP_MANUFACTURER_ID,   MCP_98243_DEVICE_ID,   MCP_98243_MASK, -4,
    110 	"Microchip Tech MCP98243" },
    111     { MCP_MANUFACTURER_ID,   MCP_98242_DEVICE_ID,   MCP_98242_MASK, -4,
    112 	"Microchip Tech MCP98242" },
    113     { ADT_MANUFACTURER_ID,   ADT_7408_DEVICE_ID,    ADT_7408_MASK,   4,
    114 	"Analog Devices ADT7408" },
    115     { NXP_MANUFACTURER_ID,   NXP_SE98_DEVICE_ID,    NXP_SE98_MASK,   3,
    116 	"NXP Semiconductors SE97B/SE98" },
    117     { NXP_MANUFACTURER_ID,   NXP_SE97_DEVICE_ID,    NXP_SE97_MASK,   3,
    118 	"NXP Semiconductors SE97" },
    119     { STTS_MANUFACTURER_ID,  STTS_424E_DEVICE_ID,   STTS_424E_MASK,  2,
    120 	"STmicroelectronics STTS424E" },
    121     { STTS_MANUFACTURER_ID,  STTS_424_DEVICE_ID,    STTS_424_MASK,   2,
    122 	"STmicroelectronics STTS424" },
    123     { STTS_MANUFACTURER_ID,  STTS_2002_DEVICE_ID,    STTS_2002_MASK,   2,
    124 	"STmicroelectronics STTS2002" },
    125     { STTS_MANUFACTURER_ID,  STTS_2004_DEVICE_ID,    STTS_2004_MASK,   2,
    126 	"STmicroelectronics STTS2002" },
    127     { STTS_MANUFACTURER_ID,  STTS_3000_DEVICE_ID,    STTS_3000_MASK,   2,
    128 	"STmicroelectronics STTS3000" },
    129     { CAT_MANUFACTURER_ID,   CAT_34TS02_DEVICE_ID,  CAT_34TS02_MASK, 4,
    130 	"Catalyst CAT34TS02/CAT6095" },
    131     { CAT_MANUFACTURER_ID,   CAT_34TS02C_DEVICE_ID,  CAT_34TS02C_MASK, 4,
    132 	"Catalyst CAT34TS02C" },
    133     { IDT_MANUFACTURER_ID,   IDT_TS3000B3_DEVICE_ID, IDT_TS3000B3_MASK, 4,
    134 	"Integrated Device Technology TS3000B3/TSE2002B3" },
    135     { 0, 0, 0, 2, "Unknown" }
    136 };
    137 
    138 static int
    139 sdtemp_lookup(uint16_t mfg, uint16_t devrev)
    140 {
    141 	int i;
    142 
    143 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++) {
    144 		if (mfg != sdtemp_dev_table[i].sdtemp_mfg_id)
    145 			continue;
    146 		if ((devrev & sdtemp_dev_table[i].sdtemp_mask) ==
    147 		    sdtemp_dev_table[i].sdtemp_devrev)
    148 			break;
    149 	}
    150 
    151 	return i;
    152 }
    153 
    154 static int
    155 sdtemp_match(device_t parent, cfdata_t cf, void *aux)
    156 {
    157 	struct i2c_attach_args *ia = aux;
    158 	uint16_t mfgid, devid;
    159 	struct sdtemp_softc sc;
    160 	int i, error;
    161 
    162 	sc.sc_tag = ia->ia_tag;
    163 	sc.sc_address = ia->ia_addr;
    164 
    165 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
    166 		return 0;
    167 
    168 	/* Verify that we can read the manufacturer ID  & Device ID */
    169 	iic_acquire_bus(sc.sc_tag, 0);
    170 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
    171 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
    172 	iic_release_bus(sc.sc_tag, 0);
    173 
    174 	if (error)
    175 		return 0;
    176 
    177 	i = sdtemp_lookup(mfgid, devid);
    178 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
    179 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
    180 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
    181 		    devid & 0xff, sc.sc_address);
    182 		return 0;
    183 	}
    184 
    185 	return 1;
    186 }
    187 
    188 static void
    189 sdtemp_attach(device_t parent, device_t self, void *aux)
    190 {
    191 	struct sdtemp_softc *sc = device_private(self);
    192 	struct i2c_attach_args *ia = aux;
    193 	uint16_t mfgid, devid;
    194 	int i, error;
    195 
    196 	sc->sc_tag = ia->ia_tag;
    197 	sc->sc_address = ia->ia_addr;
    198 	sc->sc_dev = self;
    199 
    200 	iic_acquire_bus(sc->sc_tag, 0);
    201 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
    202 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
    203 		iic_release_bus(sc->sc_tag, 0);
    204 		aprint_error(": attach error %d\n", error);
    205 		return;
    206 	}
    207 	i = sdtemp_lookup(mfgid, devid);
    208 	sc->sc_resolution =
    209 	    sdtemp_dev_table[i].sdtemp_resolution;
    210 
    211 	aprint_naive(": Temp Sensor\n");
    212 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
    213 
    214 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
    215 		aprint_debug_dev(self,
    216 		    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
    217 		    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
    218 
    219 	/*
    220 	 * Alarm capability is required;  if not present, this is likely
    221 	 * not a real sdtemp device.
    222 	 */
    223 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
    224 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
    225 		iic_release_bus(sc->sc_tag, 0);
    226 		aprint_error_dev(self,
    227 		    "required alarm capability not present!\n");
    228 		return;
    229 	}
    230 	/* Set the configuration to defaults. */
    231 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
    232 	if (error != 0) {
    233 		iic_release_bus(sc->sc_tag, 0);
    234 		aprint_error_dev(self, "error %d writing config register\n",
    235 		    error);
    236 		return;
    237 	}
    238 	/* If variable resolution, set to max */
    239 	if (sc->sc_resolution < 0) {
    240 		sc->sc_resolution = ~sc->sc_resolution;
    241 		error = sdtemp_write_16(sc, SDTEMP_REG_RESOLUTION,
    242 					sc->sc_resolution & 0x3);
    243 		if (error != 0) {
    244 			iic_release_bus(sc->sc_tag, 0);
    245 			aprint_error_dev(self,
    246 			    "error %d writing resolution register\n", error);
    247 			return;
    248 		} else
    249 			sc->sc_resolution++;
    250 	}
    251 	iic_release_bus(sc->sc_tag, 0);
    252 
    253 	/* Hook us into the sysmon_envsys subsystem */
    254 	sc->sc_sme = sysmon_envsys_create();
    255 	sc->sc_sme->sme_name = device_xname(self);
    256 	sc->sc_sme->sme_cookie = sc;
    257 	sc->sc_sme->sme_refresh = sdtemp_refresh;
    258 	sc->sc_sme->sme_get_limits = sdtemp_get_limits;
    259 	sc->sc_sme->sme_set_limits = sdtemp_set_limits;
    260 
    261 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
    262 	if (!sc->sc_sensor) {
    263 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
    264 		goto bad2;
    265 	}
    266 
    267 	/* Initialize sensor data. */
    268 	sc->sc_sensor->units =  ENVSYS_STEMP;
    269 	sc->sc_sensor->state = ENVSYS_SINVALID;
    270 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
    271 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
    272 	    sizeof(sc->sc_sensor->desc));
    273 	snprintf(sc->sc_sensor->desc, sizeof(sc->sc_sensor->desc),
    274 	    "DIMM %d temperature", sc->sc_address - SDTEMP_ADDR);
    275 
    276 	/* Now attach the sensor */
    277 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
    278 		aprint_error_dev(self, "unable to attach sensor\n");
    279 		goto bad;
    280 	}
    281 
    282 	/* Register the device */
    283 	error = sysmon_envsys_register(sc->sc_sme);
    284 	if (error) {
    285 		aprint_error_dev(self, "error %d registering with sysmon\n",
    286 		    error);
    287 		goto bad;
    288 	}
    289 
    290 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
    291 		aprint_error_dev(self, "couldn't establish power handler\n");
    292 
    293 	/* Retrieve and display hardware monitor limits */
    294 	sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &sc->sc_deflims,
    295 	    &sc->sc_defprops);
    296 	aprint_normal_dev(self, "Hardware limits: ");
    297 	i = 0;
    298 	if (sc->sc_defprops & PROP_WARNMIN) {
    299 		aprint_normal("low %dC",
    300 		              __UK2C(sc->sc_deflims.sel_warnmin));
    301 		i++;
    302 	}
    303 	if (sc->sc_defprops & PROP_WARNMAX) {
    304 		aprint_normal("%shigh %dC ", (i)?", ":"",
    305 			      __UK2C(sc->sc_deflims.sel_warnmax));
    306 		i++;
    307 	}
    308 	if (sc->sc_defprops & PROP_CRITMAX) {
    309 		aprint_normal("%scritical %dC ", (i)?", ":"",
    310 			      __UK2C(sc->sc_deflims.sel_critmax));
    311 		i++;
    312 	}
    313 	aprint_normal("%s\n", (i)?"":"none set");
    314 
    315 	return;
    316 
    317 bad:
    318 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    319 bad2:
    320 	sysmon_envsys_destroy(sc->sc_sme);
    321 }
    322 
    323 static int
    324 sdtemp_detach(device_t self, int flags)
    325 {
    326 	struct sdtemp_softc *sc = device_private(self);
    327 
    328 	pmf_device_deregister(self);
    329 
    330 	if (sc->sc_sme)
    331 		sysmon_envsys_unregister(sc->sc_sme);
    332 	if (sc->sc_sensor)
    333 		kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    334 
    335 	return 0;
    336 }
    337 
    338 /* Retrieve current limits from device, and encode in uKelvins */
    339 static void
    340 sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    341 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    342 {
    343 	struct sdtemp_softc *sc = sme->sme_cookie;
    344 	uint16_t lim;
    345 
    346 	*props = 0;
    347 	iic_acquire_bus(sc->sc_tag, 0);
    348 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
    349 		limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
    350 		*props |= PROP_WARNMIN;
    351 	}
    352 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
    353 		limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
    354 		*props |= PROP_WARNMAX;
    355 	}
    356 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
    357 		limits->sel_critmax = sdtemp_decode_temp(sc, lim);
    358 		*props |= PROP_CRITMAX;
    359 	}
    360 	iic_release_bus(sc->sc_tag, 0);
    361 	if (*props != 0)
    362 		*props |= PROP_DRIVER_LIMITS;
    363 }
    364 
    365 /* Send current limit values to the device */
    366 static void
    367 sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    368 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    369 {
    370 	uint16_t val;
    371 	struct sdtemp_softc *sc = sme->sme_cookie;
    372 
    373 	if (limits == NULL) {
    374 		limits = &sc->sc_deflims;
    375 		props  = &sc->sc_defprops;
    376 	}
    377 	iic_acquire_bus(sc->sc_tag, 0);
    378 	if (*props & PROP_WARNMIN) {
    379 		val = __UK2C(limits->sel_warnmin);
    380 		(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
    381 					(val << 4) & SDTEMP_TEMP_MASK);
    382 	}
    383 	if (*props & PROP_WARNMAX) {
    384 		val = __UK2C(limits->sel_warnmax);
    385 		(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
    386 					(val << 4) & SDTEMP_TEMP_MASK);
    387 	}
    388 	if (*props & PROP_CRITMAX) {
    389 		val = __UK2C(limits->sel_critmax);
    390 		(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
    391 					(val << 4) & SDTEMP_TEMP_MASK);
    392 	}
    393 	iic_release_bus(sc->sc_tag, 0);
    394 
    395 	/*
    396 	 * If at least one limit is set that we can handle, and no
    397 	 * limits are set that we cannot handle, tell sysmon that
    398 	 * the driver will take care of monitoring the limits!
    399 	 */
    400 	if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
    401 		*props &= ~PROP_DRIVER_LIMITS;
    402 	else if (*props & PROP_LIMITS)
    403 		*props |= PROP_DRIVER_LIMITS;
    404 	else
    405 		*props &= ~PROP_DRIVER_LIMITS;
    406 }
    407 
    408 #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
    409 
    410 /* Read a 8-bit value from a register */
    411 static int
    412 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
    413 {
    414 	int error;
    415 
    416 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    417 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    418 
    419 	return error;
    420 }
    421 
    422 static int
    423 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
    424 {
    425 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    426 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
    427 }
    428 #endif /* NOT_YET */
    429 
    430 /* Read a 16-bit value from a register */
    431 static int
    432 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
    433 {
    434 	int error;
    435 
    436 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    437 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    438 	if (error)
    439 		return error;
    440 
    441 	*valp = be16toh(*valp);
    442 
    443 	return 0;
    444 }
    445 
    446 static int
    447 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
    448 {
    449 	uint16_t temp;
    450 
    451 	temp = htobe16(val);
    452 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    453 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
    454 }
    455 
    456 static uint32_t
    457 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
    458 {
    459 	uint32_t val;
    460 	int32_t stemp;
    461 
    462 	/* Get only the temperature bits */
    463 	temp &= SDTEMP_TEMP_MASK;
    464 
    465 	/* If necessary, extend the sign bit */
    466 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
    467 	    (temp & SDTEMP_TEMP_NEGATIVE))
    468 		temp |= SDTEMP_TEMP_SIGN_EXT;
    469 
    470 	/* Mask off only bits valid within current resolution */
    471 	temp &= ~(0xf >> sc->sc_resolution);
    472 
    473 	/* Treat as signed and extend to 32-bits */
    474 	stemp = (int16_t)temp;
    475 
    476 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
    477 	val = (stemp * 62500) + 273150000;
    478 
    479 	return val;
    480 }
    481 
    482 static void
    483 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    484 {
    485 	struct sdtemp_softc *sc = sme->sme_cookie;
    486 	uint16_t val;
    487 	int error;
    488 
    489 	iic_acquire_bus(sc->sc_tag, 0);
    490 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
    491 	iic_release_bus(sc->sc_tag, 0);
    492 
    493 	if (error) {
    494 		edata->state = ENVSYS_SINVALID;
    495 		return;
    496 	}
    497 
    498 	edata->value_cur = sdtemp_decode_temp(sc, val);
    499 
    500 	/* Now check for limits */
    501 	if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
    502 		edata->state = ENVSYS_SVALID;
    503 	else if ((val & SDTEMP_ABOVE_CRIT) &&
    504 		    (edata->upropset & PROP_CRITMAX))
    505 		edata->state = ENVSYS_SCRITOVER;
    506 	else if ((val & SDTEMP_ABOVE_UPPER) &&
    507 		    (edata->upropset & PROP_WARNMAX))
    508 		edata->state = ENVSYS_SWARNOVER;
    509 	else if ((val & SDTEMP_BELOW_LOWER) &&
    510 		    (edata->upropset & PROP_WARNMIN))
    511 		edata->state = ENVSYS_SWARNUNDER;
    512 	else
    513 		edata->state = ENVSYS_SVALID;
    514 }
    515 
    516 /*
    517  * power management functions
    518  *
    519  * We go into "shutdown" mode at suspend time, and return to normal
    520  * mode upon resume.  This reduces power consumption by disabling
    521  * the A/D converter.
    522  */
    523 
    524 static bool
    525 sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
    526 {
    527 	struct sdtemp_softc *sc = device_private(dev);
    528 	int error;
    529 	uint16_t config;
    530 
    531 	iic_acquire_bus(sc->sc_tag, 0);
    532 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    533 	if (error == 0) {
    534 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
    535 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    536 	}
    537 	iic_release_bus(sc->sc_tag, 0);
    538 	return (error == 0);
    539 }
    540 
    541 static bool
    542 sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
    543 {
    544 	struct sdtemp_softc *sc = device_private(dev);
    545 	int error;
    546 	uint16_t config;
    547 
    548 	iic_acquire_bus(sc->sc_tag, 0);
    549 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    550 	if (error == 0) {
    551 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
    552 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    553 	}
    554 	iic_release_bus(sc->sc_tag, 0);
    555 	return (error == 0);
    556 }
    557 
    558 MODULE(MODULE_CLASS_DRIVER, sdtemp, "i2cexec,sysmon_envsys");
    559 
    560 #ifdef _MODULE
    561 #include "ioconf.c"
    562 #endif
    563 
    564 static int
    565 sdtemp_modcmd(modcmd_t cmd, void *opaque)
    566 {
    567 	int error = 0;
    568 
    569 	switch (cmd) {
    570 	case MODULE_CMD_INIT:
    571 #ifdef _MODULE
    572 		error = config_init_component(cfdriver_ioconf_sdtemp,
    573 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    574 #endif
    575 		return error;
    576 	case MODULE_CMD_FINI:
    577 #ifdef _MODULE
    578 		error = config_fini_component(cfdriver_ioconf_sdtemp,
    579 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    580 #endif
    581 		return error;
    582 	default:
    583 		return ENOTTY;
    584 	}
    585 }
    586