pseq: Create UCD90160Device class

Create the UCD90160Device class in the phosphor-power-sequencer
application.

This class represents the UCD90160 power sequencer device.

Change-Id: If61ccef83b52625f3ebe9fdea88f5f80a5e61f9f
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/phosphor-power-sequencer/src/meson.build b/phosphor-power-sequencer/src/meson.build
index 1f6c04c..eb641b9 100644
--- a/phosphor-power-sequencer/src/meson.build
+++ b/phosphor-power-sequencer/src/meson.build
@@ -10,6 +10,7 @@
     'rail.cpp',
     'services.cpp',
     'standard_device.cpp',
+    'ucd90160_device.cpp',
     'ucd90320_device.cpp',
     'ucd90x_device.cpp',
     implicit_include_directories: false,
diff --git a/phosphor-power-sequencer/src/ucd90160_device.cpp b/phosphor-power-sequencer/src/ucd90160_device.cpp
new file mode 100644
index 0000000..58e6a3e
--- /dev/null
+++ b/phosphor-power-sequencer/src/ucd90160_device.cpp
@@ -0,0 +1,79 @@
+/**
+ * Copyright © 2024 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ucd90160_device.hpp"
+
+#include "format_utils.hpp"
+#include "standard_device.hpp"
+
+#include <algorithm>
+#include <array>
+#include <format>
+#include <span>
+
+namespace phosphor::power::sequencer
+{
+
+/**
+ * UCD90160 GPIO names.
+ *
+ * The array indices correspond to the Pin IDs defined in the UCD90160 PMBus
+ * interface documentation.  These Pin IDs are the same as the libgpiod line
+ * offsets used to obtain the GPIO values.
+ */
+static constexpr std::array<const char*, 26> gpioNames = {
+    "FPWM1_GPIO5", "FPWM2_GPIO6",  "FPWM3_GPIO7",  "FPWM4_GPIO8",
+    "FPWM5_GPIO9", "FPWM6_GPIO10", "FPWM7_GPIO11", "FPWM8_GPIO12",
+    "GPI1_PWM1",   "GPI2_PWM2",    "GPI3_PWM3",    "GPI4_PWM4",
+    "GPIO14",      "GPIO15",       "TDO_GPIO20",   "TCK_GPIO19",
+    "TMS_GPIO22",  "TDI_GPIO21",   "GPIO1",        "GPIO2",
+    "GPIO3",       "GPIO4",        "GPIO13",       "GPIO16",
+    "GPIO17",      "GPIO18"};
+
+void UCD90160Device::storeGPIOValues(
+    Services& services, const std::vector<int>& values,
+    std::map<std::string, std::string>& additionalData)
+{
+    // Verify the expected number of GPIO values were passed in
+    if (values.size() != gpioNames.size())
+    {
+        // Unexpected number of values; store as a plain list of integers
+        StandardDevice::storeGPIOValues(services, values, additionalData);
+        return;
+    }
+
+    // Store GPIO names and values in additional data and journal.
+    // Use groups of GPIOs in journal to minimize number of entries.
+    services.logInfoMsg(std::format("Device {} GPIO values:", name));
+    unsigned int groupSize{4};
+    auto namesSpan = std::span{gpioNames};
+    auto valuesSpan = std::span{values};
+    std::string namesStr, valuesStr;
+    for (unsigned int i = 0; i < gpioNames.size(); ++i)
+    {
+        additionalData.emplace(gpioNames[i], std::format("{}", values[i]));
+        if ((i % groupSize) == 0)
+        {
+            unsigned int gpiosLeft = gpioNames.size() - i;
+            unsigned int count = std::min(groupSize, gpiosLeft);
+            namesStr = format_utils::toString(namesSpan.subspan(i, count));
+            valuesStr = format_utils::toString(valuesSpan.subspan(i, count));
+            services.logInfoMsg(std::format("{}: {}", namesStr, valuesStr));
+        }
+    }
+}
+
+} // namespace phosphor::power::sequencer
diff --git a/phosphor-power-sequencer/src/ucd90160_device.hpp b/phosphor-power-sequencer/src/ucd90160_device.hpp
new file mode 100644
index 0000000..4b62766
--- /dev/null
+++ b/phosphor-power-sequencer/src/ucd90160_device.hpp
@@ -0,0 +1,70 @@
+/**
+ * Copyright © 2024 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "rail.hpp"
+#include "services.hpp"
+#include "ucd90x_device.hpp"
+
+#include <cstdint>
+#include <map>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+namespace phosphor::power::sequencer
+{
+
+/**
+ * @class UCD90160Device
+ *
+ * Class representing the UCD90160 power sequencer device.
+ */
+class UCD90160Device : public UCD90xDevice
+{
+  public:
+    // Specify which compiler-generated methods we want
+    UCD90160Device() = delete;
+    UCD90160Device(const UCD90160Device&) = delete;
+    UCD90160Device(UCD90160Device&&) = delete;
+    UCD90160Device& operator=(const UCD90160Device&) = delete;
+    UCD90160Device& operator=(UCD90160Device&&) = delete;
+    virtual ~UCD90160Device() = default;
+
+    /**
+     * Constructor.
+     *
+     * @param rails Voltage rails that are enabled and monitored by this device
+     * @param services System services like hardware presence and the journal
+     * @param bus I2C bus for the device
+     * @param address I2C address for the device
+     */
+    explicit UCD90160Device(std::vector<std::unique_ptr<Rail>> rails,
+                            Services& services, uint8_t bus, uint16_t address) :
+        UCD90xDevice(deviceName, std::move(rails), services, bus, address)
+    {}
+
+    constexpr static std::string deviceName{"UCD90160"};
+
+  protected:
+    /** @copydoc UCD90xDevice::storeGPIOValues() */
+    virtual void storeGPIOValues(
+        Services& services, const std::vector<int>& values,
+        std::map<std::string, std::string>& additionalData) override;
+};
+
+} // namespace phosphor::power::sequencer
diff --git a/phosphor-power-sequencer/test/meson.build b/phosphor-power-sequencer/test/meson.build
index 21a4bb2..39bafc1 100644
--- a/phosphor-power-sequencer/test/meson.build
+++ b/phosphor-power-sequencer/test/meson.build
@@ -6,6 +6,7 @@
                 'pmbus_driver_device_tests.cpp',
                 'rail_tests.cpp',
                 'standard_device_tests.cpp',
