blob: d0466eb0b57103ee9d4cf3af579170aa3984d21e [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 McCarney0b1a0e72020-03-11 18:01:44 -050021#include "presence_detection.hpp"
22#include "rail.hpp"
Bob King23243f82020-07-29 10:38:57 +080023#include "services.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060024
25#include <memory>
Shawn McCarneya2461b32019-10-24 18:53:01 -050026#include <string>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060027#include <utility>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050028#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050029
Shawn McCarneyea7385b2019-11-07 12:19:32 -060030namespace phosphor::power::regulators
Shawn McCarneya2461b32019-10-24 18:53:01 -050031{
32
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050033// Forward declarations to avoid circular dependencies
34class Chassis;
35class System;
36
Shawn McCarneya2461b32019-10-24 18:53:01 -050037/**
38 * @class Device
39 *
40 * A hardware device, such as a voltage regulator or I/O expander.
41 */
42class 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 McCarneyafb7fc32019-12-11 19:42:03 -060057 * @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 McCarney0b1a0e72020-03-11 18:01:44 -050060 * @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 McCarneya2461b32019-10-24 18:53:01 -050064 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050065 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 McCarneyafb7fc32019-12-11 19:42:03 -060072 id{id},
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050073 isRegulatorDevice{isRegulator}, fru{fru},
74 i2cInterface{std::move(i2cInterface)}, presenceDetection{std::move(
75 presenceDetection)},
76 configuration{std::move(configuration)}, rails{std::move(rails)}
Shawn McCarneya2461b32019-10-24 18:53:01 -050077 {
78 }
79
80 /**
Shawn McCarneydb0b8332020-04-06 14:13:04 -050081 * 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 McCarneyb4d18a42020-06-02 10:27:05 -050090 * Closes this device.
91 *
92 * Closes any interfaces that are open to this device. Releases any other
93 * operating system resources associated with this device.
94 */
95 void close();
96
97 /**
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050098 * Configure this device.
99 *
100 * Applies the configuration changes that are defined for this device, if
101 * any.
102 *
103 * Also configures the voltage rails produced by this device, if any.
104 *
105 * This method should be called during the boot before regulators are
106 * enabled.
107 *
Bob King23243f82020-07-29 10:38:57 +0800108 * @param services system services like error logging and the journal
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500109 * @param system system that contains the chassis
110 * @param chassis chassis that contains this device
111 */
Bob King23243f82020-07-29 10:38:57 +0800112 void configure(Services& services, System& system, Chassis& chassis);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500113
114 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500115 * Returns the configuration changes to apply to this device, if any.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500116 *
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500117 * @return Pointer to Configuration object. Will equal nullptr if no
118 * configuration changes are defined for this device.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500119 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500120 const std::unique_ptr<Configuration>& getConfiguration() const
Shawn McCarneya2461b32019-10-24 18:53:01 -0500121 {
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500122 return configuration;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600123 }
124
125 /**
126 * Returns the Field-Replaceable Unit (FRU) for this device.
127 *
128 * Returns the D-Bus inventory path of the FRU. If the device itself is not
129 * a FRU, returns the FRU that contains the device.
130 *
131 * @return FRU for this device
132 */
133 const std::string& getFRU() const
134 {
135 return fru;
136 }
137
138 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500139 * Returns the I2C interface to this device.
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600140 *
141 * @return I2C interface to device
142 */
143 i2c::I2CInterface& getI2CInterface()
144 {
145 return *i2cInterface;
146 }
147
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500148 /**
149 * Returns the unique ID of this device.
150 *
151 * @return device ID
152 */
153 const std::string& getID() const
154 {
155 return id;
156 }
157
158 /**
159 * Returns the presence detection for this device, if any.
160 *
161 * @return Pointer to PresenceDetection object. Will equal nullptr if no
162 * presence detection is defined for this device.
163 */
164 const std::unique_ptr<PresenceDetection>& getPresenceDetection() const
165 {
166 return presenceDetection;
167 }
168
169 /**
170 * Returns the voltage rails produced by this device, if any.
171 *
172 * @return voltage rails
173 */
174 const std::vector<std::unique_ptr<Rail>>& getRails() const
175 {
176 return rails;
177 }
178
179 /**
180 * Returns whether this device is a voltage regulator.
181 *
182 * @return true if device is a voltage regulator, false otherwise
183 */
184 bool isRegulator() const
185 {
186 return isRegulatorDevice;
187 }
188
Bob King8e1cd0b2020-07-08 13:30:27 +0800189 /**
190 * Monitors the sensors for the voltage rails produced by this device, if
191 * any.
192 *
193 * This method should be called once per second.
194 *
Bob King8a552922020-08-05 17:02:31 +0800195 * @param services system services like error logging and the journal
Bob King8e1cd0b2020-07-08 13:30:27 +0800196 * @param system system that contains the chassis
197 * @param chassis chassis that contains the device
198 */
Bob King8a552922020-08-05 17:02:31 +0800199 void monitorSensors(Services& services, System& system, Chassis& chassis);
Bob King8e1cd0b2020-07-08 13:30:27 +0800200
Shawn McCarneya2461b32019-10-24 18:53:01 -0500201 private:
202 /**
203 * Unique ID of this device.
204 */
205 const std::string id{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600206
207 /**
208 * Indicates whether this device is a voltage regulator.
209 */
210 const bool isRegulatorDevice{false};
211
212 /**
213 * Field-Replaceable Unit (FRU) for this device.
214 *
215 * Set to the D-Bus inventory path of the FRU. If the device itself is not
216 * a FRU, set to the FRU that contains the device.
217 */
218 const std::string fru{};
219
220 /**
221 * I2C interface to this device.
222 */
223 std::unique_ptr<i2c::I2CInterface> i2cInterface{};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500224
225 /**
226 * Presence detection for this device, if any. Set to nullptr if no
227 * presence detection is defined for this device.
228 */
229 std::unique_ptr<PresenceDetection> presenceDetection{};
230
231 /**
232 * Configuration changes to apply to this device, if any. Set to nullptr if
233 * no configuration changes are defined for this device.
234 */
235 std::unique_ptr<Configuration> configuration{};
236
237 /**
238 * Voltage rails produced by this device, if any. Vector is empty if no
239 * voltage rails are defined for this device.
240 */
241 std::vector<std::unique_ptr<Rail>> rails{};
Shawn McCarneya2461b32019-10-24 18:53:01 -0500242};
243
Shawn McCarneyea7385b2019-11-07 12:19:32 -0600244} // namespace phosphor::power::regulators