Avoid writing same data to IoExpander and ClockBuffer

During initialization hsbp-manager sets zero at bit position of clock buffer register and IO output register that has a NVMe drive in it.

Code changes are made to make sure that if the bit positions are
already set to zero, we can avoid the write to clock buffer register
and IO output register.

Tested:
- Made sure the working of hsbp manager is not affected.
- Made sure that write to clock buffer register and IO output register
  does not happen if we restart hsbp manager service, as bits are
  already set to zeros.

Change-Id: Iaa08be8eba93ecc51ef70cc4bd9fb371de6474a1
Signed-off-by: Arun Lal K M <arun.lal@intel.com>
diff --git a/hsbp-manager/src/hsbp_manager.cpp b/hsbp-manager/src/hsbp_manager.cpp
index b3b1ff5..c14397d 100644
--- a/hsbp-manager/src/hsbp_manager.cpp
+++ b/hsbp-manager/src/hsbp_manager.cpp
@@ -168,6 +168,7 @@
                 }
 
                 std::bitset<8> currByte(read);
+                bool writeRequired = false;
 
                 /* Set zero only at bit position that we have a NVMe drive (i.e.
                  * ignore where byteMap is "-"). We do not want to touch other
@@ -176,20 +177,25 @@
                 {
                     if (byte->second.at(bit) != "-")
                     {
+                        writeRequired = true;
                         currByte.reset(bit);
                     }
                 }
 
-                int ret = i2c_smbus_write_byte_data(
-                    file, static_cast<uint8_t>(outCtrlBaseAddr + i),
-                    static_cast<uint8_t>(currByte.to_ulong()));
-
-                if (ret < 0)
+                if (writeRequired)
                 {
-                    std::cerr << "ClockBuffer : \"" << name
-                              << "\" - Error: Unable to write data to clock "
-                                 "buffer register\n";
-                    return;
+                    int ret = i2c_smbus_write_byte_data(
+                        file, static_cast<uint8_t>(outCtrlBaseAddr + i),
+                        static_cast<uint8_t>(currByte.to_ulong()));
+
+                    if (ret < 0)
+                    {
+                        std::cerr
+                            << "ClockBuffer : \"" << name
+                            << "\" - Error: Unable to write data to clock "
+                               "buffer register\n";
+                        return;
+                    }
                 }
             }
         }
@@ -394,6 +400,7 @@
                 return;
             }
 
+            bool writeRequired = false;
             std::bitset<8> currCtrlVal(read1);
             std::bitset<8> currOutVal(read2);
 
@@ -404,31 +411,37 @@
             {
                 if (io->second.at(bit) != "-")
                 {
+                    writeRequired = true;
                     currCtrlVal.reset(bit);
                     currOutVal.reset(bit);
                 }
             }
 
-            int ret1 = i2c_smbus_write_byte_data(
-                file, static_cast<uint8_t>(confIORegAddr + i),
-                static_cast<uint8_t>(currCtrlVal.to_ulong()));
-            if (ret1 < 0)
+            if (writeRequired)
             {
-                std::cerr << "IoExpander : \"" << name
-                          << "\" - Error: Unable to write data to IO expander "
-                             "IO control register\n";
-                return;
-            }
+                int ret1 = i2c_smbus_write_byte_data(
+                    file, static_cast<uint8_t>(confIORegAddr + i),
+                    static_cast<uint8_t>(currCtrlVal.to_ulong()));
+                if (ret1 < 0)
+                {
+                    std::cerr
+                        << "IoExpander : \"" << name
+                        << "\" - Error: Unable to write data to IO expander "
+                           "IO control register\n";
+                    return;
+                }
 
-            int ret2 = i2c_smbus_write_byte_data(
-                file, static_cast<uint8_t>(outCtrlBaseAddr + i),
-                static_cast<uint8_t>(currOutVal.to_ulong()));
-            if (ret2 < 0)
-            {
-                std::cerr << "IoExpander : \"" << name
-                          << "\" - Error: Unable to write data to IO expander "
-                             "IO output register\n";
-                return;
+                int ret2 = i2c_smbus_write_byte_data(
+                    file, static_cast<uint8_t>(outCtrlBaseAddr + i),
+                    static_cast<uint8_t>(currOutVal.to_ulong()));
+                if (ret2 < 0)
+                {
+                    std::cerr
+                        << "IoExpander : \"" << name
+                        << "\" - Error: Unable to write data to IO expander "
+                           "IO output register\n";
+                    return;
+                }
             }
         }
         initialized = true;