Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 1 | #!/bin/sh -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 | |
| 6 | ucd_bus="0" |
| 7 | ucd_addr="0x64" |
| 8 | ucd_path="/sys/bus/i2c/drivers/ucd9000" |
| 9 | ucd_driver="${ucd_bus}-00${ucd_addr#0x}" |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 10 | ucd_retries="5" |
| 11 | |
| 12 | retry() |
| 13 | { |
| 14 | local i=0 |
| 15 | until [ $i -ge $ucd_retries ]; do |
| 16 | i=$((i+1)) |
| 17 | retry_output=`$@` && break |
| 18 | done |
| 19 | local ret=$? |
| 20 | if [ $i -eq $ucd_retries ]; then exit $ret; fi |
| 21 | } |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 22 | |
| 23 | # Usage: ucd_get address |
| 24 | # Result stored in $ucd_reg |
| 25 | ucd_get() |
| 26 | { |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 27 | retry i2cget -y $ucd_bus $ucd_addr $1 b |
| 28 | ucd_reg=$retry_output |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | # Usage: ucd_get address value |
| 32 | ucd_set() |
| 33 | { |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 34 | retry i2cset -y $ucd_bus $ucd_addr $1 $2 b |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | unbind_ucd() |
| 38 | { |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 39 | if [ -e $ucd_path/$ucd_driver ]; then |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 40 | echo -e "\tUnbinding UCD driver $ucd_driver" |
| 41 | echo $ucd_driver > $ucd_path/unbind |
| 42 | else |
| 43 | echo -e "\tWarning: $ucd_path/$ucd_driver doesn't exist" |
| 44 | fi |
| 45 | } |
| 46 | |
| 47 | rebind_ucd() |
| 48 | { |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 49 | if [ -e $ucd_path ]; then |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 50 | echo -e "\tBinding UCD driver $ucd_driver" |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 51 | local i=0 |
| 52 | until [ -d $ucd_path/$ucd_driver ] || [ $i -ge $ucd_retries ]; do |
| 53 | i=$((i+1)) |
Xo Wang | 9ba0c89 | 2017-02-13 15:38:03 -0800 | [diff] [blame] | 54 | echo $ucd_driver > $ucd_path/bind || ret=$? |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 55 | done |
Xo Wang | 9ba0c89 | 2017-02-13 15:38:03 -0800 | [diff] [blame] | 56 | if [ ! -d $ucd_path/$ucd_driver ]; then exit $ret; fi |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 57 | fi |
| 58 | } |
| 59 | |
| 60 | vcs_set_gpios() |
| 61 | { |
| 62 | unbind_ucd |
| 63 | echo -e "\tSetting UCD GPIO 5 to $1" |
| 64 | ucd_set 0xFA 5 |
| 65 | ucd_set 0xFB $1 |
| 66 | ucd_set 0xFB $1 |
| 67 | echo -e "\tSetting UCD GPIO 6 to $1" |
| 68 | ucd_set 0xFA 6 |
| 69 | ucd_set 0xFB $1 |
| 70 | ucd_set 0xFB $1 |
| 71 | rebind_ucd |
| 72 | } |
| 73 | |
| 74 | vcs_get() |
| 75 | { |
| 76 | echo Reading VCS settings |
| 77 | unbind_ucd |
| 78 | ucd_set 0xFA 5 |
| 79 | ucd_get 0xFB |
| 80 | local val=`echo $ucd_reg | grep -i -c 0x0f` |
| 81 | echo -e "\tUCD GPIO 5 state=$val" |
| 82 | ucd_set 0xFA 6 |
| 83 | ucd_get 0xFB |
| 84 | local val=`echo $ucd_reg | grep -i -c 0x0f` |
| 85 | echo -e "\tUCD GPIO 6 state=$val" |
| 86 | rebind_ucd |
| 87 | } |
| 88 | |
| 89 | |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 90 | if [ "$1" == "on" ]; then |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 91 | echo Turning on VCS |
| 92 | vcs_set_gpios 0x7 |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 93 | elif [ "$1" == "off" ]; then |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 94 | echo Turning off VCS |
| 95 | vcs_set_gpios 0x3 |
| 96 | else |
| 97 | vcs_get |
| 98 | echo "$0 <on|off>" to set state |
| 99 | fi |