meta-quanta: meta-gsj: Add quanta-nvme-powerctrl service

Add quanta-nvme-powerctrl which included three features:
1. When BMC bootup, initial nvme ssd powoer output.
2. monitor ssd present update ssd power output.
3. Add bucstl commands to set Present property to d-bus
   nvme manager and set fault led if needed.

Change-Id: I05da565190d178a213a679f66e87419c0c562456
Signed-off-by: Samuel Jiang <Samuel.Jiang@quantatw.com>
Signed-off-by: tony lee <tony.lee@quantatw.com>
diff --git a/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/init_once.sh b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/init_once.sh
new file mode 100644
index 0000000..2ea02ef
--- /dev/null
+++ b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/init_once.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+function set_gpio() {
+  #$1 gpio pin
+  echo $1 > /sys/class/gpio/export
+}
+
+function set_gpio_direction(){
+    #$1 gpio pin, $2 'in','high','low'
+    echo $2 > /sys/class/gpio/gpio$1/direction
+}
+
+function read_gpio_input(){
+    #$1 read input gpio pin
+    cat /sys/class/gpio/gpio$1/value
+}
+
+function read_present_set_related_power(){
+    #$1 read present gpio, $2 output power gpio,$3 output direction
+    var=$(cat /sys/class/gpio/gpio$1/value)
+    # present 0 is plugged,present 1 is removal
+    if [ "$var" == "0" ];then
+        set_gpio_direction $2 "high"
+    else 
+        set_gpio_direction $2 "low"
+    fi
+}
+
+
+## Initial U2_PRESNET_N
+U2_PRESENT=( 148 149 150 151 152 153 154 155 )
+for i in "${U2_PRESENT[@]}";
+do 
+    set_gpio $i;
+    set_gpio_direction $i 'in';
+done
+
+## Initial POWER_U2_EN
+POWER_U2=( 195 196 202 199 198 197 127 126 )
+for i in "${POWER_U2[@]}";
+do
+    set_gpio $i;
+done
+
+## Initial PWRGD_U2
+PWRGD_U2=( 161 162 163 164 165 166 167 168 )
+for i in "${PWRGD_U2[@]}";
+do 
+    set_gpio $i;
+    set_gpio_direction $i 'in';
+done
+
+### Initial SSD Power reference U2_PRESNET_N
+for i in {0..7};
+do
+    read_present_set_related_power "${U2_PRESENT[$i]}" "${POWER_U2[$i]}";
+done 
+
+
+exit 0;
\ No newline at end of file
diff --git a/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_gpio.service b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_gpio.service
new file mode 100644
index 0000000..a5073d3
--- /dev/null
+++ b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_gpio.service
@@ -0,0 +1,10 @@
+[Unit]
+Description = configure GPIO for SSD Power Control
+Wants=org.openbmc.records.events.service
+After=org.openbmc.records.events.service xyz.openbmc_project.nvme.manager.service
+
+[Service]
+ExecStart=/usr/bin/init_once.sh
+
+[Install]
+WantedBy=obmc-standby.target
\ No newline at end of file
diff --git a/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_powermanager.service b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_powermanager.service
new file mode 100644
index 0000000..4a4cd62
--- /dev/null
+++ b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_powermanager.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=SSD NVME Power Manager
+Wants=org.openbmc.records.events.service
+After=org.openbmc.records.events.service nvme_gpio.service xyz.openbmc_project.nvme.manager.service
+
+[Service]
+ExecStart=/usr/bin/nvme_powermanager.sh
+Restart=always
+
+
+[Install]
+WantedBy=obmc-standby.target
\ No newline at end of file
diff --git a/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_powermanager.sh b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_powermanager.sh
new file mode 100644
index 0000000..91beec5
--- /dev/null
+++ b/meta-gsj/recipes-phosphor/quanta-nvme-powerctrl/files/nvme_powermanager.sh
@@ -0,0 +1,73 @@
+#!/bin/bash
+
+U2_PRESENT_STATUS=( 1 1 1 1 1 1 1 1 )
+U2_PRESENT=( 148 149 150 151 152 153 154 155 )
+POWER_U2=( 195 196 202 199 198 197 127 126 )
+PWRGD_U2=( 161 162 163 164 165 166 167 168 )
+
+
+function set_gpio_direction(){
+    #$1 gpio pin, $2 'in','high','low'
+    echo $2 > /sys/class/gpio/gpio$1/direction
+}
+
+function read_present_set_related_power(){
+    #$1 read present number, $2 output power gpio
+    var="${U2_PRESENT_STATUS[$1]}"
+    # present 0 is plugged,present 1 is removal
+    if [ "$var" == "0" ];then
+        set_gpio_direction $2 "high"
+    else 
+        set_gpio_direction $2 "low"
+    fi
+}
+
+function update_u2_status(){
+  #$1 read present gpio
+  var=$(cat /sys/class/gpio/gpio$2/value)
+  U2_PRESENT_STATUS[$1]="$var"
+}
+
+function check_present_and_powergood(){
+    #$2 present gpio, $3 powergood gpio
+    present=$(cat /sys/class/gpio/gpio$2/value)
+    pwrgd=$(cat /sys/class/gpio/gpio$3/value)
+    path=`expr $1`
+    if [ "$present" -eq 0 ] && [ "$pwrgd" -eq 1 ];then        
+        busctl set-property xyz.openbmc_project.nvme.manager /xyz/openbmc_project/nvme/$path xyz.openbmc_project.Inventory.Item Present b true
+    else        
+        busctl set-property xyz.openbmc_project.nvme.manager /xyz/openbmc_project/nvme/$path xyz.openbmc_project.Inventory.Item Present b false
+            if [ "$present" -eq "$pwrgd" ];then
+                #set fault led
+                busctl set-property xyz.openbmc_project.LED.GroupManager /xyz/openbmc_project/led/groups/led\_u2\_$1\_fault xyz.openbmc_project.Led.Group Asserted b true
+            else
+                busctl set-property xyz.openbmc_project.LED.GroupManager /xyz/openbmc_project/led/groups/led\_u2\_$1\_fault xyz.openbmc_project.Led.Group Asserted b false
+            fi
+        
+    fi
+    
+
+}
+
+##Initial U2 present status
+for i in {0..7};
+do 
+    update_u2_status $i "${U2_PRESENT[$i]}"
+done
+
+
+## Loop while
+while :
+do
+  for i in {0..7};
+  do
+    ## 1 scend scan all loop
+    sleep 0.125
+    read=$(cat /sys/class/gpio/gpio${U2_PRESENT[$i]}/value)
+    if [ "${U2_PRESENT_STATUS[$1]}" != read ];then
+        update_u2_status $i "${U2_PRESENT[$i]}"
+        read_present_set_related_power $i "${POWER_U2[$i]}"
+        check_present_and_powergood $i "${U2_PRESENT[$i]}" "${POWER_U2[$i]}"
+    fi 
+  done
+done