Add GPIO class
This class is used for accessing a GPIO via the
/dev/gpiochipX interface.
It requests a GPIO line handle from the GPIO device to
do the actual operation on.
The GPIO number to use, for AST chips at least, is the
actual GPIO number, such as GPIOA0 = 0 and GPIOA7 = 7.
The class currently only supports writes.
Change-Id: I1c2ae38c23c5db502d5f14bcf9aa2e35094f1e9b
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/gpio-util/gpio.hpp b/gpio-util/gpio.hpp
new file mode 100644
index 0000000..ba8cce3
--- /dev/null
+++ b/gpio-util/gpio.hpp
@@ -0,0 +1,108 @@
+#pragma once
+
+#include <linux/gpio.h>
+#include "file.hpp"
+
+namespace phosphor
+{
+namespace gpio
+{
+
+ typedef std::remove_reference<decltype(
+ gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
+
+ typedef std::remove_reference<decltype(
+ gpiohandle_data::values[0])>::type gpioValue_t;
+
+/**
+ * Represents a GPIO.
+ *
+ * Operations are setting low or high.
+ *
+ * Read support may be added in the future.
+ */
+class GPIO
+{
+ public:
+
+ /**
+ * If the GPIO is an input or output
+ */
+ enum class Direction
+ {
+ input,
+ output
+ };
+
+ /**
+ * The possible values - low or high
+ */
+ enum class Value
+ {
+ low,
+ high
+ };
+
+ GPIO() = delete;
+ GPIO(const GPIO&) = delete;
+ GPIO(GPIO&&) = default;
+ GPIO& operator=(const GPIO&) = delete;
+ GPIO& operator=(GPIO&&) = default;
+ ~GPIO() = default;
+
+ /**
+ * Constructor
+ *
+ * @param[in] device - the GPIO device file
+ * @param[in] gpio - the GPIO number
+ * @param[in] direction - the GPIO direction
+ */
+ GPIO(const std::string& device,
+ gpioNum_t gpio,
+ Direction direction) :
+ device(device),
+ gpio(gpio),
+ direction(direction)
+ {
+ }
+
+ /**
+ * Sets the GPIO value
+ *
+ * Requests the GPIO line if it hasn't been done already.
+ */
+ void set(Value value);
+
+ private:
+
+ /**
+ * Requests a GPIO line from the GPIO device
+ *
+ * @param[in] defaultValue - The default value, required for
+ * output GPIOs only.
+ */
+ void requestLine(Value defaultValue = Value::high);
+
+ /**
+ * The GPIO device name, like /dev/gpiochip0
+ */
+ const std::string device;
+
+ /**
+ * The GPIO number
+ */
+ const gpioNum_t gpio;
+
+ /**
+ * The GPIO direction
+ */
+ const Direction direction;
+
+ /**
+ * File descriptor for the GPIO line
+ */
+ FileDescriptor lineFD;
+};
+
+}
+}