Lines Matching +full:fan +full:- +full:0
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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. */
69 struct pmac_fan fan; member
89 /* We can read the PWM and the RPM from a PWM controlled fan.
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,
121 { 0, 0 },
130 DRIVER_MODULE(fcu, iicbus, fcu_driver, 0, 0);
138 int try = 0; in fcu_write()
141 { addr, IIC_M_WR, 0, buf } in fcu_write()
144 msg[0].len = len + 1; in fcu_write()
145 buf[0] = reg; in fcu_write()
150 if (iicbus_transfer(dev, msg, 1) == 0) in fcu_write()
151 return (0); in fcu_write()
155 return (-1); in fcu_write()
165 int err, try = 0; in fcu_read_1()
175 if (err != 0) in fcu_read_1()
179 return (0); in fcu_read_1()
183 return (-1); in fcu_read_1()
201 if (strcmp(name, "fan") != 0 || strcmp(compatible, "fcu") != 0) in fcu_probe()
205 sc->sc_dev = dev; in fcu_probe()
206 sc->sc_addr = iicbus_get_addr(dev); in fcu_probe()
208 device_set_desc(dev, "Apple Fan Control Unit"); in fcu_probe()
210 return (0); in fcu_probe()
220 sc->enum_hook.ich_func = fcu_start; in fcu_attach()
221 sc->enum_hook.ich_arg = dev; in fcu_attach()
226 * the master. The openpic on mac-io is controlling the htpic. in fcu_attach()
227 * This one gets attached after the mac-io probing and then the in fcu_attach()
231 if (config_intrhook_establish(&sc->enum_hook) != 0) in fcu_attach()
234 return (0); in fcu_attach()
240 unsigned char buf[1] = { 0xff }; in fcu_start()
248 fcu_write(sc->sc_dev, sc->sc_addr, 0xe, buf, 1); in fcu_start()
249 fcu_write(sc->sc_dev, sc->sc_addr, 0x2e, buf, 1); in fcu_start()
250 fcu_read_1(sc->sc_dev, sc->sc_addr, 0, buf); in fcu_start()
251 fcu_rpm_shift = (buf[0] == 1) ? 2 : 3; in fcu_start()
260 config_intrhook_disestablish(&sc->enum_hook); in fcu_start()
265 fcu_fan_set_rpm(struct fcu_fan *fan, int rpm) in fcu_fan_set_rpm() argument
271 sc = device_get_softc(fan->dev); in fcu_fan_set_rpm()
274 rpm = max(fan->fan.min_rpm, rpm); in fcu_fan_set_rpm()
275 rpm = min(fan->fan.max_rpm, rpm); in fcu_fan_set_rpm()
277 if (fan->type == FCU_FAN_RPM) { in fcu_fan_set_rpm()
278 reg = FCU_RPM_SET(fan->id); in fcu_fan_set_rpm()
279 fan->setpoint = rpm; in fcu_fan_set_rpm()
281 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type); in fcu_fan_set_rpm()
285 buf[0] = rpm >> (8 - fcu_rpm_shift); in fcu_fan_set_rpm()
288 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 2) < 0) in fcu_fan_set_rpm()
291 return (0); in fcu_fan_set_rpm()
295 fcu_fan_get_rpm(struct fcu_fan *fan) in fcu_fan_get_rpm() argument
299 uint8_t buff[2] = { 0, 0 }; in fcu_fan_get_rpm()
300 uint8_t active = 0, avail = 0, fail = 0; in fcu_fan_get_rpm()
303 sc = device_get_softc(fan->dev); in fcu_fan_get_rpm()
305 if (fan->type == FCU_FAN_RPM) { in fcu_fan_get_rpm()
306 /* Check if the fan is available. */ in fcu_fan_get_rpm()
308 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0) in fcu_fan_get_rpm()
309 return (-1); in fcu_fan_get_rpm()
310 if ((avail & (1 << fan->id)) == 0) { in fcu_fan_get_rpm()
311 device_printf(fan->dev, in fcu_fan_get_rpm()
312 "RPM Fan not available ID: %d\n", fan->id); in fcu_fan_get_rpm()
313 return (-1); in fcu_fan_get_rpm()
315 /* Check if we have a failed fan. */ in fcu_fan_get_rpm()
317 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0) in fcu_fan_get_rpm()
318 return (-1); in fcu_fan_get_rpm()
319 if ((fail & (1 << fan->id)) != 0) { in fcu_fan_get_rpm()
320 device_printf(fan->dev, in fcu_fan_get_rpm()
321 "RPM Fan failed ID: %d\n", fan->id); in fcu_fan_get_rpm()
322 return (-1); in fcu_fan_get_rpm()
324 /* Check if fan is active. */ in fcu_fan_get_rpm()
326 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0) in fcu_fan_get_rpm()
327 return (-1); in fcu_fan_get_rpm()
328 if ((active & (1 << fan->id)) == 0) { in fcu_fan_get_rpm()
329 device_printf(fan->dev, "RPM Fan not active ID: %d\n", in fcu_fan_get_rpm()
330 fan->id); in fcu_fan_get_rpm()
331 return (-1); in fcu_fan_get_rpm()
333 reg = FCU_RPM_READ(fan->id); in fcu_fan_get_rpm()
336 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type); in fcu_fan_get_rpm()
337 return (-1); in fcu_fan_get_rpm()
341 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buff) < 0) in fcu_fan_get_rpm()
342 return (-1); in fcu_fan_get_rpm()
344 rpm = (buff[0] << (8 - fcu_rpm_shift)) | buff[1] >> fcu_rpm_shift; in fcu_fan_get_rpm()
350 fcu_fan_set_pwm(struct fcu_fan *fan, int pwm) in fcu_fan_set_pwm() argument
356 sc = device_get_softc(fan->dev); in fcu_fan_set_pwm()
359 pwm = max(fan->fan.min_rpm, pwm); in fcu_fan_set_pwm()
360 pwm = min(fan->fan.max_rpm, pwm); in fcu_fan_set_pwm()
362 if (fan->type == FCU_FAN_PWM) { in fcu_fan_set_pwm()
363 reg = FCU_PWM_SGET(fan->id); in fcu_fan_set_pwm()
368 fan->setpoint = pwm; in fcu_fan_set_pwm()
370 device_printf(fan->dev, "Unknown fan type: %d\n", fan->type); in fcu_fan_set_pwm()
374 buf[0] = (pwm * 2550) / 1000; in fcu_fan_set_pwm()
376 if (fcu_write(sc->sc_dev, sc->sc_addr, reg, buf, 1) < 0) in fcu_fan_set_pwm()
378 return (0); in fcu_fan_set_pwm()
382 fcu_fan_get_pwm(device_t dev, struct fcu_fan *fan, int *pwm, int *rpm) in fcu_fan_get_pwm() argument
387 uint8_t active = 0, avail = 0, fail = 0; in fcu_fan_get_pwm()
391 if (fan->type == FCU_FAN_PWM) { in fcu_fan_get_pwm()
392 /* Check if the fan is available. */ in fcu_fan_get_pwm()
394 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &avail) < 0) in fcu_fan_get_pwm()
395 return (-1); in fcu_fan_get_pwm()
396 if ((avail & (1 << fan->id)) == 0) { in fcu_fan_get_pwm()
397 device_printf(dev, "PWM Fan not available ID: %d\n", in fcu_fan_get_pwm()
398 fan->id); in fcu_fan_get_pwm()
399 return (-1); in fcu_fan_get_pwm()
401 /* Check if we have a failed fan. */ in fcu_fan_get_pwm()
403 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &fail) < 0) in fcu_fan_get_pwm()
404 return (-1); in fcu_fan_get_pwm()
405 if ((fail & (1 << fan->id)) != 0) { in fcu_fan_get_pwm()
406 device_printf(dev, "PWM Fan failed ID: %d\n", fan->id); in fcu_fan_get_pwm()
407 return (-1); in fcu_fan_get_pwm()
409 /* Check if fan is active. */ in fcu_fan_get_pwm()
411 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, &active) < 0) in fcu_fan_get_pwm()
412 return (-1); in fcu_fan_get_pwm()
413 if ((active & (1 << fan->id)) == 0) { in fcu_fan_get_pwm()
414 device_printf(dev, "PWM Fan not active ID: %d\n", in fcu_fan_get_pwm()
415 fan->id); in fcu_fan_get_pwm()
416 return (-1); in fcu_fan_get_pwm()
418 reg = FCU_PWM_SGET(fan->id); in fcu_fan_get_pwm()
420 device_printf(dev, "Unknown fan type: %d\n", fan->type); in fcu_fan_get_pwm()
425 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0) in fcu_fan_get_pwm()
426 return (-1); in fcu_fan_get_pwm()
428 *pwm = (buf[0] * 1000) / 2550; in fcu_fan_get_pwm()
431 reg = FCU_PWM_RPM(fan->id); in fcu_fan_get_pwm()
432 if (fcu_read_1(sc->sc_dev, sc->sc_addr, reg, buf) < 0) in fcu_fan_get_pwm()
433 return (-1); in fcu_fan_get_pwm()
435 *rpm = (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift; in fcu_fan_get_pwm()
437 return (0); in fcu_fan_get_pwm()
442 * and we have allocated memory for sc->sc_fans, we fill in the properties.
452 int i = 0, j, len = 0, prop_len, prev_len = 0; in fcu_fill_fan_prop()
458 /* Fill the fan location property. */ in fcu_fill_fan_prop()
459 prop_len = OF_getprop(child, "hwctrl-location", location, in fcu_fill_fan_prop()
462 if (sc->sc_fans != NULL) { in fcu_fill_fan_prop()
463 strcpy(sc->sc_fans[i].fan.name, location + len); in fcu_fill_fan_prop()
469 if (sc->sc_fans == NULL) in fcu_fill_fan_prop()
472 /* Fill the fan type property. */ in fcu_fill_fan_prop()
473 len = 0; in fcu_fill_fan_prop()
474 i = 0; in fcu_fill_fan_prop()
475 prev_len = 0; in fcu_fill_fan_prop()
476 prop_len = OF_getprop(child, "hwctrl-type", type, sizeof(type)); in fcu_fill_fan_prop()
478 if (strcmp(type + len, "fan-rpm") == 0) in fcu_fill_fan_prop()
479 sc->sc_fans[i].type = FCU_FAN_RPM; in fcu_fill_fan_prop()
481 sc->sc_fans[i].type = FCU_FAN_PWM; in fcu_fill_fan_prop()
487 /* Fill the fan ID property. */ in fcu_fill_fan_prop()
488 prop_len = OF_getprop(child, "hwctrl-id", id, sizeof(id)); in fcu_fill_fan_prop()
489 for (j = 0; j < i; j++) in fcu_fill_fan_prop()
490 sc->sc_fans[j].id = ((id[j] >> 8) & 0x0f) % 8; in fcu_fill_fan_prop()
492 /* Fill the fan zone property. */ in fcu_fill_fan_prop()
493 prop_len = OF_getprop(child, "hwctrl-zone", id, sizeof(id)); in fcu_fill_fan_prop()
494 for (j = 0; j < i; j++) in fcu_fill_fan_prop()
495 sc->sc_fans[j].fan.zone = id[j]; in fcu_fill_fan_prop()
497 /* Finish setting up fan properties */ in fcu_fill_fan_prop()
498 for (j = 0; j < i; j++) { in fcu_fill_fan_prop()
499 sc->sc_fans[j].dev = sc->sc_dev; in fcu_fill_fan_prop()
500 if (sc->sc_fans[j].type == FCU_FAN_RPM) { in fcu_fill_fan_prop()
501 sc->sc_fans[j].fan.min_rpm = 4800 >> fcu_rpm_shift; in fcu_fill_fan_prop()
502 sc->sc_fans[j].fan.max_rpm = 56000 >> fcu_rpm_shift; in fcu_fill_fan_prop()
503 sc->sc_fans[j].setpoint = in fcu_fill_fan_prop()
504 fcu_fan_get_rpm(&sc->sc_fans[j]); in fcu_fill_fan_prop()
505 sc->sc_fans[j].fan.read = in fcu_fill_fan_prop()
507 sc->sc_fans[j].fan.set = in fcu_fill_fan_prop()
510 sc->sc_fans[j].fan.min_rpm = 30; /* Percent */ in fcu_fill_fan_prop()
511 sc->sc_fans[j].fan.max_rpm = 100; in fcu_fill_fan_prop()
512 sc->sc_fans[j].fan.read = NULL; in fcu_fill_fan_prop()
513 sc->sc_fans[j].fan.set = in fcu_fill_fan_prop()
516 sc->sc_fans[j].fan.default_rpm = sc->sc_fans[j].fan.max_rpm; in fcu_fill_fan_prop()
527 struct fcu_fan *fan; in fcu_fanrpm_sysctl() local
528 int rpm = 0, pwm = 0, error = 0; in fcu_fanrpm_sysctl()
532 fan = &sc->sc_fans[arg2 & 0x00ff]; in fcu_fanrpm_sysctl()
533 if (fan->type == FCU_FAN_RPM) { in fcu_fanrpm_sysctl()
534 rpm = fcu_fan_get_rpm(fan); in fcu_fanrpm_sysctl()
535 if (rpm < 0) in fcu_fanrpm_sysctl()
537 error = sysctl_handle_int(oidp, &rpm, 0, req); in fcu_fanrpm_sysctl()
539 error = fcu_fan_get_pwm(fcu, fan, &pwm, &rpm); in fcu_fanrpm_sysctl()
540 if (error < 0) in fcu_fanrpm_sysctl()
543 switch (arg2 & 0xff00) { in fcu_fanrpm_sysctl()
545 error = sysctl_handle_int(oidp, &pwm, 0, req); in fcu_fanrpm_sysctl()
548 error = sysctl_handle_int(oidp, &rpm, 0, req); in fcu_fanrpm_sysctl()
556 /* We can only read the RPM from a PWM controlled fan, so return. */ in fcu_fanrpm_sysctl()
557 if ((arg2 & 0xff00) == FCU_PWM_SYSCTL_RPM) in fcu_fanrpm_sysctl()
558 return (0); in fcu_fanrpm_sysctl()
560 if (error || !req->newptr) in fcu_fanrpm_sysctl()
563 if (fan->type == FCU_FAN_RPM) in fcu_fanrpm_sysctl()
564 return (fcu_fan_set_rpm(fan, rpm)); in fcu_fanrpm_sysctl()
566 return (fcu_fan_set_pwm(fan, pwm)); in fcu_fanrpm_sysctl()
580 sc->sc_nfans = 0; in fcu_attach_fans()
583 sc->sc_nfans = fcu_fill_fan_prop(dev); in fcu_attach_fans()
585 device_printf(dev, "%d fans detected!\n", sc->sc_nfans); in fcu_attach_fans()
587 if (sc->sc_nfans == 0) { in fcu_attach_fans()
592 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct fcu_fan), M_FCU, in fcu_attach_fans()
598 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "FCU Fan Information"); in fcu_attach_fans()
601 sc->sc_nfans = fcu_fill_fan_prop(dev); in fcu_attach_fans()
604 for (i = 0; i < sc->sc_nfans; i++) in fcu_attach_fans()
605 pmac_thermal_fan_register(&sc->sc_fans[i].fan); in fcu_attach_fans()
608 for (i = 0; i < sc->sc_nfans; i++) { in fcu_attach_fans()
609 for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) { in fcu_attach_fans()
610 sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]); in fcu_attach_fans()
614 sysctl_name[j] = 0; in fcu_attach_fans()
616 if (sc->sc_fans[i].type == FCU_FAN_RPM) { in fcu_attach_fans()
619 0, "Fan Information"); in fcu_attach_fans()
622 &(sc->sc_fans[i].fan.min_rpm), 0, in fcu_attach_fans()
626 &(sc->sc_fans[i].fan.max_rpm), 0, in fcu_attach_fans()
628 /* I use i to pass the fan id. */ in fcu_attach_fans()
631 dev, i, fcu_fanrpm_sysctl, "I", "Fan RPM"); in fcu_attach_fans()
633 fcu_fan_get_pwm(dev, &sc->sc_fans[i], in fcu_attach_fans()
634 &sc->sc_fans[i].setpoint, in fcu_attach_fans()
635 &sc->sc_fans[i].rpm); in fcu_attach_fans()
639 0, "Fan Information"); in fcu_attach_fans()
642 &(sc->sc_fans[i].fan.min_rpm), 0, in fcu_attach_fans()
646 &(sc->sc_fans[i].fan.max_rpm), 0, in fcu_attach_fans()
648 /* I use i to pass the fan id or'ed with the type in fcu_attach_fans()
654 "Fan PWM in %"); in fcu_attach_fans()
658 "Fan RPM"); in fcu_attach_fans()
662 /* Dump fan location, type & RPM. */ in fcu_attach_fans()
665 for (i = 0; i < sc->sc_nfans; i++) { in fcu_attach_fans()
667 "RPM: %d\n", sc->sc_fans[i].fan.name, in fcu_attach_fans()
668 sc->sc_fans[i].type, sc->sc_fans[i].id, in fcu_attach_fans()
669 (sc->sc_fans[i].type == FCU_FAN_RPM) ? in fcu_attach_fans()
670 sc->sc_fans[i].setpoint : in fcu_attach_fans()
671 sc->sc_fans[i].rpm ); in fcu_attach_fans()