2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2010 Andreas Tobler
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/callout.h>
36 #include <sys/ctype.h>
37 #include <sys/kernel.h>
38 #include <sys/reboot.h>
40 #include <sys/sysctl.h>
41 #include <sys/limits.h>
43 #include <machine/bus.h>
44 #include <machine/md_var.h>
46 #include <dev/iicbus/iicbus.h>
47 #include <dev/iicbus/iiconf.h>
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <powerpc/powermac/powermac_thermal.h>
54 * /u3@0,f8000000/i2c@f8001000/fan@15e
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
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. */
83 struct intr_config_hook enum_hook;
85 struct fcu_fan *sc_fans;
89 /* We can read the PWM and the RPM from a PWM controlled fan.
90 * Offer both values via sysctl.
93 FCU_PWM_SYSCTL_PWM = 1 << 8,
94 FCU_PWM_SYSCTL_RPM = 2 << 8
97 static int fcu_rpm_shift;
99 /* Regular bus attachment functions */
100 static int fcu_probe(device_t);
101 static int fcu_attach(device_t);
103 /* Utility functions */
104 static void fcu_attach_fans(device_t dev);
105 static int fcu_fill_fan_prop(device_t dev);
106 static int fcu_fan_set_rpm(struct fcu_fan *fan, int rpm);
107 static int fcu_fan_get_rpm(struct fcu_fan *fan);
108 static int fcu_fan_set_pwm(struct fcu_fan *fan, int pwm);
109 static int fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm,
111 static int fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS);
112 static void fcu_start(void *xdev);
113 static int fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buf,
115 static int fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data);
117 static device_method_t fcu_methods[] = {
118 /* Device interface */
119 DEVMETHOD(device_probe, fcu_probe),
120 DEVMETHOD(device_attach, fcu_attach),
124 static driver_t fcu_driver = {
127 sizeof(struct fcu_softc)
130 DRIVER_MODULE(fcu, iicbus, fcu_driver, 0, 0);
131 static MALLOC_DEFINE(M_FCU, "fcu", "FCU Sensor Information");
134 fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff,
137 unsigned char buf[4];
140 struct iic_msg msg[] = {
141 { addr, IIC_M_WR, 0, buf }
144 msg[0].len = len + 1;
146 memcpy(buf + 1, buff, len);
150 if (iicbus_transfer(dev, msg, 1) == 0)
154 device_printf(dev, "iicbus write failed\n");
157 pause("fcu_write", hz);
162 fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
167 struct iic_msg msg[2] = {
168 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
169 { addr, IIC_M_RD, 1, buf },
174 err = iicbus_transfer(dev, msg, 2);
178 *data = *((uint8_t*)buf);
182 device_printf(dev, "iicbus read failed\n");
185 pause("fcu_read_1", hz);
190 fcu_probe(device_t dev)
192 const char *name, *compatible;
193 struct fcu_softc *sc;
195 name = ofw_bus_get_name(dev);
196 compatible = ofw_bus_get_compat(dev);
201 if (strcmp(name, "fan") != 0 || strcmp(compatible, "fcu") != 0)
204 sc = device_get_softc(dev);
206 sc->sc_addr = iicbus_get_addr(dev);
208 device_set_desc(dev, "Apple Fan Control Unit");
214 fcu_attach(device_t dev)
216 struct fcu_softc *sc;
218 sc = device_get_softc(dev);
220 sc->enum_hook.ich_func = fcu_start;
221 sc->enum_hook.ich_arg = dev;
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.
231 if (config_intrhook_establish(&sc->enum_hook) != 0)
238 fcu_start(void *xdev)
240 unsigned char buf[1] = { 0xff };
241 struct fcu_softc *sc;
243 device_t dev = (device_t)xdev;
245 sc = device_get_softc(dev);
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;
253 device_printf(dev, "FCU initialized, RPM shift: %d\n",
256 /* Detect and attach child devices. */
258 fcu_attach_fans(dev);
260 config_intrhook_disestablish(&sc->enum_hook);
265 fcu_fan_set_rpm(struct fcu_fan *fan, int rpm)
268 struct fcu_softc *sc;
269 unsigned char buf[2];
271 sc = device_get_softc(fan->dev);
273 /* Clamp to allowed range */
274 rpm = max(fan->fan.min_rpm, rpm);
275 rpm = min(fan->fan.max_rpm, rpm);
277 if (fan->type == FCU_FAN_RPM) {
278 reg = FCU_RPM_SET(fan->id);
281 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
285 buf[0] = rpm >> (8 - fcu_rpm_shift);
286 buf[1] = rpm << fcu_rpm_shift;
288 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 2) < 0)
295 fcu_fan_get_rpm(struct fcu_fan *fan)
298 struct fcu_softc *sc;
299 uint8_t buff[2] = { 0, 0 };
300 uint8_t active = 0, avail = 0, fail = 0;
303 sc = device_get_softc(fan->dev);
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)
310 if ((avail & (1 << fan->id)) == 0) {
311 device_printf(fan->dev,
312 "RPM Fan not available ID: %d\n", fan->id);
315 /* Check if we have a failed fan. */
317 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
319 if ((fail & (1 << fan->id)) != 0) {
320 device_printf(fan->dev,
321 "RPM Fan failed ID: %d\n", fan->id);
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)
328 if ((active & (1 << fan->id)) == 0) {
329 device_printf(fan->dev, "RPM Fan not active ID: %d\n",
333 reg = FCU_RPM_READ(fan->id);
336 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
340 /* It seems that we can read the fans rpm. */
341 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buff) < 0)
344 rpm = (buff[0] << (8 - fcu_rpm_shift)) | buff[1] >> fcu_rpm_shift;
350 fcu_fan_set_pwm(struct fcu_fan *fan, int pwm)
353 struct fcu_softc *sc;
356 sc = device_get_softc(fan->dev);
358 /* Clamp to allowed range */
359 pwm = max(fan->fan.min_rpm, pwm);
360 pwm = min(fan->fan.max_rpm, pwm);
362 if (fan->type == FCU_FAN_PWM) {
363 reg = FCU_PWM_SGET(fan->id);
370 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
374 buf[0] = (pwm * 2550) / 1000;
376 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 1) < 0)
382 fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm, int *rpm)
385 struct fcu_softc *sc;
387 uint8_t active = 0, avail = 0, fail = 0;
389 sc = device_get_softc(dev);
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)
396 if ((avail & (1 << fan->id)) == 0) {
397 device_printf(dev, "PWM Fan not available ID: %d\n",
401 /* Check if we have a failed fan. */
403 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
405 if ((fail & (1 << fan->id)) != 0) {
406 device_printf(dev, "PWM Fan failed ID: %d\n", fan->id);
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)
413 if ((active & (1 << fan->id)) == 0) {
414 device_printf(dev, "PWM Fan not active ID: %d\n",
418 reg = FCU_PWM_SGET(fan->id);
420 device_printf(dev, "Unknown fan type: %d\n", fan->type);
424 /* It seems that we can read the fans pwm. */
425 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
428 *pwm = (buf[0] * 1000) / 2550;
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)
435 *rpm = (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
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.
445 fcu_fill_fan_prop(device_t dev)
448 struct fcu_softc *sc;
452 int i = 0, j, len = 0, prop_len, prev_len = 0;
454 sc = device_get_softc(dev);
456 child = ofw_bus_get_node(dev);
458 /* Fill the fan location property. */
459 prop_len = OF_getprop(child, "hwctrl-location", location,
461 while (len < prop_len) {
462 if (sc->sc_fans != NULL) {
463 strcpy(sc->sc_fans[i].fan.name, location + len);
465 prev_len = strlen(location + len) + 1;
469 if (sc->sc_fans == NULL)
472 /* Fill the fan type property. */
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;
481 sc->sc_fans[i].type = FCU_FAN_PWM;
482 prev_len = strlen(type + len) + 1;
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;
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];
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);
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);
516 sc->sc_fans[j].fan.default_rpm = sc->sc_fans[j].fan.max_rpm;
523 fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
526 struct fcu_softc *sc;
528 int rpm = 0, pwm = 0, error = 0;
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);
537 error = sysctl_handle_int(oidp, &rpm, 0, req);
539 error = fcu_fan_get_pwm(fcu, fan, &pwm, &rpm);
543 switch (arg2 & 0xff00) {
544 case FCU_PWM_SYSCTL_PWM:
545 error = sysctl_handle_int(oidp, &pwm, 0, req);
547 case FCU_PWM_SYSCTL_RPM:
548 error = sysctl_handle_int(oidp, &rpm, 0, req);
551 /* This should never happen */
556 /* We can only read the RPM from a PWM controlled fan, so return. */
557 if ((arg2 & 0xff00) == FCU_PWM_SYSCTL_RPM)
560 if (error || !req->newptr)
563 if (fan->type == FCU_FAN_RPM)
564 return (fcu_fan_set_rpm(fan, rpm));
566 return (fcu_fan_set_pwm(fan, pwm));
570 fcu_attach_fans(device_t dev)
572 struct fcu_softc *sc;
573 struct sysctl_oid *oid, *fanroot_oid;
574 struct sysctl_ctx_list *ctx;
575 char sysctl_name[32];
578 sc = device_get_softc(dev);
582 /* Count the actual number of fans. */
583 sc->sc_nfans = fcu_fill_fan_prop(dev);
585 device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
587 if (sc->sc_nfans == 0) {
588 device_printf(dev, "WARNING: No fans detected!\n");
592 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct fcu_fan), M_FCU,
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");
600 /* Now we can fill the properties into the allocated struct. */
601 sc->sc_nfans = fcu_fill_fan_prop(dev);
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);
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] = '_';
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");
633 fcu_fan_get_pwm(dev, &sc->sc_fans[i],
634 &sc->sc_fans[i].setpoint,
635 &sc->sc_fans[i].rpm);
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.
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",
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",
662 /* Dump fan location, type & RPM. */
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 );