meta-bletchley: introduce Bletchley common functions

Add a common functions script for reusing the functions which are
called frequently.

This patch depends on the patchs below:
* https://lore.kernel.org/all/20220613095150.21917-4-potin.lai.pt@gmail.com/

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I6e1602b00f1b0a19e77506be6eabe18c796e52fe
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
index d74909c..b4a6988 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
@@ -2,41 +2,9 @@
 #
 # Control step motor rotate of sled
 
-function set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-    echo " success"
-    return 0
-}
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
 
-function get_gpio()
-{
-    NET_NAME=$1
-    RET_VAL=2
-
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "get_gpio: can not find gpio, $NET_NAME" >&2
-        return 1
-    fi
-    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
-        echo "get_gpio: get ${NET_NAME} failed" >&2
-        return 1
-    fi
-    echo "${RET_VAL}"
-    return 0
-}
 #######################################
 # Setting step motor control pins to start/stop motor
 # Arguments:
@@ -79,8 +47,7 @@
 fi
 
 #Check if sled is present
-SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
-if [ "$SLED_PRESENT" != 0 ];then
+if ! is_sled_present "${SLED_NUM}"; then
     echo "${SLED} is not present!"
     exit 1
 fi
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 89369ac..87b3a17 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
@@ -7,6 +7,9 @@
 
 export PATH=$PATH:/usr/libexec
 
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
+
 PWM_CLASS_PATH="/sys/class/pwm/pwmchip0"
 #Sleld 1~6 using bmc pwm8~13 as motor driver stick
 PWM_NUM_OFFSET=7
@@ -45,42 +48,6 @@
     fi
 }
 
-function set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-    echo " success"
-    return 0
-}
-
-function get_gpio()
-{
-    NET_NAME=$1
-    RET_VAL=2
-
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "get_gpio: can not find gpio, $NET_NAME" >&2
-        return 1
-    fi
-    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
-        echo "get_gpio: get ${NET_NAME} failed" >&2
-        return 1
-    fi
-    echo "${RET_VAL}"
-    return 0
-}
-
 #Init gpio pins for step motor control
 function init_gpios() {
     echo "Init GPIOs:"
@@ -107,8 +74,7 @@
 fi
 
 #Check if sled is present
-SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
-if [ "$SLED_PRESENT" != 0 ];then
+if ! is_sled_present "${SLED_NUM}"; then
     echo "${SLED} is not present, skip motor initialize"
     exit 1
 fi
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
index 41f81c2..f28ab5e 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
@@ -2,6 +2,8 @@
 Description=Motor Initialize for sled%i
 StartLimitIntervalSec=90
 StartLimitBurst=5
+After=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i
+Wants=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i
 
 [Service]
 ExecStart=/usr/libexec/motor-init sled%i
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 def4689..e430f8f 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
@@ -6,6 +6,9 @@
 
 export PATH=$PATH:/usr/sbin:/usr/libexec
 
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
+
 DELAY_POWER_ON="0.5"
 DELAY_POWER_OFF="5"
 DELAY_POWER_RECOVERY_MODE="10"
@@ -189,42 +192,6 @@
     echo "$bus"
 }
 
-function set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-    echo " success"
-    return 0
-}
-
-function get_gpio()
-{
-    NET_NAME=$1
-    RET_VAL=2
-
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "get_gpio: can not find gpio, $NET_NAME" >&2
-        return 1
-    fi
-    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
-        echo "get_gpio: get ${NET_NAME} failed" >&2
-        return 1
-    fi
-    echo "${RET_VAL}"
-    return 0
-}
-
 function get_ac_status()
 {
     i2c_bus=$(get_bus_num "$1")
@@ -619,8 +586,7 @@
 fi
 
 #Check if sled is present
-SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
-if [ "$SLED_PRESENT" != 0 ];then
+if ! is_sled_present "${SLED_NUM}"; then
     echo "${SLED} is not present!"
     exit 1
 elif ! is_valid_sled_action "$ACTION"; then