| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 3 | #include "power_interface.hpp" | 
| Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 4 | #include "power_sequencer_monitor.hpp" | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 5 | #include "utility.hpp" | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 6 |  | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 7 | #include <gpiod.hpp> | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 9 | #include <sdbusplus/bus/match.hpp> | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 10 | #include <sdbusplus/message.hpp> | 
 | 11 | #include <sdbusplus/server/object.hpp> | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 12 | #include <sdeventplus/clock.hpp> | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 13 | #include <sdeventplus/event.hpp> | 
 | 14 | #include <sdeventplus/utility/timer.hpp> | 
 | 15 |  | 
 | 16 | #include <chrono> | 
 | 17 |  | 
 | 18 | namespace phosphor::power::sequencer | 
 | 19 | { | 
 | 20 |  | 
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 21 | using PowerObject = sdbusplus::server::object_t<PowerInterface>; | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 22 |  | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 23 | /** | 
 | 24 |  * @class PowerControl | 
 | 25 |  * This class implements GPIO control of power on / off, and monitoring of the | 
 | 26 |  * chassis power good. | 
 | 27 |  */ | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 28 | class PowerControl : public PowerObject | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 29 | { | 
 | 30 |   public: | 
 | 31 |     PowerControl() = delete; | 
 | 32 |     PowerControl(const PowerControl&) = delete; | 
 | 33 |     PowerControl& operator=(const PowerControl&) = delete; | 
 | 34 |     PowerControl(PowerControl&&) = delete; | 
 | 35 |     PowerControl& operator=(PowerControl&&) = delete; | 
 | 36 |     ~PowerControl() = default; | 
 | 37 |  | 
 | 38 |     /** | 
 | 39 |      * Creates a controller object for power on and off. | 
| Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 40 |      * @param bus D-Bus bus object | 
 | 41 |      * @param event event object | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 42 |      */ | 
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 43 |     PowerControl(sdbusplus::bus_t& bus, const sdeventplus::Event& event); | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 44 |  | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 45 |     /** @copydoc PowerInterface::getPgood() */ | 
 | 46 |     int getPgood() const override; | 
 | 47 |  | 
 | 48 |     /** @copydoc PowerInterface::getPgoodTimeout() */ | 
 | 49 |     int getPgoodTimeout() const override; | 
 | 50 |  | 
 | 51 |     /** @copydoc PowerInterface::getState() */ | 
 | 52 |     int getState() const override; | 
 | 53 |  | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 54 |     /** | 
 | 55 |      * Callback function to handle interfacesAdded D-Bus signals | 
 | 56 |      * @param msg Expanded sdbusplus message data | 
 | 57 |      */ | 
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 58 |     void interfacesAddedHandler(sdbusplus::message_t& msg); | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 59 |  | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 60 |     /** @copydoc PowerInterface::setPgoodTimeout() */ | 
 | 61 |     void setPgoodTimeout(int timeout) override; | 
 | 62 |  | 
 | 63 |     /** @copydoc PowerInterface::setState() */ | 
 | 64 |     void setState(int state) override; | 
 | 65 |  | 
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 66 |     /** @copydoc PowerInterface::setPowerSupplyError() */ | 
 | 67 |     void setPowerSupplyError(const std::string& error) override; | 
 | 68 |  | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 69 |   private: | 
 | 70 |     /** | 
 | 71 |      * The D-Bus bus object | 
 | 72 |      */ | 
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 73 |     sdbusplus::bus_t& bus; | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 74 |  | 
 | 75 |     /** | 
| Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 76 |      * The power sequencer device to monitor. | 
 | 77 |      */ | 
| Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 78 |     std::unique_ptr<PowerSequencerMonitor> device; | 
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 79 |  | 
 | 80 |     /** | 
 | 81 |      * Indicates if a specific power sequencer device has already been found. | 
 | 82 |      */ | 
 | 83 |     bool deviceFound{false}; | 
| Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 84 |  | 
 | 85 |     /** | 
| Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 86 |      * Indicates if a failure has already been found. Cleared at power on. | 
 | 87 |      */ | 
 | 88 |     bool failureFound{false}; | 
 | 89 |  | 
 | 90 |     /** | 
 | 91 |      * Indicates if a state transition is taking place | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 92 |      */ | 
 | 93 |     bool inStateTransition{false}; | 
 | 94 |  | 
 | 95 |     /** | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 96 |      * The match to Entity Manager interfaces added. | 
 | 97 |      */ | 
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 98 |     sdbusplus::bus::match_t match; | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 99 |  | 
 | 100 |     /** | 
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 101 |      * Minimum time from cold start to power on constant | 
 | 102 |      */ | 
 | 103 |     static constexpr std::chrono::seconds minimumColdStartTime{15}; | 
 | 104 |  | 
 | 105 |     /** | 
 | 106 |      * Minimum time from power off to power on constant | 
 | 107 |      */ | 
 | 108 |     static constexpr std::chrono::seconds minimumPowerOffTime{25}; | 
 | 109 |  | 
 | 110 |     /** | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 111 |      * Power good | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 112 |      */ | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 113 |     int pgood{0}; | 
 | 114 |  | 
 | 115 |     /** | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 116 |      * GPIO line object for chassis power good | 
 | 117 |      */ | 
 | 118 |     gpiod::line pgoodLine; | 
 | 119 |  | 
 | 120 |     /** | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 121 |      * Power good timeout constant | 
 | 122 |      */ | 
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 123 |     static constexpr std::chrono::seconds pgoodTimeout{10}; | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 124 |  | 
 | 125 |     /** | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 126 |      * Point in time at which power good timeout will take place | 
 | 127 |      */ | 
 | 128 |     std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime; | 
 | 129 |  | 
 | 130 |     /** | 
| Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 131 |      * Timer to wait after pgood failure. This is to allow the power supplies | 
 | 132 |      * and other hardware time to complete failure processing. | 
 | 133 |      */ | 
 | 134 |     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pgoodWaitTimer; | 
 | 135 |  | 
 | 136 |     /** | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 137 |      * Poll interval constant | 
 | 138 |      */ | 
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 139 |     static constexpr std::chrono::milliseconds pollInterval{3000}; | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 140 |  | 
 | 141 |     /** | 
| Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 142 |      * GPIO line object for power on / power off control | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 143 |      */ | 
 | 144 |     gpiod::line powerControlLine; | 
 | 145 |  | 
 | 146 |     /** | 
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 147 |      * Point in time at which minumum power off time will have passed | 
 | 148 |      */ | 
 | 149 |     std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime; | 
 | 150 |  | 
 | 151 |     /** | 
| Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 152 |      * Power supply error.  Cleared at power on. | 
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 153 |      */ | 
 | 154 |     std::string powerSupplyError; | 
 | 155 |  | 
 | 156 |     /** | 
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 157 |      * Power state | 
 | 158 |      */ | 
 | 159 |     int state{0}; | 
 | 160 |  | 
 | 161 |     /** | 
 | 162 |      * Power good timeout | 
 | 163 |      */ | 
 | 164 |     std::chrono::seconds timeout{pgoodTimeout}; | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 165 |  | 
 | 166 |     /** | 
 | 167 |      * Timer to poll the pgood | 
 | 168 |      */ | 
 | 169 |     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; | 
 | 170 |  | 
 | 171 |     /** | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 172 |      * Get the device properties | 
| Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 173 |      * @param properties A map of property names and values | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 174 |      */ | 
 | 175 |     void getDeviceProperties(util::DbusPropertyMap& properties); | 
 | 176 |  | 
 | 177 |     /** | 
| Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 178 |      * Callback to begin failure processing after observing pgood failure wait | 
 | 179 |      */ | 
 | 180 |     void onFailureCallback(); | 
 | 181 |  | 
 | 182 |     /** | 
 | 183 |      * Begin pgood failute processing | 
 | 184 |      * @param timeout if the failure state was determined by timing out | 
 | 185 |      */ | 
 | 186 |     void onFailure(bool timeout); | 
 | 187 |  | 
 | 188 |     /** | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 189 |      * Polling method for monitoring the system power good | 
 | 190 |      */ | 
 | 191 |     void pollPgood(); | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 192 |  | 
 | 193 |     /** | 
| Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 194 |      * Set up power sequencer device | 
 | 195 |      */ | 
 | 196 |     void setUpDevice(); | 
 | 197 |  | 
 | 198 |     /** | 
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 199 |      * Set up GPIOs | 
 | 200 |      */ | 
 | 201 |     void setUpGpio(); | 
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 202 | }; | 
 | 203 |  | 
 | 204 | } // namespace phosphor::power::sequencer |