blob: ab24e4b74ade7c960d4e4035929a430bbab996a1 [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"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060023
24#include <memory>
Shawn McCarneya2461b32019-10-24 18:53:01 -050025#include <string>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060026#include <utility>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050027#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050028
Shawn McCarneyea7385b2019-11-07 12:19:32 -060029namespace phosphor::power::regulators
Shawn McCarneya2461b32019-10-24 18:53:01 -050030{
31
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050032// Forward declarations to avoid circular dependencies
33class Chassis;
34class System;
35
Shawn McCarneya2461b32019-10-24 18:53:01 -050036/**
37 * @class Device
38 *
39 * A hardware device, such as a voltage regulator or I/O expander.
40 */
41class Device
42{
43 public:
44 // Specify which compiler-generated methods we want
45 Device() = delete;
46 Device(const Device&) = delete;
47 Device(Device&&) = delete;
48 Device& operator=(const Device&) = delete;
49 Device& operator=(Device&&) = delete;
50 ~Device() = default;
51
52 /**
53 * Constructor.
54 *
55 * @param id unique device ID
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060056 * @param isRegulator indicates whether this device is a voltage regulator
57 * @param fru Field-Replaceable Unit (FRU) for this device
58 * @param i2cInterface I2C interface to this device
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050059 * @param presenceDetection presence detection for this device, if any
60 * @param configuration configuration changes to apply to this device, if
61 * any
62 * @param rails voltage rails produced by this device, if any
Shawn McCarneya2461b32019-10-24 18:53:01 -050063 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050064 explicit Device(
65 const std::string& id, bool isRegulator, const std::string& fru,
66 std::unique_ptr<i2c::I2CInterface> i2cInterface,
67 std::unique_ptr<PresenceDetection> presenceDetection = nullptr,
68 std::unique_ptr<Configuration> configuration = nullptr,
69 std::vector<std::unique_ptr<Rail>> rails =
70 std::vector<std::unique_ptr<Rail>>{}) :
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060071 id{id},
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050072 isRegulatorDevice{isRegulator}, fru{fru},
73 i2cInterface{std::move(i2cInterface)}, presenceDetection{std::move(
74 presenceDetection)},
75 configuration{std::move(configuration)}, rails{std::move(rails)}
Shawn McCarneya2461b32019-10-24 18:53:01 -050076 {
77 }
78
79 /**
Shawn McCarneydb0b8332020-04-06 14:13:04 -050080 * Adds this Device object to the specified IDMap.
81 *
82 * Also adds any Rail objects in this Device to the IDMap.
83 *
84 * @param idMap mapping from IDs to the associated Device/Rail/Rule objects
85 */
86 void addToIDMap(IDMap& idMap);
87
88 /**
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050089 * Closes this device.
90 *
91 * Closes any interfaces that are open to this device. Releases any other
92 * operating system resources associated with this device.
93 */
94 void close();
95
96 /**
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050097 * Configure this device.
98 *
99 * Applies the configuration changes that are defined for this device, if
100 * any.
101 *
102 * Also configures the voltage rails produced by this device, if any.
103 *
104 * This method should be called during the boot before regulators are
105 * enabled.
106 *
107 * @param system system that contains the chassis
108 * @param chassis chassis that contains this device
109 */
110 void configure(System& system, Chassis& chassis);
111
112 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500113 * Returns the configuration changes to apply to this device, if any.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500114 *
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500115 * @return Pointer to Configuration object. Will equal nullptr if no
116 * configuration changes are defined for this device.
Shawn McCarneya2461b32019-10-24 18:53:01 -0500117 */
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500118 const std::unique_ptr<Configuration>& getConfiguration() const
Shawn McCarneya2461b32019-10-24 18:53:01 -0500119 {
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500120 return configuration;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600121 }
122
123 /**
124 * Returns the Field-Replaceable Unit (FRU) for this device.
125 *
126 * Returns the D-Bus inventory path of the FRU. If the device itself is not
127 * a FRU, returns the FRU that contains the device.
128 *
129 * @return FRU for this device
130 */
131 const std::string& getFRU() const
132 {
133 return fru;
134 }
135
136 /**
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500137 * Returns the I2C interface to this device.
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600138 *
139 * @return I2C interface to device
140 */
141 i2c::I2CInterface& getI2CInterface()
142 {
143 return *i2cInterface;
144 }
145
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500146 /**
147 * Returns the unique ID of this device.
148 *
149 * @return device ID
150 */
151 const std::string& getID() const
152 {
153 return id;
154 }
155
156 /**
157 * Returns the presence detection for this device, if any.
158 *
159 * @return Pointer to PresenceDetection object. Will equal nullptr if no
160 * presence detection is defined for this device.
161 */
162 const std::unique_ptr<PresenceDetection>& getPresenceDetection() const
163 {
164 return presenceDetection;
165 }
166
167 /**
168 * Returns the voltage rails produced by this device, if any.
169 *
170 * @return voltage rails
171 */
172 const std::vector<std::unique_ptr<Rail>>& getRails() const
173 {
174 return rails;
175 }
176
177 /**
178 * Returns whether this device is a voltage regulator.
179 *
180 * @return true if device is a voltage regulator, false otherwise
181 */
182 bool isRegulator() const
183 {
184 return isRegulatorDevice;
185 }
186
Shawn McCarneya2461b32019-10-24 18:53:01 -0500187 private:
188 /**
189 * Unique ID of this device.
190 */
191 const std::string id{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600192
193 /**
194 * Indicates whether this device is a voltage regulator.
195 */
196 const bool isRegulatorDevice{false};
197
198 /**
199 * Field-Replaceable Unit (FRU) for this device.
200 *
201 * Set to the D-Bus inventory path of the FRU. If the device itself is not
202 * a FRU, set to the FRU that contains the device.
203 */
204 const std::string fru{};
205
206 /**
207 * I2C interface to this device.
208 */
209 std::unique_ptr<i2c::I2CInterface> i2cInterface{};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500210
211 /**
212 * Presence detection for this device, if any. Set to nullptr if no
213 * presence detection is defined for this device.
214 */
215 std::unique_ptr<PresenceDetection> presenceDetection{};
216
217 /**
218 * Configuration changes to apply to this device, if any. Set to nullptr if
219 * no configuration changes are defined for this device.
220 */
221 std::unique_ptr<Configuration> configuration{};
222
223 /**
224 * Voltage rails produced by this device, if any. Vector is empty if no
225 * voltage rails are defined for this device.
226 */
227 std::vector<std::unique_ptr<Rail>> rails{};
Shawn McCarneya2461b32019-10-24 18:53:01 -0500228};
229
Shawn McCarneyea7385b2019-11-07 12:19:32 -0600230} // namespace phosphor::power::regulators