Home | History | Annotate | Line # | Download | only in i2c
sdtemp.c revision 1.3
      1 /*      $NetBSD: sdtemp.c,v 1.3 2009/06/01 20:08:44 pgoyette 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.3 2009/06/01 20:08:44 pgoyette 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/sysctl.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 	int sc_resolution;
     56 	uint16_t sc_capability;
     57 	uint16_t sc_low_lim, sc_high_lim, sc_crit_lim;
     58 };
     59 
     60 static int  sdtemp_match(device_t, cfdata_t, void *);
     61 static void sdtemp_attach(device_t, device_t, void *);
     62 
     63 CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
     64 	sdtemp_match, sdtemp_attach, NULL, NULL);
     65 
     66 static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     67 #ifdef NOT_YET
     68 static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
     69 static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
     70 #endif /* NOT YET */
     71 static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
     72 static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
     73 static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
     74 static void	sdtemp_set_thresh(struct sdtemp_softc *, int, uint16_t);
     75 static bool	sdtemp_pmf_suspend(device_t PMF_FN_PROTO);
     76 static bool	sdtemp_pmf_resume(device_t PMF_FN_PROTO);
     77 
     78 SYSCTL_SETUP_PROTO(sysctl_sdtemp_setup);
     79 static int sdtemp_sysctl_helper(SYSCTLFN_PROTO);
     80 
     81 struct sdtemp_dev_entry {
     82 	const uint16_t sdtemp_mfg_id;
     83 	const uint8_t  sdtemp_dev_id;
     84 	const uint8_t  sdtemp_rev_id;
     85 	const uint8_t  sdtemp_resolution;
     86 	const char    *sdtemp_desc;
     87 };
     88 
     89 /* sysctl stuff */
     90 static int hw_node = CTL_EOL;
     91 
     92 /*
     93  * List of devices known to conform to JEDEC JC42.4
     94  *
     95  * NOTE: A non-negative value for resolution indicates that the sensor
     96  * resolution is fixed at that number of fractional bits;  a negative
     97  * value indicates that the sensor needs to be configured.  In either
     98  * case, trip-point registers are fixed at two-bit (0.25C) resolution.
     99  */
    100 static const struct sdtemp_dev_entry
    101 sdtemp_dev_table[] = {
    102     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,    0xff, 3,
    103 	"Maxim MAX604" },
    104     { MCP_MANUFACTURER_ID,   MCP_9805_DEVICE_ID,    0xff, 2,
    105 	"Microchip Tech MCP9805" },
    106     { MCP_MANUFACTURER_ID,   MCP_98242_DEVICE_ID,   0xff, -4,
    107 	"Microchip Tech MCP98242" },
    108     { ADT_MANUFACTURER_ID,   ADT_7408_DEVICE_ID,    0xff, 4,
    109 	"Analog Devices ADT7408" },
    110     { NXP_MANUFACTURER_ID,   NXP_SE97_DEVICE_ID,    0xff, 3,
    111 	"NXP Semiconductors SE97/SE98" },
    112     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x00, 2,
    113 	"STmicroelectronics STTS424E02-DA" },
    114     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x01, 2,
    115 	"STmicroelectronics STTS424E02-DN" },
    116     { CAT_MANUFACTURER_ID,   CAT_34TS02_DEVICE_ID,  0xff, 4,
    117 	"Catalyst CAT34TS02/CAT6095" },
    118     { 0, 0, 0, 2, "Unknown" }
    119 };
    120 
    121 static int
    122 sdtemp_lookup(uint16_t mfg, uint16_t dev, uint16_t rev)
    123 {
    124 	int i;
    125 
    126 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++)
    127 		if (sdtemp_dev_table[i].sdtemp_mfg_id == mfg &&
    128 		    sdtemp_dev_table[i].sdtemp_dev_id == dev &&
    129 		    (sdtemp_dev_table[i].sdtemp_rev_id == 0xff ||
    130 		     sdtemp_dev_table[i].sdtemp_rev_id == rev))
    131 			break;
    132 
    133 	return i;
    134 }
    135 
    136 static int
    137 sdtemp_match(device_t parent, cfdata_t cf, void *aux)
    138 {
    139 	struct i2c_attach_args *ia = aux;
    140 	uint16_t mfgid, devid;
    141 	struct sdtemp_softc sc;
    142 	int i, error;
    143 
    144 	sc.sc_tag = ia->ia_tag;
    145 	sc.sc_address = ia->ia_addr;
    146 
    147 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
    148 		return 0;
    149 
    150 	/* Verify that we can read the manufacturer ID  & Device ID */
    151 	iic_acquire_bus(sc.sc_tag, 0);
    152 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
    153 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
    154 	iic_release_bus(sc.sc_tag, 0);
    155 
    156 	if (error)
    157 		return 0;
    158 
    159 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
    160 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
    161 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
    162 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
    163 		    devid & 0xff, sc.sc_address);
    164 		return 0;
    165 	}
    166 
    167 	return 1;
    168 }
    169 
    170 static void
    171 sdtemp_attach(device_t parent, device_t self, void *aux)
    172 {
    173 	struct sdtemp_softc *sc = device_private(self);
    174 	struct i2c_attach_args *ia = aux;
    175 	const struct sysctlnode *node = NULL;
    176 	uint16_t mfgid, devid;
    177 	int32_t	dev_sysctl_num;
    178 	int i, error;
    179 
    180 	sc->sc_tag = ia->ia_tag;
    181 	sc->sc_address = ia->ia_addr;
    182 	sc->sc_dev = self;
    183 
    184 	iic_acquire_bus(sc->sc_tag, 0);
    185 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
    186 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
    187 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    188 		aprint_error(": attach error %d\n", error);
    189 		return;
    190 	}
    191 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
    192 	sc->sc_resolution =
    193 	    sdtemp_dev_table[i].sdtemp_resolution;
    194 
    195 	aprint_naive(": Temp Sensor\n");
    196 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
    197 
    198 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
    199 		aprint_debug_dev(self,
    200 		    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
    201 		    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
    202 
    203 	/*
    204 	 * Alarm capability is required;  if not present, this is likely
    205 	 * not a real sdtemp device.
    206 	 */
    207 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
    208 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
    209 		iic_release_bus(sc->sc_tag, 0);
    210 		aprint_error_dev(self,
    211 		    "required alarm capability not present!\n");
    212 		return;
    213 	}
    214 	/* Set the configuration to defaults. */
    215 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
    216 	if (error != 0) {
    217 		iic_release_bus(sc->sc_tag, 0);
    218 		aprint_error_dev(self, "error %d writing config register\n",
    219 		    error);
    220 		return;
    221 	}
    222 	/* If variable resolution, set to max */
    223 	if (sc->sc_resolution < 0) {
    224 		sc->sc_resolution = ~sc->sc_resolution;
    225 		error = sdtemp_write_16(sc, SDTEMP_REG_RESOLUTION,
    226 					sc->sc_resolution & 0x3);
    227 		if (error != 0) {
    228 			iic_release_bus(sc->sc_tag, 0);
    229 			aprint_error_dev(self,
    230 			    "error %d writing resolution register\n", error);
    231 			return;
    232 		} else
    233 			sc->sc_resolution++;
    234 	}
    235 	iic_release_bus(sc->sc_tag, 0);
    236 
    237 	/* Hook us into the sysmon_envsys subsystem */
    238 	sc->sc_sme = sysmon_envsys_create();
    239 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
    240 	if (!sc->sc_sensor) {
    241 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
    242 		goto bad2;
    243 	}
    244 
    245 	/* Initialize sensor data. */
    246 	sc->sc_sensor->units =  ENVSYS_STEMP;
    247 	sc->sc_sensor->state = ENVSYS_SINVALID;
    248 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
    249 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
    250 	    sizeof(sc->sc_sensor->desc));
    251 
    252 	/* Now attach the sensor */
    253 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
    254 		aprint_error_dev(self, "unable to attach sensor\n");
    255 		goto bad;
    256 	}
    257 
    258 	/* Register the device */
    259 	sc->sc_sme->sme_name = device_xname(self);
    260 	sc->sc_sme->sme_cookie = sc;
    261 	sc->sc_sme->sme_refresh = sdtemp_refresh;
    262 
    263 	error = sysmon_envsys_register(sc->sc_sme);
    264 	if (error) {
    265 		aprint_error_dev(self, "error %d registering with sysmon\n",
    266 		    error);
    267 		goto bad;
    268 	}
    269 
    270 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
    271 		aprint_error_dev(self, "couldn't establish power handler\n");
    272 
    273 
    274 	/* Retrieve and display hardware monitor limits */
    275 	i = 0;
    276 	aprint_normal_dev(self, "");
    277 	iic_acquire_bus(sc->sc_tag, 0);
    278 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &sc->sc_low_lim) == 0 &&
    279 	    sc->sc_low_lim != 0) {
    280 		sc->sc_low_lim >>= 4;
    281 		aprint_normal("low limit %dC ", sc->sc_low_lim);
    282 		i++;
    283 	}
    284 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &sc->sc_high_lim) == 0 &&
    285 	    sc->sc_high_lim != 0) {
    286 		sc->sc_high_lim >>= 4;
    287 		aprint_normal("high limit %dC ", sc->sc_high_lim);
    288 		i++;
    289 	}
    290 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &sc->sc_crit_lim) == 0 &&
    291 	    sc->sc_crit_lim != 0) {
    292 		sc->sc_crit_lim >>= 4;
    293 		aprint_normal("critical limit %dC ", sc->sc_crit_lim);
    294 		i++;
    295 	}
    296 	iic_release_bus(sc->sc_tag, 0);
    297 	if (i == 0)
    298 		aprint_normal("no hardware limits set\n");
    299 	else
    300 		aprint_normal("\n");
    301 
    302 	/* Create our sysctl tree.  We just store the softc pointer for
    303 	 * now;  the sysctl_helper function will take care of creating
    304 	 * a real string on the fly.  We explicitly specify the new nodes'
    305 	 * sysctl_num in order to identify the specific limit rather than
    306 	 * using CTL_CREATE;  this is OK since we're the only place that
    307 	 * touches the sysctl tree for the device.
    308 	 */
    309 
    310 	if (hw_node != CTL_EOL)
    311 		sysctl_createv(NULL, 0, NULL, &node, 0,
    312 		    CTLTYPE_NODE, device_xname(self),
    313 		    NULL, NULL, 0, NULL, 0,
    314 		    CTL_HW, CTL_CREATE, CTL_EOL);
    315 	if (node != NULL) {
    316 		dev_sysctl_num = node->sysctl_num;
    317 		sysctl_createv(NULL, 0, NULL, &node, 0,
    318 		    CTLTYPE_NODE, "limits",
    319 		    SYSCTL_DESCR("temperature limits"),
    320 		    NULL, 0, NULL, 0,
    321 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
    322 	}
    323 	if (node != NULL) {
    324 		sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READWRITE,
    325 		    CTLTYPE_INT, "low_limit",
    326 		    SYSCTL_DESCR("alarm window lower limit"),
    327 		    sdtemp_sysctl_helper, 0, sc, sizeof(int),
    328 		    CTL_HW, dev_sysctl_num, node->sysctl_num,
    329 			SDTEMP_REG_LOWER_LIM, CTL_EOL);
    330 		sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READWRITE,
    331 		    CTLTYPE_INT, "high_limit",
    332 		    SYSCTL_DESCR("alarm window upper limit"),
    333 		    sdtemp_sysctl_helper, 0, sc, sizeof(int),
    334 		    CTL_HW, dev_sysctl_num, node->sysctl_num,
    335 			SDTEMP_REG_UPPER_LIM, CTL_EOL);
    336 		sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READWRITE,
    337 		    CTLTYPE_INT, "crit_limit",
    338 		    SYSCTL_DESCR("critical alarm limit"),
    339 		    sdtemp_sysctl_helper, 0, sc, sizeof(int),
    340 		    CTL_HW, dev_sysctl_num, node->sysctl_num,
    341 			SDTEMP_REG_CRIT_LIM, CTL_EOL);
    342 	}
    343 	return;
    344 
    345 bad:
    346 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    347 bad2:
    348 	sysmon_envsys_destroy(sc->sc_sme);
    349 }
    350 
    351 /* Set up the threshold registers */
    352 static void
    353 sdtemp_set_thresh(struct sdtemp_softc *sc, int reg, uint16_t val)
    354 {
    355 	int error;
    356 	uint16_t *valp;
    357 
    358 	switch (reg) {
    359 	case SDTEMP_REG_LOWER_LIM:
    360 		valp = &sc->sc_low_lim;
    361 		break;
    362 	case SDTEMP_REG_UPPER_LIM:
    363 		valp = &sc->sc_high_lim;
    364 		break;
    365 	case SDTEMP_REG_CRIT_LIM:
    366 		valp = &sc->sc_crit_lim;
    367 		break;
    368 	default:
    369 		return;
    370 	}
    371 
    372 	iic_acquire_bus(sc->sc_tag, 0);
    373 	error = sdtemp_write_16(sc, reg, (val << 4) & SDTEMP_TEMP_MASK);
    374 	iic_release_bus(sc->sc_tag, 0);
    375 
    376 	if (error == 0)
    377 		*valp = val;
    378 }
    379 
    380 #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
    381 
    382 /* Read a 8-bit value from a register */
    383 static int
    384 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
    385 {
    386 	int error;
    387 
    388 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    389 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    390 
    391 	return error;
    392 }
    393 
    394 static int
    395 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
    396 {
    397 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    398 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
    399 }
    400 #endif /* NOT_YET */
    401 
    402 /* Read a 16-bit value from a register */
    403 static int
    404 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
    405 {
    406 	int error;
    407 
    408 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    409 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    410 	if (error)
    411 		return error;
    412 
    413 	*valp = be16toh(*valp);
    414 
    415 	return 0;
    416 }
    417 
    418 static int
    419 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
    420 {
    421 	uint16_t temp;
    422 
    423 	temp = htobe16(val);
    424 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    425 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
    426 }
    427 
    428 static uint32_t
    429 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
    430 {
    431 	uint32_t val;
    432 	int32_t stemp;
    433 
    434 	/* Get only the temperature bits */
    435 	temp &= SDTEMP_TEMP_MASK;
    436 
    437 	/* If necessary, extend the sign bit */
    438 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
    439 	    (temp & SDTEMP_TEMP_NEGATIVE))
    440 		temp |= SDTEMP_TEMP_SIGN_EXT;
    441 
    442 	/* Mask off only bits valid within current resolution */
    443 	temp &= ~(0xf >> sc->sc_resolution);
    444 
    445 	/* Treat as signed and extend to 32-bits */
    446 	stemp = (int16_t)temp;
    447 
    448 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
    449 	val = (stemp * 62500) + 273150000;
    450 
    451 	return val;
    452 }
    453 
    454 static void
    455 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    456 {
    457 	struct sdtemp_softc *sc = sme->sme_cookie;
    458 	uint16_t val;
    459 	int error;
    460 
    461 	iic_acquire_bus(sc->sc_tag, 0);
    462 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
    463 	iic_release_bus(sc->sc_tag, 0);
    464 
    465 	if (error) {
    466 		edata->state = ENVSYS_SINVALID;
    467 		return;
    468 	}
    469 
    470 	edata->value_cur = sdtemp_decode_temp(sc, val);
    471 
    472 	/* Now check for limits */
    473 	if (val & SDTEMP_ABOVE_CRIT)
    474 		edata->state = ENVSYS_SCRITOVER;
    475 	else if (val & SDTEMP_ABOVE_UPPER)
    476 		edata->state = ENVSYS_SWARNOVER;
    477 	else if (val & SDTEMP_BELOW_LOWER)
    478 		edata->state = ENVSYS_SWARNUNDER;
    479 	else
    480 		edata->state = ENVSYS_SVALID;
    481 }
    482 
    483 SYSCTL_SETUP(sysctl_sdtemp_setup, "sysctl hw.sdtemp subtree setup")
    484 {
    485 	const struct sysctlnode *node;
    486 
    487 	if (sysctl_createv(clog, 0, NULL, &node,
    488 			   CTLFLAG_PERMANENT,
    489 			   CTLTYPE_NODE, "hw", NULL,
    490 			   NULL, 0, NULL, 0,
    491 			   CTL_HW, CTL_EOL) != 0)
    492 		return;
    493 
    494 	hw_node = node->sysctl_num;
    495 }
    496 
    497 /*
    498  * The sysctl node actually contains just a pointer to our softc.  We
    499  * extract the individual limits on the fly, and if necessary replace
    500  * the value with the new value specified by the user.
    501  *
    502  * Inspired by similar code in sys/net/if_tap.c
    503  */
    504 static int
    505 sdtemp_sysctl_helper(SYSCTLFN_ARGS)
    506 {
    507 	struct sdtemp_softc *sc;
    508 	struct sysctlnode node;
    509 	int error, reg;
    510 	uint16_t reg_value;
    511 	int lim_value;
    512 
    513 	node = *rnode;
    514 	sc = node.sysctl_data;
    515 	reg = node.sysctl_num;
    516 
    517 	iic_acquire_bus(sc->sc_tag, 0);
    518 	error = sdtemp_read_16(sc, reg, &reg_value);
    519 	iic_release_bus(sc->sc_tag, 0);
    520 
    521 #ifdef DEBUG
    522 	aprint_verbose_dev(sc->sc_dev, "(%s) sc %p reg %d val 0x%04x err %d\n",
    523 	    __func__, sc, reg, reg_value, error);
    524 #endif
    525 
    526 	if (error == 0) {
    527 		lim_value = reg_value >> 4;
    528 		node.sysctl_data = &lim_value;
    529 		error = sysctl_lookup(SYSCTLFN_CALL(&node));
    530 	}
    531 	if (error || newp == NULL)
    532 		return (error);
    533 
    534 	/*
    535 	 * We're being asked to update the sysctl value, so retrieve
    536 	 * the new value and check for valid range
    537 	 */
    538 	lim_value = *(int *)node.sysctl_data;
    539 	if (lim_value < -256 || lim_value > 255)
    540 		return (EINVAL);
    541 
    542 	sdtemp_set_thresh(sc, reg, (uint16_t)lim_value);
    543 
    544 	return (0);
    545 }
    546 
    547 /*
    548  * power management functions
    549  *
    550  * We go into "shutdown" mode at suspend time, and return to normal
    551  * mode upon resume.  This reduces power consumption by disabling
    552  * the A/D converter.
    553  */
    554 
    555 static bool
    556 sdtemp_pmf_suspend(device_t dev PMF_FN_ARGS)
    557 {
    558 	struct sdtemp_softc *sc = device_private(dev);
    559 	int error;
    560 	uint16_t config;
    561 
    562 	iic_acquire_bus(sc->sc_tag, 0);
    563 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    564 	if (error == 0) {
    565 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
    566 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    567 	}
    568 	iic_release_bus(sc->sc_tag, 0);
    569 	return (error == 0);
    570 }
    571 
    572 static bool
    573 sdtemp_pmf_resume(device_t dev PMF_FN_ARGS)
    574 {
    575 	struct sdtemp_softc *sc = device_private(dev);
    576 	int error;
    577 	uint16_t config;
    578 
    579 	iic_acquire_bus(sc->sc_tag, 0);
    580 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    581 	if (error == 0) {
    582 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
    583 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    584 	}
    585 	iic_release_bus(sc->sc_tag, 0);
    586 	return (error == 0);
    587 }
    588