ad7418.c (164419) | ad7418.c (181305) |
---|---|
1/*- 2 * Copyright (c) 2006 Sam Leffler. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. --- 9 unchanged lines hidden (view full) --- 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. 23 */ 24 25#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2006 Sam Leffler. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. --- 9 unchanged lines hidden (view full) --- 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. 23 */ 24 25#include <sys/cdefs.h> |
26__FBSDID("$FreeBSD: head/sys/dev/iicbus/ad7418.c 164419 2006-11-19 23:39:54Z sam $"); | 26__FBSDID("$FreeBSD: head/sys/dev/iicbus/ad7418.c 181305 2008-08-04 21:14:24Z jhb $"); |
27/* 28 * Analog Devices AD7418 chip sitting on the I2C bus. 29 */ 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/lock.h> 34#include <sys/module.h> | 27/* 28 * Analog Devices AD7418 chip sitting on the I2C bus. 29 */ 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/lock.h> 34#include <sys/module.h> |
35#include <sys/mutex.h> | |
36#include <sys/bus.h> 37#include <sys/resource.h> 38#include <sys/rman.h> 39#include <sys/sysctl.h> | 35#include <sys/bus.h> 36#include <sys/resource.h> 37#include <sys/rman.h> 38#include <sys/sysctl.h> |
39#include <sys/sx.h> |
|
40 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> 47 --- 13 unchanged lines hidden (view full) --- 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) */ 66 67struct ad7418_softc { 68 device_t sc_dev; | 40 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> 47 --- 13 unchanged lines hidden (view full) --- 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) */ 66 67struct ad7418_softc { 68 device_t sc_dev; |
69 struct mtx sc_mtx; | 69 struct sx sc_lock; |
70 int sc_curchan; /* current channel */ 71 int sc_curtemp; 72 int sc_curvolt; 73 int sc_lastupdate; /* in ticks */ 74}; 75 76static void ad7418_update(struct ad7418_softc *); 77static int ad7418_read_1(device_t dev, int reg); --- 8 unchanged lines hidden (view full) --- 86} 87 88static int 89ad7418_sysctl_temp(SYSCTL_HANDLER_ARGS) 90{ 91 struct ad7418_softc *sc = arg1; 92 int temp; 93 | 70 int sc_curchan; /* current channel */ 71 int sc_curtemp; 72 int sc_curvolt; 73 int sc_lastupdate; /* in ticks */ 74}; 75 76static void ad7418_update(struct ad7418_softc *); 77static int ad7418_read_1(device_t dev, int reg); --- 8 unchanged lines hidden (view full) --- 86} 87 88static int 89ad7418_sysctl_temp(SYSCTL_HANDLER_ARGS) 90{ 91 struct ad7418_softc *sc = arg1; 92 int temp; 93 |
94 sx_xlock(&sc->sc_lock); |
|
94 ad7418_update(sc); 95 temp = (sc->sc_curtemp / 64) * 25; | 95 ad7418_update(sc); 96 temp = (sc->sc_curtemp / 64) * 25; |
97 sx_xunlock(&sc->sc_lock); |
|
96 return sysctl_handle_int(oidp, &temp, 0, req); 97} 98 99static int 100ad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS) 101{ 102 struct ad7418_softc *sc = arg1; 103 int volt; 104 | 98 return sysctl_handle_int(oidp, &temp, 0, req); 99} 100 101static int 102ad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS) 103{ 104 struct ad7418_softc *sc = arg1; 105 int volt; 106 |
107 sx_xlock(&sc->sc_lock); |
|
105 ad7418_update(sc); 106 volt = (sc->sc_curvolt >> 6) * 564 / 10; | 108 ad7418_update(sc); 109 volt = (sc->sc_curvolt >> 6) * 564 / 10; |
110 sx_xunlock(&sc->sc_lock); |
|
107 return sysctl_handle_int(oidp, &volt, 0, req); 108} 109 110static int 111ad7418_attach(device_t dev) 112{ 113 struct ad7418_softc *sc = device_get_softc(dev); 114 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev); 115 struct sysctl_oid *tree = device_get_sysctl_tree(dev); 116 int conf; 117 118 sc->sc_dev = dev; | 111 return sysctl_handle_int(oidp, &volt, 0, req); 112} 113 114static int 115ad7418_attach(device_t dev) 116{ 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); 120 int conf; 121 122 sc->sc_dev = dev; |
119 mtx_init(&sc->sc_mtx, "ad7418", "ad7418", MTX_DEF); | 123 sx_init(&sc->sc_mtx, "ad7418"); |
120 121 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 122 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 123 ad7418_sysctl_temp, "I", "operating temperature"); 124 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 125 "volt", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 126 ad7418_sysctl_voltage, "I", "input voltage"); 127 --- 37 unchanged lines hidden (view full) --- 165 return; 166 ad7418_write_1(sc->sc_dev, AD7418_CONF, 167 (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan); 168 sc->sc_curchan = chan; 169#if 0 170 /* 171 * NB: Linux driver delays here but chip data sheet 172 * says nothing and things appear to work fine w/o | 124 125 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 126 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 127 ad7418_sysctl_temp, "I", "operating temperature"); 128 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 129 "volt", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 130 ad7418_sysctl_voltage, "I", "input voltage"); 131 --- 37 unchanged lines hidden (view full) --- 169 return; 170 ad7418_write_1(sc->sc_dev, AD7418_CONF, 171 (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan); 172 sc->sc_curchan = chan; 173#if 0 174 /* 175 * NB: Linux driver delays here but chip data sheet 176 * says nothing and things appear to work fine w/o |
173 * a delay on channel change. If this is enabled 174 * be sure to account for losing the mutex below 175 * in ad7418_update. | 177 * a delay on channel change. |
176 */ | 178 */ |
177 mtx_assert(&sc->sc_mtx, MA_OWNED); | |
178 /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */ | 179 /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */ |
179 msleep(sc, &sc->sc_mtx, 0, "ad7418", 1); | 180 tsleep(sc, 0, "ad7418", hz/1000); |
180#endif 181} 182 183static int 184ad7418_read_2(device_t dev, int reg) 185{ 186 uint8_t addr = reg; 187 uint8_t data[2]; --- 6 unchanged lines hidden (view full) --- 194 -1 : ((data[0] << 8) | data[1]); 195} 196 197static void 198ad7418_update(struct ad7418_softc *sc) 199{ 200 int v; 201 | 181#endif 182} 183 184static int 185ad7418_read_2(device_t dev, int reg) 186{ 187 uint8_t addr = reg; 188 uint8_t data[2]; --- 6 unchanged lines hidden (view full) --- 195 -1 : ((data[0] << 8) | data[1]); 196} 197 198static void 199ad7418_update(struct ad7418_softc *sc) 200{ 201 int v; 202 |
202 mtx_lock(&sc->sc_mtx); | 203 sx_assert(&sc->sc_lock, SA_XLOCKED); |
203 /* NB: no point in updating any faster than the chip */ 204 if (ticks - sc->sc_lastupdate > hz) { 205 ad7418_set_channel(sc, AD7418_CHAN_TEMP); 206 v = ad7418_read_2(sc->sc_dev, AD7418_TEMP); 207 if (v >= 0) 208 sc->sc_curtemp = v; 209 ad7418_set_channel(sc, AD7418_CHAN_VOLT); 210 v = ad7418_read_2(sc->sc_dev, AD7418_VOLT); 211 if (v >= 0) 212 sc->sc_curvolt = v; 213 sc->sc_lastupdate = ticks; 214 } | 204 /* NB: no point in updating any faster than the chip */ 205 if (ticks - sc->sc_lastupdate > hz) { 206 ad7418_set_channel(sc, AD7418_CHAN_TEMP); 207 v = ad7418_read_2(sc->sc_dev, AD7418_TEMP); 208 if (v >= 0) 209 sc->sc_curtemp = v; 210 ad7418_set_channel(sc, AD7418_CHAN_VOLT); 211 v = ad7418_read_2(sc->sc_dev, AD7418_VOLT); 212 if (v >= 0) 213 sc->sc_curvolt = v; 214 sc->sc_lastupdate = ticks; 215 } |
215 mtx_unlock(&sc->sc_mtx); | |
216} 217 218static device_method_t ad7418_methods[] = { 219 DEVMETHOD(device_probe, ad7418_probe), 220 DEVMETHOD(device_attach, ad7418_attach), 221 222 {0, 0}, 223}; 224 225static driver_t ad7418_driver = { 226 "ad7418", 227 ad7418_methods, 228 sizeof(struct ad7418_softc), 229}; 230static devclass_t ad7418_devclass; 231 232DRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0); 233MODULE_VERSION(ad7418, 1); 234MODULE_DEPEND(ad7418, iicbus, 1, 1, 1); | 216} 217 218static device_method_t ad7418_methods[] = { 219 DEVMETHOD(device_probe, ad7418_probe), 220 DEVMETHOD(device_attach, ad7418_attach), 221 222 {0, 0}, 223}; 224 225static driver_t ad7418_driver = { 226 "ad7418", 227 ad7418_methods, 228 sizeof(struct ad7418_softc), 229}; 230static devclass_t ad7418_devclass; 231 232DRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0); 233MODULE_VERSION(ad7418, 1); 234MODULE_DEPEND(ad7418, iicbus, 1, 1, 1); |