xref: /freebsd/sys/dev/iicbus/sensor/max6690.c (revision fdafd315)
17c569caaSEmmanuel Vadot /*-
27c569caaSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
37c569caaSEmmanuel Vadot  *
47c569caaSEmmanuel Vadot  * Copyright (c) 2010 Andreas Tobler
57c569caaSEmmanuel Vadot  *
67c569caaSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
77c569caaSEmmanuel Vadot  * modification, are permitted provided that the following conditions
87c569caaSEmmanuel Vadot  * are met:
97c569caaSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
107c569caaSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
117c569caaSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
127c569caaSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
137c569caaSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
147c569caaSEmmanuel Vadot  *
157c569caaSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167c569caaSEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177c569caaSEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187c569caaSEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197c569caaSEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
207c569caaSEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
217c569caaSEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
227c569caaSEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
237c569caaSEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247c569caaSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257c569caaSEmmanuel Vadot  * SUCH DAMAGE.
267c569caaSEmmanuel Vadot  */
277c569caaSEmmanuel Vadot 
287c569caaSEmmanuel Vadot #include <sys/param.h>
297c569caaSEmmanuel Vadot #include <sys/bus.h>
307c569caaSEmmanuel Vadot #include <sys/systm.h>
317c569caaSEmmanuel Vadot #include <sys/module.h>
327c569caaSEmmanuel Vadot #include <sys/callout.h>
337c569caaSEmmanuel Vadot #include <sys/conf.h>
347c569caaSEmmanuel Vadot #include <sys/cpu.h>
357c569caaSEmmanuel Vadot #include <sys/ctype.h>
367c569caaSEmmanuel Vadot #include <sys/kernel.h>
377c569caaSEmmanuel Vadot #include <sys/reboot.h>
387c569caaSEmmanuel Vadot #include <sys/rman.h>
397c569caaSEmmanuel Vadot #include <sys/sysctl.h>
407c569caaSEmmanuel Vadot #include <sys/limits.h>
417c569caaSEmmanuel Vadot 
427c569caaSEmmanuel Vadot #include <machine/bus.h>
437c569caaSEmmanuel Vadot #include <machine/md_var.h>
447c569caaSEmmanuel Vadot 
457c569caaSEmmanuel Vadot #include <dev/iicbus/iicbus.h>
467c569caaSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
477c569caaSEmmanuel Vadot 
487c569caaSEmmanuel Vadot #include <dev/ofw/openfirm.h>
497c569caaSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
507c569caaSEmmanuel Vadot #include <powerpc/powermac/powermac_thermal.h>
517c569caaSEmmanuel Vadot 
527c569caaSEmmanuel Vadot /* Inlet, Backside, U3 Heatsink sensor: MAX6690. */
537c569caaSEmmanuel Vadot 
547c569caaSEmmanuel Vadot #define MAX6690_INT_TEMP    0x0
557c569caaSEmmanuel Vadot #define MAX6690_EXT_TEMP    0x1
567c569caaSEmmanuel Vadot #define MAX6690_RSL_STATUS  0x2
577c569caaSEmmanuel Vadot #define MAX6690_EEXT_TEMP   0x10
587c569caaSEmmanuel Vadot #define MAX6690_IEXT_TEMP   0x11
597c569caaSEmmanuel Vadot #define MAX6690_TEMP_MASK   0xe0
607c569caaSEmmanuel Vadot 
617c569caaSEmmanuel Vadot struct max6690_sensor {
627c569caaSEmmanuel Vadot 	struct pmac_therm therm;
637c569caaSEmmanuel Vadot 	device_t dev;
647c569caaSEmmanuel Vadot 
657c569caaSEmmanuel Vadot 	int     id;
667c569caaSEmmanuel Vadot };
677c569caaSEmmanuel Vadot 
687c569caaSEmmanuel Vadot /* Regular bus attachment functions */
697c569caaSEmmanuel Vadot static int  max6690_probe(device_t);
707c569caaSEmmanuel Vadot static int  max6690_attach(device_t);
717c569caaSEmmanuel Vadot 
727c569caaSEmmanuel Vadot /* Utility functions */
737c569caaSEmmanuel Vadot static int  max6690_sensor_read(struct max6690_sensor *sens);
747c569caaSEmmanuel Vadot static int  max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS);
757c569caaSEmmanuel Vadot static void max6690_start(void *xdev);
767c569caaSEmmanuel Vadot static int  max6690_read(device_t dev, uint32_t addr, uint8_t reg,
777c569caaSEmmanuel Vadot 			 uint8_t *data);
787c569caaSEmmanuel Vadot 
797c569caaSEmmanuel Vadot struct max6690_softc {
807c569caaSEmmanuel Vadot 	device_t		sc_dev;
817c569caaSEmmanuel Vadot 	struct intr_config_hook enum_hook;
827c569caaSEmmanuel Vadot 	uint32_t                sc_addr;
837c569caaSEmmanuel Vadot 	struct max6690_sensor   *sc_sensors;
847c569caaSEmmanuel Vadot 	int                     sc_nsensors;
857c569caaSEmmanuel Vadot };
867c569caaSEmmanuel Vadot static device_method_t  max6690_methods[] = {
877c569caaSEmmanuel Vadot 	/* Device interface */
887c569caaSEmmanuel Vadot 	DEVMETHOD(device_probe,		max6690_probe),
897c569caaSEmmanuel Vadot 	DEVMETHOD(device_attach,	max6690_attach),
907c569caaSEmmanuel Vadot 	{ 0, 0 },
917c569caaSEmmanuel Vadot };
927c569caaSEmmanuel Vadot 
937c569caaSEmmanuel Vadot static driver_t max6690_driver = {
947c569caaSEmmanuel Vadot 	"max6690",
957c569caaSEmmanuel Vadot 	max6690_methods,
967c569caaSEmmanuel Vadot 	sizeof(struct max6690_softc)
977c569caaSEmmanuel Vadot };
987c569caaSEmmanuel Vadot 
997c569caaSEmmanuel Vadot DRIVER_MODULE(max6690, iicbus, max6690_driver, 0, 0);
1007c569caaSEmmanuel Vadot static MALLOC_DEFINE(M_MAX6690, "max6690", "Temp-Monitor MAX6690");
1017c569caaSEmmanuel Vadot 
1027c569caaSEmmanuel Vadot static int
max6690_read(device_t dev,uint32_t addr,uint8_t reg,uint8_t * data)1037c569caaSEmmanuel Vadot max6690_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
1047c569caaSEmmanuel Vadot {
1057c569caaSEmmanuel Vadot 	uint8_t buf[4];
1067c569caaSEmmanuel Vadot 	uint8_t busy[1], rsl;
1077c569caaSEmmanuel Vadot 	int err, try = 0;
1087c569caaSEmmanuel Vadot 
1097c569caaSEmmanuel Vadot 	/* Busy register RSL. */
1107c569caaSEmmanuel Vadot 	rsl = MAX6690_RSL_STATUS;
1117c569caaSEmmanuel Vadot 	/* first read the status register, 0x2. If busy, retry. */
1127c569caaSEmmanuel Vadot 	struct iic_msg msg[4] = {
1137c569caaSEmmanuel Vadot 	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &rsl },
1147c569caaSEmmanuel Vadot 	    { addr, IIC_M_RD, 1, busy },
1157c569caaSEmmanuel Vadot 	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
1167c569caaSEmmanuel Vadot 	    { addr, IIC_M_RD, 1, buf },
1177c569caaSEmmanuel Vadot 	};
1187c569caaSEmmanuel Vadot 
1197c569caaSEmmanuel Vadot 	for (;;)
1207c569caaSEmmanuel Vadot 	{
1217c569caaSEmmanuel Vadot 		err = iicbus_transfer(dev, msg, nitems(msg));
1227c569caaSEmmanuel Vadot 		if (err != 0)
1237c569caaSEmmanuel Vadot 			goto retry;
1247c569caaSEmmanuel Vadot 		if (busy[0] & 0x80)
1257c569caaSEmmanuel Vadot 			goto retry;
1267c569caaSEmmanuel Vadot 		/* Check for invalid value and retry. */
1277c569caaSEmmanuel Vadot 		if (buf[0] == 0xff)
1287c569caaSEmmanuel Vadot 			goto retry;
1297c569caaSEmmanuel Vadot 
1307c569caaSEmmanuel Vadot 		*data = *((uint8_t*)buf);
1317c569caaSEmmanuel Vadot 		return (0);
1327c569caaSEmmanuel Vadot 
1337c569caaSEmmanuel Vadot 	retry:
1347c569caaSEmmanuel Vadot 		if (++try > 5) {
1357c569caaSEmmanuel Vadot 			device_printf(dev, "iicbus read failed\n");
1367c569caaSEmmanuel Vadot 			return (-1);
1377c569caaSEmmanuel Vadot 		}
1387c569caaSEmmanuel Vadot 		pause("max6690_read", hz);
1397c569caaSEmmanuel Vadot 	}
1407c569caaSEmmanuel Vadot }
1417c569caaSEmmanuel Vadot 
1427c569caaSEmmanuel Vadot static int
max6690_probe(device_t dev)1437c569caaSEmmanuel Vadot max6690_probe(device_t dev)
1447c569caaSEmmanuel Vadot {
1457c569caaSEmmanuel Vadot 	const char  *name, *compatible;
1467c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
1477c569caaSEmmanuel Vadot 
1487c569caaSEmmanuel Vadot 	name = ofw_bus_get_name(dev);
1497c569caaSEmmanuel Vadot 	compatible = ofw_bus_get_compat(dev);
1507c569caaSEmmanuel Vadot 
1517c569caaSEmmanuel Vadot 	if (!name)
1527c569caaSEmmanuel Vadot 		return (ENXIO);
1537c569caaSEmmanuel Vadot 
1547c569caaSEmmanuel Vadot 	if (strcmp(name, "temp-monitor") != 0 ||
1557c569caaSEmmanuel Vadot 	    strcmp(compatible, "max6690") != 0)
1567c569caaSEmmanuel Vadot 		return (ENXIO);
1577c569caaSEmmanuel Vadot 
1587c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
1597c569caaSEmmanuel Vadot 	sc->sc_dev = dev;
1607c569caaSEmmanuel Vadot 	sc->sc_addr = iicbus_get_addr(dev);
1617c569caaSEmmanuel Vadot 
1627c569caaSEmmanuel Vadot 	device_set_desc(dev, "Temp-Monitor MAX6690");
1637c569caaSEmmanuel Vadot 
1647c569caaSEmmanuel Vadot 	return (0);
1657c569caaSEmmanuel Vadot }
1667c569caaSEmmanuel Vadot 
1677c569caaSEmmanuel Vadot /*
1687c569caaSEmmanuel Vadot  * This function returns the number of sensors. If we call it the second time
1697c569caaSEmmanuel Vadot  * and we have allocated memory for sc->sc_sensors, we fill in the properties.
1707c569caaSEmmanuel Vadot  */
1717c569caaSEmmanuel Vadot static int
max6690_fill_sensor_prop(device_t dev)1727c569caaSEmmanuel Vadot max6690_fill_sensor_prop(device_t dev)
1737c569caaSEmmanuel Vadot {
1747c569caaSEmmanuel Vadot 	phandle_t child;
1757c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
1767c569caaSEmmanuel Vadot 	u_int id[8];
1777c569caaSEmmanuel Vadot 	char location[96];
1787c569caaSEmmanuel Vadot 	int i = 0, j, len = 0, prop_len, prev_len = 0;
1797c569caaSEmmanuel Vadot 
1807c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
1817c569caaSEmmanuel Vadot 
1827c569caaSEmmanuel Vadot 	child = ofw_bus_get_node(dev);
1837c569caaSEmmanuel Vadot 
1847c569caaSEmmanuel Vadot 	/* Fill the sensor location property. */
1857c569caaSEmmanuel Vadot 	prop_len = OF_getprop(child, "hwsensor-location", location,
1867c569caaSEmmanuel Vadot 			      sizeof(location));
1877c569caaSEmmanuel Vadot 	while (len < prop_len) {
1887c569caaSEmmanuel Vadot 		if (sc->sc_sensors != NULL)
1897c569caaSEmmanuel Vadot 			strcpy(sc->sc_sensors[i].therm.name, location + len);
1907c569caaSEmmanuel Vadot 		prev_len = strlen(location + len) + 1;
1917c569caaSEmmanuel Vadot 		len += prev_len;
1927c569caaSEmmanuel Vadot 		i++;
1937c569caaSEmmanuel Vadot 	}
1947c569caaSEmmanuel Vadot 	if (sc->sc_sensors == NULL)
1957c569caaSEmmanuel Vadot 		return (i);
1967c569caaSEmmanuel Vadot 
1977c569caaSEmmanuel Vadot 	/* Fill the sensor id property. */
1987c569caaSEmmanuel Vadot 	prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
1997c569caaSEmmanuel Vadot 	for (j = 0; j < i; j++)
2007c569caaSEmmanuel Vadot 		sc->sc_sensors[j].id = (id[j] & 0xf);
2017c569caaSEmmanuel Vadot 
2027c569caaSEmmanuel Vadot 	/* Fill the sensor zone property. */
2037c569caaSEmmanuel Vadot 	prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
2047c569caaSEmmanuel Vadot 	for (j = 0; j < i; j++)
2057c569caaSEmmanuel Vadot 		sc->sc_sensors[j].therm.zone = id[j];
2067c569caaSEmmanuel Vadot 
2077c569caaSEmmanuel Vadot 	/* Set up remaining sensor properties */
2087c569caaSEmmanuel Vadot 	for (j = 0; j < i; j++) {
2097c569caaSEmmanuel Vadot 		sc->sc_sensors[j].dev = dev;
2107c569caaSEmmanuel Vadot 
2117c569caaSEmmanuel Vadot 		/*
2127c569caaSEmmanuel Vadot 		 * Target value for "KODIAK DIODE" (= northbridge die) should
2137c569caaSEmmanuel Vadot 		 * be 64C (value from Linux). It operates fine at that
2147c569caaSEmmanuel Vadot 		 * temperature and has limited responsivity to the fan aimed at
2157c569caaSEmmanuel Vadot 		 * it, so no point in trying to cool it to 40C.
2167c569caaSEmmanuel Vadot 		 */
2177c569caaSEmmanuel Vadot 		if (strcmp(sc->sc_sensors[j].therm.name, "KODIAK DIODE") == 0)
2187c569caaSEmmanuel Vadot 			sc->sc_sensors[j].therm.target_temp = 640 + ZERO_C_TO_K;
2197c569caaSEmmanuel Vadot 		else
2207c569caaSEmmanuel Vadot 			sc->sc_sensors[j].therm.target_temp = 400 + ZERO_C_TO_K;
2217c569caaSEmmanuel Vadot 		sc->sc_sensors[j].therm.max_temp = 850 + ZERO_C_TO_K;
2227c569caaSEmmanuel Vadot 
2237c569caaSEmmanuel Vadot 		sc->sc_sensors[j].therm.read =
2247c569caaSEmmanuel Vadot 		    (int (*)(struct pmac_therm *))(max6690_sensor_read);
2257c569caaSEmmanuel Vadot 	}
2267c569caaSEmmanuel Vadot 
2277c569caaSEmmanuel Vadot 	return (i);
2287c569caaSEmmanuel Vadot }
2297c569caaSEmmanuel Vadot static int
max6690_attach(device_t dev)2307c569caaSEmmanuel Vadot max6690_attach(device_t dev)
2317c569caaSEmmanuel Vadot {
2327c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
2337c569caaSEmmanuel Vadot 
2347c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
2357c569caaSEmmanuel Vadot 
2367c569caaSEmmanuel Vadot 	sc->enum_hook.ich_func = max6690_start;
2377c569caaSEmmanuel Vadot 	sc->enum_hook.ich_arg = dev;
2387c569caaSEmmanuel Vadot 
2397c569caaSEmmanuel Vadot 	/* We have to wait until interrupts are enabled. I2C read and write
2407c569caaSEmmanuel Vadot 	 * only works if the interrupts are available.
2417c569caaSEmmanuel Vadot 	 * The unin/i2c is controlled by the htpic on unin. But this is not
2427c569caaSEmmanuel Vadot 	 * the master. The openpic on mac-io is controlling the htpic.
2437c569caaSEmmanuel Vadot 	 * This one gets attached after the mac-io probing and then the
2447c569caaSEmmanuel Vadot 	 * interrupts will be available.
2457c569caaSEmmanuel Vadot 	 */
2467c569caaSEmmanuel Vadot 
2477c569caaSEmmanuel Vadot 	if (config_intrhook_establish(&sc->enum_hook) != 0)
2487c569caaSEmmanuel Vadot 		return (ENOMEM);
2497c569caaSEmmanuel Vadot 
2507c569caaSEmmanuel Vadot 	return (0);
2517c569caaSEmmanuel Vadot }
2527c569caaSEmmanuel Vadot 
2537c569caaSEmmanuel Vadot static void
max6690_start(void * xdev)2547c569caaSEmmanuel Vadot max6690_start(void *xdev)
2557c569caaSEmmanuel Vadot {
2567c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
2577c569caaSEmmanuel Vadot 	struct sysctl_oid *oid, *sensroot_oid;
2587c569caaSEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
2597c569caaSEmmanuel Vadot 	char sysctl_desc[40], sysctl_name[32];
2607c569caaSEmmanuel Vadot 	int i, j;
2617c569caaSEmmanuel Vadot 
2627c569caaSEmmanuel Vadot 	device_t dev = (device_t)xdev;
2637c569caaSEmmanuel Vadot 
2647c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
2657c569caaSEmmanuel Vadot 
2667c569caaSEmmanuel Vadot 	sc->sc_nsensors = 0;
2677c569caaSEmmanuel Vadot 
2687c569caaSEmmanuel Vadot 	/* Count the actual number of sensors. */
2697c569caaSEmmanuel Vadot 	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
2707c569caaSEmmanuel Vadot 
2717c569caaSEmmanuel Vadot 	device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
2727c569caaSEmmanuel Vadot 
2737c569caaSEmmanuel Vadot 	if (sc->sc_nsensors == 0)
2747c569caaSEmmanuel Vadot 		device_printf(dev, "WARNING: No MAX6690 sensors detected!\n");
2757c569caaSEmmanuel Vadot 
2767c569caaSEmmanuel Vadot 	sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct max6690_sensor),
2777c569caaSEmmanuel Vadot 				 M_MAX6690, M_WAITOK | M_ZERO);
2787c569caaSEmmanuel Vadot 
2797c569caaSEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
2807c569caaSEmmanuel Vadot 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
2817c569caaSEmmanuel Vadot 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
2827c569caaSEmmanuel Vadot 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "MAX6690 Sensor Information");
2837c569caaSEmmanuel Vadot 
2847c569caaSEmmanuel Vadot 	/* Now we can fill the properties into the allocated struct. */
2857c569caaSEmmanuel Vadot 	sc->sc_nsensors = max6690_fill_sensor_prop(dev);
2867c569caaSEmmanuel Vadot 
2877c569caaSEmmanuel Vadot 	/* Register with powermac_thermal */
2887c569caaSEmmanuel Vadot 	for (i = 0; i < sc->sc_nsensors; i++)
2897c569caaSEmmanuel Vadot 		pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
2907c569caaSEmmanuel Vadot 
2917c569caaSEmmanuel Vadot 	/* Add sysctls for the sensors. */
2927c569caaSEmmanuel Vadot 	for (i = 0; i < sc->sc_nsensors; i++) {
2937c569caaSEmmanuel Vadot 		for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
2947c569caaSEmmanuel Vadot 			sysctl_name[j] =
2957c569caaSEmmanuel Vadot 			    tolower(sc->sc_sensors[i].therm.name[j]);
2967c569caaSEmmanuel Vadot 			if (isspace(sysctl_name[j]))
2977c569caaSEmmanuel Vadot 				sysctl_name[j] = '_';
2987c569caaSEmmanuel Vadot 		}
2997c569caaSEmmanuel Vadot 		sysctl_name[j] = 0;
3007c569caaSEmmanuel Vadot 
3017c569caaSEmmanuel Vadot 		sprintf(sysctl_desc,"%s %s", sc->sc_sensors[i].therm.name,
3027c569caaSEmmanuel Vadot 			"(C)");
3037c569caaSEmmanuel Vadot 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
3047c569caaSEmmanuel Vadot 		    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3057c569caaSEmmanuel Vadot 		    "Sensor Information");
3067c569caaSEmmanuel Vadot 		/* I use i to pass the sensor id. */
3077c569caaSEmmanuel Vadot 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
3087c569caaSEmmanuel Vadot 				CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
3097c569caaSEmmanuel Vadot 				dev, i % 2,
3107c569caaSEmmanuel Vadot 				max6690_sensor_sysctl, "IK", sysctl_desc);
3117c569caaSEmmanuel Vadot 
3127c569caaSEmmanuel Vadot 	}
3137c569caaSEmmanuel Vadot 	/* Dump sensor location & ID. */
3147c569caaSEmmanuel Vadot 	if (bootverbose) {
3157c569caaSEmmanuel Vadot 		device_printf(dev, "Sensors\n");
3167c569caaSEmmanuel Vadot 		for (i = 0; i < sc->sc_nsensors; i++) {
3177c569caaSEmmanuel Vadot 			device_printf(dev, "Location : %s ID: %d\n",
3187c569caaSEmmanuel Vadot 				      sc->sc_sensors[i].therm.name,
3197c569caaSEmmanuel Vadot 				      sc->sc_sensors[i].id);
3207c569caaSEmmanuel Vadot 		}
3217c569caaSEmmanuel Vadot 	}
3227c569caaSEmmanuel Vadot 
3237c569caaSEmmanuel Vadot 	config_intrhook_disestablish(&sc->enum_hook);
3247c569caaSEmmanuel Vadot }
3257c569caaSEmmanuel Vadot 
3267c569caaSEmmanuel Vadot static int
max6690_sensor_read(struct max6690_sensor * sens)3277c569caaSEmmanuel Vadot max6690_sensor_read(struct max6690_sensor *sens)
3287c569caaSEmmanuel Vadot {
3297c569caaSEmmanuel Vadot 	uint8_t reg_int = 0, reg_ext = 0;
3307c569caaSEmmanuel Vadot 	uint8_t integer = 0;
3317c569caaSEmmanuel Vadot 	uint8_t fraction = 0;
3327c569caaSEmmanuel Vadot 	int err, temp;
3337c569caaSEmmanuel Vadot 
3347c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
3357c569caaSEmmanuel Vadot 
3367c569caaSEmmanuel Vadot 	sc = device_get_softc(sens->dev);
3377c569caaSEmmanuel Vadot 
3387c569caaSEmmanuel Vadot 	/* The internal sensor id's are even, the external are odd. */
3397c569caaSEmmanuel Vadot 	if ((sens->id % 2) == 0) {
3407c569caaSEmmanuel Vadot 		reg_int = MAX6690_INT_TEMP;
3417c569caaSEmmanuel Vadot 		reg_ext = MAX6690_IEXT_TEMP;
3427c569caaSEmmanuel Vadot 	} else {
3437c569caaSEmmanuel Vadot 		reg_int = MAX6690_EXT_TEMP;
3447c569caaSEmmanuel Vadot 		reg_ext = MAX6690_EEXT_TEMP;
3457c569caaSEmmanuel Vadot 	}
3467c569caaSEmmanuel Vadot 
3477c569caaSEmmanuel Vadot 	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_int, &integer);
3487c569caaSEmmanuel Vadot 
3497c569caaSEmmanuel Vadot 	if (err < 0)
3507c569caaSEmmanuel Vadot 		return (-1);
3517c569caaSEmmanuel Vadot 
3527c569caaSEmmanuel Vadot 	err = max6690_read(sc->sc_dev, sc->sc_addr, reg_ext, &fraction);
3537c569caaSEmmanuel Vadot 
3547c569caaSEmmanuel Vadot 	if (err < 0)
3557c569caaSEmmanuel Vadot 		return (-1);
3567c569caaSEmmanuel Vadot 
3577c569caaSEmmanuel Vadot 	fraction &= MAX6690_TEMP_MASK;
3587c569caaSEmmanuel Vadot 
3597c569caaSEmmanuel Vadot 	/* The temperature is in tenth kelvin, the fractional part resolution
3607c569caaSEmmanuel Vadot 	   is 0.125.
3617c569caaSEmmanuel Vadot 	*/
3627c569caaSEmmanuel Vadot 	temp = (integer * 10) + (fraction >> 5) * 10 / 8;
3637c569caaSEmmanuel Vadot 
3647c569caaSEmmanuel Vadot 	return (temp + ZERO_C_TO_K);
3657c569caaSEmmanuel Vadot }
3667c569caaSEmmanuel Vadot 
3677c569caaSEmmanuel Vadot static int
max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)3687c569caaSEmmanuel Vadot max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)
3697c569caaSEmmanuel Vadot {
3707c569caaSEmmanuel Vadot 	device_t dev;
3717c569caaSEmmanuel Vadot 	struct max6690_softc *sc;
3727c569caaSEmmanuel Vadot 	struct max6690_sensor *sens;
3737c569caaSEmmanuel Vadot 	int error;
3747c569caaSEmmanuel Vadot 	int temp;
3757c569caaSEmmanuel Vadot 
3767c569caaSEmmanuel Vadot 	dev = arg1;
3777c569caaSEmmanuel Vadot 	sc = device_get_softc(dev);
3787c569caaSEmmanuel Vadot 	sens = &sc->sc_sensors[arg2];
3797c569caaSEmmanuel Vadot 
3807c569caaSEmmanuel Vadot 	temp = max6690_sensor_read(sens);
3817c569caaSEmmanuel Vadot 	if (temp < 0)
3827c569caaSEmmanuel Vadot 		return (EIO);
3837c569caaSEmmanuel Vadot 
3847c569caaSEmmanuel Vadot 	error = sysctl_handle_int(oidp, &temp, 0, req);
3857c569caaSEmmanuel Vadot 
3867c569caaSEmmanuel Vadot 	return (error);
3877c569caaSEmmanuel Vadot }
388