blob: b65b12d1cc66e9eade922215adfa70ae8cae1bf4 [file] [log] [blame]
Xo Wang9baac6f2016-12-15 15:24:22 -08001#!/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
6ucd_bus="0"
7ucd_addr="0x64"
8ucd_path="/sys/bus/i2c/drivers/ucd9000"
9ucd_driver="${ucd_bus}-00${ucd_addr#0x}"
Xo Wang4e689672017-01-27 15:00:25 -080010ucd_retries="5"
11
12retry()
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 Wang9baac6f2016-12-15 15:24:22 -080022
23# Usage: ucd_get address
24# Result stored in $ucd_reg
25ucd_get()
26{
Xo Wang4e689672017-01-27 15:00:25 -080027 retry i2cget -y $ucd_bus $ucd_addr $1 b
28 ucd_reg=$retry_output
Xo Wang9baac6f2016-12-15 15:24:22 -080029}
30
31# Usage: ucd_get address value
32ucd_set()
33{
Xo Wang4e689672017-01-27 15:00:25 -080034 retry i2cset -y $ucd_bus $ucd_addr $1 $2 b
Xo Wang9baac6f2016-12-15 15:24:22 -080035}
36
37unbind_ucd()
38{
Xo Wang4e689672017-01-27 15:00:25 -080039 if [ -e $ucd_path/$ucd_driver ]; then
Xo Wang9baac6f2016-12-15 15:24:22 -080040 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
47rebind_ucd()
48{
Xo Wang4e689672017-01-27 15:00:25 -080049 if [ -e $ucd_path ]; then
Xo Wang9baac6f2016-12-15 15:24:22 -080050 echo -e "\tBinding UCD driver $ucd_driver"
Xo Wang4e689672017-01-27 15:00:25 -080051 local i=0
52 until [ -d $ucd_path/$ucd_driver ] || [ $i -ge $ucd_retries ]; do
53 i=$((i+1))
Xo Wang9ba0c892017-02-13 15:38:03 -080054 echo $ucd_driver > $ucd_path/bind || ret=$?
Xo Wang4e689672017-01-27 15:00:25 -080055 done
Xo Wang9ba0c892017-02-13 15:38:03 -080056 if [ ! -d $ucd_path/$ucd_driver ]; then exit $ret; fi
Xo Wang9baac6f2016-12-15 15:24:22 -080057 fi
58}
59
60vcs_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
74vcs_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 Wang4e689672017-01-27 15:00:25 -080090if [ "$1" == "on" ]; then
Xo Wang9baac6f2016-12-15 15:24:22 -080091 echo Turning on VCS
92 vcs_set_gpios 0x7
Xo Wang4e689672017-01-27 15:00:25 -080093elif [ "$1" == "off" ]; then
Xo Wang9baac6f2016-12-15 15:24:22 -080094 echo Turning off VCS
95 vcs_set_gpios 0x3
96else
97 vcs_get
98 echo "$0 <on|off>" to set state
99fi