ad7418.c revision 164419
1164419Ssam/*- 2164419Ssam * Copyright (c) 2006 Sam Leffler. All rights reserved. 3164419Ssam * 4164419Ssam * Redistribution and use in source and binary forms, with or without 5164419Ssam * modification, are permitted provided that the following conditions 6164419Ssam * are met: 7164419Ssam * 1. Redistributions of source code must retain the above copyright 8164419Ssam * notice, this list of conditions and the following disclaimer. 9164419Ssam * 2. Redistributions in binary form must reproduce the above copyright 10164419Ssam * notice, this list of conditions and the following disclaimer in the 11164419Ssam * documentation and/or other materials provided with the distribution. 12164419Ssam * 13164419Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14164419Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15164419Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16164419Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17164419Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18164419Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19164419Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20164419Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21164419Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22164419Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23164419Ssam */ 24164419Ssam 25164419Ssam#include <sys/cdefs.h> 26164419Ssam__FBSDID("$FreeBSD: head/sys/dev/iicbus/ad7418.c 164419 2006-11-19 23:39:54Z sam $"); 27164419Ssam/* 28164419Ssam * Analog Devices AD7418 chip sitting on the I2C bus. 29164419Ssam */ 30164419Ssam#include <sys/param.h> 31164419Ssam#include <sys/systm.h> 32164419Ssam#include <sys/kernel.h> 33164419Ssam#include <sys/lock.h> 34164419Ssam#include <sys/module.h> 35164419Ssam#include <sys/mutex.h> 36164419Ssam#include <sys/bus.h> 37164419Ssam#include <sys/resource.h> 38164419Ssam#include <sys/rman.h> 39164419Ssam#include <sys/sysctl.h> 40164419Ssam 41164419Ssam#include <machine/bus.h> 42164419Ssam#include <machine/cpu.h> 43164419Ssam#include <machine/cpufunc.h> 44164419Ssam#include <machine/frame.h> 45164419Ssam#include <machine/resource.h> 46164419Ssam#include <machine/intr.h> 47164419Ssam 48164419Ssam#include <dev/iicbus/iiconf.h> 49164419Ssam 50164419Ssam#include "iicbus_if.h" 51164419Ssam 52164419Ssam#define IIC_M_WR 0 /* write operation */ 53164419Ssam 54164419Ssam#define AD7418_ADDR 0x50 /* slave address */ 55164419Ssam 56164419Ssam#define AD7418_TEMP 0 /* Temperature Value (r/o) */ 57164419Ssam#define AD7418_CONF 1 /* Config Register (r/w) */ 58164419Ssam#define AD7418_CONF_SHUTDOWN 0x01 59164419Ssam#define AD7418_CONF_CHAN 0xe0 /* channel select mask */ 60164419Ssam#define AD7418_CHAN_TEMP 0x00 /* temperature channel */ 61164419Ssam#define AD7418_CHAN_VOLT 0x80 /* voltage channel */ 62164419Ssam#define AD7418_THYST 2 /* Thyst Setpoint (r/o) */ 63164419Ssam#define AD7418_TOTI 3 /* Toti Setpoint */ 64164419Ssam#define AD7418_VOLT 4 /* ADC aka Voltage (r/o) */ 65164419Ssam#define AD7418_CONF2 5 /* Config2 Register (r/w) */ 66164419Ssam 67164419Ssamstruct ad7418_softc { 68164419Ssam device_t sc_dev; 69164419Ssam struct mtx sc_mtx; 70164419Ssam int sc_curchan; /* current channel */ 71164419Ssam int sc_curtemp; 72164419Ssam int sc_curvolt; 73164419Ssam int sc_lastupdate; /* in ticks */ 74164419Ssam}; 75164419Ssam 76164419Ssamstatic void ad7418_update(struct ad7418_softc *); 77164419Ssamstatic int ad7418_read_1(device_t dev, int reg); 78164419Ssamstatic int ad7418_write_1(device_t dev, int reg, int v); 79164419Ssam 80164419Ssamstatic int 81164419Ssamad7418_probe(device_t dev) 82164419Ssam{ 83164419Ssam /* XXX really probe? */ 84164419Ssam device_set_desc(dev, "Analog Devices AD7418 ADC"); 85164419Ssam return (0); 86164419Ssam} 87164419Ssam 88164419Ssamstatic int 89164419Ssamad7418_sysctl_temp(SYSCTL_HANDLER_ARGS) 90164419Ssam{ 91164419Ssam struct ad7418_softc *sc = arg1; 92164419Ssam int temp; 93164419Ssam 94164419Ssam ad7418_update(sc); 95164419Ssam temp = (sc->sc_curtemp / 64) * 25; 96164419Ssam return sysctl_handle_int(oidp, &temp, 0, req); 97164419Ssam} 98164419Ssam 99164419Ssamstatic int 100164419Ssamad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS) 101164419Ssam{ 102164419Ssam struct ad7418_softc *sc = arg1; 103164419Ssam int volt; 104164419Ssam 105164419Ssam ad7418_update(sc); 106164419Ssam volt = (sc->sc_curvolt >> 6) * 564 / 10; 107164419Ssam return sysctl_handle_int(oidp, &volt, 0, req); 108164419Ssam} 109164419Ssam 110164419Ssamstatic int 111164419Ssamad7418_attach(device_t dev) 112164419Ssam{ 113164419Ssam struct ad7418_softc *sc = device_get_softc(dev); 114164419Ssam struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev); 115164419Ssam struct sysctl_oid *tree = device_get_sysctl_tree(dev); 116164419Ssam int conf; 117164419Ssam 118164419Ssam sc->sc_dev = dev; 119164419Ssam mtx_init(&sc->sc_mtx, "ad7418", "ad7418", MTX_DEF); 120164419Ssam 121164419Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 122164419Ssam "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 123164419Ssam ad7418_sysctl_temp, "I", "operating temperature"); 124164419Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 125164419Ssam "volt", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 126164419Ssam ad7418_sysctl_voltage, "I", "input voltage"); 127164419Ssam 128164419Ssam /* enable chip if configured in shutdown mode */ 129164419Ssam conf = ad7418_read_1(dev, AD7418_CONF); 130164419Ssam if (conf >= 0 && (conf & AD7418_CONF_SHUTDOWN)) 131164419Ssam ad7418_write_1(dev, AD7418_CONF, conf &~ AD7418_CONF_SHUTDOWN); 132164419Ssam 133164419Ssam return (0); 134164419Ssam} 135164419Ssam 136164419Ssamstatic int 137164419Ssamad7418_read_1(device_t dev, int reg) 138164419Ssam{ 139164419Ssam uint8_t addr = reg; 140164419Ssam uint8_t data[1]; 141164419Ssam struct iic_msg msgs[2] = { 142164419Ssam { AD7418_ADDR, IIC_M_WR, 1, &addr }, 143164419Ssam { AD7418_ADDR, IIC_M_RD, 1, data }, 144164419Ssam }; 145164419Ssam return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0]; 146164419Ssam} 147164419Ssam 148164419Ssamstatic int 149164419Ssamad7418_write_1(device_t dev, int reg, int v) 150164419Ssam{ 151164419Ssam /* NB: register pointer precedes actual data */ 152164419Ssam uint8_t data[2]; 153164419Ssam struct iic_msg msgs[1] = { 154164419Ssam { AD7418_ADDR, IIC_M_WR, 2, data }, 155164419Ssam }; 156164419Ssam data[0] = reg; 157164419Ssam data[1] = v & 0xff; 158164419Ssam return iicbus_transfer(dev, msgs, 1); 159164419Ssam} 160164419Ssam 161164419Ssamstatic void 162164419Ssamad7418_set_channel(struct ad7418_softc *sc, int chan) 163164419Ssam{ 164164419Ssam if (sc->sc_curchan == chan) 165164419Ssam return; 166164419Ssam ad7418_write_1(sc->sc_dev, AD7418_CONF, 167164419Ssam (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan); 168164419Ssam sc->sc_curchan = chan; 169164419Ssam#if 0 170164419Ssam /* 171164419Ssam * NB: Linux driver delays here but chip data sheet 172164419Ssam * says nothing and things appear to work fine w/o 173164419Ssam * a delay on channel change. If this is enabled 174164419Ssam * be sure to account for losing the mutex below 175164419Ssam * in ad7418_update. 176164419Ssam */ 177164419Ssam mtx_assert(&sc->sc_mtx, MA_OWNED); 178164419Ssam /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */ 179164419Ssam msleep(sc, &sc->sc_mtx, 0, "ad7418", 1); 180164419Ssam#endif 181164419Ssam} 182164419Ssam 183164419Ssamstatic int 184164419Ssamad7418_read_2(device_t dev, int reg) 185164419Ssam{ 186164419Ssam uint8_t addr = reg; 187164419Ssam uint8_t data[2]; 188164419Ssam struct iic_msg msgs[2] = { 189164419Ssam { AD7418_ADDR, IIC_M_WR, 1, &addr }, 190164419Ssam { AD7418_ADDR, IIC_M_RD, 2, data }, 191164419Ssam }; 192164419Ssam /* NB: D15..D8 precede D7..D0 per data sheet (Fig 12) */ 193164419Ssam return iicbus_transfer(dev, msgs, 2) != 0 ? 194164419Ssam -1 : ((data[0] << 8) | data[1]); 195164419Ssam} 196164419Ssam 197164419Ssamstatic void 198164419Ssamad7418_update(struct ad7418_softc *sc) 199164419Ssam{ 200164419Ssam int v; 201164419Ssam 202164419Ssam mtx_lock(&sc->sc_mtx); 203164419Ssam /* NB: no point in updating any faster than the chip */ 204164419Ssam if (ticks - sc->sc_lastupdate > hz) { 205164419Ssam ad7418_set_channel(sc, AD7418_CHAN_TEMP); 206164419Ssam v = ad7418_read_2(sc->sc_dev, AD7418_TEMP); 207164419Ssam if (v >= 0) 208164419Ssam sc->sc_curtemp = v; 209164419Ssam ad7418_set_channel(sc, AD7418_CHAN_VOLT); 210164419Ssam v = ad7418_read_2(sc->sc_dev, AD7418_VOLT); 211164419Ssam if (v >= 0) 212164419Ssam sc->sc_curvolt = v; 213164419Ssam sc->sc_lastupdate = ticks; 214164419Ssam } 215164419Ssam mtx_unlock(&sc->sc_mtx); 216164419Ssam} 217164419Ssam 218164419Ssamstatic device_method_t ad7418_methods[] = { 219164419Ssam DEVMETHOD(device_probe, ad7418_probe), 220164419Ssam DEVMETHOD(device_attach, ad7418_attach), 221164419Ssam 222164419Ssam {0, 0}, 223164419Ssam}; 224164419Ssam 225164419Ssamstatic driver_t ad7418_driver = { 226164419Ssam "ad7418", 227164419Ssam ad7418_methods, 228164419Ssam sizeof(struct ad7418_softc), 229164419Ssam}; 230164419Ssamstatic devclass_t ad7418_devclass; 231164419Ssam 232164419SsamDRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0); 233164419SsamMODULE_VERSION(ad7418, 1); 234164419SsamMODULE_DEPEND(ad7418, iicbus, 1, 1, 1); 235