blob: 1baf104297b815281d1989aa13284c557a930ecb [file] [log] [blame]
Matt Spinler8df7be82017-01-09 15:42:05 -06001#!/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
7use strict;
8use warnings;
9
10use mrw::Targets;
11use mrw::Util;
12use Getopt::Long;
13
14use constant {
15 I2C_TYPE => "i2c"
16};
17
18my $serverwizFile;
19my @hwmon;
20
21GetOptions("x=s" => \$serverwizFile) or printUsage();
22
23if (not defined $serverwizFile) {
24 printUsage();
25}
26
27my $g_targetObj = Targets->new;
28$g_targetObj->loadXML($serverwizFile);
29
30my $bmc = Util::getBMCTarget($g_targetObj);
31
32getI2CSensors($bmc, \@hwmon);
33
34exit 0;
35
36
37#Returns an array of hashes that represent hwmon enabled I2C sensors.
38sub 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.
67sub 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
93sub printUsage
94{
95 print "$0 -x [XML filename]\n";
96 exit(1);
97}