xref: /netbsd-src/sys/dev/i2c/adm1026.c (revision 1e4ed9cf7e89ab508938eb1e717dad4d09b603c5)
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.13 2021/06/14 13:52:11 jdc 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 	bool sc_multi_read;
91 
92 	uint8_t sc_rev, sc_cfg[2];
93 	int sc_nfans, sc_ntemps;	/* Map sysmon numbers to sensors */
94 	int sc_fandiv[ADM1026_MAX_FANS], sc_temp_off[ADM1026_MAX_TEMPS];
95 	struct sysmon_envsys *sc_sme;
96 	envsys_data_t sc_sensor[ADM1026_MAX_FANS + ADM1026_MAX_TEMPS +
97 	    ADM1026_MAX_VOLTS];
98 };
99 
100 static int adm1026_match(device_t, cfdata_t, void *);
101 static int adm1026_ident(i2c_tag_t, i2c_addr_t, int, uint8_t*);
102 static void adm1026_attach(device_t, device_t, void *);
103 static int adm1026_detach(device_t, int);
104 bool adm1026_pmf_suspend(device_t, const pmf_qual_t *);
105 bool adm1026_pmf_resume(device_t, const pmf_qual_t *);
106 
107 static int adm1026_setup_fans(struct adm1026_softc *sc, int div2_val);
108 static int adm1026_setup_temps(struct adm1026_softc *sc);
109 static int adm1026_setup_volts(struct adm1026_softc *sc);
110 
111 void adm1026_refresh(struct sysmon_envsys *sme, envsys_data_t *edata);
112 static void adm1026_read_fan(struct adm1026_softc *sc, envsys_data_t *edata);
113 static void adm1026_read_temp(struct adm1026_softc *sc, envsys_data_t *edata);
114 static void adm1026_read_volt(struct adm1026_softc *sc, envsys_data_t *edata);
115 
116 static int adm1026_read_reg_int(i2c_tag_t, i2c_addr_t,
117     uint8_t reg, bool multi_read, uint8_t *val);
118 static int adm1026_write_reg(struct adm1026_softc *sc,
119     uint8_t reg, uint8_t val);
120 
121 static inline int
adm1026_read_reg(struct adm1026_softc * sc,uint8_t reg,uint8_t * val)122 adm1026_read_reg(struct adm1026_softc *sc, uint8_t reg, uint8_t *val)
123 {
124 	return adm1026_read_reg_int(sc->sc_tag, sc->sc_address, reg,
125 	    sc->sc_multi_read, val);
126 }
127 
128 CFATTACH_DECL_NEW(adm1026hm, sizeof(struct adm1026_softc),
129 	adm1026_match, adm1026_attach, adm1026_detach, NULL);
130 
131 static const struct device_compatible_entry compat_data[] = {
132 	{ .compat = "i2c-adm1026" },
133 	DEVICE_COMPAT_EOL
134 };
135 
136 static int
adm1026_match(device_t parent,cfdata_t cf,void * aux)137 adm1026_match(device_t parent, cfdata_t cf, void *aux)
138 {
139 	struct i2c_attach_args *ia = aux;
140 	int match_result;
141 	uint8_t rev;
142 
143 	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
144 		return match_result;
145 
146 	if (ia->ia_addr == ADM1026_ADDR1
147 	    && adm1026_ident(ia->ia_tag, ia->ia_addr, 1, &rev))
148 		return I2C_MATCH_ADDRESS_AND_PROBE;
149 
150 	return 0;
151 }
152 
153 static int
adm1026_ident(i2c_tag_t tag,i2c_addr_t addr,int probe_only,uint8_t * rev)154 adm1026_ident(i2c_tag_t tag, i2c_addr_t addr, int probe_only, uint8_t *rev)
155 {
156 	uint8_t val;
157 	int err;
158 
159 	/* Manufacturer ID and revision/stepping */
160 	err = adm1026_read_reg_int(tag, addr, ADM1026_ID, false, &val);
161 	if (err || val != ADM1026_MANF_ID) {
162 		if (!probe_only)
163 			aprint_verbose("adm1026_ident: "
164 			    "manufacturer ID invalid or missing\n");
165 		return 0;
166 	}
167 	err = adm1026_read_reg_int(tag, addr, ADM1026_REV, false, rev);
168 	if (err || ADM1026_REVISION(*rev) != ADM1026_MANF_REV) {
169 		if (!probe_only)
170 			aprint_verbose("adm1026_ident: "
171 			    "manufacturer revision invalid or missing\n");
172 		return 0;
173 	}
174 	return 1;
175 }
176 
177 static void
adm1026_attach(device_t parent,device_t self,void * aux)178 adm1026_attach(device_t parent, device_t self, void *aux)
179 {
180 	struct adm1026_softc *sc = device_private(self);
181 	struct i2c_attach_args *ia = aux;
182 	prop_dictionary_t props = device_properties(self);
183 	uint8_t val, fan_div2;
184 	int err, div2_val;
185 
186 	sc->sc_tag = ia->ia_tag;
187 	sc->sc_address = ia->ia_addr;
188 	sc->sc_dev = self;
189 
190 	sc->sc_multi_read = false;
191 	prop_dictionary_get_bool(props, "multi_read", &sc->sc_multi_read);
192 	if (prop_dictionary_get_uint8(props, "fan_div2", &fan_div2) != 0)
193 		div2_val = fan_div2;
194 	else
195 		div2_val = -1;
196 
197 	(void) adm1026_ident(sc->sc_tag, sc->sc_address, 0, &sc->sc_rev);
198 	aprint_normal(": ADM1026 hardware monitor: rev. 0x%x, step. 0x%x\n",
199 	    ADM1026_REVISION(sc->sc_rev), ADM1026_STEPPING(sc->sc_rev));
200 
201 	/*
202 	 * Start monitoring if not already monitoring.
203 	 * Wait 1.8s for the fan readings to stabilise.
204 	 */
205 	if ((err = adm1026_read_reg(sc, ADM1026_CONF1, &val)) != 0) {
206 		aprint_error_dev(sc->sc_dev, ": unable to read conf1\n");
207 		return;
208 	}
209 	if (!(val & ADM1026_CONF1_MONITOR)) {
210 		aprint_normal_dev(sc->sc_dev,
211 		    ": starting monitoring, waiting 1.8s for readings\n");
212 		val |= ADM1026_CONF1_MONITOR;
213 		if ((err = adm1026_write_reg(sc, ADM1026_CONF1, val)) != 0) {
214 			aprint_error_dev(sc->sc_dev,
215 			    ": unable to write conf1\n");
216 			return;
217 		}
218 		delay(1800000);
219 	}
220 	sc->sc_cfg[0] = val;
221 
222 	sc->sc_sme = sysmon_envsys_create();
223 	sc->sc_nfans = 0;
224 	sc->sc_ntemps = 0;
225 	if (adm1026_setup_fans(sc, div2_val))
226 		goto bad;
227 	if (adm1026_setup_temps(sc))
228 		goto bad;
229 	if (adm1026_setup_volts(sc))
230 		goto bad;
231 	aprint_normal_dev(self, "%d fans, %d temperatures, %d voltages\n",
232 	    sc->sc_nfans, sc->sc_ntemps, sc->sc_ntemps == 3 ? 15 : 17);
233 
234         sc->sc_sme->sme_name = device_xname(self);
235         sc->sc_sme->sme_cookie = sc;
236         sc->sc_sme->sme_refresh = adm1026_refresh;
237 	if (sysmon_envsys_register(sc->sc_sme)) {
238 		aprint_error_dev(self,
239 		    "unable to register with sysmon\n");
240 		goto bad;
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 	return;
247 
248 bad:
249 	sysmon_envsys_destroy(sc->sc_sme);
250 	sc->sc_sme = NULL;
251 	return;
252 }
253 
254 /*
255  * We could stop (suspend/detach) and restart (resume) monitoring,
256  * but we don't do that because some machines have separate
257  * management hardware which can read the sensors.
258  */
259 bool
adm1026_pmf_suspend(device_t dev,const pmf_qual_t * qual)260 adm1026_pmf_suspend(device_t dev, const pmf_qual_t *qual)
261 {
262 	return true;
263 }
264 
265 bool
adm1026_pmf_resume(device_t dev,const pmf_qual_t * qual)266 adm1026_pmf_resume(device_t dev, const pmf_qual_t *qual)
267 {
268 	return true;
269 }
270 
271 static int
adm1026_detach(device_t self,int flags)272 adm1026_detach(device_t self, int flags)
273 {
274 	struct adm1026_softc *sc = device_private(self);
275 
276 	pmf_device_deregister(self);
277 
278 	if (sc->sc_sme != NULL)
279 		sysmon_envsys_unregister(sc->sc_sme);
280 
281 	return 0;
282 }
283 
284 static int
adm1026_setup_fans(struct adm1026_softc * sc,int div2_val)285 adm1026_setup_fans(struct adm1026_softc *sc, int div2_val)
286 {
287 	int i, err = 0;
288 	uint8_t div1, div2;
289 
290 	/* Read fan-related registers (configuration and divisors) */
291 	if ((err = adm1026_read_reg(sc, ADM1026_CONF2, &sc->sc_cfg[1])) != 0) {
292 		aprint_error_dev(sc->sc_dev, "unable to read conf2\n");
293 		return 0;
294 	}
295 	if ((err = adm1026_read_reg(sc, ADM1026_FAN_DIV1, &div1)) != 0) {
296 		aprint_error_dev(sc->sc_dev, "unable to read fan_div1\n");
297 		return 0;
298 	}
299 	sc->sc_fandiv[0] = 1 << ADM1026_FAN0_DIV(div1);
300 	sc->sc_fandiv[1] = 1 << ADM1026_FAN1_DIV(div1);
301 	sc->sc_fandiv[2] = 1 << ADM1026_FAN2_DIV(div1);
302 	sc->sc_fandiv[3] = 1 << ADM1026_FAN3_DIV(div1);
303 	if (div2_val < 0) {
304 		if ((err =
305 		    adm1026_read_reg(sc, ADM1026_FAN_DIV2, &div2)) != 0) {
306 			aprint_error_dev(sc->sc_dev,
307 			    "unable to read fan_div2\n");
308 			return 0;
309 		}
310 	} else
311 		div2 = div2_val;
312 	sc->sc_fandiv[4] = 1 << ADM1026_FAN4_DIV(div2);
313 	sc->sc_fandiv[5] = 1 << ADM1026_FAN5_DIV(div2);
314 	sc->sc_fandiv[6] = 1 << ADM1026_FAN6_DIV(div2);
315 	sc->sc_fandiv[7] = 1 << ADM1026_FAN7_DIV(div2);
316 
317 	for (i = 0; i < ADM1026_MAX_FANS; i++) {
318 		sc->sc_sensor[ADM1026_FAN_NUM(i)].state = ENVSYS_SINVALID;
319 		/* Check configuration2 register to see which pins are fans. */
320 		if (ADM1026_PIN_IS_FAN(sc->sc_cfg[1], i)) {
321 			sc->sc_sensor[ADM1026_FAN_NUM(i)].units =
322 			    ENVSYS_SFANRPM;
323 			snprintf(sc->sc_sensor[ADM1026_FAN_NUM(i)].desc,
324 			    sizeof(sc->sc_sensor[ADM1026_FAN_NUM(i)].desc),
325 			    "fan %d", ADM1026_FAN_NUM(i));
326 			if (sysmon_envsys_sensor_attach(
327 			    sc->sc_sme, &sc->sc_sensor[ADM1026_FAN_NUM(i)])) {
328 				aprint_error_dev(sc->sc_dev,
329 				    "unable to attach fan %d at sysmon\n", i);
330 				return 1;
331 			}
332 			sc->sc_nfans++;
333 		}
334 	}
335 	return 0;
336 }
337 
338 static int
adm1026_setup_temps(struct adm1026_softc * sc)339 adm1026_setup_temps(struct adm1026_softc *sc)
340 {
341 	int i;
342 	uint8_t val;
343 
344 	/* Temperature offsets */
345 	if (adm1026_read_reg(sc, ADM1026_INT_TEMP_OFF, &val)
346 	    != 0) {
347 		aprint_error_dev(sc->sc_dev, "unable to read int temp. off.\n");
348 		return 0;
349 	}
350 	if (val & 0x80)
351 		sc->sc_temp_off[0] = 0 - 1000000 * (val & 0x7f);
352 	else
353 		sc->sc_temp_off[0] = 1000000 * (val & 0x7f);
354 	if (adm1026_read_reg(sc, ADM1026_TDM1_OFF, &val) != 0) {
355 		aprint_error_dev(sc->sc_dev, "unable to read tdm1 off.\n");
356 		return 0;
357 	}
358 	if (val & 0x80)
359 		sc->sc_temp_off[1] = 0 - 1000000 * (val & 0x7f);
360 	else
361 		sc->sc_temp_off[1] = 1000000 * (val & 0x7f);
362 	if (adm1026_read_reg(sc, ADM1026_TDM2_OFF, &val) != 0) {
363 		aprint_error_dev(sc->sc_dev, "unable to read tdm2 off.\n");
364 		return 0;
365 	}
366 	if (val & 0x80)
367 		sc->sc_temp_off[2] = 0 - 1000000 * (val & 0x7f);
368 	else
369 		sc->sc_temp_off[2] = 1000000 * (val & 0x7f);
370 
371 	strlcpy(sc->sc_sensor[ADM1026_TEMP_NUM(0)].desc, "internal",
372 	    sizeof(sc->sc_sensor[ADM1026_TEMP_NUM(0)].desc));
373 	strlcpy(sc->sc_sensor[ADM1026_TEMP_NUM(1)].desc, "external 1",
374 	    sizeof(sc->sc_sensor[ADM1026_TEMP_NUM(1)].desc));
375 	strlcpy(sc->sc_sensor[ADM1026_TEMP_NUM(2)].desc, "external 2",
376 	    sizeof(sc->sc_sensor[ADM1026_TEMP_NUM(2)].desc));
377 	for (i = 0; i < ADM1026_MAX_TEMPS; i++) {
378 		/* Check configuration1 register to see if TDM2 is configured */
379 		if (i == 2 && !ADM1026_PIN_IS_TDM2(sc->sc_cfg[0]))
380 			continue;
381 		sc->sc_sensor[ADM1026_TEMP_NUM(i)].units = ENVSYS_STEMP;
382 		sc->sc_sensor[ADM1026_TEMP_NUM(i)].state = ENVSYS_SINVALID;
383 		if (sysmon_envsys_sensor_attach(
384 		    sc->sc_sme, &sc->sc_sensor[ADM1026_TEMP_NUM(i)])) {
385 			aprint_error_dev(sc->sc_dev,
386 			    "unable to attach temp %d at sysmon\n", i);
387 			return 1;
388 		}
389 		sc->sc_ntemps++;
390 	}
391 	return 0;
392 }
393 
394 static int
adm1026_setup_volts(struct adm1026_softc * sc)395 adm1026_setup_volts(struct adm1026_softc *sc)
396 {
397 	int i;
398 
399 	for (i = 0; i < ADM1026_MAX_VOLTS; i++) {
400 		/* Check configuration1 register to see if TDM2 is configured */
401 		if (adm1026_volts_table[i].check_tdm2 &&
402 		    ADM1026_PIN_IS_TDM2(sc->sc_cfg[0]))
403 			continue;
404 		strlcpy(sc->sc_sensor[ADM1026_VOLT_NUM(i)].desc,
405 		    adm1026_volts_table[i].desc,
406 		    sizeof(sc->sc_sensor[ADM1026_VOLT_NUM(i)].desc));
407 		sc->sc_sensor[ADM1026_VOLT_NUM(i)].units = ENVSYS_SVOLTS_DC;
408 		sc->sc_sensor[ADM1026_VOLT_NUM(i)].state = ENVSYS_SINVALID;
409 		if (sysmon_envsys_sensor_attach(
410 		    sc->sc_sme, &sc->sc_sensor[ADM1026_VOLT_NUM(i)])) {
411 			aprint_error_dev(sc->sc_dev,
412 			    "unable to attach volts %d at sysmon\n", i);
413 			return 1;
414 		}
415 	}
416 	return 0;
417 }
418 
419 void
adm1026_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)420 adm1026_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
421 {
422 	struct adm1026_softc *sc = sme->sme_cookie;
423 
424 	if (edata->sensor < sc->sc_nfans)
425 		adm1026_read_fan(sc, edata);
426 	else if (edata->sensor < sc->sc_nfans + sc->sc_ntemps)
427 		adm1026_read_temp(sc, edata);
428 	else
429 		adm1026_read_volt(sc, edata);
430 }
431 
432 static void
adm1026_read_fan(struct adm1026_softc * sc,envsys_data_t * edata)433 adm1026_read_fan(struct adm1026_softc *sc, envsys_data_t *edata)
434 {
435 	int fan = ADM1026_NUM_FAN(edata->sensor);
436 	int err;
437 	uint8_t	val;
438 
439 	if ((err = adm1026_read_reg(sc, ADM1026_FAN_VAL(fan), &val)) != 0) {
440 		edata->state = ENVSYS_SINVALID;
441 		return;
442 	}
443 	if (val == 0xff || val == 0x00)	/* Fan missing or stopped */
444 		edata->value_cur = 0;
445 	else
446 		edata->value_cur = 1350000 / (val * sc->sc_fandiv[fan]);
447 	edata->state = ENVSYS_SVALID;
448 }
449 
450 static void
adm1026_read_temp(struct adm1026_softc * sc,envsys_data_t * edata)451 adm1026_read_temp(struct adm1026_softc *sc, envsys_data_t *edata)
452 {
453 	int temp = ADM1026_NUM_TEMP(edata->sensor);
454 	int err;
455 	uint8_t	val;
456 
457 	if (temp == 0)
458 		err = adm1026_read_reg(sc, ADM1026_INT_TEMP_VAL, &val);
459 	else if (temp == 1)
460 		err = adm1026_read_reg(sc, ADM1026_TDM1_VAL, &val);
461 	else
462 		err = adm1026_read_reg(sc, ADM1026_TDM2_AIN9_VAL, &val);
463 	if (err) {
464 		edata->state = ENVSYS_SINVALID;
465 		return;
466 	}
467 
468 	if (val & 0x80)	/* Negative temperature */
469 		edata->value_cur = 273150000 - sc->sc_temp_off[temp] -
470 		    1000000 * (val & 0x7f);
471 	else		/* Positive temperature */
472 		edata->value_cur = 273150000 - sc->sc_temp_off[temp] +
473 		    1000000 * val;
474 	edata->state = ENVSYS_SVALID;
475 }
476 
477 static void
adm1026_read_volt(struct adm1026_softc * sc,envsys_data_t * edata)478 adm1026_read_volt(struct adm1026_softc *sc, envsys_data_t *edata)
479 {
480 	int volt = ADM1026_NUM_VOLT(edata->sensor);
481 	int err;
482 	uint8_t	val;
483 
484 	err = adm1026_read_reg(sc, adm1026_volts_table[volt].reg, &val);
485 	if (err) {
486 		edata->state = ENVSYS_SINVALID;
487 		return;
488 	}
489 	/* Vbatt is not valid for < 1.5V */
490 	if (volt == 0 && val < 0x60)
491 		edata->state = ENVSYS_SINVALID;
492 	edata->value_cur = (int) val * adm1026_volts_table[volt].incr;
493 	edata->state = ENVSYS_SVALID;
494 }
495 
496 static int
adm1026_read_reg_int(i2c_tag_t tag,i2c_addr_t addr,uint8_t reg,bool multi_read,uint8_t * val)497 adm1026_read_reg_int(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
498     bool multi_read, uint8_t *val)
499 {
500 #define ADM1026_READ_RETRIES	5
501 	int i, j, err = 0;
502 	uint8_t creg, cval, tmp[ADM1026_READ_RETRIES + 1];
503 
504 	if ((err = iic_acquire_bus(tag, 0)) != 0)
505 		return err;
506 	/* Standard ADM1026 */
507 	if (multi_read == false) {
508 		err = iic_exec(tag, I2C_OP_READ_WITH_STOP,
509 		    addr, &reg, 1, val, 1, 0);
510 	/*
511 	 * The ADM1026 found in some Sun machines sometimes reads bogus values.
512 	 * We'll read at least twice and check that we get (nearly) the same
513 	 * value.  If not, we'll read another register and then re-read the
514 	 * first.
515 	 */
516 	} else {
517 		if (reg == ADM1026_CONF1)
518 			creg = ADM1026_CONF2;
519 		else
520 			creg = ADM1026_CONF1;
521 		if ((err = iic_exec(tag, I2C_OP_READ_WITH_STOP,
522 		    addr, &reg, 1, &tmp[0], 1, 0)) != 0) {
523 			iic_release_bus(tag, 0);
524 			return err;
525 		}
526 		for (i = 1; i <= ADM1026_READ_RETRIES; i++) {
527 			if ((err = iic_exec(tag, I2C_OP_READ_WITH_STOP,
528 			    addr, &reg, 1, &tmp[i], 1, 0)) != 0)
529 				break;
530 			for (j = 0; j < i; j++)
531 				if (abs(tmp[j] - tmp[i]) < 3) {
532 					*val = tmp[i];
533 					iic_release_bus(tag, 0);
534 					return 0;
535 				}
536 			if ((err = iic_exec(tag, I2C_OP_READ_WITH_STOP,
537 			    addr, &creg, 1, &cval, 1, 0)) != 0)
538 				break;
539 			err = -1;	/* Return error if we don't match. */
540 		}
541 	}
542 	iic_release_bus(tag, 0);
543 	return err;
544 }
545 
546 static int
adm1026_write_reg(struct adm1026_softc * sc,uint8_t reg,uint8_t val)547 adm1026_write_reg(struct adm1026_softc *sc, uint8_t reg, uint8_t val)
548 {
549 	int err = 0;
550 
551 	if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
552 		return err;
553 	err = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
554 	    &reg, 1, &val, 1, 0);
555 	iic_release_bus(sc->sc_tag, 0);
556 	return err;
557 }
558