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