xref: /netbsd-src/sys/dev/i2c/sdtemp.c (revision c8da0e5fefd3800856b306200a18b2315c7fbb9f)
1 /*      $NetBSD: sdtemp.c,v 1.1 2009/05/09 15:04:25 pgoyette Exp $        */
2 
3 /*
4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Goyette.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sdtemp.c,v 1.1 2009/05/09 15:04:25 pgoyette Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kmem.h>
38 #include <sys/device.h>
39 #include <sys/kernel.h>
40 #include <sys/endian.h>
41 #include <sys/sysctl.h>
42 
43 #include <dev/sysmon/sysmonvar.h>
44 
45 #include <dev/i2c/i2cvar.h>
46 #include <dev/i2c/sdtemp_reg.h>
47 
48 struct sdtemp_softc {
49 	device_t sc_dev;
50 	i2c_tag_t sc_tag;
51 	int sc_address;
52 
53 	struct sysmon_envsys *sc_sme;
54 	envsys_data_t *sc_sensor;
55 	int sc_resolution;
56 	uint16_t sc_capability;
57 	uint16_t sc_low_lim, sc_high_lim, sc_crit_lim;
58 };
59 
60 static int  sdtemp_match(device_t, cfdata_t, void *);
61 static void sdtemp_attach(device_t, device_t, void *);
62 
63 CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
64 	sdtemp_match, sdtemp_attach, NULL, NULL);
65 
66 static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
67 #ifdef NOT_YET
68 static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
69 static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
70 #endif /* NOT YET */
71 static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
72 static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
73 static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
74 static void	sdtemp_set_thresh(struct sdtemp_softc *, int, uint16_t);
75 static bool	sdtemp_pmf_suspend(device_t PMF_FN_PROTO);
76 static bool	sdtemp_pmf_resume(device_t PMF_FN_PROTO);
77 
78 SYSCTL_SETUP_PROTO(sysctl_sdtemp_setup);
79 static int sdtemp_sysctl_helper(SYSCTLFN_PROTO);
80 
81 struct sdtemp_dev_entry {
82 	const uint16_t sdtemp_mfg_id;
83 	const uint8_t  sdtemp_dev_id;
84 	const uint8_t  sdtemp_rev_id;
85 	const uint8_t  sdtemp_resolution;
86 	const char    *sdtemp_desc;
87 };
88 
89 /* sysctl stuff */
90 static int hw_node = CTL_EOL;
91 
92 /*
93  * List of devices known to conform to JEDEC JC42.4
94  *
95  * NOTE: A non-negative value for resolution indicates that the sensor
96  * resolution is fixed at that number of fractional bits;  a negative
97  * value indicates that the sensor needs to be configured.  In either
98  * case, trip-point registers are fixed at two-bit (0.25C) resolution.
99  */
100 static const struct sdtemp_dev_entry
101 sdtemp_dev_table[] = {
102     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,    0xff, 3,
103 	"Maxim MAX604" },
104     { MCP_MANUFACTURER_ID,   MCP_9805_DEVICE_ID,    0xff, 2,
105 	"Microchip Tech MCP9805" },
106     { MCP_MANUFACTURER_ID,   MCP_98242_DEVICE_ID,   0xff, -4,
107 	"Microchip Tech MCP98242" },
108     { ADT_MANUFACTURER_ID,   ADT_7408_DEVICE_ID,    0xff, 4,
109 	"Analog Devices ADT7408" },
110     { NXP_MANUFACTURER_ID,   NXP_SE97_DEVICE_ID,    0xff, 3,
111 	"NXP Semiconductors SE97/SE98" },
112     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x00, 2,
113 	"STmicroelectronics STTS424E02-DA" },
114     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x01, 2,
115 	"STmicroelectronics STTS424E02-DN" },
116     { CAT_MANUFACTURER_ID,   CAT_34TS02_DEVICE_ID,  0xff, 4,
117 	"Catalyst CAT34TS02/CAT6095" },
118     { 0, 0, 0, 2, "Unknown" }
119 };
120 
121 static int
122 sdtemp_lookup(uint16_t mfg, uint16_t dev, uint16_t rev)
123 {
124 	int i;
125 
126 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++)
127 		if (sdtemp_dev_table[i].sdtemp_mfg_id == mfg &&
128 		    sdtemp_dev_table[i].sdtemp_dev_id == dev &&
129 		    (sdtemp_dev_table[i].sdtemp_rev_id == 0xff ||
130 		     sdtemp_dev_table[i].sdtemp_rev_id == rev))
131 			break;
132 
133 	return i;
134 }
135 
136 static int
137 sdtemp_match(device_t parent, cfdata_t cf, void *aux)
138 {
139 	struct i2c_attach_args *ia = aux;
140 	uint16_t mfgid, devid;
141 	struct sdtemp_softc sc;
142 	int i, error;
143 
144 	sc.sc_tag = ia->ia_tag;
145 	sc.sc_address = ia->ia_addr;
146 
147 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
148 		return 0;
149 
150 	/* Verify that we can read the manufacturer ID  & Device ID */
151 	iic_acquire_bus(sc.sc_tag, 0);
152 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
153 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
154 	iic_release_bus(sc.sc_tag, 0);
155 
156 	if (error)
157 		return 0;
158 
159 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
160 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
161 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
162 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
163 		    devid & 0xff, sc.sc_address);
164 		return 0;
165 	}
166 
167 	return 1;
168 }
169 
170 static void
171 sdtemp_attach(device_t parent, device_t self, void *aux)
172 {
173 	struct sdtemp_softc *sc = device_private(self);
174 	struct i2c_attach_args *ia = aux;
175 	const struct sysctlnode *node = NULL;
176 	uint16_t mfgid, devid;
177 	int32_t	dev_sysctl_num;
178 	int i, error;
179 
180 	sc->sc_tag = ia->ia_tag;
181 	sc->sc_address = ia->ia_addr;
182 	sc->sc_dev = self;
183 
184 	iic_acquire_bus(sc->sc_tag, 0);
185 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
186 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
187 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
188 		aprint_error(": attach error %d\n", error);
189 		return;
190 	}
191 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
192 	sc->sc_resolution =
193 	    sdtemp_dev_table[i].sdtemp_resolution;
194 
195 	aprint_naive(": Temp Sensor\n");
196 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
197 
198 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
199 		aprint_debug_dev(self,
200 		    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
201 		    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
202 
203 	/*
204 	 * Alarm capability is required;  if not present, this is likely
205 	 * not a real sdtemp device.
206 	 */
207 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
208 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
209 		iic_release_bus(sc->sc_tag, 0);
210 		aprint_error_dev(self,
211 		    "required alarm capability not present!\n");
212 		return;
213 	}
214 	/* Set the configuration to defaults. */
215 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
216 	if (error != 0) {
217 		iic_release_bus(sc->sc_tag, 0);
218 		aprint_error_dev(self, "error %d writing config register\n",
219 		    error);
220 		return;
221 	}
222 	/* If variable resolution, set to max */
223 	if (sc->sc_resolution < 0) {
224 		sc->sc_resolution = ~sc->sc_resolution;
225 		error = sdtemp_write_16(sc, SDTEMP_REG_RESOLUTION,
226 					sc->sc_resolution & 0x3);
227 		if (error != 0) {
228 			iic_release_bus(sc->sc_tag, 0);
229 			aprint_error_dev(self,
230 			    "error %d writing resolution register\n", error);
231 			return;
232 		} else
233 			sc->sc_resolution++;
234 	}
235 	iic_release_bus(sc->sc_tag, 0);
236 
237 	/* Hook us into the sysmon_envsys subsystem */
238 	sc->sc_sme = sysmon_envsys_create();
239 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
240 	if (!sc->sc_sensor) {
241 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
242 		goto bad2;
243 	}
244 
245 	/* Initialize sensor data. */
246 	sc->sc_sensor->units =  ENVSYS_STEMP;
247 	sc->sc_sensor->state = ENVSYS_SINVALID;
248 #ifdef ENVSYS_FMONLIMITS
249 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
250 #else
251 	sc->sc_sensor->flags |= ENVSYS_FMONWARNOVER | ENVSYS_FMONWARNUNDER |
252 				ENVSYS_FMONCRITOVER;
253 #endif
254 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
255 	    sizeof(sc->sc_sensor->desc));
256 
257 	/* Now attach the sensor */
258 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
259 		aprint_error_dev(self, "unable to attach sensor\n");
260 		goto bad;
261 	}
262 
263 	/* Register the device */
264 	sc->sc_sme->sme_name = device_xname(self);
265 	sc->sc_sme->sme_cookie = sc;
266 	sc->sc_sme->sme_refresh = sdtemp_refresh;
267 
268 	error = sysmon_envsys_register(sc->sc_sme);
269 	if (error) {
270 		aprint_error_dev(self, "error %d registering with sysmon\n",
271 		    error);
272 		goto bad;
273 	}
274 
275 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
276 		aprint_error_dev(self, "couldn't establish power handler\n");
277 
278 
279 	/* Retrieve and display hardware monitor limits */
280 	i = 0;
281 	aprint_normal_dev(self, "");
282 	iic_acquire_bus(sc->sc_tag, 0);
283 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &sc->sc_low_lim) == 0 &&
284 	    sc->sc_low_lim != 0) {
285 		aprint_normal("low limit %d ", sc->sc_low_lim);
286 		i++;
287 	}
288 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &sc->sc_high_lim) == 0 &&
289 	    sc->sc_high_lim != 0) {
290 		aprint_normal("high limit %d ", sc->sc_high_lim);
291 		i++;
292 	}
293 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &sc->sc_crit_lim) == 0 &&
294 	    sc->sc_crit_lim != 0) {
295 		aprint_normal("critical limit %d ", sc->sc_crit_lim);
296 		i++;
297 	}
298 	iic_release_bus(sc->sc_tag, 0);
299 	if (i == 0)
300 		aprint_normal("no hardware limits set\n");
301 	else
302 		aprint_normal("\n");
303 
304 	/* Create our sysctl tree.  We just store the softc pointer for
305 	 * now;  the sysctl_helper function will take care of creating
306 	 * a real string on the fly.  We explicitly specify the new nodes'
307 	 * sysctl_num in order to identify the specific limit rather than
308 	 * using CTL_CREATE;  this is OK since we're the only place that
309 	 * touches the sysctl tree for the device.
310 	 */
311 
312 	if (hw_node != CTL_EOL)
313 		sysctl_createv(NULL, 0, NULL, &node, 0,
314 		    CTLTYPE_NODE, device_xname(self),
315 		    NULL, NULL, 0, NULL, 0,
316 		    CTL_HW, CTL_CREATE, CTL_EOL);
317 	if (node != NULL) {
318 		dev_sysctl_num = node->sysctl_num;
319 		sysctl_createv(NULL, 0, NULL, &node, 0,
320 		    CTLTYPE_NODE, "limits",
321 		    SYSCTL_DESCR("temperature limits"),
322 		    NULL, 0, NULL, 0,
323 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
324 	}
325 	if (node != NULL) {
326 		sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READWRITE,
327 		    CTLTYPE_INT, "low_limit",
328 		    SYSCTL_DESCR("alarm window lower limit"),
329 		    sdtemp_sysctl_helper, 0, sc, sizeof(int),
330 		    CTL_HW, dev_sysctl_num, node->sysctl_num,
331 			SDTEMP_REG_LOWER_LIM, CTL_EOL);
332 		sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READWRITE,
333 		    CTLTYPE_INT, "high_limit",
334 		    SYSCTL_DESCR("alarm window upper limit"),
335 		    sdtemp_sysctl_helper, 0, sc, sizeof(int),
336 		    CTL_HW, dev_sysctl_num, node->sysctl_num,
337 			SDTEMP_REG_UPPER_LIM, CTL_EOL);
338 		sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READWRITE,
339 		    CTLTYPE_INT, "crit_limit",
340 		    SYSCTL_DESCR("critical alarm limit"),
341 		    sdtemp_sysctl_helper, 0, sc, sizeof(int),
342 		    CTL_HW, dev_sysctl_num, node->sysctl_num,
343 			SDTEMP_REG_CRIT_LIM, CTL_EOL);
344 	}
345 	return;
346 
347 bad:
348 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
349 bad2:
350 	sysmon_envsys_destroy(sc->sc_sme);
351 }
352 
353 /* Set up the threshold registers */
354 static void
355 sdtemp_set_thresh(struct sdtemp_softc *sc, int reg, uint16_t val)
356 {
357 	int error;
358 	uint16_t *valp;
359 
360 	switch (reg) {
361 	case SDTEMP_REG_LOWER_LIM:
362 		valp = &sc->sc_low_lim;
363 		break;
364 	case SDTEMP_REG_UPPER_LIM:
365 		valp = &sc->sc_high_lim;
366 		break;
367 	case SDTEMP_REG_CRIT_LIM:
368 		valp = &sc->sc_crit_lim;
369 		break;
370 	default:
371 		return;
372 	}
373 
374 	iic_acquire_bus(sc->sc_tag, 0);
375 	error = sdtemp_write_16(sc, reg, (val << 4) & SDTEMP_TEMP_MASK);
376 	iic_release_bus(sc->sc_tag, 0);
377 
378 	if (error == 0)
379 		*valp = val;
380 }
381 
382 #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
383 
384 /* Read a 8-bit value from a register */
385 static int
386 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
387 {
388 	int error;
389 
390 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
391 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
392 
393 	return error;
394 }
395 
396 static int
397 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
398 {
399 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
400 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
401 }
402 #endif /* NOT_YET */
403 
404 /* Read a 16-bit value from a register */
405 static int
406 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
407 {
408 	int error;
409 
410 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
411 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
412 	if (error)
413 		return error;
414 
415 	*valp = be16toh(*valp);
416 
417 	return 0;
418 }
419 
420 static int
421 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
422 {
423 	uint16_t temp;
424 
425 	temp = htobe16(val);
426 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
427 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
428 }
429 
430 static uint32_t
431 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
432 {
433 	uint32_t val;
434 	int32_t stemp;
435 
436 	/* Get only the temperature bits */
437 	temp &= SDTEMP_TEMP_MASK;
438 
439 	/* If necessary, extend the sign bit */
440 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
441 	    (temp & SDTEMP_TEMP_NEGATIVE))
442 		temp |= SDTEMP_TEMP_SIGN_EXT;
443 
444 	/* Mask off only bits valid within current resolution */
445 	temp &= ~(0xf >> sc->sc_resolution);
446 
447 	/* Treat as signed and extend to 32-bits */
448 	stemp = (int16_t)temp;
449 
450 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
451 	val = (stemp * 62500) + 273150000;
452 
453 	return val;
454 }
455 
456 static void
457 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
458 {
459 	struct sdtemp_softc *sc = sme->sme_cookie;
460 	uint16_t val;
461 	int error;
462 
463 	iic_acquire_bus(sc->sc_tag, 0);
464 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
465 	iic_release_bus(sc->sc_tag, 0);
466 
467 	if (error) {
468 		edata->state = ENVSYS_SINVALID;
469 		return;
470 	}
471 
472 	edata->value_cur = sdtemp_decode_temp(sc, val);
473 
474 	/* Now check for limits */
475 	if (val & SDTEMP_ABOVE_CRIT)
476 		edata->state = ENVSYS_SCRITOVER;
477 	else if (val & SDTEMP_ABOVE_UPPER)
478 		edata->state = ENVSYS_SWARNOVER;
479 	else if (val & SDTEMP_BELOW_LOWER)
480 		edata->state = ENVSYS_SWARNUNDER;
481 	else
482 		edata->state = ENVSYS_SVALID;
483 }
484 
485 SYSCTL_SETUP(sysctl_sdtemp_setup, "sysctl hw.sdtemp subtree setup")
486 {
487 	const struct sysctlnode *node;
488 
489 	if (sysctl_createv(clog, 0, NULL, &node,
490 			   CTLFLAG_PERMANENT,
491 			   CTLTYPE_NODE, "hw", NULL,
492 			   NULL, 0, NULL, 0,
493 			   CTL_HW, CTL_EOL) != 0)
494 		return;
495 
496 	hw_node = node->sysctl_num;
497 }
498 
499 /*
500  * The sysctl node actually contains just a pointer to our softc.  We
501  * extract the individual limits on the fly, and if necessary replace
502  * the value with the new value specified by the user.
503  *
504  * Inspired by similar code in sys/net/if_tap.c
505  */
506 static int
507 sdtemp_sysctl_helper(SYSCTLFN_ARGS)
508 {
509 	struct sdtemp_softc *sc;
510 	struct sysctlnode node;
511 	int error, reg;
512 	uint16_t reg_value;
513 	int lim_value;
514 
515 	node = *rnode;
516 	sc = node.sysctl_data;
517 	reg = node.sysctl_num;
518 
519 	iic_acquire_bus(sc->sc_tag, 0);
520 	error = sdtemp_read_16(sc, reg, &reg_value);
521 	iic_release_bus(sc->sc_tag, 0);
522 
523 #ifdef DEBUG
524 	aprint_verbose_dev(sc->sc_dev, "(%s) sc %p reg %d val 0x%04x err %d\n",
525 	    __func__, sc, reg, reg_value, error);
526 #endif
527 
528 	if (error == 0) {
529 		lim_value = reg_value >> 4;
530 		node.sysctl_data = &lim_value;
531 		error = sysctl_lookup(SYSCTLFN_CALL(&node));
532 	}
533 	if (error || newp == NULL)
534 		return (error);
535 
536 	/*
537 	 * We're being asked to update the sysctl value, so retrieve
538 	 * the new value and check for valid range
539 	 */
540 	lim_value = *(int *)node.sysctl_data;
541 	if (lim_value < -256 || lim_value > 255)
542 		return (EINVAL);
543 
544 	sdtemp_set_thresh(sc, reg, (uint16_t)lim_value);
545 
546 	return (0);
547 }
548 
549 /*
550  * power management functions
551  *
552  * We go into "shutdown" mode at suspend time, and return to normal
553  * mode upon resume.  This reduces power consumption by disabling
554  * the A/D converter.
555  */
556 
557 static bool
558 sdtemp_pmf_suspend(device_t dev PMF_FN_ARGS)
559 {
560 	struct sdtemp_softc *sc = device_private(dev);
561 	int error;
562 	uint16_t config;
563 
564 	iic_acquire_bus(sc->sc_tag, 0);
565 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
566 	if (error == 0) {
567 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
568 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
569 	}
570 	iic_release_bus(sc->sc_tag, 0);
571 	return (error == 0);
572 }
573 
574 static bool
575 sdtemp_pmf_resume(device_t dev PMF_FN_ARGS)
576 {
577 	struct sdtemp_softc *sc = device_private(dev);
578 	int error;
579 	uint16_t config;
580 
581 	iic_acquire_bus(sc->sc_tag, 0);
582 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
583 	if (error == 0) {
584 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
585 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
586 	}
587 	iic_release_bus(sc->sc_tag, 0);
588 	return (error == 0);
589 }
590