1*1e8046caSmsaitoh /* $NetBSD: sdtemp.c,v 1.41 2021/12/01 21:33:19 msaitoh Exp $ */
2b3c79debSpgoyette
3b3c79debSpgoyette /*
4b3c79debSpgoyette * Copyright (c) 2009 The NetBSD Foundation, Inc.
5b3c79debSpgoyette * All rights reserved.
6b3c79debSpgoyette *
7b3c79debSpgoyette * This code is derived from software contributed to The NetBSD Foundation
8b3c79debSpgoyette * by Paul Goyette.
9b3c79debSpgoyette *
10b3c79debSpgoyette * Redistribution and use in source and binary forms, with or without
11b3c79debSpgoyette * modification, are permitted provided that the following conditions
12b3c79debSpgoyette * are met:
13b3c79debSpgoyette * 1. Redistributions of source code must retain the above copyright
14b3c79debSpgoyette * notice, this list of conditions and the following disclaimer.
15b3c79debSpgoyette * 2. Redistributions in binary form must reproduce the above copyright
16b3c79debSpgoyette * notice, this list of conditions and the following disclaimer in the
17b3c79debSpgoyette * documentation and/or other materials provided with the distribution.
18b3c79debSpgoyette *
19b3c79debSpgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b3c79debSpgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b3c79debSpgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b3c79debSpgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b3c79debSpgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b3c79debSpgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b3c79debSpgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b3c79debSpgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b3c79debSpgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b3c79debSpgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b3c79debSpgoyette * POSSIBILITY OF SUCH DAMAGE.
30b3c79debSpgoyette */
31b3c79debSpgoyette
32b3c79debSpgoyette #include <sys/cdefs.h>
33*1e8046caSmsaitoh __KERNEL_RCSID(0, "$NetBSD: sdtemp.c,v 1.41 2021/12/01 21:33:19 msaitoh Exp $");
34b3c79debSpgoyette
35b3c79debSpgoyette #include <sys/param.h>
36b3c79debSpgoyette #include <sys/systm.h>
37b3c79debSpgoyette #include <sys/kmem.h>
38b3c79debSpgoyette #include <sys/device.h>
39b3c79debSpgoyette #include <sys/kernel.h>
40b3c79debSpgoyette #include <sys/endian.h>
414fc96f83Sjmcneill #include <sys/module.h>
42b3c79debSpgoyette
43b3c79debSpgoyette #include <dev/sysmon/sysmonvar.h>
44b3c79debSpgoyette
45b3c79debSpgoyette #include <dev/i2c/i2cvar.h>
46b3c79debSpgoyette #include <dev/i2c/sdtemp_reg.h>
47b3c79debSpgoyette
48b3c79debSpgoyette struct sdtemp_softc {
49b3c79debSpgoyette device_t sc_dev;
50b3c79debSpgoyette i2c_tag_t sc_tag;
51b3c79debSpgoyette int sc_address;
52b3c79debSpgoyette
53b3c79debSpgoyette struct sysmon_envsys *sc_sme;
54b3c79debSpgoyette envsys_data_t *sc_sensor;
550321f1cdSpgoyette sysmon_envsys_lim_t sc_deflims;
560321f1cdSpgoyette uint32_t sc_defprops;
57b3c79debSpgoyette int sc_resolution;
58eaa54fc0Smsaitoh uint16_t sc_mfgid;
59eaa54fc0Smsaitoh uint16_t sc_devid;
60eaa54fc0Smsaitoh uint16_t sc_devid_masked;
61b3c79debSpgoyette uint16_t sc_capability;
62b3c79debSpgoyette };
63b3c79debSpgoyette
64b3c79debSpgoyette static int sdtemp_match(device_t, cfdata_t, void *);
65b3c79debSpgoyette static void sdtemp_attach(device_t, device_t, void *);
664fc96f83Sjmcneill static int sdtemp_detach(device_t, int);
67b3c79debSpgoyette
68b3c79debSpgoyette CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
694fc96f83Sjmcneill sdtemp_match, sdtemp_attach, sdtemp_detach, NULL);
70b3c79debSpgoyette
71b3c79debSpgoyette static void sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
72c1af0765Spgoyette static void sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
73f7613febSpgoyette sysmon_envsys_lim_t *, uint32_t *);
74c1af0765Spgoyette static void sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
75f7613febSpgoyette sysmon_envsys_lim_t *, uint32_t *);
76b3c79debSpgoyette #ifdef NOT_YET
77b3c79debSpgoyette static int sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
78b3c79debSpgoyette static int sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
79b3c79debSpgoyette #endif /* NOT YET */
80b3c79debSpgoyette static int sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
81b3c79debSpgoyette static int sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
82b3c79debSpgoyette static uint32_t sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
83c1b390d4Sdyoung static bool sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
84c1b390d4Sdyoung static bool sdtemp_pmf_resume(device_t, const pmf_qual_t *);
85eaa54fc0Smsaitoh /* Device dependent config functions */
86eaa54fc0Smsaitoh static void sdtemp_config_mcp(struct sdtemp_softc *);
87eaa54fc0Smsaitoh static void sdtemp_config_idt(struct sdtemp_softc *);
88b3c79debSpgoyette
89b3c79debSpgoyette struct sdtemp_dev_entry {
90b3c79debSpgoyette const uint16_t sdtemp_mfg_id;
91bdf3e0ffSpgoyette const uint16_t sdtemp_devrev;
92bdf3e0ffSpgoyette const uint16_t sdtemp_mask;
93eaa54fc0Smsaitoh void (*sdtemp_config)(struct sdtemp_softc *);
94b3c79debSpgoyette const char *sdtemp_desc;
95b3c79debSpgoyette };
96b3c79debSpgoyette
975fb0a3a4Spgoyette /* Convert sysmon_envsys uKelvin value to simple degC */
985fb0a3a4Spgoyette
995fb0a3a4Spgoyette #define __UK2C(uk) (((uk) - 273150000) / 1000000)
100b3c79debSpgoyette
101eaa54fc0Smsaitoh /* List of devices known to conform to JEDEC JC42.4 */
102eaa54fc0Smsaitoh
103eaa54fc0Smsaitoh #define CMCP sdtemp_config_mcp
104eaa54fc0Smsaitoh #define CIDT sdtemp_config_idt
105eaa54fc0Smsaitoh
106b3c79debSpgoyette static const struct sdtemp_dev_entry
107b3c79debSpgoyette sdtemp_dev_table[] = {
108eaa54fc0Smsaitoh { AT_MANUFACTURER_ID, AT_30TS00_DEVICE_ID, AT_30TS00_MASK, NULL,
109eaa54fc0Smsaitoh "Atmel AT30TS00" },
110eaa54fc0Smsaitoh { AT2_MANUFACTURER_ID, AT2_30TSE004_DEVICE_ID, AT2_30TSE004_MASK, NULL,
111eaa54fc0Smsaitoh "Atmel AT30TSE004" },
112eaa54fc0Smsaitoh { GT_MANUFACTURER_ID, GT_30TS00_DEVICE_ID, GT_30TS00_MASK, NULL,
113eaa54fc0Smsaitoh "Giantec GT30TS00" },
114eaa54fc0Smsaitoh { GT2_MANUFACTURER_ID, GT2_34TS02_DEVICE_ID, GT2_34TS02_MASK, NULL,
115eaa54fc0Smsaitoh "Giantec GT34TS02" },
116eaa54fc0Smsaitoh { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID, MAX_6604_MASK, NULL,
117e2679ccbSpgoyette "Maxim MAX6604" },
1189e24688bSmsaitoh { MAXIM_MANUFACTURER_ID, MAX_6604_2_DEVICE_ID, MAX_6604_MASK, NULL,
1199e24688bSmsaitoh "Maxim MAX6604" },
120eaa54fc0Smsaitoh { MCP_MANUFACTURER_ID, MCP_9804_DEVICE_ID, MCP_9804_MASK, CMCP,
121eaa54fc0Smsaitoh "Microchip Tech MCP9804" },
122eaa54fc0Smsaitoh { MCP_MANUFACTURER_ID, MCP_9805_DEVICE_ID, MCP_9805_MASK, NULL,
123bdf3e0ffSpgoyette "Microchip Tech MCP9805/MCP9843" },
124eaa54fc0Smsaitoh { MCP_MANUFACTURER_ID, MCP_98242_DEVICE_ID, MCP_98242_MASK, CMCP,
125b3c79debSpgoyette "Microchip Tech MCP98242" },
126eaa54fc0Smsaitoh { MCP_MANUFACTURER_ID, MCP_98243_DEVICE_ID, MCP_98243_MASK, CMCP,
127eaa54fc0Smsaitoh "Microchip Tech MCP98243" },
128eaa54fc0Smsaitoh { MCP_MANUFACTURER_ID, MCP_98244_DEVICE_ID, MCP_98244_MASK, CMCP,
129eaa54fc0Smsaitoh "Microchip Tech MCP98244" },
1309e24688bSmsaitoh { MCP2_MANUFACTURER_ID, MCP2_EMC1501_DEVICE_ID, MCP2_EMC1501_MASK, NULL,
1319e24688bSmsaitoh "Microchip Tech EMC1501" },
132eaa54fc0Smsaitoh { ADT_MANUFACTURER_ID, ADT_7408_DEVICE_ID, ADT_7408_MASK, NULL,
133b3c79debSpgoyette "Analog Devices ADT7408" },
134eaa54fc0Smsaitoh { NXP_MANUFACTURER_ID, NXP_SE98_DEVICE_ID, NXP_SE98_MASK, NULL,
135bdf3e0ffSpgoyette "NXP Semiconductors SE97B/SE98" },
136eaa54fc0Smsaitoh { NXP_MANUFACTURER_ID, NXP_SE97_DEVICE_ID, NXP_SE97_MASK, NULL,
137bdf3e0ffSpgoyette "NXP Semiconductors SE97" },
138eaa54fc0Smsaitoh { STTS_MANUFACTURER_ID, STTS_424E_DEVICE_ID, STTS_424E_MASK, NULL,
139bdf3e0ffSpgoyette "STmicroelectronics STTS424E" },
140eaa54fc0Smsaitoh { STTS_MANUFACTURER_ID, STTS_424_DEVICE_ID, STTS_424_MASK, NULL,
141bdf3e0ffSpgoyette "STmicroelectronics STTS424" },
142eaa54fc0Smsaitoh { STTS_MANUFACTURER_ID, STTS_2002_DEVICE_ID, STTS_2002_MASK, NULL,
1438aa54a5dSmlelstv "STmicroelectronics STTS2002" },
144eaa54fc0Smsaitoh { STTS_MANUFACTURER_ID, STTS_2004_DEVICE_ID, STTS_2004_MASK, NULL,
145f6eb233cSmsaitoh "STmicroelectronics STTS2004" },
146eaa54fc0Smsaitoh { STTS_MANUFACTURER_ID, STTS_3000_DEVICE_ID, STTS_3000_MASK, NULL,
1478aa54a5dSmlelstv "STmicroelectronics STTS3000" },
148eaa54fc0Smsaitoh { CAT_MANUFACTURER_ID, CAT_34TS02_DEVICE_ID, CAT_34TS02_MASK, NULL,
149b3c79debSpgoyette "Catalyst CAT34TS02/CAT6095" },
150eaa54fc0Smsaitoh { CAT_MANUFACTURER_ID, CAT_34TS02C_DEVICE_ID, CAT_34TS02C_MASK, NULL,
151b469f187Smsaitoh "Catalyst CAT34TS02C" },
152eaa54fc0Smsaitoh { CAT_MANUFACTURER_ID, CAT_34TS04_DEVICE_ID, CAT_34TS04_MASK, NULL,
153eaa54fc0Smsaitoh "Catalyst CAT34TS04" },
154eaa54fc0Smsaitoh { IDT_MANUFACTURER_ID, IDT_TSE2004GB2_DEVICE_ID,IDT_TSE2004GB2_MASK, NULL,
155eaa54fc0Smsaitoh "Integrated Device Technology TSE2004GB2" },
156eaa54fc0Smsaitoh { IDT_MANUFACTURER_ID, IDT_TS3000B3_DEVICE_ID, IDT_TS3000B3_MASK, CIDT,
1578ce82797Spgoyette "Integrated Device Technology TS3000B3/TSE2002B3" },
158eaa54fc0Smsaitoh { IDT_MANUFACTURER_ID, IDT_TS3000GB0_DEVICE_ID, IDT_TS3000GB0_MASK, CIDT,
159eaa54fc0Smsaitoh "Integrated Device Technology TS3000GB0" },
160eaa54fc0Smsaitoh { IDT_MANUFACTURER_ID, IDT_TS3000GB2_DEVICE_ID, IDT_TS3000GB2_MASK, CIDT,
161eaa54fc0Smsaitoh "Integrated Device Technology TS3000GB2" },
162eaa54fc0Smsaitoh { IDT_MANUFACTURER_ID, IDT_TS3001GB2_DEVICE_ID, IDT_TS3001GB2_MASK, CIDT,
163eaa54fc0Smsaitoh "Integrated Device Technology TS3001GB2" },
1648cd40e14Smsaitoh /*
1658cd40e14Smsaitoh * Don't change the location of the following two entries. Device specific
1668cd40e14Smsaitoh * entry must be located at above.
1678cd40e14Smsaitoh */
1688cd40e14Smsaitoh { 0, TSE2004AV_ID, TSE2004AV_MASK, NULL,
1698cd40e14Smsaitoh "TSE2004av compliant device (generic driver)" },
170eaa54fc0Smsaitoh { 0, 0, 0, NULL, "Unknown" }
171eaa54fc0Smsaitoh };
172eaa54fc0Smsaitoh
173eaa54fc0Smsaitoh #undef CMCP
174eaa54fc0Smsaitoh #undef CIDT
175eaa54fc0Smsaitoh
176eaa54fc0Smsaitoh static const char *temp_resl[] = {
177eaa54fc0Smsaitoh "0.5C",
178eaa54fc0Smsaitoh "0.25C",
179eaa54fc0Smsaitoh "0.125C",
180eaa54fc0Smsaitoh "0.0625C"
181b3c79debSpgoyette };
182b3c79debSpgoyette
183b3c79debSpgoyette static int
sdtemp_lookup(uint16_t mfg,uint16_t devrev)184bdf3e0ffSpgoyette sdtemp_lookup(uint16_t mfg, uint16_t devrev)
185b3c79debSpgoyette {
186b3c79debSpgoyette int i;
187b3c79debSpgoyette
188bdf3e0ffSpgoyette for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++) {
189bdf3e0ffSpgoyette if (mfg != sdtemp_dev_table[i].sdtemp_mfg_id)
190bdf3e0ffSpgoyette continue;
191bdf3e0ffSpgoyette if ((devrev & sdtemp_dev_table[i].sdtemp_mask) ==
192bdf3e0ffSpgoyette sdtemp_dev_table[i].sdtemp_devrev)
193b3c79debSpgoyette break;
194bdf3e0ffSpgoyette }
1958cd40e14Smsaitoh /* Check TSE2004av */
1968cd40e14Smsaitoh if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0)
1978cd40e14Smsaitoh && (SDTEMP_IS_TSE2004AV(devrev) == 0))
1988cd40e14Smsaitoh i++; /* Unknown */
199b3c79debSpgoyette
200b3c79debSpgoyette return i;
201b3c79debSpgoyette }
202b3c79debSpgoyette
203b3c79debSpgoyette static int
sdtemp_match(device_t parent,cfdata_t cf,void * aux)204b3c79debSpgoyette sdtemp_match(device_t parent, cfdata_t cf, void *aux)
205b3c79debSpgoyette {
206b3c79debSpgoyette struct i2c_attach_args *ia = aux;
2078cd40e14Smsaitoh uint16_t mfgid, devid, cap;
208b3c79debSpgoyette struct sdtemp_softc sc;
209b3c79debSpgoyette int i, error;
210b3c79debSpgoyette
211b3c79debSpgoyette sc.sc_tag = ia->ia_tag;
212b3c79debSpgoyette sc.sc_address = ia->ia_addr;
213b3c79debSpgoyette
214b3c79debSpgoyette if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
215b3c79debSpgoyette return 0;
216b3c79debSpgoyette
217d996324cSmsaitoh /*
218d996324cSmsaitoh * Verify that we can read the manufacturer ID, Device ID and the
219d996324cSmsaitoh * capability
220d996324cSmsaitoh */
22102c4325eSmsaitoh error = iic_acquire_bus(sc.sc_tag, 0);
22202c4325eSmsaitoh if (error)
22302c4325eSmsaitoh return 0;
224b3c79debSpgoyette error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID, &mfgid) |
2258cd40e14Smsaitoh sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid) |
2268cd40e14Smsaitoh sdtemp_read_16(&sc, SDTEMP_REG_CAPABILITY, &cap);
227b3c79debSpgoyette iic_release_bus(sc.sc_tag, 0);
228b3c79debSpgoyette
229b3c79debSpgoyette if (error)
230b3c79debSpgoyette return 0;
231b3c79debSpgoyette
232bdf3e0ffSpgoyette i = sdtemp_lookup(mfgid, devid);
2338cd40e14Smsaitoh if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0) &&
2348cd40e14Smsaitoh (sdtemp_dev_table[i].sdtemp_devrev == 0)) {
2351fdf1827Smsaitoh aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
2361fdf1827Smsaitoh "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
237b3c79debSpgoyette devid & 0xff, sc.sc_address);
238b3c79debSpgoyette return 0;
239b3c79debSpgoyette }
240b3c79debSpgoyette
2418cd40e14Smsaitoh /*
242d996324cSmsaitoh * Check by SDTEMP_IS_TSE2004AV() might not be enough, so check the
243d996324cSmsaitoh * alarm capability, too.
2448cd40e14Smsaitoh */
2458cd40e14Smsaitoh if ((cap & SDTEMP_CAP_HAS_ALARM) == 0)
2468cd40e14Smsaitoh return 0;
2478cd40e14Smsaitoh
248aa41e992Sthorpej return I2C_MATCH_ADDRESS_AND_PROBE;
249b3c79debSpgoyette }
250b3c79debSpgoyette
251b3c79debSpgoyette static void
sdtemp_attach(device_t parent,device_t self,void * aux)252b3c79debSpgoyette sdtemp_attach(device_t parent, device_t self, void *aux)
253b3c79debSpgoyette {
254b3c79debSpgoyette struct sdtemp_softc *sc = device_private(self);
255b3c79debSpgoyette struct i2c_attach_args *ia = aux;
256b3c79debSpgoyette uint16_t mfgid, devid;
257b3c79debSpgoyette int i, error;
258b3c79debSpgoyette
259b3c79debSpgoyette sc->sc_tag = ia->ia_tag;
260b3c79debSpgoyette sc->sc_address = ia->ia_addr;
261b3c79debSpgoyette sc->sc_dev = self;
262b3c79debSpgoyette
26302c4325eSmsaitoh error = iic_acquire_bus(sc->sc_tag, 0);
26402c4325eSmsaitoh if (error)
26502c4325eSmsaitoh return;
26602c4325eSmsaitoh
267b3c79debSpgoyette if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID, &mfgid)) != 0 ||
268b3c79debSpgoyette (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
2693441b533Spgoyette iic_release_bus(sc->sc_tag, 0);
270b3c79debSpgoyette aprint_error(": attach error %d\n", error);
271b3c79debSpgoyette return;
272b3c79debSpgoyette }
273eaa54fc0Smsaitoh sc->sc_mfgid = mfgid;
274eaa54fc0Smsaitoh sc->sc_devid = devid;
275bdf3e0ffSpgoyette i = sdtemp_lookup(mfgid, devid);
276eaa54fc0Smsaitoh sc->sc_devid_masked = devid & sdtemp_dev_table[i].sdtemp_mask;
277b3c79debSpgoyette
278b3c79debSpgoyette aprint_naive(": Temp Sensor\n");
279b3c79debSpgoyette aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
280b3c79debSpgoyette
2818cd40e14Smsaitoh if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
2828cd40e14Smsaitoh if (SDTEMP_IS_TSE2004AV(devid))
2838cd40e14Smsaitoh aprint_normal_dev(self, "TSE2004av compliant. "
2848cd40e14Smsaitoh "Manufacturer ID 0x%04hx, Device revision 0x%02x\n",
2858cd40e14Smsaitoh mfgid, devid & TSE2004AV_REV);
2868cd40e14Smsaitoh else {
2878cd40e14Smsaitoh aprint_error_dev(self,
2881fdf1827Smsaitoh "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
2891fdf1827Smsaitoh mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
2908cd40e14Smsaitoh iic_release_bus(sc->sc_tag, 0);
2918cd40e14Smsaitoh aprint_error_dev(self, "It should no happen. "
2928cd40e14Smsaitoh "Why attach() found me?\n");
2938cd40e14Smsaitoh return;
2948cd40e14Smsaitoh }
2958cd40e14Smsaitoh }
296b3c79debSpgoyette
297eaa54fc0Smsaitoh error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
298eaa54fc0Smsaitoh aprint_debug_dev(self, "capability reg = %04x\n", sc->sc_capability);
299eaa54fc0Smsaitoh sc->sc_resolution
300eaa54fc0Smsaitoh = __SHIFTOUT(sc->sc_capability, SDTEMP_CAP_RESOLUTION);
301eaa54fc0Smsaitoh /*
302eaa54fc0Smsaitoh * Call device dependent function here. Currently, it's used for
303eaa54fc0Smsaitoh * the resolution.
304eaa54fc0Smsaitoh *
305eaa54fc0Smsaitoh * IDT's devices and some Microchip's devices have the resolution
306eaa54fc0Smsaitoh * register in the vendor specific registers area. The devices'
307eaa54fc0Smsaitoh * resolution bits in the capability register are not the maximum
308d2a72d85Skhorben * resolution but the current value of the setting.
309eaa54fc0Smsaitoh */
310eaa54fc0Smsaitoh if (sdtemp_dev_table[i].sdtemp_config != NULL)
311eaa54fc0Smsaitoh sdtemp_dev_table[i].sdtemp_config(sc);
312eaa54fc0Smsaitoh
313eaa54fc0Smsaitoh aprint_normal_dev(self, "%s accuracy",
314eaa54fc0Smsaitoh (sc->sc_capability & SDTEMP_CAP_ACCURACY_1C) ? "high" : "default");
315eaa54fc0Smsaitoh if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) != 0)
316eaa54fc0Smsaitoh aprint_normal(", wider range");
317eaa54fc0Smsaitoh aprint_normal(", %s resolution", temp_resl[sc->sc_resolution]);
318eaa54fc0Smsaitoh if ((sc->sc_capability & SDTEMP_CAP_VHV) != 0)
319eaa54fc0Smsaitoh aprint_debug(", high voltage standoff");
320eaa54fc0Smsaitoh aprint_debug(", %s timeout",
321eaa54fc0Smsaitoh (sc->sc_capability & SDTEMP_CAP_TMOUT) ? "25-35ms" : "10-60ms");
322eaa54fc0Smsaitoh if ((sc->sc_capability & SDTEMP_CAP_EVSD) != 0)
323eaa54fc0Smsaitoh aprint_normal(", event with shutdown");
324eaa54fc0Smsaitoh aprint_normal("\n");
325b3c79debSpgoyette /*
326b3c79debSpgoyette * Alarm capability is required; if not present, this is likely
327b3c79debSpgoyette * not a real sdtemp device.
328b3c79debSpgoyette */
329b3c79debSpgoyette if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
330b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
331b3c79debSpgoyette aprint_error_dev(self,
332b3c79debSpgoyette "required alarm capability not present!\n");
333b3c79debSpgoyette return;
334b3c79debSpgoyette }
335b3c79debSpgoyette /* Set the configuration to defaults. */
336b3c79debSpgoyette error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
337b3c79debSpgoyette if (error != 0) {
338b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
339b3c79debSpgoyette aprint_error_dev(self, "error %d writing config register\n",
340b3c79debSpgoyette error);
341b3c79debSpgoyette return;
342b3c79debSpgoyette }
343b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
344b3c79debSpgoyette
345b3c79debSpgoyette /* Hook us into the sysmon_envsys subsystem */
346b3c79debSpgoyette sc->sc_sme = sysmon_envsys_create();
3475fb0a3a4Spgoyette sc->sc_sme->sme_name = device_xname(self);
3485fb0a3a4Spgoyette sc->sc_sme->sme_cookie = sc;
3495fb0a3a4Spgoyette sc->sc_sme->sme_refresh = sdtemp_refresh;
3505fb0a3a4Spgoyette sc->sc_sme->sme_get_limits = sdtemp_get_limits;
3515fb0a3a4Spgoyette sc->sc_sme->sme_set_limits = sdtemp_set_limits;
3525fb0a3a4Spgoyette
35302991323Schs sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_SLEEP);
354b3c79debSpgoyette
355b3c79debSpgoyette /* Initialize sensor data. */
356b3c79debSpgoyette sc->sc_sensor->units = ENVSYS_STEMP;
357b3c79debSpgoyette sc->sc_sensor->state = ENVSYS_SINVALID;
358b3c79debSpgoyette sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
359b3c79debSpgoyette (void)strlcpy(sc->sc_sensor->desc, device_xname(self),
360b3c79debSpgoyette sizeof(sc->sc_sensor->desc));
3619387374cSsoren snprintf(sc->sc_sensor->desc, sizeof(sc->sc_sensor->desc),
3629387374cSsoren "DIMM %d temperature", sc->sc_address - SDTEMP_ADDR);
363b3c79debSpgoyette
364b3c79debSpgoyette /* Now attach the sensor */
365b3c79debSpgoyette if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
366b3c79debSpgoyette aprint_error_dev(self, "unable to attach sensor\n");
367b3c79debSpgoyette goto bad;
368b3c79debSpgoyette }
369b3c79debSpgoyette
370b3c79debSpgoyette /* Register the device */
371b3c79debSpgoyette error = sysmon_envsys_register(sc->sc_sme);
372b3c79debSpgoyette if (error) {
373b3c79debSpgoyette aprint_error_dev(self, "error %d registering with sysmon\n",
374b3c79debSpgoyette error);
375b3c79debSpgoyette goto bad;
376b3c79debSpgoyette }
377b3c79debSpgoyette
378b3c79debSpgoyette if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
379b3c79debSpgoyette aprint_error_dev(self, "couldn't establish power handler\n");
380b3c79debSpgoyette
381b3c79debSpgoyette /* Retrieve and display hardware monitor limits */
3825cafa818Snjoly sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &sc->sc_deflims,
3835cafa818Snjoly &sc->sc_defprops);
384eda587d6Spgoyette aprint_normal_dev(self, "Hardware limits: ");
3855fb0a3a4Spgoyette i = 0;
3865cafa818Snjoly if (sc->sc_defprops & PROP_WARNMIN) {
387eda587d6Spgoyette aprint_normal("low %dC",
3885cafa818Snjoly __UK2C(sc->sc_deflims.sel_warnmin));
389b3c79debSpgoyette i++;
390b3c79debSpgoyette }
3915cafa818Snjoly if (sc->sc_defprops & PROP_WARNMAX) {
392eda587d6Spgoyette aprint_normal("%shigh %dC ", (i)?", ":"",
3935cafa818Snjoly __UK2C(sc->sc_deflims.sel_warnmax));
394b3c79debSpgoyette i++;
395b3c79debSpgoyette }
3965cafa818Snjoly if (sc->sc_defprops & PROP_CRITMAX) {
397eda587d6Spgoyette aprint_normal("%scritical %dC ", (i)?", ":"",
3985cafa818Snjoly __UK2C(sc->sc_deflims.sel_critmax));
399b3c79debSpgoyette i++;
400b3c79debSpgoyette }
401eda587d6Spgoyette aprint_normal("%s\n", (i)?"":"none set");
402b3c79debSpgoyette
403b3c79debSpgoyette return;
404b3c79debSpgoyette
405b3c79debSpgoyette bad:
406b3c79debSpgoyette kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
407b3c79debSpgoyette sysmon_envsys_destroy(sc->sc_sme);
40865c5d463Smlelstv sc->sc_sme = NULL;
409b3c79debSpgoyette }
410b3c79debSpgoyette
4114fc96f83Sjmcneill static int
sdtemp_detach(device_t self,int flags)4124fc96f83Sjmcneill sdtemp_detach(device_t self, int flags)
4134fc96f83Sjmcneill {
4144fc96f83Sjmcneill struct sdtemp_softc *sc = device_private(self);
4154fc96f83Sjmcneill
4164fc96f83Sjmcneill pmf_device_deregister(self);
4174fc96f83Sjmcneill
4184fc96f83Sjmcneill if (sc->sc_sme)
4194fc96f83Sjmcneill sysmon_envsys_unregister(sc->sc_sme);
4204fc96f83Sjmcneill if (sc->sc_sensor)
4214fc96f83Sjmcneill kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
4224fc96f83Sjmcneill
4234fc96f83Sjmcneill return 0;
4244fc96f83Sjmcneill }
4254fc96f83Sjmcneill
4265fb0a3a4Spgoyette /* Retrieve current limits from device, and encode in uKelvins */
427b3c79debSpgoyette static void
sdtemp_get_limits(struct sysmon_envsys * sme,envsys_data_t * edata,sysmon_envsys_lim_t * limits,uint32_t * props)428c1af0765Spgoyette sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
429f7613febSpgoyette sysmon_envsys_lim_t *limits, uint32_t *props)
430b3c79debSpgoyette {
4315fb0a3a4Spgoyette struct sdtemp_softc *sc = sme->sme_cookie;
4325fb0a3a4Spgoyette uint16_t lim;
433b3c79debSpgoyette
434f7613febSpgoyette *props = 0;
43502c4325eSmsaitoh if (iic_acquire_bus(sc->sc_tag, 0) != 0)
43602c4325eSmsaitoh return;
43702c4325eSmsaitoh
4385fb0a3a4Spgoyette if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
4395fb0a3a4Spgoyette limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
440f7613febSpgoyette *props |= PROP_WARNMIN;
4415fb0a3a4Spgoyette }
4425fb0a3a4Spgoyette if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
4435fb0a3a4Spgoyette limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
444f7613febSpgoyette *props |= PROP_WARNMAX;
4455fb0a3a4Spgoyette }
4465fb0a3a4Spgoyette if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
4475fb0a3a4Spgoyette limits->sel_critmax = sdtemp_decode_temp(sc, lim);
448f7613febSpgoyette *props |= PROP_CRITMAX;
4495fb0a3a4Spgoyette }
4505fb0a3a4Spgoyette iic_release_bus(sc->sc_tag, 0);
451f7613febSpgoyette if (*props != 0)
452f7613febSpgoyette *props |= PROP_DRIVER_LIMITS;
453b3c79debSpgoyette }
454b3c79debSpgoyette
4555fb0a3a4Spgoyette /* Send current limit values to the device */
4565fb0a3a4Spgoyette static void
sdtemp_set_limits(struct sysmon_envsys * sme,envsys_data_t * edata,sysmon_envsys_lim_t * limits,uint32_t * props)457c1af0765Spgoyette sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
458f7613febSpgoyette sysmon_envsys_lim_t *limits, uint32_t *props)
4595fb0a3a4Spgoyette {
4605fb0a3a4Spgoyette uint16_t val;
4615fb0a3a4Spgoyette struct sdtemp_softc *sc = sme->sme_cookie;
4625fb0a3a4Spgoyette
4630321f1cdSpgoyette if (limits == NULL) {
4640321f1cdSpgoyette limits = &sc->sc_deflims;
4650321f1cdSpgoyette props = &sc->sc_defprops;
4660321f1cdSpgoyette }
46702c4325eSmsaitoh if (iic_acquire_bus(sc->sc_tag, 0) != 0)
46802c4325eSmsaitoh return;
46902c4325eSmsaitoh
470f7613febSpgoyette if (*props & PROP_WARNMIN) {
4715fb0a3a4Spgoyette val = __UK2C(limits->sel_warnmin);
4725fb0a3a4Spgoyette (void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
4735fb0a3a4Spgoyette (val << 4) & SDTEMP_TEMP_MASK);
4745fb0a3a4Spgoyette }
475f7613febSpgoyette if (*props & PROP_WARNMAX) {
4765fb0a3a4Spgoyette val = __UK2C(limits->sel_warnmax);
4775fb0a3a4Spgoyette (void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
4785fb0a3a4Spgoyette (val << 4) & SDTEMP_TEMP_MASK);
4795fb0a3a4Spgoyette }
480f7613febSpgoyette if (*props & PROP_CRITMAX) {
4815fb0a3a4Spgoyette val = __UK2C(limits->sel_critmax);
4825fb0a3a4Spgoyette (void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
4835fb0a3a4Spgoyette (val << 4) & SDTEMP_TEMP_MASK);
4845fb0a3a4Spgoyette }
485b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
486b3c79debSpgoyette
4875fb0a3a4Spgoyette /*
4885fb0a3a4Spgoyette * If at least one limit is set that we can handle, and no
4895fb0a3a4Spgoyette * limits are set that we cannot handle, tell sysmon that
4905fb0a3a4Spgoyette * the driver will take care of monitoring the limits!
4915fb0a3a4Spgoyette */
492f7613febSpgoyette if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
493f7613febSpgoyette *props &= ~PROP_DRIVER_LIMITS;
494f7613febSpgoyette else if (*props & PROP_LIMITS)
495f7613febSpgoyette *props |= PROP_DRIVER_LIMITS;
4965fb0a3a4Spgoyette else
497f7613febSpgoyette *props &= ~PROP_DRIVER_LIMITS;
498b3c79debSpgoyette }
499b3c79debSpgoyette
500b3c79debSpgoyette #ifdef NOT_YET /* All registers on these sensors are 16-bits */
501b3c79debSpgoyette
502b3c79debSpgoyette /* Read a 8-bit value from a register */
503b3c79debSpgoyette static int
sdtemp_read_8(struct sdtemp_softc * sc,uint8_t reg,uint8_t * valp)504b3c79debSpgoyette sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
505b3c79debSpgoyette {
506b3c79debSpgoyette int error;
507b3c79debSpgoyette
508b3c79debSpgoyette error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
509b3c79debSpgoyette sc->sc_address, ®, 1, valp, sizeof(*valp), 0);
510b3c79debSpgoyette
511b3c79debSpgoyette return error;
512b3c79debSpgoyette }
513b3c79debSpgoyette
514b3c79debSpgoyette static int
sdtemp_write_8(struct sdtemp_softc * sc,uint8_t reg,uint8_t val)515b3c79debSpgoyette sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
516b3c79debSpgoyette {
517b3c79debSpgoyette return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
518b3c79debSpgoyette sc->sc_address, ®, 1, &val, sizeof(val), 0);
519b3c79debSpgoyette }
520b3c79debSpgoyette #endif /* NOT_YET */
521b3c79debSpgoyette
522b3c79debSpgoyette /* Read a 16-bit value from a register */
523b3c79debSpgoyette static int
sdtemp_read_16(struct sdtemp_softc * sc,uint8_t reg,uint16_t * valp)524b3c79debSpgoyette sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
525b3c79debSpgoyette {
526b3c79debSpgoyette int error;
527b3c79debSpgoyette
528b3c79debSpgoyette error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
529b3c79debSpgoyette sc->sc_address, ®, 1, valp, sizeof(*valp), 0);
530b3c79debSpgoyette if (error)
531b3c79debSpgoyette return error;
532b3c79debSpgoyette
533b3c79debSpgoyette *valp = be16toh(*valp);
534b3c79debSpgoyette
535b3c79debSpgoyette return 0;
536b3c79debSpgoyette }
537b3c79debSpgoyette
538b3c79debSpgoyette static int
sdtemp_write_16(struct sdtemp_softc * sc,uint8_t reg,uint16_t val)539b3c79debSpgoyette sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
540b3c79debSpgoyette {
541b3c79debSpgoyette uint16_t temp;
542b3c79debSpgoyette
543b3c79debSpgoyette temp = htobe16(val);
544b3c79debSpgoyette return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
545b3c79debSpgoyette sc->sc_address, ®, 1, &temp, sizeof(temp), 0);
546b3c79debSpgoyette }
547b3c79debSpgoyette
548b3c79debSpgoyette static uint32_t
sdtemp_decode_temp(struct sdtemp_softc * sc,uint16_t temp)549b3c79debSpgoyette sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
550b3c79debSpgoyette {
551b3c79debSpgoyette uint32_t val;
552b3c79debSpgoyette int32_t stemp;
553b3c79debSpgoyette
554b3c79debSpgoyette /* Get only the temperature bits */
555b3c79debSpgoyette temp &= SDTEMP_TEMP_MASK;
556b3c79debSpgoyette
557b3c79debSpgoyette /* If necessary, extend the sign bit */
558b3c79debSpgoyette if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
559b3c79debSpgoyette (temp & SDTEMP_TEMP_NEGATIVE))
560b3c79debSpgoyette temp |= SDTEMP_TEMP_SIGN_EXT;
561b3c79debSpgoyette
562b3c79debSpgoyette /* Mask off only bits valid within current resolution */
563eaa54fc0Smsaitoh temp &= ~(0x7 >> sc->sc_resolution);
564b3c79debSpgoyette
565b3c79debSpgoyette /* Treat as signed and extend to 32-bits */
566b3c79debSpgoyette stemp = (int16_t)temp;
567b3c79debSpgoyette
568b3c79debSpgoyette /* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
569b3c79debSpgoyette val = (stemp * 62500) + 273150000;
570b3c79debSpgoyette
571b3c79debSpgoyette return val;
572b3c79debSpgoyette }
573b3c79debSpgoyette
574b3c79debSpgoyette static void
sdtemp_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)575b3c79debSpgoyette sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
576b3c79debSpgoyette {
577b3c79debSpgoyette struct sdtemp_softc *sc = sme->sme_cookie;
578b3c79debSpgoyette uint16_t val;
579b3c79debSpgoyette int error;
580b3c79debSpgoyette
58102c4325eSmsaitoh error = iic_acquire_bus(sc->sc_tag, 0);
58224cca438Smsaitoh if (error) {
58324cca438Smsaitoh edata->state = ENVSYS_SINVALID;
58402c4325eSmsaitoh return;
58524cca438Smsaitoh }
58602c4325eSmsaitoh
587b3c79debSpgoyette error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
588b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
589b3c79debSpgoyette
590b3c79debSpgoyette if (error) {
591b3c79debSpgoyette edata->state = ENVSYS_SINVALID;
592b3c79debSpgoyette return;
593b3c79debSpgoyette }
594b3c79debSpgoyette
595b3c79debSpgoyette edata->value_cur = sdtemp_decode_temp(sc, val);
596b3c79debSpgoyette
597b3c79debSpgoyette /* Now check for limits */
5985fb0a3a4Spgoyette if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
5995fb0a3a4Spgoyette edata->state = ENVSYS_SVALID;
600100b434aSpgoyette else if ((val & SDTEMP_ABOVE_CRIT) &&
601100b434aSpgoyette (edata->upropset & PROP_CRITMAX))
602b3c79debSpgoyette edata->state = ENVSYS_SCRITOVER;
603100b434aSpgoyette else if ((val & SDTEMP_ABOVE_UPPER) &&
604100b434aSpgoyette (edata->upropset & PROP_WARNMAX))
605b3c79debSpgoyette edata->state = ENVSYS_SWARNOVER;
606100b434aSpgoyette else if ((val & SDTEMP_BELOW_LOWER) &&
607100b434aSpgoyette (edata->upropset & PROP_WARNMIN))
608b3c79debSpgoyette edata->state = ENVSYS_SWARNUNDER;
609b3c79debSpgoyette else
610b3c79debSpgoyette edata->state = ENVSYS_SVALID;
611b3c79debSpgoyette }
612b3c79debSpgoyette
613b3c79debSpgoyette /*
614d996324cSmsaitoh * Power management functions
615b3c79debSpgoyette *
616b3c79debSpgoyette * We go into "shutdown" mode at suspend time, and return to normal
617b3c79debSpgoyette * mode upon resume. This reduces power consumption by disabling
618b3c79debSpgoyette * the A/D converter.
619b3c79debSpgoyette */
620b3c79debSpgoyette
621b3c79debSpgoyette static bool
sdtemp_pmf_suspend(device_t dev,const pmf_qual_t * qual)622c1b390d4Sdyoung sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
623b3c79debSpgoyette {
624b3c79debSpgoyette struct sdtemp_softc *sc = device_private(dev);
625b3c79debSpgoyette int error;
626b3c79debSpgoyette uint16_t config;
627b3c79debSpgoyette
62802c4325eSmsaitoh error = iic_acquire_bus(sc->sc_tag, 0);
62902c4325eSmsaitoh if (error != 0)
63002c4325eSmsaitoh return false;
63102c4325eSmsaitoh
632b3c79debSpgoyette error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
633b3c79debSpgoyette if (error == 0) {
634b3c79debSpgoyette config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
635b3c79debSpgoyette error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
636b3c79debSpgoyette }
637b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
638b3c79debSpgoyette return (error == 0);
639b3c79debSpgoyette }
640b3c79debSpgoyette
641b3c79debSpgoyette static bool
sdtemp_pmf_resume(device_t dev,const pmf_qual_t * qual)642c1b390d4Sdyoung sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
643b3c79debSpgoyette {
644b3c79debSpgoyette struct sdtemp_softc *sc = device_private(dev);
645b3c79debSpgoyette int error;
646b3c79debSpgoyette uint16_t config;
647b3c79debSpgoyette
64802c4325eSmsaitoh error = iic_acquire_bus(sc->sc_tag, 0);
64902c4325eSmsaitoh if (error != 0)
65002c4325eSmsaitoh return false;
65102c4325eSmsaitoh
652b3c79debSpgoyette error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
653b3c79debSpgoyette if (error == 0) {
654b3c79debSpgoyette config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
655b3c79debSpgoyette error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
656b3c79debSpgoyette }
657b3c79debSpgoyette iic_release_bus(sc->sc_tag, 0);
658b3c79debSpgoyette return (error == 0);
659b3c79debSpgoyette }
6604fc96f83Sjmcneill
661eaa54fc0Smsaitoh /* Device dependent config functions */
662eaa54fc0Smsaitoh
663eaa54fc0Smsaitoh static void
sdtemp_config_mcp(struct sdtemp_softc * sc)664eaa54fc0Smsaitoh sdtemp_config_mcp(struct sdtemp_softc *sc)
665eaa54fc0Smsaitoh {
666eaa54fc0Smsaitoh int rv;
667eaa54fc0Smsaitoh uint8_t resolreg;
668eaa54fc0Smsaitoh
669eaa54fc0Smsaitoh /* Note that MCP9805 has no resolution register */
670eaa54fc0Smsaitoh switch (sc->sc_devid_masked) {
671eaa54fc0Smsaitoh case MCP_9804_DEVICE_ID:
672eaa54fc0Smsaitoh case MCP_98242_DEVICE_ID:
673eaa54fc0Smsaitoh case MCP_98243_DEVICE_ID:
674eaa54fc0Smsaitoh resolreg = SDTEMP_REG_MCP_RESOLUTION_9804;
675eaa54fc0Smsaitoh break;
676eaa54fc0Smsaitoh case MCP_98244_DEVICE_ID:
677eaa54fc0Smsaitoh resolreg = SDTEMP_REG_MCP_RESOLUTION_98244;
678eaa54fc0Smsaitoh break;
679eaa54fc0Smsaitoh default:
680eaa54fc0Smsaitoh aprint_error("%s: %s: unknown device ID (%04hx)\n",
681eaa54fc0Smsaitoh device_xname(sc->sc_dev), __func__, sc->sc_devid_masked);
682eaa54fc0Smsaitoh return;
683eaa54fc0Smsaitoh }
684eaa54fc0Smsaitoh
685eaa54fc0Smsaitoh /*
686eaa54fc0Smsaitoh * Set resolution to the max.
687eaa54fc0Smsaitoh *
688eaa54fc0Smsaitoh * Even if it fails, the resolution will be the default. It's not a
689eaa54fc0Smsaitoh * fatal error.
690eaa54fc0Smsaitoh */
691eaa54fc0Smsaitoh rv = sdtemp_write_16(sc, resolreg, SDTEMP_CAP_RESOLUTION_MAX);
692eaa54fc0Smsaitoh if (rv == 0)
693eaa54fc0Smsaitoh sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
694eaa54fc0Smsaitoh else
695*1e8046caSmsaitoh aprint_debug_dev(sc->sc_dev,
696*1e8046caSmsaitoh "error %d writing resolution register\n", rv);
697eaa54fc0Smsaitoh }
698eaa54fc0Smsaitoh
699eaa54fc0Smsaitoh static void
sdtemp_config_idt(struct sdtemp_softc * sc)700eaa54fc0Smsaitoh sdtemp_config_idt(struct sdtemp_softc *sc)
701eaa54fc0Smsaitoh {
702eaa54fc0Smsaitoh int rv;
703eaa54fc0Smsaitoh
704eaa54fc0Smsaitoh /*
705eaa54fc0Smsaitoh * Set resolution to the max.
706eaa54fc0Smsaitoh *
707eaa54fc0Smsaitoh * Even if it fails, the resolution will be the default. It's not a
708eaa54fc0Smsaitoh * fatal error.
709eaa54fc0Smsaitoh */
710eaa54fc0Smsaitoh rv = sdtemp_write_16(sc, SDTEMP_REG_IDT_RESOLUTION,
711eaa54fc0Smsaitoh __SHIFTIN(SDTEMP_CAP_RESOLUTION_MAX, SDTEMP_CAP_RESOLUTION));
712eaa54fc0Smsaitoh if (rv == 0)
713eaa54fc0Smsaitoh sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
714eaa54fc0Smsaitoh else
715*1e8046caSmsaitoh aprint_debug_dev(sc->sc_dev,
716*1e8046caSmsaitoh "error %d writing resolution register\n", rv);
717eaa54fc0Smsaitoh }
7189e8ea18eSmsaitoh
7199e8ea18eSmsaitoh MODULE(MODULE_CLASS_DRIVER, sdtemp, "i2cexec,sysmon_envsys");
7209e8ea18eSmsaitoh
7219e8ea18eSmsaitoh #ifdef _MODULE
7229e8ea18eSmsaitoh #include "ioconf.c"
7239e8ea18eSmsaitoh #endif
7249e8ea18eSmsaitoh
7259e8ea18eSmsaitoh static int
sdtemp_modcmd(modcmd_t cmd,void * opaque)7269e8ea18eSmsaitoh sdtemp_modcmd(modcmd_t cmd, void *opaque)
7279e8ea18eSmsaitoh {
7289e8ea18eSmsaitoh int error = 0;
7299e8ea18eSmsaitoh
7309e8ea18eSmsaitoh switch (cmd) {
7319e8ea18eSmsaitoh case MODULE_CMD_INIT:
7329e8ea18eSmsaitoh #ifdef _MODULE
7339e8ea18eSmsaitoh error = config_init_component(cfdriver_ioconf_sdtemp,
7349e8ea18eSmsaitoh cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
7359e8ea18eSmsaitoh #endif
7369e8ea18eSmsaitoh return error;
7379e8ea18eSmsaitoh case MODULE_CMD_FINI:
7389e8ea18eSmsaitoh #ifdef _MODULE
7399e8ea18eSmsaitoh error = config_fini_component(cfdriver_ioconf_sdtemp,
7409e8ea18eSmsaitoh cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
7419e8ea18eSmsaitoh #endif
7429e8ea18eSmsaitoh return error;
7439e8ea18eSmsaitoh default:
7449e8ea18eSmsaitoh return ENOTTY;
7459e8ea18eSmsaitoh }
7469e8ea18eSmsaitoh }
747