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