mtw(4) remove misplaced DEBUG_FLAGS
[freebsd/src.git] / sys / powerpc / powermac / smusat.c
blob5639daa2850104dd91b2a0f2b8be18d51d7f4449
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2010 Nathan Whitehorn
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/cpu.h>
36 #include <sys/ctype.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <dev/iicbus/iicbus.h>
41 #include <dev/iicbus/iiconf.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/openfirm.h>
45 #include <powerpc/powermac/powermac_thermal.h>
47 struct smu_sensor {
48 struct pmac_therm therm;
49 device_t dev;
51 cell_t reg;
52 enum {
53 SMU_CURRENT_SENSOR,
54 SMU_VOLTAGE_SENSOR,
55 SMU_POWER_SENSOR,
56 SMU_TEMP_SENSOR
57 } type;
60 static int smusat_probe(device_t);
61 static int smusat_attach(device_t);
62 static int smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS);
63 static int smusat_sensor_read(struct smu_sensor *sens);
65 static MALLOC_DEFINE(M_SMUSAT, "smusat", "SMU Sattelite Sensors");
67 static device_method_t smusat_methods[] = {
68 /* Device interface */
69 DEVMETHOD(device_probe, smusat_probe),
70 DEVMETHOD(device_attach, smusat_attach),
71 { 0, 0 },
74 struct smusat_softc {
75 struct smu_sensor *sc_sensors;
76 int sc_nsensors;
78 uint8_t sc_cache[16];
79 time_t sc_last_update;
82 static driver_t smusat_driver = {
83 "smusat",
84 smusat_methods,
85 sizeof(struct smusat_softc)
88 DRIVER_MODULE(smusat, iicbus, smusat_driver, 0, 0);
90 static int
91 smusat_probe(device_t dev)
93 const char *compat = ofw_bus_get_compat(dev);
95 if (compat == NULL || strcmp(compat, "smu-sat") != 0)
96 return (ENXIO);
98 device_set_desc(dev, "SMU Satellite Sensors");
99 return (0);
102 static int
103 smusat_attach(device_t dev)
105 phandle_t child;
106 struct smu_sensor *sens;
107 struct smusat_softc *sc;
108 struct sysctl_oid *sensroot_oid;
109 struct sysctl_ctx_list *ctx;
110 char type[32];
111 int i;
113 sc = device_get_softc(dev);
114 sc->sc_nsensors = 0;
115 sc->sc_last_update = 0;
117 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
118 child = OF_peer(child))
119 sc->sc_nsensors++;
121 if (sc->sc_nsensors == 0) {
122 device_printf(dev, "WARNING: No sensors detected!\n");
123 return (-1);
126 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
127 M_SMUSAT, M_WAITOK | M_ZERO);
129 sens = sc->sc_sensors;
130 sc->sc_nsensors = 0;
132 ctx = device_get_sysctl_ctx(dev);
133 sensroot_oid = device_get_sysctl_tree(dev);
135 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
136 child = OF_peer(child)) {
137 char sysctl_name[40], sysctl_desc[40];
138 const char *units;
140 sens->dev = dev;
141 sens->reg = 0;
142 OF_getprop(child, "reg", &sens->reg, sizeof(sens->reg));
143 if (sens->reg < 0x30)
144 continue;
145 sens->reg -= 0x30;
147 OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
148 OF_getprop(child, "location", sens->therm.name,
149 sizeof(sens->therm.name));
151 OF_getprop(child, "device_type", type, sizeof(type));
153 if (strcmp(type, "current-sensor") == 0) {
154 sens->type = SMU_CURRENT_SENSOR;
155 units = "mA";
156 } else if (strcmp(type, "temp-sensor") == 0) {
157 sens->type = SMU_TEMP_SENSOR;
158 units = "C";
159 } else if (strcmp(type, "voltage-sensor") == 0) {
160 sens->type = SMU_VOLTAGE_SENSOR;
161 units = "mV";
162 } else if (strcmp(type, "power-sensor") == 0) {
163 sens->type = SMU_POWER_SENSOR;
164 units = "mW";
165 } else {
166 continue;
169 for (i = 0; i < strlen(sens->therm.name); i++) {
170 sysctl_name[i] = tolower(sens->therm.name[i]);
171 if (isspace(sysctl_name[i]))
172 sysctl_name[i] = '_';
174 sysctl_name[i] = 0;
176 sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
177 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
178 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
179 sc->sc_nsensors, smusat_sensor_sysctl,
180 (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
182 if (sens->type == SMU_TEMP_SENSOR) {
183 /* Make up some numbers */
184 sens->therm.target_temp = 500 + 2731; /* 50 C */
185 sens->therm.max_temp = 900 + 2731; /* 90 C */
186 sens->therm.read =
187 (int (*)(struct pmac_therm *))smusat_sensor_read;
188 pmac_thermal_sensor_register(&sens->therm);
191 sens++;
192 sc->sc_nsensors++;
195 return (0);
198 static int
199 smusat_updatecache(device_t dev)
201 uint8_t reg = 0x3f;
202 uint8_t value[16];
203 struct smusat_softc *sc = device_get_softc(dev);
204 int error;
205 struct iic_msg msgs[2] = {
206 {0, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
207 {0, IIC_M_RD, 16, value},
210 msgs[0].slave = msgs[1].slave = iicbus_get_addr(dev);
211 error = iicbus_transfer(dev, msgs, 2);
212 if (error)
213 return (error);
215 sc->sc_last_update = time_uptime;
216 memcpy(sc->sc_cache, value, sizeof(value));
217 return (0);
220 static int
221 smusat_sensor_read(struct smu_sensor *sens)
223 int value, error;
224 device_t dev;
225 struct smusat_softc *sc;
227 dev = sens->dev;
228 sc = device_get_softc(dev);
229 error = 0;
231 if (time_uptime - sc->sc_last_update > 1)
232 error = smusat_updatecache(dev);
233 if (error)
234 return (-error);
236 value = (sc->sc_cache[sens->reg*2] << 8) +
237 sc->sc_cache[sens->reg*2 + 1];
238 if (value == 0xffff) {
239 sc->sc_last_update = 0; /* Result was bad, don't cache */
240 return (-EINVAL);
243 switch (sens->type) {
244 case SMU_TEMP_SENSOR:
245 /* 16.16 */
246 value <<= 10;
247 /* From 16.16 to 0.1 C */
248 value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731;
249 break;
250 case SMU_VOLTAGE_SENSOR:
251 /* 16.16 */
252 value <<= 4;
253 /* Kill the .16 */
254 value >>= 16;
255 break;
256 case SMU_CURRENT_SENSOR:
257 /* 16.16 */
258 value <<= 8;
259 /* Kill the .16 */
260 value >>= 16;
261 break;
262 case SMU_POWER_SENSOR:
263 /* Doesn't exist */
264 break;
267 return (value);
270 static int
271 smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS)
273 device_t dev;
274 struct smusat_softc *sc;
275 struct smu_sensor *sens;
276 int value, error;
278 dev = arg1;
279 sc = device_get_softc(dev);
280 sens = &sc->sc_sensors[arg2];
282 value = smusat_sensor_read(sens);
283 if (value < 0)
284 return (EBUSY);
286 error = sysctl_handle_int(oidp, &value, 0, req);
288 return (error);