NetBSD-Bugs archive

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

kern/40420: adding SMSC LPC47M192 hardware monitor at SMBus



>Number:         40420
>Category:       kern
>Synopsis:       adding SMSC LPC47M192 hardware monitor at SMBus
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kern-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sat Jan 17 06:25:00 +0000 2009
>Originator:     Takahiro Hayashi
>Release:        NetBSD current(5.99.6) Jan 10 13:08:54 UTC 2009
>Organization:
>Environment:
System: NetBSD halt 5.99.6 NetBSD 5.99.6 (UNION) #0: Thu Jan 15 20:41:02 JST 
2009  root@halt:/usr/obj/sys/arch/i386/compile/UNION i386
Architecture: i386
Machine: i386
>Description:
        This patch adds SMSC LPC47M192 hardware monitor at SMBus
        to the kernel.
        For example, envstat(8) will display:

% envstat
                   Current  CritMax  CritMin  CritCap     Unit
[smsctemp0]
         +2.5V:      2.568                                   V
          Vccp:      1.156                                   V
         +3.3V:      3.287                                   V
           +5V:      4.980                                   V
          +12V:     12.327                                   V
           Vcc:      3.304                                   V
         +1.5V:      1.587                                   V
         +1.8V:      1.784                                   V
  Remote Temp1:     46.000                                degC
  Ambient Temp:     38.000                                degC
  Remote Temp2:     41.000                                degC
% dmesg | grep smsctemp
smsctemp0 at iic0 addr 0x2d: LPC47M192 hardware monitor
% 

        I tested on Intel D945GCLF and D945GCLF2 on i386/amd64.
        This patch does not support fan monitoring.

>How-To-Repeat:
>Fix:
        apply this patch and add line "smsctemp* at iic? addr 0x2d"
        to the kernel configuration file.

Index: sys/dev/i2c/files.i2c
===================================================================
RCS file: /cvsroot/src/sys/dev/i2c/files.i2c,v
retrieving revision 1.23
diff -u -r1.23 files.i2c
--- sys/dev/i2c/files.i2c       30 Oct 2008 12:52:46 -0000      1.23
+++ sys/dev/i2c/files.i2c       17 Jan 2009 06:02:02 -0000
@@ -110,3 +110,8 @@
 device admtemp: sysmon_envsys
 attach admtemp at iic
 file   dev/i2c/adm1021.c               admtemp
