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/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
 }