xref: /freebsd/sys/dev/iicbus/sensor/max6690.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1*7c569caaSEmmanuel Vadot /*-
2*7c569caaSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3*7c569caaSEmmanuel Vadot  *
4*7c569caaSEmmanuel Vadot  * Copyright (c) 2010 Andreas Tobler
5*7c569caaSEmmanuel Vadot  *
6*7c569caaSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7*7c569caaSEmmanuel Vadot  * modification, are permitted provided that the following conditions
8*7c569caaSEmmanuel Vadot  * are met:
9*7c569caaSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10*7c569caaSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11*7c569caaSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12*7c569caaSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13*7c569caaSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14*7c569caaSEmmanuel Vadot  *
15*7c569caaSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*7c569caaSEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*7c569caaSEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*7c569caaSEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*7c569caaSEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20*7c569caaSEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21*7c569caaSEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22*7c569caaSEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23*7c569caaSEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*7c569caaSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*7c569caaSEmmanuel Vadot  * SUCH DAMAGE.
26*7c569caaSEmmanuel Vadot  */
27*7c569caaSEmmanuel Vadot 
28*7c569caaSEmmanuel Vadot #include <sys/param.h>
29*7c569caaSEmmanuel Vadot #include <sys/bus.h>
30*7c569caaSEmmanuel Vadot #include <sys/systm.h>
31*7c569caaSEmmanuel Vadot #include <sys/module.h>
32*7c569caaSEmmanuel Vadot #include <sys/callout.h>
33*7c569caaSEmmanuel Vadot #include <sys/conf.h>
34*7c569caaSEmmanuel Vadot #include <sys/cpu.h>
35*7c569caaSEmmanuel Vadot #include <sys/ctype.h>
36*7c569caaSEmmanuel Vadot #include <sys/kernel.h>
37*7c569caaSEmmanuel Vadot #include <sys/reboot.h>
38*7c569caaSEmmanuel Vadot #include <sys/rman.h>
39*7c569caaSEmmanuel Vadot #include <sys/sysctl.h>
40*7c569caaSEmmanuel Vadot #include <sys/limits.h>
41*7c569caaSEmmanuel Vadot 
42*7c569caaSEmmanuel Vadot #include <machine/bus.h>
43*7c569caaSEmmanuel Vadot #include <machine/md_var.h>
44*7c569caaSEmmanuel Vadot 
45*7c569caaSEmmanuel Vadot #include <dev/iicbus/iicbus.h>
46*7c569caaSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
47*7c569caaSEmmanuel Vadot 
48*7c569caaSEmmanuel Vadot #include <dev/ofw/openfirm.h>
49*7c569caaSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
50*7c569caaSEmmanuel Vadot #include <powerpc/powermac/powermac_thermal.h>
51*7c569caaSEmmanuel Vadot 
52*7c569caaSEmmanuel Vadot /* Inlet, Backside, U3 Heatsink sensor: MAX6690. */
53*7c569caaSEmmanuel Vadot 
54*7c569caaSEmmanuel Vadot #define MAX6690_INT_TEMP    0x0
55*7c569caaSEmmanuel Vadot #define MAX6690_EXT_TEMP    0x1
56*7c569caaSEmmanuel Vadot #define MAX6690_RSL_STATUS  0x2
57*7c569caaSEmmanuel Vadot #define MAX6690_EEXT_TEMP   0x10
58*7c569caaSEmmanuel Vadot #define MAX6690_IEXT_TEMP   0x11
59*7c569caaSEmmanuel Vadot #define MAX6690_TEMP_MASK   0xe0
60*7c569caaSEmmanuel Vadot 
61*7c569caaSEmmanuel Vadot struct max6690_sensor {
62*7c569caaSEmmanuel Vadot 	struct pmac_therm therm;
63*7c569caaSEmmanuel Vadot 	device_t dev;
64*7c569caaSEmmanuel Vadot 
65*7c569caaSEmmanuel Vadot 	int     id;
66*7c569caaSEmmanuel Vadot };
67*7c569caaSEmmanuel Vadot 
68*7c569caaSEmmanuel Vadot /* Regular bus attachment functions */
69*7c569caaSEmmanuel Vadot static int  max6690_probe(device_t);
70*7c569caaSEmmanuel Vadot static int  max6690_attach(device_t);
71*7c569caaSEmmanuel Vadot 
72*7c569caaSEmmanuel Vadot /* Utility functions */
73*7c569caaSEmmanuel Vadot static int  max6690_sensor_read(struct max6690_sensor *sens);
74*7c569caaSEmmanuel Vadot static int  max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS);
75*7c569caaSEmmanuel Vadot static void max6690_start(void *xdev);
76*7c569caaSEmmanuel Vadot static int  max6690_read(device_t dev, uint32_t addr, uint8_t reg,
77*7c569caaSEmmanuel Vadot 			 uint8_t *data);
78*7c569caaSEmmanuel Vadot 
79*7c569caaSEmmanuel Vadot struct max6690_softc {
80*7c569caaSEmmanuel Vadot 	device_t		sc_dev;
81*7c569caaSEmmanuel Vadot 	struct intr_config_hook enum_hook;
82*7c569caaSEmmanuel Vadot 	uint32_t                sc_addr;
83*7c569caaSEmmanuel Vadot 	struct max6690_sensor   *sc_sensors;
84*7c569caaSEmmanuel Vadot 	int                     sc_nsensors;
85*7c569caaSEmmanuel Vadot };
86*7c569caaSEmmanuel Vadot static device_method_t  max6690_methods[] = {
87*7c569caaSEmmanuel Vadot 	/* Device interface */
88*7c569caaSEmmanuel Vadot 	DEVMETHOD(device_probe,		max6690_probe),
89*7c569caaSEmmanuel Vadot 	DEVMETHOD(device_attach,	max6690_attach),
90*7c569caaSEmmanuel Vadot 	{ 0, 0 },
91*7c569caaSEmmanuel Vadot };
92*7c569caaSEmmanuel Vadot 
93*7c569caaSEmmanuel Vadot static driver_t max6690_driver = {
94*7c569caaSEmmanuel Vadot 	"max6690",
95*7c569caaSEmmanuel Vadot 	max6690_methods,
96*7c569caaSEmmanuel Vadot 	sizeof(struct max6690_softc)
97*7c569caaSEmmanuel Vadot };
98*7c569caaSEmmanuel Vadot 
99*7c569caaSEmmanuel Vadot DRIVER_MODULE(max6690, iicbus, max6690_driver, 0, 0);
100*7c569caaSEmmanuel Vadot static MALLOC_DEFINE(M_MAX6690, "max6690", "Temp-Monitor MAX6690");
101*7c569caaSEmmanuel Vadot 
102*7c569caaSEmmanuel Vadot static int
max6690_read(device_t dev,uint32_t addr,uint8_t reg,uint8_t * data)103*7c569caaSEmmanuel Vadot max6690_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
104*7c569caaSEmmanuel Vadot {
105*7c569caaSEmmanuel Vadot 	uint8_t buf[4];
106*7c569caaSEmmanuel Vadot 	uint8_t busy[1], rsl;
107*7c569caaSEmmanuel Vadot 	int err, try = 0;
108*7c569caaSEmmanuel Vadot 
109*7c569caaSEmmanuel Vadot 	/* Busy register RSL. */
110*7c569caaSEmmanuel Vadot 	rsl = MAX6690_RSL_STATUS;
111*7c569caaSEmmanuel Vadot 	/* first read the status register, 0x2. If busy, retry. */
112*7c569caaSEmmanuel Vadot 	struct iic_msg msg[4] = {
113*7c569caaSEmmanuel Vadot 	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &rsl },
114*7c569caaSEmmanuel Vadot 	    { addr, IIC_M_RD, 1, busy },
115*7c569caaSEmmanuel Vadot 	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
116*7c569caaSEmmanuel Vadot 	    { addr, IIC_M_RD, 1, buf },
117*7c569caaSEmmanuel Vadot 	};
118*7c569caaSEmmanuel Vadot 
119*7c569caaSEmmanuel Vadot 	for (;;)
120*7c569caaSEmmanuel Vadot 	{
121*7c569caaSEmmanuel Vadot 		err = iicbus_transfer(dev, msg, nitems(msg));
122*7c569caaSEmmanuel Vadot 		if (err != 0)
123*7c569caaSEmmanuel Vadot 			goto retry;
124*7c569caaSEmmanuel Vadot 		if (busy[0] & 0x80)
125*7c569caaSEmmanuel Vadot 			goto retry;
126*7c569caaSEmmanuel Vadot 		/* Check for invalid value and retry. */
127*7c569caaSEmmanuel Vadot 		if (buf[0] == 0xff)
128*7c569caaSEmmanuel Vadot 			goto retry;
129*7c569caaSEmmanuel Vadot 
130*7c569caaSEmmanuel Vadot 		*data = *((uint8_t*)buf);
131*7c569caaSEmmanuel Vadot 		return (0);
132*7c569caaSEmmanuel Vadot 
133*7c569caaSEmmanuel Vadot 	retry:
134*7c569caaSEmmanuel Vadot 		if (++try > 5) {
135*7c569caaSEmmanuel Vadot 			device_printf(dev, "iicbus read failed\n");
136*7c569caaSEmmanuel Vadot 			return (-1);
137*7c569caaSEmmanuel Vadot 		}
138*7c569caaSEmmanuel Vadot 		pause("max6690_read", hz);
139*7c569caaSEmmanuel Vadot 	}
140*7c569caaSEmmanuel Vadot }
141*7c569caaSEmmanuel Vadot 
142*7c569caaSEmmanuel Vadot static int
max6690_probe(device_t dev)143*7c569caaSEmmanuel Vadot max6690_probe(device_t dev)
144*7c569caaSEmmanuel Vadot {
145*7c569caaSEmmanuel Vadot 	const char  *name, *compatible;
146*7c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
147*7c569caaSEmmanuel Vadot 
148*7c569caaSEmmanuel Vadot 	name = ofw_bus_get_name(dev);
149*7c569caaSEmmanuel Vadot 	compatible = ofw_bus_get_compat(dev);
150*7c569caaSEmmanuel Vadot 
151*7c569caaSEmmanuel Vadot 	if (!name)
152*7c569caaSEmmanuel Vadot 		return (ENXIO);
153*7c569caaSEmmanuel Vadot 
154*7c569caaSEmmanuel Vadot 	if (strcmp(name, "temp-monitor") != 0 ||
155*7c569caaSEmmanuel Vadot 	    strcmp(compatible, "max6690") != 0)
156*7c569caaSEmmanuel Vadot 		return (ENXIO);
157*7c569caaSEmmanuel Vadot 
158*7c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
159*7c569caaSEmmanuel Vadot 	sc->sc_dev = dev;
160*7c569caaSEmmanuel Vadot 	sc->sc_addr = iicbus_get_addr(dev);
161*7c569caaSEmmanuel Vadot 
162*7c569caaSEmmanuel Vadot 	device_set_desc(dev, "Temp-Monitor MAX6690");
163*7c569caaSEmmanuel Vadot 
164*7c569caaSEmmanuel Vadot 	return (0);
165*7c569caaSEmmanuel Vadot }
166*7c569caaSEmmanuel Vadot 
167*7c569caaSEmmanuel Vadot /*
168*7c569caaSEmmanuel Vadot  * This function returns the number of sensors. If we call it the second time
169*7c569caaSEmmanuel Vadot  * and we have allocated memory for sc->sc_sensors, we fill in the properties.
170*7c569caaSEmmanuel Vadot  */
171*7c569caaSEmmanuel Vadot static int
max6690_fill_sensor_prop(device_t dev)172*7c569caaSEmmanuel Vadot max6690_fill_sensor_prop(device_t dev)
173*7c569caaSEmmanuel Vadot {
174*7c569caaSEmmanuel Vadot 	phandle_t child;
175*7c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
176*7c569caaSEmmanuel Vadot 	u_int id[8];
177*7c569caaSEmmanuel Vadot 	char location[96];
178*7c569caaSEmmanuel Vadot 	int i = 0, j, len = 0, prop_len, prev_len = 0;
179*7c569caaSEmmanuel Vadot 
180*7c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
181*7c569caaSEmmanuel Vadot 
182*7c569caaSEmmanuel Vadot 	child = ofw_bus_get_node(dev);
183*7c569caaSEmmanuel Vadot 
184*7c569caaSEmmanuel Vadot 	/* Fill the sensor location property. */
185*7c569caaSEmmanuel Vadot 	prop_len = OF_getprop(child, "hwsensor-location", location,
186*7c569caaSEmmanuel Vadot 			      sizeof(location));
187*7c569caaSEmmanuel Vadot 	while (len < prop_len) {
188*7c569caaSEmmanuel Vadot 		if (sc->sc_sensors != NULL)
189*7c569caaSEmmanuel Vadot 			strcpy(sc->sc_sensors[i].therm.name, location + len);
190*7c569caaSEmmanuel Vadot 		prev_len = strlen(location + len) + 1;
191*7c569caaSEmmanuel Vadot 		len += prev_len;
192*7c569caaSEmmanuel Vadot 		i++;
193*7c569caaSEmmanuel Vadot 	}
194*7c569caaSEmmanuel Vadot 	if (sc->sc_sensors == NULL)
195*7c569caaSEmmanuel Vadot 		return (i);
196*7c569caaSEmmanuel Vadot 
197*7c569caaSEmmanuel Vadot 	/* Fill the sensor id property. */
198*7c569caaSEmmanuel Vadot 	prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
199*7c569caaSEmmanuel Vadot 	for (j = 0; j < i; j++)
200*7c569caaSEmmanuel Vadot 		sc->sc_sensors[j].id = (id[j] & 0xf);
201*7c569caaSEmmanuel Vadot 
202*7c569caaSEmmanuel Vadot 	/* Fill the sensor zone property. */
203*7c569caaSEmmanuel Vadot 	prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
204*7c569caaSEmmanuel Vadot 	for (j = 0; j < i; j++)
205*7c569caaSEmmanuel Vadot 		sc->sc_sensors[j].therm.zone = id[j];
206*7c569caaSEmmanuel Vadot 
207*7c569caaSEmmanuel Vadot 	/* Set up remaining sensor properties */
208*7c569caaSEmmanuel Vadot 	for (j = 0; j < i; j++) {
209*7c569caaSEmmanuel Vadot 		sc->sc_sensors[j].dev = dev;
210*7c569caaSEmmanuel Vadot 
211*7c569caaSEmmanuel Vadot 		/*
212*7c569caaSEmmanuel Vadot 		 * Target value for "KODIAK DIODE" (= northbridge die) should
213*7c569caaSEmmanuel Vadot 		 * be 64C (value from Linux). It operates fine at that
214*7c569caaSEmmanuel Vadot 		 * temperature and has limited responsivity to the fan aimed at
215*7c569caaSEmmanuel Vadot 		 * it, so no point in trying to cool it to 40C.
216*7c569caaSEmmanuel Vadot 		 */
217*7c569caaSEmmanuel Vadot 		if (strcmp(sc->sc_sensors[j].therm.name, "KODIAK DIODE") == 0)
218*7c569caaSEmmanuel Vadot 			sc->sc_sensors[j].therm.target_temp = 640 + ZERO_C_TO_K;
219*7c569caaSEmmanuel Vadot 		else
220*7c569caaSEmmanuel Vadot 			sc->sc_sensors[j].therm.target_temp = 400 + ZERO_C_TO_K;
221*7c569caaSEmmanuel Vadot 		sc->sc_sensors[j].therm.max_temp = 850 + ZERO_C_TO_K;
222*7c569caaSEmmanuel Vadot 
223*7c569caaSEmmanuel Vadot 		sc->sc_sensors[j].therm.read =
224*7c569caaSEmmanuel Vadot 		    (int (*)(struct pmac_therm *))(max6690_sensor_read);
225*7c569caaSEmmanuel Vadot 	}
226*7c569caaSEmmanuel Vadot 
227*7c569caaSEmmanuel Vadot 	return (i);
228*7c569caaSEmmanuel Vadot }
229*7c569caaSEmmanuel Vadot static int
max6690_attach(device_t dev)230*7c569caaSEmmanuel Vadot max6690_attach(device_t dev)
231*7c569caaSEmmanuel Vadot {
232*7c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
233*7c569caaSEmmanuel Vadot 
234*7c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
235*7c569caaSEmmanuel Vadot 
236*7c569caaSEmmanuel Vadot 	sc->enum_hook.ich_func = max6690_start;
237*7c569caaSEmmanuel Vadot 	sc->enum_hook.ich_arg = dev;
238*7c569caaSEmmanuel Vadot 
239*7c569caaSEmmanuel Vadot 	/* We have to wait until interrupts are enabled. I2C read and write
240*7c569caaSEmmanuel Vadot 	 * only works if the interrupts are available.
241*7c569caaSEmmanuel Vadot 	 * The unin/i2c is controlled by the htpic on unin. But this is not
242*7c569caaSEmmanuel Vadot 	 * the master. The openpic on mac-io is controlling the htpic.
243*7c569caaSEmmanuel Vadot 	 * This one gets attached after the mac-io probing and then the
244*7c569caaSEmmanuel Vadot 	 * interrupts will be available.
245*7c569caaSEmmanuel Vadot 	 */
246*7c569caaSEmmanuel Vadot 
247*7c569caaSEmmanuel Vadot 	if (config_intrhook_establish(&sc->enum_hook) != 0)
248*7c569caaSEmmanuel Vadot 		return (ENOMEM);
249*7c569caaSEmmanuel Vadot 
250*7c569caaSEmmanuel Vadot 	return (0);
251*7c569caaSEmmanuel Vadot }
252*7c569caaSEmmanuel Vadot 
253*7c569caaSEmmanuel Vadot static void
max6690_start(void * xdev)254*7c569caaSEmmanuel Vadot max6690_start(void *xdev)
255*7c569caaSEmmanuel Vadot {
256*7c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
257*7c569caaSEmmanuel Vadot 	struct sysctl_oid *oid, *sensroot_oid;
258*7c569caaSEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
259*7c569caaSEmmanuel Vadot 	char sysctl_desc[40], sysctl_name[32];
260*7c569caaSEmmanuel Vadot 	int i, j;
261*7c569caaSEmmanuel Vadot 
262*7c569caaSEmmanuel Vadot 	device_t dev = (device_t)xdev;
263*7c569caaSEmmanuel Vadot 
264*7c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
265*7c569caaSEmmanuel Vadot 
266*7c569caaSEmmanuel Vadot 	sc->sc_nsensors = 0;
267*7c569caaSEmmanuel Vadot 
268*7c569caaSEmmanuel Vadot 	/* Count the actual number of sensors. */
269*7c569caaSEmmanuel Vadot 	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
270*7c569caaSEmmanuel Vadot 
271*7c569caaSEmmanuel Vadot 	device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
272*7c569caaSEmmanuel Vadot 
273*7c569caaSEmmanuel Vadot 	if (sc->sc_nsensors == 0)
274*7c569caaSEmmanuel Vadot 		device_printf(dev, "WARNING: No MAX6690 sensors detected!\n");
275*7c569caaSEmmanuel Vadot 
276*7c569caaSEmmanuel Vadot 	sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct max6690_sensor),
277*7c569caaSEmmanuel Vadot 				 M_MAX6690, M_WAITOK | M_ZERO);
278*7c569caaSEmmanuel Vadot 
279*7c569caaSEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
280*7c569caaSEmmanuel Vadot 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
281*7c569caaSEmmanuel Vadot 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
282*7c569caaSEmmanuel Vadot 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "MAX6690 Sensor Information");
283*7c569caaSEmmanuel Vadot 
284*7c569caaSEmmanuel Vadot 	/* Now we can fill the properties into the allocated struct. */
285*7c569caaSEmmanuel Vadot 	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
286*7c569caaSEmmanuel Vadot 
287*7c569caaSEmmanuel Vadot 	/* Register with powermac_thermal */
288*7c569caaSEmmanuel Vadot 	for (i = 0; i < sc->sc_nsensors; i++)
289*7c569caaSEmmanuel Vadot 		pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
290*7c569caaSEmmanuel Vadot 
291*7c569caaSEmmanuel Vadot 	/* Add sysctls for the sensors. */
292*7c569caaSEmmanuel Vadot 	for (i = 0; i < sc->sc_nsensors; i++) {
293*7c569caaSEmmanuel Vadot 		for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
294*7c569caaSEmmanuel Vadot 			sysctl_name[j] =
295*7c569caaSEmmanuel Vadot 			    tolower(sc->sc_sensors[i].therm.name[j]);
296*7c569caaSEmmanuel Vadot 			if (isspace(sysctl_name[j]))
297*7c569caaSEmmanuel Vadot 				sysctl_name[j] = '_';
298*7c569caaSEmmanuel Vadot 		}
299*7c569caaSEmmanuel Vadot 		sysctl_name[j] = 0;
300*7c569caaSEmmanuel Vadot 
301*7c569caaSEmmanuel Vadot 		sprintf(sysctl_desc,"%s %s", sc->sc_sensors[i].therm.name,
302*7c569caaSEmmanuel Vadot 			"(C)");
303*7c569caaSEmmanuel Vadot 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
304*7c569caaSEmmanuel Vadot 		    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
305*7c569caaSEmmanuel Vadot 		    "Sensor Information");
306*7c569caaSEmmanuel Vadot 		/* I use i to pass the sensor id. */
307*7c569caaSEmmanuel Vadot 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
308*7c569caaSEmmanuel Vadot 				CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
309*7c569caaSEmmanuel Vadot 				dev, i % 2,
310*7c569caaSEmmanuel Vadot 				max6690_sensor_sysctl, "IK", sysctl_desc);
311*7c569caaSEmmanuel Vadot 
312*7c569caaSEmmanuel Vadot 	}
313*7c569caaSEmmanuel Vadot 	/* Dump sensor location & ID. */
314*7c569caaSEmmanuel Vadot 	if (bootverbose) {
315*7c569caaSEmmanuel Vadot 		device_printf(dev, "Sensors\n");
316*7c569caaSEmmanuel Vadot 		for (i = 0; i < sc->sc_nsensors; i++) {
317*7c569caaSEmmanuel Vadot 			device_printf(dev, "Location : %s ID: %d\n",
318*7c569caaSEmmanuel Vadot 				      sc->sc_sensors[i].therm.name,
319*7c569caaSEmmanuel Vadot 				      sc->sc_sensors[i].id);
320*7c569caaSEmmanuel Vadot 		}
321*7c569caaSEmmanuel Vadot 	}
322*7c569caaSEmmanuel Vadot 
323*7c569caaSEmmanuel Vadot 	config_intrhook_disestablish(&sc->enum_hook);
324*7c569caaSEmmanuel Vadot }
325*7c569caaSEmmanuel Vadot 
326*7c569caaSEmmanuel Vadot static int
max6690_sensor_read(struct max6690_sensor * sens)327*7c569caaSEmmanuel Vadot max6690_sensor_read(struct max6690_sensor *sens)
328*7c569caaSEmmanuel Vadot {
329*7c569caaSEmmanuel Vadot 	uint8_t reg_int = 0, reg_ext = 0;
330*7c569caaSEmmanuel Vadot 	uint8_t integer = 0;
331*7c569caaSEmmanuel Vadot 	uint8_t fraction = 0;
332*7c569caaSEmmanuel Vadot 	int err, temp;
333*7c569caaSEmmanuel Vadot 
334*7c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
335*7c569caaSEmmanuel Vadot 
336*7c569caaSEmmanuel Vadot 	sc = device_get_softc(sens->dev);
337*7c569caaSEmmanuel Vadot 
338*7c569caaSEmmanuel Vadot 	/* The internal sensor id's are even, the external are odd. */
339*7c569caaSEmmanuel Vadot 	if ((sens->id % 2) == 0) {
340*7c569caaSEmmanuel Vadot 		reg_int = MAX6690_INT_TEMP;
341*7c569caaSEmmanuel Vadot 		reg_ext = MAX6690_IEXT_TEMP;
342*7c569caaSEmmanuel Vadot 	} else {
343*7c569caaSEmmanuel Vadot 		reg_int = MAX6690_EXT_TEMP;
344*7c569caaSEmmanuel Vadot 		reg_ext = MAX6690_EEXT_TEMP;
345*7c569caaSEmmanuel Vadot 	}
346*7c569caaSEmmanuel Vadot 
347*7c569caaSEmmanuel Vadot 	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_int, &integer);
348*7c569caaSEmmanuel Vadot 
349*7c569caaSEmmanuel Vadot 	if (err < 0)
350*7c569caaSEmmanuel Vadot 		return (-1);
351*7c569caaSEmmanuel Vadot 
352*7c569caaSEmmanuel Vadot 	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_ext, &fraction);
353*7c569caaSEmmanuel Vadot 
354*7c569caaSEmmanuel Vadot 	if (err < 0)
355*7c569caaSEmmanuel Vadot 		return (-1);
356*7c569caaSEmmanuel Vadot 
357*7c569caaSEmmanuel Vadot 	fraction &= MAX6690_TEMP_MASK;
358*7c569caaSEmmanuel Vadot 
359*7c569caaSEmmanuel Vadot 	/* The temperature is in tenth kelvin, the fractional part resolution
360*7c569caaSEmmanuel Vadot 	   is 0.125.
361*7c569caaSEmmanuel Vadot 	*/
362*7c569caaSEmmanuel Vadot 	temp = (integer * 10) + (fraction >> 5) * 10 / 8;
363*7c569caaSEmmanuel Vadot 
364*7c569caaSEmmanuel Vadot 	return (temp + ZERO_C_TO_K);
365*7c569caaSEmmanuel Vadot }
366*7c569caaSEmmanuel Vadot 
367*7c569caaSEmmanuel Vadot static int
max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)368*7c569caaSEmmanuel Vadot max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)
369*7c569caaSEmmanuel Vadot {
370*7c569caaSEmmanuel Vadot 	device_t dev;
371*7c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
372*7c569caaSEmmanuel Vadot 	struct max6690_sensor *sens;
373*7c569caaSEmmanuel Vadot 	int error;
374*7c569caaSEmmanuel Vadot 	int temp;
375*7c569caaSEmmanuel Vadot 
376*7c569caaSEmmanuel Vadot 	dev = arg1;
377*7c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
378*7c569caaSEmmanuel Vadot 	sens = &sc->sc_sensors[arg2];
379*7c569caaSEmmanuel Vadot 
380*7c569caaSEmmanuel Vadot 	temp = max6690_sensor_read(sens);
381*7c569caaSEmmanuel Vadot 	if (temp < 0)
382*7c569caaSEmmanuel Vadot 		return (EIO);
383*7c569caaSEmmanuel Vadot 
384*7c569caaSEmmanuel Vadot 	error = sysctl_handle_int(oidp, &temp, 0, req);
385*7c569caaSEmmanuel Vadot 
386*7c569caaSEmmanuel Vadot 	return (error);
387*7c569caaSEmmanuel Vadot }
388