Home | History | Annotate | Line # | Download | only in i2c
sdtemp.c revision 1.23.4.2
      1 /*      $NetBSD: sdtemp.c,v 1.23.4.2 2016/12/08 07:34:34 snj 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.4.2 2016/12/08 07:34:34 snj 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_mfgid;
     59 	uint16_t sc_devid;
     60 	uint16_t sc_devid_masked;
     61 	uint16_t sc_capability;
     62 };
     63 
     64 static int  sdtemp_match(device_t, cfdata_t, void *);
     65 static void sdtemp_attach(device_t, device_t, void *);
     66 static int  sdtemp_detach(device_t, int);
     67 
     68 CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
     69 	sdtemp_match, sdtemp_attach, sdtemp_detach, NULL);
     70 
     71 static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     72 static void	sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
     73 				  sysmon_envsys_lim_t *, uint32_t *);
     74 static void	sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
     75 				  sysmon_envsys_lim_t *, uint32_t *);
     76 #ifdef NOT_YET
     77 static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
     78 static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
     79 #endif /* NOT YET */
     80 static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
     81 static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
     82 static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
     83 static bool	sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
     84 static bool	sdtemp_pmf_resume(device_t, const pmf_qual_t *);
     85 /* Device dependent config functions */
     86 static void	sdtemp_config_mcp(struct sdtemp_softc *);
     87 static void	sdtemp_config_idt(struct sdtemp_softc *);
     88 
     89 struct sdtemp_dev_entry {
     90 	const uint16_t sdtemp_mfg_id;
     91 	const uint16_t sdtemp_devrev;
     92 	const uint16_t sdtemp_mask;
     93 	void         (*sdtemp_config)(struct sdtemp_softc *);
     94 	const char    *sdtemp_desc;
     95 };
     96 
     97 /* Convert sysmon_envsys uKelvin value to simple degC */
     98 
     99 #define	__UK2C(uk) (((uk) - 273150000) / 1000000)
    100 
    101 /* List of devices known to conform to JEDEC JC42.4 */
    102 
    103 #define	CMCP sdtemp_config_mcp
    104 #define	CIDT sdtemp_config_idt
    105 
    106 static const struct sdtemp_dev_entry
    107 sdtemp_dev_table[] = {
    108     { AT_MANUFACTURER_ID,   AT_30TS00_DEVICE_ID,     AT_30TS00_MASK,	  NULL,
    109 	"Atmel AT30TS00" },
    110     { AT2_MANUFACTURER_ID,  AT2_30TSE004_DEVICE_ID,  AT2_30TSE004_MASK,	  NULL,
    111 	"Atmel AT30TSE004" },
    112     { GT_MANUFACTURER_ID,   GT_30TS00_DEVICE_ID,     GT_30TS00_MASK,	  NULL,
    113 	"Giantec GT30TS00" },
    114     { GT2_MANUFACTURER_ID,  GT2_34TS02_DEVICE_ID,    GT2_34TS02_MASK,	  NULL,
    115 	"Giantec GT34TS02" },
    116     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,     MAX_6604_MASK,	  NULL,
    117 	"Maxim MAX6604" },
    118     { MCP_MANUFACTURER_ID,  MCP_9804_DEVICE_ID,	     MCP_9804_MASK,	  CMCP,
    119 	"Microchip Tech MCP9804" },
    120     { MCP_MANUFACTURER_ID,  MCP_9805_DEVICE_ID,	     MCP_9805_MASK,	  NULL,
    121 	"Microchip Tech MCP9805/MCP9843" },
    122     { MCP_MANUFACTURER_ID,  MCP_98242_DEVICE_ID,     MCP_98242_MASK,	  CMCP,
    123 	"Microchip Tech MCP98242" },
    124     { MCP_MANUFACTURER_ID,  MCP_98243_DEVICE_ID,     MCP_98243_MASK,	  CMCP,
    125 	"Microchip Tech MCP98243" },
    126     { MCP_MANUFACTURER_ID,  MCP_98244_DEVICE_ID,     MCP_98244_MASK,	  CMCP,
    127 	"Microchip Tech MCP98244" },
    128     { ADT_MANUFACTURER_ID,  ADT_7408_DEVICE_ID,	     ADT_7408_MASK,	  NULL,
    129 	"Analog Devices ADT7408" },
    130     { NXP_MANUFACTURER_ID,  NXP_SE98_DEVICE_ID,	     NXP_SE98_MASK,	  NULL,
    131 	"NXP Semiconductors SE97B/SE98" },
    132     { NXP_MANUFACTURER_ID,  NXP_SE97_DEVICE_ID,	     NXP_SE97_MASK,	  NULL,
    133 	"NXP Semiconductors SE97" },
    134     { STTS_MANUFACTURER_ID, STTS_424E_DEVICE_ID,     STTS_424E_MASK,	  NULL,
    135 	"STmicroelectronics STTS424E" },
    136     { STTS_MANUFACTURER_ID, STTS_424_DEVICE_ID,	     STTS_424_MASK,	  NULL,
    137 	"STmicroelectronics STTS424" },
    138     { STTS_MANUFACTURER_ID, STTS_2002_DEVICE_ID,     STTS_2002_MASK,	  NULL,
    139 	"STmicroelectronics STTS2002" },
    140     { STTS_MANUFACTURER_ID, STTS_2004_DEVICE_ID,     STTS_2004_MASK,	  NULL,
    141 	"STmicroelectronics STTS2004" },
    142     { STTS_MANUFACTURER_ID, STTS_3000_DEVICE_ID,     STTS_3000_MASK,	  NULL,
    143 	"STmicroelectronics STTS3000" },
    144     { CAT_MANUFACTURER_ID,  CAT_34TS02_DEVICE_ID,    CAT_34TS02_MASK,	  NULL,
    145 	"Catalyst CAT34TS02/CAT6095" },
    146     { CAT_MANUFACTURER_ID,  CAT_34TS02C_DEVICE_ID,   CAT_34TS02C_MASK,	  NULL,
    147 	"Catalyst CAT34TS02C" },
    148     { CAT_MANUFACTURER_ID,  CAT_34TS04_DEVICE_ID,    CAT_34TS04_MASK,	  NULL,
    149 	"Catalyst CAT34TS04" },
    150     { IDT_MANUFACTURER_ID,  IDT_TSE2004GB2_DEVICE_ID,IDT_TSE2004GB2_MASK, NULL,
    151 	"Integrated Device Technology TSE2004GB2" },
    152     { IDT_MANUFACTURER_ID,  IDT_TS3000B3_DEVICE_ID,  IDT_TS3000B3_MASK,	  CIDT,
    153 	"Integrated Device Technology TS3000B3/TSE2002B3" },
    154     { IDT_MANUFACTURER_ID,  IDT_TS3000GB0_DEVICE_ID, IDT_TS3000GB0_MASK,  CIDT,
    155 	"Integrated Device Technology TS3000GB0" },
    156     { IDT_MANUFACTURER_ID,  IDT_TS3000GB2_DEVICE_ID, IDT_TS3000GB2_MASK,  CIDT,
    157 	"Integrated Device Technology TS3000GB2" },
    158     { IDT_MANUFACTURER_ID,  IDT_TS3001GB2_DEVICE_ID, IDT_TS3001GB2_MASK,  CIDT,
    159 	"Integrated Device Technology TS3001GB2" },
    160     /*
    161      * Don't change the location of the following two entries. Device specific
    162      * entry must be located at above.
    163      */
    164     { 0,		    TSE2004AV_ID,	     TSE2004AV_MASK,	  NULL,
    165 	"TSE2004av compliant device (generic driver)" },
    166     { 0, 0, 0, NULL, "Unknown" }
    167 };
    168 
    169 #undef CMCP
    170 #undef CIDT
    171 
    172 static const char *temp_resl[] = {
    173 	"0.5C",
    174 	"0.25C",
    175 	"0.125C",
    176 	"0.0625C"
    177 };
    178 
    179 static int
    180 sdtemp_lookup(uint16_t mfg, uint16_t devrev)
    181 {
    182 	int i;
    183 
    184 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++) {
    185 		if (mfg != sdtemp_dev_table[i].sdtemp_mfg_id)
    186 			continue;
    187 		if ((devrev & sdtemp_dev_table[i].sdtemp_mask) ==
    188 		    sdtemp_dev_table[i].sdtemp_devrev)
    189 			break;
    190 	}
    191 	/* Check TSE2004av */
    192 	if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0)
    193 	    && (SDTEMP_IS_TSE2004AV(devrev) == 0))
    194 			i++; /* Unknown */
    195 
    196 	return i;
    197 }
    198 
    199 static int
    200 sdtemp_match(device_t parent, cfdata_t cf, void *aux)
    201 {
    202 	struct i2c_attach_args *ia = aux;
    203 	uint16_t mfgid, devid, cap;
    204 	struct sdtemp_softc sc;
    205 	int i, error;
    206 
    207 	sc.sc_tag = ia->ia_tag;
    208 	sc.sc_address = ia->ia_addr;
    209 
    210 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
    211 		return 0;
    212 
    213 	/* Verify that we can read the manufacturer ID, Device ID and the capability */
    214 	iic_acquire_bus(sc.sc_tag, 0);
    215 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
    216 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid) |
    217 		sdtemp_read_16(&sc, SDTEMP_REG_CAPABILITY, &cap);
    218 	iic_release_bus(sc.sc_tag, 0);
    219 
    220 	if (error)
    221 		return 0;
    222 
    223 	i = sdtemp_lookup(mfgid, devid);
    224 	if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0) &&
    225 	    (sdtemp_dev_table[i].sdtemp_devrev == 0)) {
    226 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
    227 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
    228 		    devid & 0xff, sc.sc_address);
    229 		return 0;
    230 	}
    231 
    232 	/*
    233 	 * Check by SDTEMP_IS_TSE2004AV() might not be enough, so check the alarm
    234 	 * capability, too.
    235 	 */
    236 	if ((cap & SDTEMP_CAP_HAS_ALARM) == 0)
    237 		return 0;
    238 
    239 	return 1;
    240 }
    241 
    242 static void
    243 sdtemp_attach(device_t parent, device_t self, void *aux)
    244 {
    245 	struct sdtemp_softc *sc = device_private(self);
    246 	struct i2c_attach_args *ia = aux;
    247 	uint16_t mfgid, devid;
    248 	int i, error;
    249 
    250 	sc->sc_tag = ia->ia_tag;
    251 	sc->sc_address = ia->ia_addr;
    252 	sc->sc_dev = self;
    253 
    254 	iic_acquire_bus(sc->sc_tag, 0);
    255 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
    256 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
    257 		iic_release_bus(sc->sc_tag, 0);
    258 		aprint_error(": attach error %d\n", error);
    259 		return;
    260 	}
    261 	sc->sc_mfgid = mfgid;
    262 	sc->sc_devid = devid;
    263 	i = sdtemp_lookup(mfgid, devid);
    264 	sc->sc_devid_masked = devid & sdtemp_dev_table[i].sdtemp_mask;
    265 
    266 	aprint_naive(": Temp Sensor\n");
    267 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
    268 
    269 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
    270 		if (SDTEMP_IS_TSE2004AV(devid))
    271 			aprint_normal_dev(self, "TSE2004av compliant. "
    272 			    "Manufacturer ID 0x%04hx, Device revision 0x%02x\n",
    273 			    mfgid, devid & TSE2004AV_REV);
    274 		else {
    275 			aprint_error_dev(self,
    276 			    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
    277 			    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
    278 			iic_release_bus(sc->sc_tag, 0);
    279 			aprint_error_dev(self, "It should no happen. "
    280 			    "Why attach() found me?\n");
    281 			return;
    282 		}
    283 	}
    284 
    285 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
    286 	aprint_debug_dev(self, "capability reg = %04x\n", sc->sc_capability);
    287 	sc->sc_resolution
    288 	    = __SHIFTOUT(sc->sc_capability, SDTEMP_CAP_RESOLUTION);
    289 	/*
    290 	 * Call device dependent function here. Currently, it's used for
    291 	 * the resolution.
    292 	 *
    293 	 * IDT's devices and some Microchip's devices have the resolution
    294 	 * register in the vendor specific registers area. The devices'
    295 	 * resolution bits in the capability register are not the maximum
    296 	 * resolution but the current vaule of the setting.
    297 	 */
    298 	if (sdtemp_dev_table[i].sdtemp_config != NULL)
    299 		sdtemp_dev_table[i].sdtemp_config(sc);
    300 
    301 	aprint_normal_dev(self, "%s accuracy",
    302 	    (sc->sc_capability & SDTEMP_CAP_ACCURACY_1C) ? "high" : "default");
    303 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) != 0)
    304 		aprint_normal(", wider range");
    305 	aprint_normal(", %s resolution", temp_resl[sc->sc_resolution]);
    306 	if ((sc->sc_capability & SDTEMP_CAP_VHV) != 0)
    307 		aprint_debug(", high voltage standoff");
    308 	aprint_debug(", %s timeout",
    309 	    (sc->sc_capability & SDTEMP_CAP_TMOUT) ? "25-35ms" : "10-60ms");
    310 	if ((sc->sc_capability & SDTEMP_CAP_EVSD) != 0)
    311 		aprint_normal(", event with shutdown");
    312 	aprint_normal("\n");
    313 	/*
    314 	 * Alarm capability is required;  if not present, this is likely
    315 	 * not a real sdtemp device.
    316 	 */
    317 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
    318 		iic_release_bus(sc->sc_tag, 0);
    319 		aprint_error_dev(self,
    320 		    "required alarm capability not present!\n");
    321 		return;
    322 	}
    323 	/* Set the configuration to defaults. */
    324 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
    325 	if (error != 0) {
    326 		iic_release_bus(sc->sc_tag, 0);
    327 		aprint_error_dev(self, "error %d writing config register\n",
    328 		    error);
    329 		return;
    330 	}
    331 	iic_release_bus(sc->sc_tag, 0);
    332 
    333 	/* Hook us into the sysmon_envsys subsystem */
    334 	sc->sc_sme = sysmon_envsys_create();
    335 	sc->sc_sme->sme_name = device_xname(self);
    336 	sc->sc_sme->sme_cookie = sc;
    337 	sc->sc_sme->sme_refresh = sdtemp_refresh;
    338 	sc->sc_sme->sme_get_limits = sdtemp_get_limits;
    339 	sc->sc_sme->sme_set_limits = sdtemp_set_limits;
    340 
    341 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
    342 	if (!sc->sc_sensor) {
    343 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
    344 		goto bad2;
    345 	}
    346 
    347 	/* Initialize sensor data. */
    348 	sc->sc_sensor->units =  ENVSYS_STEMP;
    349 	sc->sc_sensor->state = ENVSYS_SINVALID;
    350 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
    351 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
    352 	    sizeof(sc->sc_sensor->desc));
    353 	snprintf(sc->sc_sensor->desc, sizeof(sc->sc_sensor->desc),
    354 	    "DIMM %d temperature", sc->sc_address - SDTEMP_ADDR);
    355 
    356 	/* Now attach the sensor */
    357 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
    358 		aprint_error_dev(self, "unable to attach sensor\n");
    359 		goto bad;
    360 	}
    361 
    362 	/* Register the device */
    363 	error = sysmon_envsys_register(sc->sc_sme);
    364 	if (error) {
    365 		aprint_error_dev(self, "error %d registering with sysmon\n",
    366 		    error);
    367 		goto bad;
    368 	}
    369 
    370 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
    371 		aprint_error_dev(self, "couldn't establish power handler\n");
    372 
    373 	/* Retrieve and display hardware monitor limits */
    374 	sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &sc->sc_deflims,
    375 	    &sc->sc_defprops);
    376 	aprint_normal_dev(self, "Hardware limits: ");
    377 	i = 0;
    378 	if (sc->sc_defprops & PROP_WARNMIN) {
    379 		aprint_normal("low %dC",
    380 		              __UK2C(sc->sc_deflims.sel_warnmin));
    381 		i++;
    382 	}
    383 	if (sc->sc_defprops & PROP_WARNMAX) {
    384 		aprint_normal("%shigh %dC ", (i)?", ":"",
    385 			      __UK2C(sc->sc_deflims.sel_warnmax));
    386 		i++;
    387 	}
    388 	if (sc->sc_defprops & PROP_CRITMAX) {
    389 		aprint_normal("%scritical %dC ", (i)?", ":"",
    390 			      __UK2C(sc->sc_deflims.sel_critmax));
    391 		i++;
    392 	}
    393 	aprint_normal("%s\n", (i)?"":"none set");
    394 
    395 	return;
    396 
    397 bad:
    398 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    399 bad2:
    400 	sysmon_envsys_destroy(sc->sc_sme);
    401 }
    402 
    403 static int
    404 sdtemp_detach(device_t self, int flags)
    405 {
    406 	struct sdtemp_softc *sc = device_private(self);
    407 
    408 	pmf_device_deregister(self);
    409 
    410 	if (sc->sc_sme)
    411 		sysmon_envsys_unregister(sc->sc_sme);
    412 	if (sc->sc_sensor)
    413 		kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    414 
    415 	return 0;
    416 }
    417 
    418 /* Retrieve current limits from device, and encode in uKelvins */
    419 static void
    420 sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    421 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    422 {
    423 	struct sdtemp_softc *sc = sme->sme_cookie;
    424 	uint16_t lim;
    425 
    426 	*props = 0;
    427 	iic_acquire_bus(sc->sc_tag, 0);
    428 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
    429 		limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
    430 		*props |= PROP_WARNMIN;
    431 	}
    432 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
    433 		limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
    434 		*props |= PROP_WARNMAX;
    435 	}
    436 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
    437 		limits->sel_critmax = sdtemp_decode_temp(sc, lim);
    438 		*props |= PROP_CRITMAX;
    439 	}
    440 	iic_release_bus(sc->sc_tag, 0);
    441 	if (*props != 0)
    442 		*props |= PROP_DRIVER_LIMITS;
    443 }
    444 
    445 /* Send current limit values to the device */
    446 static void
    447 sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    448 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    449 {
    450 	uint16_t val;
    451 	struct sdtemp_softc *sc = sme->sme_cookie;
    452 
    453 	if (limits == NULL) {
    454 		limits = &sc->sc_deflims;
    455 		props  = &sc->sc_defprops;
    456 	}
    457 	iic_acquire_bus(sc->sc_tag, 0);
    458 	if (*props & PROP_WARNMIN) {
    459 		val = __UK2C(limits->sel_warnmin);
    460 		(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
    461 					(val << 4) & SDTEMP_TEMP_MASK);
    462 	}
    463 	if (*props & PROP_WARNMAX) {
    464 		val = __UK2C(limits->sel_warnmax);
    465 		(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
    466 					(val << 4) & SDTEMP_TEMP_MASK);
    467 	}
    468 	if (*props & PROP_CRITMAX) {
    469 		val = __UK2C(limits->sel_critmax);
    470 		(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
    471 					(val << 4) & SDTEMP_TEMP_MASK);
    472 	}
    473 	iic_release_bus(sc->sc_tag, 0);
    474 
    475 	/*
    476 	 * If at least one limit is set that we can handle, and no
    477 	 * limits are set that we cannot handle, tell sysmon that
    478 	 * the driver will take care of monitoring the limits!
    479 	 */
    480 	if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
    481 		*props &= ~PROP_DRIVER_LIMITS;
    482 	else if (*props & PROP_LIMITS)
    483 		*props |= PROP_DRIVER_LIMITS;
    484 	else
    485 		*props &= ~PROP_DRIVER_LIMITS;
    486 }
    487 
    488 #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
    489 
    490 /* Read a 8-bit value from a register */
    491 static int
    492 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
    493 {
    494 	int error;
    495 
    496 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    497 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    498 
    499 	return error;
    500 }
    501 
    502 static int
    503 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
    504 {
    505 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    506 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
    507 }
    508 #endif /* NOT_YET */
    509 
    510 /* Read a 16-bit value from a register */
    511 static int
    512 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
    513 {
    514 	int error;
    515 
    516 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    517 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    518 	if (error)
    519 		return error;
    520 
    521 	*valp = be16toh(*valp);
    522 
    523 	return 0;
    524 }
    525 
    526 static int
    527 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
    528 {
    529 	uint16_t temp;
    530 
    531 	temp = htobe16(val);
    532 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    533 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
    534 }
    535 
    536 static uint32_t
    537 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
    538 {
    539 	uint32_t val;
    540 	int32_t stemp;
    541 
    542 	/* Get only the temperature bits */
    543 	temp &= SDTEMP_TEMP_MASK;
    544 
    545 	/* If necessary, extend the sign bit */
    546 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
    547 	    (temp & SDTEMP_TEMP_NEGATIVE))
    548 		temp |= SDTEMP_TEMP_SIGN_EXT;
    549 
    550 	/* Mask off only bits valid within current resolution */
    551 	temp &= ~(0x7 >> sc->sc_resolution);
    552 
    553 	/* Treat as signed and extend to 32-bits */
    554 	stemp = (int16_t)temp;
    555 
    556 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
    557 	val = (stemp * 62500) + 273150000;
    558 
    559 	return val;
    560 }
    561 
    562 static void
    563 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    564 {
    565 	struct sdtemp_softc *sc = sme->sme_cookie;
    566 	uint16_t val;
    567 	int error;
    568 
    569 	iic_acquire_bus(sc->sc_tag, 0);
    570 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
    571 	iic_release_bus(sc->sc_tag, 0);
    572 
    573 	if (error) {
    574 		edata->state = ENVSYS_SINVALID;
    575 		return;
    576 	}
    577 
    578 	edata->value_cur = sdtemp_decode_temp(sc, val);
    579 
    580 	/* Now check for limits */
    581 	if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
    582 		edata->state = ENVSYS_SVALID;
    583 	else if ((val & SDTEMP_ABOVE_CRIT) &&
    584 		    (edata->upropset & PROP_CRITMAX))
    585 		edata->state = ENVSYS_SCRITOVER;
    586 	else if ((val & SDTEMP_ABOVE_UPPER) &&
    587 		    (edata->upropset & PROP_WARNMAX))
    588 		edata->state = ENVSYS_SWARNOVER;
    589 	else if ((val & SDTEMP_BELOW_LOWER) &&
    590 		    (edata->upropset & PROP_WARNMIN))
    591 		edata->state = ENVSYS_SWARNUNDER;
    592 	else
    593 		edata->state = ENVSYS_SVALID;
    594 }
    595 
    596 /*
    597  * power management functions
    598  *
    599  * We go into "shutdown" mode at suspend time, and return to normal
    600  * mode upon resume.  This reduces power consumption by disabling
    601  * the A/D converter.
    602  */
    603 
    604 static bool
    605 sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
    606 {
    607 	struct sdtemp_softc *sc = device_private(dev);
    608 	int error;
    609 	uint16_t config;
    610 
    611 	iic_acquire_bus(sc->sc_tag, 0);
    612 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    613 	if (error == 0) {
    614 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
    615 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    616 	}
    617 	iic_release_bus(sc->sc_tag, 0);
    618 	return (error == 0);
    619 }
    620 
    621 static bool
    622 sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
    623 {
    624 	struct sdtemp_softc *sc = device_private(dev);
    625 	int error;
    626 	uint16_t config;
    627 
    628 	iic_acquire_bus(sc->sc_tag, 0);
    629 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    630 	if (error == 0) {
    631 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
    632 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    633 	}
    634 	iic_release_bus(sc->sc_tag, 0);
    635 	return (error == 0);
    636 }
    637 
    638 /* Device dependent config functions */
    639 
    640 static void
    641 sdtemp_config_mcp(struct sdtemp_softc *sc)
    642 {
    643 	int rv;
    644 	uint8_t resolreg;
    645 
    646 	/* Note that MCP9805 has no resolution register */
    647 	switch (sc->sc_devid_masked) {
    648 	case MCP_9804_DEVICE_ID:
    649 	case MCP_98242_DEVICE_ID:
    650 	case MCP_98243_DEVICE_ID:
    651 		resolreg = SDTEMP_REG_MCP_RESOLUTION_9804;
    652 		break;
    653 	case MCP_98244_DEVICE_ID:
    654 		resolreg = SDTEMP_REG_MCP_RESOLUTION_98244;
    655 		break;
    656 	default:
    657 		aprint_error("%s: %s: unknown device ID (%04hx)\n",
    658 		    device_xname(sc->sc_dev), __func__, sc->sc_devid_masked);
    659 		return;
    660 	}
    661 
    662 	/*
    663 	 * Set resolution to the max.
    664 	 *
    665 	 * Even if it fails, the resolution will be the default. It's not a
    666 	 * fatal error.
    667 	 */
    668 	rv = sdtemp_write_16(sc, resolreg, SDTEMP_CAP_RESOLUTION_MAX);
    669 	if (rv == 0)
    670 		sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
    671 	else
    672 		aprint_error("%s: error %d writing resolution register\n",
    673 		    device_xname(sc->sc_dev), rv);
    674 }
    675 
    676 static void
    677 sdtemp_config_idt(struct sdtemp_softc *sc)
    678 {
    679 	int rv;
    680 
    681 	/*
    682 	 * Set resolution to the max.
    683 	 *
    684 	 * Even if it fails, the resolution will be the default. It's not a
    685 	 * fatal error.
    686 	 */
    687 	rv = sdtemp_write_16(sc, SDTEMP_REG_IDT_RESOLUTION,
    688 	    __SHIFTIN(SDTEMP_CAP_RESOLUTION_MAX, SDTEMP_CAP_RESOLUTION));
    689 	if (rv == 0)
    690 		sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
    691 	else
    692 		aprint_error("%s: error %d writing resolution register\n",
    693 		    device_xname(sc->sc_dev), rv);
    694 }
    695 
    696 MODULE(MODULE_CLASS_DRIVER, sdtemp, "i2cexec");
    697 
    698 #ifdef _MODULE
    699 #include "ioconf.c"
    700 #endif
    701 
    702 static int
    703 sdtemp_modcmd(modcmd_t cmd, void *opaque)
    704 {
    705 	int error = 0;
    706 
    707 	switch (cmd) {
    708 	case MODULE_CMD_INIT:
    709 #ifdef _MODULE
    710 		error = config_init_component(cfdriver_ioconf_sdtemp,
    711 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    712 #endif
    713 		return error;
    714 	case MODULE_CMD_FINI:
    715 #ifdef _MODULE
    716 		error = config_fini_component(cfdriver_ioconf_sdtemp,
    717 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    718 #endif
    719 		return error;
    720 	default:
    721 		return ENOTTY;
    722 	}
    723 }
    724