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