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>
57 static int smusat_probe(device_t);
58 static int smusat_attach(device_t);
59 static int smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS);
61 MALLOC_DEFINE(M_SMUSAT, "smusat", "SMU Sattelite Sensors");
63 static device_method_t smusat_methods[] = {
64 /* Device interface */
65 DEVMETHOD(device_probe, smusat_probe),
66 DEVMETHOD(device_attach, smusat_attach),
71 struct smu_sensor *sc_sensors;
75 time_t sc_last_update;
78 static driver_t smusat_driver = {
81 sizeof(struct smusat_softc)
84 static devclass_t smusat_devclass;
86 DRIVER_MODULE(smusat, iicbus, smusat_driver, smusat_devclass, 0, 0);
89 smusat_probe(device_t dev)
91 const char *compat = ofw_bus_get_compat(dev);
93 if (compat == NULL || strcmp(compat, "smu-sat") != 0)
96 device_set_desc(dev, "SMU Satellite Sensors");
101 smusat_attach(device_t dev)
104 struct smu_sensor *sens;
105 struct smusat_softc *sc;
106 struct sysctl_oid *sensroot_oid;
107 struct sysctl_ctx_list *ctx;
111 sc = device_get_softc(dev);
113 sc->sc_last_update = 0;
115 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
116 child = OF_peer(child))
119 if (sc->sc_nsensors == 0) {
120 device_printf(dev, "WARNING: No sensors detected!\n");
124 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
125 M_SMUSAT, M_WAITOK | M_ZERO);
127 sens = sc->sc_sensors;
130 ctx = device_get_sysctl_ctx(dev);
131 sensroot_oid = device_get_sysctl_tree(dev);
133 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
134 child = OF_peer(child)) {
135 char sysctl_name[40], sysctl_desc[40];
139 OF_getprop(child, "reg", &sens->reg, sizeof(sens->reg));
140 if (sens->reg < 0x30)
144 OF_getprop(child, "location", sens->location,
145 sizeof(sens->location));
147 OF_getprop(child, "device_type", type, sizeof(type));
149 if (strcmp(type, "current-sensor") == 0) {
150 sens->type = SMU_CURRENT_SENSOR;
152 } else if (strcmp(type, "temp-sensor") == 0) {
153 sens->type = SMU_TEMP_SENSOR;
155 } else if (strcmp(type, "voltage-sensor") == 0) {
156 sens->type = SMU_VOLTAGE_SENSOR;
158 } else if (strcmp(type, "power-sensor") == 0) {
159 sens->type = SMU_POWER_SENSOR;
165 for (i = 0; i < strlen(sens->location); i++) {
166 sysctl_name[i] = tolower(sens->location[i]);
167 if (isspace(sysctl_name[i]))
168 sysctl_name[i] = '_';
172 sprintf(sysctl_desc,"%s (%s)", sens->location, units);
173 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
174 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
175 sc->sc_nsensors, smusat_sensor_sysctl, "I", sysctl_desc);
185 smusat_updatecache(device_t dev)
188 struct smusat_softc *sc = device_get_softc(dev);
189 struct iic_msg msgs[2] = {
190 {0, IIC_M_WR | IIC_M_NOSTOP, 1, ®},
191 {0, IIC_M_RD, 16, sc->sc_cache},
194 msgs[0].slave = msgs[1].slave = iicbus_get_addr(dev);
195 sc->sc_last_update = time_uptime;
197 return (iicbus_transfer(dev, msgs, 2));
201 smusat_sensor_read(device_t dev, struct smu_sensor *sens, int *val)
204 struct smusat_softc *sc;
206 sc = device_get_softc(dev);
208 if (time_uptime - sc->sc_last_update > 1)
209 smusat_updatecache(dev);
211 value = (sc->sc_cache[sens->reg*2] << 8) +
212 sc->sc_cache[sens->reg*2 + 1];
214 switch (sens->type) {
215 case SMU_TEMP_SENSOR:
221 case SMU_VOLTAGE_SENSOR:
227 case SMU_CURRENT_SENSOR:
233 case SMU_POWER_SENSOR:
243 smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS)
246 struct smusat_softc *sc;
247 struct smu_sensor *sens;
251 sc = device_get_softc(dev);
252 sens = &sc->sc_sensors[arg2];
254 error = smusat_sensor_read(dev, sens, &value);
258 error = sysctl_handle_int(oidp, &value, 0, req);