+
+# SMSC LPC47M192 hardware monitor
+device smsctemp: sysmon_envsys
+attach smsctemp at iic
+file   dev/i2c/smsctemp.c              smsctemp
--- /dev/null   2009-01-04 00:00:53.000000000 +0900
+++ sys/dev/i2c/smsctemp.c      2009-01-17 14:42:13.000000000 +0900
@@ -0,0 +1,334 @@
+/* $NetBSD$ */
+
+/*
+ * 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>
+__KERNEL_RCSID(0, "$NetBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+
+#include <dev/i2c/i2cvar.h>
+#include <dev/sysmon/sysmonvar.h>
+#include <dev/i2c/smsctempvar.h>
+
+/*
+ * A driver for SMSC LPC47M192 hardware monitor at SMBus.
+ * This driver supports 8 Voltage and 3 Temperature sensors.
+ * Fan RPM monitoring is not supported in this driver because
+ * they are seen on ISA bus.
+ */
+
+static int smsctemp_match(device_t, cfdata_t, void *);
+static void smsctemp_attach(device_t, device_t, void *);
+static uint8_t smsctemp_readreg(struct smsctemp_sc *, int);
+static void smsctemp_writereg(struct smsctemp_sc *, int, int);
+static void smsctemp_sensors_setup(struct smsctemp_sc *,
+    struct smsctemp_sensor *);
+static void smsctemp_refresh_volt(struct smsctemp_sc *, envsys_data_t *);
+static void smsctemp_refresh_temp(struct smsctemp_sc *, envsys_data_t *);
+static void smsctemp_refresh(struct sysmon_envsys *, envsys_data_t *);
+
+CFATTACH_DECL_NEW(smsctemp, sizeof(struct smsctemp_sc),
+    smsctemp_match, smsctemp_attach, NULL, NULL);
+
+
+static struct smsctemp_sensor smsctemp_lpc47m192[] = {
+       {
+               .desc = "+2.5V",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x20,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =   13000,
+               .vmax = 3320000
+       },
+       {
+               .desc = "Vccp",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x21,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =   12000,
+               .vmax = 2988000
+       },
+       {
+               .desc = "+3.3V",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x22,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =   17000,
+               .vmax = 4383000
+       },
+       {
+               .desc = "+5V",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x23,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =   26000,
+               .vmax = 6640000
+       },
+       {
+               .desc = "+12V",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x24,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =    62000,
+               .vmax = 15938000
+       },
+       {
+               .desc = "Vcc",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x25,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =   17000,
+               .vmax = 4383000
+       },
+       {
+               .desc = "+1.5V",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x50,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =    8000,
+               .vmax = 1992000
+       },
+       {
+               .desc = "+1.8V",
+               .type = ENVSYS_SVOLTS_DC,
+               .reg = 0x51,
+               .refresh = smsctemp_refresh_volt,
+               .vmin =    9000,
+               .vmax = 2391000
+       },
+       {
+               .desc = "Remote Temp1",
+               .type = ENVSYS_STEMP,
+               .reg = 0x26,
+               .refresh = smsctemp_refresh_temp,
+               .vmin = 0,
+               .vmax = 0
+       },
+       {
+               .desc = "Ambient Temp",
+               .type = ENVSYS_STEMP,
+               .reg = 0x27,
+               .refresh = smsctemp_refresh_temp,
+               .vmin = 0,
+               .vmax = 0
+       },
+       {
+               .desc = "Remote Temp2",
+               .type = ENVSYS_STEMP,
+               .reg = 0x52,
+               .refresh = smsctemp_refresh_temp,
+               .vmax = 0,
+               .vmin = 0
+       },
+
+       { .desc = NULL }
+};
+
+
+static int
+smsctemp_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct i2c_attach_args *ia = aux;
+       uint8_t cmd, cid, rev;
+
+       /* Must supply an address */
+       if (ia->ia_addr < 1)
+               return 0;
+
+       iic_acquire_bus(ia->ia_tag, 0);
+
+       cmd = SMSCTEMP_REG_COMPANY;
+       if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP,
+           ia->ia_addr, &cmd, sizeof cmd, &cid, sizeof cid, 0)) {
+               iic_release_bus(ia->ia_tag, 0);
+               return 0;
+       }
+       cmd = SMSCTEMP_REG_STEPPING;
+       if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP,
+           ia->ia_addr, &cmd, sizeof cmd, &rev, sizeof rev, 0)) {
+               iic_release_bus(ia->ia_tag, 0);
+               return 0;
+       }
+
+       if ( cid != SMSC_CID_47M192 || rev != SMSC_REV_47M192) {
+               iic_release_bus(ia->ia_tag, 0);
+               return 0;
+       }
+
+       iic_release_bus(ia->ia_tag, 0);
+       return 1;
+}
+
+
+static void
+smsctemp_attach(device_t parent, device_t self, void *aux)
+{
+       struct smsctemp_sc *sc = device_private(self);
+       struct i2c_attach_args *ia = aux;
+       uint8_t cid, rev;
+       int i;
+
+       sc->sc_dev = self;
+       sc->sc_tag = ia->ia_tag;
+       sc->sc_addr = ia->ia_addr;
+       sc->smsctemp_readreg = smsctemp_readreg;
+       sc->smsctemp_writereg = smsctemp_writereg;
+       sc->smsctemp_sensors = NULL;
+
+       cid = sc->smsctemp_readreg(sc, SMSCTEMP_REG_COMPANY);
+       rev = sc->smsctemp_readreg(sc, SMSCTEMP_REG_STEPPING);
+       switch (cid) {
+       case SMSC_CID_47M192:
+               if (rev == SMSC_REV_47M192) {
+                       smsctemp_sensors_setup(sc, smsctemp_lpc47m192);
+                       aprint_normal(": LPC47M192 hardware monitor\n");
+               }
+               break;
+       default:
+               /* unknown chip */
+               break;
+       }
+
+       if (sc->smsctemp_sensors == NULL) {
+               aprint_normal(": unknown chip: cid 0x%02x rev 0x%02x\n",
+                   cid, rev);
+               return;
+       }
+
+       if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
+               aprint_error_dev(sc->sc_dev,
+                   "unable to create sysmon structure\n");
+               return;
+       }
+
+       for (i = 0; i < sc->numsensors; i++) {
+               if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sensors[i])) {
+                       aprint_error_dev(sc->sc_dev,
+                           "unable to attach sensor\n");
+                       sysmon_envsys_destroy(sc->sc_sme);
+                       return;
+               }
+       }
+
+       sc->sc_sme->sme_name = device_xname(sc->sc_dev);
+       sc->sc_sme->sme_cookie = sc;
+       sc->sc_sme->sme_refresh = smsctemp_refresh;
+       if (sysmon_envsys_register(sc->sc_sme)) {
+               aprint_error_dev(sc->sc_dev,
+                   "unable to register with sysmon\n");
+               sysmon_envsys_destroy(sc->sc_sme);
+               return;
+       }
+}
+
+
+static uint8_t
+smsctemp_readreg(struct smsctemp_sc *sc, int reg)
+{
+       uint8_t cmd, data;
+
+       iic_acquire_bus(sc->sc_tag, 0);
+
+       cmd = reg;
+       iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
+                sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
+
+       iic_release_bus(sc->sc_tag, 0);
+
+       return data;
+}
+
+static void
+smsctemp_writereg(struct smsctemp_sc *sc, int reg, int val)
+{
+       uint8_t cmd, data;
+
+       iic_acquire_bus(sc->sc_tag, 0);
+
+       cmd = reg;
+       data = val;
+       iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
+                sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
+
+       iic_release_bus(sc->sc_tag, 0);
+}
+
+
+static void
+smsctemp_sensors_setup(struct smsctemp_sc *sc, struct smsctemp_sensor *sens)
+{
+       int i;
+
+       for (i = 0; sens[i].desc; i++) {
+               strlcpy(sc->sensors[i].desc, sens[i].desc,
+                   sizeof(sc->sensors[i].desc));
+               sc->sensors[i].units = sens[i].type;
+               sc->numsensors++;
+       }
+       sc->smsctemp_sensors = sens;
+}
+
+
+static void
+smsctemp_refresh_volt(struct smsctemp_sc *sc, envsys_data_t *edata)
+{
+       struct smsctemp_sensor *sens = &sc->smsctemp_sensors[edata->sensor];
+       int data;
+
+       data = (*sc->smsctemp_readreg)(sc, sens->reg);
+       if (data == 0xff) {
+               edata->state = ENVSYS_SINVALID;
+       } else {
+               edata->value_cur =
+                   (sens->vmax - sens->vmin)/255 * data + sens->vmin;
+               edata->state = ENVSYS_SVALID;
+       }
+}
+
+static void
+smsctemp_refresh_temp(struct smsctemp_sc *sc, envsys_data_t *edata)
+{
+       struct smsctemp_sensor *sens = &sc->smsctemp_sensors[edata->sensor];
+       int data;
+
+       data = (*sc->smsctemp_readreg)(sc, sens->reg);
+       if (data == 0xff) {
+               edata->state = ENVSYS_SINVALID;
+       } else {
+               /* convert data(degC) to uK */
+               edata->value_cur = 273150000 + 1000000 * data;
+               edata->state = ENVSYS_SVALID;
+       }
+}
+
+static void
+smsctemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
+{
+       struct smsctemp_sc *sc = sme->sme_cookie;
+
+       sc->smsctemp_sensors[edata->sensor].refresh(sc, edata);
+}
--- /dev/null   2009-01-04 00:00:53.000000000 +0900
+++ sys/dev/i2c/smsctempvar.h   2009-01-17 14:08:15.000000000 +0900
@@ -0,0 +1,62 @@
+/* $NetBSD$ */
+
+/*
+ * 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.
+ */
+
+#ifndef _DEV_I2C_SMSCTEMPVAR_H_
+#define _DEV_I2C_SMSCTEMPVAR_H_
+
+#define SMSCTEMP_REG_COMPANY   0x3e
+#define SMSCTEMP_REG_STEPPING  0x3f
+#define SMSCTEMP_REG_CONFIG    0x40
+
+#define SMSC_CID_47M192                0x55    /* LPC47M192/LPC47M997 */
+#define SMSC_REV_47M192                0x20
+
+#define SMSCTEMP_NUM_SENSORS   12
+
+
+struct smsctemp_sc {
+       device_t        sc_dev;
+       i2c_tag_t       sc_tag;
+       i2c_addr_t      sc_addr;
+
+       uint8_t (*smsctemp_readreg)(struct smsctemp_sc *, int);
+       void    (*smsctemp_writereg)(struct smsctemp_sc *, int, int);
+
+       struct sysmon_envsys *sc_sme;
+       envsys_data_t sensors[SMSCTEMP_NUM_SENSORS];
+       int             numsensors;
+       struct smsctemp_sensor *smsctemp_sensors;
+};
+
+struct smsctemp_sensor {
+       const char      *desc;
+       enum envsys_units type;
+       uint8_t         reg;
+       void            (*refresh)(struct smsctemp_sc *, envsys_data_t *);
+       int             vmin;           /* value in uV for 0x00 */
+       int             vmax;           /* value in uV for 0xff */
+};
+
+#endif /* _DEV_I2C_SMSCTEMPVAR_H_ */

-- 
 <tkhr.hash%gmail.com@localhost>



Home | Main Index | Thread Index | Old Index