xref: /freebsd/sys/powerpc/powermac/smusat.c (revision 9efb0787bb4daabfe744f7a9eeead789eba4539d)
1*9efb0787SNathan Whitehorn /*-
2*9efb0787SNathan Whitehorn  * Copyright (c) 2010 Nathan Whitehorn
3*9efb0787SNathan Whitehorn  * All rights reserved.
4*9efb0787SNathan Whitehorn  *
5*9efb0787SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
6*9efb0787SNathan Whitehorn  * modification, are permitted provided that the following conditions
7*9efb0787SNathan Whitehorn  * are met:
8*9efb0787SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
9*9efb0787SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
10*9efb0787SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
11*9efb0787SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
12*9efb0787SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
13*9efb0787SNathan Whitehorn  *
14*9efb0787SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*9efb0787SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*9efb0787SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*9efb0787SNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*9efb0787SNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19*9efb0787SNathan Whitehorn  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20*9efb0787SNathan Whitehorn  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21*9efb0787SNathan Whitehorn  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22*9efb0787SNathan Whitehorn  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*9efb0787SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*9efb0787SNathan Whitehorn  * SUCH DAMAGE.
25*9efb0787SNathan Whitehorn  *
26*9efb0787SNathan Whitehorn  */
27*9efb0787SNathan Whitehorn 
28*9efb0787SNathan Whitehorn #include <sys/cdefs.h>
29*9efb0787SNathan Whitehorn __FBSDID("$FreeBSD$");
30*9efb0787SNathan Whitehorn 
31*9efb0787SNathan Whitehorn #include <sys/param.h>
32*9efb0787SNathan Whitehorn #include <sys/systm.h>
33*9efb0787SNathan Whitehorn #include <sys/module.h>
34*9efb0787SNathan Whitehorn #include <sys/bus.h>
35*9efb0787SNathan Whitehorn #include <sys/conf.h>
36*9efb0787SNathan Whitehorn #include <sys/cpu.h>
37*9efb0787SNathan Whitehorn #include <sys/ctype.h>
38*9efb0787SNathan Whitehorn #include <sys/kernel.h>
39*9efb0787SNathan Whitehorn #include <sys/sysctl.h>
40*9efb0787SNathan Whitehorn 
41*9efb0787SNathan Whitehorn #include <dev/iicbus/iicbus.h>
42*9efb0787SNathan Whitehorn #include <dev/iicbus/iiconf.h>
43*9efb0787SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
44*9efb0787SNathan Whitehorn #include <dev/ofw/openfirm.h>
45*9efb0787SNathan Whitehorn 
46*9efb0787SNathan Whitehorn struct smu_sensor {
47*9efb0787SNathan Whitehorn 	cell_t	reg;
48*9efb0787SNathan Whitehorn 	char	location[32];
49*9efb0787SNathan Whitehorn 	enum {
50*9efb0787SNathan Whitehorn 		SMU_CURRENT_SENSOR,
51*9efb0787SNathan Whitehorn 		SMU_VOLTAGE_SENSOR,
52*9efb0787SNathan Whitehorn 		SMU_POWER_SENSOR,
53*9efb0787SNathan Whitehorn 		SMU_TEMP_SENSOR
54*9efb0787SNathan Whitehorn 	} type;
55*9efb0787SNathan Whitehorn };
56*9efb0787SNathan Whitehorn 
57*9efb0787SNathan Whitehorn static int	smusat_probe(device_t);
58*9efb0787SNathan Whitehorn static int	smusat_attach(device_t);
59*9efb0787SNathan Whitehorn static int	smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS);
60*9efb0787SNathan Whitehorn 
61*9efb0787SNathan Whitehorn MALLOC_DEFINE(M_SMUSAT, "smusat", "SMU Sattelite Sensors");
62*9efb0787SNathan Whitehorn 
63*9efb0787SNathan Whitehorn static device_method_t  smusat_methods[] = {
64*9efb0787SNathan Whitehorn 	/* Device interface */
65*9efb0787SNathan Whitehorn 	DEVMETHOD(device_probe,		smusat_probe),
66*9efb0787SNathan Whitehorn 	DEVMETHOD(device_attach,	smusat_attach),
67*9efb0787SNathan Whitehorn 	{ 0, 0 },
68*9efb0787SNathan Whitehorn };
69*9efb0787SNathan Whitehorn 
70*9efb0787SNathan Whitehorn struct smusat_softc {
71*9efb0787SNathan Whitehorn 	struct smu_sensor *sc_sensors;
72*9efb0787SNathan Whitehorn 	int	sc_nsensors;
73*9efb0787SNathan Whitehorn 
74*9efb0787SNathan Whitehorn 	uint8_t	sc_cache[16];
75*9efb0787SNathan Whitehorn 	time_t	sc_last_update;
76*9efb0787SNathan Whitehorn };
77*9efb0787SNathan Whitehorn 
78*9efb0787SNathan Whitehorn static driver_t smusat_driver = {
79*9efb0787SNathan Whitehorn 	"smusat",
80*9efb0787SNathan Whitehorn 	smusat_methods,
81*9efb0787SNathan Whitehorn 	sizeof(struct smusat_softc)
82*9efb0787SNathan Whitehorn };
83*9efb0787SNathan Whitehorn 
84*9efb0787SNathan Whitehorn static devclass_t smusat_devclass;
85*9efb0787SNathan Whitehorn 
86*9efb0787SNathan Whitehorn DRIVER_MODULE(smusat, iicbus, smusat_driver, smusat_devclass, 0, 0);
87*9efb0787SNathan Whitehorn 
88*9efb0787SNathan Whitehorn static int
89*9efb0787SNathan Whitehorn smusat_probe(device_t dev)
90*9efb0787SNathan Whitehorn {
91*9efb0787SNathan Whitehorn 	const char *compat = ofw_bus_get_compat(dev);
92*9efb0787SNathan Whitehorn 
93*9efb0787SNathan Whitehorn 	if (compat == NULL || strcmp(compat, "smu-sat") != 0)
94*9efb0787SNathan Whitehorn 		return (ENXIO);
95*9efb0787SNathan Whitehorn 
96*9efb0787SNathan Whitehorn 	device_set_desc(dev, "SMU Satellite Sensors");
97*9efb0787SNathan Whitehorn 	return (0);
98*9efb0787SNathan Whitehorn }
99*9efb0787SNathan Whitehorn 
100*9efb0787SNathan Whitehorn static int
101*9efb0787SNathan Whitehorn smusat_attach(device_t dev)
102*9efb0787SNathan Whitehorn {
103*9efb0787SNathan Whitehorn 	phandle_t child;
104*9efb0787SNathan Whitehorn 	struct smu_sensor *sens;
105*9efb0787SNathan Whitehorn 	struct smusat_softc *sc;
106*9efb0787SNathan Whitehorn 	struct sysctl_oid *sensroot_oid;
107*9efb0787SNathan Whitehorn 	struct sysctl_ctx_list *ctx;
108*9efb0787SNathan Whitehorn 	char type[32];
109*9efb0787SNathan Whitehorn 	int i;
110*9efb0787SNathan Whitehorn 
111*9efb0787SNathan Whitehorn 	sc = device_get_softc(dev);
112*9efb0787SNathan Whitehorn 	sc->sc_nsensors = 0;
113*9efb0787SNathan Whitehorn 	sc->sc_last_update = 0;
114*9efb0787SNathan Whitehorn 
115*9efb0787SNathan Whitehorn 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
116*9efb0787SNathan Whitehorn 	    child = OF_peer(child))
117*9efb0787SNathan Whitehorn 		sc->sc_nsensors++;
118*9efb0787SNathan Whitehorn 
119*9efb0787SNathan Whitehorn 	if (sc->sc_nsensors == 0) {
120*9efb0787SNathan Whitehorn 		device_printf(dev, "WARNING: No sensors detected!\n");
121*9efb0787SNathan Whitehorn 		return (-1);
122*9efb0787SNathan Whitehorn 	}
123*9efb0787SNathan Whitehorn 
124*9efb0787SNathan Whitehorn 	sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
125*9efb0787SNathan Whitehorn 	    M_SMUSAT, M_WAITOK | M_ZERO);
126*9efb0787SNathan Whitehorn 
127*9efb0787SNathan Whitehorn 	sens = sc->sc_sensors;
128*9efb0787SNathan Whitehorn 	sc->sc_nsensors = 0;
129*9efb0787SNathan Whitehorn 
130*9efb0787SNathan Whitehorn 	ctx = device_get_sysctl_ctx(dev);
131*9efb0787SNathan Whitehorn 	sensroot_oid = device_get_sysctl_tree(dev);
132*9efb0787SNathan Whitehorn 
133*9efb0787SNathan Whitehorn 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
134*9efb0787SNathan Whitehorn 	    child = OF_peer(child)) {
135*9efb0787SNathan Whitehorn 		char sysctl_name[40], sysctl_desc[40];
136*9efb0787SNathan Whitehorn 		const char *units;
137*9efb0787SNathan Whitehorn 
138*9efb0787SNathan Whitehorn 		sens->reg = 0;
139*9efb0787SNathan Whitehorn 		OF_getprop(child, "reg", &sens->reg, sizeof(sens->reg));
140*9efb0787SNathan Whitehorn 		if (sens->reg < 0x30)
141*9efb0787SNathan Whitehorn 			continue;
142*9efb0787SNathan Whitehorn 
143*9efb0787SNathan Whitehorn 		sens->reg -= 0x30;
144*9efb0787SNathan Whitehorn 		OF_getprop(child, "location", sens->location,
145*9efb0787SNathan Whitehorn 		    sizeof(sens->location));
146*9efb0787SNathan Whitehorn 
147*9efb0787SNathan Whitehorn 		OF_getprop(child, "device_type", type, sizeof(type));
148*9efb0787SNathan Whitehorn 
149*9efb0787SNathan Whitehorn 		if (strcmp(type, "current-sensor") == 0) {
150*9efb0787SNathan Whitehorn 			sens->type = SMU_CURRENT_SENSOR;
151*9efb0787SNathan Whitehorn 			units = "mA";
152*9efb0787SNathan Whitehorn 		} else if (strcmp(type, "temp-sensor") == 0) {
153*9efb0787SNathan Whitehorn 			sens->type = SMU_TEMP_SENSOR;
154*9efb0787SNathan Whitehorn 			units = "C";
155*9efb0787SNathan Whitehorn 		} else if (strcmp(type, "voltage-sensor") == 0) {
156*9efb0787SNathan Whitehorn 			sens->type = SMU_VOLTAGE_SENSOR;
157*9efb0787SNathan Whitehorn 			units = "mV";
158*9efb0787SNathan Whitehorn 		} else if (strcmp(type, "power-sensor") == 0) {
159*9efb0787SNathan Whitehorn 			sens->type = SMU_POWER_SENSOR;
160*9efb0787SNathan Whitehorn 			units = "mW";
161*9efb0787SNathan Whitehorn 		} else {
162*9efb0787SNathan Whitehorn 			continue;
163*9efb0787SNathan Whitehorn 		}
164*9efb0787SNathan Whitehorn 
165*9efb0787SNathan Whitehorn 		for (i = 0; i < strlen(sens->location); i++) {
166*9efb0787SNathan Whitehorn 			sysctl_name[i] = tolower(sens->location[i]);
167*9efb0787SNathan Whitehorn 			if (isspace(sysctl_name[i]))
168*9efb0787SNathan Whitehorn 				sysctl_name[i] = '_';
169*9efb0787SNathan Whitehorn 		}
170*9efb0787SNathan Whitehorn 		sysctl_name[i] = 0;
171*9efb0787SNathan Whitehorn 
172*9efb0787SNathan Whitehorn 		sprintf(sysctl_desc,"%s (%s)", sens->location, units);
173*9efb0787SNathan Whitehorn 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
174*9efb0787SNathan Whitehorn 		    sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
175*9efb0787SNathan Whitehorn 		    sc->sc_nsensors, smusat_sensor_sysctl, "I", sysctl_desc);
176*9efb0787SNathan Whitehorn 
177*9efb0787SNathan Whitehorn 		sens++;
178*9efb0787SNathan Whitehorn 		sc->sc_nsensors++;
179*9efb0787SNathan Whitehorn 	}
180*9efb0787SNathan Whitehorn 
181*9efb0787SNathan Whitehorn 	return (0);
182*9efb0787SNathan Whitehorn }
183*9efb0787SNathan Whitehorn 
184*9efb0787SNathan Whitehorn static int
185*9efb0787SNathan Whitehorn smusat_updatecache(device_t dev)
186*9efb0787SNathan Whitehorn {
187*9efb0787SNathan Whitehorn 	uint8_t reg = 0x3f;
188*9efb0787SNathan Whitehorn 	struct smusat_softc *sc = device_get_softc(dev);
189*9efb0787SNathan Whitehorn 	struct iic_msg msgs[2] = {
190*9efb0787SNathan Whitehorn 	    {0, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
191*9efb0787SNathan Whitehorn 	    {0, IIC_M_RD, 16, sc->sc_cache},
192*9efb0787SNathan Whitehorn 	};
193*9efb0787SNathan Whitehorn 
194*9efb0787SNathan Whitehorn 	msgs[0].slave = msgs[1].slave = iicbus_get_addr(dev);
195*9efb0787SNathan Whitehorn 	sc->sc_last_update = time_uptime;
196*9efb0787SNathan Whitehorn 
197*9efb0787SNathan Whitehorn 	return (iicbus_transfer(dev, msgs, 2));
198*9efb0787SNathan Whitehorn }
199*9efb0787SNathan Whitehorn 
200*9efb0787SNathan Whitehorn static int
201*9efb0787SNathan Whitehorn smusat_sensor_read(device_t dev, struct smu_sensor *sens, int *val)
202*9efb0787SNathan Whitehorn {
203*9efb0787SNathan Whitehorn 	int value;
204*9efb0787SNathan Whitehorn 	struct smusat_softc *sc;
205*9efb0787SNathan Whitehorn 
206*9efb0787SNathan Whitehorn 	sc = device_get_softc(dev);
207*9efb0787SNathan Whitehorn 
208*9efb0787SNathan Whitehorn 	if (time_uptime - sc->sc_last_update > 1)
209*9efb0787SNathan Whitehorn 		smusat_updatecache(dev);
210*9efb0787SNathan Whitehorn 
211*9efb0787SNathan Whitehorn 	value = (sc->sc_cache[sens->reg*2] << 8) +
212*9efb0787SNathan Whitehorn 	    sc->sc_cache[sens->reg*2 + 1];
213*9efb0787SNathan Whitehorn 
214*9efb0787SNathan Whitehorn 	switch (sens->type) {
215*9efb0787SNathan Whitehorn 	case SMU_TEMP_SENSOR:
216*9efb0787SNathan Whitehorn 		/* 16.16 */
217*9efb0787SNathan Whitehorn 		value <<= 10;
218*9efb0787SNathan Whitehorn 		/* Kill the .16 */
219*9efb0787SNathan Whitehorn 		value >>= 16;
220*9efb0787SNathan Whitehorn 		break;
221*9efb0787SNathan Whitehorn 	case SMU_VOLTAGE_SENSOR:
222*9efb0787SNathan Whitehorn 		/* 16.16 */
223*9efb0787SNathan Whitehorn 		value <<= 4;
224*9efb0787SNathan Whitehorn 		/* Kill the .16 */
225*9efb0787SNathan Whitehorn 		value >>= 16;
226*9efb0787SNathan Whitehorn 		break;
227*9efb0787SNathan Whitehorn 	case SMU_CURRENT_SENSOR:
228*9efb0787SNathan Whitehorn 		/* 16.16 */
229*9efb0787SNathan Whitehorn 		value <<= 8;
230*9efb0787SNathan Whitehorn 		/* Kill the .16 */
231*9efb0787SNathan Whitehorn 		value >>= 16;
232*9efb0787SNathan Whitehorn 		break;
233*9efb0787SNathan Whitehorn 	case SMU_POWER_SENSOR:
234*9efb0787SNathan Whitehorn 		/* Doesn't exist */
235*9efb0787SNathan Whitehorn 		break;
236*9efb0787SNathan Whitehorn 	}
237*9efb0787SNathan Whitehorn 
238*9efb0787SNathan Whitehorn 	*val = value;
239*9efb0787SNathan Whitehorn 	return (0);
240*9efb0787SNathan Whitehorn }
241*9efb0787SNathan Whitehorn 
242*9efb0787SNathan Whitehorn static int
243*9efb0787SNathan Whitehorn smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS)
244*9efb0787SNathan Whitehorn {
245*9efb0787SNathan Whitehorn 	device_t dev;
246*9efb0787SNathan Whitehorn 	struct smusat_softc *sc;
247*9efb0787SNathan Whitehorn 	struct smu_sensor *sens;
248*9efb0787SNathan Whitehorn 	int value, error;
249*9efb0787SNathan Whitehorn 
250*9efb0787SNathan Whitehorn 	dev = arg1;
251*9efb0787SNathan Whitehorn 	sc = device_get_softc(dev);
252*9efb0787SNathan Whitehorn 	sens = &sc->sc_sensors[arg2];
253*9efb0787SNathan Whitehorn 
254*9efb0787SNathan Whitehorn 	error = smusat_sensor_read(dev, sens, &value);
255*9efb0787SNathan Whitehorn 	if (error != 0)
256*9efb0787SNathan Whitehorn 		return (error);
257*9efb0787SNathan Whitehorn 
258*9efb0787SNathan Whitehorn 	error = sysctl_handle_int(oidp, &value, 0, req);
259*9efb0787SNathan Whitehorn 
260*9efb0787SNathan Whitehorn 	return (error);
261*9efb0787SNathan Whitehorn }
262*9efb0787SNathan Whitehorn 
263