Powercap: add p8 support

P8 uses i2c-occ and powercap is not created.
Add P8 support by creating powercap object with i2c device name.

Fixes openbmc/openbmc#2688

Change-Id: Ia63070d63f4392cc4b084ab628cdbdcf4206c883
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/i2c_occ.cpp b/i2c_occ.cpp
index c364967..00e4187 100644
--- a/i2c_occ.cpp
+++ b/i2c_occ.cpp
@@ -12,6 +12,8 @@
 
 namespace fs = std::experimental::filesystem;
 
+// The occ_master sysfs file
+constexpr auto OCC_MASTER_FILE = "occ_master";
 // The device name's length, e.g. "p8-occ-hwmon"
 constexpr auto DEVICE_NAME_LENGTH = 12;
 // The occ name's length, e.g. "occ"
@@ -21,6 +23,13 @@
 static_assert(sizeof(I2C_OCC_DEVICE_NAME) -1 == DEVICE_NAME_LENGTH);
 static_assert(sizeof(OCC_NAME) -1 == OCC_NAME_LENGTH);
 
+static bool isMasterOcc(const fs::directory_entry& p)
+{
+    auto f = p / OCC_MASTER_FILE;
+    auto str = getFileContent(f);
+    return (!str.empty()) && (str[0] == '1');
+}
+
 std::string getFileContent(const fs::path& f)
 {
     std::string ret(DEVICE_NAME_LENGTH, 0);
@@ -46,10 +55,22 @@
             auto str = getFileContent(f);
             if (str == I2C_OCC_DEVICE_NAME)
             {
-                result.emplace_back(p.path().filename());
+                if (isMasterOcc(p))
+                {
+                    // Insert master occ at the beginning
+                    result.emplace(result.begin(), p.path().filename());
+                }
+                else
+                {
+                    result.emplace_back(p.path().filename());
+                }
             }
         }
-        std::sort(result.begin(), result.end());
+    }
+    if (!result.empty())
+    {
+        // Sort the occ devices except for master
+        std::sort(result.begin() + 1, result.end());
     }
     return result;
 }