owtemp.c revision 1.16 1 /* $NetBSD: owtemp.c,v 1.16 2011/06/20 17:24:16 pgoyette Exp $ */
2 /* $OpenBSD: owtemp.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3
4 /*
5 * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * 1-Wire temperature family type device driver.
22 */
23
24 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.16 2011/06/20 17:24:16 pgoyette Exp $");
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/device.h>
30 #include <sys/kernel.h>
31 #include <sys/proc.h>
32
33 #include <dev/sysmon/sysmonvar.h>
34
35 #include <dev/onewire/onewiredevs.h>
36 #include <dev/onewire/onewirereg.h>
37 #include <dev/onewire/onewirevar.h>
38
39 #define DS_CMD_CONVERT 0x44
40 #define DS_CMD_READ_SCRATCHPAD 0xbe
41
42 struct owtemp_softc {
43 void * sc_onewire;
44 u_int64_t sc_rom;
45
46 envsys_data_t sc_sensor;
47 struct sysmon_envsys *sc_sme;
48
49 uint32_t (*sc_owtemp_decode)(const uint8_t *);
50
51 int sc_dying;
52 };
53
54 static int owtemp_match(device_t, cfdata_t, void *);
55 static void owtemp_attach(device_t, device_t, void *);
56 static int owtemp_detach(device_t, int);
57 static int owtemp_activate(device_t, enum devact);
58
59 static void owtemp_update(void *);
60
61 CFATTACH_DECL_NEW(owtemp, sizeof(struct owtemp_softc),
62 owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
63
64 extern struct cfdriver owtemp_cd;
65
66 static const struct onewire_matchfam owtemp_fams[] = {
67 { ONEWIRE_FAMILY_DS1920 },
68 { ONEWIRE_FAMILY_DS18B20 },
69 { ONEWIRE_FAMILY_DS1822 },
70 };
71
72 static void owtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
73
74 static uint32_t owtemp_decode_ds18b20(const uint8_t *);
75 static uint32_t owtemp_decode_ds1920(const uint8_t *);
76
77 static int
78 owtemp_match(device_t parent, cfdata_t match, void *aux)
79 {
80 return (onewire_matchbyfam(aux, owtemp_fams,
81 __arraycount(owtemp_fams)));
82 }
83
84 static void
85 owtemp_attach(device_t parent, device_t self, void *aux)
86 {
87 struct owtemp_softc *sc = device_private(self);
88 struct onewire_attach_args *oa = aux;
89
90 aprint_naive("\n");
91
92 sc->sc_onewire = oa->oa_onewire;
93 sc->sc_rom = oa->oa_rom;
94
95 switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
96 case ONEWIRE_FAMILY_DS18B20:
97 case ONEWIRE_FAMILY_DS1822:
98 sc->sc_owtemp_decode = owtemp_decode_ds18b20;
99 break;
100 case ONEWIRE_FAMILY_DS1920:
101 sc->sc_owtemp_decode = owtemp_decode_ds1920;
102 break;
103 }
104
105 sc->sc_sme = sysmon_envsys_create();
106
107 /* Initialize sensor */
108 sc->sc_sensor.units = ENVSYS_STEMP;
109 sc->sc_sensor.state = ENVSYS_SINVALID;
110 (void)strlcpy(sc->sc_sensor.desc,
111 device_xname(self), sizeof(sc->sc_sensor.desc));
112 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
113 sysmon_envsys_destroy(sc->sc_sme);
114 return;
115 }
116
117 /* Hook into system monitor. */
118 sc->sc_sme->sme_name = device_xname(self);
119 sc->sc_sme->sme_cookie = sc;
120 sc->sc_sme->sme_refresh = owtemp_refresh;
121
122 if (sysmon_envsys_register(sc->sc_sme)) {
123 aprint_error_dev(self, "unable to register with sysmon\n");
124 sysmon_envsys_destroy(sc->sc_sme);
125 return;
126 }
127
128 aprint_normal("\n");
129 }
130
131 static int
132 owtemp_detach(device_t self, int flags)
133 {
134 struct owtemp_softc *sc = device_private(self);
135
136 sysmon_envsys_unregister(sc->sc_sme);
137
138 return 0;
139 }
140
141 static int
142 owtemp_activate(device_t self, enum devact act)
143 {
144 struct owtemp_softc *sc = device_private(self);
145
146 switch (act) {
147 case DVACT_DEACTIVATE:
148 sc->sc_dying = 1;
149 return 0;
150 default:
151 return EOPNOTSUPP;
152 }
153 }
154
155 static void
156 owtemp_update(void *arg)
157 {
158 struct owtemp_softc *sc = arg;
159 u_int8_t data[9];
160
161 onewire_lock(sc->sc_onewire);
162 if (onewire_reset(sc->sc_onewire) != 0)
163 goto done;
164 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
165
166 /*
167 * Start temperature conversion. The conversion takes up to 750ms.
168 * After sending the command, the data line must be held high for
169 * at least 750ms to provide power during the conversion process.
170 * As such, no other activity may take place on the 1-Wire bus for
171 * at least this period.
172 */
173 onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
174 tsleep(sc, PRIBIO, "owtemp", hz);
175
176 if (onewire_reset(sc->sc_onewire) != 0)
177 goto done;
178 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
179
180 /*
181 * The result of the temperature measurement is placed in the
182 * first two bytes of the scratchpad.
183 */
184 onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
185 onewire_read_block(sc->sc_onewire, data, 9);
186 #if 0
187 if (onewire_crc(data, 8) == data[8]) {
188 sc->sc_sensor.value = 273150000 +
189 (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
190 }
191 #endif
192
193 sc->sc_sensor.value_cur = sc->sc_owtemp_decode(data);
194 sc->sc_sensor.state = ENVSYS_SVALID;
195
196 done:
197 onewire_unlock(sc->sc_onewire);
198 }
199
200 static void
201 owtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
202 {
203 struct owtemp_softc *sc = sme->sme_cookie;
204
205 owtemp_update(sc);
206 }
207
208 static uint32_t
209 owtemp_decode_ds18b20(const uint8_t *buf)
210 {
211 int temp;
212
213 /*
214 * Sign-extend the MSB byte, and add in the fractions of a
215 * degree contained in the LSB (precision 1/16th DegC).
216 */
217 temp = (int8_t)buf[1];
218 temp = (temp << 8) | buf[0];
219
220 /*
221 * Conversion to uK is simple.
222 */
223 return (temp * 62500 + 273150000);
224 }
225
226 static uint32_t
227 owtemp_decode_ds1920(const uint8_t *buf)
228 {
229 int temp;
230
231 /*
232 * Sign-extend the MSB byte, and add in the fractions of a
233 * degree contained in the LSB (precision 1/2 DegC).
234 */
235 temp = (int8_t)buf[1];
236 temp = (temp << 8) | buf[0];
237
238 /* Convert to uK */
239 return (temp * 500000 + 273150000);
240 }
241