Add ir35221 bind/unbind service to vrm control

Add service to bind and unbind ir35221 devices so that i2cset/get
won't run into problems with device conflicts during power on/off
script execution.

Resolves openbmc/openbmc#1702

Change-Id: I77020313f11700acaecf0c04f2c67b7ea7b4da6b
Signed-off-by: Christopher Bostic <cbostic@linux.vnet.ibm.com>
diff --git a/meta-witherspoon/recipes-phosphor/chassis/vrm-control.bb b/meta-witherspoon/recipes-phosphor/chassis/vrm-control.bb
index acb4002..e2c09e1 100644
--- a/meta-witherspoon/recipes-phosphor/chassis/vrm-control.bb
+++ b/meta-witherspoon/recipes-phosphor/chassis/vrm-control.bb
@@ -8,17 +8,41 @@
 RDEPENDS_${PN} += "i2c-tools bash"
 
 S = "${WORKDIR}"
-SRC_URI += "file://vrm-control.sh"
+SRC_URI += "file://vrm-control.sh \
+            file://ir35221-unbind-bind.sh"
 
 do_install() {
         install -d ${D}${bindir}
         install -m 0755 ${WORKDIR}/vrm-control.sh ${D}${bindir}/vrm-control.sh
+        install -m 0755 ${WORKDIR}/ir35221-unbind-bind.sh ${D}${bindir}/ir35221-unbind-bind.sh
 }
 
 TMPL = "vrm-control@.service"
 INSTFMT = "vrm-control@{0}.service"
-TGTFMT = "obmc-chassis-poweron@{0}.target"
-FMT = "../${TMPL}:${TGTFMT}.requires/${INSTFMT}"
+TGTFMT_ON = "obmc-chassis-poweron@{0}.target"
+FMT_ON = "../${TMPL}:${TGTFMT_ON}.requires/${INSTFMT}"
 
-SYSTEMD_SERVICE_${PN} += "${TMPL}"
-SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT', 'OBMC_CHASSIS_INSTANCES')}"
+TMPL_ON_IRBIND = "ir35221-on-bind@.service"
+INSTFMT_ON_IRBIND = "ir35221-on-bind@{0}.service"
+FMT_ON_IRBIND = "../${TMPL_ON_IRBIND}:${TGTFMT_ON}.requires/${INSTFMT_ON_IRBIND}"
+
+TMPL_ON_IRUNBIND = "ir35221-on-unbind@.service"
+INSTFMT_ON_IRUNBIND = "ir35221-on-unbind@{0}.service"
+FMT_ON_IRUNBIND = "../${TMPL_ON_IRUNBIND}:${TGTFMT_ON}.requires/${INSTFMT_ON_IRUNBIND}"
+
+TGTFMT_OFF = "obmc-chassis-poweroff@{0}.target"
+
+TMPL_OFF_IRBIND = "ir35221-off-bind@.service"
+INSTFMT_OFF_IRBIND = "ir35221-off-bind@{0}.service"
+FMT_OFF_IRBIND = "../${TMPL_OFF_IRBIND}:${TGTFMT_OFF}.requires/${INSTFMT_OFF_IRBIND}"
+
+TMPL_OFF_IRUNBIND = "ir35221-off-unbind@.service"
+INSTFMT_OFF_IRUNBIND = "ir35221-off-unbind@{0}.service"
+FMT_OFF_IRUNBIND = "../${TMPL_OFF_IRUNBIND}:${TGTFMT_OFF}.requires/${INSTFMT_OFF_IRUNBIND}"
+
+SYSTEMD_SERVICE_${PN} += "${TMPL} ${TMPL_ON_IRUNBIND} ${TMPL_ON_IRBIND} ${TMPL_OFF_IRUNBIND} ${TMPL_OFF_IRBIND}"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT_ON', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT_ON_IRBIND', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT_ON_IRUNBIND', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT_OFF_IRBIND', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT_OFF_IRUNBIND', 'OBMC_CHASSIS_INSTANCES')}"
diff --git a/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-off-bind@.service b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-off-bind@.service
new file mode 100644
index 0000000..aa5a079
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-off-bind@.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Power off mode bind ir35221 device driver devs
+After=obmc-power-stop@%i.target
+Wants=obmc-power-stop@i.target
+After=vcs_off_poweroff@%i.service
+Conflicts=obmc-chassis-poweron@%i.target
+
+[Service]
+ExecStart=/usr/bin/ir35221-unbind-bind.sh bind
+Type=oneshot
+RemainAfterExit=yes
+
+[Install]
+WantedBy=obmc-chassis-poweroff@%i.target
diff --git a/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-off-unbind@.service b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-off-unbind@.service
new file mode 100644
index 0000000..ef46134
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-off-unbind@.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Power off mode unbind ir35221 device driver devs
+After=obmc-host-stop-pre@%i.target
+Before=vcs_off_poweroff@%i.service
+Conflicts=obmc-chassis-poweron@%i.target
+
+[Service]
+ExecStart=/usr/bin/ir35221-unbind-bind.sh unbind
+Type=oneshot
+RemainAfterExit=yes
+
+[Install]
+WantedBy=obmc-chassis-poweroff@%i.target
diff --git a/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-on-bind@.service b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-on-bind@.service
new file mode 100644
index 0000000..49d1638
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-on-bind@.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=Power on bind ir35221 device driver devs
+After=vcs_on@%i.service
+Wants=obmc-host-start-pre@%i.target
+Before=obmc-host-start-pre@%i.target
+Conflicts=obmc-chassis-poweroff@%i.target
+ConditionPathExists=!/run/openbmc/chassis@%i-on
+
+[Service]
+ExecStart=/usr/bin/ir35221-unbind-bind.sh bind
+Type=oneshot
+RemainAfterExit=yes
+
+[Install]
+WantedBy=obmc-chassis-poweron@%i.target
diff --git a/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-on-unbind@.service b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-on-unbind@.service
new file mode 100644
index 0000000..7638538
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-on-unbind@.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=power on mode unbind ir35221 device driver devs
+Wants=obmc-power-start-pre@%i.target
+Before=obmc-power-start-pre@%i.target
+Conflicts=obmc-chassis-poweroff@%i.target
+ConditionPathExists=!/run/openbmc/chassis@%i-on
+
+[Service]
+ExecStart=/usr/bin/ir35221-unbind-bind.sh unbind
+Type=oneshot
+RemainAfterExit=yes
+
+[Install]
+WantedBy=obmc-chassis-poweron@%i.target
diff --git a/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-unbind-bind.sh b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-unbind-bind.sh
new file mode 100644
index 0000000..eea5070
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/chassis/vrm-control/ir35221-unbind-bind.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+# #########################################################
+# Script to run on witherspoon BMC to unbind/bind the ir35221
+# driver's devices
+
+echo 4-0070 > /sys/bus/i2c/drivers/ir35221/$1
+echo 4-0071 > /sys/bus/i2c/drivers/ir35221/$1
+echo 5-0070 > /sys/bus/i2c/drivers/ir35221/$1
+echo 5-0071 > /sys/bus/i2c/drivers/ir35221/$1