xref: /openbmc/phosphor-power/pmbus.hpp (revision 415094c1)
1 #pragma once
2 
3 #include <filesystem>
4 #include <string>
5 #include <vector>
6 
7 namespace phosphor
8 {
9 namespace pmbus
10 {
11 
12 namespace fs = std::filesystem;
13 
14 // The file name Linux uses to capture the STATUS_WORD from pmbus.
15 constexpr auto STATUS_WORD = "status0";
16 
17 // The file name Linux uses to capture the STATUS_INPUT from pmbus.
18 constexpr auto STATUS_INPUT = "status0_input";
19 
20 // Voltage out status.
21 // Overvoltage fault or warning, Undervoltage fault or warning, maximum or
22 // minimum warning, ....
23 // Uses Page substitution
24 constexpr auto STATUS_VOUT = "statusP_vout";
25 
26 namespace status_vout
27 {
28 // Mask of bits that are only warnings
29 constexpr auto WARNING_MASK = 0x6A;
30 } // namespace status_vout
31 
32 // Current output status bits.
33 constexpr auto STATUS_IOUT = "status0_iout";
34 
35 // Manufacturing specific status bits
36 constexpr auto STATUS_MFR = "status0_mfr";
37 
38 // Reports on the status of any fans installed in position 1 and 2.
39 constexpr auto STATUS_FANS_1_2 = "status0_fans12";
40 
41 // Reports on temperature faults or warnings. Overtemperature fault,
42 // overtemperature warning, undertemperature warning, undertemperature fault.
43 constexpr auto STATUS_TEMPERATURE = "status0_temp";
44 
45 namespace status_word
46 {
47 constexpr auto VOUT_FAULT = 0x8000;
48 
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.
53 constexpr auto INPUT_FAULT_WARN = 0x2000;
54 
55 // The bit mask representing the MFRSPECIFIC fault, bit 4 of STATUS_WORD high
56 // byte. A manufacturer specific fault or warning has occurred.
57 constexpr auto MFR_SPECIFIC_FAULT = 0x1000;
58 
59 // The bit mask representing the POWER_GOOD Negated bit of the STATUS_WORD.
60 constexpr auto POWER_GOOD_NEGATED = 0x0800;
61 
62 // The bit mask representing the FAN FAULT or WARNING bit of the STATUS_WORD.
63 // Bit 2 of the high byte of STATUS_WORD.
64 constexpr auto FAN_FAULT = 0x0400;
65 
66 // The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD.
67 constexpr auto UNIT_IS_OFF = 0x0040;
68 
69 // Bit 5 of the STATUS_BYTE, or lower byte of STATUS_WORD is used to indicate
70 // an output overvoltage fault.
71 constexpr auto VOUT_OV_FAULT = 0x0020;
72 
73 // The bit mask representing that an output overcurrent fault has occurred.
74 constexpr auto IOUT_OC_FAULT = 0x0010;
75 
76 // 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.
80 constexpr auto VIN_UV_FAULT = 0x0008;
81 
82 // The bit mask representing the TEMPERATURE FAULT or WARNING bit of the
83 // STATUS_WORD. Bit 2 of the low byte (STATUS_BYTE).
84 constexpr auto TEMPERATURE_FAULT_WARN = 0x0004;
85 
86 } // namespace status_word
87 
88 namespace status_vout
89 {
90 // The IBM CFF power supply driver maps MFR's OV_FAULT and VAUX_FAULT to this
91 // bit.
92 constexpr auto OV_FAULT = 0x80;
93 
94 // The IBM CFF power supply driver maps MFR's UV_FAULT to this bit.
95 constexpr auto UV_FAULT = 0x10;
96 } // namespace status_vout
97 
98 namespace status_temperature
99 {
100 // Overtemperature Fault
101 constexpr auto OT_FAULT = 0x80;
102 } // namespace status_temperature
103 
104 constexpr 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.
120 constexpr auto ON_OFF_CONFIG_CONTROL_PIN_ONLY = 0x15;
121 
122 /**
123  * Where the access should be done
124  */
125 enum class Type
126 {
127     Base,            // base device directory
128     Hwmon,           // hwmon directory
129     Debug,           // pmbus debug directory
130     DeviceDebug,     // device debug directory
131     HwmonDeviceDebug // hwmon device debug directory
132 };
133 
134 /**
135  * @class PMBusBase
136  *
137  * This is a base class for PMBus to assist with unit testing via mocking.
138  */
139 class PMBusBase
140 {
141   public:
142     virtual ~PMBusBase() = default;
143 
144     virtual uint64_t read(const std::string& name, Type type) = 0;
145     virtual std::string readString(const std::string& name, Type type) = 0;
146     virtual void writeBinary(const std::string& name, std::vector<uint8_t> data,
147                              Type type) = 0;
148     virtual void findHwmonDir() = 0;
149     virtual const fs::path& path() const = 0;
150 };
151 
152 /**
153  * Wrapper function for PMBus
154  *
155  * @param[in] bus - I2C bus
156  * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
157  *
158  * @return PMBusBase pointer
159  */
160 std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
161                                        const std::string& address);
162 
163 /**
164  * @class PMBus
165  *
166  * This class is an interface to communicating with PMBus devices
167  * by reading and writing sysfs files.
168  *
169  * Based on the Type parameter, the accesses can either be done
170  * in the base device directory (the one passed into the constructor),
171  * or in the hwmon directory for the device.
172  */
173 class PMBus : public PMBusBase
174 {
175   public:
176     PMBus() = delete;
177     virtual ~PMBus() = default;
178     PMBus(const PMBus&) = default;
179     PMBus& operator=(const PMBus&) = default;
180     PMBus(PMBus&&) = default;
181     PMBus& operator=(PMBus&&) = default;
182 
183     /**
184      * Constructor
185      *
186      * @param[in] path - path to the sysfs directory
187      */
188     PMBus(const std::string& path) : basePath(path)
189     {
190         findHwmonDir();
191     }
192 
193     /**
194      * Constructor
195      *
196      * This version is required when DeviceDebug
197      * access will be used.
198      *
199      * @param[in] path - path to the sysfs directory
200      * @param[in] driverName - the device driver name
201      * @param[in] instance - chip instance number
202      */
203     PMBus(const std::string& path, const std::string& driverName,
204           size_t instance) :
205         basePath(path),
206         driverName(driverName), instance(instance)
207     {
208         findHwmonDir();
209     }
210 
211     /**
212      * Wrapper function for PMBus
213      *
214      * @param[in] bus - I2C bus
215      * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
216      *
217      * @return PMBusBase pointer
218      */
219     static std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
220                                                   const std::string& address);
221 
222     /**
223      * Reads a file in sysfs that represents a single bit,
224      * therefore doing a PMBus read.
225      *
226      * @param[in] name - path concatenated to
227      *                   basePath to read
228      * @param[in] type - Path type
229      *
230      * @return bool - false if result was 0, else true
231      */
232     bool readBit(const std::string& name, Type type);
233 
234     /**
235      * Reads a file in sysfs that represents a single bit,
236      * where the page number passed in is substituted
237      * into the name in place of the 'P' character in it.
238      *
239      * @param[in] name - path concatenated to
240      *                   basePath to read
241      * @param[in] page - page number
242      * @param[in] type - Path type
243      *
244      * @return bool - false if result was 0, else true
245      */
246     bool readBitInPage(const std::string& name, size_t page, Type type);
247     /**
248      * Checks if the file for the given name and type exists.
249      *
250      * @param[in] name   - path concatenated to basePath to read
251      * @param[in] type   - Path type
252      *
253      * @return bool - True if file exists, false if it does not.
254      */
255     bool exists(const std::string& name, Type type);
256 
257     /**
258      * Read byte(s) from file in sysfs.
259      *
260      * @param[in] name   - path concatenated to basePath to read
261      * @param[in] type   - Path type
262      *
263      * @return uint64_t - Up to 8 bytes of data read from file.
264      */
265     uint64_t read(const std::string& name, Type type) override;
266 
267     /**
268      * Read a string from file in sysfs.
269      *
270      * @param[in] name   - path concatenated to basePath to read
271      * @param[in] type   - Path type
272      *
273      * @return string - The data read from the file.
274      */
275     std::string readString(const std::string& name, Type type) override;
276 
277     /**
278      * Read data from a binary file in sysfs.
279      *
280      * @param[in] name   - path concatenated to basePath to read
281      * @param[in] type   - Path type
282      * @param[in] length - length of data to read, in bytes
283      *
284      * @return vector<uint8_t> - The data read from the file.
285      */
286     std::vector<uint8_t> readBinary(const std::string& name, Type type,
287                                     size_t length);
288 
289     /**
290      * Writes an integer value to the file, therefore doing
291      * a PMBus write.
292      *
293      * @param[in] name - path concatenated to
294      *                   basePath to write
295      * @param[in] value - the value to write
296      * @param[in] type - Path type
297      */
298     void write(const std::string& name, int value, Type type);
299 
300     /**
301      * Writes binary data to a file in sysfs.
302      *
303      * @param[in] name - path concatenated to basePath to write
304      * @param[in] data - The data to write to the file
305      * @param[in] type - Path type
306      */
307     void writeBinary(const std::string& name, std::vector<uint8_t> data,
308                      Type type) override;
309 
310     /**
311      * Returns the sysfs base path of this device
312      */
313     const fs::path& path() const override
314     {
315         return basePath;
316     }
317 
318     /**
319      * Replaces the 'P' in the string passed in with
320      * the page number passed in.
321      *
322      * For example:
323      *   insertPageNum("inP_enable", 42)
324      *   returns "in42_enable"
325      *
326      * @param[in] templateName - the name string, with a 'P' in it
327      * @param[in] page - the page number to insert where the P was
328      *
329      * @return string - the new string with the page number in it
330      */
331     static std::string insertPageNum(const std::string& templateName,
332                                      size_t page);
333 
334     /**
335      * Finds the path relative to basePath to the hwmon directory
336      * for the device and stores it in hwmonRelPath.
337      */
338     void findHwmonDir() override;
339 
340     /**
341      * Returns the path to use for the passed in type.
342      *
343      * @param[in] type - Path type
344      *
345      * @return fs::path - the full path
346      */
347     fs::path getPath(Type type);
348 
349   private:
350     /**
351      * Returns the device name
352      *
353      * This is found in the 'name' file in basePath.
354      *
355      * @return string - the device name
356      */
357     std::string getDeviceName();
358 
359     /**
360      * The sysfs device path
361      */
362     fs::path basePath;
363 
364     /**
365      * The directory name under the basePath hwmon directory
366      */
367     fs::path hwmonDir;
368 
369     /**
370      * The device driver name.  Used for finding the device
371      * debug directory.  Not required if that directory
372      * isn't used.
373      */
374     std::string driverName;
375 
376     /**
377      * The device instance number.
378      *
379      * Used in conjunction with the driver name for finding
380      * the debug directory.  Not required if that directory
381      * isn't used.
382      */
383     size_t instance = 0;
384 
385     /**
386      * The pmbus debug path with status files
387      */
388     const fs::path debugPath = "/sys/kernel/debug/";
389 };
390 
391 } // namespace pmbus
392 } // namespace phosphor
393