xref: /openbmc/phosphor-power/gpio.hpp (revision 35aa1433)
1 #pragma once
2 
3 #include <linux/gpio.h>
4 #include "file.hpp"
5 
6 namespace witherspoon
7 {
8 namespace gpio
9 {
10 
11 typedef std::remove_reference<decltype(
12      gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
13 
14 typedef std::remove_reference<decltype(
15      gpiohandle_data::values[0])>::type gpioValue_t;
16 
17 /**
18  * If the GPIO is an input or output
19  */
20 enum class Direction
21 {
22     input,
23     output
24 };
25 
26 /**
27  * The possible values - low or high
28  */
29 enum class Value
30 {
31     low,
32     high
33 };
34 
35 /**
36  * Represents a GPIO.
37  *
38  * Currently supports reading a GPIO.
39  *
40  * Write support may be added in the future.
41  */
42 class GPIO
43 {
44     public:
45 
46         GPIO() = delete;
47         GPIO(const GPIO&) = delete;
48         GPIO(GPIO&&) = default;
49         GPIO& operator=(const GPIO&) = delete;
50         GPIO& operator=(GPIO&&) = default;
51         ~GPIO() = default;
52 
53         /**
54          * Constructor
55          *
56          * @param[in] device - the GPIO device file
57          * @param[in] gpio - the GPIO number
58          * @param[in] direction - the GPIO direction
59          */
60         GPIO(const std::string& device,
61              gpioNum_t gpio,
62              Direction direction) :
63             device(device),
64             gpio(gpio),
65             direction(direction)
66         {
67         }
68 
69         /**
70          * Reads the GPIO value
71          *
72          * Requests the GPIO line if it hasn't been done already.
73          *
74          * @return Value - the GPIO value
75          */
76         Value read();
77 
78         /**
79          * Sets the GPIO value to low or high
80          *
81          * Requests the GPIO line if it hasn't been done already.
82          *
83          * @param[in] Value - the value to set
84          */
85         void set(Value value);
86 
87     private:
88 
89         /**
90          * Requests a GPIO line from the GPIO device
91          *
92          * @param[in] defaultValue - The default value, required for
93          *                           output GPIOs only.
94          */
95         void requestLine(Value defaultValue = Value::high);
96 
97         /**
98          * The GPIO device name, like /dev/gpiochip0
99          */
100         const std::string device;
101 
102         /**
103          * The GPIO number
104          */
105         const gpioNum_t gpio;
106 
107         /**
108          * The GPIO direction
109          */
110         const Direction direction;
111 
112         /**
113          * File descriptor for the GPIO line
114          */
115         power::util::FileDescriptor lineFD;
116 };
117 
118 }
119 }
120