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