lm75.c revision 1.30 1 1.30 macallan /* $NetBSD: lm75.c,v 1.30 2017/10/01 05:12: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.30 macallan __KERNEL_RCSID(0, "$NetBSD: lm75.c,v 1.30 2017/10/01 05:12: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.30 macallan #ifdef macppc
53 1.30 macallan #define HAVE_OF 1
54 1.30 macallan #endif
55 1.30 macallan
56 1.30 macallan #ifdef HAVE_OF
57 1.30 macallan #include <dev/ofw/openfirm.h>
58 1.30 macallan #endif
59 1.30 macallan
60 1.1 thorpej struct lmtemp_softc {
61 1.23 macallan device_t sc_dev;
62 1.1 thorpej i2c_tag_t sc_tag;
63 1.1 thorpej int sc_address;
64 1.1 thorpej
65 1.18 xtraeme struct sysmon_envsys *sc_sme;
66 1.16 xtraeme envsys_data_t sc_sensor;
67 1.23 macallan int sc_tmax;
68 1.28 jdc uint32_t sc_smax, sc_smin, sc_scrit;
69 1.9 kiyohara
70 1.27 jdc uint32_t (*sc_lmtemp_decode)(const uint8_t *, int);
71 1.28 jdc void (*sc_lmtemp_encode)(const uint32_t, uint8_t *, int);
72 1.1 thorpej };
73 1.1 thorpej
74 1.18 xtraeme static int lmtemp_match(device_t, cfdata_t, void *);
75 1.18 xtraeme static void lmtemp_attach(device_t, device_t, void *);
76 1.1 thorpej
77 1.18 xtraeme CFATTACH_DECL_NEW(lmtemp, sizeof(struct lmtemp_softc),
78 1.1 thorpej lmtemp_match, lmtemp_attach, NULL, NULL);
79 1.1 thorpej
80 1.16 xtraeme static void lmtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
81 1.18 xtraeme static int lmtemp_config_write(struct lmtemp_softc *, uint8_t);
82 1.28 jdc static int lmtemp_temp_write(struct lmtemp_softc *, uint8_t, uint32_t,
83 1.28 jdc int);
84 1.27 jdc static int lmtemp_temp_read(struct lmtemp_softc *, uint8_t, uint32_t *,
85 1.27 jdc int);
86 1.27 jdc static uint32_t lmtemp_decode_lm75(const uint8_t *, int);
87 1.27 jdc static uint32_t lmtemp_decode_ds75(const uint8_t *, int);
88 1.27 jdc static uint32_t lmtemp_decode_lm77(const uint8_t *, int);
89 1.28 jdc static void lmtemp_encode_lm75(const uint32_t, uint8_t *, int);
90 1.28 jdc static void lmtemp_encode_ds75(const uint32_t, uint8_t *, int);
91 1.28 jdc static void lmtemp_encode_lm77(const uint32_t, uint8_t *, int);
92 1.28 jdc static void lmtemp_getlim_lm75(struct sysmon_envsys *, envsys_data_t *,
93 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
94 1.28 jdc static void lmtemp_getlim_lm77(struct sysmon_envsys *, envsys_data_t *,
95 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
96 1.28 jdc static void lmtemp_setlim_lm75(struct sysmon_envsys *, envsys_data_t *,
97 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
98 1.28 jdc static void lmtemp_setlim_lm77(struct sysmon_envsys *, envsys_data_t *,
99 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
100 1.9 kiyohara
101 1.23 macallan static void lmtemp_setup_sysctl(struct lmtemp_softc *);
102 1.23 macallan static int sysctl_lm75_temp(SYSCTLFN_ARGS);
103 1.21 martin
104 1.21 martin static const char * lmtemp_compats[] = {
105 1.21 martin "i2c-lm75",
106 1.30 macallan "ds1775",
107 1.21 martin /*
108 1.21 martin * see XXX in _attach() below: add code once non-lm75 matches are
109 1.21 martin * added here!
110 1.21 martin */
111 1.21 martin NULL
112 1.21 martin };
113 1.21 martin
114 1.9 kiyohara enum {
115 1.9 kiyohara lmtemp_lm75 = 0,
116 1.9 kiyohara lmtemp_ds75,
117 1.9 kiyohara lmtemp_lm77,
118 1.9 kiyohara };
119 1.9 kiyohara static const struct {
120 1.9 kiyohara int lmtemp_type;
121 1.9 kiyohara const char *lmtemp_name;
122 1.9 kiyohara int lmtemp_addrmask;
123 1.9 kiyohara int lmtemp_addr;
124 1.27 jdc uint32_t (*lmtemp_decode)(const uint8_t *, int);
125 1.28 jdc void (*lmtemp_encode)(const uint32_t, uint8_t *, int);
126 1.28 jdc void (*lmtemp_getlim)(struct sysmon_envsys *, envsys_data_t *,
127 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
128 1.28 jdc void (*lmtemp_setlim)(struct sysmon_envsys *, envsys_data_t *,
129 1.28 jdc sysmon_envsys_lim_t *, uint32_t *);
130 1.9 kiyohara } lmtemptbl[] = {
131 1.28 jdc { lmtemp_lm75, "LM75", LM75_ADDRMASK, LM75_ADDR,
132 1.28 jdc lmtemp_decode_lm75, lmtemp_encode_lm75,
133 1.28 jdc lmtemp_getlim_lm75, lmtemp_setlim_lm75 },
134 1.28 jdc { lmtemp_ds75, "DS75", LM75_ADDRMASK, LM75_ADDR,
135 1.28 jdc lmtemp_decode_ds75, lmtemp_encode_ds75,
136 1.28 jdc lmtemp_getlim_lm75, lmtemp_setlim_lm75 },
137 1.28 jdc { lmtemp_lm77, "LM77", LM77_ADDRMASK, LM77_ADDR,
138 1.28 jdc lmtemp_decode_lm77, lmtemp_encode_lm77,
139 1.28 jdc lmtemp_getlim_lm77, lmtemp_setlim_lm77 },
140 1.28 jdc { -1, NULL, 0, 0,
141 1.28 jdc NULL, NULL,
142 1.28 jdc NULL, NULL }
143 1.9 kiyohara };
144 1.1 thorpej
145 1.1 thorpej static int
146 1.18 xtraeme lmtemp_match(device_t parent, cfdata_t cf, void *aux)
147 1.1 thorpej {
148 1.1 thorpej struct i2c_attach_args *ia = aux;
149 1.9 kiyohara int i;
150 1.9 kiyohara
151 1.21 martin if (ia->ia_name == NULL) {
152 1.21 martin /*
153 1.21 martin * Indirect config - not much we can do!
154 1.21 martin */
155 1.21 martin for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
156 1.21 martin if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
157 1.21 martin break;
158 1.21 martin if (lmtemptbl[i].lmtemp_type == -1)
159 1.21 martin return 0;
160 1.21 martin
161 1.21 martin if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
162 1.21 martin lmtemptbl[i].lmtemp_addr)
163 1.21 martin return 1;
164 1.21 martin } else {
165 1.21 martin /*
166 1.21 martin * Direct config - match via the list of compatible
167 1.26 phx * hardware or simply match the device name.
168 1.21 martin */
169 1.26 phx if (ia->ia_ncompat > 0) {
170 1.26 phx if (iic_compat_match(ia, lmtemp_compats))
171 1.26 phx return 1;
172 1.26 phx } else {
173 1.26 phx if (strcmp(ia->ia_name, "lmtemp") == 0)
174 1.26 phx return 1;
175 1.26 phx }
176 1.21 martin }
177 1.21 martin
178 1.1 thorpej
179 1.18 xtraeme return 0;
180 1.1 thorpej }
181 1.1 thorpej
182 1.1 thorpej static void
183 1.18 xtraeme lmtemp_attach(device_t parent, device_t self, void *aux)
184 1.1 thorpej {
185 1.6 thorpej struct lmtemp_softc *sc = device_private(self);
186 1.1 thorpej struct i2c_attach_args *ia = aux;
187 1.30 macallan char name[64];
188 1.9 kiyohara int i;
189 1.9 kiyohara
190 1.23 macallan sc->sc_dev = self;
191 1.21 martin if (ia->ia_name == NULL) {
192 1.21 martin for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
193 1.21 martin if (lmtemptbl[i].lmtemp_type ==
194 1.21 martin device_cfdata(self)->cf_flags)
195 1.21 martin break;
196 1.21 martin } else {
197 1.30 macallan if (strcmp(ia->ia_name, "ds1775") == 0) {
198 1.30 macallan i = 1; /* LMTYPE_DS75 */
199 1.30 macallan } else {
200 1.30 macallan /* XXX - add code when adding other direct matches! */
201 1.30 macallan i = 0;
202 1.30 macallan }
203 1.21 martin }
204 1.1 thorpej
205 1.1 thorpej sc->sc_tag = ia->ia_tag;
206 1.1 thorpej sc->sc_address = ia->ia_addr;
207 1.1 thorpej
208 1.1 thorpej aprint_naive(": Temperature Sensor\n");
209 1.21 martin if (ia->ia_name) {
210 1.21 martin aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
211 1.21 martin lmtemptbl[i].lmtemp_name);
212 1.21 martin } else {
213 1.21 martin aprint_normal(": %s Temperature Sensor\n",
214 1.21 martin lmtemptbl[i].lmtemp_name);
215 1.21 martin }
216 1.1 thorpej
217 1.27 jdc sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
218 1.28 jdc sc->sc_lmtemp_encode = lmtemptbl[i].lmtemp_encode;
219 1.27 jdc
220 1.27 jdc iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
221 1.27 jdc
222 1.28 jdc /* Read temperature limit(s) and remember initial value(s). */
223 1.28 jdc if (i == lmtemp_lm77) {
224 1.29 jdc if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT,
225 1.29 jdc &sc->sc_scrit, 1) != 0) {
226 1.29 jdc aprint_error_dev(self,
227 1.29 jdc "unable to read low register\n");
228 1.29 jdc iic_release_bus(sc->sc_tag, I2C_F_POLL);
229 1.29 jdc return;
230 1.29 jdc }
231 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT,
232 1.29 jdc &sc->sc_smin, 1) != 0) {
233 1.28 jdc aprint_error_dev(self,
234 1.28 jdc "unable to read low register\n");
235 1.28 jdc iic_release_bus(sc->sc_tag, I2C_F_POLL);
236 1.28 jdc return;
237 1.28 jdc }
238 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT,
239 1.28 jdc &sc->sc_smax, 1) != 0) {
240 1.28 jdc aprint_error_dev(self,
241 1.28 jdc "unable to read high register\n");
242 1.28 jdc iic_release_bus(sc->sc_tag, I2C_F_POLL);
243 1.28 jdc return;
244 1.28 jdc }
245 1.29 jdc } else { /* LM75 or compatible */
246 1.29 jdc if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT,
247 1.29 jdc &sc->sc_smax, 1) != 0) {
248 1.29 jdc aprint_error_dev(self, "unable to read Tos register\n");
249 1.29 jdc iic_release_bus(sc->sc_tag, I2C_F_POLL);
250 1.29 jdc return;
251 1.29 jdc }
252 1.28 jdc }
253 1.29 jdc sc->sc_tmax = sc->sc_smax;
254 1.27 jdc
255 1.23 macallan if (i == lmtemp_lm75)
256 1.23 macallan lmtemp_setup_sysctl(sc);
257 1.23 macallan
258 1.1 thorpej /* Set the configuration of the LM75 to defaults. */
259 1.23 macallan if (lmtemp_config_write(sc, LM75_CONFIG_FAULT_QUEUE_4) != 0) {
260 1.18 xtraeme aprint_error_dev(self, "unable to write config register\n");
261 1.1 thorpej iic_release_bus(sc->sc_tag, I2C_F_POLL);
262 1.1 thorpej return;
263 1.1 thorpej }
264 1.1 thorpej iic_release_bus(sc->sc_tag, I2C_F_POLL);
265 1.1 thorpej
266 1.16 xtraeme sc->sc_sme = sysmon_envsys_create();
267 1.1 thorpej /* Initialize sensor data. */
268 1.16 xtraeme sc->sc_sensor.units = ENVSYS_STEMP;
269 1.22 pgoyette sc->sc_sensor.state = ENVSYS_SINVALID;
270 1.28 jdc sc->sc_sensor.flags = ENVSYS_FMONLIMITS;
271 1.30 macallan
272 1.30 macallan (void)strlcpy(name,
273 1.21 martin ia->ia_name? ia->ia_name : device_xname(self),
274 1.18 xtraeme sizeof(sc->sc_sensor.desc));
275 1.30 macallan #ifdef HAVE_OF
276 1.30 macallan int ch;
277 1.30 macallan ch = OF_child(ia->ia_cookie);
278 1.30 macallan if (ch != 0) {
279 1.30 macallan OF_getprop(ch, "location", name, 64);
280 1.30 macallan }
281 1.30 macallan #endif
282 1.30 macallan (void)strlcpy(sc->sc_sensor.desc, name,
283 1.30 macallan sizeof(sc->sc_sensor.desc));
284 1.16 xtraeme if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
285 1.16 xtraeme sysmon_envsys_destroy(sc->sc_sme);
286 1.16 xtraeme return;
287 1.16 xtraeme }
288 1.1 thorpej
289 1.7 riz /* Hook into system monitor. */
290 1.18 xtraeme sc->sc_sme->sme_name = device_xname(self);
291 1.16 xtraeme sc->sc_sme->sme_cookie = sc;
292 1.16 xtraeme sc->sc_sme->sme_refresh = lmtemp_refresh;
293 1.28 jdc sc->sc_sme->sme_get_limits = lmtemptbl[i].lmtemp_getlim;
294 1.28 jdc sc->sc_sme->sme_set_limits = lmtemptbl[i].lmtemp_setlim;
295 1.1 thorpej
296 1.16 xtraeme if (sysmon_envsys_register(sc->sc_sme)) {
297 1.18 xtraeme aprint_error_dev(self, "unable to register with sysmon\n");
298 1.16 xtraeme sysmon_envsys_destroy(sc->sc_sme);
299 1.16 xtraeme }
300 1.1 thorpej }
301 1.1 thorpej
302 1.1 thorpej static int
303 1.1 thorpej lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
304 1.1 thorpej {
305 1.1 thorpej uint8_t cmdbuf[2];
306 1.1 thorpej
307 1.1 thorpej cmdbuf[0] = LM75_REG_CONFIG;
308 1.1 thorpej cmdbuf[1] = val;
309 1.1 thorpej
310 1.18 xtraeme return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
311 1.18 xtraeme sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL);
312 1.1 thorpej }
313 1.1 thorpej
314 1.1 thorpej static int
315 1.28 jdc lmtemp_temp_write(struct lmtemp_softc *sc, uint8_t reg, uint32_t val, int degc)
316 1.23 macallan {
317 1.23 macallan uint8_t cmdbuf[3];
318 1.23 macallan
319 1.23 macallan cmdbuf[0] = reg;
320 1.28 jdc sc->sc_lmtemp_encode(val, &cmdbuf[1], degc);
321 1.23 macallan
322 1.23 macallan return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
323 1.23 macallan sc->sc_address, cmdbuf, 1, &cmdbuf[1], 2, I2C_F_POLL);
324 1.23 macallan }
325 1.23 macallan
326 1.23 macallan static int
327 1.27 jdc lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp,
328 1.27 jdc int degc)
329 1.1 thorpej {
330 1.2 scw int error;
331 1.1 thorpej uint8_t cmdbuf[1];
332 1.1 thorpej uint8_t buf[LM75_TEMP_LEN];
333 1.1 thorpej
334 1.1 thorpej cmdbuf[0] = which;
335 1.1 thorpej
336 1.1 thorpej error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
337 1.1 thorpej sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
338 1.1 thorpej if (error)
339 1.18 xtraeme return error;
340 1.1 thorpej
341 1.27 jdc *valp = sc->sc_lmtemp_decode(buf, degc);
342 1.18 xtraeme return 0;
343 1.1 thorpej }
344 1.1 thorpej
345 1.1 thorpej static void
346 1.1 thorpej lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
347 1.1 thorpej {
348 1.1 thorpej uint32_t val;
349 1.1 thorpej int error;
350 1.1 thorpej
351 1.27 jdc error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val, 0);
352 1.1 thorpej if (error) {
353 1.1 thorpej #if 0
354 1.25 chs aprint_error_dev(sc->sc_dev, "unable to read temperature, error = %d\n",
355 1.19 cegger error);
356 1.1 thorpej #endif
357 1.16 xtraeme sc->sc_sensor.state = ENVSYS_SINVALID;
358 1.1 thorpej return;
359 1.1 thorpej }
360 1.1 thorpej
361 1.16 xtraeme sc->sc_sensor.value_cur = val;
362 1.16 xtraeme sc->sc_sensor.state = ENVSYS_SVALID;
363 1.1 thorpej }
364 1.1 thorpej
365 1.16 xtraeme static void
366 1.16 xtraeme lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
367 1.1 thorpej {
368 1.1 thorpej struct lmtemp_softc *sc = sme->sme_cookie;
369 1.1 thorpej
370 1.1 thorpej iic_acquire_bus(sc->sc_tag, 0); /* also locks our instance */
371 1.1 thorpej lmtemp_refresh_sensor_data(sc);
372 1.1 thorpej iic_release_bus(sc->sc_tag, 0); /* also unlocks our instance */
373 1.1 thorpej }
374 1.2 scw
375 1.28 jdc static void
376 1.28 jdc lmtemp_getlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
377 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
378 1.28 jdc {
379 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
380 1.28 jdc uint32_t val;
381 1.28 jdc
382 1.28 jdc *props &= ~(PROP_CRITMAX);
383 1.28 jdc
384 1.29 jdc iic_acquire_bus(sc->sc_tag, 0);
385 1.28 jdc if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &val, 0) == 0) {
386 1.28 jdc limits->sel_critmax = val;
387 1.28 jdc *props |= PROP_CRITMAX;
388 1.28 jdc }
389 1.29 jdc iic_release_bus(sc->sc_tag, 0);
390 1.28 jdc }
391 1.28 jdc
392 1.28 jdc static void
393 1.28 jdc lmtemp_getlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
394 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
395 1.28 jdc {
396 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
397 1.28 jdc uint32_t val;
398 1.28 jdc
399 1.28 jdc *props &= ~(PROP_CRITMAX | PROP_WARNMAX | PROP_WARNMIN);
400 1.28 jdc
401 1.29 jdc iic_acquire_bus(sc->sc_tag, 0);
402 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT, &val, 0) == 0) {
403 1.28 jdc limits->sel_critmax = val;
404 1.28 jdc *props |= PROP_CRITMAX;
405 1.28 jdc }
406 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT, &val, 0) == 0) {
407 1.28 jdc limits->sel_warnmax = val;
408 1.28 jdc *props |= PROP_WARNMAX;
409 1.28 jdc }
410 1.28 jdc if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT, &val, 0) == 0) {
411 1.28 jdc limits->sel_warnmin = val;
412 1.28 jdc *props |= PROP_WARNMIN;
413 1.28 jdc }
414 1.29 jdc iic_release_bus(sc->sc_tag, 0);
415 1.28 jdc }
416 1.28 jdc
417 1.28 jdc static void
418 1.28 jdc lmtemp_setlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
419 1.28 jdc sysmon_envsys_lim_t *limits, uint32_t *props)
420 1.28 jdc {
421 1.28 jdc struct lmtemp_softc *sc = sme->sme_cookie;
422 1.28 jdc int32_t limit;
423 1.28 jdc
424 1.28 jdc if (*props & PROP_CRITMAX) {
425 1.28 jdc if (limits == NULL) /* Restore defaults */
426 1.28 jdc limit = sc->sc_smax;
427 1.28 jdc else
428 1.28 jdc limit = limits->sel_critmax;
429 1.29 jdc iic_acquire_bus(sc->sc_tag, 0);
430 1.28 jdc lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
431 1.28 jdc limit - 5000000, 0);
432 1.28 jdc lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT, limit, 0);
433 1.29 jdc iic_release_bus(sc->sc_tag, 0);
434 1.28 jdc
435 1.28 jdc /* Synchronise sysctl */
436 1.28 jdc sc->sc_tmax = (limit - 273150000) / 1000000;
437 1.28 jdc }
438 1.28 jdc }
439 1.28 jdc
440 1.28 jdc static void
441 1.28 jdc lmtemp_setlim_lm77(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.29 jdc iic_acquire_bus(sc->sc_tag, 0);
448 1.28 jdc if (*props & PROP_CRITMAX) {
449 1.28 jdc if (limits == NULL) /* Restore defaults */
450 1.29 jdc limit = sc->sc_scrit;
451 1.28 jdc else
452 1.28 jdc limit = limits->sel_critmax;
453 1.28 jdc lmtemp_temp_write(sc, LM77_REG_TCRIT_SET_POINT, limit, 0);
454 1.28 jdc }
455 1.28 jdc if (*props & PROP_WARNMAX) {
456 1.28 jdc if (limits == NULL) /* Restore defaults */
457 1.28 jdc limit = sc->sc_smax;
458 1.28 jdc else
459 1.28 jdc limit = limits->sel_warnmax;
460 1.28 jdc lmtemp_temp_write(sc, LM77_REG_THIGH_SET_POINT, limit, 0);
461 1.28 jdc }
462 1.28 jdc if (*props & PROP_WARNMIN) {
463 1.28 jdc if (limits == NULL) /* Restore defaults */
464 1.29 jdc limit = sc->sc_smin;
465 1.28 jdc else
466 1.28 jdc limit = limits->sel_warnmin;
467 1.28 jdc lmtemp_temp_write(sc, LM77_REG_TLOW_SET_POINT, limit, 0);
468 1.28 jdc }
469 1.29 jdc iic_release_bus(sc->sc_tag, 0);
470 1.28 jdc }
471 1.28 jdc
472 1.2 scw static uint32_t
473 1.27 jdc lmtemp_decode_lm75(const uint8_t *buf, int degc)
474 1.2 scw {
475 1.20 briggs int temp;
476 1.2 scw uint32_t val;
477 1.2 scw
478 1.20 briggs /*
479 1.20 briggs * LM75 temps are the most-significant 9 bits of a 16-bit reg.
480 1.20 briggs * sign-extend the MSB and add in the 0.5 from the LSB
481 1.20 briggs */
482 1.20 briggs temp = (int8_t) buf[0];
483 1.20 briggs temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
484 1.2 scw
485 1.27 jdc /* Temp is given in 1/2 deg. C, we convert to C or uK. */
486 1.27 jdc if (degc)
487 1.27 jdc val = temp / 2;
488 1.27 jdc else
489 1.27 jdc val = temp * 500000 + 273150000;
490 1.2 scw
491 1.18 xtraeme return val;
492 1.2 scw }
493 1.2 scw
494 1.2 scw static uint32_t
495 1.27 jdc lmtemp_decode_ds75(const uint8_t *buf, int degc)
496 1.2 scw {
497 1.2 scw int temp;
498 1.2 scw
499 1.2 scw /*
500 1.2 scw * Sign-extend the MSB byte, and add in the fractions of a
501 1.7 riz * degree contained in the LSB (precision 1/16th DegC).
502 1.2 scw */
503 1.2 scw temp = (int8_t)buf[0];
504 1.2 scw temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
505 1.2 scw
506 1.2 scw /*
507 1.27 jdc * Conversion to C or uK is simple.
508 1.2 scw */
509 1.27 jdc if (degc)
510 1.27 jdc return temp / 16;
511 1.27 jdc else
512 1.27 jdc return (temp * 62500 + 273150000);
513 1.2 scw }
514 1.2 scw
515 1.9 kiyohara static uint32_t
516 1.27 jdc lmtemp_decode_lm77(const uint8_t *buf, int degc)
517 1.9 kiyohara {
518 1.9 kiyohara int temp;
519 1.9 kiyohara uint32_t val;
520 1.9 kiyohara
521 1.9 kiyohara /*
522 1.9 kiyohara * Describe each bits of temperature registers on LM77.
523 1.9 kiyohara * D15 - D12: Sign
524 1.9 kiyohara * D11 - D3 : Bit8(MSB) - Bit0
525 1.9 kiyohara */
526 1.9 kiyohara temp = (int8_t)buf[0];
527 1.10 kiyohara temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
528 1.9 kiyohara
529 1.27 jdc /* Temp is given in 1/2 deg. C, we convert to C or uK. */
530 1.27 jdc if (degc)
531 1.27 jdc val = temp / 2;
532 1.27 jdc else
533 1.27 jdc val = temp * 500000 + 273150000;
534 1.9 kiyohara
535 1.18 xtraeme return val;
536 1.9 kiyohara }
537 1.9 kiyohara
538 1.28 jdc static void lmtemp_encode_lm75(const uint32_t val, uint8_t *buf, int degc)
539 1.28 jdc {
540 1.28 jdc int temp;
541 1.28 jdc
542 1.28 jdc /* Convert from C or uK to register format */
543 1.28 jdc if (degc)
544 1.28 jdc temp = val * 2;
545 1.28 jdc else
546 1.28 jdc temp = (val - 273150000) / 500000;
547 1.28 jdc buf[0] = (temp >> 1) & 0xff;
548 1.28 jdc buf[1] = (temp & 1) << 7;
549 1.28 jdc }
550 1.28 jdc
551 1.28 jdc static void lmtemp_encode_ds75(const uint32_t val, uint8_t *buf, int degc)
552 1.28 jdc {
553 1.28 jdc int temp;
554 1.28 jdc
555 1.28 jdc /* Convert from C or uK to register format */
556 1.28 jdc if (degc)
557 1.28 jdc temp = val * 16;
558 1.28 jdc else
559 1.28 jdc temp = (val - 273150000) / 62500;
560 1.28 jdc buf[0] = (temp >> 4) & 0xff;
561 1.28 jdc buf[1] = (temp & 0xf) << 4;
562 1.28 jdc }
563 1.28 jdc
564 1.28 jdc static void lmtemp_encode_lm77(const uint32_t val, uint8_t *buf, int degc)
565 1.28 jdc {
566 1.28 jdc int temp;
567 1.28 jdc
568 1.28 jdc /* Convert from C or uK to register format */
569 1.28 jdc if (degc)
570 1.28 jdc temp = val * 2;
571 1.28 jdc else
572 1.28 jdc temp = (val - 273150000) / 500000;
573 1.28 jdc buf[0] = (temp >> 5) & 0xff;
574 1.28 jdc buf[1] = (temp & 0x1f) << 3;
575 1.28 jdc }
576 1.28 jdc
577 1.23 macallan static void
578 1.23 macallan lmtemp_setup_sysctl(struct lmtemp_softc *sc)
579 1.23 macallan {
580 1.23 macallan const struct sysctlnode *me = NULL, *node = NULL;
581 1.23 macallan
582 1.23 macallan sysctl_createv(NULL, 0, NULL, &me,
583 1.23 macallan CTLFLAG_READWRITE,
584 1.23 macallan CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
585 1.23 macallan NULL, 0, NULL, 0,
586 1.23 macallan CTL_MACHDEP, CTL_CREATE, CTL_EOL);
587 1.23 macallan
588 1.23 macallan sysctl_createv(NULL, 0, NULL, &node,
589 1.23 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
590 1.23 macallan CTLTYPE_INT, "temp", "Threshold temperature",
591 1.24 dsl sysctl_lm75_temp, 1, (void *)sc, 0,
592 1.23 macallan CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
593 1.23 macallan }
594 1.23 macallan
595 1.23 macallan static int
596 1.23 macallan sysctl_lm75_temp(SYSCTLFN_ARGS)
597 1.23 macallan {
598 1.23 macallan struct sysctlnode node = *rnode;
599 1.23 macallan struct lmtemp_softc *sc = node.sysctl_data;
600 1.23 macallan int temp;
601 1.23 macallan
602 1.23 macallan if (newp) {
603 1.23 macallan
604 1.23 macallan /* we're asked to write */
605 1.23 macallan node.sysctl_data = &sc->sc_tmax;
606 1.23 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
607 1.23 macallan
608 1.23 macallan temp = *(int *)node.sysctl_data;
609 1.23 macallan sc->sc_tmax = temp;
610 1.23 macallan iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
611 1.23 macallan lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
612 1.28 jdc sc->sc_tmax - 5, 1);
613 1.23 macallan lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT,
614 1.28 jdc sc->sc_tmax, 1);
615 1.23 macallan iic_release_bus(sc->sc_tag, I2C_F_POLL);
616 1.28 jdc
617 1.28 jdc /* Synchronise envsys - calls lmtemp_getlim_lm75() */
618 1.28 jdc sysmon_envsys_update_limits(sc->sc_sme, &sc->sc_sensor);
619 1.23 macallan return 0;
620 1.23 macallan }
621 1.23 macallan return EINVAL;
622 1.23 macallan } else {
623 1.23 macallan
624 1.23 macallan node.sysctl_data = &sc->sc_tmax;
625 1.23 macallan node.sysctl_size = 4;
626 1.23 macallan return (sysctl_lookup(SYSCTLFN_CALL(&node)));
627 1.23 macallan }
628 1.23 macallan
629 1.23 macallan return 0;
630 1.23 macallan }
631 1.23 macallan
632 1.23 macallan SYSCTL_SETUP(sysctl_lmtemp_setup, "sysctl lmtemp subtree setup")
633 1.23 macallan {
634 1.23 macallan
635 1.23 macallan sysctl_createv(NULL, 0, NULL, NULL,
636 1.23 macallan CTLFLAG_PERMANENT,
637 1.23 macallan CTLTYPE_NODE, "machdep", NULL,
638 1.23 macallan NULL, 0, NULL, 0,
639 1.23 macallan CTL_MACHDEP, CTL_EOL);
640 1.23 macallan }
641 1.23 macallan
642 1.23 macallan
643