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 | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 17 | // The file name Linux uses to capture the VIN_UV_FAULT bit from the STATUS_WORD |
| 18 | constexpr auto VIN_UV_FAULT = "in1_alarm"; |
Matt Spinler | e7e432b | 2017-08-21 15:01:40 -0500 | [diff] [blame] | 19 | |
| 20 | // Uses Page substitution |
| 21 | constexpr auto STATUS_VOUT = "statusP_vout"; |
| 22 | |
| 23 | namespace status_word |
| 24 | { |
| 25 | constexpr auto VOUT_FAULT = 0x8000; |
| 26 | } |
| 27 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 28 | /** |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 29 | * If the access should be done in the base |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 30 | * device directory, the hwmon directory, the |
| 31 | * pmbus debug directory, or the device debug |
| 32 | * directory. |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 33 | */ |
| 34 | enum class Type |
| 35 | { |
| 36 | Base, |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 37 | Hwmon, |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 38 | Debug, |
| 39 | DeviceDebug |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /** |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 43 | * @class PMBus |
| 44 | * |
| 45 | * This class is an interface to communicating with PMBus devices |
| 46 | * by reading and writing sysfs files. |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 47 | * |
| 48 | * Based on the Type parameter, the accesses can either be done |
| 49 | * in the base device directory (the one passed into the constructor), |
| 50 | * or in the hwmon directory for the device. |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 51 | */ |
| 52 | class PMBus |
| 53 | { |
| 54 | public: |
| 55 | |
| 56 | PMBus() = delete; |
| 57 | ~PMBus() = default; |
| 58 | PMBus(const PMBus&) = default; |
| 59 | PMBus& operator=(const PMBus&) = default; |
| 60 | PMBus(PMBus&&) = default; |
| 61 | PMBus& operator=(PMBus&&) = default; |
| 62 | |
| 63 | /** |
| 64 | * Constructor |
| 65 | * |
| 66 | * @param[in] path - path to the sysfs directory |
| 67 | */ |
| 68 | PMBus(const std::string& path) : |
| 69 | basePath(path) |
| 70 | { |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 71 | findHwmonDir(); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 75 | * Constructor |
| 76 | * |
| 77 | * This version is required when DeviceDebug |
| 78 | * access will be used. |
| 79 | * |
| 80 | * @param[in] path - path to the sysfs directory |
| 81 | * @param[in] driverName - the device driver name |
| 82 | * @param[in] instance - chip instance number |
| 83 | */ |
| 84 | PMBus(const std::string& path, |
| 85 | const std::string& driverName, |
| 86 | size_t instance) : |
| 87 | basePath(path), |
| 88 | driverName(driverName), |
| 89 | instance(instance) |
| 90 | { |
| 91 | findHwmonDir(); |
| 92 | } |
| 93 | |
| 94 | /** |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 95 | * Reads a file in sysfs that represents a single bit, |
| 96 | * therefore doing a PMBus read. |
| 97 | * |
| 98 | * @param[in] name - path concatenated to |
| 99 | * basePath to read |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 100 | * @param[in] type - Path type |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 101 | * |
| 102 | * @return bool - false if result was 0, else true |
| 103 | */ |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 104 | bool readBit(const std::string& name, Type type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Reads a file in sysfs that represents a single bit, |
| 108 | * where the page number passed in is substituted |
| 109 | * into the name in place of the 'P' character in it. |
| 110 | * |
| 111 | * @param[in] name - path concatenated to |
| 112 | * basePath to read |
| 113 | * @param[in] page - page number |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 114 | * @param[in] type - Path type |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 115 | * |
| 116 | * @return bool - false if result was 0, else true |
| 117 | */ |
| 118 | bool readBitInPage(const std::string& name, |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 119 | size_t page, |
| 120 | Type type); |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 121 | /** |
| 122 | * Read byte(s) from file in sysfs. |
| 123 | * |
| 124 | * @param[in] name - path concatenated to basePath to read |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 125 | * @param[in] type - Path type |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 126 | * |
| 127 | * @return uint64_t - Up to 8 bytes of data read from file. |
| 128 | */ |
| 129 | uint64_t read(const std::string& name, Type type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Writes an integer value to the file, therefore doing |
| 133 | * a PMBus write. |
| 134 | * |
| 135 | * @param[in] name - path concatenated to |
| 136 | * basePath to write |
| 137 | * @param[in] value - the value to write |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 138 | * @param[in] type - Path type |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 139 | */ |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 140 | void write(const std::string& name, int value, Type type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Returns the sysfs base path of this device |
| 144 | */ |
| 145 | inline const auto& path() const |
| 146 | { |
| 147 | return basePath; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Replaces the 'P' in the string passed in with |
| 152 | * the page number passed in. |
| 153 | * |
| 154 | * For example: |
| 155 | * insertPageNum("inP_enable", 42) |
| 156 | * returns "in42_enable" |
| 157 | * |
| 158 | * @param[in] templateName - the name string, with a 'P' in it |
| 159 | * @param[in] page - the page number to insert where the P was |
| 160 | * |
| 161 | * @return string - the new string with the page number in it |
| 162 | */ |
| 163 | static std::string insertPageNum(const std::string& templateName, |
| 164 | size_t page); |
| 165 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 166 | /** |
| 167 | * Finds the path relative to basePath to the hwmon directory |
| 168 | * for the device and stores it in hwmonRelPath. |
| 169 | */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 170 | void findHwmonDir(); |
| 171 | |
| 172 | /** |
| 173 | * Returns the path to use for the passed in type. |
| 174 | * |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 175 | * @param[in] type - Path type |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 176 | * |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 177 | * @return fs::path - the full path |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 178 | */ |
| 179 | fs::path getPath(Type type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 180 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 181 | private: |
| 182 | |
| 183 | /** |
| 184 | * The sysfs device path |
| 185 | */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 186 | fs::path basePath; |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 187 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 188 | /** |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 189 | * The directory name under the basePath hwmon directory |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 190 | */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 191 | fs::path hwmonDir; |
| 192 | |
| 193 | /** |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 194 | * The device driver name. Used for finding the device |
| 195 | * debug directory. Not required if that directory |
| 196 | * isn't used. |
| 197 | */ |
| 198 | std::string driverName; |
| 199 | |
| 200 | /** |
| 201 | * The device instance number. |
| 202 | * |
| 203 | * Used in conjuction with the driver name for finding |
| 204 | * the debug directory. Not required if that directory |
| 205 | * isn't used. |
| 206 | */ |
| 207 | size_t instance = 0; |
| 208 | |
| 209 | /** |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 210 | * The pmbus debug path with status files |
| 211 | */ |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 212 | const fs::path debugPath = "/sys/kernel/debug/"; |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 213 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | } |
| 217 | } |