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