1#!/bin/sh -e
2# AVSBus control for PMBUS voltage regulator modules (VRMs)
3# Switches output voltage target between
4# - VOUT_COMMAND register (AVSBus disabled, default on Zaius)
5# - AVSBus target output (AVSBus enabled, voltage set by host)
6
7cpu0_i2c_bus="7"
8cpu1_i2c_bus="8"
9buses="$cpu0_i2c_bus $cpu1_i2c_bus"
10vdd_i2c_addr_page="0x60:0x01"
11vdn_i2c_addr_page="0x64:0x01"
12vcs_i2c_addr_page="0x64:0x00"
13addrs_pages="$vdd_i2c_addr_page $vdn_i2c_addr_page $vcs_i2c_addr_page"
14
15i2c_path="/sys/bus/i2c/devices/"
16
17# Usage: vrm_avs_enable <bus> <i2c_address> <page>
18# Initializes the AVSBus VOUT setpoint to the value in PMBus VOUT_COMMAND
19vrm_avs_enable()
20{
21    echo Enabling AVSBus on bus $1 VRM @$2 rail $3...
22    echo 1 > $(echo ${i2c_path}/$1-$(printf "%04x" $2)/hwmon/hwmon*/avs$(printf "%d" $3)_enable)
23}
24
25# Usage: vrm_avs_disable <bus> <i2c_address> <page>
26# Sets OPERATION PMBUS register to
27# - Enable/Disable: On
28# - VOUT Source: VOUT_COMMAND
29# - AVSBus Copy: VOUT_COMMAND remains unchanged
30vrm_avs_disable()
31{
32    echo Disabling AVSBus on bus $1 VRM @$2 rail $3...
33    echo 0 > $(echo ${i2c_path}/$1-$(printf "%04x" $2)/hwmon/hwmon*/avs$(printf "%d" $3)_enable)
34}
35
36# Usage: for_each_rail <command>
37# <command> will be invoked with <bus> <i2c_address> <page>
38for_each_rail()
39{
40    for bus in $buses
41    do
42        for addr_page in $addrs_pages
43        do
44            $1 $bus `echo $addr_page | tr : " "`
45        done
46    done
47}
48
49if [ "$1" == "enable" ]
50then
51    for_each_rail vrm_avs_enable
52elif [ "$1" == "disable" ]
53then
54    for_each_rail vrm_avs_disable
55else
56    echo "\"$0 <enable|disable>\" to control whether VRMs use AVSBus"
57    echo "\"$0 <vdn_max>\" to set VDN rails VOUT_MAX to 1.1V"
58fi
59