2 * Copyright (c) 2010 Andreas Tobler
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,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/callout.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
42 #include <sys/limits.h>
44 #include <machine/bus.h>
45 #include <machine/md_var.h>
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <powerpc/powermac/powermac_thermal.h>
54 /* Inlet, Backside, U3 Heatsink sensor: MAX6690. */
56 #define MAX6690_INT_TEMP 0x0
57 #define MAX6690_EXT_TEMP 0x1
58 #define MAX6690_RSL_STATUS 0x2
59 #define MAX6690_EEXT_TEMP 0x10
60 #define MAX6690_IEXT_TEMP 0x11
61 #define MAX6690_TEMP_MASK 0xe0
63 struct max6690_sensor {
64 struct pmac_therm therm;
70 /* Regular bus attachment functions */
71 static int max6690_probe(device_t);
72 static int max6690_attach(device_t);
74 /* Utility functions */
75 static int max6690_sensor_read(struct max6690_sensor *sens);
76 static int max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS);
77 static void max6690_start(void *xdev);
78 static int max6690_read(device_t dev, uint32_t addr, uint8_t reg,
81 struct max6690_softc {
83 struct intr_config_hook enum_hook;
85 struct max6690_sensor *sc_sensors;
88 static device_method_t max6690_methods[] = {
89 /* Device interface */
90 DEVMETHOD(device_probe, max6690_probe),
91 DEVMETHOD(device_attach, max6690_attach),
95 static driver_t max6690_driver = {
98 sizeof(struct max6690_softc)
101 static devclass_t max6690_devclass;
103 DRIVER_MODULE(max6690, iicbus, max6690_driver, max6690_devclass, 0, 0);
104 static MALLOC_DEFINE(M_MAX6690, "max6690", "Temp-Monitor MAX6690");
107 max6690_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
110 uint8_t busy[1], rsl;
113 /* Busy register RSL. */
114 rsl = MAX6690_RSL_STATUS;
115 /* first read the status register, 0x2. If busy, retry. */
116 struct iic_msg msg[4] = {
117 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &rsl },
118 { addr, IIC_M_RD, 1, busy },
119 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
120 { addr, IIC_M_RD, 1, buf },
125 err = iicbus_transfer(dev, msg, 4);
130 /* Check for invalid value and retry. */
134 *data = *((uint8_t*)buf);
139 device_printf(dev, "iicbus read failed\n");
142 pause("max6690_read", hz);
147 max6690_probe(device_t dev)
149 const char *name, *compatible;
150 struct max6690_softc *sc;
152 name = ofw_bus_get_name(dev);
153 compatible = ofw_bus_get_compat(dev);
158 if (strcmp(name, "temp-monitor") != 0 ||
159 strcmp(compatible, "max6690") != 0)
162 sc = device_get_softc(dev);
164 sc->sc_addr = iicbus_get_addr(dev);
166 device_set_desc(dev, "Temp-Monitor MAX6690");
172 * This function returns the number of sensors. If we call it the second time
173 * and we have allocated memory for sc->sc_sensors, we fill in the properties.
176 max6690_fill_sensor_prop(device_t dev)
179 struct max6690_softc *sc;
182 int i = 0, j, len = 0, prop_len, prev_len = 0;
184 sc = device_get_softc(dev);
186 child = ofw_bus_get_node(dev);
188 /* Fill the sensor location property. */
189 prop_len = OF_getprop(child, "hwsensor-location", location,
191 while (len < prop_len) {
192 if (sc->sc_sensors != NULL)
193 strcpy(sc->sc_sensors[i].therm.name, location + len);
194 prev_len = strlen(location + len) + 1;
198 if (sc->sc_sensors == NULL)
201 /* Fill the sensor id property. */
202 prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
203 for (j = 0; j < i; j++)
204 sc->sc_sensors[j].id = (id[j] & 0xf);
206 /* Fill the sensor zone property. */
207 prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
208 for (j = 0; j < i; j++)
209 sc->sc_sensors[j].therm.zone = id[j];
211 /* Set up remaining sensor properties */
212 for (j = 0; j < i; j++) {
213 sc->sc_sensors[j].dev = dev;
215 sc->sc_sensors[j].therm.target_temp = 400 + ZERO_C_TO_K;
216 sc->sc_sensors[j].therm.max_temp = 800 + ZERO_C_TO_K;
218 sc->sc_sensors[j].therm.read =
219 (int (*)(struct pmac_therm *))(max6690_sensor_read);
225 max6690_attach(device_t dev)
227 struct max6690_softc *sc;
229 sc = device_get_softc(dev);
231 sc->enum_hook.ich_func = max6690_start;
232 sc->enum_hook.ich_arg = dev;
234 /* We have to wait until interrupts are enabled. I2C read and write
235 * only works if the interrupts are available.
236 * The unin/i2c is controlled by the htpic on unin. But this is not
237 * the master. The openpic on mac-io is controlling the htpic.
238 * This one gets attached after the mac-io probing and then the
239 * interrupts will be available.
242 if (config_intrhook_establish(&sc->enum_hook) != 0)
249 max6690_start(void *xdev)
251 struct max6690_softc *sc;
252 struct sysctl_oid *oid, *sensroot_oid;
253 struct sysctl_ctx_list *ctx;
254 char sysctl_name[32];
257 device_t dev = (device_t)xdev;
259 sc = device_get_softc(dev);
263 /* Count the actual number of sensors. */
264 sc->sc_nsensors = max6690_fill_sensor_prop(dev);
266 device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
268 if (sc->sc_nsensors == 0)
269 device_printf(dev, "WARNING: No MAX6690 sensors detected!\n");
271 sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct max6690_sensor),
272 M_MAX6690, M_WAITOK | M_ZERO);
274 ctx = device_get_sysctl_ctx(dev);
275 sensroot_oid = SYSCTL_ADD_NODE(ctx,
276 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
277 CTLFLAG_RD, 0, "MAX6690 Sensor Information");
279 /* Now we can fill the properties into the allocated struct. */
280 sc->sc_nsensors = max6690_fill_sensor_prop(dev);
282 /* Register with powermac_thermal */
283 for (i = 0; i < sc->sc_nsensors; i++)
284 pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
286 /* Add sysctls for the sensors. */
287 for (i = 0; i < sc->sc_nsensors; i++) {
288 for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
290 tolower(sc->sc_sensors[i].therm.name[j]);
291 if (isspace(sysctl_name[j]))
292 sysctl_name[j] = '_';
296 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
298 sysctl_name, CTLFLAG_RD, 0,
299 "Sensor Information");
300 /* I use i to pass the sensor id. */
301 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
302 CTLTYPE_INT | CTLFLAG_RD, dev, i % 2,
303 max6690_sensor_sysctl, "IK",
304 "Sensor Temp in °C");
307 /* Dump sensor location & ID. */
309 device_printf(dev, "Sensors\n");
310 for (i = 0; i < sc->sc_nsensors; i++) {
311 device_printf(dev, "Location : %s ID: %d\n",
312 sc->sc_sensors[i].therm.name,
313 sc->sc_sensors[i].id);
317 config_intrhook_disestablish(&sc->enum_hook);
321 max6690_sensor_read(struct max6690_sensor *sens)
323 uint8_t reg_int = 0, reg_ext = 0;
325 uint8_t fraction = 0;
328 struct max6690_softc *sc;
330 sc = device_get_softc(sens->dev);
332 /* The internal sensor id's are even, the external are odd. */
333 if ((sens->id % 2) == 0) {
334 reg_int = MAX6690_INT_TEMP;
335 reg_ext = MAX6690_IEXT_TEMP;
337 reg_int = MAX6690_EXT_TEMP;
338 reg_ext = MAX6690_EEXT_TEMP;
341 err = max6690_read(sc->sc_dev, sc->sc_addr, reg_int, &integer);
342 err = max6690_read(sc->sc_dev, sc->sc_addr, reg_ext, &fraction);
347 fraction &= MAX6690_TEMP_MASK;
349 /* The temperature is in tenth kelvin, the fractional part resolution
352 temp = (integer * 10) + (fraction >> 5) * 10 / 8;
354 return (temp + ZERO_C_TO_K);
358 max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)
361 struct max6690_softc *sc;
362 struct max6690_sensor *sens;
367 sc = device_get_softc(dev);
368 sens = &sc->sc_sensors[arg2];
370 temp = max6690_sensor_read(sens);
374 error = sysctl_handle_int(oidp, &temp, 0, req);