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