xref: /netbsd-src/sys/dev/i2c/sdtemp.c (revision 5c46dd73a9bcb28b2994504ea090f64066b17a77)
1 /*      $NetBSD: sdtemp.c,v 1.13 2010/04/10 19:02:39 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.13 2010/04/10 19:02:39 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 
42 #include <dev/sysmon/sysmonvar.h>
43 
44 #include <dev/i2c/i2cvar.h>
45 #include <dev/i2c/sdtemp_reg.h>
46 
47 struct sdtemp_softc {
48 	device_t sc_dev;
49 	i2c_tag_t sc_tag;
50 	int sc_address;
51 
52 	struct sysmon_envsys *sc_sme;
53 	envsys_data_t *sc_sensor;
54 	sysmon_envsys_lim_t sc_deflims;
55 	uint32_t sc_defprops;
56 	int sc_resolution;
57 	uint16_t sc_capability;
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 static void	sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
68 				  sysmon_envsys_lim_t *, uint32_t *);
69 static void	sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
70 				  sysmon_envsys_lim_t *, uint32_t *);
71 #ifdef NOT_YET
72 static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
73 static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
74 #endif /* NOT YET */
75 static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
76 static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
77 static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
78 static bool	sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
79 static bool	sdtemp_pmf_resume(device_t, const pmf_qual_t *);
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 /* Convert sysmon_envsys uKelvin value to simple degC */
90 
91 #define	__UK2C(uk) (((uk) - 273150000) / 1000000)
92 
93 /*
94  * List of devices known to conform to JEDEC JC42.4
95  *
96  * NOTE: A non-negative value for resolution indicates that the sensor
97  * resolution is fixed at that number of fractional bits;  a negative
98  * value indicates that the sensor needs to be configured.  In either
99  * case, trip-point registers are fixed at two-bit (0.25C) resolution.
100  */
101 static const struct sdtemp_dev_entry
102 sdtemp_dev_table[] = {
103     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,    0xff, 3,
104 	"Maxim MAX604" },
105     { MCP_MANUFACTURER_ID,   MCP_9805_DEVICE_ID,    0xff, 2,
106 	"Microchip Tech MCP9805" },
107     { MCP_MANUFACTURER_ID,   MCP_98242_DEVICE_ID,   0xff, -4,
108 	"Microchip Tech MCP98242" },
109     { ADT_MANUFACTURER_ID,   ADT_7408_DEVICE_ID,    0xff, 4,
110 	"Analog Devices ADT7408" },
111     { NXP_MANUFACTURER_ID,   NXP_SE97_DEVICE_ID,    0xff, 3,
112 	"NXP Semiconductors SE97/SE98" },
113     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x00, 2,
114 	"STmicroelectronics STTS424E02-DA" },
115     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x01, 2,
116 	"STmicroelectronics STTS424E02-DN" },
117     { CAT_MANUFACTURER_ID,   CAT_34TS02_DEVICE_ID,  0xff, 4,
118 	"Catalyst CAT34TS02/CAT6095" },
119     { 0, 0, 0, 2, "Unknown" }
120 };
121 
122 static int
123 sdtemp_lookup(uint16_t mfg, uint16_t dev, uint16_t rev)
124 {
125 	int i;
126 
127 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++)
128 		if (sdtemp_dev_table[i].sdtemp_mfg_id == mfg &&
129 		    sdtemp_dev_table[i].sdtemp_dev_id == dev &&
130 		    (sdtemp_dev_table[i].sdtemp_rev_id == 0xff ||
131 		     sdtemp_dev_table[i].sdtemp_rev_id == rev))
132 			break;
133 
134 	return i;
135 }
136 
137 static int
138 sdtemp_match(device_t parent, cfdata_t cf, void *aux)
139 {
140 	struct i2c_attach_args *ia = aux;
141 	uint16_t mfgid, devid;
142 	struct sdtemp_softc sc;
143 	int i, error;
144 
145 	sc.sc_tag = ia->ia_tag;
146 	sc.sc_address = ia->ia_addr;
147 
148 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
149 		return 0;
150 
151 	/* Verify that we can read the manufacturer ID  & Device ID */
152 	iic_acquire_bus(sc.sc_tag, 0);
153 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
154 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
155 	iic_release_bus(sc.sc_tag, 0);
156 
157 	if (error)
158 		return 0;
159 
160 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
161 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
162 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
163 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
164 		    devid & 0xff, sc.sc_address);
165 		return 0;
166 	}
167 
168 	return 1;
169 }
170 
171 static void
172 sdtemp_attach(device_t parent, device_t self, void *aux)
173 {
174 	struct sdtemp_softc *sc = device_private(self);
175 	struct i2c_attach_args *ia = aux;
176 	sysmon_envsys_lim_t limits;
177 	uint32_t props;
178 	uint16_t mfgid, devid;
179 	int i, error;
180 
181 	sc->sc_tag = ia->ia_tag;
182 	sc->sc_address = ia->ia_addr;
183 	sc->sc_dev = self;
184 
185 	iic_acquire_bus(sc->sc_tag, 0);
186 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
187 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
188 		iic_release_bus(sc->sc_tag, 0);
189 		aprint_error(": attach error %d\n", error);
190 		return;
191 	}
192 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
193 	sc->sc_resolution =
194 	    sdtemp_dev_table[i].sdtemp_resolution;
195 
196 	aprint_naive(": Temp Sensor\n");
197 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
198 
199 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
200 		aprint_debug_dev(self,
201 		    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
202 		    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
203 
204 	/*
205 	 * Alarm capability is required;  if not present, this is likely
206 	 * not a real sdtemp device.
207 	 */
208 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
209 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
210 		iic_release_bus(sc->sc_tag, 0);
211 		aprint_error_dev(self,
212 		    "required alarm capability not present!\n");
213 		return;
214 	}
215 	/* Set the configuration to defaults. */
216 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
217 	if (error != 0) {
218 		iic_release_bus(sc->sc_tag, 0);
219 		aprint_error_dev(self, "error %d writing config register\n",
220 		    error);
221 		return;
222 	}
223 	/* If variable resolution, set to max */
224 	if (sc->sc_resolution < 0) {
225 		sc->sc_resolution = ~sc->sc_resolution;
226 		error = sdtemp_write_16(sc, SDTEMP_REG_RESOLUTION,
227 					sc->sc_resolution & 0x3);
228 		if (error != 0) {
229 			iic_release_bus(sc->sc_tag, 0);
230 			aprint_error_dev(self,
231 			    "error %d writing resolution register\n", error);
232 			return;
233 		} else
234 			sc->sc_resolution++;
235 	}
236 	iic_release_bus(sc->sc_tag, 0);
237 
238 	/* Hook us into the sysmon_envsys subsystem */
239 	sc->sc_sme = sysmon_envsys_create();
240 	sc->sc_sme->sme_name = device_xname(self);
241 	sc->sc_sme->sme_cookie = sc;
242 	sc->sc_sme->sme_refresh = sdtemp_refresh;
243 	sc->sc_sme->sme_get_limits = sdtemp_get_limits;
244 	sc->sc_sme->sme_set_limits = sdtemp_set_limits;
245 
246 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
247 	if (!sc->sc_sensor) {
248 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
249 		goto bad2;
250 	}
251 
252 	/* Initialize sensor data. */
253 	sc->sc_sensor->units =  ENVSYS_STEMP;
254 	sc->sc_sensor->state = ENVSYS_SINVALID;
255 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
256 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
257 	    sizeof(sc->sc_sensor->desc));
258 
259 	/* Now attach the sensor */
260 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
261 		aprint_error_dev(self, "unable to attach sensor\n");
262 		goto bad;
263 	}
264 
265 	/* Register the device */
266 	error = sysmon_envsys_register(sc->sc_sme);
267 	if (error) {
268 		aprint_error_dev(self, "error %d registering with sysmon\n",
269 		    error);
270 		goto bad;
271 	}
272 
273 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
274 		aprint_error_dev(self, "couldn't establish power handler\n");
275 
276 	/* Retrieve and display hardware monitor limits */
277 	sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &limits, &props);
278 	aprint_normal_dev(self, "");
279 	i = 0;
280 	if (props & PROP_WARNMIN) {
281 		aprint_normal("low limit %dC", __UK2C(limits.sel_warnmin));
282 		i++;
283 	}
284 	if (props & PROP_WARNMAX) {
285 		aprint_normal("%shigh limit %dC ", (i)?", ":"",
286 			      __UK2C(limits.sel_warnmax));
287 		i++;
288 	}
289 	if (props & PROP_CRITMAX) {
290 		aprint_normal("%scritical limit %dC ", (i)?", ":"",
291 			      __UK2C(limits.sel_critmax));
292 		i++;
293 	}
294 	if (i == 0)
295 		aprint_normal("no hardware limits set\n");
296 	else
297 		aprint_normal("\n");
298 
299 	return;
300 
301 bad:
302 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
303 bad2:
304 	sysmon_envsys_destroy(sc->sc_sme);
305 }
306 
307 /* Retrieve current limits from device, and encode in uKelvins */
308 static void
309 sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
310 		  sysmon_envsys_lim_t *limits, uint32_t *props)
311 {
312 	struct sdtemp_softc *sc = sme->sme_cookie;
313 	uint16_t lim;
314 
315 	*props = 0;
316 	iic_acquire_bus(sc->sc_tag, 0);
317 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
318 		limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
319 		*props |= PROP_WARNMIN;
320 	}
321 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
322 		limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
323 		*props |= PROP_WARNMAX;
324 	}
325 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
326 		limits->sel_critmax = sdtemp_decode_temp(sc, lim);
327 		*props |= PROP_CRITMAX;
328 	}
329 	iic_release_bus(sc->sc_tag, 0);
330 	if (*props != 0)
331 		*props |= PROP_DRIVER_LIMITS;
332 	if (sc->sc_defprops == 0) {
333 		sc->sc_deflims  = *limits;
334 		sc->sc_defprops = *props;
335 	}
336 }
337 
338 /* Send current limit values to the device */
339 static void
340 sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
341 		  sysmon_envsys_lim_t *limits, uint32_t *props)
342 {
343 	uint16_t val;
344 	struct sdtemp_softc *sc = sme->sme_cookie;
345 
346 	if (limits == NULL) {
347 		limits = &sc->sc_deflims;
348 		props  = &sc->sc_defprops;
349 	}
350 	iic_acquire_bus(sc->sc_tag, 0);
351 	if (*props & PROP_WARNMIN) {
352 		val = __UK2C(limits->sel_warnmin);
353 		(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
354 					(val << 4) & SDTEMP_TEMP_MASK);
355 	}
356 	if (*props & PROP_WARNMAX) {
357 		val = __UK2C(limits->sel_warnmax);
358 		(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
359 					(val << 4) & SDTEMP_TEMP_MASK);
360 	}
361 	if (*props & PROP_CRITMAX) {
362 		val = __UK2C(limits->sel_critmax);
363 		(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
364 					(val << 4) & SDTEMP_TEMP_MASK);
365 	}
366 	iic_release_bus(sc->sc_tag, 0);
367 
368 	/*
369 	 * If at least one limit is set that we can handle, and no
370 	 * limits are set that we cannot handle, tell sysmon that
371 	 * the driver will take care of monitoring the limits!
372 	 */
373 	if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
374 		*props &= ~PROP_DRIVER_LIMITS;
375 	else if (*props & PROP_LIMITS)
376 		*props |= PROP_DRIVER_LIMITS;
377 	else
378 		*props &= ~PROP_DRIVER_LIMITS;
379 }
380 
381 #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
382 
383 /* Read a 8-bit value from a register */
384 static int
385 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
386 {
387 	int error;
388 
389 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
390 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
391 
392 	return error;
393 }
394 
395 static int
396 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
397 {
398 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
399 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
400 }
401 #endif /* NOT_YET */
402 
403 /* Read a 16-bit value from a register */
404 static int
405 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
406 {
407 	int error;
408 
409 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
410 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
411 	if (error)
412 		return error;
413 
414 	*valp = be16toh(*valp);
415 
416 	return 0;
417 }
418 
419 static int
420 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
421 {
422 	uint16_t temp;
423 
424 	temp = htobe16(val);
425 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
426 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
427 }
428 
429 static uint32_t
430 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
431 {
432 	uint32_t val;
433 	int32_t stemp;
434 
435 	/* Get only the temperature bits */
436 	temp &= SDTEMP_TEMP_MASK;
437 
438 	/* If necessary, extend the sign bit */
439 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
440 	    (temp & SDTEMP_TEMP_NEGATIVE))
441 		temp |= SDTEMP_TEMP_SIGN_EXT;
442 
443 	/* Mask off only bits valid within current resolution */
444 	temp &= ~(0xf >> sc->sc_resolution);
445 
446 	/* Treat as signed and extend to 32-bits */
447 	stemp = (int16_t)temp;
448 
449 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
450 	val = (stemp * 62500) + 273150000;
451 
452 	return val;
453 }
454 
455 static void
456 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
457 {
458 	struct sdtemp_softc *sc = sme->sme_cookie;
459 	uint16_t val;
460 	int error;
461 
462 	iic_acquire_bus(sc->sc_tag, 0);
463 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
464 	iic_release_bus(sc->sc_tag, 0);
465 
466 	if (error) {
467 		edata->state = ENVSYS_SINVALID;
468 		return;
469 	}
470 
471 	edata->value_cur = sdtemp_decode_temp(sc, val);
472 
473 	/* Now check for limits */
474 	if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
475 		edata->state = ENVSYS_SVALID;
476 	else if (val & SDTEMP_ABOVE_CRIT)
477 		edata->state = ENVSYS_SCRITOVER;
478 	else if (val & SDTEMP_ABOVE_UPPER)
479 		edata->state = ENVSYS_SWARNOVER;
480 	else if (val & SDTEMP_BELOW_LOWER)
481 		edata->state = ENVSYS_SWARNUNDER;
482 	else
483 		edata->state = ENVSYS_SVALID;
484 }
485 
486 /*
487  * power management functions
488  *
489  * We go into "shutdown" mode at suspend time, and return to normal
490  * mode upon resume.  This reduces power consumption by disabling
491  * the A/D converter.
492  */
493 
494 static bool
495 sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
496 {
497 	struct sdtemp_softc *sc = device_private(dev);
498 	int error;
499 	uint16_t config;
500 
501 	iic_acquire_bus(sc->sc_tag, 0);
502 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
503 	if (error == 0) {
504 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
505 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
506 	}
507 	iic_release_bus(sc->sc_tag, 0);
508 	return (error == 0);
509 }
510 
511 static bool
512 sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
513 {
514 	struct sdtemp_softc *sc = device_private(dev);
515 	int error;
516 	uint16_t config;
517 
518 	iic_acquire_bus(sc->sc_tag, 0);
519 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
520 	if (error == 0) {
521 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
522 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
523 	}
524 	iic_release_bus(sc->sc_tag, 0);
525 	return (error == 0);
526 }
527