blob: ba8cce3f638b9d02d475888e16534de4c64bd026 [file] [log] [blame]
Matt Spinler36df1972017-05-25 10:23:05 -05001#pragma once
2
3#include <linux/gpio.h>
4#include "file.hpp"
5
6namespace phosphor
7{
8namespace 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 * Represents a GPIO.
19 *
20 * Operations are setting low or high.
21 *
22 * Read support may be added in the future.
23 */
24class GPIO
25{
26 public:
27
28 /**
29 * If the GPIO is an input or output
30 */
31 enum class Direction
32 {
33 input,
34 output
35 };
36
37 /**
38 * The possible values - low or high
39 */
40 enum class Value
41 {
42 low,
43 high
44 };
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 * Sets the GPIO value
71 *
72 * Requests the GPIO line if it hasn't been done already.
73 */
74 void set(Value value);
75
76 private:
77
78 /**
79 * Requests a GPIO line from the GPIO device
80 *
81 * @param[in] defaultValue - The default value, required for
82 * output GPIOs only.
83 */
84 void requestLine(Value defaultValue = Value::high);
85
86 /**
87 * The GPIO device name, like /dev/gpiochip0
88 */
89 const std::string device;
90
91 /**
92 * The GPIO number
93 */
94 const gpioNum_t gpio;
95
96 /**
97 * The GPIO direction
98 */
99 const Direction direction;
100
101 /**
102 * File descriptor for the GPIO line
103 */
104 FileDescriptor lineFD;
105};
106
107}
108}