xref: /openbmc/phosphor-power/pmbus.hpp (revision 8d195771)
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 POWER_GOOD Negated bit of the STATUS_WORD.
56 constexpr auto POWER_GOOD_NEGATED = 0x0800;
57 
58 // The bit mask representing the FAN FAULT or WARNING bit of the STATUS_WORD.
59 // Bit 2 of the high byte of STATUS_WORD.
60 constexpr auto FAN_FAULT = 0x0400;
61 
62 // The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD.
63 constexpr auto UNIT_IS_OFF = 0x0040;
64 
65 // Bit 5 of the STATUS_BYTE, or lower byte of STATUS_WORD is used to indicate
66 // an output overvoltage fault.
67 constexpr auto VOUT_OV_FAULT = 0x0020;
68 
69 // The bit mask representing that an output overcurrent fault has occurred.
70 constexpr auto IOUT_OC_FAULT = 0x0010;
71 
72 // The IBM CFF power supply driver does map this bit to in1_alarm, however,
73 // since a number of the other bits are not mapped that way for STATUS_WORD,
74 // this code will just read the entire STATUS_WORD and use bit masking to find
75 // out if that fault is on.
76 constexpr auto VIN_UV_FAULT = 0x0008;
77 
78 // The bit mask representing the TEMPERATURE FAULT or WARNING bit of the
79 // STATUS_WORD. Bit 2 of the low byte (STATUS_BYTE).
80 constexpr auto TEMPERATURE_FAULT_WARN = 0x0004;
81 
82 } // namespace status_word
83 
84 namespace status_vout
85 {
86 // The IBM CFF power supply driver maps MFR's OV_FAULT and VAUX_FAULT to this
87 // bit.
88 constexpr auto OV_FAULT = 0x80;
89 
90 // The IBM CFF power supply driver maps MFR's UV_FAULT to this bit.
91 constexpr auto UV_FAULT = 0x10;
92 } // namespace status_vout
93 
94 namespace status_temperature
95 {
96 // Overtemperature Fault
97 constexpr auto OT_FAULT = 0x80;
98 } // namespace status_temperature
99 
100 /**
101  * Where the access should be done
102  */
103 enum class Type
104 {
105     Base,            // base device directory
106     Hwmon,           // hwmon directory
107     Debug,           // pmbus debug directory
108     DeviceDebug,     // device debug directory
109     HwmonDeviceDebug // hwmon device debug directory
110 };
111 
112 /**
113  * @class PMBusBase
114  *
115  * This is a base class for PMBus to assist with unit testing via mocking.
116  */
117 class PMBusBase
118 {
119   public:
120     virtual ~PMBusBase() = default;
121 };
122 
123 /**
124  * Wrapper function for PMBus
125  *
126  * @param[in] bus - I2C bus
127  * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
128  *
129  * @return PMBusBase pointer
130  */
131 std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
132                                        const std::string& address);
133 
134 /**
135  * @class PMBus
136  *
137  * This class is an interface to communicating with PMBus devices
138  * by reading and writing sysfs files.
139  *
140  * Based on the Type parameter, the accesses can either be done
141  * in the base device directory (the one passed into the constructor),
142  * or in the hwmon directory for the device.
143  */
144 class PMBus : public PMBusBase
145 {
146   public:
147     PMBus() = delete;
148     virtual ~PMBus() = default;
149     PMBus(const PMBus&) = default;
150     PMBus& operator=(const PMBus&) = default;
151     PMBus(PMBus&&) = default;
152     PMBus& operator=(PMBus&&) = default;
153 
154     /**
155      * Constructor
156      *
157      * @param[in] path - path to the sysfs directory
158      */
159     PMBus(const std::string& path) : basePath(path)
160     {
161         findHwmonDir();
162     }
163 
164     /**
165      * Constructor
166      *
167      * This version is required when DeviceDebug
168      * access will be used.
169      *
170      * @param[in] path - path to the sysfs directory
171      * @param[in] driverName - the device driver name
172      * @param[in] instance - chip instance number
173      */
174     PMBus(const std::string& path, const std::string& driverName,
175           size_t instance) :
176         basePath(path),
177         driverName(driverName), instance(instance)
178     {
179         findHwmonDir();
180     }
181 
182     /**
183      * Wrapper function for PMBus
184      *
185      * @param[in] bus - I2C bus
186      * @param[in] address - I2C address (as a 2-byte string, e.g. 0069)
187      *
188      * @return PMBusBase pointer
189      */
190     static std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
191                                                   const std::string& address);
192 
193     /**
194      * Reads a file in sysfs that represents a single bit,
195      * therefore doing a PMBus read.
196      *
197      * @param[in] name - path concatenated to
198      *                   basePath to read
199      * @param[in] type - Path type
200      *
201      * @return bool - false if result was 0, else true
202      */
203     bool readBit(const std::string& name, Type type);
204 
205     /**
206      * Reads a file in sysfs that represents a single bit,
207      * where the page number passed in is substituted
208      * into the name in place of the 'P' character in it.
209      *
210      * @param[in] name - path concatenated to
211      *                   basePath to read
212      * @param[in] page - page number
213      * @param[in] type - Path type
214      *
215      * @return bool - false if result was 0, else true
216      */
217     bool readBitInPage(const std::string& name, size_t page, Type type);
218     /**
219      * Checks if the file for the given name and type exists.
220      *
221      * @param[in] name   - path concatenated to basePath to read
222      * @param[in] type   - Path type
223      *
224      * @return bool - True if file exists, false if it does not.
225      */
226     bool exists(const std::string& name, Type type);
227 
228     /**
229      * Read byte(s) from file in sysfs.
230      *
231      * @param[in] name   - path concatenated to basePath to read
232      * @param[in] type   - Path type
233      *
234      * @return uint64_t - Up to 8 bytes of data read from file.
235      */
236     uint64_t read(const std::string& name, Type type);
237 
238     /**
239      * Read a string from file in sysfs.
240      *
241      * @param[in] name   - path concatenated to basePath to read
242      * @param[in] type   - Path type
243      *
244      * @return string - The data read from the file.
245      */
246     std::string readString(const std::string& name, Type type);
247 
248     /**
249      * Read data from a binary file in sysfs.
250      *
251      * @param[in] name   - path concatenated to basePath to read
252      * @param[in] type   - Path type
253      * @param[in] length - length of data to read, in bytes
254      *
255      * @return vector<uint8_t> - The data read from the file.
256      */
257     std::vector<uint8_t> readBinary(const std::string& name, Type type,
258                                     size_t length);
259 
260     /**
261      * Writes an integer value to the file, therefore doing
262      * a PMBus write.
263      *
264      * @param[in] name - path concatenated to
265      *                   basePath to write
266      * @param[in] value - the value to write
267      * @param[in] type - Path type
268      */
269     void write(const std::string& name, int value, Type type);
270 
271     /**
272      * Returns the sysfs base path of this device
273      */
274     inline const auto& path() const
275     {
276         return basePath;
277     }
278 
279     /**
280      * Replaces the 'P' in the string passed in with
281      * the page number passed in.
282      *
283      * For example:
284      *   insertPageNum("inP_enable", 42)
285      *   returns "in42_enable"
286      *
287      * @param[in] templateName - the name string, with a 'P' in it
288      * @param[in] page - the page number to insert where the P was
289      *
290      * @return string - the new string with the page number in it
291      */
292     static std::string insertPageNum(const std::string& templateName,
293                                      size_t page);
294 
295     /**
296      * Finds the path relative to basePath to the hwmon directory
297      * for the device and stores it in hwmonRelPath.
298      */
299     void findHwmonDir();
300 
301     /**
302      * Returns the path to use for the passed in type.
303      *
304      * @param[in] type - Path type
305      *
306      * @return fs::path - the full path
307      */
308     fs::path getPath(Type type);
309 
310   private:
311     /**
312      * Returns the device name
313      *
314      * This is found in the 'name' file in basePath.
315      *
316      * @return string - the device name
317      */
318     std::string getDeviceName();
319 
320     /**
321      * The sysfs device path
322      */
323     fs::path basePath;
324 
325     /**
326      * The directory name under the basePath hwmon directory
327      */
328     fs::path hwmonDir;
329 
330     /**
331      * The device driver name.  Used for finding the device
332      * debug directory.  Not required if that directory
333      * isn't used.
334      */
335     std::string driverName;
336 
337     /**
338      * The device instance number.
339      *
340      * Used in conjunction with the driver name for finding
341      * the debug directory.  Not required if that directory
342      * isn't used.
343      */
344     size_t instance = 0;
345 
346     /**
347      * The pmbus debug path with status files
348      */
349     const fs::path debugPath = "/sys/kernel/debug/";
350 };
351 
352 } // namespace pmbus
353 } // namespace phosphor
354