Current-Users archive

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

LM78 driver available on i2c



The attached file will implement an i2c bus attachment for the LM78 and related chips (see 'man 4 lm'). These chips were previously supported only on isa bus.

I don't have any appropriate hardware myself, so I'd really appreciate it if someone could test it out before I commit anything. I also need someone to verify that I haven't broken anything with the original isa attachment code, since I did move the isa bus info out of the primary softc; I also modified the lm_probe() routine to be truly bus-agnostic by replacing bus_space_*() operations with calls to the appropriate lmsc->lm_{read,write}() routines. (This was necessary since the i2c bus doesn't do bus_space_*() stuff as far as I can see.)

----------------------------------------------------------------------
|   Paul Goyette   | PGP DSS Key fingerprint: |  E-mail addresses:   |
| Customer Service | FA29 0E3B 35AF E8AE 6651 |  paul%whooppee.com@localhost   |
| Network Engineer | 0786 F758 55DE 53BA 7731 | pgoyette%juniper.net@localhost |
----------------------------------------------------------------------
Index: sys/conf/files
===================================================================
RCS file: /cvsroot/src/sys/conf/files,v
retrieving revision 1.920
diff -u -p -r1.920 files
--- sys/conf/files      11 Oct 2008 13:40:57 -0000      1.920
+++ sys/conf/files      11 Oct 2008 16:35:29 -0000
@@ -302,6 +302,13 @@ define     audiobell
 device video
 attach video at videobus
 
+# National Semiconductor LM7[89]
+#
+# (included here so files.i2c can define an attachment)
+
+device lm: sysmon_envsys
+file   dev/ic/nslm7x.c                 lm                      needs-flag
+
 # I2C device support
 include "dev/i2c/files.i2c"
 
@@ -811,11 +818,6 @@ file       dev/ic/attimer.c                attimer         
        needs-f
 device clmpcc: tty
 file   dev/ic/clmpcc.c                 clmpcc                  needs-flag
 
-# National Semiconductor LM7[89]
-#
-device lm: sysmon_envsys
-file   dev/ic/nslm7x.c                 lm                      needs-flag
-
 # Abit uGuru
 #
 device ug: sysmon_envsys
Index: sys/dev/i2c/lm_i2c.c
===================================================================
RCS file: sys/dev/i2c/lm_i2c.c
diff -N sys/dev/i2c/lm_i2c.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ sys/dev/i2c/lm_i2c.c        11 Oct 2008 16:35:29 -0000
@@ -0,0 +1,143 @@
+/*     $NetBSD$        */
+
+/*-
+ * Copyright (c) 2000 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Bill Squier.
+ *
+ * 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/kernel.h>
+#include <sys/device.h>
+#include <sys/conf.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#include <dev/sysmon/sysmonvar.h>
+
+#include <dev/ic/nslm7xvar.h>
+
+int    lm_i2c_match(device_t, cfdata_t, void *);
+void   lm_i2c_attach(device_t, device_t, void *);
+int    lm_i2c_detach(device_t, int);
+
+uint8_t lm_i2c_readreg(struct lm_softc *, int);
+void   lm_i2c_writereg(struct lm_softc *, int, int);
+
+struct lm_i2c_softc {
+       struct lm_softc sc_lmsc;
+       i2c_tag_t sc_tag;
+       i2c_addr_t sc_addr;
+};
+
+CFATTACH_DECL_NEW(lm_iic, sizeof(struct lm_i2c_softc),
+    lm_i2c_match, lm_i2c_attach, lm_i2c_detach, NULL);
+
+int
+lm_i2c_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct i2c_attach_args *ia = aux;
+       int rv = 0;
+       struct lm_i2c_softc sc;
+
+       /* Must supply an address */
+       if (ia->ia_addr < 1)
+               return 0;
+
+       /* Bus independent probe */
+       sc.sc_lmsc.lm_writereg = lm_i2c_writereg;
+       sc.sc_lmsc.lm_readreg = lm_i2c_readreg;
+       sc.sc_tag = ia->ia_tag;
+       sc.sc_addr = ia->ia_addr;
+       rv = lm_probe(&sc.sc_lmsc);
+
+       return rv;
+}
+
+
+void
+lm_i2c_attach(device_t parent, device_t self, void *aux)
+{
+       struct lm_i2c_softc *sc = device_private(self);
+       struct i2c_attach_args *ia = aux;
+
+       sc->sc_tag = ia->ia_tag;
+       sc->sc_addr = ia->ia_addr;
+
+       /* Bus-independent attachment */
+       sc->sc_lmsc.sc_dev = self;
+       sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
+       sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
+
+       lm_attach(&sc->sc_lmsc);
+}
+
+int
+lm_i2c_detach(device_t self, int flags)
+{
+       struct lm_i2c_softc *sc = device_private(self);
+
+       lm_detach(&sc->sc_lmsc);
+       return 0;
+}
+
+uint8_t
+lm_i2c_readreg(struct lm_softc *lmsc, int reg)
+{
+       struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
+       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;
+}
+
+
+void
+lm_i2c_writereg(struct lm_softc *lmsc, int reg, int val)
+{
+       struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
+       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);
+}
Index: sys/dev/i2c/files.i2c
===================================================================
RCS file: /cvsroot/src/sys/dev/i2c/files.i2c,v
retrieving revision 1.20
diff -u -p -r1.20 files.i2c
--- sys/dev/i2c/files.i2c       7 Oct 2008 19:32:30 -0000       1.20
+++ sys/dev/i2c/files.i2c       11 Oct 2008 16:35:29 -0000
@@ -49,6 +49,10 @@ device       lmtemp: sysmon_envsys
 attach lmtemp at iic
 file   dev/i2c/lm75.c                          lmtemp
 
