blob: 8f5d914758b490b649e5ca5fa0e20d39e358b583 [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
55// The bit mask representing the POWER_GOOD Negated bit of the STATUS_WORD.
56constexpr auto POWER_GOOD_NEGATED = 0x0800;
57
Brandon Wyman12661f12017-08-31 15:28:21 -050058// The bit mask representing the FAN FAULT or WARNING bit of the STATUS_WORD.
59// Bit 2 of the high byte of STATUS_WORD.
60constexpr auto FAN_FAULT = 0x0400;
61
Brandon Wyman764c7972017-08-22 17:05:36 -050062// The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD.
63constexpr auto UNIT_IS_OFF = 0x0040;
64
Brandon Wymanab05c072017-08-30 18:26:41 -050065// Bit 5 of the STATUS_BYTE, or lower byte of STATUS_WORD is used to indicate
66// an output overvoltage fault.
67constexpr auto VOUT_OV_FAULT = 0x0020;
68
Brandon Wymanb165c252017-08-25 18:59:54 -050069// The bit mask representing that an output overcurrent fault has occurred.
70constexpr auto IOUT_OC_FAULT = 0x0010;
71
Brandon Wyman764c7972017-08-22 17:05:36 -050072// 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.
76constexpr auto VIN_UV_FAULT = 0x0008;
77
Brandon Wyman875b3632017-09-13 18:46:03 -050078// The bit mask representing the TEMPERATURE FAULT or WARNING bit of the
79// STATUS_WORD. Bit 2 of the low byte (STATUS_BYTE).
80constexpr auto TEMPERATURE_FAULT_WARN = 0x0004;
81
Matt Spinlerf0f02b92018-10-25 16:12:43 -050082} // namespace status_word
Brandon Wyman875b3632017-09-13 18:46:03 -050083
84namespace status_temperature
85{
86// Overtemperature Fault
87constexpr auto OT_FAULT = 0x80;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050088} // namespace status_temperature
Matt Spinlere7e432b2017-08-21 15:01:40 -050089
Matt Spinler015e3ad2017-08-01 11:20:47 -050090/**
Matt Spinler4dc46782018-01-04 14:29:16 -060091 * Where the access should be done
Matt Spinler57868bc2017-08-03 10:07:41 -050092 */
93enum class Type
94{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050095 Base, // base device directory
96 Hwmon, // hwmon directory
97 Debug, // pmbus debug directory
98 DeviceDebug, // device debug directory
99 HwmonDeviceDebug // hwmon device debug directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500100};
101
102/**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500103 * @class PMBus
104 *
105 * This class is an interface to communicating with PMBus devices
106 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -0500107 *
108 * Based on the Type parameter, the accesses can either be done
109 * in the base device directory (the one passed into the constructor),
110 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -0500111 */
112class PMBus
113{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500114 public:
115 PMBus() = delete;
116 ~PMBus() = default;
117 PMBus(const PMBus&) = default;
118 PMBus& operator=(const PMBus&) = default;
119 PMBus(PMBus&&) = default;
120 PMBus& operator=(PMBus&&) = default;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500121
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500122 /**
123 * Constructor
124 *
125 * @param[in] path - path to the sysfs directory
126 */
127 PMBus(const std::string& path) : basePath(path)
128 {
129 findHwmonDir();
130 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500131
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500132 /**
133 * Constructor
134 *
135 * This version is required when DeviceDebug
136 * access will be used.
137 *
138 * @param[in] path - path to the sysfs directory
139 * @param[in] driverName - the device driver name
140 * @param[in] instance - chip instance number
141 */
142 PMBus(const std::string& path, const std::string& driverName,
143 size_t instance) :
144 basePath(path),
145 driverName(driverName), instance(instance)
146 {
147 findHwmonDir();
148 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500149
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500150 /**
151 * Reads a file in sysfs that represents a single bit,
152 * therefore doing a PMBus read.
153 *
154 * @param[in] name - path concatenated to
155 * basePath to read
156 * @param[in] type - Path type
157 *
158 * @return bool - false if result was 0, else true
159 */
160 bool readBit(const std::string& name, Type type);
Matt Spinler8f0d9532017-08-21 11:22:37 -0500161
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500162 /**
163 * Reads a file in sysfs that represents a single bit,
164 * where the page number passed in is substituted
165 * into the name in place of the 'P' character in it.
166 *
167 * @param[in] name - path concatenated to
168 * basePath to read
169 * @param[in] page - page number
170 * @param[in] type - Path type
171 *
172 * @return bool - false if result was 0, else true
173 */
174 bool readBitInPage(const std::string& name, size_t page, Type type);
175 /**
176 * Checks if the file for the given name and type exists.
177 *
178 * @param[in] name - path concatenated to basePath to read
179 * @param[in] type - Path type
180 *
181 * @return bool - True if file exists, false if it does not.
182 */
183 bool exists(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500184
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500185 /**
186 * Read byte(s) from file in sysfs.
187 *
188 * @param[in] name - path concatenated to basePath to read
189 * @param[in] type - Path type
190 *
191 * @return uint64_t - Up to 8 bytes of data read from file.
192 */
193 uint64_t read(const std::string& name, Type type);
Brandon Wyman3b7b38b2017-09-25 16:43:45 -0500194
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500195 /**
196 * Read a string from file in sysfs.
197 *
198 * @param[in] name - path concatenated to basePath to read
199 * @param[in] type - Path type
200 *
201 * @return string - The data read from the file.
202 */
203 std::string readString(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500204
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500205 /**
206 * Read data from a binary file in sysfs.
207 *
208 * @param[in] name - path concatenated to basePath to read
209 * @param[in] type - Path type
210 * @param[in] length - length of data to read, in bytes
211 *
212 * @return vector<uint8_t> - The data read from the file.
213 */
214 std::vector<uint8_t> readBinary(const std::string& name, Type type,
215 size_t length);
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600216
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500217 /**
218 * Writes an integer value to the file, therefore doing
219 * a PMBus write.
220 *
221 * @param[in] name - path concatenated to
222 * basePath to write
223 * @param[in] value - the value to write
224 * @param[in] type - Path type
225 */
226 void write(const std::string& name, int value, Type type);
Matt Spinlerfa23e332018-01-18 11:24:58 -0600227
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500228 /**
229 * Returns the sysfs base path of this device
230 */
231 inline const auto& path() const
232 {
233 return basePath;
234 }
Matt Spinler015e3ad2017-08-01 11:20:47 -0500235
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500236 /**
237 * Replaces the 'P' in the string passed in with
238 * the page number passed in.
239 *
240 * For example:
241 * insertPageNum("inP_enable", 42)
242 * returns "in42_enable"
243 *
244 * @param[in] templateName - the name string, with a 'P' in it
245 * @param[in] page - the page number to insert where the P was
246 *
247 * @return string - the new string with the page number in it
248 */
249 static std::string insertPageNum(const std::string& templateName,
250 size_t page);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500251
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500252 /**
253 * Finds the path relative to basePath to the hwmon directory
254 * for the device and stores it in hwmonRelPath.
255 */
256 void findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -0500257
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500258 /**
259 * Returns the path to use for the passed in type.
260 *
261 * @param[in] type - Path type
262 *
263 * @return fs::path - the full path
264 */
265 fs::path getPath(Type type);
Brandon Wymanff5f3392017-08-11 17:43:22 -0500266
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500267 private:
268 /**
269 * Returns the device name
270 *
271 * This is found in the 'name' file in basePath.
272 *
273 * @return string - the device name
274 */
275 std::string getDeviceName();
Matt Spinler57868bc2017-08-03 10:07:41 -0500276
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500277 /**
278 * The sysfs device path
279 */
280 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500281
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500282 /**
283 * The directory name under the basePath hwmon directory
284 */
285 fs::path hwmonDir;
Matt Spinlerba053482018-01-04 14:26:05 -0600286
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500287 /**
288 * The device driver name. Used for finding the device
289 * debug directory. Not required if that directory
290 * isn't used.
291 */
292 std::string driverName;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500293
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500294 /**
295 * The device instance number.
296 *
297 * Used in conjunction with the driver name for finding
298 * the debug directory. Not required if that directory
299 * isn't used.
300 */
301 size_t instance = 0;
Brandon Wymanff5f3392017-08-11 17:43:22 -0500302
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500303 /**
304 * The pmbus debug path with status files
305 */
306 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler015e3ad2017-08-01 11:20:47 -0500307};
308
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500309} // namespace pmbus
Lei YUab093322019-10-09 16:43:22 +0800310} // namespace phosphor