blob: 4bdc23ffe51ffd6effe9187fa05286e936d0bdea [file] [log] [blame]
Matt Spinler015e3ad2017-08-01 11:20:47 -05001#pragma once
2
Brandon Wyman9c7897c2019-03-28 17:42:34 -05003#include <filesystem>
Matt Spinler015e3ad2017-08-01 11:20:47 -05004#include <string>
5#include <vector>
6
Lei YUab093322019-10-09 16:43:22 +08007namespace phosphor
Matt Spinler015e3ad2017-08-01 11:20:47 -05008{
9namespace pmbus
10{
11
Brandon Wyman9c7897c2019-03-28 17:42:34 -050012namespace fs = std::filesystem;
Brandon Wymanff5f3392017-08-11 17:43:22 -050013
Adriana Kobylak4175ffb2021-08-02 14:51:05 +000014// The file name Linux uses to capture the READ_VIN from pmbus.
15constexpr auto READ_VIN = "in1_input";
16
17namespace in_input
18{
19// VIN thresholds in Volts
20constexpr auto VIN_VOLTAGE_MIN = 20;
21constexpr auto VIN_VOLTAGE_110_THRESHOLD = 160;
22
23// VIN actual values in Volts
24// VIN_VOLTAGE_0: VIN < VIN_VOLTAGE_MIN
25// VIN_VOLTAGE_110: VIN_VOLTAGE_MIN < VIN < VIN_VOLTAGE_110_THRESHOLD
26// VIN_VOLTAGE_220: VIN_VOLTAGE_110_THRESHOLD < VIN
27constexpr auto VIN_VOLTAGE_0 = 0;
28constexpr auto VIN_VOLTAGE_110 = 110;
29constexpr auto VIN_VOLTAGE_220 = 220;
30} // namespace in_input
31
Brandon Wyman10295542017-08-09 18:20:44 -050032// The file name Linux uses to capture the STATUS_WORD from pmbus.
Matt Spinlere7e432b2017-08-21 15:01:40 -050033constexpr auto STATUS_WORD = "status0";
Brandon Wyman10295542017-08-09 18:20:44 -050034
Brandon Wyman253dc9b2017-08-12 13:45:52 -050035// The file name Linux uses to capture the STATUS_INPUT from pmbus.
36constexpr auto STATUS_INPUT = "status0_input";
Matt Spinlere7e432b2017-08-21 15:01:40 -050037
Brandon Wyman764c7972017-08-22 17:05:36 -050038// Voltage out status.
39// Overvoltage fault or warning, Undervoltage fault or warning, maximum or
40// minimum warning, ....
Matt Spinlere7e432b2017-08-21 15:01:40 -050041// Uses Page substitution
42constexpr auto STATUS_VOUT = "statusP_vout";
43
Matt Spinlerde16d052017-12-13 13:22:14 -060044namespace status_vout
45{
46// Mask of bits that are only warnings
47constexpr auto WARNING_MASK = 0x6A;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050048} // namespace status_vout
Matt Spinlerde16d052017-12-13 13:22:14 -060049
Brandon Wyman764c7972017-08-22 17:05:36 -050050// Current output status bits.
51constexpr auto STATUS_IOUT = "status0_iout";
52
53// Manufacturing specific status bits
54constexpr auto STATUS_MFR = "status0_mfr";
55
Brandon Wyman12661f12017-08-31 15:28:21 -050056// Reports on the status of any fans installed in position 1 and 2.
57constexpr auto STATUS_FANS_1_2 = "status0_fans12";
58
59// Reports on temperature faults or warnings. Overtemperature fault,
60// overtemperature warning, undertemperature warning, undertemperature fault.
61constexpr auto STATUS_TEMPERATURE = "status0_temp";
62
Matt Spinlere7e432b2017-08-21 15:01:40 -050063namespace status_word
64{
65constexpr auto VOUT_FAULT = 0x8000;
Brandon Wyman764c7972017-08-22 17:05:36 -050066
67// The IBM CFF power supply driver does map this bit to power1_alarm in the
68// hwmon space, but since the other bits that need to be checked do not have
69// a similar mapping, the code will just read STATUS_WORD and use bit masking
70// to see if the INPUT FAULT OR WARNING bit is on.
71constexpr auto INPUT_FAULT_WARN = 0x2000;
72
Brandon Wyman3f1242f2020-01-28 13:11:25 -060073// The bit mask representing the MFRSPECIFIC fault, bit 4 of STATUS_WORD high
74// byte. A manufacturer specific fault or warning has occurred.
75constexpr auto MFR_SPECIFIC_FAULT = 0x1000;
76
Brandon Wyman764c7972017-08-22 17:05:36 -050077// The bit mask representing the POWER_GOOD Negated bit of the STATUS_WORD.
78constexpr auto POWER_GOOD_NEGATED = 0x0800;
79
Brandon Wyman12661f12017-08-31 15:28:21 -050080// The bit mask representing the FAN FAULT or WARNING bit of the STATUS_WORD.
81// Bit 2 of the high byte of STATUS_WORD.
82constexpr auto FAN_FAULT = 0x0400;
83
Brandon Wyman764c7972017-08-22 17:05:36 -050084// The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD.
85constexpr auto UNIT_IS_OFF = 0x0040;
86
Brandon Wymanab05c072017-08-30 18:26:41 -050087// Bit 5 of the STATUS_BYTE, or lower byte of STATUS_WORD is used to indicate
88// an output overvoltage fault.
89constexpr auto VOUT_OV_FAULT = 0x0020;
90
Brandon Wymanb165c252017-08-25 18:59:54 -050091// The bit mask representing that an output overcurrent fault has occurred.
92constexpr auto IOUT_OC_FAULT = 0x0010;
93
Brandon Wyman764c7972017-08-22 17:05:36 -050094// The IBM CFF power supply driver does map this bit to in1_alarm, however,
95// since a number of the other bits are not mapped that way for STATUS_WORD,
96// this code will just read the entire STATUS_WORD and use bit masking to find
97// out if that fault is on.
98constexpr auto VIN_UV_FAULT = 0x0008;
99
Brandon Wyman875b3632017-09-13 18:46:03 -0500100// The bit mask representing the TEMPERATURE FAULT or WARNING bit of the
101// STATUS_WORD. Bit 2 of the low byte (STATUS_BYTE).
102constexpr auto TEMPERATURE_FAULT_WARN = 0x0004;
103
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500104} // namespace status_word
Brandon Wyman875b3632017-09-13 18:46:03 -0500105
Lei YUe8c9cd62019-11-04 14:24:41 +0800106namespace status_vout
107{
108// The IBM CFF power supply driver maps MFR's OV_FAULT and VAUX_FAULT to this
109// bit.
110constexpr auto OV_FAULT = 0x80;
111
112// The IBM CFF power supply driver maps MFR's UV_FAULT to this bit.
113constexpr auto UV_FAULT = 0x10;
114} // namespace status_vout
115
Brandon Wyman875b3632017-09-13 18:46:03 -0500116namespace status_temperature
117{
118// Overtemperature Fault
119constexpr auto OT_FAULT = 0x80;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500120} // namespace status_temperature
Matt Spinlere7e432b2017-08-21 15:01:40 -0500121
Brandon Wyman59a35792020-06-04 12:37:40 -0500122constexpr auto ON_OFF_CONFIG = "on_off_config";
123
124// From PMBus Specification Part II Revsion 1.2:
125// The ON_OFF_CONFIG command configures the combination of CONTROL pin input
126// and serial bus commands needed to turn the unit on and off. This includes how
127// the unit responds when power is applied.
128// Bits [7:5] - 000 - Reserved
129// Bit 4 - 1 - Unit does not power up until commanded by the CONTROL pin and
130// OPERATION command (as programmed in bits [3:0]).
131// Bit 3 - 0 - Unit ignores the on/off portion of the OPERATION command from
132// serial bus.
133// Bit 2 - 1 - Unit requires the CONTROL pin to be asserted to start the unit.
134// Bit 1 - 0 - Polarity of the CONTROL pin. Active low (Pull pin low to start
135// the unit).
136// Bit 0 - 1 - Turn off the output and stop transferring energy to the output as
137// fast as possible.
138constexpr auto ON_OFF_CONFIG_CONTROL_PIN_ONLY = 0x15;
139
Matt Spinler015e3ad2017-08-01 11:20:47 -0500140/**
Matt Spinler4dc46782018-01-04 14:29:16 -0600141 * Where the access should be done
Matt Spinler57868bc2017-08-03 10:07:41 -0500142 */
143enum class Type
144{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500145 Base, // base device directory
146 Hwmon, // hwmon directory
147 Debug, // pmbus debug directory
148 DeviceDebug, // device debug directory
149 HwmonDeviceDebug // hwmon device debug directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500150};
151
152/**
Brandon Wyman8d195772020-01-27 15:03:51 -0600153 * @class PMBusBase
154 *
155 * This is a base class for PMBus to assist with unit testing via mocking.
156 */
157class PMBusBase
158{
159 public:
160 virtual ~PMBusBase() = default;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600161
162 virtual uint64_t read(const std::string& name, Type type) = 0;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500163 virtual std::string readString(const std::string& name, Type type) = 0;
Brandon Wyman59a35792020-06-04 12:37:40 -0500164 virtual void writeBinary(const std::string& name, std::vector<uint8_t> data,
165 Type type) = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600166 virtual void findHwmonDir() = 0;
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500167 virtual const fs::path& path() const = 0;
Brandon Wyman8d195772020-01-27 15:03:51 -0600168};
169
170/**
171 * Wrapper function for PMBus
172 *
173 * @param[in] bus - I2C bus
174 * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
175 *
176 * @return PMBusBase pointer
177 */
178std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
179 const std::string& address);
180
181/**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500182 * @class PMBus
183 *
184 * This class is an interface to communicating with PMBus devices
185 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -0500186 *
187 * Based on the Type parameter, the accesses can either be done
188 * in the base device directory (the one passed into the constructor),
189 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -0500190 */
Brandon Wyman8d195772020-01-27 15:03:51 -0600191class PMBus : public PMBusBase
Matt Spinler015e3ad2017-08-01 11:20:47 -0500192{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500193 public:
194 PMBus() = delete;
Brandon Wyman8d195772020-01-27 15:03:51 -0600195 virtual ~PMBus() = default;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500196 PMBus(const PMBus&) = default;
197 PMBus& operator=(const PMBus&) = default;
198 PMBus(PMBus&&) = default;
199 PMBus& operator=(PMBus&&) = default;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500200
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500201 /**
202 * Constructor
203 *
204 * @param[in] path - path to the sysfs directory
205 */
206 PMBus(const std::string& path) : basePath(path)
207 {
208 findHwmonDir();
209 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500210
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500211 /**
212 * Constructor
213 *
214 * This version is required when DeviceDebug
215 * access will be used.
216 *
217 * @param[in] path - path to the sysfs directory
218 * @param[in] driverName - the device driver name
219 * @param[in] instance - chip instance number
220 */
221 PMBus(const std::string& path, const std::string& driverName,
222 size_t instance) :
223 basePath(path),
224 driverName(driverName), instance(instance)
225 {
226 findHwmonDir();
227 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500228
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500229 /**
Brandon Wyman8d195772020-01-27 15:03:51 -0600230 * Wrapper function for PMBus
231 *
232 * @param[in] bus - I2C bus
233 * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
234 *
235 * @return PMBusBase pointer
236 */
237 static std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
238 const std::string& address);
239
240 /**
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500241 * Reads a file in sysfs that represents a single bit,
242 * therefore doing a PMBus read.
243 *
244 * @param[in] name - path concatenated to
245 * basePath to read
246 * @param[in] type - Path type
247 *
248 * @return bool - false if result was 0, else true
249 */
250 bool readBit(const std::string& name, Type type);
Matt Spinler8f0d9532017-08-21 11:22:37 -0500251
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500252 /**
253 * Reads a file in sysfs that represents a single bit,
254 * where the page number passed in is substituted
255 * into the name in place of the 'P' character in it.
256 *
257 * @param[in] name - path concatenated to
258 * basePath to read
259 * @param[in] page - page number
260 * @param[in] type - Path type
261 *
262 * @return bool - false if result was 0, else true
263 */
264 bool readBitInPage(const std::string& name, size_t page, Type type);
265 /**
266 * Checks if the file for the given name and type exists.
267 *
268 * @param[in] name - path concatenated to basePath to read
269 * @param[in] type - Path type
270 *
271 * @return bool - True if file exists, false if it does not.
272 */
273 bool exists(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500274
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500275 /**
276 * Read byte(s) from file in sysfs.
277 *
278 * @param[in] name - path concatenated to basePath to read
279 * @param[in] type - Path type
280 *
281 * @return uint64_t - Up to 8 bytes of data read from file.
282 */
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600283 uint64_t read(const std::string& name, Type type) override;
Brandon Wyman3b7b38b2017-09-25 16:43:45 -0500284
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500285 /**
286 * Read a string from file in sysfs.
287 *
288 * @param[in] name - path concatenated to basePath to read
289 * @param[in] type - Path type
290 *
291 * @return string - The data read from the file.
292 */
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500293 std::string readString(const std::string& name, Type type) override;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500294
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500295 /**
296 * Read data from a binary file in sysfs.
297 *
298 * @param[in] name - path concatenated to basePath to read
299 * @param[in] type - Path type
300 * @param[in] length - length of data to read, in bytes
301 *
302 * @return vector<uint8_t> - The data read from the file.
303 */
304 std::vector<uint8_t> readBinary(const std::string& name, Type type,
305 size_t length);
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600306
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500307 /**
308 * Writes an integer value to the file, therefore doing
309 * a PMBus write.
310 *
311 * @param[in] name - path concatenated to
312 * basePath to write
313 * @param[in] value - the value to write
314 * @param[in] type - Path type
315 */
316 void write(const std::string& name, int value, Type type);
Matt Spinlerfa23e332018-01-18 11:24:58 -0600317
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500318 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500319 * Writes binary data to a file in sysfs.
320 *
321 * @param[in] name - path concatenated to basePath to write
322 * @param[in] data - The data to write to the file
323 * @param[in] type - Path type
324 */
325 void writeBinary(const std::string& name, std::vector<uint8_t> data,
326 Type type) override;
327
328 /**
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500329 * Returns the sysfs base path of this device
330 */
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500331 const fs::path& path() const override
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500332 {
333 return basePath;
334 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500335
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500336 /**
337 * Replaces the 'P' in the string passed in with
338 * the page number passed in.
339 *
340 * For example:
341 * insertPageNum("inP_enable", 42)
342 * returns "in42_enable"
343 *
344 * @param[in] templateName - the name string, with a 'P' in it
345 * @param[in] page - the page number to insert where the P was
346 *
347 * @return string - the new string with the page number in it
348 */
349 static std::string insertPageNum(const std::string& templateName,
350 size_t page);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500351
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500352 /**
353 * Finds the path relative to basePath to the hwmon directory
354 * for the device and stores it in hwmonRelPath.
355 */
Brandon Wyman9564e942020-11-10 14:01:42 -0600356 void findHwmonDir() override;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500357
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500358 /**
359 * Returns the path to use for the passed in type.
360 *
361 * @param[in] type - Path type
362 *
363 * @return fs::path - the full path
364 */
365 fs::path getPath(Type type);
Brandon Wymanff5f3392017-08-11 17:43:22 -0500366
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500367 private:
368 /**
369 * Returns the device name
370 *
371 * This is found in the 'name' file in basePath.
372 *
373 * @return string - the device name
374 */
375 std::string getDeviceName();
Matt Spinler57868bc2017-08-03 10:07:41 -0500376
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500377 /**
378 * The sysfs device path
379 */
380 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500381
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500382 /**
383 * The directory name under the basePath hwmon directory
384 */
385 fs::path hwmonDir;
Matt Spinlerba053482018-01-04 14:26:05 -0600386
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500387 /**
388 * The device driver name. Used for finding the device
389 * debug directory. Not required if that directory
390 * isn't used.
391 */
392 std::string driverName;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500393
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500394 /**
395 * The device instance number.
396 *
397 * Used in conjunction with the driver name for finding
398 * the debug directory. Not required if that directory
399 * isn't used.
400 */
401 size_t instance = 0;
Brandon Wymanff5f3392017-08-11 17:43:22 -0500402
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500403 /**
404 * The pmbus debug path with status files
405 */
406 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler015e3ad2017-08-01 11:20:47 -0500407};
408
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500409} // namespace pmbus
Lei YUab093322019-10-09 16:43:22 +0800410} // namespace phosphor