blob: a8f35b68e3ea8500c64bd294af834c876fdbc834 [file] [log] [blame]
Shawn McCarneya2461b32019-10-24 18:53:01 -05001/**
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 McCarney0b1a0e72020-03-11 18:01:44 -050018#include "configuration.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060019#include "i2c_interface.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050020#include "id_map.hpp"
Shawn McCarney32252592021-09-08 15:29:36 -050021#include "phase_fault_detection.hpp"
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050022#include "presence_detection.hpp"
23#include "rail.hpp"
Bob King23243f82020-07-29 10:38:57 +080024#include "services.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060025
26#include <memory>
Shawn McCarneya2461b32019-10-24 18:53:01 -050027#include <string>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060028#include <utility>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050029#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050030
Shawn McCarneyea7385b2019-11-07 12:19:32 -060031namespace phosphor::power::regulators
Shawn McCarneya2461b32019-10-24 18:53:01 -050032{
33
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050034// Forward declarations to avoid circular dependencies
35class Chassis;
36class System;
37
Shawn McCarneya2461b32019-10-24 18:53:01 -050038/**
39 * @class Device
40 *
41 * A hardware device, such as a voltage regulator or I/O expander.
42 */
43class 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 McCarneyafb7fc32019-12-11 19:42:03 -060058 * @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 McCarney0b1a0e72020-03-11 18:01:44 -050061 * @param presenceDetection presence detection for this device, if any
62 * @param configuration configuration changes to apply to this device, if
63 * any
Shawn McCarney32252592021-09-08 15:29:36 -050064 * @param phaseFaultDetection phase fault detection for this device, if any
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050065 * @param rails voltage rails produced by this device, if any
Shawn McCarneya2461b32019-10-24 18:53:01 -050066 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050067 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 McCarney32252592021-09-08 15:29:36 -050072 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection = nullptr,
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050073 std::vector<std::unique_ptr<Rail>> rails =
74 std::vector<std::unique_ptr<Rail>>{}) :
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060075 id{id},
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050076 isRegulatorDevice{isRegulator}, fru{fru},
77 i2cInterface{std::move(i2cInterface)}, presenceDetection{std::move(
78 presenceDetection)},
Shawn McCarney32252592021-09-08 15:29:36 -050079 configuration{std::move(configuration)},
80 phaseFaultDetection{std::move(phaseFaultDetection)}, rails{std::move(
81 rails)}
Shawn McCarneya2461b32019-10-24 18:53:01 -050082 {
83 }
84
85 /**
Shawn McCarneydb0b8332020-04-06 14:13:04 -050086 * 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 McCarney9bd94d32021-01-25 19:40:42 -060095 * Clear any cached data about hardware devices.
96 */
97 void clearCache();
98
99 /**
Shawn McCarney371e2442021-05-14 14:18:07 -0500100 * 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 McCarneyb4d18a42020-06-02 10:27:05 -0500110 * 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 Kingd692d6d2020-09-14 13:42:57 +0800114 *
115 * @param services system services like error logging and the journal
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500116 */
Bob Kingd692d6d2020-09-14 13:42:57 +0800117 void close(Services& services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500118
119 /**
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500120 * 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 King23243f82020-07-29 10:38:57 +0800130 * @param services system services like error logging and the journal
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500131 * @param system system that contains the chassis
132 * @param chassis chassis that contains this device
133 */
Bob King23243f82020-07-29 10:38:57 +0800134 void configure(Services& services, System& system, Chassis& chassis);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500135
136 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500137 * Returns the configuration changes to apply to this device, if any.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500138 *
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500139 * @return Pointer to Configuration object. Will equal nullptr if no
140 * configuration changes are defined for this device.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500141 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500142 const std::unique_ptr<Configuration>& getConfiguration() const
Shawn McCarneya2461b32019-10-24 18:53:01 -0500143 {
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500144 return configuration;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600145 }
146
147 /**
148 * Returns the Field-Replaceable Unit (FRU) for this device.
149 *
150 * Returns the D-Bus inventory path of the FRU. If the device itself is not
151 * a FRU, returns the FRU that contains the device.
152 *
153 * @return FRU for this device
154 */
155 const std::string& getFRU() const
156 {
157 return fru;
158 }
159
160 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500161 * Returns the I2C interface to this device.
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600162 *
163 * @return I2C interface to device
164 */
165 i2c::I2CInterface& getI2CInterface()
166 {
167 return *i2cInterface;
168 }
169
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500170 /**
171 * Returns the unique ID of this device.
172 *
173 * @return device ID
174 */
175 const std::string& getID() const
176 {
177 return id;
178 }
179
180 /**
Shawn McCarney32252592021-09-08 15:29:36 -0500181 * Returns the phase fault detection for this device, if any.
182 *
183 * @return Pointer to PhaseFaultDetection object. Will equal nullptr if no
184 * phase fault detection is defined for this device.
185 */
186 const std::unique_ptr<PhaseFaultDetection>& getPhaseFaultDetection() const
187 {
188 return phaseFaultDetection;
189 }
190
191 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500192 * Returns the presence detection for this device, if any.
193 *
194 * @return Pointer to PresenceDetection object. Will equal nullptr if no
195 * presence detection is defined for this device.
196 */
197 const std::unique_ptr<PresenceDetection>& getPresenceDetection() const
198 {
199 return presenceDetection;
200 }
201
202 /**
203 * Returns the voltage rails produced by this device, if any.
204 *
205 * @return voltage rails
206 */
207 const std::vector<std::unique_ptr<Rail>>& getRails() const
208 {
209 return rails;
210 }
211
212 /**
Shawn McCarney48033bf2021-01-27 17:56:49 -0600213 * Returns whether this device is present.
214 *
215 * @return true if device is present, false otherwise
216 */
217 bool isPresent(Services& services, System& system, Chassis& chassis)
218 {
219 if (presenceDetection)
220 {
221 // Execute presence detection to determine if device is present
222 return presenceDetection->execute(services, system, chassis, *this);
223 }
224 else
225 {
226 // No presence detection defined; assume device is present
227 return true;
228 }
229 }
230
231 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500232 * Returns whether this device is a voltage regulator.
233 *
234 * @return true if device is a voltage regulator, false otherwise
235 */
236 bool isRegulator() const
237 {
238 return isRegulatorDevice;
239 }
240
Bob King8e1cd0b2020-07-08 13:30:27 +0800241 /**
242 * Monitors the sensors for the voltage rails produced by this device, if
243 * any.
244 *
245 * This method should be called once per second.
246 *
Bob King8a552922020-08-05 17:02:31 +0800247 * @param services system services like error logging and the journal
Bob King8e1cd0b2020-07-08 13:30:27 +0800248 * @param system system that contains the chassis
249 * @param chassis chassis that contains the device
250 */
Bob King8a552922020-08-05 17:02:31 +0800251 void monitorSensors(Services& services, System& system, Chassis& chassis);
Bob King8e1cd0b2020-07-08 13:30:27 +0800252
Shawn McCarneya2461b32019-10-24 18:53:01 -0500253 private:
254 /**
255 * Unique ID of this device.
256 */
257 const std::string id{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600258
259 /**
260 * Indicates whether this device is a voltage regulator.
261 */
262 const bool isRegulatorDevice{false};
263
264 /**
265 * Field-Replaceable Unit (FRU) for this device.
266 *
267 * Set to the D-Bus inventory path of the FRU. If the device itself is not
268 * a FRU, set to the FRU that contains the device.
269 */
270 const std::string fru{};
271
272 /**
273 * I2C interface to this device.
274 */
275 std::unique_ptr<i2c::I2CInterface> i2cInterface{};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500276
277 /**
278 * Presence detection for this device, if any. Set to nullptr if no
279 * presence detection is defined for this device.
280 */
281 std::unique_ptr<PresenceDetection> presenceDetection{};
282
283 /**
284 * Configuration changes to apply to this device, if any. Set to nullptr if
285 * no configuration changes are defined for this device.
286 */
287 std::unique_ptr<Configuration> configuration{};
288
289 /**
Shawn McCarney32252592021-09-08 15:29:36 -0500290 * Phase fault detection for this device, if any. Set to nullptr if no
291 * phase fault detection is defined for this device.
292 */
293 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
294
295 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500296 * Voltage rails produced by this device, if any. Vector is empty if no
297 * voltage rails are defined for this device.
298 */
299 std::vector<std::unique_ptr<Rail>> rails{};
Shawn McCarneya2461b32019-10-24 18:53:01 -0500300};
301
Shawn McCarneyea7385b2019-11-07 12:19:32 -0600302} // namespace phosphor::power::regulators