+# National Semiconductor LM78 temp sensor/fan controller
+attach lm at iic with lm_iic
+file   dev/i2c/lm_i2c.c                        lm_iic
+
 # Dallas DS1307 Real Time Clock
 device dsrtc
 attach dsrtc at iic
Index: sys/dev/ic/nslm7x.c
===================================================================
RCS file: /cvsroot/src/sys/dev/ic/nslm7x.c,v
retrieving revision 1.47
diff -u -p -r1.47 nslm7x.c
--- sys/dev/ic/nslm7x.c 28 Apr 2008 20:23:50 -0000      1.47
+++ sys/dev/ic/nslm7x.c 11 Oct 2008 16:35:29 -0000
@@ -1625,22 +1625,25 @@ lm_generic_banksel(struct lm_softc *lmsc
 
 /*
  * bus independent probe
+ *
+ * prerequisites:  lmsc contains valid lm_{read,write}reg() routines
+ * and associated bus access data is present in attachment's softc
  */
 int
-lm_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
+lm_probe(struct lm_softc *lmsc)
 {
        uint8_t cr;
        int rv;
 
        /* Check for some power-on defaults */
-       bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
+       (*lmsc->lm_writereg)(lmsc, LMC_ADDR, LMD_CONFIG);
 
        /* Perform LM78 reset */
-       /* bus_space_write_1(iot, ioh, LMC_DATA, 0x80); */
+       /*(*lmsc->lm_writereg)(lmsc, LMC_DATA, 0x80); */
 
        /* XXX - Why do I have to reselect the register? */
-       bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
-       cr = bus_space_read_1(iot, ioh, LMC_DATA);
+       (*lmsc->lm_writereg)(lmsc, LMC_ADDR, LMD_CONFIG);
+       cr = (*lmsc->lm_readreg)(lmsc, LMC_DATA);
 
        /* XXX - spec says *only* 0x08! */
        if ((cr == 0x08) || (cr == 0x01) || (cr == 0x03))
@@ -1653,10 +1656,6 @@ lm_probe(bus_space_tag_t iot, bus_space_
        return rv;
 }
 
-
-/*
- * pre:  lmsc contains valid busspace tag and handle
- */
 void
 lm_attach(struct lm_softc *lmsc)
 {
Index: sys/dev/ic/nslm7xvar.h
===================================================================
RCS file: /cvsroot/src/sys/dev/ic/nslm7xvar.h,v
retrieving revision 1.25
diff -u -p -r1.25 nslm7xvar.h
--- sys/dev/ic/nslm7xvar.h      28 Apr 2008 20:23:51 -0000      1.25
+++ sys/dev/ic/nslm7xvar.h      11 Oct 2008 16:35:29 -0000
@@ -152,8 +152,6 @@
 
 struct lm_softc {
        device_t sc_dev;
-       bus_space_tag_t lm_iot;
-       bus_space_handle_t lm_ioh;
 
        callout_t sc_callout;
 
@@ -182,6 +180,6 @@ struct lm_sensor {
 
 void   lm_attach(struct lm_softc *);
 void   lm_detach(struct lm_softc *);
-int    lm_probe(bus_space_tag_t, bus_space_handle_t);
+int    lm_probe(struct lm_softc *);
 
 #endif /* _DEV_ISA_NSLM7XVAR_H_ */
Index: sys/dev/isa/lm_isa.c
===================================================================
RCS file: /cvsroot/src/sys/dev/isa/lm_isa.c,v
retrieving revision 1.20
diff -u -p -r1.20 lm_isa.c
--- sys/dev/isa/lm_isa.c        28 Apr 2008 20:23:52 -0000      1.20
+++ sys/dev/isa/lm_isa.c        11 Oct 2008 16:35:29 -0000
@@ -54,7 +54,13 @@ int  lm_isa_detach(device_t, int);
 uint8_t lm_isa_readreg(struct lm_softc *, int);
 void   lm_isa_writereg(struct lm_softc *, int, int);
 
-CFATTACH_DECL_NEW(lm_isa, sizeof(struct lm_softc),
+struct lm_isa_softc {
+       struct lm_softc lmsc;
+       bus_space_tag_t lm_iot;
+       bus_space_handle_t lm_ioh;
+};
+
+CFATTACH_DECL_NEW(lm_isa, sizeof(struct lm_isa_softc),
     lm_isa_match, lm_isa_attach, lm_isa_detach, NULL);
 
 int
@@ -62,6 +68,7 @@ lm_isa_match(device_t parent, cfdata_t m
 {
        bus_space_handle_t ioh;
        struct isa_attach_args *ia = aux;
+       struct lm_isa_softc sc;
        int rv;
 
        /* Must supply an address */
@@ -79,7 +86,11 @@ lm_isa_match(device_t parent, cfdata_t m
 
 
        /* Bus independent probe */
-       rv = lm_probe(ia->ia_iot, ioh);
+       sc.lm_iot = ia->ia_iot;
+       sc.lm_ioh = ioh;
+       sc.lmsc.lm_writereg = lm_isa_writereg;
+       sc.lmsc.lm_readreg = lm_isa_readreg;
+       rv = lm_probe(&sc.lmsc);
 
        bus_space_unmap(ia->ia_iot, ioh, 8);
 
@@ -99,46 +110,50 @@ lm_isa_match(device_t parent, cfdata_t m
 void
 lm_isa_attach(device_t parent, device_t self, void *aux)
 {
-       struct lm_softc *lmsc = device_private(self);
+       struct lm_isa_softc *sc = device_private(self);
        struct isa_attach_args *ia = aux;
 
-       lmsc->lm_iot = ia->ia_iot;
+       sc->lm_iot = ia->ia_iot;
 
        if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 8, 0,
-           &lmsc->lm_ioh)) {
+           &sc->lm_ioh)) {
                aprint_error(": can't map i/o space\n");
                return;
        }
 
        /* Bus-independent attachment */
-       lmsc->sc_dev = self;
-       lmsc->lm_writereg = lm_isa_writereg;
-       lmsc->lm_readreg = lm_isa_readreg;
+       sc->lmsc.sc_dev = self;
+       sc->lmsc.lm_writereg = lm_isa_writereg;
+       sc->lmsc.lm_readreg = lm_isa_readreg;
 
-       lm_attach(lmsc);
+       lm_attach(&sc->sc_lmsc);
 }
 
 int
 lm_isa_detach(device_t self, int flags)
 {
-       struct lm_softc *lmsc = device_private(self);
+       struct lm_isa_softc *sc = device_private(self);
 
-       lm_detach(lmsc);
-       bus_space_unmap(lmsc->lm_iot, lmsc->lm_ioh, 8);
+       lm_detach(&sc->sc_lmsc);
+       bus_space_unmap(sc->lm_iot, sc->lm_ioh, 8);
        return 0;
 }
 
 uint8_t
-lm_isa_readreg(struct lm_softc *sc, int reg)
+lm_isa_readreg(struct lm_softc *lmsc, int reg)
 {
+       struct lm_isa_softc *sc = (struct lm_isa_softc *)lmsc;
+
        bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
        return bus_space_read_1(sc->lm_iot, sc->lm_ioh, LMC_DATA);
 }
 
 
 void
-lm_isa_writereg(struct lm_softc *sc, int reg, int val)
+lm_isa_writereg(struct lm_softc *lmsc, int reg, int val)
 {
+       struct lm_isa_softc *sc = (struct lm_isa_softc *)lmsc;
+
        bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
        bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_DATA, val);
 }


Home | Main Index | Thread Index | Old Index