sdtemp.c revision 1.9.2.2 1 /* $NetBSD: sdtemp.c,v 1.9.2.2 2010/08/17 06:46:08 uebayasi 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.9.2.2 2010/08/17 06:46:08 uebayasi 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 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, &sc->sc_deflims,
283 &sc->sc_defprops);
284 aprint_normal_dev(self, "");
285 i = 0;
286 if (sc->sc_defprops & PROP_WARNMIN) {
287 aprint_normal("low limit %dC",
288 __UK2C(sc->sc_deflims.sel_warnmin));
289 i++;
290 }
291 if (sc->sc_defprops & PROP_WARNMAX) {
292 aprint_normal("%shigh limit %dC ", (i)?", ":"",
293 __UK2C(sc->sc_deflims.sel_warnmax));
294 i++;
295 }
296 if (sc->sc_defprops & PROP_CRITMAX) {
297 aprint_normal("%scritical limit %dC ", (i)?", ":"",
298 __UK2C(sc->sc_deflims.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 }
340
341 /* Send current limit values to the device */
342 static void
343 sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
344 sysmon_envsys_lim_t *limits, uint32_t *props)
345 {
346 uint16_t val;
347 struct sdtemp_softc *sc = sme->sme_cookie;
348
349 if (limits == NULL) {
350 limits = &sc->sc_deflims;
351 props = &sc->sc_defprops;
352 }
353 iic_acquire_bus(sc->sc_tag, 0);
354 if (*props & PROP_WARNMIN) {
355 val = __UK2C(limits->sel_warnmin);
356 (void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
357 (val << 4) & SDTEMP_TEMP_MASK);
358 }
359 if (*props & PROP_WARNMAX) {
360 val = __UK2C(limits->sel_warnmax);
361 (void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
362 (val << 4) & SDTEMP_TEMP_MASK);
363 }
364 if (*props & PROP_CRITMAX) {
365 val = __UK2C(limits->sel_critmax);
366 (void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
367 (val << 4) & SDTEMP_TEMP_MASK);
368 }
369 iic_release_bus(sc->sc_tag, 0);
370
371 /*
372 * If at least one limit is set that we can handle, and no
373 * limits are set that we cannot handle, tell sysmon that
374 * the driver will take care of monitoring the limits!
375 */
376 if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
377 *props &= ~PROP_DRIVER_LIMITS;
378 else if (*props & PROP_LIMITS)
379 *props |= PROP_DRIVER_LIMITS;
380 else
381 *props &= ~PROP_DRIVER_LIMITS;
382 }
383
384 #ifdef NOT_YET /* All registers on these sensors are 16-bits */
385
386 /* Read a 8-bit value from a register */
387 static int
388 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
389 {
390 int error;
391
392 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
393 sc->sc_address, ®, 1, valp, sizeof(*valp), 0);
394
395 return error;
396 }
397
398 static int
399 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
400 {
401 return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
402 sc->sc_address, ®, 1, &val, sizeof(val), 0);
403 }
404 #endif /* NOT_YET */
405
406 /* Read a 16-bit value from a register */
407 static int
408 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
409 {
410 int error;
411
412 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
413 sc->sc_address, ®, 1, valp, sizeof(*valp), 0);
414 if (error)
415 return error;
416
417 *valp = be16toh(*valp);
418
419 return 0;
420 }
421
422 static int
423 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
424 {
425 uint16_t temp;
426
427 temp = htobe16(val);
428 return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
429 sc->sc_address, ®, 1, &temp, sizeof(temp), 0);
430 }
431
432 static uint32_t
433 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
434 {
435 uint32_t val;
436 int32_t stemp;
437
438 /* Get only the temperature bits */
439 temp &= SDTEMP_TEMP_MASK;
440
441 /* If necessary, extend the sign bit */
442 if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
443 (temp & SDTEMP_TEMP_NEGATIVE))
444 temp |= SDTEMP_TEMP_SIGN_EXT;
445
446 /* Mask off only bits valid within current resolution */
447 temp &= ~(0xf >> sc->sc_resolution);
448
449 /* Treat as signed and extend to 32-bits */
450 stemp = (int16_t)temp;
451
452 /* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
453 val = (stemp * 62500) + 273150000;
454
455 return val;
456 }
457
458 static void
459 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
460 {
461 struct sdtemp_softc *sc = sme->sme_cookie;
462 uint16_t val;
463 int error;
464
465 iic_acquire_bus(sc->sc_tag, 0);
466 error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
467 iic_release_bus(sc->sc_tag, 0);
468
469 if (error) {
470 edata->state = ENVSYS_SINVALID;
471 return;
472 }
473
474 edata->value_cur = sdtemp_decode_temp(sc, val);
475
476 /* Now check for limits */
477 if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
478 edata->state = ENVSYS_SVALID;
479 else if ((val & SDTEMP_ABOVE_CRIT) &&
480 (edata->upropset & PROP_CRITMAX))
481 edata->state = ENVSYS_SCRITOVER;
482 else if ((val & SDTEMP_ABOVE_UPPER) &&
483 (edata->upropset & PROP_WARNMAX))
484 edata->state = ENVSYS_SWARNOVER;
485 else if ((val & SDTEMP_BELOW_LOWER) &&
486 (edata->upropset & PROP_WARNMIN))
487 edata->state = ENVSYS_SWARNUNDER;
488 else
489 edata->state = ENVSYS_SVALID;
490 }
491
492 /*
493 * power management functions
494 *
495 * We go into "shutdown" mode at suspend time, and return to normal
496 * mode upon resume. This reduces power consumption by disabling
497 * the A/D converter.
498 */
499
500 static bool
501 sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
502 {
503 struct sdtemp_softc *sc = device_private(dev);
504 int error;
505 uint16_t config;
506
507 iic_acquire_bus(sc->sc_tag, 0);
508 error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
509 if (error == 0) {
510 config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
511 error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
512 }
513 iic_release_bus(sc->sc_tag, 0);
514 return (error == 0);
515 }
516
517 static bool
518 sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
519 {
520 struct sdtemp_softc *sc = device_private(dev);
521 int error;
522 uint16_t config;
523
524 iic_acquire_bus(sc->sc_tag, 0);
525 error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
526 if (error == 0) {
527 config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
528 error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
529 }
530 iic_release_bus(sc->sc_tag, 0);
531 return (error == 0);
532 }
533