Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/ow/ow_temp.c
- This file was added.
/*- | |||||
* Copyright (c) 2015 M. Warner Losh <imp@freebsd.org> | |||||
* All rights reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice unmodified, this list of conditions, and the following | |||||
* disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <sys/param.h> | |||||
#include <sys/systm.h> | |||||
#include <sys/kernel.h> | |||||
#include <sys/bus.h> | |||||
#include <sys/errno.h> | |||||
#include <sys/libkern.h> | |||||
#include <sys/malloc.h> | |||||
#include <sys/module.h> | |||||
#include <sys/sysctl.h> | |||||
#include <dev/ow/ow.h> | |||||
#define OWT_DS1820 0x10 | |||||
#define OWT_DS18B20 0x28 | |||||
#define CONVERT_T 0x44 | |||||
#define COPY_SCRATCHPAD 0x48 | |||||
#define WRITE_SCRATCHPAD 0x4e | |||||
#define READ_POWER_SUPPY 0xb4 | |||||
#define RECALL_EE 0xb8 | |||||
#define READ_SCRATCHPAD 0xbe | |||||
struct ow_temp_softc | |||||
{ | |||||
device_t dev; | |||||
int type; | |||||
int temp; | |||||
struct callout temp_callout; | |||||
struct mtx temp_lock; | |||||
}; | |||||
static int | |||||
ow_temp_probe(device_t dev) | |||||
{ | |||||
switch (ow_get_family(dev)) { | |||||
case OWT_DS1820: | |||||
device_set_desc(dev, "One Wire Temperature"); | |||||
return BUS_PROBE_DEFAULT; | |||||
case OWT_DS18B20: | |||||
device_set_desc(dev, "Advanced One Wire Temperature"); | |||||
return BUS_PROBE_DEFAULT; | |||||
default: | |||||
return ENXIO; | |||||
} | |||||
} | |||||
static int | |||||
ow_temp_read_scratchpad(device_t dev, uint8_t *scratch, int len) | |||||
{ | |||||
struct ow_cmd cmd; | |||||
uint8_t *mep; | |||||
device_t lldev = device_get_parent(device_get_parent(dev)); | |||||
/* RESET MATCH myid READ_SCRATCHPAD < data */ | |||||
mep = ow_get_romid(dev); | |||||
memset(&cmd, 0, sizeof(cmd)); | |||||
cmd.rom_cmd = MATCH_ROM; | |||||
cmd.rom_extra_len = 8; | |||||
memcpy(&cmd.rom_extra, mep, 8); | |||||
cmd.flags = OW_FLAG_SEND_XPT_CMD; | |||||
cmd.xpt_cmd = READ_SCRATCHPAD; | |||||
cmd.xpt_read_len = len; | |||||
ow_send_command(lldev, &cmd); | |||||
memcpy(scratch, cmd.xpt_read, len); | |||||
return 0; | |||||
} | |||||
static int | |||||
ow_temp_convert_t(device_t dev) | |||||
{ | |||||
struct ow_cmd cmd; | |||||
device_t lldev = device_get_parent(device_get_parent(dev)); | |||||
memset(&cmd, 0, sizeof(cmd)); | |||||
cmd.rom_cmd = SKIP_ROM; | |||||
cmd.flags = OW_FLAG_SEND_XPT_CMD; | |||||
cmd.xpt_cmd = CONVERT_T; | |||||
ow_send_command(lldev, &cmd); | |||||
return 0; | |||||
} | |||||
static void ow_temp_poll(void *xsc); | |||||
static void | |||||
ow_temp_read(void *xsc) | |||||
{ | |||||
struct ow_temp_softc *sc = xsc; | |||||
uint8_t scratch[8]; | |||||
/* XXX should verify scratch pad is OK */ | |||||
if (ow_temp_read_scratchpad(sc->dev, scratch, sizeof(scratch)) == 0) | |||||
sc->temp = (int16_t)(scratch[0] | (scratch[1] << 8)); | |||||
callout_reset(&sc->temp_callout, 4 * hz, ow_temp_poll, sc); | |||||
} | |||||
static void | |||||
ow_temp_poll(void *xsc) | |||||
{ | |||||
struct ow_temp_softc *sc = xsc; | |||||
ow_temp_convert_t(sc->dev); | |||||
/* have to wait a second to read this data */ | |||||
callout_reset(&sc->temp_callout, hz, ow_temp_read, sc); | |||||
} | |||||
static int | |||||
ow_temp_attach(device_t dev) | |||||
{ | |||||
struct ow_temp_softc *sc; | |||||
sc = device_get_softc(dev); | |||||
sc->dev = dev; | |||||
sc->type = ow_get_family(dev); | |||||
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), | |||||
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), | |||||
OID_AUTO, "temperature", CTLFLAG_RD, | |||||
&sc->temp, 0, | |||||
"Current Temperature"); | |||||
/* | |||||
* Just do this for unit 0 to avoid locking | |||||
* the ow bus until that code can be put | |||||
* into place. | |||||
*/ | |||||
if (device_get_unit(dev) == 0) { | |||||
printf("Setting up callout for unit 0\n"); | |||||
sc->temp = 1; | |||||
mtx_init(&sc->temp_lock, "lock for doing temperature", NULL, MTX_DEF); | |||||
callout_init_mtx(&sc->temp_callout, &sc->temp_lock, 0); | |||||
callout_reset(&sc->temp_callout, 5 * hz, ow_temp_poll, sc); | |||||
} | |||||
return 0; | |||||
} | |||||
static int | |||||
ow_temp_detach(device_t dev) | |||||
{ | |||||
struct ow_temp_softc *sc; | |||||
sc = device_get_softc(dev); | |||||
if (device_get_unit(dev) == 0) { | |||||
callout_drain(&sc->temp_callout); | |||||
mtx_destroy(&sc->temp_lock); | |||||
} | |||||
return 0; | |||||
} | |||||
devclass_t ow_temp_devclass; | |||||
static device_method_t ow_temp_methods[] = { | |||||
/* Device interface */ | |||||
DEVMETHOD(device_probe, ow_temp_probe), | |||||
DEVMETHOD(device_attach, ow_temp_attach), | |||||
DEVMETHOD(device_detach, ow_temp_detach), | |||||
{ 0, 0 } | |||||
}; | |||||
static driver_t ow_temp_driver = { | |||||
"ow_temp", | |||||
ow_temp_methods, | |||||
sizeof(struct ow_temp_softc), | |||||
}; | |||||
DRIVER_MODULE(ow_temp, ow, ow_temp_driver, ow_temp_devclass, 0, 0); | |||||
MODULE_DEPEND(ow_temp, ow, 1, 1, 1); |