meta-fii: meta-mori: Fix mori-lib.sh gpio functions

The functions in mori-lib.sh return the wrong values for get_gpio_num
and get_gpio_ctrl. get_gpio_num is not prepared to handle a device that
has more than 1 gpiobank associated with it. get_gpio_ctrl is dependent
on get_gpio_num to retrieve the correct identifying number for the gpio,
and the path location of the gpio state differs for gpios added by some
devices compared to the norm. mori-lib.sh is modify to account for these
differences.

Tested: I ran on real machine and checked that a gpio added by each type
of device is capable of being read with get_gpio_ctrl without error.

Signed-off-by: Kyle Nieman <kyle.nieman@fii-na.com>
Change-Id: I212ae4f14bbb57b9643befd0aadd8ac5ef4726da
diff --git a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
index e96b329..3054321 100644
--- a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
@@ -16,8 +16,30 @@
     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)
+        BUS_ADDR=$(gpiodetect | grep gpiochip"${CHIP_PIN[0]}" | \
+                   grep -o '\[.*]' | tr -d ' \[\]')
+        GPIO_BASE_DIR=$(cd /sys/bus/i2c/devices/"$BUS_ADDR"/gpio/ || \
+                        exit; ls -1 -v)
+        # Check that there is a single gpiobank per i2c device
+        GPIO_BANKS=$(cd /sys/bus/i2c/devices/"$BUS_ADDR"/ || \
+                     exit ;  ls -1 -d -v gpiochip*)
+        # Determine which GPIO_BASE to use based on the place of the GPIO_BANK
+        # in comparision to GPIO_BANKS
+        # gpiochip# is set in reverse order of numbering for location of
+        # GPIO_BASE_DIR
+        count=$(echo "$GPIO_BANKS" | wc -w)
+        for X in ${GPIO_BANKS}
+        do
+            if [[ $(gpiofind "$1" | cut -d " " -f 1) == "$X" ]]; then
+                # Used to select the correct GPIO_BASE value
+                #shellcheck disable=SC2086
+                GPIO_BASE_DIR=$(echo ${GPIO_BASE_DIR} | cut -d " " -f $count)
+                break
+            fi
+            count=$((count-1))
+        done
+        tmp="/sys/bus/i2c/devices/$BUS_ADDR/gpio/${GPIO_BASE_DIR[0]}/base"
+        GPIO_BASE=$(cat "$tmp")
         echo "$((GPIO_BASE+CHIP_PIN[1]))"
     else
         echo "$((CHIP_PIN[0]*32+CHIP_PIN[1]))"
@@ -36,7 +58,12 @@
 function get_gpio_ctrl() {
     GPIO_NUM=$(get_gpio_num "$1")
     echo "$GPIO_NUM" > /sys/class/gpio/export
-    cat /sys/class/gpio/gpio"$GPIO_NUM"/value
+    # GPIOs added by drivers use different path for value most but not all
+    # drivers follow this trend
+    # Try reading like traditional GPIO, if fails, try reading in driver format
+    if ! cat /sys/class/gpio/gpio"$GPIO_NUM"/value 2> /dev/null ; then
+        cat /sys/class/gpio/"$1"/value
+    fi
     echo "$GPIO_NUM" > /sys/class/gpio/unexport
 }