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