blob: 88e3075eeea6c6119abb1ba53b9b009863b8a83f [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>>{}) :
Patrick Williamsf5402192024-08-16 15:20:53 -040075 id{id}, isRegulatorDevice{isRegulator}, fru{fru},
Patrick Williams48781ae2023-05-10 07:50:50 -050076 i2cInterface{std::move(i2cInterface)},
77 presenceDetection{std::move(presenceDetection)},
Shawn McCarney32252592021-09-08 15:29:36 -050078 configuration{std::move(configuration)},
Patrick Williams48781ae2023-05-10 07:50:50 -050079 phaseFaultDetection{std::move(phaseFaultDetection)},
80 rails{std::move(rails)}
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000081 {}
Shawn McCarneya2461b32019-10-24 18:53:01 -050082
83 /**
Shawn McCarneydb0b8332020-04-06 14:13:04 -050084 * Adds this Device object to the specified IDMap.
85 *
86 * Also adds any Rail objects in this Device to the IDMap.
87 *
88 * @param idMap mapping from IDs to the associated Device/Rail/Rule objects
89 */
90 void addToIDMap(IDMap& idMap);
91
92 /**
Shawn McCarney9bd94d32021-01-25 19:40:42 -060093 * Clear any cached data about hardware devices.
94 */
95 void clearCache();
96
97 /**
Shawn McCarney371e2442021-05-14 14:18:07 -050098 * Clears all error history.
99 *
100 * All data on previously logged errors will be deleted. If errors occur
101 * again in the future they will be logged again.
102 *
103 * This method is normally called when the system is being powered on.
104 */
105 void clearErrorHistory();
106
107 /**
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500108 * Closes this device.
109 *
110 * Closes any interfaces that are open to this device. Releases any other
111 * operating system resources associated with this device.
Bob Kingd692d6d2020-09-14 13:42:57 +0800112 *
113 * @param services system services like error logging and the journal
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500114 */
Bob Kingd692d6d2020-09-14 13:42:57 +0800115 void close(Services& services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500116
117 /**
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500118 * Configure this device.
119 *
120 * Applies the configuration changes that are defined for this device, if
121 * any.
122 *
123 * Also configures the voltage rails produced by this device, if any.
124 *
125 * This method should be called during the boot before regulators are
126 * enabled.
127 *
Bob King23243f82020-07-29 10:38:57 +0800128 * @param services system services like error logging and the journal
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500129 * @param system system that contains the chassis
130 * @param chassis chassis that contains this device
131 */
Bob King23243f82020-07-29 10:38:57 +0800132 void configure(Services& services, System& system, Chassis& chassis);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500133
134 /**
Shawn McCarney1fd0b142021-09-09 10:04:22 -0500135 * Detect redundant phase faults in this device.
136 *
137 * Does nothing if phase fault detection is not defined for this device.
138 *
Shawn McCarney54b3ab92021-09-14 17:28:56 -0500139 * This method should be called repeatedly based on a timer.
Shawn McCarney1fd0b142021-09-09 10:04:22 -0500140 *
141 * @param services system services like error logging and the journal
142 * @param system system that contains the chassis
143 * @param chassis chassis that contains the device
144 */
145 void detectPhaseFaults(Services& services, System& system,
146 Chassis& chassis);
147
148 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500149 * Returns the configuration changes to apply to this device, if any.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500150 *
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500151 * @return Pointer to Configuration object. Will equal nullptr if no
152 * configuration changes are defined for this device.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500153 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500154 const std::unique_ptr<Configuration>& getConfiguration() const
Shawn McCarneya2461b32019-10-24 18:53:01 -0500155 {
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500156 return configuration;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600157 }
158
159 /**
160 * Returns the Field-Replaceable Unit (FRU) for this device.
161 *
162 * Returns the D-Bus inventory path of the FRU. If the device itself is not
163 * a FRU, returns the FRU that contains the device.
164 *
165 * @return FRU for this device
166 */
167 const std::string& getFRU() const
168 {
169 return fru;
170 }
171
172 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500173 * Returns the I2C interface to this device.
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600174 *
175 * @return I2C interface to device
176 */
177 i2c::I2CInterface& getI2CInterface()
178 {
179 return *i2cInterface;
180 }
181
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500182 /**
183 * Returns the unique ID of this device.
184 *
185 * @return device ID
186 */
187 const std::string& getID() const
188 {
189 return id;
190 }
191
192 /**
Shawn McCarney32252592021-09-08 15:29:36 -0500193 * Returns the phase fault detection for this device, if any.
194 *
195 * @return Pointer to PhaseFaultDetection object. Will equal nullptr if no
196 * phase fault detection is defined for this device.
197 */
198 const std::unique_ptr<PhaseFaultDetection>& getPhaseFaultDetection() const
199 {
200 return phaseFaultDetection;
201 }
202
203 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500204 * Returns the presence detection for this device, if any.
205 *
206 * @return Pointer to PresenceDetection object. Will equal nullptr if no
207 * presence detection is defined for this device.
208 */
209 const std::unique_ptr<PresenceDetection>& getPresenceDetection() const
210 {
211 return presenceDetection;
212 }
213
214 /**
215 * Returns the voltage rails produced by this device, if any.
216 *
217 * @return voltage rails
218 */
219 const std::vector<std::unique_ptr<Rail>>& getRails() const
220 {
221 return rails;
222 }
223
224 /**
Shawn McCarney48033bf2021-01-27 17:56:49 -0600225 * Returns whether this device is present.
226 *
227 * @return true if device is present, false otherwise
228 */
229 bool isPresent(Services& services, System& system, Chassis& chassis)
230 {
231 if (presenceDetection)
232 {
233 // Execute presence detection to determine if device is present
234 return presenceDetection->execute(services, system, chassis, *this);
235 }
236 else
237 {
238 // No presence detection defined; assume device is present
239 return true;
240 }
241 }
242
243 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500244 * Returns whether this device is a voltage regulator.
245 *
246 * @return true if device is a voltage regulator, false otherwise
247 */
248 bool isRegulator() const
249 {
250 return isRegulatorDevice;
251 }
252
Bob King8e1cd0b2020-07-08 13:30:27 +0800253 /**
254 * Monitors the sensors for the voltage rails produced by this device, if
255 * any.
256 *
Shawn McCarney54b3ab92021-09-14 17:28:56 -0500257 * This method should be called repeatedly based on a timer.
Bob King8e1cd0b2020-07-08 13:30:27 +0800258 *
Bob King8a552922020-08-05 17:02:31 +0800259 * @param services system services like error logging and the journal
Bob King8e1cd0b2020-07-08 13:30:27 +0800260 * @param system system that contains the chassis
261 * @param chassis chassis that contains the device
262 */
Bob King8a552922020-08-05 17:02:31 +0800263 void monitorSensors(Services& services, System& system, Chassis& chassis);
Bob King8e1cd0b2020-07-08 13:30:27 +0800264
Shawn McCarneya2461b32019-10-24 18:53:01 -0500265 private:
266 /**
267 * Unique ID of this device.
268 */
269 const std::string id{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600270
271 /**
272 * Indicates whether this device is a voltage regulator.
273 */
274 const bool isRegulatorDevice{false};
275
276 /**
277 * Field-Replaceable Unit (FRU) for this device.
278 *
279 * Set to the D-Bus inventory path of the FRU. If the device itself is not
280 * a FRU, set to the FRU that contains the device.
281 */
282 const std::string fru{};
283
284 /**
285 * I2C interface to this device.
286 */
287 std::unique_ptr<i2c::I2CInterface> i2cInterface{};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500288
289 /**
290 * Presence detection for this device, if any. Set to nullptr if no
291 * presence detection is defined for this device.
292 */
293 std::unique_ptr<PresenceDetection> presenceDetection{};
294
295 /**
296 * Configuration changes to apply to this device, if any. Set to nullptr if
297 * no configuration changes are defined for this device.
298 */
299 std::unique_ptr<Configuration> configuration{};
300
301 /**
Shawn McCarney32252592021-09-08 15:29:36 -0500302 * Phase fault detection for this device, if any. Set to nullptr if no
303 * phase fault detection is defined for this device.
304 */
305 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
306
307 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500308 * Voltage rails produced by this device, if any. Vector is empty if no
309 * voltage rails are defined for this device.
310 */
311 std::vector<std::unique_ptr<Rail>> rails{};
Shawn McCarneya2461b32019-10-24 18:53:01 -0500312};
313
Shawn McCarneyea7385b2019-11-07 12:19:32 -0600314} // namespace phosphor::power::regulators