sdtemp.c revision 1.29 1 /* $NetBSD: sdtemp.c,v 1.29 2016/07/26 08:13:57 msaitoh 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.29 2016/07/26 08:13:57 msaitoh 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/module.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 sysmon_envsys_lim_t sc_deflims;
56 uint32_t sc_defprops;
57 int sc_resolution;
58 uint16_t sc_mfgid;
59 uint16_t sc_devid;
60 uint16_t sc_devid_masked;
61 uint16_t sc_capability;
62 };
63
64 static int sdtemp_match(device_t, cfdata_t, void *);
65 static void sdtemp_attach(device_t, device_t, void *);
66 static int sdtemp_detach(device_t, int);
67
68 CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
69 sdtemp_match, sdtemp_attach, sdtemp_detach, NULL);
70
71 static void sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
72 static void sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
73 sysmon_envsys_lim_t *, uint32_t *);
74 static void sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
75 sysmon_envsys_lim_t *, uint32_t *);
76 #ifdef NOT_YET
77 static int sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
78 static int sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
79 #endif /* NOT YET */
80 static int sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
81 static int sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
82 static uint32_t sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
83 static bool sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
84 static bool sdtemp_pmf_resume(device_t, const pmf_qual_t *);
85 /* Device dependent config functions */
86 static void sdtemp_config_mcp(struct sdtemp_softc *);
87 static void sdtemp_config_idt(struct sdtemp_softc *);
88
89 struct sdtemp_dev_entry {
90 const uint16_t sdtemp_mfg_id;
91 const uint16_t sdtemp_devrev;
92 const uint16_t sdtemp_mask;
93 void (*sdtemp_config)(struct sdtemp_softc *);
94 const char *sdtemp_desc;
95 };
96
97 /* Convert sysmon_envsys uKelvin value to simple degC */
98
99 #define __UK2C(uk) (((uk) - 273150000) / 1000000)
100
101 /* List of devices known to conform to JEDEC JC42.4 */
102
103 #define CMCP sdtemp_config_mcp
104 #define CIDT sdtemp_config_idt
105
106 static const struct sdtemp_dev_entry
107 sdtemp_dev_table[] = {
108 { AT_MANUFACTURER_ID, AT_30TS00_DEVICE_ID, AT_30TS00_MASK, NULL,
109 "Atmel AT30TS00" },
110 { AT2_MANUFACTURER_ID, AT2_30TSE004_DEVICE_ID, AT2_30TSE004_MASK, NULL,
111 "Atmel AT30TSE004" },
112 { GT_MANUFACTURER_ID, GT_30TS00_DEVICE_ID, GT_30TS00_MASK, NULL,
113 "Giantec GT30TS00" },
114 { GT2_MANUFACTURER_ID, GT2_34TS02_DEVICE_ID, GT2_34TS02_MASK, NULL,
115 "Giantec GT34TS02" },
116 { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID, MAX_6604_MASK, NULL,
117 "Maxim MAX6604" },
118 { MCP_MANUFACTURER_ID, MCP_9804_DEVICE_ID, MCP_9804_MASK, CMCP,
119 "Microchip Tech MCP9804" },
120 { MCP_MANUFACTURER_ID, MCP_9805_DEVICE_ID, MCP_9805_MASK, NULL,
121 "Microchip Tech MCP9805/MCP9843" },
122 { MCP_MANUFACTURER_ID, MCP_98242_DEVICE_ID, MCP_98242_MASK, CMCP,
123 "Microchip Tech MCP98242" },
124 { MCP_MANUFACTURER_ID, MCP_98243_DEVICE_ID, MCP_98243_MASK, CMCP,
125 "Microchip Tech MCP98243" },
126 { MCP_MANUFACTURER_ID, MCP_98244_DEVICE_ID, MCP_98244_MASK, CMCP,
127 "Microchip Tech MCP98244" },
128 { ADT_MANUFACTURER_ID, ADT_7408_DEVICE_ID, ADT_7408_MASK, NULL,
129 "Analog Devices ADT7408" },
130 { NXP_MANUFACTURER_ID, NXP_SE98_DEVICE_ID, NXP_SE98_MASK, NULL,
131 "NXP Semiconductors SE97B/SE98" },
132 { NXP_MANUFACTURER_ID, NXP_SE97_DEVICE_ID, NXP_SE97_MASK, NULL,
133 "NXP Semiconductors SE97" },
134 { STTS_MANUFACTURER_ID, STTS_424E_DEVICE_ID, STTS_424E_MASK, NULL,
135 "STmicroelectronics STTS424E" },
136 { STTS_MANUFACTURER_ID, STTS_424_DEVICE_ID, STTS_424_MASK, NULL,
137 "STmicroelectronics STTS424" },
138 { STTS_MANUFACTURER_ID, STTS_2002_DEVICE_ID, STTS_2002_MASK, NULL,
139 "STmicroelectronics STTS2002" },
140 { STTS_MANUFACTURER_ID, STTS_2004_DEVICE_ID, STTS_2004_MASK, NULL,
141 "STmicroelectronics STTS2004" },
142 { STTS_MANUFACTURER_ID, STTS_3000_DEVICE_ID, STTS_3000_MASK, NULL,
143 "STmicroelectronics STTS3000" },
144 { CAT_MANUFACTURER_ID, CAT_34TS02_DEVICE_ID, CAT_34TS02_MASK, NULL,
145 "Catalyst CAT34TS02/CAT6095" },
146 { CAT_MANUFACTURER_ID, CAT_34TS02C_DEVICE_ID, CAT_34TS02C_MASK, NULL,
147 "Catalyst CAT34TS02C" },
148 { CAT_MANUFACTURER_ID, CAT_34TS04_DEVICE_ID, CAT_34TS04_MASK, NULL,
149 "Catalyst CAT34TS04" },
150 { IDT_MANUFACTURER_ID, IDT_TSE2002GB2_DEVICE_ID,IDT_TSE2002GB2_MASK, CIDT,
151 "Integrated Device Technology TSE2002GB2" },
152 { IDT_MANUFACTURER_ID, IDT_TSE2004GB2_DEVICE_ID,IDT_TSE2004GB2_MASK, NULL,
153 "Integrated Device Technology TSE2004GB2" },
154 { IDT_MANUFACTURER_ID, IDT_TS3000B3_DEVICE_ID, IDT_TS3000B3_MASK, CIDT,
155 "Integrated Device Technology TS3000B3/TSE2002B3" },
156 { IDT_MANUFACTURER_ID, IDT_TS3000GB0_DEVICE_ID, IDT_TS3000GB0_MASK, CIDT,
157 "Integrated Device Technology TS3000GB0" },
158 { IDT_MANUFACTURER_ID, IDT_TS3000GB2_DEVICE_ID, IDT_TS3000GB2_MASK, CIDT,
159 "Integrated Device Technology TS3000GB2" },
160 { IDT_MANUFACTURER_ID, IDT_TS3001GB2_DEVICE_ID, IDT_TS3001GB2_MASK, CIDT,
161 "Integrated Device Technology TS3001GB2" },
162 { 0, 0, 0, NULL, "Unknown" }
163 };
164
165 #undef CMCP
166 #undef CIDT
167
168 static const char *temp_resl[] = {
169 "0.5C",
170 "0.25C",
171 "0.125C",
172 "0.0625C"
173 };
174
175 static int
176 sdtemp_lookup(uint16_t mfg, uint16_t devrev)
177 {
178 int i;
179
180 for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++) {
181 if (mfg != sdtemp_dev_table[i].sdtemp_mfg_id)
182 continue;
183 if ((devrev & sdtemp_dev_table[i].sdtemp_mask) ==
184 sdtemp_dev_table[i].sdtemp_devrev)
185 break;
186 }
187
188 return i;
189 }
190
191 static int
192 sdtemp_match(device_t parent, cfdata_t cf, void *aux)
193 {
194 struct i2c_attach_args *ia = aux;
195 uint16_t mfgid, devid;
196 struct sdtemp_softc sc;
197 int i, error;
198
199 sc.sc_tag = ia->ia_tag;
200 sc.sc_address = ia->ia_addr;
201
202 if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
203 return 0;
204
205 /* Verify that we can read the manufacturer ID & Device ID */
206 iic_acquire_bus(sc.sc_tag, 0);
207 error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID, &mfgid) |
208 sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
209 iic_release_bus(sc.sc_tag, 0);
210
211 if (error)
212 return 0;
213
214 i = sdtemp_lookup(mfgid, devid);
215 if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
216 aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%04x "
217 "rev 0x%02x at address 0x%02x\n", mfgid, devid,
218 devid & 0xff, sc.sc_address);
219 return 0;
220 }
221
222 return 1;
223 }
224
225 static void
226 sdtemp_attach(device_t parent, device_t self, void *aux)
227 {
228 struct sdtemp_softc *sc = device_private(self);
229 struct i2c_attach_args *ia = aux;
230 uint16_t mfgid, devid;
231 int i, error;
232
233 sc->sc_tag = ia->ia_tag;
234 sc->sc_address = ia->ia_addr;
235 sc->sc_dev = self;
236
237 iic_acquire_bus(sc->sc_tag, 0);
238 if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID, &mfgid)) != 0 ||
239 (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
240 iic_release_bus(sc->sc_tag, 0);
241 aprint_error(": attach error %d\n", error);
242 return;
243 }
244 sc->sc_mfgid = mfgid;
245 sc->sc_devid = devid;
246 i = sdtemp_lookup(mfgid, devid);
247 sc->sc_devid_masked = devid & sdtemp_dev_table[i].sdtemp_mask;
248
249 aprint_naive(": Temp Sensor\n");
250 aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
251
252 if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
253 aprint_debug_dev(self,
254 "mfg 0x%04x dev 0x%04x rev 0x%02x at addr 0x%02x\n",
255 mfgid, devid, devid & 0xff, ia->ia_addr);
256
257 error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
258 aprint_debug_dev(self, "capability reg = %04x\n", sc->sc_capability);
259 sc->sc_resolution
260 = __SHIFTOUT(sc->sc_capability, SDTEMP_CAP_RESOLUTION);
261 /*
262 * Call device dependent function here. Currently, it's used for
263 * the resolution.
264 *
265 * IDT's devices and some Microchip's devices have the resolution
266 * register in the vendor specific registers area. The devices'
267 * resolution bits in the capability register are not the maximum
268 * resolution but the current vaule of the setting.
269 */
270 if (sdtemp_dev_table[i].sdtemp_config != NULL)
271 sdtemp_dev_table[i].sdtemp_config(sc);
272
273 aprint_normal_dev(self, "%s accuracy",
274 (sc->sc_capability & SDTEMP_CAP_ACCURACY_1C) ? "high" : "default");
275 if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) != 0)
276 aprint_normal(", wider range");
277 aprint_normal(", %s resolution", temp_resl[sc->sc_resolution]);
278 if ((sc->sc_capability & SDTEMP_CAP_VHV) != 0)
279 aprint_debug(", high voltage standoff");
280 aprint_debug(", %s timeout",
281 (sc->sc_capability & SDTEMP_CAP_TMOUT) ? "25-35ms" : "10-60ms");
282 if ((sc->sc_capability & SDTEMP_CAP_EVSD) != 0)
283 aprint_normal(", event with shutdown");
284 aprint_normal("\n");
285 /*
286 * Alarm capability is required; if not present, this is likely
287 * not a real sdtemp device.
288 */
289 if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
290 iic_release_bus(sc->sc_tag, 0);
291 aprint_error_dev(self,
292 "required alarm capability not present!\n");
293 return;
294 }
295 /* Set the configuration to defaults. */
296 error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
297 if (error != 0) {
298 iic_release_bus(sc->sc_tag, 0);
299 aprint_error_dev(self, "error %d writing config register\n",
300 error);
301 return;
302 }
303 iic_release_bus(sc->sc_tag, 0);
304
305 /* Hook us into the sysmon_envsys subsystem */
306 sc->sc_sme = sysmon_envsys_create();
307 sc->sc_sme->sme_name = device_xname(self);
308 sc->sc_sme->sme_cookie = sc;
309 sc->sc_sme->sme_refresh = sdtemp_refresh;
310 sc->sc_sme->sme_get_limits = sdtemp_get_limits;
311 sc->sc_sme->sme_set_limits = sdtemp_set_limits;
312
313 sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
314 if (!sc->sc_sensor) {
315 aprint_error_dev(self, "unable to allocate sc_sensor\n");
316 goto bad2;
317 }
318
319 /* Initialize sensor data. */
320 sc->sc_sensor->units = ENVSYS_STEMP;
321 sc->sc_sensor->state = ENVSYS_SINVALID;
322 sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
323 (void)strlcpy(sc->sc_sensor->desc, device_xname(self),
324 sizeof(sc->sc_sensor->desc));
325 snprintf(sc->sc_sensor->desc, sizeof(sc->sc_sensor->desc),
326 "DIMM %d temperature", sc->sc_address - SDTEMP_ADDR);
327
328 /* Now attach the sensor */
329 if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
330 aprint_error_dev(self, "unable to attach sensor\n");
331 goto bad;
332 }
333
334 /* Register the device */
335 error = sysmon_envsys_register(sc->sc_sme);
336 if (error) {
337 aprint_error_dev(self, "error %d registering with sysmon\n",
338 error);
339 goto bad;
340 }
341
342 if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
343 aprint_error_dev(self, "couldn't establish power handler\n");
344
345 /* Retrieve and display hardware monitor limits */
346 sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &sc->sc_deflims,
347 &sc->sc_defprops);
348 aprint_normal_dev(self, "Hardware limits: ");
349 i = 0;
350 if (sc->sc_defprops & PROP_WARNMIN) {
351 aprint_normal("low %dC",
352 __UK2C(sc->sc_deflims.sel_warnmin));
353 i++;
354 }
355 if (sc->sc_defprops & PROP_WARNMAX) {
356 aprint_normal("%shigh %dC ", (i)?", ":"",
357 __UK2C(sc->sc_deflims.sel_warnmax));
358 i++;
359 }
360 if (sc->sc_defprops & PROP_CRITMAX) {
361 aprint_normal("%scritical %dC ", (i)?", ":"",
362 __UK2C(sc->sc_deflims.sel_critmax));
363 i++;
364 }
365 aprint_normal("%s\n", (i)?"":"none set");
366
367 return;
368
369 bad:
370 kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
371 bad2:
372 sysmon_envsys_destroy(sc->sc_sme);
373 }
374
375 static int
376 sdtemp_detach(device_t self, int flags)
377 {
378 struct sdtemp_softc *sc = device_private(self);
379
380 pmf_device_deregister(self);
381
382 if (sc->sc_sme)
383 sysmon_envsys_unregister(sc->sc_sme);
384 if (sc->sc_sensor)
385 kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
386
387 return 0;
388 }
389
390 /* Retrieve current limits from device, and encode in uKelvins */
391 static void
392 sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
393 sysmon_envsys_lim_t *limits, uint32_t *props)
394 {
395 struct sdtemp_softc *sc = sme->sme_cookie;
396 uint16_t lim;
397
398 *props = 0;
399 iic_acquire_bus(sc->sc_tag, 0);
400 if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
401 limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
402 *props |= PROP_WARNMIN;
403 }
404 if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
405 limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
406 *props |= PROP_WARNMAX;
407 }
408 if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
409 limits->sel_critmax = sdtemp_decode_temp(sc, lim);
410 *props |= PROP_CRITMAX;
411 }
412 iic_release_bus(sc->sc_tag, 0);
413 if (*props != 0)
414 *props |= PROP_DRIVER_LIMITS;
415 }
416
417 /* Send current limit values to the device */
418 static void
419 sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
420 sysmon_envsys_lim_t *limits, uint32_t *props)
421 {
422 uint16_t val;
423 struct sdtemp_softc *sc = sme->sme_cookie;
424
425 if (limits == NULL) {
426 limits = &sc->sc_deflims;
427 props = &sc->sc_defprops;
428 }
429 iic_acquire_bus(sc->sc_tag, 0);
430 if (*props & PROP_WARNMIN) {
431 val = __UK2C(limits->sel_warnmin);
432 (void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
433 (val << 4) & SDTEMP_TEMP_MASK);
434 }
435 if (*props & PROP_WARNMAX) {
436 val = __UK2C(limits->sel_warnmax);
437 (void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
438 (val << 4) & SDTEMP_TEMP_MASK);
439 }
440 if (*props & PROP_CRITMAX) {
441 val = __UK2C(limits->sel_critmax);
442 (void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
443 (val << 4) & SDTEMP_TEMP_MASK);
444 }
445 iic_release_bus(sc->sc_tag, 0);
446
447 /*
448 * If at least one limit is set that we can handle, and no
449 * limits are set that we cannot handle, tell sysmon that
450 * the driver will take care of monitoring the limits!
451 */
452 if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
453 *props &= ~PROP_DRIVER_LIMITS;
454 else if (*props & PROP_LIMITS)
455 *props |= PROP_DRIVER_LIMITS;
456 else
457 *props &= ~PROP_DRIVER_LIMITS;
458 }
459
460 #ifdef NOT_YET /* All registers on these sensors are 16-bits */
461
462 /* Read a 8-bit value from a register */
463 static int
464 sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
465 {
466 int error;
467
468 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
469 sc->sc_address, ®, 1, valp, sizeof(*valp), 0);
470
471 return error;
472 }
473
474 static int
475 sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
476 {
477 return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
478 sc->sc_address, ®, 1, &val, sizeof(val), 0);
479 }
480 #endif /* NOT_YET */
481
482 /* Read a 16-bit value from a register */
483 static int
484 sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
485 {
486 int error;
487
488 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
489 sc->sc_address, ®, 1, valp, sizeof(*valp), 0);
490 if (error)
491 return error;
492
493 *valp = be16toh(*valp);
494
495 return 0;
496 }
497
498 static int
499 sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
500 {
501 uint16_t temp;
502
503 temp = htobe16(val);
504 return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
505 sc->sc_address, ®, 1, &temp, sizeof(temp), 0);
506 }
507
508 static uint32_t
509 sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
510 {
511 uint32_t val;
512 int32_t stemp;
513
514 /* Get only the temperature bits */
515 temp &= SDTEMP_TEMP_MASK;
516
517 /* If necessary, extend the sign bit */
518 if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
519 (temp & SDTEMP_TEMP_NEGATIVE))
520 temp |= SDTEMP_TEMP_SIGN_EXT;
521
522 /* Mask off only bits valid within current resolution */
523 temp &= ~(0x7 >> sc->sc_resolution);
524
525 /* Treat as signed and extend to 32-bits */
526 stemp = (int16_t)temp;
527
528 /* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
529 val = (stemp * 62500) + 273150000;
530
531 return val;
532 }
533
534 static void
535 sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
536 {
537 struct sdtemp_softc *sc = sme->sme_cookie;
538 uint16_t val;
539 int error;
540
541 iic_acquire_bus(sc->sc_tag, 0);
542 error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
543 iic_release_bus(sc->sc_tag, 0);
544
545 if (error) {
546 edata->state = ENVSYS_SINVALID;
547 return;
548 }
549
550 edata->value_cur = sdtemp_decode_temp(sc, val);
551
552 /* Now check for limits */
553 if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
554 edata->state = ENVSYS_SVALID;
555 else if ((val & SDTEMP_ABOVE_CRIT) &&
556 (edata->upropset & PROP_CRITMAX))
557 edata->state = ENVSYS_SCRITOVER;
558 else if ((val & SDTEMP_ABOVE_UPPER) &&
559 (edata->upropset & PROP_WARNMAX))
560 edata->state = ENVSYS_SWARNOVER;
561 else if ((val & SDTEMP_BELOW_LOWER) &&
562 (edata->upropset & PROP_WARNMIN))
563 edata->state = ENVSYS_SWARNUNDER;
564 else
565 edata->state = ENVSYS_SVALID;
566 }
567
568 /*
569 * power management functions
570 *
571 * We go into "shutdown" mode at suspend time, and return to normal
572 * mode upon resume. This reduces power consumption by disabling
573 * the A/D converter.
574 */
575
576 static bool
577 sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
578 {
579 struct sdtemp_softc *sc = device_private(dev);
580 int error;
581 uint16_t config;
582
583 iic_acquire_bus(sc->sc_tag, 0);
584 error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
585 if (error == 0) {
586 config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
587 error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
588 }
589 iic_release_bus(sc->sc_tag, 0);
590 return (error == 0);
591 }
592
593 static bool
594 sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
595 {
596 struct sdtemp_softc *sc = device_private(dev);
597 int error;
598 uint16_t config;
599
600 iic_acquire_bus(sc->sc_tag, 0);
601 error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
602 if (error == 0) {
603 config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
604 error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
605 }
606 iic_release_bus(sc->sc_tag, 0);
607 return (error == 0);
608 }
609
610 /* Device dependent config functions */
611
612 static void
613 sdtemp_config_mcp(struct sdtemp_softc *sc)
614 {
615 int rv;
616 uint8_t resolreg;
617
618 /* Note that MCP9805 has no resolution register */
619 switch (sc->sc_devid_masked) {
620 case MCP_9804_DEVICE_ID:
621 case MCP_98242_DEVICE_ID:
622 case MCP_98243_DEVICE_ID:
623 resolreg = SDTEMP_REG_MCP_RESOLUTION_9804;
624 break;
625 case MCP_98244_DEVICE_ID:
626 resolreg = SDTEMP_REG_MCP_RESOLUTION_98244;
627 break;
628 default:
629 aprint_error("%s: %s: unknown device ID (%04hx)\n",
630 device_xname(sc->sc_dev), __func__, sc->sc_devid_masked);
631 return;
632 }
633
634 /*
635 * Set resolution to the max.
636 *
637 * Even if it fails, the resolution will be the default. It's not a
638 * fatal error.
639 */
640 rv = sdtemp_write_16(sc, resolreg, SDTEMP_CAP_RESOLUTION_MAX);
641 if (rv == 0)
642 sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
643 else
644 aprint_error("%s: error %d writing resolution register\n",
645 device_xname(sc->sc_dev), rv);
646 }
647
648 static void
649 sdtemp_config_idt(struct sdtemp_softc *sc)
650 {
651 int rv;
652
653 /*
654 * Set resolution to the max.
655 *
656 * Even if it fails, the resolution will be the default. It's not a
657 * fatal error.
658 */
659 rv = sdtemp_write_16(sc, SDTEMP_REG_IDT_RESOLUTION,
660 __SHIFTIN(SDTEMP_CAP_RESOLUTION_MAX, SDTEMP_CAP_RESOLUTION));
661 if (rv == 0)
662 sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
663 else
664 aprint_error("%s: error %d writing resolution register\n",
665 device_xname(sc->sc_dev), rv);
666 }
667
668 MODULE(MODULE_CLASS_DRIVER, sdtemp, "i2cexec,sysmon_envsys");
669
670 #ifdef _MODULE
671 #include "ioconf.c"
672 #endif
673
674 static int
675 sdtemp_modcmd(modcmd_t cmd, void *opaque)
676 {
677 int error = 0;
678
679 switch (cmd) {
680 case MODULE_CMD_INIT:
681 #ifdef _MODULE
682 error = config_init_component(cfdriver_ioconf_sdtemp,
683 cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
684 #endif
685 return error;
686 case MODULE_CMD_FINI:
687 #ifdef _MODULE
688 error = config_fini_component(cfdriver_ioconf_sdtemp,
689 cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
690 #endif
691 return error;
692 default:
693 return ENOTTY;
694 }
695 }
696