#!/bin/bash
#
# Copyright 2020-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

force=$(check_force_arg "$@")
warn_and_confirm "$force"

#
# We prefer writing to sysfs nodes (generated by syscpld driver) rather
# than "i2cset", because i2cset could potentially conflict with syscpld
# driver (who may be talking to 12-0031 at the same time).
#
FANCPLD_RST_N="${SYSCPLD_SYSFS_DIR}/fan_rackmon_rst_n"

echo "Reseting fancpld.."
if [ -f "$FANCPLD_RST_N" ]; then
    echo 0 > "$FANCPLD_RST_N"  # write 0 to trigger FAN_RACKMON card reset
    echo 1 > "$FANCPLD_RST_N"  # write 1 for normal operation
else
    echo "Warning: unable to locate $FANCPLD_RST_N: use i2cset instead.."
    i2cset -f -y 12 0x31 0x39 0xfe
    i2cset -f -y 12 0x31 0x39 0xff
fi

exit 0
