blob: 9ab57c0f331ad67ed20f83f980f4a42e5eed46fb [file] [log] [blame]
Matt Spinler015e3ad2017-08-01 11:20:47 -05001#pragma once
2
3#include <experimental/filesystem>
4#include <string>
5#include <vector>
6
7namespace witherspoon
8{
9namespace pmbus
10{
11
Brandon Wymanff5f3392017-08-11 17:43:22 -050012namespace fs = std::experimental::filesystem;
13
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;
30}
31
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
82}
83
84namespace status_temperature
85{
86// Overtemperature Fault
87constexpr auto OT_FAULT = 0x80;
Matt Spinlere7e432b2017-08-21 15:01:40 -050088}
89
Matt Spinler015e3ad2017-08-01 11:20:47 -050090/**
Matt Spinler57868bc2017-08-03 10:07:41 -050091 * If the access should be done in the base
Matt Spinler8f0d9532017-08-21 11:22:37 -050092 * device directory, the hwmon directory, the
93 * pmbus debug directory, or the device debug
94 * directory.
Matt Spinler57868bc2017-08-03 10:07:41 -050095 */
96enum class Type
97{
98 Base,
Brandon Wymanff5f3392017-08-11 17:43:22 -050099 Hwmon,
Matt Spinler8f0d9532017-08-21 11:22:37 -0500100 Debug,
101 DeviceDebug
Matt Spinler57868bc2017-08-03 10:07:41 -0500102};
103
104/**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500105 * @class PMBus
106 *
107 * This class is an interface to communicating with PMBus devices
108 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -0500109 *
110 * Based on the Type parameter, the accesses can either be done
111 * in the base device directory (the one passed into the constructor),
112 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -0500113 */
114class PMBus
115{
116 public:
117
118 PMBus() = delete;
119 ~PMBus() = default;
120 PMBus(const PMBus&) = default;
121 PMBus& operator=(const PMBus&) = default;
122 PMBus(PMBus&&) = default;
123 PMBus& operator=(PMBus&&) = default;
124
125 /**
126 * Constructor
127 *
128 * @param[in] path - path to the sysfs directory
129 */
130 PMBus(const std::string& path) :
131 basePath(path)
132 {
Brandon Wymanff5f3392017-08-11 17:43:22 -0500133 findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -0500134 }
135
136 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500137 * Constructor
138 *
139 * This version is required when DeviceDebug
140 * access will be used.
141 *
142 * @param[in] path - path to the sysfs directory
143 * @param[in] driverName - the device driver name
144 * @param[in] instance - chip instance number
145 */
146 PMBus(const std::string& path,
147 const std::string& driverName,
148 size_t instance) :
149 basePath(path),
150 driverName(driverName),
151 instance(instance)
152 {
153 findHwmonDir();
154 }
155
156 /**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500157 * Reads a file in sysfs that represents a single bit,
158 * therefore doing a PMBus read.
159 *
160 * @param[in] name - path concatenated to
161 * basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500162 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500163 *
164 * @return bool - false if result was 0, else true
165 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500166 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500167
168 /**
169 * Reads a file in sysfs that represents a single bit,
170 * where the page number passed in is substituted
171 * into the name in place of the 'P' character in it.
172 *
173 * @param[in] name - path concatenated to
174 * basePath to read
175 * @param[in] page - page number
Matt Spinler8f0d9532017-08-21 11:22:37 -0500176 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500177 *
178 * @return bool - false if result was 0, else true
179 */
180 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -0500181 size_t page,
182 Type type);
Brandon Wymanf855e822017-08-08 18:04:47 -0500183 /**
Brandon Wyman3b7b38b2017-09-25 16:43:45 -0500184 * Checks if the file for the given name and type exists.
185 *
186 * @param[in] name - path concatenated to basePath to read
187 * @param[in] type - Path type
188 *
189 * @return bool - True if file exists, false if it does not.
190 */
191 bool exists(const std::string& name, Type type);
192
193 /**
Brandon Wymanf855e822017-08-08 18:04:47 -0500194 * Read byte(s) from file in sysfs.
195 *
196 * @param[in] name - path concatenated to basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500197 * @param[in] type - Path type
Brandon Wymanf855e822017-08-08 18:04:47 -0500198 *
199 * @return uint64_t - Up to 8 bytes of data read from file.
200 */
201 uint64_t read(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500202
203 /**
204 * Writes an integer value to the file, therefore doing
205 * a PMBus write.
206 *
207 * @param[in] name - path concatenated to
208 * basePath to write
209 * @param[in] value - the value to write
Matt Spinler8f0d9532017-08-21 11:22:37 -0500210 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500211 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500212 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500213
214 /**
215 * Returns the sysfs base path of this device
216 */
217 inline const auto& path() const
218 {
219 return basePath;
220 }
221
222 /**
223 * Replaces the 'P' in the string passed in with
224 * the page number passed in.
225 *
226 * For example:
227 * insertPageNum("inP_enable", 42)
228 * returns "in42_enable"
229 *
230 * @param[in] templateName - the name string, with a 'P' in it
231 * @param[in] page - the page number to insert where the P was
232 *
233 * @return string - the new string with the page number in it
234 */
235 static std::string insertPageNum(const std::string& templateName,
236 size_t page);
237
Matt Spinler57868bc2017-08-03 10:07:41 -0500238 /**
239 * Finds the path relative to basePath to the hwmon directory
240 * for the device and stores it in hwmonRelPath.
241 */
Brandon Wyman764c7972017-08-22 17:05:36 -0500242 void findHwmonDir();
Brandon Wymanff5f3392017-08-11 17:43:22 -0500243
244 /**
245 * Returns the path to use for the passed in type.
246 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500247 * @param[in] type - Path type
Brandon Wymanff5f3392017-08-11 17:43:22 -0500248 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500249 * @return fs::path - the full path
Brandon Wymanff5f3392017-08-11 17:43:22 -0500250 */
Brandon Wyman764c7972017-08-22 17:05:36 -0500251 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500252
Matt Spinler015e3ad2017-08-01 11:20:47 -0500253 private:
254
255 /**
Matt Spinlerba053482018-01-04 14:26:05 -0600256 * Returns the device name
257 *
258 * This is found in the 'name' file in basePath.
259 *
260 * @return string - the device name
261 */
262 std::string getDeviceName();
263
264 /**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500265 * The sysfs device path
266 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500267 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500268
Matt Spinler57868bc2017-08-03 10:07:41 -0500269 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500270 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500271 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500272 fs::path hwmonDir;
273
274 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500275 * The device driver name. Used for finding the device
276 * debug directory. Not required if that directory
277 * isn't used.
278 */
279 std::string driverName;
280
281 /**
282 * The device instance number.
283 *
284 * Used in conjuction with the driver name for finding
285 * the debug directory. Not required if that directory
286 * isn't used.
287 */
288 size_t instance = 0;
289
290 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500291 * The pmbus debug path with status files
292 */
Matt Spinler8f0d9532017-08-21 11:22:37 -0500293 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500294
Matt Spinler015e3ad2017-08-01 11:20:47 -0500295};
296
297}
298}