1015e3adeSMatt Spinler #pragma once 2015e3adeSMatt Spinler 3015e3adeSMatt Spinler #include <experimental/filesystem> 4015e3adeSMatt Spinler #include <string> 5015e3adeSMatt Spinler #include <vector> 6015e3adeSMatt Spinler 7015e3adeSMatt Spinler namespace witherspoon 8015e3adeSMatt Spinler { 9015e3adeSMatt Spinler namespace pmbus 10015e3adeSMatt Spinler { 11015e3adeSMatt Spinler 12ff5f339cSBrandon Wyman namespace fs = std::experimental::filesystem; 13ff5f339cSBrandon Wyman 14015e3adeSMatt Spinler /** 1557868bc5SMatt Spinler * If the access should be done in the base 16ff5f339cSBrandon Wyman * device directory, the hwmon directory, or 17ff5f339cSBrandon Wyman * the debug director. 1857868bc5SMatt Spinler */ 1957868bc5SMatt Spinler enum class Type 2057868bc5SMatt Spinler { 2157868bc5SMatt Spinler Base, 22ff5f339cSBrandon Wyman Hwmon, 23ff5f339cSBrandon Wyman Debug 2457868bc5SMatt Spinler }; 2557868bc5SMatt Spinler 2657868bc5SMatt Spinler /** 27015e3adeSMatt Spinler * @class PMBus 28015e3adeSMatt Spinler * 29015e3adeSMatt Spinler * This class is an interface to communicating with PMBus devices 30015e3adeSMatt Spinler * by reading and writing sysfs files. 3157868bc5SMatt Spinler * 3257868bc5SMatt Spinler * Based on the Type parameter, the accesses can either be done 3357868bc5SMatt Spinler * in the base device directory (the one passed into the constructor), 3457868bc5SMatt Spinler * or in the hwmon directory for the device. 35015e3adeSMatt Spinler */ 36015e3adeSMatt Spinler class PMBus 37015e3adeSMatt Spinler { 38015e3adeSMatt Spinler public: 39015e3adeSMatt Spinler 40015e3adeSMatt Spinler PMBus() = delete; 41015e3adeSMatt Spinler ~PMBus() = default; 42015e3adeSMatt Spinler PMBus(const PMBus&) = default; 43015e3adeSMatt Spinler PMBus& operator=(const PMBus&) = default; 44015e3adeSMatt Spinler PMBus(PMBus&&) = default; 45015e3adeSMatt Spinler PMBus& operator=(PMBus&&) = default; 46015e3adeSMatt Spinler 47015e3adeSMatt Spinler /** 48015e3adeSMatt Spinler * Constructor 49015e3adeSMatt Spinler * 50015e3adeSMatt Spinler * @param[in] path - path to the sysfs directory 51015e3adeSMatt Spinler */ 52015e3adeSMatt Spinler PMBus(const std::string& path) : 53015e3adeSMatt Spinler basePath(path) 54015e3adeSMatt Spinler { 55ff5f339cSBrandon Wyman findHwmonDir(); 56015e3adeSMatt Spinler } 57015e3adeSMatt Spinler 58015e3adeSMatt Spinler /** 59015e3adeSMatt Spinler * Reads a file in sysfs that represents a single bit, 60015e3adeSMatt Spinler * therefore doing a PMBus read. 61015e3adeSMatt Spinler * 62015e3adeSMatt Spinler * @param[in] name - path concatenated to 63015e3adeSMatt Spinler * basePath to read 64*f855e82aSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug (path type) 65015e3adeSMatt Spinler * 66015e3adeSMatt Spinler * @return bool - false if result was 0, else true 67015e3adeSMatt Spinler */ 6857868bc5SMatt Spinler bool readBit(const std::string& name, Type type); 69015e3adeSMatt Spinler 70015e3adeSMatt Spinler /** 71015e3adeSMatt Spinler * Reads a file in sysfs that represents a single bit, 72015e3adeSMatt Spinler * where the page number passed in is substituted 73015e3adeSMatt Spinler * into the name in place of the 'P' character in it. 74015e3adeSMatt Spinler * 75015e3adeSMatt Spinler * @param[in] name - path concatenated to 76015e3adeSMatt Spinler * basePath to read 77015e3adeSMatt Spinler * @param[in] page - page number 78*f855e82aSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug (path type) 79015e3adeSMatt Spinler * 80015e3adeSMatt Spinler * @return bool - false if result was 0, else true 81015e3adeSMatt Spinler */ 82015e3adeSMatt Spinler bool readBitInPage(const std::string& name, 8357868bc5SMatt Spinler size_t page, 8457868bc5SMatt Spinler Type type); 85*f855e82aSBrandon Wyman /** 86*f855e82aSBrandon Wyman * Read byte(s) from file in sysfs. 87*f855e82aSBrandon Wyman * 88*f855e82aSBrandon Wyman * @param[in] name - path concatenated to basePath to read 89*f855e82aSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug (path type) 90*f855e82aSBrandon Wyman * 91*f855e82aSBrandon Wyman * @return uint64_t - Up to 8 bytes of data read from file. 92*f855e82aSBrandon Wyman */ 93*f855e82aSBrandon Wyman uint64_t read(const std::string& name, Type type); 94015e3adeSMatt Spinler 95015e3adeSMatt Spinler /** 96015e3adeSMatt Spinler * Writes an integer value to the file, therefore doing 97015e3adeSMatt Spinler * a PMBus write. 98015e3adeSMatt Spinler * 99015e3adeSMatt Spinler * @param[in] name - path concatenated to 100015e3adeSMatt Spinler * basePath to write 101015e3adeSMatt Spinler * @param[in] value - the value to write 102*f855e82aSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug (path type) 103015e3adeSMatt Spinler */ 10457868bc5SMatt Spinler void write(const std::string& name, int value, Type type); 105015e3adeSMatt Spinler 106015e3adeSMatt Spinler /** 107015e3adeSMatt Spinler * Returns the sysfs base path of this device 108015e3adeSMatt Spinler */ 109015e3adeSMatt Spinler inline const auto& path() const 110015e3adeSMatt Spinler { 111015e3adeSMatt Spinler return basePath; 112015e3adeSMatt Spinler } 113015e3adeSMatt Spinler 114015e3adeSMatt Spinler /** 115015e3adeSMatt Spinler * Replaces the 'P' in the string passed in with 116015e3adeSMatt Spinler * the page number passed in. 117015e3adeSMatt Spinler * 118015e3adeSMatt Spinler * For example: 119015e3adeSMatt Spinler * insertPageNum("inP_enable", 42) 120015e3adeSMatt Spinler * returns "in42_enable" 121015e3adeSMatt Spinler * 122015e3adeSMatt Spinler * @param[in] templateName - the name string, with a 'P' in it 123015e3adeSMatt Spinler * @param[in] page - the page number to insert where the P was 124015e3adeSMatt Spinler * 125015e3adeSMatt Spinler * @return string - the new string with the page number in it 126015e3adeSMatt Spinler */ 127015e3adeSMatt Spinler static std::string insertPageNum(const std::string& templateName, 128015e3adeSMatt Spinler size_t page); 129015e3adeSMatt Spinler 13057868bc5SMatt Spinler /** 13157868bc5SMatt Spinler * Finds the path relative to basePath to the hwmon directory 13257868bc5SMatt Spinler * for the device and stores it in hwmonRelPath. 13357868bc5SMatt Spinler */ 134ff5f339cSBrandon Wyman void findHwmonDir(); 135ff5f339cSBrandon Wyman 136ff5f339cSBrandon Wyman /** 137ff5f339cSBrandon Wyman * Returns the path to use for the passed in type. 138ff5f339cSBrandon Wyman * 139*f855e82aSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug (path type) 140ff5f339cSBrandon Wyman * 141ff5f339cSBrandon Wyman * @return fs::path - the full path to Base, Hwmon, or Debug path 142ff5f339cSBrandon Wyman */ 143ff5f339cSBrandon Wyman fs::path getPath(Type type); 14457868bc5SMatt Spinler 145015e3adeSMatt Spinler private: 146015e3adeSMatt Spinler 147015e3adeSMatt Spinler /** 148015e3adeSMatt Spinler * The sysfs device path 149015e3adeSMatt Spinler */ 150ff5f339cSBrandon Wyman fs::path basePath; 151015e3adeSMatt Spinler 15257868bc5SMatt Spinler /** 153ff5f339cSBrandon Wyman * The directory name under the basePath hwmon directory 15457868bc5SMatt Spinler */ 155ff5f339cSBrandon Wyman fs::path hwmonDir; 156ff5f339cSBrandon Wyman 157ff5f339cSBrandon Wyman /** 158ff5f339cSBrandon Wyman * The pmbus debug path with status files 159ff5f339cSBrandon Wyman */ 160ff5f339cSBrandon Wyman const fs::path debugPath = "/sys/kernel/debug/pmbus/"; 16157868bc5SMatt Spinler 162015e3adeSMatt Spinler }; 163015e3adeSMatt Spinler 164015e3adeSMatt Spinler } 165015e3adeSMatt Spinler } 166