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