Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <experimental/filesystem> |
| 4 | #include <string> |
| 5 | #include <vector> |
| 6 | |
| 7 | namespace witherspoon |
| 8 | { |
| 9 | namespace pmbus |
| 10 | { |
| 11 | |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 12 | namespace fs = std::experimental::filesystem; |
| 13 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 14 | // The file name Linux uses to capture the STATUS_WORD from pmbus. |
Matt Spinler | e7e432b | 2017-08-21 15:01:40 -0500 | [diff] [blame] | 15 | constexpr auto STATUS_WORD = "status0"; |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 16 | |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 17 | // The file name Linux uses to capture the STATUS_INPUT from pmbus. |
| 18 | constexpr auto STATUS_INPUT = "status0_input"; |
Matt Spinler | e7e432b | 2017-08-21 15:01:40 -0500 | [diff] [blame] | 19 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 20 | // Voltage out status. |
| 21 | // Overvoltage fault or warning, Undervoltage fault or warning, maximum or |
| 22 | // minimum warning, .... |
Matt Spinler | e7e432b | 2017-08-21 15:01:40 -0500 | [diff] [blame] | 23 | // Uses Page substitution |
| 24 | constexpr auto STATUS_VOUT = "statusP_vout"; |
| 25 | |
Matt Spinler | de16d05 | 2017-12-13 13:22:14 -0600 | [diff] [blame] | 26 | namespace status_vout |
| 27 | { |
| 28 | // Mask of bits that are only warnings |
| 29 | constexpr auto WARNING_MASK = 0x6A; |
| 30 | } |
| 31 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 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 | |
Brandon Wyman | 12661f1 | 2017-08-31 15:28:21 -0500 | [diff] [blame] | 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 | |
Matt Spinler | e7e432b | 2017-08-21 15:01:40 -0500 | [diff] [blame] | 45 | namespace status_word |
| 46 | { |
| 47 | constexpr auto VOUT_FAULT = 0x8000; |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 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 | |
Brandon Wyman | 12661f1 | 2017-08-31 15:28:21 -0500 | [diff] [blame] | 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 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 62 | // The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD. |
| 63 | constexpr auto UNIT_IS_OFF = 0x0040; |
| 64 | |
Brandon Wyman | ab05c07 | 2017-08-30 18:26:41 -0500 | [diff] [blame] | 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 | |
Brandon Wyman | b165c25 | 2017-08-25 18:59:54 -0500 | [diff] [blame] | 69 | // The bit mask representing that an output overcurrent fault has occurred. |
| 70 | constexpr auto IOUT_OC_FAULT = 0x0010; |
| 71 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 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 | |
Brandon Wyman | 875b363 | 2017-09-13 18:46:03 -0500 | [diff] [blame] | 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 | } |
| 83 | |
| 84 | namespace status_temperature |
| 85 | { |
| 86 | // Overtemperature Fault |
| 87 | constexpr auto OT_FAULT = 0x80; |
Matt Spinler | e7e432b | 2017-08-21 15:01:40 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 90 | /** |
Matt Spinler | 4dc4678 | 2018-01-04 14:29:16 -0600 | [diff] [blame] | 91 | * Where the access should be done |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 92 | */ |
| 93 | enum class Type |
| 94 | { |
Matt Spinler | 4dc4678 | 2018-01-04 14:29:16 -0600 | [diff] [blame] | 95 | 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 Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /** |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 103 | * @class PMBus |
| 104 | * |
| 105 | * This class is an interface to communicating with PMBus devices |
| 106 | * by reading and writing sysfs files. |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 107 | * |
| 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 Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 111 | */ |
| 112 | class PMBus |
| 113 | { |
| 114 | public: |
| 115 | |
| 116 | PMBus() = delete; |
| 117 | ~PMBus() = default; |
| 118 | PMBus(const PMBus&) = default; |
| 119 | PMBus& operator=(const PMBus&) = default; |
| 120 | PMBus(PMBus&&) = default; |
| 121 | PMBus& operator=(PMBus&&) = default; |
| 122 | |
| 123 | /** |
| 124 | * Constructor |
| 125 | * |
| 126 | * @param[in] path - path to the sysfs directory |
| 127 | */ |
| 128 | PMBus(const std::string& path) : |
| 129 | basePath(path) |
| 130 | { |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 131 | findHwmonDir(); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 135 | * Constructor |
| 136 | * |
| 137 | * This version is required when DeviceDebug |
| 138 | * access will be used. |
| 139 | * |
| 140 | * @param[in] path - path to the sysfs directory |
| 141 | * @param[in] driverName - the device driver name |
| 142 | * @param[in] instance - chip instance number |
| 143 | */ |
| 144 | PMBus(const std::string& path, |
| 145 | const std::string& driverName, |
| 146 | size_t instance) : |
| 147 | basePath(path), |
| 148 | driverName(driverName), |
| 149 | instance(instance) |
| 150 | { |
| 151 | findHwmonDir(); |
| 152 | } |
| 153 | |
| 154 | /** |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 155 | * Reads a file in sysfs that represents a single bit, |
| 156 | * therefore doing a PMBus read. |
| 157 | * |
| 158 | * @param[in] name - path concatenated to |
| 159 | * basePath to read |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 160 | * @param[in] type - Path type |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 161 | * |
| 162 | * @return bool - false if result was 0, else true |
| 163 | */ |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 164 | bool readBit(const std::string& name, Type type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Reads a file in sysfs that represents a single bit, |
| 168 | * where the page number passed in is substituted |
| 169 | * into the name in place of the 'P' character in it. |
| 170 | * |
| 171 | * @param[in] name - path concatenated to |
| 172 | * basePath to read |
| 173 | * @param[in] page - page number |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 174 | * @param[in] type - Path type |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 175 | * |
| 176 | * @return bool - false if result was 0, else true |
| 177 | */ |
| 178 | bool readBitInPage(const std::string& name, |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 179 | size_t page, |
| 180 | Type type); |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 181 | /** |
Brandon Wyman | 3b7b38b | 2017-09-25 16:43:45 -0500 | [diff] [blame] | 182 | * Checks if the file for the given name and type exists. |
| 183 | * |
| 184 | * @param[in] name - path concatenated to basePath to read |
| 185 | * @param[in] type - Path type |
| 186 | * |
| 187 | * @return bool - True if file exists, false if it does not. |
| 188 | */ |
| 189 | bool exists(const std::string& name, Type type); |
| 190 | |
| 191 | /** |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 192 | * Read byte(s) from file in sysfs. |
| 193 | * |
| 194 | * @param[in] name - path concatenated to basePath to read |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 195 | * @param[in] type - Path type |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 196 | * |
| 197 | * @return uint64_t - Up to 8 bytes of data read from file. |
| 198 | */ |
| 199 | uint64_t read(const std::string& name, Type type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 200 | |
| 201 | /** |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 202 | * Read a string from file in sysfs. |
| 203 | * |
| 204 | * @param[in] name - path concatenated to basePath to read |
| 205 | * @param[in] type - Path type |
| 206 | * |
| 207 | * @return string - The data read from the file. |
| 208 | */ |
| 209 | std::string readString(const std::string& name, Type type); |
| 210 | |
| 211 | /** |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 212 | * Read data from a binary file in sysfs. |
| 213 | * |
| 214 | * @param[in] name - path concatenated to basePath to read |
| 215 | * @param[in] type - Path type |
| 216 | * @param[in] length - length of data to read, in bytes |
| 217 | * |
| 218 | * @return vector<uint8_t> - The data read from the file. |
| 219 | */ |
| 220 | std::vector<uint8_t> readBinary(const std::string& name, |
| 221 | Type type, |
| 222 | size_t length); |
| 223 | |
| 224 | /** |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 225 | * Writes an integer value to the file, therefore doing |
| 226 | * a PMBus write. |
| 227 | * |
| 228 | * @param[in] name - path concatenated to |
| 229 | * basePath to write |
| 230 | * @param[in] value - the value to write |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 231 | * @param[in] type - Path type |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 232 | */ |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 233 | void write(const std::string& name, int value, Type type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * Returns the sysfs base path of this device |
| 237 | */ |
| 238 | inline const auto& path() const |
| 239 | { |
| 240 | return basePath; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Replaces the 'P' in the string passed in with |
| 245 | * the page number passed in. |
| 246 | * |
| 247 | * For example: |
| 248 | * insertPageNum("inP_enable", 42) |
| 249 | * returns "in42_enable" |
| 250 | * |
| 251 | * @param[in] templateName - the name string, with a 'P' in it |
| 252 | * @param[in] page - the page number to insert where the P was |
| 253 | * |
| 254 | * @return string - the new string with the page number in it |
| 255 | */ |
| 256 | static std::string insertPageNum(const std::string& templateName, |
| 257 | size_t page); |
| 258 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 259 | /** |
| 260 | * Finds the path relative to basePath to the hwmon directory |
| 261 | * for the device and stores it in hwmonRelPath. |
| 262 | */ |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 263 | void findHwmonDir(); |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Returns the path to use for the passed in type. |
| 267 | * |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 268 | * @param[in] type - Path type |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 269 | * |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 270 | * @return fs::path - the full path |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 271 | */ |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 272 | fs::path getPath(Type type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 273 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 274 | private: |
| 275 | |
| 276 | /** |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 277 | * Returns the device name |
| 278 | * |
| 279 | * This is found in the 'name' file in basePath. |
| 280 | * |
| 281 | * @return string - the device name |
| 282 | */ |
| 283 | std::string getDeviceName(); |
| 284 | |
| 285 | /** |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 286 | * The sysfs device path |
| 287 | */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 288 | fs::path basePath; |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 289 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 290 | /** |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 291 | * The directory name under the basePath hwmon directory |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 292 | */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 293 | fs::path hwmonDir; |
| 294 | |
| 295 | /** |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 296 | * The device driver name. Used for finding the device |
| 297 | * debug directory. Not required if that directory |
| 298 | * isn't used. |
| 299 | */ |
| 300 | std::string driverName; |
| 301 | |
| 302 | /** |
| 303 | * The device instance number. |
| 304 | * |
Gunnar Mills | cab4834 | 2018-06-13 15:57:13 -0500 | [diff] [blame^] | 305 | * Used in conjunction with the driver name for finding |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 306 | * the debug directory. Not required if that directory |
| 307 | * isn't used. |
| 308 | */ |
| 309 | size_t instance = 0; |
| 310 | |
| 311 | /** |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 312 | * The pmbus debug path with status files |
| 313 | */ |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 314 | const fs::path debugPath = "/sys/kernel/debug/"; |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 315 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | } |
| 319 | } |