Thang Q. Nguyen | fe4a607 | 2023-06-09 08:55:58 +0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | fanSensorService="xyz.openbmc_project.FanSensor" |
| 4 | |
| 5 | sensorPath="/xyz/openbmc_project/sensors/fan_tach/" |
| 6 | pwmTargetPath="/xyz/openbmc_project/control/fanpwm/" |
| 7 | |
| 8 | sensorValueInterfaceName="xyz.openbmc_project.Sensor.Value" |
| 9 | sensorValuePropertyName="Value" |
| 10 | |
| 11 | pwmTargetInterfaceName="xyz.openbmc_project.Control.FanPwm" |
| 12 | pwmTargetPropertyName="Target" |
| 13 | |
| 14 | function stop_phosphor_fan_services() { |
| 15 | systemctl stop phosphor-fan-control@0.service |
| 16 | systemctl stop phosphor-fan-monitor@0.service |
| 17 | systemctl stop phosphor-fan-presence-tach@0.service |
| 18 | } |
| 19 | |
| 20 | function start_phosphor_fan_services() { |
| 21 | systemctl start phosphor-fan-control@0.service |
| 22 | systemctl start phosphor-fan-monitor@0.service |
| 23 | systemctl start phosphor-fan-presence-tach@0.service |
| 24 | } |
| 25 | |
| 26 | function read_speed() { |
| 27 | fan_val=$(busctl get-property "$fanSensorService" "${sensorPath}$1" "$sensorValueInterfaceName" "$sensorValuePropertyName") |
| 28 | busctl_error=$? |
| 29 | if (( busctl_error != 0 )); then |
| 30 | echo "Error: get-property $sensorValuePropertyName failed! " |
| 31 | exit 1 |
| 32 | fi |
| 33 | |
| 34 | pwm_target=$(busctl get-property "$fanSensorService" "${pwmTargetPath}$2" "$pwmTargetInterfaceName" "$pwmTargetPropertyName") |
| 35 | busctl_error=$? |
| 36 | if (( busctl_error != 0 )); then |
| 37 | echo "Error: get-property $pwmTargetPropertyName failed! " |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | fan_val=$(echo "$fan_val" | cut -d " " -f 2) |
| 42 | pwm_target=$(echo "$pwm_target" | cut -d " " -f 2) |
| 43 | |
| 44 | # Convert fan PWM to Duty cycle, adding 127 for rounding. |
| 45 | pwm_duty_cyle=$(((("$pwm_target" * 100) + 127) / 255)) |
| 46 | |
| 47 | echo "$1, PWM: $pwm_target, Duty cycle: $pwm_duty_cyle%, Speed(RPM): $fan_val" |
| 48 | } |
| 49 | |
| 50 | function set_pwm() { |
| 51 | |
| 52 | # Convert Fan Duty cycle to PWM, adding 50 for rounding. |
| 53 | fan_pwm=$(((($2 * 255) + 50) / 100)) |
| 54 | |
| 55 | busctl set-property "$fanSensorService" "${pwmTargetPath}$1" "$pwmTargetInterfaceName" "$pwmTargetPropertyName" t "$fan_pwm" |
| 56 | busctl_error=$? |
| 57 | if (( busctl_error != 0 )); then |
| 58 | echo "Error: set-property $pwmTargetPropertyName failed! " |
| 59 | exit 255 |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | function getstatus() { |
| 64 | fan_ctl_stt=$(systemctl is-active phosphor-fan-control@0.service | grep inactive) |
| 65 | fan_monitor_stt=$(systemctl is-active phosphor-fan-monitor@0.service | grep inactive) |
| 66 | if [[ -z "$fan_ctl_stt" && -z "$fan_monitor_stt" ]]; then |
| 67 | echo "Thermal Control operational status: Enabled" |
| 68 | exit 0 |
| 69 | else |
| 70 | echo "Thermal Control operational status: Disabled" |
| 71 | exit 1 |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | function setstatus() { |
| 76 | if [ "$1" == 0 ]; then |
| 77 | # Enable fan services |
| 78 | start_phosphor_fan_services |
| 79 | else |
| 80 | # Disable fan services |
| 81 | stop_phosphor_fan_services |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | function setspeed() { |
| 86 | # Get fan_pwm value of the fan |
| 87 | case "$1" in |
| 88 | 0) fan_pwm=PWM7 |
| 89 | ;; |
| 90 | 1) fan_pwm=PWM5 |
| 91 | ;; |
| 92 | 2) fan_pwm=PWM4 |
| 93 | ;; |
| 94 | 3) fan_pwm=PWM3 |
| 95 | ;; |
| 96 | 4) fan_pwm=PWM1 |
| 97 | ;; |
| 98 | 5) fan_pwm=PWM0 |
| 99 | ;; |
| 100 | *) echo "fan $1 doesn't exit" |
| 101 | exit 1 |
| 102 | ;; |
| 103 | esac |
| 104 | |
| 105 | set_pwm "$fan_pwm" "$2" |
| 106 | exit 0 |
| 107 | } |
| 108 | |
| 109 | function getspeed() { |
| 110 | |
| 111 | # Mapping fan number to fan_input and fan_pwm index |
| 112 | case "$1" in |
| 113 | 0) fan_input_f=FAN0_F |
| 114 | fan_input_r=FAN0_R |
| 115 | fan_pwm=PWM7 |
| 116 | ;; |
| 117 | 1) fan_input_f=FAN1_F |
| 118 | fan_input_r=FAN1_R |
| 119 | fan_pwm=PWM5 |
| 120 | ;; |
| 121 | 2) fan_input_f=FAN2_F |
| 122 | fan_input_r=FAN2_R |
| 123 | fan_pwm=PWM4 |
| 124 | ;; |
| 125 | 3) fan_input_f=FAN3_F |
| 126 | fan_input_r=FAN3_R |
| 127 | fan_pwm=PWM3 |
| 128 | ;; |
| 129 | 4) fan_input_f=FAN4_F |
| 130 | fan_input_r=FAN4_R |
| 131 | fan_pwm=PWM1 |
| 132 | ;; |
| 133 | 5) fan_input_f=FAN5_F |
| 134 | fan_input_r=FAN5_F |
| 135 | fan_pwm=PWM0 |
| 136 | ;; |
| 137 | *) echo "fan $1 doesn't exit" |
| 138 | exit 1 |
| 139 | ;; |
| 140 | esac |
| 141 | |
| 142 | # Get fan speed, each fan number has two values is front and rear |
| 143 | read_speed "$fan_input_f" "$fan_pwm" |
| 144 | read_speed "$fan_input_r" "$fan_pwm" |
| 145 | |
| 146 | exit 0 |
| 147 | } |
| 148 | |
| 149 | # Usage of this utility |
| 150 | function usage() { |
| 151 | echo "Usage:" |
| 152 | echo " ampere_fanctrl.sh [getstatus] [setstatus <0|1>] [setspeed <fan> <duty>] [getspeed <fan>]" |
| 153 | echo " fan: 0-5" |
| 154 | echo " duty: 1-100" |
| 155 | } |
| 156 | |
| 157 | if [ "$1" == "getstatus" ]; then |
| 158 | getstatus |
| 159 | elif [ "$1" == "setstatus" ]; then |
| 160 | setstatus "$2" |
| 161 | elif [ "$1" == "setspeed" ]; then |
| 162 | stop_phosphor_fan_services |
| 163 | setspeed "$2" "$3" |
| 164 | elif [ "$1" == "getspeed" ]; then |
| 165 | getspeed "$2" |
| 166 | else |
| 167 | usage |
| 168 | fi |