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