Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 3 | #include "file.hpp" |
| 4 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 5 | #include <linux/gpio.h> |
| 6 | |
| 7 | #include <string> |
| 8 | #include <type_traits> |
| 9 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 10 | namespace phosphor |
| 11 | { |
| 12 | namespace gpio |
| 13 | { |
| 14 | |
Patrick Williams | f28e7be | 2021-10-06 13:13:43 -0500 | [diff] [blame] | 15 | typedef std::remove_reference< |
| 16 | decltype(gpiohandle_request::lineoffsets[0])>::type gpioNum_t; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 17 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 18 | typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type |
| 19 | gpioValue_t; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Represents a GPIO. |
| 23 | * |
| 24 | * Operations are setting low or high. |
| 25 | * |
| 26 | * Read support may be added in the future. |
| 27 | */ |
| 28 | class GPIO |
| 29 | { |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 30 | public: |
| 31 | /** |
| 32 | * If the GPIO is an input or output |
| 33 | */ |
| 34 | enum class Direction |
| 35 | { |
| 36 | input, |
| 37 | output |
| 38 | }; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 39 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 40 | /** |
| 41 | * The possible values - low or high |
| 42 | */ |
| 43 | enum class Value |
| 44 | { |
| 45 | low, |
| 46 | high |
| 47 | }; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 48 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 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 Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 55 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 56 | /** |
| 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) |
Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 65 | {} |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 66 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Sets the GPIO value |
| 69 | * |
| 70 | * Requests the GPIO line if it hasn't been done already. |
| 71 | */ |
| 72 | void set(Value value); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 73 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 74 | private: |
| 75 | /** |
| 76 | * Requests a GPIO line from the GPIO device |
| 77 | * |
| 78 | * @param[in] defaultValue - The default value, required for |
| 79 | * output GPIOs only. |
| 80 | */ |
| 81 | void requestLine(Value defaultValue = Value::high); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 82 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 83 | /** |
| 84 | * The GPIO device name, like /dev/gpiochip0 |
| 85 | */ |
| 86 | const std::string device; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 87 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 88 | /** |
| 89 | * The GPIO number |
| 90 | */ |
| 91 | const gpioNum_t gpio; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 92 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 93 | /** |
| 94 | * The GPIO direction |
| 95 | */ |
| 96 | const Direction direction; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 97 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 98 | /** |
| 99 | * File descriptor for the GPIO line |
| 100 | */ |
| 101 | FileDescriptor lineFD; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 104 | } // namespace gpio |
| 105 | } // namespace phosphor |