owtemp.c revision 1.7 1 /* $NetBSD: owtemp.c,v 1.7 2007/07/01 07:37:21 xtraeme 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.7 2007/07/01 07:37:21 xtraeme 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 struct device sc_dev;
44
45 void * sc_onewire;
46 u_int64_t sc_rom;
47
48 envsys_data_t sc_sensor[1];
49 struct sysmon_envsys sc_sysmon;
50
51 uint32_t (*sc_owtemp_decode)(const uint8_t *);
52
53 int sc_dying;
54 };
55
56 int owtemp_match(struct device *, struct cfdata *, void *);
57 void owtemp_attach(struct device *, struct device *, void *);
58 int owtemp_detach(struct device *, int);
59 int owtemp_activate(struct device *, enum devact);
60
61 void owtemp_update(void *);
62
63 CFATTACH_DECL(owtemp, sizeof(struct owtemp_softc),
64 owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
65
66 extern struct cfdriver owtemp_cd;
67
68 static const struct onewire_matchfam owtemp_fams[] = {
69 { ONEWIRE_FAMILY_DS1920 },
70 { ONEWIRE_FAMILY_DS18B20 },
71 { ONEWIRE_FAMILY_DS1822 },
72 };
73
74 static int owtemp_gtredata(struct sysmon_envsys *, envsys_data_t *);
75
76 static uint32_t owtemp_decode_ds18b20(const uint8_t *);
77 static uint32_t owtemp_decode_ds1920(const uint8_t *);
78
79 int
80 owtemp_match(struct device *parent, struct cfdata *cf, void *aux)
81 {
82 return (onewire_matchbyfam(aux, owtemp_fams,
83 __arraycount(owtemp_fams)));
84 }
85
86 void
87 owtemp_attach(struct device *parent, struct device *self, void *aux)
88 {
89 struct owtemp_softc *sc = device_private(self);
90 struct onewire_attach_args *oa = aux;
91 prop_string_t desc;
92
93 sc->sc_onewire = oa->oa_onewire;
94 sc->sc_rom = oa->oa_rom;
95
96 switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
97 case ONEWIRE_FAMILY_DS18B20:
98 case ONEWIRE_FAMILY_DS1822:
99 sc->sc_owtemp_decode = owtemp_decode_ds18b20;
100 break;
101 case ONEWIRE_FAMILY_DS1920:
102 sc->sc_owtemp_decode = owtemp_decode_ds1920;
103 break;
104 }
105
106 /* Initialize sensor */
107 sc->sc_sensor[0].sensor = 0;
108 sc->sc_sensor[0].state = ENVSYS_SVALID;
109 sc->sc_sensor[0].units = ENVSYS_STEMP;
110 desc = prop_dictionary_get(device_properties(&sc->sc_dev),
111 "envsys-description");
112 if (desc != NULL &&
113 prop_object_type(desc) == PROP_TYPE_STRING &&
114 prop_string_size(desc) > 0)
115 strcpy(sc->sc_sensor[0].desc, prop_string_cstring_nocopy(desc));
116 else
117 strcpy(sc->sc_sensor[0].desc, sc->sc_dev.dv_xname);
118
119 /* Hook into system monitor. */
120 sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
121 sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
122 sc->sc_sysmon.sme_cookie = sc;
123 sc->sc_sysmon.sme_gtredata = owtemp_gtredata;
124 sc->sc_sysmon.sme_nsensors = 1;
125
126 if (sysmon_envsys_register(&sc->sc_sysmon))
127 aprint_error("%s: unable to register with sysmon\n",
128 sc->sc_dev.dv_xname);
129
130 #if 0 /* Old OpenBSD code */
131 strlcpy(sc->sc_sensor.device, sc->sc_dev.dv_xname,
132 sizeof(sc->sc_sensor.device));
133 sc->sc_sensor.type = SENSOR_TEMP;
134 strlcpy(sc->sc_sensor.desc, "Temp", sizeof(sc->sc_sensor.desc));
135
136 if (sensor_task_register(sc, owtemp_update, 5)) {
137 printf(": unable to register update task\n");
138 return;
139 }
140 sensor_add(&sc->sc_sensor);
141 #endif
142
143 printf("\n");
144 }
145
146 int
147 owtemp_detach(struct device *self, int flags)
148 {
149 struct owtemp_softc *sc = device_private(self);
150
151 sysmon_envsys_unregister(&sc->sc_sysmon);
152
153 return 0;
154 }
155
156 int
157 owtemp_activate(struct device *self, enum devact act)
158 {
159 struct owtemp_softc *sc = device_private(self);
160
161 switch (act) {
162 case DVACT_ACTIVATE:
163 return (EOPNOTSUPP);
164 case DVACT_DEACTIVATE:
165 sc->sc_dying = 1;
166 break;
167 }
168
169 return (0);
170 }
171
172 void
173 owtemp_update(void *arg)
174 {
175 struct owtemp_softc *sc = arg;
176 u_int8_t data[9];
177
178 onewire_lock(sc->sc_onewire, 0);
179 if (onewire_reset(sc->sc_onewire) != 0)
180 goto done;
181 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
182
183 /*
184 * Start temperature conversion. The conversion takes up to 750ms.
185 * After sending the command, the data line must be held high for
186 * at least 750ms to provide power during the conversion process.
187 * As such, no other activity may take place on the 1-Wire bus for
188 * at least this period.
189 */
190 onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
191 tsleep(sc, PRIBIO, "owtemp", hz);
192
193 if (onewire_reset(sc->sc_onewire) != 0)
194 goto done;
195 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
196
197 /*
198 * The result of the temperature measurement is placed in the
199 * first two bytes of the scratchpad.
200 */
201 onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
202 onewire_read_block(sc->sc_onewire, data, 9);
203 #if 0
204 if (onewire_crc(data, 8) == data[8]) {
205 sc->sc_sensor.value = 273150000 +
206 (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
207 }
208 #endif
209
210 sc->sc_sensor[0].value_cur = sc->sc_owtemp_decode(data);
211 sc->sc_sensor[0].state |= ENVSYS_SVALID;
212
213 done:
214 onewire_unlock(sc->sc_onewire);
215 }
216
217 static int
218 owtemp_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
219 {
220 struct owtemp_softc *sc = sme->sme_cookie;
221
222 owtemp_update(sc);
223 return 0;
224 }
225
226 static uint32_t
227 owtemp_decode_ds18b20(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/16th DegC).
234 */
235 temp = (int8_t)buf[1];
236 temp = (temp << 8) | buf[0];
237
238 /*
239 * Conversion to uK is simple.
240 */
241 return (temp * 62500 + 273150000);
242 }
243
244 static uint32_t
245 owtemp_decode_ds1920(const uint8_t *buf)
246 {
247 int temp;
248
249 /*
250 * Sign-extend the MSB byte, and add in the fractions of a
251 * degree contained in the LSB (precision 1/2 DegC).
252 */
253 temp = (int8_t)buf[1];
254 temp = (temp << 8) | buf[0];
255
256 /* Convert to uK */
257 return (temp * 500000 + 273150000);
258 }
259