blob: d4ac47b56fd5198a76cc33097853192c3e1b3078 [file] [log] [blame]
Matt Spinler36df1972017-05-25 10:23:05 -05001#pragma once
2
Matt Spinler36df1972017-05-25 10:23:05 -05003#include "file.hpp"
4
Patrick Venturedace6802018-11-01 16:52:10 -07005#include <linux/gpio.h>
6
7#include <string>
8#include <type_traits>
9
Matt Spinler36df1972017-05-25 10:23:05 -050010namespace phosphor
11{
12namespace gpio
13{
14
Patrick Venturedace6802018-11-01 16:52:10 -070015typedef std::remove_reference<decltype(
16 gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
Matt Spinler36df1972017-05-25 10:23:05 -050017
Patrick Venturedace6802018-11-01 16:52:10 -070018typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type
19 gpioValue_t;
Matt Spinler36df1972017-05-25 10:23:05 -050020
21/**
22 * Represents a GPIO.
23 *
24 * Operations are setting low or high.
25 *
26 * Read support may be added in the future.
27 */
28class GPIO
29{
Patrick Venturedace6802018-11-01 16:52:10 -070030 public:
31 /**
32 * If the GPIO is an input or output
33 */
34 enum class Direction
35 {
36 input,
37 output
38 };
Matt Spinler36df1972017-05-25 10:23:05 -050039
Patrick Venturedace6802018-11-01 16:52:10 -070040 /**
41 * The possible values - low or high
42 */
43 enum class Value
44 {
45 low,
46 high
47 };
Matt Spinler36df1972017-05-25 10:23:05 -050048
Patrick Venturedace6802018-11-01 16:52:10 -070049 GPIO() = delete;
50 GPIO(const GPIO&) = delete;
51 GPIO(GPIO&&) = default;
52 GPIO& operator=(const GPIO&) = delete;
53 GPIO& operator=(GPIO&&) = default;
54 ~GPIO() = default;
Matt Spinler36df1972017-05-25 10:23:05 -050055
Patrick Venturedace6802018-11-01 16:52:10 -070056 /**
57 * Constructor
58 *
59 * @param[in] device - the GPIO device file
60 * @param[in] gpio - the GPIO number
61 * @param[in] direction - the GPIO direction
62 */
63 GPIO(const std::string& device, gpioNum_t gpio, Direction direction) :
64 device(device), gpio(gpio), direction(direction)
65 {
66 }
Matt Spinler36df1972017-05-25 10:23:05 -050067
Patrick Venturedace6802018-11-01 16:52:10 -070068 /**
69 * Sets the GPIO value
70 *
71 * Requests the GPIO line if it hasn't been done already.
72 */
73 void set(Value value);
Matt Spinler36df1972017-05-25 10:23:05 -050074
Patrick Venturedace6802018-11-01 16:52:10 -070075 private:
76 /**
77 * Requests a GPIO line from the GPIO device
78 *
79 * @param[in] defaultValue - The default value, required for
80 * output GPIOs only.
81 */
82 void requestLine(Value defaultValue = Value::high);
Matt Spinler36df1972017-05-25 10:23:05 -050083
Patrick Venturedace6802018-11-01 16:52:10 -070084 /**
85 * The GPIO device name, like /dev/gpiochip0
86 */
87 const std::string device;
Matt Spinler36df1972017-05-25 10:23:05 -050088
Patrick Venturedace6802018-11-01 16:52:10 -070089 /**
90 * The GPIO number
91 */
92 const gpioNum_t gpio;
Matt Spinler36df1972017-05-25 10:23:05 -050093
Patrick Venturedace6802018-11-01 16:52:10 -070094 /**
95 * The GPIO direction
96 */
97 const Direction direction;
Matt Spinler36df1972017-05-25 10:23:05 -050098
Patrick Venturedace6802018-11-01 16:52:10 -070099 /**
100 * File descriptor for the GPIO line
101 */
102 FileDescriptor lineFD;
Matt Spinler36df1972017-05-25 10:23:05 -0500103};
104
Patrick Venturedace6802018-11-01 16:52:10 -0700105} // namespace gpio
106} // namespace phosphor