blob: 7d93a4a7586400ecdddfa7aaf7152bd7e89c1cc7 [file] [log] [blame]
Tom Tung48ea66b2021-08-06 14:19:14 +08001#!/bin/bash
2# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
William A. Kennington IIIc17e6442023-06-02 16:29:54 -070016# shellcheck source=meta-google/recipes-google/gpio/gpio-ctrl/lib.sh
Tom Tung48ea66b2021-08-06 14:19:14 +080017source /usr/share/gpio-host-pwr/lib.sh || exit
18
19gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit
20gpio_init "$HOST_GPIO_PGOOD" || exit
21
22# Set the power status LED
23if [ -n "$HOST_LED_PWR" ]; then
24 echo 'none' > /sys/class/leds/"$HOST_LED_PWR"/trigger || true
25 echo '0' > /sys/class/leds/"$HOST_LED_PWR"/brightness || true
26fi
27
28# Ensure the watchdog is no longer going to run
29host_pwr_stop_watchdog || true
30
31# We don't want to do anything if the machine is already off
32pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || exit
33if (( pgood == 0 )); then
34 echo 'Host is already off, doing nothing' >&2
35 exit 0
36fi
37
38# Do a long push of the button if PGOOD
39echo 'Stopping host power' >&2
40rc=0
41gpio_set_value "$HOST_GPIO_PWR_BTN" 1 || rc=$?
42
43# Loop until we realize that host power is off
44# Limit the loop count to 10 seconds as we should never
45# have to wait this long for poweroff
46s=$SECONDS
47while true; do
48 if ! pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")"; then
49 rc=1
50 break
51 fi
52 if (( pgood == 0 )); then
53 echo 'Host is now off' >&2
54 break
55 fi
56 if (( SECONDS - s > 10 )); then
57 echo 'Poweroff timed out, terminating' >&2
58 rc=2
59 break
60 fi
61 sleep 0.1
62done
63
64gpio_set_value "$HOST_GPIO_PWR_BTN" 0 || rc=$?
65
66exit $rc