1#!/bin/bash
2
3# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
4source /usr/libexec/fb-common-functions
5
6check_valid_sgpio() {
7    sgpio_vaild0=$(gpioget 2 146)
8    sgpio_vaild1=$(gpioget 2 198)
9    sgpio_vaild2=$(gpioget 2 18)
10
11    if [[ "$sgpio_vaild0" == "0" && "$sgpio_vaild1" == "0" && "$sgpio_vaild2" == "1" ]]; then
12        return 0  # true
13    else
14        return 1  # false
15    fi
16}
17
18# Power Good Status
19power_status() {
20    check_valid_sgpio
21    valid_sgpio=$?
22
23    if [ "$(get_gpio "host0-ready")" -eq 1 ] && [ "$valid_sgpio" -eq 0 ] ; then
24        echo "on"
25    else
26        echo "off"
27    fi
28}
29
30# Force DC off
31force_power_off() {
32    if [ "$(power_status)" == "on" ]; then
33        set_gpio power-host-control 0
34        sleep 6
35        set_gpio power-host-control 1
36        sleep 1
37    fi
38}
39
40# Graceful DC off
41graceful_power_off() {
42    if [ "$(power_status)" == "on" ]; then
43        set_gpio power-host-control 0
44        sleep 1
45        set_gpio power-host-control 1
46        sleep 1
47
48        # wait host power off
49        sleep 10
50    fi
51}
52
53# DC on
54power_on() {
55    if [ "$(power_status)" == "off" ]; then
56        set_gpio power-host-control 0
57        sleep 1
58        set_gpio power-host-control 1
59        sleep 1
60
61        for i in $(seq 1 10)
62        do
63            sleep 1
64            if [ "$(power_status)" == "on" ]; then
65               return 0
66            fi
67
68            if [ "$i" -eq 10 ]; then
69               return 1
70            fi
71        done
72    fi
73    return 0
74}
75
76# Host reset
77power_reset() {
78    set_gpio reset-control 0
79    sleep 1
80    set_gpio reset-control 1
81    sleep 1
82    return 0
83}
84