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