Home | History | Annotate | Line # | Download | only in i2c
lm75.c revision 1.28
      1 /*	$NetBSD: lm75.c,v 1.28 2016/01/03 17:27:57 jdc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: lm75.c,v 1.28 2016/01/03 17:27:57 jdc Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 #include <sys/kernel.h>
     45 #include <sys/sysctl.h>
     46 
     47 #include <dev/sysmon/sysmonvar.h>
     48 
     49 #include <dev/i2c/i2cvar.h>
     50 #include <dev/i2c/lm75reg.h>
     51 
     52 struct lmtemp_softc {
     53 	device_t sc_dev;
     54 	i2c_tag_t sc_tag;
     55 	int sc_address;
     56 
     57 	struct sysmon_envsys *sc_sme;
     58 	envsys_data_t sc_sensor;
     59 	int sc_tmax;
     60 	uint32_t sc_smax, sc_smin, sc_scrit;
     61 
     62 	uint32_t (*sc_lmtemp_decode)(const uint8_t *, int);
     63 	void (*sc_lmtemp_encode)(const uint32_t, uint8_t *, int);
     64 };
     65 
     66 static int  lmtemp_match(device_t, cfdata_t, void *);
     67 static void lmtemp_attach(device_t, device_t, void *);
     68 
     69 CFATTACH_DECL_NEW(lmtemp, sizeof(struct lmtemp_softc),
     70 	lmtemp_match, lmtemp_attach, NULL, NULL);
     71 
     72 static void	lmtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     73 static int	lmtemp_config_write(struct lmtemp_softc *, uint8_t);
     74 static int	lmtemp_temp_write(struct lmtemp_softc *, uint8_t, uint32_t,
     75 				int);
     76 static int	lmtemp_temp_read(struct lmtemp_softc *, uint8_t, uint32_t *,
     77 				int);
     78 static uint32_t lmtemp_decode_lm75(const uint8_t *, int);
     79 static uint32_t lmtemp_decode_ds75(const uint8_t *, int);
     80 static uint32_t lmtemp_decode_lm77(const uint8_t *, int);
     81 static void	lmtemp_encode_lm75(const uint32_t, uint8_t *, int);
     82 static void	lmtemp_encode_ds75(const uint32_t, uint8_t *, int);
     83 static void	lmtemp_encode_lm77(const uint32_t, uint8_t *, int);
     84 static void	lmtemp_getlim_lm75(struct sysmon_envsys *, envsys_data_t *,
     85 				sysmon_envsys_lim_t *, uint32_t *);
     86 static void	lmtemp_getlim_lm77(struct sysmon_envsys *, envsys_data_t *,
     87 				sysmon_envsys_lim_t *, uint32_t *);
     88 static void	lmtemp_setlim_lm75(struct sysmon_envsys *, envsys_data_t *,
     89 				sysmon_envsys_lim_t *, uint32_t *);
     90 static void	lmtemp_setlim_lm77(struct sysmon_envsys *, envsys_data_t *,
     91 				sysmon_envsys_lim_t *, uint32_t *);
     92 
     93 static void	lmtemp_setup_sysctl(struct lmtemp_softc *);
     94 static int	sysctl_lm75_temp(SYSCTLFN_ARGS);
     95 
     96 static const char * lmtemp_compats[] = {
     97 	"i2c-lm75",
     98 	/*
     99 	 * see XXX in _attach() below: add code once non-lm75 matches are
    100 	 * added here!
    101 	 */
    102 	NULL
    103 };
    104 
    105 enum {
    106 	lmtemp_lm75 = 0,
    107 	lmtemp_ds75,
    108 	lmtemp_lm77,
    109 };
    110 static const struct {
    111 	int lmtemp_type;
    112 	const char *lmtemp_name;
    113 	int lmtemp_addrmask;
    114 	int lmtemp_addr;
    115 	uint32_t (*lmtemp_decode)(const uint8_t *, int);
    116 	void (*lmtemp_encode)(const uint32_t, uint8_t *, int);
    117 	void (*lmtemp_getlim)(struct sysmon_envsys *, envsys_data_t *,
    118 		sysmon_envsys_lim_t *, uint32_t *);
    119 	void (*lmtemp_setlim)(struct sysmon_envsys *, envsys_data_t *,
    120 		sysmon_envsys_lim_t *, uint32_t *);
    121 } lmtemptbl[] = {
    122 	{ lmtemp_lm75,	"LM75",	LM75_ADDRMASK,	LM75_ADDR,
    123 	    lmtemp_decode_lm75,	lmtemp_encode_lm75,
    124 	    lmtemp_getlim_lm75,	lmtemp_setlim_lm75 },
    125 	{ lmtemp_ds75,	"DS75",	LM75_ADDRMASK,	LM75_ADDR,
    126 	    lmtemp_decode_ds75,	lmtemp_encode_ds75,
    127 	    lmtemp_getlim_lm75,	lmtemp_setlim_lm75 },
    128 	{ lmtemp_lm77,	"LM77",	LM77_ADDRMASK,	LM77_ADDR,
    129 	    lmtemp_decode_lm77, lmtemp_encode_lm77,
    130 	    lmtemp_getlim_lm77,	lmtemp_setlim_lm77 },
    131 	{ -1,		NULL,	 0,		0,
    132 	    NULL,		NULL,
    133 	    NULL,		NULL }
    134 };
    135 
    136 static int
    137 lmtemp_match(device_t parent, cfdata_t cf, void *aux)
    138 {
    139 	struct i2c_attach_args *ia = aux;
    140 	int i;
    141 
    142 	if (ia->ia_name == NULL) {
    143 		/*
    144 		 * Indirect config - not much we can do!
    145 		 */
    146 		for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    147 			if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
    148 				break;
    149 		if (lmtemptbl[i].lmtemp_type == -1)
    150 			return 0;
    151 
    152 		if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
    153 		    lmtemptbl[i].lmtemp_addr)
    154 			return 1;
    155 	} else {
    156 		/*
    157 		 * Direct config - match via the list of compatible
    158 		 * hardware or simply match the device name.
    159 		 */
    160 		if (ia->ia_ncompat > 0) {
    161 			if (iic_compat_match(ia, lmtemp_compats))
    162 				return 1;
    163 		} else {
    164 			if (strcmp(ia->ia_name, "lmtemp") == 0)
    165 				return 1;
    166 		}
    167 	}
    168 
    169 
    170 	return 0;
    171 }
    172 
    173 static void
    174 lmtemp_attach(device_t parent, device_t self, void *aux)
    175 {
    176 	struct lmtemp_softc *sc = device_private(self);
    177 	struct i2c_attach_args *ia = aux;
    178 	int i;
    179 
    180 	sc->sc_dev = self;
    181 	if (ia->ia_name == NULL) {
    182 		for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    183 			if (lmtemptbl[i].lmtemp_type ==
    184 			    device_cfdata(self)->cf_flags)
    185 				break;
    186 	} else {
    187 		/* XXX - add code when adding other direct matches! */
    188 		i = 0;
    189 	}
    190 
    191 	sc->sc_tag = ia->ia_tag;
    192 	sc->sc_address = ia->ia_addr;
    193 
    194 	aprint_naive(": Temperature Sensor\n");
    195 	if (ia->ia_name) {
    196 		aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
    197 			lmtemptbl[i].lmtemp_name);
    198 	} else {
    199 		aprint_normal(": %s Temperature Sensor\n",
    200 			lmtemptbl[i].lmtemp_name);
    201 	}
    202 
    203 	sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
    204 	sc->sc_lmtemp_encode = lmtemptbl[i].lmtemp_encode;
    205 
    206 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    207 
    208 	/* Read temperature limit(s) and remember initial value(s). */
    209 	if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &sc->sc_smax, 1)
    210 	    != 0) {
    211 		aprint_error_dev(self, "unable to read Tos register\n");
    212 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    213 		return;
    214 	}
    215 	sc->sc_tmax = sc->sc_smax;
    216 	if (i == lmtemp_lm77) {
    217 		if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT,
    218 		    &sc->sc_smax, 1) != 0) {
    219 			aprint_error_dev(self,
    220 			    "unable to read low register\n");
    221 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    222 			return;
    223 		}
    224 		if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT,
    225 		    &sc->sc_smax, 1) != 0) {
    226 			aprint_error_dev(self,
    227 			    "unable to read high register\n");
    228 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    229 			return;
    230 		}
    231 	}
    232 
    233 	if (i == lmtemp_lm75)
    234 		lmtemp_setup_sysctl(sc);
    235 
    236 	/* Set the configuration of the LM75 to defaults. */
    237 	if (lmtemp_config_write(sc, LM75_CONFIG_FAULT_QUEUE_4) != 0) {
    238 		aprint_error_dev(self, "unable to write config register\n");
    239 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    240 		return;
    241 	}
    242 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    243 
    244 	sc->sc_sme = sysmon_envsys_create();
    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,
    250 	    ia->ia_name? ia->ia_name : device_xname(self),
    251 	    sizeof(sc->sc_sensor.desc));
    252 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    253 		sysmon_envsys_destroy(sc->sc_sme);
    254 		return;
    255 	}
    256 
    257 	/* Hook into system monitor. */
    258 	sc->sc_sme->sme_name = device_xname(self);
    259 	sc->sc_sme->sme_cookie = sc;
    260 	sc->sc_sme->sme_refresh = lmtemp_refresh;
    261 	sc->sc_sme->sme_get_limits = lmtemptbl[i].lmtemp_getlim;
    262 	sc->sc_sme->sme_set_limits = lmtemptbl[i].lmtemp_setlim;
    263 
    264 	if (sysmon_envsys_register(sc->sc_sme)) {
    265 		aprint_error_dev(self, "unable to register with sysmon\n");
    266 		sysmon_envsys_destroy(sc->sc_sme);
    267 	}
    268 }
    269 
    270 static int
    271 lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
    272 {
    273 	uint8_t cmdbuf[2];
    274 
    275 	cmdbuf[0] = LM75_REG_CONFIG;
    276 	cmdbuf[1] = val;
    277 
    278 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    279 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL);
    280 }
    281 
    282 static int
    283 lmtemp_temp_write(struct lmtemp_softc *sc, uint8_t reg, uint32_t val, int degc)
    284 {
    285 	uint8_t cmdbuf[3];
    286 
    287 	cmdbuf[0] = reg;
    288 	sc->sc_lmtemp_encode(val, &cmdbuf[1], degc);
    289 
    290 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    291 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 2, I2C_F_POLL);
    292 }
    293 
    294 static int
    295 lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp,
    296     int degc)
    297 {
    298 	int error;
    299 	uint8_t cmdbuf[1];
    300 	uint8_t buf[LM75_TEMP_LEN];
    301 
    302 	cmdbuf[0] = which;
    303 
    304 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    305 	    sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
    306 	if (error)
    307 		return error;
    308 
    309 	*valp = sc->sc_lmtemp_decode(buf, degc);
    310 	return 0;
    311 }
    312 
    313 static void
    314 lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
    315 {
    316 	uint32_t val;
    317 	int error;
    318 
    319 	error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val, 0);
    320 	if (error) {
    321 #if 0
    322 		aprint_error_dev(sc->sc_dev, "unable to read temperature, error = %d\n",
    323 		    error);
    324 #endif
    325 		sc->sc_sensor.state = ENVSYS_SINVALID;
    326 		return;
    327 	}
    328 
    329 	sc->sc_sensor.value_cur = val;
    330 	sc->sc_sensor.state = ENVSYS_SVALID;
    331 }
    332 
    333 static void
    334 lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    335 {
    336 	struct lmtemp_softc *sc = sme->sme_cookie;
    337 
    338 	iic_acquire_bus(sc->sc_tag, 0);	/* also locks our instance */
    339 	lmtemp_refresh_sensor_data(sc);
    340 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    341 }
    342 
    343 static void
    344 lmtemp_getlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
    345     sysmon_envsys_lim_t *limits, uint32_t *props)
    346 {
    347 	struct lmtemp_softc *sc = sme->sme_cookie;
    348 	uint32_t val;
    349 
    350 	*props &= ~(PROP_CRITMAX);
    351 
    352 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    353 	if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &val, 0) == 0) {
    354 		limits->sel_critmax = val;
    355 		*props |= PROP_CRITMAX;
    356 	}
    357 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    358 }
    359 
    360 static void
    361 lmtemp_getlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
    362     sysmon_envsys_lim_t *limits, uint32_t *props)
    363 {
    364 	struct lmtemp_softc *sc = sme->sme_cookie;
    365 	uint32_t val;
    366 
    367 	*props &= ~(PROP_CRITMAX | PROP_WARNMAX | PROP_WARNMIN);
    368 
    369 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    370 	if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT, &val, 0) == 0) {
    371 		limits->sel_critmax = val;
    372 		*props |= PROP_CRITMAX;
    373 	}
    374 	if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT, &val, 0) == 0) {
    375 		limits->sel_warnmax = val;
    376 		*props |= PROP_WARNMAX;
    377 	}
    378 	if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT, &val, 0) == 0) {
    379 		limits->sel_warnmin = val;
    380 		*props |= PROP_WARNMIN;
    381 	}
    382 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    383 }
    384 
    385 static void
    386 lmtemp_setlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
    387     sysmon_envsys_lim_t *limits, uint32_t *props)
    388 {
    389 	struct lmtemp_softc *sc = sme->sme_cookie;
    390 	int32_t limit;
    391 
    392 	if (*props & PROP_CRITMAX) {
    393 		if (limits == NULL)	/* Restore defaults */
    394 			limit = sc->sc_smax;
    395 		else
    396 			limit = limits->sel_critmax;
    397 		iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    398 		lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
    399 		    limit - 5000000, 0);
    400 		lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT, limit, 0);
    401 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    402 
    403 		/* Synchronise sysctl */
    404 		sc->sc_tmax = (limit - 273150000) / 1000000;
    405 	}
    406 }
    407 
    408 static void
    409 lmtemp_setlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
    410     sysmon_envsys_lim_t *limits, uint32_t *props)
    411 {
    412 	struct lmtemp_softc *sc = sme->sme_cookie;
    413 	int32_t limit;
    414 
    415 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    416 	if (*props & PROP_CRITMAX) {
    417 		if (limits == NULL)	/* Restore defaults */
    418 			limit = sc->sc_smax;
    419 		else
    420 			limit = limits->sel_critmax;
    421 		lmtemp_temp_write(sc, LM77_REG_TCRIT_SET_POINT, limit, 0);
    422 	}
    423 	if (*props & PROP_WARNMAX) {
    424 		if (limits == NULL)	/* Restore defaults */
    425 			limit = sc->sc_smax;
    426 		else
    427 			limit = limits->sel_warnmax;
    428 		lmtemp_temp_write(sc, LM77_REG_THIGH_SET_POINT, limit, 0);
    429 	}
    430 	if (*props & PROP_WARNMIN) {
    431 		if (limits == NULL)	/* Restore defaults */
    432 			limit = sc->sc_smax;
    433 		else
    434 			limit = limits->sel_warnmin;
    435 		lmtemp_temp_write(sc, LM77_REG_TLOW_SET_POINT, limit, 0);
    436 	}
    437 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    438 }
    439 
    440 static uint32_t
    441 lmtemp_decode_lm75(const uint8_t *buf, int degc)
    442 {
    443 	int temp;
    444 	uint32_t val;
    445 
    446 	/*
    447 	 * LM75 temps are the most-significant 9 bits of a 16-bit reg.
    448 	 * sign-extend the MSB and add in the 0.5 from the LSB
    449 	 */
    450 	temp = (int8_t) buf[0];
    451 	temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
    452 
    453 	/* Temp is given in 1/2 deg. C, we convert to C or uK. */
    454 	if (degc)
    455 		val = temp / 2;
    456 	else
    457 		val = temp * 500000 + 273150000;
    458 
    459 	return val;
    460 }
    461 
    462 static uint32_t
    463 lmtemp_decode_ds75(const uint8_t *buf, int degc)
    464 {
    465 	int temp;
    466 
    467 	/*
    468 	 * Sign-extend the MSB byte, and add in the fractions of a
    469 	 * degree contained in the LSB (precision 1/16th DegC).
    470 	 */
    471 	temp = (int8_t)buf[0];
    472 	temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
    473 
    474 	/*
    475 	 * Conversion to C or uK is simple.
    476 	 */
    477 	if (degc)
    478 		return temp / 16;
    479 	else
    480 		return (temp * 62500 + 273150000);
    481 }
    482 
    483 static uint32_t
    484 lmtemp_decode_lm77(const uint8_t *buf, int degc)
    485 {
    486 	int temp;
    487 	uint32_t val;
    488 
    489 	/*
    490 	 * Describe each bits of temperature registers on LM77.
    491 	 *   D15 - D12:	Sign
    492 	 *   D11 - D3 :	Bit8(MSB) - Bit0
    493 	 */
    494 	temp = (int8_t)buf[0];
    495 	temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
    496 
    497 	/* Temp is given in 1/2 deg. C, we convert to C or uK. */
    498 	if (degc)
    499 		val = temp / 2;
    500 	else
    501 		val = temp * 500000 + 273150000;
    502 
    503 	return val;
    504 }
    505 
    506 static void lmtemp_encode_lm75(const uint32_t val, uint8_t *buf, int degc)
    507 {
    508 	int temp;
    509 
    510 	/* Convert from C or uK to register format */
    511 	if (degc)
    512 		temp = val * 2;
    513 	else
    514 		temp = (val - 273150000) / 500000;
    515 	buf[0] = (temp >> 1) & 0xff;
    516 	buf[1] = (temp & 1) << 7;
    517 }
    518 
    519 static void lmtemp_encode_ds75(const uint32_t val, uint8_t *buf, int degc)
    520 {
    521 	int temp;
    522 
    523 	/* Convert from C or uK to register format */
    524 	if (degc)
    525 		temp = val * 16;
    526 	else
    527 		temp = (val - 273150000) / 62500;
    528 	buf[0] = (temp >> 4) & 0xff;
    529 	buf[1] = (temp & 0xf) << 4;
    530 }
    531 
    532 static void lmtemp_encode_lm77(const uint32_t val, uint8_t *buf, int degc)
    533 {
    534 	int temp;
    535 
    536 	/* Convert from C or uK to register format */
    537 	if (degc)
    538 		temp = val * 2;
    539 	else
    540 		temp = (val - 273150000) / 500000;
    541 	buf[0] = (temp >> 5) & 0xff;
    542 	buf[1] = (temp & 0x1f) << 3;
    543 }
    544 
    545 static void
    546 lmtemp_setup_sysctl(struct lmtemp_softc *sc)
    547 {
    548 	const struct sysctlnode *me = NULL, *node = NULL;
    549 
    550 	sysctl_createv(NULL, 0, NULL, &me,
    551 	    CTLFLAG_READWRITE,
    552 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    553 	    NULL, 0, NULL, 0,
    554 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    555 
    556 	sysctl_createv(NULL, 0, NULL, &node,
    557 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    558 	    CTLTYPE_INT, "temp", "Threshold temperature",
    559 	    sysctl_lm75_temp, 1, (void *)sc, 0,
    560 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    561 }
    562 
    563 static int
    564 sysctl_lm75_temp(SYSCTLFN_ARGS)
    565 {
    566 	struct sysctlnode node = *rnode;
    567 	struct lmtemp_softc *sc = node.sysctl_data;
    568 	int temp;
    569 
    570 	if (newp) {
    571 
    572 		/* we're asked to write */
    573 		node.sysctl_data = &sc->sc_tmax;
    574 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    575 
    576 			temp = *(int *)node.sysctl_data;
    577 			sc->sc_tmax = temp;
    578 			iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    579 			lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
    580 			    sc->sc_tmax - 5, 1);
    581 			lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT,
    582 			    sc->sc_tmax, 1);
    583 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    584 
    585 			/* Synchronise envsys - calls lmtemp_getlim_lm75() */
    586 			sysmon_envsys_update_limits(sc->sc_sme, &sc->sc_sensor);
    587 			return 0;
    588 		}
    589 		return EINVAL;
    590 	} else {
    591 
    592 		node.sysctl_data = &sc->sc_tmax;
    593 		node.sysctl_size = 4;
    594 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    595 	}
    596 
    597 	return 0;
    598 }
    599 
    600 SYSCTL_SETUP(sysctl_lmtemp_setup, "sysctl lmtemp subtree setup")
    601 {
    602 
    603 	sysctl_createv(NULL, 0, NULL, NULL,
    604 		       CTLFLAG_PERMANENT,
    605 		       CTLTYPE_NODE, "machdep", NULL,
    606 		       NULL, 0, NULL, 0,
    607 		       CTL_MACHDEP, CTL_EOL);
    608 }
    609 
    610 
    611