Enable group gpio configuration
This change adds support to configure single
as well as group gpio config using a single api(configGroupGpio()).
This change is introduced to support the button/input
interfaces which has multiple gpios associated with them.
As an improvement reading of gpio def json file is
done once in main function rather than reading it
everytime before creating the button interface object.
Signed-off-by: Naveen Moses <naveen.mosess@hcl.com>
Change-Id: Ib73dda618c78fd2f14b5d3432fd04c9f4cd2dd9b
diff --git a/inc/gpio.hpp b/inc/gpio.hpp
index ca83082..ba4b8ad 100644
--- a/inc/gpio.hpp
+++ b/inc/gpio.hpp
@@ -16,13 +16,40 @@
#pragma once
#include <sdbusplus/bus.hpp>
+#include <string>
+#include <vector>
-int configGpio(const char* gpioName, int* fd, sdbusplus::bus::bus& bus);
-void closeGpio(int fd);
-bool gpioDefined(const std::string& gpioName);
-
-template <typename T>
-bool hasGpio()
+// this struct has the gpio config for single gpio
+struct gpioInfo
{
- return gpioDefined(T::getGpioName());
-}
+ int fd; // io fd mapped with the gpio
+ uint32_t number;
+ std::string direction;
+};
+
+// this struct represents button interface
+struct buttonConfig
+{
+ std::string formFactorName; // name of the button interface
+ std::vector<gpioInfo> gpios; // holds single or group gpio config
+ // corresponding to button interface
+};
+
+/**
+ * @brief iterates over the list of gpios and configures gpios them
+ * config which is set from gpio defs json file.
+ * The fd of the configured gpio is stored in buttonConfig.gpios container
+ * @return int returns 0 on successful config of all gpios
+ */
+
+int configGroupGpio(sdbusplus::bus::bus& bus, buttonConfig& buttonCfg);
+
+/**
+ * @brief configures and initializes the single gpio
+ * @return int returns 0 on successful config of all gpios
+ */
+
+int configGpio(sdbusplus::bus::bus& bus, gpioInfo& gpioConfig);
+
+uint32_t getGpioNum(const std::string& gpioPin);
+void closeGpio(int fd);
diff --git a/inc/id_button.hpp b/inc/id_button.hpp
index 8116185..cb32125 100644
--- a/inc/id_button.hpp
+++ b/inc/id_button.hpp
@@ -32,17 +32,20 @@
{
IDButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
+ buttonConfig& buttonCfg,
sd_event_io_handler_t handler = IDButton::EventHandler) :
sdbusplus::server::object::object<
sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>(
bus, path),
- fd(-1), bus(bus), event(event), callbackHandler(handler)
+ fd(-1), buttonIFConfig(buttonCfg), bus(bus), event(event),
+ callbackHandler(handler)
{
int ret = -1;
- // config gpio
- ret = ::configGpio(ID_BUTTON, &fd, bus);
+ // config group gpio based on the gpio defs read from the json file
+ ret = configGroupGpio(bus, buttonIFConfig);
+
if (ret < 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -50,6 +53,9 @@
throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
IOError();
}
+ // initialize the button io fd from the buttonConfig
+ // which has fd stored when configGroupGpio is called
+ fd = buttonIFConfig.gpios[0].fd;
char buf;
::read(fd, &buf, sizeof(buf));
@@ -73,7 +79,7 @@
void simPress() override;
- static const char* getGpioName()
+ static const std::string getGpioName()
{
return ID_BUTTON;
}
@@ -142,6 +148,7 @@
private:
int fd;
+ buttonConfig buttonIFConfig;
sdbusplus::bus::bus& bus;
EventPtr& event;
sd_event_io_handler_t callbackHandler;
diff --git a/inc/power_button.hpp b/inc/power_button.hpp
index e71d9f1..283b149 100644
--- a/inc/power_button.hpp
+++ b/inc/power_button.hpp
@@ -33,17 +33,20 @@
{
PowerButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
+ buttonConfig& buttonCfg,
sd_event_io_handler_t handler = PowerButton::EventHandler) :
sdbusplus::server::object::object<
sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
bus, path),
- fd(-1), bus(bus), event(event), callbackHandler(handler)
+ fd(-1), buttonIFConfig(buttonCfg), bus(bus), event(event),
+ callbackHandler(handler)
{
int ret = -1;
- // config gpio
- ret = ::configGpio(POWER_BUTTON, &fd, bus);
+ // config group gpio based on the gpio defs read from the json file
+ ret = configGroupGpio(bus, buttonIFConfig);
+
if (ret < 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -52,6 +55,10 @@
IOError();
}
+ // initialize the button io fd from the buttonConfig
+ // which has fd stored when configGroupGpio is called
+ fd = buttonIFConfig.gpios[0].fd;
+
char buf;
::read(fd, &buf, sizeof(buf));
@@ -75,7 +82,7 @@
void simPress() override;
void simLongPress() override;
- static const char* getGpioName()
+ static const std::string getGpioName()
{
return POWER_BUTTON;
}
@@ -168,6 +175,7 @@
private:
int fd;
+ buttonConfig buttonIFConfig; // button iface io details
sdbusplus::bus::bus& bus;
EventPtr& event;
sd_event_io_handler_t callbackHandler;
diff --git a/inc/reset_button.hpp b/inc/reset_button.hpp
index 5066bb1..478628b 100644
--- a/inc/reset_button.hpp
+++ b/inc/reset_button.hpp
@@ -32,17 +32,20 @@
{
ResetButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
+ buttonConfig& buttonCfg,
sd_event_io_handler_t handler = ResetButton::EventHandler) :
sdbusplus::server::object::object<
sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Reset>(
bus, path),
- fd(-1), bus(bus), event(event), callbackHandler(handler)
+ fd(-1), buttonIFConfig(buttonCfg), bus(bus), event(event),
+ callbackHandler(handler)
{
int ret = -1;
- // config gpio
- ret = ::configGpio(RESET_BUTTON, &fd, bus);
+ // config group gpio based on the gpio defs read from the json file
+ ret = configGroupGpio(bus, buttonIFConfig);
+
if (ret < 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -51,6 +54,10 @@
IOError();
}
+ // initialize the button io fd from the buttonConfig
+ // which has fd stored when configGroupGpio is called
+ fd = buttonIFConfig.gpios[0].fd;
+
char buf;
::read(fd, &buf, sizeof(buf));
@@ -73,7 +80,7 @@
void simPress() override;
- static const char* getGpioName()
+ static const std::string getGpioName()
{
return RESET_BUTTON;
}
@@ -142,6 +149,7 @@
private:
int fd;
+ buttonConfig buttonIFConfig; // button iface io details
sdbusplus::bus::bus& bus;
EventPtr& event;
sd_event_io_handler_t callbackHandler;