Lines Matching +full:pwm +full:- +full:fan

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
75 struct pmac_fan fan;
88 /* We can read the PWM and the RPM from a PWM controlled fan.
276 sc->sc_cmd_phys = segs[0].ds_addr;
288 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
289 sc->sc_cur_cmd = NULL;
290 sc->sc_doorbellirqid = -1;
292 sc->sc_u3 = 0;
293 if (OF_finddevice("/u3") != -1)
294 sc->sc_u3 = 1;
302 NULL, &(sc->sc_dmatag));
303 sc->sc_bt = &bs_le_tag;
304 bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox);
310 bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK |
311 BUS_DMA_ZERO, &sc->sc_cmd_dmamap);
312 bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap,
313 sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0);
314 STAILQ_INIT(&sc->sc_cmdq);
326 /* Some SMUs have RPM and PWM controlled fans which do not sit
342 if (strncmp(name, "smu-i2c-control", 15) == 0)
353 sc->sc_cpu_diode_scale = (data[4] << 8) + data[5];
354 sc->sc_cpu_diode_offset = (data[6] << 8) + data[7];
357 sc->sc_cpu_volt_scale = (data[4] << 8) + data[5];
358 sc->sc_cpu_volt_offset = (data[6] << 8) + data[7];
359 sc->sc_cpu_curr_scale = (data[8] << 8) + data[9];
360 sc->sc_cpu_curr_offset = (data[10] << 8) + data[11];
363 sc->sc_slots_pow_scale = (data[4] << 8) + data[5];
364 sc->sc_slots_pow_offset = (data[6] << 8) + data[7];
369 sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled");
383 sc->sc_doorbellirqid = 0;
384 sc->sc_doorbellirq = bus_alloc_resource_any(smu_doorbell, SYS_RES_IRQ,
385 &sc->sc_doorbellirqid, RF_ACTIVE);
386 bus_setup_intr(smu_doorbell, sc->sc_doorbellirq,
388 &sc->sc_doorbellirqcookie);
389 powerpc_config_intr(rman_get_start(sc->sc_doorbellirq),
421 mtx_assert(&sc->sc_mtx, MA_OWNED);
423 if (sc->sc_u3)
426 sc->sc_cur_cmd = cmd;
429 sc->sc_cmd->cmd = cmd->cmd;
430 sc->sc_cmd->len = cmd->len;
431 memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data));
432 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE);
433 bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys);
435 /* Flush the cacheline it is in -- SMU bypasses the cache */
436 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
456 mtx_lock(&sc->sc_mtx);
458 if (sc->sc_cur_cmd == NULL) /* spurious */
462 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
464 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
466 sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd;
467 sc->sc_cur_cmd->len = sc->sc_cmd->len;
468 memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data,
469 sizeof(sc->sc_cmd->data));
470 wakeup(sc->sc_cur_cmd);
471 sc->sc_cur_cmd = NULL;
472 if (sc->sc_u3)
477 if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) {
478 sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq);
479 STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q);
480 smu_send_cmd(smu, sc->sc_cur_cmd);
483 mtx_unlock(&sc->sc_mtx);
494 cmd_code = cmd->cmd;
496 mtx_lock(&sc->sc_mtx);
497 if (sc->sc_cur_cmd != NULL) {
498 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
501 mtx_unlock(&sc->sc_mtx);
506 if (sc->sc_doorbellirqid < 0) {
511 } while (sc->sc_cur_cmd != NULL);
519 mtx_lock(&sc->sc_mtx);
520 if (cmd->cmd == cmd_code) { /* Never processed */
522 if (sc->sc_cur_cmd == cmd)
523 sc->sc_cur_cmd = NULL;
525 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
527 mtx_unlock(&sc->sc_mtx);
531 mtx_unlock(&sc->sc_mtx);
536 if (cmd->cmd == ((~cmd_code) & 0xff))
600 if (level->rel_set[0].freq == 10000 /* max */)
609 if (level->rel_set[0].freq < 10000 /* max */)
638 if (strcmp(name, "smu-doorbell") != 0)
654 * Sensor and fan management
658 smu_fan_check_old_style(struct smu_fan *fan)
660 device_t smu = fan->dev;
665 if (sc->old_style_fans != -1)
666 return (sc->old_style_fans);
669 * Apple has two fan control mechanisms. We can't distinguish
677 cmd.data[1] = fan->reg;
683 sc->old_style_fans = (error != 0);
685 return (sc->old_style_fans);
689 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
691 device_t smu = fan->dev;
699 rpm = max(fan->fan.min_rpm, rpm);
700 rpm = min(fan->fan.max_rpm, rpm);
702 smu_fan_check_old_style(fan);
704 if (!fan->old_style) {
707 cmd.data[1] = fan->reg;
713 fan->old_style = 1;
716 cmd.data[0] = 0x00; /* RPM fan. */
717 cmd.data[1] = 1 << fan->reg;
718 cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff;
719 cmd.data[3 + 2*fan->reg] = rpm & 0xff;
724 fan->setpoint = rpm;
730 smu_fan_read_rpm(struct smu_fan *fan)
732 device_t smu = fan->dev;
736 smu_fan_check_old_style(fan);
738 if (!fan->old_style) {
742 cmd.data[1] = fan->reg;
746 fan->old_style = 1;
751 if (fan->old_style) {
760 rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
766 smu_fan_set_pwm(struct smu_fan *fan, int pwm)
768 device_t smu = fan->dev;
776 pwm = max(fan->fan.min_rpm, pwm);
777 pwm = min(fan->fan.max_rpm, pwm);
780 * Apple has two fan control mechanisms. We can't distinguish
785 if (!fan->old_style) {
788 cmd.data[1] = fan->reg;
789 cmd.data[2] = (pwm >> 8) & 0xff;
790 cmd.data[3] = pwm & 0xff;
794 fan->old_style = 1;
797 if (fan->old_style) {
799 cmd.data[0] = 0x10; /* PWM fan. */
800 cmd.data[1] = 1 << fan->reg;
801 cmd.data[2 + 2*fan->reg] = (pwm >> 8) & 0xff;
802 cmd.data[3 + 2*fan->reg] = pwm & 0xff;
807 fan->setpoint = pwm;
813 smu_fan_read_pwm(struct smu_fan *fan, int *pwm, int *rpm)
815 device_t smu = fan->dev;
819 if (!fan->old_style) {
823 cmd.data[1] = fan->reg;
827 fan->old_style = 1;
832 if (fan->old_style) {
841 *rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
843 if (fan->old_style) {
847 cmd.data[1] = 1 << fan->reg;
853 *pwm = cmd.data[fan->reg*2+2];
863 struct smu_fan *fan;
864 int pwm = 0, rpm, error = 0;
868 fan = &sc->sc_fans[arg2 & 0xff];
870 if (fan->type == SMU_FAN_RPM) {
871 rpm = smu_fan_read_rpm(fan);
877 error = smu_fan_read_pwm(fan, &pwm, &rpm);
883 error = sysctl_handle_int(oidp, &pwm, 0, req);
893 /* We can only read the RPM from a PWM controlled fan, so return. */
897 if (error || !req->newptr)
900 sc->sc_lastuserchange = time_uptime;
902 if (fan->type == SMU_FAN_RPM)
903 return (smu_fan_set_rpm(fan, rpm));
905 return (smu_fan_set_pwm(fan, pwm));
911 struct smu_fan *fan;
916 fan = &sc->sc_fans[id];
919 /* We have either RPM or PWM controlled fans. */
920 if (strcmp(type, "fan-rpm-control") == 0)
921 fan->type = SMU_FAN_RPM;
923 fan->type = SMU_FAN_PWM;
925 fan->dev = dev;
926 fan->old_style = 0;
927 OF_getprop(child, "reg", &fan->reg,
929 OF_getprop(child, "min-value", &fan->fan.min_rpm,
931 OF_getprop(child, "max-value", &fan->fan.max_rpm,
933 OF_getprop(child, "zone", &fan->fan.zone,
936 if (OF_getprop(child, "unmanaged-value",
937 &fan->fan.default_rpm,
939 fan->fan.default_rpm = fan->fan.max_rpm;
941 OF_getprop(child, "location", fan->fan.name,
942 sizeof(fan->fan.name));
944 if (fan->type == SMU_FAN_RPM)
945 fan->setpoint = smu_fan_read_rpm(fan);
947 smu_fan_read_pwm(fan, &fan->setpoint, &fan->rpm);
951 * after allocating the fan struct, fill the properties of the fans.
968 if (strncmp(name, "rpm-fans", 9) == 0 ||
969 strncmp(name, "pwm-fans", 9) == 0 ||
974 /* When allocated, fill the fan properties. */
975 if (sc->sc_fans != NULL) {
977 nfans - 1);
991 struct smu_fan *fan;
1001 sc->sc_nfans = smu_count_fans(dev);
1002 if (sc->sc_nfans == 0)
1006 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
1013 for (i = 0; i < sc->sc_nfans; i++)
1014 pmac_thermal_fan_register(&sc->sc_fans[i].fan);
1019 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "SMU Fan Information");
1022 for (i = 0; i < sc->sc_nfans; i++) {
1023 fan = &sc->sc_fans[i];
1024 for (j = 0; j < strlen(fan->fan.name); j++) {
1025 sysctl_name[j] = tolower(fan->fan.name[j]);
1030 if (fan->type == SMU_FAN_RPM) {
1034 "Fan Information");
1037 &fan->fan.min_rpm, 0,
1041 &fan->fan.max_rpm, 0,
1046 smu_fanrpm_sysctl, "I", "Fan RPM");
1048 fan->fan.read = (int (*)(struct pmac_fan *))smu_fan_read_rpm;
1049 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_rpm;
1055 "Fan Information");
1058 &fan->fan.min_rpm, 0,
1059 "Minimum allowed PWM in %");
1062 &fan->fan.max_rpm, 0,
1063 "Maximum allowed PWM in %");
1065 "pwm",CTLTYPE_INT | CTLFLAG_RW |
1068 smu_fanrpm_sysctl, "I", "Fan PWM in %");
1073 smu_fanrpm_sysctl, "I", "Fan RPM");
1074 fan->fan.read = NULL;
1075 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_pwm;
1078 device_printf(dev, "Fan: %s type: %d\n",
1079 fan->fan.name, fan->type);
1086 device_t smu = sens->dev;
1094 cmd.data[0] = sens->reg;
1099 return (-1);
1104 switch (sens->type) {
1106 value *= sc->sc_cpu_diode_scale;
1108 value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
1115 value *= sc->sc_cpu_volt_scale;
1116 value += sc->sc_cpu_volt_offset;
1125 value *= sc->sc_cpu_curr_scale;
1126 value += sc->sc_cpu_curr_offset;
1135 value *= sc->sc_slots_pow_scale;
1136 value += sc->sc_slots_pow_offset;
1159 sens = &sc->sc_sensors[arg2];
1182 sc->sc_nsensors = 0;
1185 sc->sc_nsensors++;
1187 if (sc->sc_nsensors == 0) {
1192 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
1195 sens = sc->sc_sensors;
1196 sc->sc_nsensors = 0;
1207 sens->dev = dev;
1210 if (strcmp(type, "current-sensor") == 0) {
1211 sens->type = SMU_CURRENT_SENSOR;
1213 } else if (strcmp(type, "temp-sensor") == 0) {
1214 sens->type = SMU_TEMP_SENSOR;
1216 } else if (strcmp(type, "voltage-sensor") == 0) {
1217 sens->type = SMU_VOLTAGE_SENSOR;
1219 } else if (strcmp(type, "power-sensor") == 0) {
1220 sens->type = SMU_POWER_SENSOR;
1226 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
1227 OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
1228 OF_getprop(child, "location", sens->therm.name,
1229 sizeof(sens->therm.name));
1231 for (i = 0; i < strlen(sens->therm.name); i++) {
1232 sysctl_name[i] = tolower(sens->therm.name[i]);
1238 sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
1242 dev, sc->sc_nsensors, smu_sensor_sysctl,
1243 (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
1245 if (sens->type == SMU_TEMP_SENSOR) {
1247 sens->therm.target_temp = 500 + 2731; /* 50 C */
1248 sens->therm.max_temp = 900 + 2731; /* 90 C */
1250 sens->therm.read =
1252 pmac_thermal_sensor_register(&sens->therm);
1256 sc->sc_nsensors++;
1296 if (error || !req->newptr)
1377 cmd.data[7] = bin2bcd(ct.year - 2000);
1429 if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1442 dinfo->obd_name);
1460 if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1472 mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1473 sc->sc_iic_inuse = 0;
1476 OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1477 sizeof(sc->sc_busno));
1493 mtx_lock(&sc->sc_mtx);
1494 while (sc->sc_iic_inuse)
1495 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1497 sc->sc_iic_inuse = 1;
1502 cmd.data[0] = sc->sc_busno;
1537 mtx_unlock(&sc->sc_mtx);
1539 mtx_lock(&sc->sc_mtx);
1547 mtx_unlock(&sc->sc_mtx);
1549 mtx_lock(&sc->sc_mtx);
1554 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1563 msgs[i].len = cmd.len - 1;
1567 sc->sc_iic_inuse = 0;
1568 mtx_unlock(&sc->sc_mtx);