blob: 0670876618873e90a41a4d0d77545e7934dd79d9 [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>
17
18namespace phosphor::power::sequencer
19{
20
Jim Wright22318a32021-08-27 15:56:09 -050021using PowerObject = sdbusplus::server::object::object<PowerInterface>;
22
Jim Wright539b6082021-08-02 14:50:23 -050023/**
24 * @class PowerControl
25 * This class implements GPIO control of power on / off, and monitoring of the
26 * chassis power good.
27 */
Jim Wright22318a32021-08-27 15:56:09 -050028class PowerControl : public PowerObject
Jim Wright539b6082021-08-02 14:50:23 -050029{
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.
40 * @param[in] bus D-Bus bus object
41 * @param[in] event event object
42 */
43 PowerControl(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
44
Jim Wright22318a32021-08-27 15:56:09 -050045 /** @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 Wright2d99bf72021-11-19 11:18:12 -060054 /**
55 * Callback function to handle interfacesAdded D-Bus signals
56 * @param msg Expanded sdbusplus message data
57 */
58 void interfacesAddedHandler(sdbusplus::message::message& msg);
59
Jim Wright22318a32021-08-27 15:56:09 -050060 /** @copydoc PowerInterface::setPgoodTimeout() */
61 void setPgoodTimeout(int timeout) override;
62
63 /** @copydoc PowerInterface::setState() */
64 void setState(int state) override;
65
Jim Wrightccea2d22021-12-10 14:10:46 -060066 /** @copydoc PowerInterface::setPowerSupplyError() */
67 void setPowerSupplyError(const std::string& error) override;
68
Jim Wright539b6082021-08-02 14:50:23 -050069 private:
70 /**
71 * The D-Bus bus object
72 */
73 sdbusplus::bus::bus& bus;
74
75 /**
Jim Wright7945dd22021-04-06 16:55:15 -050076 * The power sequencer device to monitor.
77 */
Jim Wright930458c2022-01-24 14:37:27 -060078 std::unique_ptr<PowerSequencerMonitor> device;
Jim Wrightccea2d22021-12-10 14:10:46 -060079
80 /**
81 * Indicates if a specific power sequencer device has already been found.
82 */
83 bool deviceFound{false};
Jim Wright7945dd22021-04-06 16:55:15 -050084
85 /**
Jim Wright48752622022-02-28 20:37:53 -060086 * 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 Wright7a5dd992021-08-31 16:56:52 -050092 */
93 bool inStateTransition{false};
94
95 /**
Jim Wright2d99bf72021-11-19 11:18:12 -060096 * The match to Entity Manager interfaces added.
97 */
Jim Wrightccea2d22021-12-10 14:10:46 -060098 sdbusplus::bus::match_t match;
Jim Wright2d99bf72021-11-19 11:18:12 -060099
100 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600101 * 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 Wright22318a32021-08-27 15:56:09 -0500111 * Power good
Jim Wright539b6082021-08-02 14:50:23 -0500112 */
Jim Wright22318a32021-08-27 15:56:09 -0500113 int pgood{0};
114
115 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500116 * GPIO line object for chassis power good
117 */
118 gpiod::line pgoodLine;
119
120 /**
Jim Wright22318a32021-08-27 15:56:09 -0500121 * Power good timeout constant
122 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600123 static constexpr std::chrono::seconds pgoodTimeout{10};
Jim Wright22318a32021-08-27 15:56:09 -0500124
125 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500126 * 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 Wright22318a32021-08-27 15:56:09 -0500131 * Poll interval constant
132 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600133 static constexpr std::chrono::milliseconds pollInterval{3000};
Jim Wright22318a32021-08-27 15:56:09 -0500134
135 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500136 * GPIO line object for power-on / power-off control
137 */
138 gpiod::line powerControlLine;
139
140 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600141 * Point in time at which minumum power off time will have passed
142 */
143 std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime;
144
145 /**
Jim Wright48752622022-02-28 20:37:53 -0600146 * Power supply error. Cleared at power on.
Jim Wrightccea2d22021-12-10 14:10:46 -0600147 */
148 std::string powerSupplyError;
149
150 /**
Jim Wright22318a32021-08-27 15:56:09 -0500151 * Power state
152 */
153 int state{0};
154
155 /**
156 * Power good timeout
157 */
158 std::chrono::seconds timeout{pgoodTimeout};
Jim Wright539b6082021-08-02 14:50:23 -0500159
160 /**
161 * Timer to poll the pgood
162 */
163 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
164
165 /**
Jim Wright2d99bf72021-11-19 11:18:12 -0600166 * Get the device properties
167 * @param[in] properties A map of property names and values
168 */
169 void getDeviceProperties(util::DbusPropertyMap& properties);
170
171 /**
Jim Wright539b6082021-08-02 14:50:23 -0500172 * Polling method for monitoring the system power good
173 */
174 void pollPgood();
Jim Wright7a5dd992021-08-31 16:56:52 -0500175
176 /**
Jim Wright2d99bf72021-11-19 11:18:12 -0600177 * Set up power sequencer device
178 */
179 void setUpDevice();
180
181 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500182 * Set up GPIOs
183 */
184 void setUpGpio();
Jim Wright539b6082021-08-02 14:50:23 -0500185};
186
187} // namespace phosphor::power::sequencer