Home | History | Annotate | Line # | Download | only in i2c
      1 /*	$NetBSD: lm75.c,v 1.49 2025/09/21 13:54:56 thorpej 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.49 2025/09/21 13:54:56 thorpej 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 enum {
     97 	lmtemp_lm75 = 0,
     98 	lmtemp_ds75 = 1,
     99 	lmtemp_lm77 = 2,
    100 };
    101 
    102 static const struct device_compatible_entry compat_data[] = {
    103 	{ .compat = "national,lm75",	.value = lmtemp_lm75 },
    104 	{ .compat = "i2c-lm75",		.value = lmtemp_lm75 },
    105 	{ .compat = "lm75",		.value = lmtemp_lm75 },
    106 
    107 	/* XXX Linux treats ds1775 and ds75 differently. */
    108 	{ .compat = "dallas,ds1775",	.value = lmtemp_ds75 },
    109 	{ .compat = "ds1775",		.value = lmtemp_ds75 },
    110 
    111 	{ .compat = "national,lm77",	.value = lmtemp_lm77 },
    112 
    113 	/*
    114 	 * see XXX in _attach() below: add code once non-lm75 matches are
    115 	 * added here!
    116 	 */
    117 	DEVICE_COMPAT_EOL
    118 };
    119 
    120 static const struct {
    121 	const char *lmtemp_name;
    122 	int lmtemp_addrmask;
    123 	int lmtemp_addr;
    124 	uint32_t (*lmtemp_decode)(const uint8_t *, int);
    125 	void (*lmtemp_encode)(const uint32_t, uint8_t *, int);
    126 	void (*lmtemp_getlim)(struct sysmon_envsys *, envsys_data_t *,
    127 		sysmon_envsys_lim_t *, uint32_t *);
    128 	void (*lmtemp_setlim)(struct sysmon_envsys *, envsys_data_t *,
    129 		sysmon_envsys_lim_t *, uint32_t *);
    130 } lmtemptbl[] = {
    131 [lmtemp_lm75] =
    132 	{
    133 		.lmtemp_name = "LM75",
    134 		.lmtemp_addrmask = LM75_ADDRMASK,
    135 		.lmtemp_addr = LM75_ADDR,
    136 		.lmtemp_decode = lmtemp_decode_lm75,
    137 		.lmtemp_encode = lmtemp_encode_lm75,
    138 		.lmtemp_getlim = lmtemp_getlim_lm75,
    139 		.lmtemp_setlim = lmtemp_setlim_lm75,
    140 	},
    141 [lmtemp_ds75] =
    142 	{
    143 		.lmtemp_name = "DS75",
    144 		.lmtemp_addrmask = LM75_ADDRMASK,
    145 		.lmtemp_addr = LM75_ADDR,
    146 		.lmtemp_decode = lmtemp_decode_ds75,
    147 		.lmtemp_encode = lmtemp_encode_ds75,
    148 		.lmtemp_getlim = lmtemp_getlim_lm75,
    149 		.lmtemp_setlim = lmtemp_setlim_lm75,
    150 	},
    151 [lmtemp_lm77] =
    152 	{
    153 		.lmtemp_name = "LM77",
    154 		.lmtemp_addrmask = LM77_ADDRMASK,
    155 		.lmtemp_addr = LM77_ADDR,
    156 		.lmtemp_decode = lmtemp_decode_lm77,
    157 		.lmtemp_encode = lmtemp_encode_lm77,
    158 		.lmtemp_getlim = lmtemp_getlim_lm77,
    159 		.lmtemp_setlim = lmtemp_setlim_lm77,
    160 	},
    161 };
    162 
    163 static int
    164 lmtemp_match(device_t parent, cfdata_t cf, void *aux)
    165 {
    166 	struct i2c_attach_args *ia = aux;
    167 	int i, match_result;
    168 
    169 	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
    170 		return match_result;
    171 
    172 	/*
    173 	 * Indirect config - not much we can do!
    174 	 */
    175 	for (i = 0; i < __arraycount(lmtemptbl); i++) {
    176 		if (i == cf->cf_flags) {
    177 			break;
    178 		}
    179 	}
    180 	if (i == __arraycount(lmtemptbl)) {
    181 		return 0;
    182 	}
    183 
    184 	if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
    185 	    lmtemptbl[i].lmtemp_addr)
    186 		return I2C_MATCH_ADDRESS_ONLY;
    187 
    188 	return 0;
    189 }
    190 
    191 static void
    192 lmtemp_attach(device_t parent, device_t self, void *aux)
    193 {
    194 	struct lmtemp_softc *sc = device_private(self);
    195 	struct i2c_attach_args *ia = aux;
    196 	const struct device_compatible_entry *dce;
    197 	char name[64];
    198 	const char *desc;
    199 	int i;
    200 	uint8_t config = LM75_CONFIG_FAULT_QUEUE_4;
    201 
    202 	sc->sc_dev = self;
    203 	dce = iic_compatible_lookup(ia, compat_data);
    204 	if (dce != NULL) {
    205 		i = (int)dce->value;
    206 	} else {
    207 		for (i = 0; i < __arraycount(lmtemptbl); i++) {
    208 			if (i == device_cfdata(self)->cf_flags) {
    209 				break;
    210 			}
    211 		}
    212 		KASSERT(i < __arraycount(lmtemptbl));
    213 	}
    214 
    215 	sc->sc_tag = ia->ia_tag;
    216 	sc->sc_address = ia->ia_addr;
    217 
    218 	aprint_naive(": Temperature Sensor\n");
    219 	if (ia->ia_name) {
    220 		aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
    221 			lmtemptbl[i].lmtemp_name);
    222 	} else {
    223 		aprint_normal(": %s Temperature Sensor\n",
    224 			lmtemptbl[i].lmtemp_name);
    225 	}
    226 
    227 	sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
    228 	sc->sc_lmtemp_encode = lmtemptbl[i].lmtemp_encode;
    229 
    230 	if (iic_acquire_bus(sc->sc_tag, 0)) {
    231 		aprint_error_dev(self,
    232 		    "unable to acquire I2C bus\n");
    233 		return;
    234 	}
    235 
    236 	/* Read temperature limit(s) and remember initial value(s). */
    237 	if (i == lmtemp_lm77) {
    238 		if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT,
    239 		    &sc->sc_scrit, 1) != 0) {
    240 			aprint_error_dev(self,
    241 			    "unable to read low register\n");
    242 			iic_release_bus(sc->sc_tag, 0);
    243 			return;
    244 		}
    245 		if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT,
    246 		    &sc->sc_smin, 1) != 0) {
    247 			aprint_error_dev(self,
    248 			    "unable to read low register\n");
    249 			iic_release_bus(sc->sc_tag, 0);
    250 			return;
    251 		}
    252 		if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT,
    253 		    &sc->sc_smax, 1) != 0) {
    254 			aprint_error_dev(self,
    255 			    "unable to read high register\n");
    256 			iic_release_bus(sc->sc_tag, 0);
    257 			return;
    258 		}
    259 	} else {	/* LM75 or compatible */
    260 		if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT,
    261 		    &sc->sc_smax, 1) != 0) {
    262 			aprint_error_dev(self, "unable to read Tos register\n");
    263 			iic_release_bus(sc->sc_tag, 0);
    264 			return;
    265 		}
    266 	}
    267 	sc->sc_tmax = sc->sc_smax;
    268 
    269 	if (i == lmtemp_lm75)
    270 		lmtemp_setup_sysctl(sc);
    271 
    272 	/* DS75 has better resolution */
    273 	if (i == lmtemp_ds75)
    274 		config = LM75_CONFIG_FAULT_QUEUE_4 | DS75_CONFIG_RES_11BIT;
    275 
    276 	/* Set the configuration of the LM75 to defaults. */
    277 	if (lmtemp_config_write(sc, config) != 0) {
    278 		aprint_error_dev(self, "unable to write config register\n");
    279 		iic_release_bus(sc->sc_tag, 0);
    280 		return;
    281 	}
    282 	iic_release_bus(sc->sc_tag, 0);
    283 
    284 	sc->sc_sme = sysmon_envsys_create();
    285 	/* Initialize sensor data. */
    286 	sc->sc_sensor.units = ENVSYS_STEMP;
    287 	sc->sc_sensor.state = ENVSYS_SINVALID;
    288 	sc->sc_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FHAS_ENTROPY;
    289 
    290 	(void)strlcpy(name,
    291 	    ia->ia_name? ia->ia_name : device_xname(self),
    292 	    sizeof(sc->sc_sensor.desc));
    293 
    294 	if (prop_dictionary_get_string(device_properties(self), "s00", &desc)) {
    295 		strncpy(name, desc, 64);
    296 	}
    297 
    298 	(void)strlcpy(sc->sc_sensor.desc, name,
    299 	    sizeof(sc->sc_sensor.desc));
    300 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    301 		sysmon_envsys_destroy(sc->sc_sme);
    302 		return;
    303 	}
    304 
    305 	/* Hook into system monitor. */
    306 	sc->sc_sme->sme_name = device_xname(self);
    307 	sc->sc_sme->sme_cookie = sc;
    308 	sc->sc_sme->sme_refresh = lmtemp_refresh;
    309 	sc->sc_sme->sme_get_limits = lmtemptbl[i].lmtemp_getlim;
    310 	sc->sc_sme->sme_set_limits = lmtemptbl[i].lmtemp_setlim;
    311 
    312 	if (sysmon_envsys_register(sc->sc_sme)) {
    313 		aprint_error_dev(self, "unable to register with sysmon\n");
    314 		sysmon_envsys_destroy(sc->sc_sme);
    315 	}
    316 }
    317 
    318 static int
    319 lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
    320 {
    321 	uint8_t cmdbuf[2];
    322 
    323 	cmdbuf[0] = LM75_REG_CONFIG;
    324 	cmdbuf[1] = val;
    325 
    326 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    327 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0);
    328 }
    329 
    330 static int
    331 lmtemp_temp_write(struct lmtemp_softc *sc, uint8_t reg, uint32_t val, int degc)
    332 {
    333 	uint8_t cmdbuf[3];
    334 
    335 	cmdbuf[0] = reg;
    336 	sc->sc_lmtemp_encode(val, &cmdbuf[1], degc);
    337 
    338 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    339 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 2, 0);
    340 }
    341 
    342 static int
    343 lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp,
    344     int degc)
    345 {
    346 	int error;
    347 	uint8_t cmdbuf[1];
    348 	uint8_t buf[LM75_TEMP_LEN];
    349 
    350 	cmdbuf[0] = which;
    351 
    352 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    353 	    sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
    354 	if (error)
    355 		return error;
    356 
    357 	*valp = sc->sc_lmtemp_decode(buf, degc);
    358 	return 0;
    359 }
    360 
    361 static void
    362 lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
    363 {
    364 	uint32_t val;
    365 	int error;
    366 
    367 	error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val, 0);
    368 	if (error) {
    369 #if 0
    370 		aprint_error_dev(sc->sc_dev, "unable to read temperature, error = %d\n",
    371 		    error);
    372 #endif
    373 		sc->sc_sensor.state = ENVSYS_SINVALID;
    374 		return;
    375 	}
    376 
    377 	sc->sc_sensor.value_cur = val;
    378 	sc->sc_sensor.state = ENVSYS_SVALID;
    379 }
    380 
    381 static void
    382 lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    383 {
    384 	struct lmtemp_softc *sc = sme->sme_cookie;
    385 
    386 	if (iic_acquire_bus(sc->sc_tag, 0))	/* also locks our instance */
    387 		return;
    388 	lmtemp_refresh_sensor_data(sc);
    389 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    390 }
    391 
    392 static void
    393 lmtemp_getlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
    394     sysmon_envsys_lim_t *limits, uint32_t *props)
    395 {
    396 	struct lmtemp_softc *sc = sme->sme_cookie;
    397 	uint32_t val;
    398 
    399 	*props &= ~(PROP_CRITMAX);
    400 
    401 	if (iic_acquire_bus(sc->sc_tag, 0))
    402 		return;
    403 	if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &val, 0) == 0) {
    404 		limits->sel_critmax = val;
    405 		*props |= PROP_CRITMAX;
    406 	}
    407 	iic_release_bus(sc->sc_tag, 0);
    408 }
    409 
    410 static void
    411 lmtemp_getlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
    412     sysmon_envsys_lim_t *limits, uint32_t *props)
    413 {
    414 	struct lmtemp_softc *sc = sme->sme_cookie;
    415 	uint32_t val;
    416 
    417 	*props &= ~(PROP_CRITMAX | PROP_WARNMAX | PROP_WARNMIN);
    418 
    419 	if (iic_acquire_bus(sc->sc_tag, 0))
    420 		return;
    421 	if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT, &val, 0) == 0) {
    422 		limits->sel_critmax = val;
    423 		*props |= PROP_CRITMAX;
    424 	}
    425 	if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT, &val, 0) == 0) {
    426 		limits->sel_warnmax = val;
    427 		*props |= PROP_WARNMAX;
    428 	}
    429 	if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT, &val, 0) == 0) {
    430 		limits->sel_warnmin = val;
    431 		*props |= PROP_WARNMIN;
    432 	}
    433 	iic_release_bus(sc->sc_tag, 0);
    434 }
    435 
    436 static void
    437 lmtemp_setlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
    438     sysmon_envsys_lim_t *limits, uint32_t *props)
    439 {
    440 	struct lmtemp_softc *sc = sme->sme_cookie;
    441 	int32_t limit;
    442 
    443 	if (*props & PROP_CRITMAX) {
    444 		if (limits == NULL)	/* Restore defaults */
    445 			limit = sc->sc_smax;
    446 		else
    447 			limit = limits->sel_critmax;
    448 		if (iic_acquire_bus(sc->sc_tag, 0))
    449 			return;
    450 		lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
    451 		    limit - 5000000, 0);
    452 		lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT, limit, 0);
    453 		iic_release_bus(sc->sc_tag, 0);
    454 
    455 		/* Synchronise sysctl */
    456 		sc->sc_tmax = (limit - 273150000) / 1000000;
    457 	}
    458 }
    459 
    460 static void
    461 lmtemp_setlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
    462     sysmon_envsys_lim_t *limits, uint32_t *props)
    463 {
    464 	struct lmtemp_softc *sc = sme->sme_cookie;
    465 	int32_t limit;
    466 
    467 	iic_acquire_bus(sc->sc_tag, 0);
    468 	if (*props & PROP_CRITMAX) {
    469 		if (limits == NULL)	/* Restore defaults */
    470 			limit = sc->sc_scrit;
    471 		else
    472 			limit = limits->sel_critmax;
    473 		lmtemp_temp_write(sc, LM77_REG_TCRIT_SET_POINT, limit, 0);
    474 	}
    475 	if (*props & PROP_WARNMAX) {
    476 		if (limits == NULL)	/* Restore defaults */
    477 			limit = sc->sc_smax;
    478 		else
    479 			limit = limits->sel_warnmax;
    480 		lmtemp_temp_write(sc, LM77_REG_THIGH_SET_POINT, limit, 0);
    481 	}
    482 	if (*props & PROP_WARNMIN) {
    483 		if (limits == NULL)	/* Restore defaults */
    484 			limit = sc->sc_smin;
    485 		else
    486 			limit = limits->sel_warnmin;
    487 		lmtemp_temp_write(sc, LM77_REG_TLOW_SET_POINT, limit, 0);
    488 	}
    489 	iic_release_bus(sc->sc_tag, 0);
    490 }
    491 
    492 static uint32_t
    493 lmtemp_decode_lm75(const uint8_t *buf, int degc)
    494 {
    495 	int temp;
    496 	uint32_t val;
    497 
    498 	/*
    499 	 * LM75 temps are the most-significant 9 bits of a 16-bit reg.
    500 	 * sign-extend the MSB and add in the 0.5 from the LSB
    501 	 */
    502 	temp = (int8_t) buf[0];
    503 	temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
    504 
    505 	/* Temp is given in 1/2 deg. C, we convert to C or uK. */
    506 	if (degc)
    507 		val = temp / 2;
    508 	else
    509 		val = temp * 500000 + 273150000;
    510 
    511 	return val;
    512 }
    513 
    514 static uint32_t
    515 lmtemp_decode_ds75(const uint8_t *buf, int degc)
    516 {
    517 	int temp;
    518 
    519 	/*
    520 	 * Sign-extend the MSB byte, and add in the fractions of a
    521 	 * degree contained in the LSB (precision 1/16th DegC).
    522 	 */
    523 	temp = (int8_t)buf[0];
    524 	temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
    525 
    526 	/*
    527 	 * Conversion to C or uK is simple.
    528 	 */
    529 	if (degc)
    530 		return temp / 16;
    531 	else
    532 		return (temp * 62500 + 273150000);
    533 }
    534 
    535 static uint32_t
    536 lmtemp_decode_lm77(const uint8_t *buf, int degc)
    537 {
    538 	int temp;
    539 	uint32_t val;
    540 
    541 	/*
    542 	 * Describe each bits of temperature registers on LM77.
    543 	 *   D15 - D12:	Sign
    544 	 *   D11 - D3 :	Bit8(MSB) - Bit0
    545 	 */
    546 	temp = (int8_t)buf[0];
    547 	temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
    548 
    549 	/* Temp is given in 1/2 deg. C, we convert to C or uK. */
    550 	if (degc)
    551 		val = temp / 2;
    552 	else
    553 		val = temp * 500000 + 273150000;
    554 
    555 	return val;
    556 }
    557 
    558 static void lmtemp_encode_lm75(const uint32_t val, uint8_t *buf, int degc)
    559 {
    560 	int temp;
    561 
    562 	/* Convert from C or uK to register format */
    563 	if (degc)
    564 		temp = val * 2;
    565 	else
    566 		temp = (val - 273150000) / 500000;
    567 	buf[0] = (temp >> 1) & 0xff;
    568 	buf[1] = (temp & 1) << 7;
    569 }
    570 
    571 static void lmtemp_encode_ds75(const uint32_t val, uint8_t *buf, int degc)
    572 {
    573 	int temp;
    574 
    575 	/* Convert from C or uK to register format */
    576 	if (degc)
    577 		temp = val * 16;
    578 	else
    579 		temp = (val - 273150000) / 62500;
    580 	buf[0] = (temp >> 4) & 0xff;
    581 	buf[1] = (temp & 0xf) << 4;
    582 }
    583 
    584 static void lmtemp_encode_lm77(const uint32_t val, uint8_t *buf, int degc)
    585 {
    586 	int temp;
    587 
    588 	/* Convert from C or uK to register format */
    589 	if (degc)
    590 		temp = val * 2;
    591 	else
    592 		temp = (val - 273150000) / 500000;
    593 	buf[0] = (temp >> 5) & 0xff;
    594 	buf[1] = (temp & 0x1f) << 3;
    595 }
    596 
    597 static void
    598 lmtemp_setup_sysctl(struct lmtemp_softc *sc)
    599 {
    600 	const struct sysctlnode *me = NULL, *node = NULL;
    601 
    602 	sysctl_createv(NULL, 0, NULL, &me,
    603 	    CTLFLAG_READWRITE,
    604 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    605 	    NULL, 0, NULL, 0,
    606 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    607 
    608 	sysctl_createv(NULL, 0, NULL, &node,
    609 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    610 	    CTLTYPE_INT, "temp", "Threshold temperature",
    611 	    sysctl_lm75_temp, 1, (void *)sc, 0,
    612 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    613 }
    614 
    615 static int
    616 sysctl_lm75_temp(SYSCTLFN_ARGS)
    617 {
    618 	struct sysctlnode node = *rnode;
    619 	struct lmtemp_softc *sc = node.sysctl_data;
    620 	int temp, error;
    621 
    622 	if (newp) {
    623 
    624 		/* we're asked to write */
    625 		node.sysctl_data = &sc->sc_tmax;
    626 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    627 
    628 			temp = *(int *)node.sysctl_data;
    629 			sc->sc_tmax = temp;
    630 			error = iic_acquire_bus(sc->sc_tag, 0);
    631 			if (error)
    632 				return error;
    633 			lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
    634 			    sc->sc_tmax - 5, 1);
    635 			lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT,
    636 			    sc->sc_tmax, 1);
    637 			iic_release_bus(sc->sc_tag, 0);
    638 
    639 			/* Synchronise envsys - calls lmtemp_getlim_lm75() */
    640 			sysmon_envsys_update_limits(sc->sc_sme, &sc->sc_sensor);
    641 			return 0;
    642 		}
    643 		return EINVAL;
    644 	} else {
    645 
    646 		node.sysctl_data = &sc->sc_tmax;
    647 		node.sysctl_size = 4;
    648 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    649 	}
    650 
    651 	return 0;
    652 }
    653 
    654 SYSCTL_SETUP(sysctl_lmtemp_setup, "sysctl lmtemp subtree setup")
    655 {
    656 
    657 	sysctl_createv(NULL, 0, NULL, NULL,
    658 		       CTLFLAG_PERMANENT,
    659 		       CTLTYPE_NODE, "machdep", NULL,
    660 		       NULL, 0, NULL, 0,
    661 		       CTL_MACHDEP, CTL_EOL);
    662 }
    663 
    664 
    665