2 * Copyright (c) 2006 Sam Leffler. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 * Analog Devices AD7418 chip sitting on the I2C bus.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/resource.h>
38 #include <sys/sysctl.h>
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/cpufunc.h>
44 #include <machine/frame.h>
45 #include <machine/resource.h>
46 #include <machine/intr.h>
48 #include <dev/iicbus/iiconf.h>
50 #include "iicbus_if.h"
52 #define IIC_M_WR 0 /* write operation */
54 #define AD7418_ADDR 0x50 /* slave address */
56 #define AD7418_TEMP 0 /* Temperature Value (r/o) */
57 #define AD7418_CONF 1 /* Config Register (r/w) */
58 #define AD7418_CONF_SHUTDOWN 0x01
59 #define AD7418_CONF_CHAN 0xe0 /* channel select mask */
60 #define AD7418_CHAN_TEMP 0x00 /* temperature channel */
61 #define AD7418_CHAN_VOLT 0x80 /* voltage channel */
62 #define AD7418_THYST 2 /* Thyst Setpoint (r/o) */
63 #define AD7418_TOTI 3 /* Toti Setpoint */
64 #define AD7418_VOLT 4 /* ADC aka Voltage (r/o) */
65 #define AD7418_CONF2 5 /* Config2 Register (r/w) */
70 int sc_curchan; /* current channel */
73 int sc_lastupdate; /* in ticks */
76 static void ad7418_update(struct ad7418_softc *);
77 static int ad7418_read_1(device_t dev, int reg);
78 static int ad7418_write_1(device_t dev, int reg, int v);
81 ad7418_probe(device_t dev)
83 /* XXX really probe? */
84 device_set_desc(dev, "Analog Devices AD7418 ADC");
85 return (BUS_PROBE_NOWILDCARD);
89 ad7418_sysctl_temp(SYSCTL_HANDLER_ARGS)
91 struct ad7418_softc *sc = arg1;
94 sx_xlock(&sc->sc_lock);
96 temp = (sc->sc_curtemp / 64) * 25;
97 sx_xunlock(&sc->sc_lock);
98 return sysctl_handle_int(oidp, &temp, 0, req);
102 ad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS)
104 struct ad7418_softc *sc = arg1;
107 sx_xlock(&sc->sc_lock);
109 volt = (sc->sc_curvolt >> 6) * 564 / 10;
110 sx_xunlock(&sc->sc_lock);
111 return sysctl_handle_int(oidp, &volt, 0, req);
115 ad7418_attach(device_t dev)
117 struct ad7418_softc *sc = device_get_softc(dev);
118 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
119 struct sysctl_oid *tree = device_get_sysctl_tree(dev);
123 sc->sc_lastupdate = ticks - hz;
125 sx_init(&sc->sc_lock, "ad7418");
127 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
128 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
129 ad7418_sysctl_temp, "I", "operating temperature");
130 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
131 "volt", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
132 ad7418_sysctl_voltage, "I", "input voltage");
134 /* enable chip if configured in shutdown mode */
135 conf = ad7418_read_1(dev, AD7418_CONF);
136 if (conf >= 0 && (conf & AD7418_CONF_SHUTDOWN))
137 ad7418_write_1(dev, AD7418_CONF, conf &~ AD7418_CONF_SHUTDOWN);
143 ad7418_read_1(device_t dev, int reg)
147 struct iic_msg msgs[2] = {
148 { AD7418_ADDR, IIC_M_WR, 1, &addr },
149 { AD7418_ADDR, IIC_M_RD, 1, data },
151 return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
155 ad7418_write_1(device_t dev, int reg, int v)
157 /* NB: register pointer precedes actual data */
159 struct iic_msg msgs[1] = {
160 { AD7418_ADDR, IIC_M_WR, 2, data },
164 return iicbus_transfer(dev, msgs, 1);
168 ad7418_set_channel(struct ad7418_softc *sc, int chan)
170 if (sc->sc_curchan == chan)
172 ad7418_write_1(sc->sc_dev, AD7418_CONF,
173 (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan);
174 sc->sc_curchan = chan;
177 * NB: Linux driver delays here but chip data sheet
178 * says nothing and things appear to work fine w/o
179 * a delay on channel change.
181 /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */
182 tsleep(sc, 0, "ad7418", hz/1000);
187 ad7418_read_2(device_t dev, int reg)
191 struct iic_msg msgs[2] = {
192 { AD7418_ADDR, IIC_M_WR, 1, &addr },
193 { AD7418_ADDR, IIC_M_RD, 2, data },
195 /* NB: D15..D8 precede D7..D0 per data sheet (Fig 12) */
196 return iicbus_transfer(dev, msgs, 2) != 0 ?
197 -1 : ((data[0] << 8) | data[1]);
201 ad7418_update(struct ad7418_softc *sc)
205 sx_assert(&sc->sc_lock, SA_XLOCKED);
206 /* NB: no point in updating any faster than the chip */
207 if (ticks - sc->sc_lastupdate > hz) {
208 ad7418_set_channel(sc, AD7418_CHAN_TEMP);
209 v = ad7418_read_2(sc->sc_dev, AD7418_TEMP);
212 ad7418_set_channel(sc, AD7418_CHAN_VOLT);
213 v = ad7418_read_2(sc->sc_dev, AD7418_VOLT);
216 sc->sc_lastupdate = ticks;
220 static device_method_t ad7418_methods[] = {
221 DEVMETHOD(device_probe, ad7418_probe),
222 DEVMETHOD(device_attach, ad7418_attach),
227 static driver_t ad7418_driver = {
230 sizeof(struct ad7418_softc),
232 static devclass_t ad7418_devclass;
234 DRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0);
235 MODULE_VERSION(ad7418, 1);
236 MODULE_DEPEND(ad7418, iicbus, 1, 1, 1);