smusat.c revision 208842
1/*-
2 * Copyright (c) 2010 Nathan Whitehorn
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/powerpc/powermac/smusat.c 208842 2010-06-05 17:51:37Z nwhitehorn $");
30
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
46struct smu_sensor {
47	cell_t	reg;
48	char	location[32];
49	enum {
50		SMU_CURRENT_SENSOR,
51		SMU_VOLTAGE_SENSOR,
52		SMU_POWER_SENSOR,
53		SMU_TEMP_SENSOR
54	} type;
55};
56
57static int	smusat_probe(device_t);
58static int	smusat_attach(device_t);
59static int	smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS);
60
61MALLOC_DEFINE(M_SMUSAT, "smusat", "SMU Sattelite Sensors");
62
63static device_method_t  smusat_methods[] = {
64	/* Device interface */
65	DEVMETHOD(device_probe,		smusat_probe),
66	DEVMETHOD(device_attach,	smusat_attach),
67	{ 0, 0 },
68};
69
70struct smusat_softc {
71	struct smu_sensor *sc_sensors;
72	int	sc_nsensors;
73
74	uint8_t	sc_cache[16];
75	time_t	sc_last_update;
76};
77
78static driver_t smusat_driver = {
79	"smusat",
80	smusat_methods,
81	sizeof(struct smusat_softc)
82};
83
84static devclass_t smusat_devclass;
85
86DRIVER_MODULE(smusat, iicbus, smusat_driver, smusat_devclass, 0, 0);
87
88static int
89smusat_probe(device_t dev)
90{
91	const char *compat = ofw_bus_get_compat(dev);
92
93	if (compat == NULL || strcmp(compat, "smu-sat") != 0)
94		return (ENXIO);
95
96	device_set_desc(dev, "SMU Satellite Sensors");
97	return (0);
98}
99
100static int
101smusat_attach(device_t dev)
102{
103	phandle_t child;
104	struct smu_sensor *sens;
105	struct smusat_softc *sc;
106	struct sysctl_oid *sensroot_oid;
107	struct sysctl_ctx_list *ctx;
108	char type[32];
109	int i;
110
111	sc = device_get_softc(dev);
112	sc->sc_nsensors = 0;
113	sc->sc_last_update = 0;
114
115	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
116	    child = OF_peer(child))
117		sc->sc_nsensors++;
118
119	if (sc->sc_nsensors == 0) {
120		device_printf(dev, "WARNING: No sensors detected!\n");
121		return (-1);
122	}
123
124	sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
125	    M_SMUSAT, M_WAITOK | M_ZERO);
126
127	sens = sc->sc_sensors;
128	sc->sc_nsensors = 0;
129
130	ctx = device_get_sysctl_ctx(dev);
131	sensroot_oid = device_get_sysctl_tree(dev);
132
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];
136		const char *units;
137
138		sens->reg = 0;
139		OF_getprop(child, "reg", &sens->reg, sizeof(sens->reg));
140		if (sens->reg < 0x30)
141			continue;
142
143		sens->reg -= 0x30;
144		OF_getprop(child, "location", sens->location,
145		    sizeof(sens->location));
146
147		OF_getprop(child, "device_type", type, sizeof(type));
148
149		if (strcmp(type, "current-sensor") == 0) {
150			sens->type = SMU_CURRENT_SENSOR;
151			units = "mA";
152		} else if (strcmp(type, "temp-sensor") == 0) {
153			sens->type = SMU_TEMP_SENSOR;
154			units = "C";
155		} else if (strcmp(type, "voltage-sensor") == 0) {
156			sens->type = SMU_VOLTAGE_SENSOR;
157			units = "mV";
158		} else if (strcmp(type, "power-sensor") == 0) {
159			sens->type = SMU_POWER_SENSOR;
160			units = "mW";
161		} else {
162			continue;
163		}
164
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] = '_';
169		}
170		sysctl_name[i] = 0;
171
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);
176
177		sens++;
178		sc->sc_nsensors++;
179	}
180
181	return (0);
182}
183
184static int
185smusat_updatecache(device_t dev)
186{
187	uint8_t reg = 0x3f;
188	struct smusat_softc *sc = device_get_softc(dev);
189	struct iic_msg msgs[2] = {
190	    {0, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
191	    {0, IIC_M_RD, 16, sc->sc_cache},
192	};
193
194	msgs[0].slave = msgs[1].slave = iicbus_get_addr(dev);
195	sc->sc_last_update = time_uptime;
196
197	return (iicbus_transfer(dev, msgs, 2));
198}
199
200static int
201smusat_sensor_read(device_t dev, struct smu_sensor *sens, int *val)
202{
203	int value;
204	struct smusat_softc *sc;
205
206	sc = device_get_softc(dev);
207
208	if (time_uptime - sc->sc_last_update > 1)
209		smusat_updatecache(dev);
210
211	value = (sc->sc_cache[sens->reg*2] << 8) +
212	    sc->sc_cache[sens->reg*2 + 1];
213
214	switch (sens->type) {
215	case SMU_TEMP_SENSOR:
216		/* 16.16 */
217		value <<= 10;
218		/* Kill the .16 */
219		value >>= 16;
220		break;
221	case SMU_VOLTAGE_SENSOR:
222		/* 16.16 */
223		value <<= 4;
224		/* Kill the .16 */
225		value >>= 16;
226		break;
227	case SMU_CURRENT_SENSOR:
228		/* 16.16 */
229		value <<= 8;
230		/* Kill the .16 */
231		value >>= 16;
232		break;
233	case SMU_POWER_SENSOR:
234		/* Doesn't exist */
235		break;
236	}
237
238	*val = value;
239	return (0);
240}
241
242static int
243smusat_sensor_sysctl(SYSCTL_HANDLER_ARGS)
244{
245	device_t dev;
246	struct smusat_softc *sc;
247	struct smu_sensor *sens;
248	int value, error;
249
250	dev = arg1;
251	sc = device_get_softc(dev);
252	sens = &sc->sc_sensors[arg2];
253
254	error = smusat_sensor_read(dev, sens, &value);
255	if (error != 0)
256		return (error);
257
258	error = sysctl_handle_int(oidp, &value, 0, req);
259
260	return (error);
261}
262
263