1*501f4c78SPotin Lai#!/bin/bash
2*501f4c78SPotin Lai
3*501f4c78SPotin Laiget_gpio()
4*501f4c78SPotin Lai{
5*501f4c78SPotin Lai    local NET_NAME=$1
6*501f4c78SPotin Lai    local RET_VAL
7*501f4c78SPotin Lai
8*501f4c78SPotin Lai    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
9*501f4c78SPotin Lai    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
10*501f4c78SPotin Lai        echo "get_gpio: can not find gpio, $NET_NAME" >&2
11*501f4c78SPotin Lai        return 1
12*501f4c78SPotin Lai    fi
13*501f4c78SPotin Lai    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
14*501f4c78SPotin Lai        echo "get_gpio: get ${NET_NAME} failed" >&2
15*501f4c78SPotin Lai        return 1
16*501f4c78SPotin Lai    fi
17*501f4c78SPotin Lai    echo "${RET_VAL}"
18*501f4c78SPotin Lai    return 0
19*501f4c78SPotin Lai}
20*501f4c78SPotin Lai
21*501f4c78SPotin Laiset_gpio()
22*501f4c78SPotin Lai{
23*501f4c78SPotin Lai    local NET_NAME=$1
24*501f4c78SPotin Lai    local OUT_VAL=$2
25*501f4c78SPotin Lai    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
26*501f4c78SPotin Lai    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
27*501f4c78SPotin Lai        echo "set_gpio: can not find gpio, $NET_NAME"
28*501f4c78SPotin Lai        return 1
29*501f4c78SPotin Lai    fi
30*501f4c78SPotin Lai
31*501f4c78SPotin Lai    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
32*501f4c78SPotin Lai    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
33*501f4c78SPotin Lai        echo " failed"
34*501f4c78SPotin Lai        return 1
35*501f4c78SPotin Lai    fi
36*501f4c78SPotin Lai
37*501f4c78SPotin Lai    echo " success"
38*501f4c78SPotin Lai    return 0
39*501f4c78SPotin Lai}
40*501f4c78SPotin Lai
41*501f4c78SPotin Laiset_fan()
42*501f4c78SPotin Lai{
43*501f4c78SPotin Lai    FAN_ID=$1
44*501f4c78SPotin Lai    FAN_DUTY=$2
45*501f4c78SPotin Lai    SYSFA_PWM_PATH=""
46*501f4c78SPotin Lai
47*501f4c78SPotin Lai    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
48*501f4c78SPotin Lai    do
49*501f4c78SPotin Lai        if [ -e "$file" ]; then
50*501f4c78SPotin Lai            SYSFA_PWM_PATH="$file"
51*501f4c78SPotin Lai            break
52*501f4c78SPotin Lai        fi
53*501f4c78SPotin Lai    done
54*501f4c78SPotin Lai
55*501f4c78SPotin Lai    if [ -z "$SYSFA_PWM_PATH" ]; then
56*501f4c78SPotin Lai        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
57*501f4c78SPotin Lai        return 1
58*501f4c78SPotin Lai    fi
59*501f4c78SPotin Lai
60*501f4c78SPotin Lai    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
61*501f4c78SPotin Lai        echo "set_fan: incorrect fan duty, $FAN_DUTY"
62*501f4c78SPotin Lai        return 1
63*501f4c78SPotin Lai    fi
64*501f4c78SPotin Lai
65*501f4c78SPotin Lai    # convert duty (0-100) to pwm value (0-255)
66*501f4c78SPotin Lai    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
67*501f4c78SPotin Lai
68*501f4c78SPotin Lai    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
69*501f4c78SPotin Lai    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
70*501f4c78SPotin Lai        echo " failed"
71*501f4c78SPotin Lai        return 1
72*501f4c78SPotin Lai    fi
73*501f4c78SPotin Lai
74*501f4c78SPotin Lai    echo " success"
75*501f4c78SPotin Lai    return 0
76*501f4c78SPotin Lai}
77*501f4c78SPotin Lai
78*501f4c78SPotin Lai
79*501f4c78SPotin Laiis_sled_present()
80*501f4c78SPotin Lai{
81*501f4c78SPotin Lai    local SLED_ID=$1
82*501f4c78SPotin Lai    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
83*501f4c78SPotin Lai    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
84*501f4c78SPotin Lai    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
85*501f4c78SPotin Lai    local PRST_VAL
86*501f4c78SPotin Lai
87*501f4c78SPotin Lai    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
88*501f4c78SPotin Lai    if [ "$PRST_VAL" != "true" ]; then
89*501f4c78SPotin Lai        # not present
90*501f4c78SPotin Lai        return 1
91*501f4c78SPotin Lai    fi
92*501f4c78SPotin Lai
93*501f4c78SPotin Lai    # present
94*501f4c78SPotin Lai    return 0
95*501f4c78SPotin Lai}
96