xref: /freebsd/sys/powerpc/powermac/fcu.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Andreas Tobler
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/callout.h>
37 #include <sys/conf.h>
38 #include <sys/cpu.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/limits.h>
45 
46 #include <machine/bus.h>
47 #include <machine/md_var.h>
48 
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/iicbus/iiconf.h>
51 
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <powerpc/powermac/powermac_thermal.h>
55 
56 /* FCU registers
57  * /u3@0,f8000000/i2c@f8001000/fan@15e
58  */
59 #define FCU_RPM_FAIL      0x0b      /* fans states in bits 0<1-6>7 */
60 #define FCU_RPM_AVAILABLE 0x0c
61 #define FCU_RPM_ACTIVE    0x0d
62 #define FCU_RPM_READ(x)   0x11 + (x) * 2
63 #define FCU_RPM_SET(x)    0x10 + (x) * 2
64 
65 #define FCU_PWM_FAIL      0x2b
66 #define FCU_PWM_AVAILABLE 0x2c
67 #define FCU_PWM_ACTIVE    0x2d
68 #define FCU_PWM_RPM(x)    0x31 + (x) * 2 /* Get RPM. */
69 #define FCU_PWM_SGET(x)   0x30 + (x) * 2 /* Set or get PWM. */
70 
71 struct fcu_fan {
72 	struct	pmac_fan fan;
73 	device_t dev;
74 
75 	int     id;
76 	enum {
77 		FCU_FAN_RPM,
78 		FCU_FAN_PWM
79 	} type;
80 	int     setpoint;
81 	int     rpm;
82 };
83 
84 struct fcu_softc {
85 	device_t		sc_dev;
86 	struct intr_config_hook enum_hook;
87 	uint32_t                sc_addr;
88 	struct fcu_fan		*sc_fans;
89 	int			sc_nfans;
90 };
91 
92 /* We can read the PWM and the RPM from a PWM controlled fan.
93  * Offer both values via sysctl.
94  */
95 enum {
96 	FCU_PWM_SYSCTL_PWM   = 1 << 8,
97 	FCU_PWM_SYSCTL_RPM   = 2 << 8
98 };
99 
100 static int fcu_rpm_shift;
101 
102 /* Regular bus attachment functions */
103 static int  fcu_probe(device_t);
104 static int  fcu_attach(device_t);
105 
106 /* Utility functions */
107 static void fcu_attach_fans(device_t dev);
108 static int  fcu_fill_fan_prop(device_t dev);
109 static int  fcu_fan_set_rpm(struct fcu_fan *fan, int rpm);
110 static int  fcu_fan_get_rpm(struct fcu_fan *fan);
111 static int  fcu_fan_set_pwm(struct fcu_fan *fan, int pwm);
112 static int  fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm,
113 			    int *rpm);
114 static int  fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS);
115 static void fcu_start(void *xdev);
116 static int  fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buf,
117 		      int len);
118 static int  fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data);
119 
120 static device_method_t  fcu_methods[] = {
121 	/* Device interface */
122 	DEVMETHOD(device_probe,		fcu_probe),
123 	DEVMETHOD(device_attach,	fcu_attach),
124 	{ 0, 0 },
125 };
126 
127 static driver_t fcu_driver = {
128 	"fcu",
129 	fcu_methods,
130 	sizeof(struct fcu_softc)
131 };
132 
133 static devclass_t fcu_devclass;
134 
135 DRIVER_MODULE(fcu, iicbus, fcu_driver, fcu_devclass, 0, 0);
136 static MALLOC_DEFINE(M_FCU, "fcu", "FCU Sensor Information");
137 
138 static int
139 fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff,
140 	  int len)
141 {
142 	unsigned char buf[4];
143 	int try = 0;
144 
145 	struct iic_msg msg[] = {
146 		{ addr, IIC_M_WR, 0, buf }
147 	};
148 
149 	msg[0].len = len + 1;
150 	buf[0] = reg;
151 	memcpy(buf + 1, buff, len);
152 
153 	for (;;)
154 	{
155 		if (iicbus_transfer(dev, msg, 1) == 0)
156 			return (0);
157 
158 		if (++try > 5) {
159 			device_printf(dev, "iicbus write failed\n");
160 			return (-1);
161 		}
162 		pause("fcu_write", hz);
163 	}
164 }
165 
166 static int
167 fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
168 {
169 	uint8_t buf[4];
170 	int err, try = 0;
171 
172 	struct iic_msg msg[2] = {
173 	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
174 	    { addr, IIC_M_RD, 1, buf },
175 	};
176 
177 	for (;;)
178 	{
179 		  err = iicbus_transfer(dev, msg, 2);
180 		  if (err != 0)
181 			  goto retry;
182 
183 		  *data = *((uint8_t*)buf);
184 		  return (0);
185 	retry:
186 		  if (++try > 5) {
187 			  device_printf(dev, "iicbus read failed\n");
188 			  return (-1);
189 		  }
190 		  pause("fcu_read_1", hz);
191 	}
192 }
193 
194 static int
195 fcu_probe(device_t dev)
196 {
197 	const char  *name, *compatible;
198 	struct fcu_softc *sc;
199 
200 	name = ofw_bus_get_name(dev);
201 	compatible = ofw_bus_get_compat(dev);
202 
203 	if (!name)
204 		return (ENXIO);
205 
206 	if (strcmp(name, "fan") != 0 || strcmp(compatible, "fcu") != 0)
207 		return (ENXIO);
208 
209 	sc = device_get_softc(dev);
210 	sc->sc_dev = dev;
211 	sc->sc_addr = iicbus_get_addr(dev);
212 
213 	device_set_desc(dev, "Apple Fan Control Unit");
214 
215 	return (0);
216 }
217 
218 static int
219 fcu_attach(device_t dev)
220 {
221 	struct fcu_softc *sc;
222 
223 	sc = device_get_softc(dev);
224 
225 	sc->enum_hook.ich_func = fcu_start;
226 	sc->enum_hook.ich_arg = dev;
227 
228 	/* We have to wait until interrupts are enabled. I2C read and write
229 	 * only works if the interrupts are available.
230 	 * The unin/i2c is controlled by the htpic on unin. But this is not
231 	 * the master. The openpic on mac-io is controlling the htpic.
232 	 * This one gets attached after the mac-io probing and then the
233 	 * interrupts will be available.
234 	 */
235 
236 	if (config_intrhook_establish(&sc->enum_hook) != 0)
237 		return (ENOMEM);
238 
239 	return (0);
240 }
241 
242 static void
243 fcu_start(void *xdev)
244 {
245 	unsigned char buf[1] = { 0xff };
246 	struct fcu_softc *sc;
247 
248 	device_t dev = (device_t)xdev;
249 
250 	sc = device_get_softc(dev);
251 
252 	/* Start the fcu device. */
253 	fcu_write(sc->sc_dev, sc->sc_addr, 0xe, buf, 1);
254 	fcu_write(sc->sc_dev, sc->sc_addr, 0x2e, buf, 1);
255 	fcu_read_1(sc->sc_dev, sc->sc_addr, 0, buf);
256 	fcu_rpm_shift = (buf[0] == 1) ? 2 : 3;
257 
258 	device_printf(dev, "FCU initialized, RPM shift: %d\n",
259 		      fcu_rpm_shift);
260 
261 	/* Detect and attach child devices. */
262 
263 	fcu_attach_fans(dev);
264 
265 	config_intrhook_disestablish(&sc->enum_hook);
266 
267 }
268 
269 static int
270 fcu_fan_set_rpm(struct fcu_fan *fan, int rpm)
271 {
272 	uint8_t reg;
273 	struct fcu_softc *sc;
274 	unsigned char buf[2];
275 
276 	sc = device_get_softc(fan->dev);
277 
278 	/* Clamp to allowed range */
279 	rpm = max(fan->fan.min_rpm, rpm);
280 	rpm = min(fan->fan.max_rpm, rpm);
281 
282 	if (fan->type == FCU_FAN_RPM) {
283 		reg = FCU_RPM_SET(fan->id);
284 		fan->setpoint = rpm;
285 	} else {
286 		device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
287 		return (ENXIO);
288 	}
289 
290 	buf[0] = rpm >> (8 - fcu_rpm_shift);
291 	buf[1] = rpm << fcu_rpm_shift;
292 
293 	if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 2) < 0)
294 		return (EIO);
295 
296 	return (0);
297 }
298 
299 static int
300 fcu_fan_get_rpm(struct fcu_fan *fan)
301 {
302 	uint8_t reg;
303 	struct fcu_softc *sc;
304 	uint8_t buff[2] = { 0, 0 };
305 	uint8_t active = 0, avail = 0, fail = 0;
306 	int rpm;
307 
308 	sc = device_get_softc(fan->dev);
309 
310 	if (fan->type == FCU_FAN_RPM) {
311 		/* Check if the fan is available. */
312 		reg = FCU_RPM_AVAILABLE;
313 		if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0)
314 			return (-1);
315 		if ((avail & (1 << fan->id)) == 0) {
316 			device_printf(fan->dev,
317 			    "RPM Fan not available ID: %d\n", fan->id);
318 			return (-1);
319 		}
320 		/* Check if we have a failed fan. */
321 		reg = FCU_RPM_FAIL;
322 		if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
323 			return (-1);
324 		if ((fail & (1 << fan->id)) != 0) {
325 			device_printf(fan->dev,
326 			    "RPM Fan failed ID: %d\n", fan->id);
327 			return (-1);
328 		}
329 		/* Check if fan is active. */
330 		reg = FCU_RPM_ACTIVE;
331 		if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0)
332 			return (-1);
333 		if ((active & (1 << fan->id)) == 0) {
334 			device_printf(fan->dev, "RPM Fan not active ID: %d\n",
335 				      fan->id);
336 			return (-1);
337 		}
338 		reg = FCU_RPM_READ(fan->id);
339 
340 	} else {
341 		device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
342 		return (-1);
343 	}
344 
345 	/* It seems that we can read the fans rpm. */
346 	if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buff) < 0)
347 		return (-1);
348 
349 	rpm = (buff[0] << (8 - fcu_rpm_shift)) | buff[1] >> fcu_rpm_shift;
350 
351 	return (rpm);
352 }
353 
354 static int
355 fcu_fan_set_pwm(struct fcu_fan *fan, int pwm)
356 {
357 	uint8_t reg;
358 	struct fcu_softc *sc;
359 	uint8_t buf[2];
360 
361 	sc = device_get_softc(fan->dev);
362 
363 	/* Clamp to allowed range */
364 	pwm = max(fan->fan.min_rpm, pwm);
365 	pwm = min(fan->fan.max_rpm, pwm);
366 
367 	if (fan->type == FCU_FAN_PWM) {
368 		reg = FCU_PWM_SGET(fan->id);
369 		if (pwm > 100)
370 			pwm = 100;
371 		if (pwm < 30)
372 			pwm = 30;
373 		fan->setpoint = pwm;
374 	} else {
375 		device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
376 		return (EIO);
377 	}
378 
379 	buf[0] = (pwm * 2550) / 1000;
380 
381 	if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 1) < 0)
382 		return (EIO);
383 	return (0);
384 }
385 
386 static int
387 fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm, int *rpm)
388 {
389 	uint8_t reg;
390 	struct fcu_softc *sc;
391 	uint8_t buf[2];
392 	uint8_t active = 0, avail = 0, fail = 0;
393 
394 	sc = device_get_softc(dev);
395 
396 	if (fan->type == FCU_FAN_PWM) {
397 		/* Check if the fan is available. */
398 		reg = FCU_PWM_AVAILABLE;
399 		if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0)
400 			return (-1);
401 		if ((avail & (1 << fan->id)) == 0) {
402 			device_printf(dev, "PWM Fan not available ID: %d\n",
403 				      fan->id);
404 			return (-1);
405 		}
406 		/* Check if we have a failed fan. */
407 		reg = FCU_PWM_FAIL;
408 		if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
409 			return (-1);
410 		if ((fail & (1 << fan->id)) != 0) {
411 			device_printf(dev, "PWM Fan failed ID: %d\n", fan->id);
412 			return (-1);
413 		}
414 		/* Check if fan is active. */
415 		reg = FCU_PWM_ACTIVE;
416 		if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0)
417 			return (-1);
418 		if ((active & (1 << fan->id)) == 0) {
419 			device_printf(dev, "PWM Fan not active ID: %d\n",
420 				      fan->id);
421 			return (-1);
422 		}
423 		reg = FCU_PWM_SGET(fan->id);
424 	} else {
425 		device_printf(dev, "Unknown fan type: %d\n", fan->type);
426 		return (EIO);
427 	}
428 
429 	/* It seems that we can read the fans pwm. */
430 	if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
431 		return (-1);
432 
433 	*pwm = (buf[0] * 1000) / 2550;
434 
435 	/* Now read the rpm. */
436 	reg = FCU_PWM_RPM(fan->id);
437 	if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
438 		return (-1);
439 
440 	*rpm = (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
441 
442 	return (0);
443 }
444 
445 /*
446  * This function returns the number of fans. If we call it the second time
447  * and we have allocated memory for sc->sc_fans, we fill in the properties.
448  */
449 static int
450 fcu_fill_fan_prop(device_t dev)
451 {
452 	phandle_t child;
453 	struct fcu_softc *sc;
454 	u_int id[8];
455 	char location[96];
456 	char type[64];
457 	int i = 0, j, len = 0, prop_len, prev_len = 0;
458 
459 	sc = device_get_softc(dev);
460 
461 	child = ofw_bus_get_node(dev);
462 
463 	/* Fill the fan location property. */
464 	prop_len = OF_getprop(child, "hwctrl-location", location,
465 			      sizeof(location));
466 	while (len < prop_len) {
467 		if (sc->sc_fans != NULL) {
468 			strcpy(sc->sc_fans[i].fan.name, location + len);
469 		}
470 		prev_len = strlen(location + len) + 1;
471 		len += prev_len;
472 		i++;
473 	}
474 	if (sc->sc_fans == NULL)
475 		return (i);
476 
477 	/* Fill the fan type property. */
478 	len = 0;
479 	i = 0;
480 	prev_len = 0;
481 	prop_len = OF_getprop(child, "hwctrl-type", type, sizeof(type));
482 	while (len < prop_len) {
483 		if (strcmp(type + len, "fan-rpm") == 0)
484 			sc->sc_fans[i].type = FCU_FAN_RPM;
485 		else
486 			sc->sc_fans[i].type = FCU_FAN_PWM;
487 		prev_len = strlen(type + len) + 1;
488 		len += prev_len;
489 		i++;
490 	}
491 
492 	/* Fill the fan ID property. */
493 	prop_len = OF_getprop(child, "hwctrl-id", id, sizeof(id));
494 	for (j = 0; j < i; j++)
495 		sc->sc_fans[j].id = ((id[j] >> 8) & 0x0f) % 8;
496 
497 	/* Fill the fan zone property. */
498 	prop_len = OF_getprop(child, "hwctrl-zone", id, sizeof(id));
499 	for (j = 0; j < i; j++)
500 		sc->sc_fans[j].fan.zone = id[j];
501 
502 	/* Finish setting up fan properties */
503 	for (j = 0; j < i; j++) {
504 		sc->sc_fans[j].dev = sc->sc_dev;
505 		if (sc->sc_fans[j].type == FCU_FAN_RPM) {
506 			sc->sc_fans[j].fan.min_rpm = 4800 >> fcu_rpm_shift;
507 			sc->sc_fans[j].fan.max_rpm = 56000 >> fcu_rpm_shift;
508 			sc->sc_fans[j].setpoint =
509 			    fcu_fan_get_rpm(&sc->sc_fans[j]);
510 			sc->sc_fans[j].fan.read =
511 			    (int (*)(struct pmac_fan *))(fcu_fan_get_rpm);
512 			sc->sc_fans[j].fan.set =
513 			    (int (*)(struct pmac_fan *, int))(fcu_fan_set_rpm);
514 		} else {
515 			sc->sc_fans[j].fan.min_rpm = 30;	/* Percent */
516 			sc->sc_fans[j].fan.max_rpm = 100;
517 			sc->sc_fans[j].fan.read = NULL;
518 			sc->sc_fans[j].fan.set =
519 			    (int (*)(struct pmac_fan *, int))(fcu_fan_set_pwm);
520 		}
521 		sc->sc_fans[j].fan.default_rpm = sc->sc_fans[j].fan.max_rpm;
522 	}
523 
524 	return (i);
525 }
526 
527 static int
528 fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
529 {
530 	device_t fcu;
531 	struct fcu_softc *sc;
532 	struct fcu_fan *fan;
533 	int rpm = 0, pwm = 0, error = 0;
534 
535 	fcu = arg1;
536 	sc = device_get_softc(fcu);
537 	fan = &sc->sc_fans[arg2 & 0x00ff];
538 	if (fan->type == FCU_FAN_RPM) {
539 		rpm = fcu_fan_get_rpm(fan);
540 		if (rpm < 0)
541 			return (EIO);
542 		error = sysctl_handle_int(oidp, &rpm, 0, req);
543 	} else {
544 		error = fcu_fan_get_pwm(fcu, fan, &pwm, &rpm);
545 		if (error < 0)
546 			return (EIO);
547 
548 		switch (arg2 & 0xff00) {
549 		case FCU_PWM_SYSCTL_PWM:
550 			error = sysctl_handle_int(oidp, &pwm, 0, req);
551 			break;
552 		case FCU_PWM_SYSCTL_RPM:
553 			error = sysctl_handle_int(oidp, &rpm, 0, req);
554 			break;
555 		default:
556 			/* This should never happen */
557 			return (EINVAL);
558 		}
559 	}
560 
561 	/* We can only read the RPM from a PWM controlled fan, so return. */
562 	if ((arg2 & 0xff00) == FCU_PWM_SYSCTL_RPM)
563 		return (0);
564 
565 	if (error || !req->newptr)
566 		return (error);
567 
568 	if (fan->type == FCU_FAN_RPM)
569 		return (fcu_fan_set_rpm(fan, rpm));
570 	else
571 		return (fcu_fan_set_pwm(fan, pwm));
572 }
573 
574 static void
575 fcu_attach_fans(device_t dev)
576 {
577 	struct fcu_softc *sc;
578 	struct sysctl_oid *oid, *fanroot_oid;
579 	struct sysctl_ctx_list *ctx;
580 	char sysctl_name[32];
581 	int i, j;
582 
583 	sc = device_get_softc(dev);
584 
585 	sc->sc_nfans = 0;
586 
587 	/* Count the actual number of fans. */
588 	sc->sc_nfans = fcu_fill_fan_prop(dev);
589 
590 	device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
591 
592 	if (sc->sc_nfans == 0) {
593 		device_printf(dev, "WARNING: No fans detected!\n");
594 		return;
595 	}
596 
597 	sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct fcu_fan), M_FCU,
598 			     M_WAITOK | M_ZERO);
599 
600 	ctx = device_get_sysctl_ctx(dev);
601 	fanroot_oid = SYSCTL_ADD_NODE(ctx,
602 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
603 	    CTLFLAG_RD, 0, "FCU Fan Information");
604 
605 	/* Now we can fill the properties into the allocated struct. */
606 	sc->sc_nfans = fcu_fill_fan_prop(dev);
607 
608 	/* Register fans with pmac_thermal */
609 	for (i = 0; i < sc->sc_nfans; i++)
610 		pmac_thermal_fan_register(&sc->sc_fans[i].fan);
611 
612 	/* Add sysctls for the fans. */
613 	for (i = 0; i < sc->sc_nfans; i++) {
614 		for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) {
615 			sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]);
616 			if (isspace(sysctl_name[j]))
617 				sysctl_name[j] = '_';
618 		}
619 		sysctl_name[j] = 0;
620 
621 		if (sc->sc_fans[i].type == FCU_FAN_RPM) {
622 			oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
623 					      OID_AUTO, sysctl_name,
624 					      CTLFLAG_RD, 0, "Fan Information");
625 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
626 				       "minrpm", CTLFLAG_RD,
627 				       &(sc->sc_fans[i].fan.min_rpm), 0,
628 				       "Minimum allowed RPM");
629 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
630 				       "maxrpm", CTLFLAG_RD,
631 				       &(sc->sc_fans[i].fan.max_rpm), 0,
632 				       "Maximum allowed RPM");
633 			/* I use i to pass the fan id. */
634 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
635 					"rpm", CTLTYPE_INT | CTLFLAG_RW, dev, i,
636 					fcu_fanrpm_sysctl, "I", "Fan RPM");
637 		} else {
638 			fcu_fan_get_pwm(dev, &sc->sc_fans[i],
639 					&sc->sc_fans[i].setpoint,
640 					&sc->sc_fans[i].rpm);
641 
642 			oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
643 					      OID_AUTO, sysctl_name,
644 					      CTLFLAG_RD, 0, "Fan Information");
645 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
646 				       "minpwm", CTLFLAG_RD,
647 				       &(sc->sc_fans[i].fan.min_rpm), 0,
648 				       "Minimum allowed PWM in %");
649 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
650 				       "maxpwm", CTLFLAG_RD,
651 				       &(sc->sc_fans[i].fan.max_rpm), 0,
652 				       "Maximum allowed PWM in %");
653 			/* I use i to pass the fan id or'ed with the type
654 			 * of info I want to display/modify.
655 			 */
656 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
657 					"pwm", CTLTYPE_INT | CTLFLAG_RW, dev,
658 					FCU_PWM_SYSCTL_PWM | i,
659 					fcu_fanrpm_sysctl, "I", "Fan PWM in %");
660 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
661 					"rpm", CTLTYPE_INT | CTLFLAG_RD, dev,
662 					FCU_PWM_SYSCTL_RPM | i,
663 					fcu_fanrpm_sysctl, "I", "Fan RPM");
664 		}
665 	}
666 
667 	/* Dump fan location, type & RPM. */
668 	if (bootverbose) {
669 		device_printf(dev, "Fans\n");
670 		for (i = 0; i < sc->sc_nfans; i++) {
671 			device_printf(dev, "Location: %s type: %d ID: %d "
672 				      "RPM: %d\n", sc->sc_fans[i].fan.name,
673 				      sc->sc_fans[i].type, sc->sc_fans[i].id,
674 				      (sc->sc_fans[i].type == FCU_FAN_RPM) ?
675 				      sc->sc_fans[i].setpoint :
676 				      sc->sc_fans[i].rpm );
677 	    }
678 	}
679 }
680