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