meta-fii: meta-kudo: Use GPIO line names instead of number

Replaces hard-coded GPIO throughout the Kudo layer with a reference
to its line name from DTS.

The process to dynamically obtain the GPIO number for Get is added
to kudo-lib.sh,
 1. Use `gpiofind <Line Name>` to obtain gpiochip# and pin#
 2. If the gpiochip# exceeds the number of GPIO banks of Nuvoton,
    then it is an I/O Expander.
    a. Use `gpiodetect` and extract the I2C bus-addr
    b. Obtain GPIO base value from sysfs using bus-addr
    c. The GPIO number is then the (base + pin#)
 3. If the gpiochip# is within Nuvoton GPIO banks, then calculate
    the GPIO number as ((gpiochip#)*32 + pin#)
Then the GPIO number can be used in the export to obtain the value.

Tested with Nuvoton:
 - Can read/set Nuvoton GPIO with defined line names
 - Can read/set GPIO from I/O Expanders with defined line names

Signed-off-by: Charles Boyer <Charles.Boyer@fii-usa.com>
Change-Id: I651c0707a8bbc170d3c5a5fc261e5f9c62453e9f
diff --git a/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh b/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
index 8b2ce0b..934af16 100644
--- a/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
+++ b/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
@@ -16,9 +16,9 @@
 force_off() {
   echo "Powering down Server"
 
-  set_gpio_ctrl 203 out 1
+  set_gpio_ctrl POWER_OUT 1
   sleep 6
-  set_gpio_ctrl 203 out 0
+  set_gpio_ctrl POWER_OUT 0
 }
 
 power_off() {
@@ -29,9 +29,9 @@
 power_on() {
   echo "Powering on Server"
 
-  set_gpio_ctrl 203 out 1
+  set_gpio_ctrl POWER_OUT 1
   sleep 1
-  set_gpio_ctrl 203 out 0
+  set_gpio_ctrl POWER_OUT 0
   busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On
 }
 
@@ -45,8 +45,8 @@
 }
 
 host_status() {
-  BOOT_OK=$(get_gpio_ctrl 194)
-  S5_N=$(get_gpio_ctrl 204)
+  BOOT_OK=$(get_gpio_ctrl S0_FW_BOOT_OK)
+  S5_N=$(get_gpio_ctrl S0_SLPS5_N)
   if [ "$S5_N" == 1 ] || [ "$BOOT_OK" == 1 ]; then
     echo "on"
   else
@@ -66,9 +66,9 @@
     echo "Triggering graceful shutdown"
     mkdir /run/openbmc
     timestamp > "/run/openbmc/host@0-shutdown-req-time"
-    set_gpio_ctrl 70 out 0
+    set_gpio_ctrl S0_SHD_REQ 0
     sleep 3
-    set_gpio_ctrl 70 out 1
+    set_gpio_ctrl S0_SHD_REQ 1
   fi
 }
 
@@ -76,9 +76,9 @@
   if [ "$(host_status)" == "on" ]; then
     echo "Triggering sysreset pin"
     busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
-    set_gpio_ctrl 65 out 0
+    set_gpio_ctrl S0_SYSRESET 0
     sleep 1
-    set_gpio_ctrl 65 out 1
+    set_gpio_ctrl S0_SYSRESET 1
   else
     echo "Host is off, cannot reset."
   fi
diff --git a/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-fw.sh b/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-fw.sh
index 6231aeb..6f9ec72 100644
--- a/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-fw.sh
+++ b/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-fw.sh
@@ -74,23 +74,23 @@
 }
 
 function fwbmccpld() {
-  # BMC_JTAG_MUX_1 #218 0:BMC 1:MB
-  set_gpio_ctrl 218 out 0
+  # BMC_JTAG_MUX_1 0:BMC 1:MB
+  set_gpio_ctrl MB_JTAG_MUX_SEL 0
   if [ "$(loadsvf -d /dev/jtag0 -s $1 -m 0)" -ne  0 ]; then
     echo "BMC CPLD update failed" >&2
     return 1
   fi
   wait
-  set_gpio_ctrl 218 out 1
+  set_gpio_ctrl MB_JTAG_MUX_SEL 1
 
   return 0
 }
 
 function fwmbcpld() {
-  # BMC_JTAG_MUX_1 #218 0:BMC 1:MB
-  # BMC_JTAG_SEL #164 0:BMC 1:CPU
-  set_gpio_ctrl 218 out 1
-  set_gpio_ctrl 164 out 1
+  # BMC_JTAG_MUX_1 0:BMC 1:MB
+  # BMC_JTAG_SEL 0:BMC 1:CPU
+  set_gpio_ctrl MB_JTAG_MUX_SEL 1
+  set_gpio_ctrl BMC_JTAG_MUX_SEL  1
   if [ "$(loadsvf -d /dev/jtag0 -s $1 -m 0)" -ne  0 ]; then
     echo "Mobo CPLD update failed" >&2
     return 1
@@ -101,11 +101,11 @@
 }
 
 function fwscp() {
-  # BMC_I2C_BACKUP_SEL #168 0:failover, 1:main
-  # BMC_CPU_EEPROM_I2C_SEL #85 0:BMC, 1:CPU
-  scp_eeprom_sel=$(get_gpio_ctrl 168)
-  set_gpio_ctrl 168 out 1
-  set_gpio_ctrl 85 out 0
+  # BMC_I2C_BACKUP_SEL 0:failover, 1:main
+  # BMC_CPU_EEPROM_I2C_SEL 0:BMC, 1:CPU
+  scp_eeprom_sel=$(get_gpio_ctrl BACKUP_SCP_SEL)
+  set_gpio_ctrl BACKUP_SCP_SEL 1
+  set_gpio_ctrl CPU_EEPROM_SEL 0
   #shellcheck disable=SC2010
   I2C_BUS_DEV=$(ls -l $devpath/"13-0077/" | grep channel-0 | awk '{ print $11}' | cut -c 8-)
   if [ "$(ampere_eeprom_prog -b $I2C_BUS_DEV -s 0x50 -p -f $1)" -ne  0 ]; then
@@ -113,18 +113,18 @@
     return 1
   fi
   wait
-  set_gpio_ctrl 85 out 1
-  set_gpio_ctrl 168 out "$scp_eeprom_sel"
+  set_gpio_ctrl CPU_EEPROM_SEL 1
+  set_gpio_ctrl BACKUP_SCP_SEL "$scp_eeprom_sel"
 
   return 0
 }
 
 function fwscpback() {
-  # BMC_I2C_BACKUP_SEL #168 0:failover, 1:main
-  # BMC_CPU_EEPROM_I2C_SEL #85 0:BMC, 1:CPU
-  scp_eeprom_sel=$(get_gpio_ctrl 168)
-  set_gpio_ctrl 168 out 0
-  set_gpio_ctrl 85 out 0
+  # BMC_I2C_BACKUP_SEL 0:failover, 1:main
+  # BMC_CPU_EEPROM_I2C_SEL 0:BMC, 1:CPU
+  scp_eeprom_sel=$(get_gpio_ctrl BACKUP_SCP_SEL)
+  set_gpio_ctrl BACKUP_SCP_SEL 0
+  set_gpio_ctrl CPU_EEPROM_SEL 0
   #shellcheck disable=SC2010
   I2C_BUS_DEV=$(ls -l $devpath/"13-0077/" | grep channel-0 | awk '{ print $11}' | cut -c 8-)
   if [ "$(ampere_eeprom_prog -b $I2C_BUS_DEV -s 0x50 -p -f $1)" -ne  0 ]; then
@@ -132,8 +132,8 @@
     return 1
   fi
   wait
-  set_gpio_ctrl 85 out 1
-  set_gpio_ctrl 168 out "$scp_eeprom_sel"
+  set_gpio_ctrl CPU_EEPROM_SEL 1
+  set_gpio_ctrl BACKUP_SCP_SEL "$scp_eeprom_sel"
 
   return 0
 }
diff --git a/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-lib.sh b/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-lib.sh
index 503e413..063cc55 100644
--- a/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-lib.sh
+++ b/meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-lib.sh
@@ -1,19 +1,38 @@
 #!/bin/bash
 
+# get_gpio_num
+# Dynamically obtains GPIO number from chip base and I2C expanders through line name
+# line-name
+function get_gpio_num() {
+    #shellcheck disable=SC2207
+    CHIP_PIN=($(gpiofind "$1" | awk '{print substr ($1, 9 ), $2 }'))
+    #shellcheck disable=SC2128
+    if [ -z "$CHIP_PIN" ]; then
+        echo "Could not find GPIO with name: $1"
+        return 1
+    fi
+
+    if [ "${CHIP_PIN[0]}" -gt 7 ]; then
+        BUS_ADDR=$(gpiodetect | grep gpiochip"${CHIP_PIN[0]}" | awk '{print substr($2, 2, length($2) - 2)}')
+        GPIO_BASE=$(cat /sys/bus/i2c/devices/"$BUS_ADDR"/gpio/*/base)
+        echo "$((GPIO_BASE+CHIP_PIN[1]))"
+    else
+        echo "$((CHIP_PIN[0]*32+CHIP_PIN[1]))"
+    fi
+}
+
 # set_gpio_ctrl
-# pin #, direction, high(1)/low(0)
+# line-name, high(1)/low(0)
 function set_gpio_ctrl() {
-  echo "$1" > /sys/class/gpio/export
-  echo "$2" > /sys/class//gpio/gpio"$1"/direction
-  echo "$3" > /sys/class/gpio/gpio"$1"/value
-  echo "$1" > /sys/class/gpio/unexport
-  sleep 1
+    #shellcheck disable=SC2046
+    gpioset $(gpiofind "$1")="$2"
 }
 
 # get_gpio_ctrl
-# pin #
+# line-name
 function get_gpio_ctrl() {
-  echo "$1" > /sys/class/gpio/export
-  cat /sys/class/gpio/gpio"$1"/value
-  echo "$1" > /sys/class/gpio/unexport
+    GPIO_NUM=$(get_gpio_num "$1")
+    echo "$GPIO_NUM" > /sys/class/gpio/export
+    cat /sys/class/gpio/gpio"$GPIO_NUM"/value
+    echo "$GPIO_NUM" > /sys/class/gpio/unexport
 }
diff --git a/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-boot/init_once.sh b/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-boot/init_once.sh
index cd80674..fa5395d 100644
--- a/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-boot/init_once.sh
+++ b/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-boot/init_once.sh
@@ -6,17 +6,17 @@
 
 function set_mux_default(){
     # set all mux route to CPU before power on host
-    # BMC_CPU_RTC_I2C_SEL #120
-    set_gpio_ctrl 120 out 1
-    # BMC_CPU_DDR_I2C_SEL #84
-    set_gpio_ctrl 84 out 1
-    # BMC_CPU_EEPROM_I2C_SEL #85
-    set_gpio_ctrl 85 out 1
-    # BMC_CPU_PMBUS_SEL #86
-    set_gpio_ctrl 86 out 1
+    # BMC_CPU_RTC_I2C_SEL
+    set_gpio_ctrl CPU_RTC_SEL 1
+    # BMC_CPU_DDR_I2C_SEL
+    set_gpio_ctrl CPU_DDR_SEL 1
+    # BMC_CPU_EEPROM_I2C_SEL
+    set_gpio_ctrl CPU_EEPROM_SEL 1
+    # BMC_CPU_PMBUS_SEL
+    set_gpio_ctrl CPU_VRD_SEL 1
     # LED control
-    # LED_BMC_LIVE #7
-    set_gpio_ctrl 7 out 1
+    # LED_BMC_LIVE
+    set_gpio_ctrl LED_BMC_ALIVE 1
 
     # SPI control
     # Send command to CPLD to switch the bios spi interface to host
@@ -32,27 +32,27 @@
 # Set BMC_I2C_BACKUP_SEL to secondary.
 if [[ $boardver -lt 64 ]]; then
     echo "EVT system. Choosing secondary SCP EEPROM."
-    set_gpio_ctrl 168 out 0
+    set_gpio_ctrl BACKUP_SCP_SEL 0
     set_mux_default
     # Power control
-    # S0_BMC_OK, GPIO 69
-    set_gpio_ctrl 69 out 1
+    # S0_BMC_OK
+    set_gpio_ctrl S0_BMC_OK 1
 else
     echo "DVT or PVT system"
     # sleep so that FRU and all ipmitool Devices are ready before HOST OS
-    # gpio 143 for HPM_STBY_RST_N do to DC-SCM spec
-    set_gpio_ctrl 143 out 1     # on DVT this became HPM_STBY_RST_N (EVT1 came from CPLD)
+    # HPM_STBY_RST_N to DC-SCM spec
+    set_gpio_ctrl HPM_STBY_RST_N 1     # on DVT this became HPM_STBY_RST_N (EVT1 came from CPLD)
     sleep 5 # for the MUX to get ready
     set_mux_default
     # Power control
-    # S0_BMC_OK, GPIO 69
-    set_gpio_ctrl 69 out 1
+    # S0_BMC_OK
+    set_gpio_ctrl S0_BMC_OK 1
 fi
 
 # Disable CPU 1 CLK when cpu not detected
 # echo init_once cpu $CPU1_STATUS > /dev/ttyS0
 # echo init_once board $boardver > /dev/ttyS0
-CPU1_STATUS_N=$(get_gpio_ctrl 136)
+CPU1_STATUS_N=$(get_gpio_ctrl S1_STATUS_N)
 if [[ $CPU1_STATUS_N == 1 ]]; then
     #Execute this only on DVT systems
     if [[ $boardver -lt 64 ]]; then
diff --git a/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-cmd/kudo.sh b/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-cmd/kudo.sh
index 694c23c..58d0d0e 100644
--- a/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-cmd/kudo.sh
+++ b/meta-fii/meta-kudo/recipes-kudo/kudo-sys-utility/kudo-cmd/kudo.sh
@@ -45,38 +45,38 @@
 function reset() {
   case $1 in
     hotswap)
-      # Virtual reset #94
-      set_gpio_ctrl 94 out 1
+      # Virtual reset
+      set_gpio_ctrl HOTSWAP 1
       ;;
     system)
-      # S0 system reset #65
-      set_gpio_ctrl 65 out 0
+      # S0 system reset
+      set_gpio_ctrl S0_SYSRESET 0
       sleep 1
-      set_gpio_ctrl 65 out 1
+      set_gpio_ctrl S0_SYSRESET 1
       ;;
     btn)
-      # power button #203
-      set_gpio_ctrl 203 out 1
+      # virtual power button
+      set_gpio_ctrl POWER_OUT 1
       sleep 1
-      set_gpio_ctrl 203 out 0
+      set_gpio_ctrl POWER_OUT 0
       ;;
     shutdown)
-      # BMC_CPU_SHD_REQ #70
-      set_gpio_ctrl 70 out 0
+      # BMC_CPU_SHD_REQ
+      set_gpio_ctrl S0_SHD_REQ 0
       sleep 3
-      set_gpio_ctrl 70 out 1
+      set_gpio_ctrl S0_SHD_REQ 1
       ;;
     forceOff)
-      # power button #203
-      set_gpio_ctrl 203 out 1
+      # virtual power button
+      set_gpio_ctrl POWER_OUT 1
       sleep 6
-      set_gpio_ctrl 203 out 0
+      set_gpio_ctrl POWER_OUT 0
       ;;
     display)
-      echo "Virtual reset   #94" "$(get_gpio_ctrl 94)"
-      echo "S0 System reset #65" "$(get_gpio_ctrl 65)"
-      echo "Power Button   #203" "$(get_gpio_ctrl 203)"
-      echo "BMC_CPU SHD Req #70" "$(get_gpio_ctrl 70)"
+      echo "Virtual reset   #$(get_gpio_num HOTSWAP)" "$(get_gpio_ctrl HOTSWAP)"
+      echo "S0 System reset #$(get_gpio_num S0_SYSRESET)" "$(get_gpio_ctrl S0_SYSRESET)"
+      echo "Power Button   #$(get_gpio_num POWER_OUT)" "$(get_gpio_ctrl POWER_OUT)"
+      echo "BMC_CPU SHD Req #$(get_gpio_num S0_SHD_REQ)" "$(get_gpio_ctrl S0_SHD_REQ)"
       ;;
     *)
       usage_rst
@@ -149,49 +149,49 @@
       fi
       ;;
     swhost)
-      set_gpio_ctrl 167 out 1
+      set_gpio_ctrl S0_UART0_BMC_SEL 1
       ;;
     swscp1)
-      set_gpio_ctrl 161 out 1
-      set_gpio_ctrl 177 out 1
-      set_gpio_ctrl 198 out 0
+      set_gpio_ctrl S0_UART1_BMC_SEL 1
+      set_gpio_ctrl S1_UART0_BMC_SEL 1
+      set_gpio_ctrl S1_UART1_BMC_SEL 0
       ;;
     swscp2)
-      set_gpio_ctrl 161 out 1
-      set_gpio_ctrl 177 out 1
-      set_gpio_ctrl 198 out 1
+      set_gpio_ctrl S0_UART1_BMC_SEL 1
+      set_gpio_ctrl S1_UART0_BMC_SEL 1
+      set_gpio_ctrl S1_UART1_BMC_SEL 1
       ;;
     swhosthr)
-      set_gpio_ctrl 167 out 0
+      set_gpio_ctrl S0_UART0_BMC_SEL 0
       ;;
     swscphr)
-      set_gpio_ctrl 161 out 0
-      set_gpio_ctrl 177 out 0
+      set_gpio_ctrl S0_UART1_BMC_SEL 0
+      set_gpio_ctrl S1_UART0_BMC_SEL 0
       ;;
     display)
-      if [ "$(get_gpio_ctrl 167)" -eq 1 ]; then
+      if [ "$(get_gpio_ctrl S0_UART0_BMC_SEL)" -eq 1 ]; then
         echo " CPU host to BMC console"
       else
         echo " CPU host to header"
       fi
-      if [ "$(get_gpio_ctrl 161)" -eq 1 ] && [ "$(get_gpio_ctrl 177)" -eq 1 ]; then
-        if [ "$(get_gpio_ctrl 198)" -eq 1 ]; then
+      if [ "$(get_gpio_ctrl S0_UART1_BMC_SEL)" -eq 1 ] && [ "$(get_gpio_ctrl S1_UART0_BMC_SEL)" -eq 1 ]; then
+        if [ "$(get_gpio_ctrl S1_UART1_BMC_SEL)" -eq 1 ]; then
           echo " SCP2 host to BMC console"
         else
           echo " SCP1 host to BMC console"
         fi
-      elif [ "$(get_gpio_ctrl 161)" -eq 0 ] && [ "$(get_gpio_ctrl 177)" -eq 0 ]; then
-        if [ "$(get_gpio_ctrl 198)" -eq 1 ]; then
+      elif [ "$(get_gpio_ctrl S0_UART1_BMC_SEL)" -eq 0 ] && [ "$(get_gpio_ctrl S1_UART0_BMC_SEL)" -eq 0 ]; then
+        if [ "$(get_gpio_ctrl S1_UART1_BMC_SEL)" -eq 1 ]; then
           echo " SCP2 host to Header"
         else
           echo " SCP1 host to Header"
         fi
       else
         echo "It's unknown status"
-        echo "167" "$(get_gpio_ctrl 167)"
-        echo "161" "$(get_gpio_ctrl 161)"
-        echo "177" "$(get_gpio_ctrl 177)"
-        echo "198" "$(get_gpio_ctrl 198)"
+        echo "S0_UART0_BMC_SEL $(get_gpio_ctrl S0_UART0_BMC_SEL)"
+        echo "S0_UART1_BMC_SEL $(get_gpio_ctrl S0_UART1_BMC_SEL)"
+        echo "S1_UART0_BMC_SEL $(get_gpio_ctrl S1_UART0_BMC_SEL)"
+        echo "S1_UART1_BMC_SEL $(get_gpio_ctrl S1_UART1_BMC_SEL)"
       fi
       ;;
     *)
diff --git a/meta-fii/meta-kudo/recipes-phosphor/console/obmc-console/kudo_uart_mux_ctrl.sh b/meta-fii/meta-kudo/recipes-phosphor/console/obmc-console/kudo_uart_mux_ctrl.sh
index b945339..9947483 100644
--- a/meta-fii/meta-kudo/recipes-phosphor/console/obmc-console/kudo_uart_mux_ctrl.sh
+++ b/meta-fii/meta-kudo/recipes-phosphor/console/obmc-console/kudo_uart_mux_ctrl.sh
@@ -31,12 +31,11 @@
 
 case "$1" in
   ttyS1)
-    set_gpio_ctrl 167 out 1
+    set_gpio_ctrl S0_UART0_BMC_SEL 1
     ;;
   ttyS3)
-    set_gpio_ctrl 161 out 1
-    set_gpio_ctrl 183 out 1 
-    set_gpio_ctrl 198 out 0
+    set_gpio_ctrl S0_UART1_BMC_SEL 1
+    set_gpio_ctrl S1_UART1_BMC_SEL 0
     ;;
   *)
     echo "Invalid tty passed to $0. Exiting!" > /dev/ttyS0