2 * Copyright (c) 2003 Julien Bordet <zejames@greyhats.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * $OpenBSD: it.c,v 1.22 2007/03/22 16:55:31 deraadt Exp $
28 #include <sys/param.h>
29 #include <sys/kernel.h>
31 #include <sys/module.h>
34 #include <bus/isa/isavar.h>
35 #include <sys/systm.h>
37 #include <sys/sensors.h>
42 #define DPRINTF(x) do { kprintf x; } while (0)
48 * IT87-compatible chips can typically measure voltages up to 4.096 V.
49 * To measure higher voltages the input is attenuated with (external)
50 * resistors. Negative voltages are measured using a reference
51 * voltage. So we have to convert the sensor values back to real
52 * voltages by applying the appropriate resistor factor.
54 #define RFACT_NONE 10000
55 #define RFACT(x, y) (RFACT_NONE * ((x) + (y)) / (y))
57 int it_probe(struct device *);
58 int it_attach(struct device *);
59 int it_detach(struct device *);
60 u_int8_t it_readreg(struct it_softc *, int);
61 void it_writereg(struct it_softc *, int, int);
62 void it_setup_volt(struct it_softc *, int, int);
63 void it_setup_temp(struct it_softc *, int, int);
64 void it_setup_fan(struct it_softc *, int, int);
66 void it_generic_stemp(struct it_softc *, struct ksensor *);
67 void it_generic_svolt(struct it_softc *, struct ksensor *);
68 void it_generic_fanrpm(struct it_softc *, struct ksensor *);
70 void it_refresh_sensor_data(struct it_softc *);
71 void it_refresh(void *);
73 static device_method_t it_methods[] = {
74 /* Methods from the device interface */
75 DEVMETHOD(device_probe, it_probe),
76 DEVMETHOD(device_attach, it_attach),
77 DEVMETHOD(device_detach, it_detach),
79 /* Terminate method list */
83 static driver_t it_driver = {
86 sizeof (struct it_softc)
89 static devclass_t it_devclass;
91 DRIVER_MODULE(it, isa, it_driver, it_devclass, NULL, NULL);
94 const int it_vrfact[] = {
107 it_probe(struct device *dev)
109 struct resource *iores;
112 bus_space_handle_t ioh;
115 iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &iorid,
116 0ul, ~0ul, 8, RF_ACTIVE);
118 DPRINTF(("%s: can't map i/o space\n", __func__));
121 iot = rman_get_bustag(iores);
122 ioh = rman_get_bushandle(iores);
124 /* Check Vendor ID */
125 bus_space_write_1(iot, ioh, ITC_ADDR, ITD_CHIPID);
126 cr = bus_space_read_1(iot, ioh, ITC_DATA);
127 bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores);
128 DPRINTF(("it: vendor id 0x%x\n", cr));
129 if (cr != IT_ID_IT87)
136 it_attach(struct device *dev)
138 struct it_softc *sc = device_get_softc(dev);
143 sc->sc_iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->sc_iorid,
144 0ul, ~0ul, 8, RF_ACTIVE);
145 if (sc->sc_iores == NULL) {
146 device_printf(dev, "can't map i/o space\n");
149 sc->sc_iot = rman_get_bustag(sc->sc_iores);
150 sc->sc_ioh = rman_get_bushandle(sc->sc_iores);
152 sc->numsensors = IT_NUM_SENSORS;
154 it_setup_fan(sc, 0, 3);
155 it_setup_volt(sc, 3, 9);
156 it_setup_temp(sc, 12, 3);
158 if (sensor_task_register(sc, it_refresh, 5)) {
159 device_printf(sc->sc_dev, "unable to register update task\n");
163 /* Activate monitoring */
164 cr = it_readreg(sc, ITD_CONFIG);
166 it_writereg(sc, ITD_CONFIG, cr);
168 /* Initialize sensors */
169 strlcpy(sc->sensordev.xname, device_get_nameunit(sc->sc_dev),
170 sizeof(sc->sensordev.xname));
171 for (i = 0; i < sc->numsensors; ++i)
172 sensor_attach(&sc->sensordev, &sc->sensors[i]);
173 sensordev_install(&sc->sensordev);
179 it_detach(struct device *dev)
181 struct it_softc *sc = device_get_softc(dev);
184 sensordev_deinstall(&sc->sensordev);
185 sensor_task_unregister(sc);
187 error = bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_iorid,
196 it_readreg(struct it_softc *sc, int reg)
198 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITC_ADDR, reg);
199 return (bus_space_read_1(sc->sc_iot, sc->sc_ioh, ITC_DATA));
203 it_writereg(struct it_softc *sc, int reg, int val)
205 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITC_ADDR, reg);
206 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITC_DATA, val);
210 it_setup_volt(struct it_softc *sc, int start, int n)
214 for (i = 0; i < n; ++i) {
215 sc->sensors[start + i].type = SENSOR_VOLTS_DC;
218 ksnprintf(sc->sensors[start + 0].desc, sizeof(sc->sensors[0].desc),
220 ksnprintf(sc->sensors[start + 1].desc, sizeof(sc->sensors[1].desc),
222 ksnprintf(sc->sensors[start + 2].desc, sizeof(sc->sensors[2].desc),
224 ksnprintf(sc->sensors[start + 3].desc, sizeof(sc->sensors[3].desc),
226 ksnprintf(sc->sensors[start + 4].desc, sizeof(sc->sensors[4].desc),
228 ksnprintf(sc->sensors[start + 5].desc, sizeof(sc->sensors[5].desc),
230 ksnprintf(sc->sensors[start + 6].desc, sizeof(sc->sensors[6].desc),
232 ksnprintf(sc->sensors[start + 7].desc, sizeof(sc->sensors[7].desc),
234 ksnprintf(sc->sensors[start + 8].desc, sizeof(sc->sensors[8].desc),
239 it_setup_temp(struct it_softc *sc, int start, int n)
243 for (i = 0; i < n; ++i)
244 sc->sensors[start + i].type = SENSOR_TEMP;
248 it_setup_fan(struct it_softc *sc, int start, int n)
252 for (i = 0; i < n; ++i)
253 sc->sensors[start + i].type = SENSOR_FANRPM;
257 it_generic_stemp(struct it_softc *sc, struct ksensor *sensors)
261 for (i = 0; i < 3; i++) {
262 sdata = it_readreg(sc, ITD_SENSORTEMPBASE + i);
263 /* Convert temperature to Fahrenheit degres */
264 sensors[i].value = sdata * 1000000 + 273150000;
269 it_generic_svolt(struct it_softc *sc, struct ksensor *sensors)
273 for (i = 0; i < 9; i++) {
274 sdata = it_readreg(sc, ITD_SENSORVOLTBASE + i);
275 DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
276 /* voltage returned as (mV >> 4) */
277 sensors[i].value = (sdata << 4);
278 /* these two values are negative and formula is different */
279 if (i == 5 || i == 6)
280 sensors[i].value = ((sdata << 4) - IT_VREF);
281 /* rfact is (factor * 10^4) */
282 sensors[i].value *= it_vrfact[i];
283 /* division by 10 gets us back to uVDC */
284 sensors[i].value /= 10;
285 if (i == 5 || i == 6)
286 sensors[i].value += IT_VREF * 1000;
291 it_generic_fanrpm(struct it_softc *sc, struct ksensor *sensors)
293 int i, sdata, divisor, odivisor, ndivisor;
295 odivisor = ndivisor = divisor = it_readreg(sc, ITD_FAN);
296 for (i = 0; i < 3; i++, divisor >>= 3) {
297 sensors[i].flags &= ~SENSOR_FINVALID;
298 if ((sdata = it_readreg(sc, ITD_SENSORFANBASE + i)) == 0xff) {
299 sensors[i].flags |= SENSOR_FINVALID;
303 ndivisor &= ~(7 << (i * 3));
304 ndivisor |= ((divisor + 1) & 7) << (i * 3);
306 } else if (sdata == 0) {
307 sensors[i].value = 0;
310 divisor = divisor & 1 ? 3 : 1;
311 sensors[i].value = 1350000 / (sdata << (divisor & 7));
314 if (ndivisor != odivisor)
315 it_writereg(sc, ITD_FAN, ndivisor);
319 * pre: last read occurred >= 1.5 seconds ago
320 * post: sensors[] current data are the latest from the chip
323 it_refresh_sensor_data(struct it_softc *sc)
325 /* Refresh our stored data for every sensor */
326 it_generic_stemp(sc, &sc->sensors[12]);
327 it_generic_svolt(sc, &sc->sensors[3]);
328 it_generic_fanrpm(sc, &sc->sensors[0]);
332 it_refresh(void *arg)
334 struct it_softc *sc = (struct it_softc *)arg;
336 it_refresh_sensor_data(sc);