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