blob: 1699dcca360a33528c99a8d6a9fe8c911ed836db [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 Wright2d99bf72021-11-19 11:18:12 -06004#include "utility.hpp"
Jim Wright22318a32021-08-27 15:56:09 -05005
Jim Wright7a5dd992021-08-31 16:56:52 -05006#include <gpiod.hpp>
Jim Wright539b6082021-08-02 14:50:23 -05007#include <sdbusplus/bus.hpp>
Jim Wright2d99bf72021-11-19 11:18:12 -06008#include <sdbusplus/bus/match.hpp>
Jim Wright539b6082021-08-02 14:50:23 -05009#include <sdbusplus/message.hpp>
10#include <sdbusplus/server/object.hpp>
Jim Wright7a5dd992021-08-31 16:56:52 -050011#include <sdeventplus/clock.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050012#include <sdeventplus/event.hpp>
13#include <sdeventplus/utility/timer.hpp>
14
15#include <chrono>
16
17namespace phosphor::power::sequencer
18{
19
Jim Wright22318a32021-08-27 15:56:09 -050020using PowerObject = sdbusplus::server::object::object<PowerInterface>;
21
Jim Wright539b6082021-08-02 14:50:23 -050022/**
23 * @class PowerControl
24 * This class implements GPIO control of power on / off, and monitoring of the
25 * chassis power good.
26 */
Jim Wright22318a32021-08-27 15:56:09 -050027class PowerControl : public PowerObject
Jim Wright539b6082021-08-02 14:50:23 -050028{
29 public:
30 PowerControl() = delete;
31 PowerControl(const PowerControl&) = delete;
32 PowerControl& operator=(const PowerControl&) = delete;
33 PowerControl(PowerControl&&) = delete;
34 PowerControl& operator=(PowerControl&&) = delete;
35 ~PowerControl() = default;
36
37 /**
38 * Creates a controller object for power on and off.
39 * @param[in] bus D-Bus bus object
40 * @param[in] event event object
41 */
42 PowerControl(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
43
Jim Wright22318a32021-08-27 15:56:09 -050044 /** @copydoc PowerInterface::getPgood() */
45 int getPgood() const override;
46
47 /** @copydoc PowerInterface::getPgoodTimeout() */
48 int getPgoodTimeout() const override;
49
50 /** @copydoc PowerInterface::getState() */
51 int getState() const override;
52
Jim Wright2d99bf72021-11-19 11:18:12 -060053 /**
54 * Callback function to handle interfacesAdded D-Bus signals
55 * @param msg Expanded sdbusplus message data
56 */
57 void interfacesAddedHandler(sdbusplus::message::message& msg);
58
Jim Wright22318a32021-08-27 15:56:09 -050059 /** @copydoc PowerInterface::setPgoodTimeout() */
60 void setPgoodTimeout(int timeout) override;
61
62 /** @copydoc PowerInterface::setState() */
63 void setState(int state) override;
64
Jim Wright539b6082021-08-02 14:50:23 -050065 private:
66 /**
67 * The D-Bus bus object
68 */
69 sdbusplus::bus::bus& bus;
70
71 /**
Jim Wright7a5dd992021-08-31 16:56:52 -050072 * Indicates if a state transistion is taking place
73 */
74 bool inStateTransition{false};
75
76 /**
Jim Wright2d99bf72021-11-19 11:18:12 -060077 * The match to Entity Manager interfaces added.
78 */
79 std::unique_ptr<sdbusplus::bus::match_t> match;
80
81 /**
Jim Wright22318a32021-08-27 15:56:09 -050082 * Power good
Jim Wright539b6082021-08-02 14:50:23 -050083 */
Jim Wright22318a32021-08-27 15:56:09 -050084 int pgood{0};
85
86 /**
Jim Wright7a5dd992021-08-31 16:56:52 -050087 * GPIO line object for chassis power good
88 */
89 gpiod::line pgoodLine;
90
91 /**
Jim Wright22318a32021-08-27 15:56:09 -050092 * Power good timeout constant
93 */
94 static constexpr std::chrono::seconds pgoodTimeout{
95 std::chrono::seconds(10)};
96
97 /**
Jim Wright7a5dd992021-08-31 16:56:52 -050098 * Point in time at which power good timeout will take place
99 */
100 std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime;
101
102 /**
Jim Wright22318a32021-08-27 15:56:09 -0500103 * Poll interval constant
104 */
105 static constexpr std::chrono::milliseconds pollInterval{
106 std::chrono::milliseconds(3000)};
107
108 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500109 * GPIO line object for power-on / power-off control
110 */
111 gpiod::line powerControlLine;
112
113 /**
Jim Wright22318a32021-08-27 15:56:09 -0500114 * Power state
115 */
116 int state{0};
117
118 /**
119 * Power good timeout
120 */
121 std::chrono::seconds timeout{pgoodTimeout};
Jim Wright539b6082021-08-02 14:50:23 -0500122
123 /**
124 * Timer to poll the pgood
125 */
126 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
127
128 /**
Jim Wright2d99bf72021-11-19 11:18:12 -0600129 * Get the device properties
130 * @param[in] properties A map of property names and values
131 */
132 void getDeviceProperties(util::DbusPropertyMap& properties);
133
134 /**
Jim Wright539b6082021-08-02 14:50:23 -0500135 * Polling method for monitoring the system power good
136 */
137 void pollPgood();
Jim Wright7a5dd992021-08-31 16:56:52 -0500138
139 /**
Jim Wright2d99bf72021-11-19 11:18:12 -0600140 * Set up power sequencer device
141 */
142 void setUpDevice();
143
144 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500145 * Set up GPIOs
146 */
147 void setUpGpio();
Jim Wright539b6082021-08-02 14:50:23 -0500148};
149
150} // namespace phosphor::power::sequencer