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