2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/callout.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
43 #include <sys/sysctl.h>
44 #include <sys/limits.h>
46 #include <machine/bus.h>
47 #include <machine/md_var.h>
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/iicbus/iiconf.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <powerpc/powermac/powermac_thermal.h>
57 * /u3@0,f8000000/i2c@f8001000/fan@15e
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
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. */
86 struct intr_config_hook enum_hook;
88 struct fcu_fan *sc_fans;
92 /* We can read the PWM and the RPM from a PWM controlled fan.
93 * Offer both values via sysctl.
96 FCU_PWM_SYSCTL_PWM = 1 << 8,
97 FCU_PWM_SYSCTL_RPM = 2 << 8
100 static int fcu_rpm_shift;
102 /* Regular bus attachment functions */
103 static int fcu_probe(device_t);
104 static int fcu_attach(device_t);
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,
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,
118 static int fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data);
120 static device_method_t fcu_methods[] = {
121 /* Device interface */
122 DEVMETHOD(device_probe, fcu_probe),
123 DEVMETHOD(device_attach, fcu_attach),
127 static driver_t fcu_driver = {
130 sizeof(struct fcu_softc)
133 static devclass_t fcu_devclass;
135 DRIVER_MODULE(fcu, iicbus, fcu_driver, fcu_devclass, 0, 0);
136 static MALLOC_DEFINE(M_FCU, "fcu", "FCU Sensor Information");
139 fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff,
142 unsigned char buf[4];
145 struct iic_msg msg[] = {
146 { addr, IIC_M_WR, 0, buf }
149 msg[0].len = len + 1;
151 memcpy(buf + 1, buff, len);
155 if (iicbus_transfer(dev, msg, 1) == 0)
159 device_printf(dev, "iicbus write failed\n");
162 pause("fcu_write", hz);
167 fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
172 struct iic_msg msg[2] = {
173 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
174 { addr, IIC_M_RD, 1, buf },
179 err = iicbus_transfer(dev, msg, 2);
183 *data = *((uint8_t*)buf);
187 device_printf(dev, "iicbus read failed\n");
190 pause("fcu_read_1", hz);
195 fcu_probe(device_t dev)
197 const char *name, *compatible;
198 struct fcu_softc *sc;
200 name = ofw_bus_get_name(dev);
201 compatible = ofw_bus_get_compat(dev);
206 if (strcmp(name, "fan") != 0 || strcmp(compatible, "fcu") != 0)
209 sc = device_get_softc(dev);
211 sc->sc_addr = iicbus_get_addr(dev);
213 device_set_desc(dev, "Apple Fan Control Unit");
219 fcu_attach(device_t dev)
221 struct fcu_softc *sc;
223 sc = device_get_softc(dev);
225 sc->enum_hook.ich_func = fcu_start;
226 sc->enum_hook.ich_arg = dev;
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.
236 if (config_intrhook_establish(&sc->enum_hook) != 0)
243 fcu_start(void *xdev)
245 unsigned char buf[1] = { 0xff };
246 struct fcu_softc *sc;
248 device_t dev = (device_t)xdev;
250 sc = device_get_softc(dev);
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;
258 device_printf(dev, "FCU initialized, RPM shift: %d\n",
261 /* Detect and attach child devices. */
263 fcu_attach_fans(dev);
265 config_intrhook_disestablish(&sc->enum_hook);
270 fcu_fan_set_rpm(struct fcu_fan *fan, int rpm)
273 struct fcu_softc *sc;
274 unsigned char buf[2];
276 sc = device_get_softc(fan->dev);
278 /* Clamp to allowed range */
279 rpm = max(fan->fan.min_rpm, rpm);
280 rpm = min(fan->fan.max_rpm, rpm);
282 if (fan->type == FCU_FAN_RPM) {
283 reg = FCU_RPM_SET(fan->id);
286 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
290 buf[0] = rpm >> (8 - fcu_rpm_shift);
291 buf[1] = rpm << fcu_rpm_shift;
293 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 2) < 0)
300 fcu_fan_get_rpm(struct fcu_fan *fan)
303 struct fcu_softc *sc;
304 uint8_t buff[2] = { 0, 0 };
305 uint8_t active = 0, avail = 0, fail = 0;
308 sc = device_get_softc(fan->dev);
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)
315 if ((avail & (1 << fan->id)) == 0) {
316 device_printf(fan->dev,
317 "RPM Fan not available ID: %d\n", fan->id);
320 /* Check if we have a failed fan. */
322 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
324 if ((fail & (1 << fan->id)) != 0) {
325 device_printf(fan->dev,
326 "RPM Fan failed ID: %d\n", fan->id);
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)
333 if ((active & (1 << fan->id)) == 0) {
334 device_printf(fan->dev, "RPM Fan not active ID: %d\n",
338 reg = FCU_RPM_READ(fan->id);
341 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
345 /* It seems that we can read the fans rpm. */
346 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buff) < 0)
349 rpm = (buff[0] << (8 - fcu_rpm_shift)) | buff[1] >> fcu_rpm_shift;
355 fcu_fan_set_pwm(struct fcu_fan *fan, int pwm)
358 struct fcu_softc *sc;
361 sc = device_get_softc(fan->dev);
363 /* Clamp to allowed range */
364 pwm = max(fan->fan.min_rpm, pwm);
365 pwm = min(fan->fan.max_rpm, pwm);
367 if (fan->type == FCU_FAN_PWM) {
368 reg = FCU_PWM_SGET(fan->id);
375 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
379 buf[0] = (pwm * 2550) / 1000;
381 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 1) < 0)
387 fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm, int *rpm)
390 struct fcu_softc *sc;
392 uint8_t active = 0, avail = 0, fail = 0;
394 sc = device_get_softc(dev);
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)
401 if ((avail & (1 << fan->id)) == 0) {
402 device_printf(dev, "PWM Fan not available ID: %d\n",
406 /* Check if we have a failed fan. */
408 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
410 if ((fail & (1 << fan->id)) != 0) {
411 device_printf(dev, "PWM Fan failed ID: %d\n", fan->id);
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)
418 if ((active & (1 << fan->id)) == 0) {
419 device_printf(dev, "PWM Fan not active ID: %d\n",
423 reg = FCU_PWM_SGET(fan->id);
425 device_printf(dev, "Unknown fan type: %d\n", fan->type);
429 /* It seems that we can read the fans pwm. */
430 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
433 *pwm = (buf[0] * 1000) / 2550;
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)
440 *rpm = (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
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.
450 fcu_fill_fan_prop(device_t dev)
453 struct fcu_softc *sc;
457 int i = 0, j, len = 0, prop_len, prev_len = 0;
459 sc = device_get_softc(dev);
461 child = ofw_bus_get_node(dev);
463 /* Fill the fan location property. */
464 prop_len = OF_getprop(child, "hwctrl-location", location,
466 while (len < prop_len) {
467 if (sc->sc_fans != NULL) {
468 strcpy(sc->sc_fans[i].fan.name, location + len);
470 prev_len = strlen(location + len) + 1;
474 if (sc->sc_fans == NULL)
477 /* Fill the fan type property. */
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;
486 sc->sc_fans[i].type = FCU_FAN_PWM;
487 prev_len = strlen(type + len) + 1;
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;
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];
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);
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);
521 sc->sc_fans[j].fan.default_rpm = sc->sc_fans[j].fan.max_rpm;
528 fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
531 struct fcu_softc *sc;
533 int rpm = 0, pwm = 0, error = 0;
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);
542 error = sysctl_handle_int(oidp, &rpm, 0, req);
544 error = fcu_fan_get_pwm(fcu, fan, &pwm, &rpm);
548 switch (arg2 & 0xff00) {
549 case FCU_PWM_SYSCTL_PWM:
550 error = sysctl_handle_int(oidp, &pwm, 0, req);
552 case FCU_PWM_SYSCTL_RPM:
553 error = sysctl_handle_int(oidp, &rpm, 0, req);
556 /* This should never happen */
561 /* We can only read the RPM from a PWM controlled fan, so return. */
562 if ((arg2 & 0xff00) == FCU_PWM_SYSCTL_RPM)
565 if (error || !req->newptr)
568 if (fan->type == FCU_FAN_RPM)
569 return (fcu_fan_set_rpm(fan, rpm));
571 return (fcu_fan_set_pwm(fan, pwm));
575 fcu_attach_fans(device_t dev)
577 struct fcu_softc *sc;
578 struct sysctl_oid *oid, *fanroot_oid;
579 struct sysctl_ctx_list *ctx;
580 char sysctl_name[32];
583 sc = device_get_softc(dev);
587 /* Count the actual number of fans. */
588 sc->sc_nfans = fcu_fill_fan_prop(dev);
590 device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
592 if (sc->sc_nfans == 0) {
593 device_printf(dev, "WARNING: No fans detected!\n");
597 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct fcu_fan), M_FCU,
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");
605 /* Now we can fill the properties into the allocated struct. */
606 sc->sc_nfans = fcu_fill_fan_prop(dev);
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);
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] = '_';
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");
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,
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.
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");
667 /* Dump fan location, type & RPM. */
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 );