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 | { |
Robert Lippert | 6c77e48 | 2017-05-26 12:45:46 -0700 | [diff] [blame] | 27 | retry i2cget -f -y $ucd_bus $ucd_addr $1 b |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 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 | { |
Robert Lippert | 6c77e48 | 2017-05-26 12:45:46 -0700 | [diff] [blame] | 34 | retry i2cset -f -y $ucd_bus $ucd_addr $1 $2 b |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | vcs_set_gpios() |
| 38 | { |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 39 | echo -e "\tSetting UCD GPIO 5 to $1" |
| 40 | ucd_set 0xFA 5 |
| 41 | ucd_set 0xFB $1 |
| 42 | ucd_set 0xFB $1 |
| 43 | echo -e "\tSetting UCD GPIO 6 to $1" |
| 44 | ucd_set 0xFA 6 |
| 45 | ucd_set 0xFB $1 |
| 46 | ucd_set 0xFB $1 |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | vcs_get() |
| 50 | { |
| 51 | echo Reading VCS settings |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 52 | ucd_set 0xFA 5 |
| 53 | ucd_get 0xFB |
| 54 | local val=`echo $ucd_reg | grep -i -c 0x0f` |
| 55 | echo -e "\tUCD GPIO 5 state=$val" |
| 56 | ucd_set 0xFA 6 |
| 57 | ucd_get 0xFB |
| 58 | local val=`echo $ucd_reg | grep -i -c 0x0f` |
| 59 | echo -e "\tUCD GPIO 6 state=$val" |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 63 | if [ "$1" == "on" ]; then |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 64 | echo Turning on VCS |
| 65 | vcs_set_gpios 0x7 |
Xo Wang | 4e68967 | 2017-01-27 15:00:25 -0800 | [diff] [blame] | 66 | elif [ "$1" == "off" ]; then |
Xo Wang | 9baac6f | 2016-12-15 15:24:22 -0800 | [diff] [blame] | 67 | echo Turning off VCS |
| 68 | vcs_set_gpios 0x3 |
| 69 | else |
| 70 | vcs_get |
| 71 | echo "$0 <on|off>" to set state |
| 72 | fi |