Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/i2c - document the config register



details:   https://anonhg.NetBSD.org/src/rev/06c47be552aa
branches:  trunk
changeset: 969706:06c47be552aa
user:      macallan <macallan%NetBSD.org@localhost>
date:      Fri Feb 28 13:18:25 2020 +0000

description:
- document the config register
- do some setup and send a start command
- mark the sensor as invalid if readings are out of range
now this works properly on my PCI-X G5

diffstat:

 sys/dev/i2c/dstemp.c |  49 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 45 insertions(+), 4 deletions(-)

diffs (98 lines):

diff -r c7563f49aa64 -r 06c47be552aa sys/dev/i2c/dstemp.c
--- a/sys/dev/i2c/dstemp.c      Fri Feb 28 13:00:51 2020 +0000
+++ b/sys/dev/i2c/dstemp.c      Fri Feb 28 13:18:25 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: dstemp.c,v 1.4 2018/06/26 06:03:57 thorpej Exp $ */
+/* $NetBSD: dstemp.c,v 1.5 2020/02/28 13:18:25 macallan Exp $ */
 
 /*-
  * Copyright (c) 2018 Michael Lorenz
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dstemp.c,v 1.4 2018/06/26 06:03:57 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dstemp.c,v 1.5 2020/02/28 13:18:25 macallan Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -55,6 +55,18 @@
 #define DSTEMP_TLOW            0xa2    /* low threshold, 2 bytes */
 #define DSTEMP_CONFIG          0xac    /* 1 byte */
 
+#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
+
 struct dstemp_softc {
        device_t        sc_dev;
        i2c_tag_t       sc_i2c;
@@ -68,6 +80,7 @@
 static void    dstemp_attach(device_t, device_t, void *);
 
 static void    dstemp_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
+static void    dstemp_init(struct dstemp_softc *);
 
 CFATTACH_DECL_NEW(dstemp, sizeof(struct dstemp_softc),
     dstemp_match, dstemp_attach, NULL, NULL);
@@ -106,6 +119,8 @@
        aprint_naive("\n");
        aprint_normal(": DS1361\n");
 
+       dstemp_init(sc);
+
        sc->sc_sme = sysmon_envsys_create();
        sc->sc_sme->sme_name = device_xname(self);
        sc->sc_sme->sme_cookie = sc;
@@ -128,6 +143,29 @@
 }
 
 static void
+dstemp_init(struct dstemp_softc *sc)
+{
+       int error;
+       uint8_t cmd[2], data;
+
+       iic_acquire_bus(sc->sc_i2c, 0);
+       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);
+}
+
+static void
 dstemp_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
 {
        struct dstemp_softc *sc = sme->sme_cookie;
@@ -145,8 +183,11 @@
                edata->state = ENVSYS_SINVALID;
        } else {
                edata->value_cur =
-                   ((uint64_t)(data>>4) * 62500) +
+                   ((uint64_t)(be16toh(data) >> 4) * 62500) +
                    + 273150000;
-               edata->state = ENVSYS_SVALID;
+               if (edata->value_cur > (273150000 + 120000000)) {
+                       edata->state = ENVSYS_SINVALID;
+               } else
+                       edata->state = ENVSYS_SVALID;
        }
 }



Home | Main Index | Thread Index | Old Index