#!/bin/sh
# Copyright (C) 2023/08/29 VideologyInc
# Licensed on MIT

i2cdetect_enabled() {
	return 0
}

# Hardware detection and device-tree-overlay loading script.
# Loads device-tree-overlays based on the i2c devices detected, and the overlays file.

# | I2C_OVERLAYS structure:  |
# |  addr  |  overlay_fname  |
# |--------|-----------------|
# |  0x40  |  device.dtbo    |

OVERLAYS_FILE="/storage/config/overlays"
I2C_OVERLAYS="/storage/config/i2c-overlays"
CAM_OVERLAYS="/etc/cam-overlays"
# get machine from swudpate-config
. "/etc/default/swupdate"
MACHINE=$hardware

function get_slot
{
    for i in `cat /proc/cmdline`; do
        if [[ $i == bootslot=* ]]; then
            CURRENT_SLOT="${i: -1}"
        fi;
    done
    [ "$CURRENT_SLOT" = "1" ] || CURRENT_SLOT="0";
}

get_cam_overlay() {
    CAM_OVERLAYS=$1
    i2c_name=$2
    cat $CAM_OVERLAYS | while read -r line || [[ -n "$line" ]]; do
        addr=$(echo $line | awk '{ print $1 }')
        cam_type=$(echo $line | awk '{ print $2 }')
        if i2cget -y "$i2c_name" "$addr" 0x00 > /dev/null 2>&1; then echo $cam_type; break; fi
    done
}

i2cdetect_run() {
    # get named gpios
    GPIOS=$(gpioinfo | awk '{ print $3}' | grep '\"*\"' | tr -d '"')
   
   # disable the reset GPIOs for the cameras
    for f in $GPIOS; do
        if [[ "$f" == *RST_CAM* ]]; then
            pp=$(gpiofind $f)
            gpioset ${pp}=0
        fi
    done

    # enable the power-en GPIOs for the cameras and gpio ports
    for f in /sys/devices/platform/scailx_leds/leds/*power/brightness; do echo 1 > $f; done
     
    # enable the reset GPIOs for the cameras
    for f in $GPIOS; do
        if [[ "$f" == *RST_CAM* ]]; then
            pp=$(gpiofind $f)
            gpioset ${pp}=1
        fi
    done

    # mount configfs so we can dynamically load DTB overlays
    mount -t configfs configfs /sys/kernel/config/
    sleep 2.5

    get_slot
    # add everything from the overlays file:
    if [ -f $OVERLAYS_FILE ]; then
        for line in $(cat $OVERLAYS_FILE); do
            mkdir /sys/kernel/config/device-tree/overlays/${line%.*}
            cat /boot/bsp${CURRENT_SLOT}/devicetree/$line > /sys/kernel/config/device-tree/overlays/${line%.*}/dtbo
        done
    fi

    # Itterate over the camera devices
    info "Loading camera overlays"
    [ -f "/boot/bsp${CURRENT_SLOT}/devicetree/cam-overlays" ] && CAM_OVERLAYS="/boot/bsp${CURRENT_SLOT}/devicetree/cam-overlays"
 
    # prime the I2C busses for any slaves that need a few clock pulses
    for i2c in /dev/i2c_names/csi*i2c; do i2cget -a -y "$(cat $i2c)" 0x7f 2>/dev/null; done
    
    for i in 0 1 2 3 4 5; do
        i2c_name=$(strings "/dev/i2c_names/csi${i}_i2c" 2> /dev/null) || continue
        overlay=$(get_cam_overlay $CAM_OVERLAYS $i2c_name)
        [ -z "$overlay" ] && overlay="none.dtbo"
        
        info "Loading camera overlay $overlay for CSI-$i on i2c bus $i2c_name at address $addr"
        # load the overlay
        mkdir -p "/sys/kernel/config/device-tree/overlays/${MACHINE}-cam${i}-${overlay%.*}"
        cat "/boot/bsp${CURRENT_SLOT}/devicetree/${MACHINE}-cam${i}-${overlay}" > "/sys/kernel/config/device-tree/overlays/${MACHINE}-cam${i}-${overlay%.*}/dtbo" || \
        cat "/boot/bsp${CURRENT_SLOT}/devicetree/*cam${i}-${overlay}" > "/sys/kernel/config/device-tree/overlays/${MACHINE}-cam${i}-${overlay%.*}/dtbo"
    done

    # look for i2c devices
    if [ -f $I2C_OVERLAYS ]; then
        while read -r line; do 
            addr=$(echo $line | awk '{ print $1 }')
            overlay=$(echo $line | awk '{ print $2 }')

            # load the overlay
            for f in /sys/bus/i2c/devices/i2c-*/name; do
                i2c_name=$(strings $f 2> /dev/null) || continue
                if i2cget -y "$i2c_name" "$addr" 0x00 > /dev/null 2>&1; then
                    info "Found device on i2c bus $f at address $addr. Loading overlay $overlay"
                    # load the overlay
                    mkdir -p "/sys/kernel/config/device-tree/overlays/${overlay%.*}"
                    cat "/boot/bsp${CURRENT_SLOT}/devicetree/${overlay}" > "/config/device-tree/overlays/${overlay%.*}/dtbo"
                fi
            done
        done < $I2C_OVERLAYS
    fi
}