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