blob: 0969995a0092f00656a801368904b0256b493887 [file] [log] [blame]
Matt Spinleree7adb72017-08-21 15:17:19 -05001#pragma once
2
Shawn McCarney9462e062020-09-15 14:39:17 -05003#include "file_descriptor.hpp"
Matt Spinleree7adb72017-08-21 15:17:19 -05004
Matt Spinlerf0f02b92018-10-25 16:12:43 -05005#include <linux/gpio.h>
6
7#include <string>
8#include <type_traits>
9
Lei YUab093322019-10-09 16:43:22 +080010namespace phosphor
Matt Spinleree7adb72017-08-21 15:17:19 -050011{
12namespace gpio
13{
14
15typedef std::remove_reference<decltype(
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016 gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
Matt Spinleree7adb72017-08-21 15:17:19 -050017
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type
19 gpioValue_t;
Matt Spinleree7adb72017-08-21 15:17:19 -050020
21/**
22 * If the GPIO is an input or output
23 */
24enum class Direction
25{
26 input,
27 output
28};
29
30/**
31 * The possible values - low or high
32 */
33enum class Value
34{
35 low,
36 high
37};
38
39/**
40 * Represents a GPIO.
41 *
42 * Currently supports reading a GPIO.
43 *
44 * Write support may be added in the future.
45 */
46class GPIO
47{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050048 public:
49 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 Spinleree7adb72017-08-21 15:17:19 -050055
Matt Spinlerf0f02b92018-10-25 16:12:43 -050056 /**
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 Spinleree7adb72017-08-21 15:17:19 -050067
Matt Spinlerf0f02b92018-10-25 16:12:43 -050068 /**
69 * Reads the GPIO value
70 *
71 * Requests the GPIO line if it hasn't been done already.
72 *
73 * @return Value - the GPIO value
74 */
75 Value read();
Matt Spinleree7adb72017-08-21 15:17:19 -050076
Matt Spinlerf0f02b92018-10-25 16:12:43 -050077 /**
78 * Sets the GPIO value to low or high
79 *
80 * Requests the GPIO line if it hasn't been done already.
81 *
82 * @param[in] Value - the value to set
83 */
84 void set(Value value);
Matt Spinleree7adb72017-08-21 15:17:19 -050085
Matt Spinlerf0f02b92018-10-25 16:12:43 -050086 private:
87 /**
88 * Requests a GPIO line from the GPIO device
89 *
90 * @param[in] defaultValue - The default value, required for
91 * output GPIOs only.
92 */
93 void requestLine(Value defaultValue = Value::high);
Matt Spinler35aa1432018-01-18 11:13:20 -060094
Matt Spinlerf0f02b92018-10-25 16:12:43 -050095 /**
96 * The GPIO device name, like /dev/gpiochip0
97 */
98 const std::string device;
Matt Spinleree7adb72017-08-21 15:17:19 -050099
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500100 /**
101 * The GPIO number
102 */
103 const gpioNum_t gpio;
Matt Spinleree7adb72017-08-21 15:17:19 -0500104
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500105 /**
106 * The GPIO direction
107 */
108 const Direction direction;
Matt Spinleree7adb72017-08-21 15:17:19 -0500109
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500110 /**
111 * File descriptor for the GPIO line
112 */
113 power::util::FileDescriptor lineFD;
Matt Spinleree7adb72017-08-21 15:17:19 -0500114};
115
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500116} // namespace gpio
Lei YUab093322019-10-09 16:43:22 +0800117} // namespace phosphor