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