owtemp.c revision 1.3.8.3 1 1.3.8.3 yamt /* $NetBSD: owtemp.c,v 1.3.8.3 2006/12/30 20:48:38 yamt Exp $ */
2 1.3.8.2 yamt /* $OpenBSD: owtemp.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3 1.3.8.2 yamt
4 1.3.8.2 yamt /*
5 1.3.8.2 yamt * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 1.3.8.2 yamt *
7 1.3.8.2 yamt * Permission to use, copy, modify, and distribute this software for any
8 1.3.8.2 yamt * purpose with or without fee is hereby granted, provided that the above
9 1.3.8.2 yamt * copyright notice and this permission notice appear in all copies.
10 1.3.8.2 yamt *
11 1.3.8.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.3.8.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.3.8.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.3.8.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.3.8.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.3.8.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.3.8.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.3.8.2 yamt */
19 1.3.8.2 yamt
20 1.3.8.2 yamt /*
21 1.3.8.2 yamt * 1-Wire temperature family type device driver.
22 1.3.8.2 yamt */
23 1.3.8.2 yamt
24 1.3.8.2 yamt #include <sys/cdefs.h>
25 1.3.8.3 yamt __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.3.8.3 2006/12/30 20:48:38 yamt Exp $");
26 1.3.8.2 yamt
27 1.3.8.2 yamt #include <sys/param.h>
28 1.3.8.2 yamt #include <sys/systm.h>
29 1.3.8.2 yamt #include <sys/device.h>
30 1.3.8.2 yamt #include <sys/kernel.h>
31 1.3.8.2 yamt #include <sys/proc.h>
32 1.3.8.2 yamt
33 1.3.8.2 yamt #include <dev/sysmon/sysmonvar.h>
34 1.3.8.2 yamt
35 1.3.8.2 yamt #include <dev/onewire/onewiredevs.h>
36 1.3.8.2 yamt #include <dev/onewire/onewirereg.h>
37 1.3.8.2 yamt #include <dev/onewire/onewirevar.h>
38 1.3.8.2 yamt
39 1.3.8.2 yamt #define DS_CMD_CONVERT 0x44
40 1.3.8.2 yamt #define DS_CMD_READ_SCRATCHPAD 0xbe
41 1.3.8.2 yamt
42 1.3.8.2 yamt struct owtemp_softc {
43 1.3.8.2 yamt struct device sc_dev;
44 1.3.8.2 yamt
45 1.3.8.2 yamt void * sc_onewire;
46 1.3.8.2 yamt u_int64_t sc_rom;
47 1.3.8.2 yamt
48 1.3.8.2 yamt struct envsys_tre_data sc_sensor[1];
49 1.3.8.2 yamt struct envsys_basic_info sc_info[1];
50 1.3.8.2 yamt
51 1.3.8.2 yamt struct sysmon_envsys sc_sysmon;
52 1.3.8.2 yamt
53 1.3.8.2 yamt uint32_t (*sc_owtemp_decode)(const uint8_t *);
54 1.3.8.2 yamt
55 1.3.8.2 yamt int sc_dying;
56 1.3.8.2 yamt };
57 1.3.8.2 yamt
58 1.3.8.2 yamt int owtemp_match(struct device *, struct cfdata *, void *);
59 1.3.8.2 yamt void owtemp_attach(struct device *, struct device *, void *);
60 1.3.8.2 yamt int owtemp_detach(struct device *, int);
61 1.3.8.2 yamt int owtemp_activate(struct device *, enum devact);
62 1.3.8.2 yamt
63 1.3.8.2 yamt void owtemp_update(void *);
64 1.3.8.2 yamt
65 1.3.8.2 yamt CFATTACH_DECL(owtemp, sizeof(struct owtemp_softc),
66 1.3.8.2 yamt owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
67 1.3.8.2 yamt
68 1.3.8.2 yamt extern struct cfdriver owtemp_cd;
69 1.3.8.2 yamt
70 1.3.8.2 yamt static const struct onewire_matchfam owtemp_fams[] = {
71 1.3.8.2 yamt { ONEWIRE_FAMILY_DS1920 },
72 1.3.8.2 yamt { ONEWIRE_FAMILY_DS18B20 },
73 1.3.8.2 yamt { ONEWIRE_FAMILY_DS1822 },
74 1.3.8.2 yamt };
75 1.3.8.2 yamt
76 1.3.8.2 yamt static const struct envsys_range owtemp_ranges[] = {
77 1.3.8.2 yamt { 0, 1, ENVSYS_STEMP },
78 1.3.8.2 yamt { 1, 0, -1 },
79 1.3.8.2 yamt };
80 1.3.8.2 yamt
81 1.3.8.2 yamt static int owtemp_gtredata(struct sysmon_envsys *,
82 1.3.8.2 yamt struct envsys_tre_data *);
83 1.3.8.2 yamt static int owtemp_streinfo(struct sysmon_envsys *,
84 1.3.8.2 yamt struct envsys_basic_info *);
85 1.3.8.2 yamt
86 1.3.8.2 yamt static uint32_t owtemp_decode_ds18b20(const uint8_t *);
87 1.3.8.2 yamt static uint32_t owtemp_decode_ds1920(const uint8_t *);
88 1.3.8.2 yamt
89 1.3.8.2 yamt int
90 1.3.8.3 yamt owtemp_match(struct device *parent, struct cfdata *cf,
91 1.3.8.3 yamt void *aux)
92 1.3.8.2 yamt {
93 1.3.8.2 yamt return (onewire_matchbyfam(aux, owtemp_fams,
94 1.3.8.2 yamt sizeof(owtemp_fams) /sizeof(owtemp_fams[0])));
95 1.3.8.2 yamt }
96 1.3.8.2 yamt
97 1.3.8.2 yamt void
98 1.3.8.2 yamt owtemp_attach(struct device *parent, struct device *self, void *aux)
99 1.3.8.2 yamt {
100 1.3.8.2 yamt struct owtemp_softc *sc = device_private(self);
101 1.3.8.2 yamt struct onewire_attach_args *oa = aux;
102 1.3.8.2 yamt prop_string_t desc;
103 1.3.8.2 yamt
104 1.3.8.2 yamt sc->sc_onewire = oa->oa_onewire;
105 1.3.8.2 yamt sc->sc_rom = oa->oa_rom;
106 1.3.8.2 yamt
107 1.3.8.2 yamt switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
108 1.3.8.2 yamt case ONEWIRE_FAMILY_DS18B20:
109 1.3.8.2 yamt case ONEWIRE_FAMILY_DS1822:
110 1.3.8.2 yamt sc->sc_owtemp_decode = owtemp_decode_ds18b20;
111 1.3.8.2 yamt break;
112 1.3.8.2 yamt case ONEWIRE_FAMILY_DS1920:
113 1.3.8.2 yamt sc->sc_owtemp_decode = owtemp_decode_ds1920;
114 1.3.8.2 yamt break;
115 1.3.8.2 yamt }
116 1.3.8.2 yamt
117 1.3.8.2 yamt /* Initialize sensor */
118 1.3.8.2 yamt sc->sc_sensor[0].sensor = sc->sc_info[0].sensor = 0;
119 1.3.8.2 yamt sc->sc_sensor[0].validflags = ENVSYS_FVALID;
120 1.3.8.2 yamt sc->sc_info[0].validflags = ENVSYS_FVALID;
121 1.3.8.2 yamt sc->sc_sensor[0].warnflags = ENVSYS_WARN_OK;
122 1.3.8.2 yamt
123 1.3.8.2 yamt sc->sc_sensor[0].units = sc->sc_info[0].units = ENVSYS_STEMP;
124 1.3.8.2 yamt desc = prop_dictionary_get(device_properties(&sc->sc_dev),
125 1.3.8.3 yamt "envsys-description");
126 1.3.8.2 yamt if (desc != NULL &&
127 1.3.8.2 yamt prop_object_type(desc) == PROP_TYPE_STRING &&
128 1.3.8.2 yamt prop_string_size(desc) > 0)
129 1.3.8.2 yamt strcpy(sc->sc_info[0].desc, prop_string_cstring_nocopy(desc));
130 1.3.8.2 yamt else
131 1.3.8.2 yamt strcpy(sc->sc_info[0].desc, sc->sc_dev.dv_xname);
132 1.3.8.2 yamt
133 1.3.8.2 yamt /* Hook into system monitor. */
134 1.3.8.2 yamt sc->sc_sysmon.sme_ranges = owtemp_ranges;
135 1.3.8.2 yamt sc->sc_sysmon.sme_sensor_info = sc->sc_info;
136 1.3.8.2 yamt sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
137 1.3.8.2 yamt sc->sc_sysmon.sme_cookie = sc;
138 1.3.8.2 yamt
139 1.3.8.2 yamt sc->sc_sysmon.sme_gtredata = owtemp_gtredata;
140 1.3.8.2 yamt sc->sc_sysmon.sme_streinfo = owtemp_streinfo;
141 1.3.8.2 yamt
142 1.3.8.2 yamt sc->sc_sysmon.sme_nsensors = 1;
143 1.3.8.2 yamt sc->sc_sysmon.sme_envsys_version = 1000;
144 1.3.8.2 yamt
145 1.3.8.2 yamt if (sysmon_envsys_register(&sc->sc_sysmon))
146 1.3.8.2 yamt aprint_error("%s: unable to register with sysmon\n",
147 1.3.8.2 yamt sc->sc_dev.dv_xname);
148 1.3.8.2 yamt
149 1.3.8.2 yamt #if 0 /* Old OpenBSD code */
150 1.3.8.2 yamt strlcpy(sc->sc_sensor.device, sc->sc_dev.dv_xname,
151 1.3.8.2 yamt sizeof(sc->sc_sensor.device));
152 1.3.8.2 yamt sc->sc_sensor.type = SENSOR_TEMP;
153 1.3.8.2 yamt strlcpy(sc->sc_sensor.desc, "Temp", sizeof(sc->sc_sensor.desc));
154 1.3.8.2 yamt
155 1.3.8.2 yamt if (sensor_task_register(sc, owtemp_update, 5)) {
156 1.3.8.2 yamt printf(": unable to register update task\n");
157 1.3.8.2 yamt return;
158 1.3.8.2 yamt }
159 1.3.8.2 yamt sensor_add(&sc->sc_sensor);
160 1.3.8.2 yamt #endif
161 1.3.8.2 yamt
162 1.3.8.2 yamt printf("\n");
163 1.3.8.2 yamt }
164 1.3.8.2 yamt
165 1.3.8.2 yamt int
166 1.3.8.2 yamt owtemp_detach(struct device *self, int flags)
167 1.3.8.2 yamt {
168 1.3.8.2 yamt struct owtemp_softc *sc = device_private(self);
169 1.3.8.2 yamt
170 1.3.8.2 yamt sysmon_envsys_unregister(&sc->sc_sysmon);
171 1.3.8.2 yamt
172 1.3.8.2 yamt return 0;
173 1.3.8.2 yamt }
174 1.3.8.2 yamt
175 1.3.8.2 yamt int
176 1.3.8.2 yamt owtemp_activate(struct device *self, enum devact act)
177 1.3.8.2 yamt {
178 1.3.8.2 yamt struct owtemp_softc *sc = device_private(self);
179 1.3.8.2 yamt
180 1.3.8.2 yamt switch (act) {
181 1.3.8.2 yamt case DVACT_ACTIVATE:
182 1.3.8.2 yamt return (EOPNOTSUPP);
183 1.3.8.2 yamt case DVACT_DEACTIVATE:
184 1.3.8.2 yamt sc->sc_dying = 1;
185 1.3.8.2 yamt break;
186 1.3.8.2 yamt }
187 1.3.8.2 yamt
188 1.3.8.2 yamt return (0);
189 1.3.8.2 yamt }
190 1.3.8.2 yamt
191 1.3.8.2 yamt void
192 1.3.8.2 yamt owtemp_update(void *arg)
193 1.3.8.2 yamt {
194 1.3.8.2 yamt struct owtemp_softc *sc = arg;
195 1.3.8.2 yamt u_int8_t data[9];
196 1.3.8.2 yamt
197 1.3.8.2 yamt onewire_lock(sc->sc_onewire, 0);
198 1.3.8.2 yamt if (onewire_reset(sc->sc_onewire) != 0)
199 1.3.8.2 yamt goto done;
200 1.3.8.2 yamt onewire_matchrom(sc->sc_onewire, sc->sc_rom);
201 1.3.8.2 yamt
202 1.3.8.2 yamt /*
203 1.3.8.2 yamt * Start temperature conversion. The conversion takes up to 750ms.
204 1.3.8.2 yamt * After sending the command, the data line must be held high for
205 1.3.8.2 yamt * at least 750ms to provide power during the conversion process.
206 1.3.8.2 yamt * As such, no other activity may take place on the 1-Wire bus for
207 1.3.8.2 yamt * at least this period.
208 1.3.8.2 yamt */
209 1.3.8.2 yamt onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
210 1.3.8.2 yamt tsleep(sc, PRIBIO, "owtemp", hz);
211 1.3.8.2 yamt
212 1.3.8.2 yamt if (onewire_reset(sc->sc_onewire) != 0)
213 1.3.8.2 yamt goto done;
214 1.3.8.2 yamt onewire_matchrom(sc->sc_onewire, sc->sc_rom);
215 1.3.8.2 yamt
216 1.3.8.2 yamt /*
217 1.3.8.2 yamt * The result of the temperature measurement is placed in the
218 1.3.8.2 yamt * first two bytes of the scratchpad.
219 1.3.8.2 yamt */
220 1.3.8.2 yamt onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
221 1.3.8.2 yamt onewire_read_block(sc->sc_onewire, data, 9);
222 1.3.8.2 yamt #if 0
223 1.3.8.2 yamt if (onewire_crc(data, 8) == data[8]) {
224 1.3.8.2 yamt sc->sc_sensor.value = 273150000 +
225 1.3.8.2 yamt (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
226 1.3.8.2 yamt }
227 1.3.8.2 yamt #endif
228 1.3.8.2 yamt
229 1.3.8.2 yamt sc->sc_sensor[0].cur.data_us = sc->sc_owtemp_decode(data);
230 1.3.8.2 yamt sc->sc_sensor[0].validflags |= ENVSYS_FCURVALID;
231 1.3.8.2 yamt
232 1.3.8.2 yamt done:
233 1.3.8.2 yamt onewire_unlock(sc->sc_onewire);
234 1.3.8.2 yamt }
235 1.3.8.2 yamt
236 1.3.8.2 yamt static int
237 1.3.8.2 yamt owtemp_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
238 1.3.8.2 yamt {
239 1.3.8.2 yamt struct owtemp_softc *sc = sme->sme_cookie;
240 1.3.8.2 yamt
241 1.3.8.2 yamt owtemp_update(sc);
242 1.3.8.2 yamt *tred = sc->sc_sensor[tred->sensor];
243 1.3.8.2 yamt
244 1.3.8.2 yamt return 0;
245 1.3.8.2 yamt }
246 1.3.8.2 yamt
247 1.3.8.2 yamt static int
248 1.3.8.2 yamt owtemp_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
249 1.3.8.2 yamt {
250 1.3.8.2 yamt struct owtemp_softc *sc = sme->sme_cookie;
251 1.3.8.2 yamt
252 1.3.8.2 yamt onewire_lock(sc->sc_onewire,0); /* Also locks our instance */
253 1.3.8.2 yamt
254 1.3.8.2 yamt memcpy(sc->sc_info[binfo->sensor].desc, binfo->desc,
255 1.3.8.2 yamt sizeof(sc->sc_info[binfo->sensor].desc));
256 1.3.8.2 yamt sc->sc_info[binfo->sensor].desc[
257 1.3.8.2 yamt sizeof(sc->sc_info[binfo->sensor].desc) - 1] = '\0';
258 1.3.8.2 yamt
259 1.3.8.2 yamt onewire_unlock(sc->sc_onewire);
260 1.3.8.2 yamt
261 1.3.8.2 yamt binfo->validflags = ENVSYS_FVALID;
262 1.3.8.2 yamt
263 1.3.8.2 yamt return 0;
264 1.3.8.2 yamt }
265 1.3.8.2 yamt
266 1.3.8.2 yamt static uint32_t
267 1.3.8.2 yamt owtemp_decode_ds18b20(const uint8_t *buf)
268 1.3.8.2 yamt {
269 1.3.8.2 yamt int temp;
270 1.3.8.2 yamt
271 1.3.8.2 yamt /*
272 1.3.8.2 yamt * Sign-extend the MSB byte, and add in the fractions of a
273 1.3.8.2 yamt * degree contained in the LSB (precision 1/16th DegC).
274 1.3.8.2 yamt */
275 1.3.8.2 yamt temp = (int8_t)buf[1];
276 1.3.8.2 yamt temp = (temp << 8) | buf[0];
277 1.3.8.2 yamt
278 1.3.8.2 yamt /*
279 1.3.8.2 yamt * Conversion to uK is simple.
280 1.3.8.2 yamt */
281 1.3.8.2 yamt return (temp * 62500 + 273150000);
282 1.3.8.2 yamt }
283 1.3.8.2 yamt
284 1.3.8.2 yamt static uint32_t
285 1.3.8.2 yamt owtemp_decode_ds1920(const uint8_t *buf)
286 1.3.8.2 yamt {
287 1.3.8.2 yamt int temp;
288 1.3.8.2 yamt
289 1.3.8.2 yamt /*
290 1.3.8.2 yamt * Sign-extend the MSB byte, and add in the fractions of a
291 1.3.8.2 yamt * degree contained in the LSB (precision 1/2 DegC).
292 1.3.8.2 yamt */
293 1.3.8.2 yamt temp = (int8_t)buf[1];
294 1.3.8.2 yamt temp = (temp << 8) | buf[0];
295 1.3.8.2 yamt
296 1.3.8.2 yamt /* Convert to uK */
297 1.3.8.2 yamt return (temp * 500000 + 273150000);
298 1.3.8.2 yamt }
299