blob: 30fbf7480b65242140573526d54bc5ff90b85843 [file] [log] [blame]
Brandon Wymana0f33ce2019-10-17 18:32:29 -05001#pragma once
2
Brandon Wyman8d195772020-01-27 15:03:51 -06003#include "pmbus.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06004#include "types.hpp"
Brandon Wyman3f1242f2020-01-28 13:11:25 -06005#include "utility.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06006
7#include <sdbusplus/bus/match.hpp>
8
Brandon Wymana0f33ce2019-10-17 18:32:29 -05009namespace phosphor::power::psu
10{
Brandon Wyman3f1242f2020-01-28 13:11:25 -060011
Brandon Wymana0f33ce2019-10-17 18:32:29 -050012/**
13 * @class PowerSupply
14 * Represents a PMBus power supply device.
15 */
16class PowerSupply
17{
18 public:
Brandon Wymanaed1f752019-11-25 18:10:52 -060019 PowerSupply() = delete;
Brandon Wymana0f33ce2019-10-17 18:32:29 -050020 PowerSupply(const PowerSupply&) = delete;
21 PowerSupply(PowerSupply&&) = delete;
22 PowerSupply& operator=(const PowerSupply&) = delete;
23 PowerSupply& operator=(PowerSupply&&) = delete;
24 ~PowerSupply() = default;
25
26 /**
Brandon Wymanc63941c2020-01-27 16:49:33 -060027 * @param[in] invpath - String for inventory path to use
28 * @param[in] i2cbus - The bus number this power supply is on
29 * @param[in] i2caddr - The 16-bit I2C address of the power supply
Brandon Wymanaed1f752019-11-25 18:10:52 -060030 */
Brandon Wymanc63941c2020-01-27 16:49:33 -060031 PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
32 std::uint8_t i2cbus, const std::string& i2caddr) :
33 bus(bus),
Brandon Wyman8d195772020-01-27 15:03:51 -060034 inventoryPath(invpath),
35 pmbusIntf(phosphor::pmbus::createPMBus(i2cbus, i2caddr))
Brandon Wymanaed1f752019-11-25 18:10:52 -060036 {
37 // Setup the functions to call when the D-Bus inventory path for the
38 // Present property changes.
39 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
40 bus,
41 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
42 INVENTORY_IFACE),
43 [this](auto& msg) { this->inventoryChanged(msg); });
44 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
45 bus,
46 sdbusplus::bus::match::rules::interfacesAdded() +
47 sdbusplus::bus::match::rules::path_namespace(inventoryPath),
48 [this](auto& msg) { this->inventoryChanged(msg); });
49 // Get the current state of the Present property.
50 updatePresence();
51 }
52
Brandon Wyman3f1242f2020-01-28 13:11:25 -060053 phosphor::pmbus::PMBusBase& getPMBus()
54 {
55 return *pmbusIntf;
56 }
57
Brandon Wymanaed1f752019-11-25 18:10:52 -060058 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -050059 * Power supply specific function to analyze for faults/errors.
60 *
61 * Various PMBus status bits will be checked for fault conditions.
62 * If a certain fault bits are on, the appropriate error will be
63 * committed.
64 */
Brandon Wyman3f1242f2020-01-28 13:11:25 -060065 void analyze();
Brandon Wymana0f33ce2019-10-17 18:32:29 -050066
67 /**
68 * Write PMBus CLEAR_FAULTS
69 *
70 * This function will be called in various situations in order to clear
71 * any fault status bits that may have been set, in order to start over
72 * with a clean state. Presence changes and power state changes will
73 * want to clear any faults logged.
74 */
75 void clearFaults()
76 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -060077 faultFound = false;
78 inputFault = false;
79 mfrFault = false;
80 vinUVFault = false;
Brandon Wymana0f33ce2019-10-17 18:32:29 -050081 }
82
83 /**
84 * @brief Adds properties to the inventory.
85 *
86 * Reads the values from the device and writes them to the
87 * associated power supply D-Bus inventory object.
88 *
89 * This needs to be done on startup, and each time the presence
90 * state changes.
91 *
92 * Properties added:
93 * - Serial Number
94 * - Part Number
95 * - CCIN (Customer Card Identification Number) - added as the Model
96 * - Firmware version
97 */
98 void updateInventory()
99 {
100 }
101
Brandon Wymanaed1f752019-11-25 18:10:52 -0600102 /**
103 * @brief Accessor function to indicate present status
104 */
105 bool isPresent() const
106 {
107 return present;
108 }
109
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600110 /**
111 * @brief Returns true if a fault was found.
112 */
113 bool isFaulted() const
114 {
115 return faultFound;
116 }
117
118 /**
119 * @brief Returns true if INPUT fault occurred.
120 */
121 bool hasInputFault() const
122 {
123 return inputFault;
124 }
125
126 /**
127 * @brief Returns true if MFRSPECIFIC occurred.
128 */
129 bool hasMFRFault() const
130 {
131 return mfrFault;
132 }
133
134 /**
135 * @brief Returns true if VIN_UV_FAULT occurred.
136 */
137 bool hasVINUVFault() const
138 {
139 return vinUVFault;
140 }
141
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500142 private:
Brandon Wymanaed1f752019-11-25 18:10:52 -0600143 /** @brief systemd bus member */
144 sdbusplus::bus::bus& bus;
145
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500146 /** @brief True if a fault has already been found and not cleared */
147 bool faultFound = false;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600148
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600149 /** @brief True if bit 5 of STATUS_WORD high byte is on. */
150 bool inputFault = false;
151
152 /** @brief True if bit 4 of STATUS_WORD high byte is on. */
153 bool mfrFault = false;
154
155 /** @brief True if bit 3 of STATUS_WORD low byte is on. */
156 bool vinUVFault = false;
157
Brandon Wymanaed1f752019-11-25 18:10:52 -0600158 /**
159 * @brief D-Bus path to use for this power supply's inventory status.
160 **/
161 std::string inventoryPath;
162
163 /** @brief True if the power supply is present. */
164 bool present = false;
165
166 /** @brief D-Bus match variable used to subscribe to Present property
167 * changes.
168 **/
169 std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
170
171 /** @brief D-Bus match variable used to subscribe for Present property
172 * interface added.
173 */
174 std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch;
175
176 /**
Brandon Wyman8d195772020-01-27 15:03:51 -0600177 * @brief Pointer to the PMBus interface
178 *
179 * Used to read or write to/from PMBus power supply devices.
180 */
181 std::unique_ptr<phosphor::pmbus::PMBusBase> pmbusIntf;
182
183 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600184 * @brief Updates the presence status by querying D-Bus
185 *
186 * The D-Bus inventory properties for this power supply will be read to
187 * determine if the power supply is present or not and update this
188 * object's present member variable to reflect current status.
189 **/
190 void updatePresence();
191
192 /**
193 * @brief Callback for inventory property changes
194 *
195 * Process change of Present property for power supply.
196 *
197 * @param[in] msg - Data associated with Present change signal
198 **/
199 void inventoryChanged(sdbusplus::message::message& msg);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500200};
201
202} // namespace phosphor::power::psu