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 * 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. 12 * 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. 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 * 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. 12 * 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. 23 */ 24 25#include <sys/cdefs.h>
|
123 sx_init(&sc->sc_lock, "ad7418"); 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 132 /* enable chip if configured in shutdown mode */ 133 conf = ad7418_read_1(dev, AD7418_CONF); 134 if (conf >= 0 && (conf & AD7418_CONF_SHUTDOWN)) 135 ad7418_write_1(dev, AD7418_CONF, conf &~ AD7418_CONF_SHUTDOWN); 136 137 return (0); 138} 139 140static int 141ad7418_read_1(device_t dev, int reg) 142{ 143 uint8_t addr = reg; 144 uint8_t data[1]; 145 struct iic_msg msgs[2] = { 146 { AD7418_ADDR, IIC_M_WR, 1, &addr }, 147 { AD7418_ADDR, IIC_M_RD, 1, data }, 148 }; 149 return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0]; 150} 151 152static int 153ad7418_write_1(device_t dev, int reg, int v) 154{ 155 /* NB: register pointer precedes actual data */ 156 uint8_t data[2]; 157 struct iic_msg msgs[1] = { 158 { AD7418_ADDR, IIC_M_WR, 2, data }, 159 }; 160 data[0] = reg; 161 data[1] = v & 0xff; 162 return iicbus_transfer(dev, msgs, 1); 163} 164 165static void 166ad7418_set_channel(struct ad7418_softc *sc, int chan) 167{ 168 if (sc->sc_curchan == chan) 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 177 * a delay on channel change. 178 */ 179 /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */ 180 tsleep(sc, 0, "ad7418", hz/1000); 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]; 189 struct iic_msg msgs[2] = { 190 { AD7418_ADDR, IIC_M_WR, 1, &addr }, 191 { AD7418_ADDR, IIC_M_RD, 2, data }, 192 }; 193 /* NB: D15..D8 precede D7..D0 per data sheet (Fig 12) */ 194 return iicbus_transfer(dev, msgs, 2) != 0 ? 195 -1 : ((data[0] << 8) | data[1]); 196} 197 198static void 199ad7418_update(struct ad7418_softc *sc) 200{ 201 int v; 202 203 sx_assert(&sc->sc_lock, SA_XLOCKED); 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 } 216} 217 218static device_method_t ad7418_methods[] = { 219 DEVMETHOD(device_probe, ad7418_probe), 220 DEVMETHOD(device_attach, ad7418_attach), 221 222 DEVMETHOD_END 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);
| 125 sx_init(&sc->sc_lock, "ad7418"); 126 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"); 133 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); 138 139 return (0); 140} 141 142static int 143ad7418_read_1(device_t dev, int reg) 144{ 145 uint8_t addr = reg; 146 uint8_t data[1]; 147 struct iic_msg msgs[2] = { 148 { AD7418_ADDR, IIC_M_WR, 1, &addr }, 149 { AD7418_ADDR, IIC_M_RD, 1, data }, 150 }; 151 return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0]; 152} 153 154static int 155ad7418_write_1(device_t dev, int reg, int v) 156{ 157 /* NB: register pointer precedes actual data */ 158 uint8_t data[2]; 159 struct iic_msg msgs[1] = { 160 { AD7418_ADDR, IIC_M_WR, 2, data }, 161 }; 162 data[0] = reg; 163 data[1] = v & 0xff; 164 return iicbus_transfer(dev, msgs, 1); 165} 166 167static void 168ad7418_set_channel(struct ad7418_softc *sc, int chan) 169{ 170 if (sc->sc_curchan == chan) 171 return; 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; 175#if 0 176 /* 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. 180 */ 181 /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */ 182 tsleep(sc, 0, "ad7418", hz/1000); 183#endif 184} 185 186static int 187ad7418_read_2(device_t dev, int reg) 188{ 189 uint8_t addr = reg; 190 uint8_t data[2]; 191 struct iic_msg msgs[2] = { 192 { AD7418_ADDR, IIC_M_WR, 1, &addr }, 193 { AD7418_ADDR, IIC_M_RD, 2, data }, 194 }; 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]); 198} 199 200static void 201ad7418_update(struct ad7418_softc *sc) 202{ 203 int v; 204 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); 210 if (v >= 0) 211 sc->sc_curtemp = v; 212 ad7418_set_channel(sc, AD7418_CHAN_VOLT); 213 v = ad7418_read_2(sc->sc_dev, AD7418_VOLT); 214 if (v >= 0) 215 sc->sc_curvolt = v; 216 sc->sc_lastupdate = ticks; 217 } 218} 219 220static device_method_t ad7418_methods[] = { 221 DEVMETHOD(device_probe, ad7418_probe), 222 DEVMETHOD(device_attach, ad7418_attach), 223 224 DEVMETHOD_END 225}; 226 227static driver_t ad7418_driver = { 228 "ad7418", 229 ad7418_methods, 230 sizeof(struct ad7418_softc), 231}; 232static devclass_t ad7418_devclass; 233 234DRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0); 235MODULE_VERSION(ad7418, 1); 236MODULE_DEPEND(ad7418, iicbus, 1, 1, 1);
|