2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Andreas Tobler
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/callout.h>
38 #include <sys/ctype.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <sys/limits.h>
45 #include <machine/bus.h>
46 #include <machine/md_var.h>
48 #include <dev/iicbus/iicbus.h>
49 #include <dev/iicbus/iiconf.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <powerpc/powermac/powermac_thermal.h>
56 * /u3@0,f8000000/i2c@f8001000/fan@15e
58 #define FCU_RPM_FAIL 0x0b /* fans states in bits 0<1-6>7 */
59 #define FCU_RPM_AVAILABLE 0x0c
60 #define FCU_RPM_ACTIVE 0x0d
61 #define FCU_RPM_READ(x) 0x11 + (x) * 2
62 #define FCU_RPM_SET(x) 0x10 + (x) * 2
64 #define FCU_PWM_FAIL 0x2b
65 #define FCU_PWM_AVAILABLE 0x2c
66 #define FCU_PWM_ACTIVE 0x2d
67 #define FCU_PWM_RPM(x) 0x31 + (x) * 2 /* Get RPM. */
68 #define FCU_PWM_SGET(x) 0x30 + (x) * 2 /* Set or get PWM. */
85 struct intr_config_hook enum_hook;
87 struct fcu_fan *sc_fans;
91 /* We can read the PWM and the RPM from a PWM controlled fan.
92 * Offer both values via sysctl.
95 FCU_PWM_SYSCTL_PWM = 1 << 8,
96 FCU_PWM_SYSCTL_RPM = 2 << 8
99 static int fcu_rpm_shift;
101 /* Regular bus attachment functions */
102 static int fcu_probe(device_t);
103 static int fcu_attach(device_t);
105 /* Utility functions */
106 static void fcu_attach_fans(device_t dev);
107 static int fcu_fill_fan_prop(device_t dev);
108 static int fcu_fan_set_rpm(struct fcu_fan *fan, int rpm);
109 static int fcu_fan_get_rpm(struct fcu_fan *fan);
110 static int fcu_fan_set_pwm(struct fcu_fan *fan, int pwm);
111 static int fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm,
113 static int fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS);
114 static void fcu_start(void *xdev);
115 static int fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buf,
117 static int fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data);
119 static device_method_t fcu_methods[] = {
120 /* Device interface */
121 DEVMETHOD(device_probe, fcu_probe),
122 DEVMETHOD(device_attach, fcu_attach),
126 static driver_t fcu_driver = {
129 sizeof(struct fcu_softc)
132 static devclass_t fcu_devclass;
134 DRIVER_MODULE(fcu, iicbus, fcu_driver, fcu_devclass, 0, 0);
135 static MALLOC_DEFINE(M_FCU, "fcu", "FCU Sensor Information");
138 fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff,
141 unsigned char buf[4];
144 struct iic_msg msg[] = {
145 { addr, IIC_M_WR, 0, buf }
148 msg[0].len = len + 1;
150 memcpy(buf + 1, buff, len);
154 if (iicbus_transfer(dev, msg, nitems(msg)) == 0)
158 device_printf(dev, "iicbus write failed\n");
161 pause("fcu_write", hz);
166 fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
171 struct iic_msg msg[2] = {
172 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
173 { addr, IIC_M_RD, 1, buf },
178 err = iicbus_transfer(dev, msg, nitems(msg));
182 *data = *((uint8_t*)buf);
186 device_printf(dev, "iicbus read failed\n");
189 pause("fcu_read_1", hz);
194 fcu_probe(device_t dev)
196 const char *name, *compatible;
197 struct fcu_softc *sc;
199 name = ofw_bus_get_name(dev);
200 compatible = ofw_bus_get_compat(dev);
205 if (strcmp(name, "fan") != 0 || strcmp(compatible, "fcu") != 0)
208 sc = device_get_softc(dev);
210 sc->sc_addr = iicbus_get_addr(dev);
212 device_set_desc(dev, "Apple Fan Control Unit");
218 fcu_attach(device_t dev)
220 struct fcu_softc *sc;
222 sc = device_get_softc(dev);
224 sc->enum_hook.ich_func = fcu_start;
225 sc->enum_hook.ich_arg = dev;
227 /* We have to wait until interrupts are enabled. I2C read and write
228 * only works if the interrupts are available.
229 * The unin/i2c is controlled by the htpic on unin. But this is not
230 * the master. The openpic on mac-io is controlling the htpic.
231 * This one gets attached after the mac-io probing and then the
232 * interrupts will be available.
235 if (config_intrhook_establish(&sc->enum_hook) != 0)
242 fcu_start(void *xdev)
244 unsigned char buf[1] = { 0xff };
245 struct fcu_softc *sc;
247 device_t dev = (device_t)xdev;
249 sc = device_get_softc(dev);
251 /* Start the fcu device. */
252 fcu_write(sc->sc_dev, sc->sc_addr, 0xe, buf, sizeof(buf));
253 fcu_write(sc->sc_dev, sc->sc_addr, 0x2e, buf, sizeof(buf));
254 fcu_read_1(sc->sc_dev, sc->sc_addr, 0, buf);
255 fcu_rpm_shift = (buf[0] == 1) ? 2 : 3;
257 device_printf(dev, "FCU initialized, RPM shift: %d\n",
260 /* Detect and attach child devices. */
262 fcu_attach_fans(dev);
264 config_intrhook_disestablish(&sc->enum_hook);
269 fcu_fan_set_rpm(struct fcu_fan *fan, int rpm)
272 struct fcu_softc *sc;
273 unsigned char buf[2];
275 sc = device_get_softc(fan->dev);
277 /* Clamp to allowed range */
278 rpm = max(fan->fan.min_rpm, rpm);
279 rpm = min(fan->fan.max_rpm, rpm);
281 if (fan->type == FCU_FAN_RPM) {
282 reg = FCU_RPM_SET(fan->id);
285 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
289 buf[0] = rpm >> (8 - fcu_rpm_shift);
290 buf[1] = rpm << fcu_rpm_shift;
292 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, sizeof(buf)) < 0)
299 fcu_fan_get_rpm(struct fcu_fan *fan)
302 struct fcu_softc *sc;
303 uint8_t buff[2] = { 0, 0 };
304 uint8_t active = 0, avail = 0, fail = 0;
307 sc = device_get_softc(fan->dev);
309 if (fan->type == FCU_FAN_RPM) {
310 /* Check if the fan is available. */
311 reg = FCU_RPM_AVAILABLE;
312 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0)
314 if ((avail & (1 << fan->id)) == 0) {
315 device_printf(fan->dev,
316 "RPM Fan not available ID: %d\n", fan->id);
319 /* Check if we have a failed fan. */
321 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
323 if ((fail & (1 << fan->id)) != 0) {
324 device_printf(fan->dev,
325 "RPM Fan failed ID: %d %#x\n", fan->id, fail);
328 /* Check if fan is active. */
329 reg = FCU_RPM_ACTIVE;
330 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0)
332 if ((active & (1 << fan->id)) == 0) {
333 device_printf(fan->dev, "RPM Fan not active ID: %d\n",
337 reg = FCU_RPM_READ(fan->id);
340 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
344 /* It seems that we can read the fans rpm. */
345 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buff) < 0)
348 rpm = (buff[0] << (8 - fcu_rpm_shift)) | buff[1] >> fcu_rpm_shift;
354 fcu_fan_set_pwm(struct fcu_fan *fan, int pwm)
357 struct fcu_softc *sc;
360 sc = device_get_softc(fan->dev);
362 /* Clamp to allowed range */
363 pwm = max(fan->fan.min_rpm, pwm);
364 pwm = min(fan->fan.max_rpm, pwm);
366 if (fan->type == FCU_FAN_PWM) {
367 reg = FCU_PWM_SGET(fan->id);
374 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
378 buf[0] = (pwm * 2550) / 1000;
380 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, sizeof(buf)) < 0)
386 fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm, int *rpm)
389 struct fcu_softc *sc;
391 uint8_t active = 0, avail = 0, fail = 0;
393 sc = device_get_softc(dev);
395 if (fan->type == FCU_FAN_PWM) {
396 /* Check if the fan is available. */
397 reg = FCU_PWM_AVAILABLE;
398 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0)
400 if ((avail & (1 << fan->id)) == 0) {
401 device_printf(dev, "PWM Fan not available ID: %d\n",
405 /* Check if we have a failed fan. */
407 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
409 if ((fail & (1 << fan->id)) != 0) {
410 device_printf(dev, "PWM Fan failed ID: %d\n", fan->id);
413 /* Check if fan is active. */
414 reg = FCU_PWM_ACTIVE;
415 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0)
417 if ((active & (1 << fan->id)) == 0) {
418 device_printf(dev, "PWM Fan not active ID: %d\n",
422 reg = FCU_PWM_SGET(fan->id);
424 device_printf(dev, "Unknown fan type: %d\n", fan->type);
428 /* It seems that we can read the fans pwm. */
429 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
432 *pwm = (buf[0] * 1000) / 2550;
434 /* Now read the rpm. */
435 reg = FCU_PWM_RPM(fan->id);
436 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
439 *rpm = (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
445 * This function returns the number of fans. If we call it the second time
446 * and we have allocated memory for sc->sc_fans, we fill in the properties.
449 fcu_fill_fan_prop(device_t dev)
452 struct fcu_softc *sc;
456 int i = 0, j, len = 0, prop_len, prev_len = 0;
458 sc = device_get_softc(dev);
460 child = ofw_bus_get_node(dev);
462 /* Fill the fan location property. */
463 prop_len = OF_getprop(child, "hwctrl-location", location,
465 while (len < prop_len) {
466 if (sc->sc_fans != NULL) {
467 strcpy(sc->sc_fans[i].fan.name, location + len);
469 prev_len = strlen(location + len) + 1;
473 if (sc->sc_fans == NULL)
476 /* Fill the fan type property. */
480 prop_len = OF_getprop(child, "hwctrl-type", type, sizeof(type));
481 while (len < prop_len) {
482 if (strcmp(type + len, "fan-rpm") == 0)
483 sc->sc_fans[i].type = FCU_FAN_RPM;
485 sc->sc_fans[i].type = FCU_FAN_PWM;
486 prev_len = strlen(type + len) + 1;
491 /* Fill the fan ID property. */
492 prop_len = OF_getprop(child, "hwctrl-id", id, sizeof(id));
493 for (j = 0; j < i; j++)
494 sc->sc_fans[j].id = ((id[j] >> 8) & 0x0f) % 8;
496 /* Fill the fan zone property. */
497 prop_len = OF_getprop(child, "hwctrl-zone", id, sizeof(id));
498 for (j = 0; j < i; j++)
499 sc->sc_fans[j].fan.zone = id[j];
501 /* Finish setting up fan properties */
502 for (j = 0; j < i; j++) {
503 sc->sc_fans[j].dev = sc->sc_dev;
504 if (sc->sc_fans[j].type == FCU_FAN_RPM) {
505 sc->sc_fans[j].fan.min_rpm = 4800 >> fcu_rpm_shift;
506 sc->sc_fans[j].fan.max_rpm = 56000 >> fcu_rpm_shift;
507 sc->sc_fans[j].setpoint =
508 fcu_fan_get_rpm(&sc->sc_fans[j]);
509 sc->sc_fans[j].fan.read =
510 (int (*)(struct pmac_fan *))(fcu_fan_get_rpm);
511 sc->sc_fans[j].fan.set =
512 (int (*)(struct pmac_fan *, int))(fcu_fan_set_rpm);
514 sc->sc_fans[j].fan.min_rpm = 30; /* Percent */
515 sc->sc_fans[j].fan.max_rpm = 100;
516 sc->sc_fans[j].fan.read = NULL;
517 sc->sc_fans[j].fan.set =
518 (int (*)(struct pmac_fan *, int))(fcu_fan_set_pwm);
520 sc->sc_fans[j].fan.default_rpm = sc->sc_fans[j].fan.max_rpm;
527 fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
530 struct fcu_softc *sc;
532 int rpm = 0, pwm = 0, error = 0;
535 sc = device_get_softc(fcu);
536 fan = &sc->sc_fans[arg2 & 0x00ff];
537 if (fan->type == FCU_FAN_RPM) {
538 rpm = fcu_fan_get_rpm(fan);
541 error = sysctl_handle_int(oidp, &rpm, 0, req);
543 error = fcu_fan_get_pwm(fcu, fan, &pwm, &rpm);
547 switch (arg2 & 0xff00) {
548 case FCU_PWM_SYSCTL_PWM:
549 error = sysctl_handle_int(oidp, &pwm, 0, req);
551 case FCU_PWM_SYSCTL_RPM:
552 error = sysctl_handle_int(oidp, &rpm, 0, req);
555 /* This should never happen */
560 /* We can only read the RPM from a PWM controlled fan, so return. */
561 if ((arg2 & 0xff00) == FCU_PWM_SYSCTL_RPM)
564 if (error || !req->newptr)
567 if (fan->type == FCU_FAN_RPM)
568 return (fcu_fan_set_rpm(fan, rpm));
570 return (fcu_fan_set_pwm(fan, pwm));
574 fcu_attach_fans(device_t dev)
576 struct fcu_softc *sc;
577 struct sysctl_oid *oid, *fanroot_oid;
578 struct sysctl_ctx_list *ctx;
579 char sysctl_name[32];
582 sc = device_get_softc(dev);
586 /* Count the actual number of fans. */
587 sc->sc_nfans = fcu_fill_fan_prop(dev);
589 device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
591 if (sc->sc_nfans == 0) {
592 device_printf(dev, "WARNING: No fans detected!\n");
596 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct fcu_fan), M_FCU,
599 ctx = device_get_sysctl_ctx(dev);
600 fanroot_oid = SYSCTL_ADD_NODE(ctx,
601 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
602 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "FCU Fan Information");
604 /* Now we can fill the properties into the allocated struct. */
605 sc->sc_nfans = fcu_fill_fan_prop(dev);
607 /* Register fans with pmac_thermal */
608 for (i = 0; i < sc->sc_nfans; i++)
609 pmac_thermal_fan_register(&sc->sc_fans[i].fan);
611 /* Add sysctls for the fans. */
612 for (i = 0; i < sc->sc_nfans; i++) {
613 for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) {
614 sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]);
615 if (isspace(sysctl_name[j]))
616 sysctl_name[j] = '_';
620 if (sc->sc_fans[i].type == FCU_FAN_RPM) {
621 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
622 OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE,
623 0, "Fan Information");
624 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
625 "minrpm", CTLFLAG_RD,
626 &(sc->sc_fans[i].fan.min_rpm), 0,
627 "Minimum allowed RPM");
628 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
629 "maxrpm", CTLFLAG_RD,
630 &(sc->sc_fans[i].fan.max_rpm), 0,
631 "Maximum allowed RPM");
632 /* I use i to pass the fan id. */
633 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
634 "rpm", CTLTYPE_INT | CTLFLAG_RW |
635 CTLFLAG_MPSAFE, dev, i,
636 fcu_fanrpm_sysctl, "I", "Fan RPM");
638 fcu_fan_get_pwm(dev, &sc->sc_fans[i],
639 &sc->sc_fans[i].setpoint,
640 &sc->sc_fans[i].rpm);
642 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
643 OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE,
644 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.
656 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
657 "pwm", CTLTYPE_INT | CTLFLAG_RW |
659 FCU_PWM_SYSCTL_PWM | i,
660 fcu_fanrpm_sysctl, "I", "Fan PWM in %");
661 "rpm", CTLTYPE_INT | CTLFLAG_RD |
663 FCU_PWM_SYSCTL_RPM | i,
664 fcu_fanrpm_sysctl, "I", "Fan RPM");
668 /* Dump fan location, type & RPM. */
670 device_printf(dev, "Fans\n");
671 for (i = 0; i < sc->sc_nfans; i++) {
672 device_printf(dev, "Location: %s type: %d ID: %d "
673 "RPM: %d\n", sc->sc_fans[i].fan.name,
674 sc->sc_fans[i].type, sc->sc_fans[i].id,
675 (sc->sc_fans[i].type == FCU_FAN_RPM) ?
676 sc->sc_fans[i].setpoint :
677 sc->sc_fans[i].rpm );