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