2 * Copyright (c) 2010 Andreas Tobler
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/callout.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
42 #include <sys/limits.h>
44 #include <machine/bus.h>
45 #include <machine/md_var.h>
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <powerpc/powermac/powermac_thermal.h>
55 * /u3@0,f8000000/i2c@f8001000/fan@15e
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
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. */
84 struct intr_config_hook enum_hook;
86 struct fcu_fan *sc_fans;
90 /* We can read the PWM and the RPM from a PWM controlled fan.
91 * Offer both values via sysctl.
94 FCU_PWM_SYSCTL_PWM = 1 << 8,
95 FCU_PWM_SYSCTL_RPM = 2 << 8
98 static int fcu_rpm_shift;
100 /* Regular bus attachment functions */
101 static int fcu_probe(device_t);
102 static int fcu_attach(device_t);
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,
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,
116 static int fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data);
118 static device_method_t fcu_methods[] = {
119 /* Device interface */
120 DEVMETHOD(device_probe, fcu_probe),
121 DEVMETHOD(device_attach, fcu_attach),
125 static driver_t fcu_driver = {
128 sizeof(struct fcu_softc)
131 static devclass_t fcu_devclass;
133 DRIVER_MODULE(fcu, iicbus, fcu_driver, fcu_devclass, 0, 0);
134 static MALLOC_DEFINE(M_FCU, "fcu", "FCU Sensor Information");
137 fcu_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff,
140 unsigned char buf[4];
143 struct iic_msg msg[] = {
144 { addr, IIC_M_WR, 0, buf }
147 msg[0].len = len + 1;
149 memcpy(buf + 1, buff, len);
153 if (iicbus_transfer(dev, msg, 1) == 0)
157 device_printf(dev, "iicbus write failed\n");
160 pause("fcu_write", hz);
165 fcu_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
170 struct iic_msg msg[2] = {
171 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
172 { addr, IIC_M_RD, 1, buf },
177 err = iicbus_transfer(dev, msg, 2);
181 *data = *((uint8_t*)buf);
185 device_printf(dev, "iicbus read failed\n");
188 pause("fcu_read_1", hz);
193 fcu_probe(device_t dev)
195 const char *name, *compatible;
196 struct fcu_softc *sc;
198 name = ofw_bus_get_name(dev);
199 compatible = ofw_bus_get_compat(dev);
204 if (strcmp(name, "fan") != 0 || strcmp(compatible, "fcu") != 0)
207 sc = device_get_softc(dev);
209 sc->sc_addr = iicbus_get_addr(dev);
211 device_set_desc(dev, "Apple Fan Control Unit");
217 fcu_attach(device_t dev)
219 struct fcu_softc *sc;
221 sc = device_get_softc(dev);
223 sc->enum_hook.ich_func = fcu_start;
224 sc->enum_hook.ich_arg = dev;
226 /* We have to wait until interrupts are enabled. I2C read and write
227 * only works if the interrupts are available.
228 * The unin/i2c is controlled by the htpic on unin. But this is not
229 * the master. The openpic on mac-io is controlling the htpic.
230 * This one gets attached after the mac-io probing and then the
231 * interrupts will be available.
234 if (config_intrhook_establish(&sc->enum_hook) != 0)
241 fcu_start(void *xdev)
243 unsigned char buf[1] = { 0xff };
244 struct fcu_softc *sc;
246 device_t dev = (device_t)xdev;
248 sc = device_get_softc(dev);
250 /* Start the fcu device. */
251 fcu_write(sc->sc_dev, sc->sc_addr, 0xe, buf, 1);
252 fcu_write(sc->sc_dev, sc->sc_addr, 0x2e, buf, 1);
253 fcu_read_1(sc->sc_dev, sc->sc_addr, 0, buf);
254 fcu_rpm_shift = (buf[0] == 1) ? 2 : 3;
256 device_printf(dev, "FCU initialized, RPM shift: %d\n",
259 /* Detect and attach child devices. */
261 fcu_attach_fans(dev);
263 config_intrhook_disestablish(&sc->enum_hook);
268 fcu_fan_set_rpm(struct fcu_fan *fan, int rpm)
271 struct fcu_softc *sc;
272 unsigned char buf[2];
274 sc = device_get_softc(fan->dev);
276 /* Clamp to allowed range */
277 rpm = max(fan->fan.min_rpm, rpm);
278 rpm = min(fan->fan.max_rpm, rpm);
280 if (fan->type == FCU_FAN_RPM) {
281 reg = FCU_RPM_SET(fan->id);
284 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
288 buf[0] = rpm >> (8 - fcu_rpm_shift);
289 buf[1] = rpm << fcu_rpm_shift;
291 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 2) < 0)
298 fcu_fan_get_rpm(struct fcu_fan *fan)
301 struct fcu_softc *sc;
302 uint8_t buff[2] = { 0, 0 };
303 uint8_t active = 0, avail = 0, fail = 0;
306 sc = device_get_softc(fan->dev);
308 if (fan->type == FCU_FAN_RPM) {
309 /* Check if the fan is available. */
310 reg = FCU_RPM_AVAILABLE;
311 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0)
313 if ((avail & (1 << fan->id)) == 0) {
314 device_printf(fan->dev,
315 "RPM Fan not available ID: %d\n", fan->id);
318 /* Check if we have a failed fan. */
320 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
322 if ((fail & (1 << fan->id)) != 0) {
323 device_printf(fan->dev,
324 "RPM Fan failed ID: %d\n", fan->id);
327 /* Check if fan is active. */
328 reg = FCU_RPM_ACTIVE;
329 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0)
331 if ((active & (1 << fan->id)) == 0) {
332 device_printf(fan->dev, "RPM Fan not active ID: %d\n",
336 reg = FCU_RPM_READ(fan->id);
339 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
343 /* It seems that we can read the fans rpm. */
344 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buff) < 0)
347 rpm = (buff[0] << (8 - fcu_rpm_shift)) | buff[1] >> fcu_rpm_shift;
353 fcu_fan_set_pwm(struct fcu_fan *fan, int pwm)
356 struct fcu_softc *sc;
359 sc = device_get_softc(fan->dev);
361 /* Clamp to allowed range */
362 pwm = max(fan->fan.min_rpm, pwm);
363 pwm = min(fan->fan.max_rpm, pwm);
365 if (fan->type == FCU_FAN_PWM) {
366 reg = FCU_PWM_SGET(fan->id);
373 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type);
377 buf[0] = (pwm * 2550) / 1000;
379 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 1) < 0)
385 fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm, int *rpm)
388 struct fcu_softc *sc;
390 uint8_t active = 0, avail = 0, fail = 0;
392 sc = device_get_softc(dev);
394 if (fan->type == FCU_FAN_PWM) {
395 /* Check if the fan is available. */
396 reg = FCU_PWM_AVAILABLE;
397 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0)
399 if ((avail & (1 << fan->id)) == 0) {
400 device_printf(dev, "PWM Fan not available ID: %d\n",
404 /* Check if we have a failed fan. */
406 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0)
408 if ((fail & (1 << fan->id)) != 0) {
409 device_printf(dev, "PWM Fan failed ID: %d\n", fan->id);
412 /* Check if fan is active. */
413 reg = FCU_PWM_ACTIVE;
414 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0)
416 if ((active & (1 << fan->id)) == 0) {
417 device_printf(dev, "PWM Fan not active ID: %d\n",
421 reg = FCU_PWM_SGET(fan->id);
423 device_printf(dev, "Unknown fan type: %d\n", fan->type);
427 /* It seems that we can read the fans pwm. */
428 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
431 *pwm = (buf[0] * 1000) / 2550;
433 /* Now read the rpm. */
434 reg = FCU_PWM_RPM(fan->id);
435 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0)
438 *rpm = (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
444 * This function returns the number of fans. If we call it the second time
445 * and we have allocated memory for sc->sc_fans, we fill in the properties.
448 fcu_fill_fan_prop(device_t dev)
451 struct fcu_softc *sc;
455 int i = 0, j, len = 0, prop_len, prev_len = 0;
457 sc = device_get_softc(dev);
459 child = ofw_bus_get_node(dev);
461 /* Fill the fan location property. */
462 prop_len = OF_getprop(child, "hwctrl-location", location,
464 while (len < prop_len) {
465 if (sc->sc_fans != NULL) {
466 strcpy(sc->sc_fans[i].fan.name, location + len);
468 prev_len = strlen(location + len) + 1;
472 if (sc->sc_fans == NULL)
475 /* Fill the fan type property. */
479 prop_len = OF_getprop(child, "hwctrl-type", type, sizeof(type));
480 while (len < prop_len) {
481 if (strcmp(type + len, "fan-rpm") == 0)
482 sc->sc_fans[i].type = FCU_FAN_RPM;
484 sc->sc_fans[i].type = FCU_FAN_PWM;
485 prev_len = strlen(type + len) + 1;
490 /* Fill the fan ID property. */
491 prop_len = OF_getprop(child, "hwctrl-id", id, sizeof(id));
492 for (j = 0; j < i; j++)
493 sc->sc_fans[j].id = ((id[j] >> 8) & 0x0f) % 8;
495 /* Fill the fan zone property. */
496 prop_len = OF_getprop(child, "hwctrl-zone", id, sizeof(id));
497 for (j = 0; j < i; j++)
498 sc->sc_fans[j].fan.zone = id[j];
500 /* Finish setting up fan properties */
501 for (j = 0; j < i; j++) {
502 sc->sc_fans[j].dev = sc->sc_dev;
503 if (sc->sc_fans[j].type == FCU_FAN_RPM) {
504 sc->sc_fans[j].fan.min_rpm = 4800 >> fcu_rpm_shift;
505 sc->sc_fans[j].fan.max_rpm = 56000 >> fcu_rpm_shift;
506 sc->sc_fans[j].setpoint =
507 fcu_fan_get_rpm(&sc->sc_fans[j]);
508 sc->sc_fans[j].fan.read =
509 (int (*)(struct pmac_fan *))(fcu_fan_get_rpm);
510 sc->sc_fans[j].fan.set =
511 (int (*)(struct pmac_fan *, int))(fcu_fan_set_rpm);
513 sc->sc_fans[j].fan.min_rpm = 30; /* Percent */
514 sc->sc_fans[j].fan.max_rpm = 100;
515 sc->sc_fans[j].fan.read = NULL;
516 sc->sc_fans[j].fan.set =
517 (int (*)(struct pmac_fan *, int))(fcu_fan_set_pwm);
519 sc->sc_fans[j].fan.default_rpm = sc->sc_fans[j].fan.max_rpm;
526 fcu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
529 struct fcu_softc *sc;
531 int rpm = 0, pwm = 0, error = 0;
534 sc = device_get_softc(fcu);
535 fan = &sc->sc_fans[arg2 & 0x00ff];
536 if (fan->type == FCU_FAN_RPM) {
537 rpm = fcu_fan_get_rpm(fan);
540 error = sysctl_handle_int(oidp, &rpm, 0, req);
542 error = fcu_fan_get_pwm(fcu, fan, &pwm, &rpm);
546 switch (arg2 & 0xff00) {
547 case FCU_PWM_SYSCTL_PWM:
548 error = sysctl_handle_int(oidp, &pwm, 0, req);
550 case FCU_PWM_SYSCTL_RPM:
551 error = sysctl_handle_int(oidp, &rpm, 0, req);
554 /* This should never happen */
559 /* We can only read the RPM from a PWM controlled fan, so return. */
560 if ((arg2 & 0xff00) == FCU_PWM_SYSCTL_RPM)
563 if (error || !req->newptr)
566 if (fan->type == FCU_FAN_RPM)
567 return (fcu_fan_set_rpm(fan, rpm));
569 return (fcu_fan_set_pwm(fan, pwm));
573 fcu_attach_fans(device_t dev)
575 struct fcu_softc *sc;
576 struct sysctl_oid *oid, *fanroot_oid;
577 struct sysctl_ctx_list *ctx;
578 char sysctl_name[32];
581 sc = device_get_softc(dev);
585 /* Count the actual number of fans. */
586 sc->sc_nfans = fcu_fill_fan_prop(dev);
588 device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
590 if (sc->sc_nfans == 0) {
591 device_printf(dev, "WARNING: No fans detected!\n");
595 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct fcu_fan), M_FCU,
598 ctx = device_get_sysctl_ctx(dev);
599 fanroot_oid = SYSCTL_ADD_NODE(ctx,
600 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
601 CTLFLAG_RD, 0, "FCU Fan Information");
603 /* Now we can fill the properties into the allocated struct. */
604 sc->sc_nfans = fcu_fill_fan_prop(dev);
606 /* Register fans with pmac_thermal */
607 for (i = 0; i < sc->sc_nfans; i++)
608 pmac_thermal_fan_register(&sc->sc_fans[i].fan);
610 /* Add sysctls for the fans. */
611 for (i = 0; i < sc->sc_nfans; i++) {
612 for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) {
613 sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]);
614 if (isspace(sysctl_name[j]))
615 sysctl_name[j] = '_';
619 if (sc->sc_fans[i].type == FCU_FAN_RPM) {
620 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
621 OID_AUTO, sysctl_name,
622 CTLFLAG_RD, 0, "Fan Information");
623 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
624 "minrpm", CTLFLAG_RD,
625 &(sc->sc_fans[i].fan.min_rpm), 0,
626 "Minimum allowed RPM");
627 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
628 "maxrpm", CTLFLAG_RD,
629 &(sc->sc_fans[i].fan.max_rpm), 0,
630 "Maximum allowed RPM");
631 /* I use i to pass the fan id. */
632 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
633 "rpm", CTLTYPE_INT | CTLFLAG_RW, dev, i,
634 fcu_fanrpm_sysctl, "I", "Fan RPM");
636 fcu_fan_get_pwm(dev, &sc->sc_fans[i],
637 &sc->sc_fans[i].setpoint,
638 &sc->sc_fans[i].rpm);
640 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
641 OID_AUTO, sysctl_name,
642 CTLFLAG_RD, 0, "Fan Information");
643 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
644 "minpwm", CTLFLAG_RD,
645 &(sc->sc_fans[i].fan.min_rpm), 0,
646 "Minimum allowed PWM in %");
647 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
648 "maxpwm", CTLFLAG_RD,
649 &(sc->sc_fans[i].fan.max_rpm), 0,
650 "Maximum allowed PWM in %");
651 /* I use i to pass the fan id or'ed with the type
652 * of info I want to display/modify.
654 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
655 "pwm", CTLTYPE_INT | CTLFLAG_RW, dev,
656 FCU_PWM_SYSCTL_PWM | i,
657 fcu_fanrpm_sysctl, "I", "Fan PWM in %");
658 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
659 "rpm", CTLTYPE_INT | CTLFLAG_RD, dev,
660 FCU_PWM_SYSCTL_RPM | i,
661 fcu_fanrpm_sysctl, "I", "Fan RPM");
665 /* Dump fan location, type & RPM. */
667 device_printf(dev, "Fans\n");
668 for (i = 0; i < sc->sc_nfans; i++) {
669 device_printf(dev, "Location: %s type: %d ID: %d "
670 "RPM: %d\n", sc->sc_fans[i].fan.name,
671 sc->sc_fans[i].type, sc->sc_fans[i].id,
672 (sc->sc_fans[i].type == FCU_FAN_RPM) ?
673 sc->sc_fans[i].setpoint :
674 sc->sc_fans[i].rpm );