Home | History | Annotate | Line # | Download | only in i2c
adm1026.c revision 1.2.18.1
      1 /*-
      2  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Julian Coleman.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: adm1026.c,v 1.2.18.1 2018/06/25 07:25:50 pgoyette Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 #include <sys/kernel.h>
     37 #include <sys/sysctl.h>
     38 
     39 #include <dev/sysmon/sysmonvar.h>
     40 
     41 #include <dev/i2c/i2cvar.h>
     42 #include <dev/i2c/adm1026reg.h>
     43 
     44 /* Voltage/analog sensors descriptions and registers */
     45 struct adm1026_volts_info {
     46 	const char* desc;
     47 	int incr;
     48 	uint8_t reg, check_tdm2;
     49 };
     50 
     51 /* Voltage maximums (in mV) from datasheet table 7 divided by 255 increments */
     52 static struct adm1026_volts_info adm1026_volts_table[] = {
     53 	{ "Vbatt", 15624, ADM1026_VBAT_VAL, 0 },
     54 	{ "V3.3 standby", 17345, ADM1026_33VSTBY_VAL, 0 },
     55 	{ "V3.3 main", 17345, ADM1026_33VMAIN_VAL, 0 },
     56 	{ "V5.0", 26016, ADM1026_50V_VAL, 0 },
     57 	{ "Vccp", 11718, ADM1026_VCCP_VAL, 0 },
     58 	{ "V+12", 62502, ADM1026_12V_VAL, 0 },
     59 	{ "V-12", -62502, ADM1026_N12V_VAL, 0 },
     60 	{ "V3.0 0", 11718, ADM1026_AIN_VAL(0), 0 },
     61 	{ "V3.0 1", 11718, ADM1026_AIN_VAL(1), 0 },
     62 	{ "V3.0 2", 11718, ADM1026_AIN_VAL(2), 0 },
     63 	{ "V3.0 3", 11718, ADM1026_AIN_VAL(3), 0 },
     64 	{ "V3.0 4", 11718, ADM1026_AIN_VAL(4), 0 },
     65 	{ "V3.0 5", 11718, ADM1026_AIN_VAL(5), 0 },
     66 	{ "V2.5 0", 9765, ADM1026_AIN_VAL(6), 0 },
     67 	{ "V2.5 1", 9765, ADM1026_AIN_VAL(7), 0 },
     68 	{ "V2.5 2", 9765, ADM1026_AIN8_VAL, 1 },
     69 	{ "V2.5 3", 9765, ADM1026_TDM2_AIN9_VAL, 1 }
     70 };
     71 
     72 /* Maximum number of each type of sensor */
     73 #define ADM1026_MAX_FANS	8
     74 #define ADM1026_MAX_TEMPS	3
     75 #define ADM1026_MAX_VOLTS	(sizeof(adm1026_volts_table) / \
     76 				    sizeof (adm1026_volts_table[0]))
     77 
     78 /* Map sensor to/from sysmon numbers */
     79 #define ADM1026_FAN_NUM(x)	(x)
     80 #define ADM1026_TEMP_NUM(x)	(x + sc->sc_nfans)
     81 #define ADM1026_VOLT_NUM(x)	(x + sc->sc_nfans + sc->sc_ntemps)
     82 #define ADM1026_NUM_FAN(x)	(x)
     83 #define ADM1026_NUM_TEMP(x)	(x - sc->sc_nfans)
     84 #define ADM1026_NUM_VOLT(x)	(x - sc->sc_nfans - sc->sc_ntemps)
     85 
     86 struct adm1026_softc {
     87 	device_t sc_dev;
     88 	i2c_tag_t sc_tag;
     89 	int sc_address;
     90 	int sc_iic_flags;
     91 	bool sc_multi_read;
     92 
     93 	uint8_t sc_rev, sc_cfg[2];
     94 	int sc_nfans, sc_ntemps;	/* Map sysmon numbers to sensors */
     95 	int sc_fandiv[ADM1026_MAX_FANS], sc_temp_off[ADM1026_MAX_TEMPS];
     96 	struct sysmon_envsys *sc_sme;
     97 	envsys_data_t sc_sensor[ADM1026_MAX_FANS + ADM1026_MAX_TEMPS +
     98 	    ADM1026_MAX_VOLTS];
     99 };
    100 
    101 static int adm1026_match(device_t, cfdata_t, void *);
    102 static int adm1026_ident(struct adm1026_softc *sc);
    103 static void adm1026_attach(device_t, device_t, void *);
    104 static int adm1026_detach(device_t, int);
    105 bool adm1026_pmf_suspend(device_t dev, const pmf_qual_t *qual);
    106 bool adm1026_pmf_resume(device_t dev, const pmf_qual_t *qual);
    107 
    108 static void adm1026_setup_fans(struct adm1026_softc *sc, int div2_val);
    109 static void adm1026_setup_temps(struct adm1026_softc *sc);
    110 static void adm1026_setup_volts(struct adm1026_softc *sc);
    111 
    112 void adm1026_refresh(struct sysmon_envsys *sme, envsys_data_t *edata);
    113 static void adm1026_read_fan(struct adm1026_softc *sc, envsys_data_t *edata);
    114 static void adm1026_read_temp(struct adm1026_softc *sc, envsys_data_t *edata);
    115 static void adm1026_read_volt(struct adm1026_softc *sc, envsys_data_t *edata);
    116 
    117 static int adm1026_read_reg(struct adm1026_softc *sc,
    118     uint8_t reg, uint8_t *val);
    119 static int adm1026_write_reg(struct adm1026_softc *sc,
    120     uint8_t reg, uint8_t val);
    121 
    122 CFATTACH_DECL_NEW(adm1026hm, sizeof(struct adm1026_softc),
    123 	adm1026_match, adm1026_attach, adm1026_detach, NULL);
    124 
    125 static const char * adm1026_compats[] = {
    126 	"i2c-adm1026",
    127 	NULL
    128 };
    129 
    130 static const struct device_compatible_entry adm1026_compat_data[] = {
    131 	DEVICE_COMPAT_ENTRY(adm1026_compats),
    132 	DEVICE_COMPAT_TERMINATOR
    133 };
    134 
    135 static int
    136 adm1026_match(device_t parent, cfdata_t cf, void *aux)
    137 {
    138 	struct i2c_attach_args *ia = aux;
    139 	struct adm1026_softc sc;	/* For chip ident */
    140 	int match_result;
    141 
    142 	sc.sc_tag = ia->ia_tag;
    143 	sc.sc_address = ia->ia_addr;
    144 	sc.sc_iic_flags = 0;
    145 
    146 	if (iic_use_direct_match(ia, cf, adm1026_compat_data, &match_result))
    147 		return match_result;
    148 
    149 	if ((ia->ia_addr & ADM1026_ADDRMASK) == ADM1026_ADDR &&
    150 	    adm1026_ident(&sc))
    151 		return I2C_MATCH_ADDRESS_AND_PROBE;
    152 
    153 	return 0;
    154 }
    155 
    156 static int
    157 adm1026_ident(struct adm1026_softc *sc)
    158 {
    159 	uint8_t val;
    160 	int err;
    161 
    162 	/* Manufacturer ID and revision/stepping */
    163 	err = adm1026_read_reg(sc, ADM1026_ID, &val);
    164 	if (err || val != ADM1026_MANF_ID) {
    165 		aprint_verbose("adm1026_ident: "
    166 		    "manufacturer ID invalid or missing\n");
    167 		return 0;
    168 	}
    169 	err = adm1026_read_reg(sc, ADM1026_REV, &sc->sc_rev);
    170 	if (err || ADM1026_REVISION(sc->sc_rev) != ADM1026_MANF_REV) {
    171 		aprint_verbose("adm1026_ident: "
    172 		    "manufacturer revision invalid or missing\n");
    173 		return 0;
    174 	}
    175 	return 1;
    176 }
    177 
    178 static void
    179 adm1026_attach(device_t parent, device_t self, void *aux)
    180 {
    181 	struct adm1026_softc *sc = device_private(self);
    182 	struct i2c_attach_args *ia = aux;
    183 	prop_dictionary_t props = device_properties(self);
    184 	uint8_t val, fan_div2;
    185 	int err, div2_val;
    186 
    187 	sc->sc_tag = ia->ia_tag;
    188 	sc->sc_address = ia->ia_addr;
    189 	sc->sc_dev = self;
    190 	sc->sc_iic_flags = I2C_F_POLL;	/* Use polling during autoconf */
    191 
    192 	sc->sc_multi_read = false;
    193 	prop_dictionary_get_bool(props, "multi_read", &sc->sc_multi_read);
    194 	if (prop_dictionary_get_uint8(props, "fan_div2", &fan_div2) != 0)
    195 		div2_val = fan_div2;
    196 	else
    197 		div2_val = -1;
    198 
    199 	(void) adm1026_ident(sc);
    200 	aprint_normal(": ADM1026 hardware monitor: rev. 0x%x, step. 0x%x\n",
    201 	    ADM1026_REVISION(sc->sc_rev), ADM1026_STEPPING(sc->sc_rev));
    202 
    203 	/*
    204 	 * Start monitoring if not already monitoring.
    205 	 * Wait 1.8s for the fan readings to stabilise.
    206 	 */
    207 	if ((err = adm1026_read_reg(sc, ADM1026_CONF1, &val)) != 0) {
    208 		aprint_error_dev(sc->sc_dev, ": unable to read conf1\n");
    209 		return;
    210 	}
    211 	if (!(val & ADM1026_CONF1_MONITOR)) {
    212 		aprint_normal_dev(sc->sc_dev,
    213 		    ": starting monitoring, waiting 1.8s for readings\n");
    214 		val |= ADM1026_CONF1_MONITOR;
    215 		if ((err = adm1026_write_reg(sc, ADM1026_CONF1, val)) != 0) {
    216 			aprint_error_dev(sc->sc_dev,
    217 			    ": unable to write conf1\n");
    218 			return;
    219 		}
    220 		delay(1800000);
    221 	}
    222 	sc->sc_cfg[0] = val;
    223 
    224 	sc->sc_sme = sysmon_envsys_create();
    225 	sc->sc_nfans = 0;
    226 	adm1026_setup_fans(sc, div2_val);
    227 	sc->sc_ntemps = 0;
    228 	adm1026_setup_temps(sc);
    229 	adm1026_setup_volts(sc);
    230 	aprint_normal_dev(self, "%d fans, %d temperatures, %d voltages\n",
    231 	    sc->sc_nfans, sc->sc_ntemps, sc->sc_ntemps == 3 ? 15 : 17);
    232 
    233         sc->sc_sme->sme_name = device_xname(self);
    234         sc->sc_sme->sme_cookie = sc;
    235         sc->sc_sme->sme_refresh = adm1026_refresh;
    236 	if (sysmon_envsys_register(sc->sc_sme)) {
    237 		aprint_error_dev(self,
    238 		    "unable to register with sysmon\n");
    239 		sysmon_envsys_destroy(sc->sc_sme);
    240 		return;
    241 	}
    242 
    243 	if (!pmf_device_register(self, adm1026_pmf_suspend, adm1026_pmf_resume))
    244 		aprint_error_dev(self, "couldn't establish power handler\n");
    245 
    246 	sc->sc_iic_flags = 0;	/* Drop polling flag */
    247 
    248 	return;
    249 }
    250 
    251 /*
    252  * We could stop (suspend/detach) and restart (resume) monitoring,
    253  * but we don't do that because some machines have separate
    254  * management hardware which can read the sensors.
    255  */
    256 bool
    257 adm1026_pmf_suspend(device_t dev, const pmf_qual_t *qual)
    258 {
    259 	return true;
    260 }
    261 
    262 bool
    263 adm1026_pmf_resume(device_t dev, const pmf_qual_t *qual)
    264 {
    265 	return true;
    266 }
    267 
    268 static int
    269 adm1026_detach(device_t self, int flags)
    270 {
    271 	struct adm1026_softc *sc = device_private(self);
    272 
    273 	pmf_device_deregister(self);
    274 
    275 	sysmon_envsys_unregister(sc->sc_sme);
    276 	sc->sc_sme = NULL;
    277 
    278 	return 0;
    279 }
    280 
    281 static void
    282 adm1026_setup_fans(struct adm1026_softc *sc, int div2_val)
    283 {
    284 	int i, err = 0;
    285 	uint8_t div1, div2;
    286 
    287 	/* Read fan-related registers (configuration and divisors) */
    288 	if ((err = adm1026_read_reg(sc, ADM1026_CONF2, &sc->sc_cfg[1])) != 0) {
    289 		aprint_error_dev(sc->sc_dev, "unable to read conf2\n");
    290 		return;
    291 	}
    292 	if ((err = adm1026_read_reg(sc, ADM1026_FAN_DIV1, &div1)) != 0) {
    293 		aprint_error_dev(sc->sc_dev, "unable to read fan_div1\n");
    294 		return;
    295 	}
    296 	sc->sc_fandiv[0] = 1 << ADM1026_FAN0_DIV(div1);
    297 	sc->sc_fandiv[1] = 1 << ADM1026_FAN1_DIV(div1);
    298 	sc->sc_fandiv[2] = 1 << ADM1026_FAN2_DIV(div1);
    299 	sc->sc_fandiv[3] = 1 << ADM1026_FAN3_DIV(div1);
    300 	if (div2_val < 0) {
    301 		if ((err =
    302 		    adm1026_read_reg(sc, ADM1026_FAN_DIV2, &div2)) != 0) {
    303 			aprint_error_dev(sc->sc_dev,
    304 			    "unable to read fan_div2\n");
    305 			return;
    306 		}
    307 	} else
    308 		div2 = div2_val;
    309 	sc->sc_fandiv[4] = 1 << ADM1026_FAN4_DIV(div2);
    310 	sc->sc_fandiv[5] = 1 << ADM1026_FAN5_DIV(div2);
    311 	sc->sc_fandiv[6] = 1 << ADM1026_FAN6_DIV(div2);
    312 	sc->sc_fandiv[7] = 1 << ADM1026_FAN7_DIV(div2);
    313 
    314 	for (i = 0; i < ADM1026_MAX_FANS; i++) {
    315 		sc->sc_sensor[ADM1026_FAN_NUM(i)].state = ENVSYS_SINVALID;
    316 		/* Check configuration2 register to see which pins are fans. */
    317 		if (ADM1026_PIN_IS_FAN(sc->sc_cfg[1], i)) {
    318 			sc->sc_sensor[ADM1026_FAN_NUM(i)].units =
    319 			    ENVSYS_SFANRPM;
    320 			snprintf(sc->sc_sensor[ADM1026_FAN_NUM(i)].desc,
    321 			    sizeof(sc->sc_sensor[ADM1026_FAN_NUM(i)].desc),
    322 			    "fan %d", ADM1026_FAN_NUM(i));
    323 			sc->sc_nfans++;
    324 			if (sysmon_envsys_sensor_attach(
    325 			    sc->sc_sme, &sc->sc_sensor[ADM1026_FAN_NUM(i)])) {
    326 				sysmon_envsys_destroy(sc->sc_sme);
    327 				aprint_error_dev(sc->sc_dev,
    328 				    "unable to attach fan %d at sysmon\n", i);
    329 				return;
    330 			}
    331 		}
    332 	}
    333 }
    334 
    335 static void
    336 adm1026_setup_temps(struct adm1026_softc *sc)
    337 {
    338 	int i;
    339 	uint8_t val;
    340 
    341 	/* Temperature offsets */
    342 	if (adm1026_read_reg(sc, ADM1026_INT_TEMP_OFF, &val)
    343 	    != 0) {
    344 		aprint_error_dev(sc->sc_dev, "unable to read int temp. off.\n");
    345 		return;
    346 	}
    347 	if (val & 0x80)
    348 		sc->sc_temp_off[0] = 0 - 1000000 * (val & 0x7f);
    349 	else
    350 		sc->sc_temp_off[0] = 1000000 * (val & 0x7f);
    351 	if (adm1026_read_reg(sc, ADM1026_TDM1_OFF, &val) != 0) {
    352 		aprint_error_dev(sc->sc_dev, "unable to read tdm1 off.\n");
    353 		return;
    354 	}
    355 	if (val & 0x80)
    356 		sc->sc_temp_off[1] = 0 - 1000000 * (val & 0x7f);
    357 	else
    358 		sc->sc_temp_off[1] = 1000000 * (val & 0x7f);
    359 	if (adm1026_read_reg(sc, ADM1026_TDM2_OFF, &val) != 0) {
    360 		aprint_error_dev(sc->sc_dev, "unable to read tdm2 off.\n");
    361 		return;
    362 	}
    363 	if (val & 0x80)
    364 		sc->sc_temp_off[2] = 0 - 1000000 * (val & 0x7f);
    365 	else
    366 		sc->sc_temp_off[2] = 1000000 * (val & 0x7f);
    367 
    368 	strlcpy(sc->sc_sensor[ADM1026_TEMP_NUM(0)].desc, "internal",
    369 	    sizeof(sc->sc_sensor[ADM1026_TEMP_NUM(0)].desc));
    370 	strlcpy(sc->sc_sensor[ADM1026_TEMP_NUM(1)].desc, "external 1",
    371 	    sizeof(sc->sc_sensor[ADM1026_TEMP_NUM(1)].desc));
    372 	strlcpy(sc->sc_sensor[ADM1026_TEMP_NUM(2)].desc, "external 2",
    373 	    sizeof(sc->sc_sensor[ADM1026_TEMP_NUM(2)].desc));
    374 	for (i = 0; i < ADM1026_MAX_TEMPS; i++) {
    375 		/* Check configuration1 register to see if TDM2 is configured */
    376 		if (i == 2 && !ADM1026_PIN_IS_TDM2(sc->sc_cfg[0]))
    377 			continue;
    378 		sc->sc_sensor[ADM1026_TEMP_NUM(i)].units = ENVSYS_STEMP;
    379 		sc->sc_sensor[ADM1026_TEMP_NUM(i)].state = ENVSYS_SINVALID;
    380 		sc->sc_ntemps++;
    381 		if (sysmon_envsys_sensor_attach(
    382 		    sc->sc_sme, &sc->sc_sensor[ADM1026_TEMP_NUM(i)])) {
    383 			sysmon_envsys_destroy(sc->sc_sme);
    384 			aprint_error_dev(sc->sc_dev,
    385 			    "unable to attach temp %d at sysmon\n", i);
    386 			return;
    387 		}
    388 	}
    389 }
    390 
    391 static void
    392 adm1026_setup_volts(struct adm1026_softc *sc)
    393 {
    394 	int i;
    395 
    396 	for (i = 0; i < ADM1026_MAX_VOLTS; i++) {
    397 		/* Check configuration1 register to see if TDM2 is configured */
    398 		if (adm1026_volts_table[i].check_tdm2 &&
    399 		    ADM1026_PIN_IS_TDM2(sc->sc_cfg[0]))
    400 			continue;
    401 		strlcpy(sc->sc_sensor[ADM1026_VOLT_NUM(i)].desc,
    402 		    adm1026_volts_table[i].desc,
    403 		    sizeof(sc->sc_sensor[ADM1026_VOLT_NUM(i)].desc));
    404 		sc->sc_sensor[ADM1026_VOLT_NUM(i)].units = ENVSYS_SVOLTS_DC;
    405 		sc->sc_sensor[ADM1026_VOLT_NUM(i)].state = ENVSYS_SINVALID;
    406 		if (sysmon_envsys_sensor_attach(
    407 		    sc->sc_sme, &sc->sc_sensor[ADM1026_VOLT_NUM(i)])) {
    408 			sysmon_envsys_destroy(sc->sc_sme);
    409 			aprint_error_dev(sc->sc_dev,
    410 			    "unable to attach volts %d at sysmon\n", i);
    411 			return;
    412 		}
    413 	}
    414 }
    415 
    416 void
    417 adm1026_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    418 {
    419 	struct adm1026_softc *sc = sme->sme_cookie;
    420 
    421 	if (edata->sensor < sc->sc_nfans)
    422 		adm1026_read_fan(sc, edata);
    423 	else if (edata->sensor < sc->sc_nfans + sc->sc_ntemps)
    424 		adm1026_read_temp(sc, edata);
    425 	else
    426 		adm1026_read_volt(sc, edata);
    427 }
    428 
    429 static void
    430 adm1026_read_fan(struct adm1026_softc *sc, envsys_data_t *edata)
    431 {
    432 	int fan = ADM1026_NUM_FAN(edata->sensor);
    433 	int err;
    434 	uint8_t	val;
    435 
    436 	if ((err = adm1026_read_reg(sc, ADM1026_FAN_VAL(fan), &val)) != 0) {
    437 		edata->state = ENVSYS_SINVALID;
    438 		return;
    439 	}
    440 	if (val == 0xff || val == 0x00)	/* Fan missing or stopped */
    441 		edata->value_cur = 0;
    442 	else
    443 		edata->value_cur = 1350000 / (val * sc->sc_fandiv[fan]);
    444 	edata->state = ENVSYS_SVALID;
    445 }
    446 
    447 static void
    448 adm1026_read_temp(struct adm1026_softc *sc, envsys_data_t *edata)
    449 {
    450 	int temp = ADM1026_NUM_TEMP(edata->sensor);
    451 	int err;
    452 	uint8_t	val;
    453 
    454 	if (temp == 0)
    455 		err = adm1026_read_reg(sc, ADM1026_INT_TEMP_VAL, &val);
    456 	else if (temp == 1)
    457 		err = adm1026_read_reg(sc, ADM1026_TDM1_VAL, &val);
    458 	else
    459 		err = adm1026_read_reg(sc, ADM1026_TDM2_AIN9_VAL, &val);
    460 	if (err) {
    461 		edata->state = ENVSYS_SINVALID;
    462 		return;
    463 	}
    464 
    465 	if (val & 0x80)	/* Negative temperature */
    466 		edata->value_cur = 273150000 - sc->sc_temp_off[temp] -
    467 		    1000000 * (val & 0x7f);
    468 	else		/* Positive temperature */
    469 		edata->value_cur = 273150000 - sc->sc_temp_off[temp] +
    470 		    1000000 * val;
    471 	edata->state = ENVSYS_SVALID;
    472 }
    473 
    474 static void
    475 adm1026_read_volt(struct adm1026_softc *sc, envsys_data_t *edata)
    476 {
    477 	int volt = ADM1026_NUM_VOLT(edata->sensor);
    478 	int err;
    479 	uint8_t	val;
    480 
    481 	err = adm1026_read_reg(sc, adm1026_volts_table[volt].reg, &val);
    482 	if (err) {
    483 		edata->state = ENVSYS_SINVALID;
    484 		return;
    485 	}
    486 	/* Vbatt is not valid for < 1.5V */
    487 	if (volt == 0 && val < 0x60)
    488 		edata->state = ENVSYS_SINVALID;
    489 	edata->value_cur = (int) val * adm1026_volts_table[volt].incr;
    490 	edata->state = ENVSYS_SVALID;
    491 }
    492 
    493 static int
    494 adm1026_read_reg(struct adm1026_softc *sc, uint8_t reg, uint8_t *val)
    495 {
    496 #define ADM1026_READ_RETRIES	5
    497 	int i, j, err = 0;
    498 	uint8_t creg, cval, tmp[ADM1026_READ_RETRIES + 1];
    499 
    500 	if ((err = iic_acquire_bus(sc->sc_tag, sc->sc_iic_flags)) != 0)
    501 		return err;
    502 	/* Standard ADM1026 */
    503 	if (sc->sc_multi_read == false) {
    504 		err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    505 		    sc->sc_address, &reg, 1, val, 1, 0);
    506 	/*
    507 	 * The ADM1026 found in some Sun machines sometimes reads bogus values.
    508 	 * We'll read at least twice and check that we get (nearly) the same
    509 	 * value.  If not, we'll read another register and then re-read the
    510 	 * first.
    511 	 */
    512 	} else {
    513 		if (reg == ADM1026_CONF1)
    514 			creg = ADM1026_CONF2;
    515 		else
    516 			creg = ADM1026_CONF1;
    517 		if ((err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    518 		    sc->sc_address, &reg, 1, &tmp[0], 1, 0)) != 0) {
    519 			iic_release_bus(sc->sc_tag, sc->sc_iic_flags);
    520 			return err;
    521 		}
    522 		for (i = 1; i <= ADM1026_READ_RETRIES; i++) {
    523 			if ((err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    524 			    sc->sc_address, &reg, 1, &tmp[i], 1, 0)) != 0)
    525 				break;
    526 			for (j = 0; j < i; j++)
    527 				if (abs(tmp[j] - tmp[i]) < 3) {
    528 					*val = tmp[i];
    529 					iic_release_bus(sc->sc_tag,
    530 					    sc->sc_iic_flags);
    531 					return 0;
    532 				}
    533 			if ((err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    534 			    sc->sc_address, &creg, 1, &cval, 1, 0)) != 0)
    535 				break;
    536 			err = -1;	/* Return error if we don't match. */
    537 		}
    538 	}
    539 	iic_release_bus(sc->sc_tag, sc->sc_iic_flags);
    540 	return err;
    541 }
    542 
    543 static int
    544 adm1026_write_reg(struct adm1026_softc *sc, uint8_t reg, uint8_t val)
    545 {
    546 	int err = 0;
    547 
    548 	if ((err = iic_acquire_bus(sc->sc_tag, sc->sc_iic_flags)) != 0)
    549 		return err;
    550 	err = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    551 	    &reg, 1, &val, 1, 0);
    552 	iic_release_bus(sc->sc_tag, sc->sc_iic_flags);
    553 	return err;
    554 }
    555