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