meta-bletchley: use gpiomon instead of polling gpio value

Remove gpio polling of switch detect pin in power-ctrl and motor-init.
Use gpiomon and timeout commands instead to improve performance and
timing issue.

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I9e1eb26b070a57f424b341890733dc63c170bf79
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
index 87b3a17..7c250f4 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
@@ -90,25 +90,18 @@
 INIT_POS=$(get_gpio "$DETECT_PIN1")
 
 if [ "$INIT_POS" -eq 1 ];then
-    time_count=0
     echo "Making motor back to initial position..."
     motor-ctrl "$SLED" r >/dev/null
-    while  [ "$INIT_POS" -eq 1 ]  ;do
-        INIT_POS=$(get_gpio "$DETECT_PIN1")
-        sleep 0.1
-        time_count=$(( time_count + 1 ))
-        if [  $time_count -gt $CALIBRATE_TIMEOUT ];then
-            echo "Error: Step motor run over 1 cycle but switch never triggered"
-            break
-        fi
-    done
+    wait_gpio_falling "$DETECT_PIN1" "$CALIBRATE_TIMEOUT"
     motor-ctrl "$SLED" s >/dev/null
 fi
 
+INIT_POS=$(get_gpio "$DETECT_PIN1")
 if [ "$INIT_POS" -eq 0 ];then
     echo "Motor calibrated to initial position."
     exit 0
 else
+    echo "Error: Step motor run over 1 cycle but switch never triggered"
     echo "Find motor initial position failed"
     exit 1
 fi
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
index 8de9e5a..232b6f3 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
@@ -12,7 +12,7 @@
 DELAY_POWER_ON="0.5"
 DELAY_POWER_OFF="5"
 DELAY_POWER_RECOVERY_MODE="10"
-POWER_BTN_TIMEOUT_CNT=60
+POWER_BTN_TIMEOUT_SEC=10
 
 REV_EVT="EVT"
 REV_DVT="DVT"
@@ -98,23 +98,6 @@
     return 0
 }
 
-#Switch pull low while it be touched
-function wait_for_switch()
-{
-    TARGET_PIN=$1
-    TARGET_SWITCH=1
-    TIME_CNT=0
-    while  [ "$TARGET_SWITCH" -eq 1 ]  ;do
-        TARGET_SWITCH=$(get_gpio "$TARGET_PIN")
-        sleep 0.1
-        TIME_CNT=$(( TIME_CNT +1))
-         if [  $TIME_CNT -gt $POWER_BTN_TIMEOUT_CNT ];then
-             echo "Error: Too long to get target switch, force exit"  >&2
-             break
-         fi
-    done
-}
-
 function trigger_power_button()
 {
     local sled_num=$1
@@ -127,7 +110,7 @@
 
     echo "Motor go forward to press Power key"
     motor-ctrl "sled${sled_num}" f >/dev/null
-    wait_for_switch "${GPIO_DETECT_PIN0}"
+    wait_gpio_falling "${GPIO_DETECT_PIN0}" "$POWER_BTN_TIMEOUT_SEC"
     motor-ctrl "sled${sled_num}" s >/dev/null
 
     if [ "$(get_gpio  "$GPIO_DETECT_PIN0")" -eq 0 ];then
@@ -139,7 +122,7 @@
     fi
 
     motor-ctrl "sled${sled_num}" r >/dev/null
-    wait_for_switch "${GPIO_DETECT_PIN1}"
+    wait_gpio_falling "${GPIO_DETECT_PIN1}" "$POWER_BTN_TIMEOUT_SEC"
     motor-ctrl "sled${sled_num}" s >/dev/null
     if [ "$(get_gpio  "$GPIO_DETECT_PIN1")" -eq 0 ];then
         echo "Motor reverse to initial position successful"
@@ -159,7 +142,7 @@
     fi
 
     motor-ctrl "sled${sled_num}" r >/dev/null
-    wait_for_switch "${GPIO_DETECT_PIN1}"
+    wait_gpio_falling "${GPIO_DETECT_PIN1}" "$POWER_BTN_TIMEOUT_SEC"
     motor-ctrl "sled${sled_num}" s >/dev/null
     if [ "$(get_gpio  "$GPIO_DETECT_PIN1")" -eq 0 ];then
         echo "Motor reverse to initial position successful"
@@ -178,7 +161,7 @@
 
     echo "Motor go forward to press Power button"
     motor-ctrl "sled${sled_num}" f >/dev/null
-    wait_for_switch "${GPIO_DETECT_PIN0}"
+    wait_gpio_falling "${GPIO_DETECT_PIN0}" "$POWER_BTN_TIMEOUT_SEC"
     motor-ctrl "sled${sled_num}" s >/dev/null
 
     if [ "$(get_gpio  "$GPIO_DETECT_PIN0")" -eq 0 ];then
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
index 68d0c62..49bca93 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
@@ -38,6 +38,20 @@
     return 0
 }
 
+wait_gpio_falling()
+{
+    local NET_NAME=$1
+    local TIMEOUT_SEC=$2
+
+    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
+    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
+        echo "wait_gpio_falling: can not find gpio, $NET_NAME"
+        return 1
+    fi
+
+    timeout "$TIMEOUT_SEC" gpiomon --falling-edge --num-events=1 "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"
+}
+
 set_fan()
 {
     FAN_ID=$1