1#!/bin/bash -e
2# Read and control VCS rails by sending the UCD power sequencer I2C commands.
3# This script assumes that the UCD is controlling VCS rails as GPIOs 5 and 6.
4# Also assumes that those GPIOs are already enabled.
5
6ucd_bus="0"
7ucd_addr="0x64"
8ucd_retries="5"
9
10retry()
11{
12    local i=0
13    until [ $i -ge $ucd_retries ]; do
14        i=$((i+1))
15        retry_output=$("$@") && break
16    done
17    local ret=$?
18    if [ $i -eq $ucd_retries ]; then exit $ret; fi
19}
20
21# Usage: ucd_get address
22# Result stored in $ucd_reg
23ucd_get()
24{
25    retry i2cget -f -y $ucd_bus $ucd_addr "$1" b
26    ucd_reg=$retry_output
27}
28
29# Usage: ucd_get address value
30ucd_set()
31{
32    retry i2cset -f -y $ucd_bus $ucd_addr "$1" "$2" b
33}
34
35vcs_set_gpios()
36{
37    echo -e "\tSetting UCD GPIO 5 to $1"
38    ucd_set 0xFA 5
39    ucd_set 0xFB "$1"
40    ucd_set 0xFB "$1"
41    echo -e "\tSetting UCD GPIO 6 to $1"
42    ucd_set 0xFA 6
43    ucd_set 0xFB "$1"
44    ucd_set 0xFB "$1"
45}
46
47vcs_get()
48{
49    echo Reading VCS settings
50    ucd_set 0xFA 5
51    ucd_get 0xFB
52    local val=
53    val=$(echo "$ucd_reg" | grep -i -c 0x0f)
54    echo -e "\tUCD GPIO 5 state=$val"
55    ucd_set 0xFA 6
56    ucd_get 0xFB
57    val=$(echo "$ucd_reg" | grep -i -c 0x0f)
58    echo -e "\tUCD GPIO 6 state=$val"
59}
60
61
62if [ "$1" == "on" ]; then
63    echo Turning on VCS
64    vcs_set_gpios 0x7
65elif [ "$1" == "off" ]; then
66    echo Turning off VCS
67    vcs_set_gpios 0x3
68else
69    vcs_get
70    echo "$0 <on|off>" to set state
71fi
72