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 12*ff5f339cSBrandon Wyman namespace fs = std::experimental::filesystem; 13*ff5f339cSBrandon Wyman 14015e3adeSMatt Spinler /** 1557868bc5SMatt Spinler * If the access should be done in the base 16*ff5f339cSBrandon Wyman * device directory, the hwmon directory, or 17*ff5f339cSBrandon Wyman * the debug director. 1857868bc5SMatt Spinler */ 1957868bc5SMatt Spinler enum class Type 2057868bc5SMatt Spinler { 2157868bc5SMatt Spinler Base, 22*ff5f339cSBrandon Wyman Hwmon, 23*ff5f339cSBrandon 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 { 55*ff5f339cSBrandon 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*ff5f339cSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug 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*ff5f339cSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug 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); 85015e3adeSMatt Spinler 86015e3adeSMatt Spinler /** 87015e3adeSMatt Spinler * Writes an integer value to the file, therefore doing 88015e3adeSMatt Spinler * a PMBus write. 89015e3adeSMatt Spinler * 90015e3adeSMatt Spinler * @param[in] name - path concatenated to 91015e3adeSMatt Spinler * basePath to write 92015e3adeSMatt Spinler * @param[in] value - the value to write 93*ff5f339cSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug 94015e3adeSMatt Spinler */ 9557868bc5SMatt Spinler void write(const std::string& name, int value, Type type); 96015e3adeSMatt Spinler 97015e3adeSMatt Spinler /** 98015e3adeSMatt Spinler * Returns the sysfs base path of this device 99015e3adeSMatt Spinler */ 100015e3adeSMatt Spinler inline const auto& path() const 101015e3adeSMatt Spinler { 102015e3adeSMatt Spinler return basePath; 103015e3adeSMatt Spinler } 104015e3adeSMatt Spinler 105015e3adeSMatt Spinler /** 106015e3adeSMatt Spinler * Replaces the 'P' in the string passed in with 107015e3adeSMatt Spinler * the page number passed in. 108015e3adeSMatt Spinler * 109015e3adeSMatt Spinler * For example: 110015e3adeSMatt Spinler * insertPageNum("inP_enable", 42) 111015e3adeSMatt Spinler * returns "in42_enable" 112015e3adeSMatt Spinler * 113015e3adeSMatt Spinler * @param[in] templateName - the name string, with a 'P' in it 114015e3adeSMatt Spinler * @param[in] page - the page number to insert where the P was 115015e3adeSMatt Spinler * 116015e3adeSMatt Spinler * @return string - the new string with the page number in it 117015e3adeSMatt Spinler */ 118015e3adeSMatt Spinler static std::string insertPageNum(const std::string& templateName, 119015e3adeSMatt Spinler size_t page); 120015e3adeSMatt Spinler 12157868bc5SMatt Spinler /** 12257868bc5SMatt Spinler * Finds the path relative to basePath to the hwmon directory 12357868bc5SMatt Spinler * for the device and stores it in hwmonRelPath. 12457868bc5SMatt Spinler */ 125*ff5f339cSBrandon Wyman void findHwmonDir(); 126*ff5f339cSBrandon Wyman 127*ff5f339cSBrandon Wyman /** 128*ff5f339cSBrandon Wyman * Returns the path to use for the passed in type. 129*ff5f339cSBrandon Wyman * 130*ff5f339cSBrandon Wyman * @param[in] type - one of Base, Hwmon, or Debug 131*ff5f339cSBrandon Wyman * 132*ff5f339cSBrandon Wyman * @return fs::path - the full path to Base, Hwmon, or Debug path 133*ff5f339cSBrandon Wyman */ 134*ff5f339cSBrandon Wyman fs::path getPath(Type type); 13557868bc5SMatt Spinler 136015e3adeSMatt Spinler private: 137015e3adeSMatt Spinler 138015e3adeSMatt Spinler /** 139015e3adeSMatt Spinler * The sysfs device path 140015e3adeSMatt Spinler */ 141*ff5f339cSBrandon Wyman fs::path basePath; 142015e3adeSMatt Spinler 14357868bc5SMatt Spinler /** 144*ff5f339cSBrandon Wyman * The directory name under the basePath hwmon directory 14557868bc5SMatt Spinler */ 146*ff5f339cSBrandon Wyman fs::path hwmonDir; 147*ff5f339cSBrandon Wyman 148*ff5f339cSBrandon Wyman /** 149*ff5f339cSBrandon Wyman * The pmbus debug path with status files 150*ff5f339cSBrandon Wyman */ 151*ff5f339cSBrandon Wyman const fs::path debugPath = "/sys/kernel/debug/pmbus/"; 15257868bc5SMatt Spinler 153015e3adeSMatt Spinler }; 154015e3adeSMatt Spinler 155015e3adeSMatt Spinler } 156015e3adeSMatt Spinler } 157