blob: f623fd829ccb69e55f2ec29f04aeffb226759d78 [file] [log] [blame]
Jim Wright539b6082021-08-02 14:50:23 -05001#pragma once
2
Jim Wright22318a32021-08-27 15:56:09 -05003#include "power_interface.hpp"
Jim Wright7945dd22021-04-06 16:55:15 -05004#include "power_sequencer_monitor.hpp"
Jim Wright2d99bf72021-11-19 11:18:12 -06005#include "utility.hpp"
Jim Wright22318a32021-08-27 15:56:09 -05006
Jim Wright7a5dd992021-08-31 16:56:52 -05007#include <gpiod.hpp>
Jim Wright539b6082021-08-02 14:50:23 -05008#include <sdbusplus/bus.hpp>
Jim Wright2d99bf72021-11-19 11:18:12 -06009#include <sdbusplus/bus/match.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050010#include <sdbusplus/message.hpp>
11#include <sdbusplus/server/object.hpp>
Jim Wright7a5dd992021-08-31 16:56:52 -050012#include <sdeventplus/clock.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050013#include <sdeventplus/event.hpp>
14#include <sdeventplus/utility/timer.hpp>
15
16#include <chrono>
Jim Wrighteae2d612022-12-23 13:14:17 -060017#include <string>
Jim Wright539b6082021-08-02 14:50:23 -050018
19namespace phosphor::power::sequencer
20{
21
Patrick Williams7354ce62022-07-22 19:26:56 -050022using PowerObject = sdbusplus::server::object_t<PowerInterface>;
Jim Wright22318a32021-08-27 15:56:09 -050023
Jim Wright539b6082021-08-02 14:50:23 -050024/**
25 * @class PowerControl
26 * This class implements GPIO control of power on / off, and monitoring of the
27 * chassis power good.
28 */
Jim Wright22318a32021-08-27 15:56:09 -050029class PowerControl : public PowerObject
Jim Wright539b6082021-08-02 14:50:23 -050030{
31 public:
32 PowerControl() = delete;
33 PowerControl(const PowerControl&) = delete;
34 PowerControl& operator=(const PowerControl&) = delete;
35 PowerControl(PowerControl&&) = delete;
36 PowerControl& operator=(PowerControl&&) = delete;
37 ~PowerControl() = default;
38
39 /**
40 * Creates a controller object for power on and off.
Jim Wright213ffe92022-06-03 08:54:30 -050041 * @param bus D-Bus bus object
42 * @param event event object
Jim Wright539b6082021-08-02 14:50:23 -050043 */
Patrick Williams7354ce62022-07-22 19:26:56 -050044 PowerControl(sdbusplus::bus_t& bus, const sdeventplus::Event& event);
Jim Wright539b6082021-08-02 14:50:23 -050045
Jim Wright22318a32021-08-27 15:56:09 -050046 /** @copydoc PowerInterface::getPgood() */
47 int getPgood() const override;
48
49 /** @copydoc PowerInterface::getPgoodTimeout() */
50 int getPgoodTimeout() const override;
51
52 /** @copydoc PowerInterface::getState() */
53 int getState() const override;
54
Jim Wright2d99bf72021-11-19 11:18:12 -060055 /**
56 * Callback function to handle interfacesAdded D-Bus signals
57 * @param msg Expanded sdbusplus message data
58 */
Patrick Williams7354ce62022-07-22 19:26:56 -050059 void interfacesAddedHandler(sdbusplus::message_t& msg);
Jim Wright2d99bf72021-11-19 11:18:12 -060060
Jim Wright22318a32021-08-27 15:56:09 -050061 /** @copydoc PowerInterface::setPgoodTimeout() */
62 void setPgoodTimeout(int timeout) override;
63
64 /** @copydoc PowerInterface::setState() */
65 void setState(int state) override;
66
Jim Wrightccea2d22021-12-10 14:10:46 -060067 /** @copydoc PowerInterface::setPowerSupplyError() */
68 void setPowerSupplyError(const std::string& error) override;
69
Jim Wright539b6082021-08-02 14:50:23 -050070 private:
71 /**
72 * The D-Bus bus object
73 */
Patrick Williams7354ce62022-07-22 19:26:56 -050074 sdbusplus::bus_t& bus;
Jim Wright539b6082021-08-02 14:50:23 -050075
76 /**
Jim Wright7945dd22021-04-06 16:55:15 -050077 * The power sequencer device to monitor.
78 */
Jim Wright930458c2022-01-24 14:37:27 -060079 std::unique_ptr<PowerSequencerMonitor> device;
Jim Wrightccea2d22021-12-10 14:10:46 -060080
81 /**
82 * Indicates if a specific power sequencer device has already been found.
83 */
84 bool deviceFound{false};
Jim Wright7945dd22021-04-06 16:55:15 -050085
86 /**
Jim Wright48752622022-02-28 20:37:53 -060087 * Indicates if a failure has already been found. Cleared at power on.
88 */
89 bool failureFound{false};
90
91 /**
92 * Indicates if a state transition is taking place
Jim Wright7a5dd992021-08-31 16:56:52 -050093 */
94 bool inStateTransition{false};
95
96 /**
Jim Wright2d99bf72021-11-19 11:18:12 -060097 * The match to Entity Manager interfaces added.
98 */
Jim Wrightccea2d22021-12-10 14:10:46 -060099 sdbusplus::bus::match_t match;
Jim Wright2d99bf72021-11-19 11:18:12 -0600100
101 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600102 * Minimum time from cold start to power on constant
103 */
104 static constexpr std::chrono::seconds minimumColdStartTime{15};
105
106 /**
107 * Minimum time from power off to power on constant
108 */
109 static constexpr std::chrono::seconds minimumPowerOffTime{25};
110
111 /**
Jim Wright22318a32021-08-27 15:56:09 -0500112 * Power good
Jim Wright539b6082021-08-02 14:50:23 -0500113 */
Jim Wright22318a32021-08-27 15:56:09 -0500114 int pgood{0};
115
116 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500117 * GPIO line object for chassis power good
118 */
119 gpiod::line pgoodLine;
120
121 /**
Jim Wright22318a32021-08-27 15:56:09 -0500122 * Power good timeout constant
123 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600124 static constexpr std::chrono::seconds pgoodTimeout{10};
Jim Wright22318a32021-08-27 15:56:09 -0500125
126 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500127 * Point in time at which power good timeout will take place
128 */
129 std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime;
130
131 /**
Jim Wright1b639532022-11-19 17:41:33 -0600132 * Timer to wait after pgood failure. This is to allow the power supplies
133 * and other hardware time to complete failure processing.
134 */
135 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pgoodWaitTimer;
136
137 /**
Jim Wright22318a32021-08-27 15:56:09 -0500138 * Poll interval constant
139 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600140 static constexpr std::chrono::milliseconds pollInterval{3000};
Jim Wright22318a32021-08-27 15:56:09 -0500141
142 /**
Jim Wright213ffe92022-06-03 08:54:30 -0500143 * GPIO line object for power on / power off control
Jim Wright7a5dd992021-08-31 16:56:52 -0500144 */
145 gpiod::line powerControlLine;
146
147 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600148 * Point in time at which minumum power off time will have passed
149 */
150 std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime;
151
152 /**
Jim Wright48752622022-02-28 20:37:53 -0600153 * Power supply error. Cleared at power on.
Jim Wrightccea2d22021-12-10 14:10:46 -0600154 */
155 std::string powerSupplyError;
156
157 /**
Jim Wright22318a32021-08-27 15:56:09 -0500158 * Power state
159 */
160 int state{0};
161
162 /**
163 * Power good timeout
164 */
165 std::chrono::seconds timeout{pgoodTimeout};
Jim Wright539b6082021-08-02 14:50:23 -0500166
167 /**
168 * Timer to poll the pgood
169 */
170 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
171
172 /**
Jim Wright2d99bf72021-11-19 11:18:12 -0600173 * Get the device properties
Jim Wright213ffe92022-06-03 08:54:30 -0500174 * @param properties A map of property names and values
Jim Wright2d99bf72021-11-19 11:18:12 -0600175 */
Jim Wrighteae2d612022-12-23 13:14:17 -0600176 void getDeviceProperties(const util::DbusPropertyMap& properties);
Jim Wright2d99bf72021-11-19 11:18:12 -0600177
178 /**
Jim Wright1b639532022-11-19 17:41:33 -0600179 * Callback to begin failure processing after observing pgood failure wait
180 */
181 void onFailureCallback();
182
183 /**
184 * Begin pgood failute processing
185 * @param timeout if the failure state was determined by timing out
186 */
187 void onFailure(bool timeout);
188
189 /**
Jim Wright539b6082021-08-02 14:50:23 -0500190 * Polling method for monitoring the system power good
191 */
192 void pollPgood();
Jim Wright7a5dd992021-08-31 16:56:52 -0500193
194 /**
Jim Wright2d99bf72021-11-19 11:18:12 -0600195 * Set up power sequencer device
196 */
197 void setUpDevice();
198
199 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500200 * Set up GPIOs
201 */
202 void setUpGpio();
Jim Wright539b6082021-08-02 14:50:23 -0500203};
204
205} // namespace phosphor::power::sequencer