2021-06-21 06:04:27 +03:00
|
|
|
/* $NetBSD: dstemp.c,v 1.14 2021/06/21 03:12:54 christos Exp $ */
|
2018-02-02 00:44:17 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2018 Michael Lorenz
|
|
|
|
* 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, 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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>
|
2021-06-21 06:04:27 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: dstemp.c,v 1.14 2021/06/21 03:12:54 christos Exp $");
|
2018-02-02 00:44:17 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
|
|
|
|
#include <dev/i2c/i2cvar.h>
|
|
|
|
|
|
|
|
#include <dev/sysmon/sysmonvar.h>
|
|
|
|
|
|
|
|
#define DSTEMP_CMD_START 0x51 /* command, no data */
|
|
|
|
#define DSTEMP_CMD_POR 0x54 /* command, no data */
|
|
|
|
#define DSTEMP_CMD_STOP 0x22 /* command, no data */
|
|
|
|
#define DSTEMP_TCURRENT 0xaa /* current temperature, 2 bytes */
|
|
|
|
#define DSTEMP_THIGH 0xa1 /* high threshold, 2 bytes */
|
|
|
|
#define DSTEMP_TLOW 0xa2 /* low threshold, 2 bytes */
|
|
|
|
#define DSTEMP_CONFIG 0xac /* 1 byte */
|
|
|
|
|
2020-02-28 16:18:25 +03:00
|
|
|
#define DSTEMP_1SHOT 0x01
|
|
|
|
#define DSTEMP_POL 0x02 /* Tout polarity, 1 - active high */
|
|
|
|
#define DSTEMP_8BIT 0x00
|
|
|
|
#define DSTEMP_10BIT 0x04
|
|
|
|
#define DSTEMP_11BIT 0x08
|
|
|
|
#define DSTEMP_12BIT 0x0c
|
|
|
|
#define DSTEMP_RES_MASK 0x0c
|
|
|
|
#define DSTEMP_NVB 0x10 /* EEPROM busy */
|
|
|
|
#define DSTEMP_TLF 0x20 /* temperature low flag */
|
|
|
|
#define DSTEMP_THF 0x40 /* temperature high flag */
|
|
|
|
#define DSTEMP_DONE 0x80
|
|
|
|
|
2018-02-02 00:44:17 +03:00
|
|
|
struct dstemp_softc {
|
2020-08-22 00:28:22 +03:00
|
|
|
device_t sc_dev;
|
|
|
|
i2c_tag_t sc_i2c;
|
|
|
|
i2c_addr_t sc_addr;
|
|
|
|
prop_dictionary_t sc_prop;
|
|
|
|
struct sysmon_envsys *sc_sme;
|
|
|
|
envsys_data_t sc_sensor_temp;
|
2018-02-02 00:44:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static int dstemp_match(device_t, cfdata_t, void *);
|
|
|
|
static void dstemp_attach(device_t, device_t, void *);
|
|
|
|
|
|
|
|
static void dstemp_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
|
2020-02-28 16:18:25 +03:00
|
|
|
static void dstemp_init(struct dstemp_softc *);
|
2018-02-02 00:44:17 +03:00
|
|
|
|
|
|
|
CFATTACH_DECL_NEW(dstemp, sizeof(struct dstemp_softc),
|
|
|
|
dstemp_match, dstemp_attach, NULL, NULL);
|
|
|
|
|
2018-06-26 09:03:57 +03:00
|
|
|
static const struct device_compatible_entry compat_data[] = {
|
2021-01-30 20:37:25 +03:00
|
|
|
{ .compat = "dallas,ds1631" },
|
2021-01-18 00:42:35 +03:00
|
|
|
{ .compat = "ds1631" },
|
2021-01-27 05:29:48 +03:00
|
|
|
DEVICE_COMPAT_EOL
|
2018-06-18 20:07:07 +03:00
|
|
|
};
|
|
|
|
|
2018-02-02 00:44:17 +03:00
|
|
|
static int
|
|
|
|
dstemp_match(device_t parent, cfdata_t match, void *aux)
|
|
|
|
{
|
|
|
|
struct i2c_attach_args *ia = aux;
|
2018-06-17 00:22:13 +03:00
|
|
|
int match_result;
|
2018-02-02 00:44:17 +03:00
|
|
|
|
2018-06-26 09:03:57 +03:00
|
|
|
if (iic_use_direct_match(ia, match, compat_data, &match_result))
|
2018-06-17 00:22:13 +03:00
|
|
|
return match_result;
|
|
|
|
|
|
|
|
if ((ia->ia_addr & 0xf8) == 0x48)
|
|
|
|
return I2C_MATCH_ADDRESS_ONLY;
|
|
|
|
|
|
|
|
return 0;
|
2018-02-02 00:44:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dstemp_attach(device_t parent, device_t self, void *aux)
|
|
|
|
{
|
|
|
|
struct dstemp_softc *sc = device_private(self);
|
|
|
|
struct i2c_attach_args *ia = aux;
|
|
|
|
char name[64] = "temperature";
|
2020-08-22 00:28:22 +03:00
|
|
|
const char *desc;
|
2018-02-02 00:44:17 +03:00
|
|
|
|
|
|
|
sc->sc_dev = self;
|
|
|
|
sc->sc_i2c = ia->ia_tag;
|
|
|
|
sc->sc_addr = ia->ia_addr;
|
2020-08-22 00:28:22 +03:00
|
|
|
sc->sc_prop = ia->ia_prop;
|
2021-01-30 04:22:06 +03:00
|
|
|
prop_object_retain(sc->sc_prop);
|
2018-02-02 00:44:17 +03:00
|
|
|
|
|
|
|
aprint_naive("\n");
|
|
|
|
aprint_normal(": DS1361\n");
|
|
|
|
|
2020-02-28 16:18:25 +03:00
|
|
|
dstemp_init(sc);
|
|
|
|
|
2018-02-02 00:44:17 +03:00
|
|
|
sc->sc_sme = sysmon_envsys_create();
|
|
|
|
sc->sc_sme->sme_name = device_xname(self);
|
|
|
|
sc->sc_sme->sme_cookie = sc;
|
|
|
|
sc->sc_sme->sme_refresh = dstemp_sensors_refresh;
|
|
|
|
|
|
|
|
sc->sc_sensor_temp.units = ENVSYS_STEMP;
|
|
|
|
sc->sc_sensor_temp.state = ENVSYS_SINVALID;
|
2021-03-01 07:39:45 +03:00
|
|
|
sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
|
2020-08-22 00:28:22 +03:00
|
|
|
|
2021-06-21 06:04:27 +03:00
|
|
|
if (prop_dictionary_get_string(sc->sc_prop, "s00", &desc)) {
|
2020-08-22 00:28:22 +03:00
|
|
|
strncpy(name, desc, 64);
|
2021-06-21 06:04:27 +03:00
|
|
|
} else if (prop_dictionary_get_string(sc->sc_prop, "saa", &desc)) {
|
2020-08-22 00:28:22 +03:00
|
|
|
strncpy(name, desc, 64);
|
2018-02-02 00:44:17 +03:00
|
|
|
}
|
2020-08-22 00:28:22 +03:00
|
|
|
|
2018-02-02 00:44:17 +03:00
|
|
|
strncpy(sc->sc_sensor_temp.desc, name, sizeof(sc->sc_sensor_temp.desc));
|
|
|
|
|
|
|
|
sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
|
|
|
|
|
|
|
|
sysmon_envsys_register(sc->sc_sme);
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:18:25 +03:00
|
|
|
static void
|
|
|
|
dstemp_init(struct dstemp_softc *sc)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
uint8_t cmd[2], data;
|
|
|
|
|
2021-06-15 07:41:01 +03:00
|
|
|
if (iic_acquire_bus(sc->sc_i2c, 0))
|
|
|
|
return;
|
2020-02-28 16:18:25 +03:00
|
|
|
cmd[0] = DSTEMP_CONFIG;
|
|
|
|
data = 0;
|
|
|
|
error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
|
|
|
|
sc->sc_addr, cmd, 1, &data, 1, 0);
|
|
|
|
/* we don't want to change the POL bit, so preserve it */
|
|
|
|
cmd[1] = (data & DSTEMP_POL) | DSTEMP_12BIT;
|
|
|
|
error |= iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
|
|
|
|
sc->sc_addr, cmd, 2, NULL, 0, 0);
|
|
|
|
/* ... and start converting */
|
|
|
|
cmd[0] = DSTEMP_CMD_START;
|
|
|
|
error |= iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
|
|
|
|
sc->sc_addr, cmd, 1, NULL, 0, 0);
|
|
|
|
if (error) aprint_error_dev(sc->sc_dev, "chip initialization failed\n");
|
|
|
|
iic_release_bus(sc->sc_i2c, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-02 00:44:17 +03:00
|
|
|
static void
|
|
|
|
dstemp_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
|
|
|
|
{
|
|
|
|
struct dstemp_softc *sc = sme->sme_cookie;
|
|
|
|
uint8_t cmd = DSTEMP_TCURRENT;
|
|
|
|
uint16_t data = 0;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
2021-06-15 07:41:01 +03:00
|
|
|
error = iic_acquire_bus(sc->sc_i2c, 0);
|
|
|
|
if (error == 0) {
|
|
|
|
error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
|
|
|
|
sc->sc_addr, &cmd, 1, &data, 2, 0);
|
|
|
|
iic_release_bus(sc->sc_i2c, 0);
|
|
|
|
}
|
2018-02-02 00:44:17 +03:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
edata->state = ENVSYS_SINVALID;
|
|
|
|
} else {
|
|
|
|
edata->value_cur =
|
2020-02-28 16:18:25 +03:00
|
|
|
((uint64_t)(be16toh(data) >> 4) * 62500) +
|
2018-02-02 00:44:17 +03:00
|
|
|
+ 273150000;
|
2020-02-28 16:18:25 +03:00
|
|
|
if (edata->value_cur > (273150000 + 120000000)) {
|
|
|
|
edata->state = ENVSYS_SINVALID;
|
|
|
|
} else
|
|
|
|
edata->state = ENVSYS_SVALID;
|
2018-02-02 00:44:17 +03:00
|
|
|
}
|
|
|
|
}
|