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" |
| 4 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 5 | #include <gpiod.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdbusplus/message.hpp> |
| 8 | #include <sdbusplus/server/object.hpp> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 9 | #include <sdeventplus/clock.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 10 | #include <sdeventplus/event.hpp> |
| 11 | #include <sdeventplus/utility/timer.hpp> |
| 12 | |
| 13 | #include <chrono> |
| 14 | |
| 15 | namespace phosphor::power::sequencer |
| 16 | { |
| 17 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 18 | using PowerObject = sdbusplus::server::object::object<PowerInterface>; |
| 19 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 20 | /** |
| 21 | * @class PowerControl |
| 22 | * This class implements GPIO control of power on / off, and monitoring of the |
| 23 | * chassis power good. |
| 24 | */ |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 25 | class PowerControl : public PowerObject |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 26 | { |
| 27 | public: |
| 28 | PowerControl() = delete; |
| 29 | PowerControl(const PowerControl&) = delete; |
| 30 | PowerControl& operator=(const PowerControl&) = delete; |
| 31 | PowerControl(PowerControl&&) = delete; |
| 32 | PowerControl& operator=(PowerControl&&) = delete; |
| 33 | ~PowerControl() = default; |
| 34 | |
| 35 | /** |
| 36 | * Creates a controller object for power on and off. |
| 37 | * @param[in] bus D-Bus bus object |
| 38 | * @param[in] event event object |
| 39 | */ |
| 40 | PowerControl(sdbusplus::bus::bus& bus, const sdeventplus::Event& event); |
| 41 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 42 | /** @copydoc PowerInterface::getPgood() */ |
| 43 | int getPgood() const override; |
| 44 | |
| 45 | /** @copydoc PowerInterface::getPgoodTimeout() */ |
| 46 | int getPgoodTimeout() const override; |
| 47 | |
| 48 | /** @copydoc PowerInterface::getState() */ |
| 49 | int getState() const override; |
| 50 | |
| 51 | /** @copydoc PowerInterface::setPgoodTimeout() */ |
| 52 | void setPgoodTimeout(int timeout) override; |
| 53 | |
| 54 | /** @copydoc PowerInterface::setState() */ |
| 55 | void setState(int state) override; |
| 56 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 57 | private: |
| 58 | /** |
| 59 | * The D-Bus bus object |
| 60 | */ |
| 61 | sdbusplus::bus::bus& bus; |
| 62 | |
| 63 | /** |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 64 | * Indicates if a state transistion is taking place |
| 65 | */ |
| 66 | bool inStateTransition{false}; |
| 67 | |
| 68 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 69 | * Power good |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 70 | */ |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 71 | int pgood{0}; |
| 72 | |
| 73 | /** |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 74 | * GPIO line object for chassis power good |
| 75 | */ |
| 76 | gpiod::line pgoodLine; |
| 77 | |
| 78 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 79 | * Power good timeout constant |
| 80 | */ |
| 81 | static constexpr std::chrono::seconds pgoodTimeout{ |
| 82 | std::chrono::seconds(10)}; |
| 83 | |
| 84 | /** |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 85 | * Point in time at which power good timeout will take place |
| 86 | */ |
| 87 | std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime; |
| 88 | |
| 89 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 90 | * Poll interval constant |
| 91 | */ |
| 92 | static constexpr std::chrono::milliseconds pollInterval{ |
| 93 | std::chrono::milliseconds(3000)}; |
| 94 | |
| 95 | /** |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 96 | * GPIO line object for power-on / power-off control |
| 97 | */ |
| 98 | gpiod::line powerControlLine; |
| 99 | |
| 100 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 101 | * Power state |
| 102 | */ |
| 103 | int state{0}; |
| 104 | |
| 105 | /** |
| 106 | * Power good timeout |
| 107 | */ |
| 108 | std::chrono::seconds timeout{pgoodTimeout}; |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Timer to poll the pgood |
| 112 | */ |
| 113 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
| 114 | |
| 115 | /** |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 116 | * Polling method for monitoring the system power good |
| 117 | */ |
| 118 | void pollPgood(); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Set up GPIOs |
| 122 | */ |
| 123 | void setUpGpio(); |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace phosphor::power::sequencer |