meta-bletchley: detect-gpio-present: sled present managerment service

A service to monitor the presence status of sleds and perform
corresponding actions when sleds are inserted or removed.

This patch require phosphor-gpio-monitor-presence enabled and gpio-keys
defined in kernel dts file, the LORE link as 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: Id43c7c5c40ccad904db3e6d55870b154a68626ca
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/detect-gpio-present/files/detect-sled-present b/meta-facebook/meta-bletchley/recipes-bletchley/detect-gpio-present/files/detect-sled-present
new file mode 100644
index 0000000..62830bf
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/detect-gpio-present/files/detect-sled-present
@@ -0,0 +1,103 @@
+#!/bin/bash
+
+SLED_ID=$1
+SERVICE_NAME="xyz.openbmc_project.Inventory.Manager"
+PRESENT_OBJPATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
+DBUS_PROPERTY_INTF="org.freedesktop.DBus.Properties"
+
+bind_i2c_driver()
+{
+    I2C_BUS_ADDR=$1
+    DRIVER_NAME=$2
+    DRIVER_DIR="/sys/bus/i2c/drivers/${DRIVER_NAME}"
+    if [ ! -e "${DRIVER_DIR}/${I2C_BUS_ADDR}" ]; then
+        echo "${I2C_BUS_ADDR}" > "${DRIVER_DIR}"/bind
+    fi
+}
+
+unbind_i2c_driver()
+{
+    I2C_BUS_ADDR=$1
+    DRIVER_NAME=$2
+    DRIVER_DIR="/sys/bus/i2c/drivers/${DRIVER_NAME}"
+    if [ -e "${DRIVER_DIR}/${I2C_BUS_ADDR}" ]; then
+        echo "${I2C_BUS_ADDR}" > "${DRIVER_DIR}"/unbind
+    fi
+}
+
+sled_insert_action()
+{
+    SLED_ID=$1
+    I2C_BUS_NUM=$((SLED_ID-1))
+
+    # 0022: fusb302
+    bind_i2c_driver "${I2C_BUS_NUM}-0022" "typec_fusb302"
+
+    # 0045: ina230
+    bind_i2c_driver "${I2C_BUS_NUM}-0045" "ina2xx"
+
+    # 0040: mp5023
+    bind_i2c_driver "${I2C_BUS_NUM}-0040" "mp5023"
+
+    # 0041: pca9536
+    bind_i2c_driver "${I2C_BUS_NUM}-0041" "pca953x"
+
+    # 0076: pca9539
+    bind_i2c_driver "${I2C_BUS_NUM}-0076" "pca953x"
+
+    # 004f: tmp421
+    bind_i2c_driver "${I2C_BUS_NUM}-004f" "tmp421"
+
+    # 0067: pca9552
+    bind_i2c_driver "${I2C_BUS_NUM}-0067" "leds-pca955x"
+
+    # 0054: 24c64
+    bind_i2c_driver "${I2C_BUS_NUM}-0054" "at24"
+}
+
+sled_remove_action()
+{
+    SLED_ID=$1
+    I2C_BUS_NUM=$((SLED_ID-1))
+
+    # 0022: fusb302
+    unbind_i2c_driver "${I2C_BUS_NUM}-0022" "typec_fusb302"
+
+    # # 0045: ina230
+    # unbind_i2c_driver "${I2C_BUS_NUM}-0045" "ina2xx"
+
+    # # 0040: mp5023
+    # unbind_i2c_driver "${I2C_BUS_NUM}-0040" "mp5023"
+
+    # # 0041: pca9536
+    # unbind_i2c_driver "${I2C_BUS_NUM}-0041" "pca953x"
+
+    # # 0076: pca9539
+    # unbind_i2c_driver "${I2C_BUS_NUM}-0076" "pca953x"
+
+    # # 004f: tmp421
+    # unbind_i2c_driver "${I2C_BUS_NUM}-004f" "tmp421"
+
+    # # 0067: pca9552
+    # unbind_i2c_driver "${I2C_BUS_NUM}-0067" "leds-pca955x"
+
+    # # 0054: 24c64
+    # unbind_i2c_driver "${I2C_BUS_NUM}-0054" "at24"
+}
+
+
+dbus-monitor --system "type=signal,interface=${DBUS_PROPERTY_INTF},sender=${SERVICE_NAME},path=${PRESENT_OBJPATH}" |
+while read -r line; do
+    case "$line" in
+        *"boolean false"*)
+            echo "SLED${SLED_ID} Removal"
+            sled_remove_action "$SLED_ID"
+            ;;
+        *"boolean true"*)
+            echo "SLED${SLED_ID} Insertion"
+            sled_insert_action "$SLED_ID"
+            ;;
+    esac
+done
+
+exit 0