Add fb-powerctrl to Facebook Tiogapass

Added a new module fb-powerctrl to comtrol host power. It
includes power-util which controls power on and off of
given host server.

Tested: Build Facebook TiogaPass board and load on the
target hardware. Ensured that power on and off features
works as expected.

(From meta-facebook rev: 67abf96e04c1aab15c90480eca2b00bf9b5c51ab)

Change-Id: I21090bbe461111c15e404c544f303615fbf62c9b
Signed-off-by: Vijay Khemka <vijaykhemka@fb.com>
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/fb-powerctrl.bb b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/fb-powerctrl.bb
new file mode 100644
index 0000000..37fa835
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/fb-powerctrl.bb
@@ -0,0 +1,23 @@
+FILESEXTRAPATHS_append := "${THISDIR}/files:"
+
+inherit systemd
+
+S = "${WORKDIR}/"
+
+SRC_URI = "file://setup_gpio.sh \
+           file://power-util \
+           file://host-gpio.service \
+           file://host-poweroff.service \
+           file://host-poweron.service"
+
+DEPENDS = "systemd"
+RDEPENDS_${PN} = "bash"
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE_${PN} = "host-gpio.service host-poweron.service host-poweroff.service"
+
+do_install() {
+    install -d ${D}/usr/sbin
+    install -m 0755 ${S}setup_gpio.sh ${D}/${sbindir}/
+    install -m 0755 ${S}power-util ${D}/${sbindir}/
+}
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-gpio.service b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-gpio.service
new file mode 100644
index 0000000..00c116b
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-gpio.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Configure GPIOs for Host Power Control
+
+[Service]
+Restart=no
+RemainAfterExit=true
+Type=oneshot
+ExecStart=/usr/sbin/setup_gpio.sh
+SyslogIdentifier=setup_gpio.sh
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-poweroff.service b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-poweroff.service
new file mode 100644
index 0000000..95c7708
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-poweroff.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Shutdown Host Server
+Requires=host-gpio.service
+After=host-gpio.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/power-util mb 1 off
+SyslogIdentifier=power-util
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-poweron.service b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-poweron.service
new file mode 100644
index 0000000..b070eda
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/host-poweron.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Poweron Host Server
+Requires=host-gpio.service
+After=host-gpio.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/power-util mb 1 on
+SyslogIdentifier=power-util
+
+[Install]
+WantedBy=basic.target
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/power-util b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/power-util
new file mode 100755
index 0000000..59f91ec
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/power-util
@@ -0,0 +1,49 @@
+#!/bin/bash
+# Usage of this utility
+function usage() {
+	echo "usage: power-util mb 1 [on|off]";
+}
+
+GPIO_BASE=$(cat /sys/class/gpio/gpio*/base)
+PWR_GPIO=$(($GPIO_BASE + 32 + 3))
+BMC_RDY_GPIO=$(($GPIO_BASE + 144 +1))
+
+if [ $# -lt 3 ]; then
+	echo "Total number of parameter=$#"
+	echo "Insufficient parameter"
+	usage;
+	exit 0;
+fi
+
+if [ $1 != "mb" ]; then
+	echo "Invalid parameter1=$1"
+	usage;
+	exit 0;
+fi
+
+if [ $2 -ne 1 ]; then
+	echo "Invalid parameter2=$2, Server $2 not supported"
+	usage;
+	exit 0;
+fi
+
+if [ $3 = "on" ]; then
+	echo "Powering on Server $2"
+	echo 0 > /sys/class/gpio/gpio${BMC_RDY_GPIO}/value
+	echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
+	echo 0 > /sys/class/gpio/gpio${PWR_GPIO}/value
+	sleep 1
+	echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
+elif [ $3 = "off" ]; then
+	echo "Shutting down Server $2"
+	echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
+	sleep 1
+	echo 0 > /sys/class/gpio/gpio${PWR_GPIO}/value
+	sleep 6
+	echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
+else
+	echo "Invalid parameter3=$3"
+	usage;
+fi
+
+exit 0;
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/setup_gpio.sh b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/setup_gpio.sh
new file mode 100755
index 0000000..4e13109
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/fb-powerctrl/files/setup_gpio.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# Set all output GPIOs as such and drive them with reasonable values.
+function set_gpio_active_low() {
+  if [ $# -ne 2 ]; then
+    echo "set_gpio_active_low: need both GPIO# and initial level";
+    return;
+  fi
+
+  echo $1 > /sys/class/gpio/export
+  echo $2 > /sys/class/gpio/gpio$1/direction
+}
+
+GPIO_BASE=$(cat /sys/class/gpio/gpio*/base)
+
+# FM_BMC_READY_N, GPIO S1, active low
+set_gpio_active_low $((${GPIO_BASE} + 144 +1)) high
+
+# FM_BMC_SSB_SMI_LPC_N, GPIO Q6, active low
+set_gpio_active_low $((${GPIO_BASE} + 128 + 6)) high
+
+# FP_PWR_BTN_PASS_R_N, GPIO E3, active low
+set_gpio_active_low $((${GPIO_BASE} + 32 + 3)) high
+
+# FP_PWR_GOOD, GPIO B6, active low
+set_gpio_active_low $((${GPIO_BASE} + 8 + 6)) high
+
+exit 0;
diff --git a/meta-facebook/meta-tiogapass/recipes-fbtp/packagegroups/packagegroup-fb-apps.bb b/meta-facebook/meta-tiogapass/recipes-fbtp/packagegroups/packagegroup-fb-apps.bb
index bce17de..09cb8b2 100644
--- a/meta-facebook/meta-tiogapass/recipes-fbtp/packagegroups/packagegroup-fb-apps.bb
+++ b/meta-facebook/meta-tiogapass/recipes-fbtp/packagegroups/packagegroup-fb-apps.bb
@@ -16,4 +16,5 @@
 RDEPENDS_${PN}-system = " \
         entity-manager \
         dbus-sensors \
+        fb-powerctrl \
         "