Provide the quanta host power control.

  systemctl start obmc-chassis-poweroff@0.target
  systemctl start obmc-chassis-poweron@0.target

Added the host-gpio.service on boot to enable the power on
and power off scripts.

Testing: The scripts were launched with the state change commands above,
however they were modified such that they didn't rely on the old
namespace.

The GPIOs basically trigger the power button on the front panel.

Change-Id: I4a205e60c8f43f626f3ea0d4fedf182ccf4700ba
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/images/obmc-phosphor-image.bbappend b/meta-quanta/meta-q71l/recipes-phosphor/images/obmc-phosphor-image.bbappend
index ba64218..6124ab0 100644
--- a/meta-quanta/meta-q71l/recipes-phosphor/images/obmc-phosphor-image.bbappend
+++ b/meta-quanta/meta-q71l/recipes-phosphor/images/obmc-phosphor-image.bbappend
@@ -1 +1,3 @@
 OBMC_IMAGE_EXTRA_INSTALL_append_quanta-q71l = " spictrl"
+OBMC_IMAGE_EXTRA_INSTALL_append_quanta-q71l = " quanta-powerctrl"
+
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-gpio.service b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-gpio.service
new file mode 100644
index 0000000..68adedf
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-gpio.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Configure GPIOs for Host Power Control
+
+[Service]
+Restart=no
+RemainsAfterExit=true
+Type=oneshot
+ExecStart={sbindir}/init_once.sh
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-poweroff.service b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-poweroff.service
new file mode 100644
index 0000000..d8159a1
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-poweroff.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Stop Host
+Requires=host-gpio.service
+After=host-gpio.service
+Conflicts=obmc-chassis-power-on@0.target
+
+[Service]
+RemainAfterExit=yes
+Type=oneshot
+ExecStart={sbindir}/poweroff.sh
+
+[Install]
+WantedBy=obmc-chassis-power-off@0.target
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-poweron.service b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-poweron.service
new file mode 100644
index 0000000..ee505c9
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/host-poweron.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Start Host
+Requires=host-gpio.service
+After=host-gpio.service
+Conflicts=obmc-chassis-poweroff@0.target
+
+[Service]
+RemainAfterExit=yes
+Type=oneshot
+ExecStart={sbindir}/poweron.sh
+
+[Install]
+WantedBy=obmc-chassis-poweron@0.target
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/init_once.sh b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/init_once.sh
new file mode 100755
index 0000000..f157112
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/init_once.sh
@@ -0,0 +1,32 @@
+#!/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 1 > /sys/class/gpio/gpio$1/active_low
+  echo $2 > /sys/class/gpio/gpio$1/direction
+}
+
+GPIO_BASE=$(cat /sys/class/gpio/gpiochip*/base)
+
+# FM_BMC_READY_N, GPIO Q4, active low
+set_gpio_active_low $((${GPIO_BASE} + 128 + 4)) high
+
+# FM_BMC_SSB_SMI_LPC_N, GPIO Q6, active low
+set_gpio_active_low $((${GPIO_BASE} + 128 + 6)) high
+
+# FM_BMC_SYS_THROTTLE_N, GPIO A3, active low
+set_gpio_active_low $((${GPIO_BASE} + 0 + 3)) high
+
+# FM_BMC_SSB_SCI_LPC_N, GPIO E4, active low
+set_gpio_active_low $((${GPIO_BASE} + 32 + 4)) high
+
+# FP_PWR_BTN_PASS_R_N, GPIO D3, active low
+set_gpio_active_low $((${GPIO_BASE} + 24 + 3)) high
+
+exit 0;
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/poweroff.sh b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/poweroff.sh
new file mode 100755
index 0000000..e429131
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/poweroff.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+GPIO_BASE=$(cat /sys/class/gpio/gpiochip*/base)
+GPIO_NUM=$(($GPIO_BASE + 24 + 3))
+
+echo 1 > /sys/class/gpio/gpio${GPIO_NUM}/value
+sleep 5
+echo 0 > /sys/class/gpio/gpio${GPIO_NUM}/value
+
+exit 0;
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/poweron.sh b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/poweron.sh
new file mode 100755
index 0000000..a0f0326
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/files/poweron.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+GPIO_BASE=$(cat /sys/class/gpio/gpiochip*/base)
+GPIO_NUM=$(($GPIO_BASE + 24 + 3))
+
+echo 1 > /sys/class/gpio/gpio${GPIO_NUM}/value
+sleep 1
+echo 0 > /sys/class/gpio/gpio${GPIO_NUM}/value
+
+exit 0;
diff --git a/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/quanta-powerctrl.bb b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/quanta-powerctrl.bb
new file mode 100644
index 0000000..fd02327
--- /dev/null
+++ b/meta-quanta/meta-q71l/recipes-phosphor/quanta-powerctrl/quanta-powerctrl.bb
@@ -0,0 +1,27 @@
+FILESEXTRAPATHS_append := "${THISDIR}/files:"
+
+inherit systemd
+inherit obmc-phosphor-license
+inherit obmc-phosphor-systemd
+
+S = "${WORKDIR}/"
+
+SRC_URI = "file://init_once.sh \
+           file://poweroff.sh \
+           file://poweron.sh \
+           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}init_once.sh ${D}/${sbindir}/
+    install -m 0755 ${S}poweroff.sh ${D}/${sbindir}/
+    install -m 0755 ${S}poweron.sh ${D}/${sbindir}/
+}