Support host selector using cpld definitions

Description:
- Support host selector using cpld definitions

Design:
- Because the current structure only supports config defined by GPIO
  (Yosemite V2), but there are also have the system (Yosemite V3.5)
  gets the host-selector's selection from CPLD.

- So this commit is to extend the current configuration to use CPLD
  definitions. Also, support adding event io from the register file
  which was exported from the CLD driver.

- For example with config json below:
{
   "cpld_definitions": [
        {
            "name": "HOST_SELECTOR",
            "i2c_address": 15,
            "i2c_bus": 12,
            "register_name": "uart-selection-debug-card",
            "max_position": 4
        }
   ]
}

Dependency:
- CLD driver is required (link: https://lore.kernel.org/lkml/20230117094425.19004-1-Delphine_CC_Chiu@Wiwynn.com/)

Test Case:
- When ocp debug card uart button is pressed the position property on
  dbus is correct.

Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
Change-Id: I6746afa6d905bd3c681e2921c643b3cd4cb9944c
diff --git a/inc/button_interface.hpp b/inc/button_interface.hpp
index 9905304..b3a98cc 100644
--- a/inc/button_interface.hpp
+++ b/inc/button_interface.hpp
@@ -1,7 +1,7 @@
 #pragma once
 
+#include "button_config.hpp"
 #include "common.hpp"
-#include "gpio.hpp"
 #include "xyz/openbmc_project/Chassis/Common/error.hpp"
 
 #include <phosphor-logging/elog-errors.hpp>
@@ -10,20 +10,31 @@
 class ButtonIface
 {
   public:
-    ButtonIface(sdbusplus::bus_t& bus, EventPtr& event, buttonConfig& buttonCfg,
+    ButtonIface(sdbusplus::bus_t& bus, EventPtr& event, ButtonConfig& buttonCfg,
                 sd_event_io_handler_t handler = ButtonIface::EventHandler) :
         bus(bus),
         event(event), config(buttonCfg), callbackHandler(handler)
     {
         int ret = -1;
+        std::string configType;
 
-        // config group gpio based on the gpio defs read from the json file
-        ret = configGroupGpio(config);
+        // config group gpio or cpld based on the defs read from the json file
+        if (buttonCfg.type == ConfigType::gpio)
+        {
+            configType = "GPIO";
+            ret = configGroupGpio(config);
+        }
+        else if (buttonCfg.type == ConfigType::cpld)
+        {
+            configType = "CPLD";
+            ret = configCpld(config);
+        }
 
         if (ret < 0)
         {
             phosphor::logging::log<phosphor::logging::level::ERR>(
-                (getFormFactorType() + " : failed to config GPIO").c_str());
+                (getFormFactorType() + " : failed to config " + configType)
+                    .c_str());
             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
                 IOError();
         }
@@ -65,12 +76,11 @@
 
     virtual void init()
     {
-        // initialize the button io fd from the buttonConfig
-        // which has fd stored when configGroupGpio is called
-        for (auto gpioCfg : config.gpios)
+        // initialize the button io fd from the ButtonConfig
+        // which has fd stored when configGroupGpio or configCpld is called
+        for (auto fd : config.fds)
         {
             char buf;
-            int fd = gpioCfg.fd;
 
             int ret = ::read(fd, &buf, sizeof(buf));
             if (ret < 0)
@@ -86,7 +96,10 @@
                 phosphor::logging::log<phosphor::logging::level::ERR>(
                     (getFormFactorType() + " : failed to add to event loop")
                         .c_str());
-                ::closeGpio(fd);
+                if (fd > 0)
+                {
+                    ::close(fd);
+                }
                 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
                     IOError();
             }
@@ -100,14 +113,17 @@
      */
     virtual void deInit()
     {
-        for (auto gpioCfg : config.gpios)
+        for (auto fd : config.fds)
         {
-            ::closeGpio(gpioCfg.fd);
+            if (fd > 0)
+            {
+                ::close(fd);
+            }
         }
     }
 
     sdbusplus::bus_t& bus;
     EventPtr& event;
-    buttonConfig config;
+    ButtonConfig config;
     sd_event_io_handler_t callbackHandler;
 };