blob: 1ca6536b2c18b305c673cc16546add908125af04 [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
Brandon Wyman10295542017-08-09 18:20:44 -050014// The file name Linux uses to capture the STATUS_WORD from pmbus.
Matt Spinlere7e432b2017-08-21 15:01:40 -050015constexpr auto STATUS_WORD = "status0";
Brandon Wyman10295542017-08-09 18:20:44 -050016
Brandon Wyman253dc9b2017-08-12 13:45:52 -050017// The file name Linux uses to capture the STATUS_INPUT from pmbus.
18constexpr auto STATUS_INPUT = "status0_input";
Matt Spinlere7e432b2017-08-21 15:01:40 -050019
Brandon Wyman764c7972017-08-22 17:05:36 -050020// Voltage out status.
21// Overvoltage fault or warning, Undervoltage fault or warning, maximum or
22// minimum warning, ....
Matt Spinlere7e432b2017-08-21 15:01:40 -050023// Uses Page substitution
24constexpr auto STATUS_VOUT = "statusP_vout";
25
Matt Spinlerde16d052017-12-13 13:22:14 -060026namespace status_vout
27{
28// Mask of bits that are only warnings
29constexpr auto WARNING_MASK = 0x6A;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050030} // namespace status_vout
Matt Spinlerde16d052017-12-13 13:22:14 -060031
Brandon Wyman764c7972017-08-22 17:05:36 -050032// Current output status bits.
33constexpr auto STATUS_IOUT = "status0_iout";
34
35// Manufacturing specific status bits
36constexpr auto STATUS_MFR = "status0_mfr";
37
Brandon Wyman12661f12017-08-31 15:28:21 -050038// Reports on the status of any fans installed in position 1 and 2.
39constexpr auto STATUS_FANS_1_2 = "status0_fans12";
40
41// Reports on temperature faults or warnings. Overtemperature fault,
42// overtemperature warning, undertemperature warning, undertemperature fault.
43constexpr auto STATUS_TEMPERATURE = "status0_temp";
44
Matt Spinlere7e432b2017-08-21 15:01:40 -050045namespace status_word
46{
47constexpr auto VOUT_FAULT = 0x8000;
Brandon Wyman764c7972017-08-22 17:05:36 -050048
49// The IBM CFF power supply driver does map this bit to power1_alarm in the
50// hwmon space, but since the other bits that need to be checked do not have
51// a similar mapping, the code will just read STATUS_WORD and use bit masking
52// to see if the INPUT FAULT OR WARNING bit is on.
53constexpr auto INPUT_FAULT_WARN = 0x2000;
54
Brandon Wyman3f1242f2020-01-28 13:11:25 -060055// The bit mask representing the MFRSPECIFIC fault, bit 4 of STATUS_WORD high
56// byte. A manufacturer specific fault or warning has occurred.
57constexpr auto MFR_SPECIFIC_FAULT = 0x1000;
58
Brandon Wyman764c7972017-08-22 17:05:36 -050059// The bit mask representing the POWER_GOOD Negated bit of the STATUS_WORD.
60constexpr auto POWER_GOOD_NEGATED = 0x0800;
61
Brandon Wyman12661f12017-08-31 15:28:21 -050062// The bit mask representing the FAN FAULT or WARNING bit of the STATUS_WORD.
63// Bit 2 of the high byte of STATUS_WORD.
64constexpr auto FAN_FAULT = 0x0400;
65
Brandon Wyman764c7972017-08-22 17:05:36 -050066// The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD.
67constexpr auto UNIT_IS_OFF = 0x0040;
68
Brandon Wymanab05c072017-08-30 18:26:41 -050069// Bit 5 of the STATUS_BYTE, or lower byte of STATUS_WORD is used to indicate
70// an output overvoltage fault.
71constexpr auto VOUT_OV_FAULT = 0x0020;
72
Brandon Wymanb165c252017-08-25 18:59:54 -050073// The bit mask representing that an output overcurrent fault has occurred.
74constexpr auto IOUT_OC_FAULT = 0x0010;
75
Brandon Wyman764c7972017-08-22 17:05:36 -050076// The IBM CFF power supply driver does map this bit to in1_alarm, however,
77// since a number of the other bits are not mapped that way for STATUS_WORD,
78// this code will just read the entire STATUS_WORD and use bit masking to find
79// out if that fault is on.
80constexpr auto VIN_UV_FAULT = 0x0008;
81
Brandon Wyman875b3632017-09-13 18:46:03 -050082// The bit mask representing the TEMPERATURE FAULT or WARNING bit of the
83// STATUS_WORD. Bit 2 of the low byte (STATUS_BYTE).
84constexpr auto TEMPERATURE_FAULT_WARN = 0x0004;
85
Matt Spinlerf0f02b92018-10-25 16:12:43 -050086} // namespace status_word
Brandon Wyman875b3632017-09-13 18:46:03 -050087
Lei YUe8c9cd62019-11-04 14:24:41 +080088namespace status_vout
89{
90// The IBM CFF power supply driver maps MFR's OV_FAULT and VAUX_FAULT to this
91// bit.
92constexpr auto OV_FAULT = 0x80;
93
94// The IBM CFF power supply driver maps MFR's UV_FAULT to this bit.
95constexpr auto UV_FAULT = 0x10;
96} // namespace status_vout
97
Brandon Wyman875b3632017-09-13 18:46:03 -050098namespace status_temperature
99{
100// Overtemperature Fault
101constexpr auto OT_FAULT = 0x80;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500102} // namespace status_temperature
Matt Spinlere7e432b2017-08-21 15:01:40 -0500103
Brandon Wyman59a35792020-06-04 12:37:40 -0500104constexpr auto ON_OFF_CONFIG = "on_off_config";
105
106// From PMBus Specification Part II Revsion 1.2:
107// The ON_OFF_CONFIG command configures the combination of CONTROL pin input
108// and serial bus commands needed to turn the unit on and off. This includes how
109// the unit responds when power is applied.
110// Bits [7:5] - 000 - Reserved
111// Bit 4 - 1 - Unit does not power up until commanded by the CONTROL pin and
112// OPERATION command (as programmed in bits [3:0]).
113// Bit 3 - 0 - Unit ignores the on/off portion of the OPERATION command from
114// serial bus.
115// Bit 2 - 1 - Unit requires the CONTROL pin to be asserted to start the unit.
116// Bit 1 - 0 - Polarity of the CONTROL pin. Active low (Pull pin low to start
117// the unit).
118// Bit 0 - 1 - Turn off the output and stop transferring energy to the output as
119// fast as possible.
120constexpr auto ON_OFF_CONFIG_CONTROL_PIN_ONLY = 0x15;
121
Matt Spinler015e3ad2017-08-01 11:20:47 -0500122/**
Matt Spinler4dc46782018-01-04 14:29:16 -0600123 * Where the access should be done
Matt Spinler57868bc2017-08-03 10:07:41 -0500124 */
125enum class Type
126{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500127 Base, // base device directory
128 Hwmon, // hwmon directory
129 Debug, // pmbus debug directory
130 DeviceDebug, // device debug directory
131 HwmonDeviceDebug // hwmon device debug directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500132};
133
134/**
Brandon Wyman8d195772020-01-27 15:03:51 -0600135 * @class PMBusBase
136 *
137 * This is a base class for PMBus to assist with unit testing via mocking.
138 */
139class PMBusBase
140{
141 public:
142 virtual ~PMBusBase() = default;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600143
144 virtual uint64_t read(const std::string& name, Type type) = 0;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500145 virtual std::string readString(const std::string& name, Type type) = 0;
Brandon Wyman59a35792020-06-04 12:37:40 -0500146 virtual void writeBinary(const std::string& name, std::vector<uint8_t> data,
147 Type type) = 0;
Brandon Wyman8d195772020-01-27 15:03:51 -0600148};
149
150/**
151 * Wrapper function for PMBus
152 *
153 * @param[in] bus - I2C bus
154 * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
155 *
156 * @return PMBusBase pointer
157 */
158std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
159 const std::string& address);
160
161/**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500162 * @class PMBus
163 *
164 * This class is an interface to communicating with PMBus devices
165 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -0500166 *
167 * Based on the Type parameter, the accesses can either be done
168 * in the base device directory (the one passed into the constructor),
169 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -0500170 */
Brandon Wyman8d195772020-01-27 15:03:51 -0600171class PMBus : public PMBusBase
Matt Spinler015e3ad2017-08-01 11:20:47 -0500172{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500173 public:
174 PMBus() = delete;
Brandon Wyman8d195772020-01-27 15:03:51 -0600175 virtual ~PMBus() = default;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500176 PMBus(const PMBus&) = default;
177 PMBus& operator=(const PMBus&) = default;
178 PMBus(PMBus&&) = default;
179 PMBus& operator=(PMBus&&) = default;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500180
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500181 /**
182 * Constructor
183 *
184 * @param[in] path - path to the sysfs directory
185 */
186 PMBus(const std::string& path) : basePath(path)
187 {
188 findHwmonDir();
189 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500190
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500191 /**
192 * Constructor
193 *
194 * This version is required when DeviceDebug
195 * access will be used.
196 *
197 * @param[in] path - path to the sysfs directory
198 * @param[in] driverName - the device driver name
199 * @param[in] instance - chip instance number
200 */
201 PMBus(const std::string& path, const std::string& driverName,
202 size_t instance) :
203 basePath(path),
204 driverName(driverName), instance(instance)
205 {
206 findHwmonDir();
207 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500208
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500209 /**
Brandon Wyman8d195772020-01-27 15:03:51 -0600210 * Wrapper function for PMBus
211 *
212 * @param[in] bus - I2C bus
213 * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
214 *
215 * @return PMBusBase pointer
216 */
217 static std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
218 const std::string& address);
219
220 /**
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500221 * Reads a file in sysfs that represents a single bit,
222 * therefore doing a PMBus read.
223 *
224 * @param[in] name - path concatenated to
225 * basePath to read
226 * @param[in] type - Path type
227 *
228 * @return bool - false if result was 0, else true
229 */
230 bool readBit(const std::string& name, Type type);
Matt Spinler8f0d9532017-08-21 11:22:37 -0500231
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500232 /**
233 * Reads a file in sysfs that represents a single bit,
234 * where the page number passed in is substituted
235 * into the name in place of the 'P' character in it.
236 *
237 * @param[in] name - path concatenated to
238 * basePath to read
239 * @param[in] page - page number
240 * @param[in] type - Path type
241 *
242 * @return bool - false if result was 0, else true
243 */
244 bool readBitInPage(const std::string& name, size_t page, Type type);
245 /**
246 * Checks if the file for the given name and type exists.
247 *
248 * @param[in] name - path concatenated to basePath to read
249 * @param[in] type - Path type
250 *
251 * @return bool - True if file exists, false if it does not.
252 */
253 bool exists(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500254
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500255 /**
256 * Read byte(s) from file in sysfs.
257 *
258 * @param[in] name - path concatenated to basePath to read
259 * @param[in] type - Path type
260 *
261 * @return uint64_t - Up to 8 bytes of data read from file.
262 */
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600263 uint64_t read(const std::string& name, Type type) override;
Brandon Wyman3b7b38b2017-09-25 16:43:45 -0500264
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500265 /**
266 * Read a string from file in sysfs.
267 *
268 * @param[in] name - path concatenated to basePath to read
269 * @param[in] type - Path type
270 *
271 * @return string - The data read from the file.
272 */
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500273 std::string readString(const std::string& name, Type type) override;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500274
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500275 /**
276 * Read data from a binary file in sysfs.
277 *
278 * @param[in] name - path concatenated to basePath to read
279 * @param[in] type - Path type
280 * @param[in] length - length of data to read, in bytes
281 *
282 * @return vector<uint8_t> - The data read from the file.
283 */
284 std::vector<uint8_t> readBinary(const std::string& name, Type type,
285 size_t length);
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600286
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500287 /**
288 * Writes an integer value to the file, therefore doing
289 * a PMBus write.
290 *
291 * @param[in] name - path concatenated to
292 * basePath to write
293 * @param[in] value - the value to write
294 * @param[in] type - Path type
295 */
296 void write(const std::string& name, int value, Type type);
Matt Spinlerfa23e332018-01-18 11:24:58 -0600297
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500298 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500299 * Writes binary data to a file in sysfs.
300 *
301 * @param[in] name - path concatenated to basePath to write
302 * @param[in] data - The data to write to the file
303 * @param[in] type - Path type
304 */
305 void writeBinary(const std::string& name, std::vector<uint8_t> data,
306 Type type) override;
307
308 /**
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500309 * Returns the sysfs base path of this device
310 */
311 inline const auto& path() const
312 {
313 return basePath;
314 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500315
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500316 /**
317 * Replaces the 'P' in the string passed in with
318 * the page number passed in.
319 *
320 * For example:
321 * insertPageNum("inP_enable", 42)
322 * returns "in42_enable"
323 *
324 * @param[in] templateName - the name string, with a 'P' in it
325 * @param[in] page - the page number to insert where the P was
326 *
327 * @return string - the new string with the page number in it
328 */
329 static std::string insertPageNum(const std::string& templateName,
330 size_t page);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500331
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500332 /**
333 * Finds the path relative to basePath to the hwmon directory
334 * for the device and stores it in hwmonRelPath.
335 */
336 void findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -0500337
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500338 /**
339 * Returns the path to use for the passed in type.
340 *
341 * @param[in] type - Path type
342 *
343 * @return fs::path - the full path
344 */
345 fs::path getPath(Type type);
Brandon Wymanff5f3392017-08-11 17:43:22 -0500346
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500347 private:
348 /**
349 * Returns the device name
350 *
351 * This is found in the 'name' file in basePath.
352 *
353 * @return string - the device name
354 */
355 std::string getDeviceName();
Matt Spinler57868bc2017-08-03 10:07:41 -0500356
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500357 /**
358 * The sysfs device path
359 */
360 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500361
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500362 /**
363 * The directory name under the basePath hwmon directory
364 */
365 fs::path hwmonDir;
Matt Spinlerba053482018-01-04 14:26:05 -0600366
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500367 /**
368 * The device driver name. Used for finding the device
369 * debug directory. Not required if that directory
370 * isn't used.
371 */
372 std::string driverName;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500373
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500374 /**
375 * The device instance number.
376 *
377 * Used in conjunction with the driver name for finding
378 * the debug directory. Not required if that directory
379 * isn't used.
380 */
381 size_t instance = 0;
Brandon Wymanff5f3392017-08-11 17:43:22 -0500382
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500383 /**
384 * The pmbus debug path with status files
385 */
386 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler015e3ad2017-08-01 11:20:47 -0500387};
388
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500389} // namespace pmbus
Lei YUab093322019-10-09 16:43:22 +0800390} // namespace phosphor