1539b608fSJim Wright #pragma once 2539b608fSJim Wright 3*21254396SAndrew Geissler #include "config.h" 4*21254396SAndrew Geissler 51f8b1103SShawn McCarney #include "compatible_system_types_finder.hpp" 61f8b1103SShawn McCarney #include "device_finder.hpp" 722318a32SJim Wright #include "power_interface.hpp" 81f8b1103SShawn McCarney #include "power_sequencer_device.hpp" 91f8b1103SShawn McCarney #include "rail.hpp" 101f8b1103SShawn McCarney #include "services.hpp" 1122318a32SJim Wright 127a5dd99bSJim Wright #include <gpiod.hpp> 13539b608fSJim Wright #include <sdbusplus/bus.hpp> 14539b608fSJim Wright #include <sdbusplus/server/object.hpp> 157a5dd99bSJim Wright #include <sdeventplus/clock.hpp> 16539b608fSJim Wright #include <sdeventplus/event.hpp> 17539b608fSJim Wright #include <sdeventplus/utility/timer.hpp> 18539b608fSJim Wright 19539b608fSJim Wright #include <chrono> 201f8b1103SShawn McCarney #include <filesystem> 211f8b1103SShawn McCarney #include <memory> 221f8b1103SShawn McCarney #include <optional> 23eae2d614SJim Wright #include <string> 241f8b1103SShawn McCarney #include <vector> 25539b608fSJim Wright 26539b608fSJim Wright namespace phosphor::power::sequencer 27539b608fSJim Wright { 28539b608fSJim Wright 297354ce62SPatrick Williams using PowerObject = sdbusplus::server::object_t<PowerInterface>; 3022318a32SJim Wright 31539b608fSJim Wright /** 32539b608fSJim Wright * @class PowerControl 33539b608fSJim Wright * This class implements GPIO control of power on / off, and monitoring of the 34539b608fSJim Wright * chassis power good. 35539b608fSJim Wright */ 3622318a32SJim Wright class PowerControl : public PowerObject 37539b608fSJim Wright { 38539b608fSJim Wright public: 39539b608fSJim Wright PowerControl() = delete; 40539b608fSJim Wright PowerControl(const PowerControl&) = delete; 41539b608fSJim Wright PowerControl& operator=(const PowerControl&) = delete; 42539b608fSJim Wright PowerControl(PowerControl&&) = delete; 43539b608fSJim Wright PowerControl& operator=(PowerControl&&) = delete; 44539b608fSJim Wright ~PowerControl() = default; 45539b608fSJim Wright 46539b608fSJim Wright /** 47539b608fSJim Wright * Creates a controller object for power on and off. 48213ffe99SJim Wright * @param bus D-Bus bus object 49213ffe99SJim Wright * @param event event object 50539b608fSJim Wright */ 517354ce62SPatrick Williams PowerControl(sdbusplus::bus_t& bus, const sdeventplus::Event& event); 52539b608fSJim Wright 5322318a32SJim Wright /** @copydoc PowerInterface::getPgood() */ 5422318a32SJim Wright int getPgood() const override; 5522318a32SJim Wright 5622318a32SJim Wright /** @copydoc PowerInterface::getPgoodTimeout() */ 5722318a32SJim Wright int getPgoodTimeout() const override; 5822318a32SJim Wright 5922318a32SJim Wright /** @copydoc PowerInterface::getState() */ 6022318a32SJim Wright int getState() const override; 6122318a32SJim Wright 6222318a32SJim Wright /** @copydoc PowerInterface::setPgoodTimeout() */ 6322318a32SJim Wright void setPgoodTimeout(int timeout) override; 6422318a32SJim Wright 6522318a32SJim Wright /** @copydoc PowerInterface::setState() */ 6622318a32SJim Wright void setState(int state) override; 6722318a32SJim Wright 68ccea2d2bSJim Wright /** @copydoc PowerInterface::setPowerSupplyError() */ 69ccea2d2bSJim Wright void setPowerSupplyError(const std::string& error) override; 70ccea2d2bSJim Wright 711f8b1103SShawn McCarney /** 721f8b1103SShawn McCarney * Callback that is called when a list of compatible system types is found. 731f8b1103SShawn McCarney * 741f8b1103SShawn McCarney * @param types Compatible system types for the current system ordered from 751f8b1103SShawn McCarney * most to least specific 761f8b1103SShawn McCarney */ 771f8b1103SShawn McCarney void compatibleSystemTypesFound(const std::vector<std::string>& types); 781f8b1103SShawn McCarney 791f8b1103SShawn McCarney /** 801f8b1103SShawn McCarney * Callback that is called when a power sequencer device is found. 811f8b1103SShawn McCarney * 821f8b1103SShawn McCarney * @param properties Properties of device that was found 831f8b1103SShawn McCarney */ 841f8b1103SShawn McCarney void deviceFound(const DeviceProperties& properties); 851f8b1103SShawn McCarney 86539b608fSJim Wright private: 87539b608fSJim Wright /** 88539b608fSJim Wright * The D-Bus bus object 89539b608fSJim Wright */ 907354ce62SPatrick Williams sdbusplus::bus_t& bus; 91539b608fSJim Wright 92539b608fSJim Wright /** 931f8b1103SShawn McCarney * System services like hardware presence and the journal. 947945dd27SJim Wright */ 951f8b1103SShawn McCarney BMCServices services; 96ccea2d2bSJim Wright 97ccea2d2bSJim Wright /** 981f8b1103SShawn McCarney * Object that finds the compatible system types for the current system. 99ccea2d2bSJim Wright */ 1001f8b1103SShawn McCarney std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder; 1011f8b1103SShawn McCarney 1021f8b1103SShawn McCarney /** 1031f8b1103SShawn McCarney * Compatible system types for the current system ordered from most to least 1041f8b1103SShawn McCarney * specific. 1051f8b1103SShawn McCarney */ 1061f8b1103SShawn McCarney std::vector<std::string> compatibleSystemTypes; 1071f8b1103SShawn McCarney 1081f8b1103SShawn McCarney /** 1091f8b1103SShawn McCarney * Object that finds the power sequencer device in the system. 1101f8b1103SShawn McCarney */ 1111f8b1103SShawn McCarney std::unique_ptr<DeviceFinder> deviceFinder; 1121f8b1103SShawn McCarney 1131f8b1103SShawn McCarney /** 1141f8b1103SShawn McCarney * Power sequencer device properties. 1151f8b1103SShawn McCarney */ 1161f8b1103SShawn McCarney std::optional<DeviceProperties> deviceProperties; 1171f8b1103SShawn McCarney 1181f8b1103SShawn McCarney /** 1191f8b1103SShawn McCarney * Power sequencer device that enables and monitors the voltage rails. 1201f8b1103SShawn McCarney */ 1211f8b1103SShawn McCarney std::unique_ptr<PowerSequencerDevice> device; 1227945dd27SJim Wright 1237945dd27SJim Wright /** 12448752626SJim Wright * Indicates if a failure has already been found. Cleared at power on. 12548752626SJim Wright */ 12648752626SJim Wright bool failureFound{false}; 12748752626SJim Wright 12848752626SJim Wright /** 12948752626SJim Wright * Indicates if a state transition is taking place 1307a5dd99bSJim Wright */ 1317a5dd99bSJim Wright bool inStateTransition{false}; 1327a5dd99bSJim Wright 1337a5dd99bSJim Wright /** 134b4ad95ddSJim Wright * Minimum time from cold start to power on constant 135b4ad95ddSJim Wright */ 136b4ad95ddSJim Wright static constexpr std::chrono::seconds minimumColdStartTime{15}; 137b4ad95ddSJim Wright 138b4ad95ddSJim Wright /** 139b4ad95ddSJim Wright * Minimum time from power off to power on constant 140b4ad95ddSJim Wright */ 141b4ad95ddSJim Wright static constexpr std::chrono::seconds minimumPowerOffTime{25}; 142b4ad95ddSJim Wright 143b4ad95ddSJim Wright /** 14422318a32SJim Wright * Power good 145539b608fSJim Wright */ 14622318a32SJim Wright int pgood{0}; 14722318a32SJim Wright 14822318a32SJim Wright /** 1497a5dd99bSJim Wright * GPIO line object for chassis power good 1507a5dd99bSJim Wright */ 1517a5dd99bSJim Wright gpiod::line pgoodLine; 1527a5dd99bSJim Wright 1537a5dd99bSJim Wright /** 15422318a32SJim Wright * Power good timeout constant 15522318a32SJim Wright */ 156*21254396SAndrew Geissler static constexpr std::chrono::seconds pgoodTimeout{PGOOD_TIMEOUT}; 15722318a32SJim Wright 15822318a32SJim Wright /** 1597a5dd99bSJim Wright * Point in time at which power good timeout will take place 1607a5dd99bSJim Wright */ 1617a5dd99bSJim Wright std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime; 1627a5dd99bSJim Wright 1637a5dd99bSJim Wright /** 1641b639538SJim Wright * Timer to wait after pgood failure. This is to allow the power supplies 1651b639538SJim Wright * and other hardware time to complete failure processing. 1661b639538SJim Wright */ 1671b639538SJim Wright sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pgoodWaitTimer; 1681b639538SJim Wright 1691b639538SJim Wright /** 17022318a32SJim Wright * Poll interval constant 17122318a32SJim Wright */ 172b4ad95ddSJim Wright static constexpr std::chrono::milliseconds pollInterval{3000}; 17322318a32SJim Wright 17422318a32SJim Wright /** 175213ffe99SJim Wright * GPIO line object for power on / power off control 1767a5dd99bSJim Wright */ 1777a5dd99bSJim Wright gpiod::line powerControlLine; 1787a5dd99bSJim Wright 1797a5dd99bSJim Wright /** 180b4ad95ddSJim Wright * Point in time at which minumum power off time will have passed 181b4ad95ddSJim Wright */ 182b4ad95ddSJim Wright std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime; 183b4ad95ddSJim Wright 184b4ad95ddSJim Wright /** 18548752626SJim Wright * Power supply error. Cleared at power on. 186ccea2d2bSJim Wright */ 187ccea2d2bSJim Wright std::string powerSupplyError; 188ccea2d2bSJim Wright 189ccea2d2bSJim Wright /** 19022318a32SJim Wright * Power state 19122318a32SJim Wright */ 19222318a32SJim Wright int state{0}; 19322318a32SJim Wright 19422318a32SJim Wright /** 19522318a32SJim Wright * Power good timeout 19622318a32SJim Wright */ 19722318a32SJim Wright std::chrono::seconds timeout{pgoodTimeout}; 198539b608fSJim Wright 199539b608fSJim Wright /** 200539b608fSJim Wright * Timer to poll the pgood 201539b608fSJim Wright */ 202539b608fSJim Wright sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; 203539b608fSJim Wright 204539b608fSJim Wright /** 2051b639538SJim Wright * Callback to begin failure processing after observing pgood failure wait 2061b639538SJim Wright */ 2071b639538SJim Wright void onFailureCallback(); 2081b639538SJim Wright 2091b639538SJim Wright /** 2101f8b1103SShawn McCarney * Begin pgood failure processing 2111f8b1103SShawn McCarney * 2121f8b1103SShawn McCarney * @param wasTimeOut Indicates whether failure state was determined by 2131f8b1103SShawn McCarney * timing out 2141b639538SJim Wright */ 2151f8b1103SShawn McCarney void onFailure(bool wasTimeOut); 2161b639538SJim Wright 2171b639538SJim Wright /** 218539b608fSJim Wright * Polling method for monitoring the system power good 219539b608fSJim Wright */ 220539b608fSJim Wright void pollPgood(); 2217a5dd99bSJim Wright 2227a5dd99bSJim Wright /** 2237a5dd99bSJim Wright * Set up GPIOs 2247a5dd99bSJim Wright */ 2257a5dd99bSJim Wright void setUpGpio(); 2261f8b1103SShawn McCarney 2271f8b1103SShawn McCarney /** 2281f8b1103SShawn McCarney * Loads the JSON configuration file and creates the power sequencer device 2291f8b1103SShawn McCarney * object. 2301f8b1103SShawn McCarney * 2311f8b1103SShawn McCarney * Does nothing if the compatible system types or device properties have not 2321f8b1103SShawn McCarney * been found yet. These are obtained from D-Bus. The order in which they 2331f8b1103SShawn McCarney * are found and the time to find them varies. 2341f8b1103SShawn McCarney */ 2351f8b1103SShawn McCarney void loadConfigFileAndCreateDevice(); 2361f8b1103SShawn McCarney 2371f8b1103SShawn McCarney /** 2381f8b1103SShawn McCarney * Finds the JSON configuration file for the current system based on the 2391f8b1103SShawn McCarney * compatible system types. 2401f8b1103SShawn McCarney * 2411f8b1103SShawn McCarney * Does nothing if the compatible system types have not been found yet. 2421f8b1103SShawn McCarney * 2431f8b1103SShawn McCarney * @return absolute path to the config file, or empty path if file not found 2441f8b1103SShawn McCarney */ 2451f8b1103SShawn McCarney std::filesystem::path findConfigFile(); 2461f8b1103SShawn McCarney 2471f8b1103SShawn McCarney /** 2481f8b1103SShawn McCarney * Parses the specified JSON configuration file. 2491f8b1103SShawn McCarney * 2501f8b1103SShawn McCarney * Returns the resulting vector of Rail objects in the output parameter. 2511f8b1103SShawn McCarney * 2521f8b1103SShawn McCarney * @param configFile Absolute path to the config file 2531f8b1103SShawn McCarney * @param rails Rail objects within the config file 2541f8b1103SShawn McCarney * @return true if file was parsed successfully, false otherwise 2551f8b1103SShawn McCarney */ 2561f8b1103SShawn McCarney bool parseConfigFile(const std::filesystem::path& configFile, 2571f8b1103SShawn McCarney std::vector<std::unique_ptr<Rail>>& rails); 2581f8b1103SShawn McCarney 2591f8b1103SShawn McCarney /** 2601f8b1103SShawn McCarney * Creates the power sequencer device object based on the device properties. 2611f8b1103SShawn McCarney * 2621f8b1103SShawn McCarney * Does nothing if the device properties have not been found yet. 2631f8b1103SShawn McCarney * 2641f8b1103SShawn McCarney * @param rails Voltage rails that are enabled and monitored by the device 2651f8b1103SShawn McCarney */ 2661f8b1103SShawn McCarney void createDevice(std::vector<std::unique_ptr<Rail>> rails); 267539b608fSJim Wright }; 268539b608fSJim Wright 269539b608fSJim Wright } // namespace phosphor::power::sequencer 270