Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame^] | 1 | #!/usr/bin/env perl |
| 2 | |
| 3 | #Creates a configuration file for each hwmon sensor in the MRW |
| 4 | #for use by phosphor-hwmon. These configuration files contain |
| 5 | #labels and thresholds for the hwmon features for that sensor. |
| 6 | |
| 7 | use strict; |
| 8 | use warnings; |
| 9 | |
| 10 | use mrw::Targets; |
| 11 | use mrw::Util; |
| 12 | use Getopt::Long; |
| 13 | |
| 14 | use constant { |
| 15 | I2C_TYPE => "i2c" |
| 16 | }; |
| 17 | |
| 18 | my $serverwizFile; |
| 19 | my @hwmon; |
| 20 | |
| 21 | GetOptions("x=s" => \$serverwizFile) or printUsage(); |
| 22 | |
| 23 | if (not defined $serverwizFile) { |
| 24 | printUsage(); |
| 25 | } |
| 26 | |
| 27 | my $g_targetObj = Targets->new; |
| 28 | $g_targetObj->loadXML($serverwizFile); |
| 29 | |
| 30 | my $bmc = Util::getBMCTarget($g_targetObj); |
| 31 | |
| 32 | getI2CSensors($bmc, \@hwmon); |
| 33 | |
| 34 | exit 0; |
| 35 | |
| 36 | |
| 37 | #Returns an array of hashes that represent hwmon enabled I2C sensors. |
| 38 | sub getI2CSensors |
| 39 | { |
| 40 | my ($bmc, $hwmon) = @_; |
| 41 | my $connections = $g_targetObj->findConnections($bmc, "I2C"); |
| 42 | |
| 43 | return if ($connections eq ""); |
| 44 | |
| 45 | for my $i2c (@{$connections->{CONN}}) { |
| 46 | |
| 47 | my $chip = $i2c->{DEST_PARENT}; |
| 48 | my @hwmonUnits = Util::getChildUnitsWithTargetType($g_targetObj, |
| 49 | "unit-hwmon-feature", |
| 50 | $chip); |
| 51 | |
| 52 | #If chip didn't have hwmon units, it isn't hwmon enabled. |
| 53 | next unless (scalar @hwmonUnits > 0); |
| 54 | |
| 55 | my %entry; |
| 56 | $entry{type} = I2C_TYPE; |
| 57 | $entry{name} = lc $g_targetObj->getInstanceName($chip); |
| 58 | |
| 59 | push @$hwmon, { %entry }; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | #Reads the I2C attributes for the chip and adds them to the hash. |
| 65 | #This includes the i2C address, and register base address and |
| 66 | #offset for the I2C bus the chip is on. |
| 67 | sub getI2CAttributes |
| 68 | { |
| 69 | my ($i2c, $entry) = @_; |
| 70 | |
| 71 | #The address comes from the destination unit, and needs |
| 72 | #to be the 7 bit value in hex without the 0x. |
| 73 | my $addr = $g_targetObj->getAttribute($i2c->{DEST}, "I2C_ADDRESS"); |
| 74 | $addr = hex($addr) >> 1; |
| 75 | $entry->{addr} = sprintf("%x", $addr); |
| 76 | |
| 77 | #The reg base address and offset may be optional depending on |
| 78 | #the BMC chip type. We'll check later if it's required but missing. |
| 79 | if (!$g_targetObj->isBadAttribute($i2c->{SOURCE}, "REG_BASE_ADDRESS")) { |
| 80 | my $addr = $g_targetObj->getAttribute($i2c->{SOURCE}, |
| 81 | "REG_BASE_ADDRESS"); |
| 82 | $entry->{regBaseAddress} = sprintf("%x", hex($addr)); |
| 83 | } |
| 84 | |
| 85 | if (!$g_targetObj->isBadAttribute($i2c->{SOURCE}, "REG_OFFSET")) { |
| 86 | my $offset = $g_targetObj->getAttribute($i2c->{SOURCE}, |
| 87 | "REG_OFFSET"); |
| 88 | $entry->{regOffset} = sprintf("%x", hex($offset)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | sub printUsage |
| 94 | { |
| 95 | print "$0 -x [XML filename]\n"; |
| 96 | exit(1); |
| 97 | } |