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