lm75.c revision 1.10 1 /* $NetBSD: lm75.c,v 1.10 2006/06/12 16:07:33 kiyohara Exp $ */
2
3 /*
4 * Copyright (c) 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42
43 #include <dev/sysmon/sysmonvar.h>
44
45 #include <dev/i2c/i2cvar.h>
46 #include <dev/i2c/lm75reg.h>
47
48 struct lmtemp_softc {
49 struct device sc_dev;
50 i2c_tag_t sc_tag;
51 int sc_address;
52
53 struct envsys_tre_data sc_sensor[1];
54 struct envsys_basic_info sc_info[1];
55
56 struct sysmon_envsys sc_sysmon;
57
58 uint32_t (*sc_lmtemp_decode)(const uint8_t *);
59 };
60
61 static int lmtemp_match(struct device *, struct cfdata *, void *);
62 static void lmtemp_attach(struct device *, struct device *, void *);
63
64 CFATTACH_DECL(lmtemp, sizeof(struct lmtemp_softc),
65 lmtemp_match, lmtemp_attach, NULL, NULL);
66
67 static int lmtemp_gtredata(struct sysmon_envsys *,
68 struct envsys_tre_data *);
69 static int lmtemp_streinfo(struct sysmon_envsys *,
70 struct envsys_basic_info *);
71
72 static const struct envsys_range lmtemp_ranges[] = {
73 { 0, 1, ENVSYS_STEMP },
74 { 1, 0, -1 },
75 };
76
77 static int lmtemp_config_write(struct lmtemp_softc *, uint8_t);
78 static uint32_t lmtemp_decode_lm75(const uint8_t *);
79 static uint32_t lmtemp_decode_ds75(const uint8_t *);
80 static uint32_t lmtemp_decode_lm77(const uint8_t *);
81
82 enum {
83 lmtemp_lm75 = 0,
84 lmtemp_ds75,
85 lmtemp_lm77,
86 };
87 static const struct {
88 int lmtemp_type;
89 const char *lmtemp_name;
90 int lmtemp_addrmask;
91 int lmtemp_addr;
92 uint32_t (*lmtemp_decode)(const uint8_t *);
93 } lmtemptbl[] = {
94 { lmtemp_lm75, "LM75",
95 LM75_ADDRMASK, LM75_ADDR, lmtemp_decode_lm75 },
96 { lmtemp_ds75, "DS75",
97 LM75_ADDRMASK, LM75_ADDR, lmtemp_decode_ds75 },
98 { lmtemp_lm77, "LM77",
99 LM77_ADDRMASK, LM77_ADDR, lmtemp_decode_lm77 },
100
101 { -1, NULL,
102 0, 0, NULL }
103 };
104
105 static int
106 lmtemp_match(struct device *parent, struct cfdata *cf, void *aux)
107 {
108 struct i2c_attach_args *ia = aux;
109 int i;
110
111 for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
112 if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
113 break;
114 if (lmtemptbl[i].lmtemp_type == -1)
115 return (0);
116
117 if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
118 lmtemptbl[i].lmtemp_addr)
119 return (1);
120
121 return (0);
122 }
123
124 static void
125 lmtemp_attach(struct device *parent, struct device *self, void *aux)
126 {
127 struct lmtemp_softc *sc = device_private(self);
128 struct i2c_attach_args *ia = aux;
129 prop_string_t desc;
130 int i;
131
132 for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
133 if (lmtemptbl[i].lmtemp_type ==
134 device_cfdata(&sc->sc_dev)->cf_flags)
135 break;
136
137 sc->sc_tag = ia->ia_tag;
138 sc->sc_address = ia->ia_addr;
139
140 aprint_naive(": Temperature Sensor\n");
141 aprint_normal(": %s Temperature Sensor\n", lmtemptbl[i].lmtemp_name);
142
143 /* Set the configuration of the LM75 to defaults. */
144 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
145 if (lmtemp_config_write(sc, 0) != 0) {
146 aprint_error("%s: unable to write config register\n",
147 sc->sc_dev.dv_xname);
148 iic_release_bus(sc->sc_tag, I2C_F_POLL);
149 return;
150 }
151 iic_release_bus(sc->sc_tag, I2C_F_POLL);
152
153 /* Initialize sensor data. */
154 sc->sc_sensor[0].sensor = sc->sc_info[0].sensor = 0;
155 sc->sc_sensor[0].validflags = ENVSYS_FVALID;
156 sc->sc_info[0].validflags = ENVSYS_FVALID;
157 sc->sc_sensor[0].warnflags = ENVSYS_WARN_OK;
158
159 sc->sc_sensor[0].units = sc->sc_info[0].units = ENVSYS_STEMP;
160 desc = prop_dictionary_get(device_properties(&sc->sc_dev),
161 "description");
162 if (desc != NULL &&
163 prop_object_type(desc) == PROP_TYPE_STRING &&
164 prop_string_size(desc) > 0)
165 strcpy(sc->sc_info[0].desc, prop_string_cstring_nocopy(desc));
166 else
167 strcpy(sc->sc_info[0].desc, sc->sc_dev.dv_xname);
168
169 sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
170
171 /* Hook into system monitor. */
172 sc->sc_sysmon.sme_ranges = lmtemp_ranges;
173 sc->sc_sysmon.sme_sensor_info = sc->sc_info;
174 sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
175 sc->sc_sysmon.sme_cookie = sc;
176
177 sc->sc_sysmon.sme_gtredata = lmtemp_gtredata;
178 sc->sc_sysmon.sme_streinfo = lmtemp_streinfo;
179
180 sc->sc_sysmon.sme_nsensors = 1;
181 sc->sc_sysmon.sme_envsys_version = 1000;
182
183 if (sysmon_envsys_register(&sc->sc_sysmon))
184 aprint_error("%s: unable to register with sysmon\n",
185 sc->sc_dev.dv_xname);
186 }
187
188 static int
189 lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
190 {
191 uint8_t cmdbuf[2];
192
193 cmdbuf[0] = LM75_REG_CONFIG;
194 cmdbuf[1] = val;
195
196 return (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
197 sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL));
198 }
199
200 static int
201 lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp)
202 {
203 int error;
204 uint8_t cmdbuf[1];
205 uint8_t buf[LM75_TEMP_LEN];
206
207 cmdbuf[0] = which;
208
209 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
210 sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
211 if (error)
212 return (error);
213
214 *valp = sc->sc_lmtemp_decode(buf);
215 return (0);
216 }
217
218 static void
219 lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
220 {
221 uint32_t val;
222 int error;
223
224 error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val);
225 if (error) {
226 #if 0
227 printf("%s: unable to read temperature, error = %d\n",
228 sc->sc_dev.dv_xname, error);
229 #endif
230 sc->sc_sensor[0].validflags &= ~ENVSYS_FCURVALID;
231 return;
232 }
233
234 sc->sc_sensor[0].cur.data_us = val;
235 sc->sc_sensor[0].validflags |= ENVSYS_FCURVALID;
236 }
237
238 static int
239 lmtemp_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
240 {
241 struct lmtemp_softc *sc = sme->sme_cookie;
242
243 iic_acquire_bus(sc->sc_tag, 0); /* also locks our instance */
244
245 lmtemp_refresh_sensor_data(sc);
246 *tred = sc->sc_sensor[tred->sensor];
247
248 iic_release_bus(sc->sc_tag, 0); /* also unlocks our instance */
249
250 return (0);
251 }
252
253 static int
254 lmtemp_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
255 {
256 struct lmtemp_softc *sc = sme->sme_cookie;
257
258 iic_acquire_bus(sc->sc_tag, 0); /* also locks our instance */
259
260 memcpy(sc->sc_info[binfo->sensor].desc, binfo->desc,
261 sizeof(sc->sc_info[binfo->sensor].desc));
262 sc->sc_info[binfo->sensor].desc[
263 sizeof(sc->sc_info[binfo->sensor].desc) - 1] = '\0';
264
265 iic_release_bus(sc->sc_tag, 0); /* also unlocks our instance */
266
267 binfo->validflags = ENVSYS_FVALID;
268
269 return (0);
270 }
271
272 static uint32_t
273 lmtemp_decode_lm75(const uint8_t *buf)
274 {
275 int neg, temp;
276 uint32_t val;
277
278 if (buf[0] & 1) {
279 /* Below 0C */
280 temp = ~buf[1] + 1;
281 neg = 1;
282 } else {
283 temp = buf[1];
284 neg = 0;
285 }
286
287 /* Temp is given in 1/2 deg. C, we convert to uK. */
288 val = ((neg ? -temp : temp) / 2) * 1000000 + 273150000;
289 if (temp & 1) {
290 if (neg)
291 val -= 500000;
292 else
293 val += 500000;
294 }
295
296 return (val);
297 }
298
299 static uint32_t
300 lmtemp_decode_ds75(const uint8_t *buf)
301 {
302 int temp;
303
304 /*
305 * Sign-extend the MSB byte, and add in the fractions of a
306 * degree contained in the LSB (precision 1/16th DegC).
307 */
308 temp = (int8_t)buf[0];
309 temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
310
311 /*
312 * Conversion to uK is simple.
313 */
314 return (temp * 62500 + 273150000);
315 }
316
317 static uint32_t
318 lmtemp_decode_lm77(const uint8_t *buf)
319 {
320 int temp;
321 uint32_t val;
322
323 /*
324 * Describe each bits of temperature registers on LM77.
325 * D15 - D12: Sign
326 * D11 - D3 : Bit8(MSB) - Bit0
327 */
328 temp = (int8_t)buf[0];
329 temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
330
331 /* Temp is given in 1/2 deg. C, we convert to uK. */
332 val = temp * 500000 + 273150000;
333
334 return (val);
335 }
336
337