xref: /freebsd/sys/powerpc/powermac/smusat.c (revision b00ab754)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Nathan Whitehorn
5  * All rights reserved.
6  *
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.
15  *
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.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/cpu.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 
43 #include <dev/iicbus/iicbus.h>
44 #include <dev/iicbus/iiconf.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/openfirm.h>
47 
48 #include <powerpc/powermac/powermac_thermal.h>
49 
50 struct smu_sensor {
51 	struct pmac_therm therm;
52 	device_t dev;
53 
54 	cell_t	reg;
55 	enum {
56 		SMU_CURRENT_SENSOR,
57 		SMU_VOLTAGE_SENSOR,
58 		SMU_POWER_SENSOR,
59 		SMU_TEMP_SENSOR
60 	} type;
61 };
62 
63 static int	smusat_probe(device_t);
64 static int	smusat_attach(device_t);
65 static int	smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS);
66 static int	smusat_sensor_read(struct smu_sensor *sens);
67 
68 static MALLOC_DEFINE(M_SMUSAT, "smusat", "SMU Sattelite Sensors");
69 
70 static device_method_t  smusat_methods[] = {
71 	/* Device interface */
72 	DEVMETHOD(device_probe,		smusat_probe),
73 	DEVMETHOD(device_attach,	smusat_attach),
74 	{ 0, 0 },
75 };
76 
77 struct smusat_softc {
78 	struct smu_sensor *sc_sensors;
79 	int	sc_nsensors;
80 
81 	uint8_t	sc_cache[16];
82 	time_t	sc_last_update;
83 };
84 
85 static driver_t smusat_driver = {
86 	"smusat",
87 	smusat_methods,
88 	sizeof(struct smusat_softc)
89 };
90 
91 static devclass_t smusat_devclass;
92 
93 DRIVER_MODULE(smusat, iicbus, smusat_driver, smusat_devclass, 0, 0);
94 
95 static int
96 smusat_probe(device_t dev)
97 {
98 	const char *compat = ofw_bus_get_compat(dev);
99 
100 	if (compat == NULL || strcmp(compat, "smu-sat") != 0)
101 		return (ENXIO);
102 
103 	device_set_desc(dev, "SMU Satellite Sensors");
104 	return (0);
105 }
106 
107 static int
108 smusat_attach(device_t dev)
109 {
110 	phandle_t child;
111 	struct smu_sensor *sens;
112 	struct smusat_softc *sc;
113 	struct sysctl_oid *sensroot_oid;
114 	struct sysctl_ctx_list *ctx;
115 	char type[32];
116 	int i;
117 
118 	sc = device_get_softc(dev);
119 	sc->sc_nsensors = 0;
120 	sc->sc_last_update = 0;
121 
122 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
123 	    child = OF_peer(child))
124 		sc->sc_nsensors++;
125 
126 	if (sc->sc_nsensors == 0) {
127 		device_printf(dev, "WARNING: No sensors detected!\n");
128 		return (-1);
129 	}
130 
131 	sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
132 	    M_SMUSAT, M_WAITOK | M_ZERO);
133 
134 	sens = sc->sc_sensors;
135 	sc->sc_nsensors = 0;
136 
137 	ctx = device_get_sysctl_ctx(dev);
138 	sensroot_oid = device_get_sysctl_tree(dev);
139 
140 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
141 	    child = OF_peer(child)) {
142 		char sysctl_name[40], sysctl_desc[40];
143 		const char *units;
144 
145 		sens->dev = dev;
146 		sens->reg = 0;
147 		OF_getprop(child, "reg", &sens->reg, sizeof(sens->reg));
148 		if (sens->reg < 0x30)
149 			continue;
150 		sens->reg -= 0x30;
151 
152 		OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
153 		OF_getprop(child, "location", sens->therm.name,
154 		    sizeof(sens->therm.name));
155 
156 		OF_getprop(child, "device_type", type, sizeof(type));
157 
158 		if (strcmp(type, "current-sensor") == 0) {
159 			sens->type = SMU_CURRENT_SENSOR;
160 			units = "mA";
161 		} else if (strcmp(type, "temp-sensor") == 0) {
162 			sens->type = SMU_TEMP_SENSOR;
163 			units = "C";
164 		} else if (strcmp(type, "voltage-sensor") == 0) {
165 			sens->type = SMU_VOLTAGE_SENSOR;
166 			units = "mV";
167 		} else if (strcmp(type, "power-sensor") == 0) {
168 			sens->type = SMU_POWER_SENSOR;
169 			units = "mW";
170 		} else {
171 			continue;
172 		}
173 
174 		for (i = 0; i < strlen(sens->therm.name); i++) {
175 			sysctl_name[i] = tolower(sens->therm.name[i]);
176 			if (isspace(sysctl_name[i]))
177 				sysctl_name[i] = '_';
178 		}
179 		sysctl_name[i] = 0;
180 
181 		sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
182 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
183 		    sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
184 		    sc->sc_nsensors, smusat_sensor_sysctl,
185 		    (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
186 
187 		if (sens->type == SMU_TEMP_SENSOR) {
188 			/* Make up some numbers */
189 			sens->therm.target_temp = 500 + 2731; /* 50 C */
190 			sens->therm.max_temp = 900 + 2731; /* 90 C */
191 			sens->therm.read =
192 			    (int (*)(struct pmac_therm *))smusat_sensor_read;
193 			pmac_thermal_sensor_register(&sens->therm);
194 		}
195 
196 		sens++;
197 		sc->sc_nsensors++;
198 	}
199 
200 	return (0);
201 }
202 
203 static int
204 smusat_updatecache(device_t dev)
205 {
206 	uint8_t reg = 0x3f;
207 	uint8_t	value[16];
208 	struct smusat_softc *sc = device_get_softc(dev);
209 	int error;
210 	struct iic_msg msgs[2] = {
211 	    {0, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
212 	    {0, IIC_M_RD, 16, value},
213 	};
214 
215 	msgs[0].slave = msgs[1].slave = iicbus_get_addr(dev);
216 	error = iicbus_transfer(dev, msgs, 2);
217 	if (error)
218 		return (error);
219 
220 	sc->sc_last_update = time_uptime;
221 	memcpy(sc->sc_cache, value, sizeof(value));
222 	return (0);
223 }
224 
225 static int
226 smusat_sensor_read(struct smu_sensor *sens)
227 {
228 	int value, error;
229 	device_t dev;
230 	struct smusat_softc *sc;
231 
232 	dev = sens->dev;
233 	sc = device_get_softc(dev);
234 	error = 0;
235 
236 	if (time_uptime - sc->sc_last_update > 1)
237 		error = smusat_updatecache(dev);
238 	if (error)
239 		return (-error);
240 
241 	value = (sc->sc_cache[sens->reg*2] << 8) +
242 	    sc->sc_cache[sens->reg*2 + 1];
243 	if (value == 0xffff) {
244 		sc->sc_last_update = 0; /* Result was bad, don't cache */
245 		return (-EINVAL);
246 	}
247 
248 	switch (sens->type) {
249 	case SMU_TEMP_SENSOR:
250 		/* 16.16 */
251 		value <<= 10;
252 		/* From 16.16 to 0.1 C */
253 		value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731;
254 		break;
255 	case SMU_VOLTAGE_SENSOR:
256 		/* 16.16 */
257 		value <<= 4;
258 		/* Kill the .16 */
259 		value >>= 16;
260 		break;
261 	case SMU_CURRENT_SENSOR:
262 		/* 16.16 */
263 		value <<= 8;
264 		/* Kill the .16 */
265 		value >>= 16;
266 		break;
267 	case SMU_POWER_SENSOR:
268 		/* Doesn't exist */
269 		break;
270 	}
271 
272 	return (value);
273 }
274 
275 static int
276 smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS)
277 {
278 	device_t dev;
279 	struct smusat_softc *sc;
280 	struct smu_sensor *sens;
281 	int value, error;
282 
283 	dev = arg1;
284 	sc = device_get_softc(dev);
285 	sens = &sc->sc_sensors[arg2];
286 
287 	value = smusat_sensor_read(sens);
288 	if (value < 0)
289 		return (EBUSY);
290 
291 	error = sysctl_handle_int(oidp, &value, 0, req);
292 
293 	return (error);
294 }
295 
296