lm75.c revision 1.48 1 1.48 macallan /* $NetBSD: lm75.c,v 1.48 2025/08/25 04:31:18 macallan Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2003 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.17 lukem #include <sys/cdefs.h>
39 1.48 macallan __KERNEL_RCSID(0, "$NetBSD: lm75.c,v 1.48 2025/08/25 04:31:18 macallan Exp $");
40 1.17 lukem
41 1.1 thorpej #include <sys/param.h>
42 1.1 thorpej #include <sys/systm.h>
43 1.1 thorpej #include <sys/device.h>
44 1.1 thorpej #include <sys/kernel.h>
45 1.23 macallan #include <sys/sysctl.h>
46 1.1 thorpej
47 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
48 1.1 thorpej
49 1.1 thorpej #include <dev/i2c/i2cvar.h>
50 1.1 thorpej #include <dev/i2c/lm75reg.h>
51 1.1 thorpej
52 1.1 thorpej struct lmtemp_softc {
53 1.23 macallan device_t sc_dev;
54 1.1 thorpej i2c_tag_t sc_tag;
55 1.1 thorpej int sc_address;
56 1.36 macallan prop_dictionary_t sc_prop;
57 1.1 thorpej
58 1.18 xtraeme struct sysmon_envsys *sc_sme;
59 1.16 xtraeme envsys_data_t sc_sensor;
60 1.23 macallan int sc_tmax;
61 1.28 jdc uint32_t sc_smax, sc_smin, sc_scrit;
62 1.9 kiyohara
63 1.27 jdc uint32_t (*sc_lmtemp_decode)(const uint8_t *, int);
64 1.28 jdc void (*sc_lmtemp_encode)(const uint32_t, uint8_t *, int);
65 1.1 thorpej };
66 1.1 thorpej
67 1.18 xtraeme static int lmtemp_match(device_t, cfdata_t, void *);
68 1.18 xtraeme static void lmtemp_attach(device_t, device_t, void *);
69 1.1 thorpej
70 1.18 xtraeme CFATTACH_DECL_NEW(lmtemp, sizeof(struct lmtemp_softc),
71 1.1 thorpej lmtemp_match, lmtemp_attach, NULL, NULL);
72 1.1 thorpej
73 1.16 xtraeme static void lmtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
74 1.18 xtraeme static int lmtemp_config_write(struct lmtemp_softc *, uint8_t);
75 1.28 jdc static int lmtemp_temp_write(struct lmtemp_softc *, uint8_t, uint32_t,
76 1.28 jdc int);
77 1.27 jdc static int lmtemp_temp_read(struct lmtemp_softc *, uint8_t, uint32_t *,
78 1.27 jdc int);
79 1.27 jdc static uint32_t lmtemp_decode_lm75(const uint8_t *, int);
80 1.27 jdc static uint32_t lmtemp_decode_ds75(const uint8_t *, int);
81 1.27 jdc static uint32_t lmtemp_decode_lm77(const uint8_t *, int);
82 1.28 jdc static void lmtemp_encode_lm75(const uint32_t, uint8_t *, int);
83 1.28 jdc static void lmtemp_encode_ds75(const uint32_t, uint8_t *, int);
84 1.28 jdc static void lmtemp_encode_lm77(const uint32_t, uint8_t *, int);
85 1.28 jdc static void lmtemp_getlim_lm75(struct sysmon_envsys *, envsys_data_t *,
86 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
87 1.28 jdc static void lmtemp_getlim_lm77(struct sysmon_envsys *, envsys_data_t *,
88 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
89 1.28 jdc static void lmtemp_setlim_lm75(struct sysmon_envsys *, envsys_data_t *,
90 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
91 1.28 jdc static void lmtemp_setlim_lm77(struct sysmon_envsys *, envsys_data_t *,
92 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
93 1.9 kiyohara
94 1.23 macallan static void lmtemp_setup_sysctl(struct lmtemp_softc *);
95 1.23 macallan static int sysctl_lm75_temp(SYSCTLFN_ARGS);
96 1.21 martin
97 1.41 thorpej enum {
98 1.41 thorpej lmtemp_lm75 = 0,
99 1.41 thorpej lmtemp_ds75 = 1,
100 1.41 thorpej lmtemp_lm77 = 2,
101 1.41 thorpej };
102 1.41 thorpej
103 1.33 thorpej static const struct device_compatible_entry compat_data[] = {
104 1.41 thorpej { .compat = "national,lm75", .value = lmtemp_lm75 },
105 1.41 thorpej { .compat = "i2c-lm75", .value = lmtemp_lm75 },
106 1.41 thorpej { .compat = "lm75", .value = lmtemp_lm75 },
107 1.41 thorpej
108 1.41 thorpej /* XXX Linux treats ds1775 and ds75 differently. */
109 1.41 thorpej { .compat = "dallas,ds1775", .value = lmtemp_ds75 },
110 1.41 thorpej { .compat = "ds1775", .value = lmtemp_ds75 },
111 1.41 thorpej
112 1.41 thorpej { .compat = "national,lm77", .value = lmtemp_lm77 },
113 1.41 thorpej
114 1.21 martin /*
115 1.21 martin * see XXX in _attach() below: add code once non-lm75 matches are
116 1.21 martin * added here!
117 1.21 martin */
118 1.39 thorpej DEVICE_COMPAT_EOL
119 1.32 thorpej };
120 1.32 thorpej
121 1.9 kiyohara static const struct {
122 1.9 kiyohara const char *lmtemp_name;
123 1.9 kiyohara int lmtemp_addrmask;
124 1.9 kiyohara int lmtemp_addr;
125 1.27 jdc uint32_t (*lmtemp_decode)(const uint8_t *, int);
126 1.28 jdc void (*lmtemp_encode)(const uint32_t, uint8_t *, int);
127 1.28 jdc void (*lmtemp_getlim)(struct sysmon_envsys *, envsys_data_t *,
128 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
129 1.28 jdc void (*lmtemp_setlim)(struct sysmon_envsys *, envsys_data_t *,
130 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
131 1.9 kiyohara } lmtemptbl[] = {
132 1.41 thorpej [lmtemp_lm75] =
133 1.41 thorpej {
134 1.41 thorpej .lmtemp_name = "LM75",
135 1.41 thorpej .lmtemp_addrmask = LM75_ADDRMASK,
136 1.41 thorpej .lmtemp_addr = LM75_ADDR,
137 1.41 thorpej .lmtemp_decode = lmtemp_decode_lm75,
138 1.41 thorpej .lmtemp_encode = lmtemp_encode_lm75,
139 1.41 thorpej .lmtemp_getlim = lmtemp_getlim_lm75,
140 1.41 thorpej .lmtemp_setlim = lmtemp_setlim_lm75,
141 1.41 thorpej },
142 1.41 thorpej [lmtemp_ds75] =
143 1.41 thorpej {
144 1.41 thorpej .lmtemp_name = "DS75",
145 1.41 thorpej .lmtemp_addrmask = LM75_ADDRMASK,
146 1.41 thorpej .lmtemp_addr = LM75_ADDR,
147 1.41 thorpej .lmtemp_decode = lmtemp_decode_ds75,
148 1.41 thorpej .lmtemp_encode = lmtemp_encode_ds75,
149 1.41 thorpej .lmtemp_getlim = lmtemp_getlim_lm75,
150 1.41 thorpej .lmtemp_setlim = lmtemp_setlim_lm75,
151 1.41 thorpej },
152 1.41 thorpej [lmtemp_lm77] =
153 1.41 thorpej {
154 1.41 thorpej .lmtemp_name = "LM77",
155 1.41 thorpej .lmtemp_addrmask = LM77_ADDRMASK,
156 1.41 thorpej .lmtemp_addr = LM77_ADDR,
157 1.41 thorpej .lmtemp_decode = lmtemp_decode_lm77,
158 1.41 thorpej .lmtemp_encode = lmtemp_encode_lm77,
159 1.41 thorpej .lmtemp_getlim = lmtemp_getlim_lm77,
160 1.41 thorpej .lmtemp_setlim = lmtemp_setlim_lm77,
161 1.41 thorpej },
162 1.9 kiyohara };
163 1.1 thorpej
164 1.1 thorpej static int
165 1.18 xtraeme lmtemp_match(device_t parent, cfdata_t cf, void *aux)
166 1.1 thorpej {
167 1.1 thorpej struct i2c_attach_args *ia = aux;
168 1.31 thorpej int i, match_result;
169 1.9 kiyohara
170 1.33 thorpej if (iic_use_direct_match(ia, cf, compat_data, &match_result))
171 1.31 thorpej return match_result;
172 1.21 martin
173 1.31 thorpej /*
174 1.31 thorpej * Indirect config - not much we can do!
175 1.31 thorpej */
176 1.41 thorpej for (i = 0; i < __arraycount(lmtemptbl); i++) {
177 1.41 thorpej if (i == cf->cf_flags) {
178 1.31 thorpej break;
179 1.41 thorpej }
180 1.41 thorpej }
181 1.41 thorpej if (i == __arraycount(lmtemptbl)) {
182 1.31 thorpej return 0;
183 1.41 thorpej }
184 1.31 thorpej
185 1.31 thorpej if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
186 1.31 thorpej lmtemptbl[i].lmtemp_addr)
187 1.31 thorpej return I2C_MATCH_ADDRESS_ONLY;
188 1.1 thorpej
189 1.18 xtraeme return 0;
190 1.1 thorpej }
191 1.1 thorpej
192 1.1 thorpej static void
193 1.18 xtraeme lmtemp_attach(device_t parent, device_t self, void *aux)
194 1.1 thorpej {
195 1.6 thorpej struct lmtemp_softc *sc = device_private(self);
196 1.1 thorpej struct i2c_attach_args *ia = aux;
197 1.41 thorpej const struct device_compatible_entry *dce;
198 1.30 macallan char name[64];
199 1.36 macallan const char *desc;
200 1.9 kiyohara int i;
201 1.48 macallan uint8_t config = LM75_CONFIG_FAULT_QUEUE_4;
202 1.9 kiyohara
203 1.23 macallan sc->sc_dev = self;
204 1.41 thorpej dce = iic_compatible_lookup(ia, compat_data);
205 1.41 thorpej if (dce != NULL) {
206 1.41 thorpej i = (int)dce->value;
207 1.41 thorpej } else {
208 1.41 thorpej for (i = 0; i < __arraycount(lmtemptbl); i++) {
209 1.41 thorpej if (i == device_cfdata(self)->cf_flags) {
210 1.21 martin break;
211 1.41 thorpej }
212 1.30 macallan }
213 1.41 thorpej KASSERT(i < __arraycount(lmtemptbl));
214 1.21 martin }
215 1.1 thorpej
216 1.1 thorpej sc->sc_tag = ia->ia_tag;
217 1.1 thorpej sc->sc_address = ia->ia_addr;
218 1.36 macallan sc->sc_prop = ia->ia_prop;
219 1.46 skrll
220 1.43 macallan if (ia->ia_prop != NULL) prop_object_retain(sc->sc_prop);
221 1.1 thorpej
222 1.1 thorpej aprint_naive(": Temperature Sensor\n");
223 1.21 martin if (ia->ia_name) {
224 1.21 martin aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
225 1.21 martin lmtemptbl[i].lmtemp_name);
226 1.21 martin } else {
227 1.21 martin aprint_normal(": %s Temperature Sensor\n",
228 1.21 martin lmtemptbl[i].lmtemp_name);
229 1.21 martin }
230 1.1 thorpej
231 1.27 jdc sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
232 1.28 jdc sc->sc_lmtemp_encode = lmtemptbl[i].lmtemp_encode;
233 1.27 jdc
234 1.44 mlelstv if (iic_acquire_bus(sc->sc_tag, 0)) {
235 1.44 mlelstv aprint_error_dev(self,
236 1.44 mlelstv "unable to acquire I2C bus\n");
237 1.44 mlelstv return;
238 1.44 mlelstv }
239 1.27 jdc
240 1.28 jdc /* Read temperature limit(s) and remember initial value(s). */
241 1.28 jdc if (i == lmtemp_lm77) {
242 1.29 jdc if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT,
243 1.29 jdc &sc->sc_scrit, 1) != 0) {
244 1.29 jdc aprint_error_dev(self,
245 1.29 jdc "unable to read low register\n");
246 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
247 1.29 jdc return;
248 1.29 jdc }
249 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT,
250 1.29 jdc &sc->sc_smin, 1) != 0) {
251 1.28 jdc aprint_error_dev(self,
252 1.28 jdc "unable to read low register\n");
253 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
254 1.28 jdc return;
255 1.28 jdc }
256 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT,
257 1.28 jdc &sc->sc_smax, 1) != 0) {
258 1.28 jdc aprint_error_dev(self,
259 1.28 jdc "unable to read high register\n");
260 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
261 1.28 jdc return;
262 1.28 jdc }
263 1.29 jdc } else { /* LM75 or compatible */
264 1.29 jdc if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT,
265 1.29 jdc &sc->sc_smax, 1) != 0) {
266 1.29 jdc aprint_error_dev(self, "unable to read Tos register\n");
267 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
268 1.29 jdc return;
269 1.29 jdc }
270 1.28 jdc }
271 1.29 jdc sc->sc_tmax = sc->sc_smax;
272 1.27 jdc
273 1.23 macallan if (i == lmtemp_lm75)
274 1.23 macallan lmtemp_setup_sysctl(sc);
275 1.23 macallan
276 1.48 macallan /* DS75 has better resolution */
277 1.48 macallan if (i == lmtemp_ds75)
278 1.48 macallan config = LM75_CONFIG_FAULT_QUEUE_4 | DS75_CONFIG_RES_11BIT;
279 1.48 macallan
280 1.1 thorpej /* Set the configuration of the LM75 to defaults. */
281 1.48 macallan if (lmtemp_config_write(sc, config) != 0) {
282 1.18 xtraeme aprint_error_dev(self, "unable to write config register\n");
283 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
284 1.1 thorpej return;
285 1.1 thorpej }
286 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
287 1.1 thorpej
288 1.16 xtraeme sc->sc_sme = sysmon_envsys_create();
289 1.1 thorpej /* Initialize sensor data. */
290 1.47 skrll sc->sc_sensor.units = ENVSYS_STEMP;
291 1.47 skrll sc->sc_sensor.state = ENVSYS_SINVALID;
292 1.47 skrll sc->sc_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FHAS_ENTROPY;
293 1.30 macallan
294 1.30 macallan (void)strlcpy(name,
295 1.21 martin ia->ia_name? ia->ia_name : device_xname(self),
296 1.18 xtraeme sizeof(sc->sc_sensor.desc));
297 1.36 macallan
298 1.45 christos if (prop_dictionary_get_string(sc->sc_prop, "s00", &desc)) {
299 1.36 macallan strncpy(name, desc, 64);
300 1.30 macallan }
301 1.36 macallan
302 1.30 macallan (void)strlcpy(sc->sc_sensor.desc, name,
303 1.30 macallan sizeof(sc->sc_sensor.desc));
304 1.16 xtraeme if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
305 1.16 xtraeme sysmon_envsys_destroy(sc->sc_sme);
306 1.16 xtraeme return;
307 1.16 xtraeme }
308 1.1 thorpej
309 1.7 riz /* Hook into system monitor. */
310 1.18 xtraeme sc->sc_sme->sme_name = device_xname(self);
311 1.16 xtraeme sc->sc_sme->sme_cookie = sc;
312 1.16 xtraeme sc->sc_sme->sme_refresh = lmtemp_refresh;
313 1.28 jdc sc->sc_sme->sme_get_limits = lmtemptbl[i].lmtemp_getlim;
314 1.28 jdc sc->sc_sme->sme_set_limits = lmtemptbl[i].lmtemp_setlim;
315 1.1 thorpej
316 1.16 xtraeme if (sysmon_envsys_register(sc->sc_sme)) {
317 1.18 xtraeme aprint_error_dev(self, "unable to register with sysmon\n");
318 1.16 xtraeme sysmon_envsys_destroy(sc->sc_sme);
319 1.16 xtraeme }
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej static int
323 1.1 thorpej lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
324 1.1 thorpej {
325 1.1 thorpej uint8_t cmdbuf[2];
326 1.1 thorpej
327 1.1 thorpej cmdbuf[0] = LM75_REG_CONFIG;
328 1.1 thorpej cmdbuf[1] = val;
329 1.1 thorpej
330 1.18 xtraeme return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
331 1.35 thorpej sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0);
332 1.1 thorpej }
333 1.1 thorpej
334 1.1 thorpej static int
335 1.28 jdc lmtemp_temp_write(struct lmtemp_softc *sc, uint8_t reg, uint32_t val, int degc)
336 1.23 macallan {
337 1.23 macallan uint8_t cmdbuf[3];
338 1.23 macallan
339 1.23 macallan cmdbuf[0] = reg;
340 1.28 jdc sc->sc_lmtemp_encode(val, &cmdbuf[1], degc);
341 1.23 macallan
342 1.23 macallan return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
343 1.35 thorpej sc->sc_address, cmdbuf, 1, &cmdbuf[1], 2, 0);
344 1.23 macallan }
345 1.23 macallan
346 1.23 macallan static int
347 1.27 jdc lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp,
348 1.27 jdc int degc)
349 1.1 thorpej {
350 1.2 scw int error;
351 1.1 thorpej uint8_t cmdbuf[1];
352 1.1 thorpej uint8_t buf[LM75_TEMP_LEN];
353 1.1 thorpej
354 1.1 thorpej cmdbuf[0] = which;
355 1.1 thorpej
356 1.1 thorpej error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
357 1.1 thorpej sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
358 1.1 thorpej if (error)
359 1.18 xtraeme return error;
360 1.1 thorpej
361 1.27 jdc *valp = sc->sc_lmtemp_decode(buf, degc);
362 1.18 xtraeme return 0;
363 1.1 thorpej }
364 1.1 thorpej
365 1.1 thorpej static void
366 1.1 thorpej lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
367 1.1 thorpej {
368 1.1 thorpej uint32_t val;
369 1.1 thorpej int error;
370 1.1 thorpej
371 1.27 jdc error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val, 0);
372 1.1 thorpej if (error) {
373 1.1 thorpej #if 0
374 1.25 chs aprint_error_dev(sc->sc_dev, "unable to read temperature, error = %d\n",
375 1.19 cegger error);
376 1.1 thorpej #endif
377 1.16 xtraeme sc->sc_sensor.state = ENVSYS_SINVALID;
378 1.1 thorpej return;
379 1.1 thorpej }
380 1.1 thorpej
381 1.16 xtraeme sc->sc_sensor.value_cur = val;
382 1.16 xtraeme sc->sc_sensor.state = ENVSYS_SVALID;
383 1.1 thorpej }
384 1.1 thorpej
385 1.16 xtraeme static void
386 1.16 xtraeme lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
387 1.1 thorpej {
388 1.1 thorpej struct lmtemp_softc *sc = sme->sme_cookie;
389 1.1 thorpej
390 1.44 mlelstv if (iic_acquire_bus(sc->sc_tag, 0)) /* also locks our instance */
391 1.44 mlelstv return;
392 1.1 thorpej lmtemp_refresh_sensor_data(sc);
393 1.1 thorpej iic_release_bus(sc->sc_tag, 0); /* also unlocks our instance */
394 1.1 thorpej }
395 1.2 scw
396 1.28 jdc static void
397 1.28 jdc lmtemp_getlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
398 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
399 1.28 jdc {
400 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
401 1.28 jdc uint32_t val;
402 1.28 jdc
403 1.28 jdc *props &= ~(PROP_CRITMAX);
404 1.28 jdc
405 1.44 mlelstv if (iic_acquire_bus(sc->sc_tag, 0))
406 1.44 mlelstv return;
407 1.28 jdc if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &val, 0) == 0) {
408 1.28 jdc limits->sel_critmax = val;
409 1.28 jdc *props |= PROP_CRITMAX;
410 1.28 jdc }
411 1.29 jdc iic_release_bus(sc->sc_tag, 0);
412 1.28 jdc }
413 1.28 jdc
414 1.28 jdc static void
415 1.28 jdc lmtemp_getlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
416 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
417 1.28 jdc {
418 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
419 1.28 jdc uint32_t val;
420 1.28 jdc
421 1.28 jdc *props &= ~(PROP_CRITMAX | PROP_WARNMAX | PROP_WARNMIN);
422 1.28 jdc
423 1.44 mlelstv if (iic_acquire_bus(sc->sc_tag, 0))
424 1.44 mlelstv return;
425 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT, &val, 0) == 0) {
426 1.28 jdc limits->sel_critmax = val;
427 1.28 jdc *props |= PROP_CRITMAX;
428 1.28 jdc }
429 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT, &val, 0) == 0) {
430 1.28 jdc limits->sel_warnmax = val;
431 1.28 jdc *props |= PROP_WARNMAX;
432 1.28 jdc }
433 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT, &val, 0) == 0) {
434 1.28 jdc limits->sel_warnmin = val;
435 1.28 jdc *props |= PROP_WARNMIN;
436 1.28 jdc }
437 1.29 jdc iic_release_bus(sc->sc_tag, 0);
438 1.28 jdc }
439 1.28 jdc
440 1.28 jdc static void
441 1.46 skrll lmtemp_setlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
442 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
443 1.28 jdc {
444 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
445 1.28 jdc int32_t limit;
446 1.28 jdc
447 1.28 jdc if (*props & PROP_CRITMAX) {
448 1.28 jdc if (limits == NULL) /* Restore defaults */
449 1.28 jdc limit = sc->sc_smax;
450 1.28 jdc else
451 1.28 jdc limit = limits->sel_critmax;
452 1.44 mlelstv if (iic_acquire_bus(sc->sc_tag, 0))
453 1.44 mlelstv return;
454 1.28 jdc lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
455 1.28 jdc limit - 5000000, 0);
456 1.28 jdc lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT, limit, 0);
457 1.29 jdc iic_release_bus(sc->sc_tag, 0);
458 1.28 jdc
459 1.28 jdc /* Synchronise sysctl */
460 1.28 jdc sc->sc_tmax = (limit - 273150000) / 1000000;
461 1.28 jdc }
462 1.28 jdc }
463 1.28 jdc
464 1.28 jdc static void
465 1.46 skrll lmtemp_setlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
466 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
467 1.28 jdc {
468 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
469 1.28 jdc int32_t limit;
470 1.28 jdc
471 1.29 jdc iic_acquire_bus(sc->sc_tag, 0);
472 1.28 jdc if (*props & PROP_CRITMAX) {
473 1.28 jdc if (limits == NULL) /* Restore defaults */
474 1.29 jdc limit = sc->sc_scrit;
475 1.28 jdc else
476 1.28 jdc limit = limits->sel_critmax;
477 1.28 jdc lmtemp_temp_write(sc, LM77_REG_TCRIT_SET_POINT, limit, 0);
478 1.28 jdc }
479 1.28 jdc if (*props & PROP_WARNMAX) {
480 1.28 jdc if (limits == NULL) /* Restore defaults */
481 1.28 jdc limit = sc->sc_smax;
482 1.28 jdc else
483 1.28 jdc limit = limits->sel_warnmax;
484 1.28 jdc lmtemp_temp_write(sc, LM77_REG_THIGH_SET_POINT, limit, 0);
485 1.28 jdc }
486 1.28 jdc if (*props & PROP_WARNMIN) {
487 1.28 jdc if (limits == NULL) /* Restore defaults */
488 1.29 jdc limit = sc->sc_smin;
489 1.28 jdc else
490 1.28 jdc limit = limits->sel_warnmin;
491 1.28 jdc lmtemp_temp_write(sc, LM77_REG_TLOW_SET_POINT, limit, 0);
492 1.28 jdc }
493 1.29 jdc iic_release_bus(sc->sc_tag, 0);
494 1.28 jdc }
495 1.28 jdc
496 1.2 scw static uint32_t
497 1.27 jdc lmtemp_decode_lm75(const uint8_t *buf, int degc)
498 1.2 scw {
499 1.20 briggs int temp;
500 1.2 scw uint32_t val;
501 1.2 scw
502 1.20 briggs /*
503 1.20 briggs * LM75 temps are the most-significant 9 bits of a 16-bit reg.
504 1.20 briggs * sign-extend the MSB and add in the 0.5 from the LSB
505 1.20 briggs */
506 1.20 briggs temp = (int8_t) buf[0];
507 1.20 briggs temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
508 1.2 scw
509 1.27 jdc /* Temp is given in 1/2 deg. C, we convert to C or uK. */
510 1.27 jdc if (degc)
511 1.27 jdc val = temp / 2;
512 1.27 jdc else
513 1.27 jdc val = temp * 500000 + 273150000;
514 1.2 scw
515 1.18 xtraeme return val;
516 1.2 scw }
517 1.2 scw
518 1.2 scw static uint32_t
519 1.27 jdc lmtemp_decode_ds75(const uint8_t *buf, int degc)
520 1.2 scw {
521 1.2 scw int temp;
522 1.2 scw
523 1.2 scw /*
524 1.2 scw * Sign-extend the MSB byte, and add in the fractions of a
525 1.7 riz * degree contained in the LSB (precision 1/16th DegC).
526 1.2 scw */
527 1.2 scw temp = (int8_t)buf[0];
528 1.2 scw temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
529 1.2 scw
530 1.2 scw /*
531 1.27 jdc * Conversion to C or uK is simple.
532 1.2 scw */
533 1.27 jdc if (degc)
534 1.27 jdc return temp / 16;
535 1.27 jdc else
536 1.27 jdc return (temp * 62500 + 273150000);
537 1.2 scw }
538 1.2 scw
539 1.9 kiyohara static uint32_t
540 1.27 jdc lmtemp_decode_lm77(const uint8_t *buf, int degc)
541 1.9 kiyohara {
542 1.9 kiyohara int temp;
543 1.9 kiyohara uint32_t val;
544 1.9 kiyohara
545 1.9 kiyohara /*
546 1.9 kiyohara * Describe each bits of temperature registers on LM77.
547 1.9 kiyohara * D15 - D12: Sign
548 1.9 kiyohara * D11 - D3 : Bit8(MSB) - Bit0
549 1.9 kiyohara */
550 1.9 kiyohara temp = (int8_t)buf[0];
551 1.10 kiyohara temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
552 1.9 kiyohara
553 1.27 jdc /* Temp is given in 1/2 deg. C, we convert to C or uK. */
554 1.27 jdc if (degc)
555 1.27 jdc val = temp / 2;
556 1.27 jdc else
557 1.27 jdc val = temp * 500000 + 273150000;
558 1.9 kiyohara
559 1.18 xtraeme return val;
560 1.9 kiyohara }
561 1.9 kiyohara
562 1.28 jdc static void lmtemp_encode_lm75(const uint32_t val, uint8_t *buf, int degc)
563 1.28 jdc {
564 1.28 jdc int temp;
565 1.28 jdc
566 1.28 jdc /* Convert from C or uK to register format */
567 1.28 jdc if (degc)
568 1.28 jdc temp = val * 2;
569 1.28 jdc else
570 1.28 jdc temp = (val - 273150000) / 500000;
571 1.28 jdc buf[0] = (temp >> 1) & 0xff;
572 1.28 jdc buf[1] = (temp & 1) << 7;
573 1.28 jdc }
574 1.28 jdc
575 1.28 jdc static void lmtemp_encode_ds75(const uint32_t val, uint8_t *buf, int degc)
576 1.28 jdc {
577 1.28 jdc int temp;
578 1.28 jdc
579 1.28 jdc /* Convert from C or uK to register format */
580 1.28 jdc if (degc)
581 1.28 jdc temp = val * 16;
582 1.28 jdc else
583 1.28 jdc temp = (val - 273150000) / 62500;
584 1.28 jdc buf[0] = (temp >> 4) & 0xff;
585 1.28 jdc buf[1] = (temp & 0xf) << 4;
586 1.28 jdc }
587 1.28 jdc
588 1.28 jdc static void lmtemp_encode_lm77(const uint32_t val, uint8_t *buf, int degc)
589 1.28 jdc {
590 1.28 jdc int temp;
591 1.28 jdc
592 1.28 jdc /* Convert from C or uK to register format */
593 1.28 jdc if (degc)
594 1.28 jdc temp = val * 2;
595 1.28 jdc else
596 1.28 jdc temp = (val - 273150000) / 500000;
597 1.28 jdc buf[0] = (temp >> 5) & 0xff;
598 1.28 jdc buf[1] = (temp & 0x1f) << 3;
599 1.28 jdc }
600 1.28 jdc
601 1.23 macallan static void
602 1.23 macallan lmtemp_setup_sysctl(struct lmtemp_softc *sc)
603 1.23 macallan {
604 1.23 macallan const struct sysctlnode *me = NULL, *node = NULL;
605 1.23 macallan
606 1.23 macallan sysctl_createv(NULL, 0, NULL, &me,
607 1.23 macallan CTLFLAG_READWRITE,
608 1.23 macallan CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
609 1.23 macallan NULL, 0, NULL, 0,
610 1.23 macallan CTL_MACHDEP, CTL_CREATE, CTL_EOL);
611 1.23 macallan
612 1.23 macallan sysctl_createv(NULL, 0, NULL, &node,
613 1.23 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
614 1.23 macallan CTLTYPE_INT, "temp", "Threshold temperature",
615 1.24 dsl sysctl_lm75_temp, 1, (void *)sc, 0,
616 1.23 macallan CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
617 1.23 macallan }
618 1.23 macallan
619 1.23 macallan static int
620 1.23 macallan sysctl_lm75_temp(SYSCTLFN_ARGS)
621 1.23 macallan {
622 1.23 macallan struct sysctlnode node = *rnode;
623 1.23 macallan struct lmtemp_softc *sc = node.sysctl_data;
624 1.44 mlelstv int temp, error;
625 1.23 macallan
626 1.23 macallan if (newp) {
627 1.23 macallan
628 1.46 skrll /* we're asked to write */
629 1.23 macallan node.sysctl_data = &sc->sc_tmax;
630 1.23 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
631 1.23 macallan
632 1.23 macallan temp = *(int *)node.sysctl_data;
633 1.23 macallan sc->sc_tmax = temp;
634 1.44 mlelstv error = iic_acquire_bus(sc->sc_tag, 0);
635 1.44 mlelstv if (error)
636 1.44 mlelstv return error;
637 1.23 macallan lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
638 1.28 jdc sc->sc_tmax - 5, 1);
639 1.23 macallan lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT,
640 1.28 jdc sc->sc_tmax, 1);
641 1.35 thorpej iic_release_bus(sc->sc_tag, 0);
642 1.28 jdc
643 1.28 jdc /* Synchronise envsys - calls lmtemp_getlim_lm75() */
644 1.28 jdc sysmon_envsys_update_limits(sc->sc_sme, &sc->sc_sensor);
645 1.23 macallan return 0;
646 1.23 macallan }
647 1.23 macallan return EINVAL;
648 1.23 macallan } else {
649 1.23 macallan
650 1.23 macallan node.sysctl_data = &sc->sc_tmax;
651 1.23 macallan node.sysctl_size = 4;
652 1.23 macallan return (sysctl_lookup(SYSCTLFN_CALL(&node)));
653 1.23 macallan }
654 1.23 macallan
655 1.23 macallan return 0;
656 1.23 macallan }
657 1.23 macallan
658 1.23 macallan SYSCTL_SETUP(sysctl_lmtemp_setup, "sysctl lmtemp subtree setup")
659 1.23 macallan {
660 1.23 macallan
661 1.23 macallan sysctl_createv(NULL, 0, NULL, NULL,
662 1.23 macallan CTLFLAG_PERMANENT,
663 1.23 macallan CTLTYPE_NODE, "machdep", NULL,
664 1.23 macallan NULL, 0, NULL, 0,
665 1.23 macallan CTL_MACHDEP, CTL_EOL);
666 1.23 macallan }
667 1.23 macallan
668 1.23 macallan
669