2 * Copyright (c) 2003-2009 RMI Corporation
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.
13 * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 * temperature sensor chip sitting on the I2C bus.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <sys/resource.h>
45 #include <sys/sysctl.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/cpufunc.h>
50 #include <machine/frame.h>
51 #include <machine/resource.h>
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/iicbus/iicbus.h>
56 #include <mips/rmi/board.h>
57 #include <mips/rmi/rmi_boot_info.h>
58 #include "iicbus_if.h"
60 #define MAX6657_EXT_TEMP 1
62 struct max6657_softc {
66 int sc_lastupdate; /* in ticks */
69 static void max6657_update(struct max6657_softc *);
70 static int max6657_read(device_t dev, uint32_t addr, int reg) ;
73 max6657_probe(device_t dev)
75 device_set_desc(dev, "MAX6657MSA Temperature Sensor");
80 max6657_sysctl_temp(SYSCTL_HANDLER_ARGS)
82 struct max6657_softc *sc = arg1;
86 temp = sc->sc_curtemp ;
87 return sysctl_handle_int(oidp, &temp, 0, req);
91 max6657_attach(device_t dev)
93 struct max6657_softc *sc = device_get_softc(dev);
94 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
95 struct sysctl_oid *tree = device_get_sysctl_tree(dev);
98 printf("max6657_attach device_get_softc failed\n");
102 sc->sc_addr = iicbus_get_addr(dev);
104 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
105 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
106 max6657_sysctl_temp, "I", "operating temperature");
108 device_printf(dev, "Chip temperature {%d} Degree Celsius\n",
109 max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP));
115 max6657_read(device_t dev, uint32_t slave_addr, int reg)
119 struct iic_msg msgs[2] = {
120 { slave_addr, IIC_M_WR, 1, &addr },
121 { slave_addr, IIC_M_RD, 1, data },
124 return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
129 max6657_update(struct max6657_softc *sc)
133 /* NB: no point in updating any faster than the chip */
134 if (ticks - sc->sc_lastupdate > hz) {
135 v = max6657_read(sc->sc_dev, sc->sc_addr, MAX6657_EXT_TEMP);
138 sc->sc_lastupdate = ticks;
142 static device_method_t max6657_methods[] = {
143 DEVMETHOD(device_probe, max6657_probe),
144 DEVMETHOD(device_attach, max6657_attach),
149 static driver_t max6657_driver = {
152 sizeof(struct max6657_softc),
154 static devclass_t max6657_devclass;
156 DRIVER_MODULE(max6657, iicbus, max6657_driver, max6657_devclass, 0, 0);
157 MODULE_VERSION(max6657, 1);
158 MODULE_DEPEND(max6657, iicbus, 1, 1, 1);