lm75.c revision 1.27 1 1.27 jdc /* $NetBSD: lm75.c,v 1.27 2016/01/01 20:13:50 jdc 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.27 jdc __KERNEL_RCSID(0, "$NetBSD: lm75.c,v 1.27 2016/01/01 20:13:50 jdc 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.1 thorpej
57 1.18 xtraeme struct sysmon_envsys *sc_sme;
58 1.16 xtraeme envsys_data_t sc_sensor;
59 1.23 macallan int sc_tmax;
60 1.9 kiyohara
61 1.27 jdc uint32_t (*sc_lmtemp_decode)(const uint8_t *, int);
62 1.1 thorpej };
63 1.1 thorpej
64 1.18 xtraeme static int lmtemp_match(device_t, cfdata_t, void *);
65 1.18 xtraeme static void lmtemp_attach(device_t, device_t, void *);
66 1.1 thorpej
67 1.18 xtraeme CFATTACH_DECL_NEW(lmtemp, sizeof(struct lmtemp_softc),
68 1.1 thorpej lmtemp_match, lmtemp_attach, NULL, NULL);
69 1.1 thorpej
70 1.16 xtraeme static void lmtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
71 1.1 thorpej
72 1.18 xtraeme static int lmtemp_config_write(struct lmtemp_softc *, uint8_t);
73 1.23 macallan static int lmtemp_temp_write(struct lmtemp_softc *, int, uint16_t);
74 1.27 jdc static int lmtemp_temp_read(struct lmtemp_softc *, uint8_t, uint32_t *,
75 1.27 jdc int);
76 1.27 jdc static uint32_t lmtemp_decode_lm75(const uint8_t *, int);
77 1.27 jdc static uint32_t lmtemp_decode_ds75(const uint8_t *, int);
78 1.27 jdc static uint32_t lmtemp_decode_lm77(const uint8_t *, int);
79 1.9 kiyohara
80 1.23 macallan static void lmtemp_setup_sysctl(struct lmtemp_softc *);
81 1.23 macallan static int sysctl_lm75_temp(SYSCTLFN_ARGS);
82 1.21 martin
83 1.21 martin static const char * lmtemp_compats[] = {
84 1.21 martin "i2c-lm75",
85 1.21 martin /*
86 1.21 martin * see XXX in _attach() below: add code once non-lm75 matches are
87 1.21 martin * added here!
88 1.21 martin */
89 1.21 martin NULL
90 1.21 martin };
91 1.21 martin
92 1.9 kiyohara enum {
93 1.9 kiyohara lmtemp_lm75 = 0,
94 1.9 kiyohara lmtemp_ds75,
95 1.9 kiyohara lmtemp_lm77,
96 1.9 kiyohara };
97 1.9 kiyohara static const struct {
98 1.9 kiyohara int lmtemp_type;
99 1.9 kiyohara const char *lmtemp_name;
100 1.9 kiyohara int lmtemp_addrmask;
101 1.9 kiyohara int lmtemp_addr;
102 1.27 jdc uint32_t (*lmtemp_decode)(const uint8_t *, int);
103 1.9 kiyohara } lmtemptbl[] = {
104 1.9 kiyohara { lmtemp_lm75, "LM75",
105 1.9 kiyohara LM75_ADDRMASK, LM75_ADDR, lmtemp_decode_lm75 },
106 1.9 kiyohara { lmtemp_ds75, "DS75",
107 1.9 kiyohara LM75_ADDRMASK, LM75_ADDR, lmtemp_decode_ds75 },
108 1.9 kiyohara { lmtemp_lm77, "LM77",
109 1.9 kiyohara LM77_ADDRMASK, LM77_ADDR, lmtemp_decode_lm77 },
110 1.9 kiyohara
111 1.9 kiyohara { -1, NULL,
112 1.9 kiyohara 0, 0, NULL }
113 1.9 kiyohara };
114 1.1 thorpej
115 1.1 thorpej static int
116 1.18 xtraeme lmtemp_match(device_t parent, cfdata_t cf, void *aux)
117 1.1 thorpej {
118 1.1 thorpej struct i2c_attach_args *ia = aux;
119 1.9 kiyohara int i;
120 1.9 kiyohara
121 1.21 martin if (ia->ia_name == NULL) {
122 1.21 martin /*
123 1.21 martin * Indirect config - not much we can do!
124 1.21 martin */
125 1.21 martin for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
126 1.21 martin if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
127 1.21 martin break;
128 1.21 martin if (lmtemptbl[i].lmtemp_type == -1)
129 1.21 martin return 0;
130 1.21 martin
131 1.21 martin if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
132 1.21 martin lmtemptbl[i].lmtemp_addr)
133 1.21 martin return 1;
134 1.21 martin } else {
135 1.21 martin /*
136 1.21 martin * Direct config - match via the list of compatible
137 1.26 phx * hardware or simply match the device name.
138 1.21 martin */
139 1.26 phx if (ia->ia_ncompat > 0) {
140 1.26 phx if (iic_compat_match(ia, lmtemp_compats))
141 1.26 phx return 1;
142 1.26 phx } else {
143 1.26 phx if (strcmp(ia->ia_name, "lmtemp") == 0)
144 1.26 phx return 1;
145 1.26 phx }
146 1.21 martin }
147 1.21 martin
148 1.1 thorpej
149 1.18 xtraeme return 0;
150 1.1 thorpej }
151 1.1 thorpej
152 1.1 thorpej static void
153 1.18 xtraeme lmtemp_attach(device_t parent, device_t self, void *aux)
154 1.1 thorpej {
155 1.6 thorpej struct lmtemp_softc *sc = device_private(self);
156 1.1 thorpej struct i2c_attach_args *ia = aux;
157 1.9 kiyohara int i;
158 1.9 kiyohara
159 1.23 macallan sc->sc_dev = self;
160 1.21 martin if (ia->ia_name == NULL) {
161 1.21 martin for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
162 1.21 martin if (lmtemptbl[i].lmtemp_type ==
163 1.21 martin device_cfdata(self)->cf_flags)
164 1.21 martin break;
165 1.21 martin } else {
166 1.21 martin /* XXX - add code when adding other direct matches! */
167 1.21 martin i = 0;
168 1.21 martin }
169 1.1 thorpej
170 1.1 thorpej sc->sc_tag = ia->ia_tag;
171 1.1 thorpej sc->sc_address = ia->ia_addr;
172 1.1 thorpej
173 1.1 thorpej aprint_naive(": Temperature Sensor\n");
174 1.21 martin if (ia->ia_name) {
175 1.21 martin aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
176 1.21 martin lmtemptbl[i].lmtemp_name);
177 1.21 martin } else {
178 1.21 martin aprint_normal(": %s Temperature Sensor\n",
179 1.21 martin lmtemptbl[i].lmtemp_name);
180 1.21 martin }
181 1.1 thorpej
182 1.27 jdc sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
183 1.27 jdc
184 1.27 jdc iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
185 1.27 jdc
186 1.27 jdc /* Read temperature limit and remember initial value. */
187 1.27 jdc if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &sc->sc_tmax, 1)
188 1.27 jdc != 0) {
189 1.27 jdc iic_release_bus(sc->sc_tag, I2C_F_POLL);
190 1.27 jdc return;
191 1.27 jdc }
192 1.27 jdc
193 1.23 macallan if (i == lmtemp_lm75)
194 1.23 macallan lmtemp_setup_sysctl(sc);
195 1.23 macallan
196 1.1 thorpej /* Set the configuration of the LM75 to defaults. */
197 1.23 macallan if (lmtemp_config_write(sc, LM75_CONFIG_FAULT_QUEUE_4) != 0) {
198 1.18 xtraeme aprint_error_dev(self, "unable to write config register\n");
199 1.1 thorpej iic_release_bus(sc->sc_tag, I2C_F_POLL);
200 1.1 thorpej return;
201 1.1 thorpej }
202 1.1 thorpej iic_release_bus(sc->sc_tag, I2C_F_POLL);
203 1.1 thorpej
204 1.16 xtraeme sc->sc_sme = sysmon_envsys_create();
205 1.1 thorpej /* Initialize sensor data. */
206 1.16 xtraeme sc->sc_sensor.units = ENVSYS_STEMP;
207 1.22 pgoyette sc->sc_sensor.state = ENVSYS_SINVALID;
208 1.21 martin (void)strlcpy(sc->sc_sensor.desc,
209 1.21 martin ia->ia_name? ia->ia_name : device_xname(self),
210 1.18 xtraeme sizeof(sc->sc_sensor.desc));
211 1.16 xtraeme if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
212 1.16 xtraeme sysmon_envsys_destroy(sc->sc_sme);
213 1.16 xtraeme return;
214 1.16 xtraeme }
215 1.1 thorpej
216 1.7 riz /* Hook into system monitor. */
217 1.18 xtraeme sc->sc_sme->sme_name = device_xname(self);
218 1.16 xtraeme sc->sc_sme->sme_cookie = sc;
219 1.16 xtraeme sc->sc_sme->sme_refresh = lmtemp_refresh;
220 1.1 thorpej
221 1.16 xtraeme if (sysmon_envsys_register(sc->sc_sme)) {
222 1.18 xtraeme aprint_error_dev(self, "unable to register with sysmon\n");
223 1.16 xtraeme sysmon_envsys_destroy(sc->sc_sme);
224 1.16 xtraeme }
225 1.1 thorpej }
226 1.1 thorpej
227 1.1 thorpej static int
228 1.1 thorpej lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
229 1.1 thorpej {
230 1.1 thorpej uint8_t cmdbuf[2];
231 1.1 thorpej
232 1.1 thorpej cmdbuf[0] = LM75_REG_CONFIG;
233 1.1 thorpej cmdbuf[1] = val;
234 1.1 thorpej
235 1.18 xtraeme return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
236 1.18 xtraeme sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL);
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej static int
240 1.23 macallan lmtemp_temp_write(struct lmtemp_softc *sc, int reg, uint16_t val)
241 1.23 macallan {
242 1.23 macallan uint8_t cmdbuf[3];
243 1.23 macallan
244 1.23 macallan cmdbuf[0] = reg;
245 1.23 macallan cmdbuf[1] = (val >> 1) & 0xff;
246 1.23 macallan cmdbuf[2] = (val & 1) << 7;
247 1.23 macallan
248 1.23 macallan return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
249 1.23 macallan sc->sc_address, cmdbuf, 1, &cmdbuf[1], 2, I2C_F_POLL);
250 1.23 macallan }
251 1.23 macallan
252 1.23 macallan static int
253 1.27 jdc lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp,
254 1.27 jdc int degc)
255 1.1 thorpej {
256 1.2 scw int error;
257 1.1 thorpej uint8_t cmdbuf[1];
258 1.1 thorpej uint8_t buf[LM75_TEMP_LEN];
259 1.1 thorpej
260 1.1 thorpej cmdbuf[0] = which;
261 1.1 thorpej
262 1.1 thorpej error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
263 1.1 thorpej sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
264 1.1 thorpej if (error)
265 1.18 xtraeme return error;
266 1.1 thorpej
267 1.27 jdc *valp = sc->sc_lmtemp_decode(buf, degc);
268 1.18 xtraeme return 0;
269 1.1 thorpej }
270 1.1 thorpej
271 1.1 thorpej static void
272 1.1 thorpej lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
273 1.1 thorpej {
274 1.1 thorpej uint32_t val;
275 1.1 thorpej int error;
276 1.1 thorpej
277 1.27 jdc error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val, 0);
278 1.1 thorpej if (error) {
279 1.1 thorpej #if 0
280 1.25 chs aprint_error_dev(sc->sc_dev, "unable to read temperature, error = %d\n",
281 1.19 cegger error);
282 1.1 thorpej #endif
283 1.16 xtraeme sc->sc_sensor.state = ENVSYS_SINVALID;
284 1.1 thorpej return;
285 1.1 thorpej }
286 1.1 thorpej
287 1.16 xtraeme sc->sc_sensor.value_cur = val;
288 1.16 xtraeme sc->sc_sensor.state = ENVSYS_SVALID;
289 1.1 thorpej }
290 1.1 thorpej
291 1.16 xtraeme static void
292 1.16 xtraeme lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
293 1.1 thorpej {
294 1.1 thorpej struct lmtemp_softc *sc = sme->sme_cookie;
295 1.1 thorpej
296 1.1 thorpej iic_acquire_bus(sc->sc_tag, 0); /* also locks our instance */
297 1.1 thorpej lmtemp_refresh_sensor_data(sc);
298 1.1 thorpej iic_release_bus(sc->sc_tag, 0); /* also unlocks our instance */
299 1.1 thorpej }
300 1.2 scw
301 1.2 scw static uint32_t
302 1.27 jdc lmtemp_decode_lm75(const uint8_t *buf, int degc)
303 1.2 scw {
304 1.20 briggs int temp;
305 1.2 scw uint32_t val;
306 1.2 scw
307 1.20 briggs /*
308 1.20 briggs * LM75 temps are the most-significant 9 bits of a 16-bit reg.
309 1.20 briggs * sign-extend the MSB and add in the 0.5 from the LSB
310 1.20 briggs */
311 1.20 briggs temp = (int8_t) buf[0];
312 1.20 briggs temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
313 1.2 scw
314 1.27 jdc /* Temp is given in 1/2 deg. C, we convert to C or uK. */
315 1.27 jdc if (degc)
316 1.27 jdc val = temp / 2;
317 1.27 jdc else
318 1.27 jdc val = temp * 500000 + 273150000;
319 1.2 scw
320 1.18 xtraeme return val;
321 1.2 scw }
322 1.2 scw
323 1.2 scw static uint32_t
324 1.27 jdc lmtemp_decode_ds75(const uint8_t *buf, int degc)
325 1.2 scw {
326 1.2 scw int temp;
327 1.2 scw
328 1.2 scw /*
329 1.2 scw * Sign-extend the MSB byte, and add in the fractions of a
330 1.7 riz * degree contained in the LSB (precision 1/16th DegC).
331 1.2 scw */
332 1.2 scw temp = (int8_t)buf[0];
333 1.2 scw temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
334 1.2 scw
335 1.2 scw /*
336 1.27 jdc * Conversion to C or uK is simple.
337 1.2 scw */
338 1.27 jdc if (degc)
339 1.27 jdc return temp / 16;
340 1.27 jdc else
341 1.27 jdc return (temp * 62500 + 273150000);
342 1.2 scw }
343 1.2 scw
344 1.9 kiyohara static uint32_t
345 1.27 jdc lmtemp_decode_lm77(const uint8_t *buf, int degc)
346 1.9 kiyohara {
347 1.9 kiyohara int temp;
348 1.9 kiyohara uint32_t val;
349 1.9 kiyohara
350 1.9 kiyohara /*
351 1.9 kiyohara * Describe each bits of temperature registers on LM77.
352 1.9 kiyohara * D15 - D12: Sign
353 1.9 kiyohara * D11 - D3 : Bit8(MSB) - Bit0
354 1.9 kiyohara */
355 1.9 kiyohara temp = (int8_t)buf[0];
356 1.10 kiyohara temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
357 1.9 kiyohara
358 1.27 jdc /* Temp is given in 1/2 deg. C, we convert to C or uK. */
359 1.27 jdc if (degc)
360 1.27 jdc val = temp / 2;
361 1.27 jdc else
362 1.27 jdc val = temp * 500000 + 273150000;
363 1.9 kiyohara
364 1.18 xtraeme return val;
365 1.9 kiyohara }
366 1.9 kiyohara
367 1.23 macallan static void
368 1.23 macallan lmtemp_setup_sysctl(struct lmtemp_softc *sc)
369 1.23 macallan {
370 1.23 macallan const struct sysctlnode *me = NULL, *node = NULL;
371 1.23 macallan
372 1.23 macallan sysctl_createv(NULL, 0, NULL, &me,
373 1.23 macallan CTLFLAG_READWRITE,
374 1.23 macallan CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
375 1.23 macallan NULL, 0, NULL, 0,
376 1.23 macallan CTL_MACHDEP, CTL_CREATE, CTL_EOL);
377 1.23 macallan
378 1.23 macallan sysctl_createv(NULL, 0, NULL, &node,
379 1.23 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
380 1.23 macallan CTLTYPE_INT, "temp", "Threshold temperature",
381 1.24 dsl sysctl_lm75_temp, 1, (void *)sc, 0,
382 1.23 macallan CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
383 1.23 macallan }
384 1.23 macallan
385 1.23 macallan static int
386 1.23 macallan sysctl_lm75_temp(SYSCTLFN_ARGS)
387 1.23 macallan {
388 1.23 macallan struct sysctlnode node = *rnode;
389 1.23 macallan struct lmtemp_softc *sc = node.sysctl_data;
390 1.23 macallan int temp;
391 1.23 macallan
392 1.23 macallan if (newp) {
393 1.23 macallan
394 1.23 macallan /* we're asked to write */
395 1.23 macallan node.sysctl_data = &sc->sc_tmax;
396 1.23 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
397 1.23 macallan
398 1.23 macallan temp = *(int *)node.sysctl_data;
399 1.23 macallan sc->sc_tmax = temp;
400 1.23 macallan iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
401 1.23 macallan lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
402 1.23 macallan (sc->sc_tmax - 5) * 2);
403 1.23 macallan lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT,
404 1.23 macallan sc->sc_tmax * 2);
405 1.23 macallan iic_release_bus(sc->sc_tag, I2C_F_POLL);
406 1.23 macallan return 0;
407 1.23 macallan }
408 1.23 macallan return EINVAL;
409 1.23 macallan } else {
410 1.23 macallan
411 1.23 macallan node.sysctl_data = &sc->sc_tmax;
412 1.23 macallan node.sysctl_size = 4;
413 1.23 macallan return (sysctl_lookup(SYSCTLFN_CALL(&node)));
414 1.23 macallan }
415 1.23 macallan
416 1.23 macallan return 0;
417 1.23 macallan }
418 1.23 macallan
419 1.23 macallan SYSCTL_SETUP(sysctl_lmtemp_setup, "sysctl lmtemp subtree setup")
420 1.23 macallan {
421 1.23 macallan
422 1.23 macallan sysctl_createv(NULL, 0, NULL, NULL,
423 1.23 macallan CTLFLAG_PERMANENT,
424 1.23 macallan CTLTYPE_NODE, "machdep", NULL,
425 1.23 macallan NULL, 0, NULL, 0,
426 1.23 macallan CTL_MACHDEP, CTL_EOL);
427 1.23 macallan }
428 1.23 macallan
429 1.23 macallan
430