| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 1 | /** | 
 | 2 |  * Copyright © 2019 IBM Corporation | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 | #pragma once | 
 | 17 |  | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 18 | #include "configuration.hpp" | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 19 | #include "i2c_interface.hpp" | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 20 | #include "id_map.hpp" | 
| Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 21 | #include "phase_fault_detection.hpp" | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 22 | #include "presence_detection.hpp" | 
 | 23 | #include "rail.hpp" | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 24 | #include "services.hpp" | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 25 |  | 
 | 26 | #include <memory> | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 27 | #include <string> | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 28 | #include <utility> | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 29 | #include <vector> | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 30 |  | 
| Shawn McCarney | ea7385b | 2019-11-07 12:19:32 -0600 | [diff] [blame] | 31 | namespace phosphor::power::regulators | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 32 | { | 
 | 33 |  | 
| Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 34 | // Forward declarations to avoid circular dependencies | 
 | 35 | class Chassis; | 
 | 36 | class System; | 
 | 37 |  | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 38 | /** | 
 | 39 |  * @class Device | 
 | 40 |  * | 
 | 41 |  * A hardware device, such as a voltage regulator or I/O expander. | 
 | 42 |  */ | 
 | 43 | class Device | 
 | 44 | { | 
 | 45 |   public: | 
 | 46 |     // Specify which compiler-generated methods we want | 
 | 47 |     Device() = delete; | 
 | 48 |     Device(const Device&) = delete; | 
 | 49 |     Device(Device&&) = delete; | 
 | 50 |     Device& operator=(const Device&) = delete; | 
 | 51 |     Device& operator=(Device&&) = delete; | 
 | 52 |     ~Device() = default; | 
 | 53 |  | 
 | 54 |     /** | 
 | 55 |      * Constructor. | 
 | 56 |      * | 
 | 57 |      * @param id unique device ID | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 58 |      * @param isRegulator indicates whether this device is a voltage regulator | 
 | 59 |      * @param fru Field-Replaceable Unit (FRU) for this device | 
 | 60 |      * @param i2cInterface I2C interface to this device | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 61 |      * @param presenceDetection presence detection for this device, if any | 
 | 62 |      * @param configuration configuration changes to apply to this device, if | 
 | 63 |      *                      any | 
| Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 64 |      * @param phaseFaultDetection phase fault detection for this device, if any | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 65 |      * @param rails voltage rails produced by this device, if any | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 66 |      */ | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 67 |     explicit Device( | 
 | 68 |         const std::string& id, bool isRegulator, const std::string& fru, | 
 | 69 |         std::unique_ptr<i2c::I2CInterface> i2cInterface, | 
 | 70 |         std::unique_ptr<PresenceDetection> presenceDetection = nullptr, | 
 | 71 |         std::unique_ptr<Configuration> configuration = nullptr, | 
| Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 72 |         std::unique_ptr<PhaseFaultDetection> phaseFaultDetection = nullptr, | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 73 |         std::vector<std::unique_ptr<Rail>> rails = | 
 | 74 |             std::vector<std::unique_ptr<Rail>>{}) : | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 75 |         id{id}, | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 76 |         isRegulatorDevice{isRegulator}, fru{fru}, | 
 | 77 |         i2cInterface{std::move(i2cInterface)}, presenceDetection{std::move( | 
 | 78 |                                                    presenceDetection)}, | 
| Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 79 |         configuration{std::move(configuration)}, | 
 | 80 |         phaseFaultDetection{std::move(phaseFaultDetection)}, rails{std::move( | 
 | 81 |                                                                  rails)} | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 82 |     { | 
 | 83 |     } | 
 | 84 |  | 
 | 85 |     /** | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 86 |      * Adds this Device object to the specified IDMap. | 
 | 87 |      * | 
 | 88 |      * Also adds any Rail objects in this Device to the IDMap. | 
 | 89 |      * | 
 | 90 |      * @param idMap mapping from IDs to the associated Device/Rail/Rule objects | 
 | 91 |      */ | 
 | 92 |     void addToIDMap(IDMap& idMap); | 
 | 93 |  | 
 | 94 |     /** | 
| Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 95 |      * Clear any cached data about hardware devices. | 
 | 96 |      */ | 
 | 97 |     void clearCache(); | 
 | 98 |  | 
 | 99 |     /** | 
| Shawn McCarney | 371e244 | 2021-05-14 14:18:07 -0500 | [diff] [blame] | 100 |      * Clears all error history. | 
 | 101 |      * | 
 | 102 |      * All data on previously logged errors will be deleted.  If errors occur | 
 | 103 |      * again in the future they will be logged again. | 
 | 104 |      * | 
 | 105 |      * This method is normally called when the system is being powered on. | 
 | 106 |      */ | 
 | 107 |     void clearErrorHistory(); | 
 | 108 |  | 
 | 109 |     /** | 
| Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 110 |      * Closes this device. | 
 | 111 |      * | 
 | 112 |      * Closes any interfaces that are open to this device.  Releases any other | 
 | 113 |      * operating system resources associated with this device. | 
| Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 114 |      * | 
 | 115 |      * @param services system services like error logging and the journal | 
| Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 116 |      */ | 
| Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 117 |     void close(Services& services); | 
| Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 118 |  | 
 | 119 |     /** | 
| Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 120 |      * Configure this device. | 
 | 121 |      * | 
 | 122 |      * Applies the configuration changes that are defined for this device, if | 
 | 123 |      * any. | 
 | 124 |      * | 
 | 125 |      * Also configures the voltage rails produced by this device, if any. | 
 | 126 |      * | 
 | 127 |      * This method should be called during the boot before regulators are | 
 | 128 |      * enabled. | 
 | 129 |      * | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 130 |      * @param services system services like error logging and the journal | 
| Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 131 |      * @param system system that contains the chassis | 
 | 132 |      * @param chassis chassis that contains this device | 
 | 133 |      */ | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 134 |     void configure(Services& services, System& system, Chassis& chassis); | 
| Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 135 |  | 
 | 136 |     /** | 
| Shawn McCarney | 1fd0b14 | 2021-09-09 10:04:22 -0500 | [diff] [blame^] | 137 |      * Detect redundant phase faults in this device. | 
 | 138 |      * | 
 | 139 |      * Does nothing if phase fault detection is not defined for this device. | 
 | 140 |      * | 
 | 141 |      * This method should be called every 15 seconds. | 
 | 142 |      * | 
 | 143 |      * @param services system services like error logging and the journal | 
 | 144 |      * @param system system that contains the chassis | 
 | 145 |      * @param chassis chassis that contains the device | 
 | 146 |      */ | 
 | 147 |     void detectPhaseFaults(Services& services, System& system, | 
 | 148 |                            Chassis& chassis); | 
 | 149 |  | 
 | 150 |     /** | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 151 |      * Returns the configuration changes to apply to this device, if any. | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 152 |      * | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 153 |      * @return Pointer to Configuration object.  Will equal nullptr if no | 
 | 154 |      *         configuration changes are defined for this device. | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 155 |      */ | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 156 |     const std::unique_ptr<Configuration>& getConfiguration() const | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 157 |     { | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 158 |         return configuration; | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 159 |     } | 
 | 160 |  | 
 | 161 |     /** | 
 | 162 |      * Returns the Field-Replaceable Unit (FRU) for this device. | 
 | 163 |      * | 
 | 164 |      * Returns the D-Bus inventory path of the FRU.  If the device itself is not | 
 | 165 |      * a FRU, returns the FRU that contains the device. | 
 | 166 |      * | 
 | 167 |      * @return FRU for this device | 
 | 168 |      */ | 
 | 169 |     const std::string& getFRU() const | 
 | 170 |     { | 
 | 171 |         return fru; | 
 | 172 |     } | 
 | 173 |  | 
 | 174 |     /** | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 175 |      * Returns the I2C interface to this device. | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 176 |      * | 
 | 177 |      * @return I2C interface to device | 
 | 178 |      */ | 
 | 179 |     i2c::I2CInterface& getI2CInterface() | 
 | 180 |     { | 
 | 181 |         return *i2cInterface; | 
 | 182 |     } | 
 | 183 |  | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 184 |     /** | 
 | 185 |      * Returns the unique ID of this device. | 
 | 186 |      * | 
 | 187 |      * @return device ID | 
 | 188 |      */ | 
 | 189 |     const std::string& getID() const | 
 | 190 |     { | 
 | 191 |         return id; | 
 | 192 |     } | 
 | 193 |  | 
 | 194 |     /** | 
| Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 195 |      * Returns the phase fault detection for this device, if any. | 
 | 196 |      * | 
 | 197 |      * @return Pointer to PhaseFaultDetection object.  Will equal nullptr if no | 
 | 198 |      *         phase fault detection is defined for this device. | 
 | 199 |      */ | 
 | 200 |     const std::unique_ptr<PhaseFaultDetection>& getPhaseFaultDetection() const | 
 | 201 |     { | 
 | 202 |         return phaseFaultDetection; | 
 | 203 |     } | 
 | 204 |  | 
 | 205 |     /** | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 206 |      * Returns the presence detection for this device, if any. | 
 | 207 |      * | 
 | 208 |      * @return Pointer to PresenceDetection object.  Will equal nullptr if no | 
 | 209 |      *         presence detection is defined for this device. | 
 | 210 |      */ | 
 | 211 |     const std::unique_ptr<PresenceDetection>& getPresenceDetection() const | 
 | 212 |     { | 
 | 213 |         return presenceDetection; | 
 | 214 |     } | 
 | 215 |  | 
 | 216 |     /** | 
 | 217 |      * Returns the voltage rails produced by this device, if any. | 
 | 218 |      * | 
 | 219 |      * @return voltage rails | 
 | 220 |      */ | 
 | 221 |     const std::vector<std::unique_ptr<Rail>>& getRails() const | 
 | 222 |     { | 
 | 223 |         return rails; | 
 | 224 |     } | 
 | 225 |  | 
 | 226 |     /** | 
| Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 227 |      * Returns whether this device is present. | 
 | 228 |      * | 
 | 229 |      * @return true if device is present, false otherwise | 
 | 230 |      */ | 
 | 231 |     bool isPresent(Services& services, System& system, Chassis& chassis) | 
 | 232 |     { | 
 | 233 |         if (presenceDetection) | 
 | 234 |         { | 
 | 235 |             // Execute presence detection to determine if device is present | 
 | 236 |             return presenceDetection->execute(services, system, chassis, *this); | 
 | 237 |         } | 
 | 238 |         else | 
 | 239 |         { | 
 | 240 |             // No presence detection defined; assume device is present | 
 | 241 |             return true; | 
 | 242 |         } | 
 | 243 |     } | 
 | 244 |  | 
 | 245 |     /** | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 246 |      * Returns whether this device is a voltage regulator. | 
 | 247 |      * | 
 | 248 |      * @return true if device is a voltage regulator, false otherwise | 
 | 249 |      */ | 
 | 250 |     bool isRegulator() const | 
 | 251 |     { | 
 | 252 |         return isRegulatorDevice; | 
 | 253 |     } | 
 | 254 |  | 
| Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 255 |     /** | 
 | 256 |      * Monitors the sensors for the voltage rails produced by this device, if | 
 | 257 |      * any. | 
 | 258 |      * | 
 | 259 |      * This method should be called once per second. | 
 | 260 |      * | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 261 |      * @param services system services like error logging and the journal | 
| Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 262 |      * @param system system that contains the chassis | 
 | 263 |      * @param chassis chassis that contains the device | 
 | 264 |      */ | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 265 |     void monitorSensors(Services& services, System& system, Chassis& chassis); | 
| Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 266 |  | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 267 |   private: | 
 | 268 |     /** | 
 | 269 |      * Unique ID of this device. | 
 | 270 |      */ | 
 | 271 |     const std::string id{}; | 
| Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 272 |  | 
 | 273 |     /** | 
 | 274 |      * Indicates whether this device is a voltage regulator. | 
 | 275 |      */ | 
 | 276 |     const bool isRegulatorDevice{false}; | 
 | 277 |  | 
 | 278 |     /** | 
 | 279 |      * Field-Replaceable Unit (FRU) for this device. | 
 | 280 |      * | 
 | 281 |      * Set to the D-Bus inventory path of the FRU.  If the device itself is not | 
 | 282 |      * a FRU, set to the FRU that contains the device. | 
 | 283 |      */ | 
 | 284 |     const std::string fru{}; | 
 | 285 |  | 
 | 286 |     /** | 
 | 287 |      * I2C interface to this device. | 
 | 288 |      */ | 
 | 289 |     std::unique_ptr<i2c::I2CInterface> i2cInterface{}; | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 290 |  | 
 | 291 |     /** | 
 | 292 |      * Presence detection for this device, if any.  Set to nullptr if no | 
 | 293 |      * presence detection is defined for this device. | 
 | 294 |      */ | 
 | 295 |     std::unique_ptr<PresenceDetection> presenceDetection{}; | 
 | 296 |  | 
 | 297 |     /** | 
 | 298 |      * Configuration changes to apply to this device, if any.  Set to nullptr if | 
 | 299 |      * no configuration changes are defined for this device. | 
 | 300 |      */ | 
 | 301 |     std::unique_ptr<Configuration> configuration{}; | 
 | 302 |  | 
 | 303 |     /** | 
| Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 304 |      * Phase fault detection for this device, if any.  Set to nullptr if no | 
 | 305 |      * phase fault detection is defined for this device. | 
 | 306 |      */ | 
 | 307 |     std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; | 
 | 308 |  | 
 | 309 |     /** | 
| Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 310 |      * Voltage rails produced by this device, if any.  Vector is empty if no | 
 | 311 |      * voltage rails are defined for this device. | 
 | 312 |      */ | 
 | 313 |     std::vector<std::unique_ptr<Rail>> rails{}; | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 314 | }; | 
 | 315 |  | 
| Shawn McCarney | ea7385b | 2019-11-07 12:19:32 -0600 | [diff] [blame] | 316 | } // namespace phosphor::power::regulators |