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