2 * Copyright (c) 2010 Nathan Whitehorn
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
41 #include <dev/iicbus/iicbus.h>
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/openfirm.h>
46 #include <powerpc/powermac/powermac_thermal.h>
49 struct pmac_therm therm;
61 static int smusat_probe(device_t);
62 static int smusat_attach(device_t);
63 static int smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS);
64 static int smusat_sensor_read(struct smu_sensor *sens);
66 static MALLOC_DEFINE(M_SMUSAT, "smusat", "SMU Sattelite Sensors");
68 static device_method_t smusat_methods[] = {
69 /* Device interface */
70 DEVMETHOD(device_probe, smusat_probe),
71 DEVMETHOD(device_attach, smusat_attach),
76 struct smu_sensor *sc_sensors;
80 time_t sc_last_update;
83 static driver_t smusat_driver = {
86 sizeof(struct smusat_softc)
89 static devclass_t smusat_devclass;
91 DRIVER_MODULE(smusat, iicbus, smusat_driver, smusat_devclass, 0, 0);
94 smusat_probe(device_t dev)
96 const char *compat = ofw_bus_get_compat(dev);
98 if (compat == NULL || strcmp(compat, "smu-sat") != 0)
101 device_set_desc(dev, "SMU Satellite Sensors");
106 smusat_attach(device_t dev)
109 struct smu_sensor *sens;
110 struct smusat_softc *sc;
111 struct sysctl_oid *sensroot_oid;
112 struct sysctl_ctx_list *ctx;
116 sc = device_get_softc(dev);
118 sc->sc_last_update = 0;
120 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
121 child = OF_peer(child))
124 if (sc->sc_nsensors == 0) {
125 device_printf(dev, "WARNING: No sensors detected!\n");
129 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
130 M_SMUSAT, M_WAITOK | M_ZERO);
132 sens = sc->sc_sensors;
135 ctx = device_get_sysctl_ctx(dev);
136 sensroot_oid = device_get_sysctl_tree(dev);
138 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
139 child = OF_peer(child)) {
140 char sysctl_name[40], sysctl_desc[40];
145 OF_getprop(child, "reg", &sens->reg, sizeof(sens->reg));
146 if (sens->reg < 0x30)
150 OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
151 OF_getprop(child, "location", sens->therm.name,
152 sizeof(sens->therm.name));
154 OF_getprop(child, "device_type", type, sizeof(type));
156 if (strcmp(type, "current-sensor") == 0) {
157 sens->type = SMU_CURRENT_SENSOR;
159 } else if (strcmp(type, "temp-sensor") == 0) {
160 sens->type = SMU_TEMP_SENSOR;
162 } else if (strcmp(type, "voltage-sensor") == 0) {
163 sens->type = SMU_VOLTAGE_SENSOR;
165 } else if (strcmp(type, "power-sensor") == 0) {
166 sens->type = SMU_POWER_SENSOR;
172 for (i = 0; i < strlen(sens->therm.name); i++) {
173 sysctl_name[i] = tolower(sens->therm.name[i]);
174 if (isspace(sysctl_name[i]))
175 sysctl_name[i] = '_';
179 sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
180 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
181 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
182 sc->sc_nsensors, smusat_sensor_sysctl,
183 (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
185 if (sens->type == SMU_TEMP_SENSOR) {
186 /* Make up some numbers */
187 sens->therm.target_temp = 500 + 2732; /* 50 C */
188 sens->therm.max_temp = 900 + 2732; /* 90 C */
190 (int (*)(struct pmac_therm *))smusat_sensor_read;
191 pmac_thermal_sensor_register(&sens->therm);
202 smusat_updatecache(device_t dev)
206 struct smusat_softc *sc = device_get_softc(dev);
208 struct iic_msg msgs[2] = {
209 {0, IIC_M_WR | IIC_M_NOSTOP, 1, ®},
210 {0, IIC_M_RD, 16, value},
213 msgs[0].slave = msgs[1].slave = iicbus_get_addr(dev);
214 error = iicbus_transfer(dev, msgs, 2);
218 sc->sc_last_update = time_uptime;
219 memcpy(sc->sc_cache, value, sizeof(value));
224 smusat_sensor_read(struct smu_sensor *sens)
228 struct smusat_softc *sc;
231 sc = device_get_softc(dev);
234 if (time_uptime - sc->sc_last_update > 1)
235 error = smusat_updatecache(dev);
239 value = (sc->sc_cache[sens->reg*2] << 8) +
240 sc->sc_cache[sens->reg*2 + 1];
241 if (value == 0xffff) {
242 sc->sc_last_update = 0; /* Result was bad, don't cache */
246 switch (sens->type) {
247 case SMU_TEMP_SENSOR:
250 /* From 16.16 to 0.1 C */
251 value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2732;
253 case SMU_VOLTAGE_SENSOR:
259 case SMU_CURRENT_SENSOR:
265 case SMU_POWER_SENSOR:
274 smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS)
277 struct smusat_softc *sc;
278 struct smu_sensor *sens;
282 sc = device_get_softc(dev);
283 sens = &sc->sc_sensors[arg2];
285 value = smusat_sensor_read(sens);
289 error = sysctl_handle_int(oidp, &value, 0, req);