+                'ucd90160_device_tests.cpp',
                 'ucd90320_device_tests.cpp',
                 'ucd90x_device_tests.cpp',
                 dependencies: [
diff --git a/phosphor-power-sequencer/test/ucd90160_device_tests.cpp b/phosphor-power-sequencer/test/ucd90160_device_tests.cpp
new file mode 100644
index 0000000..7718c3b
--- /dev/null
+++ b/phosphor-power-sequencer/test/ucd90160_device_tests.cpp
@@ -0,0 +1,282 @@
+/**
+ * Copyright © 2024 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mock_pmbus.hpp"
+#include "mock_services.hpp"
+#include "pmbus.hpp"
+#include "rail.hpp"
+#include "services.hpp"
+#include "ucd90160_device.hpp"
+
+#include <cstdint>
+#include <map>
+#include <memory>
+#include <optional>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::sequencer;
+using namespace phosphor::pmbus;
+
+using ::testing::Return;
+
+/**
+ * Creates a Rail object that checks for a pgood fault using a GPIO.
+ *
+ * @param name Unique name for the rail
+ * @param gpio GPIO line to read to determine the pgood status of the rail
+ * @return Rail object
+ */
+static std::unique_ptr<Rail> createRail(const std::string& name,
+                                        unsigned int gpioLine)
+{
+    std::optional<std::string> presence{};
+    std::optional<uint8_t> page{};
+    bool isPowerSupplyRail{false};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimit{false};
+    bool activeLow{false};
+    std::optional<GPIO> gpio{GPIO{gpioLine, activeLow}};
+    return std::make_unique<Rail>(name, presence, page, isPowerSupplyRail,
+                                  checkStatusVout, compareVoltageToLimit, gpio);
+}
+
+TEST(UCD90160DeviceTests, Constructor)
+{
+    MockServices services;
+
+    std::vector<std::unique_ptr<Rail>> rails;
+    rails.emplace_back(createRail("VDD", 5));
+    rails.emplace_back(createRail("VIO", 7));
+    uint8_t bus{3};
+    uint16_t address{0x72};
+    UCD90160Device device{std::move(rails), services, bus, address};
+
+    EXPECT_EQ(device.getName(), "UCD90160");
+    EXPECT_EQ(device.getRails().size(), 2);
+    EXPECT_EQ(device.getRails()[0]->getName(), "VDD");
+    EXPECT_EQ(device.getRails()[1]->getName(), "VIO");
+    EXPECT_EQ(device.getBus(), bus);
+    EXPECT_EQ(device.getAddress(), address);
+    EXPECT_EQ(device.getDriverName(), "ucd9000");
+    EXPECT_EQ(device.getInstance(), 0);
+    EXPECT_NE(&(device.getPMBusInterface()), nullptr);
+}
+
+TEST(UCD90160DeviceTests, StoreGPIOValues)
+{
+    // This is a protected method and cannot be called directly from a gtest.
+    // Call findPgoodFault() which calls storeGPIOValues().
+
+    // Test where works
+    {
+        std::vector<int> gpioValues{
+            1, 0, 0, 1, // group 1 in journal
+            1, 1, 0, 0, // group 2 in journal
+            1, 0, 1, 1, // group 3 in journal
+            0, 0, 1, 1, // group 4 in journal
+            1, 0, 0, 0, // group 5 in journal
+            1, 0, 0, 1, // group 6 in journal
+            1, 1        // group 7 in journal
+        };
+
+        MockServices services;
+        EXPECT_CALL(services, getGPIOValues("ucd90160"))
+            .Times(1)
+            .WillOnce(Return(gpioValues));
+        EXPECT_CALL(services, logInfoMsg("Device UCD90160 GPIO values:"))
+            .Times(1);
+        EXPECT_CALL(
+            services,
+            logInfoMsg("[FPWM1_GPIO5, FPWM2_GPIO6, FPWM3_GPIO7, FPWM4_GPIO8]: "
+                       "[1, 0, 0, 1]"))
+            .Times(1);
+        EXPECT_CALL(
+            services,
+            logInfoMsg(
+                "[FPWM5_GPIO9, FPWM6_GPIO10, FPWM7_GPIO11, FPWM8_GPIO12]: "
+                "[1, 1, 0, 0]"))
+            .Times(1);
+        EXPECT_CALL(services,
+                    logInfoMsg("[GPI1_PWM1, GPI2_PWM2, GPI3_PWM3, GPI4_PWM4]: "
+                               "[1, 0, 1, 1]"))
+            .Times(1);
+        EXPECT_CALL(services,
+                    logInfoMsg("[GPIO14, GPIO15, TDO_GPIO20, TCK_GPIO19]: "
+                               "[0, 0, 1, 1]"))
+            .Times(1);
+        EXPECT_CALL(services,
+                    logInfoMsg("[TMS_GPIO22, TDI_GPIO21, GPIO1, GPIO2]: "
+                               "[1, 0, 0, 0]"))
+            .Times(1);
+        EXPECT_CALL(services, logInfoMsg("[GPIO3, GPIO4, GPIO13, GPIO16]: "
+                                         "[1, 0, 0, 1]"))
+            .Times(1);
+        EXPECT_CALL(services, logInfoMsg("[GPIO17, GPIO18]: "
+                                         "[1, 1]"))
+            .Times(1);
+        EXPECT_CALL(services,
+                    logInfoMsg("Device UCD90160 MFR_STATUS: 0x123456789abc"))
+            .Times(1);
+        EXPECT_CALL(
+            services,
+            logErrorMsg(
+                "Pgood fault found in rail monitored by device UCD90160"))
+            .Times(1);
+        EXPECT_CALL(services, logErrorMsg("Pgood fault detected in rail VDD"))
+            .Times(1);
+        EXPECT_CALL(
+            services,
+            logErrorMsg(
+                "Rail VDD pgood GPIO line offset 2 has inactive value 0"))
+            .Times(1);
+
+        std::vector<std::unique_ptr<Rail>> rails;
+        rails.emplace_back(createRail("VDD", 2));
+        uint8_t bus{3};
+        uint16_t address{0x72};
+        UCD90160Device device{std::move(rails), services, bus, address};
+
+        MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
+        EXPECT_CALL(pmbus, getPath(Type::Hwmon))
+            .Times(1)
+            .WillOnce(Return("/tmp"));
+        EXPECT_CALL(pmbus, read("mfr_status", Type::HwmonDeviceDebug, true))
+            .Times(1)
+            .WillOnce(Return(0x123456789abcull));
+
+        // Call findPgoodFault() which calls storeGPIOValues()
+        std::string powerSupplyError{};
+        std::map<std::string, std::string> additionalData{};
+        std::string error = device.findPgoodFault(services, powerSupplyError,
+                                                  additionalData);
+        EXPECT_EQ(error,
+                  "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault");
+        EXPECT_EQ(additionalData.size(), 31);
+        EXPECT_EQ(additionalData["MFR_STATUS"], "0x123456789abc");
+        EXPECT_EQ(additionalData["DEVICE_NAME"], "UCD90160");
+        EXPECT_EQ(additionalData["FPWM1_GPIO5"], "1");
+        EXPECT_EQ(additionalData["FPWM2_GPIO6"], "0");
+        EXPECT_EQ(additionalData["FPWM3_GPIO7"], "0");
+        EXPECT_EQ(additionalData["FPWM4_GPIO8"], "1");
+        EXPECT_EQ(additionalData["FPWM5_GPIO9"], "1");
+        EXPECT_EQ(additionalData["FPWM6_GPIO10"], "1");
+        EXPECT_EQ(additionalData["FPWM7_GPIO11"], "0");
+        EXPECT_EQ(additionalData["FPWM8_GPIO12"], "0");
+        EXPECT_EQ(additionalData["GPI1_PWM1"], "1");
+        EXPECT_EQ(additionalData["GPI2_PWM2"], "0");
+        EXPECT_EQ(additionalData["GPI3_PWM3"], "1");
+        EXPECT_EQ(additionalData["GPI4_PWM4"], "1");
+        EXPECT_EQ(additionalData["GPIO14"], "0");
+        EXPECT_EQ(additionalData["GPIO15"], "0");
+        EXPECT_EQ(additionalData["TDO_GPIO20"], "1");
+        EXPECT_EQ(additionalData["TCK_GPIO19"], "1");
+        EXPECT_EQ(additionalData["TMS_GPIO22"], "1");
+        EXPECT_EQ(additionalData["TDI_GPIO21"], "0");
+        EXPECT_EQ(additionalData["GPIO1"], "0");
+        EXPECT_EQ(additionalData["GPIO2"], "0");
+        EXPECT_EQ(additionalData["GPIO3"], "1");
+        EXPECT_EQ(additionalData["GPIO4"], "0");
+        EXPECT_EQ(additionalData["GPIO13"], "0");
+        EXPECT_EQ(additionalData["GPIO16"], "1");
+        EXPECT_EQ(additionalData["GPIO17"], "1");
+        EXPECT_EQ(additionalData["GPIO18"], "1");
+        EXPECT_EQ(additionalData["RAIL_NAME"], "VDD");
+        EXPECT_EQ(additionalData["GPIO_LINE"], "2");
+        EXPECT_EQ(additionalData["GPIO_VALUE"], "0");
+    }
+
+    // Test where there are the wrong number of GPIOs (27 instead of 26)
+    {
+        std::vector<int> gpioValues{
+            1, 0, 0, 1, // group 1 in journal
+            1, 1, 0, 0, // group 2 in journal
+            1, 0, 1, 1, // group 3 in journal
+            0, 0, 1, 1, // group 4 in journal
+            1, 0, 0, 0, // group 5 in journal
+            1, 0, 0, 1, // group 6 in journal
+            1, 1, 0     // group 7 in journal + extra value
+        };
+
+        MockServices services;
+        EXPECT_CALL(services, getGPIOValues("ucd90160"))
+            .Times(1)
+            .WillOnce(Return(gpioValues));
+        EXPECT_CALL(services, logInfoMsg("Device UCD90160 GPIO values: ["
+                                         "1, 0, 0, 1, "
+                                         "1, 1, 0, 0, "
+                                         "1, 0, 1, 1, "
+                                         "0, 0, 1, 1, "
+                                         "1, 0, 0, 0, "
+                                         "1, 0, 0, 1, "
+                                         "1, 1, 0]"))
+            .Times(1);
+        EXPECT_CALL(services,
+                    logInfoMsg("Device UCD90160 MFR_STATUS: 0x123456789abc"))
+            .Times(1);
+        EXPECT_CALL(
+            services,
+            logErrorMsg(
+                "Pgood fault found in rail monitored by device UCD90160"))
+            .Times(1);
+        EXPECT_CALL(services, logErrorMsg("Pgood fault detected in rail VDD"))
+            .Times(1);
+        EXPECT_CALL(
+            services,
+            logErrorMsg(
+                "Rail VDD pgood GPIO line offset 2 has inactive value 0"))
+            .Times(1);
+
+        std::vector<std::unique_ptr<Rail>> rails;
+        rails.emplace_back(createRail("VDD", 2));
+        uint8_t bus{3};
+        uint16_t address{0x72};
+        UCD90160Device device{std::move(rails), services, bus, address};
+
+        MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
+        EXPECT_CALL(pmbus, getPath(Type::Hwmon))
+            .Times(1)
+            .WillOnce(Return("/tmp"));
+        EXPECT_CALL(pmbus, read("mfr_status", Type::HwmonDeviceDebug, true))
+            .Times(1)
+            .WillOnce(Return(0x123456789abcull));
+
+        // Call findPgoodFault() which calls storeGPIOValues()
+        std::string powerSupplyError{};
+        std::map<std::string, std::string> additionalData{};
+        std::string error = device.findPgoodFault(services, powerSupplyError,
+                                                  additionalData);
+        EXPECT_EQ(error,
+                  "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault");
+        EXPECT_EQ(additionalData.size(), 6);
+        EXPECT_EQ(additionalData["MFR_STATUS"], "0x123456789abc");
+        EXPECT_EQ(additionalData["DEVICE_NAME"], "UCD90160");
+        EXPECT_EQ(additionalData["GPIO_VALUES"], "[1, 0, 0, 1, "
+                                                 "1, 1, 0, 0, "
+                                                 "1, 0, 1, 1, "
+                                                 "0, 0, 1, 1, "
+                                                 "1, 0, 0, 0, "
+                                                 "1, 0, 0, 1, "
+                                                 "1, 1, 0]");
+        EXPECT_EQ(additionalData["RAIL_NAME"], "VDD");
+        EXPECT_EQ(additionalData["GPIO_LINE"], "2");
+        EXPECT_EQ(additionalData["GPIO_VALUE"], "0");
+    }
+}