blob: a803113239da71676d6f76f06b9ecb51de0e99d8 [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
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000015typedef std::remove_reference<
16 decltype(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)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000065 {}
Matt Spinleree7adb72017-08-21 15:17:19 -050066
Matt Spinlerf0f02b92018-10-25 16:12:43 -050067 /**
68 * Reads the GPIO value
69 *
70 * Requests the GPIO line if it hasn't been done already.
71 *
72 * @return Value - the GPIO value
73 */
74 Value read();
Matt Spinleree7adb72017-08-21 15:17:19 -050075
Matt Spinlerf0f02b92018-10-25 16:12:43 -050076 /**
77 * Sets the GPIO value to low or high
78 *
79 * Requests the GPIO line if it hasn't been done already.
80 *
81 * @param[in] Value - the value to set
82 */
83 void set(Value value);
Matt Spinleree7adb72017-08-21 15:17:19 -050084
Matt Spinlerf0f02b92018-10-25 16:12:43 -050085 private:
86 /**
87 * Requests a GPIO line from the GPIO device
88 *
89 * @param[in] defaultValue - The default value, required for
90 * output GPIOs only.
91 */
92 void requestLine(Value defaultValue = Value::high);
Matt Spinler35aa1432018-01-18 11:13:20 -060093
Matt Spinlerf0f02b92018-10-25 16:12:43 -050094 /**
95 * The GPIO device name, like /dev/gpiochip0
96 */
97 const std::string device;
Matt Spinleree7adb72017-08-21 15:17:19 -050098
Matt Spinlerf0f02b92018-10-25 16:12:43 -050099 /**
100 * The GPIO number
101 */
102 const gpioNum_t gpio;
Matt Spinleree7adb72017-08-21 15:17:19 -0500103
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500104 /**
105 * The GPIO direction
106 */
107 const Direction direction;
Matt Spinleree7adb72017-08-21 15:17:19 -0500108
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500109 /**
110 * File descriptor for the GPIO line
111 */
112 power::util::FileDescriptor lineFD;
Matt Spinleree7adb72017-08-21 15:17:19 -0500113};
114
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500115} // namespace gpio
Lei YUab093322019-10-09 16:43:22 +0800116} // namespace phosphor