| Shawn McCarney | e6d7f4c | 2025-11-18 15:46:08 -0600 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright © 2025 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include <gpiod.hpp> |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | namespace phosphor::power::sequencer |
| 23 | { |
| 24 | |
| 25 | /** |
| 26 | * @class GPIO |
| 27 | * |
| 28 | * Abstract base class for a General-Purpose Input/Output pin. |
| 29 | */ |
| 30 | class GPIO |
| 31 | { |
| 32 | public: |
| 33 | GPIO() = default; |
| 34 | GPIO(const GPIO&) = delete; |
| 35 | GPIO(GPIO&&) = delete; |
| 36 | GPIO& operator=(const GPIO&) = delete; |
| 37 | GPIO& operator=(GPIO&&) = delete; |
| 38 | virtual ~GPIO() = default; |
| 39 | |
| 40 | enum class RequestType |
| 41 | { |
| 42 | Read, |
| 43 | Write |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Request ownership of the GPIO. |
| 48 | * |
| 49 | * Throws an exception if an error occurs. |
| 50 | * |
| 51 | * This is required before getting or setting the GPIO value. |
| 52 | * |
| 53 | * @param type specifies whether requesting to read or write the GPIO |
| 54 | */ |
| 55 | virtual void request(RequestType type) = 0; |
| 56 | |
| 57 | /** |
| 58 | * Gets the value of the GPIO. |
| 59 | * |
| 60 | * Throws an exception if an error occurs. |
| 61 | * |
| 62 | * @return 0 or 1 |
| 63 | */ |
| 64 | virtual int getValue() = 0; |
| 65 | |
| 66 | /** |
| 67 | * Sets the value of the GPIO. |
| 68 | * |
| 69 | * Throws an exception if an error occurs. |
| 70 | * |
| 71 | * @param value new value (0 or 1) |
| 72 | */ |
| 73 | virtual void setValue(int value) = 0; |
| 74 | |
| 75 | /** |
| 76 | * Release ownership of the GPIO. |
| 77 | * |
| 78 | * Throws an exception if an error occurs. |
| 79 | */ |
| 80 | virtual void release() = 0; |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * @class BMCGPIO |
| 85 | * |
| 86 | * Implementation of the GPIO interface using the standard BMC API (libgpiod). |
| 87 | */ |
| 88 | class BMCGPIO : public GPIO |
| 89 | { |
| 90 | public: |
| 91 | BMCGPIO() = delete; |
| 92 | BMCGPIO(const BMCGPIO&) = delete; |
| 93 | BMCGPIO(BMCGPIO&&) = delete; |
| 94 | BMCGPIO& operator=(const BMCGPIO&) = delete; |
| 95 | BMCGPIO& operator=(BMCGPIO&&) = delete; |
| 96 | virtual ~BMCGPIO() = default; |
| 97 | |
| 98 | /** |
| 99 | * Constructor. |
| 100 | * |
| 101 | * Throws an exception if a GPIO with the specified name cannot be found. |
| 102 | * |
| 103 | * @param name GPIO name |
| 104 | */ |
| 105 | explicit BMCGPIO(const std::string& name) |
| 106 | { |
| 107 | line = gpiod::find_line(name); |
| 108 | if (!line) |
| 109 | { |
| 110 | throw std::invalid_argument{"Invalid GPIO name: " + name}; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | virtual void request(RequestType type) override |
| 115 | { |
| 116 | int lineRequestType; |
| 117 | if (type == RequestType::Read) |
| 118 | { |
| 119 | lineRequestType = gpiod::line_request::DIRECTION_INPUT; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | lineRequestType = gpiod::line_request::DIRECTION_OUTPUT; |
| 124 | } |
| 125 | line.request({"phosphor-power-control", lineRequestType, 0}); |
| 126 | } |
| 127 | |
| 128 | virtual int getValue() override |
| 129 | { |
| 130 | return line.get_value(); |
| 131 | } |
| 132 | |
| 133 | virtual void setValue(int value) override |
| 134 | { |
| 135 | line.set_value(value); |
| 136 | } |
| 137 | |
| 138 | virtual void release() override |
| 139 | { |
| 140 | line.release(); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | /** |
| 145 | * GPIO line to access the pin. |
| 146 | */ |
| 147 | gpiod::line line; |
| 148 | }; |
| 149 | |
| 150 | } // namespace phosphor::power::sequencer |