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/Makefile.am b/gpio-util/Makefile.am
index ea8c763..9bdc42d 100644
--- a/gpio-util/Makefile.am
+++ b/gpio-util/Makefile.am
@@ -5,4 +5,11 @@
phosphor-gpio-util
phosphor_gpio_util_SOURCES = \
+ gpio.cpp \
main.cpp
+
+phosphor_gpio_util_LDADD = \
+ $(PHOSPHOR_LOGGING_LIBS)
+
+phosphor_gpio_util_CXXFLAGS = \
+ $(PHOSPHOR_LOGGING_CFLAGS)
diff --git a/gpio-util/gpio.cpp b/gpio-util/gpio.cpp
new file mode 100644
index 0000000..d5c2ce4
--- /dev/null
+++ b/gpio-util/gpio.cpp
@@ -0,0 +1,98 @@
+/**
+ * Copyright © 2017 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 <fcntl.h>
+#include <phosphor-logging/log.hpp>
+#include <sys/ioctl.h>
+#include "gpio.hpp"
+
+namespace phosphor
+{
+namespace gpio
+{
+
+using namespace phosphor::logging;
+
+void GPIO::set(Value value)
+{
+ assert(direction == Direction::output);
+
+ requestLine(value);
+
+ gpiohandle_data data{};
+ data.values[0] = static_cast<gpioValue_t>(value);
+
+ auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
+ if (rc == -1)
+ {
+ auto e = errno;
+ log<level::ERR>("Failed SET_LINE_VALUES ioctl",
+ entry("ERRNO=%d", e));
+ throw std::runtime_error("Failed SET_LINE_VALUES ioctl");
+ }
+}
+
+
+void GPIO::requestLine(Value defaultValue)
+{
+ //Only need to do this once
+ if (lineFD)
+ {
+ return;
+ }
+
+ FileDescriptor fd{open(device.c_str(), 0)};
+ if (fd() == -1)
+ {
+ auto e = errno;
+ log<level::ERR>("Failed opening GPIO device",
+ entry("DEVICE=%s", device),
+ entry("ERRNO=%d", e));
+ throw std::runtime_error("Failed opening GPIO device");
+ }
+
+ //Make an ioctl call to request the GPIO line, which will
+ //return the descriptor to use to access it.
+ gpiohandle_request request{};
+ strncpy(request.consumer_label,
+ "phosphor-gpio-util",
+ sizeof(request.consumer_label));
+
+ request.flags = (direction == Direction::output) ?
+ GPIOHANDLE_REQUEST_OUTPUT : GPIOHANDLE_REQUEST_INPUT;
+
+ request.lineoffsets[0] = gpio;
+ request.lines = 1;
+
+ if (direction == Direction::output)
+ {
+ request.default_values[0] = static_cast<gpioValue_t>(defaultValue);
+ }
+
+ auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
+ if (rc == -1)
+ {
+ auto e = errno;
+ log<level::ERR>("Failed GET_LINEHANDLE ioctl",
+ entry("GPIO=%d", gpio),
+ entry("ERRNO=%d", e));
+ throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
+ }
+
+ lineFD.set(request.fd);
+}
+
+}
+}
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;
+};
+
+}
+}