summaryrefslogtreecommitdiffstats
path: root/gpio-util/gpio.hpp
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2017-05-25 10:23:05 -0500
committerMatt Spinler <spinler@us.ibm.com>2017-05-30 15:25:31 -0500
commit36df1979f5c11d4e4dbe144b63f42390b6088f76 (patch)
tree42e97a07668e3f3ed7a4cc460e4888dd1e3d15bf /gpio-util/gpio.hpp
parent936a504319e01642fc5803c00ff4aa5baeafacef (diff)
downloadphosphor-gpio-monitor-36df1979f5c11d4e4dbe144b63f42390b6088f76.tar.gz
phosphor-gpio-monitor-36df1979f5c11d4e4dbe144b63f42390b6088f76.zip
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>
Diffstat (limited to 'gpio-util/gpio.hpp')
-rw-r--r--gpio-util/gpio.hpp108
1 files changed, 108 insertions, 0 deletions
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;
+};
+
+}
+}
OpenPOWER on IntegriCloud