Index: ad7418.c =================================================================== RCS file: ad7418.c diff -N ad7418.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ad7418.c 26 Feb 2007 21:40:49 -0000 @@ -0,0 +1,234 @@ +/*- + * Copyright (c) 2006 Sam Leffler. 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 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 +__FBSDID("$FreeBSD$"); +/* + * Analog Devices AD7418 chip sitting on the I2C bus. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "iicbus_if.h" + +#define IIC_M_WR 0 /* write operation */ + +#define AD7418_ADDR 0x50 /* slave address */ + +#define AD7418_TEMP 0 /* Temperature Value (r/o) */ +#define AD7418_CONF 1 /* Config Register (r/w) */ +#define AD7418_CONF_SHUTDOWN 0x01 +#define AD7418_CONF_CHAN 0xe0 /* channel select mask */ +#define AD7418_CHAN_TEMP 0x00 /* temperature channel */ +#define AD7418_CHAN_VOLT 0x80 /* voltage channel */ +#define AD7418_THYST 2 /* Thyst Setpoint (r/o) */ +#define AD7418_TOTI 3 /* Toti Setpoint */ +#define AD7418_VOLT 4 /* ADC aka Voltage (r/o) */ +#define AD7418_CONF2 5 /* Config2 Register (r/w) */ + +struct ad7418_softc { + device_t sc_dev; + struct mtx sc_mtx; + int sc_curchan; /* current channel */ + int sc_curtemp; + int sc_curvolt; + int sc_lastupdate; /* in ticks */ +}; + +static void ad7418_update(struct ad7418_softc *); +static int ad7418_read_1(device_t dev, int reg); +static int ad7418_write_1(device_t dev, int reg, int v); + +static int +ad7418_probe(device_t dev) +{ + /* XXX really probe? */ + device_set_desc(dev, "Analog Devices AD7418 ADC"); + return (0); +} + +static int +ad7418_sysctl_temp(SYSCTL_HANDLER_ARGS) +{ + struct ad7418_softc *sc = arg1; + int temp; + + ad7418_update(sc); + temp = (sc->sc_curtemp / 64) * 25; + return sysctl_handle_int(oidp, &temp, 0, req); +} + +static int +ad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS) +{ + struct ad7418_softc *sc = arg1; + int volt; + + ad7418_update(sc); + volt = (sc->sc_curvolt >> 6) * 564 / 10; + return sysctl_handle_int(oidp, &volt, 0, req); +} + +static int +ad7418_attach(device_t dev) +{ + struct ad7418_softc *sc = device_get_softc(dev); + struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev); + struct sysctl_oid *tree = device_get_sysctl_tree(dev); + int conf; + + sc->sc_dev = dev; + mtx_init(&sc->sc_mtx, "ad7418", "ad7418", MTX_DEF); + + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0, + ad7418_sysctl_temp, "I", "operating temperature"); + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "volt", CTLTYPE_INT | CTLFLAG_RD, sc, 0, + ad7418_sysctl_voltage, "I", "input voltage"); + + /* enable chip if configured in shutdown mode */ + conf = ad7418_read_1(dev, AD7418_CONF); + if (conf >= 0 && (conf & AD7418_CONF_SHUTDOWN)) + ad7418_write_1(dev, AD7418_CONF, conf &~ AD7418_CONF_SHUTDOWN); + + return (0); +} + +static int +ad7418_read_1(device_t dev, int reg) +{ + uint8_t addr = reg; + uint8_t data[1]; + struct iic_msg msgs[2] = { + { AD7418_ADDR, IIC_M_WR, 1, &addr }, + { AD7418_ADDR, IIC_M_RD, 1, data }, + }; + return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0]; +} + +static int +ad7418_write_1(device_t dev, int reg, int v) +{ + /* NB: register pointer precedes actual data */ + uint8_t data[2]; + struct iic_msg msgs[1] = { + { AD7418_ADDR, IIC_M_WR, 2, data }, + }; + data[0] = reg; + data[1] = v & 0xff; + return iicbus_transfer(dev, msgs, 1); +} + +static void +ad7418_set_channel(struct ad7418_softc *sc, int chan) +{ + if (sc->sc_curchan == chan) + return; + ad7418_write_1(sc->sc_dev, AD7418_CONF, + (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan); + sc->sc_curchan = chan; +#if 0 + /* + * NB: Linux driver delays here but chip data sheet + * says nothing and things appear to work fine w/o + * a delay on channel change. If this is enabled + * be sure to account for losing the mutex below + * in ad7418_update. + */ + mtx_assert(&sc->sc_mtx, MA_OWNED); + /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */ + msleep(sc, &sc->sc_mtx, 0, "ad7418", 1); +#endif +} + +static int +ad7418_read_2(device_t dev, int reg) +{ + uint8_t addr = reg; + uint8_t data[2]; + struct iic_msg msgs[2] = { + { AD7418_ADDR, IIC_M_WR, 1, &addr }, + { AD7418_ADDR, IIC_M_RD, 2, data }, + }; + /* NB: D15..D8 precede D7..D0 per data sheet (Fig 12) */ + return iicbus_transfer(dev, msgs, 2) != 0 ? + -1 : ((data[0] << 8) | data[1]); +} + +static void +ad7418_update(struct ad7418_softc *sc) +{ + int v; + + mtx_lock(&sc->sc_mtx); + /* NB: no point in updating any faster than the chip */ + if (ticks - sc->sc_lastupdate > hz) { + ad7418_set_channel(sc, AD7418_CHAN_TEMP); + v = ad7418_read_2(sc->sc_dev, AD7418_TEMP); + if (v >= 0) + sc->sc_curtemp = v; + ad7418_set_channel(sc, AD7418_CHAN_VOLT); + v = ad7418_read_2(sc->sc_dev, AD7418_VOLT); + if (v >= 0) + sc->sc_curvolt = v; + sc->sc_lastupdate = ticks; + } + mtx_unlock(&sc->sc_mtx); +} + +static device_method_t ad7418_methods[] = { + DEVMETHOD(device_probe, ad7418_probe), + DEVMETHOD(device_attach, ad7418_attach), + + {0, 0}, +}; + +static driver_t ad7418_driver = { + "ad7418", + ad7418_methods, + sizeof(struct ad7418_softc), +}; +static devclass_t ad7418_devclass; + +DRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0); +MODULE_VERSION(ad7418, 1); +MODULE_DEPEND(ad7418, iicbus, 1, 1, 1); Index: ds1672.c =================================================================== RCS file: ds1672.c diff -N ds1672.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ds1672.c 26 Feb 2007 21:40:58 -0000 @@ -0,0 +1,142 @@ +/*- + * Copyright (c) 2006 Sam Leffler. 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 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 +__FBSDID("$FreeBSD$"); +/* + * Dallas Semiconductor DS1672 RTC sitting on the I2C bus. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "iicbus_if.h" +#include "clock_if.h" + +#define IIC_M_WR 0 /* write operation */ + +#define DS1672_ADDR 0xd0 /* slave address */ + +#define DS1672_COUNTER 0 /* counter (bytes 0-3) */ +#define DS1672_CTRL 4 /* control (1 byte) */ +#define DS1672_TRICKLE 5 /* trickle charger (1 byte) */ + +#define NANOSEC 1000000000 + +struct ds1672_softc { + device_t sc_dev; +}; + +static int +ds1672_probe(device_t dev) +{ + /* XXX really probe? */ + device_set_desc(dev, "Dallas Semiconductor DS1672 RTC"); + return (0); +} + +static int +ds1672_attach(device_t dev) +{ + struct ds1672_softc *sc = device_get_softc(dev); + + sc->sc_dev = dev; + + clock_register(dev, 1000); + return (0); +} + +static int +ds1672_gettime(device_t dev, struct timespec *ts) +{ + uint8_t addr[1] = { DS1672_COUNTER }; + uint8_t secs[4]; + struct iic_msg msgs[2] = { + { DS1672_ADDR, IIC_M_WR, 1, addr }, + { DS1672_ADDR, IIC_M_RD, 4, secs }, + }; + int error; + + error = iicbus_transfer(dev, msgs, 2); + if (error == 0) { + /* counter has seconds since epoch */ + ts->tv_sec = (secs[3] << 24) | (secs[2] << 16) + | (secs[1] << 8) | (secs[0] << 0); + ts->tv_nsec = NANOSEC / 2; + } + return error; +} + +static int +ds1672_settime(device_t dev, struct timespec *ts) +{ + /* NB: register pointer precedes actual data */ + uint8_t data[5] = { DS1672_COUNTER }; + struct iic_msg msgs[1] = { + { DS1672_ADDR, IIC_M_WR, 5, data }, + }; + + data[1] = (ts->tv_sec >> 0) & 0xff; + data[2] = (ts->tv_sec >> 8) & 0xff; + data[3] = (ts->tv_sec >> 16) & 0xff; + data[4] = (ts->tv_sec >> 24) & 0xff; + + return iicbus_transfer(dev, msgs, 1); +} + +static device_method_t ds1672_methods[] = { + DEVMETHOD(device_probe, ds1672_probe), + DEVMETHOD(device_attach, ds1672_attach), + + DEVMETHOD(clock_gettime, ds1672_gettime), + DEVMETHOD(clock_settime, ds1672_settime), + + {0, 0}, +}; + +static driver_t ds1672_driver = { + "ds1672", + ds1672_methods, + sizeof(struct ds1672_softc), +}; +static devclass_t ds1672_devclass; + +DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0); +MODULE_VERSION(ds1672, 1); +MODULE_DEPEND(ds1672, iicbus, 1, 1, 1); Index: iic.h =================================================================== RCS file: /cognet/ncvs/src/sys/dev/iicbus/iic.h,v retrieving revision 1.3 diff -u -p -r1.3 iic.h --- iic.h 28 Aug 1999 00:44:16 -0000 1.3 +++ iic.h 26 Feb 2007 21:42:55 -0000 @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/sys/dev/iicbus/iic.h,v 1.3 1999/08/28 00:44:16 peter Exp $ + * $FreeBSD: src/sys/dev/iicbus/iic.h,v 1.5 2006/11/28 06:51:36 imp Exp $ * */ #ifndef __IIC_H @@ -31,6 +31,17 @@ #include +/* Designed to be compatible with linux's struct i2c_msg */ +struct iic_msg +{ + uint16_t slave; + uint16_t flags; +#define IIC_M_WR 0 /* Fake flag for write */ +#define IIC_M_RD 0x0001 /* read vs write */ + uint16_t len; /* msg legnth */ + uint8_t * buf; +}; + struct iiccmd { u_char slave; int count; @@ -38,10 +49,16 @@ struct iiccmd { char *buf; }; +struct iic_rdwr_data { + struct iic_msg *msgs; + uint32_t nmsgs; +}; + #define I2CSTART _IOW('i', 1, struct iiccmd) /* start condition */ #define I2CSTOP _IO('i', 2) /* stop condition */ #define I2CRSTCARD _IOW('i', 3, struct iiccmd) /* reset the card */ #define I2CWRITE _IOW('i', 4, struct iiccmd) /* send data */ #define I2CREAD _IOW('i', 5, struct iiccmd) /* receive data */ +#define I2CRDWR _IOW('i', 6, struct iic_rdwr_data) /* General read/write interface */ #endif Index: iicbb.c =================================================================== RCS file: /cognet/ncvs/src/sys/dev/iicbus/iicbb.c,v retrieving revision 1.13 diff -u -p -r1.13 iicbb.c --- iicbb.c 24 Aug 2003 17:49:13 -0000 1.13 +++ iicbb.c 26 Feb 2007 01:57:59 -0000 @@ -385,6 +385,7 @@ static int iicbb_read(device_t dev, char DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0); DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0); DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, 0, 0); +DRIVER_MODULE(iicbb, ixpiic, iicbb_driver, iicbb_devclass, 0, 0); MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); MODULE_VERSION(iicbb, IICBB_MODVER); Index: iicbus.c =================================================================== RCS file: /cognet/ncvs/src/sys/dev/iicbus/iicbus.c,v retrieving revision 1.20 diff -u -p -r1.20 iicbus.c --- iicbus.c 27 May 2004 13:29:09 -0000 1.20 +++ iicbus.c 26 Feb 2007 01:58:07 -0000 @@ -69,6 +69,8 @@ static device_method_t iicbus_methods[] DEVMETHOD(bus_driver_added, bus_generic_driver_added), DEVMETHOD(bus_print_child, bus_generic_print_child), + DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), + { 0, 0 } }; @@ -143,7 +145,12 @@ iicbus_attach(device_t dev) device_add_child(dev, "ic", -1); device_add_child(dev, "iic", -1); device_add_child(dev, "iicsmb", -1); - + device_add_child(dev, "ds1672", -1); + device_add_child(dev, "ad7418", -1); +#if 0 + /* attach any known device */ + device_add_child(dev, "iic", -1); +#endif bus_generic_attach(dev); return (0); Index: iicbus_if.m =================================================================== RCS file: /cognet/ncvs/src/sys/dev/iicbus/iicbus_if.m,v retrieving revision 1.6 diff -u -p -r1.6 iicbus_if.m --- iicbus_if.m 6 Jan 2005 01:42:47 -0000 1.6 +++ iicbus_if.m 26 Feb 2007 21:42:27 -0000 @@ -27,6 +27,7 @@ # #include +#include INTERFACE iicbus; @@ -90,7 +91,7 @@ METHOD int read { # METHOD int write { device_t dev; - char *buf; + const char *buf; int len; int *bytes; int timeout; @@ -105,3 +106,12 @@ METHOD int reset { u_char addr; u_char *oldaddr; }; + +# +# Generalized Read/Write interface +# +METHOD int transfer { + device_t dev; + struct iic_msg *msgs; + uint32_t nmsgs; +}; Index: iiconf.c =================================================================== RCS file: /cognet/ncvs/src/sys/dev/iicbus/iiconf.c,v retrieving revision 1.14 diff -u -p -r1.14 iiconf.c --- iiconf.c 24 Aug 2003 17:49:13 -0000 1.14 +++ iiconf.c 26 Feb 2007 21:41:49 -0000 @@ -331,3 +331,45 @@ iicbus_block_read(device_t bus, u_char s return (error); } + +/* + * iicbus_trasnfer() + * + * Do an aribtrary number of transfers on the iicbus. We pass these + * raw requests to the bridge driver. If the bridge driver supports + * them directly, then it manages all the details. If not, it can use + * the helper function iicbus_transfer_gen() which will do the + * transfers at a low level. + * + * Pointers passed in as part of iic_msg must be kernel pointers. + * Callers that have user addresses to manage must do so on their own. + */ +int +iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs) +{ + return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs)); +} + +/* + * Generic version of iicbus_transfer that calls the appropriate + * routines to accomplish this. See note above about acceptable + * buffer addresses. + */ +int +iicbus_transfer_gen(device_t bus, struct iic_msg *msgs, uint32_t nmsgs) +{ + int i, error, max, lenread, lenwrote; + + for (i = 0, max = 0; i < nmsgs; i++) + if (max < msgs[i].len) + max = msgs[i].len; + for (i = 0, error = 0; i < nmsgs && error == 0; i++) { + if (msgs[i].flags & IIC_M_RD) + error = iicbus_block_read(bus, msgs[i].slave, + msgs[i].buf, msgs[i].len, &lenread); + else + error = iicbus_block_write(bus, msgs[i].slave, + msgs[i].buf, msgs[i].len, &lenwrote); + } + return (error); +} Index: iiconf.h =================================================================== RCS file: /cognet/ncvs/src/sys/dev/iicbus/iiconf.h,v retrieving revision 1.8 diff -u -p -r1.8 iiconf.h --- iiconf.h 19 Jun 2003 02:50:08 -0000 1.8 +++ iiconf.h 26 Feb 2007 21:43:14 -0000 @@ -29,6 +29,8 @@ #define __IICONF_H #include +#include + #define IICPRI (PZERO+8) /* XXX sleep/wakeup queue priority */ @@ -127,6 +129,10 @@ extern int iicbus_block_read(device_t, u extern u_char iicbus_get_addr(device_t); +/* vectors of iic operations to pass to bridge */ +int iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs); +int iicbus_transfer_gen(device_t bus, struct iic_msg *msgs, uint32_t nmsgs); + #define IICBUS_MODVER 1 #define IICBUS_MINVER 1 #define IICBUS_MAXVER 1