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