1#!/bin/bash
2
3# Disable check for usage of the definitions within mori-lib.sh
4#shellcheck disable=SC2034
5
6# get_gpio_num
7# Dynamically obtains GPIO number from chip base and I2C expanders
8# line-name
9function get_gpio_num() {
10    #shellcheck disable=SC2207
11    CHIP_PIN=($(gpiofind "$1" | awk '{print substr ($1, 9 ), $2 }'))
12    #shellcheck disable=SC2128
13    if [ -z "$CHIP_PIN" ]; then
14        echo "Could not find GPIO with name: $1"
15        return 1
16    fi
17
18    if [ "${CHIP_PIN[0]}" -gt 7 ]; then
19        BUS_ADDR=$(gpiodetect | grep gpiochip"${CHIP_PIN[0]}" | \
20                   grep -o '\[.*]' | tr -d ' \[\]')
21        GPIO_BASE_DIR=$(cd /sys/bus/i2c/devices/"$BUS_ADDR"/gpio/ || \
22                        exit; ls -1 -v)
23        # Check that there is a single gpiobank per i2c device
24        GPIO_BANKS=$(cd /sys/bus/i2c/devices/"$BUS_ADDR"/ || \
25                     exit ;  ls -1 -d -v gpiochip*)
26        # Determine which GPIO_BASE to use based on the place of the GPIO_BANK
27        # in comparision to GPIO_BANKS
28        # gpiochip# is set in reverse order of numbering for location of
29        # GPIO_BASE_DIR
30        count=$(echo "$GPIO_BANKS" | wc -w)
31        for X in ${GPIO_BANKS}
32        do
33            if [[ $(gpiofind "$1" | cut -d " " -f 1) == "$X" ]]; then
34                # Used to select the correct GPIO_BASE value
35                #shellcheck disable=SC2086
36                GPIO_BASE_DIR=$(echo ${GPIO_BASE_DIR} | cut -d " " -f $count)
37                break
38            fi
39            count=$((count-1))
40        done
41        tmp="/sys/bus/i2c/devices/$BUS_ADDR/gpio/${GPIO_BASE_DIR[0]}/base"
42        GPIO_BASE=$(cat "$tmp")
43        echo "$((GPIO_BASE+CHIP_PIN[1]))"
44    else
45        echo "$((CHIP_PIN[0]*32+CHIP_PIN[1]))"
46    fi
47}
48
49# set_gpio_ctrl
50# line-name, high(1)/low(0)
51function set_gpio_ctrl() {
52    #shellcheck disable=SC2046
53    gpioset $(gpiofind "$1")="$2"
54}
55
56# get_gpio_ctrl
57# line-name
58function get_gpio_ctrl() {
59    GPIO_NUM=$(get_gpio_num "$1")
60    echo "$GPIO_NUM" > /sys/class/gpio/export
61    # GPIOs added by drivers use different path for value most but not all
62    # drivers follow this trend
63    # Try reading like traditional GPIO, if fails, try reading in driver format
64    if ! cat /sys/class/gpio/gpio"$GPIO_NUM"/value 2> /dev/null ; then
65        cat /sys/class/gpio/"$1"/value
66    fi
67    echo "$GPIO_NUM" > /sys/class/gpio/unexport
68}
69
70# rst_bios_spi
71# Resets BIOS SPI EEPROM
72rst_bios_spi() {
73  echo "Reset BIOS SPI EEPROM"
74  set_gpio_ctrl RST_BIOS_EEPROM0_N 0
75  sleep 1
76  set_gpio_ctrl RST_BIOS_EEPROM0_N 1
77}
78
79# Start definitions
80
81# I2C Definitions
82# The array is (<bus> <address>), where address is in hexadecimal.
83I2C_BMC_CPLD=(13 76)
84I2C_MB_CPLD=(0 76)
85I2C_FANCTRL=(35 2c)
86I2C_BMC_PWRSEQ=(48 59)
87I2C_MB_PWRSEQ=(40 40)
88I2C_CPU_EEPROM=(19 50)
89I2C_STBUCK=(33 74)
90I2C_HOTSWAP_CTRL=(25 1f)
91