1 #pragma once 2 3 #include <experimental/filesystem> 4 #include <string> 5 #include <vector> 6 7 namespace witherspoon 8 { 9 namespace pmbus 10 { 11 12 namespace fs = std::experimental::filesystem; 13 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 24 /** 25 * If the access should be done in the base 26 * device directory, the hwmon directory, the 27 * pmbus debug directory, or the device debug 28 * directory. 29 */ 30 enum class Type 31 { 32 Base, 33 Hwmon, 34 Debug, 35 DeviceDebug 36 }; 37 38 /** 39 * @class PMBus 40 * 41 * This class is an interface to communicating with PMBus devices 42 * by reading and writing sysfs files. 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. 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 { 67 findHwmonDir(); 68 } 69 70 /** 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 /** 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 96 * @param[in] type - Path type 97 * 98 * @return bool - false if result was 0, else true 99 */ 100 bool readBit(const std::string& name, Type type); 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 110 * @param[in] type - Path type 111 * 112 * @return bool - false if result was 0, else true 113 */ 114 bool readBitInPage(const std::string& name, 115 size_t page, 116 Type type); 117 /** 118 * Read byte(s) from file in sysfs. 119 * 120 * @param[in] name - path concatenated to basePath to read 121 * @param[in] type - Path type 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); 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 134 * @param[in] type - Path type 135 */ 136 void write(const std::string& name, int value, Type type); 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 162 /** 163 * Finds the path relative to basePath to the hwmon directory 164 * for the device and stores it in hwmonRelPath. 165 */ 166 void findHwmonDir(); 167 168 /** 169 * Returns the path to use for the passed in type. 170 * 171 * @param[in] type - Path type 172 * 173 * @return fs::path - the full path 174 */ 175 fs::path getPath(Type type); 176 177 private: 178 179 /** 180 * The sysfs device path 181 */ 182 fs::path basePath; 183 184 /** 185 * The directory name under the basePath hwmon directory 186 */ 187 fs::path hwmonDir; 188 189 /** 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 /** 206 * The pmbus debug path with status files 207 */ 208 const fs::path debugPath = "/sys/kernel/debug/"; 209 210 }; 211 212 } 213 } 214